1 /* $Id: common.c,v 1.15 2005/03/17 19:22:47 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
14 /**********************************************************************
16 *********************************************************************/
17 hb_rate_t hb_video_rates[] =
18 { { "5", 5400000 }, { "10", 2700000 }, { "12", 2250000 },
19 { "15", 1800000 }, { "23.976", 1126125 }, { "24", 1125000 },
20 { "25", 1080000 }, { "29.97", 900900 } };
21 int hb_video_rates_count = sizeof( hb_video_rates ) /
24 hb_rate_t hb_audio_rates[] =
25 { { "22.05", 22050 }, { "24", 24000 }, { "32", 32000 },
26 { "44.1", 44100 }, { "48", 48000 } };
27 int hb_audio_rates_count = sizeof( hb_audio_rates ) /
29 int hb_audio_rates_default = 3; /* 44100 Hz */
31 hb_rate_t hb_audio_bitrates[] =
32 { { "32", 32 }, { "40", 40 }, { "48", 48 }, { "56", 56 },
33 { "64", 64 }, { "80", 80 }, { "96", 96 }, { "112", 112 },
34 { "128", 128 }, { "160", 160 }, { "192", 192 }, { "224", 224 },
35 { "256", 256 }, { "320", 320 }, { "384", 384 }, { "448", 448 },
37 int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) /
39 int hb_audio_bitrates_default = 8; /* 128 kbps */
41 static hb_error_handler_t *error_handler = NULL;
43 hb_mixdown_t hb_audio_mixdowns[] =
44 { { "Mono", "HB_AMIXDOWN_MONO", "mono", HB_AMIXDOWN_MONO },
45 { "Stereo", "HB_AMIXDOWN_STEREO", "stereo", HB_AMIXDOWN_STEREO },
46 { "Dolby Surround", "HB_AMIXDOWN_DOLBY", "dpl1", HB_AMIXDOWN_DOLBY },
47 { "Dolby Pro Logic II", "HB_AMIXDOWN_DOLBYPLII", "dpl2", HB_AMIXDOWN_DOLBYPLII },
48 { "6-channel discrete", "HB_AMIXDOWN_6CH", "6ch", HB_AMIXDOWN_6CH }
50 int hb_audio_mixdowns_count = sizeof( hb_audio_mixdowns ) /
51 sizeof( hb_mixdown_t );
53 int hb_mixdown_get_mixdown_from_short_name( const char * short_name )
56 for (i = 0; i < hb_audio_mixdowns_count; i++)
58 if (strcmp(hb_audio_mixdowns[i].short_name, short_name) == 0)
60 return hb_audio_mixdowns[i].amixdown;
66 const char * hb_mixdown_get_short_name_from_mixdown( int amixdown )
69 for (i = 0; i < hb_audio_mixdowns_count; i++)
71 if (hb_audio_mixdowns[i].amixdown == amixdown)
73 return hb_audio_mixdowns[i].short_name;
79 /**********************************************************************
81 **********************************************************************
82 * Given a numerator (num) and a denominator (den), reduce them to an
83 * equivalent fraction and store the result in x and y.
84 *********************************************************************/
85 void hb_reduce( int *x, int *y, int num, int den )
87 // find the greatest common divisor of num & den by Euclid's algorithm
96 // at this point n is the gcd. if it's non-zero remove it from num
97 // and den. Otherwise just return the original values.
110 /**********************************************************************
112 **********************************************************************
113 * Given the output width (if HB_KEEP_WIDTH) or height
114 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
115 * correct height or width in order to respect the DVD aspect ratio
116 *********************************************************************/
117 void hb_fix_aspect( hb_job_t * job, int keep )
119 hb_title_t * title = job->title;
122 /* don't do anything unless the title has complete size info */
123 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
125 hb_log( "hb_fix_aspect: incomplete info for title %d: "
126 "height = %d, width = %d, aspect = %d",
127 title->height, title->width, title->aspect );
132 Widths and heights must be multiples of 16 and greater than or
134 Crop values must be multiples of 2, greater than or equal to 0
135 and less than half of the dimension */
136 job->width = MULTIPLE_16( job->width );
137 job->height = MULTIPLE_16( job->height );
138 job->width = MAX( 16, job->width );
139 job->height = MAX( 16, job->height );
140 for( i = 0; i < 4; i++ )
142 job->crop[i] = EVEN( job->crop[i] );
143 job->crop[i] = MAX( 0, job->crop[i] );
147 job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
152 job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
156 double par = (double)title->width / ( (double)title->height * title->aspect );
157 double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
158 (double)(title->width - job->crop[2] - job->crop[3] );
159 double ar = par * cropped_sar;
160 if( keep == HB_KEEP_WIDTH )
162 job->height = MULTIPLE_16( (uint64_t)( (double)job->width * ar ) );
163 job->height = MAX( 16, job->height );
167 job->width = MULTIPLE_16( (uint64_t)( (double)job->height / ar ) );
168 job->width = MAX( 16, job->width );
172 /**********************************************************************
174 **********************************************************************
176 *********************************************************************/
177 int hb_calc_bitrate( hb_job_t * job, int size )
179 int64_t avail = (int64_t) size * 1024 * 1024;
182 int samples_per_frame;
185 hb_title_t * title = job->title;
186 hb_chapter_t * chapter;
189 /* How many overhead bytes are used for each frame
209 /* Get the duration in seconds */
211 for( i = job->chapter_start; i <= job->chapter_end; i++ )
213 chapter = hb_list_item( title->list_chapter, i - 1 );
214 length += chapter->duration;
221 hb_interjob_t * interjob = hb_interjob_get( job->h );
222 avail = job->vbitrate * 125 * length;
223 avail += length * interjob->vrate * overhead / interjob->vrate_base;
227 avail -= length * job->vrate * overhead / job->vrate_base;
234 for( i = 0; i < hb_list_count(job->list_audio); i++ )
238 audio = hb_list_item( job->list_audio, i);
240 /* How many audio samples we put in each frame */
241 switch( audio->config.out.codec )
244 case HB_ACODEC_CA_AAC:
245 case HB_ACODEC_VORBIS:
246 samples_per_frame = 1024;
249 samples_per_frame = 1152;
253 samples_per_frame = 1536;
259 if( audio->config.out.codec == HB_ACODEC_AC3 ||
260 audio->config.out.codec == HB_ACODEC_DCA)
263 * For pass through we take the bitrate from the input audio
264 * bitrate as we are simply passing it through.
266 abitrate = audio->config.in.bitrate / 8;
271 * Where we are transcoding the audio we use the destination
274 abitrate = audio->config.out.bitrate * 1000 / 8;
276 avail -= length * abitrate;
279 avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
288 return ( avail / ( 125 * length ) );
291 /**********************************************************************
292 * hb_list implementation
293 **********************************************************************
294 * Basic and slow, but enough for what we need
295 *********************************************************************/
297 #define HB_LIST_DEFAULT_SIZE 20
301 /* Pointers to items in the list */
304 /* How many (void *) allocated in 'items' */
307 /* How many valid pointers in 'items' */
311 /**********************************************************************
313 **********************************************************************
314 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
315 *********************************************************************/
316 hb_list_t * hb_list_init()
320 l = calloc( sizeof( hb_list_t ), 1 );
321 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
322 l->items_alloc = HB_LIST_DEFAULT_SIZE;
327 /**********************************************************************
329 **********************************************************************
330 * Returns the number of items currently in the list
331 *********************************************************************/
332 int hb_list_count( hb_list_t * l )
334 return l->items_count;
337 /**********************************************************************
339 **********************************************************************
340 * Adds an item at the end of the list, making it bigger if necessary.
341 * Can safely be called with a NULL pointer to add, it will be ignored.
342 *********************************************************************/
343 void hb_list_add( hb_list_t * l, void * p )
350 if( l->items_count == l->items_alloc )
352 /* We need a bigger boat */
353 l->items_alloc += HB_LIST_DEFAULT_SIZE;
354 l->items = realloc( l->items,
355 l->items_alloc * sizeof( void * ) );
358 l->items[l->items_count] = p;
362 /**********************************************************************
364 **********************************************************************
365 * Remove an item from the list. Bad things will happen if called
366 * with a NULL pointer or if the item is not in the list.
367 *********************************************************************/
368 void hb_list_rem( hb_list_t * l, void * p )
372 /* Find the item in the list */
373 for( i = 0; i < l->items_count; i++ )
375 if( l->items[i] == p )
381 /* Shift all items after it sizeof( void * ) bytes earlier */
382 memmove( &l->items[i], &l->items[i+1],
383 ( l->items_count - i - 1 ) * sizeof( void * ) );
388 /**********************************************************************
390 **********************************************************************
391 * Returns item at position i, or NULL if there are not that many
393 *********************************************************************/
394 void * hb_list_item( hb_list_t * l, int i )
396 if( i < 0 || i >= l->items_count )
404 /**********************************************************************
406 **********************************************************************
407 * Assuming all items are of type hb_buffer_t, returns the total
408 * number of bytes in the list
409 *********************************************************************/
410 int hb_list_bytes( hb_list_t * l )
417 for( i = 0; i < hb_list_count( l ); i++ )
419 buf = hb_list_item( l, i );
420 ret += buf->size - buf->cur;
426 /**********************************************************************
428 **********************************************************************
429 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
430 * the list to <dst>, keeping the list unmodified.
431 *********************************************************************/
432 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
439 for( i = 0, copied = 0; copied < size; i++ )
441 buf = hb_list_item( l, i );
442 copying = MIN( buf->size - buf->cur, size - copied );
443 memcpy( &dst[copied], &buf->data[buf->cur], copying );
448 /**********************************************************************
450 **********************************************************************
451 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
452 * the list to <dst>. What's copied is removed from the list.
453 * The variable pointed by <pts> is set to the PTS of the buffer the
454 * first byte has been got from.
455 * The variable pointed by <pos> is set to the position of that byte
457 *********************************************************************/
458 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
459 uint64_t * pts, uint64_t * pos )
466 /* So we won't have to deal with NULL pointers */
467 uint64_t dummy1, dummy2;
469 if( !pts ) pts = &dummy1;
470 if( !pos ) pos = &dummy2;
472 for( copied = 0, has_pts = 0; copied < size; )
474 buf = hb_list_item( l, 0 );
475 copying = MIN( buf->size - buf->cur, size - copied );
476 memcpy( &dst[copied], &buf->data[buf->cur], copying );
486 if( buf->cur >= buf->size )
488 hb_list_rem( l, buf );
489 hb_buffer_close( &buf );
496 /**********************************************************************
498 **********************************************************************
499 * Assuming all items are of type hb_buffer_t, close them all and
501 *********************************************************************/
502 void hb_list_empty( hb_list_t ** _l )
507 while( ( b = hb_list_item( l, 0 ) ) )
510 hb_buffer_close( &b );
516 /**********************************************************************
518 **********************************************************************
519 * Free memory allocated by hb_list_init. Does NOT free contents of
520 * items still in the list.
521 *********************************************************************/
522 void hb_list_close( hb_list_t ** _l )
532 /**********************************************************************
534 **********************************************************************
535 * If verbose mode is one, print message with timestamp. Messages
536 * longer than 180 characters are stripped ;p
537 *********************************************************************/
538 void hb_log( char * log, ... )
540 char string[362]; /* 360 chars + \n + \0 */
545 if( !getenv( "HB_DEBUG" ) )
547 /* We don't want to print it */
553 now = localtime( &_now );
554 sprintf( string, "[%02d:%02d:%02d] ",
555 now->tm_hour, now->tm_min, now->tm_sec );
557 /* Convert the message to a string */
558 va_start( args, log );
559 vsnprintf( string + 11, 349, log, args );
562 /* Add the end of line */
563 strcat( string, "\n" );
566 fprintf( stderr, "%s", string );
569 int global_verbosity_level; //Necessary for hb_deep_log
570 /**********************************************************************
572 **********************************************************************
573 * If verbose mode is >= level, print message with timestamp. Messages
574 * longer than 360 characters are stripped ;p
575 *********************************************************************/
576 void hb_deep_log( hb_debug_level_t level, char * log, ... )
578 char string[362]; /* 360 chars + \n + \0 */
583 if( global_verbosity_level < level )
591 now = localtime( &_now );
592 sprintf( string, "[%02d:%02d:%02d] ",
593 now->tm_hour, now->tm_min, now->tm_sec );
595 /* Convert the message to a string */
596 va_start( args, log );
597 vsnprintf( string + 11, 349, log, args );
600 /* Add the end of line */
601 strcat( string, "\n" );
604 fprintf( stderr, "%s", string );
607 /**********************************************************************
609 **********************************************************************
610 * Using whatever output is available display this error.
611 *********************************************************************/
612 void hb_error( char * log, ... )
614 char string[181]; /* 180 chars + \0 */
617 /* Convert the message to a string */
618 va_start( args, log );
619 vsnprintf( string, 180, log, args );
623 * Got the error in a single string, send it off to be dispatched.
627 error_handler( string );
633 void hb_register_error_handler( hb_error_handler_t * handler )
635 error_handler = handler;
638 /**********************************************************************
640 **********************************************************************
642 *********************************************************************/
643 hb_title_t * hb_title_init( char * dvd, int index )
647 t = calloc( sizeof( hb_title_t ), 1 );
650 t->list_audio = hb_list_init();
651 t->list_chapter = hb_list_init();
652 t->list_subtitle = hb_list_init();
653 strcat( t->dvd, dvd );
654 // default to decoding mpeg2
656 t->video_codec = WORK_DECMPEG2;
661 /**********************************************************************
663 **********************************************************************
665 *********************************************************************/
666 void hb_title_close( hb_title_t ** _t )
668 hb_title_t * t = *_t;
670 hb_chapter_t * chapter;
671 hb_subtitle_t * subtitle;
673 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
675 hb_list_rem( t->list_audio, audio );
678 hb_list_close( &t->list_audio );
680 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
682 hb_list_rem( t->list_chapter, chapter );
685 hb_list_close( &t->list_chapter );
687 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
689 hb_list_rem( t->list_subtitle, subtitle );
692 hb_list_close( &t->list_subtitle );
696 if( t->metadata->coverart )
698 free( t->metadata->coverart );
707 /**********************************************************************
709 **********************************************************************
711 *********************************************************************/
712 void hb_filter_close( hb_filter_object_t ** _f )
714 hb_filter_object_t * f = *_f;
716 f->close( f->private_data );
727 /**********************************************************************
729 **********************************************************************
731 *********************************************************************/
732 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
734 hb_audio_t *audio = NULL;
738 audio = calloc(1, sizeof(*audio));
739 memcpy(audio, src, sizeof(*audio));
744 /**********************************************************************
746 **********************************************************************
748 *********************************************************************/
749 void hb_audio_config_init(hb_audio_config_t * audiocfg)
751 /* Set read only paramaters to invalid values */
752 audiocfg->in.codec = 0xDEADBEEF;
753 audiocfg->in.bitrate = -1;
754 audiocfg->in.samplerate = -1;
755 audiocfg->in.channel_layout = 0;
756 audiocfg->in.version = 0;
757 audiocfg->in.mode = 0;
758 audiocfg->flags.ac3 = 0;
759 audiocfg->lang.description[0] = 0;
760 audiocfg->lang.simple[0] = 0;
761 audiocfg->lang.iso639_2[0] = 0;
763 /* Initalize some sensable defaults */
764 audiocfg->in.track = audiocfg->out.track = 0;
765 audiocfg->out.codec = HB_ACODEC_FAAC;
766 audiocfg->out.bitrate = 128;
767 audiocfg->out.samplerate = 44100;
768 audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
769 audiocfg->out.dynamic_range_compression = 0;
770 audiocfg->out.name = NULL;
773 /**********************************************************************
775 **********************************************************************
777 *********************************************************************/
778 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
780 hb_title_t *title = job->title;
783 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
790 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
792 /* This most likely means the client didn't call hb_audio_config_init
798 /* Really shouldn't ignore the passed out track, but there is currently no
799 * way to handle duplicates or out-of-order track numbers.
801 audio->config.out.track = hb_list_count(job->list_audio) + 1;
802 audio->config.out.codec = audiocfg->out.codec;
803 if( audiocfg->out.codec == audio->config.in.codec )
805 /* Pass-through, copy from input. */
806 audio->config.out.samplerate = audio->config.in.samplerate;
807 audio->config.out.bitrate = audio->config.in.bitrate;
808 audio->config.out.dynamic_range_compression = 0;
809 audio->config.out.mixdown = 0;
813 /* Non pass-through, use what is given. */
814 audio->config.out.samplerate = audiocfg->out.samplerate;
815 audio->config.out.bitrate = audiocfg->out.bitrate;
816 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
817 audio->config.out.mixdown = audiocfg->out.mixdown;
820 hb_list_add(job->list_audio, audio);
824 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
826 hb_audio_t *audio = NULL;
828 if( (audio = hb_list_item(list, i)) )
829 return &(audio->config);
834 /**********************************************************************
836 **********************************************************************
838 *********************************************************************/
839 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
841 hb_subtitle_t *subtitle = NULL;
845 subtitle = calloc(1, sizeof(*subtitle));
846 memcpy(subtitle, src, sizeof(*subtitle));
851 /**********************************************************************
853 **********************************************************************
855 *********************************************************************/
856 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
858 hb_title_t *title = job->title;
859 hb_subtitle_t *subtitle;
861 subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
862 if( subtitle == NULL )
867 subtitle->config = *subtitlecfg;
868 hb_list_add(job->list_subtitle, subtitle);