--- /media/FreeAgent GoFlex Drive/src/mythtv/mythtv/libs/libmythtv/mpeg/H264Parser.cpp 2013-03-06 18:57:09.000000000 +1300 +++ mythtv/mythtv/libs/libmythtv/mpeg/H264Parser.cpp 2013-03-30 17:33:37.808881892 +1300 @@ -1,10 +1,10 @@ // MythTV headers #include "H264Parser.h" #include #include "mythlogging.h" -#include "recorders/dtvrecorder.h" // for FrameRate +#include "dtvrecorder.h" // for FrameRate extern "C" { #include "libavcodec/avcodec.h" #include "libavcodec/mpegvideo.h" #include "libavutil/internal.h" @@ -96,36 +96,37 @@ rbsp_buffer = new uint8_t[rbsp_buffer_size]; if (rbsp_buffer == 0) rbsp_buffer_size = 0; Reset(); - I_is_keyframe = true; + I_is_keyframe = false; au_contains_keyframe_message = false; } void H264Parser::Reset(void) { state_changed = false; seen_sps = false; is_keyframe = false; + sync_accumulator = 0xffffffff; AU_pending = false; frame_num = prev_frame_num = -1; slice_type = SLICE_UNDEF; prev_pic_parameter_set_id = pic_parameter_set_id = -1; prev_field_pic_flag = field_pic_flag = -1; prev_bottom_field_flag = bottom_field_flag = -1; - prev_nal_ref_idc = nal_ref_idc = 0; + prev_nal_ref_idc = nal_ref_idc = 111; prev_pic_order_cnt_type = pic_order_cnt_type = prev_pic_order_cnt_lsb = pic_order_cnt_lsb = 0; prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom = 0; prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0] = 0; prev_delta_pic_order_cnt[1] = delta_pic_order_cnt[1] = 0; prev_nal_unit_type = nal_unit_type = UNKNOWN; - prev_idr_pic_id = idr_pic_id = 0; + prev_idr_pic_id = idr_pic_id = 111; log2_max_frame_num = log2_max_pic_order_cnt_lsb = 0; seq_parameter_set_id = 0; delta_pic_order_always_zero_flag = 0; @@ -399,10 +400,11 @@ const uint64_t stream_offset) { const uint8_t *startP = bytes; const uint8_t *endP; bool found_start_code; + bool good_nal_unit; state_changed = false; on_frame = false; on_key_frame = false; @@ -472,27 +474,43 @@ 11 End of stream end_of_stream_rbsp( ) */ nal_unit_type = sync_accumulator & 0x1f; nal_ref_idc = (sync_accumulator >> 5) & 0x3; - if (nal_unit_type == SPS || nal_unit_type == PPS || - nal_unit_type == SEI || NALisSlice(nal_unit_type)) - { - /* This is a NAL we need to parse. We may have the body - * of it in the part of the stream past to us this call, - * or we may get the rest in subsequent calls to addBytes. - * Either way, we set have_unfinished_NAL, so that we - * start filling the rbsp buffer */ - have_unfinished_NAL = true; - } - else if (nal_unit_type == AU_DELIMITER || - (nal_unit_type > SPS_EXT && - nal_unit_type < AUXILIARY_SLICE)) +// nal_ref_idc shall be equal to 0 for all NAL units having nal_unit_type equal to 6, 9, 10, 11, or 12. + good_nal_unit = true; + if (nal_ref_idc && (nal_unit_type == SEI || (nal_unit_type >= AU_DELIMITER && + nal_unit_type <= FILLER_DATA)) ) + good_nal_unit = false; + +// nal_ref_idc shall not be equal to 0 for NAL units with nal_unit_type equal to 5 + if (!nal_ref_idc && (nal_unit_type == SLICE_IDR)) + good_nal_unit = false; + + if (good_nal_unit) { - set_AU_pending(); + if (nal_unit_type == SPS || nal_unit_type == PPS || + nal_unit_type == SEI || NALisSlice(nal_unit_type)) + { + /* This is a NAL we need to parse. We may have the body + * of it in the part of the stream past to us this call, + * or we may get the rest in subsequent calls to addBytes. + * Either way, we set have_unfinished_NAL, so that we + * start filling the rbsp buffer */ + have_unfinished_NAL = true; + } + else if (nal_unit_type == AU_DELIMITER || + (nal_unit_type > SPS_EXT && + nal_unit_type < AUXILIARY_SLICE)) + { + set_AU_pending(); + } } - } + else + LOG(VB_GENERAL, LOG_ERR, + "H264Parser::addbytes: malformed NAL units"); + } //found start code } return startP - bytes; } @@ -512,19 +530,22 @@ return; set_AU_pending(); decode_SEI(&gb); - } + } else if (nal_unit_type == SPS) { /* Best wait until we have the whole thing */ if (!rbsp_complete) return; set_AU_pending(); + if (!seen_sps) + SPS_offset = pkt_offset; + decode_SPS(&gb); } else if (nal_unit_type == PPS) { /* Best wait until we have the whole thing */ @@ -574,10 +595,12 @@ /* 7.4.3 Slice header semantics */ bool H264Parser::decode_Header(GetBitContext *gb) { + uint first_mb_in_slice; + is_keyframe = false; if (log2_max_frame_num == 0 || pic_order_present_flag == -1) { /* SPS or PPS has not been parsed yet */ @@ -599,21 +622,21 @@ first_mb_in_slice shall not be less than the value of first_mb_in_slice for any other slice of the current picture that precedes the current slice in decoding order and has the same value of colour_plane_id. */ - //uint first_mb_in_slice = get_ue_golomb(gb); - get_ue_golomb(gb); // Replaced above line + first_mb_in_slice = get_ue_golomb_long(gb); /* slice_type specifies the coding type of the slice according to Table 7-6. e.g. P, B, I, SP, SI When nal_unit_type is equal to 5 (IDR picture), slice_type shall be equal to 2, 4, 7, or 9 (I or SI) */ - slice_type = get_ue_golomb(gb); + slice_type = get_ue_golomb_31(gb); +// s->pict_type = golomb_to_pict_type[slice_type % 5]; /* pic_parameter_set_id specifies the picture parameter set in use. The value of pic_parameter_set_id shall be in the range of 0 to 255, inclusive. @@ -640,10 +663,12 @@ frame_num is used as an identifier for pictures and shall be represented by log2_max_frame_num_minus4 + 4 bits in the bitstream.... If the current picture is an IDR picture, frame_num shall be equal to 0. + + When max_num_ref_frames is equal to 0, slice_type shall be equal to 2, 4, 7, or 9. */ frame_num = get_bits(gb, log2_max_frame_num); /* @@ -681,13 +706,14 @@ if (nal_unit_type == SLICE_IDR) { idr_pic_id = get_ue_golomb(gb); is_keyframe = true; + I_is_keyframe = true; } else - is_keyframe |= I_is_keyframe && isKeySlice(slice_type); + is_keyframe |= isKeySlice(slice_type); /* pic_order_cnt_lsb specifies the picture order count modulo MaxPicOrderCntLsb for the top field of a coded frame or for a coded field. The size of the pic_order_cnt_lsb syntax element is log2_max_pic_order_cnt_lsb_minus4 + 4 bits. The value of the @@ -700,11 +726,11 @@ */ if (pic_order_cnt_type == 0) { pic_order_cnt_lsb = get_bits(gb, log2_max_pic_order_cnt_lsb); - if (pic_order_present_flag && !field_pic_flag) + if ((pic_order_present_flag == 1) && !field_pic_flag) delta_pic_order_cnt_bottom = get_se_golomb(gb); else delta_pic_order_cnt_bottom = 0; } else @@ -729,17 +755,22 @@ */ if (pic_order_cnt_type == 1 && !delta_pic_order_always_zero_flag) { delta_pic_order_cnt[0] = get_se_golomb(gb); - if (pic_order_present_flag && !field_pic_flag) + if ((pic_order_present_flag == 1) && !field_pic_flag) delta_pic_order_cnt[1] = get_se_golomb(gb); - else + } + else + { + if (pic_order_cnt_type == 1) + { delta_pic_order_cnt[1] = 0; - } - else - delta_pic_order_cnt[0] = 0; + delta_pic_order_cnt[0] = 0; + } + } + /* redundant_pic_cnt shall be equal to 0 for slices and slice data partitions belonging to the primary coded picture. The redundant_pic_cnt shall be greater than 0 for coded slices and @@ -758,10 +789,13 @@ * libavcodec used for example */ void H264Parser::decode_SPS(GetBitContext * gb) { int profile_idc; + int lastScale; + int nextScale; + int deltaScale; seen_sps = true; profile_idc = get_bits(gb, 8); // profile_idc get_bits1(gb); // constraint_set0_flag @@ -770,29 +804,36 @@ get_bits1(gb); // constraint_set3_flag get_bits(gb, 4); // reserved get_bits(gb, 8); // level_idc get_ue_golomb(gb); // sps_id - if (profile_idc >= 100) + if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || + profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || + profile_idc == 86 || profile_idc == 118 || profile_idc == 128 ) { // high profile if ((chroma_format_idc = get_ue_golomb(gb)) == 3) // chroma_format_idc separate_colour_plane_flag = (get_bits1(gb) == 1); get_ue_golomb(gb); // bit_depth_luma_minus8 get_ue_golomb(gb); // bit_depth_chroma_minus8 get_bits1(gb); // qpprime_y_zero_transform_bypass_flag if (get_bits1(gb)) // seq_scaling_matrix_present_flag { - for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx) + for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); idx++) { - if (get_bits1(gb)) // Scaling list presnent + if (get_bits1(gb)) // Scaling list present { + lastScale = nextScale = 8; int sl_n = ((idx < 6) ? 16 : 64); for(int sl_i = 0; sl_i < sl_n; sl_i++) { - get_se_golomb(gb); + if (nextScale != 0) + { + deltaScale = get_se_golomb(gb); + nextScale = (lastScale + deltaScale + 256) % 256; + } } } } } } @@ -807,10 +848,11 @@ log2_max_frame_num = get_ue_golomb(gb) + 4; int offset_for_non_ref_pic; int offset_for_top_to_bottom_field; uint tmp; + bool gaps_in_frame_num_allowed_flag; /* pic_order_cnt_type specifies the method to decode picture order count (as specified in subclause 8.2.1). The value of pic_order_cnt_type shall be in the range of 0 to 2, inclusive. @@ -861,15 +903,13 @@ decoding process for picture order count as specified in subclause 8.2.1. The value of offset_for_ref_frame[ i ] shall be in the range of -231 to 231 - 1, inclusive. */ tmp = get_ue_golomb(gb); - for (uint idx = 0; idx < tmp; ++idx) + for (uint idx = 0; idx < tmp; idx++) get_se_golomb(gb); // offset_for_ref_frame[i] } - (void) offset_for_non_ref_pic; // suppress unused var warning - (void) offset_for_top_to_bottom_field; // suppress unused var warning /* num_ref_frames specifies the maximum number of short-term and long-term reference frames, complementary reference field pairs, and non-paired reference fields that may be used by the decoding @@ -884,12 +924,11 @@ gaps_in_frame_num_value_allowed_flag specifies the allowed values of frame_num as specified in subclause 7.4.3 and the decoding process in case of an inferred gap between values of frame_num as specified in subclause 8.2.5.2. */ - //bool gaps_in_frame_num_allowed_flag = get_bits1(gb); - get_bits1(gb); // Replaced above line + gaps_in_frame_num_allowed_flag = get_bits1(gb); /* pic_width_in_mbs_minus1 plus 1 specifies the width of each decoded picture in units of macroblocks. 16 macroblocks in a row */ @@ -1038,44 +1077,50 @@ int recovery_frame_cnt = -1; bool exact_match_flag = false; bool broken_link_flag = false; int changing_group_slice_idc = -1; - int type = 0, size = 0; + int type = 0, size = 0, tmp_byte = 0; /* A message requires at least 2 bytes, and then * there's the stop bit plus alignment, so there * can be no message in less than 24 bits */ while (get_bits_left(gb) >= 24) { - do { - type += show_bits(gb, 8); - } while (get_bits(gb, 8) == 255); - - do { - size += show_bits(gb, 8); - } while (get_bits(gb, 8) == 255); + tmp_byte = get_bits(gb, 8); + while (tmp_byte == 255) + { + type += 255; + tmp_byte = get_bits(gb, 8); + } + type += get_bits(gb, 8); //last_payload_type_byte + + tmp_byte = get_bits(gb, 8); + while (tmp_byte == 255) + { + size += 255; + tmp_byte = get_bits(gb, 8); + } + size += get_bits(gb, 8); //last_payload_size_byte switch (type) { case SEI_TYPE_RECOVERY_POINT: recovery_frame_cnt = get_ue_golomb(gb); exact_match_flag = get_bits1(gb); broken_link_flag = get_bits1(gb); changing_group_slice_idc = get_bits(gb, 2); au_contains_keyframe_message = (recovery_frame_cnt == 0); + if ((size - 12) > 0) + skip_bits(gb, (size - 12) * 8); return; default: skip_bits(gb, size * 8); break; } } - - (void) exact_match_flag; // suppress unused var warning - (void) broken_link_flag; // suppress unused var warning - (void) changing_group_slice_idc; // suppress unused var warning } void H264Parser::vui_parameters(GetBitContext * gb) { /* @@ -1250,11 +1295,11 @@ { double aspect = 0.0; if (pic_height) - aspect = pictureWidthCropped() / (double)pictureHeightCropped(); + aspect = pic_width / (double)pic_height; switch (aspect_ratio_idc) { case 0: // Unspecified @@ -1363,5 +1408,6 @@ if (ChromaArrayType != 0) CropUnitY *= SubHeightC; uint crop = CropUnitY * frame_crop_bottom_offset; return pic_height - crop; } +