1 /* $Id: common.c,v 1.15 2005/03/17 19:22:47 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
15 /**********************************************************************
17 *********************************************************************/
18 hb_rate_t hb_video_rates[] =
19 { { "5", 5400000 }, { "10", 2700000 }, { "12", 2250000 },
20 { "15", 1800000 }, { "23.976", 1126125 }, { "24", 1125000 },
21 { "25", 1080000 }, { "29.97", 900900 } };
22 int hb_video_rates_count = sizeof( hb_video_rates ) /
25 hb_rate_t hb_audio_rates[] =
26 { { "22.05", 22050 }, { "24", 24000 }, { "32", 32000 },
27 { "44.1", 44100 }, { "48", 48000 } };
28 int hb_audio_rates_count = sizeof( hb_audio_rates ) /
30 int hb_audio_rates_default = 3; /* 44100 Hz */
32 hb_rate_t hb_audio_bitrates[] =
33 { { "32", 32 }, { "40", 40 }, { "48", 48 }, { "56", 56 },
34 { "64", 64 }, { "80", 80 }, { "96", 96 }, { "112", 112 },
35 { "128", 128 }, { "160", 160 }, { "192", 192 }, { "224", 224 },
36 { "256", 256 }, { "320", 320 }, { "384", 384 }, { "448", 448 },
38 int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) /
40 int hb_audio_bitrates_default = 8; /* 128 kbps */
42 static hb_error_handler_t *error_handler = NULL;
44 hb_mixdown_t hb_audio_mixdowns[] =
45 { { "Mono", "HB_AMIXDOWN_MONO", "mono", HB_AMIXDOWN_MONO },
46 { "Stereo", "HB_AMIXDOWN_STEREO", "stereo", HB_AMIXDOWN_STEREO },
47 { "Dolby Surround", "HB_AMIXDOWN_DOLBY", "dpl1", HB_AMIXDOWN_DOLBY },
48 { "Dolby Pro Logic II", "HB_AMIXDOWN_DOLBYPLII", "dpl2", HB_AMIXDOWN_DOLBYPLII },
49 { "6-channel discrete", "HB_AMIXDOWN_6CH", "6ch", HB_AMIXDOWN_6CH }
51 int hb_audio_mixdowns_count = sizeof( hb_audio_mixdowns ) /
52 sizeof( hb_mixdown_t );
54 int hb_mixdown_get_mixdown_from_short_name( const char * short_name )
57 for (i = 0; i < hb_audio_mixdowns_count; i++)
59 if (strcmp(hb_audio_mixdowns[i].short_name, short_name) == 0)
61 return hb_audio_mixdowns[i].amixdown;
67 const char * hb_mixdown_get_short_name_from_mixdown( int amixdown )
70 for (i = 0; i < hb_audio_mixdowns_count; i++)
72 if (hb_audio_mixdowns[i].amixdown == amixdown)
74 return hb_audio_mixdowns[i].short_name;
80 /**********************************************************************
82 **********************************************************************
83 * Given a numerator (num) and a denominator (den), reduce them to an
84 * equivalent fraction and store the result in x and y.
85 *********************************************************************/
86 void hb_reduce( int *x, int *y, int num, int den )
88 // find the greatest common divisor of num & den by Euclid's algorithm
97 // at this point n is the gcd. if it's non-zero remove it from num
98 // and den. Otherwise just return the original values.
111 /**********************************************************************
113 **********************************************************************
114 * Given the output width (if HB_KEEP_WIDTH) or height
115 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
116 * correct height or width in order to respect the DVD aspect ratio
117 *********************************************************************/
118 void hb_fix_aspect( hb_job_t * job, int keep )
120 hb_title_t * title = job->title;
123 /* don't do anything unless the title has complete size info */
124 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
126 hb_log( "hb_fix_aspect: incomplete info for title %d: "
127 "height = %d, width = %d, aspect = %d",
128 title->height, title->width, title->aspect );
133 Widths and heights must be multiples of 16 and greater than or
135 Crop values must be multiples of 2, greater than or equal to 0
136 and less than half of the dimension */
137 job->width = MULTIPLE_16( job->width );
138 job->height = MULTIPLE_16( job->height );
139 job->width = MAX( 16, job->width );
140 job->height = MAX( 16, job->height );
141 for( i = 0; i < 4; i++ )
143 job->crop[i] = EVEN( job->crop[i] );
144 job->crop[i] = MAX( 0, job->crop[i] );
148 job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
153 job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
157 double par = (double)title->width / ( (double)title->height * title->aspect );
158 double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
159 (double)(title->width - job->crop[2] - job->crop[3] );
160 double ar = par * cropped_sar;
161 if( keep == HB_KEEP_WIDTH )
163 job->height = MULTIPLE_16( (uint64_t)( (double)job->width * ar ) );
164 job->height = MAX( 16, job->height );
168 job->width = MULTIPLE_16( (uint64_t)( (double)job->height / ar ) );
169 job->width = MAX( 16, job->width );
173 /**********************************************************************
175 **********************************************************************
177 *********************************************************************/
178 int hb_calc_bitrate( hb_job_t * job, int size )
180 int64_t avail = (int64_t) size * 1024 * 1024;
183 int samples_per_frame;
186 hb_title_t * title = job->title;
187 hb_chapter_t * chapter;
190 /* How many overhead bytes are used for each frame
210 /* Get the duration in seconds */
212 for( i = job->chapter_start; i <= job->chapter_end; i++ )
214 chapter = hb_list_item( title->list_chapter, i - 1 );
215 length += chapter->duration;
222 hb_interjob_t * interjob = hb_interjob_get( job->h );
223 avail = job->vbitrate * 125 * length;
224 avail += length * interjob->vrate * overhead / interjob->vrate_base;
228 avail -= length * job->vrate * overhead / job->vrate_base;
235 for( i = 0; i < hb_list_count(job->list_audio); i++ )
239 audio = hb_list_item( job->list_audio, i);
241 /* How many audio samples we put in each frame */
242 switch( audio->config.out.codec )
245 case HB_ACODEC_CA_AAC:
246 case HB_ACODEC_VORBIS:
247 samples_per_frame = 1024;
250 samples_per_frame = 1152;
254 samples_per_frame = 1536;
260 if( audio->config.out.codec == HB_ACODEC_AC3 ||
261 audio->config.out.codec == HB_ACODEC_DCA)
264 * For pass through we take the bitrate from the input audio
265 * bitrate as we are simply passing it through.
267 abitrate = audio->config.in.bitrate / 8;
272 * Where we are transcoding the audio we use the destination
275 abitrate = audio->config.out.bitrate * 1000 / 8;
277 avail -= length * abitrate;
280 avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
289 return ( avail / ( 125 * length ) );
292 /**********************************************************************
293 * hb_list implementation
294 **********************************************************************
295 * Basic and slow, but enough for what we need
296 *********************************************************************/
298 #define HB_LIST_DEFAULT_SIZE 20
302 /* Pointers to items in the list */
305 /* How many (void *) allocated in 'items' */
308 /* How many valid pointers in 'items' */
312 /**********************************************************************
314 **********************************************************************
315 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
316 *********************************************************************/
317 hb_list_t * hb_list_init()
321 l = calloc( sizeof( hb_list_t ), 1 );
322 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
323 l->items_alloc = HB_LIST_DEFAULT_SIZE;
328 /**********************************************************************
330 **********************************************************************
331 * Returns the number of items currently in the list
332 *********************************************************************/
333 int hb_list_count( hb_list_t * l )
335 return l->items_count;
338 /**********************************************************************
340 **********************************************************************
341 * Adds an item at the end of the list, making it bigger if necessary.
342 * Can safely be called with a NULL pointer to add, it will be ignored.
343 *********************************************************************/
344 void hb_list_add( hb_list_t * l, void * p )
351 if( l->items_count == l->items_alloc )
353 /* We need a bigger boat */
354 l->items_alloc += HB_LIST_DEFAULT_SIZE;
355 l->items = realloc( l->items,
356 l->items_alloc * sizeof( void * ) );
359 l->items[l->items_count] = p;
363 /**********************************************************************
365 **********************************************************************
366 * Remove an item from the list. Bad things will happen if called
367 * with a NULL pointer or if the item is not in the list.
368 *********************************************************************/
369 void hb_list_rem( hb_list_t * l, void * p )
373 /* Find the item in the list */
374 for( i = 0; i < l->items_count; i++ )
376 if( l->items[i] == p )
382 /* Shift all items after it sizeof( void * ) bytes earlier */
383 memmove( &l->items[i], &l->items[i+1],
384 ( l->items_count - i - 1 ) * sizeof( void * ) );
389 /**********************************************************************
391 **********************************************************************
392 * Returns item at position i, or NULL if there are not that many
394 *********************************************************************/
395 void * hb_list_item( hb_list_t * l, int i )
397 if( i < 0 || i >= l->items_count )
405 /**********************************************************************
407 **********************************************************************
408 * Assuming all items are of type hb_buffer_t, returns the total
409 * number of bytes in the list
410 *********************************************************************/
411 int hb_list_bytes( hb_list_t * l )
418 for( i = 0; i < hb_list_count( l ); i++ )
420 buf = hb_list_item( l, i );
421 ret += buf->size - buf->cur;
427 /**********************************************************************
429 **********************************************************************
430 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
431 * the list to <dst>, keeping the list unmodified.
432 *********************************************************************/
433 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
440 for( i = 0, copied = 0; copied < size; i++ )
442 buf = hb_list_item( l, i );
443 copying = MIN( buf->size - buf->cur, size - copied );
444 memcpy( &dst[copied], &buf->data[buf->cur], copying );
449 /**********************************************************************
451 **********************************************************************
452 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
453 * the list to <dst>. What's copied is removed from the list.
454 * The variable pointed by <pts> is set to the PTS of the buffer the
455 * first byte has been got from.
456 * The variable pointed by <pos> is set to the position of that byte
458 *********************************************************************/
459 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
460 uint64_t * pts, uint64_t * pos )
467 /* So we won't have to deal with NULL pointers */
468 uint64_t dummy1, dummy2;
470 if( !pts ) pts = &dummy1;
471 if( !pos ) pos = &dummy2;
473 for( copied = 0, has_pts = 0; copied < size; )
475 buf = hb_list_item( l, 0 );
476 copying = MIN( buf->size - buf->cur, size - copied );
477 memcpy( &dst[copied], &buf->data[buf->cur], copying );
487 if( buf->cur >= buf->size )
489 hb_list_rem( l, buf );
490 hb_buffer_close( &buf );
497 /**********************************************************************
499 **********************************************************************
500 * Assuming all items are of type hb_buffer_t, close them all and
502 *********************************************************************/
503 void hb_list_empty( hb_list_t ** _l )
508 while( ( b = hb_list_item( l, 0 ) ) )
511 hb_buffer_close( &b );
517 /**********************************************************************
519 **********************************************************************
520 * Free memory allocated by hb_list_init. Does NOT free contents of
521 * items still in the list.
522 *********************************************************************/
523 void hb_list_close( hb_list_t ** _l )
533 /**********************************************************************
535 **********************************************************************
536 * If verbose mode is one, print message with timestamp. Messages
537 * longer than 180 characters are stripped ;p
538 *********************************************************************/
539 void hb_log( char * log, ... )
541 char string[362]; /* 360 chars + \n + \0 */
546 if( !getenv( "HB_DEBUG" ) )
548 /* We don't want to print it */
554 now = localtime( &_now );
555 sprintf( string, "[%02d:%02d:%02d] ",
556 now->tm_hour, now->tm_min, now->tm_sec );
558 /* Convert the message to a string */
559 va_start( args, log );
560 vsnprintf( string + 11, 349, log, args );
563 /* Add the end of line */
564 strcat( string, "\n" );
567 fprintf( stderr, "%s", string );
570 int global_verbosity_level; //Necessary for hb_deep_log
571 /**********************************************************************
573 **********************************************************************
574 * If verbose mode is >= level, print message with timestamp. Messages
575 * longer than 360 characters are stripped ;p
576 *********************************************************************/
577 void hb_deep_log( hb_debug_level_t level, char * log, ... )
579 char string[362]; /* 360 chars + \n + \0 */
584 if( global_verbosity_level < level )
592 now = localtime( &_now );
593 sprintf( string, "[%02d:%02d:%02d] ",
594 now->tm_hour, now->tm_min, now->tm_sec );
596 /* Convert the message to a string */
597 va_start( args, log );
598 vsnprintf( string + 11, 349, log, args );
601 /* Add the end of line */
602 strcat( string, "\n" );
605 fprintf( stderr, "%s", string );
608 /**********************************************************************
610 **********************************************************************
611 * Using whatever output is available display this error.
612 *********************************************************************/
613 void hb_error( char * log, ... )
615 char string[181]; /* 180 chars + \0 */
618 /* Convert the message to a string */
619 va_start( args, log );
620 vsnprintf( string, 180, log, args );
624 * Got the error in a single string, send it off to be dispatched.
628 error_handler( string );
634 void hb_register_error_handler( hb_error_handler_t * handler )
636 error_handler = handler;
639 /**********************************************************************
641 **********************************************************************
643 *********************************************************************/
644 hb_title_t * hb_title_init( char * dvd, int index )
648 t = calloc( sizeof( hb_title_t ), 1 );
651 t->list_audio = hb_list_init();
652 t->list_chapter = hb_list_init();
653 t->list_subtitle = hb_list_init();
654 strcat( t->dvd, dvd );
655 // default to decoding mpeg2
657 t->video_codec = WORK_DECMPEG2;
662 /**********************************************************************
664 **********************************************************************
666 *********************************************************************/
667 void hb_title_close( hb_title_t ** _t )
669 hb_title_t * t = *_t;
671 hb_chapter_t * chapter;
672 hb_subtitle_t * subtitle;
674 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
676 hb_list_rem( t->list_audio, audio );
679 hb_list_close( &t->list_audio );
681 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
683 hb_list_rem( t->list_chapter, chapter );
686 hb_list_close( &t->list_chapter );
688 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
690 hb_list_rem( t->list_subtitle, subtitle );
693 hb_list_close( &t->list_subtitle );
697 if( t->metadata->coverart )
699 free( t->metadata->coverart );
708 /**********************************************************************
710 **********************************************************************
712 *********************************************************************/
713 void hb_filter_close( hb_filter_object_t ** _f )
715 hb_filter_object_t * f = *_f;
717 f->close( f->private_data );
728 /**********************************************************************
730 **********************************************************************
732 *********************************************************************/
733 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
735 hb_audio_t *audio = NULL;
739 audio = calloc(1, sizeof(*audio));
740 memcpy(audio, src, sizeof(*audio));
745 /**********************************************************************
747 **********************************************************************
749 *********************************************************************/
750 void hb_audio_config_init(hb_audio_config_t * audiocfg)
752 /* Set read only paramaters to invalid values */
753 audiocfg->in.codec = 0xDEADBEEF;
754 audiocfg->in.bitrate = -1;
755 audiocfg->in.samplerate = -1;
756 audiocfg->in.channel_layout = 0;
757 audiocfg->in.version = 0;
758 audiocfg->in.mode = 0;
759 audiocfg->flags.ac3 = 0;
760 audiocfg->lang.description[0] = 0;
761 audiocfg->lang.simple[0] = 0;
762 audiocfg->lang.iso639_2[0] = 0;
764 /* Initalize some sensable defaults */
765 audiocfg->in.track = audiocfg->out.track = 0;
766 audiocfg->out.codec = HB_ACODEC_FAAC;
767 audiocfg->out.bitrate = 128;
768 audiocfg->out.samplerate = 44100;
769 audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
770 audiocfg->out.dynamic_range_compression = 0;
771 audiocfg->out.name = NULL;
774 /**********************************************************************
776 **********************************************************************
778 *********************************************************************/
779 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
781 hb_title_t *title = job->title;
784 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
791 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
793 /* This most likely means the client didn't call hb_audio_config_init
799 /* Really shouldn't ignore the passed out track, but there is currently no
800 * way to handle duplicates or out-of-order track numbers.
802 audio->config.out.track = hb_list_count(job->list_audio) + 1;
803 audio->config.out.codec = audiocfg->out.codec;
804 if( audiocfg->out.codec == audio->config.in.codec )
806 /* Pass-through, copy from input. */
807 audio->config.out.samplerate = audio->config.in.samplerate;
808 audio->config.out.bitrate = audio->config.in.bitrate;
809 audio->config.out.dynamic_range_compression = 0;
810 audio->config.out.mixdown = 0;
814 /* Non pass-through, use what is given. */
815 audio->config.out.samplerate = audiocfg->out.samplerate;
816 audio->config.out.bitrate = audiocfg->out.bitrate;
817 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
818 audio->config.out.mixdown = audiocfg->out.mixdown;
821 hb_list_add(job->list_audio, audio);
825 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
827 hb_audio_t *audio = NULL;
829 if( (audio = hb_list_item(list, i)) )
830 return &(audio->config);
835 /**********************************************************************
837 **********************************************************************
839 *********************************************************************/
840 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
842 hb_subtitle_t *subtitle = NULL;
846 subtitle = calloc(1, sizeof(*subtitle));
847 memcpy(subtitle, src, sizeof(*subtitle));
852 /**********************************************************************
854 **********************************************************************
856 *********************************************************************/
857 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
859 hb_title_t *title = job->title;
860 hb_subtitle_t *subtitle;
862 subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
863 if( subtitle == NULL )
868 subtitle->config = *subtitlecfg;
869 hb_list_add(job->list_subtitle, subtitle);
873 int hb_srt_add( const hb_job_t * job,
874 const hb_subtitle_config_t * subtitlecfg,
877 hb_subtitle_t *subtitle;
878 iso639_lang_t *language = NULL;
881 subtitle = calloc( 1, sizeof( *subtitle ) );
883 subtitle->format = TEXTSUB;
884 subtitle->source = SRTSUB;
886 language = lang_for_code2( lang );
891 strcpy( subtitle->lang, language->eng_name );
892 strncpy( subtitle->iso639_2, lang, 4 );
894 subtitle->config = *subtitlecfg;
895 subtitle->config.dest = PASSTHRUSUB;
897 hb_list_add(job->list_subtitle, subtitle);