content/media/fmp4/demuxer/avc.cc

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "mp4_demuxer/avc.h"
michael@0 6
michael@0 7 #include <algorithm>
michael@0 8 #include <vector>
michael@0 9
michael@0 10 #include "mp4_demuxer/box_definitions.h"
michael@0 11 #include "mp4_demuxer/box_reader.h"
michael@0 12
michael@0 13 namespace mp4_demuxer {
michael@0 14
michael@0 15 static const uint8_t kAnnexBStartCode[] = {0, 0, 0, 1};
michael@0 16 static const int kAnnexBStartCodeSize = 4;
michael@0 17
michael@0 18 static bool ConvertAVCToAnnexBInPlaceForLengthSize4(std::vector<uint8_t>* buf) {
michael@0 19 const int kLengthSize = 4;
michael@0 20 size_t pos = 0;
michael@0 21 while (pos + kLengthSize < buf->size()) {
michael@0 22 int nal_size = (*buf)[pos];
michael@0 23 nal_size = (nal_size << 8) + (*buf)[pos+1];
michael@0 24 nal_size = (nal_size << 8) + (*buf)[pos+2];
michael@0 25 nal_size = (nal_size << 8) + (*buf)[pos+3];
michael@0 26 std::copy(kAnnexBStartCode, kAnnexBStartCode + kAnnexBStartCodeSize,
michael@0 27 buf->begin() + pos);
michael@0 28 pos += kLengthSize + nal_size;
michael@0 29 }
michael@0 30 return pos == buf->size();
michael@0 31 }
michael@0 32
michael@0 33 // static
michael@0 34 bool AVC::ConvertFrameToAnnexB(int length_size, std::vector<uint8_t>* buffer) {
michael@0 35 RCHECK(length_size == 1 || length_size == 2 || length_size == 4);
michael@0 36
michael@0 37 if (length_size == 4)
michael@0 38 return ConvertAVCToAnnexBInPlaceForLengthSize4(buffer);
michael@0 39
michael@0 40 std::vector<uint8_t> temp;
michael@0 41 temp.swap(*buffer);
michael@0 42 buffer->reserve(temp.size() + 32);
michael@0 43
michael@0 44 size_t pos = 0;
michael@0 45 while (pos + length_size < temp.size()) {
michael@0 46 int nal_size = temp[pos];
michael@0 47 if (length_size == 2) nal_size = (nal_size << 8) + temp[pos+1];
michael@0 48 pos += length_size;
michael@0 49
michael@0 50 RCHECK(pos + nal_size <= temp.size());
michael@0 51 buffer->insert(buffer->end(), kAnnexBStartCode,
michael@0 52 kAnnexBStartCode + kAnnexBStartCodeSize);
michael@0 53 buffer->insert(buffer->end(), temp.begin() + pos,
michael@0 54 temp.begin() + pos + nal_size);
michael@0 55 pos += nal_size;
michael@0 56 }
michael@0 57 return pos == temp.size();
michael@0 58 }
michael@0 59
michael@0 60 // static
michael@0 61 bool AVC::ConvertConfigToAnnexB(
michael@0 62 const AVCDecoderConfigurationRecord& avc_config,
michael@0 63 std::vector<uint8_t>* buffer) {
michael@0 64 DCHECK(buffer->empty());
michael@0 65 buffer->clear();
michael@0 66 int total_size = 0;
michael@0 67 for (size_t i = 0; i < avc_config.sps_list.size(); i++)
michael@0 68 total_size += avc_config.sps_list[i].size() + kAnnexBStartCodeSize;
michael@0 69 for (size_t i = 0; i < avc_config.pps_list.size(); i++)
michael@0 70 total_size += avc_config.pps_list[i].size() + kAnnexBStartCodeSize;
michael@0 71 buffer->reserve(total_size);
michael@0 72
michael@0 73 for (size_t i = 0; i < avc_config.sps_list.size(); i++) {
michael@0 74 buffer->insert(buffer->end(), kAnnexBStartCode,
michael@0 75 kAnnexBStartCode + kAnnexBStartCodeSize);
michael@0 76 buffer->insert(buffer->end(), avc_config.sps_list[i].begin(),
michael@0 77 avc_config.sps_list[i].end());
michael@0 78 }
michael@0 79
michael@0 80 for (size_t i = 0; i < avc_config.pps_list.size(); i++) {
michael@0 81 buffer->insert(buffer->end(), kAnnexBStartCode,
michael@0 82 kAnnexBStartCode + kAnnexBStartCodeSize);
michael@0 83 buffer->insert(buffer->end(), avc_config.pps_list[i].begin(),
michael@0 84 avc_config.pps_list[i].end());
michael@0 85 }
michael@0 86 return true;
michael@0 87 }
michael@0 88
michael@0 89 } // namespace mp4_demuxer

mercurial