OSDN Git Service

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