Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: chromecast/media/cma/test/frame_segmenter_for_test.cc

Issue 609383004: Chromecast: adds test frame-segmenter code to support CMA unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cma-decrypt-context
Patch Set: base/callback include Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "chromecast/media/cma/test/frame_segmenter_for_test.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/port.h"
11 #include "base/run_loop.h"
12 #include "chromecast/media/cma/base/decoder_buffer_adapter.h"
13 #include "media/base/decoder_buffer.h"
14 #include "media/base/demuxer.h"
15 #include "media/base/media_log.h"
16 #include "media/base/test_helpers.h"
17 #include "media/ffmpeg/ffmpeg_common.h"
damienv1 2014/10/03 05:42:09 Do we need this include ?
gunsch 2014/10/03 16:26:17 Huh, apparently not. That reduces the DEPS footpri
18 #include "media/filters/ffmpeg_demuxer.h"
19 #include "media/filters/file_data_source.h"
20 #include "media/filters/h264_parser.h"
21
22 namespace chromecast {
23 namespace media {
24
25 namespace {
26
27 struct AudioFrameHeader {
28 size_t offset;
29 size_t frame_size;
30 int sampling_frequency;
31 };
32
33 size_t mp3_bitrate[] = {
34 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 };
35 size_t mp3_sample_rate[] = { 44100, 48000, 32000, 0 };
36
37 AudioFrameHeader FindNextMp3Header(const uint8* data, size_t data_size) {
38 bool found = false;
39 AudioFrameHeader header;
40 header.frame_size = 0;
41 if (data_size < 4)
42 return header;
43
44 for (size_t k = 0; k < data_size - 4 && !found; k++) {
45 // Mp3 Header:
46 // syncword: 11111111111
47 // Mpeg1: 11
48 // Layer3: 01
49 if (!(data[k + 0] == 0xff && (data[k + 1] & 0xfe) == 0xfa))
50 continue;
51
52 int bitrate_index = (data[k + 2] >> 4);
53 if (bitrate_index == 0 || bitrate_index == 15) {
54 // Free size or bad bitrate => not supported.
55 continue;
56 }
57
58 int sample_rate_index = (data[k + 2] >> 2) & 0x3;
59 if (sample_rate_index == 3)
60 continue;
61
62 size_t frame_size =
63 ((1152 / 8) * mp3_bitrate[bitrate_index] * 1000) /
64 mp3_sample_rate[sample_rate_index];
65 if (data[k + 2] & 0x2)
66 frame_size++;
67
68 // Make sure the frame is complete.
69 if (k + frame_size > data_size)
70 break;
71
72 if (k + frame_size < data_size - 3 &&
73 !(data[k + frame_size + 0] == 0xff &&
74 (data[k + frame_size + 1] & 0xfe) == 0xfa)) {
75 continue;
76 }
77
78 found = true;
79 header.offset = k;
80 header.frame_size = frame_size;
81 header.sampling_frequency = mp3_sample_rate[sample_rate_index];
82 }
83 return header;
84 }
85
86 }
87
88 std::list<scoped_refptr<DecoderBufferBase> > Mp3SegmenterForTest(
89 const uint8* data, size_t data_size) {
90 size_t offset = 0;
91 std::list<scoped_refptr<DecoderBufferBase> > audio_frames;
92 base::TimeDelta timestamp;
93
94 while (true) {
95 AudioFrameHeader header = FindNextMp3Header(&data[offset],
96 data_size - offset);
97 if (header.frame_size == 0)
98 break;
99
100 header.offset += offset;
101 offset = header.offset + header.frame_size;
102
103 scoped_refptr< ::media::DecoderBuffer> buffer(
104 ::media::DecoderBuffer::CopyFrom(
105 &data[header.offset], header.frame_size));
106 buffer->set_timestamp(timestamp);
107 audio_frames.push_back(
108 scoped_refptr<DecoderBufferBase>(new DecoderBufferAdapter(buffer)));
109
110 // 1152 samples in an MP3 frame.
111 timestamp += base::TimeDelta::FromMicroseconds(
112 (GG_UINT64_C(1152) * 1000 * 1000) / header.sampling_frequency);
113 }
114 return audio_frames;
115 }
116
117 struct H264AccessUnit {
118 H264AccessUnit();
119
120 size_t offset;
121 size_t size;
122 int has_vcl;
123 int poc;
124 };
125
126 H264AccessUnit::H264AccessUnit()
127 : offset(0),
128 size(0),
129 has_vcl(false),
130 poc(0) {
131 }
132
133 std::list<scoped_refptr<DecoderBufferBase> > H264SegmenterForTest(
134 const uint8* data, size_t data_size) {
135 std::list<scoped_refptr<DecoderBufferBase> > video_frames;
136 std::list<H264AccessUnit> access_unit_list;
137 H264AccessUnit access_unit;
138
139 int prev_pic_order_cnt_lsb = 0;
140 int pic_order_cnt_msb = 0;
141
142 scoped_ptr< ::media::H264Parser> h264_parser(new ::media::H264Parser());
143 h264_parser->SetStream(data, data_size);
144
145 size_t offset = 0;
146 while (true) {
147 bool is_eos = false;
148 ::media::H264NALU nalu;
149 switch (h264_parser->AdvanceToNextNALU(&nalu)) {
150 case ::media::H264Parser::kOk:
151 break;
152 case ::media::H264Parser::kInvalidStream:
153 case ::media::H264Parser::kUnsupportedStream:
154 return video_frames;
155 case ::media::H264Parser::kEOStream:
156 is_eos = true;
157 break;
158 }
159 if (is_eos)
160 break;
161
162 // To get the NALU syncword offset, substract 3 or 4
163 // which corresponds to the possible syncword lengths.
164 size_t nalu_offset = nalu.data - data;
165 nalu_offset -= 3;
166 if (nalu_offset > 0 && data[nalu_offset-1] == 0)
167 nalu_offset--;
168
169 switch (nalu.nal_unit_type) {
170 case ::media::H264NALU::kAUD: {
171 break;
172 }
173 case ::media::H264NALU::kSPS: {
174 int sps_id;
175 if (h264_parser->ParseSPS(&sps_id) != ::media::H264Parser::kOk)
176 return video_frames;
177 if (access_unit.has_vcl) {
178 access_unit.size = nalu_offset - access_unit.offset;
179 access_unit_list.push_back(access_unit);
180 access_unit = H264AccessUnit();
181 access_unit.offset = nalu_offset;
182 }
183 break;
184 }
185 case ::media::H264NALU::kPPS: {
186 int pps_id;
187 if (h264_parser->ParsePPS(&pps_id) != ::media::H264Parser::kOk)
188 return video_frames;
189 if (access_unit.has_vcl) {
190 access_unit.size = nalu_offset - access_unit.offset;
191 access_unit_list.push_back(access_unit);
192 access_unit = H264AccessUnit();
193 access_unit.offset = nalu_offset;
194 }
195 break;
196 }
197 case ::media::H264NALU::kIDRSlice:
198 case ::media::H264NALU::kNonIDRSlice: {
199 ::media::H264SliceHeader shdr;
200 if (h264_parser->ParseSliceHeader(nalu, &shdr) !=
201 ::media::H264Parser::kOk) {
202 return video_frames;
203 }
204 const ::media::H264PPS* pps =
205 h264_parser->GetPPS(shdr.pic_parameter_set_id);
206 if (!pps)
207 return video_frames;
208 const ::media::H264SPS* sps =
209 h264_parser->GetSPS(pps->seq_parameter_set_id);
210
211 // Very simplified way to segment H264.
212 // This assumes only 1 VCL NALU per access unit.
213 if (access_unit.has_vcl) {
214 access_unit.size = nalu_offset - access_unit.offset;
215 access_unit_list.push_back(access_unit);
216 access_unit = H264AccessUnit();
217 access_unit.offset = nalu_offset;
218 }
219
220 access_unit.has_vcl = true;
221
222 // Support only explicit POC so far.
223 if (sps->pic_order_cnt_type != 0) {
224 LOG(WARNING) << "Unsupported pic_order_cnt_type";
225 return video_frames;
226 }
227 int diff_pic_order_cnt_lsb =
228 shdr.pic_order_cnt_lsb - prev_pic_order_cnt_lsb;
229 int max_pic_order_cnt_lsb =
230 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
231 if (diff_pic_order_cnt_lsb < 0 &&
232 diff_pic_order_cnt_lsb <= -max_pic_order_cnt_lsb / 2) {
233 pic_order_cnt_msb += max_pic_order_cnt_lsb;
234 } else if (diff_pic_order_cnt_lsb > 0 &&
235 diff_pic_order_cnt_lsb > max_pic_order_cnt_lsb / 2) {
236 pic_order_cnt_msb -= max_pic_order_cnt_lsb;
237 }
238 access_unit.poc = pic_order_cnt_msb + shdr.pic_order_cnt_lsb;
239 prev_pic_order_cnt_lsb = shdr.pic_order_cnt_lsb;
240 break;
241 }
242 default: {
243 }
244 }
245 }
246
247 // Emit the last access unit.
248 if (access_unit.has_vcl) {
249 access_unit.size = data_size - access_unit.offset;
250 access_unit_list.push_back(access_unit);
251 }
252
253 // Create the list of buffers.
254 // Totally arbitrary decision: assume a delta POC of 1 is 20ms (50Hz field
255 // rate).
256 base::TimeDelta poc_duration = base::TimeDelta::FromMilliseconds(20);
257 for (std::list<H264AccessUnit>::iterator it = access_unit_list.begin();
258 it != access_unit_list.end(); ++it) {
259 scoped_refptr< ::media::DecoderBuffer> buffer(
260 ::media::DecoderBuffer::CopyFrom(&data[it->offset], it->size));
261 buffer->set_timestamp(it->poc * poc_duration);
262 video_frames.push_back(
263 scoped_refptr<DecoderBufferBase>(new DecoderBufferAdapter(buffer)));
264 }
265
266 return video_frames;
267 }
268
269 void FFDemuxNeedKeyCB(const std::string& type,
damienv1 2014/10/03 05:42:09 nit: This is usually the style used to typedef a c
gunsch 2014/10/03 16:26:16 Done.
270 const std::vector<uint8>& init_data) {
271 LOG(FATAL) << "Unexpected test failure: file is encrypted.";
272 }
273
274 void DemuxReadCB(BufferList* buffer_list,
damienv1 2014/10/03 05:42:09 Same: OnNewBuffer.
gunsch 2014/10/03 16:26:16 Done.
275 const base::Closure& finished_cb,
276 ::media::DemuxerStream::Status status,
277 const scoped_refptr< ::media::DecoderBuffer>& buffer) {
278 CHECK_EQ(status, ::media::DemuxerStream::kOk);
279 CHECK(buffer.get());
280 CHECK(buffer_list);
281 buffer_list->push_back(new DecoderBufferAdapter(buffer));
282 finished_cb.Run();
283 }
284
285 class FakeDemuxerHost : public ::media::DemuxerHost {
286 public:
287 // DemuxerHost implementation.
288 virtual void AddBufferedTimeRange(base::TimeDelta start,
289 base::TimeDelta end) OVERRIDE {}
290 virtual void SetDuration(base::TimeDelta duration) OVERRIDE {}
291 virtual void OnDemuxerError(::media::PipelineStatus error) OVERRIDE {
292 LOG(FATAL) << "OnDemuxerError: " << error;
293 }
294 virtual void AddTextStream(::media::DemuxerStream* text_stream,
295 const ::media::TextTrackConfig& config) OVERRIDE {
296 }
297 virtual void RemoveTextStream(::media::DemuxerStream* text_stream) OVERRIDE {
298 }
299 };
300
301 DemuxResult::DemuxResult() {
302 }
303
304 DemuxResult::~DemuxResult() {
305 }
306
307 DemuxResult FFmpegDemuxForTest(const base::FilePath& filepath,
308 bool audio) {
309 FakeDemuxerHost fake_demuxer_host;
310 ::media::FileDataSource data_source;
311 CHECK(data_source.Initialize(filepath));
312
313 ::media::FFmpegDemuxer demuxer(base::MessageLoopProxy::current(),
314 &data_source,
damienv1 2014/10/03 05:42:09 Alignment.
gunsch 2014/10/03 16:26:16 Done.
315 base::Bind(&FFDemuxNeedKeyCB),
316 new ::media::MediaLog());
317 ::media::WaitableMessageLoopEvent init_event;
318 demuxer.Initialize(&fake_demuxer_host,
319 init_event.GetPipelineStatusCB(),
320 false);
321 init_event.RunAndWaitForStatus(::media::PIPELINE_OK);
322
323 ::media::DemuxerStream* stream = demuxer.GetStream(
324 audio ? ::media::DemuxerStream::AUDIO : ::media::DemuxerStream::VIDEO);
325 CHECK(stream);
326
327 DemuxResult demux_result;
328 if (audio) {
329 demux_result.audio_config = stream->audio_decoder_config();
330 } else {
331 demux_result.video_config = stream->video_decoder_config();
332 }
333
334 bool end_of_stream = false;
335 while (!end_of_stream) {
336 base::RunLoop run_loop;
337 stream->Read(base::Bind(&DemuxReadCB,
338 base::Unretained(&demux_result.frames),
339 run_loop.QuitClosure()));
340 run_loop.Run();
341 CHECK(!demux_result.frames.empty());
342 end_of_stream = demux_result.frames.back()->end_of_stream();
343 }
344
345 demuxer.Stop();
346 return demux_result;
347 }
348
349 } // namespace media
350 } // namespace chromecast
OLDNEW
« no previous file with comments | « chromecast/media/cma/test/frame_segmenter_for_test.h ('k') | chromecast/media/cma/test/mock_frame_consumer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698