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