OSDN Git Service

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