OSDN Git Service

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