h264.c 39.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * h264.c: h264/avc video packetizer
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
5
 * $Id$
6 7 8
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
9
 *          Gildas Bazin <gbazin@videolan.org>
10
 *          Derk-Jan Hartman <hartman at videolan dot org>
11
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15 16 17 18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 26 27 28 29 30
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

31 32 33 34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
#include <vlc_common.h>
36
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
37 38 39
#include <vlc_sout.h>
#include <vlc_codec.h>
#include <vlc_block.h>
40

41 42
#include <vlc_block_helper.h>
#include <vlc_bits.h>
43
#include "h264_nal.h"
44
#include "h264_slice.h"
45
#include "hxxx_nal.h"
46
#include "hxxx_sei.h"
47
#include "hxxx_common.h"
48
#include "packetizer_helper.h"
49
#include "startcode_helper.h"
50

51 52
#include <limits.h>

53 54 55 56 57 58
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

59 60 61 62 63 64 65
vlc_module_begin ()
    set_category( CAT_SOUT )
    set_subcategory( SUBCAT_SOUT_PACKETIZER )
    set_description( N_("H.264 video packetizer") )
    set_capability( "packetizer", 50 )
    set_callbacks( Open, Close )
vlc_module_end ()
66 67 68 69 70


/****************************************************************************
 * Local prototypes
 ****************************************************************************/
71

72 73
struct decoder_sys_t
{
74 75
    /* */
    packetizer_t packetizer;
76

77
    /* */
78
    bool    b_slice;
79 80 81 82 83 84
    struct
    {
        block_t *p_head;
        block_t **pp_append;
    } frame, leading;

85 86 87
    /* a new sps/pps can be transmitted outside of iframes */
    bool    b_new_sps;
    bool    b_new_pps;
88

89 90 91 92 93 94 95 96 97 98 99 100 101
    struct
    {
        block_t *p_block;
        h264_sequence_parameter_set_t *p_sps;
    } sps[H264_SPS_ID_MAX + 1];
    struct
    {
        block_t *p_block;
        h264_picture_parameter_set_t *p_pps;
    } pps[H264_PPS_ID_MAX + 1];
    const h264_sequence_parameter_set_t *p_active_sps;
    const h264_picture_parameter_set_t *p_active_pps;

102
    /* avcC data */
103
    uint8_t i_avcC_length_size;
104

105
    /* From SEI for current frame */
106
    uint8_t i_pic_struct;
107
    uint8_t i_dpb_output_delay;
108
    unsigned i_recovery_frame_cnt;
109

110
    /* Useful values of the Slice Header */
111
    h264_slice_t slice;
Laurent Aimar's avatar
Laurent Aimar committed
112 113

    /* */
114
    int i_next_block_flags;
115 116
    bool b_recovered;
    unsigned i_recoveryfnum;
117
    unsigned i_recoverystartfnum;
118

119
    /* POC */
120
    h264_poc_context_t pocctx;
121 122 123 124 125 126
    struct
    {
        mtime_t pts;
        int num;
    } prevdatedpoc;

Laurent Aimar's avatar
Laurent Aimar committed
127 128
    mtime_t i_frame_pts;
    mtime_t i_frame_dts;
129

130 131
    date_t dts;

132
    /* */
133
    cc_storage_t *p_ccs;
134 135
};

136
#define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT)
137 138
#define BLOCK_FLAG_PRIVATE_SEI (2 << BLOCK_FLAG_PRIVATE_SHIFT)
#define BLOCK_FLAG_DROP        (4 << BLOCK_FLAG_PRIVATE_SHIFT)
139

140 141
static block_t *Packetize( decoder_t *, block_t ** );
static block_t *PacketizeAVC1( decoder_t *, block_t ** );
142
static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t * );
143
static void PacketizeFlush( decoder_t * );
144

145 146 147 148
static void PacketizeReset( void *p_private, bool b_broken );
static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
static int PacketizeValidate( void *p_private, block_t * );

149
static block_t *ParseNALBlock( decoder_t *, bool *pb_ts_used, block_t * );
150

151 152 153
static block_t *OutputPicture( decoder_t *p_dec );
static void PutSPS( decoder_t *p_dec, block_t *p_frag );
static void PutPPS( decoder_t *p_dec, block_t *p_frag );
154
static bool ParseSliceHeader( decoder_t *p_dec, const block_t *p_frag, h264_slice_t *p_slice );
155
static bool ParseSeiCallback( const hxxx_sei_data_t *, void * );
156 157


158 159
static const uint8_t p_h264_startcode[3] = { 0x00, 0x00, 0x01 };

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
/*****************************************************************************
 * Helpers
 *****************************************************************************/

static void StoreSPS( decoder_sys_t *p_sys, uint8_t i_id,
                      block_t *p_block, h264_sequence_parameter_set_t *p_sps )
{
    if( p_sys->sps[i_id].p_block )
        block_Release( p_sys->sps[i_id].p_block );
    if( p_sys->sps[i_id].p_sps )
        h264_release_sps( p_sys->sps[i_id].p_sps );
    if( p_sys->sps[i_id].p_sps == p_sys->p_active_sps )
        p_sys->p_active_sps = NULL;
    p_sys->sps[i_id].p_block = p_block;
    p_sys->sps[i_id].p_sps = p_sps;
}

static void StorePPS( decoder_sys_t *p_sys, uint8_t i_id,
                      block_t *p_block, h264_picture_parameter_set_t *p_pps )
{
    if( p_sys->pps[i_id].p_block )
        block_Release( p_sys->pps[i_id].p_block );
    if( p_sys->pps[i_id].p_pps )
        h264_release_pps( p_sys->pps[i_id].p_pps );
    if( p_sys->pps[i_id].p_pps == p_sys->p_active_pps )
        p_sys->p_active_pps = NULL;
    p_sys->pps[i_id].p_block = p_block;
    p_sys->pps[i_id].p_pps = p_pps;
}

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
static void ActivateSets( decoder_t *p_dec, const h264_sequence_parameter_set_t *p_sps,
                                            const h264_picture_parameter_set_t *p_pps )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    p_sys->p_active_pps = p_pps;
    p_sys->p_active_sps = p_sps;

    if( p_sps )
    {
        p_dec->fmt_out.i_profile = p_sps->i_profile;
        p_dec->fmt_out.i_level = p_sps->i_level;

        (void) h264_get_picture_size( p_sps, &p_dec->fmt_out.video.i_width,
                                      &p_dec->fmt_out.video.i_height,
                                      &p_dec->fmt_out.video.i_visible_width,
                                      &p_dec->fmt_out.video.i_visible_height );

        if( p_sps->vui.i_sar_num != 0 && p_sps->vui.i_sar_den != 0 )
        {
            p_dec->fmt_out.video.i_sar_num = p_sps->vui.i_sar_num;
            p_dec->fmt_out.video.i_sar_den = p_sps->vui.i_sar_den;
        }

        if( p_sps->vui.b_valid )
        {
216 217
            if( !p_dec->fmt_in.video.i_frame_rate_base &&
                p_sps->vui.i_num_units_in_tick > 0 && p_sps->vui.i_time_scale > 1 )
218
            {
219 220
                const unsigned i_rate_base = p_sps->vui.i_num_units_in_tick;
                const unsigned i_rate = p_sps->vui.i_time_scale >> 1; /* num_clock_ts == 2 */
221
                if( i_rate_base != p_dec->fmt_out.video.i_frame_rate_base ||
222 223 224 225
                    i_rate != p_dec->fmt_out.video.i_frame_rate )
                {
                    p_dec->fmt_out.video.i_frame_rate_base = i_rate_base;
                    p_dec->fmt_out.video.i_frame_rate = i_rate;
226
                    date_Change( &p_sys->dts, p_sps->vui.i_time_scale, p_sps->vui.i_num_units_in_tick );
227
                }
228
            }
229
            if( p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF )
230 231 232 233
                h264_get_colorimetry( p_sps, &p_dec->fmt_out.video.primaries,
                                      &p_dec->fmt_out.video.transfer,
                                      &p_dec->fmt_out.video.space,
                                      &p_dec->fmt_out.video.b_color_range_full );
234
        }
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

        if( p_dec->fmt_out.i_extra == 0 && p_pps )
        {
            const block_t *p_spsblock = NULL;
            const block_t *p_ppsblock = NULL;
            for( size_t i=0; i<=H264_SPS_ID_MAX && !p_spsblock; i++ )
                if( p_sps == p_sys->sps[i].p_sps )
                    p_spsblock = p_sys->sps[i].p_block;

            for( size_t i=0; i<=H264_PPS_ID_MAX && !p_ppsblock; i++ )
                if( p_pps == p_sys->pps[i].p_pps )
                    p_ppsblock = p_sys->pps[i].p_block;

            if( p_spsblock && p_ppsblock )
            {
250
                size_t i_alloc = p_ppsblock->i_buffer + p_spsblock->i_buffer;
251 252 253 254 255
                p_dec->fmt_out.p_extra = malloc( i_alloc );
                if( p_dec->fmt_out.p_extra )
                {
                    uint8_t*p_buf = p_dec->fmt_out.p_extra;
                    p_dec->fmt_out.i_extra = i_alloc;
256 257
                    memcpy( &p_buf[0], p_spsblock->p_buffer, p_spsblock->i_buffer );
                    memcpy( &p_buf[p_spsblock->i_buffer], p_ppsblock->p_buffer,
258 259 260 261
                            p_ppsblock->i_buffer );
                }
            }
        }
262 263 264
    }
}

265
static bool IsFirstVCLNALUnit( const h264_slice_t *p_prev, const h264_slice_t *p_cur )
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
{
    /* Detection of the first VCL NAL unit of a primary coded picture
     * (cf. 7.4.1.2.4) */
    if( p_cur->i_frame_num != p_prev->i_frame_num ||
        p_cur->i_pic_parameter_set_id != p_prev->i_pic_parameter_set_id ||
        p_cur->i_field_pic_flag != p_prev->i_field_pic_flag ||
       !p_cur->i_nal_ref_idc != !p_prev->i_nal_ref_idc )
        return true;
    if( (p_cur->i_bottom_field_flag != -1) &&
        (p_prev->i_bottom_field_flag != -1) &&
        (p_cur->i_bottom_field_flag != p_prev->i_bottom_field_flag) )
        return true;
    if( p_cur->i_pic_order_cnt_type == 0 &&
       ( p_cur->i_pic_order_cnt_lsb != p_prev->i_pic_order_cnt_lsb ||
         p_cur->i_delta_pic_order_cnt_bottom != p_prev->i_delta_pic_order_cnt_bottom ) )
        return true;
    else if( p_cur->i_pic_order_cnt_type == 1 &&
           ( p_cur->i_delta_pic_order_cnt0 != p_prev->i_delta_pic_order_cnt0 ||
             p_cur->i_delta_pic_order_cnt1 != p_prev->i_delta_pic_order_cnt1 ) )
        return true;
    if( ( p_cur->i_nal_type == H264_NAL_SLICE_IDR || p_prev->i_nal_type == H264_NAL_SLICE_IDR ) &&
        ( p_cur->i_nal_type != p_prev->i_nal_type || p_cur->i_idr_pic_id != p_prev->i_idr_pic_id ) )
        return true;

    return false;
}

293 294
static void DropStoredNAL( decoder_sys_t *p_sys )
{
295 296 297 298 299 300
    block_ChainRelease( p_sys->frame.p_head );
    block_ChainRelease( p_sys->leading.p_head );
    p_sys->frame.p_head = NULL;
    p_sys->frame.pp_append = &p_sys->frame.p_head;
    p_sys->leading.p_head = NULL;
    p_sys->leading.pp_append = &p_sys->leading.p_head;
301 302
}

303 304
/*****************************************************************************
 * Open: probe the packetizer and return score
305 306
 * When opening after demux, the packetizer is only loaded AFTER the decoder
 * That means that what you set in fmt_out is ignored by the decoder in this special case
307 308 309 310 311
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    decoder_t     *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
312
    int i;
313

314 315
    const bool b_avc = (p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1' ));

316 317
    if( p_dec->fmt_in.i_codec != VLC_CODEC_H264 )
        return VLC_EGENERIC;
318
    if( b_avc && p_dec->fmt_in.i_extra < 7 )
319 320 321 322 323
        return VLC_EGENERIC;

    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
    {
324
        return VLC_ENOMEM;
325
    }
326

327 328 329 330 331 332 333
    p_sys->p_ccs = cc_storage_new();
    if( unlikely(!p_sys->p_ccs) )
    {
        free( p_dec->p_sys );
        return VLC_ENOMEM;
    }

334
    packetizer_Init( &p_sys->packetizer,
335
                     p_h264_startcode, sizeof(p_h264_startcode), startcode_FindAnnexB,
336
                     p_h264_startcode, 1, 5,
337 338
                     PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );

339
    p_sys->b_slice = false;
340 341 342 343
    p_sys->frame.p_head = NULL;
    p_sys->frame.pp_append = &p_sys->frame.p_head;
    p_sys->leading.p_head = NULL;
    p_sys->leading.pp_append = &p_sys->leading.p_head;
344 345
    p_sys->b_new_sps = false;
    p_sys->b_new_pps = false;
346

347
    for( i = 0; i <= H264_SPS_ID_MAX; i++ )
348 349 350 351 352
    {
        p_sys->sps[i].p_sps = NULL;
        p_sys->sps[i].p_block = NULL;
    }
    p_sys->p_active_sps = NULL;
353
    for( i = 0; i <= H264_PPS_ID_MAX; i++ )
354 355 356 357 358
    {
        p_sys->pps[i].p_pps = NULL;
        p_sys->pps[i].p_block = NULL;
    }
    p_sys->p_active_pps = NULL;
359
    p_sys->i_recovery_frame_cnt = UINT_MAX;
360

361
    h264_slice_init( &p_sys->slice );
362

363
    p_sys->i_next_block_flags = 0;
364 365
    p_sys->b_recovered = false;
    p_sys->i_recoveryfnum = UINT_MAX;
366 367
    p_sys->i_frame_dts = VLC_TS_INVALID;
    p_sys->i_frame_pts = VLC_TS_INVALID;
368
    p_sys->i_dpb_output_delay = 0;
Laurent Aimar's avatar
Laurent Aimar committed
369

370 371 372 373
    /* POC */
    h264_poc_context_init( &p_sys->pocctx );
    p_sys->prevdatedpoc.pts = VLC_TS_INVALID;

374
    date_Init( &p_sys->dts, 30000 * 2, 1001 );
375 376
    date_Set( &p_sys->dts, VLC_TS_INVALID );

377 378
    /* Setup properties */
    es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
379
    p_dec->fmt_out.i_codec = VLC_CODEC_H264;
380
    p_dec->fmt_out.b_packetized = true;
381

382
    if( p_dec->fmt_in.video.i_frame_rate_base &&
383 384
        p_dec->fmt_in.video.i_frame_rate &&
        p_dec->fmt_in.video.i_frame_rate <= UINT_MAX / 2 )
385 386 387 388 389
    {
        date_Change( &p_sys->dts, p_dec->fmt_in.video.i_frame_rate * 2,
                                  p_dec->fmt_in.video.i_frame_rate_base );
    }

390
    if( b_avc )
391
    {
392 393 394 395
        /* This type of stream is produced by mp4 and matroska
         * when we want to store it in another streamformat, you need to convert
         * The fmt_in.p_extra should ALWAYS contain the avcC
         * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
396
        if( h264_isavcC( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ) )
397
        {
398
            free( p_dec->fmt_out.p_extra );
399 400 401 402 403 404
            size_t i_size;
            p_dec->fmt_out.p_extra = h264_avcC_to_AnnexB_NAL( p_dec->fmt_in.p_extra,
                                                              p_dec->fmt_in.i_extra,
                                                             &i_size,
                                                             &p_sys->i_avcC_length_size );
            p_dec->fmt_out.i_extra = i_size;
405
            p_sys->b_recovered = !!p_dec->fmt_out.i_extra;
406 407

            if(!p_dec->fmt_out.p_extra)
408
            {
409
                msg_Err( p_dec, "Invalid AVC extradata");
410
                Close( p_this );
411
                return VLC_EGENERIC;
412
            }
413
        }
414 415
        else
        {
416
            msg_Err( p_dec, "Invalid or missing AVC extradata");
417
            Close( p_this );
418
            return VLC_EGENERIC;
419
        }
420

421 422
        /* Set callback */
        p_dec->pf_packetize = PacketizeAVC1;
423 424 425
    }
    else
    {
426
        /* This type of stream contains data with 3 of 4 byte startcodes
427 428
         * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
         * The fmt_out.p_extra should be the same */
429

430 431
        /* Set callback */
        p_dec->pf_packetize = Packetize;
432 433 434 435 436 437 438 439 440 441 442
    }

    /* */
    if( p_dec->fmt_out.i_extra > 0 )
    {
        packetizer_Header( &p_sys->packetizer,
                           p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
    }

    if( b_avc )
    {
443 444
        /* FIXME: that's not correct for every AVC */
        if( !p_sys->b_new_pps || !p_sys->b_new_sps )
445 446
        {
            msg_Err( p_dec, "Invalid or missing SPS %d or PPS %d in AVC extradata",
447
                     p_sys->b_new_sps, p_sys->b_new_pps );
448
            Close( p_this );
449 450
            return VLC_EGENERIC;
        }
451

452 453
        msg_Dbg( p_dec, "Packetizer fed with AVC, nal length size=%d",
                         p_sys->i_avcC_length_size );
454 455
    }

456 457
    /* CC are the same for H264/AVC in T35 sections (ETSI TS 101 154)  */
    p_dec->pf_get_cc = GetCc;
458
    p_dec->pf_flush = PacketizeFlush;
459

460 461 462 463 464 465 466 467 468 469
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: clean up the packetizer
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;
470
    int i;
471

472
    DropStoredNAL( p_sys );
473
    for( i = 0; i <= H264_SPS_ID_MAX; i++ )
474
        StoreSPS( p_sys, i, NULL, NULL );
475
    for( i = 0; i <= H264_PPS_ID_MAX; i++ )
476 477
        StorePPS( p_sys, i, NULL, NULL );

478 479
    packetizer_Clean( &p_sys->packetizer );

480
    cc_storage_delete( p_sys->p_ccs );
481

482 483 484
    free( p_sys );
}

485 486 487 488 489 490 491
static void PacketizeFlush( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    packetizer_Flush( &p_sys->packetizer );
}

492 493
/****************************************************************************
 * Packetize: the whole thing
494 495
 * Search for the startcodes 3 or more bytes
 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
496 497 498 499 500
 ****************************************************************************/
static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

501
    return packetizer_Packetize( &p_sys->packetizer, pp_block );
502 503
}

504
/****************************************************************************
505 506
 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
 * Will always use 4 byte 0 0 0 1 startcodes
507
 * Will prepend a SPS and PPS before each keyframe
508 509 510 511 512
 ****************************************************************************/
static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

513 514
    return PacketizeXXC1( p_dec, p_sys->i_avcC_length_size,
                          pp_block, ParseNALBlock );
515 516
}

517 518 519
/*****************************************************************************
 * GetCc:
 *****************************************************************************/
520
static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
521
{
522
    return cc_storage_get_current( p_dec->p_sys->p_ccs, p_desc );
523 524
}

Laurent Aimar's avatar
Laurent Aimar committed
525 526 527
/****************************************************************************
 * Helpers
 ****************************************************************************/
528 529 530 531 532 533 534 535 536 537 538 539 540 541
static void ResetOutputVariables( decoder_sys_t *p_sys )
{
    p_sys->i_frame_dts = VLC_TS_INVALID;
    p_sys->i_frame_pts = VLC_TS_INVALID;
    p_sys->slice.type = H264_SLICE_TYPE_UNKNOWN;
    p_sys->b_new_sps = false;
    p_sys->b_new_pps = false;
    p_sys->b_slice = false;
    /* From SEI */
    p_sys->i_dpb_output_delay = 0;
    p_sys->i_pic_struct = UINT8_MAX;
    p_sys->i_recovery_frame_cnt = UINT_MAX;
}

542 543 544 545 546
static void PacketizeReset( void *p_private, bool b_broken )
{
    decoder_t *p_dec = p_private;
    decoder_sys_t *p_sys = p_dec->p_sys;

547
    if( b_broken || !p_sys->b_slice )
548
    {
549
        DropStoredNAL( p_sys );
550
        ResetOutputVariables( p_sys );
551 552
        p_sys->p_active_pps = NULL;
        p_sys->p_active_sps = NULL;
553 554 555
        /* POC */
        h264_poc_context_init( &p_sys->pocctx );
        p_sys->prevdatedpoc.pts = VLC_TS_INVALID;
556
    }
557
    p_sys->i_next_block_flags = BLOCK_FLAG_DISCONTINUITY;
558 559
    p_sys->b_recovered = false;
    p_sys->i_recoveryfnum = UINT_MAX;
560
    date_Set( &p_sys->dts, VLC_TS_INVALID );
561 562 563 564 565 566
}
static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
{
    decoder_t *p_dec = p_private;

    /* Remove trailing 0 bytes */
567
    while( p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
568 569 570 571 572 573 574 575 576 577 578
        p_block->i_buffer--;

    return ParseNALBlock( p_dec, pb_ts_used, p_block );
}
static int PacketizeValidate( void *p_private, block_t *p_au )
{
    VLC_UNUSED(p_private);
    VLC_UNUSED(p_au);
    return VLC_SUCCESS;
}

579 580
/*****************************************************************************
 * ParseNALBlock: parses annexB type NALs
581
 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
582
 *****************************************************************************/
583
static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag )
584 585 586 587
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_pic = NULL;

588
    const int i_nal_type = p_frag->p_buffer[4]&0x1f;
Laurent Aimar's avatar
Laurent Aimar committed
589 590
    const mtime_t i_frag_dts = p_frag->i_dts;
    const mtime_t i_frag_pts = p_frag->i_pts;
591

592
    if( p_sys->b_slice && (!p_sys->p_active_pps || !p_sys->p_active_sps) )
593
    {
594
        msg_Warn( p_dec, "waiting for SPS/PPS" );
595

596
        /* Reset context */
597
        DropStoredNAL( p_sys );
598
        ResetOutputVariables( p_sys );
599
        cc_storage_reset( p_sys->p_ccs );
600 601
    }

602
    switch( i_nal_type )
603
    {
604 605 606 607 608 609
        /*** Slices ***/
        case H264_NAL_SLICE:
        case H264_NAL_SLICE_DPA:
        case H264_NAL_SLICE_DPB:
        case H264_NAL_SLICE_DPC:
        case H264_NAL_SLICE_IDR:
610
        {
611
            h264_slice_t newslice;
612

613 614 615 616 617 618
            if( i_nal_type == H264_NAL_SLICE_IDR )
            {
                p_sys->b_recovered = true;
                p_sys->i_recovery_frame_cnt = UINT_MAX;
                p_sys->i_recoveryfnum = UINT_MAX;
            }
619

620
            if( ParseSliceHeader( p_dec, p_frag, &newslice ) )
621
            {
622 623 624 625
                /* Only IDR carries the id, to be propagated */
                if( newslice.i_idr_pic_id == -1 )
                    newslice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;

626
                bool b_new_picture = IsFirstVCLNALUnit( &p_sys->slice, &newslice );
627
                if( b_new_picture )
628
                {
629 630 631 632 633 634 635 636 637 638 639
                    /* Parse SEI for that frame now we should have matched SPS/PPS */
                    for( block_t *p_sei = p_sys->leading.p_head; p_sei; p_sei = p_sei->p_next )
                    {
                        if( (p_sei->i_flags & BLOCK_FLAG_PRIVATE_SEI) == 0 )
                            continue;
                        HxxxParse_AnnexB_SEI( p_sei->p_buffer, p_sei->i_buffer,
                                              1 /* nal header */, ParseSeiCallback, p_dec );
                    }

                    if( p_sys->b_slice )
                        p_pic = OutputPicture( p_dec );
640 641
                }

642 643 644 645 646 647 648
                /* */
                p_sys->slice = newslice;
            }
            else
            {
                p_sys->p_active_pps = NULL;
                /* Fragment will be discarded later on */
649
            }
650
            p_sys->b_slice = true;
651

652 653
            block_ChainLastAppend( &p_sys->frame.pp_append, p_frag );
        } break;
654

655
        /*** Prefix NALs ***/
656

657 658 659
        case H264_NAL_AU_DELIMITER:
            if( p_sys->b_slice )
                p_pic = OutputPicture( p_dec );
660

661 662
            /* clear junk if no pic, we're always the first nal */
            DropStoredNAL( p_sys );
663

664
            p_frag->i_flags |= BLOCK_FLAG_PRIVATE_AUD;
665

666 667
            block_ChainLastAppend( &p_sys->leading.pp_append, p_frag );
        break;
Laurent Aimar's avatar
Laurent Aimar committed
668

669 670 671 672 673 674 675
        case H264_NAL_SPS:
        case H264_NAL_PPS:
            if( p_sys->b_slice )
                p_pic = OutputPicture( p_dec );

            /* Stored for insert on keyframes */
            if( i_nal_type == H264_NAL_SPS )
676
            {
677 678
                PutSPS( p_dec, p_frag );
                p_sys->b_new_sps = true;
679 680 681
            }
            else
            {
682 683
                PutPPS( p_dec, p_frag );
                p_sys->b_new_pps = true;
684
            }
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
        break;

        case H264_NAL_SEI:
            if( p_sys->b_slice )
                p_pic = OutputPicture( p_dec );

            p_frag->i_flags |= BLOCK_FLAG_PRIVATE_SEI;
            block_ChainLastAppend( &p_sys->leading.pp_append, p_frag );
        break;

        case H264_NAL_SPS_EXT:
        case H264_NAL_PREFIX: /* first slice/VCL associated data */
        case H264_NAL_SUBSET_SPS:
        case H264_NAL_DEPTH_PS:
        case H264_NAL_RESERVED_17:
        case H264_NAL_RESERVED_18:
            if( p_sys->b_slice )
                p_pic = OutputPicture( p_dec );

            block_ChainLastAppend( &p_sys->leading.pp_append, p_frag );
        break;

        /*** Suffix NALs ***/

        case H264_NAL_END_OF_SEQ:
        case H264_NAL_END_OF_STREAM:
            /* Early end of packetization */
            block_ChainLastAppend( &p_sys->frame.pp_append, p_frag );

            /* important for still pictures/menus */
            p_sys->i_next_block_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
            if( p_sys->b_slice )
                p_pic = OutputPicture( p_dec );
        break;

        case H264_NAL_SLICE_WP: // post
        case H264_NAL_UNKNOWN:
        case H264_NAL_FILLER_DATA:
        case H264_NAL_SLICE_EXT:
        case H264_NAL_SLICE_3D_EXT:
        case H264_NAL_RESERVED_22:
        case H264_NAL_RESERVED_23:
        default: /* others 24..31, including unknown */
            block_ChainLastAppend( &p_sys->frame.pp_append, p_frag );
        break;
730 731
    }

732
    *pb_ts_used = false;
733
    if( p_sys->i_frame_dts <= VLC_TS_INVALID &&
734
        p_sys->i_frame_pts <= VLC_TS_INVALID )
Laurent Aimar's avatar
Laurent Aimar committed
735 736 737
    {
        p_sys->i_frame_dts = i_frag_dts;
        p_sys->i_frame_pts = i_frag_pts;
738
        *pb_ts_used = true;
739 740
        if( i_frag_dts > VLC_TS_INVALID )
            date_Set( &p_sys->dts, i_frag_dts );
Laurent Aimar's avatar
Laurent Aimar committed
741
    }
742 743 744 745 746 747 748

    if( p_pic && (p_pic->i_flags & BLOCK_FLAG_DROP) )
    {
        block_Release( p_pic );
        p_pic = NULL;
    }

749 750
    return p_pic;
}
751

752 753 754
static block_t *OutputPicture( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
755 756
    block_t *p_pic = NULL;
    block_t **pp_pic_last = &p_pic;
757

758
    if( unlikely(!p_sys->frame.p_head) )
759
    {
760
        assert( p_sys->frame.p_head );
761 762
        DropStoredNAL( p_sys );
        ResetOutputVariables( p_sys );
763
        cc_storage_reset( p_sys->p_ccs );
764 765 766
        return NULL;
    }

767 768 769 770
    /* Bind matched/referred PPS and SPS */
    const h264_picture_parameter_set_t *p_pps = p_sys->p_active_pps;
    const h264_sequence_parameter_set_t *p_sps = p_sys->p_active_sps;
    if( !p_pps || !p_sps )
771 772
    {
        DropStoredNAL( p_sys );
773
        ResetOutputVariables( p_sys );
774
        cc_storage_reset( p_sys->p_ccs );
775
        return NULL;
776 777
    }

778 779
    if( !p_sys->b_recovered && p_sys->i_recoveryfnum == UINT_MAX &&
         p_sys->i_recovery_frame_cnt == UINT_MAX && p_sys->slice.type == H264_SLICE_TYPE_I )
780
    {
781 782 783 784 785 786 787 788 789 790 791 792
        /* No way to recover using SEI, just sync on I Slice */
        p_sys->b_recovered = true;
    }

    bool b_need_sps_pps = p_sys->slice.type == H264_SLICE_TYPE_I &&
                          p_sys->p_active_pps && p_sys->p_active_sps;

    /* Handle SEI recovery */
    if ( !p_sys->b_recovered && p_sys->i_recovery_frame_cnt != UINT_MAX &&
         p_sys->i_recoveryfnum == UINT_MAX )
    {
        p_sys->i_recoveryfnum = p_sys->slice.i_frame_num + p_sys->i_recovery_frame_cnt;
793
        p_sys->i_recoverystartfnum = p_sys->slice.i_frame_num;
794 795 796 797 798 799 800 801
        b_need_sps_pps = true; /* SPS/PPS must be inserted for SEI recovery */
        msg_Dbg( p_dec, "Recovering using SEI, prerolling %u reference pics", p_sys->i_recovery_frame_cnt );
    }

    if( p_sys->i_recoveryfnum != UINT_MAX )
    {
        assert(p_sys->b_recovered == false);
        const unsigned maxFrameNum = 1 << (p_sps->i_log2_max_frame_num + 4);
802 803 804 805 806 807

        if( ( p_sys->i_recoveryfnum > maxFrameNum &&
              p_sys->slice.i_frame_num < p_sys->i_recoverystartfnum &&
              p_sys->slice.i_frame_num >= p_sys->i_recoveryfnum % maxFrameNum ) ||
            ( p_sys->i_recoveryfnum <= maxFrameNum &&
              p_sys->slice.i_frame_num >= p_sys->i_recoveryfnum ) )
808 809 810 811 812
        {
            p_sys->i_recoveryfnum = UINT_MAX;
            p_sys->b_recovered = true;
            msg_Dbg( p_dec, "Recovery from SEI recovery point complete" );
        }
813
    }
814

815 816 817
    /* Gather PPS/SPS if required */
    block_t *p_xpsnal = NULL;
    block_t **pp_xpsnal_tail = &p_xpsnal;
818
    if( b_need_sps_pps || p_sys->b_new_sps || p_sys->b_new_pps )
819
    {
820
        for( int i = 0; i <= H264_SPS_ID_MAX && (b_need_sps_pps || p_sys->b_new_sps); i++ )
821
        {
822
            if( p_sys->sps[i].p_block )
823
                block_ChainLastAppend( &pp_xpsnal_tail, block_Duplicate( p_sys->sps[i].p_block ) );
824
        }
825
        for( int i = 0; i < H264_PPS_ID_MAX && (b_need_sps_pps || p_sys->b_new_pps); i++ )
826
        {
827
            if( p_sys->pps[i].p_block )
828
                block_ChainLastAppend( &pp_xpsnal_tail, block_Duplicate( p_sys->pps[i].p_block ) );
829
        }
830
    }
831 832

    /* Now rebuild NAL Sequence, inserting PPS/SPS if any */
833
    if( p_sys->frame.p_head->i_flags & BLOCK_FLAG_PRIVATE_AUD )
834
    {
835 836
        block_t *p_au = p_sys->frame.p_head;
        p_sys->frame.p_head = p_au->p_next;
837 838
        p_au->p_next = NULL;
        block_ChainLastAppend( &pp_pic_last, p_au );
839
    }
840

841 842 843
    if( p_xpsnal )
        block_ChainLastAppend( &pp_pic_last, p_xpsnal );

844 845
    if( p_sys->leading.p_head )
        block_ChainLastAppend( &pp_pic_last, p_sys->leading.p_head );
846

847 848 849
    assert( p_sys->frame.p_head );
    if( p_sys->frame.p_head )
        block_ChainLastAppend( &pp_pic_last, p_sys->frame.p_head );
850 851

    /* Reset chains, now empty */
852 853 854 855
    p_sys->frame.p_head = NULL;
    p_sys->frame.pp_append = &p_sys->frame.p_head;
    p_sys->leading.p_head = NULL;
    p_sys->leading.pp_append = &p_sys->leading.p_head;
856 857 858

    p_pic = block_ChainGather( p_pic );

859
    if( !p_pic )
860 861
    {
        ResetOutputVariables( p_sys );
862
        cc_storage_reset( p_sys->p_ccs );
863
        return NULL;
864
    }
865

866 867 868
    /* clear up flags gathered */
    p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_MASK;

869 870 871 872 873
    /* for PTS Fixup, interlaced fields (multiple AU/block) */
    int tFOC = 0, bFOC = 0, PictureOrderCount = 0;
    h264_compute_poc( p_sps, &p_sys->slice, &p_sys->pocctx, &PictureOrderCount, &tFOC, &bFOC );

    unsigned i_num_clock_ts = h264_get_num_ts( p_sps, &p_sys->slice, p_sys->i_pic_struct, tFOC, bFOC );
874

875
    if( p_sps->frame_mbs_only_flag == 0 && p_sps->vui.b_pic_struct_present_flag )
876 877 878
    {
        switch( p_sys->i_pic_struct )
        {
879
        /* Top and Bottom field slices */
880
        case 1:
881
        case 2:
882
            p_pic->i_flags |= BLOCK_FLAG_SINGLE_FIELD;
883 884
            p_pic->i_flags |= (!p_sys->slice.i_bottom_field_flag) ? BLOCK_FLAG_TOP_FIELD_FIRST
                                                                  : BLOCK_FLAG_BOTTOM_FIELD_FIRST;
885
            break;
886
        /* Each of the following slices contains multiple fields */
887 888 889 890
        case 3:
            p_pic->i_flags |= BLOCK_FLAG_TOP_FIELD_FIRST;
            break;
        case 4:
891 892 893 894 895
            p_pic->i_flags |= BLOCK_FLAG_BOTTOM_FIELD_FIRST;
            break;
        case 5:
            p_pic->i_flags |= BLOCK_FLAG_TOP_FIELD_FIRST;
            break;
896 897 898 899 900 901 902 903
        case 6:
            p_pic->i_flags |= BLOCK_FLAG_BOTTOM_FIELD_FIRST;
            break;
        default:
            break;
        }
    }

904
    /* set dts/pts to current block timestamps */
905 906
    p_pic->i_dts = p_sys->i_frame_dts;
    p_pic->i_pts = p_sys->i_frame_pts;
907 908 909

    /* Fixup missing timestamps after split (multiple AU/block)*/
    if( p_pic->i_dts <= VLC_TS_INVALID )
910
        p_pic->i_dts = date_Get( &p_sys->dts );
911

912
    if( p_sys->slice.type == H264_SLICE_TYPE_I )
913 914 915 916
        p_sys->prevdatedpoc.pts = VLC_TS_INVALID;

    if( p_pic->i_pts == VLC_TS_INVALID )
    {
917 918
        if( p_sys->prevdatedpoc.pts > VLC_TS_INVALID &&
            date_Get( &p_sys->dts ) != VLC_TS_INVALID )
919 920 921 922 923 924 925 926 927 928 929 930 931 932
        {
            date_t pts = p_sys->dts;
            date_Set( &pts, p_sys->prevdatedpoc.pts );

            int diff = tFOC - p_sys->prevdatedpoc.num;
            if( diff > 0 )
                date_Increment( &pts, diff );
            else
                date_Decrement( &pts, -diff );

            p_pic->i_pts = date_Get( &pts );
        }
        /* In case there's no PTS at all */
        else if( p_sys->slice.i_nal_ref_idc == 0 &&
933
                 p_sys->slice.type == H264_SLICE_TYPE_B )
934 935 936
        {
            p_pic->i_pts = p_pic->i_dts;
        }
937 938
        else if( p_sys->slice.type == H264_SLICE_TYPE_I &&
                 date_Get( &p_sys->dts ) != VLC_TS_INVALID )
939 940 941 942 943 944 945 946 947 948 949 950
        {
            /* Hell no PTS on IDR. We're totally blind */
            date_t pts = p_sys->dts;
            date_Increment( &pts, 2 );
            p_pic->i_pts = date_Get( &pts );
        }
    }

    if( p_pic->i_pts > VLC_TS_INVALID )
    {
        p_sys->prevdatedpoc.pts = p_pic->i_pts;
        p_sys->prevdatedpoc.num = PictureOrderCount;
951
    }
952

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
    if( p_pic->i_length == 0 )
    {
        if( p_sps->vui.i_time_scale )
        {
            p_pic->i_length = CLOCK_FREQ * i_num_clock_ts *
                              p_sps->vui.i_num_units_in_tick / p_sps->vui.i_time_scale;
        }
        else
        {
            date_t next = p_sys->dts;
            date_Increment( &next, i_num_clock_ts );
            p_pic->i_length = date_Get( &next ) - date_Get( &p_sys->dts );
        }
    }

968
#if 0
969
    msg_Err(p_dec, "F/BOC %d/%d POC %d %d rec %d flags %x ref%d fn %d fp %d %d pts %ld len %ld",
970
                    tFOC, bFOC, PictureOrderCount,
971
                    p_sys->slice.type, p_sys->b_recovered, p_pic->i_flags,
972
                    p_sys->slice.i_nal_ref_idc, p_sys->slice.i_frame_num, p_sys->slice.i_field_pic_flag,
973
                    p_pic->i_pts - p_pic->i_dts, p_pic->i_pts % (100*CLOCK_FREQ), p_pic->i_length);
974 975
#endif

976 977
    /* save for next pic fixups */
    if( date_Get( &p_sys->dts ) != VLC_TS_INVALID )
978
    {
979 980 981
        if( p_sys->i_next_block_flags & BLOCK_FLAG_DISCONTINUITY )
            date_Set( &p_sys->dts, VLC_TS_INVALID );
        else
982
            date_Increment( &p_sys->dts, i_num_clock_ts );
983
    }
984 985

    if( p_pic )
986
    {
987 988
        p_pic->i_flags |= p_sys->i_next_block_flags;
        p_sys->i_next_block_flags = 0;
989
    }
990

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    switch( p_sys->slice.type )
    {
        case H264_SLICE_TYPE_P:
            p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
            break;
        case H264_SLICE_TYPE_B:
            p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
            break;
        case H264_SLICE_TYPE_I:
            p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
        default:
            break;
    }

1005 1006 1007 1008 1009 1010 1011 1012
    if( !p_sys->b_recovered )
    {
        if( p_sys->i_recoveryfnum != UINT_MAX ) /* recovering from SEI */
            p_pic->i_flags |= BLOCK_FLAG_PREROLL;
        else
            p_pic->i_flags |= BLOCK_FLAG_DROP;
    }

1013 1014
    p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_AUD;

1015
    /* reset after output */
1016
    ResetOutputVariables( p_sys );
1017

1018
    /* CC */
1019
    cc_storage_commit( p_sys->p_ccs, p_pic );
1020

1021 1022
    return p_pic;
}
1023

1024 1025 1026
static void PutSPS( decoder_t *p_dec, block_t *p_frag )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
1027

1028 1029 1030 1031
    const uint8_t *p_buffer = p_frag->p_buffer;
    size_t i_buffer = p_frag->i_buffer;

    if( !hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
1032 1033
    {
        block_Release( p_frag );
1034
        return;
1035
    }
1036 1037 1038

    h264_sequence_parameter_set_t *p_sps = h264_decode_sps( p_buffer, i_buffer, true );
    if( !p_sps )
1039
    {
1040
        msg_Warn( p_dec, "invalid SPS" );
1041 1042 1043 1044
        block_Release( p_frag );
        return;
    }

1045
    /* We have a new SPS */
1046
    if( !p_sys->sps[p_sps->i_id].p_sps )
1047
        msg_Dbg( p_dec, "found NAL_SPS (sps_id=%d)", p_sps->i_id );
1048

1049
    StoreSPS( p_sys, p_sps->i_id, p_frag, p_sps );
1050
}
1051

1052 1053 1054
static void PutPPS( decoder_t *p_dec, block_t *p_frag )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
1055 1056
    const uint8_t *p_buffer = p_frag->p_buffer;
    size_t i_buffer = p_frag->i_buffer;
1057

1058
    if( !hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
1059 1060
    {
        block_Release( p_frag );
1061
        return;
1062
    }
1063 1064 1065

    h264_picture_parameter_set_t *p_pps = h264_decode_pps( p_buffer, i_buffer, true );
    if( !p_pps )
1066
    {
1067
        msg_Warn( p_dec, "invalid PPS" );
1068 1069 1070
        block_Release( p_frag );
        return;
    }
1071

1072
    /* We have a new PPS */
1073
    if( !p_sys->pps[p_pps->i_id].p_pps )
François Cartegnie's avatar