You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "common_video/h264/pps_parser.h"
  11. #include <cstdint>
  12. #include <vector>
  13. #include "common_video/h264/h264_common.h"
  14. #include "rtc_base/bit_buffer.h"
  15. #include "rtc_base/checks.h"
  16. #define RETURN_EMPTY_ON_FAIL(x) \
  17. if (!(x)) { \
  18. return absl::nullopt; \
  19. }
  20. namespace {
  21. const int kMaxPicInitQpDeltaValue = 25;
  22. const int kMinPicInitQpDeltaValue = -26;
  23. } // namespace
  24. namespace webrtc {
  25. // General note: this is based off the 02/2014 version of the H.264 standard.
  26. // You can find it on this page:
  27. // http://www.itu.int/rec/T-REC-H.264
  28. absl::optional<PpsParser::PpsState> PpsParser::ParsePps(const uint8_t* data,
  29. size_t length) {
  30. // First, parse out rbsp, which is basically the source buffer minus emulation
  31. // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
  32. // section 7.3.1 of the H.264 standard.
  33. std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
  34. rtc::BitBuffer bit_buffer(unpacked_buffer.data(), unpacked_buffer.size());
  35. return ParseInternal(&bit_buffer);
  36. }
  37. bool PpsParser::ParsePpsIds(const uint8_t* data,
  38. size_t length,
  39. uint32_t* pps_id,
  40. uint32_t* sps_id) {
  41. RTC_DCHECK(pps_id);
  42. RTC_DCHECK(sps_id);
  43. // First, parse out rbsp, which is basically the source buffer minus emulation
  44. // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
  45. // section 7.3.1 of the H.264 standard.
  46. std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
  47. rtc::BitBuffer bit_buffer(unpacked_buffer.data(), unpacked_buffer.size());
  48. return ParsePpsIdsInternal(&bit_buffer, pps_id, sps_id);
  49. }
  50. absl::optional<uint32_t> PpsParser::ParsePpsIdFromSlice(const uint8_t* data,
  51. size_t length) {
  52. std::vector<uint8_t> unpacked_buffer = H264::ParseRbsp(data, length);
  53. rtc::BitBuffer slice_reader(unpacked_buffer.data(), unpacked_buffer.size());
  54. uint32_t golomb_tmp;
  55. // first_mb_in_slice: ue(v)
  56. if (!slice_reader.ReadExponentialGolomb(&golomb_tmp))
  57. return absl::nullopt;
  58. // slice_type: ue(v)
  59. if (!slice_reader.ReadExponentialGolomb(&golomb_tmp))
  60. return absl::nullopt;
  61. // pic_parameter_set_id: ue(v)
  62. uint32_t slice_pps_id;
  63. if (!slice_reader.ReadExponentialGolomb(&slice_pps_id))
  64. return absl::nullopt;
  65. return slice_pps_id;
  66. }
  67. absl::optional<PpsParser::PpsState> PpsParser::ParseInternal(
  68. rtc::BitBuffer* bit_buffer) {
  69. PpsState pps;
  70. RETURN_EMPTY_ON_FAIL(ParsePpsIdsInternal(bit_buffer, &pps.id, &pps.sps_id));
  71. uint32_t bits_tmp;
  72. uint32_t golomb_ignored;
  73. // entropy_coding_mode_flag: u(1)
  74. uint32_t entropy_coding_mode_flag;
  75. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&entropy_coding_mode_flag, 1));
  76. pps.entropy_coding_mode_flag = entropy_coding_mode_flag != 0;
  77. // bottom_field_pic_order_in_frame_present_flag: u(1)
  78. uint32_t bottom_field_pic_order_in_frame_present_flag;
  79. RETURN_EMPTY_ON_FAIL(
  80. bit_buffer->ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1));
  81. pps.bottom_field_pic_order_in_frame_present_flag =
  82. bottom_field_pic_order_in_frame_present_flag != 0;
  83. // num_slice_groups_minus1: ue(v)
  84. uint32_t num_slice_groups_minus1;
  85. RETURN_EMPTY_ON_FAIL(
  86. bit_buffer->ReadExponentialGolomb(&num_slice_groups_minus1));
  87. if (num_slice_groups_minus1 > 0) {
  88. uint32_t slice_group_map_type;
  89. // slice_group_map_type: ue(v)
  90. RETURN_EMPTY_ON_FAIL(
  91. bit_buffer->ReadExponentialGolomb(&slice_group_map_type));
  92. if (slice_group_map_type == 0) {
  93. for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
  94. ++i_group) {
  95. // run_length_minus1[iGroup]: ue(v)
  96. RETURN_EMPTY_ON_FAIL(
  97. bit_buffer->ReadExponentialGolomb(&golomb_ignored));
  98. }
  99. } else if (slice_group_map_type == 1) {
  100. // TODO(sprang): Implement support for dispersed slice group map type.
  101. // See 8.2.2.2 Specification for dispersed slice group map type.
  102. } else if (slice_group_map_type == 2) {
  103. for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1;
  104. ++i_group) {
  105. // top_left[iGroup]: ue(v)
  106. RETURN_EMPTY_ON_FAIL(
  107. bit_buffer->ReadExponentialGolomb(&golomb_ignored));
  108. // bottom_right[iGroup]: ue(v)
  109. RETURN_EMPTY_ON_FAIL(
  110. bit_buffer->ReadExponentialGolomb(&golomb_ignored));
  111. }
  112. } else if (slice_group_map_type == 3 || slice_group_map_type == 4 ||
  113. slice_group_map_type == 5) {
  114. // slice_group_change_direction_flag: u(1)
  115. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 1));
  116. // slice_group_change_rate_minus1: ue(v)
  117. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
  118. } else if (slice_group_map_type == 6) {
  119. // pic_size_in_map_units_minus1: ue(v)
  120. uint32_t pic_size_in_map_units_minus1;
  121. RETURN_EMPTY_ON_FAIL(
  122. bit_buffer->ReadExponentialGolomb(&pic_size_in_map_units_minus1));
  123. uint32_t slice_group_id_bits = 0;
  124. uint32_t num_slice_groups = num_slice_groups_minus1 + 1;
  125. // If num_slice_groups is not a power of two an additional bit is required
  126. // to account for the ceil() of log2() below.
  127. if ((num_slice_groups & (num_slice_groups - 1)) != 0)
  128. ++slice_group_id_bits;
  129. while (num_slice_groups > 0) {
  130. num_slice_groups >>= 1;
  131. ++slice_group_id_bits;
  132. }
  133. for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) {
  134. // slice_group_id[i]: u(v)
  135. // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits.
  136. RETURN_EMPTY_ON_FAIL(
  137. bit_buffer->ReadBits(&bits_tmp, slice_group_id_bits));
  138. }
  139. }
  140. }
  141. // num_ref_idx_l0_default_active_minus1: ue(v)
  142. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
  143. // num_ref_idx_l1_default_active_minus1: ue(v)
  144. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
  145. // weighted_pred_flag: u(1)
  146. uint32_t weighted_pred_flag;
  147. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&weighted_pred_flag, 1));
  148. pps.weighted_pred_flag = weighted_pred_flag != 0;
  149. // weighted_bipred_idc: u(2)
  150. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&pps.weighted_bipred_idc, 2));
  151. // pic_init_qp_minus26: se(v)
  152. RETURN_EMPTY_ON_FAIL(
  153. bit_buffer->ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26));
  154. // Sanity-check parsed value
  155. if (pps.pic_init_qp_minus26 > kMaxPicInitQpDeltaValue ||
  156. pps.pic_init_qp_minus26 < kMinPicInitQpDeltaValue) {
  157. RETURN_EMPTY_ON_FAIL(false);
  158. }
  159. // pic_init_qs_minus26: se(v)
  160. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
  161. // chroma_qp_index_offset: se(v)
  162. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
  163. // deblocking_filter_control_present_flag: u(1)
  164. // constrained_intra_pred_flag: u(1)
  165. RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2));
  166. // redundant_pic_cnt_present_flag: u(1)
  167. RETURN_EMPTY_ON_FAIL(
  168. bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1));
  169. return pps;
  170. }
  171. bool PpsParser::ParsePpsIdsInternal(rtc::BitBuffer* bit_buffer,
  172. uint32_t* pps_id,
  173. uint32_t* sps_id) {
  174. // pic_parameter_set_id: ue(v)
  175. if (!bit_buffer->ReadExponentialGolomb(pps_id))
  176. return false;
  177. // seq_parameter_set_id: ue(v)
  178. if (!bit_buffer->ReadExponentialGolomb(sps_id))
  179. return false;
  180. return true;
  181. }
  182. } // namespace webrtc