Coverage Report

Created: 2023-06-07 07:26

/src/vlc/modules/demux/pva.c
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * pva.c: PVA demuxer
3
 *****************************************************************************
4
 * Copyright (C) 2004 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
/*****************************************************************************
24
 * Preamble
25
 *****************************************************************************/
26
27
#ifdef HAVE_CONFIG_H
28
# include "config.h"
29
#endif
30
31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
33
#include <vlc_demux.h>
34
35
#include "mpeg/pes.h"
36
37
/*****************************************************************************
38
 * Module descriptor
39
 *****************************************************************************/
40
static int  Open    ( vlc_object_t * );
41
static void Close  ( vlc_object_t * );
42
43
4
vlc_module_begin ()
44
2
    set_description( N_("PVA demuxer" ) )
45
2
    set_capability( "demux", 10 )
46
2
    set_subcategory( SUBCAT_INPUT_DEMUX )
47
2
    set_callbacks( Open, Close )
48
2
    add_shortcut( "pva" )
49
2
    add_file_extension("pva")
50
2
vlc_module_end ()
51
52
/*****************************************************************************
53
 * Local prototypes
54
 *****************************************************************************/
55
56
typedef struct
57
{
58
    es_out_id_t *p_video;
59
    es_out_id_t *p_audio;
60
61
    /* counter */
62
    int          i_vc;
63
    int          i_ac;
64
65
    /* audio/video block */
66
    block_t     *p_pes; /* audio */
67
    block_t     *p_es;  /* video */
68
69
    int64_t     b_pcr_audio;
70
} demux_sys_t;
71
72
static int  Demux   ( demux_t *p_demux );
73
static int  Control ( demux_t *p_demux, int i_query, va_list args );
74
75
static int  ReSynch ( demux_t * );
76
static void ParsePES( demux_t * );
77
78
/*****************************************************************************
79
 * Open
80
 *****************************************************************************/
81
static int Open( vlc_object_t *p_this )
82
39
{
83
39
    demux_t     *p_demux = (demux_t*)p_this;
84
39
    demux_sys_t *p_sys;
85
39
    es_format_t  fmt;
86
39
    const uint8_t *p_peek;
87
88
39
    if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
89
39
    if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
90
39
    {
91
        /* In case we had forced this demuxer we try to resynch */
92
39
        if( !p_demux->obj.force || ReSynch( p_demux ) )
93
39
            return VLC_EGENERIC;
94
39
    }
95
96
0
    p_sys = malloc( sizeof( demux_sys_t ) );
97
0
    if( unlikely(p_sys == NULL) )
98
0
        return VLC_ENOMEM;
99
100
    /* Fill p_demux field */
101
0
    p_demux->pf_demux = Demux;
102
0
    p_demux->pf_control = Control;
103
0
    p_demux->p_sys = p_sys;
104
105
    /* Register one audio and one video stream */
106
0
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_MPGA );
107
0
    fmt.b_packetized = false;
108
0
    p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
109
110
0
    es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_MPGV );
111
0
    fmt.b_packetized = false;
112
0
    p_sys->p_video = es_out_Add( p_demux->out, &fmt );
113
114
0
    p_sys->i_vc    = -1;
115
0
    p_sys->i_ac    = -1;
116
0
    p_sys->p_pes   = NULL;
117
0
    p_sys->p_es    = NULL;
118
119
0
    p_sys->b_pcr_audio = false;
120
121
0
    return VLC_SUCCESS;
122
0
}
123
124
/*****************************************************************************
125
 * Close
126
 *****************************************************************************/
127
static void Close( vlc_object_t *p_this )
128
0
{
129
0
    demux_t     *p_demux = (demux_t*)p_this;
130
0
    demux_sys_t *p_sys = p_demux->p_sys;
131
132
0
    block_ChainRelease( p_sys->p_es );
133
0
    block_ChainRelease( p_sys->p_pes );
134
135
0
    free( p_sys );
136
0
}
137
138
139
/*****************************************************************************
140
 * Demux:
141
 *****************************************************************************
142
 * See http://multimedia.cx/mirror/av_format_v1.pdf
143
 *****************************************************************************/
144
static int Demux( demux_t *p_demux )
145
0
{
146
0
    demux_sys_t *p_sys = p_demux->p_sys;
147
148
0
    const uint8_t *p_peek;
149
0
    int         i_size;
150
0
    block_t     *p_frame;
151
0
    int64_t     i_pts;
152
0
    int         i_skip;
153
154
0
    if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
155
0
    {
156
0
        msg_Warn( p_demux, "eof ?" );
157
0
        return VLC_DEMUXER_EOF;
158
0
    }
159
0
    if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
160
0
    {
161
0
        msg_Warn( p_demux, "lost synchro" );
162
0
        if( ReSynch( p_demux ) )
163
0
        {
164
0
            return VLC_DEMUXER_EGENERIC;
165
0
        }
166
0
        if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
167
0
        {
168
0
            msg_Warn( p_demux, "eof ?" );
169
0
            return VLC_DEMUXER_EOF;
170
0
        }
171
0
    }
172
173
0
    i_size = GetWBE( &p_peek[6] );
174
0
    switch( p_peek[2] )
175
0
    {
176
0
        case 0x01:  /* VideoStream */
177
0
            if( p_sys->i_vc < 0 )
178
0
            {
179
0
                msg_Dbg( p_demux, "first packet for video" );
180
0
            }
181
0
            else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
182
0
            {
183
0
                msg_Dbg( p_demux, "packet lost (video)" );
184
0
                if( p_sys->p_es )
185
0
                {
186
0
                    block_ChainRelease( p_sys->p_es );
187
0
                    p_sys->p_es = NULL;
188
0
                }
189
0
            }
190
0
            p_sys->i_vc = p_peek[3];
191
192
            /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
193
0
            i_pts = -1;
194
0
            i_skip = 8;
195
0
            if( p_peek[5]&0x10 )
196
0
            {
197
0
                int i_pre = p_peek[5]&0x3;
198
199
0
                if( ( p_frame = vlc_stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
200
0
                {
201
0
                    i_pts = GetDWBE( &p_frame->p_buffer[8] );
202
0
                    if( p_frame->i_buffer > 12 )
203
0
                    {
204
0
                        p_frame->p_buffer += 12;
205
0
                        p_frame->i_buffer -= 12;
206
0
                        block_ChainAppend( &p_sys->p_es, p_frame );
207
0
                    }
208
0
                    else
209
0
                    {
210
0
                        block_Release( p_frame );
211
0
                    }
212
0
                }
213
0
                i_size -= 4 + i_pre;
214
0
                i_skip  = 0;
215
                /* Set PCR */
216
0
                if( ( p_frame = p_sys->p_es ) )
217
0
                {
218
219
0
                    if( p_frame->i_pts != VLC_TICK_INVALID && !p_sys->b_pcr_audio )
220
0
                    {
221
0
                        es_out_SetPCR( p_demux->out, p_frame->i_pts);
222
0
                    }
223
224
0
                    p_frame = block_ChainGather( p_frame );
225
0
                    if( likely(p_frame) )
226
0
                        es_out_Send( p_demux->out, p_sys->p_video, p_frame );
227
228
0
                    p_sys->p_es = NULL;
229
0
                }
230
0
            }
231
232
0
            if( ( p_frame = vlc_stream_Block( p_demux->s, i_size + i_skip ) ) )
233
0
            {
234
0
                p_frame->p_buffer += i_skip;
235
0
                p_frame->i_buffer -= i_skip;
236
0
                if( i_pts >= 0 )
237
0
                    p_frame->i_pts = FROM_SCALE(i_pts);
238
0
                block_ChainAppend( &p_sys->p_es, p_frame );
239
0
            }
240
0
            break;
241
242
0
        case 0x02:  /* MainAudioStream */
243
0
            if( p_sys->i_ac < 0 )
244
0
            {
245
0
                msg_Dbg( p_demux, "first packet for audio" );
246
0
            }
247
0
            else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
248
0
            {
249
0
                msg_Dbg( p_demux, "packet lost (audio)" );
250
0
                if( p_sys->p_pes )
251
0
                {
252
0
                    block_ChainRelease( p_sys->p_pes );
253
0
                    p_sys->p_pes = NULL;
254
0
                }
255
0
            }
256
0
            p_sys->i_ac = p_peek[3];
257
258
0
            if( p_peek[5]&0x10 && p_sys->p_pes )
259
0
            {
260
0
                ParsePES( p_demux );
261
0
            }
262
0
            if( ( p_frame = vlc_stream_Block( p_demux->s, i_size + 8 ) ) )
263
0
            {
264
0
                p_frame->p_buffer += 8;
265
0
                p_frame->i_buffer -= 8;
266
                /* XXX this a hack, some streams aren't compliant and
267
                 * don't set pes_start flag */
268
0
                if( p_sys->p_pes && p_frame->i_buffer > 4 &&
269
0
                    p_frame->p_buffer[0] == 0x00 &&
270
0
                    p_frame->p_buffer[1] == 0x00 &&
271
0
                    p_frame->p_buffer[2] == 0x01 )
272
0
                {
273
0
                    ParsePES( p_demux );
274
0
                }
275
0
                block_ChainAppend( &p_sys->p_pes, p_frame );
276
0
            }
277
0
            break;
278
279
0
        default:
280
0
            msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
281
0
            if( vlc_stream_Read( p_demux->s, NULL, i_size + 8 ) != (i_size + 8) )
282
0
                return VLC_DEMUXER_EOF;
283
0
            break;
284
0
    }
285
0
    return VLC_DEMUXER_SUCCESS;
286
0
}
287
288
/*****************************************************************************
289
 * Control:
290
 *****************************************************************************/
291
static int Control( demux_t *p_demux, int i_query, va_list args )
292
0
{
293
    /* demux_sys_t *p_sys = p_demux->p_sys; */
294
0
    double f, *pf;
295
0
    int64_t i64;
296
0
    switch( i_query )
297
0
    {
298
0
        case DEMUX_CAN_SEEK:
299
0
            return vlc_stream_vaControl( p_demux->s, i_query, args );
300
301
0
        case DEMUX_GET_POSITION:
302
0
            if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
303
0
            {
304
0
                pf = va_arg( args, double * );
305
0
                double current = vlc_stream_Tell( p_demux->s );
306
0
                *pf = current / (double)i64;
307
0
                return VLC_SUCCESS;
308
0
            }
309
0
            return VLC_EGENERIC;
310
311
0
        case DEMUX_SET_POSITION:
312
0
            f = va_arg( args, double );
313
0
            i64 = stream_Size( p_demux->s );
314
315
0
            if( vlc_stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
316
0
            {
317
0
                return VLC_EGENERIC;
318
0
            }
319
0
            return VLC_SUCCESS;
320
321
#if 0
322
        case DEMUX_GET_TIME:
323
            pi64 = va_arg( args, vlc_tick_t * );
324
            if( p_sys->i_time < 0 )
325
            {
326
                *pi64 = 0;
327
                return VLC_EGENERIC;
328
            }
329
            *pi64 = p_sys->i_time;
330
            return VLC_SUCCESS;
331
332
#if 0
333
        case DEMUX_GET_LENGTH:
334
            pi64 = va_arg( args, vlc_tick_t * );
335
            if( p_sys->i_mux_rate > 0 )
336
            {
337
                *pi64 = vlc_tick_from_samples( stream_Size( p_demux->s ) / 50, p_sys->i_mux_rate);
338
                return VLC_SUCCESS;
339
            }
340
            *pi64 = 0;
341
            return VLC_EGENERIC;
342
343
#endif
344
        case DEMUX_GET_FPS:
345
            pf = va_arg( args, double * );
346
            *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
347
            return VLC_SUCCESS;
348
#endif
349
0
        case DEMUX_CAN_PAUSE:
350
0
        case DEMUX_SET_PAUSE_STATE:
351
0
        case DEMUX_CAN_CONTROL_PACE:
352
0
        case DEMUX_GET_PTS_DELAY:
353
0
            return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
354
355
0
        case DEMUX_SET_TIME:
356
0
        default:
357
0
            return VLC_EGENERIC;
358
0
    }
359
0
}
360
361
/*****************************************************************************
362
 * ReSynch:
363
 *****************************************************************************/
364
static int ReSynch( demux_t *p_demux )
365
0
{
366
0
    for( ;; )
367
0
    {
368
0
        const uint8_t *p_peek;
369
0
        int i_peek = vlc_stream_Peek( p_demux->s, &p_peek, 1024 );
370
0
        if( i_peek < 8 )
371
0
            break;
372
373
0
        int i_skip = 0;
374
375
0
        while( i_skip < i_peek - 5 )
376
0
        {
377
0
            if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
378
0
            {
379
0
                if( i_skip > 0
380
0
                 && vlc_stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
381
0
                    return VLC_EGENERIC;
382
0
                return VLC_SUCCESS;
383
0
            }
384
0
            p_peek++;
385
0
            i_skip++;
386
0
        }
387
388
0
        if( vlc_stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
389
0
            break;
390
0
    }
391
392
0
    return VLC_EGENERIC;
393
0
}
394
395
static void ParsePES( demux_t *p_demux )
396
0
{
397
0
    demux_sys_t *p_sys = p_demux->p_sys;
398
0
    block_t     *p_pes = p_sys->p_pes;
399
0
    uint8_t     hdr[30];
400
401
0
    unsigned    i_skip;
402
0
    stime_t     i_dts = -1;
403
0
    stime_t     i_pts = -1;
404
405
0
    p_sys->p_pes = NULL;
406
407
    /* FIXME find real max size */
408
0
    block_ChainExtract( p_pes, hdr, 30 );
409
410
    /* See ยง2.4.3.6 of ISO 13818-1 */
411
0
    if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
412
0
    {
413
0
        msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
414
0
                  hdr[0], hdr[1],hdr[2],hdr[3] );
415
0
        block_ChainRelease( p_pes );
416
0
        return;
417
0
    }
418
    // hdr[4] i_pes_size, 2 bytes
419
    // hdr[6] Marker -> original_or_copy
420
421
    /* we assume mpeg2 PES */
422
0
    i_skip = hdr[8] + 9;
423
0
    if( hdr[7]&0x80 )    /* has pts */
424
0
    {
425
0
        i_pts = GetPESTimestamp( &hdr[9] );
426
427
0
        if( hdr[7]&0x40 )    /* has dts */
428
0
        {
429
0
             i_dts = GetPESTimestamp( &hdr[14] );
430
0
        }
431
0
    }
432
433
0
    p_pes = block_ChainGather( p_pes );
434
0
    if( unlikely(p_pes == NULL) )
435
0
        return;
436
0
    if( p_pes->i_buffer <= i_skip )
437
0
    {
438
0
        block_ChainRelease( p_pes );
439
0
        return;
440
0
    }
441
442
0
    p_pes->i_buffer -= i_skip;
443
0
    p_pes->p_buffer += i_skip;
444
445
0
    if( i_dts >= 0 )
446
0
        p_pes->i_dts = FROM_SCALE(i_dts);
447
0
    if( i_pts >= 0 )
448
0
        p_pes->i_pts = FROM_SCALE(i_pts);
449
450
    /* Set PCR */
451
0
    if( p_pes->i_pts != VLC_TICK_INVALID )
452
0
    {
453
0
        es_out_SetPCR( p_demux->out, p_pes->i_pts);
454
0
        p_sys->b_pcr_audio = true;
455
0
    }
456
0
    es_out_Send( p_demux->out, p_sys->p_audio, p_pes );
457
0
}