OSDN Git Service

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