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.m0k.org/>.
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 },
47 { "AC-3 Pass-through", "HB_AMIXDOWN_AC3", "ac-3", HB_AMIXDOWN_AC3 },
48 { "Dolby Pro Logic II + AC-3", "HB_AMIXDOWN_DOLBYPLII_AC3", "dpl2+ac3", HB_AMIXDOWN_DOLBYPLII_AC3 },
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 /**********************************************************************
81 **********************************************************************
82 * Given a numerator (num) and a denominator (den), reduce them to an
83 * equivalent fraction and store the result in x and y.
84 *********************************************************************/
85 void hb_reduce( int *x, int *y, int num, int den )
87 int lower = MIN( num, den );
91 for( i = lower - 1; i > 1; --i )
93 if( ( num % i == 0 ) && ( den % i == 0 ) )
102 /**********************************************************************
104 **********************************************************************
105 * Given the output width (if HB_KEEP_WIDTH) or height
106 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
107 * correct height or width in order to respect the DVD aspect ratio
108 *********************************************************************/
109 void hb_fix_aspect( hb_job_t * job, int keep )
111 hb_title_t * title = job->title;
114 /* don't do anything unless the title has complete size info */
115 if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
117 hb_log( "hb_fix_aspect: incomplete info for title %d: "
118 "height = %d, width = %d, aspect = %d",
119 title->height, title->width, title->aspect );
124 Widths and heights must be multiples of 16 and greater than or
126 Crop values must be multiples of 2, greater than or equal to 0
127 and less than half of the dimension */
128 job->width = MULTIPLE_16( job->width );
129 job->height = MULTIPLE_16( job->height );
130 job->width = MAX( 16, job->width );
131 job->height = MAX( 16, job->height );
132 for( i = 0; i < 4; i++ )
134 job->crop[i] = EVEN( job->crop[i] );
135 job->crop[i] = MAX( 0, job->crop[i] );
139 job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
144 job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
148 if( keep == HB_KEEP_WIDTH )
150 job->height = MULTIPLE_16(
151 (uint64_t) job->width * title->width * HB_ASPECT_BASE *
152 ( title->height - job->crop[0] - job->crop[1] ) /
153 ( (uint64_t) title->height * title->aspect *
154 ( title->width - job->crop[2] - job->crop[3] ) ) );
155 job->height = MAX( 16, job->height );
159 job->width = MULTIPLE_16(
160 (uint64_t) job->height * title->height * title->aspect *
161 ( title->width - job->crop[2] - job->crop[3] ) /
162 ( (uint64_t) title->width * HB_ASPECT_BASE *
163 ( title->height - job->crop[0] - job->crop[1] ) ) );
164 job->width = MAX( 16, job->width );
168 /**********************************************************************
170 **********************************************************************
172 *********************************************************************/
173 int hb_calc_bitrate( hb_job_t * job, int size )
175 int64_t avail = (int64_t) size * 1024 * 1024;
178 int samples_per_frame;
181 hb_title_t * title = job->title;
182 hb_chapter_t * chapter;
185 /* How many overhead bytes are used for each frame
205 /* How many audio samples we put in each frame */
206 switch( job->acodec )
209 case HB_ACODEC_VORBIS:
210 samples_per_frame = 1024;
213 samples_per_frame = 1152;
216 samples_per_frame = 1536;
222 /* Get the duration in seconds */
224 for( i = job->chapter_start; i <= job->chapter_end; i++ )
226 chapter = hb_list_item( title->list_chapter, i - 1 );
227 length += chapter->duration;
233 avail -= length * job->vrate * overhead / job->vrate_base;
235 for( i = 0; job->audios[i] >= 0; i++ )
239 if( job->acodec & HB_ACODEC_AC3 ||
240 job->audio_mixdowns[i] == HB_AMIXDOWN_AC3)
243 * For AC-3 we take the bitrate from the input audio
244 * bitrate as we are simply passing it through.
246 audio = hb_list_item( title->list_audio, job->audios[i] );
247 abitrate = audio->bitrate / 8;
252 * Where we are transcoding the audio we use the destination
255 abitrate = job->abitrate * 1000 / 8;
257 avail -= length * abitrate;
260 avail -= length * job->arate * overhead / samples_per_frame;
268 return ( avail / ( 125 * length ) );
271 /**********************************************************************
272 * hb_list implementation
273 **********************************************************************
274 * Basic and slow, but enough for what we need
275 *********************************************************************/
277 #define HB_LIST_DEFAULT_SIZE 20
281 /* Pointers to items in the list */
284 /* How many (void *) allocated in 'items' */
287 /* How many valid pointers in 'items' */
291 /**********************************************************************
293 **********************************************************************
294 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
295 *********************************************************************/
296 hb_list_t * hb_list_init()
300 l = calloc( sizeof( hb_list_t ), 1 );
301 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
302 l->items_alloc = HB_LIST_DEFAULT_SIZE;
307 /**********************************************************************
309 **********************************************************************
310 * Returns the number of items currently in the list
311 *********************************************************************/
312 int hb_list_count( hb_list_t * l )
314 return l->items_count;
317 /**********************************************************************
319 **********************************************************************
320 * Adds an item at the end of the list, making it bigger if necessary.
321 * Can safely be called with a NULL pointer to add, it will be ignored.
322 *********************************************************************/
323 void hb_list_add( hb_list_t * l, void * p )
330 if( l->items_count == l->items_alloc )
332 /* We need a bigger boat */
333 l->items_alloc += HB_LIST_DEFAULT_SIZE;
334 l->items = realloc( l->items,
335 l->items_alloc * sizeof( void * ) );
338 l->items[l->items_count] = p;
342 /**********************************************************************
344 **********************************************************************
345 * Remove an item from the list. Bad things will happen if called
346 * with a NULL pointer or if the item is not in the list.
347 *********************************************************************/
348 void hb_list_rem( hb_list_t * l, void * p )
352 /* Find the item in the list */
353 for( i = 0; i < l->items_count; i++ )
355 if( l->items[i] == p )
361 /* Shift all items after it sizeof( void * ) bytes earlier */
362 memmove( &l->items[i], &l->items[i+1],
363 ( l->items_count - i - 1 ) * sizeof( void * ) );
368 /**********************************************************************
370 **********************************************************************
371 * Returns item at position i, or NULL if there are not that many
373 *********************************************************************/
374 void * hb_list_item( hb_list_t * l, int i )
376 if( i < 0 || i >= l->items_count )
384 /**********************************************************************
386 **********************************************************************
387 * Assuming all items are of type hb_buffer_t, returns the total
388 * number of bytes in the list
389 *********************************************************************/
390 int hb_list_bytes( hb_list_t * l )
397 for( i = 0; i < hb_list_count( l ); i++ )
399 buf = hb_list_item( l, i );
400 ret += buf->size - buf->cur;
406 /**********************************************************************
408 **********************************************************************
409 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
410 * the list to <dst>, keeping the list unmodified.
411 *********************************************************************/
412 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
419 for( i = 0, copied = 0; copied < size; i++ )
421 buf = hb_list_item( l, i );
422 copying = MIN( buf->size - buf->cur, size - copied );
423 memcpy( &dst[copied], &buf->data[buf->cur], copying );
428 /**********************************************************************
430 **********************************************************************
431 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
432 * the list to <dst>. What's copied is removed from the list.
433 * The variable pointed by <pts> is set to the PTS of the buffer the
434 * first byte has been got from.
435 * The variable pointed by <pos> is set to the position of that byte
437 *********************************************************************/
438 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
439 uint64_t * pts, uint64_t * pos )
446 /* So we won't have to deal with NULL pointers */
447 uint64_t dummy1, dummy2;
449 if( !pts ) pts = &dummy1;
450 if( !pos ) pos = &dummy2;
452 for( copied = 0, has_pts = 0; copied < size; )
454 buf = hb_list_item( l, 0 );
455 copying = MIN( buf->size - buf->cur, size - copied );
456 memcpy( &dst[copied], &buf->data[buf->cur], copying );
466 if( buf->cur >= buf->size )
468 hb_list_rem( l, buf );
469 hb_buffer_close( &buf );
476 /**********************************************************************
478 **********************************************************************
479 * Assuming all items are of type hb_buffer_t, close them all and
481 *********************************************************************/
482 void hb_list_empty( hb_list_t ** _l )
487 while( ( b = hb_list_item( l, 0 ) ) )
490 hb_buffer_close( &b );
496 /**********************************************************************
498 **********************************************************************
499 * Free memory allocated by hb_list_init. Does NOT free contents of
500 * items still in the list.
501 *********************************************************************/
502 void hb_list_close( hb_list_t ** _l )
512 /**********************************************************************
514 **********************************************************************
515 * If verbose mode is one, print message with timestamp. Messages
516 * longer than 180 characters are stripped ;p
517 *********************************************************************/
518 void hb_log( char * log, ... )
520 char string[362]; /* 360 chars + \n + \0 */
525 if( !getenv( "HB_DEBUG" ) )
527 /* We don't want to print it */
533 now = localtime( &_now );
534 sprintf( string, "[%02d:%02d:%02d] ",
535 now->tm_hour, now->tm_min, now->tm_sec );
537 /* Convert the message to a string */
538 va_start( args, log );
539 vsnprintf( string + 11, 349, log, args );
542 /* Add the end of line */
543 strcat( string, "\n" );
546 fprintf( stderr, "%s", string );
549 /**********************************************************************
551 **********************************************************************
552 * Using whatever output is available display this error.
553 *********************************************************************/
554 void hb_error( char * log, ... )
556 char string[181]; /* 180 chars + \0 */
559 /* Convert the message to a string */
560 va_start( args, log );
561 vsnprintf( string, 180, log, args );
565 * Got the error in a single string, send it off to be dispatched.
569 error_handler( string );
575 void hb_register_error_handler( hb_error_handler_t * handler )
577 error_handler = handler;
580 /**********************************************************************
582 **********************************************************************
584 *********************************************************************/
585 hb_title_t * hb_title_init( char * dvd, int index )
589 t = calloc( sizeof( hb_title_t ), 1 );
592 t->list_audio = hb_list_init();
593 t->list_chapter = hb_list_init();
594 t->list_subtitle = hb_list_init();
595 strcat( t->dvd, dvd );
600 /**********************************************************************
602 **********************************************************************
604 *********************************************************************/
605 void hb_title_close( hb_title_t ** _t )
607 hb_title_t * t = *_t;
609 hb_chapter_t * chapter;
610 hb_subtitle_t * subtitle;
612 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
614 hb_list_rem( t->list_audio, audio );
617 hb_list_close( &t->list_audio );
619 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
621 hb_list_rem( t->list_chapter, chapter );
624 hb_list_close( &t->list_chapter );
626 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
628 hb_list_rem( t->list_subtitle, subtitle );
631 hb_list_close( &t->list_subtitle );
637 /**********************************************************************
639 **********************************************************************
641 *********************************************************************/
642 void hb_filter_close( hb_filter_object_t ** _f )
644 hb_filter_object_t * f = *_f;
646 f->close( f->private_data );