OSDN Git Service

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