OSDN Git Service

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