| 1 | // MythTV headers |
| 2 | #include "H264parser.h" |
| 3 | |
| 4 | extern "C" { |
| 5 | // from libavcodec |
| 6 | extern const uint8_t *ff_find_start_code(const uint8_t * p, const uint8_t *end, uint32_t * state); |
| 7 | #include "avcodec.h" |
| 8 | } |
| 9 | |
| 10 | // #include <math.h> |
| 11 | |
| 12 | /* |
| 13 | Most of the comments below were cut&paste from ITU-T Rec. H.264 |
| 14 | as found here: http://www.itu.int/rec/T-REC-H.264/e |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | Useful definitions: |
| 19 | |
| 20 | * access unit: A set of NAL units always containing exactly one |
| 21 | primary coded picture. In addition to the primary coded picture, an |
| 22 | access unit may also contain one or more redundant coded pictures |
| 23 | or other NAL units not containing slices or slice data partitions |
| 24 | of a coded picture. The decoding of an access unit always results |
| 25 | in a decoded picture. |
| 26 | |
| 27 | * instantaneous decoding refresh (IDR) access unit: An access unit in |
| 28 | which the primary coded picture is an IDR picture. |
| 29 | |
| 30 | * instantaneous decoding refresh (IDR) picture: A coded picture |
| 31 | containing only slices with I or SI slice types that causes the |
| 32 | decoding process to mark all reference pictures as "unused for |
| 33 | reference" immediately after decoding the IDR picture. After the |
| 34 | decoding of an IDR picture all following coded pictures in decoding |
| 35 | order can be decoded without inter prediction from any picture |
| 36 | decoded prior to the IDR picture. The first picture of each coded |
| 37 | video sequence is an IDR picture. |
| 38 | |
| 39 | * NAL unit: A syntax structure containing an indication of the type |
| 40 | of data to follow and bytes containing that data in the form of an |
| 41 | RBSP interspersed as necessary with emulation prevention bytes. |
| 42 | |
| 43 | * raw byte sequence payload (RBSP): A syntax structure containing an |
| 44 | integer number of bytes that is encapsulated in a NAL unit. An RBSP |
| 45 | is either empty or has the form of a string of data bits containing |
| 46 | syntax elements followed by an RBSP stop bit and followed by zero |
| 47 | or more subsequent bits equal to 0. |
| 48 | |
| 49 | * raw byte sequence payload (RBSP) stop bit: A bit equal to 1 present |
| 50 | within a raw byte sequence payload (RBSP) after a string of data |
| 51 | bits. The location of the end of the string of data bits within an |
| 52 | RBSP can be identified by searching from the end of the RBSP for |
| 53 | the RBSP stop bit, which is the last non-zero bit in the RBSP. |
| 54 | |
| 55 | * parity: The parity of a field can be top or bottom. |
| 56 | |
| 57 | * picture: A collective term for a field or a frame. |
| 58 | |
| 59 | * picture parameter set: A syntax structure containing syntax |
| 60 | elements that apply to zero or more entire coded pictures as |
| 61 | determined by the pic_parameter_set_id syntax element found in each |
| 62 | slice header. |
| 63 | |
| 64 | * primary coded picture: The coded representation of a picture to be |
| 65 | used by the decoding process for a bitstream conforming to this |
| 66 | Recommendation | International Standard. The primary coded picture |
| 67 | contains all macroblocks of the picture. The only pictures that |
| 68 | have a normative effect on the decoding process are primary coded |
| 69 | pictures. See also redundant coded picture. |
| 70 | |
| 71 | * VCL: Video Coding Layer |
| 72 | |
| 73 | - The VCL is specified to efficiently represent the content of the |
| 74 | video data. The NAL is specified to format that data and provide |
| 75 | header information in a manner appropriate for conveyance on a |
| 76 | variety of communication channels or storage media. All data are |
| 77 | contained in NAL units, each of which contains an integer number of |
| 78 | bytes. A NAL unit specifies a generic format for use in both |
| 79 | packet-oriented and bitstream systems. The format of NAL units for |
| 80 | both packet-oriented transport and byte stream is identical except |
| 81 | that each NAL unit can be preceded by a start code prefix and extra |
| 82 | padding bytes in the byte stream format. |
| 83 | |
| 84 | */ |
| 85 | |
| 86 | H264parser::H264parser(void) |
| 87 | { |
| 88 | Reset(); |
| 89 | } |
| 90 | |
| 91 | void H264parser::Reset(void) |
| 92 | { |
| 93 | state_changed = false; |
| 94 | seen_sps = seen_IDR = false; |
| 95 | |
| 96 | sync_accumulator = 0xffffffff; |
| 97 | find_AU = false; |
| 98 | AU_pending = false; |
| 99 | |
| 100 | NAL_type = UNKNOWN; |
| 101 | |
| 102 | frame_num = prev_frame_num = -1; |
| 103 | slice_type = SLICE_UNDEF; |
| 104 | prev_pic_parameter_set_id = pic_parameter_set_id = -1; |
| 105 | prev_field_pic_flag = field_pic_flag = -1; |
| 106 | prev_bottom_field_flag = bottom_field_flag = -1; |
| 107 | prev_nal_ref_idc = nal_ref_idc = 0; |
| 108 | prev_pic_order_cnt_type = pic_order_cnt_type = |
| 109 | prev_pic_order_cnt_lsb = pic_order_cnt_lsb = 0; |
| 110 | prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom = 0; |
| 111 | prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0] = 0; |
| 112 | prev_delta_pic_order_cnt[1] = delta_pic_order_cnt[1] = 0; |
| 113 | prev_nal_unit_type = nal_unit_type = 0; |
| 114 | prev_idr_pic_id = idr_pic_id = 0; |
| 115 | |
| 116 | log2_max_frame_num = log2_max_pic_order_cnt_lsb = 0; |
| 117 | seq_parameter_set_id = 0; |
| 118 | |
| 119 | delta_pic_order_always_zero_flag = 0; |
| 120 | separate_colour_plane_flag = 0; |
| 121 | frame_mbs_only_flag = -1; |
| 122 | pic_order_present_flag = -1; |
| 123 | redundant_pic_cnt_present_flag = 0; |
| 124 | |
| 125 | num_ref_frames = 0; |
| 126 | redundant_pic_cnt = 0; |
| 127 | |
| 128 | // pic_width_in_mbs = pic_height_in_map_units = 0; |
| 129 | pic_width = pic_height = 0; |
| 130 | frame_crop_left_offset = frame_crop_right_offset = 0; |
| 131 | frame_crop_top_offset = frame_crop_bottom_offset = 0; |
| 132 | sar_width = sar_height = 0; |
| 133 | |
| 134 | AU_offset = frame_start_offset = keyframe_start_offset = 0; |
| 135 | on_frame = on_key_frame = false; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | bool H264parser::new_AU(void) |
| 140 | { |
| 141 | /* |
| 142 | An access unit consists of one primary coded picture, zero or more |
| 143 | corresponding redundant coded pictures, and zero or more non-VCL NAL |
| 144 | units. The association of VCL NAL units to primary or redundant coded |
| 145 | pictures is described in subclause 7.4.1.2.5. |
| 146 | |
| 147 | The first access unit in the bitstream starts with the first NAL unit |
| 148 | of the bitstream. |
| 149 | |
| 150 | The first of any of the following NAL units after the last VCL NAL |
| 151 | unit of a primary coded picture specifies the start of a new access |
| 152 | unit. |
| 153 | |
| 154 | â access unit delimiter NAL unit (when present) |
| 155 | â sequence parameter set NAL unit (when present) |
| 156 | â picture parameter set NAL unit (when present) |
| 157 | â SEI NAL unit (when present) |
| 158 | â NAL units with nal_unit_type in the range of 14 to 18, inclusive |
| 159 | â first VCL NAL unit of a primary coded picture (always present) |
| 160 | */ |
| 161 | |
| 162 | /* |
| 163 | 7.4.1.2.4 Detection of the first VCL NAL unit of a primary coded |
| 164 | picture This subclause specifies constraints on VCL NAL unit syntax |
| 165 | that are sufficient to enable the detection of the first VCL NAL unit |
| 166 | of each primary coded picture. |
| 167 | |
| 168 | Any coded slice NAL unit or coded slice data partition A NAL unit of |
| 169 | the primary coded picture of the current access unit shall be |
| 170 | different from any coded slice NAL unit or coded slice data partition |
| 171 | A NAL unit of the primary coded picture of the previous access unit in |
| 172 | one or more of the following ways. |
| 173 | |
| 174 | - frame_num differs in value. The value of frame_num used to |
| 175 | test this condition is the value of frame_num that appears in |
| 176 | the syntax of the slice header, regardless of whether that value |
| 177 | is inferred to have been equal to 0 for subsequent use in the |
| 178 | decoding process due to the presence of |
| 179 | memory_management_control_operation equal to 5. |
| 180 | Note: If the current picture is an IDR picture FrameNum and |
| 181 | PrevRefFrameNum are set equal to 0. |
| 182 | - pic_parameter_set_id differs in value. |
| 183 | - field_pic_flag differs in value. |
| 184 | - bottom_field_flag is present in both and differs in value. |
| 185 | - nal_ref_idc differs in value with one of the nal_ref_idc values |
| 186 | being equal to 0. |
| 187 | - pic_order_cnt_type is equal to 0 for both and either |
| 188 | pic_order_cnt_lsb differs in value, or delta_pic_order_cnt_bottom |
| 189 | differs in value. |
| 190 | - pic_order_cnt_type is equal to 1 for both and either |
| 191 | delta_pic_order_cnt[0] differs in value, or |
| 192 | delta_pic_order_cnt[1] differs in value. |
| 193 | - nal_unit_type differs in value with one of the nal_unit_type values |
| 194 | being equal to 5. |
| 195 | - nal_unit_type is equal to 5 for both and idr_pic_id differs in |
| 196 | value. |
| 197 | |
| 198 | NOTE â Some of the VCL NAL units in redundant coded pictures or some |
| 199 | non-VCL NAL units (e.g. an access unit delimiter NAL unit) may also |
| 200 | be used for the detection of the boundary between access units, and |
| 201 | may therefore aid in the detection of the start of a new primary |
| 202 | coded picture. |
| 203 | |
| 204 | */ |
| 205 | |
| 206 | bool result = false; |
| 207 | |
| 208 | if (prev_frame_num != -1) |
| 209 | { |
| 210 | // Need previous slice information for comparison |
| 211 | |
| 212 | if (NAL_type == AU_DELIMITER || |
| 213 | NAL_type == SPS || |
| 214 | NAL_type == PPS || |
| 215 | NAL_type == SEI || |
| 216 | (NAL_type > SPS_EXT && NAL_type < AUXILIARY_SLICE)) |
| 217 | result = true; |
| 218 | else if (NAL_type != SLICE_IDR && frame_num != prev_frame_num) |
| 219 | result = true; |
| 220 | else if (prev_pic_parameter_set_id != -1 && |
| 221 | pic_parameter_set_id != prev_pic_parameter_set_id) |
| 222 | result = true; |
| 223 | else if (field_pic_flag != prev_field_pic_flag) |
| 224 | result = true; |
| 225 | else if ((bottom_field_flag != -1 && prev_bottom_field_flag != -1) && |
| 226 | bottom_field_flag != prev_bottom_field_flag) |
| 227 | result = true; |
| 228 | else if ((nal_ref_idc == 0 || prev_nal_ref_idc == 0) && |
| 229 | nal_ref_idc != prev_nal_ref_idc) |
| 230 | result = true; |
| 231 | else if ((pic_order_cnt_type == 0 && prev_pic_order_cnt_type == 0) && |
| 232 | (pic_order_cnt_lsb != prev_pic_order_cnt_lsb || |
| 233 | delta_pic_order_cnt_bottom != |
| 234 | prev_delta_pic_order_cnt_bottom)) |
| 235 | result = true; |
| 236 | else if ((pic_order_cnt_type == 1 && prev_pic_order_cnt_type == 1) && |
| 237 | (delta_pic_order_cnt[0] != prev_delta_pic_order_cnt[0] || |
| 238 | delta_pic_order_cnt[1] != prev_delta_pic_order_cnt[1])) |
| 239 | result = true; |
| 240 | else if ((nal_unit_type == SLICE_IDR || |
| 241 | prev_nal_unit_type == SLICE_IDR) && |
| 242 | nal_unit_type != prev_nal_unit_type) |
| 243 | result = true; |
| 244 | else if ((nal_unit_type == SLICE_IDR && |
| 245 | prev_nal_unit_type == SLICE_IDR) && |
| 246 | idr_pic_id != prev_idr_pic_id) |
| 247 | result = true; |
| 248 | } |
| 249 | |
| 250 | prev_frame_num = frame_num; |
| 251 | prev_pic_parameter_set_id = pic_parameter_set_id; |
| 252 | prev_field_pic_flag = field_pic_flag; |
| 253 | prev_bottom_field_flag = bottom_field_flag; |
| 254 | prev_nal_ref_idc = nal_ref_idc; |
| 255 | prev_pic_order_cnt_lsb = pic_order_cnt_lsb; |
| 256 | prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom; |
| 257 | prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0]; |
| 258 | prev_delta_pic_order_cnt[1] = delta_pic_order_cnt[1]; |
| 259 | prev_nal_unit_type = nal_unit_type; |
| 260 | prev_idr_pic_id = idr_pic_id; |
| 261 | |
| 262 | return result; |
| 263 | } |
| 264 | |
| 265 | uint32_t H264parser::addBytes(const uint8_t *bytes, |
| 266 | const uint32_t byte_count, |
| 267 | const uint64_t stream_offset) |
| 268 | { |
| 269 | const uint8_t *byteP = bytes; |
| 270 | const uint8_t *endP = bytes + byte_count; |
| 271 | |
| 272 | uint8_t first_byte; |
| 273 | |
| 274 | state_changed = false; |
| 275 | |
| 276 | while (byteP < endP) |
| 277 | { |
| 278 | byteP = ff_find_start_code(byteP, endP, &sync_accumulator); |
| 279 | |
| 280 | if ((sync_accumulator & 0xffffff00) == 0x00000100) |
| 281 | { |
| 282 | /* |
| 283 | nal_unit_type specifies the type of RBSP data structure contained in |
| 284 | the NAL unit as specified in Table 7-1. VCL NAL units |
| 285 | are specified as those NAL units having nal_unit_type |
| 286 | equal to 1 to 5, inclusive. All remaining NAL units |
| 287 | are called non-VCL NAL units: |
| 288 | |
| 289 | 0 Unspecified |
| 290 | 1 Coded slice of a non-IDR picture slice_layer_without_partitioning_rbsp( ) |
| 291 | 2 Coded slice data partition A slice_data_partition_a_layer_rbsp( ) |
| 292 | 3 Coded slice data partition B slice_data_partition_b_layer_rbsp( ) |
| 293 | 4 Coded slice data partition C slice_data_partition_c_layer_rbsp( ) |
| 294 | 5 Coded slice of an IDR picture slice_layer_without_partitioning_rbsp( ) |
| 295 | 6 Supplemental enhancement information (SEI) 5 sei_rbsp( ) |
| 296 | 7 Sequence parameter set (SPS) seq_parameter_set_rbsp( ) |
| 297 | 8 Picture parameter set pic_parameter_set_rbsp( ) |
| 298 | 9 Access unit delimiter access_unit_delimiter_rbsp( ) |
| 299 | 10 End of sequence end_of_seq_rbsp( ) |
| 300 | 11 End of stream end_of_stream_rbsp( ) |
| 301 | */ |
| 302 | first_byte = *(byteP - 1); |
| 303 | NAL_type = first_byte & 0x1f; |
| 304 | nal_ref_idc = (first_byte >> 5) & 0x3; |
| 305 | |
| 306 | if (NALisSlice(NAL_type) || NAL_type == SPS || NAL_type == PPS) |
| 307 | { |
| 308 | /* |
| 309 | bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE |
| 310 | bytes larger then the actual read bits |
| 311 | */ |
| 312 | if (byteP + 1 + FF_INPUT_BUFFER_PADDING_SIZE < endP) |
| 313 | { |
| 314 | init_get_bits(&gb, byteP, 8 * (endP - byteP)); |
| 315 | |
| 316 | if (NAL_type == SPS) |
| 317 | decode_SPS(&gb); |
| 318 | else if (NAL_type == PPS) |
| 319 | decode_PPS(&gb); |
| 320 | else |
| 321 | find_AU = decode_Header(&gb); |
| 322 | |
| 323 | byteP += (get_bits_count(&gb) / 8); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (find_AU && new_AU()) |
| 328 | { |
| 329 | /* After finding a new AU, don't look for another one |
| 330 | until we decode a SLICE */ |
| 331 | find_AU = false; |
| 332 | AU_pending = true; |
| 333 | AU_offset = stream_offset; |
| 334 | } |
| 335 | |
| 336 | if (AU_pending && NALisSlice(NAL_type)) |
| 337 | { |
| 338 | /* Once we know the slice type of a new AU, we can |
| 339 | * determine if it is a keyframe or just a frame */ |
| 340 | |
| 341 | AU_pending = false; |
| 342 | state_changed = true; |
| 343 | |
| 344 | on_frame = true; |
| 345 | frame_start_offset = AU_offset; |
| 346 | |
| 347 | if (seen_IDR && isKeySlice(slice_type)) |
| 348 | { |
| 349 | on_key_frame = true; |
| 350 | keyframe_start_offset = AU_offset; |
| 351 | } |
| 352 | else |
| 353 | on_key_frame = false; |
| 354 | } |
| 355 | else |
| 356 | on_frame = on_key_frame = false; |
| 357 | |
| 358 | return byteP - bytes; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | return byteP - bytes; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | 7.4.3 Slice header semantics |
| 367 | */ |
| 368 | bool H264parser::decode_Header(GetBitContext *gb) |
| 369 | { |
| 370 | uint first_mb_in_slice; |
| 371 | |
| 372 | if (log2_max_frame_num == 0 || pic_order_present_flag == -1) |
| 373 | { |
| 374 | // SPS or PPS has not been parsed yet |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | first_mb_in_slice specifies the address of the first macroblock |
| 380 | in the slice. When arbitrary slice order is not allowed as |
| 381 | specified in Annex A, the value of first_mb_in_slice is |
| 382 | constrained as follows. |
| 383 | |
| 384 | â If separate_colour_plane_flag is equal to 0, the value of |
| 385 | first_mb_in_slice shall not be less than the value of |
| 386 | first_mb_in_slice for any other slice of the current picture |
| 387 | that precedes the current slice in decoding order. |
| 388 | |
| 389 | â Otherwise (separate_colour_plane_flag is equal to 1), the value of |
| 390 | first_mb_in_slice shall not be less than the value of |
| 391 | first_mb_in_slice for any other slice of the current picture |
| 392 | that precedes the current slice in decoding order and has the |
| 393 | same value of colour_plane_id. |
| 394 | */ |
| 395 | first_mb_in_slice = get_ue_golomb(gb); |
| 396 | |
| 397 | /* |
| 398 | slice_type specifies the coding type of the slice according to |
| 399 | Table 7-6. e.g. P, B, I, SP, SI |
| 400 | |
| 401 | When nal_unit_type is equal to 5 (IDR picture), slice_type shall |
| 402 | be equal to 2, 4, 7, or 9 (I or SI) |
| 403 | */ |
| 404 | slice_type = get_ue_golomb(gb); |
| 405 | |
| 406 | /* |
| 407 | pic_parameter_set_id specifies the picture parameter set in |
| 408 | use. The value of pic_parameter_set_id shall be in the range of |
| 409 | 0 to 255, inclusive. |
| 410 | */ |
| 411 | pic_parameter_set_id = get_ue_golomb(gb); |
| 412 | |
| 413 | /* |
| 414 | separate_colour_plane_flag equal to 1 specifies that the three |
| 415 | colour components of the 4:4:4 chroma format are coded |
| 416 | separately. separate_colour_plane_flag equal to 0 specifies that |
| 417 | the colour components are not coded separately. When |
| 418 | separate_colour_plane_flag is not present, it shall be inferred |
| 419 | to be equal to 0. When separate_colour_plane_flag is equal to 1, |
| 420 | the primary coded picture consists of three separate components, |
| 421 | each of which consists of coded samples of one colour plane (Y, |
| 422 | Cb or Cr) that each use the monochrome coding syntax. In this |
| 423 | case, each colour plane is associated with a specific |
| 424 | colour_plane_id value. |
| 425 | */ |
| 426 | if (separate_colour_plane_flag) |
| 427 | get_bits(gb, 2); // colour_plane_id |
| 428 | |
| 429 | /* |
| 430 | frame_num is used as an identifier for pictures and shall be |
| 431 | represented by log2_max_frame_num_minus4 + 4 bits in the |
| 432 | bitstream.... |
| 433 | |
| 434 | If the current picture is an IDR picture, frame_num shall be equal to 0. |
| 435 | */ |
| 436 | |
| 437 | frame_num = get_bits(gb, log2_max_frame_num); |
| 438 | if (NAL_type == SLICE_IDR) |
| 439 | { |
| 440 | frame_num = 0; |
| 441 | seen_IDR = true; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | field_pic_flag equal to 1 specifies that the slice is a slice of a |
| 446 | coded field. field_pic_flag equal to 0 specifies that the slice is a |
| 447 | slice of a coded frame. When field_pic_flag is not present it shall be |
| 448 | inferred to be equal to 0. |
| 449 | |
| 450 | bottom_field_flag equal to 1 specifies that the slice is part of a |
| 451 | coded bottom field. bottom_field_flag equal to 0 specifies that the |
| 452 | picture is a coded top field. When this syntax element is not present |
| 453 | for the current slice, it shall be inferred to be equal to 0. |
| 454 | */ |
| 455 | |
| 456 | if (!frame_mbs_only_flag) |
| 457 | { |
| 458 | field_pic_flag = get_bits1(gb); |
| 459 | bottom_field_flag = field_pic_flag ? get_bits1(gb) : 0; |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | field_pic_flag = 0; |
| 464 | bottom_field_flag = -1; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | idr_pic_id identifies an IDR picture. The values of idr_pic_id |
| 469 | in all the slices of an IDR picture shall remain unchanged. When |
| 470 | two consecutive access units in decoding order are both IDR |
| 471 | access units, the value of idr_pic_id in the slices of the first |
| 472 | such IDR access unit shall differ from the idr_pic_id in the |
| 473 | second such IDR access unit. The value of idr_pic_id shall be in |
| 474 | the range of 0 to 65535, inclusive. |
| 475 | */ |
| 476 | if (nal_unit_type == SLICE_IDR) |
| 477 | idr_pic_id = get_ue_golomb(gb); |
| 478 | |
| 479 | /* |
| 480 | pic_order_cnt_lsb specifies the picture order count modulo |
| 481 | MaxPicOrderCntLsb for the top field of a coded frame or for a coded |
| 482 | field. The size of the pic_order_cnt_lsb syntax element is |
| 483 | log2_max_pic_order_cnt_lsb_minus4 + 4 bits. The value of the |
| 484 | pic_order_cnt_lsb shall be in the range of 0 to MaxPicOrderCntLsb â 1, |
| 485 | inclusive. |
| 486 | |
| 487 | delta_pic_order_cnt_bottom specifies the picture order count |
| 488 | difference between the bottom field and the top field of a coded |
| 489 | frame. |
| 490 | */ |
| 491 | if (pic_order_cnt_type == 0) |
| 492 | { |
| 493 | pic_order_cnt_lsb = get_bits(gb, log2_max_pic_order_cnt_lsb); |
| 494 | |
| 495 | if (pic_order_present_flag && !field_pic_flag) |
| 496 | delta_pic_order_cnt_bottom = get_se_golomb(gb); |
| 497 | else |
| 498 | delta_pic_order_cnt_bottom = 0; |
| 499 | } |
| 500 | else |
| 501 | delta_pic_order_cnt_bottom = 0; |
| 502 | |
| 503 | /* |
| 504 | delta_pic_order_cnt[ 0 ] specifies the picture order count |
| 505 | difference from the expected picture order count for the top |
| 506 | field of a coded frame or for a coded field as specified in |
| 507 | subclause 8.2.1. The value of delta_pic_order_cnt[ 0 ] shall be |
| 508 | in the range of -231 to 231 - 1, inclusive. When this syntax |
| 509 | element is not present in the bitstream for the current slice, |
| 510 | it shall be inferred to be equal to 0. |
| 511 | |
| 512 | delta_pic_order_cnt[ 1 ] specifies the picture order count |
| 513 | difference from the expected picture order count for the bottom |
| 514 | field of a coded frame specified in subclause 8.2.1. The value |
| 515 | of delta_pic_order_cnt[ 1 ] shall be in the range of -231 to 231 |
| 516 | - 1, inclusive. When this syntax element is not present in the |
| 517 | bitstream for the current slice, it shall be inferred to be |
| 518 | equal to 0. |
| 519 | */ |
| 520 | if (pic_order_cnt_type == 1 && !delta_pic_order_always_zero_flag) |
| 521 | { |
| 522 | delta_pic_order_cnt[0] = get_se_golomb(gb); |
| 523 | |
| 524 | if (pic_order_present_flag && !field_pic_flag) |
| 525 | delta_pic_order_cnt[1] = get_se_golomb(gb); |
| 526 | else |
| 527 | delta_pic_order_cnt[1] = 0; |
| 528 | } |
| 529 | else |
| 530 | delta_pic_order_cnt[0] = 0; |
| 531 | |
| 532 | /* |
| 533 | redundant_pic_cnt shall be equal to 0 for slices and slice data |
| 534 | partitions belonging to the primary coded picture. The |
| 535 | redundant_pic_cnt shall be greater than 0 for coded slices and |
| 536 | coded slice data partitions in redundant coded pictures. When |
| 537 | redundant_pic_cnt is not present, its value shall be inferred to |
| 538 | be equal to 0. The value of redundant_pic_cnt shall be in the |
| 539 | range of 0 to 127, inclusive. |
| 540 | */ |
| 541 | |
| 542 | redundant_pic_cnt = redundant_pic_cnt_present_flag ? get_ue_golomb(gb) : 0; |
| 543 | |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * libavcodec used for example |
| 549 | */ |
| 550 | void H264parser::decode_SPS(GetBitContext * gb) |
| 551 | { |
| 552 | int profile_idc, chroma_format_idc; |
| 553 | |
| 554 | seen_sps = true; |
| 555 | |
| 556 | profile_idc = get_bits(gb, 8); // profile_idc |
| 557 | get_bits1(gb); // constraint_set0_flag |
| 558 | get_bits1(gb); // constraint_set1_flag |
| 559 | get_bits1(gb); // constraint_set2_flag |
| 560 | get_bits1(gb); // constraint_set3_flag |
| 561 | get_bits(gb, 4); // reserved |
| 562 | get_bits(gb, 8); // level_idc |
| 563 | get_ue_golomb(gb); // sps_id |
| 564 | |
| 565 | if (profile_idc >= 100) |
| 566 | { // high profile |
| 567 | if ((chroma_format_idc = get_ue_golomb(gb)) == 3) // chroma_format_idc |
| 568 | separate_colour_plane_flag = (get_bits1(gb) == 1); |
| 569 | |
| 570 | get_ue_golomb(gb); // bit_depth_luma_minus8 |
| 571 | get_ue_golomb(gb); // bit_depth_chroma_minus8 |
| 572 | get_bits1(gb); // qpprime_y_zero_transform_bypass_flag |
| 573 | |
| 574 | if (get_bits1(gb)) // seq_scaling_matrix_present_flag |
| 575 | { |
| 576 | for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx) |
| 577 | { |
| 578 | get_bits1(gb); // scaling_list |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | log2_max_frame_num_minus4 specifies the value of the variable |
| 585 | MaxFrameNum that is used in frame_num related derivations as |
| 586 | follows: |
| 587 | |
| 588 | MaxFrameNum = 2( log2_max_frame_num_minus4 + 4 ) |
| 589 | */ |
| 590 | log2_max_frame_num = get_ue_golomb(gb) + 4; |
| 591 | |
| 592 | int offset_for_non_ref_pic; |
| 593 | int offset_for_top_to_bottom_field; |
| 594 | uint tmp; |
| 595 | bool gaps_in_frame_num_allowed_flag; |
| 596 | |
| 597 | /* |
| 598 | pic_order_cnt_type specifies the method to decode picture order |
| 599 | count (as specified in subclause 8.2.1). The value of |
| 600 | pic_order_cnt_type shall be in the range of 0 to 2, inclusive. |
| 601 | */ |
| 602 | pic_order_cnt_type = get_ue_golomb(gb); |
| 603 | if (pic_order_cnt_type == 0) |
| 604 | { |
| 605 | /* |
| 606 | log2_max_pic_order_cnt_lsb_minus4 specifies the value of the |
| 607 | variable MaxPicOrderCntLsb that is used in the decoding |
| 608 | process for picture order count as specified in subclause |
| 609 | 8.2.1 as follows: |
| 610 | |
| 611 | MaxPicOrderCntLsb = 2( log2_max_pic_order_cnt_lsb_minus4 + 4 ) |
| 612 | |
| 613 | The value of log2_max_pic_order_cnt_lsb_minus4 shall be in |
| 614 | the range of 0 to 12, inclusive. |
| 615 | */ |
| 616 | log2_max_pic_order_cnt_lsb = get_ue_golomb(gb) + 4; |
| 617 | } |
| 618 | else if (pic_order_cnt_type == 1) |
| 619 | { |
| 620 | /* |
| 621 | delta_pic_order_always_zero_flag equal to 1 specifies that |
| 622 | delta_pic_order_cnt[ 0 ] and delta_pic_order_cnt[ 1 ] are |
| 623 | not present in the slice headers of the sequence and shall |
| 624 | be inferred to be equal to |
| 625 | 0. delta_pic_order_always_zero_flag |
| 626 | */ |
| 627 | delta_pic_order_always_zero_flag = get_bits1(gb); |
| 628 | /* |
| 629 | offset_for_non_ref_pic is used to calculate the picture |
| 630 | order count of a non-reference picture as specified in |
| 631 | 8.2.1. The value of offset_for_non_ref_pic shall be in the |
| 632 | range of -231 to 231 - 1, inclusive. |
| 633 | */ |
| 634 | offset_for_non_ref_pic = get_se_golomb(gb); |
| 635 | /* |
| 636 | offset_for_top_to_bottom_field is used to calculate the |
| 637 | picture order count of a bottom field as specified in |
| 638 | subclause 8.2.1. The value of offset_for_top_to_bottom_field |
| 639 | shall be in the range of -231 to 231 - 1, inclusive. |
| 640 | */ |
| 641 | offset_for_top_to_bottom_field = get_se_golomb(gb); |
| 642 | /* |
| 643 | offset_for_ref_frame[ i ] is an element of a list of |
| 644 | num_ref_frames_in_pic_order_cnt_cycle values used in the |
| 645 | decoding process for picture order count as specified in |
| 646 | subclause 8.2.1. The value of offset_for_ref_frame[ i ] |
| 647 | shall be in the range of -231 to 231 - 1, inclusive. |
| 648 | */ |
| 649 | tmp = get_ue_golomb(gb); |
| 650 | for (uint idx = 0; idx < tmp; ++idx) |
| 651 | get_se_golomb(gb); // offset_for_ref_frame[i] |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | num_ref_frames specifies the maximum number of short-term and |
| 656 | long-term reference frames, complementary reference field pairs, |
| 657 | and non-paired reference fields that may be used by the decoding |
| 658 | process for inter prediction of any picture in the |
| 659 | sequence. num_ref_frames also determines the size of the sliding |
| 660 | window operation as specified in subclause 8.2.5.3. The value of |
| 661 | num_ref_frames shall be in the range of 0 to MaxDpbSize (as |
| 662 | specified in subclause A.3.1 or A.3.2), inclusive. |
| 663 | */ |
| 664 | num_ref_frames = get_ue_golomb(gb); |
| 665 | /* |
| 666 | gaps_in_frame_num_value_allowed_flag specifies the allowed |
| 667 | values of frame_num as specified in subclause 7.4.3 and the |
| 668 | decoding process in case of an inferred gap between values of |
| 669 | frame_num as specified in subclause 8.2.5.2. |
| 670 | */ |
| 671 | gaps_in_frame_num_allowed_flag = get_bits1(gb); |
| 672 | |
| 673 | /* |
| 674 | pic_width_in_mbs_minus1 plus 1 specifies the width of each |
| 675 | decoded picture in units of macroblocks. 16 macroblocks in a row |
| 676 | */ |
| 677 | pic_width = (get_ue_golomb(gb) + 1) * 16; |
| 678 | /* |
| 679 | pic_height_in_map_units_minus1 plus 1 specifies the height in |
| 680 | slice group map units of a decoded frame or field. 16 |
| 681 | macroblocks in each column. |
| 682 | */ |
| 683 | pic_height = (get_ue_golomb(gb) + 1) * 16; |
| 684 | |
| 685 | /* |
| 686 | frame_mbs_only_flag equal to 0 specifies that coded pictures of |
| 687 | the coded video sequence may either be coded fields or coded |
| 688 | frames. frame_mbs_only_flag equal to 1 specifies that every |
| 689 | coded picture of the coded video sequence is a coded frame |
| 690 | containing only frame macroblocks. |
| 691 | */ |
| 692 | frame_mbs_only_flag = get_bits1(gb); |
| 693 | if (!frame_mbs_only_flag) |
| 694 | { |
| 695 | pic_height *= 2; |
| 696 | /* |
| 697 | mb_adaptive_frame_field_flag equal to 0 specifies no |
| 698 | switching between frame and field macroblocks within a |
| 699 | picture. mb_adaptive_frame_field_flag equal to 1 specifies |
| 700 | the possible use of switching between frame and field |
| 701 | macroblocks within frames. When mb_adaptive_frame_field_flag |
| 702 | is not present, it shall be inferred to be equal to 0. |
| 703 | */ |
| 704 | get_bits1(gb); // mb_adaptive_frame_field_flag |
| 705 | } |
| 706 | |
| 707 | get_bits1(gb); // direct_8x8_inference_flag |
| 708 | |
| 709 | /* |
| 710 | frame_cropping_flag equal to 1 specifies that the frame cropping |
| 711 | offset parameters follow next in the sequence parameter |
| 712 | set. frame_cropping_flag equal to 0 specifies that the frame |
| 713 | cropping offset parameters are not present. |
| 714 | */ |
| 715 | if (get_bits1(gb)) // frame_cropping_flag |
| 716 | { |
| 717 | frame_crop_left_offset = get_ue_golomb(gb); |
| 718 | frame_crop_right_offset = get_ue_golomb(gb); |
| 719 | frame_crop_top_offset = get_ue_golomb(gb); |
| 720 | frame_crop_bottom_offset = get_ue_golomb(gb); |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | vui_parameters_present_flag equal to 1 specifies that the |
| 725 | vui_parameters( ) syntax structure as specified in Annex E is |
| 726 | present. vui_parameters_present_flag equal to 0 specifies that |
| 727 | the vui_parameters( ) syntax structure as specified in Annex E |
| 728 | is not present. |
| 729 | */ |
| 730 | if (get_bits1(gb)) // vui_parameters_present_flag |
| 731 | vui_parameters(gb); |
| 732 | } |
| 733 | |
| 734 | void H264parser::decode_PPS(GetBitContext * gb) |
| 735 | { |
| 736 | /* |
| 737 | pic_parameter_set_id identifies the picture parameter set that |
| 738 | is referred to in the slice header. The value of |
| 739 | pic_parameter_set_id shall be in the range of 0 to 255, |
| 740 | inclusive. |
| 741 | */ |
| 742 | pic_parameter_set_id = get_ue_golomb(gb); |
| 743 | /* |
| 744 | seq_parameter_set_id refers to the active sequence parameter |
| 745 | set. The value of seq_parameter_set_id shall be in the range of |
| 746 | 0 to 31, inclusive. |
| 747 | */ |
| 748 | seq_parameter_set_id = get_ue_golomb(gb); |
| 749 | get_bits1(gb); // entropy_coding_mode_flag; |
| 750 | /* |
| 751 | pic_order_present_flag equal to 1 specifies that the picture |
| 752 | order count related syntax elements are present in the slice |
| 753 | headers as specified in subclause 7.3.3. pic_order_present_flag |
| 754 | equal to 0 specifies that the picture order count related syntax |
| 755 | elements are not present in the slice headers. |
| 756 | */ |
| 757 | pic_order_present_flag = get_bits1(gb); |
| 758 | |
| 759 | #if 0 // Rest not currently needed, and requires <math.h> |
| 760 | uint num_slice_groups = get_ue_golomb(gb) + 1; |
| 761 | if (num_slice_groups > 1) // num_slice_groups (minus 1) |
| 762 | { |
| 763 | uint idx; |
| 764 | |
| 765 | switch (get_ue_golomb(gb)) // slice_group_map_type |
| 766 | { |
| 767 | case 0: |
| 768 | for (idx = 0; idx < num_slice_groups; ++idx) |
| 769 | get_ue_golomb(gb); // run_length_minus1[idx] |
| 770 | break; |
| 771 | case 1: |
| 772 | for (idx = 0; idx < num_slice_groups; ++idx) |
| 773 | { |
| 774 | get_ue_golomb(gb); // top_left[idx] |
| 775 | get_ue_golomb(gb); // bottom_right[idx] |
| 776 | } |
| 777 | break; |
| 778 | case 3: |
| 779 | case 4: |
| 780 | case 5: |
| 781 | get_bits1(gb); // slice_group_change_direction_flag |
| 782 | get_ue_golomb(gb); // slice_group_change_rate_minus1 |
| 783 | break; |
| 784 | case 6: |
| 785 | uint pic_size_in_map_units = get_ue_golomb(gb) + 1; |
| 786 | uint num_bits = (int)ceil(log2(num_slice_groups)); |
| 787 | for (idx = 0; idx < pic_size_in_map_units; ++idx) |
| 788 | { |
| 789 | get_bits(gb, num_bits); //slice_group_id[idx] |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | get_ue_golomb(gb); // num_ref_idx_10_active_minus1 |
| 795 | get_ue_golomb(gb); // num_ref_idx_11_active_minus1 |
| 796 | get_bits1(gb); // weighted_pred_flag; |
| 797 | get_bits(gb, 2); // weighted_bipred_idc |
| 798 | get_se_golomb(gb); // pic_init_qp_minus26 |
| 799 | get_se_golomb(gb); // pic_init_qs_minus26 |
| 800 | get_se_golomb(gb); // chroma_qp_index_offset |
| 801 | get_bits1(gb); // deblocking_filter_control_present_flag |
| 802 | get_bits1(gb); // constrained_intra_pref_flag |
| 803 | redundant_pic_cnt_present_flag = get_bits1(gb); |
| 804 | #endif |
| 805 | } |
| 806 | |
| 807 | void H264parser::vui_parameters(GetBitContext * gb) |
| 808 | { |
| 809 | /* |
| 810 | aspect_ratio_info_present_flag equal to 1 specifies that |
| 811 | aspect_ratio_idc is present. aspect_ratio_info_present_flag |
| 812 | equal to 0 specifies that aspect_ratio_idc is not present. |
| 813 | */ |
| 814 | if (get_bits1(gb)) //aspect_ratio_info_present_flag |
| 815 | { |
| 816 | /* |
| 817 | aspect_ratio_idc specifies the value of the sample aspect |
| 818 | ratio of the luma samples. Table E-1 shows the meaning of |
| 819 | the code. When aspect_ratio_idc indicates Extended_SAR, the |
| 820 | sample aspect ratio is represented by sar_width and |
| 821 | sar_height. When the aspect_ratio_idc syntax element is not |
| 822 | present, aspect_ratio_idc value shall be inferred to be |
| 823 | equal to 0. |
| 824 | */ |
| 825 | uint8_t aspect_ratio_idc = get_bits(gb, 8); |
| 826 | |
| 827 | switch (aspect_ratio_idc) |
| 828 | { |
| 829 | case 0: |
| 830 | // Unspecified |
| 831 | break; |
| 832 | case 1: |
| 833 | // 1:1 |
| 834 | /* |
| 835 | 1280x720 16:9 frame without overscan |
| 836 | 1920x1080 16:9 frame without overscan (cropped from 1920x1088) |
| 837 | 640x480 4:3 frame without overscan |
| 838 | */ |
| 839 | break; |
| 840 | case 2: |
| 841 | // 12:11 |
| 842 | /* |
| 843 | 720x576 4:3 frame with horizontal overscan |
| 844 | 352x288 4:3 frame without overscan |
| 845 | */ |
| 846 | break; |
| 847 | case 3: |
| 848 | // 10:11 |
| 849 | /* |
| 850 | 720x480 4:3 frame with horizontal overscan |
| 851 | 352x240 4:3 frame without overscan |
| 852 | */ |
| 853 | break; |
| 854 | case 4: |
| 855 | // 16:11 |
| 856 | /* |
| 857 | 720x576 16:9 frame with horizontal overscan |
| 858 | 540x576 4:3 frame with horizontal overscan |
| 859 | */ |
| 860 | break; |
| 861 | case 5: |
| 862 | // 40:33 |
| 863 | /* |
| 864 | 720x480 16:9 frame with horizontal overscan |
| 865 | 540x480 4:3 frame with horizontal overscan |
| 866 | */ |
| 867 | break; |
| 868 | case 6: |
| 869 | // 24:11 |
| 870 | /* |
| 871 | 352x576 4:3 frame without overscan |
| 872 | 540x576 16:9 frame with horizontal overscan |
| 873 | */ |
| 874 | break; |
| 875 | case 7: |
| 876 | // 20:11 |
| 877 | /* |
| 878 | 352x480 4:3 frame without overscan |
| 879 | 480x480 16:9 frame with horizontal overscan |
| 880 | */ |
| 881 | break; |
| 882 | case 8: |
| 883 | // 32:11 |
| 884 | /* |
| 885 | 352x576 16:9 frame without overscan |
| 886 | */ |
| 887 | break; |
| 888 | case 9: |
| 889 | // 80:33 |
| 890 | /* |
| 891 | 352x480 16:9 frame without overscan |
| 892 | */ |
| 893 | break; |
| 894 | case 10: |
| 895 | // 18:11 |
| 896 | /* |
| 897 | 480x576 4:3 frame with horizontal overscan |
| 898 | */ |
| 899 | break; |
| 900 | case 11: |
| 901 | // 15:11 |
| 902 | /* |
| 903 | 480x480 4:3 frame with horizontal overscan |
| 904 | */ |
| 905 | break; |
| 906 | case 12: |
| 907 | // 64:33 |
| 908 | /* |
| 909 | 540x576 16:9 frame with horizontal overscan |
| 910 | */ |
| 911 | break; |
| 912 | case 13: |
| 913 | // 160:99 |
| 914 | /* |
| 915 | 540x576 16:9 frame with horizontal overscan |
| 916 | */ |
| 917 | break; |
| 918 | case EXTENDED_SAR: |
| 919 | sar_width = get_bits(gb, 16); |
| 920 | sar_height = get_bits(gb, 16); |
| 921 | break; |
| 922 | } |
| 923 | } |
| 924 | else |
| 925 | sar_width = sar_height = 0; |
| 926 | } |