OSDN Git Service

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