OSDN Git Service

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