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 },
37 { "512", 512 }, { "576", 576 }, { "640", 640 }, { "768", 768 } };
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 // Given an input bitrate, find closest match in the set of allowed bitrates
81 int hb_find_closest_audio_bitrate(int bitrate)
86 // result is highest rate if none found during search.
87 // rate returned will always be <= rate asked for.
88 result = hb_audio_bitrates[0].rate;
89 for (ii = hb_audio_bitrates_count-1; ii >= 0; ii--)
91 if (bitrate >= hb_audio_bitrates[ii].rate)
93 result = hb_audio_bitrates[ii].rate;
100 // Get the bitrate low and high limits for a codec/samplerate/mixdown triplet
101 void hb_get_audio_bitrate_limits(uint32_t codec, int samplerate, int mixdown, int *low, int *high)
105 channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(mixdown);
109 *low = 32 * channels;
110 if (samplerate > 24000)
120 case HB_ACODEC_CA_AAC:
121 if (samplerate > 24000)
123 *low = channels * 32;
135 *low = channels * 16;
136 *high = channels * 64;
146 *low = 32 * channels;
147 if (samplerate > 24000)
149 *high = 160 * channels;
153 *high = 128 * channels;
159 case HB_ACODEC_VORBIS:
160 *high = channels * 80;
161 if (samplerate > 24000)
165 // Vorbis minimum is around 30kbps/ch for 6ch
166 // at rates > 24k (32k/44.1k/48k)
167 *low = 32 * channels;
168 *high = 128 * channels;
172 // Allow 24kbps mono and 48kbps stereo at rates > 24k
174 *low = 24 * channels;
175 if (samplerate > 32000)
176 *high = channels * 224;
178 *high = channels * 160;
183 *low = channels * 16;
184 *high = 80 * channels;
189 *low = hb_audio_bitrates[0].rate;
190 *high = hb_audio_bitrates[hb_audio_bitrates_count-1].rate;
195 // Given an input bitrate, sanitize it. Check low and high limits and
196 // make sure it is in the set of allowed bitrates.
197 int hb_get_best_audio_bitrate( uint32_t codec, int bitrate, int samplerate, int mixdown)
201 hb_get_audio_bitrate_limits(codec, samplerate, mixdown, &low, &high);
206 bitrate = hb_find_closest_audio_bitrate(bitrate);
210 // Get the default bitrate for a given codec/samplerate/mixdown triplet.
211 int hb_get_default_audio_bitrate( uint32_t codec, int samplerate, int mixdown )
213 int bitrate, channels;
216 channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(mixdown);
218 // Min bitrate is established such that we get good quality
219 // audio as a minimum.
220 sr_shift = (samplerate <= 24000) ? 1 : 0;
227 else if (channels <= 2)
233 bitrate = channels * 80;
235 bitrate >>= sr_shift;
236 bitrate = hb_get_best_audio_bitrate( codec, bitrate, samplerate, mixdown );
240 int hb_get_best_mixdown( uint32_t codec, int layout )
242 switch (layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK)
244 // stereo input or something not handled below
246 case HB_INPUT_CH_LAYOUT_STEREO:
247 // mono gets mixed up to stereo & more than stereo gets mixed down
248 return HB_AMIXDOWN_STEREO;
251 case HB_INPUT_CH_LAYOUT_MONO:
252 // everything else passes through
253 return HB_AMIXDOWN_MONO;
255 // dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input
256 // the A52 flags don't allow for a way to distinguish between DPL1 and
257 // DPL2 on a DVD so we always assume a DPL1 source for A52_DOLBY.
258 case HB_INPUT_CH_LAYOUT_DOLBY:
259 return HB_AMIXDOWN_DOLBY;
261 // 4 channel discrete
262 case HB_INPUT_CH_LAYOUT_2F2R:
263 case HB_INPUT_CH_LAYOUT_3F1R:
264 // a52dec and libdca can't upmix to 6ch,
265 // so we must downmix these.
266 return HB_AMIXDOWN_DOLBYPLII;
268 // 5 or 6 channel discrete
269 case HB_INPUT_CH_LAYOUT_3F2R:
270 if ( ! ( layout & HB_INPUT_CH_LAYOUT_HAS_LFE ) )
272 // we don't do 5 channel discrete so mixdown to DPLII
273 // a52dec and libdca can't upmix to 6ch,
274 // so we must downmix this.
275 return HB_AMIXDOWN_DOLBYPLII;
282 return HB_AMIXDOWN_DOLBYPLII;
285 return HB_AMIXDOWN_6CH;
291 int hb_get_default_mixdown( uint32_t codec, int layout )
293 switch (layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK)
295 // stereo input or something not handled below
297 case HB_INPUT_CH_LAYOUT_STEREO:
298 // mono gets mixed up to stereo & more than stereo gets mixed down
299 return HB_AMIXDOWN_STEREO;
302 case HB_INPUT_CH_LAYOUT_MONO:
303 // everything else passes through
304 return HB_AMIXDOWN_MONO;
306 // dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input
307 // the A52 flags don't allow for a way to distinguish between DPL1 and
308 // DPL2 on a DVD so we always assume a DPL1 source for A52_DOLBY.
309 case HB_INPUT_CH_LAYOUT_DOLBY:
310 return HB_AMIXDOWN_DOLBY;
312 // 4 channel discrete
313 case HB_INPUT_CH_LAYOUT_2F2R:
314 case HB_INPUT_CH_LAYOUT_3F1R:
315 // a52dec and libdca can't upmix to 6ch,
316 // so we must downmix these.
317 return HB_AMIXDOWN_DOLBYPLII;
319 // 5 or 6 channel discrete
320 case HB_INPUT_CH_LAYOUT_3F2R:
321 if ( ! ( layout & HB_INPUT_CH_LAYOUT_HAS_LFE ) )
323 // we don't do 5 channel discrete so mixdown to DPLII
324 // a52dec and libdca can't upmix to 6ch,
325 // so we must downmix this.
326 return HB_AMIXDOWN_DOLBYPLII;
333 return HB_AMIXDOWN_6CH;
336 return HB_AMIXDOWN_DOLBYPLII;
342 /**********************************************************************
344 **********************************************************************
345 * Given a numerator (num) and a denominator (den), reduce them to an
346 * equivalent fraction and store the result in x and y.
347 *********************************************************************/
348 void hb_reduce( int *x, int *y, int num, int den )
350 // find the greatest common divisor of num & den by Euclid's algorithm
351 int n = num, d = den;
359 // at this point n is the gcd. if it's non-zero remove it from num
360 // and den. Otherwise just return the original values.
373 /**********************************************************************
375 **********************************************************************
376 * Given the output width (if HB_KEEP_WIDTH) or height
377 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
378 * correct height or width in order to respect the DVD aspect ratio
379 *********************************************************************/
380 void hb_fix_aspect( hb_job_t * job, int keep )
382 hb_title_t * title = job->title;
388 /* don't do anything unless the title has complete size info */
389 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
391 hb_log( "hb_fix_aspect: incomplete info for title %d: "
392 "height = %d, width = %d, aspect = %.3f",
393 title->index, title->height, title->width, title->aspect );
397 // min_width and min_height should be multiples of modulus
400 modulus = job->modulus ? job->modulus : 16;
402 for( i = 0; i < 4; i++ )
404 // Sanity check crop values are zero or positive multiples of 2
408 job->crop[i] = MIN( EVEN( job->crop[i] ), EVEN( ( title->height / 2 ) - ( min_height / 2 ) ) );
409 job->crop[i] = MAX( 0, job->crop[i] );
414 job->crop[i] = MIN( EVEN( job->crop[i] ), EVEN( ( title->width / 2 ) - ( min_width / 2 ) ) );
415 job->crop[i] = MAX( 0, job->crop[i] );
419 double par = (double)title->width / ( (double)title->height * title->aspect );
420 double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
421 (double)( title->width - job->crop[2] - job->crop[3] );
422 double ar = par * cropped_sar;
424 // Dimensions must be greater than minimum and multiple of modulus
425 if( keep == HB_KEEP_WIDTH )
427 job->width = MULTIPLE_MOD( job->width, modulus );
428 job->width = MAX( min_width, job->width );
429 job->height = MULTIPLE_MOD( (uint64_t)( (double)job->width * ar ), modulus );
430 job->height = MAX( min_height, job->height );
434 job->height = MULTIPLE_MOD( job->height, modulus );
435 job->height = MAX( min_height, job->height );
436 job->width = MULTIPLE_MOD( (uint64_t)( (double)job->height / ar ), modulus );
437 job->width = MAX( min_width, job->width );
441 /**********************************************************************
443 **********************************************************************
445 *********************************************************************/
446 int hb_calc_bitrate( hb_job_t * job, int size )
448 int64_t avail = (int64_t) size * 1024 * 1024;
451 int samples_per_frame;
454 hb_title_t * title = job->title;
455 hb_chapter_t * chapter;
458 /* How many overhead bytes are used for each frame
478 /* Get the duration in seconds */
480 for( i = job->chapter_start; i <= job->chapter_end; i++ )
482 chapter = hb_list_item( title->list_chapter, i - 1 );
483 length += chapter->duration;
490 hb_interjob_t * interjob = hb_interjob_get( job->h );
491 avail = job->vbitrate * 125 * length;
492 avail += length * interjob->vrate * overhead / interjob->vrate_base;
496 avail -= length * job->vrate * overhead / job->vrate_base;
503 for( i = 0; i < hb_list_count(job->list_audio); i++ )
507 audio = hb_list_item( job->list_audio, i);
509 /* How many audio samples we put in each frame */
510 switch( audio->config.out.codec )
513 case HB_ACODEC_CA_AAC:
514 case HB_ACODEC_VORBIS:
515 samples_per_frame = 1024;
518 samples_per_frame = 1152;
520 case HB_ACODEC_AC3_PASS:
521 case HB_ACODEC_DCA_PASS:
524 samples_per_frame = 1536;
530 if( audio->config.out.codec == HB_ACODEC_AC3_PASS ||
531 audio->config.out.codec == HB_ACODEC_DCA_PASS)
534 * For pass through we take the bitrate from the input audio
535 * bitrate as we are simply passing it through.
537 abitrate = audio->config.in.bitrate / 8;
542 * Where we are transcoding the audio we use the destination
545 abitrate = audio->config.out.bitrate * 1000 / 8;
547 avail -= length * abitrate;
550 avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
559 return ( avail / ( 125 * length ) );
562 /**********************************************************************
563 * hb_list implementation
564 **********************************************************************
565 * Basic and slow, but enough for what we need
566 *********************************************************************/
568 #define HB_LIST_DEFAULT_SIZE 20
572 /* Pointers to items in the list */
575 /* How many (void *) allocated in 'items' */
578 /* How many valid pointers in 'items' */
582 /**********************************************************************
584 **********************************************************************
585 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
586 *********************************************************************/
587 hb_list_t * hb_list_init()
591 l = calloc( sizeof( hb_list_t ), 1 );
592 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
593 l->items_alloc = HB_LIST_DEFAULT_SIZE;
598 /**********************************************************************
600 **********************************************************************
601 * Returns the number of items currently in the list
602 *********************************************************************/
603 int hb_list_count( hb_list_t * l )
605 return l->items_count;
608 /**********************************************************************
610 **********************************************************************
611 * Adds an item at the end of the list, making it bigger if necessary.
612 * Can safely be called with a NULL pointer to add, it will be ignored.
613 *********************************************************************/
614 void hb_list_add( hb_list_t * l, void * p )
621 if( l->items_count == l->items_alloc )
623 /* We need a bigger boat */
624 l->items_alloc += HB_LIST_DEFAULT_SIZE;
625 l->items = realloc( l->items,
626 l->items_alloc * sizeof( void * ) );
629 l->items[l->items_count] = p;
633 /**********************************************************************
635 **********************************************************************
636 * Remove an item from the list. Bad things will happen if called
637 * with a NULL pointer or if the item is not in the list.
638 *********************************************************************/
639 void hb_list_rem( hb_list_t * l, void * p )
643 /* Find the item in the list */
644 for( i = 0; i < l->items_count; i++ )
646 if( l->items[i] == p )
652 /* Shift all items after it sizeof( void * ) bytes earlier */
653 memmove( &l->items[i], &l->items[i+1],
654 ( l->items_count - i - 1 ) * sizeof( void * ) );
659 /**********************************************************************
661 **********************************************************************
662 * Returns item at position i, or NULL if there are not that many
664 *********************************************************************/
665 void * hb_list_item( hb_list_t * l, int i )
667 if( i < 0 || i >= l->items_count )
675 /**********************************************************************
677 **********************************************************************
678 * Assuming all items are of type hb_buffer_t, returns the total
679 * number of bytes in the list
680 *********************************************************************/
681 int hb_list_bytes( hb_list_t * l )
688 for( i = 0; i < hb_list_count( l ); i++ )
690 buf = hb_list_item( l, i );
691 ret += buf->size - buf->cur;
697 /**********************************************************************
699 **********************************************************************
700 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
701 * the list to <dst>, keeping the list unmodified.
702 *********************************************************************/
703 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
710 for( i = 0, copied = 0; copied < size; i++ )
712 buf = hb_list_item( l, i );
713 copying = MIN( buf->size - buf->cur, size - copied );
714 memcpy( &dst[copied], &buf->data[buf->cur], copying );
719 /**********************************************************************
721 **********************************************************************
722 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
723 * the list to <dst>. What's copied is removed from the list.
724 * The variable pointed by <pts> is set to the PTS of the buffer the
725 * first byte has been got from.
726 * The variable pointed by <pos> is set to the position of that byte
728 *********************************************************************/
729 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
730 uint64_t * pts, uint64_t * pos )
737 /* So we won't have to deal with NULL pointers */
738 uint64_t dummy1, dummy2;
740 if( !pts ) pts = &dummy1;
741 if( !pos ) pos = &dummy2;
743 for( copied = 0, has_pts = 0; copied < size; )
745 buf = hb_list_item( l, 0 );
746 copying = MIN( buf->size - buf->cur, size - copied );
747 memcpy( &dst[copied], &buf->data[buf->cur], copying );
757 if( buf->cur >= buf->size )
759 hb_list_rem( l, buf );
760 hb_buffer_close( &buf );
767 /**********************************************************************
769 **********************************************************************
770 * Assuming all items are of type hb_buffer_t, close them all and
772 *********************************************************************/
773 void hb_list_empty( hb_list_t ** _l )
778 while( ( b = hb_list_item( l, 0 ) ) )
781 hb_buffer_close( &b );
787 /**********************************************************************
789 **********************************************************************
790 * Free memory allocated by hb_list_init. Does NOT free contents of
791 * items still in the list.
792 *********************************************************************/
793 void hb_list_close( hb_list_t ** _l )
803 /**********************************************************************
805 **********************************************************************
806 * If verbose mode is one, print message with timestamp. Messages
807 * longer than 180 characters are stripped ;p
808 *********************************************************************/
809 void hb_log( char * log, ... )
811 char string[362]; /* 360 chars + \n + \0 */
816 if( !getenv( "HB_DEBUG" ) )
818 /* We don't want to print it */
824 now = localtime( &_now );
825 sprintf( string, "[%02d:%02d:%02d] ",
826 now->tm_hour, now->tm_min, now->tm_sec );
828 /* Convert the message to a string */
829 va_start( args, log );
830 vsnprintf( string + 11, 349, log, args );
833 /* Add the end of line */
834 strcat( string, "\n" );
837 fprintf( stderr, "%s", string );
840 int global_verbosity_level; //Necessary for hb_deep_log
841 /**********************************************************************
843 **********************************************************************
844 * If verbose mode is >= level, print message with timestamp. Messages
845 * longer than 360 characters are stripped ;p
846 *********************************************************************/
847 void hb_deep_log( hb_debug_level_t level, char * log, ... )
849 char string[362]; /* 360 chars + \n + \0 */
854 if( global_verbosity_level < level )
862 now = localtime( &_now );
863 sprintf( string, "[%02d:%02d:%02d] ",
864 now->tm_hour, now->tm_min, now->tm_sec );
866 /* Convert the message to a string */
867 va_start( args, log );
868 vsnprintf( string + 11, 349, log, args );
871 /* Add the end of line */
872 strcat( string, "\n" );
875 fprintf( stderr, "%s", string );
878 /**********************************************************************
880 **********************************************************************
881 * Using whatever output is available display this error.
882 *********************************************************************/
883 void hb_error( char * log, ... )
885 char string[181]; /* 180 chars + \0 */
886 char rep_string[181];
887 static char last_string[181];
888 static int last_error_count = 0;
889 static uint64_t last_series_error_time = 0;
890 static hb_lock_t *mutex = 0;
894 /* Convert the message to a string */
895 va_start( args, log );
896 vsnprintf( string, 180, log, args );
901 mutex = hb_lock_init();
906 time_now = hb_get_date();
908 if( strcmp( string, last_string) == 0 )
911 * The last error and this one are the same, don't log it
912 * just count it instead, unless it was more than one second
916 if( last_series_error_time + ( 1000 * 1 ) > time_now )
924 * A new error, or the same one more than 10sec since the last one
925 * did we have any of the same counted up?
927 if( last_error_count > 0 )
930 * Print out the last error to ensure context for the last
935 error_handler( last_string );
937 hb_log( "%s", last_string );
940 if( last_error_count > 1 )
943 * Only print out the repeat message for more than 2 of the
944 * same, since we just printed out two of them already.
946 snprintf( rep_string, 180, "Last error repeated %d times",
947 last_error_count - 1 );
951 error_handler( rep_string );
953 hb_log( "%s", rep_string );
957 last_error_count = 0;
960 last_series_error_time = time_now;
962 strcpy( last_string, string );
965 * Got the error in a single string, send it off to be dispatched.
969 error_handler( string );
971 hb_log( "%s", string );
977 void hb_register_error_handler( hb_error_handler_t * handler )
979 error_handler = handler;
982 /**********************************************************************
984 **********************************************************************
986 *********************************************************************/
987 hb_title_t * hb_title_init( char * path, int index )
991 t = calloc( sizeof( hb_title_t ), 1 );
994 t->list_audio = hb_list_init();
995 t->list_chapter = hb_list_init();
996 t->list_subtitle = hb_list_init();
997 t->list_attachment = hb_list_init();
998 strcat( t->path, path );
999 // default to decoding mpeg2
1001 t->video_codec = WORK_DECMPEG2;
1006 /**********************************************************************
1008 **********************************************************************
1010 *********************************************************************/
1011 void hb_title_close( hb_title_t ** _t )
1013 hb_title_t * t = *_t;
1015 hb_chapter_t * chapter;
1016 hb_subtitle_t * subtitle;
1017 hb_attachment_t * attachment;
1019 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
1021 hb_list_rem( t->list_audio, audio );
1024 hb_list_close( &t->list_audio );
1026 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
1028 hb_list_rem( t->list_chapter, chapter );
1031 hb_list_close( &t->list_chapter );
1033 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
1035 hb_list_rem( t->list_subtitle, subtitle );
1036 if ( subtitle->extradata )
1038 free( subtitle->extradata );
1039 subtitle->extradata = NULL;
1043 hb_list_close( &t->list_subtitle );
1045 while( ( attachment = hb_list_item( t->list_attachment, 0 ) ) )
1047 hb_list_rem( t->list_attachment, attachment );
1048 if ( attachment->name )
1050 free( attachment->name );
1051 attachment->name = NULL;
1053 if ( attachment->data )
1055 free( attachment->data );
1056 attachment->data = NULL;
1060 hb_list_close( &t->list_attachment );
1064 if( t->metadata->coverart )
1066 free( t->metadata->coverart );
1068 free( t->metadata );
1075 /**********************************************************************
1077 **********************************************************************
1079 *********************************************************************/
1080 void hb_filter_close( hb_filter_object_t ** _f )
1082 hb_filter_object_t * f = *_f;
1084 f->close( f->private_data );
1089 free( f->settings );
1095 /**********************************************************************
1097 **********************************************************************
1099 *********************************************************************/
1100 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
1102 hb_audio_t *audio = NULL;
1106 audio = calloc(1, sizeof(*audio));
1107 memcpy(audio, src, sizeof(*audio));
1112 /**********************************************************************
1114 **********************************************************************
1116 *********************************************************************/
1117 void hb_audio_config_init(hb_audio_config_t * audiocfg)
1119 /* Set read only paramaters to invalid values */
1120 audiocfg->in.codec = 0xDEADBEEF;
1121 audiocfg->in.bitrate = -1;
1122 audiocfg->in.samplerate = -1;
1123 audiocfg->in.channel_layout = 0;
1124 audiocfg->in.version = 0;
1125 audiocfg->in.mode = 0;
1126 audiocfg->flags.ac3 = 0;
1127 audiocfg->lang.description[0] = 0;
1128 audiocfg->lang.simple[0] = 0;
1129 audiocfg->lang.iso639_2[0] = 0;
1131 /* Initalize some sensable defaults */
1132 audiocfg->in.track = audiocfg->out.track = 0;
1133 audiocfg->out.codec = HB_ACODEC_FAAC;
1134 audiocfg->out.bitrate = 128;
1135 audiocfg->out.samplerate = 44100;
1136 audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
1137 audiocfg->out.dynamic_range_compression = 0;
1138 audiocfg->out.name = NULL;
1141 /**********************************************************************
1143 **********************************************************************
1145 *********************************************************************/
1146 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
1148 hb_title_t *title = job->title;
1151 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
1158 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
1160 /* This most likely means the client didn't call hb_audio_config_init
1166 /* Really shouldn't ignore the passed out track, but there is currently no
1167 * way to handle duplicates or out-of-order track numbers.
1169 audio->config.out.track = hb_list_count(job->list_audio) + 1;
1170 audio->config.out.codec = audiocfg->out.codec;
1171 if( (audiocfg->out.codec & HB_ACODEC_MASK) == audio->config.in.codec &&
1172 (audiocfg->out.codec & HB_ACODEC_PASS_FLAG ) )
1174 /* Pass-through, copy from input. */
1175 audio->config.out.samplerate = audio->config.in.samplerate;
1176 audio->config.out.bitrate = audio->config.in.bitrate;
1177 audio->config.out.dynamic_range_compression = 0;
1178 audio->config.out.mixdown = 0;
1182 /* Non pass-through, use what is given. */
1183 audio->config.out.codec &= ~HB_ACODEC_PASS_FLAG;
1184 audio->config.out.samplerate = audiocfg->out.samplerate;
1185 audio->config.out.bitrate = audiocfg->out.bitrate;
1186 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
1187 audio->config.out.mixdown = audiocfg->out.mixdown;
1190 hb_list_add(job->list_audio, audio);
1194 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
1196 hb_audio_t *audio = NULL;
1198 if( (audio = hb_list_item(list, i)) )
1199 return &(audio->config);
1204 /**********************************************************************
1206 **********************************************************************
1208 *********************************************************************/
1209 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
1211 hb_subtitle_t *subtitle = NULL;
1215 subtitle = calloc(1, sizeof(*subtitle));
1216 memcpy(subtitle, src, sizeof(*subtitle));
1217 if ( src->extradata )
1219 subtitle->extradata = malloc( src->extradata_size );
1220 memcpy( subtitle->extradata, src->extradata, src->extradata_size );
1226 /**********************************************************************
1228 **********************************************************************
1230 *********************************************************************/
1231 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
1233 hb_title_t *title = job->title;
1234 hb_subtitle_t *subtitle;
1236 subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
1237 if( subtitle == NULL )
1242 subtitle->config = *subtitlecfg;
1243 hb_list_add(job->list_subtitle, subtitle);
1247 int hb_srt_add( const hb_job_t * job,
1248 const hb_subtitle_config_t * subtitlecfg,
1251 hb_subtitle_t *subtitle;
1252 iso639_lang_t *language = NULL;
1255 subtitle = calloc( 1, sizeof( *subtitle ) );
1257 subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
1258 subtitle->format = TEXTSUB;
1259 subtitle->source = SRTSUB;
1261 language = lang_for_code2( lang );
1266 strcpy( subtitle->lang, language->eng_name );
1267 strncpy( subtitle->iso639_2, lang, 4 );
1269 subtitle->config = *subtitlecfg;
1270 subtitle->config.dest = PASSTHRUSUB;
1272 hb_list_add(job->list_subtitle, subtitle);
1278 char * hb_strdup_printf( char * fmt, ... )
1286 str = malloc( size );
1292 /* Try to print in the allocated space. */
1293 va_start( ap, fmt );
1294 len = vsnprintf( str, size, fmt, ap );
1297 /* If that worked, return the string. */
1298 if ( len > -1 && len < size )
1303 /* Else try again with more space. */
1304 if ( len > -1 ) /* glibc 2.1 */
1305 size = len + 1; /* precisely what is needed */
1306 else /* glibc 2.0 */
1307 size *= 2; /* twice the old size */
1308 tmp = realloc( str, size );
1319 /**********************************************************************
1320 * hb_attachment_copy
1321 **********************************************************************
1323 *********************************************************************/
1324 hb_attachment_t *hb_attachment_copy(const hb_attachment_t *src)
1326 hb_attachment_t *attachment = NULL;
1330 attachment = calloc(1, sizeof(*attachment));
1331 memcpy(attachment, src, sizeof(*attachment));
1334 attachment->name = strdup( src->name );
1338 attachment->data = malloc( src->size );
1339 memcpy( attachment->data, src->data, src->size );
1345 /**********************************************************************
1347 **********************************************************************
1348 * Converts a YCbCr pixel to an RGB pixel.
1350 * This conversion is lossy (due to rounding and clamping).
1353 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
1354 *********************************************************************/
1355 int hb_yuv2rgb(int yuv)
1360 y = (yuv >> 16) & 0xff;
1361 Cb = (yuv >> 8) & 0xff;
1364 r = 1.164 * (y - 16) + 2.018 * (Cb - 128);
1365 g = 1.164 * (y - 16) - 0.813 * (Cr - 128) - 0.391 * (Cb - 128);
1366 b = 1.164 * (y - 16) + 1.596 * (Cr - 128);
1368 r = (r < 0) ? 0 : r;
1369 g = (g < 0) ? 0 : g;
1370 b = (b < 0) ? 0 : b;
1372 r = (r > 255) ? 255 : r;
1373 g = (g > 255) ? 255 : g;
1374 b = (b > 255) ? 255 : b;
1376 return (r << 16) | (g << 8) | b;
1379 /**********************************************************************
1381 **********************************************************************
1382 * Converts an RGB pixel to a YCbCr pixel.
1384 * This conversion is lossy (due to rounding and clamping).
1387 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
1388 *********************************************************************/
1389 int hb_rgb2yuv(int rgb)
1394 r = (rgb >> 16) & 0xff;
1395 g = (rgb >> 8) & 0xff;
1398 y = 16. + ( 0.257 * r) + (0.504 * g) + (0.098 * b);
1399 Cb = 128. + (-0.148 * r) - (0.291 * g) + (0.439 * b);
1400 Cr = 128. + ( 0.439 * r) - (0.368 * g) - (0.071 * b);
1402 y = (y < 0) ? 0 : y;
1403 Cb = (Cb < 0) ? 0 : Cb;
1404 Cr = (Cr < 0) ? 0 : Cr;
1406 y = (y > 255) ? 255 : y;
1407 Cb = (Cb > 255) ? 255 : Cb;
1408 Cr = (Cr > 255) ? 255 : Cr;
1410 return (y << 16) | (Cb << 8) | Cr;
1413 const char * hb_subsource_name( int source )