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. */
13 /**********************************************************************
15 *********************************************************************/
16 hb_rate_t hb_video_rates[] =
17 { { "5", 5400000 }, { "10", 2700000 }, { "12", 2250000 },
18 { "15", 1800000 }, { "23.976", 1126125 }, { "24", 1125000 },
19 { "25", 1080000 }, { "29.97", 900900 } };
20 int hb_video_rates_count = sizeof( hb_video_rates ) /
23 hb_rate_t hb_audio_rates[] =
24 { { "22.05", 22050 }, { "24", 24000 }, { "32", 32000 },
25 { "44.1", 44100 }, { "48", 48000 } };
26 int hb_audio_rates_count = sizeof( hb_audio_rates ) /
28 int hb_audio_rates_default = 3; /* 44100 Hz */
30 hb_rate_t hb_audio_bitrates[] =
31 { { "32", 32 }, { "40", 40 }, { "48", 48 }, { "56", 56 },
32 { "64", 64 }, { "80", 80 }, { "96", 96 }, { "112", 112 },
33 { "128", 128 }, { "160", 160 }, { "192", 192 }, { "224", 224 },
34 { "256", 256 }, { "320", 320 }, { "384", 384 } };
35 int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) /
37 int hb_audio_bitrates_default = 8; /* 128 kbps */
39 static hb_error_handler_t *error_handler = NULL;
41 hb_mixdown_t hb_audio_mixdowns[] =
42 { { "Mono", "HB_AMIXDOWN_MONO", "mono", HB_AMIXDOWN_MONO },
43 { "Stereo", "HB_AMIXDOWN_STEREO", "stereo", HB_AMIXDOWN_STEREO },
44 { "Dolby Surround", "HB_AMIXDOWN_DOLBY", "dpl1", HB_AMIXDOWN_DOLBY },
45 { "Dolby Pro Logic II", "HB_AMIXDOWN_DOLBYPLII", "dpl2", HB_AMIXDOWN_DOLBYPLII },
46 { "6-channel discrete", "HB_AMIXDOWN_6CH", "6ch", HB_AMIXDOWN_6CH }
48 int hb_audio_mixdowns_count = sizeof( hb_audio_mixdowns ) /
49 sizeof( hb_mixdown_t );
51 int hb_mixdown_get_mixdown_from_short_name( const char * short_name )
54 for (i = 0; i < hb_audio_mixdowns_count; i++)
56 if (strcmp(hb_audio_mixdowns[i].short_name, short_name) == 0)
58 return hb_audio_mixdowns[i].amixdown;
64 const char * hb_mixdown_get_short_name_from_mixdown( int amixdown )
67 for (i = 0; i < hb_audio_mixdowns_count; i++)
69 if (hb_audio_mixdowns[i].amixdown == amixdown)
71 return hb_audio_mixdowns[i].short_name;
77 /**********************************************************************
79 **********************************************************************
80 * Given a numerator (num) and a denominator (den), reduce them to an
81 * equivalent fraction and store the result in x and y.
82 *********************************************************************/
83 void hb_reduce( int *x, int *y, int num, int den )
85 // find the greatest common divisor of num & den by Euclid's algorithm
94 // at this point n is the gcd. if it's non-zero remove it from num
95 // and den. Otherwise just return the original values.
108 /**********************************************************************
110 **********************************************************************
111 * Given the output width (if HB_KEEP_WIDTH) or height
112 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
113 * correct height or width in order to respect the DVD aspect ratio
114 *********************************************************************/
115 void hb_fix_aspect( hb_job_t * job, int keep )
117 hb_title_t * title = job->title;
120 /* don't do anything unless the title has complete size info */
121 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
123 hb_log( "hb_fix_aspect: incomplete info for title %d: "
124 "height = %d, width = %d, aspect = %d",
125 title->height, title->width, title->aspect );
130 Widths and heights must be multiples of 16 and greater than or
132 Crop values must be multiples of 2, greater than or equal to 0
133 and less than half of the dimension */
134 job->width = MULTIPLE_16( job->width );
135 job->height = MULTIPLE_16( job->height );
136 job->width = MAX( 16, job->width );
137 job->height = MAX( 16, job->height );
138 for( i = 0; i < 4; i++ )
140 job->crop[i] = EVEN( job->crop[i] );
141 job->crop[i] = MAX( 0, job->crop[i] );
145 job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
150 job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
154 double par = (double)title->width / ( (double)title->height * title->aspect );
155 double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
156 (double)(title->width - job->crop[2] - job->crop[3] );
157 double ar = par * cropped_sar;
158 if( keep == HB_KEEP_WIDTH )
160 job->height = MULTIPLE_16( (uint64_t)( (double)job->width * ar ) );
161 job->height = MAX( 16, job->height );
165 job->width = MULTIPLE_16( (uint64_t)( (double)job->height / ar ) );
166 job->width = MAX( 16, job->width );
170 /**********************************************************************
172 **********************************************************************
174 *********************************************************************/
175 int hb_calc_bitrate( hb_job_t * job, int size )
177 int64_t avail = (int64_t) size * 1024 * 1024;
180 int samples_per_frame;
183 hb_title_t * title = job->title;
184 hb_chapter_t * chapter;
187 /* How many overhead bytes are used for each frame
207 /* Get the duration in seconds */
209 for( i = job->chapter_start; i <= job->chapter_end; i++ )
211 chapter = hb_list_item( title->list_chapter, i - 1 );
212 length += chapter->duration;
218 avail -= length * job->vrate * overhead / job->vrate_base;
220 for( i = 0; i < hb_list_count(job->list_audio); i++ )
224 audio = hb_list_item( job->list_audio, i);
226 /* How many audio samples we put in each frame */
227 switch( audio->config.out.codec )
230 case HB_ACODEC_VORBIS:
231 samples_per_frame = 1024;
234 samples_per_frame = 1152;
238 samples_per_frame = 1536;
244 if( audio->config.out.codec == HB_ACODEC_AC3 ||
245 audio->config.out.codec == HB_ACODEC_DCA)
248 * For pass through we take the bitrate from the input audio
249 * bitrate as we are simply passing it through.
251 abitrate = audio->config.in.bitrate / 8;
256 * Where we are transcoding the audio we use the destination
259 abitrate = audio->config.out.bitrate * 1000 / 8;
261 avail -= length * abitrate;
264 avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
272 return ( avail / ( 125 * length ) );
275 /**********************************************************************
276 * hb_list implementation
277 **********************************************************************
278 * Basic and slow, but enough for what we need
279 *********************************************************************/
281 #define HB_LIST_DEFAULT_SIZE 20
285 /* Pointers to items in the list */
288 /* How many (void *) allocated in 'items' */
291 /* How many valid pointers in 'items' */
295 /**********************************************************************
297 **********************************************************************
298 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
299 *********************************************************************/
300 hb_list_t * hb_list_init()
304 l = calloc( sizeof( hb_list_t ), 1 );
305 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
306 l->items_alloc = HB_LIST_DEFAULT_SIZE;
311 /**********************************************************************
313 **********************************************************************
314 * Returns the number of items currently in the list
315 *********************************************************************/
316 int hb_list_count( hb_list_t * l )
318 return l->items_count;
321 /**********************************************************************
323 **********************************************************************
324 * Adds an item at the end of the list, making it bigger if necessary.
325 * Can safely be called with a NULL pointer to add, it will be ignored.
326 *********************************************************************/
327 void hb_list_add( hb_list_t * l, void * p )
334 if( l->items_count == l->items_alloc )
336 /* We need a bigger boat */
337 l->items_alloc += HB_LIST_DEFAULT_SIZE;
338 l->items = realloc( l->items,
339 l->items_alloc * sizeof( void * ) );
342 l->items[l->items_count] = p;
346 /**********************************************************************
348 **********************************************************************
349 * Remove an item from the list. Bad things will happen if called
350 * with a NULL pointer or if the item is not in the list.
351 *********************************************************************/
352 void hb_list_rem( hb_list_t * l, void * p )
356 /* Find the item in the list */
357 for( i = 0; i < l->items_count; i++ )
359 if( l->items[i] == p )
365 /* Shift all items after it sizeof( void * ) bytes earlier */
366 memmove( &l->items[i], &l->items[i+1],
367 ( l->items_count - i - 1 ) * sizeof( void * ) );
372 /**********************************************************************
374 **********************************************************************
375 * Returns item at position i, or NULL if there are not that many
377 *********************************************************************/
378 void * hb_list_item( hb_list_t * l, int i )
380 if( i < 0 || i >= l->items_count )
388 /**********************************************************************
390 **********************************************************************
391 * Assuming all items are of type hb_buffer_t, returns the total
392 * number of bytes in the list
393 *********************************************************************/
394 int hb_list_bytes( hb_list_t * l )
401 for( i = 0; i < hb_list_count( l ); i++ )
403 buf = hb_list_item( l, i );
404 ret += buf->size - buf->cur;
410 /**********************************************************************
412 **********************************************************************
413 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
414 * the list to <dst>, keeping the list unmodified.
415 *********************************************************************/
416 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
423 for( i = 0, copied = 0; copied < size; i++ )
425 buf = hb_list_item( l, i );
426 copying = MIN( buf->size - buf->cur, size - copied );
427 memcpy( &dst[copied], &buf->data[buf->cur], copying );
432 /**********************************************************************
434 **********************************************************************
435 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
436 * the list to <dst>. What's copied is removed from the list.
437 * The variable pointed by <pts> is set to the PTS of the buffer the
438 * first byte has been got from.
439 * The variable pointed by <pos> is set to the position of that byte
441 *********************************************************************/
442 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
443 uint64_t * pts, uint64_t * pos )
450 /* So we won't have to deal with NULL pointers */
451 uint64_t dummy1, dummy2;
453 if( !pts ) pts = &dummy1;
454 if( !pos ) pos = &dummy2;
456 for( copied = 0, has_pts = 0; copied < size; )
458 buf = hb_list_item( l, 0 );
459 copying = MIN( buf->size - buf->cur, size - copied );
460 memcpy( &dst[copied], &buf->data[buf->cur], copying );
470 if( buf->cur >= buf->size )
472 hb_list_rem( l, buf );
473 hb_buffer_close( &buf );
480 /**********************************************************************
482 **********************************************************************
483 * Assuming all items are of type hb_buffer_t, close them all and
485 *********************************************************************/
486 void hb_list_empty( hb_list_t ** _l )
491 while( ( b = hb_list_item( l, 0 ) ) )
494 hb_buffer_close( &b );
500 /**********************************************************************
502 **********************************************************************
503 * Free memory allocated by hb_list_init. Does NOT free contents of
504 * items still in the list.
505 *********************************************************************/
506 void hb_list_close( hb_list_t ** _l )
516 /**********************************************************************
518 **********************************************************************
519 * If verbose mode is one, print message with timestamp. Messages
520 * longer than 180 characters are stripped ;p
521 *********************************************************************/
522 void hb_log( char * log, ... )
524 char string[362]; /* 360 chars + \n + \0 */
529 if( !getenv( "HB_DEBUG" ) )
531 /* We don't want to print it */
537 now = localtime( &_now );
538 sprintf( string, "[%02d:%02d:%02d] ",
539 now->tm_hour, now->tm_min, now->tm_sec );
541 /* Convert the message to a string */
542 va_start( args, log );
543 vsnprintf( string + 11, 349, log, args );
546 /* Add the end of line */
547 strcat( string, "\n" );
550 fprintf( stderr, "%s", string );
553 int global_verbosity_level; //Necessary for hb_deep_log
554 /**********************************************************************
556 **********************************************************************
557 * If verbose mode is >= level, print message with timestamp. Messages
558 * longer than 360 characters are stripped ;p
559 *********************************************************************/
560 void hb_deep_log( hb_debug_level_t level, char * log, ... )
562 char string[362]; /* 360 chars + \n + \0 */
567 if( global_verbosity_level < level )
575 now = localtime( &_now );
576 sprintf( string, "[%02d:%02d:%02d] ",
577 now->tm_hour, now->tm_min, now->tm_sec );
579 /* Convert the message to a string */
580 va_start( args, log );
581 vsnprintf( string + 11, 349, log, args );
584 /* Add the end of line */
585 strcat( string, "\n" );
588 fprintf( stderr, "%s", string );
591 /**********************************************************************
593 **********************************************************************
594 * Using whatever output is available display this error.
595 *********************************************************************/
596 void hb_error( char * log, ... )
598 char string[181]; /* 180 chars + \0 */
601 /* Convert the message to a string */
602 va_start( args, log );
603 vsnprintf( string, 180, log, args );
607 * Got the error in a single string, send it off to be dispatched.
611 error_handler( string );
617 void hb_register_error_handler( hb_error_handler_t * handler )
619 error_handler = handler;
622 /**********************************************************************
624 **********************************************************************
626 *********************************************************************/
627 hb_title_t * hb_title_init( char * dvd, int index )
631 t = calloc( sizeof( hb_title_t ), 1 );
634 t->list_audio = hb_list_init();
635 t->list_chapter = hb_list_init();
636 t->list_subtitle = hb_list_init();
637 strcat( t->dvd, dvd );
638 // default to decoding mpeg2
640 t->video_codec = WORK_DECMPEG2;
645 /**********************************************************************
647 **********************************************************************
649 *********************************************************************/
650 void hb_title_close( hb_title_t ** _t )
652 hb_title_t * t = *_t;
654 hb_chapter_t * chapter;
655 hb_subtitle_t * subtitle;
657 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
659 hb_list_rem( t->list_audio, audio );
662 hb_list_close( &t->list_audio );
664 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
666 hb_list_rem( t->list_chapter, chapter );
669 hb_list_close( &t->list_chapter );
671 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
673 hb_list_rem( t->list_subtitle, subtitle );
676 hb_list_close( &t->list_subtitle );
680 if( t->metadata->coverart )
682 free( t->metadata->coverart );
691 /**********************************************************************
693 **********************************************************************
695 *********************************************************************/
696 void hb_filter_close( hb_filter_object_t ** _f )
698 hb_filter_object_t * f = *_f;
700 f->close( f->private_data );
711 /**********************************************************************
713 **********************************************************************
715 *********************************************************************/
716 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
718 hb_audio_t *audio = NULL;
722 audio = calloc(1, sizeof(*audio));
723 memcpy(audio, src, sizeof(*audio));
728 /**********************************************************************
730 **********************************************************************
732 *********************************************************************/
733 void hb_audio_config_init(hb_audio_config_t * audiocfg)
735 /* Set read only paramaters to invalid values */
736 audiocfg->in.codec = 0xDEADBEEF;
737 audiocfg->in.bitrate = -1;
738 audiocfg->in.samplerate = -1;
739 audiocfg->in.channel_layout = 0;
740 audiocfg->in.version = 0;
741 audiocfg->in.mode = 0;
742 audiocfg->flags.ac3 = 0;
743 audiocfg->lang.description[0] = 0;
744 audiocfg->lang.simple[0] = 0;
745 audiocfg->lang.iso639_2[0] = 0;
747 /* Initalize some sensable defaults */
748 audiocfg->in.track = audiocfg->out.track = 0;
749 audiocfg->out.codec = HB_ACODEC_FAAC;
750 audiocfg->out.bitrate = 128;
751 audiocfg->out.samplerate = 44100;
752 audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
753 audiocfg->out.dynamic_range_compression = 0;
754 audiocfg->out.name = NULL;
757 /**********************************************************************
759 **********************************************************************
761 *********************************************************************/
762 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
764 hb_title_t *title = job->title;
767 audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
774 if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
776 /* This most likely means the client didn't call hb_audio_config_init
782 /* Really shouldn't ignore the passed out track, but there is currently no
783 * way to handle duplicates or out-of-order track numbers.
785 audio->config.out.track = hb_list_count(job->list_audio) + 1;
786 audio->config.out.codec = audiocfg->out.codec;
787 if( audiocfg->out.codec == audio->config.in.codec )
789 /* Pass-through, copy from input. */
790 audio->config.out.samplerate = audio->config.in.samplerate;
791 audio->config.out.bitrate = audio->config.in.bitrate;
792 audio->config.out.dynamic_range_compression = 0;
793 audio->config.out.mixdown = 0;
797 /* Non pass-through, use what is given. */
798 audio->config.out.samplerate = audiocfg->out.samplerate;
799 audio->config.out.bitrate = audiocfg->out.bitrate;
800 audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
801 audio->config.out.mixdown = audiocfg->out.mixdown;
804 hb_list_add(job->list_audio, audio);
808 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
810 hb_audio_t *audio = NULL;
812 if( (audio = hb_list_item(list, i)) )
813 return &(audio->config);