OSDN Git Service

Double hb_log() max line length to avoid truncation of x264 options in display.
[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                 case HB_MUX_MKV:
181             overhead = 6;
182             break; 
183         case HB_MUX_AVI:
184             overhead = 24;
185             break; 
186         case HB_MUX_OGM:
187             overhead = 6;
188             break;
189         default:
190             return 0;
191     }
192
193     /* How many audio samples we put in each frame */
194     switch( job->acodec )
195     {
196         case HB_ACODEC_FAAC:
197         case HB_ACODEC_VORBIS:
198             samples_per_frame = 1024;
199             break;
200         case HB_ACODEC_LAME:
201             samples_per_frame = 1152;
202             break;
203         case HB_ACODEC_AC3:
204             samples_per_frame = 1536;
205             break;
206         default:
207             return 0;
208     }
209
210     /* Get the duration in seconds */
211     length = 0;
212     for( i = job->chapter_start; i <= job->chapter_end; i++ )
213     {
214         chapter = hb_list_item( title->list_chapter, i - 1 );
215         length += chapter->duration;
216     }
217     length += 135000;
218     length /= 90000;
219
220     /* Video overhead */
221     avail -= length * job->vrate * overhead / job->vrate_base;
222
223     for( i = 0; job->audios[i] >= 0; i++ )
224     {
225         /* Audio data */
226         int abitrate;
227         if( job->acodec & HB_ACODEC_AC3 )
228         {
229             audio = hb_list_item( title->list_audio, job->audios[i] );
230             abitrate = audio->bitrate / 8;
231         }
232         else
233         {
234             abitrate = job->abitrate * 1000 / 8;
235         }
236         avail -= length * abitrate;
237         
238         /* Audio overhead */
239         avail -= length * job->arate * overhead / samples_per_frame;
240     }
241
242     if( avail < 0 )
243     {
244         return 0;
245     }
246
247     return ( avail / ( 125 * length ) );
248 }
249
250 /**********************************************************************
251  * hb_list implementation
252  **********************************************************************
253  * Basic and slow, but enough for what we need
254  *********************************************************************/
255
256 #define HB_LIST_DEFAULT_SIZE 20
257
258 struct hb_list_s
259 {
260     /* Pointers to items in the list */
261     void ** items;
262
263     /* How many (void *) allocated in 'items' */
264     int     items_alloc;
265
266     /* How many valid pointers in 'items' */
267     int     items_count;
268 };
269
270 /**********************************************************************
271  * hb_list_init
272  **********************************************************************
273  * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
274  *********************************************************************/
275 hb_list_t * hb_list_init()
276 {
277     hb_list_t * l;
278
279     l              = calloc( sizeof( hb_list_t ), 1 );
280     l->items       = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
281     l->items_alloc = HB_LIST_DEFAULT_SIZE;
282
283     return l;
284 }
285
286 /**********************************************************************
287  * hb_list_count
288  **********************************************************************
289  * Returns the number of items currently in the list
290  *********************************************************************/
291 int hb_list_count( hb_list_t * l )
292 {
293     return l->items_count;
294 }
295
296 /**********************************************************************
297  * hb_list_add
298  **********************************************************************
299  * Adds an item at the end of the list, making it bigger if necessary.
300  * Can safely be called with a NULL pointer to add, it will be ignored.
301  *********************************************************************/
302 void hb_list_add( hb_list_t * l, void * p )
303 {
304     if( !p )
305     {
306         return;
307     }
308
309     if( l->items_count == l->items_alloc )
310     {
311         /* We need a bigger boat */
312         l->items_alloc += HB_LIST_DEFAULT_SIZE;
313         l->items        = realloc( l->items,
314                                    l->items_alloc * sizeof( void * ) );
315     }
316
317     l->items[l->items_count] = p;
318     (l->items_count)++;
319 }
320
321 /**********************************************************************
322  * hb_list_rem
323  **********************************************************************
324  * Remove an item from the list. Bad things will happen if called
325  * with a NULL pointer or if the item is not in the list.
326  *********************************************************************/
327 void hb_list_rem( hb_list_t * l, void * p )
328 {
329     int i;
330
331     /* Find the item in the list */
332     for( i = 0; i < l->items_count; i++ )
333     {
334         if( l->items[i] == p )
335         {
336             break;
337         }
338     }
339
340     /* Shift all items after it sizeof( void * ) bytes earlier */
341     memmove( &l->items[i], &l->items[i+1],
342              ( l->items_count - i - 1 ) * sizeof( void * ) );
343
344     (l->items_count)--;
345 }
346
347 /**********************************************************************
348  * hb_list_item
349  **********************************************************************
350  * Returns item at position i, or NULL if there are not that many
351  * items in the list
352  *********************************************************************/
353 void * hb_list_item( hb_list_t * l, int i )
354 {
355     if( i < 0 || i >= l->items_count )
356     {
357         return NULL;
358     }
359
360     return l->items[i];
361 }
362
363 /**********************************************************************
364  * hb_list_bytes
365  **********************************************************************
366  * Assuming all items are of type hb_buffer_t, returns the total
367  * number of bytes in the list
368  *********************************************************************/
369 int hb_list_bytes( hb_list_t * l )
370 {
371     hb_buffer_t * buf;
372     int           ret;
373     int           i;
374
375     ret = 0;
376     for( i = 0; i < hb_list_count( l ); i++ )
377     {
378         buf  = hb_list_item( l, i );
379         ret += buf->size - buf->cur;
380     }
381
382     return ret;                                                                 
383 }
384
385 /**********************************************************************
386  * hb_list_seebytes
387  **********************************************************************
388  * Assuming all items are of type hb_buffer_t, copy <size> bytes from
389  * the list to <dst>, keeping the list unmodified.
390  *********************************************************************/
391 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
392 {
393     hb_buffer_t * buf;
394     int           copied;
395     int           copying;
396     int           i;
397     
398     for( i = 0, copied = 0; copied < size; i++ )
399     {
400         buf     = hb_list_item( l, i );
401         copying = MIN( buf->size - buf->cur, size - copied );
402         memcpy( &dst[copied], &buf->data[buf->cur], copying );
403         copied += copying;
404     }                                                                           
405 }
406
407 /**********************************************************************
408  * hb_list_getbytes
409  **********************************************************************
410  * Assuming all items are of type hb_buffer_t, copy <size> bytes from
411  * the list to <dst>. What's copied is removed from the list.
412  * The variable pointed by <pts> is set to the PTS of the buffer the
413  * first byte has been got from.
414  * The variable pointed by <pos> is set to the position of that byte
415  * in that buffer.
416  *********************************************************************/
417 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
418                        uint64_t * pts, uint64_t * pos )
419 {
420     hb_buffer_t * buf;
421     int           copied;
422     int           copying;
423     uint8_t       has_pts;
424     
425     /* So we won't have to deal with NULL pointers */
426      uint64_t dummy1, dummy2;
427
428     if( !pts ) pts = &dummy1;
429     if( !pos ) pos = &dummy2;
430
431     for( copied = 0, has_pts = 0; copied < size;  )
432     {
433         buf     = hb_list_item( l, 0 );
434         copying = MIN( buf->size - buf->cur, size - copied );
435         memcpy( &dst[copied], &buf->data[buf->cur], copying );
436
437         if( !has_pts )
438         {
439             *pts    = buf->start;
440             *pos    = buf->cur;
441             has_pts = 1;
442         }
443
444         buf->cur += copying;
445         if( buf->cur >= buf->size )
446         {
447             hb_list_rem( l, buf );
448             hb_buffer_close( &buf );
449         }
450
451         copied += copying;
452     }                                                                           
453 }
454
455 /**********************************************************************
456  * hb_list_empty
457  **********************************************************************
458  * Assuming all items are of type hb_buffer_t, close them all and
459  * close the list.
460  *********************************************************************/
461 void hb_list_empty( hb_list_t ** _l )
462 {
463     hb_list_t * l = *_l;
464     hb_buffer_t * b;
465
466     while( ( b = hb_list_item( l, 0 ) ) )
467     {
468         hb_list_rem( l, b );
469         hb_buffer_close( &b );
470     }
471
472     hb_list_close( _l );
473 }
474
475 /**********************************************************************
476  * hb_list_close
477  **********************************************************************
478  * Free memory allocated by hb_list_init. Does NOT free contents of
479  * items still in the list.
480  *********************************************************************/
481 void hb_list_close( hb_list_t ** _l )
482 {
483     hb_list_t * l = *_l;
484
485     free( l->items );
486     free( l );
487
488     *_l = NULL;
489 }
490
491 /**********************************************************************
492  * hb_log
493  **********************************************************************
494  * If verbose mode is one, print message with timestamp. Messages
495  * longer than 180 characters are stripped ;p
496  *********************************************************************/
497 void hb_log( char * log, ... )
498 {
499     char        string[362]; /* 360 chars + \n + \0 */
500     time_t      _now;
501     struct tm * now;
502     va_list     args;
503
504     if( !getenv( "HB_DEBUG" ) )
505     {
506         /* We don't want to print it */
507         return;
508     }
509
510     /* Get the time */
511     _now = time( NULL );
512     now  = localtime( &_now );
513     sprintf( string, "[%02d:%02d:%02d] ",
514              now->tm_hour, now->tm_min, now->tm_sec );
515
516     /* Convert the message to a string */
517     va_start( args, log );
518     vsnprintf( string + 11, 169, log, args );
519     va_end( args );
520
521     /* Add the end of line */
522     strcat( string, "\n" );
523
524     /* Print it */
525     fprintf( stderr, "%s", string );
526 }
527
528 /**********************************************************************
529  * hb_error
530  **********************************************************************
531  * Using whatever output is available display this error. 
532  *********************************************************************/
533 void hb_error( char * log, ... )
534 {
535     char        string[181]; /* 180 chars + \0 */
536     va_list     args;
537
538     /* Convert the message to a string */
539     va_start( args, log );
540     vsnprintf( string, 180, log, args );
541     va_end( args );
542
543     /*
544      * Got the error in a single string, send it off to be dispatched.
545      */
546     if( error_handler )
547     {
548         error_handler( string );
549     } else {
550         hb_log( string );
551     }
552 }
553
554 void hb_register_error_handler( hb_error_handler_t * handler )
555 {
556     error_handler = handler;
557 }
558
559 /**********************************************************************
560  * hb_title_init
561  **********************************************************************
562  * 
563  *********************************************************************/
564 hb_title_t * hb_title_init( char * dvd, int index )
565 {
566     hb_title_t * t;
567
568     t = calloc( sizeof( hb_title_t ), 1 );
569
570     t->index         = index;
571     t->list_audio    = hb_list_init();
572     t->list_chapter  = hb_list_init();
573     t->list_subtitle = hb_list_init();
574     strcat( t->dvd, dvd );
575
576     return t;
577 }
578
579 /**********************************************************************
580  * hb_title_close
581  **********************************************************************
582  * 
583  *********************************************************************/
584 void hb_title_close( hb_title_t ** _t )
585 {
586     hb_title_t * t = *_t;
587     hb_audio_t * audio;
588     hb_chapter_t * chapter;
589     hb_subtitle_t * subtitle;
590
591     while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
592     {
593         hb_list_rem( t->list_audio, audio );
594         free( audio );
595     }
596     hb_list_close( &t->list_audio );
597     
598     while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
599     {
600         hb_list_rem( t->list_chapter, chapter );
601         free( chapter );
602     }
603     hb_list_close( &t->list_chapter );
604     
605     while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
606     {
607         hb_list_rem( t->list_subtitle, subtitle );
608         free( subtitle );
609     }
610     hb_list_close( &t->list_subtitle );
611
612     free( t );
613     *_t = NULL;
614 }
615
616 /**********************************************************************
617  * hb_filter_close
618  **********************************************************************
619  * 
620  *********************************************************************/
621 void hb_filter_close( hb_filter_object_t ** _f )
622 {
623     hb_filter_object_t * f = *_f;
624
625     f->close( f->private_data );
626
627     if( f->name )
628         free( f->name );
629     if( f->settings )
630         free( f->settings );
631
632     free( f );
633     *_f = NULL;
634 }
635