OSDN Git Service

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