OSDN Git Service

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