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 ) /
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 // Given an input bitrate, find closest match in the set of allowed bitrates
80 int hb_find_closest_audio_bitrate(int bitrate)
85 // result is highest rate if none found during search.
86 // rate returned will always be <= rate asked for.
87 result = hb_audio_bitrates[0].rate;
88 for (ii = hb_audio_bitrates_count-1; ii >= 0; ii--)
90 if (bitrate >= hb_audio_bitrates[ii].rate)
92 result = hb_audio_bitrates[ii].rate;
99 // Get the bitrate low and high limits for a codec/samplerate/mixdown triplet
100 // The limits have been empirically determined through testing. Max bitrates
101 // in table below. Numbers in parenthesis are the target bitrate chosen.
103 Encoder 1 channel 2 channels 6 channels
106 24kHz 86 (128) 173 (256) 460 (768)
107 48kHz 152 (160) 304 (320) 759 (768)
110 24kHz 97 (80) 177 (160) 527 (512)
111 48kHz 241 (224) 465 (448) 783 (768)
114 24kHz 146 (768) 138 (768)
115 48kHz 318 (768) 318 (768)
118 24kHz 318 (320) 318 (320) 318 (320)
119 48kHz 636 (640) 636 (640) 636 (640)
121 Core Audio (core audio api provides range of allowed bitrates)
122 24kHz 16-64 32-128 80-320
123 44.1kHz 64-320 160-768
124 48kHz 32-256 64-320 160-768
126 Core Audio (minimum limits found in testing)
132 void hb_get_audio_bitrate_limits(uint32_t codec, int samplerate, int mixdown, int *low, int *high)
136 channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(mixdown);
140 *low = 32 * channels;
141 if (samplerate > 24000)
151 case HB_ACODEC_CA_AAC:
152 if (samplerate > 44100)
154 *low = channels * 40;
163 else if (samplerate > 24000)
165 *low = channels * 32;
177 *low = channels * 16;
178 *high = channels * 64;
187 *low = 32 * channels;
188 if (samplerate > 24000)
190 *high = 160 * channels;
194 *high = 128 * channels;
200 case HB_ACODEC_VORBIS:
201 *high = channels * 80;
202 if (samplerate > 24000)
206 // Vorbis minimum is around 30kbps/ch for 6ch
207 // at rates > 24k (32k/44.1k/48k)
208 *low = 32 * channels;
209 *high = 128 * channels;
213 // Allow 24kbps mono and 48kbps stereo at rates > 24k
215 *low = 24 * channels;
216 if (samplerate > 32000)
217 *high = channels * 224;
219 *high = channels * 160;
224 *low = channels * 16;
225 *high = 80 * channels;
230 *low = hb_audio_bitrates[0].rate;
231 *high = hb_audio_bitrates[hb_audio_bitrates_count-1].rate;
236 // Given an input bitrate, sanitize it. Check low and high limits and
237 // make sure it is in the set of allowed bitrates.
238 int hb_get_best_audio_bitrate( uint32_t codec, int bitrate, int samplerate, int mixdown)
242 hb_get_audio_bitrate_limits(codec, samplerate, mixdown, &low, &high);
247 bitrate = hb_find_closest_audio_bitrate(bitrate);
251 // Get the default bitrate for a given codec/samplerate/mixdown triplet.
252 int hb_get_default_audio_bitrate( uint32_t codec, int samplerate, int mixdown )
254 int bitrate, channels;
257 channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(mixdown);
259 // Min bitrate is established such that we get good quality
260 // audio as a minimum.
261 sr_shift = (samplerate <= 24000) ? 1 : 0;
268 else if (channels <= 2)
274 bitrate = channels * 80;
276 bitrate >>= sr_shift;
277 bitrate = hb_get_best_audio_bitrate( codec, bitrate, samplerate, mixdown );
281 int hb_get_best_mixdown( uint32_t codec, int layout, int mixdown )
286 if (codec & HB_ACODEC_PASS_FLAG)
288 // Audio pass-thru. No mixdown.
291 switch (layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK)
293 // stereo input or something not handled below
295 case HB_INPUT_CH_LAYOUT_STEREO:
296 // mono gets mixed up to stereo & more than stereo gets mixed down
297 best_mixdown = HB_AMIXDOWN_STEREO;
301 case HB_INPUT_CH_LAYOUT_MONO:
302 // everything else passes through
303 best_mixdown = 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 best_mixdown = HB_AMIXDOWN_DOLBY;
313 // 4 channel discrete
314 case HB_INPUT_CH_LAYOUT_2F2R:
315 case HB_INPUT_CH_LAYOUT_3F1R:
316 // a52dec and libdca can't upmix to 6ch,
317 // so we must downmix these.
318 best_mixdown = HB_AMIXDOWN_DOLBYPLII;
321 // 5, 6, 7, or 8 channel discrete
322 case HB_INPUT_CH_LAYOUT_4F2R:
323 case HB_INPUT_CH_LAYOUT_3F4R:
324 case HB_INPUT_CH_LAYOUT_3F2R:
325 if ( ! ( layout & HB_INPUT_CH_LAYOUT_HAS_LFE ) )
327 // we don't do 5 channel discrete so mixdown to DPLII
328 // a52dec and libdca can't upmix to 6ch,
329 // so we must downmix this.
330 best_mixdown = HB_AMIXDOWN_DOLBYPLII;
337 best_mixdown = HB_AMIXDOWN_DOLBYPLII;
341 best_mixdown = HB_AMIXDOWN_6CH;
347 // return the best that is not greater than the requested mixdown
348 // 0 means the caller requested the best available mixdown
349 if( best_mixdown > mixdown && mixdown != 0 )
350 best_mixdown = mixdown;
355 int hb_get_default_mixdown( uint32_t codec, int layout )
360 // the AC3 encoder defaults to the best mixdown up to 6-channel
362 mixdown = HB_AMIXDOWN_6CH;
364 // other encoders default to the best mixdown up to DPLII
366 mixdown = HB_AMIXDOWN_DOLBYPLII;
369 // return the best available mixdown up to the selected default
370 return hb_get_best_mixdown( codec, layout, mixdown );
373 /**********************************************************************
375 **********************************************************************
376 * Given a numerator (num) and a denominator (den), reduce them to an
377 * equivalent fraction and store the result in x and y.
378 *********************************************************************/
379 void hb_reduce( int *x, int *y, int num, int den )
381 // find the greatest common divisor of num & den by Euclid's algorithm
382 int n = num, d = den;
390 // at this point n is the gcd. if it's non-zero remove it from num
391 // and den. Otherwise just return the original values.
404 /**********************************************************************
406 **********************************************************************
407 * Given the output width (if HB_KEEP_WIDTH) or height
408 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
409 * correct height or width in order to respect the DVD aspect ratio
410 *********************************************************************/
411 void hb_fix_aspect( hb_job_t * job, int keep )
413 hb_title_t * title = job->title;
419 /* don't do anything unless the title has complete size info */
420 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
422 hb_log( "hb_fix_aspect: incomplete info for title %d: "
423 "height = %d, width = %d, aspect = %.3f",
424 title->index, title->height, title->width, title->aspect );
428 // min_width and min_height should be multiples of modulus
431 modulus = job->modulus ? job->modulus : 16;
433 for( i = 0; i < 4; i++ )
435 // Sanity check crop values are zero or positive multiples of 2
439 job->crop[i] = MIN( EVEN( job->crop[i] ), EVEN( ( title->height / 2 ) - ( min_height / 2 ) ) );
440 job->crop[i] = MAX( 0, job->crop[i] );
445 job->crop[i] = MIN( EVEN( job->crop[i] ), EVEN( ( title->width / 2 ) - ( min_width / 2 ) ) );
446 job->crop[i] = MAX( 0, job->crop[i] );
450 double par = (double)title->width / ( (double)title->height * title->aspect );
451 double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
452 (double)( title->width - job->crop[2] - job->crop[3] );
453 double ar = par * cropped_sar;
455 // Dimensions must be greater than minimum and multiple of modulus
456 if( keep == HB_KEEP_WIDTH )
458 job->width = MULTIPLE_MOD( job->width, modulus );
459 job->width = MAX( min_width, job->width );
460 job->height = MULTIPLE_MOD( (uint64_t)( (double)job->width * ar ), modulus );
461 job->height = MAX( min_height, job->height );
465 job->height = MULTIPLE_MOD( job->height, modulus );
466 job->height = MAX( min_height, job->height );
467 job->width = MULTIPLE_MOD( (uint64_t)( (double)job->height / ar ), modulus );
468 job->width = MAX( min_width, job->width );
472 /**********************************************************************
474 **********************************************************************
476 *********************************************************************/
477 int hb_calc_bitrate( hb_job_t * job, int size )
479 int64_t avail = (int64_t) size * 1024 * 1024;
482 int samples_per_frame;
485 hb_title_t * title = job->title;
486 hb_chapter_t * chapter;
489 /* How many overhead bytes are used for each frame
501 /* Get the duration in seconds */
503 for( i = job->chapter_start; i <= job->chapter_end; i++ )
505 chapter = hb_list_item( title->list_chapter, i - 1 );
506 length += chapter->duration;
513 hb_interjob_t * interjob = hb_interjob_get( job->h );
514 avail = job->vbitrate * 125 * length;
515 avail += length * interjob->vrate * overhead / interjob->vrate_base;
519 avail -= length * job->vrate * overhead / job->vrate_base;
526 for( i = 0; i < hb_list_count(job->list_audio); i++ )
530 audio = hb_list_item( job->list_audio, i);
532 /* How many audio samples we put in each frame */
533 switch( audio->config.out.codec )
536 case HB_ACODEC_CA_AAC:
537 case HB_ACODEC_VORBIS:
538 samples_per_frame = 1024;
541 samples_per_frame = 1152;
543 case HB_ACODEC_AC3_PASS:
544 case HB_ACODEC_DCA_PASS:
547 samples_per_frame = 1536;
553 if( audio->config.out.codec == HB_ACODEC_AC3_PASS ||
554 audio->config.out.codec == HB_ACODEC_DCA_PASS)
557 * For pass through we take the bitrate from the input audio
558 * bitrate as we are simply passing it through.
560 abitrate = audio->config.in.bitrate / 8;
565 * Where we are transcoding the audio we use the destination
568 abitrate = audio->config.out.bitrate * 1000 / 8;
570 avail -= length * abitrate;
573 avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
582 return ( avail / ( 125 * length ) );
585 /**********************************************************************
586 * hb_list implementation
587 **********************************************************************
588 * Basic and slow, but enough for what we need
589 *********************************************************************/
591 #define HB_LIST_DEFAULT_SIZE 20
595 /* Pointers to items in the list */
598 /* How many (void *) allocated in 'items' */
601 /* How many valid pointers in 'items' */
605 /**********************************************************************
607 **********************************************************************
608 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
609 *********************************************************************/
610 hb_list_t * hb_list_init()
614 l = calloc( sizeof( hb_list_t ), 1 );
615 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
616 l->items_alloc = HB_LIST_DEFAULT_SIZE;
621 /**********************************************************************
623 **********************************************************************
624 * Returns the number of items currently in the list
625 *********************************************************************/
626 int hb_list_count( hb_list_t * l )
628 return l->items_count;
631 /**********************************************************************
633 **********************************************************************
634 * Adds an item at the end of the list, making it bigger if necessary.
635 * Can safely be called with a NULL pointer to add, it will be ignored.
636 *********************************************************************/
637 void hb_list_add( hb_list_t * l, void * p )
644 if( l->items_count == l->items_alloc )
646 /* We need a bigger boat */
647 l->items_alloc += HB_LIST_DEFAULT_SIZE;
648 l->items = realloc( l->items,
649 l->items_alloc * sizeof( void * ) );
652 l->items[l->items_count] = p;
656 /**********************************************************************
658 **********************************************************************
659 * Remove an item from the list. Bad things will happen if called
660 * with a NULL pointer or if the item is not in the list.
661 *********************************************************************/
662 void hb_list_rem( hb_list_t * l, void * p )
666 /* Find the item in the list */
667 for( i = 0; i < l->items_count; i++ )
669 if( l->items[i] == p )
675 /* Shift all items after it sizeof( void * ) bytes earlier */
676 memmove( &l->items[i], &l->items[i+1],
677 ( l->items_count - i - 1 ) * sizeof( void * ) );
682 /**********************************************************************
684 **********************************************************************
685 * Returns item at position i, or NULL if there are not that many
687 *********************************************************************/
688 void * hb_list_item( hb_list_t * l, int i )
690 if( i < 0 || i >= l->items_count )
698 /**********************************************************************
700 **********************************************************************
701 * Assuming all items are of type hb_buffer_t, returns the total
702 * number of bytes in the list
703 *********************************************************************/
704 int hb_list_bytes( hb_list_t * l )
711 for( i = 0; i < hb_list_count( l ); i++ )
713 buf = hb_list_item( l, i );
714 ret += buf->size - buf->cur;
720 /**********************************************************************
722 **********************************************************************
723 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
724 * the list to <dst>, keeping the list unmodified.
725 *********************************************************************/
726 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
733 for( i = 0, copied = 0; copied < size; i++ )
735 buf = hb_list_item( l, i );
736 copying = MIN( buf->size - buf->cur, size - copied );
737 memcpy( &dst[copied], &buf->data[buf->cur], copying );
742 /**********************************************************************
744 **********************************************************************
745 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
746 * the list to <dst>. What's copied is removed from the list.
747 * The variable pointed by <pts> is set to the PTS of the buffer the
748 * first byte has been got from.
749 * The variable pointed by <pos> is set to the position of that byte
751 *********************************************************************/
752 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
753 uint64_t * pts, uint64_t * pos )
760 /* So we won't have to deal with NULL pointers */
761 uint64_t dummy1, dummy2;
763 if( !pts ) pts = &dummy1;
764 if( !pos ) pos = &dummy2;
766 for( copied = 0, has_pts = 0; copied < size; )
768 buf = hb_list_item( l, 0 );
769 copying = MIN( buf->size - buf->cur, size - copied );
770 memcpy( &dst[copied], &buf->data[buf->cur], copying );
780 if( buf->cur >= buf->size )
782 hb_list_rem( l, buf );
783 hb_buffer_close( &buf );
790 /**********************************************************************
792 **********************************************************************
793 * Assuming all items are of type hb_buffer_t, close them all and
795 *********************************************************************/
796 void hb_list_empty( hb_list_t ** _l )
801 while( ( b = hb_list_item( l, 0 ) ) )
804 hb_buffer_close( &b );
810 /**********************************************************************
812 **********************************************************************
813 * Free memory allocated by hb_list_init. Does NOT free contents of
814 * items still in the list.
815 *********************************************************************/
816 void hb_list_close( hb_list_t ** _l )
826 /**********************************************************************
828 **********************************************************************
829 * If verbose mode is one, print message with timestamp. Messages
830 * longer than 180 characters are stripped ;p
831 *********************************************************************/
832 void hb_log( char * log, ... )
834 char string[362]; /* 360 chars + \n + \0 */
839 if( !getenv( "HB_DEBUG" ) )
841 /* We don't want to print it */
847 now = localtime( &_now );
848 sprintf( string, "[%02d:%02d:%02d] ",
849 now->tm_hour, now->tm_min, now->tm_sec );
851 /* Convert the message to a string */
852 va_start( args, log );
853 vsnprintf( string + 11, 349, log, args );
856 /* Add the end of line */
857 strcat( string, "\n" );
860 fprintf( stderr, "%s", string );
863 int global_verbosity_level; //Necessary for hb_deep_log
864 /**********************************************************************
866 **********************************************************************
867 * If verbose mode is >= level, print message with timestamp. Messages
868 * longer than 360 characters are stripped ;p
869 *********************************************************************/
870 void hb_deep_log( hb_debug_level_t level, char * log, ... )
872 char string[362]; /* 360 chars + \n + \0 */
877 if( global_verbosity_level < level )
885 now = localtime( &_now );
886 sprintf( string, "[%02d:%02d:%02d] ",
887 now->tm_hour, now->tm_min, now->tm_sec );
889 /* Convert the message to a string */
890 va_start( args, log );
891 vsnprintf( string + 11, 349, log, args );
894 /* Add the end of line */
895 strcat( string, "\n" );
898 fprintf( stderr, "%s", string );
901 /**********************************************************************
903 **********************************************************************
904 * Using whatever output is available display this error.
905 *********************************************************************/
906 void hb_error( char * log, ... )
908 char string[181]; /* 180 chars + \0 */
909 char rep_string[181];
910 static char last_string[181];
911 static int last_error_count = 0;
912 static uint64_t last_series_error_time = 0;
913 static hb_lock_t *mutex = 0;
917 /* Convert the message to a string */
918 va_start( args, log );
919 vsnprintf( string, 180, log, args );
924 mutex = hb_lock_init();
929 time_now = hb_get_date();
931 if( strcmp( string, last_string) == 0 )
934 * The last error and this one are the same, don't log it
935 * just count it instead, unless it was more than one second
939 if( last_series_error_time + ( 1000 * 1 ) > time_now )
947 * A new error, or the same one more than 10sec since the last one
948 * did we have any of the same counted up?
950 if( last_error_count > 0 )
953 * Print out the last error to ensure context for the last
958 error_handler( last_string );
960 hb_log( "%s", last_string );
963 if( last_error_count > 1 )
966 * Only print out the repeat message for more than 2 of the
967 * same, since we just printed out two of them already.
969 snprintf( rep_string, 180, "Last error repeated %d times",
970 last_error_count - 1 );
974 error_handler( rep_string );
976 hb_log( "%s", rep_string );
980 last_error_count = 0;
983 last_series_error_time = time_now;
985 strcpy( last_string, string );
988 * Got the error in a single string, send it off to be dispatched.
992 error_handler( string );
994 hb_log( "%s", string );
1000 void hb_register_error_handler( hb_error_handler_t * handler )
1002 error_handler = handler;
1005 /**********************************************************************
1007 **********************************************************************
1009 *********************************************************************/
1010 hb_title_t * hb_title_init( char * path, int index )
1014 t = calloc( sizeof( hb_title_t ), 1 );
1017 t->list_audio = hb_list_init();
1018 t->list_chapter = hb_list_init();
1019 t->list_subtitle = hb_list_init();
1020 t->list_attachment = hb_list_init();
1021 strcat( t->path, path );
1022 // default to decoding mpeg2
1024 t->video_codec = WORK_DECMPEG2;
1029 /**********************************************************************
1031 **********************************************************************
1033 *********************************************************************/
1034 void hb_title_close( hb_title_t ** _t )
1036 hb_title_t * t = *_t;
1038 hb_chapter_t * chapter;
1039 hb_subtitle_t * subtitle;
1040 hb_attachment_t * attachment;
1042 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
1044 hb_list_rem( t->list_audio, audio );
1047 hb_list_close( &t->list_audio );
1049 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
1051 hb_list_rem( t->list_chapter, chapter );
1054 hb_list_close( &t->list_chapter );
1056 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
1058 hb_list_rem( t->list_subtitle, subtitle );
1059 if ( subtitle->extradata )
1061 free( subtitle->extradata );
1062 subtitle->extradata = NULL;
1066 hb_list_close( &t->list_subtitle );
1068 while( ( attachment = hb_list_item( t->list_attachment, 0 ) ) )
1070 hb_list_rem( t->list_attachment, attachment );
1071 if ( attachment->name )
1073 free( attachment->name );
1074 attachment->name = NULL;
1076 if ( attachment->data )
1078 free( attachment->data );
1079 attachment->data = NULL;
1083 hb_list_close( &t->list_attachment );
1087 if( t->metadata->coverart )
1089 free( t->metadata->coverart );
1091 free( t->metadata );
1098 /**********************************************************************
1100 **********************************************************************
1102 *********************************************************************/
1103 void hb_filter_close( hb_filter_object_t ** _f )
1105 hb_filter_object_t * f = *_f;
1107 f->close( f->private_data );
1112 free( f->settings );
1118 /**********************************************************************
1120 **********************************************************************
1122 *********************************************************************/
1123 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
1125 hb_audio_t *audio = NULL;
1129 audio = calloc(1, sizeof(*audio));
1130 memcpy(audio, src, sizeof(*audio));
1135 /**********************************************************************
1137 **********************************************************************
1139 *********************************************************************/
1140 void hb_audio_config_init(hb_audio_config_t * audiocfg)
1142 /* Set read only paramaters to invalid values */
1143 audiocfg->in.codec = 0xDEADBEEF;
1144 audiocfg->in.bitrate = -1;
1145 audiocfg->in.samplerate = -1;
1146 audiocfg->in.channel_layout = 0;
1147 audiocfg->in.version = 0;
1148 audiocfg->in.mode = 0;
1149 audiocfg->flags.ac3 = 0;
1150 audiocfg->lang.description[0] = 0;
1151 audiocfg->lang.simple[0] = 0;
1152 audiocfg->lang.iso639_2[0] = 0;
1154 /* Initalize some sensable defaults */
1155 audiocfg->in.track = audiocfg->out.track = 0;
1156 audiocfg->out.codec = HB_ACODEC_FAAC;
1157 audiocfg->out.bitrate = 128;
1158 audiocfg->out.samplerate = 44100;
1159 audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
1160 audiocfg->out.dynamic_range_compression = 0;
1161 audiocfg->out.name = NULL;
1164 /**********************************************************************
1166 **********************************************************************
1168 *********************************************************************/
1169 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
1171 hb_title_t *title = job->title;
1174 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
1181 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
1183 /* This most likely means the client didn't call hb_audio_config_init
1189 /* Really shouldn't ignore the passed out track, but there is currently no
1190 * way to handle duplicates or out-of-order track numbers.
1192 audio->config.out.track = hb_list_count(job->list_audio) + 1;
1193 audio->config.out.codec = audiocfg->out.codec;
1194 if( (audiocfg->out.codec & HB_ACODEC_MASK) == audio->config.in.codec &&
1195 (audiocfg->out.codec & HB_ACODEC_PASS_FLAG ) )
1197 /* Pass-through, copy from input. */
1198 audio->config.out.samplerate = audio->config.in.samplerate;
1199 audio->config.out.bitrate = audio->config.in.bitrate;
1200 audio->config.out.dynamic_range_compression = 0;
1201 audio->config.out.mixdown = 0;
1205 /* Non pass-through, use what is given. */
1206 audio->config.out.codec &= ~HB_ACODEC_PASS_FLAG;
1207 audio->config.out.samplerate = audiocfg->out.samplerate;
1208 audio->config.out.bitrate = audiocfg->out.bitrate;
1209 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
1210 audio->config.out.mixdown = audiocfg->out.mixdown;
1213 hb_list_add(job->list_audio, audio);
1217 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
1219 hb_audio_t *audio = NULL;
1221 if( (audio = hb_list_item(list, i)) )
1222 return &(audio->config);
1227 /**********************************************************************
1229 **********************************************************************
1231 *********************************************************************/
1232 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
1234 hb_subtitle_t *subtitle = NULL;
1238 subtitle = calloc(1, sizeof(*subtitle));
1239 memcpy(subtitle, src, sizeof(*subtitle));
1240 if ( src->extradata )
1242 subtitle->extradata = malloc( src->extradata_size );
1243 memcpy( subtitle->extradata, src->extradata, src->extradata_size );
1249 /**********************************************************************
1251 **********************************************************************
1253 *********************************************************************/
1254 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
1256 hb_title_t *title = job->title;
1257 hb_subtitle_t *subtitle;
1259 subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
1260 if( subtitle == NULL )
1265 subtitle->config = *subtitlecfg;
1266 hb_list_add(job->list_subtitle, subtitle);
1270 int hb_srt_add( const hb_job_t * job,
1271 const hb_subtitle_config_t * subtitlecfg,
1274 hb_subtitle_t *subtitle;
1275 iso639_lang_t *language = NULL;
1278 subtitle = calloc( 1, sizeof( *subtitle ) );
1280 subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
1281 subtitle->format = TEXTSUB;
1282 subtitle->source = SRTSUB;
1284 language = lang_for_code2( lang );
1289 strcpy( subtitle->lang, language->eng_name );
1290 strncpy( subtitle->iso639_2, lang, 4 );
1292 subtitle->config = *subtitlecfg;
1293 subtitle->config.dest = PASSTHRUSUB;
1295 hb_list_add(job->list_subtitle, subtitle);
1301 char * hb_strdup_printf( char * fmt, ... )
1309 str = malloc( size );
1315 /* Try to print in the allocated space. */
1316 va_start( ap, fmt );
1317 len = vsnprintf( str, size, fmt, ap );
1320 /* If that worked, return the string. */
1321 if ( len > -1 && len < size )
1326 /* Else try again with more space. */
1327 if ( len > -1 ) /* glibc 2.1 */
1328 size = len + 1; /* precisely what is needed */
1329 else /* glibc 2.0 */
1330 size *= 2; /* twice the old size */
1331 tmp = realloc( str, size );
1342 /**********************************************************************
1343 * hb_attachment_copy
1344 **********************************************************************
1346 *********************************************************************/
1347 hb_attachment_t *hb_attachment_copy(const hb_attachment_t *src)
1349 hb_attachment_t *attachment = NULL;
1353 attachment = calloc(1, sizeof(*attachment));
1354 memcpy(attachment, src, sizeof(*attachment));
1357 attachment->name = strdup( src->name );
1361 attachment->data = malloc( src->size );
1362 memcpy( attachment->data, src->data, src->size );
1368 /**********************************************************************
1370 **********************************************************************
1371 * Converts a YCbCr pixel to an RGB pixel.
1373 * This conversion is lossy (due to rounding and clamping).
1376 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
1377 *********************************************************************/
1378 int hb_yuv2rgb(int yuv)
1383 y = (yuv >> 16) & 0xff;
1384 Cb = (yuv >> 8) & 0xff;
1387 r = 1.164 * (y - 16) + 2.018 * (Cb - 128);
1388 g = 1.164 * (y - 16) - 0.813 * (Cr - 128) - 0.391 * (Cb - 128);
1389 b = 1.164 * (y - 16) + 1.596 * (Cr - 128);
1391 r = (r < 0) ? 0 : r;
1392 g = (g < 0) ? 0 : g;
1393 b = (b < 0) ? 0 : b;
1395 r = (r > 255) ? 255 : r;
1396 g = (g > 255) ? 255 : g;
1397 b = (b > 255) ? 255 : b;
1399 return (r << 16) | (g << 8) | b;
1402 /**********************************************************************
1404 **********************************************************************
1405 * Converts an RGB pixel to a YCbCr pixel.
1407 * This conversion is lossy (due to rounding and clamping).
1410 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
1411 *********************************************************************/
1412 int hb_rgb2yuv(int rgb)
1417 r = (rgb >> 16) & 0xff;
1418 g = (rgb >> 8) & 0xff;
1421 y = 16. + ( 0.257 * r) + (0.504 * g) + (0.098 * b);
1422 Cb = 128. + (-0.148 * r) - (0.291 * g) + (0.439 * b);
1423 Cr = 128. + ( 0.439 * r) - (0.368 * g) - (0.071 * b);
1425 y = (y < 0) ? 0 : y;
1426 Cb = (Cb < 0) ? 0 : Cb;
1427 Cr = (Cr < 0) ? 0 : Cr;
1429 y = (y > 255) ? 255 : y;
1430 Cb = (Cb > 255) ? 255 : Cb;
1431 Cr = (Cr > 255) ? 255 : Cr;
1433 return (y << 16) | (Cb << 8) | Cr;
1436 const char * hb_subsource_name( int source )