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;
196 *high = 96 * channels;
202 case HB_ACODEC_VORBIS:
203 *high = channels * 80;
204 if (samplerate > 24000)
208 // Vorbis minimum is around 30kbps/ch for 6ch
209 // at rates > 24k (32k/44.1k/48k)
210 *low = 32 * channels;
211 *high = 128 * channels;
215 // Allow 24kbps mono and 48kbps stereo at rates > 24k
217 *low = 24 * channels;
218 if (samplerate > 32000)
219 *high = channels * 224;
221 *high = channels * 160;
226 *low = channels * 16;
227 *high = 80 * channels;
232 *low = hb_audio_bitrates[0].rate;
233 if (samplerate > 24000)
240 *low = hb_audio_bitrates[0].rate;
241 *high = hb_audio_bitrates[hb_audio_bitrates_count-1].rate;
246 // Given an input bitrate, sanitize it. Check low and high limits and
247 // make sure it is in the set of allowed bitrates.
248 int hb_get_best_audio_bitrate( uint32_t codec, int bitrate, int samplerate, int mixdown)
252 hb_get_audio_bitrate_limits(codec, samplerate, mixdown, &low, &high);
257 bitrate = hb_find_closest_audio_bitrate(bitrate);
261 // Get the default bitrate for a given codec/samplerate/mixdown triplet.
262 int hb_get_default_audio_bitrate( uint32_t codec, int samplerate, int mixdown )
264 int bitrate, channels;
267 channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(mixdown);
269 // Min bitrate is established such that we get good quality
270 // audio as a minimum.
271 sr_shift = (samplerate <= 24000) ? 1 : 0;
278 else if (channels <= 2)
284 bitrate = channels * 80;
286 bitrate >>= sr_shift;
287 bitrate = hb_get_best_audio_bitrate( codec, bitrate, samplerate, mixdown );
291 int hb_get_best_mixdown( uint32_t codec, int layout, int mixdown )
296 if (codec & HB_ACODEC_PASS_FLAG)
298 // Audio pass-thru. No mixdown.
301 switch (layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK)
303 // stereo input or something not handled below
305 case HB_INPUT_CH_LAYOUT_STEREO:
306 // mono gets mixed up to stereo & more than stereo gets mixed down
307 best_mixdown = HB_AMIXDOWN_STEREO;
311 case HB_INPUT_CH_LAYOUT_MONO:
312 // everything else passes through
313 best_mixdown = HB_AMIXDOWN_MONO;
316 // dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input
317 // the A52 flags don't allow for a way to distinguish between DPL1 and
318 // DPL2 on a DVD so we always assume a DPL1 source for A52_DOLBY.
319 case HB_INPUT_CH_LAYOUT_DOLBY:
320 best_mixdown = HB_AMIXDOWN_DOLBY;
323 // 4 channel discrete
324 case HB_INPUT_CH_LAYOUT_2F2R:
325 case HB_INPUT_CH_LAYOUT_3F1R:
326 // a52dec and libdca can't upmix to 6ch,
327 // so we must downmix these.
328 best_mixdown = HB_AMIXDOWN_DOLBYPLII;
331 // 5, 6, 7, or 8 channel discrete
332 case HB_INPUT_CH_LAYOUT_4F2R:
333 case HB_INPUT_CH_LAYOUT_3F4R:
334 case HB_INPUT_CH_LAYOUT_3F2R:
335 if ( ! ( layout & HB_INPUT_CH_LAYOUT_HAS_LFE ) )
337 // we don't do 5 channel discrete so mixdown to DPLII
338 // a52dec and libdca can't upmix to 6ch,
339 // so we must downmix this.
340 best_mixdown = HB_AMIXDOWN_DOLBYPLII;
347 best_mixdown = HB_AMIXDOWN_DOLBYPLII;
351 best_mixdown = HB_AMIXDOWN_6CH;
357 // return the best that is not greater than the requested mixdown
358 // 0 means the caller requested the best available mixdown
359 if( best_mixdown > mixdown && mixdown != 0 )
360 best_mixdown = mixdown;
365 int hb_get_default_mixdown( uint32_t codec, int layout )
370 // the AC3 encoder defaults to the best mixdown up to 6-channel
372 mixdown = HB_AMIXDOWN_6CH;
374 // other encoders default to the best mixdown up to DPLII
376 mixdown = HB_AMIXDOWN_DOLBYPLII;
379 // return the best available mixdown up to the selected default
380 return hb_get_best_mixdown( codec, layout, mixdown );
383 /**********************************************************************
385 **********************************************************************
386 * Given a numerator (num) and a denominator (den), reduce them to an
387 * equivalent fraction and store the result in x and y.
388 *********************************************************************/
389 void hb_reduce( int *x, int *y, int num, int den )
391 // find the greatest common divisor of num & den by Euclid's algorithm
392 int n = num, d = den;
400 // at this point n is the gcd. if it's non-zero remove it from num
401 // and den. Otherwise just return the original values.
414 /**********************************************************************
416 **********************************************************************
417 * Given the output width (if HB_KEEP_WIDTH) or height
418 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
419 * correct height or width in order to respect the DVD aspect ratio
420 *********************************************************************/
421 void hb_fix_aspect( hb_job_t * job, int keep )
423 hb_title_t * title = job->title;
429 /* don't do anything unless the title has complete size info */
430 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
432 hb_log( "hb_fix_aspect: incomplete info for title %d: "
433 "height = %d, width = %d, aspect = %.3f",
434 title->index, title->height, title->width, title->aspect );
438 // min_width and min_height should be multiples of modulus
441 modulus = job->modulus ? job->modulus : 16;
443 for( i = 0; i < 4; i++ )
445 // Sanity check crop values are zero or positive multiples of 2
449 job->crop[i] = MIN( EVEN( job->crop[i] ), EVEN( ( title->height / 2 ) - ( min_height / 2 ) ) );
450 job->crop[i] = MAX( 0, job->crop[i] );
455 job->crop[i] = MIN( EVEN( job->crop[i] ), EVEN( ( title->width / 2 ) - ( min_width / 2 ) ) );
456 job->crop[i] = MAX( 0, job->crop[i] );
460 double par = (double)title->width / ( (double)title->height * title->aspect );
461 double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
462 (double)( title->width - job->crop[2] - job->crop[3] );
463 double ar = par * cropped_sar;
465 // Dimensions must be greater than minimum and multiple of modulus
466 if( keep == HB_KEEP_WIDTH )
468 job->width = MULTIPLE_MOD( job->width, modulus );
469 job->width = MAX( min_width, job->width );
470 job->height = MULTIPLE_MOD( (uint64_t)( (double)job->width * ar ), modulus );
471 job->height = MAX( min_height, job->height );
475 job->height = MULTIPLE_MOD( job->height, modulus );
476 job->height = MAX( min_height, job->height );
477 job->width = MULTIPLE_MOD( (uint64_t)( (double)job->height / ar ), modulus );
478 job->width = MAX( min_width, job->width );
482 /**********************************************************************
484 **********************************************************************
486 *********************************************************************/
487 int hb_calc_bitrate( hb_job_t * job, int size )
489 int64_t avail = (int64_t) size * 1024 * 1024;
492 int samples_per_frame;
495 hb_title_t * title = job->title;
496 hb_chapter_t * chapter;
499 /* How many overhead bytes are used for each frame
511 /* Get the duration in seconds */
513 for( i = job->chapter_start; i <= job->chapter_end; i++ )
515 chapter = hb_list_item( title->list_chapter, i - 1 );
516 length += chapter->duration;
523 hb_interjob_t * interjob = hb_interjob_get( job->h );
524 avail = job->vbitrate * 125 * length;
525 avail += length * interjob->vrate * overhead / interjob->vrate_base;
529 avail -= length * job->vrate * overhead / job->vrate_base;
536 for( i = 0; i < hb_list_count(job->list_audio); i++ )
540 audio = hb_list_item( job->list_audio, i);
542 /* How many audio samples we put in each frame */
543 switch( audio->config.out.codec )
546 case HB_ACODEC_CA_AAC:
547 case HB_ACODEC_VORBIS:
548 samples_per_frame = 1024;
551 samples_per_frame = 1152;
553 case HB_ACODEC_AC3_PASS:
554 case HB_ACODEC_DCA_PASS:
557 samples_per_frame = 1536;
563 if( audio->config.out.codec == HB_ACODEC_AC3_PASS ||
564 audio->config.out.codec == HB_ACODEC_DCA_PASS)
567 * For pass through we take the bitrate from the input audio
568 * bitrate as we are simply passing it through.
570 abitrate = audio->config.in.bitrate / 8;
575 * Where we are transcoding the audio we use the destination
578 abitrate = audio->config.out.bitrate * 1000 / 8;
580 avail -= length * abitrate;
583 avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
592 return ( avail / ( 125 * length ) );
595 /**********************************************************************
596 * hb_list implementation
597 **********************************************************************
598 * Basic and slow, but enough for what we need
599 *********************************************************************/
601 #define HB_LIST_DEFAULT_SIZE 20
605 /* Pointers to items in the list */
608 /* How many (void *) allocated in 'items' */
611 /* How many valid pointers in 'items' */
615 /**********************************************************************
617 **********************************************************************
618 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
619 *********************************************************************/
620 hb_list_t * hb_list_init()
624 l = calloc( sizeof( hb_list_t ), 1 );
625 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
626 l->items_alloc = HB_LIST_DEFAULT_SIZE;
631 /**********************************************************************
633 **********************************************************************
634 * Returns the number of items currently in the list
635 *********************************************************************/
636 int hb_list_count( hb_list_t * l )
638 return l->items_count;
641 /**********************************************************************
643 **********************************************************************
644 * Adds an item at the end of the list, making it bigger if necessary.
645 * Can safely be called with a NULL pointer to add, it will be ignored.
646 *********************************************************************/
647 void hb_list_add( hb_list_t * l, void * p )
654 if( l->items_count == l->items_alloc )
656 /* We need a bigger boat */
657 l->items_alloc += HB_LIST_DEFAULT_SIZE;
658 l->items = realloc( l->items,
659 l->items_alloc * sizeof( void * ) );
662 l->items[l->items_count] = p;
666 /**********************************************************************
668 **********************************************************************
669 * Remove an item from the list. Bad things will happen if called
670 * with a NULL pointer or if the item is not in the list.
671 *********************************************************************/
672 void hb_list_rem( hb_list_t * l, void * p )
676 /* Find the item in the list */
677 for( i = 0; i < l->items_count; i++ )
679 if( l->items[i] == p )
685 /* Shift all items after it sizeof( void * ) bytes earlier */
686 memmove( &l->items[i], &l->items[i+1],
687 ( l->items_count - i - 1 ) * sizeof( void * ) );
692 /**********************************************************************
694 **********************************************************************
695 * Returns item at position i, or NULL if there are not that many
697 *********************************************************************/
698 void * hb_list_item( hb_list_t * l, int i )
700 if( i < 0 || i >= l->items_count )
708 /**********************************************************************
710 **********************************************************************
711 * Assuming all items are of type hb_buffer_t, returns the total
712 * number of bytes in the list
713 *********************************************************************/
714 int hb_list_bytes( hb_list_t * l )
721 for( i = 0; i < hb_list_count( l ); i++ )
723 buf = hb_list_item( l, i );
724 ret += buf->size - buf->cur;
730 /**********************************************************************
732 **********************************************************************
733 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
734 * the list to <dst>, keeping the list unmodified.
735 *********************************************************************/
736 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
743 for( i = 0, copied = 0; copied < size; i++ )
745 buf = hb_list_item( l, i );
746 copying = MIN( buf->size - buf->cur, size - copied );
747 memcpy( &dst[copied], &buf->data[buf->cur], copying );
752 /**********************************************************************
754 **********************************************************************
755 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
756 * the list to <dst>. What's copied is removed from the list.
757 * The variable pointed by <pts> is set to the PTS of the buffer the
758 * first byte has been got from.
759 * The variable pointed by <pos> is set to the position of that byte
761 *********************************************************************/
762 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
763 uint64_t * pts, uint64_t * pos )
770 /* So we won't have to deal with NULL pointers */
771 uint64_t dummy1, dummy2;
773 if( !pts ) pts = &dummy1;
774 if( !pos ) pos = &dummy2;
776 for( copied = 0, has_pts = 0; copied < size; )
778 buf = hb_list_item( l, 0 );
779 copying = MIN( buf->size - buf->cur, size - copied );
780 memcpy( &dst[copied], &buf->data[buf->cur], copying );
790 if( buf->cur >= buf->size )
792 hb_list_rem( l, buf );
793 hb_buffer_close( &buf );
800 /**********************************************************************
802 **********************************************************************
803 * Assuming all items are of type hb_buffer_t, close them all and
805 *********************************************************************/
806 void hb_list_empty( hb_list_t ** _l )
811 while( ( b = hb_list_item( l, 0 ) ) )
814 hb_buffer_close( &b );
820 /**********************************************************************
822 **********************************************************************
823 * Free memory allocated by hb_list_init. Does NOT free contents of
824 * items still in the list.
825 *********************************************************************/
826 void hb_list_close( hb_list_t ** _l )
836 /**********************************************************************
838 **********************************************************************
839 * If verbose mode is one, print message with timestamp. Messages
840 * longer than 180 characters are stripped ;p
841 *********************************************************************/
842 void hb_log( char * log, ... )
844 char string[362]; /* 360 chars + \n + \0 */
849 if( !getenv( "HB_DEBUG" ) )
851 /* We don't want to print it */
857 now = localtime( &_now );
858 sprintf( string, "[%02d:%02d:%02d] ",
859 now->tm_hour, now->tm_min, now->tm_sec );
861 /* Convert the message to a string */
862 va_start( args, log );
863 vsnprintf( string + 11, 349, log, args );
866 /* Add the end of line */
867 strcat( string, "\n" );
870 fprintf( stderr, "%s", string );
873 int global_verbosity_level; //Necessary for hb_deep_log
874 /**********************************************************************
876 **********************************************************************
877 * If verbose mode is >= level, print message with timestamp. Messages
878 * longer than 360 characters are stripped ;p
879 *********************************************************************/
880 void hb_deep_log( hb_debug_level_t level, char * log, ... )
882 char string[362]; /* 360 chars + \n + \0 */
887 if( global_verbosity_level < level )
895 now = localtime( &_now );
896 sprintf( string, "[%02d:%02d:%02d] ",
897 now->tm_hour, now->tm_min, now->tm_sec );
899 /* Convert the message to a string */
900 va_start( args, log );
901 vsnprintf( string + 11, 349, log, args );
904 /* Add the end of line */
905 strcat( string, "\n" );
908 fprintf( stderr, "%s", string );
911 /**********************************************************************
913 **********************************************************************
914 * Using whatever output is available display this error.
915 *********************************************************************/
916 void hb_error( char * log, ... )
918 char string[181]; /* 180 chars + \0 */
919 char rep_string[181];
920 static char last_string[181];
921 static int last_error_count = 0;
922 static uint64_t last_series_error_time = 0;
923 static hb_lock_t *mutex = 0;
927 /* Convert the message to a string */
928 va_start( args, log );
929 vsnprintf( string, 180, log, args );
934 mutex = hb_lock_init();
939 time_now = hb_get_date();
941 if( strcmp( string, last_string) == 0 )
944 * The last error and this one are the same, don't log it
945 * just count it instead, unless it was more than one second
949 if( last_series_error_time + ( 1000 * 1 ) > time_now )
957 * A new error, or the same one more than 10sec since the last one
958 * did we have any of the same counted up?
960 if( last_error_count > 0 )
963 * Print out the last error to ensure context for the last
968 error_handler( last_string );
970 hb_log( "%s", last_string );
973 if( last_error_count > 1 )
976 * Only print out the repeat message for more than 2 of the
977 * same, since we just printed out two of them already.
979 snprintf( rep_string, 180, "Last error repeated %d times",
980 last_error_count - 1 );
984 error_handler( rep_string );
986 hb_log( "%s", rep_string );
990 last_error_count = 0;
993 last_series_error_time = time_now;
995 strcpy( last_string, string );
998 * Got the error in a single string, send it off to be dispatched.
1002 error_handler( string );
1004 hb_log( "%s", string );
1010 void hb_register_error_handler( hb_error_handler_t * handler )
1012 error_handler = handler;
1015 /**********************************************************************
1017 **********************************************************************
1019 *********************************************************************/
1020 hb_title_t * hb_title_init( char * path, int index )
1024 t = calloc( sizeof( hb_title_t ), 1 );
1027 t->list_audio = hb_list_init();
1028 t->list_chapter = hb_list_init();
1029 t->list_subtitle = hb_list_init();
1030 t->list_attachment = hb_list_init();
1031 strcat( t->path, path );
1032 // default to decoding mpeg2
1034 t->video_codec = WORK_DECMPEG2;
1039 /**********************************************************************
1041 **********************************************************************
1043 *********************************************************************/
1044 void hb_title_close( hb_title_t ** _t )
1046 hb_title_t * t = *_t;
1048 hb_chapter_t * chapter;
1049 hb_subtitle_t * subtitle;
1050 hb_attachment_t * attachment;
1052 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
1054 hb_list_rem( t->list_audio, audio );
1057 hb_list_close( &t->list_audio );
1059 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
1061 hb_list_rem( t->list_chapter, chapter );
1064 hb_list_close( &t->list_chapter );
1066 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
1068 hb_list_rem( t->list_subtitle, subtitle );
1069 if ( subtitle->extradata )
1071 free( subtitle->extradata );
1072 subtitle->extradata = NULL;
1076 hb_list_close( &t->list_subtitle );
1078 while( ( attachment = hb_list_item( t->list_attachment, 0 ) ) )
1080 hb_list_rem( t->list_attachment, attachment );
1081 if ( attachment->name )
1083 free( attachment->name );
1084 attachment->name = NULL;
1086 if ( attachment->data )
1088 free( attachment->data );
1089 attachment->data = NULL;
1093 hb_list_close( &t->list_attachment );
1097 if( t->metadata->coverart )
1099 free( t->metadata->coverart );
1101 free( t->metadata );
1108 /**********************************************************************
1110 **********************************************************************
1112 *********************************************************************/
1113 void hb_filter_close( hb_filter_object_t ** _f )
1115 hb_filter_object_t * f = *_f;
1117 f->close( f->private_data );
1122 free( f->settings );
1128 /**********************************************************************
1130 **********************************************************************
1132 *********************************************************************/
1133 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
1135 hb_audio_t *audio = NULL;
1139 audio = calloc(1, sizeof(*audio));
1140 memcpy(audio, src, sizeof(*audio));
1145 /**********************************************************************
1147 **********************************************************************
1149 *********************************************************************/
1150 void hb_audio_config_init(hb_audio_config_t * audiocfg)
1152 /* Set read only paramaters to invalid values */
1153 audiocfg->in.codec = 0xDEADBEEF;
1154 audiocfg->in.bitrate = -1;
1155 audiocfg->in.samplerate = -1;
1156 audiocfg->in.channel_layout = 0;
1157 audiocfg->in.version = 0;
1158 audiocfg->in.mode = 0;
1159 audiocfg->flags.ac3 = 0;
1160 audiocfg->lang.description[0] = 0;
1161 audiocfg->lang.simple[0] = 0;
1162 audiocfg->lang.iso639_2[0] = 0;
1164 /* Initalize some sensable defaults */
1165 audiocfg->in.track = audiocfg->out.track = 0;
1166 audiocfg->out.codec = HB_ACODEC_FAAC;
1167 audiocfg->out.bitrate = 128;
1168 audiocfg->out.samplerate = 44100;
1169 audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
1170 audiocfg->out.dynamic_range_compression = 0;
1171 audiocfg->out.name = NULL;
1174 /**********************************************************************
1176 **********************************************************************
1178 *********************************************************************/
1179 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
1181 hb_title_t *title = job->title;
1184 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
1191 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
1193 /* This most likely means the client didn't call hb_audio_config_init
1199 /* Really shouldn't ignore the passed out track, but there is currently no
1200 * way to handle duplicates or out-of-order track numbers.
1202 audio->config.out.track = hb_list_count(job->list_audio) + 1;
1203 audio->config.out.codec = audiocfg->out.codec;
1204 if( (audiocfg->out.codec & HB_ACODEC_MASK) == audio->config.in.codec &&
1205 (audiocfg->out.codec & HB_ACODEC_PASS_FLAG ) )
1207 /* Pass-through, copy from input. */
1208 audio->config.out.samplerate = audio->config.in.samplerate;
1209 audio->config.out.bitrate = audio->config.in.bitrate;
1210 audio->config.out.dynamic_range_compression = 0;
1211 audio->config.out.mixdown = 0;
1215 /* Non pass-through, use what is given. */
1216 audio->config.out.codec &= ~HB_ACODEC_PASS_FLAG;
1217 audio->config.out.samplerate = audiocfg->out.samplerate;
1218 audio->config.out.bitrate = audiocfg->out.bitrate;
1219 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
1220 audio->config.out.mixdown = audiocfg->out.mixdown;
1223 hb_list_add(job->list_audio, audio);
1227 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
1229 hb_audio_t *audio = NULL;
1231 if( (audio = hb_list_item(list, i)) )
1232 return &(audio->config);
1237 /**********************************************************************
1239 **********************************************************************
1241 *********************************************************************/
1242 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
1244 hb_subtitle_t *subtitle = NULL;
1248 subtitle = calloc(1, sizeof(*subtitle));
1249 memcpy(subtitle, src, sizeof(*subtitle));
1250 if ( src->extradata )
1252 subtitle->extradata = malloc( src->extradata_size );
1253 memcpy( subtitle->extradata, src->extradata, src->extradata_size );
1259 /**********************************************************************
1261 **********************************************************************
1263 *********************************************************************/
1264 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
1266 hb_title_t *title = job->title;
1267 hb_subtitle_t *subtitle;
1269 subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
1270 if( subtitle == NULL )
1275 subtitle->config = *subtitlecfg;
1276 hb_list_add(job->list_subtitle, subtitle);
1280 int hb_srt_add( const hb_job_t * job,
1281 const hb_subtitle_config_t * subtitlecfg,
1284 hb_subtitle_t *subtitle;
1285 iso639_lang_t *language = NULL;
1288 subtitle = calloc( 1, sizeof( *subtitle ) );
1290 subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
1291 subtitle->format = TEXTSUB;
1292 subtitle->source = SRTSUB;
1294 language = lang_for_code2( lang );
1299 strcpy( subtitle->lang, language->eng_name );
1300 strncpy( subtitle->iso639_2, lang, 4 );
1302 subtitle->config = *subtitlecfg;
1303 subtitle->config.dest = PASSTHRUSUB;
1305 hb_list_add(job->list_subtitle, subtitle);
1311 char * hb_strdup_printf( char * fmt, ... )
1319 str = malloc( size );
1325 /* Try to print in the allocated space. */
1326 va_start( ap, fmt );
1327 len = vsnprintf( str, size, fmt, ap );
1330 /* If that worked, return the string. */
1331 if ( len > -1 && len < size )
1336 /* Else try again with more space. */
1337 if ( len > -1 ) /* glibc 2.1 */
1338 size = len + 1; /* precisely what is needed */
1339 else /* glibc 2.0 */
1340 size *= 2; /* twice the old size */
1341 tmp = realloc( str, size );
1352 /**********************************************************************
1353 * hb_attachment_copy
1354 **********************************************************************
1356 *********************************************************************/
1357 hb_attachment_t *hb_attachment_copy(const hb_attachment_t *src)
1359 hb_attachment_t *attachment = NULL;
1363 attachment = calloc(1, sizeof(*attachment));
1364 memcpy(attachment, src, sizeof(*attachment));
1367 attachment->name = strdup( src->name );
1371 attachment->data = malloc( src->size );
1372 memcpy( attachment->data, src->data, src->size );
1378 /**********************************************************************
1380 **********************************************************************
1381 * Converts a YCbCr pixel to an RGB pixel.
1383 * This conversion is lossy (due to rounding and clamping).
1386 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
1387 *********************************************************************/
1388 int hb_yuv2rgb(int yuv)
1393 y = (yuv >> 16) & 0xff;
1394 Cb = (yuv >> 8) & 0xff;
1397 r = 1.164 * (y - 16) + 2.018 * (Cb - 128);
1398 g = 1.164 * (y - 16) - 0.813 * (Cr - 128) - 0.391 * (Cb - 128);
1399 b = 1.164 * (y - 16) + 1.596 * (Cr - 128);
1401 r = (r < 0) ? 0 : r;
1402 g = (g < 0) ? 0 : g;
1403 b = (b < 0) ? 0 : b;
1405 r = (r > 255) ? 255 : r;
1406 g = (g > 255) ? 255 : g;
1407 b = (b > 255) ? 255 : b;
1409 return (r << 16) | (g << 8) | b;
1412 /**********************************************************************
1414 **********************************************************************
1415 * Converts an RGB pixel to a YCbCr pixel.
1417 * This conversion is lossy (due to rounding and clamping).
1420 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
1421 *********************************************************************/
1422 int hb_rgb2yuv(int rgb)
1427 r = (rgb >> 16) & 0xff;
1428 g = (rgb >> 8) & 0xff;
1431 y = 16. + ( 0.257 * r) + (0.504 * g) + (0.098 * b);
1432 Cb = 128. + (-0.148 * r) - (0.291 * g) + (0.439 * b);
1433 Cr = 128. + ( 0.439 * r) - (0.368 * g) - (0.071 * b);
1435 y = (y < 0) ? 0 : y;
1436 Cb = (Cb < 0) ? 0 : Cb;
1437 Cr = (Cr < 0) ? 0 : Cr;
1439 y = (y > 255) ? 255 : y;
1440 Cb = (Cb > 255) ? 255 : Cb;
1441 Cr = (Cr > 255) ? 255 : Cr;
1443 return (y << 16) | (Cb << 8) | Cr;
1446 const char * hb_subsource_name( int source )