OSDN Git Service

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