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. */
15 /**********************************************************************
17 *********************************************************************/
18 hb_rate_t hb_video_rates[] =
19 { { "5", 5400000 }, { "10", 2700000 }, { "12", 2250000 },
20 { "15", 1800000 }, { "23.976", 1126125 }, { "24", 1125000 },
21 { "25", 1080000 }, { "29.97", 900900 } };
22 int hb_video_rates_count = sizeof( hb_video_rates ) /
25 hb_rate_t hb_audio_rates[] =
26 { { "22.05", 22050 }, { "24", 24000 }, { "32", 32000 },
27 { "44.1", 44100 }, { "48", 48000 } };
28 int hb_audio_rates_count = sizeof( hb_audio_rates ) /
30 int hb_audio_rates_default = 3; /* 44100 Hz */
32 hb_rate_t hb_audio_bitrates[] =
33 { { "32", 32 }, { "40", 40 }, { "48", 48 }, { "56", 56 },
34 { "64", 64 }, { "80", 80 }, { "96", 96 }, { "112", 112 },
35 { "128", 128 }, { "160", 160 }, { "192", 192 }, { "224", 224 },
36 { "256", 256 }, { "320", 320 }, { "384", 384 }, { "448", 448 },
38 int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) /
40 int hb_audio_bitrates_default = 8; /* 128 kbps */
42 static hb_error_handler_t *error_handler = NULL;
44 hb_mixdown_t hb_audio_mixdowns[] =
45 { { "Mono", "HB_AMIXDOWN_MONO", "mono", HB_AMIXDOWN_MONO },
46 { "Stereo", "HB_AMIXDOWN_STEREO", "stereo", HB_AMIXDOWN_STEREO },
47 { "Dolby Surround", "HB_AMIXDOWN_DOLBY", "dpl1", HB_AMIXDOWN_DOLBY },
48 { "Dolby Pro Logic II", "HB_AMIXDOWN_DOLBYPLII", "dpl2", HB_AMIXDOWN_DOLBYPLII },
49 { "6-channel discrete", "HB_AMIXDOWN_6CH", "6ch", HB_AMIXDOWN_6CH }
51 int hb_audio_mixdowns_count = sizeof( hb_audio_mixdowns ) /
52 sizeof( hb_mixdown_t );
54 int hb_mixdown_get_mixdown_from_short_name( const char * short_name )
57 for (i = 0; i < hb_audio_mixdowns_count; i++)
59 if (strcmp(hb_audio_mixdowns[i].short_name, short_name) == 0)
61 return hb_audio_mixdowns[i].amixdown;
67 const char * hb_mixdown_get_short_name_from_mixdown( int amixdown )
70 for (i = 0; i < hb_audio_mixdowns_count; i++)
72 if (hb_audio_mixdowns[i].amixdown == amixdown)
74 return hb_audio_mixdowns[i].short_name;
80 /**********************************************************************
82 **********************************************************************
83 * Given a numerator (num) and a denominator (den), reduce them to an
84 * equivalent fraction and store the result in x and y.
85 *********************************************************************/
86 void hb_reduce( int *x, int *y, int num, int den )
88 // find the greatest common divisor of num & den by Euclid's algorithm
97 // at this point n is the gcd. if it's non-zero remove it from num
98 // and den. Otherwise just return the original values.
111 /**********************************************************************
113 **********************************************************************
114 * Given the output width (if HB_KEEP_WIDTH) or height
115 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
116 * correct height or width in order to respect the DVD aspect ratio
117 *********************************************************************/
118 void hb_fix_aspect( hb_job_t * job, int keep )
120 hb_title_t * title = job->title;
123 /* don't do anything unless the title has complete size info */
124 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
126 hb_log( "hb_fix_aspect: incomplete info for title %d: "
127 "height = %d, width = %d, aspect = %.3f",
128 title->index, title->height, title->width, title->aspect );
133 Widths and heights must be multiples of 16 and greater than or
135 Crop values must be multiples of 2, greater than or equal to 0
136 and less than half of the dimension */
137 job->width = MULTIPLE_16( job->width );
138 job->height = MULTIPLE_16( job->height );
139 job->width = MAX( 16, job->width );
140 job->height = MAX( 16, job->height );
141 for( i = 0; i < 4; i++ )
143 job->crop[i] = EVEN( job->crop[i] );
144 job->crop[i] = MAX( 0, job->crop[i] );
148 job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
153 job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
157 double par = (double)title->width / ( (double)title->height * title->aspect );
158 double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
159 (double)(title->width - job->crop[2] - job->crop[3] );
160 double ar = par * cropped_sar;
161 if( keep == HB_KEEP_WIDTH )
163 job->height = MULTIPLE_16( (uint64_t)( (double)job->width * ar ) );
164 job->height = MAX( 16, job->height );
168 job->width = MULTIPLE_16( (uint64_t)( (double)job->height / ar ) );
169 job->width = MAX( 16, job->width );
173 /**********************************************************************
175 **********************************************************************
177 *********************************************************************/
178 int hb_calc_bitrate( hb_job_t * job, int size )
180 int64_t avail = (int64_t) size * 1024 * 1024;
183 int samples_per_frame;
186 hb_title_t * title = job->title;
187 hb_chapter_t * chapter;
190 /* How many overhead bytes are used for each frame
210 /* Get the duration in seconds */
212 for( i = job->chapter_start; i <= job->chapter_end; i++ )
214 chapter = hb_list_item( title->list_chapter, i - 1 );
215 length += chapter->duration;
222 hb_interjob_t * interjob = hb_interjob_get( job->h );
223 avail = job->vbitrate * 125 * length;
224 avail += length * interjob->vrate * overhead / interjob->vrate_base;
228 avail -= length * job->vrate * overhead / job->vrate_base;
235 for( i = 0; i < hb_list_count(job->list_audio); i++ )
239 audio = hb_list_item( job->list_audio, i);
241 /* How many audio samples we put in each frame */
242 switch( audio->config.out.codec )
245 case HB_ACODEC_CA_AAC:
246 case HB_ACODEC_VORBIS:
247 samples_per_frame = 1024;
250 samples_per_frame = 1152;
254 samples_per_frame = 1536;
260 if( audio->config.out.codec == HB_ACODEC_AC3 ||
261 audio->config.out.codec == HB_ACODEC_DCA)
264 * For pass through we take the bitrate from the input audio
265 * bitrate as we are simply passing it through.
267 abitrate = audio->config.in.bitrate / 8;
272 * Where we are transcoding the audio we use the destination
275 abitrate = audio->config.out.bitrate * 1000 / 8;
277 avail -= length * abitrate;
280 avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
289 return ( avail / ( 125 * length ) );
292 /**********************************************************************
293 * hb_list implementation
294 **********************************************************************
295 * Basic and slow, but enough for what we need
296 *********************************************************************/
298 #define HB_LIST_DEFAULT_SIZE 20
302 /* Pointers to items in the list */
305 /* How many (void *) allocated in 'items' */
308 /* How many valid pointers in 'items' */
312 /**********************************************************************
314 **********************************************************************
315 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
316 *********************************************************************/
317 hb_list_t * hb_list_init()
321 l = calloc( sizeof( hb_list_t ), 1 );
322 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
323 l->items_alloc = HB_LIST_DEFAULT_SIZE;
328 /**********************************************************************
330 **********************************************************************
331 * Returns the number of items currently in the list
332 *********************************************************************/
333 int hb_list_count( hb_list_t * l )
335 return l->items_count;
338 /**********************************************************************
340 **********************************************************************
341 * Adds an item at the end of the list, making it bigger if necessary.
342 * Can safely be called with a NULL pointer to add, it will be ignored.
343 *********************************************************************/
344 void hb_list_add( hb_list_t * l, void * p )
351 if( l->items_count == l->items_alloc )
353 /* We need a bigger boat */
354 l->items_alloc += HB_LIST_DEFAULT_SIZE;
355 l->items = realloc( l->items,
356 l->items_alloc * sizeof( void * ) );
359 l->items[l->items_count] = p;
363 /**********************************************************************
365 **********************************************************************
366 * Remove an item from the list. Bad things will happen if called
367 * with a NULL pointer or if the item is not in the list.
368 *********************************************************************/
369 void hb_list_rem( hb_list_t * l, void * p )
373 /* Find the item in the list */
374 for( i = 0; i < l->items_count; i++ )
376 if( l->items[i] == p )
382 /* Shift all items after it sizeof( void * ) bytes earlier */
383 memmove( &l->items[i], &l->items[i+1],
384 ( l->items_count - i - 1 ) * sizeof( void * ) );
389 /**********************************************************************
391 **********************************************************************
392 * Returns item at position i, or NULL if there are not that many
394 *********************************************************************/
395 void * hb_list_item( hb_list_t * l, int i )
397 if( i < 0 || i >= l->items_count )
405 /**********************************************************************
407 **********************************************************************
408 * Assuming all items are of type hb_buffer_t, returns the total
409 * number of bytes in the list
410 *********************************************************************/
411 int hb_list_bytes( hb_list_t * l )
418 for( i = 0; i < hb_list_count( l ); i++ )
420 buf = hb_list_item( l, i );
421 ret += buf->size - buf->cur;
427 /**********************************************************************
429 **********************************************************************
430 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
431 * the list to <dst>, keeping the list unmodified.
432 *********************************************************************/
433 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
440 for( i = 0, copied = 0; copied < size; i++ )
442 buf = hb_list_item( l, i );
443 copying = MIN( buf->size - buf->cur, size - copied );
444 memcpy( &dst[copied], &buf->data[buf->cur], copying );
449 /**********************************************************************
451 **********************************************************************
452 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
453 * the list to <dst>. What's copied is removed from the list.
454 * The variable pointed by <pts> is set to the PTS of the buffer the
455 * first byte has been got from.
456 * The variable pointed by <pos> is set to the position of that byte
458 *********************************************************************/
459 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
460 uint64_t * pts, uint64_t * pos )
467 /* So we won't have to deal with NULL pointers */
468 uint64_t dummy1, dummy2;
470 if( !pts ) pts = &dummy1;
471 if( !pos ) pos = &dummy2;
473 for( copied = 0, has_pts = 0; copied < size; )
475 buf = hb_list_item( l, 0 );
476 copying = MIN( buf->size - buf->cur, size - copied );
477 memcpy( &dst[copied], &buf->data[buf->cur], copying );
487 if( buf->cur >= buf->size )
489 hb_list_rem( l, buf );
490 hb_buffer_close( &buf );
497 /**********************************************************************
499 **********************************************************************
500 * Assuming all items are of type hb_buffer_t, close them all and
502 *********************************************************************/
503 void hb_list_empty( hb_list_t ** _l )
508 while( ( b = hb_list_item( l, 0 ) ) )
511 hb_buffer_close( &b );
517 /**********************************************************************
519 **********************************************************************
520 * Free memory allocated by hb_list_init. Does NOT free contents of
521 * items still in the list.
522 *********************************************************************/
523 void hb_list_close( hb_list_t ** _l )
533 /**********************************************************************
535 **********************************************************************
536 * If verbose mode is one, print message with timestamp. Messages
537 * longer than 180 characters are stripped ;p
538 *********************************************************************/
539 void hb_log( char * log, ... )
541 char string[362]; /* 360 chars + \n + \0 */
546 if( !getenv( "HB_DEBUG" ) )
548 /* We don't want to print it */
554 now = localtime( &_now );
555 sprintf( string, "[%02d:%02d:%02d] ",
556 now->tm_hour, now->tm_min, now->tm_sec );
558 /* Convert the message to a string */
559 va_start( args, log );
560 vsnprintf( string + 11, 349, log, args );
563 /* Add the end of line */
564 strcat( string, "\n" );
567 fprintf( stderr, "%s", string );
570 int global_verbosity_level; //Necessary for hb_deep_log
571 /**********************************************************************
573 **********************************************************************
574 * If verbose mode is >= level, print message with timestamp. Messages
575 * longer than 360 characters are stripped ;p
576 *********************************************************************/
577 void hb_deep_log( hb_debug_level_t level, char * log, ... )
579 char string[362]; /* 360 chars + \n + \0 */
584 if( global_verbosity_level < level )
592 now = localtime( &_now );
593 sprintf( string, "[%02d:%02d:%02d] ",
594 now->tm_hour, now->tm_min, now->tm_sec );
596 /* Convert the message to a string */
597 va_start( args, log );
598 vsnprintf( string + 11, 349, log, args );
601 /* Add the end of line */
602 strcat( string, "\n" );
605 fprintf( stderr, "%s", string );
608 /**********************************************************************
610 **********************************************************************
611 * Using whatever output is available display this error.
612 *********************************************************************/
613 void hb_error( char * log, ... )
615 char string[181]; /* 180 chars + \0 */
616 char rep_string[181];
617 static char last_string[181];
618 static int last_error_count = 0;
619 static uint64_t last_series_error_time = 0;
620 static hb_lock_t *mutex = 0;
624 /* Convert the message to a string */
625 va_start( args, log );
626 vsnprintf( string, 180, log, args );
631 mutex = hb_lock_init();
636 time_now = hb_get_date();
638 if( strcmp( string, last_string) == 0 )
641 * The last error and this one are the same, don't log it
642 * just count it instead, unless it was more than one second
646 if( last_series_error_time + ( 1000 * 1 ) > time_now )
654 * A new error, or the same one more than 10sec since the last one
655 * did we have any of the same counted up?
657 if( last_error_count > 0 )
660 * Print out the last error to ensure context for the last
665 error_handler( last_string );
667 hb_log( "%s", last_string );
670 if( last_error_count > 1 )
673 * Only print out the repeat message for more than 2 of the
674 * same, since we just printed out two of them already.
676 snprintf( rep_string, 180, "Last error repeated %d times",
677 last_error_count - 1 );
681 error_handler( rep_string );
683 hb_log( "%s", rep_string );
687 last_error_count = 0;
690 last_series_error_time = time_now;
692 strcpy( last_string, string );
695 * Got the error in a single string, send it off to be dispatched.
699 error_handler( string );
701 hb_log( "%s", string );
707 void hb_register_error_handler( hb_error_handler_t * handler )
709 error_handler = handler;
712 /**********************************************************************
714 **********************************************************************
716 *********************************************************************/
717 hb_title_t * hb_title_init( char * path, int index )
721 t = calloc( sizeof( hb_title_t ), 1 );
724 t->list_audio = hb_list_init();
725 t->list_chapter = hb_list_init();
726 t->list_subtitle = hb_list_init();
727 strcat( t->path, path );
728 // default to decoding mpeg2
730 t->video_codec = WORK_DECMPEG2;
735 /**********************************************************************
737 **********************************************************************
739 *********************************************************************/
740 void hb_title_close( hb_title_t ** _t )
742 hb_title_t * t = *_t;
744 hb_chapter_t * chapter;
745 hb_subtitle_t * subtitle;
747 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
749 hb_list_rem( t->list_audio, audio );
752 hb_list_close( &t->list_audio );
754 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
756 hb_list_rem( t->list_chapter, chapter );
759 hb_list_close( &t->list_chapter );
761 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
763 hb_list_rem( t->list_subtitle, subtitle );
766 hb_list_close( &t->list_subtitle );
770 if( t->metadata->coverart )
772 free( t->metadata->coverart );
781 /**********************************************************************
783 **********************************************************************
785 *********************************************************************/
786 void hb_filter_close( hb_filter_object_t ** _f )
788 hb_filter_object_t * f = *_f;
790 f->close( f->private_data );
801 /**********************************************************************
803 **********************************************************************
805 *********************************************************************/
806 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
808 hb_audio_t *audio = NULL;
812 audio = calloc(1, sizeof(*audio));
813 memcpy(audio, src, sizeof(*audio));
818 /**********************************************************************
820 **********************************************************************
822 *********************************************************************/
823 void hb_audio_config_init(hb_audio_config_t * audiocfg)
825 /* Set read only paramaters to invalid values */
826 audiocfg->in.codec = 0xDEADBEEF;
827 audiocfg->in.bitrate = -1;
828 audiocfg->in.samplerate = -1;
829 audiocfg->in.channel_layout = 0;
830 audiocfg->in.version = 0;
831 audiocfg->in.mode = 0;
832 audiocfg->flags.ac3 = 0;
833 audiocfg->lang.description[0] = 0;
834 audiocfg->lang.simple[0] = 0;
835 audiocfg->lang.iso639_2[0] = 0;
837 /* Initalize some sensable defaults */
838 audiocfg->in.track = audiocfg->out.track = 0;
839 audiocfg->out.codec = HB_ACODEC_FAAC;
840 audiocfg->out.bitrate = 128;
841 audiocfg->out.samplerate = 44100;
842 audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
843 audiocfg->out.dynamic_range_compression = 0;
844 audiocfg->out.name = NULL;
847 /**********************************************************************
849 **********************************************************************
851 *********************************************************************/
852 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
854 hb_title_t *title = job->title;
857 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
864 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
866 /* This most likely means the client didn't call hb_audio_config_init
872 /* Really shouldn't ignore the passed out track, but there is currently no
873 * way to handle duplicates or out-of-order track numbers.
875 audio->config.out.track = hb_list_count(job->list_audio) + 1;
876 audio->config.out.codec = audiocfg->out.codec;
877 if( audiocfg->out.codec == audio->config.in.codec )
879 /* Pass-through, copy from input. */
880 audio->config.out.samplerate = audio->config.in.samplerate;
881 audio->config.out.bitrate = audio->config.in.bitrate;
882 audio->config.out.dynamic_range_compression = 0;
883 audio->config.out.mixdown = 0;
887 /* Non pass-through, use what is given. */
888 audio->config.out.samplerate = audiocfg->out.samplerate;
889 audio->config.out.bitrate = audiocfg->out.bitrate;
890 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
891 audio->config.out.mixdown = audiocfg->out.mixdown;
894 hb_list_add(job->list_audio, audio);
898 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
900 hb_audio_t *audio = NULL;
902 if( (audio = hb_list_item(list, i)) )
903 return &(audio->config);
908 /**********************************************************************
910 **********************************************************************
912 *********************************************************************/
913 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
915 hb_subtitle_t *subtitle = NULL;
919 subtitle = calloc(1, sizeof(*subtitle));
920 memcpy(subtitle, src, sizeof(*subtitle));
925 /**********************************************************************
927 **********************************************************************
929 *********************************************************************/
930 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
932 hb_title_t *title = job->title;
933 hb_subtitle_t *subtitle;
935 subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
936 if( subtitle == NULL )
941 subtitle->config = *subtitlecfg;
942 hb_list_add(job->list_subtitle, subtitle);
946 int hb_srt_add( const hb_job_t * job,
947 const hb_subtitle_config_t * subtitlecfg,
950 hb_subtitle_t *subtitle;
951 iso639_lang_t *language = NULL;
954 subtitle = calloc( 1, sizeof( *subtitle ) );
956 subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
957 subtitle->format = TEXTSUB;
958 subtitle->source = SRTSUB;
960 language = lang_for_code2( lang );
965 strcpy( subtitle->lang, language->eng_name );
966 strncpy( subtitle->iso639_2, lang, 4 );
968 subtitle->config = *subtitlecfg;
969 subtitle->config.dest = PASSTHRUSUB;
971 hb_list_add(job->list_subtitle, subtitle);
977 char * hb_strdup_printf( char * fmt, ... )
985 str = malloc( size );
991 /* Try to print in the allocated space. */
993 len = vsnprintf( str, size, fmt, ap );
996 /* If that worked, return the string. */
997 if ( len > -1 && len < size )
1002 /* Else try again with more space. */
1003 if ( len > -1 ) /* glibc 2.1 */
1004 size = len + 1; /* precisely what is needed */
1005 else /* glibc 2.0 */
1006 size *= 2; /* twice the old size */
1007 tmp = realloc( str, size );