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[hb_audio_bitrates_count-1].rate;
89 for (ii = 0; ii < hb_audio_bitrates_count; 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;
113 case HB_ACODEC_CA_AAC:
114 *low = channels * 80;
115 if (samplerate <= 44100)
116 *low = channels * 64;
117 if (samplerate <= 24000)
118 *low = channels * 32;
119 *high = hb_audio_bitrates[hb_audio_bitrates_count-1].rate;
123 *low = 32 * channels;
126 else if (channels >= 2)
132 case HB_ACODEC_VORBIS:
133 *low = channels * 16;
134 *high = hb_audio_bitrates[hb_audio_bitrates_count-1].rate;
135 if (samplerate > 24000)
139 // Vorbis minimum is around 30kbps/ch for 6ch
140 // at rates > 24k (32k/44.1k/48k)
141 *low = 32 * channels;
145 // Allow 24kbps mono and 48kbps stereo at rates > 24k
147 *low = 24 * channels;
153 *low = hb_audio_bitrates[0].rate;
154 *high = hb_audio_bitrates[hb_audio_bitrates_count-1].rate;
159 // Given an input bitrate, sanitize it. Check low and high limits and
160 // make sure it is in the set of allowed bitrates.
161 int hb_get_best_audio_bitrate( uint32_t codec, int bitrate, int samplerate, int mixdown)
165 hb_get_audio_bitrate_limits(codec, samplerate, mixdown, &low, &high);
170 bitrate = hb_find_closest_audio_bitrate(bitrate);
174 // Get the default bitrate for a given codec/samplerate/mixdown triplet.
175 int hb_get_default_audio_bitrate( uint32_t codec, int samplerate, int mixdown )
177 int bitrate, channels;
180 channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(mixdown);
182 // Min bitrate is established such that we get good quality
183 // audio as a minimum.
184 sr_shift = (samplerate <= 24000) ? 1 : 0;
191 else if (channels <= 2)
197 bitrate = channels * 80;
199 bitrate >>= sr_shift;
200 bitrate = hb_get_best_audio_bitrate( codec, bitrate, samplerate, mixdown );
204 /**********************************************************************
206 **********************************************************************
207 * Given a numerator (num) and a denominator (den), reduce them to an
208 * equivalent fraction and store the result in x and y.
209 *********************************************************************/
210 void hb_reduce( int *x, int *y, int num, int den )
212 // find the greatest common divisor of num & den by Euclid's algorithm
213 int n = num, d = den;
221 // at this point n is the gcd. if it's non-zero remove it from num
222 // and den. Otherwise just return the original values.
235 /**********************************************************************
237 **********************************************************************
238 * Given the output width (if HB_KEEP_WIDTH) or height
239 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
240 * correct height or width in order to respect the DVD aspect ratio
241 *********************************************************************/
242 void hb_fix_aspect( hb_job_t * job, int keep )
244 hb_title_t * title = job->title;
250 /* don't do anything unless the title has complete size info */
251 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
253 hb_log( "hb_fix_aspect: incomplete info for title %d: "
254 "height = %d, width = %d, aspect = %.3f",
255 title->index, title->height, title->width, title->aspect );
259 // min_width and min_height should be multiples of modulus
262 modulus = job->modulus ? job->modulus : 16;
264 for( i = 0; i < 4; i++ )
266 // Sanity check crop values are zero or positive multiples of 2
270 job->crop[i] = MIN( EVEN( job->crop[i] ), EVEN( ( title->height / 2 ) - ( min_height / 2 ) ) );
271 job->crop[i] = MAX( 0, job->crop[i] );
276 job->crop[i] = MIN( EVEN( job->crop[i] ), EVEN( ( title->width / 2 ) - ( min_width / 2 ) ) );
277 job->crop[i] = MAX( 0, job->crop[i] );
281 double par = (double)title->width / ( (double)title->height * title->aspect );
282 double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
283 (double)( title->width - job->crop[2] - job->crop[3] );
284 double ar = par * cropped_sar;
286 // Dimensions must be greater than minimum and multiple of modulus
287 if( keep == HB_KEEP_WIDTH )
289 job->width = MULTIPLE_MOD( job->width, modulus );
290 job->width = MAX( min_width, job->width );
291 job->height = MULTIPLE_MOD( (uint64_t)( (double)job->width * ar ), modulus );
292 job->height = MAX( min_height, job->height );
296 job->height = MULTIPLE_MOD( job->height, modulus );
297 job->height = MAX( min_height, job->height );
298 job->width = MULTIPLE_MOD( (uint64_t)( (double)job->height / ar ), modulus );
299 job->width = MAX( min_width, job->width );
303 /**********************************************************************
305 **********************************************************************
307 *********************************************************************/
308 int hb_calc_bitrate( hb_job_t * job, int size )
310 int64_t avail = (int64_t) size * 1024 * 1024;
313 int samples_per_frame;
316 hb_title_t * title = job->title;
317 hb_chapter_t * chapter;
320 /* How many overhead bytes are used for each frame
340 /* Get the duration in seconds */
342 for( i = job->chapter_start; i <= job->chapter_end; i++ )
344 chapter = hb_list_item( title->list_chapter, i - 1 );
345 length += chapter->duration;
352 hb_interjob_t * interjob = hb_interjob_get( job->h );
353 avail = job->vbitrate * 125 * length;
354 avail += length * interjob->vrate * overhead / interjob->vrate_base;
358 avail -= length * job->vrate * overhead / job->vrate_base;
365 for( i = 0; i < hb_list_count(job->list_audio); i++ )
369 audio = hb_list_item( job->list_audio, i);
371 /* How many audio samples we put in each frame */
372 switch( audio->config.out.codec )
375 case HB_ACODEC_CA_AAC:
376 case HB_ACODEC_VORBIS:
377 samples_per_frame = 1024;
380 samples_per_frame = 1152;
382 case HB_ACODEC_AC3_PASS:
383 case HB_ACODEC_DCA_PASS:
386 samples_per_frame = 1536;
392 if( audio->config.out.codec == HB_ACODEC_AC3_PASS ||
393 audio->config.out.codec == HB_ACODEC_DCA_PASS)
396 * For pass through we take the bitrate from the input audio
397 * bitrate as we are simply passing it through.
399 abitrate = audio->config.in.bitrate / 8;
404 * Where we are transcoding the audio we use the destination
407 abitrate = audio->config.out.bitrate * 1000 / 8;
409 avail -= length * abitrate;
412 avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
421 return ( avail / ( 125 * length ) );
424 /**********************************************************************
425 * hb_list implementation
426 **********************************************************************
427 * Basic and slow, but enough for what we need
428 *********************************************************************/
430 #define HB_LIST_DEFAULT_SIZE 20
434 /* Pointers to items in the list */
437 /* How many (void *) allocated in 'items' */
440 /* How many valid pointers in 'items' */
444 /**********************************************************************
446 **********************************************************************
447 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
448 *********************************************************************/
449 hb_list_t * hb_list_init()
453 l = calloc( sizeof( hb_list_t ), 1 );
454 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
455 l->items_alloc = HB_LIST_DEFAULT_SIZE;
460 /**********************************************************************
462 **********************************************************************
463 * Returns the number of items currently in the list
464 *********************************************************************/
465 int hb_list_count( hb_list_t * l )
467 return l->items_count;
470 /**********************************************************************
472 **********************************************************************
473 * Adds an item at the end of the list, making it bigger if necessary.
474 * Can safely be called with a NULL pointer to add, it will be ignored.
475 *********************************************************************/
476 void hb_list_add( hb_list_t * l, void * p )
483 if( l->items_count == l->items_alloc )
485 /* We need a bigger boat */
486 l->items_alloc += HB_LIST_DEFAULT_SIZE;
487 l->items = realloc( l->items,
488 l->items_alloc * sizeof( void * ) );
491 l->items[l->items_count] = p;
495 /**********************************************************************
497 **********************************************************************
498 * Remove an item from the list. Bad things will happen if called
499 * with a NULL pointer or if the item is not in the list.
500 *********************************************************************/
501 void hb_list_rem( hb_list_t * l, void * p )
505 /* Find the item in the list */
506 for( i = 0; i < l->items_count; i++ )
508 if( l->items[i] == p )
514 /* Shift all items after it sizeof( void * ) bytes earlier */
515 memmove( &l->items[i], &l->items[i+1],
516 ( l->items_count - i - 1 ) * sizeof( void * ) );
521 /**********************************************************************
523 **********************************************************************
524 * Returns item at position i, or NULL if there are not that many
526 *********************************************************************/
527 void * hb_list_item( hb_list_t * l, int i )
529 if( i < 0 || i >= l->items_count )
537 /**********************************************************************
539 **********************************************************************
540 * Assuming all items are of type hb_buffer_t, returns the total
541 * number of bytes in the list
542 *********************************************************************/
543 int hb_list_bytes( hb_list_t * l )
550 for( i = 0; i < hb_list_count( l ); i++ )
552 buf = hb_list_item( l, i );
553 ret += buf->size - buf->cur;
559 /**********************************************************************
561 **********************************************************************
562 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
563 * the list to <dst>, keeping the list unmodified.
564 *********************************************************************/
565 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
572 for( i = 0, copied = 0; copied < size; i++ )
574 buf = hb_list_item( l, i );
575 copying = MIN( buf->size - buf->cur, size - copied );
576 memcpy( &dst[copied], &buf->data[buf->cur], copying );
581 /**********************************************************************
583 **********************************************************************
584 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
585 * the list to <dst>. What's copied is removed from the list.
586 * The variable pointed by <pts> is set to the PTS of the buffer the
587 * first byte has been got from.
588 * The variable pointed by <pos> is set to the position of that byte
590 *********************************************************************/
591 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
592 uint64_t * pts, uint64_t * pos )
599 /* So we won't have to deal with NULL pointers */
600 uint64_t dummy1, dummy2;
602 if( !pts ) pts = &dummy1;
603 if( !pos ) pos = &dummy2;
605 for( copied = 0, has_pts = 0; copied < size; )
607 buf = hb_list_item( l, 0 );
608 copying = MIN( buf->size - buf->cur, size - copied );
609 memcpy( &dst[copied], &buf->data[buf->cur], copying );
619 if( buf->cur >= buf->size )
621 hb_list_rem( l, buf );
622 hb_buffer_close( &buf );
629 /**********************************************************************
631 **********************************************************************
632 * Assuming all items are of type hb_buffer_t, close them all and
634 *********************************************************************/
635 void hb_list_empty( hb_list_t ** _l )
640 while( ( b = hb_list_item( l, 0 ) ) )
643 hb_buffer_close( &b );
649 /**********************************************************************
651 **********************************************************************
652 * Free memory allocated by hb_list_init. Does NOT free contents of
653 * items still in the list.
654 *********************************************************************/
655 void hb_list_close( hb_list_t ** _l )
665 /**********************************************************************
667 **********************************************************************
668 * If verbose mode is one, print message with timestamp. Messages
669 * longer than 180 characters are stripped ;p
670 *********************************************************************/
671 void hb_log( char * log, ... )
673 char string[362]; /* 360 chars + \n + \0 */
678 if( !getenv( "HB_DEBUG" ) )
680 /* We don't want to print it */
686 now = localtime( &_now );
687 sprintf( string, "[%02d:%02d:%02d] ",
688 now->tm_hour, now->tm_min, now->tm_sec );
690 /* Convert the message to a string */
691 va_start( args, log );
692 vsnprintf( string + 11, 349, log, args );
695 /* Add the end of line */
696 strcat( string, "\n" );
699 fprintf( stderr, "%s", string );
702 int global_verbosity_level; //Necessary for hb_deep_log
703 /**********************************************************************
705 **********************************************************************
706 * If verbose mode is >= level, print message with timestamp. Messages
707 * longer than 360 characters are stripped ;p
708 *********************************************************************/
709 void hb_deep_log( hb_debug_level_t level, char * log, ... )
711 char string[362]; /* 360 chars + \n + \0 */
716 if( global_verbosity_level < level )
724 now = localtime( &_now );
725 sprintf( string, "[%02d:%02d:%02d] ",
726 now->tm_hour, now->tm_min, now->tm_sec );
728 /* Convert the message to a string */
729 va_start( args, log );
730 vsnprintf( string + 11, 349, log, args );
733 /* Add the end of line */
734 strcat( string, "\n" );
737 fprintf( stderr, "%s", string );
740 /**********************************************************************
742 **********************************************************************
743 * Using whatever output is available display this error.
744 *********************************************************************/
745 void hb_error( char * log, ... )
747 char string[181]; /* 180 chars + \0 */
748 char rep_string[181];
749 static char last_string[181];
750 static int last_error_count = 0;
751 static uint64_t last_series_error_time = 0;
752 static hb_lock_t *mutex = 0;
756 /* Convert the message to a string */
757 va_start( args, log );
758 vsnprintf( string, 180, log, args );
763 mutex = hb_lock_init();
768 time_now = hb_get_date();
770 if( strcmp( string, last_string) == 0 )
773 * The last error and this one are the same, don't log it
774 * just count it instead, unless it was more than one second
778 if( last_series_error_time + ( 1000 * 1 ) > time_now )
786 * A new error, or the same one more than 10sec since the last one
787 * did we have any of the same counted up?
789 if( last_error_count > 0 )
792 * Print out the last error to ensure context for the last
797 error_handler( last_string );
799 hb_log( "%s", last_string );
802 if( last_error_count > 1 )
805 * Only print out the repeat message for more than 2 of the
806 * same, since we just printed out two of them already.
808 snprintf( rep_string, 180, "Last error repeated %d times",
809 last_error_count - 1 );
813 error_handler( rep_string );
815 hb_log( "%s", rep_string );
819 last_error_count = 0;
822 last_series_error_time = time_now;
824 strcpy( last_string, string );
827 * Got the error in a single string, send it off to be dispatched.
831 error_handler( string );
833 hb_log( "%s", string );
839 void hb_register_error_handler( hb_error_handler_t * handler )
841 error_handler = handler;
844 /**********************************************************************
846 **********************************************************************
848 *********************************************************************/
849 hb_title_t * hb_title_init( char * path, int index )
853 t = calloc( sizeof( hb_title_t ), 1 );
856 t->list_audio = hb_list_init();
857 t->list_chapter = hb_list_init();
858 t->list_subtitle = hb_list_init();
859 t->list_attachment = hb_list_init();
860 strcat( t->path, path );
861 // default to decoding mpeg2
863 t->video_codec = WORK_DECMPEG2;
868 /**********************************************************************
870 **********************************************************************
872 *********************************************************************/
873 void hb_title_close( hb_title_t ** _t )
875 hb_title_t * t = *_t;
877 hb_chapter_t * chapter;
878 hb_subtitle_t * subtitle;
879 hb_attachment_t * attachment;
881 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
883 hb_list_rem( t->list_audio, audio );
886 hb_list_close( &t->list_audio );
888 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
890 hb_list_rem( t->list_chapter, chapter );
893 hb_list_close( &t->list_chapter );
895 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
897 hb_list_rem( t->list_subtitle, subtitle );
898 if ( subtitle->extradata )
900 free( subtitle->extradata );
901 subtitle->extradata = NULL;
905 hb_list_close( &t->list_subtitle );
907 while( ( attachment = hb_list_item( t->list_attachment, 0 ) ) )
909 hb_list_rem( t->list_attachment, attachment );
910 if ( attachment->name )
912 free( attachment->name );
913 attachment->name = NULL;
915 if ( attachment->data )
917 free( attachment->data );
918 attachment->data = NULL;
922 hb_list_close( &t->list_attachment );
926 if( t->metadata->coverart )
928 free( t->metadata->coverart );
937 /**********************************************************************
939 **********************************************************************
941 *********************************************************************/
942 void hb_filter_close( hb_filter_object_t ** _f )
944 hb_filter_object_t * f = *_f;
946 f->close( f->private_data );
957 /**********************************************************************
959 **********************************************************************
961 *********************************************************************/
962 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
964 hb_audio_t *audio = NULL;
968 audio = calloc(1, sizeof(*audio));
969 memcpy(audio, src, sizeof(*audio));
974 /**********************************************************************
976 **********************************************************************
978 *********************************************************************/
979 void hb_audio_config_init(hb_audio_config_t * audiocfg)
981 /* Set read only paramaters to invalid values */
982 audiocfg->in.codec = 0xDEADBEEF;
983 audiocfg->in.bitrate = -1;
984 audiocfg->in.samplerate = -1;
985 audiocfg->in.channel_layout = 0;
986 audiocfg->in.version = 0;
987 audiocfg->in.mode = 0;
988 audiocfg->flags.ac3 = 0;
989 audiocfg->lang.description[0] = 0;
990 audiocfg->lang.simple[0] = 0;
991 audiocfg->lang.iso639_2[0] = 0;
993 /* Initalize some sensable defaults */
994 audiocfg->in.track = audiocfg->out.track = 0;
995 audiocfg->out.codec = HB_ACODEC_FAAC;
996 audiocfg->out.bitrate = 128;
997 audiocfg->out.samplerate = 44100;
998 audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
999 audiocfg->out.dynamic_range_compression = 0;
1000 audiocfg->out.name = NULL;
1003 /**********************************************************************
1005 **********************************************************************
1007 *********************************************************************/
1008 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
1010 hb_title_t *title = job->title;
1013 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
1020 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
1022 /* This most likely means the client didn't call hb_audio_config_init
1028 /* Really shouldn't ignore the passed out track, but there is currently no
1029 * way to handle duplicates or out-of-order track numbers.
1031 audio->config.out.track = hb_list_count(job->list_audio) + 1;
1032 audio->config.out.codec = audiocfg->out.codec;
1033 if( (audiocfg->out.codec & HB_ACODEC_MASK) == audio->config.in.codec &&
1034 (audiocfg->out.codec & HB_ACODEC_PASS_FLAG ) )
1036 /* Pass-through, copy from input. */
1037 audio->config.out.samplerate = audio->config.in.samplerate;
1038 audio->config.out.bitrate = audio->config.in.bitrate;
1039 audio->config.out.dynamic_range_compression = 0;
1040 audio->config.out.mixdown = 0;
1044 /* Non pass-through, use what is given. */
1045 audio->config.out.codec &= ~HB_ACODEC_PASS_FLAG;
1046 audio->config.out.samplerate = audiocfg->out.samplerate;
1047 audio->config.out.bitrate = audiocfg->out.bitrate;
1048 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
1049 audio->config.out.mixdown = audiocfg->out.mixdown;
1052 hb_list_add(job->list_audio, audio);
1056 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
1058 hb_audio_t *audio = NULL;
1060 if( (audio = hb_list_item(list, i)) )
1061 return &(audio->config);
1066 /**********************************************************************
1068 **********************************************************************
1070 *********************************************************************/
1071 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
1073 hb_subtitle_t *subtitle = NULL;
1077 subtitle = calloc(1, sizeof(*subtitle));
1078 memcpy(subtitle, src, sizeof(*subtitle));
1079 if ( src->extradata )
1081 subtitle->extradata = malloc( src->extradata_size );
1082 memcpy( subtitle->extradata, src->extradata, src->extradata_size );
1088 /**********************************************************************
1090 **********************************************************************
1092 *********************************************************************/
1093 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
1095 hb_title_t *title = job->title;
1096 hb_subtitle_t *subtitle;
1098 subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
1099 if( subtitle == NULL )
1104 subtitle->config = *subtitlecfg;
1105 hb_list_add(job->list_subtitle, subtitle);
1109 int hb_srt_add( const hb_job_t * job,
1110 const hb_subtitle_config_t * subtitlecfg,
1113 hb_subtitle_t *subtitle;
1114 iso639_lang_t *language = NULL;
1117 subtitle = calloc( 1, sizeof( *subtitle ) );
1119 subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
1120 subtitle->format = TEXTSUB;
1121 subtitle->source = SRTSUB;
1123 language = lang_for_code2( lang );
1128 strcpy( subtitle->lang, language->eng_name );
1129 strncpy( subtitle->iso639_2, lang, 4 );
1131 subtitle->config = *subtitlecfg;
1132 subtitle->config.dest = PASSTHRUSUB;
1134 hb_list_add(job->list_subtitle, subtitle);
1140 char * hb_strdup_printf( char * fmt, ... )
1148 str = malloc( size );
1154 /* Try to print in the allocated space. */
1155 va_start( ap, fmt );
1156 len = vsnprintf( str, size, fmt, ap );
1159 /* If that worked, return the string. */
1160 if ( len > -1 && len < size )
1165 /* Else try again with more space. */
1166 if ( len > -1 ) /* glibc 2.1 */
1167 size = len + 1; /* precisely what is needed */
1168 else /* glibc 2.0 */
1169 size *= 2; /* twice the old size */
1170 tmp = realloc( str, size );
1181 /**********************************************************************
1182 * hb_attachment_copy
1183 **********************************************************************
1185 *********************************************************************/
1186 hb_attachment_t *hb_attachment_copy(const hb_attachment_t *src)
1188 hb_attachment_t *attachment = NULL;
1192 attachment = calloc(1, sizeof(*attachment));
1193 memcpy(attachment, src, sizeof(*attachment));
1196 attachment->name = strdup( src->name );
1200 attachment->data = malloc( src->size );
1201 memcpy( attachment->data, src->data, src->size );
1207 /**********************************************************************
1209 **********************************************************************
1210 * Converts a YCbCr pixel to an RGB pixel.
1212 * This conversion is lossy (due to rounding and clamping).
1215 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
1216 *********************************************************************/
1217 int hb_yuv2rgb(int yuv)
1222 y = (yuv >> 16) & 0xff;
1223 Cb = (yuv >> 8) & 0xff;
1226 r = 1.164 * (y - 16) + 2.018 * (Cb - 128);
1227 g = 1.164 * (y - 16) - 0.813 * (Cr - 128) - 0.391 * (Cb - 128);
1228 b = 1.164 * (y - 16) + 1.596 * (Cr - 128);
1230 r = (r < 0) ? 0 : r;
1231 g = (g < 0) ? 0 : g;
1232 b = (b < 0) ? 0 : b;
1234 r = (r > 255) ? 255 : r;
1235 g = (g > 255) ? 255 : g;
1236 b = (b > 255) ? 255 : b;
1238 return (r << 16) | (g << 8) | b;
1241 /**********************************************************************
1243 **********************************************************************
1244 * Converts an RGB pixel to a YCbCr pixel.
1246 * This conversion is lossy (due to rounding and clamping).
1249 * http://en.wikipedia.org/w/index.php?title=YCbCr&oldid=361987695#Technical_details
1250 *********************************************************************/
1251 int hb_rgb2yuv(int rgb)
1256 r = (rgb >> 16) & 0xff;
1257 g = (rgb >> 8) & 0xff;
1260 y = 16. + ( 0.257 * r) + (0.504 * g) + (0.098 * b);
1261 Cb = 128. + (-0.148 * r) - (0.291 * g) + (0.439 * b);
1262 Cr = 128. + ( 0.439 * r) - (0.368 * g) - (0.071 * b);
1264 y = (y < 0) ? 0 : y;
1265 Cb = (Cb < 0) ? 0 : Cb;
1266 Cr = (Cr < 0) ? 0 : Cr;
1268 y = (y > 255) ? 255 : y;
1269 Cb = (Cb > 255) ? 255 : Cb;
1270 Cr = (Cr > 255) ? 255 : Cr;
1272 return (y << 16) | (Cb << 8) | Cr;
1275 const char * hb_subsource_name( int source )