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 hb_mixdown_t hb_audio_mixdowns[] =
40 { { "Mono", "HB_AMIXDOWN_MONO", "mono", HB_AMIXDOWN_MONO },
41 { "Stereo", "HB_AMIXDOWN_STEREO", "stereo", HB_AMIXDOWN_STEREO },
42 { "Dolby Surround", "HB_AMIXDOWN_DOLBY", "dpl1", HB_AMIXDOWN_DOLBY },
43 { "Dolby Pro Logic II", "HB_AMIXDOWN_DOLBYPLII", "dpl2", HB_AMIXDOWN_DOLBYPLII },
44 { "6-channel discrete", "HB_AMIXDOWN_6CH", "6ch", HB_AMIXDOWN_6CH } };
45 int hb_audio_mixdowns_count = sizeof( hb_audio_mixdowns ) /
46 sizeof( hb_mixdown_t );
48 int hb_mixdown_get_mixdown_from_short_name( const char * short_name )
51 for (i = 0; i < hb_audio_mixdowns_count; i++)
53 if (strcmp(hb_audio_mixdowns[i].short_name, short_name) == 0)
55 return hb_audio_mixdowns[i].amixdown;
61 const char * hb_mixdown_get_short_name_from_mixdown( int amixdown )
64 for (i = 0; i < hb_audio_mixdowns_count; i++)
66 if (hb_audio_mixdowns[i].amixdown == amixdown)
68 return hb_audio_mixdowns[i].short_name;
74 /**********************************************************************
76 **********************************************************************
77 * Given a numerator (num) and a denominator (den), reduce them to an
78 * equivalent fraction and store the result in x and y.
79 *********************************************************************/
80 void hb_reduce( int *x, int *y, int num, int den )
82 int lower = MIN( num, den );
86 for( i = lower - 1; i > 1; --i )
88 if( ( num % i == 0 ) && ( den % i == 0 ) )
97 /**********************************************************************
99 **********************************************************************
100 * Given the output width (if HB_KEEP_WIDTH) or height
101 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
102 * correct height or width in order to respect the DVD aspect ratio
103 *********************************************************************/
104 void hb_fix_aspect( hb_job_t * job, int keep )
106 hb_title_t * title = job->title;
110 Widths and heights must be multiples of 16 and greater than or
112 Crop values must be multiples of 2, greater than or equal to 0
113 and less than half of the dimension */
114 job->width = MULTIPLE_16( job->width );
115 job->height = MULTIPLE_16( job->height );
116 job->width = MAX( 16, job->width );
117 job->height = MAX( 16, job->height );
118 for( i = 0; i < 4; i++ )
120 job->crop[i] = EVEN( job->crop[i] );
121 job->crop[i] = MAX( 0, job->crop[i] );
125 job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
130 job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
134 if( keep == HB_KEEP_WIDTH )
136 job->height = MULTIPLE_16(
137 (uint64_t) job->width * title->width * HB_ASPECT_BASE *
138 ( title->height - job->crop[0] - job->crop[1] ) /
139 ( (uint64_t) title->height * title->aspect *
140 ( title->width - job->crop[2] - job->crop[3] ) ) );
141 job->height = MAX( 16, job->height );
145 job->width = MULTIPLE_16(
146 (uint64_t) job->height * title->height * title->aspect *
147 ( title->width - job->crop[2] - job->crop[3] ) /
148 ( (uint64_t) title->width * HB_ASPECT_BASE *
149 ( title->height - job->crop[0] - job->crop[1] ) ) );
150 job->width = MAX( 16, job->width );
154 /**********************************************************************
156 **********************************************************************
158 *********************************************************************/
159 int hb_calc_bitrate( hb_job_t * job, int size )
161 int64_t avail = (int64_t) size * 1024 * 1024;
164 int samples_per_frame;
167 hb_title_t * title = job->title;
168 hb_chapter_t * chapter;
171 /* How many overhead bytes are used for each frame
190 /* How many audio samples we put in each frame */
191 switch( job->acodec )
194 case HB_ACODEC_VORBIS:
195 samples_per_frame = 1024;
198 samples_per_frame = 1152;
201 samples_per_frame = 1536;
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; job->audios[i] >= 0; i++ )
224 if( job->acodec & HB_ACODEC_AC3 )
226 audio = hb_list_item( title->list_audio, job->audios[i] );
227 abitrate = audio->bitrate / 8;
231 abitrate = job->abitrate * 1000 / 8;
233 avail -= length * abitrate;
236 avail -= length * job->arate * overhead / samples_per_frame;
244 return ( avail / ( 125 * length ) );
247 /**********************************************************************
248 * hb_list implementation
249 **********************************************************************
250 * Basic and slow, but enough for what we need
251 *********************************************************************/
253 #define HB_LIST_DEFAULT_SIZE 20
257 /* Pointers to items in the list */
260 /* How many (void *) allocated in 'items' */
263 /* How many valid pointers in 'items' */
267 /**********************************************************************
269 **********************************************************************
270 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
271 *********************************************************************/
272 hb_list_t * hb_list_init()
276 l = calloc( sizeof( hb_list_t ), 1 );
277 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
278 l->items_alloc = HB_LIST_DEFAULT_SIZE;
283 /**********************************************************************
285 **********************************************************************
286 * Returns the number of items currently in the list
287 *********************************************************************/
288 int hb_list_count( hb_list_t * l )
290 return l->items_count;
293 /**********************************************************************
295 **********************************************************************
296 * Adds an item at the end of the list, making it bigger if necessary.
297 * Can safely be called with a NULL pointer to add, it will be ignored.
298 *********************************************************************/
299 void hb_list_add( hb_list_t * l, void * p )
306 if( l->items_count == l->items_alloc )
308 /* We need a bigger boat */
309 l->items_alloc += HB_LIST_DEFAULT_SIZE;
310 l->items = realloc( l->items,
311 l->items_alloc * sizeof( void * ) );
314 l->items[l->items_count] = p;
318 /**********************************************************************
320 **********************************************************************
321 * Remove an item from the list. Bad things will happen if called
322 * with a NULL pointer or if the item is not in the list.
323 *********************************************************************/
324 void hb_list_rem( hb_list_t * l, void * p )
328 /* Find the item in the list */
329 for( i = 0; i < l->items_count; i++ )
331 if( l->items[i] == p )
337 /* Shift all items after it sizeof( void * ) bytes earlier */
338 memmove( &l->items[i], &l->items[i+1],
339 ( l->items_count - i - 1 ) * sizeof( void * ) );
344 /**********************************************************************
346 **********************************************************************
347 * Returns item at position i, or NULL if there are not that many
349 *********************************************************************/
350 void * hb_list_item( hb_list_t * l, int i )
352 if( i < 0 || i >= l->items_count )
360 /**********************************************************************
362 **********************************************************************
363 * Assuming all items are of type hb_buffer_t, returns the total
364 * number of bytes in the list
365 *********************************************************************/
366 int hb_list_bytes( hb_list_t * l )
373 for( i = 0; i < hb_list_count( l ); i++ )
375 buf = hb_list_item( l, i );
376 ret += buf->size - buf->cur;
382 /**********************************************************************
384 **********************************************************************
385 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
386 * the list to <dst>, keeping the list unmodified.
387 *********************************************************************/
388 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
395 for( i = 0, copied = 0; copied < size; i++ )
397 buf = hb_list_item( l, i );
398 copying = MIN( buf->size - buf->cur, size - copied );
399 memcpy( &dst[copied], &buf->data[buf->cur], copying );
404 /**********************************************************************
406 **********************************************************************
407 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
408 * the list to <dst>. What's copied is removed from the list.
409 * The variable pointed by <pts> is set to the PTS of the buffer the
410 * first byte has been got from.
411 * The variable pointed by <pos> is set to the position of that byte
413 *********************************************************************/
414 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
415 uint64_t * pts, uint64_t * pos )
422 /* So we won't have to deal with NULL pointers */
423 uint64_t dummy1, dummy2;
425 if( !pts ) pts = &dummy1;
426 if( !pos ) pos = &dummy2;
428 for( copied = 0, has_pts = 0; copied < size; )
430 buf = hb_list_item( l, 0 );
431 copying = MIN( buf->size - buf->cur, size - copied );
432 memcpy( &dst[copied], &buf->data[buf->cur], copying );
442 if( buf->cur >= buf->size )
444 hb_list_rem( l, buf );
445 hb_buffer_close( &buf );
452 /**********************************************************************
454 **********************************************************************
455 * Assuming all items are of type hb_buffer_t, close them all and
457 *********************************************************************/
458 void hb_list_empty( hb_list_t ** _l )
463 while( ( b = hb_list_item( l, 0 ) ) )
466 hb_buffer_close( &b );
472 /**********************************************************************
474 **********************************************************************
475 * Free memory allocated by hb_list_init. Does NOT free contents of
476 * items still in the list.
477 *********************************************************************/
478 void hb_list_close( hb_list_t ** _l )
488 /**********************************************************************
490 **********************************************************************
491 * If verbose mode is one, print message with timestamp. Messages
492 * longer than 80 characters are stripped ;p
493 *********************************************************************/
494 void hb_log( char * log, ... )
496 char string[82]; /* 80 chars + \n + \0 */
501 if( !getenv( "HB_DEBUG" ) )
503 /* We don't want to print it */
509 now = localtime( &_now );
510 sprintf( string, "[%02d:%02d:%02d] ",
511 now->tm_hour, now->tm_min, now->tm_sec );
513 /* Convert the message to a string */
514 va_start( args, log );
515 vsnprintf( string + 11, 69, log, args );
518 /* Add the end of line */
519 strcat( string, "\n" );
522 fprintf( stderr, "%s", string );
525 /**********************************************************************
527 **********************************************************************
529 *********************************************************************/
530 hb_title_t * hb_title_init( char * dvd, int index )
534 t = calloc( sizeof( hb_title_t ), 1 );
537 t->list_audio = hb_list_init();
538 t->list_chapter = hb_list_init();
539 t->list_subtitle = hb_list_init();
540 strcat( t->dvd, dvd );
545 /**********************************************************************
547 **********************************************************************
549 *********************************************************************/
550 void hb_title_close( hb_title_t ** _t )
552 hb_title_t * t = *_t;
554 hb_chapter_t * chapter;
555 hb_subtitle_t * subtitle;
557 while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
559 hb_list_rem( t->list_audio, audio );
562 hb_list_close( &t->list_audio );
564 while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
566 hb_list_rem( t->list_chapter, chapter );
569 hb_list_close( &t->list_chapter );
571 while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
573 hb_list_rem( t->list_subtitle, subtitle );
576 hb_list_close( &t->list_subtitle );