OSDN Git Service

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