OSDN Git Service

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