OSDN Git Service

Rate limit hb_error() messages to at most 1 message per second of the same message...
[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.fr/>.
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 #include "lang.h"
13 #include "hb.h"
14
15 /**********************************************************************
16  * Global variables
17  *********************************************************************/
18 hb_rate_t hb_video_rates[] =
19 { { "5",  5400000 }, { "10",     2700000 }, { "12", 2250000 },
20   { "15", 1800000 }, { "23.976", 1126125 }, { "24", 1125000 },
21   { "25", 1080000 }, { "29.97",  900900  } };
22 int hb_video_rates_count = sizeof( hb_video_rates ) /
23                            sizeof( hb_rate_t );
24
25 hb_rate_t hb_audio_rates[] =
26 { { "22.05", 22050 }, { "24", 24000 }, { "32", 32000 },
27   { "44.1",  44100 }, { "48", 48000 } };
28 int hb_audio_rates_count   = sizeof( hb_audio_rates ) /
29                              sizeof( hb_rate_t );
30 int hb_audio_rates_default = 3; /* 44100 Hz */
31
32 hb_rate_t hb_audio_bitrates[] =
33 { {  "32",  32 }, {  "40",  40 }, {  "48",  48 }, {  "56",  56 },
34   {  "64",  64 }, {  "80",  80 }, {  "96",  96 }, { "112", 112 },
35   { "128", 128 }, { "160", 160 }, { "192", 192 }, { "224", 224 },
36   { "256", 256 }, { "320", 320 }, { "384", 384 }, { "448", 448 },
37   { "768", 768 } };
38 int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) /
39                               sizeof( hb_rate_t );
40 int hb_audio_bitrates_default = 8; /* 128 kbps */
41
42 static hb_error_handler_t *error_handler = NULL;
43
44 hb_mixdown_t hb_audio_mixdowns[] =
45 { { "Mono",               "HB_AMIXDOWN_MONO",      "mono",   HB_AMIXDOWN_MONO      },
46   { "Stereo",             "HB_AMIXDOWN_STEREO",    "stereo", HB_AMIXDOWN_STEREO    },
47   { "Dolby Surround",     "HB_AMIXDOWN_DOLBY",     "dpl1",   HB_AMIXDOWN_DOLBY     },
48   { "Dolby Pro Logic II", "HB_AMIXDOWN_DOLBYPLII", "dpl2",   HB_AMIXDOWN_DOLBYPLII },
49   { "6-channel discrete", "HB_AMIXDOWN_6CH",       "6ch",    HB_AMIXDOWN_6CH       }
50 };
51 int hb_audio_mixdowns_count = sizeof( hb_audio_mixdowns ) /
52                               sizeof( hb_mixdown_t );
53
54 int hb_mixdown_get_mixdown_from_short_name( const char * short_name )
55 {
56     int i;
57     for (i = 0; i < hb_audio_mixdowns_count; i++)
58     {
59         if (strcmp(hb_audio_mixdowns[i].short_name, short_name) == 0)
60         {
61             return hb_audio_mixdowns[i].amixdown;
62         }
63     }
64     return 0;
65 }
66
67 const char * hb_mixdown_get_short_name_from_mixdown( int amixdown )
68 {
69     int i;
70     for (i = 0; i < hb_audio_mixdowns_count; i++)
71     {
72         if (hb_audio_mixdowns[i].amixdown == amixdown)
73         {
74             return hb_audio_mixdowns[i].short_name;
75         }
76     }
77     return "";
78 }
79
80 /**********************************************************************
81  * hb_reduce
82  **********************************************************************
83  * Given a numerator (num) and a denominator (den), reduce them to an
84  * equivalent fraction and store the result in x and y.
85  *********************************************************************/
86 void hb_reduce( int *x, int *y, int num, int den )
87 {
88     // find the greatest common divisor of num & den by Euclid's algorithm
89     int n = num, d = den;
90     while ( d )
91     {
92         int t = d;
93         d = n % d;
94         n = t;
95     }
96
97     // at this point n is the gcd. if it's non-zero remove it from num
98     // and den. Otherwise just return the original values.
99     if ( n )
100     {
101         *x = num / n;
102         *y = den / n;
103     }
104     else
105     {
106         *x = num;
107         *y = den;
108     }
109 }
110
111 /**********************************************************************
112  * hb_fix_aspect
113  **********************************************************************
114  * Given the output width (if HB_KEEP_WIDTH) or height
115  * (HB_KEEP_HEIGHT) and the current crop values, calculates the
116  * correct height or width in order to respect the DVD aspect ratio
117  *********************************************************************/
118 void hb_fix_aspect( hb_job_t * job, int keep )
119 {
120     hb_title_t * title = job->title;
121     int          i;
122
123     /* don't do anything unless the title has complete size info */
124     if ( title->height == 0 || title->width == 0 || title->aspect == 0 )
125     {
126         hb_log( "hb_fix_aspect: incomplete info for title %d: "
127                 "height = %d, width = %d, aspect = %.3f",
128                 title->index, title->height, title->width, title->aspect );
129         return;
130     }
131
132     /* Sanity checks:
133        Widths and heights must be multiples of 16 and greater than or
134        equal to 16
135        Crop values must be multiples of 2, greater than or equal to 0
136        and less than half of the dimension */
137     job->width   = MULTIPLE_16( job->width );
138     job->height  = MULTIPLE_16( job->height );
139     job->width   = MAX( 16, job->width );
140     job->height  = MAX( 16, job->height );
141     for( i = 0; i < 4; i++ )
142     {
143         job->crop[i] = EVEN( job->crop[i] );
144         job->crop[i] = MAX( 0, job->crop[i] );
145         if( i < 2 )
146         {
147             /* Top, bottom */
148             job->crop[i] = MIN( job->crop[i], ( title->height / 2 ) - 2 );
149         }
150         else
151         {
152             /* Left, right */
153             job->crop[i] = MIN( job->crop[i], ( title->width / 2 ) - 2 );
154         }
155     }
156
157     double par = (double)title->width / ( (double)title->height * title->aspect );
158     double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) /
159                          (double)(title->width - job->crop[2] - job->crop[3] );
160     double ar = par * cropped_sar;
161     if( keep == HB_KEEP_WIDTH )
162     {
163         job->height = MULTIPLE_16( (uint64_t)( (double)job->width * ar ) );
164         job->height = MAX( 16, job->height );
165     }
166     else
167     {
168         job->width = MULTIPLE_16( (uint64_t)( (double)job->height / ar ) );
169         job->width = MAX( 16, job->width );
170     }
171 }
172
173 /**********************************************************************
174  * hb_calc_bitrate
175  **********************************************************************
176  * size: in megabytes
177  *********************************************************************/
178 int hb_calc_bitrate( hb_job_t * job, int size )
179 {
180     int64_t avail = (int64_t) size * 1024 * 1024;
181     int64_t length;
182     int     overhead;
183     int     samples_per_frame;
184     int     i;
185
186     hb_title_t   * title = job->title;
187     hb_chapter_t * chapter;
188     hb_audio_t   * audio;
189
190     /* How many overhead bytes are used for each frame
191        (quite guessed) */
192     switch( job->mux )
193     {
194        case HB_MUX_MP4:
195        case HB_MUX_PSP:
196                 case HB_MUX_IPOD:
197                 case HB_MUX_MKV:
198             overhead = 6;
199             break;
200         case HB_MUX_AVI:
201             overhead = 24;
202             break;
203         case HB_MUX_OGM:
204             overhead = 6;
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     if( size == -1 )
221     {
222         hb_interjob_t * interjob = hb_interjob_get( job->h );
223         avail = job->vbitrate * 125 * length;
224         avail += length * interjob->vrate * overhead / interjob->vrate_base;
225     }
226
227     /* Video overhead */
228     avail -= length * job->vrate * overhead / job->vrate_base;
229
230     if( size == -1 )
231     {
232         goto ret;
233     }
234
235     for( i = 0; i < hb_list_count(job->list_audio); i++ )
236     {
237         /* Audio data */
238         int abitrate;
239         audio = hb_list_item( job->list_audio, i);
240
241         /* How many audio samples we put in each frame */
242         switch( audio->config.out.codec )
243         {
244             case HB_ACODEC_FAAC:
245             case HB_ACODEC_CA_AAC:
246             case HB_ACODEC_VORBIS:
247                 samples_per_frame = 1024;
248                 break;
249             case HB_ACODEC_LAME:
250                 samples_per_frame = 1152;
251                 break;
252             case HB_ACODEC_AC3:
253             case HB_ACODEC_DCA:
254                 samples_per_frame = 1536;
255                 break;
256             default:
257                 return 0;
258         }
259
260         if( audio->config.out.codec == HB_ACODEC_AC3 ||
261             audio->config.out.codec == HB_ACODEC_DCA)
262         {
263             /*
264              * For pass through we take the bitrate from the input audio
265              * bitrate as we are simply passing it through.
266              */
267             abitrate = audio->config.in.bitrate / 8;
268         }
269         else
270         {
271             /*
272              * Where we are transcoding the audio we use the destination
273              * bitrate.
274              */
275             abitrate = audio->config.out.bitrate * 1000 / 8;
276         }
277         avail -= length * abitrate;
278
279         /* Audio overhead */
280         avail -= length * audio->config.out.samplerate * overhead / samples_per_frame;
281     }
282
283 ret:
284     if( avail < 0 )
285     {
286         return 0;
287     }
288
289     return ( avail / ( 125 * length ) );
290 }
291
292 /**********************************************************************
293  * hb_list implementation
294  **********************************************************************
295  * Basic and slow, but enough for what we need
296  *********************************************************************/
297
298 #define HB_LIST_DEFAULT_SIZE 20
299
300 struct hb_list_s
301 {
302     /* Pointers to items in the list */
303     void ** items;
304
305     /* How many (void *) allocated in 'items' */
306     int     items_alloc;
307
308     /* How many valid pointers in 'items' */
309     int     items_count;
310 };
311
312 /**********************************************************************
313  * hb_list_init
314  **********************************************************************
315  * Allocates an empty list ready for HB_LIST_DEFAULT_SIZE items
316  *********************************************************************/
317 hb_list_t * hb_list_init()
318 {
319     hb_list_t * l;
320
321     l              = calloc( sizeof( hb_list_t ), 1 );
322     l->items       = calloc( HB_LIST_DEFAULT_SIZE * sizeof( void * ), 1 );
323     l->items_alloc = HB_LIST_DEFAULT_SIZE;
324
325     return l;
326 }
327
328 /**********************************************************************
329  * hb_list_count
330  **********************************************************************
331  * Returns the number of items currently in the list
332  *********************************************************************/
333 int hb_list_count( hb_list_t * l )
334 {
335     return l->items_count;
336 }
337
338 /**********************************************************************
339  * hb_list_add
340  **********************************************************************
341  * Adds an item at the end of the list, making it bigger if necessary.
342  * Can safely be called with a NULL pointer to add, it will be ignored.
343  *********************************************************************/
344 void hb_list_add( hb_list_t * l, void * p )
345 {
346     if( !p )
347     {
348         return;
349     }
350
351     if( l->items_count == l->items_alloc )
352     {
353         /* We need a bigger boat */
354         l->items_alloc += HB_LIST_DEFAULT_SIZE;
355         l->items        = realloc( l->items,
356                                    l->items_alloc * sizeof( void * ) );
357     }
358
359     l->items[l->items_count] = p;
360     (l->items_count)++;
361 }
362
363 /**********************************************************************
364  * hb_list_rem
365  **********************************************************************
366  * Remove an item from the list. Bad things will happen if called
367  * with a NULL pointer or if the item is not in the list.
368  *********************************************************************/
369 void hb_list_rem( hb_list_t * l, void * p )
370 {
371     int i;
372
373     /* Find the item in the list */
374     for( i = 0; i < l->items_count; i++ )
375     {
376         if( l->items[i] == p )
377         {
378             break;
379         }
380     }
381
382     /* Shift all items after it sizeof( void * ) bytes earlier */
383     memmove( &l->items[i], &l->items[i+1],
384              ( l->items_count - i - 1 ) * sizeof( void * ) );
385
386     (l->items_count)--;
387 }
388
389 /**********************************************************************
390  * hb_list_item
391  **********************************************************************
392  * Returns item at position i, or NULL if there are not that many
393  * items in the list
394  *********************************************************************/
395 void * hb_list_item( hb_list_t * l, int i )
396 {
397     if( i < 0 || i >= l->items_count )
398     {
399         return NULL;
400     }
401
402     return l->items[i];
403 }
404
405 /**********************************************************************
406  * hb_list_bytes
407  **********************************************************************
408  * Assuming all items are of type hb_buffer_t, returns the total
409  * number of bytes in the list
410  *********************************************************************/
411 int hb_list_bytes( hb_list_t * l )
412 {
413     hb_buffer_t * buf;
414     int           ret;
415     int           i;
416
417     ret = 0;
418     for( i = 0; i < hb_list_count( l ); i++ )
419     {
420         buf  = hb_list_item( l, i );
421         ret += buf->size - buf->cur;
422     }
423
424     return ret;
425 }
426
427 /**********************************************************************
428  * hb_list_seebytes
429  **********************************************************************
430  * Assuming all items are of type hb_buffer_t, copy <size> bytes from
431  * the list to <dst>, keeping the list unmodified.
432  *********************************************************************/
433 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size )
434 {
435     hb_buffer_t * buf;
436     int           copied;
437     int           copying;
438     int           i;
439
440     for( i = 0, copied = 0; copied < size; i++ )
441     {
442         buf     = hb_list_item( l, i );
443         copying = MIN( buf->size - buf->cur, size - copied );
444         memcpy( &dst[copied], &buf->data[buf->cur], copying );
445         copied += copying;
446     }
447 }
448
449 /**********************************************************************
450  * hb_list_getbytes
451  **********************************************************************
452  * Assuming all items are of type hb_buffer_t, copy <size> bytes from
453  * the list to <dst>. What's copied is removed from the list.
454  * The variable pointed by <pts> is set to the PTS of the buffer the
455  * first byte has been got from.
456  * The variable pointed by <pos> is set to the position of that byte
457  * in that buffer.
458  *********************************************************************/
459 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
460                        uint64_t * pts, uint64_t * pos )
461 {
462     hb_buffer_t * buf;
463     int           copied;
464     int           copying;
465     uint8_t       has_pts;
466
467     /* So we won't have to deal with NULL pointers */
468      uint64_t dummy1, dummy2;
469
470     if( !pts ) pts = &dummy1;
471     if( !pos ) pos = &dummy2;
472
473     for( copied = 0, has_pts = 0; copied < size;  )
474     {
475         buf     = hb_list_item( l, 0 );
476         copying = MIN( buf->size - buf->cur, size - copied );
477         memcpy( &dst[copied], &buf->data[buf->cur], copying );
478
479         if( !has_pts )
480         {
481             *pts    = buf->start;
482             *pos    = buf->cur;
483             has_pts = 1;
484         }
485
486         buf->cur += copying;
487         if( buf->cur >= buf->size )
488         {
489             hb_list_rem( l, buf );
490             hb_buffer_close( &buf );
491         }
492
493         copied += copying;
494     }
495 }
496
497 /**********************************************************************
498  * hb_list_empty
499  **********************************************************************
500  * Assuming all items are of type hb_buffer_t, close them all and
501  * close the list.
502  *********************************************************************/
503 void hb_list_empty( hb_list_t ** _l )
504 {
505     hb_list_t * l = *_l;
506     hb_buffer_t * b;
507
508     while( ( b = hb_list_item( l, 0 ) ) )
509     {
510         hb_list_rem( l, b );
511         hb_buffer_close( &b );
512     }
513
514     hb_list_close( _l );
515 }
516
517 /**********************************************************************
518  * hb_list_close
519  **********************************************************************
520  * Free memory allocated by hb_list_init. Does NOT free contents of
521  * items still in the list.
522  *********************************************************************/
523 void hb_list_close( hb_list_t ** _l )
524 {
525     hb_list_t * l = *_l;
526
527     free( l->items );
528     free( l );
529
530     *_l = NULL;
531 }
532
533 /**********************************************************************
534  * hb_log
535  **********************************************************************
536  * If verbose mode is one, print message with timestamp. Messages
537  * longer than 180 characters are stripped ;p
538  *********************************************************************/
539 void hb_log( char * log, ... )
540 {
541     char        string[362]; /* 360 chars + \n + \0 */
542     time_t      _now;
543     struct tm * now;
544     va_list     args;
545
546     if( !getenv( "HB_DEBUG" ) )
547     {
548         /* We don't want to print it */
549         return;
550     }
551
552     /* Get the time */
553     _now = time( NULL );
554     now  = localtime( &_now );
555     sprintf( string, "[%02d:%02d:%02d] ",
556              now->tm_hour, now->tm_min, now->tm_sec );
557
558     /* Convert the message to a string */
559     va_start( args, log );
560     vsnprintf( string + 11, 349, log, args );
561     va_end( args );
562
563     /* Add the end of line */
564     strcat( string, "\n" );
565
566     /* Print it */
567     fprintf( stderr, "%s", string );
568 }
569
570 int global_verbosity_level; //Necessary for hb_deep_log
571 /**********************************************************************
572  * hb_deep_log
573  **********************************************************************
574  * If verbose mode is >= level, print message with timestamp. Messages
575  * longer than 360 characters are stripped ;p
576  *********************************************************************/
577 void hb_deep_log( hb_debug_level_t level, char * log, ... )
578 {
579     char        string[362]; /* 360 chars + \n + \0 */
580     time_t      _now;
581     struct tm * now;
582     va_list     args;
583
584     if( global_verbosity_level < level )
585     {
586         /* Hiding message */
587         return;
588     }
589
590     /* Get the time */
591     _now = time( NULL );
592     now  = localtime( &_now );
593     sprintf( string, "[%02d:%02d:%02d] ",
594              now->tm_hour, now->tm_min, now->tm_sec );
595
596     /* Convert the message to a string */
597     va_start( args, log );
598     vsnprintf( string + 11, 349, log, args );
599     va_end( args );
600
601     /* Add the end of line */
602     strcat( string, "\n" );
603
604     /* Print it */
605     fprintf( stderr, "%s", string );
606 }
607
608 /**********************************************************************
609  * hb_error
610  **********************************************************************
611  * Using whatever output is available display this error.
612  *********************************************************************/
613 void hb_error( char * log, ... )
614 {
615     char        string[181]; /* 180 chars + \0 */
616     char        rep_string[181];
617     static char last_string[181];
618     static int  last_error_count = 0;
619     static uint64_t last_series_error_time = 0;
620     static hb_lock_t *mutex = 0;
621     va_list     args;
622     uint64_t time_now;
623
624     /* Convert the message to a string */
625     va_start( args, log );
626     vsnprintf( string, 180, log, args );
627     va_end( args );
628
629     if( !mutex )
630     {
631         mutex = hb_lock_init();
632     }
633
634     hb_lock( mutex );
635
636     time_now = hb_get_date();
637
638     if( strcmp( string, last_string) == 0 )
639     {
640         /*
641          * The last error and this one are the same, don't log it
642          * just count it instead, unless it was more than one second
643          * ago.
644          */
645         last_error_count++;
646         if( last_series_error_time + ( 1000 * 1 ) > time_now )
647         {
648             hb_unlock( mutex );
649             return;
650         } 
651     }
652     
653     /*
654      * A new error, or the same one more than 10sec since the last one
655      * did we have any of the same counted up?
656      */
657     if( last_error_count > 0 )
658     {
659         /*
660          * Print out the last error to ensure context for the last 
661          * repeated message.
662          */
663         if( error_handler )
664         {
665             error_handler( last_string );
666         } else {
667             hb_log( "%s", last_string );
668         }
669         
670         if( last_error_count > 1 )
671         {
672             /*
673              * Only print out the repeat message for more than 2 of the
674              * same, since we just printed out two of them already.
675              */
676             snprintf( rep_string, 180, "Last error repeated %d times", 
677                       last_error_count - 1 );
678             
679             if( error_handler )
680             {
681                 error_handler( rep_string );
682             } else {
683                 hb_log( "%s", rep_string );
684             }
685         }
686         
687         last_error_count = 0;
688     }
689
690     last_series_error_time = time_now;
691
692     strcpy( last_string, string );
693
694     /*
695      * Got the error in a single string, send it off to be dispatched.
696      */
697     if( error_handler )
698     {
699         error_handler( string );
700     } else {
701         hb_log( "%s", string );
702     }
703
704     hb_unlock( mutex );
705 }
706
707 void hb_register_error_handler( hb_error_handler_t * handler )
708 {
709     error_handler = handler;
710 }
711
712 /**********************************************************************
713  * hb_title_init
714  **********************************************************************
715  *
716  *********************************************************************/
717 hb_title_t * hb_title_init( char * dvd, int index )
718 {
719     hb_title_t * t;
720
721     t = calloc( sizeof( hb_title_t ), 1 );
722
723     t->index         = index;
724     t->list_audio    = hb_list_init();
725     t->list_chapter  = hb_list_init();
726     t->list_subtitle = hb_list_init();
727     strcat( t->dvd, dvd );
728     // default to decoding mpeg2
729     t->video_id      = 0xE0;
730     t->video_codec   = WORK_DECMPEG2;
731
732     return t;
733 }
734
735 /**********************************************************************
736  * hb_title_close
737  **********************************************************************
738  *
739  *********************************************************************/
740 void hb_title_close( hb_title_t ** _t )
741 {
742     hb_title_t * t = *_t;
743     hb_audio_t * audio;
744     hb_chapter_t * chapter;
745     hb_subtitle_t * subtitle;
746
747     while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
748     {
749         hb_list_rem( t->list_audio, audio );
750         free( audio );
751     }
752     hb_list_close( &t->list_audio );
753
754     while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
755     {
756         hb_list_rem( t->list_chapter, chapter );
757         free( chapter );
758     }
759     hb_list_close( &t->list_chapter );
760
761     while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
762     {
763         hb_list_rem( t->list_subtitle, subtitle );
764         free( subtitle );
765     }
766     hb_list_close( &t->list_subtitle );
767
768     if( t->metadata )
769     {
770         if( t->metadata->coverart )
771         {
772             free( t->metadata->coverart );
773         }
774         free( t->metadata );
775     }
776
777     free( t );
778     *_t = NULL;
779 }
780
781 /**********************************************************************
782  * hb_filter_close
783  **********************************************************************
784  *
785  *********************************************************************/
786 void hb_filter_close( hb_filter_object_t ** _f )
787 {
788     hb_filter_object_t * f = *_f;
789
790     f->close( f->private_data );
791
792     if( f->name )
793         free( f->name );
794     if( f->settings )
795         free( f->settings );
796
797     free( f );
798     *_f = NULL;
799 }
800
801 /**********************************************************************
802  * hb_audio_copy
803  **********************************************************************
804  *
805  *********************************************************************/
806 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
807 {
808     hb_audio_t *audio = NULL;
809
810     if( src )
811     {
812         audio = calloc(1, sizeof(*audio));
813         memcpy(audio, src, sizeof(*audio));
814     }
815     return audio;
816 }
817
818 /**********************************************************************
819  * hb_audio_new
820  **********************************************************************
821  *
822  *********************************************************************/
823 void hb_audio_config_init(hb_audio_config_t * audiocfg)
824 {
825     /* Set read only paramaters to invalid values */
826     audiocfg->in.codec = 0xDEADBEEF;
827     audiocfg->in.bitrate = -1;
828     audiocfg->in.samplerate = -1;
829     audiocfg->in.channel_layout = 0;
830     audiocfg->in.version = 0;
831     audiocfg->in.mode = 0;
832     audiocfg->flags.ac3 = 0;
833     audiocfg->lang.description[0] = 0;
834     audiocfg->lang.simple[0] = 0;
835     audiocfg->lang.iso639_2[0] = 0;
836
837     /* Initalize some sensable defaults */
838     audiocfg->in.track = audiocfg->out.track = 0;
839     audiocfg->out.codec = HB_ACODEC_FAAC;
840     audiocfg->out.bitrate = 128;
841     audiocfg->out.samplerate = 44100;
842     audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
843     audiocfg->out.dynamic_range_compression = 0;
844     audiocfg->out.name = NULL;
845 }
846
847 /**********************************************************************
848  * hb_audio_add
849  **********************************************************************
850  *
851  *********************************************************************/
852 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
853 {
854     hb_title_t *title = job->title;
855     hb_audio_t *audio;
856
857     audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
858     if( audio == NULL )
859     {
860         /* We fail! */
861         return 0;
862     }
863
864     if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
865     {
866         /* This most likely means the client didn't call hb_audio_config_init
867          * so bail.
868          */
869         return 0;
870     }
871
872     /* Really shouldn't ignore the passed out track, but there is currently no
873      * way to handle duplicates or out-of-order track numbers.
874      */
875     audio->config.out.track = hb_list_count(job->list_audio) + 1;
876     audio->config.out.codec = audiocfg->out.codec;
877     if( audiocfg->out.codec == audio->config.in.codec )
878     {
879         /* Pass-through, copy from input. */
880         audio->config.out.samplerate = audio->config.in.samplerate;
881         audio->config.out.bitrate = audio->config.in.bitrate;
882         audio->config.out.dynamic_range_compression = 0;
883         audio->config.out.mixdown = 0;
884     }
885     else
886     {
887         /* Non pass-through, use what is given. */
888         audio->config.out.samplerate = audiocfg->out.samplerate;
889         audio->config.out.bitrate = audiocfg->out.bitrate;
890         audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
891         audio->config.out.mixdown = audiocfg->out.mixdown;
892     }
893
894     hb_list_add(job->list_audio, audio);
895     return 1;
896 }
897
898 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
899 {
900     hb_audio_t *audio = NULL;
901
902     if( (audio = hb_list_item(list, i)) )
903         return &(audio->config);
904
905     return NULL;
906 }
907
908 /**********************************************************************
909  * hb_subtitle_copy
910  **********************************************************************
911  *
912  *********************************************************************/
913 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
914 {
915     hb_subtitle_t *subtitle = NULL;
916
917     if( src )
918     {
919         subtitle = calloc(1, sizeof(*subtitle));
920         memcpy(subtitle, src, sizeof(*subtitle));
921     }
922     return subtitle;
923 }
924
925 /**********************************************************************
926  * hb_subtitle_add
927  **********************************************************************
928  *
929  *********************************************************************/
930 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
931 {
932     hb_title_t *title = job->title;
933     hb_subtitle_t *subtitle;
934
935     subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
936     if( subtitle == NULL )
937     {
938         /* We fail! */
939         return 0;
940     }
941     subtitle->config = *subtitlecfg;
942     hb_list_add(job->list_subtitle, subtitle);
943     return 1;
944 }
945
946 int hb_srt_add( const hb_job_t * job, 
947                 const hb_subtitle_config_t * subtitlecfg, 
948                 const char *lang )
949 {
950     hb_subtitle_t *subtitle;
951     iso639_lang_t *language = NULL;
952     int retval = 0;
953
954     subtitle = calloc( 1, sizeof( *subtitle ) );
955     
956     subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
957     subtitle->format = TEXTSUB;
958     subtitle->source = SRTSUB;
959
960     language = lang_for_code2( lang );
961
962     if( language )
963     {
964
965         strcpy( subtitle->lang, language->eng_name );
966         strncpy( subtitle->iso639_2, lang, 4 );
967         
968         subtitle->config = *subtitlecfg;
969         subtitle->config.dest = PASSTHRUSUB;
970
971         hb_list_add(job->list_subtitle, subtitle);
972         retval = 1;
973     }
974     return retval;
975 }