1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/* $Id: Scan.c,v 1.5 2003/11/09 21:28:22 titer Exp $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.m0k.org/>.
It may be used under the terms of the GNU General Public License. */
#include "Fifo.h"
#include "Languages.h"
#include "Scan.h"
#include "Thread.h"
#include <dvdread/ifo_types.h>
#include <dvdplay/dvdplay.h>
#include <dvdplay/info.h>
#include <dvdplay/state.h>
#include <dvdplay/nav.h>
#include <mpeg2dec/mpeg2.h>
/* Local prototypes */
static void ScanThread( void * );
static HBTitle * ScanTitle( HBScan *, dvdplay_ptr vmg, int index );
static int DecodeFrame( HBScan * s, dvdplay_ptr vmg,
HBTitle * title, int which );
static char * LanguageForCode( int code );
struct HBScan
{
HBHandle * handle;
char * device;
int title;
int die;
HBThread * thread;
};
HBScan * HBScanInit( HBHandle * handle, char * device, int title )
{
HBScan * s;
if( !( s = malloc( sizeof( HBScan ) ) ) )
{
HBLog( "HBScanInit: malloc() failed, gonna crash" );
return NULL;
}
s->handle = handle;
s->device = strdup( device );
s->title = title;
s->die = 0;
s->thread = HBThreadInit( "scan", ScanThread, s,
HB_NORMAL_PRIORITY );
return s;
}
void HBScanClose( HBScan ** _s )
{
HBScan * s = *_s;
s->die = 1;
HBThreadClose( &s->thread );
free( s->device );
free( s );
*_s = NULL;
}
static void ScanThread( void * _s )
{
HBScan * s = (HBScan*) _s;
dvdplay_ptr vmg;
HBList * titleList = HBListInit();
HBTitle * title;
int i;
HBLog( "HBScan: opening device %s", s->device );
HBScanning( s->handle, 0 );
vmg = dvdplay_open( s->device, NULL, NULL );
if( !vmg )
{
HBLog( "HBScan: dvdplay_open() failed (%s)", s->device );
HBScanDone( s->handle, titleList );
return;
}
/* Detect titles */
for( i = ( s->title ? s->title - 1 : 0 );
i < ( s->title ? s->title : dvdplay_title_nr( vmg ) );
i++ )
{
if( s->die )
{
break;
}
if( !( title = ScanTitle( s, vmg, i + 1 ) ) )
{
continue;
}
HBListAdd( titleList, title );
}
HBLog( "HBScan: closing device %s", s->device );
dvdplay_close( vmg );
HBScanDone( s->handle, titleList );
}
static HBTitle * ScanTitle( HBScan * s, dvdplay_ptr vmg, int index )
{
HBTitle * title;
int audio_nr, foo;
audio_attr_t * attr;
HBAudio * audio;
int i;
uint8_t dummy[DVD_VIDEO_LB_LEN];
HBLog( "HBScan: scanning title %d", index );
HBScanning( s->handle, index );
title = HBTitleInit( s->device, index );
dvdplay_start( vmg, index );
/* Length */
title->length = dvdplay_title_time( vmg );
HBLog( "HBScan: title length is %d seconds", title->length );
/* Discard titles under 10 seconds */
if( title->length < 10 )
{
HBLog( "HBScan: ignoring title %d (too short)", index );
HBTitleClose( &title );
return NULL;
}
/* Detect languages */
dvdplay_audio_info( vmg, &audio_nr, &foo );
for( i = 0; i < audio_nr; i++ )
{
int id;
int j;
if( s->die )
{
break;
}
id = dvdplay_audio_id( vmg, i );
if( id < 1 )
{
continue;
}
if( ( id & 0xF0FF ) != 0x80BD )
{
HBLog( "HBScan: non-AC3 audio track detected, ignoring" );
continue;
}
/* Check if we don't already found an track with the same id */
audio = NULL;
for( j = 0; j < HBListCountItems( title->audioList ); j++ )
{
audio = (HBAudio*) HBListItemAt( title->audioList, j );
if( id == audio->id )
{
break;
}
else
{
audio = NULL;
}
}
if( audio )
{
HBLog( "HBScan: discarding duplicate track %x", id );
continue;
}
attr = dvdplay_audio_attr( vmg, j );
audio = HBAudioInit( id, LanguageForCode( attr->lang_code ) );
HBLog( "HBScan: new language (%x, %s)", id, audio->language );
HBListAdd( title->audioList, audio );
}
/* Discard titles with no audio tracks */
if( !HBListCountItems( title->audioList ) )
{
HBLog( "HBScan: ignoring title %d (no audio track)", index );
HBTitleClose( &title );
return NULL;
}
/* Kludge : libdvdplay wants we to read a first block before seeking */
dvdplay_read( vmg, dummy, 1 );
for( i = 0; i < 10; i++ )
{
if( s->die )
{
break;
}
if( !DecodeFrame( s, vmg, title, i ) )
{
HBLog( "HBScan: ignoring title %d (could not decode)",
index );
HBTitleClose( &title );
return NULL;
}
}
if( title->inHeight * title->aspect >
title->inWidth * VOUT_ASPECT_FACTOR )
{
title->outWidthMax = title->inWidth;
title->outHeightMax = MULTIPLE_16( (uint64_t)title->inWidth *
VOUT_ASPECT_FACTOR / title->aspect );
}
else
{
title->outWidthMax = MULTIPLE_16( (uint64_t)title->inHeight *
title->aspect / VOUT_ASPECT_FACTOR );
title->outHeightMax = title->inHeight;
}
/* Default picture size */
title->outWidth = title->outWidthMax;
title->outHeight = title->outHeightMax;
return title;
}
static int DecodeFrame( HBScan * s, dvdplay_ptr vmg,
HBTitle * title, int which )
{
int titleFirst = dvdplay_title_first( vmg );
int titleEnd = dvdplay_title_end( vmg );
int pictureStart = ( which + 1 ) * ( titleEnd - titleFirst ) / 11;
int pictureEnd = titleFirst + ( which + 2 ) *
( titleEnd - titleFirst ) / 11;
mpeg2dec_t * handle;
const mpeg2_info_t * info;
mpeg2_state_t state;
char fileName[1024];
FILE * file;
HBList * esBufferList = HBListInit();
HBBuffer * psBuffer = NULL;
HBBuffer * esBuffer = NULL;
/* Seek to the right place */
dvdplay_seek( vmg, pictureStart );
/* Init libmpeg2 */
handle = mpeg2_init();
info = mpeg2_info( handle );
/* Init the destination file */
memset( fileName, 0, 1024 );
sprintf( fileName, "/tmp/HB.%d.%d.%d", HBGetPid( s->handle ),
title->index, which );
file = fopen( fileName, "w" );
#define CLEANUP \
while( ( esBuffer = (HBBuffer*) HBListItemAt( esBufferList, 0 ) ) ) \
{ \
HBListRemove( esBufferList, esBuffer ); \
HBBufferClose( &esBuffer ); \
} \
HBListClose( &esBufferList ); \
if( psBuffer ) HBBufferClose( &psBuffer ); \
if( esBuffer ) HBBufferClose( &esBuffer ); \
mpeg2_close( handle ); \
fclose( file )
for( ;; )
{
state = mpeg2_parse( handle );
if( state == STATE_BUFFER )
{
/* Free the previous buffer */
if( esBuffer )
{
HBBufferClose( &esBuffer );
}
/* Get a new one */
while( !esBuffer )
{
while( !HBListCountItems( esBufferList ) )
{
psBuffer = HBBufferInit( DVD_VIDEO_LB_LEN );
if( dvdplay_read( vmg, psBuffer->data, 1 ) != 1 ||
!HBPStoES( &psBuffer, esBufferList ) )
{
HBLog( "HBScan: failed to get a valid PS "
"packet" );
CLEANUP;
return 0;
}
if( dvdplay_position( vmg ) >= pictureEnd )
{
HBLog( "HBScan: gone too far, aborting" );
CLEANUP;
return 0;
}
}
esBuffer = (HBBuffer*) HBListItemAt( esBufferList, 0 );
HBListRemove( esBufferList, esBuffer );
if( esBuffer->streamId != 0xE0 )
{
HBBufferClose( &esBuffer );
}
}
/* Feed libmpeg2 */
mpeg2_buffer( handle, esBuffer->data,
esBuffer->data + esBuffer->size );
}
else if( state == STATE_SEQUENCE )
{
/* Get size & framerate info */
title->inWidth = info->sequence->width;
title->inHeight = info->sequence->height;
title->aspect = (uint64_t)info->sequence->display_width *
info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
( info->sequence->display_height *
info->sequence->pixel_height );
title->rate = 27000000;
title->rateBase = info->sequence->frame_period;
}
else if( ( state == STATE_SLICE || state == STATE_END ) &&
( info->display_fbuf ) &&
( info->display_picture->flags & PIC_MASK_CODING_TYPE )
== PIC_FLAG_CODING_TYPE_I )
{
/* Write the raw picture to a file */
fwrite( info->display_fbuf->buf[0],
title->inWidth * title->inHeight, 1, file );
fwrite( info->display_fbuf->buf[1],
title->inWidth * title->inHeight / 4, 1, file );
fwrite( info->display_fbuf->buf[2],
title->inWidth * title->inHeight / 4, 1, file );
break;
}
else if( state == STATE_INVALID )
{
/* Reset libmpeg2 */
mpeg2_close( handle );
handle = mpeg2_init();
}
}
CLEANUP;
return 1;
#undef CLEANUP
}
static char * LanguageForCode( int code )
{
char codeString[2];
iso639_lang_t * lang;
codeString[0] = ( code >> 8 ) & 0xFF;
codeString[1] = code & 0xFF;
for( lang = languages; lang->engName; lang++ )
{
if( !strncmp( lang->iso639_1, codeString, 2 ) )
{
if( *lang->nativeName )
{
return lang->nativeName;
}
return lang->engName;
}
}
return "Unknown";
}
|