h264_to_annex_b_bitstream_converter.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/filters/h264_to_annex_b_bitstream_converter.h"
  5. #include <stddef.h>
  6. #include "base/logging.h"
  7. #include "media/formats/mp4/box_definitions.h"
  8. #include "media/video/h264_parser.h"
  9. namespace media {
  10. static const uint8_t kStartCodePrefix[3] = {0, 0, 1};
  11. static const uint32_t kParamSetStartCodeSize = 1 + sizeof(kStartCodePrefix);
  12. // Helper function which determines whether NAL unit of given type marks
  13. // access unit boundary.
  14. static bool IsAccessUnitBoundaryNal(int nal_unit_type) {
  15. // Check if this packet marks access unit boundary by checking the
  16. // packet type.
  17. if (nal_unit_type == 6 || // Supplemental enhancement information
  18. nal_unit_type == 7 || // Picture parameter set
  19. nal_unit_type == 8 || // Sequence parameter set
  20. nal_unit_type == 9 || // Access unit delimiter
  21. (nal_unit_type >= 14 && nal_unit_type <= 18)) { // Reserved types
  22. return true;
  23. }
  24. return false;
  25. }
  26. H264ToAnnexBBitstreamConverter::H264ToAnnexBBitstreamConverter()
  27. : configuration_processed_(false),
  28. first_nal_unit_in_access_unit_(true),
  29. nal_unit_length_field_width_(0) {
  30. }
  31. H264ToAnnexBBitstreamConverter::~H264ToAnnexBBitstreamConverter() = default;
  32. bool H264ToAnnexBBitstreamConverter::ParseConfiguration(
  33. const uint8_t* configuration_record,
  34. int configuration_record_size,
  35. mp4::AVCDecoderConfigurationRecord* avc_config) {
  36. DCHECK(configuration_record);
  37. DCHECK_GT(configuration_record_size, 0);
  38. DCHECK(avc_config);
  39. if (!avc_config->Parse(configuration_record, configuration_record_size))
  40. return false; // Error: invalid input
  41. // We're done processing the AVCDecoderConfigurationRecord,
  42. // store the needed information for parsing actual payload
  43. nal_unit_length_field_width_ = avc_config->length_size;
  44. configuration_processed_ = true;
  45. return true;
  46. }
  47. uint32_t H264ToAnnexBBitstreamConverter::GetConfigSize(
  48. const mp4::AVCDecoderConfigurationRecord& avc_config) const {
  49. uint32_t config_size = 0;
  50. for (size_t i = 0; i < avc_config.sps_list.size(); ++i)
  51. config_size += kParamSetStartCodeSize + avc_config.sps_list[i].size();
  52. for (size_t i = 0; i < avc_config.pps_list.size(); ++i)
  53. config_size += kParamSetStartCodeSize + avc_config.pps_list[i].size();
  54. return config_size;
  55. }
  56. uint32_t H264ToAnnexBBitstreamConverter::CalculateNeededOutputBufferSize(
  57. const uint8_t* input,
  58. uint32_t input_size,
  59. const mp4::AVCDecoderConfigurationRecord* avc_config) const {
  60. uint32_t output_size = 0;
  61. uint32_t data_left = input_size;
  62. bool first_nal_in_this_access_unit = first_nal_unit_in_access_unit_;
  63. if (input_size == 0)
  64. return 0; // Error: invalid input data
  65. if (!configuration_processed_) {
  66. return 0; // Error: configuration not handled, we don't know nal unit width
  67. }
  68. if (avc_config)
  69. output_size += GetConfigSize(*avc_config);
  70. CHECK(nal_unit_length_field_width_ == 1 ||
  71. nal_unit_length_field_width_ == 2 ||
  72. nal_unit_length_field_width_ == 4);
  73. // Then add the needed size for the actual packet
  74. while (data_left > 0) {
  75. if (data_left < nal_unit_length_field_width_) {
  76. return 0; // Error: not enough data for correct conversion.
  77. }
  78. // Read the next NAL unit length from the input buffer
  79. uint8_t size_of_len_field;
  80. uint32_t nal_unit_length;
  81. for (nal_unit_length = 0, size_of_len_field = nal_unit_length_field_width_;
  82. size_of_len_field > 0;
  83. input++, size_of_len_field--, data_left--) {
  84. nal_unit_length <<= 8;
  85. nal_unit_length |= *input;
  86. }
  87. if (nal_unit_length == 0) {
  88. break; // Signifies that no more data left in the buffer
  89. } else if (nal_unit_length > data_left) {
  90. return 0; // Error: Not enough data for correct conversion
  91. }
  92. data_left -= nal_unit_length;
  93. // five least significant bits of first NAL unit byte signify nal_unit_type
  94. int nal_unit_type = *input & 0x1F;
  95. if (first_nal_in_this_access_unit ||
  96. IsAccessUnitBoundaryNal(nal_unit_type)) {
  97. output_size += 1; // Extra zero_byte for these nal units
  98. first_nal_in_this_access_unit = false;
  99. }
  100. // Start code prefix
  101. output_size += sizeof(kStartCodePrefix);
  102. // Actual NAL unit size
  103. output_size += nal_unit_length;
  104. input += nal_unit_length;
  105. // No need for trailing zero bits
  106. }
  107. return output_size;
  108. }
  109. bool H264ToAnnexBBitstreamConverter::ConvertAVCDecoderConfigToByteStream(
  110. const mp4::AVCDecoderConfigurationRecord& avc_config,
  111. uint8_t* output,
  112. uint32_t* output_size) {
  113. uint8_t* out = output;
  114. uint32_t out_size = *output_size;
  115. *output_size = 0;
  116. for (size_t i = 0; i < avc_config.sps_list.size(); ++i) {
  117. if (!WriteParamSet(avc_config.sps_list[i], &out, &out_size))
  118. return false;
  119. }
  120. for (size_t i = 0; i < avc_config.pps_list.size(); ++i) {
  121. if (!WriteParamSet(avc_config.pps_list[i], &out, &out_size))
  122. return false;
  123. }
  124. nal_unit_length_field_width_ = avc_config.length_size;
  125. configuration_processed_ = true;
  126. *output_size = out - output;
  127. return true;
  128. }
  129. bool H264ToAnnexBBitstreamConverter::ConvertNalUnitStreamToByteStream(
  130. const uint8_t* input,
  131. uint32_t input_size,
  132. const mp4::AVCDecoderConfigurationRecord* avc_config,
  133. uint8_t* output,
  134. uint32_t* output_size) {
  135. const uint8_t* inscan = input; // We read the input from here progressively
  136. uint8_t* outscan = output; // We write the output to here progressively
  137. uint32_t data_left = input_size;
  138. if (input_size == 0 || *output_size == 0) {
  139. *output_size = 0;
  140. return false; // Error: invalid input
  141. }
  142. // NAL unit width should be known at this point
  143. CHECK(nal_unit_length_field_width_ == 1 ||
  144. nal_unit_length_field_width_ == 2 ||
  145. nal_unit_length_field_width_ == 4);
  146. // Do the actual conversion for the actual input packet
  147. int nal_unit_count = 0;
  148. while (data_left > 0) {
  149. uint8_t i;
  150. uint32_t nal_unit_length;
  151. // Read the next NAL unit length from the input buffer by scanning
  152. // the input stream with the specific length field width
  153. for (nal_unit_length = 0, i = nal_unit_length_field_width_;
  154. i > 0 && data_left > 0;
  155. inscan++, i--, data_left--) {
  156. nal_unit_length <<= 8;
  157. nal_unit_length |= *inscan;
  158. }
  159. if (nal_unit_length == 0) {
  160. break; // Successful conversion, end of buffer
  161. } else if (nal_unit_length > data_left) {
  162. *output_size = 0;
  163. return false; // Error: not enough data for correct conversion
  164. }
  165. // Five least significant bits of first NAL unit byte signify
  166. // nal_unit_type.
  167. int nal_unit_type = *inscan & 0x1F;
  168. nal_unit_count++;
  169. // Insert the config after the AUD if an AUD is the first NAL unit or
  170. // before all NAL units if the first one isn't an AUD.
  171. if (avc_config &&
  172. (nal_unit_type != H264NALU::kAUD || nal_unit_count > 1)) {
  173. uint32_t output_bytes_used = outscan - output;
  174. DCHECK_GE(*output_size, output_bytes_used);
  175. uint32_t config_size = *output_size - output_bytes_used;
  176. if (!ConvertAVCDecoderConfigToByteStream(*avc_config,
  177. outscan,
  178. &config_size)) {
  179. DVLOG(1) << "Failed to insert parameter sets.";
  180. *output_size = 0;
  181. return false; // Failed to convert the buffer.
  182. }
  183. outscan += config_size;
  184. avc_config = NULL;
  185. }
  186. uint32_t start_code_len;
  187. first_nal_unit_in_access_unit_ ?
  188. start_code_len = sizeof(kStartCodePrefix) + 1 :
  189. start_code_len = sizeof(kStartCodePrefix);
  190. if (static_cast<uint32_t>(outscan - output) + start_code_len +
  191. nal_unit_length >
  192. *output_size) {
  193. *output_size = 0;
  194. return false; // Error: too small output buffer
  195. }
  196. // Check if this packet marks access unit boundary by checking the
  197. // packet type.
  198. if (IsAccessUnitBoundaryNal(nal_unit_type)) {
  199. first_nal_unit_in_access_unit_ = true;
  200. }
  201. // Write extra zero-byte before start code prefix if this packet
  202. // signals next access unit.
  203. if (first_nal_unit_in_access_unit_) {
  204. *outscan = 0;
  205. outscan++;
  206. first_nal_unit_in_access_unit_ = false;
  207. }
  208. // No need to write leading zero bits.
  209. // Write start-code prefix.
  210. memcpy(outscan, kStartCodePrefix, sizeof(kStartCodePrefix));
  211. outscan += sizeof(kStartCodePrefix);
  212. // Then write the actual NAL unit from the input buffer.
  213. memcpy(outscan, inscan, nal_unit_length);
  214. inscan += nal_unit_length;
  215. data_left -= nal_unit_length;
  216. outscan += nal_unit_length;
  217. // No need for trailing zero bits.
  218. }
  219. // Successful conversion, output the freshly allocated bitstream buffer.
  220. *output_size = static_cast<uint32_t>(outscan - output);
  221. return true;
  222. }
  223. bool H264ToAnnexBBitstreamConverter::WriteParamSet(
  224. const std::vector<uint8_t>& param_set,
  225. uint8_t** out,
  226. uint32_t* out_size) const {
  227. // Strip trailing null bytes.
  228. size_t size = param_set.size();
  229. while (size && param_set[size - 1] == 0)
  230. size--;
  231. if (!size)
  232. return false;
  233. // Verify space.
  234. uint32_t bytes_left = *out_size;
  235. if (bytes_left < kParamSetStartCodeSize ||
  236. bytes_left - kParamSetStartCodeSize < size) {
  237. return false;
  238. }
  239. uint8_t* start = *out;
  240. uint8_t* buf = start;
  241. // Write the 4 byte Annex B start code.
  242. *buf++ = 0; // zero byte
  243. memcpy(buf, kStartCodePrefix, sizeof(kStartCodePrefix));
  244. buf += sizeof(kStartCodePrefix);
  245. // Copy the data.
  246. memcpy(buf, &param_set[0], size);
  247. buf += size;
  248. *out = buf;
  249. *out_size -= buf - start;
  250. return true;
  251. }
  252. } // namespace media