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 } };
35 int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) /
37 int hb_audio_bitrates_default = 8; /* 128 kbps */
39 /**********************************************************************
41 **********************************************************************
42 * Given a numerator (num) and a denominator (den), reduce them to an
43 * equivalent fraction and store the result in x and y.
44 *********************************************************************/
45 void hb_reduce( int *x, int *y, int num, int den )
47 int lower = MIN( num, den );
51 for( i = lower - 1; i > 1; --i )
53 if( ( num % i == 0 ) && ( den % i == 0 ) )
62 /**********************************************************************
64 **********************************************************************
65 * Given the output width (if HB_KEEP_WIDTH) or height
66 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
67 * correct height or width in order to respect the DVD aspect ratio
68 *********************************************************************/
69 void hb_fix_aspect( hb_job_t * job, int keep )
71 hb_title_t * title = job->title;
75 Widths and heights must be multiples of 16 and greater than or
77 Crop values must be multiples of 2, greater than or equal to 0
78 and less than half of the dimension */
79 job->width = MULTIPLE_16( job->width );
80 job->height = MULTIPLE_16( job->height );
81 job->width = MAX( 16, job->width );
82 job->height = MAX( 16, job->height );
83 for( i = 0; i < 4; i++ )
85 job->crop[i] = EVEN( job->crop[i] );
86 job->crop[i] = MAX( 0, job->crop[i] );
90 job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
95 job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
99 if( keep == HB_KEEP_WIDTH )
101 job->height = MULTIPLE_16(
102 (uint64_t) job->width * title->width * HB_ASPECT_BASE *
103 ( title->height - job->crop[0] - job->crop[1] ) /
104 ( (uint64_t) title->height * title->aspect *
105 ( title->width - job->crop[2] - job->crop[3] ) ) );
106 job->height = MAX( 16, job->height );
110 job->width = MULTIPLE_16(
111 (uint64_t) job->height * title->height * title->aspect *
112 ( title->width - job->crop[2] - job->crop[3] ) /
113 ( (uint64_t) title->width * HB_ASPECT_BASE *
114 ( title->height - job->crop[0] - job->crop[1] ) ) );
115 job->width = MAX( 16, job->width );
119 /**********************************************************************
121 **********************************************************************
123 *********************************************************************/
124 int hb_calc_bitrate( hb_job_t * job, int size )
126 int64_t avail = (int64_t) size * 1024 * 1024;
129 int samples_per_frame;
132 hb_title_t * title = job->title;
133 hb_chapter_t * chapter;
136 /* How many overhead bytes are used for each frame
155 /* How many audio samples we put in each frame */
156 switch( job->acodec )
159 case HB_ACODEC_VORBIS:
160 samples_per_frame = 1024;
163 samples_per_frame = 1152;
166 samples_per_frame = 1536;
172 /* Get the duration in seconds */
174 for( i = job->chapter_start; i <= job->chapter_end; i++ )
176 chapter = hb_list_item( title->list_chapter, i - 1 );
177 length += chapter->duration;
183 avail -= length * job->vrate * overhead / job->vrate_base;
185 for( i = 0; job->audios[i] >= 0; i++ )
189 if( job->acodec & HB_ACODEC_AC3 )
191 audio = hb_list_item( title->list_audio, job->audios[i] );
192 abitrate = audio->bitrate / 8;
196 abitrate = job->abitrate * 1000 / 8;
198 avail -= length * abitrate;
201 avail -= length * job->arate * overhead / samples_per_frame;
209 return ( avail / ( 125 * length ) );
212 /**********************************************************************
213 * hb_list implementation
214 **********************************************************************
215 * Basic and slow, but enough for what we need
216 *********************************************************************/
218 #define HB_LIST_DEFAULT_SIZE 20
222 /* Pointers to items in the list */
225 /* How many (void *) allocated in 'items' */
228 /* How many valid pointers in 'items' */
232 /**********************************************************************
234 **********************************************************************
235 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
236 *********************************************************************/
237 hb_list_t * hb_list_init()
241 l = calloc( sizeof( hb_list_t ), 1 );
242 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
243 l->items_alloc = HB_LIST_DEFAULT_SIZE;
248 /**********************************************************************
250 **********************************************************************
251 * Returns the number of items currently in the list
252 *********************************************************************/
253 int hb_list_count( hb_list_t * l )
255 return l->items_count;
258 /**********************************************************************
260 **********************************************************************
261 * Adds an item at the end of the list, making it bigger if necessary.
262 * Can safely be called with a NULL pointer to add, it will be ignored.
263 *********************************************************************/
264 void hb_list_add( hb_list_t * l, void * p )
271 if( l->items_count == l->items_alloc )
273 /* We need a bigger boat */
274 l->items_alloc += HB_LIST_DEFAULT_SIZE;
275 l->items = realloc( l->items,
276 l->items_alloc * sizeof( void * ) );
279 l->items[l->items_count] = p;
283 /**********************************************************************
285 **********************************************************************
286 * Remove an item from the list. Bad things will happen if called
287 * with a NULL pointer or if the item is not in the list.
288 *********************************************************************/
289 void hb_list_rem( hb_list_t * l, void * p )
293 /* Find the item in the list */
294 for( i = 0; i < l->items_count; i++ )
296 if( l->items[i] == p )
302 /* Shift all items after it sizeof( void * ) bytes earlier */
303 memmove( &l->items[i], &l->items[i+1],
304 ( l->items_count - i - 1 ) * sizeof( void * ) );
309 /**********************************************************************
311 **********************************************************************
312 * Returns item at position i, or NULL if there are not that many
314 *********************************************************************/
315 void * hb_list_item( hb_list_t * l, int i )
317 if( i < 0 || i >= l->items_count )
325 /**********************************************************************
327 **********************************************************************
328 * Assuming all items are of type hb_buffer_t, returns the total
329 * number of bytes in the list
330 *********************************************************************/
331 int hb_list_bytes( hb_list_t * l )
338 for( i = 0; i < hb_list_count( l ); i++ )
340 buf = hb_list_item( l, i );
341 ret += buf->size - buf->cur;
347 /**********************************************************************
349 **********************************************************************
350 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
351 * the list to <dst>, keeping the list unmodified.
352 *********************************************************************/
353 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
360 for( i = 0, copied = 0; copied < size; i++ )
362 buf = hb_list_item( l, i );
363 copying = MIN( buf->size - buf->cur, size - copied );
364 memcpy( &dst[copied], &buf->data[buf->cur], copying );
369 /**********************************************************************
371 **********************************************************************
372 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
373 * the list to <dst>. What's copied is removed from the list.
374 * The variable pointed by <pts> is set to the PTS of the buffer the
375 * first byte has been got from.
376 * The variable pointed by <pos> is set to the position of that byte
378 *********************************************************************/
379 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
380 uint64_t * pts, uint64_t * pos )
387 /* So we won't have to deal with NULL pointers */
388 uint64_t dummy1, dummy2;
390 if( !pts ) pts = &dummy1;
391 if( !pos ) pos = &dummy2;
393 for( copied = 0, has_pts = 0; copied < size; )
395 buf = hb_list_item( l, 0 );
396 copying = MIN( buf->size - buf->cur, size - copied );
397 memcpy( &dst[copied], &buf->data[buf->cur], copying );
407 if( buf->cur >= buf->size )
409 hb_list_rem( l, buf );
410 hb_buffer_close( &buf );
417 /**********************************************************************
419 **********************************************************************
420 * Assuming all items are of type hb_buffer_t, close them all and
422 *********************************************************************/
423 void hb_list_empty( hb_list_t ** _l )
428 while( ( b = hb_list_item( l, 0 ) ) )
431 hb_buffer_close( &b );
437 /**********************************************************************
439 **********************************************************************
440 * Free memory allocated by hb_list_init. Does NOT free contents of
441 * items still in the list.
442 *********************************************************************/
443 void hb_list_close( hb_list_t ** _l )
453 /**********************************************************************
455 **********************************************************************
456 * If verbose mode is one, print message with timestamp. Messages
457 * longer than 80 characters are stripped ;p
458 *********************************************************************/
459 void hb_log( char * log, ... )
461 char string[82]; /* 80 chars + \n + \0 */
466 if( !getenv( "HB_DEBUG" ) )
468 /* We don't want to print it */
474 now = localtime( &_now );
475 sprintf( string, "[%02d:%02d:%02d] ",
476 now->tm_hour, now->tm_min, now->tm_sec );
478 /* Convert the message to a string */
479 va_start( args, log );
480 vsnprintf( string + 11, 69, log, args );
483 /* Add the end of line */
484 strcat( string, "\n" );
487 fprintf( stderr, "%s", string );
490 /**********************************************************************
492 **********************************************************************
494 *********************************************************************/
495 hb_title_t * hb_title_init( char * dvd, int index )
499 t = calloc( sizeof( hb_title_t ), 1 );
502 t->list_audio = hb_list_init();
503 t->list_chapter = hb_list_init();
504 t->list_subtitle = hb_list_init();
505 strcat( t->dvd, dvd );
510 /**********************************************************************
512 **********************************************************************
514 *********************************************************************/
515 void hb_title_close( hb_title_t ** _t )
517 hb_title_t * t = *_t;
519 hb_list_close( &t->list_audio );
520 hb_list_close( &t->list_chapter );
521 hb_list_close( &t->list_subtitle );