OSDN Git Service

added call to av_log_set_level in hb_init_real based on value of verbose param.
[handbrake-jp/handbrake-jp-git.git] / libmediafork / 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.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "mediafork.h"
8 #include "a52dec/a52.h"
9
10 typedef struct
11 {
12     hb_handle_t * h;
13     
14     char        * path;
15     int           title_index;
16     hb_list_t   * list_title;
17     
18     hb_dvd_t    * dvd;
19
20 } hb_scan_t;
21
22 static void ScanFunc( void * );
23 static int  DecodePreviews( hb_scan_t *, hb_title_t * title );
24 static void LookForAC3( hb_title_t * title, hb_buffer_t * b );
25 static int  AllAC3OK( hb_title_t * title );
26
27 hb_thread_t * hb_scan_init( hb_handle_t * handle, const char * path,
28                             int title_index, hb_list_t * list_title )
29 {
30     hb_scan_t * data = calloc( sizeof( hb_scan_t ), 1 );
31
32     data->h            = handle;
33     data->path         = strdup( path );
34     data->title_index  = title_index;
35     data->list_title   = list_title;
36
37     return hb_thread_init( "scan", ScanFunc, data, HB_NORMAL_PRIORITY );
38 }
39
40 static void ScanFunc( void * _data )
41 {
42     hb_scan_t  * data = (hb_scan_t *) _data;
43     hb_title_t * title;
44     int          i;
45
46     /* Try to open the path as a DVD. If it fails, try as a file */
47     hb_log( "scan: trying to open with libdvdread" );
48     if( ( data->dvd = hb_dvd_init( data->path ) ) )
49     {
50         hb_log( "scan: DVD has %d title(s)",
51                 hb_dvd_title_count( data->dvd ) );
52         if( data->title_index )
53         {
54             /* Scan this title only */
55             hb_list_add( data->list_title, hb_dvd_title_scan( data->dvd,
56                             data->title_index ) );
57         }
58         else
59         {
60             /* Scan all titles */
61             for( i = 0; i < hb_dvd_title_count( data->dvd ); i++ )
62             {
63                 hb_list_add( data->list_title,
64                              hb_dvd_title_scan( data->dvd, i + 1 ) );
65             }
66         }
67     }
68     else
69     {
70         /* Open as a VOB file */
71         FILE * file;
72         hb_log( "scan: trying to open as VOB file" );
73         file = fopen( data->path, "rb" );
74         if( file )
75         {
76             /* XXX */
77             fclose( file );
78         }
79         else
80         {
81             hb_log( "scan: fopen failed" );
82             return;
83         }
84     }
85
86     for( i = 0; i < hb_list_count( data->list_title ); )
87     {
88         int j;
89         hb_state_t state;
90         hb_audio_t * audio;
91         hb_title_t * title_tmp = NULL;
92
93         title = hb_list_item( data->list_title, i );
94
95         /* I've seen a DVD with strictly identical titles. Check this
96            here and ignore it if redundant */
97         for( j = 0; j < i; j++ )
98         {
99             title_tmp = hb_list_item( data->list_title, j );
100             if( title->vts         == title_tmp->vts &&
101                 title->block_start == title_tmp->block_start &&
102                 title->block_end   == title_tmp->block_end &&
103                 title->block_count == title_tmp->block_count )
104             {
105                 break;
106             }
107             else
108             {
109                 title_tmp = NULL;
110             }
111         }
112         if( title_tmp )
113         {
114             hb_log( "scan: title %d is duplicate with title %d",
115                     title->index, title_tmp->index );
116             hb_list_rem( data->list_title, title );
117             free( title );
118             continue;
119         }
120
121 #define p state.param.scanning
122         /* Update the UI */
123         state.state   = HB_STATE_SCANNING;
124         p.title_cur   = title->index;
125         p.title_count = hb_dvd_title_count( data->dvd );
126         hb_set_state( data->h, &state );
127 #undef p
128
129         /* Decode previews */
130         if( !DecodePreviews( data, title ) )
131         {
132             /* TODO: free things */
133             hb_list_rem( data->list_title, title );
134             continue;
135         }
136
137         /* Make sure we found AC3 rates and bitrates */
138         for( j = 0; j < hb_list_count( title->list_audio ); )
139         {
140             audio = hb_list_item( title->list_audio, j );
141             if( audio->codec == HB_ACODEC_AC3 &&
142                 !audio->bitrate )
143             {
144                 hb_list_rem( title->list_audio, audio );
145                 free( audio );
146                 continue;
147             }
148             j++;
149         }
150
151         /* Do we still have audio */
152         if( !hb_list_count( title->list_audio ) )
153         {
154             hb_list_rem( data->list_title, title );
155             free( title );
156             continue;
157         }
158         
159         i++;
160     }
161
162     /* Init jobs templates */
163     for( i = 0; i < hb_list_count( data->list_title ); i++ )
164     {
165         hb_job_t * job;
166
167         title      = hb_list_item( data->list_title, i );
168         job        = calloc( sizeof( hb_job_t ), 1 );
169         title->job = job;
170
171         job->title = title;
172
173         /* Set defaults settings */
174         job->chapter_start = 1;
175         job->chapter_end   = hb_list_count( title->list_chapter );
176
177         /* Autocrop by default. Gnark gnark */
178         memcpy( job->crop, title->crop, 4 * sizeof( int ) );
179
180         if( title->aspect == 16 )
181         {
182             hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
183                        16 * title->height, 9 * title->width );
184         }
185         else
186         {
187             hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
188                        4 * title->height, 3 * title->width );
189         }
190
191         job->width = title->width - job->crop[2] - job->crop[3];
192 //        job->height = title->height - job->crop[0] - job->crop[1];
193         hb_fix_aspect( job, HB_KEEP_WIDTH );
194         if( job->height > title->height - job->crop[0] - job->crop[1] )
195         {
196             job->height = title->height - job->crop[0] - job->crop[1];
197             hb_fix_aspect( job, HB_KEEP_HEIGHT );
198         }
199
200     hb_log( "scan: title (%d) job->width:%d, job->height:%d",
201             i,job->width, job->height );
202
203         job->keep_ratio = 1;
204
205         job->vcodec     = HB_VCODEC_FFMPEG;
206         job->vquality   = -1.0;
207         job->vbitrate   = 1000;
208         job->pass       = 0;
209         job->vrate      = title->rate;
210         job->vrate_base = title->rate_base;
211
212         job->audios[0] = 0;
213         job->audios[1] = -1;
214
215         job->acodec   = HB_ACODEC_FAAC;
216         job->abitrate = 128;
217         job->arate    = 44100;
218
219         job->subtitle = -1;
220
221         job->mux = HB_MUX_MP4;
222     }
223
224     if( data->dvd )
225     {
226         hb_dvd_close( &data->dvd );
227     }
228 }
229
230 /***********************************************************************
231  * DecodePreviews
232  ***********************************************************************
233  * Decode 10 pictures for the given title.
234  * It assumes that data->reader and data->vts have successfully been
235  * DVDOpen()ed and ifoOpen()ed.
236  **********************************************************************/
237 static int DecodePreviews( hb_scan_t * data, hb_title_t * title )
238 {
239     int             i, ret;
240     hb_buffer_t   * buf_ps, * buf_es, * buf_raw;
241     hb_list_t     * list_es, * list_raw;
242     hb_libmpeg2_t * mpeg2;
243
244     buf_ps   = hb_buffer_init( 2048 );
245     list_es  = hb_list_init();
246     list_raw = hb_list_init();
247
248     hb_log( "scan: decoding previews for title %d", title->index );
249
250     hb_dvd_start( data->dvd, title->index, 1 );
251
252     for( i = 0; i < 10; i++ )
253     {
254         int j, k;
255         FILE * file_preview;
256         char   filename[1024];
257
258         if( !hb_dvd_seek( data->dvd, (float) ( i + 1 ) / 11.0 ) )
259         {
260             goto error;
261         }
262
263         hb_log( "scan: preview %d", i + 1 );
264
265         mpeg2 = hb_libmpeg2_init();
266
267         for( j = 0; j < 10240 ; j++ )
268         {
269             if( !hb_dvd_read( data->dvd, buf_ps ) )
270             {
271                 goto error;
272             }
273             hb_demux_ps( buf_ps, list_es );
274
275             while( ( buf_es = hb_list_item( list_es, 0 ) ) )
276             {
277                 hb_list_rem( list_es, buf_es );
278                 if( buf_es->id == 0xE0 && !hb_list_count( list_raw ) )
279                 {
280                     hb_libmpeg2_decode( mpeg2, buf_es, list_raw );
281                 }
282                 else if( !i )
283                 {
284                     LookForAC3( title, buf_es );
285                 }
286                 hb_buffer_close( &buf_es );
287
288                 if( hb_list_count( list_raw ) &&
289                     ( i || AllAC3OK( title ) ) )
290                 {
291                     /* We got a picture */
292                     break;
293                 }
294             }
295
296             if( hb_list_count( list_raw ) &&
297                 ( i || AllAC3OK( title ) ) )
298             {
299                 break;
300             }
301         }
302
303         if( !hb_list_count( list_raw ) )
304         {
305             hb_log( "scan: could not get a decoded picture" );
306             goto error;
307         }
308
309         if( !i )
310         {
311             /* Get size and rate infos */
312             title->rate = 27000000;
313             hb_libmpeg2_info( mpeg2, &title->width, &title->height,
314                               &title->rate_base );
315             title->crop[0] = title->crop[1] = title->height / 2;
316             title->crop[2] = title->crop[3] = title->width / 2;
317         }
318
319         hb_libmpeg2_close( &mpeg2 );
320
321         while( ( buf_es = hb_list_item( list_es, 0 ) ) )
322         {
323             hb_list_rem( list_es, buf_es );
324             hb_buffer_close( &buf_es );
325         }
326
327         buf_raw = hb_list_item( list_raw, 0 );
328
329         hb_get_tempory_filename( data->h, filename, "%x%d",
330                                  (int) title, i );
331
332         file_preview = fopen( filename, "w" );
333         if( file_preview )
334         {
335             fwrite( buf_raw->data, title->width * title->height * 3 / 2,
336                     1, file_preview );
337             fclose( file_preview );
338         }
339         else
340         {
341             hb_log( "scan: fopen failed (%s)", filename );
342         }
343
344 #define Y    buf_raw->data
345 #define DARK 64
346         
347         /* Detect black borders */
348         
349         for( j = 0; j < title->width; j++ )
350         {
351             for( k = 0; k < title->crop[0]; k++ )
352                 if( Y[ k * title->width + j ] > DARK )
353                 {
354                     title->crop[0] = k;
355                     break;
356                 }
357             for( k = 0; k < title->crop[1]; k++ )
358                 if( Y[ ( title->height - k - 1 ) *
359                        title->width + j ] > DARK )
360                 {
361                     title->crop[1] = k;
362                     break;
363                 }
364         }
365         for( j = 0; j < title->height; j++ )
366         {
367             for( k = 0; k < title->crop[2]; k++ ) 
368                 if( Y[ j * title->width + k ] > DARK )
369                 {
370                     title->crop[2] = k;
371                     break;
372                 }
373             for( k = 0; k < title->crop[3]; k++ )
374                 if( Y[ j * title->width +
375                         title->width - k - 1 ] > DARK )
376                 {
377                     title->crop[3] = k;
378                     break;
379                 }
380         }
381
382         while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
383         {
384             hb_list_rem( list_raw, buf_raw );
385             hb_buffer_close( &buf_raw );
386         }
387     }
388
389     title->crop[0] = EVEN( title->crop[0] );
390     title->crop[1] = EVEN( title->crop[1] );
391     title->crop[2] = EVEN( title->crop[2] );
392     title->crop[3] = EVEN( title->crop[3] );
393
394     hb_log( "scan: %dx%d, %.3f fps, autocrop = %d/%d/%d/%d",
395             title->width, title->height, (float) title->rate /
396             (float) title->rate_base, title->crop[0], title->crop[1],
397             title->crop[2], title->crop[3] );
398
399     ret = 1;
400     goto cleanup;
401
402 error:
403     ret = 0;
404
405 cleanup:
406     hb_buffer_close( &buf_ps );
407     while( ( buf_es = hb_list_item( list_es, 0 ) ) )
408     {
409         hb_list_rem( list_es, buf_es );
410         hb_buffer_close( &buf_es );
411     }
412     hb_list_close( &list_es );
413     while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
414     {
415         hb_list_rem( list_raw, buf_raw );
416         hb_buffer_close( &buf_raw );
417     }
418     hb_list_close( &list_raw );
419     hb_dvd_stop( data->dvd );
420     return ret;
421 }
422
423 static void LookForAC3( hb_title_t * title, hb_buffer_t * b ) 
424 {
425     int i;
426     int flags;
427     int rate;
428     int bitrate;
429
430     /* Figure out if this is a AC3 buffer for a known track */
431     hb_audio_t * audio = NULL;
432     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
433     {
434         audio = hb_list_item( title->list_audio, i );
435         if( audio->codec == HB_ACODEC_AC3 &&
436             audio->id    == b->id )
437         {
438             break;
439         }
440         else
441         {
442             audio = NULL;
443         }
444     }
445     if( !audio )
446     {
447         return;
448     }
449
450     if( audio->bitrate )
451     {
452         /* Already done for this track */
453         return;
454     }
455
456     for( i = 0; i < b->size - 7; i++ )
457     {
458         if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) )
459         {
460             hb_log( "scan: rate=%dHz, bitrate=%d", rate, bitrate );
461             audio->rate    = rate;
462             audio->bitrate = bitrate;
463             switch( flags & A52_CHANNEL_MASK )
464             {
465                 case A52_MONO:
466                 case A52_CHANNEL1:
467                 case A52_CHANNEL2:
468                     audio->channels = 1;
469                     break;
470                 case A52_STEREO:
471                 case A52_DOLBY:
472                 case A52_CHANNEL:
473                     audio->channels = 2;
474                     break;
475                 case A52_3F:
476                 case A52_2F1R:
477                     audio->channels = 3;
478                     break;
479                 case A52_3F1R:
480                 case A52_2F2R:
481                     audio->channels = 4;
482                     break;
483                 case A52_3F2R:
484                     audio->channels = 5;
485                     break;
486             }
487             /* XXX */
488             sprintf( audio->lang + strlen( audio->lang ),
489                      " (%d ch)", audio->channels );
490             break;
491         }
492     }
493 }
494
495 static int  AllAC3OK( hb_title_t * title )
496 {
497     int i;
498     hb_audio_t * audio;
499
500     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
501     {
502         audio = hb_list_item( title->list_audio, i );
503         if( audio->codec == HB_ACODEC_AC3 &&
504             !audio->bitrate )
505         {
506             return 0;
507         }
508     }
509
510     return 1;
511 }