1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 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 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 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
|
/*****************************************************************************
* 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.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include "h264_nal.h"
#include "hxxx_nal.h"
#include <vlc_bits.h>
#include <vlc_boxes.h>
#include <vlc_es.h>
#include <limits.h>
/* 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 } },
};
/*
* For avcC specification, see ISO/IEC 14496-15,
* For Annex B specification, see ISO/IEC 14496-10
*/
bool h264_isavcC( const uint8_t *p_buf, size_t i_buf )
{
return ( i_buf >= H264_MIN_AVCC_SIZE &&
p_buf[0] != 0x00 &&
p_buf[1] != 0x00
/* /!\Broken quicktime streams does not respect reserved bits
(p_buf[4] & 0xFC) == 0xFC &&
(p_buf[4] & 0x03) != 0x02 &&
(p_buf[5] & 0x1F) > 0x00 */
);
}
static size_t get_avcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
{
size_t i_total = 0;
if( i_buf < H264_MIN_AVCC_SIZE )
return 0;
p_buf += 5;
i_buf -= 5;
for ( unsigned int j = 0; j < 2; j++ )
{
/* First time is SPS, Second is PPS */
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++ )
{
if( i_buf < 2 )
return 0;
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;
p_buf += i_nal_size + 2;
i_buf -= i_nal_size + 2;
}
if( j == 0 && i_buf < 1 )
return 0;
}
return i_total;
}
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;
/* Read infos in first 6 bytes */
if ( pi_nal_length_size )
*pi_nal_length_size = (p_buf[4] & 0x03) + 1;
uint8_t *p_ret;
uint8_t *p_out_buf = p_ret = malloc( *pi_result );
if( !p_out_buf )
{
*pi_result = 0;
return NULL;
}
p_buf += 5;
for ( unsigned int j = 0; j < 2; j++ )
{
const unsigned int i_loop_end = p_buf[0] & (j == 0 ? 0x1f : 0xff);
p_buf++;
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;
memcpy( p_out_buf, p_buf, i_nal_size );
p_out_buf += i_nal_size;
p_buf += i_nal_size;
}
}
return p_ret;
}
void h264_AVC_to_AnnexB( uint8_t *p_buf, uint32_t i_len,
uint8_t i_nal_length_size )
{
uint32_t nal_len = 0;
uint8_t nal_pos = 0;
if( i_nal_length_size != 4 )
return;
/* This only works for a NAL length size of 4 */
/* TODO: realloc/memmove if i_nal_length_size is 2 or 1 */
while( i_len > 0 )
{
if( nal_pos < i_nal_length_size ) {
unsigned int i;
for( i = 0; nal_pos < i_nal_length_size && i < i_len; i++, nal_pos++ ) {
nal_len = (nal_len << 8) | p_buf[i];
p_buf[i] = 0;
}
if( nal_pos < i_nal_length_size )
return;
p_buf[i - 1] = 1;
p_buf += i;
i_len -= i;
}
if( nal_len > INT_MAX )
return;
if( nal_len > i_len )
{
nal_len -= i_len;
return;
}
else
{
p_buf += nal_len;
i_len -= nal_len;
nal_len = 0;
nal_pos = 0;
}
}
}
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 )
{
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; }
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 ) )
{
if( i_nal < 2 )
continue;
const enum h264_nal_unit_type_e i_nal_type = p_nal[0] & 0x1F;
if ( i_nal_type <= H264_NAL_SLICE_IDR && i_nal_type != H264_NAL_UNKNOWN )
break;
#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
}
return (pp_sps && *p_sps_size) || (pp_pps && *p_pps_size);
}
void h264_release_sps( h264_sequence_parameter_set_t *p_sps )
{
free( p_sps );
}
#define H264_CONSTRAINT_SET_FLAG(N) (0x80 >> N)
static bool h264_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
h264_sequence_parameter_set_t *p_sps )
{
int i_tmp;
int i_profile_idc = bs_read( p_bs, 8 );
p_sps->i_profile = i_profile_idc;
p_sps->i_constraint_set_flags = bs_read( p_bs, 8 );
p_sps->i_level = bs_read( p_bs, 8 );
/* sps id */
uint32_t i_sps_id = bs_read_ue( p_bs );
if( i_sps_id > H264_SPS_ID_MAX )
return false;
p_sps->i_id = i_sps_id;
if( i_profile_idc == PROFILE_H264_HIGH ||
i_profile_idc == PROFILE_H264_HIGH_10 ||
i_profile_idc == PROFILE_H264_HIGH_422 ||
i_profile_idc == PROFILE_H264_HIGH_444 || /* Old one, no longer on spec */
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 ||
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 )
{
/* chroma_format_idc */
p_sps->i_chroma_idc = bs_read_ue( p_bs );
if( p_sps->i_chroma_idc == 3 )
p_sps->b_separate_colour_planes_flag = bs_read1( p_bs );
else
p_sps->b_separate_colour_planes_flag = 0;
/* bit_depth_luma_minus8 */
p_sps->i_bit_depth_luma = bs_read_ue( p_bs ) + 8;
/* bit_depth_chroma_minus8 */
p_sps->i_bit_depth_chroma = bs_read_ue( p_bs ) + 8;
/* qpprime_y_zero_transform_bypass_flag */
bs_skip( p_bs, 1 );
/* seq_scaling_matrix_present_flag */
i_tmp = bs_read( p_bs, 1 );
if( i_tmp )
{
for( int i = 0; i < ((3 != p_sps->i_chroma_idc) ? 8 : 12); i++ )
{
/* seq_scaling_list_present_flag[i] */
i_tmp = bs_read( p_bs, 1 );
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 */
i_tmp = bs_read_se( p_bs );
i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
/* useDefaultScalingMatrixFlag = ... */
}
/* scalinglist[j] */
i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
}
}
}
}
else
{
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;
}
/* Skip i_log2_max_frame_num */
p_sps->i_log2_max_frame_num = bs_read_ue( p_bs );
if( p_sps->i_log2_max_frame_num > 12)
p_sps->i_log2_max_frame_num = 12;
/* Read poc_type */
p_sps->i_pic_order_cnt_type = bs_read_ue( p_bs );
if( p_sps->i_pic_order_cnt_type == 0 )
{
/* skip i_log2_max_poc_lsb */
p_sps->i_log2_max_pic_order_cnt_lsb = bs_read_ue( p_bs );
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 )
{
p_sps->i_delta_pic_order_always_zero_flag = bs_read( p_bs, 1 );
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 );
}
/* i_num_ref_frames */
bs_read_ue( p_bs );
/* b_gaps_in_frame_num_value_allowed */
bs_skip( p_bs, 1 );
/* Read size */
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 );
/* b_frame_mbs_only */
p_sps->frame_mbs_only_flag = bs_read( p_bs, 1 );
if( !p_sps->frame_mbs_only_flag )
p_sps->mb_adaptive_frame_field_flag = bs_read( p_bs, 1 );
/* b_direct8x8_inference */
bs_skip( p_bs, 1 );
/* crop */
if( bs_read1( p_bs ) ) /* frame_cropping_flag */
{
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.top_offset = bs_read_ue( p_bs );
p_sps->frame_crop.bottom_offset = bs_read_ue( p_bs );
}
/* vui */
i_tmp = bs_read( p_bs, 1 );
if( i_tmp )
{
p_sps->vui.b_valid = true;
/* read the aspect ratio part if any */
i_tmp = bs_read( p_bs, 1 );
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 },
};
int i_sar = bs_read( p_bs, 8 );
int w, h;
if( i_sar < 17 )
{
w = sar[i_sar].w;
h = sar[i_sar].h;
}
else if( i_sar == 255 )
{
w = bs_read( p_bs, 16 );
h = bs_read( p_bs, 16 );
}
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 */
i_tmp = bs_read( p_bs, 1 );
if ( i_tmp )
bs_read( p_bs, 1 );
/* video signal type */
i_tmp = bs_read( p_bs, 1 );
if( i_tmp )
{
bs_read( p_bs, 3 );
p_sps->vui.colour.b_full_range = bs_read( p_bs, 1 );
/* colour desc */
i_tmp = bs_read( p_bs, 1 );
if ( i_tmp )
{
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
{
p_sps->vui.colour.i_colour_primaries = HXXX_PRIMARIES_UNSPECIFIED;
p_sps->vui.colour.i_transfer_characteristics = HXXX_TRANSFER_UNSPECIFIED;
p_sps->vui.colour.i_matrix_coefficients = HXXX_MATRIX_UNSPECIFIED;
}
}
/* chroma loc info */
i_tmp = bs_read( p_bs, 1 );
if( i_tmp )
{
bs_read_ue( p_bs );
bs_read_ue( p_bs );
}
/* timing info */
p_sps->vui.b_timing_info_present_flag = bs_read( p_bs, 1 );
if( p_sps->vui.b_timing_info_present_flag )
{
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 );
}
/* Nal hrd & VC1 hrd parameters */
p_sps->vui.b_hrd_parameters_present_flag = false;
for ( int i=0; i<2; i++ )
{
i_tmp = bs_read( p_bs, 1 );
if( i_tmp )
{
p_sps->vui.b_hrd_parameters_present_flag = true;
uint32_t count = bs_read_ue( p_bs ) + 1;
if( count > 31 )
return false;
bs_read( p_bs, 4 );
bs_read( p_bs, 4 );
for( uint32_t j = 0; j < count; j++ )
{
if( bs_remain( p_bs ) < 23 )
return false;
bs_read_ue( p_bs );
bs_read_ue( p_bs );
bs_read( p_bs, 1 );
}
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 );
}
}
if( p_sps->vui.b_hrd_parameters_present_flag )
bs_read( p_bs, 1 ); /* low delay hrd */
/* pic struct info */
p_sps->vui.b_pic_struct_present_flag = bs_read( p_bs, 1 );
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 */
}
}
return true;
}
void h264_release_pps( h264_picture_parameter_set_t *p_pps )
{
free( p_pps );
}
static bool h264_parse_picture_parameter_set_rbsp( bs_t *p_bs,
h264_picture_parameter_set_t *p_pps )
{
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 )
return false;
p_pps->i_id = i_pps_id;
p_pps->i_sps_id = i_sps_id;
bs_skip( p_bs, 1 ); // entropy coding mode flag
p_pps->i_pic_order_present_flag = bs_read( p_bs, 1 );
unsigned num_slice_groups = bs_read_ue( p_bs ) + 1;
if( num_slice_groups > 8 ) /* never has value > 7. Annex A, G & J */
return false;
if( num_slice_groups > 1 )
{
unsigned slice_group_map_type = bs_read_ue( p_bs );
if( slice_group_map_type == 0 )
{
for( unsigned i = 0; i < num_slice_groups; i++ )
bs_read_ue( p_bs ); /* run_length_minus1[group] */
}
else if( slice_group_map_type == 2 )
{
for( unsigned i = 0; i < num_slice_groups; i++ )
{
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 )
{
unsigned pic_size_in_maps_units = bs_read_ue( p_bs ) + 1;
unsigned sliceGroupSize = 1;
while(num_slice_groups > 1)
{
sliceGroupSize++;
num_slice_groups = ((num_slice_groups - 1) >> 1) + 1;
}
for( unsigned i = 0; i < pic_size_in_maps_units; i++ )
{
bs_skip( p_bs, sliceGroupSize );
}
}
}
p_pps->num_ref_idx_l01_default_active_minus1[0] = bs_read_ue( p_bs );
p_pps->num_ref_idx_l01_default_active_minus1[1] = bs_read_ue( p_bs );
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 );
/* TODO */
return true;
}
static bool h264_parse_sequence_parameter_set_extension_rbsp( bs_t *p_bs,
h264_sequence_parameter_set_extension_t *p_sps_ext )
{
p_sps_ext->i_sps_id = bs_read_ue( p_bs );
if( p_sps_ext->i_sps_id > H264_SPSEXT_ID_MAX )
return false;
return true;
}
void h264_release_sps_extension( h264_sequence_parameter_set_extension_t *p_sps_ext )
{
free( p_sps_ext );
}
#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 )
IMPL_h264_generic_decode( h264_decode_sps_extension, h264_sequence_parameter_set_extension_t,
h264_parse_sequence_parameter_set_extension_rbsp, h264_release_sps_extension )
block_t *h264_NAL_to_avcC( uint8_t i_nal_length_size,
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,
const uint8_t **pp_sps_ext_buf,
const size_t *p_sps_ext_size, uint8_t i_sps_ext_count )
{
/* 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;
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 */;
}
bo_t bo;
/* 1 + 3 + 1 + 1 + 1 + i_spspps_size */
if( bo_init( &bo, 7 + i_spspps_size ) != true )
return NULL;
bo_add_8( &bo, 1 ); /* configuration version */
bo_add_mem( &bo, 3, &pp_sps_buf[0][1] ); /* i_profile/profile_compatibility/level */
bo_add_8( &bo, 0xfc | (i_nal_length_size - 1) ); /* 0b11111100 | lengthsize - 1*/
bo_add_8( &bo, 0xe0 | i_sps_count ); /* 0b11100000 | sps_count */
for( size_t i = 0; i < i_sps_count; ++i )
{
bo_add_16be( &bo, p_sps_size[i] );
bo_add_mem( &bo, p_sps_size[i], pp_sps_buf[i] );
}
bo_add_8( &bo, i_pps_count ); /* pps_count */
for( size_t i = 0; i < i_pps_count; ++i )
{
bo_add_16be( &bo, p_pps_size[i] );
bo_add_mem( &bo, p_pps_size[i], pp_pps_buf[i] );
}
const uint8_t i_profile = pp_sps_buf[0][1];
if( i_profile == PROFILE_H264_HIGH ||
i_profile == PROFILE_H264_HIGH_10 ||
i_profile == PROFILE_H264_HIGH_422 ||
i_profile == PROFILE_H264_HIGH_444 )
{
h264_sequence_parameter_set_t *p_sps = h264_decode_sps( pp_sps_buf[0], p_sps_size[0], true );
bo_add_8( &bo, 0xfc | (p_sps ? p_sps->i_chroma_idc : 0) );
bo_add_8( &bo, 0xf8 | (p_sps ? (p_sps->i_bit_depth_luma - 8) : 0) );
bo_add_8( &bo, 0xf8 | (p_sps ? (p_sps->i_bit_depth_chroma - 8) : 0) );
if( p_sps )
h264_release_sps( p_sps );
bo_add_8( &bo, i_sps_ext_count );
for( size_t i = 0; i < i_sps_ext_count; ++i )
{
bo_add_16be( &bo, p_sps_ext_size[i] );
bo_add_mem( &bo, p_sps_ext_size[i], pp_sps_ext_buf[i] );
}
}
return bo.b;
}
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;
}
/* fallthrough */
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;
}
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 )
{
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;
}
}
*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 ) * CropUnitX;
*p_vh = *p_h - ( p_sps->frame_crop.bottom_offset + p_sps->frame_crop.top_offset ) * CropUnitY;
return true;
}
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;
}
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,
bool *p_full_range )
{
if( !p_sps->vui.b_valid )
return false;
*p_primaries =
hxxx_colour_primaries_to_vlc( p_sps->vui.colour.i_colour_primaries );
*p_transfer =
hxxx_transfer_characteristics_to_vlc( p_sps->vui.colour.i_transfer_characteristics );
*p_colorspace =
hxxx_matrix_coeffs_to_vlc( p_sps->vui.colour.i_matrix_coefficients );
*p_full_range = p_sps->vui.colour.b_full_range;
return true;
}
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)
{
uint8_t *p = (uint8_t*)p_fmt->p_extra;
if(p_fmt->i_extra < 8)
return false;
/* Check the profile / level */
if (p[0] == 1 && p_fmt->i_extra >= 12)
{
if (pi_nal_length_size)
*pi_nal_length_size = 1 + (p[4]&0x03);
p += 8;
}
else if(!p[0] && !p[1]) /* FIXME: WTH is setting AnnexB data here ? */
{
if (!p[2] && p[3] == 1)
p += 4;
else if (p[2] == 1)
p += 3;
else
return false;
}
else return false;
if ( ((*p++)&0x1f) != 7) return false;
if (pi_profile)
*pi_profile = p[0];
if (pi_level)
*pi_level = p[2];
return true;
}
|