| Index: media/mp4/avc.cc
|
| diff --git a/media/mp4/avc.cc b/media/mp4/avc.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6af9d3513ccfd49faaca616af11ae1a3153f1e15
|
| --- /dev/null
|
| +++ b/media/mp4/avc.cc
|
| @@ -0,0 +1,120 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "media/mp4/avc.h"
|
| +
|
| +#include <algorithm>
|
| +
|
| +#include "media/mp4/box_reader.h"
|
| +
|
| +namespace media {
|
| +namespace mp4 {
|
| +
|
| +const static uint8 kAnnexBStartCode[] = {0, 0, 0, 1};
|
| +const static int kAnnexBStartCodeSize = 4;
|
| +
|
| +AVCDecoderConfigurationRecord::AVCDecoderConfigurationRecord() {};
|
| +AVCDecoderConfigurationRecord::~AVCDecoderConfigurationRecord() {};
|
| +
|
| +bool Parse(BoxReader* reader, AVCDecoderConfigurationRecord* avc_config) {
|
| + RCHECK(reader->Read(&avc_config->version) && avc_config->version == 1 &&
|
| + reader->Read(&avc_config->profile_indication) &&
|
| + reader->Read(&avc_config->profile_compatibility) &&
|
| + reader->Read(&avc_config->avc_level));
|
| +
|
| + uint8 length_size_minus_one;
|
| + RCHECK(reader->Read(&length_size_minus_one) &&
|
| + (length_size_minus_one & 0xfc) == 0xfc);
|
| + avc_config->length_size = (length_size_minus_one & 0x3) + 1;
|
| +
|
| + uint8 num_sps;
|
| + RCHECK(reader->Read(&num_sps) && (num_sps & 0xe0) == 0xe0);
|
| + num_sps &= 0x1f;
|
| +
|
| + avc_config->sps_list.resize(num_sps);
|
| + for (int i = 0; i < num_sps; i++) {
|
| + uint16 sps_length;
|
| + RCHECK(reader->Read(&sps_length) &&
|
| + reader->ReadVec(&avc_config->sps_list[i], sps_length));
|
| + }
|
| +
|
| + uint8 num_pps;
|
| + RCHECK(reader->Read(&num_pps));
|
| +
|
| + avc_config->pps_list.resize(num_pps);
|
| + for (int i = 0; i < num_pps; i++) {
|
| + uint16 pps_length;
|
| + RCHECK(reader->Read(&pps_length) &&
|
| + reader->ReadVec(&avc_config->pps_list[i], pps_length));
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +static bool ConvertAVCCToAnnexBInPlaceForLengthSize4(size_t buf_size,
|
| + uint8* buf) {
|
| + const size_t kLengthSize = 4;
|
| + size_t pos = 0;
|
| + while (pos + kLengthSize < buf_size) {
|
| + uint32 nal_size = buf[pos];
|
| + nal_size = (nal_size << 8) + buf[pos+1];
|
| + nal_size = (nal_size << 8) + buf[pos+2];
|
| + nal_size = (nal_size << 8) + buf[pos+3];
|
| + memcpy(&buf[pos], kAnnexBStartCode, kAnnexBStartCodeSize);
|
| + pos += kLengthSize + nal_size;
|
| + }
|
| + return pos == buf_size;
|
| +}
|
| +
|
| +bool ConvertAVCCToAnnexB(int length_size, std::vector<uint8>* buffer) {
|
| + RCHECK(length_size == 1 || length_size == 2 || length_size == 4);
|
| +
|
| + if (length_size == 4) {
|
| + return ConvertAVCCToAnnexBInPlaceForLengthSize4(buffer->size(),
|
| + &((*buffer)[0]));
|
| + } else {
|
| + std::vector<uint8_t> temp;
|
| + temp.swap(*buffer);
|
| + buffer->reserve(temp.size() + 32);
|
| +
|
| + size_t pos = 0;
|
| + while (pos + length_size < temp.size()) {
|
| + int nal_size = temp[pos];
|
| + if (length_size == 2) nal_size = (nal_size << 8) + temp[pos+1];
|
| + pos += length_size;
|
| +
|
| + RCHECK(pos + nal_size <= temp.size());
|
| + buffer->insert(buffer->end(), kAnnexBStartCode,
|
| + kAnnexBStartCode + kAnnexBStartCodeSize);
|
| + buffer->insert(buffer->end(), temp.begin() + pos,
|
| + temp.begin() + pos + nal_size);
|
| + pos += nal_size;
|
| + }
|
| + return pos == temp.size();
|
| + }
|
| +}
|
| +
|
| +ChannelLayout ConvertAACChannelCountToChannelLayout(int count) {
|
| + switch (count) {
|
| + case 1:
|
| + return CHANNEL_LAYOUT_MONO;
|
| + case 2:
|
| + return CHANNEL_LAYOUT_STEREO;
|
| + case 3:
|
| + return CHANNEL_LAYOUT_SURROUND;
|
| + case 4:
|
| + return CHANNEL_LAYOUT_4_0;
|
| + case 5:
|
| + return CHANNEL_LAYOUT_5_0;
|
| + case 6:
|
| + return CHANNEL_LAYOUT_5_1;
|
| + case 8:
|
| + return CHANNEL_LAYOUT_7_1;
|
| + default:
|
| + return CHANNEL_LAYOUT_UNSUPPORTED;
|
| + }
|
| +}
|
| +
|
| +}
|
| +}
|
|
|