h264_nal.c 24.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*****************************************************************************
 * 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.
 *****************************************************************************/

#include "h264_nal.h"
22
#include "hxxx_nal.h"
23

24
#include <vlc_bits.h>
25
#include <vlc_boxes.h>
26
#include <vlc_es.h>
27 28
#include <limits.h>

29 30 31 32 33
/*
 * For avcC specification, see ISO/IEC 14496-15,
 * For Annex B specification, see ISO/IEC 14496-10
 */

34 35
bool h264_isavcC( const uint8_t *p_buf, size_t i_buf )
{
36
    return ( i_buf >= H264_MIN_AVCC_SIZE &&
37 38 39 40 41 42
             p_buf[0] == 0x01 &&
            (p_buf[4] & 0xFC) == 0xFC &&
            (p_buf[4] & 0x03) != 0x02 &&
            (p_buf[5] & 0xE0) == 0xE0 );
}

43
static size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
44
{
45
    size_t i_total = 0;
46

47 48
    p_buf += 5;
    i_buf -= 5;
49

50 51
    if( i_buf < H264_MIN_AVCC_SIZE )
        return 0;
52 53 54 55

    for ( unsigned int j = 0; j < 2; j++ )
    {
        /* First time is SPS, Second is PPS */
56 57 58 59
        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++ )
60
        {
61 62 63 64
            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;
65
            p_buf += i_nal_size + 2;
66
            i_buf -= i_nal_size + 2;
67
        }
68 69 70
    }
    return i_total;
}
71

72 73 74 75 76 77
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;
78

79 80 81
    /* Read infos in first 6 bytes */
    if ( pi_nal_length_size )
        *pi_nal_length_size = (p_buf[4] & 0x03) + 1;
82

83 84 85
    uint8_t *p_ret;
    uint8_t *p_out_buf = p_ret = malloc( *pi_result );
    if( !p_out_buf )
86 87
    {
        *pi_result = 0;
88
        return NULL;
89
    }
90 91

    p_buf += 5;
92

93 94 95 96
    for ( unsigned int j = 0; j < 2; j++ )
    {
        const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
        p_buf++;
97

98 99 100 101 102 103 104
        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;
105

106 107
            memcpy( p_out_buf, p_buf, i_nal_size );
            p_out_buf += i_nal_size;
108 109 110 111
            p_buf += i_nal_size;
        }
    }

112
    return p_ret;
113 114
}

115
void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
116
                             uint8_t i_nal_length_size )
117
{
118 119
    uint32_t nal_len = 0;
    uint8_t nal_pos = 0;
120

121
    if( i_nal_length_size != 4 )
122 123
        return;

124 125
    /* This only works for a NAL length size of 4 */
    /* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
126 127
    while( i_len > 0 )
    {
128
        if( nal_pos < i_nal_length_size ) {
129
            unsigned int i;
130
            for( i = 0; nal_pos < i_nal_length_size && i < i_len; i++, nal_pos++ ) {
131
                nal_len = (nal_len << 8) | p_buf[i];
132 133
                p_buf[i] = 0;
            }
134
            if( nal_pos < i_nal_length_size )
135 136 137 138 139
                return;
            p_buf[i - 1] = 1;
            p_buf += i;
            i_len -= i;
        }
140
        if( nal_len > INT_MAX )
141
            return;
142
        if( nal_len > i_len )
143
        {
144
            nal_len -= i_len;
145 146 147 148
            return;
        }
        else
        {
149 150 151 152
            p_buf += nal_len;
            i_len -= nal_len;
            nal_len = 0;
            nal_pos = 0;
153 154 155 156
        }
    }
}

157 158 159 160 161 162 163 164 165 166 167 168
static block_t *h264_increase_startcode_size( block_t *p_block,
                                              size_t i_start_ofs )
{
    block_t *p_new;
    uint32_t i_buf = p_block->i_buffer - i_start_ofs;
    uint8_t *p_buf = p_block->p_buffer;
    uint8_t *p_new_buf;
    size_t i_ofs = i_start_ofs;
    size_t i_grow = 0;
    size_t i_new_ofs;

    /* Search all startcode of size 3 */
169 170 171
    unsigned i_bitflow = 0;
    unsigned i_nalcount = 0;
    while( i_buf-- )
172
    {
173 174
        i_bitflow <<= 1;
        if( *(p_buf++) != 0x01 )
175
        {
176
            i_bitflow |= 1;
177
        }
178
        else if( (i_bitflow & 0x06) == 0x06 ) /* two zero prefixed 1 */
179
        {
180 181 182
            i_nalcount++;
            if( !(i_bitflow & 0x08) ) /* max two zero prefixed 1 */
                i_grow++;
183 184 185 186 187 188
        }
   }

    if( i_grow == 0 )
        return p_block;

189 190


191 192 193 194 195
    /* Alloc a bigger buffer */
    p_new = block_Alloc( p_block->i_buffer + i_grow );
    if( !p_new )
        return NULL;
    i_buf = p_block->i_buffer - i_start_ofs;
196
    p_buf = p_block->p_buffer;
197 198 199 200 201 202 203 204 205 206
    p_new_buf = p_new->p_buffer;
    i_new_ofs = i_ofs = i_start_ofs;

    /* Copy the beginning of the buffer (same data) */
    if( i_start_ofs )
        memcpy( p_new_buf, p_buf, i_start_ofs );

    /* Copy the rest of the buffer and append a 0 before each 000001 */
    while( i_buf > 0 )
    {
207
        if( i_buf > 3 && memcmp( &p_buf[i_ofs], annexb_startcode3, 3 ) == 0 )
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        {
            if( i_ofs == 0 || p_buf[i_ofs - 1] != 0 )
                p_new_buf[i_new_ofs++] = 0;
            for( int i = 0; i < 3; ++i )
                p_new_buf[i_new_ofs++] = p_buf[i_ofs++];
            i_buf -= 3;
        } else
        {
            p_new_buf[i_new_ofs++] = p_buf[i_ofs++];
            i_buf--;
        }
   }

    block_Release( p_block );
    return p_new;
}

static int h264_replace_startcode( uint8_t *p_buf,
226
                                   uint8_t i_nal_length_size,
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                                   size_t i_startcode_ofs,
                                   size_t i_nal_size )
{
    if( i_nal_size < (unsigned) 1 << ( 8 * i_nal_length_size) )
    {
        /* NAL is too big to fit in i_nal_length_size */
        return -1;
    }

    p_buf[i_startcode_ofs++] = i_nal_size >> (--i_nal_length_size * 8);
    if( !i_nal_length_size )
        return 0;
    p_buf[i_startcode_ofs++] = i_nal_size >> (--i_nal_length_size * 8);
    if( !i_nal_length_size )
        return 0;
    p_buf[i_startcode_ofs++] = i_nal_size >> (--i_nal_length_size * 8);
    p_buf[i_startcode_ofs] = i_nal_size;
    return 0;
}

247
block_t *h264_AnnexB_to_AVC( block_t *p_block, uint8_t i_nal_length_size )
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
{
    size_t i_startcode_ofs = 0;
    size_t i_startcode_size = 0;
    uint32_t i_buf = p_block->i_buffer;
    uint8_t *p_buf = p_block->p_buffer;
    size_t i_ofs = 0;

    /* 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 )
        goto error;

    /* Replace the Annex B start code with the size of the NAL. */
    while( i_buf > 0 )
    {
263
        if( i_buf > 3 && memcmp( &p_buf[i_ofs], annexb_startcode3, 3 ) == 0 )
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        {
            if( i_startcode_size )
            {
                size_t i_nal_size = i_ofs - i_startcode_ofs - i_startcode_size;

                if( i_ofs > 0 && p_buf[i_ofs - 1] == 0 )
                    i_nal_size--;
                if( h264_replace_startcode( p_buf, i_nal_length_size,
                                            i_startcode_ofs,
                                            i_nal_size ) )
                    goto error;
            }
            if( i_ofs > 0 && p_buf[i_ofs - 1] == 0 )
            {
                /* startcode of size 3 */
                i_startcode_ofs = i_ofs - 1;
                i_startcode_size = 4;
            }
            else
            {
                i_startcode_ofs = i_ofs;
                i_startcode_size = 3;
            }

            if( i_startcode_size < i_nal_length_size )
            {
                /* i_nal_length_size can't fit in i_startcode_size. Therefore,
                 * reallocate a buffer in order to increase all startcode that
                 * are smaller than i_nal_length_size. This is not efficient but
                 * it's a corner case that won't happen often */
                p_block = h264_increase_startcode_size( p_block, i_startcode_ofs );
                if( !p_block )
                    return NULL;

                p_buf = p_block->p_buffer;
                i_startcode_size++;
            }
            i_buf -= 3;
            i_ofs += 3;
        }
        else
        {
            i_buf--;
            i_ofs++;
        }
    }

    if( i_startcode_size
     && h264_replace_startcode( p_buf, i_nal_length_size, i_startcode_ofs,
                                i_ofs - i_startcode_ofs - i_startcode_size) )
        return NULL;
    else
        return p_block;
error:
    block_Release( p_block );
    return NULL;
}

Thomas Guillem's avatar
Thomas Guillem committed
322 323 324 325 326 327
int h264_get_spspps( uint8_t *p_buf, size_t i_buf,
                     uint8_t **pp_sps, size_t *p_sps_size,
                     uint8_t **pp_pps, size_t *p_pps_size )
{
    uint8_t *p_sps = NULL, *p_pps = NULL;
    size_t i_sps_size = 0, i_pps_size = 0;
328
    int i_nal_type = H264_NAL_UNKNOWN;
329 330
    bool b_first_nal = true;
    bool b_has_zero_byte = false;
Thomas Guillem's avatar
Thomas Guillem committed
331

332
    while( i_buf > 0 )
Thomas Guillem's avatar
Thomas Guillem committed
333
    {
334
        unsigned int i_move = 1;
Thomas Guillem's avatar
Thomas Guillem committed
335

Thomas Guillem's avatar
Thomas Guillem committed
336
        /* cf B.1.1: a NAL unit starts and ends with 0x000001 or 0x00000001 */
337
        if( i_buf > 3 && !memcmp( p_buf, annexb_startcode3, 3 ) )
Thomas Guillem's avatar
Thomas Guillem committed
338
        {
339
            if( i_nal_type != H264_NAL_UNKNOWN )
340 341
            {
                /* update SPS/PPS size */
342
                if( i_nal_type == H264_NAL_SPS )
Thomas Guillem's avatar
Thomas Guillem committed
343
                    i_sps_size = p_buf - p_sps - (b_has_zero_byte ? 1 : 0);
344
                if( i_nal_type == H264_NAL_PPS )
Thomas Guillem's avatar
Thomas Guillem committed
345
                    i_pps_size = p_buf - p_pps - (b_has_zero_byte ? 1 : 0);
346 347 348 349

                if( i_sps_size && i_pps_size )
                    break;
            }
Thomas Guillem's avatar
Thomas Guillem committed
350 351 352 353 354 355 356

            if (i_buf < 4)
                return -1;
            i_nal_type = p_buf[3] & 0x1F;

            /* The start prefix is always 0x00000001 (annexb_startcode + a
             * leading zero byte) for SPS, PPS or the first NAL */
357 358
            if( !b_has_zero_byte && ( b_first_nal || i_nal_type == H264_NAL_SPS
             || i_nal_type == H264_NAL_PPS ) )
Thomas Guillem's avatar
Thomas Guillem committed
359 360 361 362 363
                return -1;
            b_first_nal = false;

            /* Pointer to the beginning of the SPS/PPS starting with the
             * leading zero byte */
364
            if( i_nal_type == H264_NAL_SPS && !p_sps )
Thomas Guillem's avatar
Thomas Guillem committed
365
                p_sps = p_buf - 1;
366
            if( i_nal_type == H264_NAL_PPS && !p_pps )
Thomas Guillem's avatar
Thomas Guillem committed
367 368 369 370 371 372 373
                p_pps = p_buf - 1;

            /* cf. 7.4.1.2.3 */
            if( i_nal_type > 18 || ( i_nal_type >= 10 && i_nal_type <= 12 ) )
                return -1;

            /* SPS/PPS are before the slices */
374
            if ( i_nal_type >= H264_NAL_SLICE && i_nal_type <= H264_NAL_SLICE_IDR )
Thomas Guillem's avatar
Thomas Guillem committed
375 376 377 378 379 380 381
                break;
            i_move = 4;
        }
        else if( b_first_nal && p_buf[0] != 0 )
        {
            /* leading_zero_8bits only before the first NAL */
            return -1;
382 383 384 385 386
        }
        b_has_zero_byte = *p_buf == 0;
        i_buf -= i_move;
        p_buf += i_move;
    }
Thomas Guillem's avatar
Thomas Guillem committed
387

388 389 390
    if( i_buf == 0 )
    {
        /* update SPS/PPS size if we reach the end of the bytestream */
391
        if( !i_sps_size && i_nal_type == H264_NAL_SPS )
Thomas Guillem's avatar
Thomas Guillem committed
392
            i_sps_size = p_buf - p_sps;
393
        if( !i_pps_size && i_nal_type == H264_NAL_PPS )
Thomas Guillem's avatar
Thomas Guillem committed
394 395 396 397 398 399 400 401 402 403 404 405
            i_pps_size = p_buf - p_pps;
    }
    if( ( !p_sps || !i_sps_size ) && ( !p_pps || !i_pps_size ) )
        return -1;
    *pp_sps = p_sps;
    *p_sps_size = i_sps_size;
    *pp_pps = p_pps;
    *p_pps_size = i_pps_size;

    return 0;
}

406
void h264_release_sps( h264_sequence_parameter_set_t *p_sps )
407
{
408 409
    free( p_sps );
}
410

411 412 413 414
static bool h264_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
                                                    h264_sequence_parameter_set_t *p_sps )
{
    int i_tmp;
415

416
    int i_profile_idc = bs_read( p_bs, 8 );
417
    p_sps->i_profile = i_profile_idc;
418 419
    p_sps->i_profile_compatibility = bs_read( p_bs, 8 );
    p_sps->i_level = bs_read( p_bs, 8 );
420
    /* sps id */
421
    p_sps->i_id = bs_read_ue( p_bs );
422
    if( p_sps->i_id >= H264_SPS_MAX )
423
        return false;
424

425 426 427
    if( i_profile_idc == PROFILE_H264_HIGH ||
        i_profile_idc == PROFILE_H264_HIGH_10 ||
        i_profile_idc == PROFILE_H264_HIGH_422 ||
428
        i_profile_idc == PROFILE_H264_HIGH_444 || /* Old one, no longer on spec */
429 430 431 432 433
        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 ||
434 435 436 437
        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 )
438 439
    {
        /* chroma_format_idc */
440
        const int i_chroma_format_idc = bs_read_ue( p_bs );
441
        if( i_chroma_format_idc == 3 )
442
            bs_skip( p_bs, 1 ); /* separate_colour_plane_flag */
443
        /* bit_depth_luma_minus8 */
444
        bs_read_ue( p_bs );
445
        /* bit_depth_chroma_minus8 */
446
        bs_read_ue( p_bs );
447
        /* qpprime_y_zero_transform_bypass_flag */
448
        bs_skip( p_bs, 1 );
449
        /* seq_scaling_matrix_present_flag */
450
        i_tmp = bs_read( p_bs, 1 );
451 452 453 454 455
        if( i_tmp )
        {
            for( int i = 0; i < ((3 != i_chroma_format_idc) ? 8 : 12); i++ )
            {
                /* seq_scaling_list_present_flag[i] */
456
                i_tmp = bs_read( p_bs, 1 );
457 458 459 460 461 462 463 464 465 466 467
                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 */
468
                        i_tmp = bs_read_se( p_bs );
469 470 471 472 473 474 475 476 477 478 479
                        i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
                        /* useDefaultScalingMatrixFlag = ... */
                    }
                    /* scalinglist[j] */
                    i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
                }
            }
        }
    }

    /* Skip i_log2_max_frame_num */
480
    p_sps->i_log2_max_frame_num = bs_read_ue( p_bs );
481 482 483
    if( p_sps->i_log2_max_frame_num > 12)
        p_sps->i_log2_max_frame_num = 12;
    /* Read poc_type */
484
    p_sps->i_pic_order_cnt_type = bs_read_ue( p_bs );
485 486 487
    if( p_sps->i_pic_order_cnt_type == 0 )
    {
        /* skip i_log2_max_poc_lsb */
488
        p_sps->i_log2_max_pic_order_cnt_lsb = bs_read_ue( p_bs );
489 490 491 492 493 494 495
        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 )
    {
        int i_cycle;
        /* skip b_delta_pic_order_always_zero */
496
        p_sps->i_delta_pic_order_always_zero_flag = bs_read( p_bs, 1 );
497
        /* skip i_offset_for_non_ref_pic */
498
        bs_read_se( p_bs );
499
        /* skip i_offset_for_top_to_bottom_field */
500
        bs_read_se( p_bs );
501
        /* read i_num_ref_frames_in_poc_cycle */
502
        i_cycle = bs_read_ue( p_bs );
503 504 505 506
        if( i_cycle > 256 ) i_cycle = 256;
        while( i_cycle > 0 )
        {
            /* skip i_offset_for_ref_frame */
507
            bs_read_se(p_bs );
508 509 510 511
            i_cycle--;
        }
    }
    /* i_num_ref_frames */
512
    bs_read_ue( p_bs );
513
    /* b_gaps_in_frame_num_value_allowed */
514
    bs_skip( p_bs, 1 );
515 516

    /* Read size */
517 518
    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 );
519 520

    /* b_frame_mbs_only */
521 522
    p_sps->frame_mbs_only_flag = bs_read( p_bs, 1 );
    if( !p_sps->frame_mbs_only_flag )
523
        bs_skip( p_bs, 1 );
524

525
    /* b_direct8x8_inference */
526
    bs_skip( p_bs, 1 );
527 528

    /* crop */
529
    if( bs_read1( p_bs ) ) /* frame_cropping_flag */
530
    {
531 532 533 534
        p_sps->frame_crop.left_offset = bs_read_ue( p_bs );
        p_sps->frame_crop.right_offset = bs_read_ue( p_bs );
        p_sps->frame_crop.right_offset = bs_read_ue( p_bs );
        p_sps->frame_crop.bottom_offset = bs_read_ue( p_bs );
535 536 537
    }

    /* vui */
538
    i_tmp = bs_read( p_bs, 1 );
539 540 541 542
    if( i_tmp )
    {
        p_sps->vui.b_valid = true;
        /* read the aspect ratio part if any */
543
        i_tmp = bs_read( p_bs, 1 );
544 545 546 547 548 549 550 551 552 553
        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 },
            };
554
            int i_sar = bs_read( p_bs, 8 );
555 556 557 558 559 560 561 562 563
            int w, h;

            if( i_sar < 17 )
            {
                w = sar[i_sar].w;
                h = sar[i_sar].h;
            }
            else if( i_sar == 255 )
            {
564 565
                w = bs_read( p_bs, 16 );
                h = bs_read( p_bs, 16 );
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
            }
            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 */
586
        i_tmp = bs_read( p_bs, 1 );
587
        if ( i_tmp )
588
            bs_read( p_bs, 1 );
589 590

        /* video signal type */
591
        i_tmp = bs_read( p_bs, 1 );
592 593
        if( i_tmp )
        {
594
            bs_read( p_bs, 4 );
595
            /* colour desc */
596
            bs_read( p_bs, 1 );
597
            if ( i_tmp )
598
                bs_read( p_bs, 24 );
599 600 601
        }

        /* chroma loc info */
602
        i_tmp = bs_read( p_bs, 1 );
603 604
        if( i_tmp )
        {
605 606
            bs_read_ue( p_bs );
            bs_read_ue( p_bs );
607 608 609
        }

        /* timing info */
610
        p_sps->vui.b_timing_info_present_flag = bs_read( p_bs, 1 );
611 612
        if( p_sps->vui.b_timing_info_present_flag )
        {
613 614 615
            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 );
616 617 618 619 620 621
        }

        /* Nal hrd & VC1 hrd parameters */
        p_sps->vui.b_cpb_dpb_delays_present_flag = false;
        for ( int i=0; i<2; i++ )
        {
622
            i_tmp = bs_read( p_bs, 1 );
623 624 625
            if( i_tmp )
            {
                p_sps->vui.b_cpb_dpb_delays_present_flag = true;
626 627 628
                uint32_t count = bs_read_ue( p_bs ) + 1;
                bs_read( p_bs, 4 );
                bs_read( p_bs, 4 );
629 630
                for( uint32_t i=0; i<count; i++ )
                {
631 632 633
                    bs_read_ue( p_bs );
                    bs_read_ue( p_bs );
                    bs_read( p_bs, 1 );
634
                }
635 636 637 638
                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 );
639 640 641 642
            }
        }

        if( p_sps->vui.b_cpb_dpb_delays_present_flag )
643
            bs_read( p_bs, 1 );
644 645

        /* pic struct info */
646
        p_sps->vui.b_pic_struct_present_flag = bs_read( p_bs, 1 );
647 648 649 650

        /* + unparsed remains */
    }

651
    return true;
652 653
}

654
void h264_release_pps( h264_picture_parameter_set_t *p_pps )
655
{
656 657
    free( p_pps );
}
658

659 660 661 662 663
static bool h264_parse_picture_parameter_set_rbsp( bs_t *p_bs,
                                                   h264_picture_parameter_set_t *p_pps )
{
    p_pps->i_id = bs_read_ue( p_bs ); // pps id
    p_pps->i_sps_id = bs_read_ue( p_bs ); // sps id
664
    if( p_pps->i_id >= H264_PPS_MAX || p_pps->i_sps_id >= H264_SPS_MAX )
665 666 667 668
        return false;

    bs_skip( p_bs, 1 ); // entropy coding mode flag
    p_pps->i_pic_order_present_flag = bs_read( p_bs, 1 );
669 670
    /* TODO */

671
    return true;
672 673
}

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
#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; \
            bs_init( &bs, p_buf, i_buf ); \
            unsigned i_bitflow = 0; \
            if( b_escaped ) \
            { \
                bs.p_fwpriv = &i_bitflow; \
                bs.pf_forward = hxxx_bsfw_ep3b_to_rbsp;  /* Does the emulated 3bytes conversion to rbsp */ \
            } \
            else (void) i_bitflow;\
            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 )

705
block_t *h264_AnnexB_NAL_to_avcC( uint8_t i_nal_length_size,
706 707 708 709 710
                                           const uint8_t *p_sps_buf,
                                           size_t i_sps_size,
                                           const uint8_t *p_pps_buf,
                                           size_t i_pps_size )
{
711 712 713
    if( i_pps_size > UINT16_MAX || i_sps_size > UINT16_MAX )
        return NULL;

714 715
    if( !hxxx_strip_AnnexB_startcode( &p_sps_buf, &i_sps_size ) ||
        !hxxx_strip_AnnexB_startcode( &p_pps_buf, &i_pps_size ) )
716
        return NULL;
717 718 719 720 721 722

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

723 724 725
    bo_t bo;
    /* 6 * int(8), i_sps_size, 1 * int(8), i_pps_size */
    if( bo_init( &bo, 7 + i_sps_size + i_pps_size ) != true )
726 727 728
        return NULL;

    bo_add_8( &bo, 1 ); /* configuration version */
729
    bo_add_mem( &bo, 3, &p_sps_buf[1] ); /* i_profile/profile_compatibility/level */
730 731 732
    bo_add_8( &bo, 0xfc | (i_nal_length_size - 1) ); /* 0b11111100 | lengthsize - 1*/

    bo_add_8( &bo, 0xe0 | (i_sps_size > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
733
    if( i_sps_size )
734
    {
735 736
        bo_add_16be( &bo, i_sps_size );
        bo_add_mem( &bo, i_sps_size, p_sps_buf );
737 738 739
    }

    bo_add_8( &bo, (i_pps_size > 0 ? 1 : 0) ); /* pps_count */
740
    if( i_pps_size )
741
    {
742 743
        bo_add_16be( &bo, i_pps_size );
        bo_add_mem( &bo, i_pps_size, p_pps_buf );
744
    }
745

746 747 748
    return bo.b;
}

749 750 751 752 753 754 755 756 757 758 759 760 761
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 )
{
    *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 );

    *p_vw = *p_w - p_sps->frame_crop.left_offset - p_sps->frame_crop.right_offset;
    *p_vh = *p_h - p_sps->frame_crop.bottom_offset - p_sps->frame_crop.top_offset;

    return true;
}

762
bool h264_get_profile_level(const es_format_t *p_fmt, size_t *p_profile,
763
                            size_t *p_level, uint8_t *pi_nal_length_size)
764 765 766 767 768 769 770 771
{
    uint8_t *p = (uint8_t*)p_fmt->p_extra;
    if(!p || !p_fmt->p_extra) return false;

    /* Check the profile / level */
    if (p_fmt->i_original_fourcc == VLC_FOURCC('a','v','c','1') && p[0] == 1)
    {
        if (p_fmt->i_extra < 12) return false;
772
        if (pi_nal_length_size) *pi_nal_length_size = 1 + (p[4]&0x03);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
        if (!(p[5]&0x1f)) return false;
        p += 8;
    }
    else
    {
        if (p_fmt->i_extra < 8) return false;
        if (!p[0] && !p[1] && !p[2] && p[3] == 1) p += 4;
        else if (!p[0] && !p[1] && p[2] == 1) p += 3;
        else return false;
    }

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

    /* Get profile/level out of first SPS */
    if (p_profile) *p_profile = p[0];
    if (p_level) *p_level = p[2];
    return true;
}