Coverage Report

Created: 2026-07-12 08:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/vc1.c
Line
Count
Source
1
/*****************************************************************************
2
 * vc1.c
3
 *****************************************************************************
4
 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Gildas Bazin <gbazin@videolan.org>
8
 *
9
 * This program is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU Lesser General Public License as published by
11
 * the Free Software Foundation; either version 2.1 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program; if not, write to the Free Software Foundation,
21
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
 *****************************************************************************/
23
24
/*****************************************************************************
25
 * Preamble
26
 *****************************************************************************/
27
28
#ifdef HAVE_CONFIG_H
29
# include "config.h"
30
#endif
31
32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
34
#include <vlc_codec.h>
35
#include <vlc_block.h>
36
37
#include <vlc_bits.h>
38
#include <vlc_block_helper.h>
39
#include "../codec/cc.h"
40
#include "packetizer_helper.h"
41
#include "hxxx_nal.h"
42
#include "hxxx_ep3b.h"
43
#include "startcode_helper.h"
44
#include "iso_color_tables.h"
45
46
/*****************************************************************************
47
 * Module descriptor
48
 *****************************************************************************/
49
static int  Open ( vlc_object_t * );
50
static void Close( vlc_object_t * );
51
52
168
vlc_module_begin ()
53
84
    set_subcategory( SUBCAT_SOUT_PACKETIZER )
54
84
    set_description( N_("VC-1 packetizer") )
55
84
    set_capability( "video packetizer", 50 )
56
168
    set_callbacks( Open, Close )
57
84
vlc_module_end ()
58
59
/*****************************************************************************
60
 * Local prototypes
61
 *****************************************************************************/
62
typedef struct
63
{
64
    /*
65
     * Input properties
66
     */
67
    packetizer_t packetizer;
68
69
    /* Current sequence header */
70
    bool b_sequence_header;
71
    struct
72
    {
73
        block_t *p_sh;
74
        bool b_advanced_profile;
75
        bool b_interlaced;
76
        bool b_frame_interpolation;
77
        bool b_range_reduction;
78
        bool b_has_bframe;
79
    } sh;
80
    bool b_entry_point;
81
    struct
82
    {
83
        block_t *p_ep;
84
    } ep;
85
86
    /* */
87
    bool  b_frame;
88
89
    /* Current frame being built */
90
    vlc_tick_t i_frame_dts;
91
    vlc_tick_t i_frame_pts;
92
    block_t    *p_frame;
93
    block_t    **pp_last;
94
    date_t     dts;
95
96
97
    bool    b_check_startcode;
98
99
    /* */
100
    uint32_t i_cc_flags;
101
    vlc_tick_t i_cc_pts;
102
    vlc_tick_t i_cc_dts;
103
    cc_data_t cc;
104
105
    cc_data_t cc_next;
106
} decoder_sys_t;
107
108
typedef enum
109
{
110
    IDU_TYPE_SEQUENCE_HEADER = 0x0f,
111
    IDU_TYPE_ENTRY_POINT = 0x0e,
112
    IDU_TYPE_FRAME = 0x0D,
113
    IDU_TYPE_FIELD = 0x0C,
114
    IDU_TYPE_SLICE = 0x0B,
115
    IDU_TYPE_END_OF_SEQUENCE = 0x0A,
116
117
    IDU_TYPE_SEQUENCE_LEVEL_USER_DATA = 0x1F,
118
    IDU_TYPE_ENTRY_POINT_USER_DATA = 0x1E,
119
    IDU_TYPE_FRAME_USER_DATA = 0x1D,
120
    IDU_TYPE_FIELD_USER_DATA = 0x1C,
121
    IDU_TYPE_SLICE_USER_DATA = 0x1B,
122
} idu_type_t;
123
124
static block_t *Packetize( decoder_t *p_dec, block_t **pp_block );
125
static void Flush( decoder_t * );
126
127
static void PacketizeReset( void *p_private, bool b_broken );
128
static int PacketizeValidate( void *p_private, block_t * );
129
static block_t *PacketizeDrain( void *p_private );
130
131
static block_t *OutputFrame( decoder_t *p_dec );
132
static block_t *ParseIDU( void *, bool *pb_ts_used, block_t *p_frag );
133
static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t * );
134
135
static const uint8_t p_vc1_startcode[3] = { 0x00, 0x00, 0x01 };
136
/*****************************************************************************
137
 * Open: probe the packetizer and return score
138
 *****************************************************************************
139
 * Tries to launch a decoder and return score so that the interface is able
140
 * to choose.
141
 *****************************************************************************/
142
static int Open( vlc_object_t *p_this )
143
9.60k
{
144
9.60k
    decoder_t     *p_dec = (decoder_t*)p_this;
145
9.60k
    decoder_sys_t *p_sys;
146
147
9.60k
    if( p_dec->fmt_in->i_codec !=  VLC_CODEC_VC1 )
148
9.48k
        return VLC_EGENERIC;
149
150
112
    p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
151
112
    if( unlikely( !p_sys ) )
152
0
        return VLC_ENOMEM;
153
154
    /* Create the output format */
155
112
    es_format_Copy( &p_dec->fmt_out, p_dec->fmt_in );
156
112
    p_dec->pf_packetize = Packetize;
157
112
    p_dec->pf_flush = Flush;
158
112
    p_dec->pf_get_cc = GetCc;
159
160
112
    packetizer_Init( &p_sys->packetizer,
161
112
                     p_vc1_startcode, sizeof(p_vc1_startcode), startcode_FindAnnexB,
162
112
                     NULL, 0, 4,
163
112
                     PacketizeReset, ParseIDU, PacketizeValidate, PacketizeDrain,
164
112
                     p_dec );
165
166
112
    p_sys->b_sequence_header = false;
167
112
    p_sys->sh.p_sh = NULL;
168
112
    p_sys->b_entry_point = false;
169
112
    p_sys->ep.p_ep = NULL;
170
171
112
    p_sys->i_frame_dts = VLC_TICK_INVALID;
172
112
    p_sys->i_frame_pts = VLC_TICK_INVALID;
173
174
112
    p_sys->b_frame = false;
175
112
    p_sys->p_frame = NULL;
176
112
    p_sys->pp_last = &p_sys->p_frame;
177
178
112
    if( p_dec->fmt_in->video.i_frame_rate && p_dec->fmt_in->video.i_frame_rate_base )
179
0
        date_Init( &p_sys->dts, p_dec->fmt_in->video.i_frame_rate * 2,
180
0
                                p_dec->fmt_in->video.i_frame_rate_base );
181
112
    else
182
112
        date_Init( &p_sys->dts, 30000*2, 1000 );
183
112
    p_sys->b_check_startcode = p_dec->fmt_in->b_packetized;
184
185
112
    if( p_dec->fmt_out.i_extra > 0 )
186
0
    {
187
0
        uint8_t *p_extra = p_dec->fmt_out.p_extra;
188
189
        /* With (some) ASF the first byte has to be stripped */
190
0
        if( p_extra[0] != 0x00 )
191
0
        {
192
0
            memmove( &p_extra[0], &p_extra[1], p_dec->fmt_out.i_extra - 1 );
193
0
            p_dec->fmt_out.i_extra--;
194
0
        }
195
196
        /* */
197
0
        if( p_dec->fmt_out.i_extra > 0 )
198
0
            packetizer_Header( &p_sys->packetizer,
199
0
                               p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
200
0
    }
201
202
    /* */
203
112
    p_sys->i_cc_pts = VLC_TICK_INVALID;
204
112
    p_sys->i_cc_dts = VLC_TICK_INVALID;
205
112
    p_sys->i_cc_flags = 0;
206
112
    cc_Init( &p_sys->cc );
207
112
    cc_Init( &p_sys->cc_next );
208
209
112
    return VLC_SUCCESS;
210
112
}
211
212
/*****************************************************************************
213
 * Close:
214
 *****************************************************************************/
215
static void Close( vlc_object_t *p_this )
216
112
{
217
112
    decoder_t     *p_dec = (decoder_t*)p_this;
218
112
    decoder_sys_t *p_sys = p_dec->p_sys;
219
220
112
    packetizer_Clean( &p_sys->packetizer );
221
112
    if( p_sys->p_frame )
222
64
        block_Release( p_sys->p_frame );
223
112
    if( p_sys->sh.p_sh )
224
82
        block_Release( p_sys->sh.p_sh );
225
112
    if( p_sys->ep.p_ep )
226
72
        block_Release( p_sys->ep.p_ep );
227
228
112
    cc_Exit( &p_sys->cc_next );
229
112
    cc_Exit( &p_sys->cc );
230
231
112
    free( p_sys );
232
112
}
233
234
/*****************************************************************************
235
 * Packetize: packetize an access unit
236
 *****************************************************************************/
237
static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
238
38.5k
{
239
38.5k
    decoder_sys_t *p_sys = p_dec->p_sys;
240
241
38.5k
    if( p_sys->b_check_startcode && pp_block && *pp_block )
242
18.7k
    {
243
        /* Fix syntax for (some?) VC1 from asf */
244
18.7k
        const unsigned i_startcode = sizeof(p_vc1_startcode);
245
246
18.7k
        block_t *p_block = *pp_block;
247
18.7k
        if( p_block->i_buffer > 0 &&
248
18.7k
            ( p_block->i_buffer < i_startcode ||
249
18.7k
              memcmp( p_block->p_buffer, p_vc1_startcode, i_startcode ) ) )
250
18.2k
        {
251
18.2k
            *pp_block = p_block = block_Realloc( p_block, i_startcode+1, p_block->i_buffer );
252
18.2k
            if( p_block )
253
18.2k
            {
254
18.2k
                memcpy( p_block->p_buffer, p_vc1_startcode, i_startcode );
255
256
18.2k
                if( p_sys->b_sequence_header && p_sys->sh.b_interlaced &&
257
4.74k
                    p_block->i_buffer > i_startcode+1 &&
258
4.74k
                    (p_block->p_buffer[i_startcode+1] & 0xc0) == 0xc0 )
259
654
                    p_block->p_buffer[i_startcode] = IDU_TYPE_FIELD;
260
17.5k
                else
261
17.5k
                    p_block->p_buffer[i_startcode] = IDU_TYPE_FRAME;
262
18.2k
            }
263
18.2k
        }
264
18.7k
        p_sys->b_check_startcode = false;
265
18.7k
    }
266
267
38.5k
    block_t *p_au = packetizer_Packetize( &p_sys->packetizer, pp_block );
268
38.5k
    if( !p_au )
269
22.8k
        p_sys->b_check_startcode = p_dec->fmt_in->b_packetized;
270
271
38.5k
    return p_au;
272
38.5k
}
273
274
static void Flush( decoder_t *p_dec )
275
0
{
276
0
    decoder_sys_t *p_sys = p_dec->p_sys;
277
278
0
    packetizer_Flush( &p_sys->packetizer );
279
0
}
280
281
static void PacketizeReset( void *p_private, bool b_flush )
282
416
{
283
416
    decoder_t *p_dec = p_private;
284
416
    decoder_sys_t *p_sys = p_dec->p_sys;
285
286
416
    if( b_flush )
287
0
    {
288
0
        if( p_sys->p_frame )
289
0
            block_ChainRelease( p_sys->p_frame );
290
0
        p_sys->p_frame = NULL;
291
0
        p_sys->pp_last = &p_sys->p_frame;
292
0
        p_sys->b_frame = false;
293
0
    }
294
295
416
    p_sys->i_frame_dts = VLC_TICK_INVALID;
296
416
    p_sys->i_frame_pts = VLC_TICK_INVALID;
297
416
    date_Set( &p_sys->dts, VLC_TICK_INVALID );
298
416
}
299
300
static int PacketizeValidate( void *p_private, block_t *p_au )
301
16.6k
{
302
16.6k
    decoder_t *p_dec = p_private;
303
16.6k
    decoder_sys_t *p_sys = p_dec->p_sys;
304
305
16.6k
    if( date_Get( &p_sys->dts ) == VLC_TICK_INVALID )
306
1.00k
    {
307
1.00k
        msg_Dbg( p_dec, "need a starting pts/dts" );
308
1.00k
        return VLC_EGENERIC;
309
1.00k
    }
310
15.6k
    VLC_UNUSED(p_au);
311
15.6k
    return VLC_SUCCESS;
312
16.6k
}
313
314
static block_t * PacketizeDrain( void *p_private )
315
112
{
316
112
    decoder_t *p_dec = p_private;
317
112
    decoder_sys_t *p_sys = p_dec->p_sys;
318
112
    return p_sys->b_frame ? OutputFrame( p_dec ) : NULL;
319
112
}
320
321
/* BuildExtraData: gather sequence header and entry point */
322
static void BuildExtraData( decoder_t *p_dec )
323
15.4k
{
324
15.4k
    decoder_sys_t *p_sys = p_dec->p_sys;
325
15.4k
    es_format_t *p_es = &p_dec->fmt_out;
326
15.4k
    size_t i_extra;
327
15.4k
    if( !p_sys->b_sequence_header || !p_sys->b_entry_point )
328
82
        return;
329
330
15.3k
    i_extra = p_sys->sh.p_sh->i_buffer + p_sys->ep.p_ep->i_buffer;
331
15.3k
    if( p_es->i_extra != i_extra )
332
9.13k
    {
333
9.13k
        p_es->i_extra = i_extra;
334
9.13k
        p_es->p_extra = xrealloc( p_es->p_extra, p_es->i_extra );
335
9.13k
    }
336
15.3k
    memcpy( p_es->p_extra,
337
15.3k
            p_sys->sh.p_sh->p_buffer, p_sys->sh.p_sh->i_buffer );
338
15.3k
    memcpy( (uint8_t*)p_es->p_extra + p_sys->sh.p_sh->i_buffer,
339
15.3k
            p_sys->ep.p_ep->p_buffer, p_sys->ep.p_ep->i_buffer );
340
15.3k
}
341
342
static block_t *OutputFrame( decoder_t *p_dec )
343
16.6k
{
344
16.6k
    decoder_sys_t *p_sys = p_dec->p_sys;
345
16.6k
    const int i_pic_flags = p_sys->p_frame->i_flags;
346
347
    /* Prepend SH and EP on I */
348
16.6k
    if( i_pic_flags & BLOCK_FLAG_TYPE_I )
349
1.47k
    {
350
1.47k
        block_t *p_list = block_Duplicate( p_sys->sh.p_sh );
351
1.47k
        block_t *p_ep = block_Duplicate( p_sys->ep.p_ep );
352
1.47k
        if( p_ep )
353
1.47k
            block_ChainAppend( &p_list, p_ep );
354
1.47k
        block_ChainAppend( &p_list, p_sys->p_frame );
355
1.47k
        p_list->i_flags = i_pic_flags;
356
1.47k
        p_sys->p_frame = p_list;
357
1.47k
    }
358
359
16.6k
    vlc_tick_t i_dts = p_sys->i_frame_dts;
360
16.6k
    vlc_tick_t i_pts = p_sys->i_frame_pts;
361
362
    /* */
363
16.6k
    block_t *p_pic = block_ChainGather( p_sys->p_frame );
364
16.6k
    if( p_pic )
365
16.6k
    {
366
16.6k
        p_pic->i_dts = p_sys->i_frame_dts;
367
16.6k
        p_pic->i_pts = p_sys->i_frame_pts;
368
16.6k
    }
369
370
    /* */
371
16.6k
    if( i_dts == VLC_TICK_INVALID )
372
2.17k
        i_dts = date_Get( &p_sys->dts );
373
14.5k
    else
374
14.5k
        date_Set( &p_sys->dts, i_dts );
375
376
16.6k
    if( i_pts == VLC_TICK_INVALID )
377
2.15k
    {
378
2.15k
        if( !p_sys->sh.b_has_bframe || (i_pic_flags & BLOCK_FLAG_TYPE_B ) )
379
1.56k
            i_pts = i_dts;
380
        /* TODO compute pts for other case */
381
2.15k
    }
382
383
16.6k
    if( p_pic )
384
16.6k
    {
385
16.6k
        p_pic->i_dts = i_dts;
386
16.6k
        p_pic->i_pts = i_pts;
387
16.6k
    }
388
389
    //msg_Dbg( p_dec, "-------------- dts=%"PRId64" pts=%"PRId64, i_dts, i_pts );
390
391
    /* We can interpolate dts/pts only if we have a frame rate */
392
16.6k
    if( p_dec->fmt_out.video.i_frame_rate && p_dec->fmt_out.video.i_frame_rate_base )
393
818
    {
394
818
        date_Increment( &p_sys->dts, 2 );
395
    //    msg_Dbg( p_dec, "-------------- XXX0 dts=%"PRId64" pts=%"PRId64" interpolated=%"PRId64,
396
    //             i_dts, i_pts, date_Get( &p_sys->dts ) );
397
818
    }
398
399
    /* CC */
400
16.6k
    p_sys->i_cc_pts = i_pts;
401
16.6k
    p_sys->i_cc_dts = i_dts;
402
16.6k
    p_sys->i_cc_flags = i_pic_flags;
403
404
16.6k
    p_sys->cc = p_sys->cc_next;
405
16.6k
    cc_Flush( &p_sys->cc_next );
406
407
    /* Reset context */
408
16.6k
    p_sys->b_frame = false;
409
16.6k
    p_sys->i_frame_dts = VLC_TICK_INVALID;
410
16.6k
    p_sys->i_frame_pts = VLC_TICK_INVALID;
411
16.6k
    p_sys->p_frame = NULL;
412
16.6k
    p_sys->pp_last = &p_sys->p_frame;
413
414
16.6k
    return p_pic;
415
16.6k
}
416
417
/* ParseIDU: parse an Independent Decoding Unit */
418
static block_t *ParseIDU( void *p_private, bool *pb_ts_used, block_t *p_frag )
419
163k
{
420
163k
    decoder_t *p_dec = p_private;
421
163k
    decoder_sys_t *p_sys = p_dec->p_sys;
422
163k
    block_t *p_pic = NULL;
423
163k
    const idu_type_t idu = p_frag->p_buffer[3];
424
425
163k
    *pb_ts_used = false;
426
163k
    if( !p_sys->b_sequence_header && idu != IDU_TYPE_SEQUENCE_HEADER )
427
9.82k
    {
428
9.82k
        msg_Warn( p_dec, "waiting for sequence header" );
429
9.82k
        block_Release( p_frag );
430
9.82k
        return NULL;
431
9.82k
    }
432
153k
    if( p_sys->b_sequence_header && !p_sys->b_entry_point && idu != IDU_TYPE_ENTRY_POINT )
433
6.17k
    {
434
6.17k
        msg_Warn( p_dec, "waiting for entry point" );
435
6.17k
        block_Release( p_frag );
436
6.17k
        return NULL;
437
6.17k
    }
438
    /* TODO we do not gather ENTRY_POINT and SEQUENCE_DATA user data
439
     * But It should not be a problem for decoder */
440
441
    /* Do we have completed a frame */
442
147k
    if( p_sys->b_frame &&
443
17.3k
        idu != IDU_TYPE_FRAME_USER_DATA &&
444
17.3k
        idu != IDU_TYPE_FIELD && idu != IDU_TYPE_FIELD_USER_DATA &&
445
16.6k
        idu != IDU_TYPE_SLICE && idu != IDU_TYPE_SLICE_USER_DATA &&
446
16.6k
        idu != IDU_TYPE_END_OF_SEQUENCE )
447
16.6k
    {
448
16.6k
        p_pic = OutputFrame( p_dec );
449
16.6k
    }
450
451
    /*  */
452
147k
    if( p_sys->i_frame_dts == VLC_TICK_INVALID && p_sys->i_frame_pts == VLC_TICK_INVALID )
453
52.8k
    {
454
52.8k
        p_sys->i_frame_dts = p_frag->i_dts;
455
52.8k
        p_sys->i_frame_pts = p_frag->i_pts;
456
52.8k
        *pb_ts_used = true;
457
52.8k
    }
458
459
    /* We will add back SH and EP on I frames */
460
147k
    block_t *p_release = NULL;
461
147k
    if( idu != IDU_TYPE_SEQUENCE_HEADER && idu != IDU_TYPE_ENTRY_POINT )
462
132k
        block_ChainLastAppend( &p_sys->pp_last, p_frag );
463
15.4k
    else
464
15.4k
        p_release = p_frag;
465
466
    /* Parse IDU */
467
147k
    if( idu == IDU_TYPE_SEQUENCE_HEADER )
468
11.0k
    {
469
11.0k
        es_format_t *p_es = &p_dec->fmt_out;
470
11.0k
        bs_t s;
471
472
        /* */
473
11.0k
        if( p_sys->sh.p_sh )
474
10.9k
            block_Release( p_sys->sh.p_sh );
475
11.0k
        p_sys->sh.p_sh = block_Duplicate( p_frag );
476
477
        /* Auto detect VC-1_SPMP_PESpacket_PayloadFormatHeader (SMPTE RP 227) for simple/main profile
478
         * TODO find a test case and valid it */
479
11.0k
        if( p_frag->i_buffer > 8 && (p_frag->p_buffer[4]&0x80) == 0 ) /* for advanced profile, the first bit is 1 */
480
4.32k
        {
481
4.32k
            const video_format_t *p_v = &p_dec->fmt_in->video;
482
4.32k
            const size_t i_potential_width  = GetWBE( &p_frag->p_buffer[4] );
483
4.32k
            const size_t i_potential_height = GetWBE( &p_frag->p_buffer[6] );
484
485
4.32k
            if( i_potential_width >= 2  && i_potential_width <= 8192 &&
486
4.16k
                i_potential_height >= 2 && i_potential_height <= 8192 )
487
3.48k
            {
488
3.48k
                if( ( p_v->i_width <= 0 && p_v->i_height <= 0  ) ||
489
34
                    ( p_v->i_width  == i_potential_width &&  p_v->i_height == i_potential_height ) )
490
3.45k
                {
491
3.45k
                    static const uint8_t startcode[4] = { 0x00, 0x00, 0x01, IDU_TYPE_SEQUENCE_HEADER };
492
3.45k
                    p_es->video.i_width  = i_potential_width;
493
3.45k
                    p_es->video.i_height = i_potential_height;
494
495
                    /* Remove it */
496
3.45k
                    p_frag->p_buffer += 4;
497
3.45k
                    p_frag->i_buffer -= 4;
498
3.45k
                    memcpy( p_frag->p_buffer, startcode, sizeof(startcode) );
499
3.45k
                }
500
3.48k
            }
501
4.32k
        }
502
503
        /* Parse it */
504
11.0k
        struct hxxx_bsfw_ep3b_ctx_s bsctx;
505
11.0k
        hxxx_bsfw_ep3b_ctx_init( &bsctx );
506
11.0k
        bs_init_custom( &s, &p_frag->p_buffer[4], p_frag->i_buffer - 4,
507
11.0k
                        &hxxx_bsfw_ep3b_callbacks, &bsctx );
508
509
11.0k
        p_dec->fmt_out.i_profile = bs_read( &s, 2 );
510
11.0k
        if( p_dec->fmt_out.i_profile == 3 )
511
9.15k
        {
512
9.15k
            p_dec->fmt_out.i_level = bs_read( &s, 3 );
513
514
            /* Advanced profile */
515
9.15k
            p_sys->sh.b_advanced_profile = true;
516
9.15k
            p_sys->sh.b_range_reduction = false;
517
9.15k
            p_sys->sh.b_has_bframe = true;
518
519
9.15k
            bs_skip( &s, 2+3+5+1 ); // chroma format + frame rate Q + bit rate Q + postprocflag
520
521
9.15k
            p_es->video.i_width  = 2*bs_read( &s, 12 )+2;
522
9.15k
            p_es->video.i_height = 2*bs_read( &s, 12 )+2;
523
524
9.15k
            if( !p_sys->b_sequence_header )
525
9.15k
                msg_Dbg( p_dec, "found sequence header for advanced profile level L%d resolution %dx%d",
526
9.15k
                         p_dec->fmt_out.i_level, p_es->video.i_width, p_es->video.i_height);
527
528
9.15k
            bs_skip( &s, 1 );// pulldown
529
9.15k
            p_sys->sh.b_interlaced = bs_read( &s, 1 );
530
9.15k
            bs_skip( &s, 1 );// frame counter
531
9.15k
            p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
532
9.15k
            bs_skip( &s, 1 );       // Reserved
533
9.15k
            bs_skip( &s, 1 );       // Psf
534
535
9.15k
            if( bs_read( &s, 1 ) )  /* Display extension */
536
4.83k
            {
537
4.83k
                const int i_display_width  = bs_read( &s, 14 )+1;
538
4.83k
                const int i_display_height = bs_read( &s, 14 )+1;
539
540
4.83k
                p_es->video.i_sar_num = i_display_width  * p_es->video.i_height;
541
4.83k
                p_es->video.i_sar_den = i_display_height * p_es->video.i_width;
542
543
4.83k
                if( !p_sys->b_sequence_header )
544
4.83k
                    msg_Dbg( p_dec, "display size %dx%d", i_display_width, i_display_height );
545
546
4.83k
                if( bs_read( &s, 1 ) )  /* Pixel aspect ratio (PAR/SAR) */
547
4.25k
                {
548
4.25k
                    static const unsigned p_ar[16][2] = {
549
4.25k
                        { 0, 0}, { 1, 1}, {12,11}, {10,11}, {16,11}, {40,33},
550
4.25k
                        {24,11}, {20,11}, {32,11}, {80,33}, {18,11}, {15,11},
551
4.25k
                        {64,33}, {160,99},{ 0, 0}, { 0, 0}
552
4.25k
                    };
553
4.25k
                    int i_ar = bs_read( &s, 4 );
554
4.25k
                    unsigned i_ar_w, i_ar_h;
555
556
4.25k
                    if( i_ar == 15 )
557
1.29k
                    {
558
1.29k
                        i_ar_w = bs_read( &s, 8 );
559
1.29k
                        i_ar_h = bs_read( &s, 8 );
560
1.29k
                    }
561
2.95k
                    else
562
2.95k
                    {
563
2.95k
                        i_ar_w = p_ar[i_ar][0];
564
2.95k
                        i_ar_h = p_ar[i_ar][1];
565
2.95k
                    }
566
4.25k
                    vlc_ureduce( &i_ar_w, &i_ar_h, i_ar_w, i_ar_h, 0 );
567
4.25k
                    if( !p_sys->b_sequence_header )
568
4.25k
                        msg_Dbg( p_dec, "aspect ratio %d:%d", i_ar_w, i_ar_h );
569
4.25k
                }
570
4.83k
            }
571
9.15k
            if( bs_read( &s, 1 ) )  /* Frame rate */
572
4.83k
            {
573
4.83k
                unsigned i_fps_num = 0;
574
4.83k
                unsigned i_fps_den = 0;
575
4.83k
                if( bs_read( &s, 1 ) )
576
2.04k
                {
577
2.04k
                    i_fps_num = bs_read( &s, 16 )+1;
578
2.04k
                    i_fps_den = 32;
579
2.04k
                }
580
2.79k
                else
581
2.79k
                {
582
2.79k
                    const int i_nr = bs_read( &s, 8 );
583
2.79k
                    const int i_dn = bs_read( &s, 4 );
584
585
2.79k
                    switch( i_nr )
586
2.79k
                    {
587
0
                    case 1: i_fps_num = 24000; break;
588
0
                    case 2: i_fps_num = 25000; break;
589
749
                    case 3: i_fps_num = 30000; break;
590
653
                    case 4: i_fps_num = 50000; break;
591
0
                    case 5: i_fps_num = 60000; break;
592
3
                    case 6: i_fps_num = 48000; break;
593
0
                    case 7: i_fps_num = 72000; break;
594
2.79k
                    }
595
2.79k
                    switch( i_dn )
596
2.79k
                    {
597
117
                    case 1: i_fps_den = 1000; break;
598
294
                    case 2: i_fps_den = 1001; break;
599
2.79k
                    }
600
2.79k
                }
601
602
4.83k
                if( i_fps_num != 0 && i_fps_den != 0 &&
603
2.33k
                   (p_dec->fmt_in->video.i_frame_rate == 0 ||
604
0
                    p_dec->fmt_in->video.i_frame_rate_base == 0) )
605
2.33k
                {
606
2.33k
                    vlc_ureduce( &p_es->video.i_frame_rate, &p_es->video.i_frame_rate_base, i_fps_num, i_fps_den, 0 );
607
608
2.33k
                    if( !p_sys->b_sequence_header )
609
4
                    {
610
4
                        msg_Dbg( p_dec, "frame rate %d/%d", p_es->video.i_frame_rate, p_es->video.i_frame_rate_base );
611
4
                        date_Change( &p_sys->dts, p_es->video.i_frame_rate * 2, p_es->video.i_frame_rate_base );
612
4
                    }
613
2.33k
                }
614
4.83k
            }
615
9.15k
            if( bs_read1( &s ) && /* Color Format */
616
4.25k
                p_dec->fmt_in->video.primaries == COLOR_PRIMARIES_UNDEF )
617
4.25k
            {
618
4.25k
                p_es->video.primaries = iso_23001_8_cp_to_vlc_primaries( bs_read( &s, 8 ) );
619
4.25k
                p_es->video.transfer = iso_23001_8_tc_to_vlc_xfer( bs_read( &s, 8 ) );
620
4.25k
                p_es->video.space = iso_23001_8_mc_to_vlc_coeffs( bs_read( &s, 8 ) );
621
4.25k
            }
622
9.15k
        }
623
1.88k
        else
624
1.88k
        {
625
            /* Simple and main profile */
626
1.88k
            p_sys->sh.b_advanced_profile = false;
627
1.88k
            p_sys->sh.b_interlaced = false;
628
629
1.88k
            if( !p_sys->b_sequence_header )
630
1.88k
                msg_Dbg( p_dec, "found sequence header for %s profile", p_dec->fmt_out.i_profile == 0 ? "simple" : "main" );
631
632
1.88k
            bs_skip( &s, 2+3+5+1+1+     // reserved + frame rate Q + bit rate Q + loop filter + reserved
633
1.88k
                         1+1+1+1+2+     // multiresolution + reserved + fast uv mc + extended mv + dquant
634
1.88k
                         1+1+1+1 );     // variable size transform + reserved + overlap + sync marker
635
1.88k
            p_sys->sh.b_range_reduction = bs_read( &s, 1 );
636
1.88k
            if( bs_read( &s, 3 ) > 0 )
637
168
                p_sys->sh.b_has_bframe = true;
638
1.71k
            else
639
1.71k
                p_sys->sh.b_has_bframe = false;
640
1.88k
            bs_skip( &s, 2 );           // quantizer
641
642
1.88k
            p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
643
1.88k
        }
644
11.0k
        p_sys->b_sequence_header = true;
645
11.0k
        BuildExtraData( p_dec );
646
11.0k
    }
647
136k
    else if( idu == IDU_TYPE_ENTRY_POINT )
648
4.39k
    {
649
4.39k
        if( p_sys->ep.p_ep )
650
4.32k
            block_Release( p_sys->ep.p_ep );
651
4.39k
        p_sys->ep.p_ep = block_Duplicate( p_frag );
652
653
4.39k
        if( !p_sys->b_entry_point )
654
4.39k
            msg_Dbg( p_dec, "found entry point" );
655
656
4.39k
        p_sys->b_entry_point = true;
657
4.39k
        BuildExtraData( p_dec );
658
4.39k
    }
659
132k
    else if( idu == IDU_TYPE_FRAME )
660
16.6k
    {
661
16.6k
        bs_t s;
662
663
        /* Parse it + interpolate pts/dts if possible */
664
16.6k
        struct hxxx_bsfw_ep3b_ctx_s bsctx;
665
16.6k
        hxxx_bsfw_ep3b_ctx_init( &bsctx );
666
16.6k
        bs_init_custom( &s, &p_frag->p_buffer[4], p_frag->i_buffer - 4,
667
16.6k
                        &hxxx_bsfw_ep3b_callbacks, &bsctx );
668
669
16.6k
        if( p_sys->sh.b_advanced_profile )
670
16.0k
        {
671
16.0k
            int i_fcm = 0;
672
673
16.0k
            if( p_sys->sh.b_interlaced )
674
4.34k
            {
675
4.34k
                if( bs_read( &s, 1 ) )
676
856
                {
677
856
                    if( bs_read( &s, 1 ) )
678
4
                        i_fcm = 1;  /* interlaced field */
679
852
                    else
680
852
                        i_fcm = 2;  /* interlaced frame */
681
856
                }
682
4.34k
            }
683
684
16.0k
            if( i_fcm == 1 ) /*interlaced field */
685
4
            {
686
                /* XXX for mixed I/P we should check reference usage before marking them I (too much work) */
687
4
                switch( bs_read( &s, 3 ) )
688
4
                {
689
0
                case 0: /* II */
690
0
                case 1: /* IP */
691
0
                case 2: /* PI */
692
0
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
693
0
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
694
0
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
695
0
                    break;
696
2
                case 3: /* PP */
697
2
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
698
2
                    break;
699
0
                case 4: /* BB */
700
0
                case 5: /* BBi */
701
0
                case 6: /* BiB */
702
2
                case 7: /* BiBi */
703
2
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
704
2
                    break;
705
4
                }
706
4
            }
707
16.0k
            else
708
16.0k
            {
709
16.0k
                if( !bs_read( &s, 1 ) )
710
11.9k
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
711
4.08k
                else if( !bs_read( &s, 1 ) )
712
2.40k
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
713
1.68k
                else if( !bs_read( &s, 1 ) )
714
879
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
715
801
                else if( !bs_read( &s, 1 ) )
716
312
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;   /* Bi */
717
489
                else
718
489
                    p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;   /* P Skip */
719
16.0k
            }
720
16.0k
        }
721
649
        else
722
649
        {
723
649
            if( p_sys->sh.b_frame_interpolation )
724
16
                bs_skip( &s, 1 );   // interpolate
725
649
            bs_skip( &s, 2 );       // frame count
726
649
            if( p_sys->sh.b_range_reduction )
727
0
                bs_skip( &s, 1 );   // range reduction
728
729
649
            if( bs_read( &s, 1 ) )
730
34
                p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
731
615
            else if( !p_sys->sh.b_has_bframe || bs_read( &s, 1 ) )
732
600
                p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
733
15
            else
734
15
                p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
735
649
        }
736
16.6k
        p_sys->b_frame = true;
737
16.6k
    }
738
115k
    else if( idu == IDU_TYPE_FRAME_USER_DATA )
739
4.11k
    {
740
4.11k
        bs_t s;
741
4.11k
        const size_t i_size = p_frag->i_buffer - 4;
742
4.11k
        struct hxxx_bsfw_ep3b_ctx_s bsctx;
743
4.11k
        hxxx_bsfw_ep3b_ctx_init( &bsctx );
744
4.11k
        bs_init_custom( &s, &p_frag->p_buffer[4], i_size, &hxxx_bsfw_ep3b_callbacks, &bsctx );
745
746
4.11k
        unsigned i_data;
747
4.11k
        uint8_t *p_data = malloc( i_size );
748
4.11k
        if( p_data )
749
4.11k
        {
750
            /* store converted data */
751
753k
            for( i_data = 0; i_data + 1 /* trailing 0x80 flush byte */<i_size; i_data++ )
752
749k
            {
753
749k
                p_data[i_data] = bs_read( &s, 8 );
754
749k
                if( bs_error(&s) )
755
133
                    break;
756
749k
            }
757
758
            /* TS 101 154 Auxiliary Data and VC-1 video */
759
4.11k
            static const uint8_t p_DVB1_user_identifier[] = {
760
4.11k
                0x47, 0x41, 0x39, 0x34 /* user identifier */
761
4.11k
            };
762
763
            /* Check if we have DVB1_data() */
764
4.11k
            if( i_data >= sizeof(p_DVB1_user_identifier) &&
765
4.10k
                !memcmp( p_data, p_DVB1_user_identifier, sizeof(p_DVB1_user_identifier) ) )
766
1.69k
            {
767
1.69k
                cc_ProbeAndExtract( &p_sys->cc_next, true, p_data, i_data );
768
1.69k
            }
769
770
4.11k
            free( p_data );
771
4.11k
        }
772
4.11k
    }
773
774
147k
    if( p_release )
775
15.4k
        block_Release( p_release );
776
147k
    return p_pic;
777
147k
}
778
779
/*****************************************************************************
780
 * GetCc:
781
 *****************************************************************************/
782
static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
783
15.6k
{
784
15.6k
    decoder_sys_t *p_sys = p_dec->p_sys;
785
15.6k
    block_t *p_cc;
786
787
15.6k
    p_cc = block_Alloc( p_sys->cc.i_data);
788
15.6k
    if( p_cc )
789
15.6k
    {
790
15.6k
        memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
791
15.6k
        p_cc->i_dts =
792
15.6k
        p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
793
15.6k
        p_cc->i_flags = p_sys->i_cc_flags & BLOCK_FLAG_TYPE_MASK;
794
795
15.6k
        p_desc->i_608_channels = p_sys->cc.i_608channels;
796
15.6k
        p_desc->i_708_channels = p_sys->cc.i_708channels;
797
15.6k
        p_desc->i_reorder_depth = p_sys->cc.b_reorder ? 4 : -1;
798
15.6k
    }
799
15.6k
    cc_Flush( &p_sys->cc );
800
15.6k
    return p_cc;
801
15.6k
}