OSDN Git Service

Added libhb and CLI support for Dolby Pro Logic II 5.0 matrix encoding. *NOT YET...
[handbrake-jp/handbrake-jp-git.git] / libhb / common.c
1 /* $Id: common.c,v 1.15 2005/03/17 19:22:47 titer Exp $
2
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. */
6
7 #include <stdarg.h>
8 #include <time.h> 
9 #include <sys/time.h>
10
11 #include "common.h"
12
13 /**********************************************************************
14  * Global variables
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 ) /
21                            sizeof( hb_rate_t );
22
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 ) /
27                              sizeof( hb_rate_t );
28 int hb_audio_rates_default = 3; /* 44100 Hz */
29
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 ) /
36                               sizeof( hb_rate_t );
37 int hb_audio_bitrates_default = 8; /* 128 kbps */
38
39 hb_mixdown_t hb_audio_mixdowns[] =
40 { { "Mono",               "HB_AMIXDOWN_MONO",      HB_AMIXDOWN_MONO      },
41   { "Stereo",             "HB_AMIXDOWN_STEREO",    HB_AMIXDOWN_STEREO    },
42   { "Dolby Surround",     "HB_AMIXDOWN_DOLBY",     HB_AMIXDOWN_DOLBY     },
43   { "Dolby Pro Logic II", "HB_AMIXDOWN_DOLBYPLII", HB_AMIXDOWN_DOLBYPLII },
44   { "6-channel discrete", "HB_AMIXDOWN_6CH",       HB_AMIXDOWN_6CH       } };
45 int hb_audio_mixdowns_count = sizeof( hb_audio_mixdowns ) /
46                               sizeof( hb_mixdown_t );
47
48 /**********************************************************************
49  * hb_reduce
50  **********************************************************************
51  * Given a numerator (num) and a denominator (den), reduce them to an
52  * equivalent fraction and store the result in x and y.
53  *********************************************************************/
54 void hb_reduce( int *x, int *y, int num, int den )
55 {
56     int lower = MIN( num, den );
57     int i;
58     *x = num;
59     *y = den;
60     for( i = lower - 1; i > 1; --i )
61     {
62         if( ( num % i == 0 ) && ( den % i == 0 ) )
63         {
64             *x = num / i;
65             *y = den / i;
66             break;
67         }
68     }
69 }
70
71 /**********************************************************************
72  * hb_fix_aspect
73  **********************************************************************
74  * Given the output width (if HB_KEEP_WIDTH) or height
75  * (HB_KEEP_HEIGHT) and the current crop values, calculates the
76  * correct height or width in order to respect the DVD aspect ratio
77  *********************************************************************/
78 void hb_fix_aspect( hb_job_t * job, int keep )
79 {
80     hb_title_t * title = job->title;
81     int          i;
82
83     /* Sanity checks:
84        Widths and heights must be multiples of 16 and greater than or
85        equal to 16
86        Crop values must be multiples of 2, greater than or equal to 0
87        and less than half of the dimension */
88     job->width   = MULTIPLE_16( job->width );
89     job->height  = MULTIPLE_16( job->height );
90     job->width   = MAX( 16, job->width );
91     job->height  = MAX( 16, job->height );
92     for( i = 0; i < 4; i++ )
93     {
94         job->crop[i] = EVEN( job->crop[i] );
95         job->crop[i] = MAX( 0, job->crop[i] );
96         if( i < 2 )
97         {
98             /* Top, bottom */
99             job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
100         }
101         else
102         {
103             /* Left, right */
104             job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
105         }
106     }
107
108     if( keep == HB_KEEP_WIDTH )
109     {
110         job->height = MULTIPLE_16(
111             (uint64_t) job->width * title->width * HB_ASPECT_BASE *
112               ( title->height - job->crop[0] - job->crop[1] ) /
113             ( (uint64_t) title->height * title->aspect *
114               ( title->width - job->crop[2] - job->crop[3] ) ) );
115         job->height = MAX( 16, job->height );
116     }
117     else
118     {
119         job->width = MULTIPLE_16(
120             (uint64_t) job->height * title->height * title->aspect *
121               ( title->width - job->crop[2] - job->crop[3] ) /
122             ( (uint64_t) title->width * HB_ASPECT_BASE *
123               ( title->height - job->crop[0] - job->crop[1] ) ) );
124         job->width = MAX( 16, job->width );
125     }
126 }
127
128 /**********************************************************************
129  * hb_calc_bitrate
130  **********************************************************************
131  * size: in megabytes
132  *********************************************************************/
133 int hb_calc_bitrate( hb_job_t * job, int size )
134 {
135     int64_t avail = (int64_t) size * 1024 * 1024;
136     int64_t length;
137     int     overhead;
138     int     samples_per_frame;
139     int     i;
140
141     hb_title_t   * title = job->title;
142     hb_chapter_t * chapter;
143     hb_audio_t   * audio;
144
145     /* How many overhead bytes are used for each frame
146        (quite guessed) */
147     switch( job->mux )
148     {
149        case HB_MUX_MP4:
150        case HB_MUX_PSP:
151                 case HB_MUX_IPOD:
152             overhead = 6;
153             break; 
154         case HB_MUX_AVI:
155             overhead = 24;
156             break; 
157         case HB_MUX_OGM:
158             overhead = 6;
159             break;
160         default:
161             return 0;
162     }
163
164     /* How many audio samples we put in each frame */
165     switch( job->acodec )
166     {
167         case HB_ACODEC_FAAC:
168         case HB_ACODEC_VORBIS:
169             samples_per_frame = 1024;
170             break;
171         case HB_ACODEC_LAME:
172             samples_per_frame = 1152;
173             break;
174         case HB_ACODEC_AC3:
175             samples_per_frame = 1536;
176             break;
177         default:
178             return 0;
179     }
180
181     /* Get the duration in seconds */
182     length = 0;
183     for( i = job->chapter_start; i <= job->chapter_end; i++ )
184     {
185         chapter = hb_list_item( title->list_chapter, i - 1 );
186         length += chapter->duration;
187     }
188     length += 135000;
189     length /= 90000;
190
191     /* Video overhead */
192     avail -= length * job->vrate * overhead / job->vrate_base;
193
194     for( i = 0; job->audios[i] >= 0; i++ )
195     {
196         /* Audio data */
197         int abitrate;
198         if( job->acodec & HB_ACODEC_AC3 )
199         {
200             audio = hb_list_item( title->list_audio, job->audios[i] );
201             abitrate = audio->bitrate / 8;
202         }
203         else
204         {
205             abitrate = job->abitrate * 1000 / 8;
206         }
207         avail -= length * abitrate;
208         
209         /* Audio overhead */
210         avail -= length * job->arate * overhead / samples_per_frame;
211     }
212
213     if( avail < 0 )
214     {
215         return 0;
216     }
217
218     return ( avail / ( 125 * length ) );
219 }
220
221 /**********************************************************************
222  * hb_list implementation
223  **********************************************************************
224  * Basic and slow, but enough for what we need
225  *********************************************************************/
226
227 #define HB_LIST_DEFAULT_SIZE 20
228
229 struct hb_list_s
230 {
231     /* Pointers to items in the list */
232     void ** items;
233
234     /* How many (void *) allocated in 'items' */
235     int     items_alloc;
236
237     /* How many valid pointers in 'items' */
238     int     items_count;
239 };
240
241 /**********************************************************************
242  * hb_list_init
243  **********************************************************************
244  * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
245  *********************************************************************/
246 hb_list_t * hb_list_init()
247 {
248     hb_list_t * l;
249
250     l              = calloc( sizeof( hb_list_t ), 1 );
251     l->items       = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
252     l->items_alloc = HB_LIST_DEFAULT_SIZE;
253
254     return l;
255 }
256
257 /**********************************************************************
258  * hb_list_count
259  **********************************************************************
260  * Returns the number of items currently in the list
261  *********************************************************************/
262 int hb_list_count( hb_list_t * l )
263 {
264     return l->items_count;
265 }
266
267 /**********************************************************************
268  * hb_list_add
269  **********************************************************************
270  * Adds an item at the end of the list, making it bigger if necessary.
271  * Can safely be called with a NULL pointer to add, it will be ignored.
272  *********************************************************************/
273 void hb_list_add( hb_list_t * l, void * p )
274 {
275     if( !p )
276     {
277         return;
278     }
279
280     if( l->items_count == l->items_alloc )
281     {
282         /* We need a bigger boat */
283         l->items_alloc += HB_LIST_DEFAULT_SIZE;
284         l->items        = realloc( l->items,
285                                    l->items_alloc * sizeof( void * ) );
286     }
287
288     l->items[l->items_count] = p;
289     (l->items_count)++;
290 }
291
292 /**********************************************************************
293  * hb_list_rem
294  **********************************************************************
295  * Remove an item from the list. Bad things will happen if called
296  * with a NULL pointer or if the item is not in the list.
297  *********************************************************************/
298 void hb_list_rem( hb_list_t * l, void * p )
299 {
300     int i;
301
302     /* Find the item in the list */
303     for( i = 0; i < l->items_count; i++ )
304     {
305         if( l->items[i] == p )
306         {
307             break;
308         }
309     }
310
311     /* Shift all items after it sizeof( void * ) bytes earlier */
312     memmove( &l->items[i], &l->items[i+1],
313              ( l->items_count - i - 1 ) * sizeof( void * ) );
314
315     (l->items_count)--;
316 }
317
318 /**********************************************************************
319  * hb_list_item
320  **********************************************************************
321  * Returns item at position i, or NULL if there are not that many
322  * items in the list
323  *********************************************************************/
324 void * hb_list_item( hb_list_t * l, int i )
325 {
326     if( i < 0 || i >= l->items_count )
327     {
328         return NULL;
329     }
330
331     return l->items[i];
332 }
333
334 /**********************************************************************
335  * hb_list_bytes
336  **********************************************************************
337  * Assuming all items are of type hb_buffer_t, returns the total
338  * number of bytes in the list
339  *********************************************************************/
340 int hb_list_bytes( hb_list_t * l )
341 {
342     hb_buffer_t * buf;
343     int           ret;
344     int           i;
345
346     ret = 0;
347     for( i = 0; i < hb_list_count( l ); i++ )
348     {
349         buf  = hb_list_item( l, i );
350         ret += buf->size - buf->cur;
351     }
352
353     return ret;                                                                 
354 }
355
356 /**********************************************************************
357  * hb_list_seebytes
358  **********************************************************************
359  * Assuming all items are of type hb_buffer_t, copy <size> bytes from
360  * the list to <dst>, keeping the list unmodified.
361  *********************************************************************/
362 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
363 {
364     hb_buffer_t * buf;
365     int           copied;
366     int           copying;
367     int           i;
368     
369     for( i = 0, copied = 0; copied < size; i++ )
370     {
371         buf     = hb_list_item( l, i );
372         copying = MIN( buf->size - buf->cur, size - copied );
373         memcpy( &dst[copied], &buf->data[buf->cur], copying );
374         copied += copying;
375     }                                                                           
376 }
377
378 /**********************************************************************
379  * hb_list_getbytes
380  **********************************************************************
381  * Assuming all items are of type hb_buffer_t, copy <size> bytes from
382  * the list to <dst>. What's copied is removed from the list.
383  * The variable pointed by <pts> is set to the PTS of the buffer the
384  * first byte has been got from.
385  * The variable pointed by <pos> is set to the position of that byte
386  * in that buffer.
387  *********************************************************************/
388 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
389                        uint64_t * pts, uint64_t * pos )
390 {
391     hb_buffer_t * buf;
392     int           copied;
393     int           copying;
394     uint8_t       has_pts;
395     
396     /* So we won't have to deal with NULL pointers */
397      uint64_t dummy1, dummy2;
398
399     if( !pts ) pts = &dummy1;
400     if( !pos ) pos = &dummy2;
401
402     for( copied = 0, has_pts = 0; copied < size;  )
403     {
404         buf     = hb_list_item( l, 0 );
405         copying = MIN( buf->size - buf->cur, size - copied );
406         memcpy( &dst[copied], &buf->data[buf->cur], copying );
407
408         if( !has_pts )
409         {
410             *pts    = buf->start;
411             *pos    = buf->cur;
412             has_pts = 1;
413         }
414
415         buf->cur += copying;
416         if( buf->cur >= buf->size )
417         {
418             hb_list_rem( l, buf );
419             hb_buffer_close( &buf );
420         }
421
422         copied += copying;
423     }                                                                           
424 }
425
426 /**********************************************************************
427  * hb_list_empty
428  **********************************************************************
429  * Assuming all items are of type hb_buffer_t, close them all and
430  * close the list.
431  *********************************************************************/
432 void hb_list_empty( hb_list_t ** _l )
433 {
434     hb_list_t * l = *_l;
435     hb_buffer_t * b;
436
437     while( ( b = hb_list_item( l, 0 ) ) )
438     {
439         hb_list_rem( l, b );
440         hb_buffer_close( &b );
441     }
442
443     hb_list_close( _l );
444 }
445
446 /**********************************************************************
447  * hb_list_close
448  **********************************************************************
449  * Free memory allocated by hb_list_init. Does NOT free contents of
450  * items still in the list.
451  *********************************************************************/
452 void hb_list_close( hb_list_t ** _l )
453 {
454     hb_list_t * l = *_l;
455
456     free( l->items );
457     free( l );
458
459     *_l = NULL;
460 }
461
462 /**********************************************************************
463  * hb_log
464  **********************************************************************
465  * If verbose mode is one, print message with timestamp. Messages
466  * longer than 80 characters are stripped ;p
467  *********************************************************************/
468 void hb_log( char * log, ... )
469 {
470     char        string[82]; /* 80 chars + \n + \0 */
471     time_t      _now;
472     struct tm * now;
473     va_list     args;
474
475     if( !getenv( "HB_DEBUG" ) )
476     {
477         /* We don't want to print it */
478         return;
479     }
480
481     /* Get the time */
482     _now = time( NULL );
483     now  = localtime( &_now );
484     sprintf( string, "[%02d:%02d:%02d] ",
485              now->tm_hour, now->tm_min, now->tm_sec );
486
487     /* Convert the message to a string */
488     va_start( args, log );
489     vsnprintf( string + 11, 69, log, args );
490     va_end( args );
491
492     /* Add the end of line */
493     strcat( string, "\n" );
494
495     /* Print it */
496     fprintf( stderr, "%s", string );
497 }
498
499 /**********************************************************************
500  * hb_title_init
501  **********************************************************************
502  * 
503  *********************************************************************/
504 hb_title_t * hb_title_init( char * dvd, int index )
505 {
506     hb_title_t * t;
507
508     t = calloc( sizeof( hb_title_t ), 1 );
509
510     t->index         = index;
511     t->list_audio    = hb_list_init();
512     t->list_chapter  = hb_list_init();
513     t->list_subtitle = hb_list_init();
514     strcat( t->dvd, dvd );
515
516     return t;
517 }
518
519 /**********************************************************************
520  * hb_title_close
521  **********************************************************************
522  * 
523  *********************************************************************/
524 void hb_title_close( hb_title_t ** _t )
525 {
526     hb_title_t * t = *_t;
527     hb_audio_t * audio;
528     hb_chapter_t * chapter;
529     hb_subtitle_t * subtitle;
530
531     while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
532     {
533         hb_list_rem( t->list_audio, audio );
534         free( audio );
535     }
536     hb_list_close( &t->list_audio );
537     
538     while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
539     {
540         hb_list_rem( t->list_chapter, chapter );
541         free( chapter );
542     }
543     hb_list_close( &t->list_chapter );
544     
545     while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
546     {
547         hb_list_rem( t->list_subtitle, subtitle );
548         free( subtitle );
549     }
550     hb_list_close( &t->list_subtitle );
551
552     free( t );
553     *_t = NULL;
554 }
555