OSDN Git Service

060daaecfc6b360db1a56ccdde8de4b8c025c1aa
[handbrake-jp/handbrake-jp-git.git] / libhb / scan.c
1 /* $Id: scan.c,v 1.52 2005/11/25 15:05:25 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 "hb.h"
8 #include "a52dec/a52.h"
9 #include "dca.h"
10
11 typedef struct
12 {
13     hb_handle_t * h;
14
15     char        * path;
16     int           title_index;
17     hb_list_t   * list_title;
18
19     hb_dvd_t    * dvd;
20         hb_stream_t * stream;
21
22 } hb_scan_t;
23
24 static void ScanFunc( void * );
25 static int  DecodePreviews( hb_scan_t *, hb_title_t * title );
26 static void LookForAudio( hb_title_t * title, hb_buffer_t * b );
27 static int  AllAudioOK( hb_title_t * title );
28
29 static const char *aspect_to_string( int aspect )
30 {
31     switch ( aspect )
32     {
33         case HB_ASPECT_BASE * 1 / 1:    return "1:1";
34         case HB_ASPECT_BASE * 4 / 3:    return "4:3";
35         case HB_ASPECT_BASE * 16 / 9:   return "16:9";
36         case HB_ASPECT_BASE * 221 / 100:   return "2.21:1";
37     }
38     static char arstr[32];
39     double a = (double)aspect / HB_ASPECT_BASE;
40     sprintf( arstr, aspect >= 1.? "%.2f:1" : "1:%.2f", a );
41     return arstr;
42 }
43
44 hb_thread_t * hb_scan_init( hb_handle_t * handle, const char * path,
45                             int title_index, hb_list_t * list_title )
46 {
47     hb_scan_t * data = calloc( sizeof( hb_scan_t ), 1 );
48
49     data->h            = handle;
50     data->path         = strdup( path );
51     data->title_index  = title_index;
52     data->list_title   = list_title;
53
54     return hb_thread_init( "scan", ScanFunc, data, HB_NORMAL_PRIORITY );
55 }
56
57 static void ScanFunc( void * _data )
58 {
59     hb_scan_t  * data = (hb_scan_t *) _data;
60     hb_title_t * title;
61     int          i;
62
63         data->dvd = NULL;
64         data->stream = NULL;
65
66     /* Try to open the path as a DVD. If it fails, try as a file */
67     hb_log( "scan: trying to open with libdvdread" );
68     if( ( data->dvd = hb_dvd_init( data->path ) ) )
69     {
70         hb_log( "scan: DVD has %d title(s)",
71                 hb_dvd_title_count( data->dvd ) );
72         if( data->title_index )
73         {
74             /* Scan this title only */
75             hb_list_add( data->list_title, hb_dvd_title_scan( data->dvd,
76                             data->title_index ) );
77         }
78         else
79         {
80             /* Scan all titles */
81             for( i = 0; i < hb_dvd_title_count( data->dvd ); i++ )
82             {
83                 hb_list_add( data->list_title,
84                              hb_dvd_title_scan( data->dvd, i + 1 ) );
85             }
86         }
87     }
88     else if ( (data->stream = hb_stream_open( data->path, 0 ) ) != NULL )
89     {
90         hb_list_add( data->list_title, hb_stream_title_scan( data->stream ) );
91     }
92     else
93     {
94         hb_log( "scan: unrecognized file type" );
95         return;
96     }
97
98     for( i = 0; i < hb_list_count( data->list_title ); )
99     {
100         int j;
101         hb_state_t state;
102         hb_audio_t * audio;
103         hb_title_t * title_tmp = NULL;
104
105         title = hb_list_item( data->list_title, i );
106
107         /* I've seen a DVD with strictly identical titles. Check this
108            here and ignore it if redundant */
109         for( j = 0; j < i; j++ )
110         {
111             title_tmp = hb_list_item( data->list_title, j );
112             if( title->vts         == title_tmp->vts &&
113                 title->block_start == title_tmp->block_start &&
114                 title->block_end   == title_tmp->block_end &&
115                 title->block_count == title_tmp->block_count )
116             {
117                 break;
118             }
119             else
120             {
121                 title_tmp = NULL;
122             }
123         }
124         if( title_tmp )
125         {
126             hb_log( "scan: title %d is duplicate with title %d",
127                     title->index, title_tmp->index );
128             hb_list_rem( data->list_title, title );
129             free( title );      /* This _will_ leak! */
130             continue;
131         }
132
133 #define p state.param.scanning
134         /* Update the UI */
135         state.state   = HB_STATE_SCANNING;
136         p.title_cur   = title->index;
137         p.title_count = data->dvd ? hb_dvd_title_count( data->dvd ) : hb_list_count(data->list_title);
138         hb_set_state( data->h, &state );
139 #undef p
140
141         /* Decode previews */
142         /* this will also detect more AC3 / DTS information */
143         if( !DecodePreviews( data, title ) )
144         {
145             /* TODO: free things */
146             hb_list_rem( data->list_title, title );
147             continue;
148         }
149
150         /* Make sure we found audio rates and bitrates */
151         for( j = 0; j < hb_list_count( title->list_audio ); )
152         {
153             audio = hb_list_item( title->list_audio, j );
154             if( !audio->config.in.bitrate )
155             {
156                 hb_log( "scan: removing audio 0x%x because no bitrate found",
157                         audio->id );
158                 hb_list_rem( title->list_audio, audio );
159                 free( audio );
160                 continue;
161             }
162             j++;
163         }
164
165         /* If we don't have any audio streams left, remove the title */
166         if( !hb_list_count( title->list_audio ) )
167         {
168             hb_list_rem( data->list_title, title );
169             free( title );
170             continue;
171         }
172
173         i++;
174     }
175
176     /* Init jobs templates */
177     for( i = 0; i < hb_list_count( data->list_title ); i++ )
178     {
179         hb_job_t * job;
180
181         title      = hb_list_item( data->list_title, i );
182         job        = calloc( sizeof( hb_job_t ), 1 );
183         title->job = job;
184
185         job->title = title;
186
187         /* Set defaults settings */
188         job->chapter_start = 1;
189         job->chapter_end   = hb_list_count( title->list_chapter );
190
191         /* Autocrop by default. Gnark gnark */
192         memcpy( job->crop, title->crop, 4 * sizeof( int ) );
193
194         /* Preserve a source's pixel aspect, if it's available. */
195         if( title->pixel_aspect_width && title->pixel_aspect_height )
196         {
197             job->pixel_aspect_width  = title->pixel_aspect_width;
198             job->pixel_aspect_height = title->pixel_aspect_height;
199         }
200
201         if( title->aspect == 16 && !job->pixel_aspect_width && !job->pixel_aspect_height)
202         {
203             hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
204                        16 * title->height, 9 * title->width );
205         }
206         else if( !job->pixel_aspect_width && !job->pixel_aspect_height )
207         {
208             hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
209                        4 * title->height, 3 * title->width );
210         }
211
212         job->width = title->width - job->crop[2] - job->crop[3];
213         hb_fix_aspect( job, HB_KEEP_WIDTH );
214         if( job->height > title->height - job->crop[0] - job->crop[1] )
215         {
216             job->height = title->height - job->crop[0] - job->crop[1];
217             hb_fix_aspect( job, HB_KEEP_HEIGHT );
218         }
219
220         hb_log( "scan: title (%d) job->width:%d, job->height:%d",
221                 i, job->width, job->height );
222
223         job->keep_ratio = 1;
224
225         job->vcodec     = HB_VCODEC_FFMPEG;
226         job->vquality   = -1.0;
227         job->vbitrate   = 1000;
228         job->pass       = 0;
229         job->vrate      = title->rate;
230         job->vrate_base = title->rate_base;
231
232         job->list_audio = hb_list_init();
233
234         job->subtitle = -1;
235
236         job->mux = HB_MUX_MP4;
237     }
238
239     if( data->dvd )
240     {
241         hb_dvd_close( &data->dvd );
242     }
243         if (data->stream)
244         {
245                 hb_stream_close(&data->stream);
246         }
247     free( data->path );
248     free( data );
249     _data = NULL;
250 }
251
252 // -----------------------------------------------
253 // stuff related to cropping
254
255 #define DARK 32
256
257 static inline int absdiff( int x, int y )
258 {
259     return x < y ? y - x : x - y;
260 }
261
262 static inline int clampBlack( int x ) 
263 {
264     // luma 'black' is 16 and anything less should be clamped at 16
265     return x < 16 ? 16 : x;
266 }
267
268 static int row_all_dark( hb_title_t *title, uint8_t* luma, int row )
269 {
270     luma += title->width * row;
271
272     // compute the average luma value of the row
273     int i, avg = 0;
274     for ( i = 0; i < title->width; ++i )
275     {
276         avg += clampBlack( luma[i] );
277     }
278     avg /= title->width;
279     if ( avg >= DARK )
280         return 0;
281
282     // since we're trying to detect smooth borders, only take the row if
283     // all pixels are within +-16 of the average (this range is fairly coarse
284     // but there's a lot of quantization noise for luma values near black
285     // so anything less will fail to crop because of the noise).
286     for ( i = 0; i < title->width; ++i )
287     {
288         if ( absdiff( avg, clampBlack( luma[i] ) ) > 16 )
289             return 0;
290     }
291     return 1;
292 }
293
294 static int column_all_dark( hb_title_t *title, uint8_t* luma, int top, int bottom,
295                             int col )
296 {
297     int stride = title->width;
298     int height = title->height - top - bottom;
299     luma += stride * top + col;
300
301     // compute the average value of the column
302     int i = height, avg = 0, row = 0;
303     for ( ; --i >= 0; row += stride )
304     {
305         avg += clampBlack( luma[row] );
306     }
307     avg /= height;
308     if ( avg >= DARK )
309         return 0;
310
311     // since we're trying to detect smooth borders, only take the column if
312     // all pixels are within +-16 of the average.
313     i = height, row = 0;
314     for ( ; --i >= 0; row += stride )
315     {
316         if ( absdiff( avg, clampBlack( luma[row] ) ) > 16 )
317             return 0;
318     }
319     return 1;
320 }
321 #undef DARK
322
323 typedef struct {
324     int n;
325     int t[10];
326     int b[10];
327     int l[10];
328     int r[10];
329 } crop_record_t;
330
331 static void record_crop( crop_record_t *crops, int t, int b, int l, int r )
332 {
333     crops->t[crops->n] = t;
334     crops->b[crops->n] = b;
335     crops->l[crops->n] = l;
336     crops->r[crops->n] = r;
337     ++crops->n;
338 }
339
340 static int compare_int( const void *a, const void *b )
341 {
342     return *(const int *)a - *(const int *)b;
343 }
344
345 static void sort_crops( crop_record_t *crops )
346 {
347     qsort( crops->t, crops->n, sizeof(crops->t[0]), compare_int );
348     qsort( crops->b, crops->n, sizeof(crops->t[0]), compare_int );
349     qsort( crops->l, crops->n, sizeof(crops->t[0]), compare_int );
350     qsort( crops->r, crops->n, sizeof(crops->t[0]), compare_int );
351 }
352
353 // -----------------------------------------------
354 // stuff related to title width/height/aspect info
355
356 typedef struct {
357     int count;              /* number of times we've seen this info entry */
358     hb_work_info_t info;    /* copy of info entry */
359 } info_list_t;
360
361 static void remember_info( info_list_t *info_list, hb_work_info_t *info )
362 {
363     for ( ; info_list->count; ++info_list )
364     {
365         if ( memcmp( &info_list->info, info, sizeof(*info) ) == 0 )
366         {
367             // we found a match - bump its count
368             ++info_list->count;
369             return;
370         }
371     }
372     // no match found - add new entry to list (info_list points to
373     // the first free slot). NB - we assume that info_list was allocated
374     // so that it's big enough even if there are no dups. I.e., 10 slots
375     // allocated if there are 10 previews.
376     info_list->count = 1;
377     info_list->info = *info;
378 }
379
380 static void most_common_info( info_list_t *info_list, hb_work_info_t *info )
381 {
382     int i, biggest = 0;
383     for ( i = 1; info_list[i].count; ++i )
384     {
385         if ( info_list[i].count > info_list[biggest].count )
386             biggest = i;
387     }
388     *info = info_list[biggest].info;
389     free( info_list );
390 }
391
392 /***********************************************************************
393  * DecodePreviews
394  ***********************************************************************
395  * Decode 10 pictures for the given title.
396  * It assumes that data->reader and data->vts have successfully been
397  * DVDOpen()ed and ifoOpen()ed.
398  **********************************************************************/
399 static int DecodePreviews( hb_scan_t * data, hb_title_t * title )
400 {
401     int             i, npreviews = 0;
402     hb_buffer_t   * buf_ps, * buf_es;
403     hb_list_t     * list_es;
404     int progressive_count = 0;
405     int interlaced_preview_count = 0;
406     info_list_t * info_list = calloc( 10+1, sizeof(*info_list) );
407     crop_record_t *crops = calloc( 1, sizeof(*crops) );
408
409     buf_ps   = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
410     list_es  = hb_list_init();
411
412     hb_log( "scan: decoding previews for title %d", title->index );
413
414     if (data->dvd)
415       hb_dvd_start( data->dvd, title->index, 1 );
416
417     for( i = 0; i < 10; i++ )
418     {
419         int j;
420         FILE * file_preview;
421         char   filename[1024];
422
423         if (data->dvd)
424         {
425           if( !hb_dvd_seek( data->dvd, (float) ( i + 1 ) / 11.0 ) )
426           {
427               continue;
428           }
429         }
430         else if (data->stream)
431         {
432           /* we start reading streams at zero rather than 1/11 because
433            * short streams may have only one sequence header in the entire
434            * file and we need it to decode any previews. */
435           if (!hb_stream_seek(data->stream, (float) i / 11.0 ) )
436           {
437               continue;
438           }
439         }
440
441         hb_log( "scan: preview %d", i + 1 );
442
443         int vcodec = title->video_codec? title->video_codec : WORK_DECMPEG2;
444         hb_work_object_t *vid_decoder = hb_get_work( vcodec );
445         vid_decoder->codec_param = title->video_codec_param;
446         vid_decoder->init( vid_decoder, NULL );
447         hb_buffer_t * vid_buf = NULL;
448
449         for( j = 0; j < 10240 ; j++ )
450         {
451             if (data->dvd)
452             {
453               if( !hb_dvd_read( data->dvd, buf_ps ) )
454               {
455                   hb_log( "Warning: Could not read data for preview %d, skipped", i + 1 );
456                   goto skip_preview;
457               }
458             }
459             else if (data->stream)
460             {
461               if ( !hb_stream_read(data->stream,buf_ps) )
462               {
463                   hb_log( "Warning: Could not read data for preview %d, skipped", i + 1 );
464                   goto skip_preview;
465               }
466             }
467             if ( title->demuxer == HB_NULL_DEMUXER )
468             {
469                 hb_demux_null( buf_ps, list_es, 0 );
470             }
471             else
472             {
473                 hb_demux_ps( buf_ps, list_es, 0 );
474             }
475
476             while( ( buf_es = hb_list_item( list_es, 0 ) ) )
477             {
478                 hb_list_rem( list_es, buf_es );
479                 if( buf_es->id == title->video_id && vid_buf == NULL )
480                 {
481                     vid_decoder->work( vid_decoder, &buf_es, &vid_buf );
482                 }
483                 else if( ! AllAudioOK( title ) )
484                 {
485                     LookForAudio( title, buf_es );
486                 }
487                 if ( buf_es )
488                     hb_buffer_close( &buf_es );
489             }
490
491             if( vid_buf && AllAudioOK( title ) )
492                 break;
493         }
494
495         if( ! vid_buf )
496         {
497             hb_log( "scan: could not get a decoded picture" );
498             continue;
499         }
500
501         /* Get size and rate infos */
502
503         hb_work_info_t vid_info;
504         vid_decoder->info( vid_decoder, &vid_info );
505         vid_decoder->close( vid_decoder );
506         free( vid_decoder );
507
508         remember_info( info_list, &vid_info );
509
510         title->width = vid_info.width;
511         title->height = vid_info.height;
512         title->rate = vid_info.rate;
513         title->rate_base = vid_info.rate_base;
514
515         if( title->rate_base == 1126125 )
516         {
517             /* Frame FPS is 23.976 (meaning it's progressive), so
518                start keeping track of how many are reporting at
519                that speed. When enough show up that way, we want
520                to make that the overall title FPS.
521             */
522             progressive_count++;
523
524             if( progressive_count < 6 )
525             {
526                 /* Not enough frames are reporting as progressive,
527                    which means we should be conservative and use
528                    29.97 as the title's FPS for now.
529                 */
530                 title->rate_base = 900900;
531             }
532             else
533             {
534                 /* A majority of the scan frames are progressive. Make that
535                     the title's FPS, and announce it once to the log.
536                 */
537                 if( progressive_count == 6 )
538                 {
539                     hb_log("Title's mostly NTSC Film, setting fps to 23.976");
540                 }
541                 title->rate_base = 1126125;
542             }
543         }
544         else if( title->rate_base == 900900 && progressive_count >= 6 )
545         {
546             /*
547              * We've already deduced that the frame rate is 23.976, so set it
548              * back again.
549              */
550             title->rate_base = 1126125;
551         }
552
553         while( ( buf_es = hb_list_item( list_es, 0 ) ) )
554         {
555             hb_list_rem( list_es, buf_es );
556             hb_buffer_close( &buf_es );
557         }
558
559         /* Check preview for interlacing artifacts */
560         if( hb_detect_comb( vid_buf, title->width, title->height, 10, 30, 9, 10, 30, 9 ) )
561         {
562             hb_log("Interlacing detected in preview frame %i", i+1);
563             interlaced_preview_count++;
564         }
565
566         hb_get_tempory_filename( data->h, filename, "%x%d",
567                                  (intptr_t)title, i );
568
569         file_preview = fopen( filename, "w" );
570         if( file_preview )
571         {
572             fwrite( vid_buf->data, title->width * title->height * 3 / 2,
573                     1, file_preview );
574             fclose( file_preview );
575         }
576         else
577         {
578             hb_log( "scan: fopen failed (%s)", filename );
579         }
580
581         /* Detect black borders */
582
583 #define Y    vid_buf->data
584         int top, bottom, left, right;
585         int h4 = title->height / 4, w4 = title->width / 4;
586
587         // When widescreen content is matted to 16:9 or 4:3 there's sometimes
588         // a thin border on the outer edge of the matte. On TV content it can be
589         // "line 21" VBI data that's normally hidden in the overscan. For HD
590         // content it can just be a diagnostic added in post production so that
591         // the frame borders are visible. We try to ignore these borders so
592         // we can crop the matte. The border width depends on the resolution
593         // (12 pixels on 1080i looks visually the same as 4 pixels on 480i)
594         // so we allow the border to be up to 1% of the frame height.
595         const int border = title->height / 100;
596
597         for ( top = border; top < h4; ++top )
598         {
599             if ( ! row_all_dark( title, Y, top ) )
600                 break;
601         }
602         if ( top <= border )
603         {
604             // we never made it past the border region - see if the rows we
605             // didn't check are dark or if we shouldn't crop at all.
606             for ( top = 0; top < border; ++top )
607             {
608                 if ( ! row_all_dark( title, Y, top ) )
609                     break;
610             }
611             if ( top >= border )
612             {
613                 top = 0;
614             }
615         }
616         for ( bottom = border; bottom < h4; ++bottom )
617         {
618             if ( ! row_all_dark( title, Y, title->height - 1 - bottom ) )
619                 break;
620         }
621         if ( bottom <= border )
622         {
623             for ( bottom = 0; bottom < border; ++bottom )
624             {
625                 if ( ! row_all_dark( title, Y, title->height - 1 - bottom ) )
626                     break;
627             }
628             if ( bottom >= border )
629             {
630                 bottom = 0;
631             }
632         }
633         for ( left = 0; left < w4; ++left )
634         {
635             if ( ! column_all_dark( title, Y, top, bottom, left ) )
636                 break;
637         }
638         for ( right = 0; right < w4; ++right )
639         {
640             if ( ! column_all_dark( title, Y, top, bottom, title->width - 1 - right ) )
641                 break;
642         }
643
644         // only record the result if all the crops are less than a quarter of
645         // the frame otherwise we can get fooled by frames with a lot of black
646         // like titles, credits & fade-thru-black transitions.
647         if ( top < h4 && bottom < h4 && left < w4 && right < w4 )
648         {
649             record_crop( crops, top, bottom, left, right );
650         }
651         ++npreviews;
652
653 skip_preview:
654         if ( vid_buf )
655             hb_buffer_close( &vid_buf );
656     }
657
658     if ( npreviews )
659     {
660         // use the most common frame info for our final title dimensions
661         hb_work_info_t vid_info;
662         most_common_info( info_list, &vid_info );
663
664         title->width = vid_info.width;
665         title->height = vid_info.height;
666         title->pixel_aspect_width = vid_info.pixel_aspect_width;
667         title->pixel_aspect_height = vid_info.pixel_aspect_height;
668
669         // compute the aspect ratio based on the storage dimensions and the
670         // pixel aspect ratio (if supplied) or just storage dimensions if no PAR.
671         title->aspect = ( (double)title->width / (double)title->height + 0.05 ) *
672                         HB_ASPECT_BASE;
673
674         double aspect = (double)title->width / (double)title->height;
675         if( title->pixel_aspect_width && title->pixel_aspect_height )
676         {
677             aspect *= (double)title->pixel_aspect_width /
678                       (double)title->pixel_aspect_height;
679         }
680         title->aspect = ( aspect + 0.05 ) * HB_ASPECT_BASE;
681
682         // don't try to crop unless we got at least 3 previews
683         if ( crops->n > 2 )
684         {
685             sort_crops( crops );
686             // The next line selects median cropping - at least
687             // 50% of the frames will have their borders removed.
688             // Other possible choices are loose cropping (i = 0) where 
689             // no non-black pixels will be cropped from any frame and a
690             // tight cropping (i = crops->n - (crops->n >> 2)) where at
691             // least 75% of the frames will have their borders removed.
692             i = crops->n >> 1;
693             title->crop[0] = EVEN( crops->t[i] );
694             title->crop[1] = EVEN( crops->b[i] );
695             title->crop[2] = EVEN( crops->l[i] );
696             title->crop[3] = EVEN( crops->r[i] );
697         }
698         free( crops );
699
700         hb_log( "scan: %d previews, %dx%d, %.3f fps, autocrop = %d/%d/%d/%d, "
701                 "aspect %s, PAR %d:%d",
702                 npreviews, title->width, title->height, (float) title->rate /
703                 (float) title->rate_base,
704                 title->crop[0], title->crop[1], title->crop[2], title->crop[3],
705                 aspect_to_string( title->aspect ), title->pixel_aspect_width,
706                 title->pixel_aspect_height );
707
708         if( interlaced_preview_count >= ( npreviews / 2 ) )
709         {
710             hb_log("Title is likely interlaced or telecined (%i out of %i previews). You should do something about that.",
711                    interlaced_preview_count, npreviews);
712             title->detected_interlacing = 1;
713         }
714         else
715         {
716             title->detected_interlacing = 0;
717         }
718     }
719
720     hb_buffer_close( &buf_ps );
721     while( ( buf_es = hb_list_item( list_es, 0 ) ) )
722     {
723         hb_list_rem( list_es, buf_es );
724         hb_buffer_close( &buf_es );
725     }
726     hb_list_close( &list_es );
727     if (data->dvd)
728       hb_dvd_stop( data->dvd );
729
730     return npreviews;
731 }
732
733 /*
734  * This routine is called for every frame from a non-video elementary stream.
735  * These are a mix of audio & subtitle streams, some of which we want & some
736  * we're ignoring. This routine checks the frame against all our audio streams
737  * to see if it's one we want and haven't identified yet. If yes, it passes the
738  * frame to a codec-specific id routine which is responsible for filling in
739  * the sample rate, bit rate, channels & other audio parameters.
740  *
741  * Since a sample rate is essential for further audio processing, any audio
742  * stream which isn't successfully id'd by is deleted at the end of the scan.
743  * This is necessary to avoid ambiguities where things that might be audio
744  * aren't (e.g., some European DVD Teletext streams use the same IDs as US ATSC
745  * AC-3 audio).
746  */
747 static void LookForAudio( hb_title_t * title, hb_buffer_t * b )
748 {
749     int i;
750
751     hb_audio_t * audio = NULL;
752     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
753     {
754         audio = hb_list_item( title->list_audio, i );
755         /* check if this elementary stream is one we want */
756         if ( audio->id == b->id )
757         {
758             break;
759         }
760         else
761         {
762             audio = NULL;
763         }
764     }
765     if( !audio || audio->config.in.bitrate != 0 )
766     {
767         /* not found or already done */
768         return;
769     }
770
771     hb_work_object_t *w = hb_codec_decoder( audio->config.in.codec );
772
773     if ( w == NULL || w->bsinfo == NULL )
774     {
775         hb_log( "Internal error in scan: unhandled audio type %d for id 0x%x",
776                 audio->config.in.codec, audio->id );
777         goto drop_audio;
778     }
779
780     hb_work_info_t info;
781     w->audio = audio;
782     w->codec_param = audio->config.in.codec_param;
783     int ret = w->bsinfo( w, b, &info );
784     if ( ret < 0 )
785     {
786         hb_log( "no info on audio type %d/0x%x for id 0x%x",
787                 audio->config.in.codec, audio->config.in.codec_param,
788                 audio->id );
789         goto drop_audio;
790     }
791     if ( !info.bitrate )
792     {
793         /* didn't find any info */
794         return;
795     }
796     audio->config.in.samplerate = info.rate;
797     audio->config.in.bitrate = info.bitrate;
798     audio->config.in.channel_layout = info.channel_layout;
799     audio->config.flags.ac3 = info.flags;
800
801     // update the audio description string based on the info we found
802     if ( audio->config.flags.ac3 & AUDIO_F_DOLBY )
803     {
804         strcat( audio->config.lang.description, " (Dolby Surround)" );
805     }
806     else
807     {
808         int layout = audio->config.in.channel_layout;
809         char *desc = audio->config.lang.description +
810                         strlen( audio->config.lang.description );
811         sprintf( desc, " (%d.%d ch)",
812                  HB_INPUT_CH_LAYOUT_GET_DISCRETE_FRONT_COUNT(layout) +
813                      HB_INPUT_CH_LAYOUT_GET_DISCRETE_REAR_COUNT(layout),
814                  HB_INPUT_CH_LAYOUT_GET_DISCRETE_LFE_COUNT(layout) );
815     }
816
817     hb_log( "scan: audio 0x%x: %s, rate=%dHz, bitrate=%d %s", audio->id,
818             info.name, audio->config.in.samplerate, audio->config.in.bitrate,
819             audio->config.lang.description );
820  
821     free( w );
822     return;
823
824     // We get here if there's no hope of finding info on an audio bitstream,
825     // either because we don't have a decoder (or a decoder with a bitstream
826     // info proc) or because the decoder's info proc said that the stream
827     // wasn't something it could handle. Delete the item from the title's
828     // audio list so we won't keep reading packets while trying to get its
829     // bitstream info.
830  drop_audio:
831     if ( w )
832         free( w );
833
834     hb_list_rem( title->list_audio, audio );
835 }
836
837 /*
838  * This routine checks to see if we've ID'd all the audio streams associated
839  * with a title. It returns 0 if there are more to ID & 1 if all are done.
840  */
841 static int  AllAudioOK( hb_title_t * title )
842 {
843     int i;
844     hb_audio_t * audio;
845
846     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
847     {
848         audio = hb_list_item( title->list_audio, i );
849         if( audio->config.in.bitrate == 0 )
850         {
851             return 0;
852         }
853     }
854     return 1;
855 }