h264_nal.c 27.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*****************************************************************************
 * Copyright © 2010-2014 VideoLAN
 *
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
 *
 * 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
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/
20 21 22
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
23

24 25
#include <assert.h>

26
#include "h264_nal.h"
27
#include "hxxx_nal.h"
28
#include "hxxx_ep3b.h"
29
#include "iso_color_tables.h"
30

31
#include <vlc_bits.h>
32
#include <vlc_boxes.h>
33
#include <vlc_es.h>
34 35
#include <limits.h>

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
/* H264 Level limits from Table A-1 */
typedef struct
{
    unsigned i_max_dpb_mbs;
} h264_level_limits_t;

enum h264_level_numbers_e
{
    H264_LEVEL_NUMBER_1_B = 9, /* special level not following the 10x rule */
    H264_LEVEL_NUMBER_1   = 10,
    H264_LEVEL_NUMBER_1_1 = 11,
    H264_LEVEL_NUMBER_1_2 = 12,
    H264_LEVEL_NUMBER_1_3 = 13,
    H264_LEVEL_NUMBER_2   = 20,
    H264_LEVEL_NUMBER_2_1 = 21,
    H264_LEVEL_NUMBER_2_2 = 22,
    H264_LEVEL_NUMBER_3   = 30,
    H264_LEVEL_NUMBER_3_1 = 31,
    H264_LEVEL_NUMBER_3_2 = 32,
    H264_LEVEL_NUMBER_4   = 40,
    H264_LEVEL_NUMBER_4_1 = 41,
    H264_LEVEL_NUMBER_4_2 = 42,
    H264_LEVEL_NUMBER_5   = 50,
    H264_LEVEL_NUMBER_5_1 = 51,
    H264_LEVEL_NUMBER_5_2 = 52,
};

const struct
{
    const uint16_t i_level;
    const h264_level_limits_t limits;
} h264_levels_limits[] = {
    { H264_LEVEL_NUMBER_1_B, { 396 } },
    { H264_LEVEL_NUMBER_1,   { 396 } },
    { H264_LEVEL_NUMBER_1_1, { 900 } },
    { H264_LEVEL_NUMBER_1_2, { 2376 } },
    { H264_LEVEL_NUMBER_1_3, { 2376 } },
    { H264_LEVEL_NUMBER_2,   { 2376 } },
    { H264_LEVEL_NUMBER_2_1, { 4752 } },
    { H264_LEVEL_NUMBER_2_2, { 8100 } },
    { H264_LEVEL_NUMBER_3,   { 8100 } },
    { H264_LEVEL_NUMBER_3_1, { 18000 } },
    { H264_LEVEL_NUMBER_3_2, { 20480 } },
    { H264_LEVEL_NUMBER_4,   { 32768 } },
    { H264_LEVEL_NUMBER_4_1, { 32768 } },
    { H264_LEVEL_NUMBER_4_2, { 34816 } },
    { H264_LEVEL_NUMBER_5,   { 110400 } },
    { H264_LEVEL_NUMBER_5_1, { 184320 } },
    { H264_LEVEL_NUMBER_5_2, { 184320 } },
};

87 88 89 90 91
/*
 * For avcC specification, see ISO/IEC 14496-15,
 * For Annex B specification, see ISO/IEC 14496-10
 */

92 93
bool h264_isavcC( const uint8_t *p_buf, size_t i_buf )
{
94
    return ( i_buf >= H264_MIN_AVCC_SIZE &&
95 96 97
             p_buf[0] != 0x00 &&
             p_buf[1] != 0x00
/*          /!\Broken quicktime streams does not respect reserved bits
98 99
            (p_buf[4] & 0xFC) == 0xFC &&
            (p_buf[4] & 0x03) != 0x02 &&
100 101
            (p_buf[5] & 0x1F) > 0x00 */
            );
102 103
}

104
static size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
105
{
106
    size_t i_total = 0;
107

108 109
    if( i_buf < H264_MIN_AVCC_SIZE )
        return 0;
110

111 112 113
    p_buf += 5;
    i_buf -= 5;

114 115 116
    for ( unsigned int j = 0; j < 2; j++ )
    {
        /* First time is SPS, Second is PPS */
117 118 119 120
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
        p_buf++; i_buf--;

        for ( unsigned int i = 0; i < i_loop_end; i++ )
121
        {
122 123 124
            if( i_buf < 2 )
                return 0;

125 126 127 128
            uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
            if(i_nal_size > i_buf - 2)
                return 0;
            i_total += i_nal_size + 4;
129
            p_buf += i_nal_size + 2;
130
            i_buf -= i_nal_size + 2;
131
        }
132 133 134

        if( j == 0 && i_buf < 1 )
            return 0;
135 136 137
    }
    return i_total;
}
138

139 140 141 142 143 144
uint8_t *h264_avcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
                                  size_t *pi_result, uint8_t *pi_nal_length_size )
{
    *pi_result = get_avcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does check min size */
    if( *pi_result == 0 )
        return NULL;
145

146 147 148
    /* Read infos in first 6 bytes */
    if ( pi_nal_length_size )
        *pi_nal_length_size = (p_buf[4] & 0x03) + 1;
149

150 151 152
    uint8_t *p_ret;
    uint8_t *p_out_buf = p_ret = malloc( *pi_result );
    if( !p_out_buf )
153 154
    {
        *pi_result = 0;
155
        return NULL;
156
    }
157 158

    p_buf += 5;
159

160 161 162 163
    for ( unsigned int j = 0; j < 2; j++ )
    {
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
        p_buf++;
164

165 166 167 168 169 170 171
        for ( unsigned int i = 0; i < i_loop_end; i++)
        {
            uint16_t i_nal_size = (p_buf[0] << 8) | p_buf[1];
            p_buf += 2;

            memcpy( p_out_buf, annexb_startcode4, 4 );
            p_out_buf += 4;
172

173 174
            memcpy( p_out_buf, p_buf, i_nal_size );
            p_out_buf += i_nal_size;
175 176 177 178
            p_buf += i_nal_size;
        }
    }

179
    return p_ret;
180 181
}

182
void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
183
                             uint8_t i_nal_length_size )
184
{
185 186
    uint32_t nal_len = 0;
    uint8_t nal_pos = 0;
187

188
    if( i_nal_length_size != 4 )
189 190
        return;

191 192
    /* This only works for a NAL length size of 4 */
    /* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
193 194
    while( i_len > 0 )
    {
195
        if( nal_pos < i_nal_length_size ) {
196
            unsigned int i;
197
            for( i = 0; nal_pos < i_nal_length_size && i < i_len; i++, nal_pos++ ) {
198
                nal_len = (nal_len << 8) | p_buf[i];
199 200
                p_buf[i] = 0;
            }
201
            if( nal_pos < i_nal_length_size )
202 203 204 205 206
                return;
            p_buf[i - 1] = 1;
            p_buf += i;
            i_len -= i;
        }
207
        if( nal_len > INT_MAX )
208
            return;
209
        if( nal_len > i_len )
210
        {
211
            nal_len -= i_len;
212 213 214 215
            return;
        }
        else
        {
216 217 218 219
            p_buf += nal_len;
            i_len -= nal_len;
            nal_len = 0;
            nal_pos = 0;
220 221 222 223
        }
    }
}

224 225 226 227
bool h264_AnnexB_get_spspps( const uint8_t *p_buf, size_t i_buf,
                             const uint8_t **pp_sps, size_t *p_sps_size,
                             const uint8_t **pp_pps, size_t *p_pps_size,
                             const uint8_t **pp_ext, size_t *p_ext_size )
Thomas Guillem's avatar
Thomas Guillem committed
228
{
229 230 231
    if( pp_sps ) { *p_sps_size = 0; *pp_sps = NULL; }
    if( pp_pps ) { *p_pps_size = 0; *pp_pps = NULL; }
    if( pp_ext ) { *p_ext_size = 0; *pp_ext = NULL; }
Thomas Guillem's avatar
Thomas Guillem committed
232

233 234 235 236 237
    hxxx_iterator_ctx_t it;
    hxxx_iterator_init( &it, p_buf, i_buf, 0 );

    const uint8_t *p_nal; size_t i_nal;
    while( hxxx_annexb_iterate_next( &it, &p_nal, &i_nal ) )
Thomas Guillem's avatar
Thomas Guillem committed
238
    {
239 240
        if( i_nal < 2 )
            continue;
Thomas Guillem's avatar
Thomas Guillem committed
241

242
        const enum h264_nal_unit_type_e i_nal_type = p_nal[0] & 0x1F;
Thomas Guillem's avatar
Thomas Guillem committed
243

244 245
        if ( i_nal_type <= H264_NAL_SLICE_IDR && i_nal_type != H264_NAL_UNKNOWN )
            break;
Thomas Guillem's avatar
Thomas Guillem committed
246

247 248 249 250 251 252 253 254 255 256
#define IFSET_NAL(type, var) \
    if( i_nal_type == type && pp_##var && *pp_##var == NULL )\
        { *pp_##var = p_nal; *p_##var##_size = i_nal; }

        IFSET_NAL(H264_NAL_SPS, sps)
        else
        IFSET_NAL(H264_NAL_PPS, pps)
        else
        IFSET_NAL(H264_NAL_SPS_EXT, ext);
#undef IFSET_NAL
Thomas Guillem's avatar
Thomas Guillem committed
257
    }
258 259

    return (pp_sps && *p_sps_size) || (pp_pps && *p_pps_size);
Thomas Guillem's avatar
Thomas Guillem committed
260 261
}

262
void h264_release_sps( h264_sequence_parameter_set_t *p_sps )
263
{
264 265
    free( p_sps );
}
266

267 268
#define H264_CONSTRAINT_SET_FLAG(N) (0x80 >> N)

269 270 271 272
static bool h264_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
                                                    h264_sequence_parameter_set_t *p_sps )
{
    int i_tmp;
273

274
    int i_profile_idc = bs_read( p_bs, 8 );
275
    p_sps->i_profile = i_profile_idc;
276
    p_sps->i_constraint_set_flags = bs_read( p_bs, 8 );
277
    p_sps->i_level = bs_read( p_bs, 8 );
278
    /* sps id */
279 280
    uint32_t i_sps_id = bs_read_ue( p_bs );
    if( i_sps_id > H264_SPS_ID_MAX )
281
        return false;
282
    p_sps->i_id = i_sps_id;
283

284 285 286
    if( i_profile_idc == PROFILE_H264_HIGH ||
        i_profile_idc == PROFILE_H264_HIGH_10 ||
        i_profile_idc == PROFILE_H264_HIGH_422 ||
287
        i_profile_idc == PROFILE_H264_HIGH_444 || /* Old one, no longer on spec */
288 289 290 291 292
        i_profile_idc == PROFILE_H264_HIGH_444_PREDICTIVE ||
        i_profile_idc == PROFILE_H264_CAVLC_INTRA ||
        i_profile_idc == PROFILE_H264_SVC_BASELINE ||
        i_profile_idc == PROFILE_H264_SVC_HIGH ||
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_HIGH ||
293 294 295 296
        i_profile_idc == PROFILE_H264_MVC_STEREO_HIGH ||
        i_profile_idc == PROFILE_H264_MVC_MULTIVIEW_DEPTH_HIGH ||
        i_profile_idc == PROFILE_H264_MVC_ENHANCED_MULTIVIEW_DEPTH_HIGH ||
        i_profile_idc == PROFILE_H264_MFC_HIGH )
297 298
    {
        /* chroma_format_idc */
299 300
        p_sps->i_chroma_idc = bs_read_ue( p_bs );
        if( p_sps->i_chroma_idc == 3 )
301 302 303
            p_sps->b_separate_colour_planes_flag = bs_read1( p_bs );
        else
            p_sps->b_separate_colour_planes_flag = 0;
304
        /* bit_depth_luma_minus8 */
305
        p_sps->i_bit_depth_luma = bs_read_ue( p_bs ) + 8;
306
        /* bit_depth_chroma_minus8 */
307
        p_sps->i_bit_depth_chroma = bs_read_ue( p_bs ) + 8;
308
        /* qpprime_y_zero_transform_bypass_flag */
309
        bs_skip( p_bs, 1 );
310
        /* seq_scaling_matrix_present_flag */
311
        i_tmp = bs_read( p_bs, 1 );
312 313
        if( i_tmp )
        {
314
            for( int i = 0; i < ((3 != p_sps->i_chroma_idc) ? 8 : 12); i++ )
315 316
            {
                /* seq_scaling_list_present_flag[i] */
317
                i_tmp = bs_read( p_bs, 1 );
318 319 320 321 322 323 324 325 326 327 328
                if( !i_tmp )
                    continue;
                const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
                /* scaling_list (...) */
                int i_lastscale = 8;
                int i_nextscale = 8;
                for( int j = 0; j < i_size_of_scaling_list; j++ )
                {
                    if( i_nextscale != 0 )
                    {
                        /* delta_scale */
329
                        i_tmp = bs_read_se( p_bs );
330 331 332 333 334 335 336 337 338
                        i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
                        /* useDefaultScalingMatrixFlag = ... */
                    }
                    /* scalinglist[j] */
                    i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
                }
            }
        }
    }
339 340
    else
    {
341 342 343
        p_sps->i_chroma_idc = 1; /* Not present == inferred to 4:2:0 */
        p_sps->i_bit_depth_luma = 8;
        p_sps->i_bit_depth_chroma = 8;
344
    }
345 346

    /* Skip i_log2_max_frame_num */
347
    p_sps->i_log2_max_frame_num = bs_read_ue( p_bs );
348 349 350
    if( p_sps->i_log2_max_frame_num > 12)
        p_sps->i_log2_max_frame_num = 12;
    /* Read poc_type */
351
    p_sps->i_pic_order_cnt_type = bs_read_ue( p_bs );
352 353 354
    if( p_sps->i_pic_order_cnt_type == 0 )
    {
        /* skip i_log2_max_poc_lsb */
355
        p_sps->i_log2_max_pic_order_cnt_lsb = bs_read_ue( p_bs );
356 357 358 359 360
        if( p_sps->i_log2_max_pic_order_cnt_lsb > 12 )
            p_sps->i_log2_max_pic_order_cnt_lsb = 12;
    }
    else if( p_sps->i_pic_order_cnt_type == 1 )
    {
361
        p_sps->i_delta_pic_order_always_zero_flag = bs_read( p_bs, 1 );
362 363 364 365 366 367 368
        p_sps->offset_for_non_ref_pic = bs_read_se( p_bs );
        p_sps->offset_for_top_to_bottom_field = bs_read_se( p_bs );
        p_sps->i_num_ref_frames_in_pic_order_cnt_cycle = bs_read_ue( p_bs );
        if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 255 )
            return false;
        for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ )
            p_sps->offset_for_ref_frame[i] = bs_read_se( p_bs );
369 370
    }
    /* i_num_ref_frames */
371
    bs_read_ue( p_bs );
372
    /* b_gaps_in_frame_num_value_allowed */
373
    bs_skip( p_bs, 1 );
374 375

    /* Read size */
376 377
    p_sps->pic_width_in_mbs_minus1 = bs_read_ue( p_bs );
    p_sps->pic_height_in_map_units_minus1 = bs_read_ue( p_bs );
378 379

    /* b_frame_mbs_only */
380 381
    p_sps->frame_mbs_only_flag = bs_read( p_bs, 1 );
    if( !p_sps->frame_mbs_only_flag )
382
        p_sps->mb_adaptive_frame_field_flag = bs_read( p_bs, 1 );
383

384
    /* b_direct8x8_inference */
385
    bs_skip( p_bs, 1 );
386 387

    /* crop */
388
    if( bs_read1( p_bs ) ) /* frame_cropping_flag */
389
    {
390 391
        p_sps->frame_crop.left_offset = bs_read_ue( p_bs );
        p_sps->frame_crop.right_offset = bs_read_ue( p_bs );
392
        p_sps->frame_crop.top_offset = bs_read_ue( p_bs );
393
        p_sps->frame_crop.bottom_offset = bs_read_ue( p_bs );
394 395 396
    }

    /* vui */
397
    i_tmp = bs_read( p_bs, 1 );
398 399 400 401
    if( i_tmp )
    {
        p_sps->vui.b_valid = true;
        /* read the aspect ratio part if any */
402
        i_tmp = bs_read( p_bs, 1 );
403 404 405 406 407 408 409 410 411 412
        if( i_tmp )
        {
            static const struct { int w, h; } sar[17] =
            {
                { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
                { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
                { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
                { 64, 33 }, { 160,99 }, {  4,  3 }, {  3,  2 },
                {  2,  1 },
            };
413
            int i_sar = bs_read( p_bs, 8 );
414 415 416 417 418 419 420 421 422
            int w, h;

            if( i_sar < 17 )
            {
                w = sar[i_sar].w;
                h = sar[i_sar].h;
            }
            else if( i_sar == 255 )
            {
423 424
                w = bs_read( p_bs, 16 );
                h = bs_read( p_bs, 16 );
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
            }
            else
            {
                w = 0;
                h = 0;
            }

            if( w != 0 && h != 0 )
            {
                p_sps->vui.i_sar_num = w;
                p_sps->vui.i_sar_den = h;
            }
            else
            {
                p_sps->vui.i_sar_num = 1;
                p_sps->vui.i_sar_den = 1;
            }
        }

        /* overscan */
445
        i_tmp = bs_read( p_bs, 1 );
446
        if ( i_tmp )
447
            bs_read( p_bs, 1 );
448 449

        /* video signal type */
450
        i_tmp = bs_read( p_bs, 1 );
451 452
        if( i_tmp )
        {
453 454
            bs_read( p_bs, 3 );
            p_sps->vui.colour.b_full_range = bs_read( p_bs, 1 );
455
            /* colour desc */
456
            i_tmp = bs_read( p_bs, 1 );
457
            if ( i_tmp )
458 459 460 461 462 463 464
            {
                p_sps->vui.colour.i_colour_primaries = bs_read( p_bs, 8 );
                p_sps->vui.colour.i_transfer_characteristics = bs_read( p_bs, 8 );
                p_sps->vui.colour.i_matrix_coefficients = bs_read( p_bs, 8 );
            }
            else
            {
465 466 467
                p_sps->vui.colour.i_colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
                p_sps->vui.colour.i_transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
                p_sps->vui.colour.i_matrix_coefficients = ISO_23001_8_MC_UNSPECIFIED;
468
            }
469 470 471
        }

        /* chroma loc info */
472
        i_tmp = bs_read( p_bs, 1 );
473 474
        if( i_tmp )
        {
475 476
            bs_read_ue( p_bs );
            bs_read_ue( p_bs );
477 478 479
        }

        /* timing info */
480
        p_sps->vui.b_timing_info_present_flag = bs_read( p_bs, 1 );
481 482
        if( p_sps->vui.b_timing_info_present_flag )
        {
483 484 485
            p_sps->vui.i_num_units_in_tick = bs_read( p_bs, 32 );
            p_sps->vui.i_time_scale = bs_read( p_bs, 32 );
            p_sps->vui.b_fixed_frame_rate = bs_read( p_bs, 1 );
486 487 488
        }

        /* Nal hrd & VC1 hrd parameters */
489
        p_sps->vui.b_hrd_parameters_present_flag = false;
490 491
        for ( int i=0; i<2; i++ )
        {
492
            i_tmp = bs_read( p_bs, 1 );
493 494
            if( i_tmp )
            {
495
                p_sps->vui.b_hrd_parameters_present_flag = true;
496
                uint32_t count = bs_read_ue( p_bs ) + 1;
497 498
                if( count > 31 )
                    return false;
499 500
                bs_read( p_bs, 4 );
                bs_read( p_bs, 4 );
501
                for( uint32_t j = 0; j < count; j++ )
502
                {
503 504
                    if( bs_remain( p_bs ) < 23 )
                        return false;
505 506 507
                    bs_read_ue( p_bs );
                    bs_read_ue( p_bs );
                    bs_read( p_bs, 1 );
508
                }
509 510 511 512
                bs_read( p_bs, 5 );
                p_sps->vui.i_cpb_removal_delay_length_minus1 = bs_read( p_bs, 5 );
                p_sps->vui.i_dpb_output_delay_length_minus1 = bs_read( p_bs, 5 );
                bs_read( p_bs, 5 );
513 514 515
            }
        }

516
        if( p_sps->vui.b_hrd_parameters_present_flag )
517
            bs_read( p_bs, 1 ); /* low delay hrd */
518 519

        /* pic struct info */
520
        p_sps->vui.b_pic_struct_present_flag = bs_read( p_bs, 1 );
521

522 523 524 525 526 527 528 529 530 531 532
        p_sps->vui.b_bitstream_restriction_flag = bs_read( p_bs, 1 );
        if( p_sps->vui.b_bitstream_restriction_flag )
        {
            bs_read( p_bs, 1 ); /* motion vector pic boundaries */
            bs_read_ue( p_bs ); /* max bytes per pic */
            bs_read_ue( p_bs ); /* max bits per mb */
            bs_read_ue( p_bs ); /* log2 max mv h */
            bs_read_ue( p_bs ); /* log2 max mv v */
            p_sps->vui.i_max_num_reorder_frames = bs_read_ue( p_bs );
            bs_read_ue( p_bs ); /* max dec frame buffering */
        }
533 534
    }

535
    return true;
536 537
}

538
void h264_release_pps( h264_picture_parameter_set_t *p_pps )
539
{
540 541
    free( p_pps );
}
542

543 544 545
static bool h264_parse_picture_parameter_set_rbsp( bs_t *p_bs,
                                                   h264_picture_parameter_set_t *p_pps )
{
546 547 548
    uint32_t i_pps_id = bs_read_ue( p_bs ); // pps id
    uint32_t i_sps_id = bs_read_ue( p_bs ); // sps id
    if( i_pps_id > H264_PPS_ID_MAX || i_sps_id > H264_SPS_ID_MAX )
549
        return false;
550 551
    p_pps->i_id = i_pps_id;
    p_pps->i_sps_id = i_sps_id;
552 553 554

    bs_skip( p_bs, 1 ); // entropy coding mode flag
    p_pps->i_pic_order_present_flag = bs_read( p_bs, 1 );
555 556

    unsigned num_slice_groups = bs_read_ue( p_bs ) + 1;
557 558
    if( num_slice_groups > 8 ) /* never has value > 7. Annex A, G & J */
        return false;
559
    if( num_slice_groups > 1 )
560 561 562 563
    {
        unsigned slice_group_map_type = bs_read_ue( p_bs );
        if( slice_group_map_type == 0 )
        {
564
            for( unsigned i = 0; i < num_slice_groups; i++ )
565 566 567 568
                bs_read_ue( p_bs ); /* run_length_minus1[group] */
        }
        else if( slice_group_map_type == 2 )
        {
569
            for( unsigned i = 0; i < num_slice_groups; i++ )
570 571 572 573 574 575 576 577 578 579 580 581
            {
                bs_read_ue( p_bs ); /* top_left[group] */
                bs_read_ue( p_bs ); /* bottom_right[group] */
            }
        }
        else if( slice_group_map_type > 2 && slice_group_map_type < 6 )
        {
            bs_read1( p_bs );   /* slice_group_change_direction_flag */
            bs_read_ue( p_bs ); /* slice_group_change_rate_minus1 */
        }
        else if( slice_group_map_type == 6 )
        {
582
            unsigned pic_size_in_maps_units = bs_read_ue( p_bs ) + 1;
583
            unsigned sliceGroupSize = 1;
584
            while(num_slice_groups > 1)
585 586
            {
                sliceGroupSize++;
587
                num_slice_groups = ((num_slice_groups - 1) >> 1) + 1;
588
            }
589
            for( unsigned i = 0; i < pic_size_in_maps_units; i++ )
590
            {
591
                bs_skip( p_bs, sliceGroupSize );
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
            }
        }
    }

    bs_read_ue( p_bs ); /* num_ref_idx_l0_default_active_minus1 */
    bs_read_ue( p_bs ); /* num_ref_idx_l1_default_active_minus1 */
    p_pps->weighted_pred_flag = bs_read( p_bs, 1 );
    p_pps->weighted_bipred_idc = bs_read( p_bs, 2 );
    bs_read_se( p_bs ); /* pic_init_qp_minus26 */
    bs_read_se( p_bs ); /* pic_init_qs_minus26 */
    bs_read_se( p_bs ); /* chroma_qp_index_offset */
    bs_read( p_bs, 1 ); /* deblocking_filter_control_present_flag */
    bs_read( p_bs, 1 ); /* constrained_intra_pred_flag */
    p_pps->i_redundant_pic_present_flag = bs_read( p_bs, 1 );

607 608
    /* TODO */

609
    return true;
610 611
}

612 613 614 615 616 617 618
#define IMPL_h264_generic_decode( name, h264type, decode, release ) \
    h264type * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
    { \
        h264type *p_h264type = calloc(1, sizeof(h264type)); \
        if(likely(p_h264type)) \
        { \
            bs_t bs; \
619
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
620 621
            if( b_escaped ) \
            { \
622 623
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
624
            } \
625
            else bs_init( &bs, p_buf, i_buf ); \
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
            bs_skip( &bs, 8 ); /* Skip nal_unit_header */ \
            if( !decode( &bs, p_h264type ) ) \
            { \
                release( p_h264type ); \
                p_h264type = NULL; \
            } \
        } \
        return p_h264type; \
    }

IMPL_h264_generic_decode( h264_decode_sps, h264_sequence_parameter_set_t,
                          h264_parse_sequence_parameter_set_rbsp, h264_release_sps )

IMPL_h264_generic_decode( h264_decode_pps, h264_picture_parameter_set_t,
                          h264_parse_picture_parameter_set_rbsp, h264_release_pps )

642
block_t *h264_NAL_to_avcC( uint8_t i_nal_length_size,
643 644 645 646
                           const uint8_t **pp_sps_buf,
                           const size_t *p_sps_size, uint8_t i_sps_count,
                           const uint8_t **pp_pps_buf,
                           const size_t *p_pps_size, uint8_t i_pps_count )
647 648 649 650 651
{
    /* The length of the NAL size is encoded using 1, 2 or 4 bytes */
    if( i_nal_length_size != 1 && i_nal_length_size != 2
     && i_nal_length_size != 4 )
        return NULL;
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    if( i_sps_count == 0 || i_sps_count > H264_SPS_ID_MAX || i_pps_count == 0 )
        return NULL;

    /* Calculate the total size of all SPS and PPS NALs */
    size_t i_spspps_size = 0;
    for( size_t i = 0; i < i_sps_count; ++i )
    {
        assert( pp_sps_buf[i] && p_sps_size[i] );
        if( p_sps_size[i] < 4 || p_sps_size[i] > UINT16_MAX )
            return NULL;
        i_spspps_size += p_sps_size[i] +  2 /* 16be size place holder */;
    }
    for( size_t i = 0; i < i_pps_count; ++i )
    {
        assert( pp_pps_buf[i] && p_pps_size[i] );
        if( p_pps_size[i] > UINT16_MAX)
            return NULL;
        i_spspps_size += p_pps_size[i] +  2 /* 16be size place holder */;
    }
671

672
    bo_t bo;
673 674
    /* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
    if( bo_init( &bo, 7 + i_spspps_size ) != true )
675 676 677
        return NULL;

    bo_add_8( &bo, 1 ); /* configuration version */
678
    bo_add_mem( &bo, 3, &pp_sps_buf[0][1] ); /* i_profile/profile_compatibility/level */
679 680
    bo_add_8( &bo, 0xfc | (i_nal_length_size - 1) ); /* 0b11111100 | lengthsize - 1*/

681 682
    bo_add_8( &bo, 0xe0 | i_sps_count ); /* 0b11100000 | sps_count */
    for( size_t i = 0; i < i_sps_count; ++i )
683
    {
684 685
        bo_add_16be( &bo, p_sps_size[i] );
        bo_add_mem( &bo, p_sps_size[i], pp_sps_buf[i] );
686 687
    }

688 689
    bo_add_8( &bo, i_pps_count ); /* pps_count */
    for( size_t i = 0; i < i_pps_count; ++i )
690
    {
691 692
        bo_add_16be( &bo, p_pps_size[i] );
        bo_add_mem( &bo, p_pps_size[i], pp_pps_buf[i] );
693
    }
694

695 696 697
    return bo.b;
}

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 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
static const h264_level_limits_t * h264_get_level_limits( const h264_sequence_parameter_set_t *p_sps )
{
    uint16_t i_level_number = p_sps->i_level;
    if( i_level_number == H264_LEVEL_NUMBER_1_1 &&
       (p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3)) )
    {
        i_level_number = H264_LEVEL_NUMBER_1_B;
    }

    for( size_t i=0; i< ARRAY_SIZE(h264_levels_limits); i++ )
        if( h264_levels_limits[i].i_level == i_level_number )
            return & h264_levels_limits[i].limits;

    return NULL;
}

static uint8_t h264_get_max_dpb_frames( const h264_sequence_parameter_set_t *p_sps )
{
    const h264_level_limits_t *limits = h264_get_level_limits( p_sps );
    if( limits )
    {
        unsigned i_frame_height_in_mbs = ( p_sps->pic_height_in_map_units_minus1 + 1 ) *
                                         ( 2 - p_sps->frame_mbs_only_flag );
        unsigned i_den = ( p_sps->pic_width_in_mbs_minus1 + 1 ) * i_frame_height_in_mbs;
        uint8_t i_max_dpb_frames = limits->i_max_dpb_mbs / i_den;
        if( i_max_dpb_frames < 16 )
            return i_max_dpb_frames;
    }
    return 16;
}

bool h264_get_dpb_values( const h264_sequence_parameter_set_t *p_sps,
                          uint8_t *pi_depth, unsigned *pi_delay )
{
    uint8_t i_max_num_reorder_frames = p_sps->vui.i_max_num_reorder_frames;
    if( !p_sps->vui.b_bitstream_restriction_flag )
    {
        switch( p_sps->i_profile ) /* E-2.1 */
        {
            case PROFILE_H264_CAVLC_INTRA:
            case PROFILE_H264_SVC_HIGH:
            case PROFILE_H264_HIGH:
            case PROFILE_H264_HIGH_10:
            case PROFILE_H264_HIGH_422:
            case PROFILE_H264_HIGH_444_PREDICTIVE:
                if( p_sps->i_constraint_set_flags & H264_CONSTRAINT_SET_FLAG(3) )
                {
                    i_max_num_reorder_frames = 0; /* all IDR */
                    break;
                }
748
                /* fallthrough */
749 750 751 752 753 754 755 756 757 758 759 760
            default:
                i_max_num_reorder_frames = h264_get_max_dpb_frames( p_sps );
                break;
        }
    }

    *pi_depth = i_max_num_reorder_frames;
    *pi_delay = 0;

    return true;
}

761 762 763
bool h264_get_picture_size( const h264_sequence_parameter_set_t *p_sps, unsigned *p_w, unsigned *p_h,
                            unsigned *p_vw, unsigned *p_vh )
{
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
    unsigned CropUnitX = 1;
    unsigned CropUnitY = 2 - p_sps->frame_mbs_only_flag;
    if( p_sps->b_separate_colour_planes_flag != 1 )
    {
        if( p_sps->i_chroma_idc > 0 )
        {
            unsigned SubWidthC = 2;
            unsigned SubHeightC = 2;
            if( p_sps->i_chroma_idc > 1 )
            {
                SubHeightC = 1;
                if( p_sps->i_chroma_idc > 2 )
                    SubWidthC = 1;
            }
            CropUnitX *= SubWidthC;
            CropUnitY *= SubHeightC;
        }
    }

783 784 785 786
    *p_w = 16 * p_sps->pic_width_in_mbs_minus1 + 16;
    *p_h = 16 * p_sps->pic_height_in_map_units_minus1 + 16;
    *p_h *= ( 2 - p_sps->frame_mbs_only_flag );

787 788
    *p_vw = *p_w - ( p_sps->frame_crop.left_offset + p_sps->frame_crop.right_offset ) * CropUnitX;
    *p_vh = *p_h - ( p_sps->frame_crop.bottom_offset + p_sps->frame_crop.top_offset ) * CropUnitY;
789 790 791 792

    return true;
}

793 794 795 796 797 798 799 800
bool h264_get_chroma_luma( const h264_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format,
                           uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma )
{
    *pi_chroma_format = p_sps->i_chroma_idc;
    *pi_depth_luma = p_sps->i_bit_depth_luma;
    *pi_depth_chroma = p_sps->i_bit_depth_chroma;
    return true;
}
801 802 803 804
bool h264_get_colorimetry( const h264_sequence_parameter_set_t *p_sps,
                           video_color_primaries_t *p_primaries,
                           video_transfer_func_t *p_transfer,
                           video_color_space_t *p_colorspace,
805
                           video_color_range_t *p_full_range )
806 807 808 809
{
    if( !p_sps->vui.b_valid )
        return false;
    *p_primaries =
810
        iso_23001_8_cp_to_vlc_primaries( p_sps->vui.colour.i_colour_primaries );
811
    *p_transfer =
812
        iso_23001_8_tc_to_vlc_xfer( p_sps->vui.colour.i_transfer_characteristics );
813
    *p_colorspace =
814
        iso_23001_8_mc_to_vlc_coeffs( p_sps->vui.colour.i_matrix_coefficients );
815
    *p_full_range = p_sps->vui.colour.b_full_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
816 817 818
    return true;
}

819

820 821
bool h264_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
                            uint8_t *pi_level, uint8_t *pi_nal_length_size)
822 823
{
    uint8_t *p = (uint8_t*)p_fmt->p_extra;
824 825
    if(p_fmt->i_extra < 8)
        return false;
826 827

    /* Check the profile / level */
828
    if (p[0] == 1 && p_fmt->i_extra >= 12)
829
    {
830 831
        if (pi_nal_length_size)
            *pi_nal_length_size = 1 + (p[4]&0x03);
832 833
        p += 8;
    }
834
    else if(!p[0] && !p[1]) /* FIXME: WTH is setting AnnexB data here ? */
835
    {
836 837 838 839 840 841
        if (!p[2] && p[3] == 1)
            p += 4;
        else if (p[2] == 1)
            p += 3;
        else
            return false;
842
    }
843
    else return false;
844 845 846

    if ( ((*p++)&0x1f) != 7) return false;

847 848 849 850 851 852
    if (pi_profile)
        *pi_profile = p[0];

    if (pi_level)
        *pi_level = p[2];

853 854
    return true;
}