adts_stream_parser.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2014 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/formats/mpeg/adts_stream_parser.h"
  5. #include <stddef.h>
  6. #include "build/build_config.h"
  7. #include "media/base/media_log.h"
  8. #include "media/formats/mp4/aac.h"
  9. #include "media/formats/mpeg/adts_constants.h"
  10. namespace media {
  11. constexpr uint32_t kADTSStartCodeMask = 0xfff00000;
  12. ADTSStreamParser::ADTSStreamParser()
  13. : MPEGAudioStreamParserBase(kADTSStartCodeMask, AudioCodec::kAAC, 0) {}
  14. ADTSStreamParser::~ADTSStreamParser() = default;
  15. int ADTSStreamParser::ParseFrameHeader(const uint8_t* data,
  16. int size,
  17. int* frame_size,
  18. int* sample_rate,
  19. ChannelLayout* channel_layout,
  20. int* sample_count,
  21. bool* metadata_frame,
  22. std::vector<uint8_t>* extra_data) {
  23. DCHECK(data);
  24. DCHECK_GE(size, 0);
  25. if (size < kADTSHeaderMinSize)
  26. return 0;
  27. BitReader reader(data, size);
  28. int sync;
  29. int version;
  30. int layer;
  31. int protection_absent;
  32. int profile;
  33. size_t sample_rate_index;
  34. size_t channel_layout_index;
  35. int frame_length;
  36. size_t num_data_blocks;
  37. int unused;
  38. if (!reader.ReadBits(12, &sync) ||
  39. !reader.ReadBits(1, &version) ||
  40. !reader.ReadBits(2, &layer) ||
  41. !reader.ReadBits(1, &protection_absent) ||
  42. !reader.ReadBits(2, &profile) ||
  43. !reader.ReadBits(4, &sample_rate_index) ||
  44. !reader.ReadBits(1, &unused) ||
  45. !reader.ReadBits(3, &channel_layout_index) ||
  46. !reader.ReadBits(4, &unused) ||
  47. !reader.ReadBits(13, &frame_length) ||
  48. !reader.ReadBits(11, &unused) ||
  49. !reader.ReadBits(2, &num_data_blocks) ||
  50. (!protection_absent && !reader.ReadBits(16, &unused))) {
  51. return -1;
  52. }
  53. DVLOG(2) << "Header data :" << std::hex << " sync 0x" << sync << " version 0x"
  54. << version << " layer 0x" << layer << " profile 0x" << profile
  55. << " sample_rate_index 0x" << sample_rate_index
  56. << " channel_layout_index 0x" << channel_layout_index;
  57. const int bytes_read = reader.bits_read() / 8;
  58. if (sync != 0xfff || layer != 0 || frame_length < bytes_read ||
  59. sample_rate_index >= kADTSFrequencyTableSize ||
  60. channel_layout_index >= kADTSChannelLayoutTableSize) {
  61. if (media_log()) {
  62. LIMITED_MEDIA_LOG(DEBUG, media_log(), adts_parse_error_limit_, 5)
  63. << "Invalid header data :" << std::hex << " sync 0x" << sync
  64. << " version 0x" << version << " layer 0x" << layer
  65. << " sample_rate_index 0x" << sample_rate_index
  66. << " channel_layout_index 0x" << channel_layout_index;
  67. }
  68. return -1;
  69. }
  70. if (sample_rate)
  71. *sample_rate = kADTSFrequencyTable[sample_rate_index];
  72. if (frame_size)
  73. *frame_size = frame_length;
  74. if (sample_count)
  75. *sample_count = (num_data_blocks + 1) * kSamplesPerAACFrame;
  76. if (channel_layout)
  77. *channel_layout = kADTSChannelLayoutTable[channel_layout_index];
  78. if (metadata_frame)
  79. *metadata_frame = false;
  80. if (extra_data) {
  81. // See mp4::AAC::Parse() for details. We don't need to worry about writing
  82. // extensions since we can't have extended ADTS by this point (it's
  83. // explicitly rejected as invalid above).
  84. DCHECK_NE(sample_rate_index, 15u);
  85. // The following code is written according to ISO 14496 Part 3 Table 1.13 -
  86. // Syntax of AudioSpecificConfig.
  87. const uint16_t esds = (((((profile + 1) << 4) + sample_rate_index) << 4) +
  88. channel_layout_index)
  89. << 3;
  90. extra_data->push_back(esds >> 8);
  91. extra_data->push_back(esds & 0xFF);
  92. }
  93. return bytes_read;
  94. }
  95. } // namespace media