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/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/filters/ffmpeg_demuxer.h" |
| 18 #include "media/filters/file_data_source.h" |
| 19 #include "media/filters/h264_parser.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 int mp3_bitrate[] = { |
| 33 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 }; |
| 34 int 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 BufferList Mp3SegmenterForTest(const uint8* data, size_t data_size) { |
| 88 size_t offset = 0; |
| 89 BufferList audio_frames; |
| 90 base::TimeDelta timestamp; |
| 91 |
| 92 while (true) { |
| 93 AudioFrameHeader header = FindNextMp3Header(&data[offset], |
| 94 data_size - offset); |
| 95 if (header.frame_size == 0) |
| 96 break; |
| 97 |
| 98 header.offset += offset; |
| 99 offset = header.offset + header.frame_size; |
| 100 |
| 101 scoped_refptr< ::media::DecoderBuffer> buffer( |
| 102 ::media::DecoderBuffer::CopyFrom( |
| 103 &data[header.offset], header.frame_size)); |
| 104 buffer->set_timestamp(timestamp); |
| 105 audio_frames.push_back( |
| 106 scoped_refptr<DecoderBufferBase>(new DecoderBufferAdapter(buffer))); |
| 107 |
| 108 // 1152 samples in an MP3 frame. |
| 109 timestamp += base::TimeDelta::FromMicroseconds( |
| 110 (GG_UINT64_C(1152) * 1000 * 1000) / header.sampling_frequency); |
| 111 } |
| 112 return audio_frames; |
| 113 } |
| 114 |
| 115 struct H264AccessUnit { |
| 116 H264AccessUnit(); |
| 117 |
| 118 size_t offset; |
| 119 size_t size; |
| 120 int has_vcl; |
| 121 int poc; |
| 122 }; |
| 123 |
| 124 H264AccessUnit::H264AccessUnit() |
| 125 : offset(0), |
| 126 size(0), |
| 127 has_vcl(false), |
| 128 poc(0) { |
| 129 } |
| 130 |
| 131 BufferList H264SegmenterForTest(const uint8* data, size_t data_size) { |
| 132 BufferList video_frames; |
| 133 std::list<H264AccessUnit> access_unit_list; |
| 134 H264AccessUnit access_unit; |
| 135 |
| 136 int prev_pic_order_cnt_lsb = 0; |
| 137 int pic_order_cnt_msb = 0; |
| 138 |
| 139 scoped_ptr< ::media::H264Parser> h264_parser(new ::media::H264Parser()); |
| 140 h264_parser->SetStream(data, data_size); |
| 141 |
| 142 size_t offset = 0; |
| 143 while (true) { |
| 144 bool is_eos = false; |
| 145 ::media::H264NALU nalu; |
| 146 switch (h264_parser->AdvanceToNextNALU(&nalu)) { |
| 147 case ::media::H264Parser::kOk: |
| 148 break; |
| 149 case ::media::H264Parser::kInvalidStream: |
| 150 case ::media::H264Parser::kUnsupportedStream: |
| 151 return video_frames; |
| 152 case ::media::H264Parser::kEOStream: |
| 153 is_eos = true; |
| 154 break; |
| 155 } |
| 156 if (is_eos) |
| 157 break; |
| 158 |
| 159 // To get the NALU syncword offset, substract 3 or 4 |
| 160 // which corresponds to the possible syncword lengths. |
| 161 size_t nalu_offset = nalu.data - data; |
| 162 nalu_offset -= 3; |
| 163 if (nalu_offset > 0 && data[nalu_offset-1] == 0) |
| 164 nalu_offset--; |
| 165 |
| 166 switch (nalu.nal_unit_type) { |
| 167 case ::media::H264NALU::kAUD: { |
| 168 break; |
| 169 } |
| 170 case ::media::H264NALU::kSPS: { |
| 171 int sps_id; |
| 172 if (h264_parser->ParseSPS(&sps_id) != ::media::H264Parser::kOk) |
| 173 return video_frames; |
| 174 if (access_unit.has_vcl) { |
| 175 access_unit.size = nalu_offset - access_unit.offset; |
| 176 access_unit_list.push_back(access_unit); |
| 177 access_unit = H264AccessUnit(); |
| 178 access_unit.offset = nalu_offset; |
| 179 } |
| 180 break; |
| 181 } |
| 182 case ::media::H264NALU::kPPS: { |
| 183 int pps_id; |
| 184 if (h264_parser->ParsePPS(&pps_id) != ::media::H264Parser::kOk) |
| 185 return video_frames; |
| 186 if (access_unit.has_vcl) { |
| 187 access_unit.size = nalu_offset - access_unit.offset; |
| 188 access_unit_list.push_back(access_unit); |
| 189 access_unit = H264AccessUnit(); |
| 190 access_unit.offset = nalu_offset; |
| 191 } |
| 192 break; |
| 193 } |
| 194 case ::media::H264NALU::kIDRSlice: |
| 195 case ::media::H264NALU::kNonIDRSlice: { |
| 196 ::media::H264SliceHeader shdr; |
| 197 if (h264_parser->ParseSliceHeader(nalu, &shdr) != |
| 198 ::media::H264Parser::kOk) { |
| 199 return video_frames; |
| 200 } |
| 201 const ::media::H264PPS* pps = |
| 202 h264_parser->GetPPS(shdr.pic_parameter_set_id); |
| 203 if (!pps) |
| 204 return video_frames; |
| 205 const ::media::H264SPS* sps = |
| 206 h264_parser->GetSPS(pps->seq_parameter_set_id); |
| 207 |
| 208 // Very simplified way to segment H264. |
| 209 // This assumes only 1 VCL NALU per access unit. |
| 210 if (access_unit.has_vcl) { |
| 211 access_unit.size = nalu_offset - access_unit.offset; |
| 212 access_unit_list.push_back(access_unit); |
| 213 access_unit = H264AccessUnit(); |
| 214 access_unit.offset = nalu_offset; |
| 215 } |
| 216 |
| 217 access_unit.has_vcl = true; |
| 218 |
| 219 // Support only explicit POC so far. |
| 220 if (sps->pic_order_cnt_type != 0) { |
| 221 LOG(WARNING) << "Unsupported pic_order_cnt_type"; |
| 222 return video_frames; |
| 223 } |
| 224 int diff_pic_order_cnt_lsb = |
| 225 shdr.pic_order_cnt_lsb - prev_pic_order_cnt_lsb; |
| 226 int max_pic_order_cnt_lsb = |
| 227 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); |
| 228 if (diff_pic_order_cnt_lsb < 0 && |
| 229 diff_pic_order_cnt_lsb <= -max_pic_order_cnt_lsb / 2) { |
| 230 pic_order_cnt_msb += max_pic_order_cnt_lsb; |
| 231 } else 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 } |
| 235 access_unit.poc = pic_order_cnt_msb + shdr.pic_order_cnt_lsb; |
| 236 prev_pic_order_cnt_lsb = shdr.pic_order_cnt_lsb; |
| 237 break; |
| 238 } |
| 239 default: { |
| 240 } |
| 241 } |
| 242 } |
| 243 |
| 244 // Emit the last access unit. |
| 245 if (access_unit.has_vcl) { |
| 246 access_unit.size = data_size - access_unit.offset; |
| 247 access_unit_list.push_back(access_unit); |
| 248 } |
| 249 |
| 250 // Create the list of buffers. |
| 251 // Totally arbitrary decision: assume a delta POC of 1 is 20ms (50Hz field |
| 252 // rate). |
| 253 base::TimeDelta poc_duration = base::TimeDelta::FromMilliseconds(20); |
| 254 for (std::list<H264AccessUnit>::iterator it = access_unit_list.begin(); |
| 255 it != access_unit_list.end(); ++it) { |
| 256 scoped_refptr< ::media::DecoderBuffer> buffer( |
| 257 ::media::DecoderBuffer::CopyFrom(&data[it->offset], it->size)); |
| 258 buffer->set_timestamp(it->poc * poc_duration); |
| 259 video_frames.push_back( |
| 260 scoped_refptr<DecoderBufferBase>(new DecoderBufferAdapter(buffer))); |
| 261 } |
| 262 |
| 263 return video_frames; |
| 264 } |
| 265 |
| 266 void OnDemuxerNeedKey(const std::string& type, |
| 267 const std::vector<uint8>& init_data) { |
| 268 LOG(FATAL) << "Unexpected test failure: file is encrypted."; |
| 269 } |
| 270 |
| 271 void OnNewBuffer(BufferList* buffer_list, |
| 272 const base::Closure& finished_cb, |
| 273 ::media::DemuxerStream::Status status, |
| 274 const scoped_refptr< ::media::DecoderBuffer>& buffer) { |
| 275 CHECK_EQ(status, ::media::DemuxerStream::kOk); |
| 276 CHECK(buffer.get()); |
| 277 CHECK(buffer_list); |
| 278 buffer_list->push_back(new DecoderBufferAdapter(buffer)); |
| 279 finished_cb.Run(); |
| 280 } |
| 281 |
| 282 class FakeDemuxerHost : public ::media::DemuxerHost { |
| 283 public: |
| 284 // DemuxerHost implementation. |
| 285 virtual void AddBufferedTimeRange(base::TimeDelta start, |
| 286 base::TimeDelta end) OVERRIDE {} |
| 287 virtual void SetDuration(base::TimeDelta duration) OVERRIDE {} |
| 288 virtual void OnDemuxerError(::media::PipelineStatus error) OVERRIDE { |
| 289 LOG(FATAL) << "OnDemuxerError: " << error; |
| 290 } |
| 291 virtual void AddTextStream(::media::DemuxerStream* text_stream, |
| 292 const ::media::TextTrackConfig& config) OVERRIDE { |
| 293 } |
| 294 virtual void RemoveTextStream(::media::DemuxerStream* text_stream) OVERRIDE { |
| 295 } |
| 296 }; |
| 297 |
| 298 DemuxResult::DemuxResult() { |
| 299 } |
| 300 |
| 301 DemuxResult::~DemuxResult() { |
| 302 } |
| 303 |
| 304 DemuxResult FFmpegDemuxForTest(const base::FilePath& filepath, |
| 305 bool audio) { |
| 306 FakeDemuxerHost fake_demuxer_host; |
| 307 ::media::FileDataSource data_source; |
| 308 CHECK(data_source.Initialize(filepath)); |
| 309 |
| 310 ::media::FFmpegDemuxer demuxer(base::MessageLoopProxy::current(), |
| 311 &data_source, |
| 312 base::Bind(&OnDemuxerNeedKey), |
| 313 new ::media::MediaLog()); |
| 314 ::media::WaitableMessageLoopEvent init_event; |
| 315 demuxer.Initialize(&fake_demuxer_host, |
| 316 init_event.GetPipelineStatusCB(), |
| 317 false); |
| 318 init_event.RunAndWaitForStatus(::media::PIPELINE_OK); |
| 319 |
| 320 ::media::DemuxerStream* stream = demuxer.GetStream( |
| 321 audio ? ::media::DemuxerStream::AUDIO : ::media::DemuxerStream::VIDEO); |
| 322 CHECK(stream); |
| 323 |
| 324 DemuxResult demux_result; |
| 325 if (audio) { |
| 326 demux_result.audio_config = stream->audio_decoder_config(); |
| 327 } else { |
| 328 demux_result.video_config = stream->video_decoder_config(); |
| 329 } |
| 330 |
| 331 bool end_of_stream = false; |
| 332 while (!end_of_stream) { |
| 333 base::RunLoop run_loop; |
| 334 stream->Read(base::Bind(&OnNewBuffer, |
| 335 base::Unretained(&demux_result.frames), |
| 336 run_loop.QuitClosure())); |
| 337 run_loop.Run(); |
| 338 CHECK(!demux_result.frames.empty()); |
| 339 end_of_stream = demux_result.frames.back()->end_of_stream(); |
| 340 } |
| 341 |
| 342 demuxer.Stop(); |
| 343 return demux_result; |
| 344 } |
| 345 |
| 346 } // namespace media |
| 347 } // namespace chromecast |
OLD | NEW |