OSDN Git Service

fix: cannot preview with QT
[handbrake-jp/handbrake-jp.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     va_list     args;
617
618     /* Convert the message to a string */
619     va_start( args, log );
620     vsnprintf( string, 180, log, args );
621     va_end( args );
622
623     /*
624      * Got the error in a single string, send it off to be dispatched.
625      */
626     if( error_handler )
627     {
628         error_handler( string );
629     } else {
630         hb_log( string );
631     }
632 }
633
634 void hb_register_error_handler( hb_error_handler_t * handler )
635 {
636     error_handler = handler;
637 }
638
639 /**********************************************************************
640  * hb_title_init
641  **********************************************************************
642  *
643  *********************************************************************/
644 hb_title_t * hb_title_init( char * dvd, int index )
645 {
646     hb_title_t * t;
647
648     t = calloc( sizeof( hb_title_t ), 1 );
649
650     t->index         = index;
651     t->list_audio    = hb_list_init();
652     t->list_chapter  = hb_list_init();
653     t->list_subtitle = hb_list_init();
654     strcat( t->dvd, dvd );
655     // default to decoding mpeg2
656     t->video_id      = 0xE0;
657     t->video_codec   = WORK_DECMPEG2;
658
659     return t;
660 }
661
662 /**********************************************************************
663  * hb_title_close
664  **********************************************************************
665  *
666  *********************************************************************/
667 void hb_title_close( hb_title_t ** _t )
668 {
669     hb_title_t * t = *_t;
670     hb_audio_t * audio;
671     hb_chapter_t * chapter;
672     hb_subtitle_t * subtitle;
673
674     while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
675     {
676         hb_list_rem( t->list_audio, audio );
677         free( audio );
678     }
679     hb_list_close( &t->list_audio );
680
681     while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) )
682     {
683         hb_list_rem( t->list_chapter, chapter );
684         free( chapter );
685     }
686     hb_list_close( &t->list_chapter );
687
688     while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
689     {
690         hb_list_rem( t->list_subtitle, subtitle );
691         free( subtitle );
692     }
693     hb_list_close( &t->list_subtitle );
694
695     if( t->metadata )
696     {
697         if( t->metadata->coverart )
698         {
699             free( t->metadata->coverart );
700         }
701         free( t->metadata );
702     }
703
704     free( t );
705     *_t = NULL;
706 }
707
708 /**********************************************************************
709  * hb_filter_close
710  **********************************************************************
711  *
712  *********************************************************************/
713 void hb_filter_close( hb_filter_object_t ** _f )
714 {
715     hb_filter_object_t * f = *_f;
716
717     f->close( f->private_data );
718
719     if( f->name )
720         free( f->name );
721     if( f->settings )
722         free( f->settings );
723
724     free( f );
725     *_f = NULL;
726 }
727
728 /**********************************************************************
729  * hb_audio_copy
730  **********************************************************************
731  *
732  *********************************************************************/
733 hb_audio_t *hb_audio_copy(const hb_audio_t *src)
734 {
735     hb_audio_t *audio = NULL;
736
737     if( src )
738     {
739         audio = calloc(1, sizeof(*audio));
740         memcpy(audio, src, sizeof(*audio));
741     }
742     return audio;
743 }
744
745 /**********************************************************************
746  * hb_audio_new
747  **********************************************************************
748  *
749  *********************************************************************/
750 void hb_audio_config_init(hb_audio_config_t * audiocfg)
751 {
752     /* Set read only paramaters to invalid values */
753     audiocfg->in.codec = 0xDEADBEEF;
754     audiocfg->in.bitrate = -1;
755     audiocfg->in.samplerate = -1;
756     audiocfg->in.channel_layout = 0;
757     audiocfg->in.version = 0;
758     audiocfg->in.mode = 0;
759     audiocfg->flags.ac3 = 0;
760     audiocfg->lang.description[0] = 0;
761     audiocfg->lang.simple[0] = 0;
762     audiocfg->lang.iso639_2[0] = 0;
763
764     /* Initalize some sensable defaults */
765     audiocfg->in.track = audiocfg->out.track = 0;
766     audiocfg->out.codec = HB_ACODEC_FAAC;
767     audiocfg->out.bitrate = 128;
768     audiocfg->out.samplerate = 44100;
769     audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII;
770     audiocfg->out.dynamic_range_compression = 0;
771     audiocfg->out.name = NULL;
772 }
773
774 /**********************************************************************
775  * hb_audio_add
776  **********************************************************************
777  *
778  *********************************************************************/
779 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
780 {
781     hb_title_t *title = job->title;
782     hb_audio_t *audio;
783
784     audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) );
785     if( audio == NULL )
786     {
787         /* We fail! */
788         return 0;
789     }
790
791     if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) )
792     {
793         /* This most likely means the client didn't call hb_audio_config_init
794          * so bail.
795          */
796         return 0;
797     }
798
799     /* Really shouldn't ignore the passed out track, but there is currently no
800      * way to handle duplicates or out-of-order track numbers.
801      */
802     audio->config.out.track = hb_list_count(job->list_audio) + 1;
803     audio->config.out.codec = audiocfg->out.codec;
804     if( audiocfg->out.codec == audio->config.in.codec )
805     {
806         /* Pass-through, copy from input. */
807         audio->config.out.samplerate = audio->config.in.samplerate;
808         audio->config.out.bitrate = audio->config.in.bitrate;
809         audio->config.out.dynamic_range_compression = 0;
810         audio->config.out.mixdown = 0;
811     }
812     else
813     {
814         /* Non pass-through, use what is given. */
815         audio->config.out.samplerate = audiocfg->out.samplerate;
816         audio->config.out.bitrate = audiocfg->out.bitrate;
817         audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression;
818         audio->config.out.mixdown = audiocfg->out.mixdown;
819     }
820
821     hb_list_add(job->list_audio, audio);
822     return 1;
823 }
824
825 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i)
826 {
827     hb_audio_t *audio = NULL;
828
829     if( (audio = hb_list_item(list, i)) )
830         return &(audio->config);
831
832     return NULL;
833 }
834
835 /**********************************************************************
836  * hb_subtitle_copy
837  **********************************************************************
838  *
839  *********************************************************************/
840 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
841 {
842     hb_subtitle_t *subtitle = NULL;
843
844     if( src )
845     {
846         subtitle = calloc(1, sizeof(*subtitle));
847         memcpy(subtitle, src, sizeof(*subtitle));
848     }
849     return subtitle;
850 }
851
852 /**********************************************************************
853  * hb_subtitle_add
854  **********************************************************************
855  *
856  *********************************************************************/
857 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
858 {
859     hb_title_t *title = job->title;
860     hb_subtitle_t *subtitle;
861
862     subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) );
863     if( subtitle == NULL )
864     {
865         /* We fail! */
866         return 0;
867     }
868     subtitle->config = *subtitlecfg;
869     hb_list_add(job->list_subtitle, subtitle);
870     return 1;
871 }
872
873 int hb_srt_add( const hb_job_t * job, 
874                 const hb_subtitle_config_t * subtitlecfg, 
875                 const char *lang )
876 {
877     hb_subtitle_t *subtitle;
878     iso639_lang_t *language = NULL;
879     int retval = 0;
880
881     subtitle = calloc( 1, sizeof( *subtitle ) );
882     
883     subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF;
884     subtitle->format = TEXTSUB;
885     subtitle->source = SRTSUB;
886
887     language = lang_for_code2( lang );
888
889     if( language )
890     {
891
892         strcpy( subtitle->lang, language->eng_name );
893         strncpy( subtitle->iso639_2, lang, 4 );
894         
895         subtitle->config = *subtitlecfg;
896         subtitle->config.dest = PASSTHRUSUB;
897
898         hb_list_add(job->list_subtitle, subtitle);
899         retval = 1;
900     }
901     return retval;
902 }