Ticket #9410: fix9410-bbc-hd3b.patch
File fix9410-bbc-hd3b.patch, 19.4 KB (added by , 10 years ago) |
---|
-
mythtv/libs/libmythtv/mpeg/H264Parser.cpp
diff --git a/mythtv/libs/libmythtv/mpeg/H264Parser.cpp b/mythtv/libs/libmythtv/mpeg/H264Parser.cpp index ac805e7..28bbad7 100644
a b 1 1 // MythTV headers 2 2 #include "H264Parser.h" 3 #include <iostream> 3 4 4 5 extern "C" { 5 6 // from libavcodec … … static const float eps = 1E-5; 92 93 93 94 H264Parser::H264Parser(void) 94 95 { 96 rbsp_buffer_size = 188 * 2; 97 rbsp_buffer = new uint8_t[rbsp_buffer_size]; 98 if (rbsp_buffer == 0) 99 rbsp_buffer_size = 0; 100 95 101 Reset(); 96 102 I_is_keyframe = true; 97 memset(&gb, 0, sizeof(gb));98 103 } 99 104 100 105 void H264Parser::Reset(void) … … void H264Parser::Reset(void) 144 149 145 150 AU_offset = frame_start_offset = keyframe_start_offset = 0; 146 151 on_frame = on_key_frame = false; 152 153 resetRBSP(); 147 154 } 148 155 149 156 … … bool H264Parser::new_AU(void) 183 190 one or more of the following ways. 184 191 185 192 - frame_num differs in value. The value of frame_num used to 186 test this condition is the value of frame_num that appears in187 the syntax of the slice header, regardless of whether that value188 is inferred to have been equal to 0 for subsequent use in the189 decoding process due to the presence of190 memory_management_control_operation equal to 5.193 test this condition is the value of frame_num that appears in 194 the syntax of the slice header, regardless of whether that value 195 is inferred to have been equal to 0 for subsequent use in the 196 decoding process due to the presence of 197 memory_management_control_operation equal to 5. 191 198 Note: If the current picture is an IDR picture FrameNum and 192 199 PrevRefFrameNum are set equal to 0. 193 200 - pic_parameter_set_id differs in value. 194 201 - field_pic_flag differs in value. 195 202 - bottom_field_flag is present in both and differs in value. 196 - nal_ref_idc differs in value with one of the nal_ref_idc values197 being equal to 0.203 - nal_ref_idc differs in value with one of the nal_ref_idc 204 values being equal to 0. 198 205 - pic_order_cnt_type is equal to 0 for both and either 199 pic_order_cnt_lsb differs in value, or delta_pic_order_cnt_bottom200 differs in value.206 pic_order_cnt_lsb differs in value, or delta_pic_order_cnt_bottom 207 differs in value. 201 208 - pic_order_cnt_type is equal to 1 for both and either 202 delta_pic_order_cnt[0] differs in value, or203 delta_pic_order_cnt[1] differs in value.209 delta_pic_order_cnt[0] differs in value, or 210 delta_pic_order_cnt[1] differs in value. 204 211 - nal_unit_type differs in value with one of the nal_unit_type values 205 being equal to 5.212 being equal to 5. 206 213 - nal_unit_type is equal to 5 for both and idr_pic_id differs in 207 value.214 value. 208 215 209 216 NOTE â Some of the VCL NAL units in redundant coded pictures or some 210 217 non-VCL NAL units (e.g. an access unit delimiter NAL unit) may also … … bool H264Parser::new_AU(void) 230 237 else if ((bottom_field_flag != -1 && prev_bottom_field_flag != -1) && 231 238 bottom_field_flag != prev_bottom_field_flag) 232 239 result = true; 240 else if ((nal_ref_idc == 0 || prev_nal_ref_idc == 0) && 241 nal_ref_idc != prev_nal_ref_idc) 242 result = true; 233 243 else if ((pic_order_cnt_type == 0 && prev_pic_order_cnt_type == 0) && 234 244 (pic_order_cnt_lsb != prev_pic_order_cnt_lsb || 235 245 delta_pic_order_cnt_bottom != … … bool H264Parser::new_AU(void) 253 263 prev_pic_parameter_set_id = pic_parameter_set_id; 254 264 prev_field_pic_flag = field_pic_flag; 255 265 prev_bottom_field_flag = bottom_field_flag; 266 prev_nal_ref_idc = nal_ref_idc; 256 267 prev_pic_order_cnt_lsb = pic_order_cnt_lsb; 257 268 prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom; 258 269 prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0]; … … bool H264Parser::new_AU(void) 263 274 return result; 264 275 } 265 276 277 void H264Parser::resetRBSP(void) 278 { 279 rbsp_index = 0; 280 consecutive_zeros = 0; 281 have_unfinished_NAL = false; 282 } 283 284 bool H264Parser::fillRBSP(const uint8_t *byteP, uint32_t byte_count, 285 bool found_start_code) 286 { 287 /* 288 bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE 289 bytes larger then the actual data 290 */ 291 uint32_t required_size = rbsp_index + byte_count + 292 FF_INPUT_BUFFER_PADDING_SIZE; 293 if (rbsp_buffer_size < required_size) 294 { 295 // Round up to packet size 296 required_size = ((required_size / 188) + 1) * 188; 297 298 /* Need a bigger buffer */ 299 uint8_t *new_buffer = new uint8_t[required_size]; 300 301 if (new_buffer == NULL) 302 { 303 /* Allocation failed. Discard the new bytes */ 304 std::cerr << "H264Parser::fillRBSP: " 305 << "FAILED to allocate RBSP buffer!\n"; 306 return false; 307 } 308 309 /* Copy across bytes from old buffer */ 310 memcpy(new_buffer, rbsp_buffer, rbsp_index); 311 delete [] rbsp_buffer; 312 rbsp_buffer = new_buffer; 313 rbsp_buffer_size = required_size; 314 } 315 316 /* Fill rbsp while we have data */ 317 while (byte_count) 318 { 319 /* Copy the byte into the rbsp, unless it 320 * is the 0x03 in a 0x000003 */ 321 if (consecutive_zeros < 2 || *byteP != 0x03) 322 rbsp_buffer[rbsp_index++] = *byteP; 323 324 if (*byteP == 0) 325 ++consecutive_zeros; 326 else 327 consecutive_zeros = 0; 328 329 ++byteP; 330 --byte_count; 331 } 332 333 /* If we've found the next start code then that, plus the first byte of 334 * the next NAL, plus the preceding zero bytes will all be in the rbsp 335 * buffer. Move rbsp_index++ back to the end of the actual rbsp data. We 336 * need to know the correct size of the rbsp to decode some NALs. */ 337 if (found_start_code) 338 { 339 if (rbsp_index >= 4) 340 { 341 rbsp_index -= 4; 342 while (rbsp_index > 0 && rbsp_buffer[rbsp_index-1] == 0) 343 --rbsp_index; 344 } 345 else 346 { 347 /* This should never happen. */ 348 std::cerr << "H264Parser::fillRBSP: " 349 << "Found start code, rbsp_index is " 350 << rbsp_index << " but it should be >4\n"; 351 } 352 } 353 354 /* Stick some 0xff on the end for get_bits to run into */ 355 memset(&rbsp_buffer[rbsp_index], 0xff, FF_INPUT_BUFFER_PADDING_SIZE); 356 return true; 357 } 358 266 359 uint32_t H264Parser::addBytes(const uint8_t *bytes, 267 360 const uint32_t byte_count, 268 361 const uint64_t stream_offset) 269 362 { 270 const uint8_t *byteP = bytes; 271 const uint8_t *endP = bytes + byte_count; 272 uint8_t first_byte; 363 const uint8_t *startP = bytes; 273 364 274 state_changed = is_keyframe = false; 365 state_changed = false; 366 on_frame = false; 367 on_key_frame = false; 275 368 276 while ( byteP < endP)369 while (startP < bytes + byte_count && !on_frame) 277 370 { 278 byteP = ff_find_start_code(byteP, endP, &sync_accumulator); 371 const uint8_t *endP; 372 bool found_start_code; 373 374 endP = ff_find_start_code(startP, 375 bytes + byte_count, &sync_accumulator); 376 377 found_start_code = ((sync_accumulator & 0xffffff00) == 0x00000100); 279 378 280 if ((sync_accumulator & 0xffffff00) == 0x00000100) 379 /* Between startP and endP we potentially have some more 380 * bytes of a NAL that we've been parsing (plus some bytes of 381 * start code) */ 382 if (have_unfinished_NAL) 281 383 { 384 if (!fillRBSP(startP, endP - startP, found_start_code)) 385 { 386 resetRBSP(); 387 return endP - bytes; 388 } 389 processRBSP(found_start_code); /* Call may set have_uinfinished_NAL 390 * to false */ 391 } 392 393 /* Dealt with everything up to endP */ 394 startP = endP; 395 396 if (found_start_code) 397 { 398 if (have_unfinished_NAL) 399 { 400 /* We've found a new start code, without completely 401 * parsing the previous NAL. Either there's a 402 * problem with the stream or with this parser. 403 */ 404 std::cerr << "H264Parser::addBytes: Found new start code, " 405 << "but previous NAL is incomplete!\n"; 406 } 407 408 /* Prepare for accepting the new NAL */ 409 resetRBSP(); 410 411 /* If we find the start of an AU somewhere from here 412 * to the next start code, the offset to associate with 413 * it is the one passed in to this call, not any of the 414 * subsequent calls. */ 415 pkt_offset = stream_offset; 282 416 /* 283 417 nal_unit_type specifies the type of RBSP data structure contained in 284 418 the NAL unit as specified in Table 7-1. VCL NAL units … … uint32_t H264Parser::addBytes(const uint8_t *bytes, 299 433 10 End of sequence end_of_seq_rbsp( ) 300 434 11 End of stream end_of_stream_rbsp( ) 301 435 */ 302 first_byte = *(byteP - 1); 303 nal_unit_type = first_byte & 0x1f; 304 nal_ref_idc = (first_byte >> 5) & 0x3; 436 nal_unit_type = sync_accumulator & 0x1f; 437 nal_ref_idc = (sync_accumulator >> 5) & 0x3; 305 438 306 439 if (nal_unit_type == SPS || nal_unit_type == PPS || 307 440 nal_unit_type == SEI || NALisSlice(nal_unit_type)) 308 441 { 309 /* 310 bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE 311 bytes larger then the actual read bits 312 */ 313 if (byteP + 1 + FF_INPUT_BUFFER_PADDING_SIZE < endP) 314 { 315 init_get_bits(&gb, byteP, 8 * (endP - byteP)); 316 317 if (nal_unit_type == SEI) 318 { 319 decode_SEI(&gb); 320 set_AU_pending(stream_offset); 321 } 322 else if (nal_unit_type == SPS) 323 { 324 decode_SPS(&gb); 325 set_AU_pending(stream_offset); 326 } 327 else if (nal_unit_type == PPS) 328 { 329 decode_PPS(&gb); 330 set_AU_pending(stream_offset); 331 } 332 else 333 { 334 decode_Header(&gb); 335 if (new_AU()) 336 set_AU_pending(stream_offset); 337 } 338 339 byteP += (get_bits_count(&gb) / 8); 340 } 442 /* This is a NAL we need to parse. We may have the body 443 * of it in the part of the stream past to us this call, 444 * or we may get the rest in subsequent calls to addBytes. 445 * Either way, we set have_unfinished_NAL, so that we 446 * start filling the rbsp buffer */ 447 have_unfinished_NAL = true; 341 448 } 342 else if (!AU_pending) 343 { 344 if (nal_unit_type == AU_DELIMITER || 449 else if (nal_unit_type == AU_DELIMITER || 345 450 (nal_unit_type > SPS_EXT && 346 451 nal_unit_type < AUXILIARY_SLICE)) 347 { 348 AU_pending = true; 349 AU_offset = stream_offset; 350 } 351 else if ((nal_ref_idc == 0 || prev_nal_ref_idc == 0) && 352 nal_ref_idc != prev_nal_ref_idc) 353 { 354 AU_pending = true; 355 AU_offset = stream_offset; 356 } 452 { 453 set_AU_pending(); 357 454 } 455 } 456 } 358 457 359 if (AU_pending && NALisSlice(nal_unit_type)) 360 { 361 /* Once we know the slice type of a new AU, we can 362 * determine if it is a keyframe or just a frame */ 458 return startP - bytes; 459 } 363 460 364 AU_pending = false;365 state_changed = true;366 461 367 on_frame = true; 368 frame_start_offset = AU_offset; 462 void H264Parser::processRBSP(bool rbsp_complete) 463 { 464 GetBitContext gb; 369 465 370 if (is_keyframe) 371 { 372 on_key_frame = true; 373 keyframe_start_offset = AU_offset; 374 } 375 else 376 on_key_frame = false; 377 } 378 else 379 on_frame = on_key_frame = false; 466 init_get_bits(&gb, rbsp_buffer, 8 * rbsp_index); 380 467 381 prev_nal_ref_idc = nal_ref_idc; 468 if (nal_unit_type == SEI) 469 { 470 /* SEI cannot be parsed without knowing its size. If 471 * we haven't got the whole rbsp, return and wait for 472 * the rest */ 473 if (!rbsp_complete) 474 return; 382 475 383 return byteP - bytes; 384 } 476 set_AU_pending(); 477 478 decode_SEI(&gb); 479 } 480 else if (nal_unit_type == SPS) 481 { 482 /* Best wait until we have the whole thing */ 483 if (!rbsp_complete) 484 return; 485 486 set_AU_pending(); 487 488 decode_SPS(&gb); 489 } 490 else if (nal_unit_type == PPS) 491 { 492 /* Best wait until we have the whole thing */ 493 if (!rbsp_complete) 494 return; 495 496 set_AU_pending(); 497 498 decode_PPS(&gb); 499 } 500 else 501 { 502 /* Need only parse the header. So return only 503 * if we have insufficient bytes */ 504 if (!rbsp_complete && rbsp_index < MAX_SLICE_HEADER_SIZE) 505 return; 506 507 decode_Header(&gb); 508 509 if (new_AU()) 510 set_AU_pending(); 385 511 } 386 512 387 return byteP - bytes; 513 /* If we got this far, we managed to parse a sufficient 514 * prefix of the current NAL. We can go onto the next. */ 515 have_unfinished_NAL = false; 516 517 if (AU_pending && NALisSlice(nal_unit_type)) 518 { 519 /* Once we know the slice type of a new AU, we can 520 * determine if it is a keyframe or just a frame */ 521 522 AU_pending = false; 523 state_changed = true; 524 525 on_frame = true; 526 frame_start_offset = AU_offset; 527 528 if (is_keyframe || au_contains_keyframe_message) 529 { 530 on_key_frame = true; 531 keyframe_start_offset = AU_offset; 532 } 533 } 388 534 } 389 535 390 536 /* … … bool H264Parser::decode_Header(GetBitContext *gb) 394 540 { 395 541 uint first_mb_in_slice; 396 542 543 is_keyframe = false; 544 397 545 if (log2_max_frame_num == 0 || pic_order_present_flag == -1) 398 546 { 399 / / SPS or PPS has not been parsed yet547 /* SPS or PPS has not been parsed yet */ 400 548 return false; 401 549 } 402 550 … … void H264Parser::decode_SPS(GetBitContext * gb) 600 748 { 601 749 for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx) 602 750 { 603 get_bits1(gb); // scaling_list 751 if (get_bits1(gb)) // Scaling list presnent 752 { 753 int sl_n = ((idx < 6) ? 16 : 64); 754 for(int sl_i = 0; sl_i < sl_n; sl_i++) 755 { 756 get_se_golomb(gb); 757 } 758 } 604 759 } 605 760 } 606 761 } … … void H264Parser::decode_SEI(GetBitContext *gb) 838 993 839 994 int type = 0, size = 0; 840 995 841 do { 842 type += show_bits(gb, 8); 843 } while (get_bits(gb, 8) == 255); 844 845 do { 846 size += show_bits(gb, 8); 847 } while (get_bits(gb, 8) == 255); 848 849 switch (type) 996 /* A message requires at least 2 bytes, and then 997 * there's the stop bit plus alignment, so there 998 * can be no message in less than 24 bits */ 999 while (get_bits_left(gb) >= 24) 850 1000 { 851 case SEI_TYPE_RECOVERY_POINT: 852 recovery_frame_cnt = get_ue_golomb(gb); 853 exact_match_flag = get_bits1(gb); 854 broken_link_flag = get_bits1(gb); 855 changing_group_slice_idc = get_bits(gb, 2); 856 is_keyframe |= (recovery_frame_cnt >= 0); 857 return; 1001 do { 1002 type += show_bits(gb, 8); 1003 } while (get_bits(gb, 8) == 255); 858 1004 859 default: 860 skip_bits(gb, size * 8); 861 break; 862 } 1005 do { 1006 size += show_bits(gb, 8); 1007 } while (get_bits(gb, 8) == 255); 863 1008 864 align_get_bits(gb); 1009 switch (type) 1010 { 1011 case SEI_TYPE_RECOVERY_POINT: 1012 recovery_frame_cnt = get_ue_golomb(gb); 1013 exact_match_flag = get_bits1(gb); 1014 broken_link_flag = get_bits1(gb); 1015 changing_group_slice_idc = get_bits(gb, 2); 1016 au_contains_keyframe_message = (recovery_frame_cnt >= 0); 1017 return; 1018 1019 default: 1020 skip_bits(gb, size * 8); 1021 break; 1022 } 1023 } 865 1024 } 866 1025 867 1026 void H264Parser::vui_parameters(GetBitContext * gb) … … void H264Parser::vui_parameters(GetBitContext * gb) 1015 1174 1016 1175 uint H264Parser::frameRate(void) const 1017 1176 { 1018 uint64_t 1177 uint64_t num; 1019 1178 uint64_t fps; 1020 1179 1021 1180 num = 500 * (uint64_t)timeScale; /* 1000 * 0.5 */ -
mythtv/libs/libmythtv/mpeg/H264Parser.h
diff --git a/mythtv/libs/libmythtv/mpeg/H264Parser.h b/mythtv/libs/libmythtv/mpeg/H264Parser.h index 2117a96..6efbb3b 100644
a b extern "C" { 48 48 class H264Parser { 49 49 public: 50 50 51 enum { 52 MAX_SLICE_HEADER_SIZE = 256 53 }; 54 51 55 // ITU-T Rec. H.264 table 7-1 52 56 enum NAL_unit_type { 53 57 UNKNOWN = 0, … … class H264Parser { 101 105 }; 102 106 103 107 H264Parser(void); 104 ~H264Parser(void) { ;}108 ~H264Parser(void) {delete [] rbsp_buffer;} 105 109 106 110 uint32_t addBytes(const uint8_t *bytes, 107 111 const uint32_t byte_count, … … class H264Parser { 158 162 private: 159 163 enum constants {EXTENDED_SAR = 255}; 160 164 161 inline void set_AU_pending( const uint64_t & stream_offset)165 inline void set_AU_pending(void) 162 166 { 163 167 if (!AU_pending) 164 168 { 165 169 AU_pending = true; 166 AU_offset = stream_offset; 170 AU_offset = pkt_offset; 171 au_contains_keyframe_message = false; 167 172 } 168 173 } 169 174 170 175 bool new_AU(void); 176 void resetRBSP(void); 177 bool fillRBSP(const uint8_t *byteP, uint32_t byte_count, 178 bool found_start_code); 179 void processRBSP(bool rbsp_complete); 171 180 bool decode_Header(GetBitContext *gb); 172 181 void decode_SPS(GetBitContext *gb); 173 182 void decode_PPS(GetBitContext * gb); … … class H264Parser { 177 186 bool AU_pending; 178 187 bool state_changed; 179 188 bool seen_sps; 189 bool au_contains_keyframe_message; 180 190 bool is_keyframe; 181 191 bool I_is_keyframe; 182 192 183 193 uint32_t sync_accumulator; 184 GetBitContext gb; 194 uint8_t *rbsp_buffer; 195 uint32_t rbsp_buffer_size; 196 uint32_t rbsp_index; 197 uint32_t consecutive_zeros; 198 bool have_unfinished_NAL; 185 199 186 200 int prev_frame_num, frame_num; 187 201 uint slice_type; … … class H264Parser { 218 232 uint32_t unitsInTick, timeScale; 219 233 bool fixedRate; 220 234 221 uint64_t AU_offset, frame_start_offset, keyframe_start_offset;235 uint64_t pkt_offset, AU_offset, frame_start_offset, keyframe_start_offset; 222 236 bool on_frame, on_key_frame; 223 237 }; 224 238