OSDN Git Service

MacGui: Presets now utilize Audio Mixdowns Thanks maurj!
[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",      "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 );
47
48 int hb_mixdown_get_mixdown_from_short_name( const char * short_name )
49 {
50     int i;
51     for (i = 0; i < hb_audio_mixdowns_count; i++)
52     {
53         if (strcmp(hb_audio_mixdowns[i].short_name, short_name) == 0)
54         {
55             return hb_audio_mixdowns[i].amixdown;
56         }
57     }
58     return 0;
59 }
60
61 const char * hb_mixdown_get_short_name_from_mixdown( int amixdown )
62 {
63     int i;
64     for (i = 0; i < hb_audio_mixdowns_count; i++)
65     {
66         if (hb_audio_mixdowns[i].amixdown == amixdown)
67         {
68             return hb_audio_mixdowns[i].short_name;
69         }
70     }
71     return "";
72 }
73
74 /**********************************************************************
75  * hb_reduce
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 )
81 {
82     int lower = MIN( num, den );
83     int i;
84     *x = num;
85     *y = den;
86     for( i = lower - 1; i > 1; --i )
87     {
88         if( ( num % i == 0 ) && ( den % i == 0 ) )
89         {
90             *x = num / i;
91             *y = den / i;
92             break;
93         }
94     }
95 }
96
97 /**********************************************************************
98  * hb_fix_aspect
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 )
105 {
106     hb_title_t * title = job->title;
107     int          i;
108
109     /* Sanity checks:
110        Widths and heights must be multiples of 16 and greater than or
111        equal to 16
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++ )
119     {
120         job->crop[i] = EVEN( job->crop[i] );
121         job->crop[i] = MAX( 0, job->crop[i] );
122         if( i < 2 )
123         {
124             /* Top, bottom */
125             job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
126         }
127         else
128         {
129             /* Left, right */
130             job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
131         }
132     }
133
134     if( keep == HB_KEEP_WIDTH )
135     {
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 );
142     }
143     else
144     {
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 );
151     }
152 }
153
154 /**********************************************************************
155  * hb_calc_bitrate
156  **********************************************************************
157  * size: in megabytes
158  *********************************************************************/
159 int hb_calc_bitrate( hb_job_t * job, int size )
160 {
161     int64_t avail = (int64_t) size * 1024 * 1024;
162     int64_t length;
163     int     overhead;
164     int     samples_per_frame;
165     int     i;
166
167     hb_title_t   * title = job->title;
168     hb_chapter_t * chapter;
169     hb_audio_t   * audio;
170
171     /* How many overhead bytes are used for each frame
172        (quite guessed) */
173     switch( job->mux )
174     {
175        case HB_MUX_MP4:
176        case HB_MUX_PSP:
177                 case HB_MUX_IPOD:
178             overhead = 6;
179             break; 
180         case HB_MUX_AVI:
181             overhead = 24;
182             break; 
183         case HB_MUX_OGM:
184             overhead = 6;
185             break;
186         default:
187             return 0;
188     }
189
190     /* How many audio samples we put in each frame */
191     switch( job->acodec )
192     {
193         case HB_ACODEC_FAAC:
194         case HB_ACODEC_VORBIS:
195             samples_per_frame = 1024;
196             break;
197         case HB_ACODEC_LAME:
198             samples_per_frame = 1152;
199             break;
200         case HB_ACODEC_AC3:
201             samples_per_frame = 1536;
202             break;
203         default:
204             return 0;
205     }
206
207     /* Get the duration in seconds */
208     length = 0;
209     for( i = job->chapter_start; i <= job->chapter_end; i++ )
210     {
211         chapter = hb_list_item( title->list_chapter, i - 1 );
212         length += chapter->duration;
213     }
214     length += 135000;
215     length /= 90000;
216
217     /* Video overhead */
218     avail -= length * job->vrate * overhead / job->vrate_base;
219
220     for( i = 0; job->audios[i] >= 0; i++ )
221     {
222         /* Audio data */
223         int abitrate;
224         if( job->acodec & HB_ACODEC_AC3 )
225         {
226             audio = hb_list_item( title->list_audio, job->audios[i] );
227             abitrate = audio->bitrate / 8;
228         }
229         else
230         {
231             abitrate = job->abitrate * 1000 / 8;
232         }
233         avail -= length * abitrate;
234         
235         /* Audio overhead */
236         avail -= length * job->arate * overhead / samples_per_frame;
237     }
238
239     if( avail < 0 )
240     {
241         return 0;
242     }
243
244     return ( avail / ( 125 * length ) );
245 }
246
247 /**********************************************************************
248  * hb_list implementation
249  **********************************************************************
250  * Basic and slow, but enough for what we need
251  *********************************************************************/
252
253 #define HB_LIST_DEFAULT_SIZE 20
254
255 struct hb_list_s
256 {
257     /* Pointers to items in the list */
258     void ** items;
259
260     /* How many (void *) allocated in 'items' */
261     int     items_alloc;
262
263     /* How many valid pointers in 'items' */
264     int     items_count;
265 };
266
267 /**********************************************************************
268  * hb_list_init
269  **********************************************************************
270  * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
271  *********************************************************************/
272 hb_list_t * hb_list_init()
273 {
274     hb_list_t * l;
275
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;
279
280     return l;
281 }
282
283 /**********************************************************************
284  * hb_list_count
285  **********************************************************************
286  * Returns the number of items currently in the list
287  *********************************************************************/
288 int hb_list_count( hb_list_t * l )
289 {
290     return l->items_count;
291 }
292
293 /**********************************************************************
294  * hb_list_add
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 )
300 {
301     if( !p )
302     {
303         return;
304     }
305
306     if( l->items_count == l->items_alloc )
307     {
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 * ) );
312     }
313
314     l->items[l->items_count] = p;
315     (l->items_count)++;
316 }
317
318 /**********************************************************************
319  * hb_list_rem
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 )
325 {
326     int i;
327
328     /* Find the item in the list */
329     for( i = 0; i < l->items_count; i++ )
330     {
331         if( l->items[i] == p )
332         {
333             break;
334         }
335     }
336
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 * ) );
340
341     (l->items_count)--;
342 }
343
344 /**********************************************************************
345  * hb_list_item
346  **********************************************************************
347  * Returns item at position i, or NULL if there are not that many
348  * items in the list
349  *********************************************************************/
350 void * hb_list_item( hb_list_t * l, int i )
351 {
352     if( i < 0 || i >= l->items_count )
353     {
354         return NULL;
355     }
356
357     return l->items[i];
358 }
359
360 /**********************************************************************
361  * hb_list_bytes
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 )
367 {
368     hb_buffer_t * buf;
369     int           ret;
370     int           i;
371
372     ret = 0;
373     for( i = 0; i < hb_list_count( l ); i++ )
374     {
375         buf  = hb_list_item( l, i );
376         ret += buf->size - buf->cur;
377     }
378
379     return ret;                                                                 
380 }
381
382 /**********************************************************************
383  * hb_list_seebytes
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 )
389 {
390     hb_buffer_t * buf;
391     int           copied;
392     int           copying;
393     int           i;
394     
395     for( i = 0, copied = 0; copied < size; i++ )
396     {
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 );
400         copied += copying;
401     }                                                                           
402 }
403
404 /**********************************************************************
405  * hb_list_getbytes
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
412  * in that buffer.
413  *********************************************************************/
414 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
415                        uint64_t * pts, uint64_t * pos )
416 {
417     hb_buffer_t * buf;
418     int           copied;
419     int           copying;
420     uint8_t       has_pts;
421     
422     /* So we won't have to deal with NULL pointers */
423      uint64_t dummy1, dummy2;
424
425     if( !pts ) pts = &dummy1;
426     if( !pos ) pos = &dummy2;
427
428     for( copied = 0, has_pts = 0; copied < size;  )
429     {
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 );
433
434         if( !has_pts )
435         {
436             *pts    = buf->start;
437             *pos    = buf->cur;
438             has_pts = 1;
439         }
440
441         buf->cur += copying;
442         if( buf->cur >= buf->size )
443         {
444             hb_list_rem( l, buf );
445             hb_buffer_close( &buf );
446         }
447
448         copied += copying;
449     }                                                                           
450 }
451
452 /**********************************************************************
453  * hb_list_empty
454  **********************************************************************
455  * Assuming all items are of type hb_buffer_t, close them all and
456  * close the list.
457  *********************************************************************/
458 void hb_list_empty( hb_list_t ** _l )
459 {
460     hb_list_t * l = *_l;
461     hb_buffer_t * b;
462
463     while( ( b = hb_list_item( l, 0 ) ) )
464     {
465         hb_list_rem( l, b );
466         hb_buffer_close( &b );
467     }
468
469     hb_list_close( _l );
470 }
471
472 /**********************************************************************
473  * hb_list_close
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 )
479 {
480     hb_list_t * l = *_l;
481
482     free( l->items );
483     free( l );
484
485     *_l = NULL;
486 }
487
488 /**********************************************************************
489  * hb_log
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, ... )
495 {
496     char        string[82]; /* 80 chars + \n + \0 */
497     time_t      _now;
498     struct tm * now;
499     va_list     args;
500
501     if( !getenv( "HB_DEBUG" ) )
502     {
503         /* We don't want to print it */
504         return;
505     }
506
507     /* Get the time */
508     _now = time( NULL );
509     now  = localtime( &_now );
510     sprintf( string, "[%02d:%02d:%02d] ",
511              now->tm_hour, now->tm_min, now->tm_sec );
512
513     /* Convert the message to a string */
514     va_start( args, log );
515     vsnprintf( string + 11, 69, log, args );
516     va_end( args );
517
518     /* Add the end of line */
519     strcat( string, "\n" );
520
521     /* Print it */
522     fprintf( stderr, "%s", string );
523 }
524
525 /**********************************************************************
526  * hb_title_init
527  **********************************************************************
528  * 
529  *********************************************************************/
530 hb_title_t * hb_title_init( char * dvd, int index )
531 {
532     hb_title_t * t;
533
534     t = calloc( sizeof( hb_title_t ), 1 );
535
536     t->index         = index;
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 );
541
542     return t;
543 }
544
545 /**********************************************************************
546  * hb_title_close
547  **********************************************************************
548  * 
549  *********************************************************************/
550 void hb_title_close( hb_title_t ** _t )
551 {
552     hb_title_t * t = *_t;
553     hb_audio_t * audio;
554     hb_chapter_t * chapter;
555     hb_subtitle_t * subtitle;
556
557     while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
558     {
559         hb_list_rem( t->list_audio, audio );
560         free( audio );
561     }
562     hb_list_close( &t->list_audio );
563     
564     while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
565     {
566         hb_list_rem( t->list_chapter, chapter );
567         free( chapter );
568     }
569     hb_list_close( &t->list_chapter );
570     
571     while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
572     {
573         hb_list_rem( t->list_subtitle, subtitle );
574         free( subtitle );
575     }
576     hb_list_close( &t->list_subtitle );
577
578     free( t );
579     *_t = NULL;
580 }
581