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 the output width (if HB_KEEP_WIDTH) or height
43 * (HB_KEEP_HEIGHT) and the current crop values, calculates the
44 * correct height or width in order to respect the DVD aspect ratio
45 *********************************************************************/
46 void hb_fix_aspect( hb_job_t * job, int keep )
48 hb_title_t * title = job->title;
52 Widths and heights must be multiples of 16 and greater than or
54 Crop values must be multiples of 2, greater than or equal to 0
55 and less than half of the dimension */
56 job->width = MULTIPLE_16( job->width );
57 job->height = MULTIPLE_16( job->height );
58 job->width = MAX( 16, job->width );
59 job->height = MAX( 16, job->height );
60 for( i = 0; i < 4; i++ )
62 job->crop[i] = EVEN( job->crop[i] );
63 job->crop[i] = MAX( 0, job->crop[i] );
67 job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
72 job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
76 if( keep == HB_KEEP_WIDTH )
78 job->height = MULTIPLE_16(
79 (uint64_t) job->width * title->width * HB_ASPECT_BASE *
80 ( title->height - job->crop[0] - job->crop[1] ) /
81 ( (uint64_t) title->height * title->aspect *
82 ( title->width - job->crop[2] - job->crop[3] ) ) );
83 job->height = MAX( 16, job->height );
87 job->width = MULTIPLE_16(
88 (uint64_t) job->height * title->height * title->aspect *
89 ( title->width - job->crop[2] - job->crop[3] ) /
90 ( (uint64_t) title->width * HB_ASPECT_BASE *
91 ( title->height - job->crop[0] - job->crop[1] ) ) );
92 job->width = MAX( 16, job->width );
96 /**********************************************************************
98 **********************************************************************
100 *********************************************************************/
101 int hb_calc_bitrate( hb_job_t * job, int size )
103 int64_t avail = (int64_t) size * 1024 * 1024;
106 int samples_per_frame;
109 hb_title_t * title = job->title;
110 hb_chapter_t * chapter;
113 /* How many overhead bytes are used for each frame
131 /* How many audio samples we put in each frame */
132 switch( job->acodec )
135 case HB_ACODEC_VORBIS:
136 samples_per_frame = 1024;
139 samples_per_frame = 1152;
142 samples_per_frame = 1536;
148 /* Get the duration in seconds */
150 for( i = job->chapter_start; i <= job->chapter_end; i++ )
152 chapter = hb_list_item( title->list_chapter, i - 1 );
153 length += chapter->duration;
159 avail -= length * job->vrate * overhead / job->vrate_base;
161 for( i = 0; job->audios[i] >= 0; i++ )
165 if( job->acodec & HB_ACODEC_AC3 )
167 audio = hb_list_item( title->list_audio, job->audios[i] );
168 abitrate = audio->bitrate / 8;
172 abitrate = job->abitrate * 1000 / 8;
174 avail -= length * abitrate;
177 avail -= length * job->arate * overhead / samples_per_frame;
185 return ( avail / ( 125 * length ) );
188 /**********************************************************************
189 * hb_list implementation
190 **********************************************************************
191 * Basic and slow, but enough for what we need
192 *********************************************************************/
194 #define HB_LIST_DEFAULT_SIZE 20
198 /* Pointers to items in the list */
201 /* How many (void *) allocated in 'items' */
204 /* How many valid pointers in 'items' */
208 /**********************************************************************
210 **********************************************************************
211 * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
212 *********************************************************************/
213 hb_list_t * hb_list_init()
217 l = calloc( sizeof( hb_list_t ), 1 );
218 l->items = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
219 l->items_alloc = HB_LIST_DEFAULT_SIZE;
224 /**********************************************************************
226 **********************************************************************
227 * Returns the number of items currently in the list
228 *********************************************************************/
229 int hb_list_count( hb_list_t * l )
231 return l->items_count;
234 /**********************************************************************
236 **********************************************************************
237 * Adds an item at the end of the list, making it bigger if necessary.
238 * Can safely be called with a NULL pointer to add, it will be ignored.
239 *********************************************************************/
240 void hb_list_add( hb_list_t * l, void * p )
247 if( l->items_count == l->items_alloc )
249 /* We need a bigger boat */
250 l->items_alloc += HB_LIST_DEFAULT_SIZE;
251 l->items = realloc( l->items,
252 l->items_alloc * sizeof( void * ) );
255 l->items[l->items_count] = p;
259 /**********************************************************************
261 **********************************************************************
262 * Remove an item from the list. Bad things will happen if called
263 * with a NULL pointer or if the item is not in the list.
264 *********************************************************************/
265 void hb_list_rem( hb_list_t * l, void * p )
269 /* Find the item in the list */
270 for( i = 0; i < l->items_count; i++ )
272 if( l->items[i] == p )
278 /* Shift all items after it sizeof( void * ) bytes earlier */
279 memmove( &l->items[i], &l->items[i+1],
280 ( l->items_count - i - 1 ) * sizeof( void * ) );
285 /**********************************************************************
287 **********************************************************************
288 * Returns item at position i, or NULL if there are not that many
290 *********************************************************************/
291 void * hb_list_item( hb_list_t * l, int i )
293 if( i < 0 || i >= l->items_count )
301 /**********************************************************************
303 **********************************************************************
304 * Assuming all items are of type hb_buffer_t, returns the total
305 * number of bytes in the list
306 *********************************************************************/
307 int hb_list_bytes( hb_list_t * l )
314 for( i = 0; i < hb_list_count( l ); i++ )
316 buf = hb_list_item( l, i );
317 ret += buf->size - buf->cur;
323 /**********************************************************************
325 **********************************************************************
326 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
327 * the list to <dst>, keeping the list unmodified.
328 *********************************************************************/
329 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
336 for( i = 0, copied = 0; copied < size; i++ )
338 buf = hb_list_item( l, i );
339 copying = MIN( buf->size - buf->cur, size - copied );
340 memcpy( &dst[copied], &buf->data[buf->cur], copying );
345 /**********************************************************************
347 **********************************************************************
348 * Assuming all items are of type hb_buffer_t, copy <size> bytes from
349 * the list to <dst>. What's copied is removed from the list.
350 * The variable pointed by <pts> is set to the PTS of the buffer the
351 * first byte has been got from.
352 * The variable pointed by <pos> is set to the position of that byte
354 *********************************************************************/
355 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
356 uint64_t * pts, int * pos )
363 /* So we won't have to deal with NULL pointers */
366 if( !pts ) pts = &dummy1;
367 if( !pos ) pos = &dummy2;
369 for( copied = 0, has_pts = 0; copied < size; )
371 buf = hb_list_item( l, 0 );
372 copying = MIN( buf->size - buf->cur, size - copied );
373 memcpy( &dst[copied], &buf->data[buf->cur], copying );
383 if( buf->cur >= buf->size )
385 hb_list_rem( l, buf );
386 hb_buffer_close( &buf );
393 /**********************************************************************
395 **********************************************************************
396 * Assuming all items are of type hb_buffer_t, close them all and
398 *********************************************************************/
399 void hb_list_empty( hb_list_t ** _l )
404 while( ( b = hb_list_item( l, 0 ) ) )
407 hb_buffer_close( &b );
413 /**********************************************************************
415 **********************************************************************
416 * Free memory allocated by hb_list_init. Does NOT free contents of
417 * items still in the list.
418 *********************************************************************/
419 void hb_list_close( hb_list_t ** _l )
429 /**********************************************************************
431 **********************************************************************
432 * If verbose mode is one, print message with timestamp. Messages
433 * longer than 80 characters are stripped ;p
434 *********************************************************************/
435 void hb_log( char * log, ... )
437 char string[82]; /* 80 chars + \n + \0 */
442 if( !getenv( "HB_DEBUG" ) )
444 /* We don't want to print it */
450 now = localtime( &_now );
451 sprintf( string, "[%02d:%02d:%02d] ",
452 now->tm_hour, now->tm_min, now->tm_sec );
454 /* Convert the message to a string */
455 va_start( args, log );
456 vsnprintf( string + 11, 69, log, args );
459 /* Add the end of line */
460 strcat( string, "\n" );
463 fprintf( stderr, "%s", string );
466 /**********************************************************************
468 **********************************************************************
470 *********************************************************************/
471 hb_title_t * hb_title_init( char * dvd, int index )
475 t = calloc( sizeof( hb_title_t ), 1 );
478 t->list_audio = hb_list_init();
479 t->list_chapter = hb_list_init();
480 t->list_subtitle = hb_list_init();
481 strcat( t->dvd, dvd );
486 /**********************************************************************
488 **********************************************************************
490 *********************************************************************/
491 void hb_title_close( hb_title_t ** _t )
493 hb_title_t * t = *_t;
495 hb_list_close( &t->list_audio );
496 hb_list_close( &t->list_chapter );
497 hb_list_close( &t->list_subtitle );