1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MPEG2PSExtractor"
19 #include <utils/Log.h>
20
21 #include "MPEG2PSExtractor.h"
22
23 #include <media/stagefright/foundation/ABitReader.h>
24 #include <media/stagefright/foundation/ABuffer.h>
25 #include <media/stagefright/foundation/ADebug.h>
26 #include <media/stagefright/foundation/AMessage.h>
27 #include <media/stagefright/foundation/ByteUtils.h>
28 #include <media/stagefright/foundation/hexdump.h>
29 #include <media/stagefright/MediaDefs.h>
30 #include <media/stagefright/MediaErrors.h>
31 #include <media/stagefright/MetaData.h>
32 #include <media/stagefright/Utils.h>
33 #include <mpeg2ts/AnotherPacketSource.h>
34 #include <mpeg2ts/ESQueue.h>
35 #include <utils/String8.h>
36
37 #include <inttypes.h>
38
39 namespace android {
40
41 struct MPEG2PSExtractor::Track : public MediaTrackHelper {
42 Track(MPEG2PSExtractor *extractor,
43 unsigned stream_id, unsigned stream_type);
44
45 virtual media_status_t start();
46 virtual media_status_t stop();
47 virtual media_status_t getFormat(AMediaFormat *);
48
49 virtual media_status_t read(
50 MediaBufferHelper **buffer, const ReadOptions *options);
51
52 protected:
53 virtual ~Track();
54
55 private:
56 friend struct MPEG2PSExtractor;
57
58 MPEG2PSExtractor *mExtractor;
59
60 unsigned mStreamID;
61 unsigned mStreamType;
62 ElementaryStreamQueue *mQueue;
63 sp<AnotherPacketSource> mSource;
64
65 status_t appendPESData(
66 unsigned PTS_DTS_flags,
67 uint64_t PTS, uint64_t DTS,
68 const uint8_t *data, size_t size);
69
70 DISALLOW_EVIL_CONSTRUCTORS(Track);
71 };
72
73 struct MPEG2PSExtractor::WrappedTrack : public MediaTrackHelper {
74 WrappedTrack(MPEG2PSExtractor *extractor, Track *track);
75
76 virtual media_status_t start();
77 virtual media_status_t stop();
78 virtual media_status_t getFormat(AMediaFormat *);
79
80 virtual media_status_t read(
81 MediaBufferHelper **buffer, const ReadOptions *options);
82
83 protected:
84 virtual ~WrappedTrack();
85
86 private:
87 MPEG2PSExtractor *mExtractor;
88 MPEG2PSExtractor::Track *mTrack;
89
90 DISALLOW_EVIL_CONSTRUCTORS(WrappedTrack);
91 };
92
93 ////////////////////////////////////////////////////////////////////////////////
94
MPEG2PSExtractor(DataSourceHelper * source)95 MPEG2PSExtractor::MPEG2PSExtractor(DataSourceHelper *source)
96 : mDataSource(source),
97 mOffset(0),
98 mFinalResult(OK),
99 mBuffer(new ABuffer(0)),
100 mScanning(true),
101 mProgramStreamMapValid(false) {
102 for (size_t i = 0; i < 500; ++i) {
103 if (feedMore() != OK) {
104 break;
105 }
106 }
107
108 // Remove all tracks that were unable to determine their format.
109 AMediaFormat *meta = AMediaFormat_new();
110 for (size_t i = mTracks.size(); i > 0;) {
111 i--;
112 Track *track = mTracks.valueAt(i);
113 if (track->getFormat(meta) != AMEDIA_OK) {
114 mTracks.removeItemsAt(i);
115 delete track;
116 }
117 }
118 AMediaFormat_delete(meta);
119
120 mScanning = false;
121 }
122
~MPEG2PSExtractor()123 MPEG2PSExtractor::~MPEG2PSExtractor() {
124 delete mDataSource;
125 for (size_t i = mTracks.size(); i > 0;) {
126 i--;
127 delete mTracks.valueAt(i);
128 }
129 }
130
countTracks()131 size_t MPEG2PSExtractor::countTracks() {
132 return mTracks.size();
133 }
134
getTrack(size_t index)135 MediaTrackHelper *MPEG2PSExtractor::getTrack(size_t index) {
136 if (index >= mTracks.size()) {
137 return NULL;
138 }
139
140 return new WrappedTrack(this, mTracks.valueAt(index));
141 }
142
getTrackMetaData(AMediaFormat * meta,size_t index,uint32_t)143 media_status_t MPEG2PSExtractor::getTrackMetaData(
144 AMediaFormat *meta,
145 size_t index, uint32_t /* flags */) {
146 if (index >= mTracks.size()) {
147 return AMEDIA_ERROR_UNKNOWN;
148 }
149
150 return mTracks.valueAt(index)->getFormat(meta);
151 }
152
getMetaData(AMediaFormat * meta)153 media_status_t MPEG2PSExtractor::getMetaData(AMediaFormat *meta) {
154 AMediaFormat_setString(meta, AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_CONTAINER_MPEG2PS);
155
156 return AMEDIA_OK;
157 }
158
flags() const159 uint32_t MPEG2PSExtractor::flags() const {
160 return CAN_PAUSE;
161 }
162
feedMore()163 status_t MPEG2PSExtractor::feedMore() {
164 Mutex::Autolock autoLock(mLock);
165
166 // How much data we're reading at a time
167 static const size_t kChunkSize = 8192;
168
169 for (;;) {
170 status_t err = dequeueChunk();
171
172 if (err == -EAGAIN && mFinalResult == OK) {
173 memmove(mBuffer->base(), mBuffer->data(), mBuffer->size());
174 mBuffer->setRange(0, mBuffer->size());
175
176 if (mBuffer->size() + kChunkSize > mBuffer->capacity()) {
177 size_t newCapacity = mBuffer->capacity() + kChunkSize;
178 sp<ABuffer> newBuffer = new ABuffer(newCapacity);
179 memcpy(newBuffer->data(), mBuffer->data(), mBuffer->size());
180 newBuffer->setRange(0, mBuffer->size());
181 mBuffer = newBuffer;
182 }
183
184 ssize_t n = mDataSource->readAt(
185 mOffset, mBuffer->data() + mBuffer->size(), kChunkSize);
186
187 if (n < (ssize_t)kChunkSize) {
188 mFinalResult = (n < 0) ? (status_t)n : ERROR_END_OF_STREAM;
189 return mFinalResult;
190 }
191
192 mBuffer->setRange(mBuffer->offset(), mBuffer->size() + n);
193 mOffset += n;
194 } else if (err != OK) {
195 mFinalResult = err;
196 return err;
197 } else {
198 return OK;
199 }
200 }
201 }
202
dequeueChunk()203 status_t MPEG2PSExtractor::dequeueChunk() {
204 if (mBuffer->size() < 4) {
205 return -EAGAIN;
206 }
207
208 if (memcmp("\x00\x00\x01", mBuffer->data(), 3)) {
209 return ERROR_MALFORMED;
210 }
211
212 unsigned chunkType = mBuffer->data()[3];
213
214 ssize_t res;
215
216 switch (chunkType) {
217 case 0xba:
218 {
219 res = dequeuePack();
220 break;
221 }
222
223 case 0xbb:
224 {
225 res = dequeueSystemHeader();
226 break;
227 }
228
229 default:
230 {
231 res = dequeuePES();
232 break;
233 }
234 }
235
236 if (res > 0) {
237 if (mBuffer->size() < (size_t)res) {
238 return -EAGAIN;
239 }
240
241 mBuffer->setRange(mBuffer->offset() + res, mBuffer->size() - res);
242 res = OK;
243 }
244
245 return res;
246 }
247
dequeuePack()248 ssize_t MPEG2PSExtractor::dequeuePack() {
249 // 32 + 2 + 3 + 1 + 15 + 1 + 15+ 1 + 9 + 1 + 22 + 1 + 1 | +5
250
251 if (mBuffer->size() < 14) {
252 return -EAGAIN;
253 }
254
255 unsigned pack_stuffing_length = mBuffer->data()[13] & 7;
256
257 return pack_stuffing_length + 14;
258 }
259
dequeueSystemHeader()260 ssize_t MPEG2PSExtractor::dequeueSystemHeader() {
261 if (mBuffer->size() < 6) {
262 return -EAGAIN;
263 }
264
265 unsigned header_length = U16_AT(mBuffer->data() + 4);
266
267 return header_length + 6;
268 }
269
dequeuePES()270 ssize_t MPEG2PSExtractor::dequeuePES() {
271 if (mBuffer->size() < 6) {
272 return -EAGAIN;
273 }
274
275 unsigned PES_packet_length = U16_AT(mBuffer->data() + 4);
276 if (PES_packet_length == 0u) {
277 ALOGE("PES_packet_length is 0");
278 return -EAGAIN;
279 }
280
281 size_t n = PES_packet_length + 6;
282
283 if (mBuffer->size() < n) {
284 return -EAGAIN;
285 }
286
287 ABitReader br(mBuffer->data(), n);
288
289 unsigned packet_startcode_prefix = br.getBits(24);
290
291 ALOGV("packet_startcode_prefix = 0x%08x", packet_startcode_prefix);
292
293 if (packet_startcode_prefix != 1) {
294 ALOGV("Supposedly payload_unit_start=1 unit does not start "
295 "with startcode.");
296
297 return ERROR_MALFORMED;
298 }
299
300 if (packet_startcode_prefix != 0x000001u) {
301 ALOGE("Wrong PES prefix");
302 return ERROR_MALFORMED;
303 }
304
305 unsigned stream_id = br.getBits(8);
306 ALOGV("stream_id = 0x%02x", stream_id);
307
308 /* unsigned PES_packet_length = */br.getBits(16);
309
310 if (stream_id == 0xbc) {
311 // program_stream_map
312
313 if (!mScanning) {
314 return n;
315 }
316
317 mStreamTypeByESID.clear();
318
319 /* unsigned current_next_indicator = */br.getBits(1);
320 /* unsigned reserved = */br.getBits(2);
321 /* unsigned program_stream_map_version = */br.getBits(5);
322 /* unsigned reserved = */br.getBits(7);
323 /* unsigned marker_bit = */br.getBits(1);
324 unsigned program_stream_info_length = br.getBits(16);
325
326 size_t offset = 0;
327 while (offset < program_stream_info_length) {
328 if (offset + 2 > program_stream_info_length) {
329 return ERROR_MALFORMED;
330 }
331
332 unsigned descriptor_tag = br.getBits(8);
333 unsigned descriptor_length = br.getBits(8);
334
335 ALOGI("found descriptor tag 0x%02x of length %u",
336 descriptor_tag, descriptor_length);
337
338 if (offset + 2 + descriptor_length > program_stream_info_length) {
339 return ERROR_MALFORMED;
340 }
341
342 br.skipBits(8 * descriptor_length);
343
344 offset += 2 + descriptor_length;
345 }
346
347 unsigned elementary_stream_map_length = br.getBits(16);
348
349 offset = 0;
350 while (offset < elementary_stream_map_length) {
351 if (offset + 4 > elementary_stream_map_length) {
352 return ERROR_MALFORMED;
353 }
354
355 unsigned stream_type = br.getBits(8);
356 unsigned elementary_stream_id = br.getBits(8);
357
358 ALOGI("elementary stream id 0x%02x has stream type 0x%02x",
359 elementary_stream_id, stream_type);
360
361 mStreamTypeByESID.add(elementary_stream_id, stream_type);
362
363 unsigned elementary_stream_info_length = br.getBits(16);
364
365 if (offset + 4 + elementary_stream_info_length
366 > elementary_stream_map_length) {
367 return ERROR_MALFORMED;
368 }
369
370 offset += 4 + elementary_stream_info_length;
371 }
372
373 /* unsigned CRC32 = */br.getBits(32);
374
375 mProgramStreamMapValid = true;
376 } else if (stream_id != 0xbe // padding_stream
377 && stream_id != 0xbf // private_stream_2
378 && stream_id != 0xf0 // ECM
379 && stream_id != 0xf1 // EMM
380 && stream_id != 0xff // program_stream_directory
381 && stream_id != 0xf2 // DSMCC
382 && stream_id != 0xf8) { // H.222.1 type E
383 /* unsigned PES_marker_bits = */br.getBits(2); // should be 0x2(hex)
384 /* unsigned PES_scrambling_control = */br.getBits(2);
385 /* unsigned PES_priority = */br.getBits(1);
386 /* unsigned data_alignment_indicator = */br.getBits(1);
387 /* unsigned copyright = */br.getBits(1);
388 /* unsigned original_or_copy = */br.getBits(1);
389
390 unsigned PTS_DTS_flags = br.getBits(2);
391 ALOGV("PTS_DTS_flags = %u", PTS_DTS_flags);
392
393 unsigned ESCR_flag = br.getBits(1);
394 ALOGV("ESCR_flag = %u", ESCR_flag);
395
396 unsigned ES_rate_flag = br.getBits(1);
397 ALOGV("ES_rate_flag = %u", ES_rate_flag);
398
399 unsigned DSM_trick_mode_flag = br.getBits(1);
400 ALOGV("DSM_trick_mode_flag = %u", DSM_trick_mode_flag);
401
402 unsigned additional_copy_info_flag = br.getBits(1);
403 ALOGV("additional_copy_info_flag = %u", additional_copy_info_flag);
404
405 /* unsigned PES_CRC_flag = */br.getBits(1);
406 /* PES_extension_flag = */br.getBits(1);
407
408 unsigned PES_header_data_length = br.getBits(8);
409 ALOGV("PES_header_data_length = %u", PES_header_data_length);
410
411 unsigned optional_bytes_remaining = PES_header_data_length;
412
413 uint64_t PTS = 0, DTS = 0;
414
415 if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
416 if (optional_bytes_remaining < 5u) {
417 return ERROR_MALFORMED;
418 }
419
420 if (br.getBits(4) != PTS_DTS_flags) {
421 return ERROR_MALFORMED;
422 }
423
424 PTS = ((uint64_t)br.getBits(3)) << 30;
425 if (br.getBits(1) != 1u) {
426 return ERROR_MALFORMED;
427 }
428 PTS |= ((uint64_t)br.getBits(15)) << 15;
429 if (br.getBits(1) != 1u) {
430 return ERROR_MALFORMED;
431 }
432 PTS |= br.getBits(15);
433 if (br.getBits(1) != 1u) {
434 return ERROR_MALFORMED;
435 }
436
437 ALOGV("PTS = %" PRIu64, PTS);
438 // ALOGI("PTS = %.2f secs", PTS / 90000.0f);
439
440 optional_bytes_remaining -= 5;
441
442 if (PTS_DTS_flags == 3) {
443 if (optional_bytes_remaining < 5u) {
444 return ERROR_MALFORMED;
445 }
446
447 if (br.getBits(4) != 1u) {
448 return ERROR_MALFORMED;
449 }
450
451 DTS = ((uint64_t)br.getBits(3)) << 30;
452 if (br.getBits(1) != 1u) {
453 return ERROR_MALFORMED;
454 }
455 DTS |= ((uint64_t)br.getBits(15)) << 15;
456 if (br.getBits(1) != 1u) {
457 return ERROR_MALFORMED;
458 }
459 DTS |= br.getBits(15);
460 if (br.getBits(1) != 1u) {
461 return ERROR_MALFORMED;
462 }
463
464 ALOGV("DTS = %" PRIu64, DTS);
465
466 optional_bytes_remaining -= 5;
467 }
468 }
469
470 if (ESCR_flag) {
471 if (optional_bytes_remaining < 6u) {
472 return ERROR_MALFORMED;
473 }
474
475 br.getBits(2);
476
477 uint64_t ESCR = ((uint64_t)br.getBits(3)) << 30;
478 if (br.getBits(1) != 1u) {
479 return ERROR_MALFORMED;
480 }
481 ESCR |= ((uint64_t)br.getBits(15)) << 15;
482 if (br.getBits(1) != 1u) {
483 return ERROR_MALFORMED;
484 }
485 ESCR |= br.getBits(15);
486 if (br.getBits(1) != 1u) {
487 return ERROR_MALFORMED;
488 }
489
490 ALOGV("ESCR = %" PRIu64, ESCR);
491 /* unsigned ESCR_extension = */br.getBits(9);
492
493 if (br.getBits(1) != 1u) {
494 return ERROR_MALFORMED;
495 }
496
497 optional_bytes_remaining -= 6;
498 }
499
500 if (ES_rate_flag) {
501 if (optional_bytes_remaining < 3u) {
502 return ERROR_MALFORMED;
503 }
504
505 if (br.getBits(1) != 1u) {
506 return ERROR_MALFORMED;
507 }
508 /* unsigned ES_rate = */br.getBits(22);
509 if (br.getBits(1) != 1u) {
510 return ERROR_MALFORMED;
511 }
512
513 optional_bytes_remaining -= 3;
514 }
515
516 if (br.numBitsLeft() < optional_bytes_remaining * 8) {
517 return ERROR_MALFORMED;
518 }
519
520 br.skipBits(optional_bytes_remaining * 8);
521
522 // ES data follows.
523
524 if (PES_packet_length < PES_header_data_length + 3) {
525 return ERROR_MALFORMED;
526 }
527
528 unsigned dataLength =
529 PES_packet_length - 3 - PES_header_data_length;
530
531 if (br.numBitsLeft() < dataLength * 8) {
532 ALOGE("PES packet does not carry enough data to contain "
533 "payload. (numBitsLeft = %zu, required = %u)",
534 br.numBitsLeft(), dataLength * 8);
535
536 return ERROR_MALFORMED;
537 }
538
539 if (br.numBitsLeft() < dataLength * 8) {
540 return ERROR_MALFORMED;
541 }
542
543 ssize_t index = mTracks.indexOfKey(stream_id);
544 if (index < 0 && mScanning) {
545 unsigned streamType;
546
547 ssize_t streamTypeIndex;
548 if (mProgramStreamMapValid
549 && (streamTypeIndex =
550 mStreamTypeByESID.indexOfKey(stream_id)) >= 0) {
551 streamType = mStreamTypeByESID.valueAt(streamTypeIndex);
552 } else if ((stream_id & ~0x1f) == 0xc0) {
553 // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7
554 // or ISO/IEC 14496-3 audio
555 streamType = ATSParser::STREAMTYPE_MPEG2_AUDIO;
556 } else if ((stream_id & ~0x0f) == 0xe0) {
557 // ISO/IEC 13818-2 or ISO/IEC 11172-2 or ISO/IEC 14496-2 video
558 streamType = ATSParser::STREAMTYPE_MPEG2_VIDEO;
559 } else {
560 streamType = ATSParser::STREAMTYPE_RESERVED;
561 }
562
563 index = mTracks.add(
564 stream_id, new Track(this, stream_id, streamType));
565 }
566
567 status_t err = OK;
568
569 if (index >= 0) {
570 err =
571 mTracks.editValueAt(index)->appendPESData(
572 PTS_DTS_flags, PTS, DTS, br.data(), dataLength);
573 }
574
575 br.skipBits(dataLength * 8);
576
577 if (err != OK) {
578 return err;
579 }
580 } else if (stream_id == 0xbe) { // padding_stream
581 if (PES_packet_length == 0u) {
582 return ERROR_MALFORMED;
583 }
584 br.skipBits(PES_packet_length * 8);
585 } else {
586 if (PES_packet_length == 0u) {
587 return ERROR_MALFORMED;
588 }
589 br.skipBits(PES_packet_length * 8);
590 }
591
592 return n;
593 }
594
595 ////////////////////////////////////////////////////////////////////////////////
596
Track(MPEG2PSExtractor * extractor,unsigned stream_id,unsigned stream_type)597 MPEG2PSExtractor::Track::Track(
598 MPEG2PSExtractor *extractor, unsigned stream_id, unsigned stream_type)
599 : mExtractor(extractor),
600 mStreamID(stream_id),
601 mStreamType(stream_type),
602 mQueue(NULL) {
603 bool supported = true;
604 ElementaryStreamQueue::Mode mode;
605
606 switch (mStreamType) {
607 case ATSParser::STREAMTYPE_H264:
608 mode = ElementaryStreamQueue::H264;
609 break;
610 case ATSParser::STREAMTYPE_MPEG2_AUDIO_ADTS:
611 mode = ElementaryStreamQueue::AAC;
612 break;
613 case ATSParser::STREAMTYPE_MPEG1_AUDIO:
614 case ATSParser::STREAMTYPE_MPEG2_AUDIO:
615 mode = ElementaryStreamQueue::MPEG_AUDIO;
616 break;
617
618 case ATSParser::STREAMTYPE_MPEG1_VIDEO:
619 case ATSParser::STREAMTYPE_MPEG2_VIDEO:
620 mode = ElementaryStreamQueue::MPEG_VIDEO;
621 break;
622
623 case ATSParser::STREAMTYPE_MPEG4_VIDEO:
624 mode = ElementaryStreamQueue::MPEG4_VIDEO;
625 break;
626
627 default:
628 supported = false;
629 break;
630 }
631
632 if (supported) {
633 mQueue = new ElementaryStreamQueue(mode);
634 } else {
635 ALOGI("unsupported stream ID 0x%02x", stream_id);
636 }
637 }
638
~Track()639 MPEG2PSExtractor::Track::~Track() {
640 delete mQueue;
641 mQueue = NULL;
642 }
643
start()644 media_status_t MPEG2PSExtractor::Track::start() {
645 if (mSource == NULL) {
646 return AMEDIA_ERROR_UNKNOWN;
647 }
648
649 // initialize with one small buffer, but allow growth
650 mBufferGroup->init(1 /* one buffer */, 256 /* buffer size */, 64 /* max number of buffers */);
651
652 if (mSource->start(NULL) == OK) { // AnotherPacketSource::start doesn't use its argument
653 return AMEDIA_OK;
654 }
655 return AMEDIA_ERROR_UNKNOWN;
656 }
657
stop()658 media_status_t MPEG2PSExtractor::Track::stop() {
659 if (mSource == NULL) {
660 return AMEDIA_ERROR_UNKNOWN;
661 }
662
663 if (mSource->stop() == OK) {
664 return AMEDIA_OK;
665 }
666 return AMEDIA_ERROR_UNKNOWN;
667 }
668
669 void copyAMessageToAMediaFormat(AMediaFormat *format, sp<AMessage> msg);
670
getFormat(AMediaFormat * meta)671 media_status_t MPEG2PSExtractor::Track::getFormat(AMediaFormat *meta) {
672 if (mSource == NULL) {
673 return AMEDIA_ERROR_UNKNOWN;
674 }
675
676 sp<MetaData> sourceMeta = mSource->getFormat();
677 sp<AMessage> msg;
678 convertMetaDataToMessage(sourceMeta, &msg);
679 copyAMessageToAMediaFormat(meta, msg);
680 return AMEDIA_OK;
681 }
682
read(MediaBufferHelper ** buffer,const ReadOptions * options)683 media_status_t MPEG2PSExtractor::Track::read(
684 MediaBufferHelper **buffer, const ReadOptions *options) {
685 if (mSource == NULL) {
686 return AMEDIA_ERROR_UNKNOWN;
687 }
688
689 status_t finalResult;
690 while (!mSource->hasBufferAvailable(&finalResult)) {
691 if (finalResult != OK) {
692 return AMEDIA_ERROR_END_OF_STREAM;
693 }
694
695 status_t err = mExtractor->feedMore();
696
697 if (err != OK) {
698 mSource->signalEOS(err);
699 }
700 }
701
702 MediaBufferBase *mbuf;
703 mSource->read(&mbuf, (MediaTrack::ReadOptions*) options);
704 size_t length = mbuf->range_length();
705 MediaBufferHelper *outbuf;
706 mBufferGroup->acquire_buffer(&outbuf, false, length);
707 memcpy(outbuf->data(), mbuf->data(), length);
708 outbuf->set_range(0, length);
709 *buffer = outbuf;
710 MetaDataBase &inMeta = mbuf->meta_data();
711 AMediaFormat *outMeta = outbuf->meta_data();
712 int64_t val64;
713 if (inMeta.findInt64(kKeyTime, &val64)) {
714 AMediaFormat_setInt64(outMeta, AMEDIAFORMAT_KEY_TIME_US, val64);
715 }
716 int32_t val32;
717 if (inMeta.findInt32(kKeyIsSyncFrame, &val32)) {
718 AMediaFormat_setInt32(outMeta, AMEDIAFORMAT_KEY_IS_SYNC_FRAME, val32);
719 }
720 if (inMeta.findInt32(kKeyCryptoMode, &val32)) {
721 AMediaFormat_setInt32(outMeta, AMEDIAFORMAT_KEY_CRYPTO_MODE, val32);
722 }
723 uint32_t bufType;
724 const void *bufData;
725 size_t bufSize;
726 if (inMeta.findData(kKeyCryptoIV, &bufType, &bufData, &bufSize)) {
727 AMediaFormat_setBuffer(outMeta, AMEDIAFORMAT_KEY_CRYPTO_IV, bufData, bufSize);
728 }
729 if (inMeta.findData(kKeyCryptoKey, &bufType, &bufData, &bufSize)) {
730 AMediaFormat_setBuffer(outMeta, AMEDIAFORMAT_KEY_CRYPTO_KEY, bufData, bufSize);
731 }
732 if (inMeta.findData(kKeyPlainSizes, &bufType, &bufData, &bufSize)) {
733 AMediaFormat_setBuffer(outMeta, AMEDIAFORMAT_KEY_CRYPTO_PLAIN_SIZES, bufData, bufSize);
734 }
735 if (inMeta.findData(kKeyEncryptedSizes, &bufType, &bufData, &bufSize)) {
736 AMediaFormat_setBuffer(outMeta, AMEDIAFORMAT_KEY_CRYPTO_ENCRYPTED_SIZES, bufData, bufSize);
737 }
738 if (inMeta.findData(kKeySEI, &bufType, &bufData, &bufSize)) {
739 AMediaFormat_setBuffer(outMeta, AMEDIAFORMAT_KEY_SEI, bufData, bufSize);
740 }
741 if (inMeta.findData(kKeyAudioPresentationInfo, &bufType, &bufData, &bufSize)) {
742 AMediaFormat_setBuffer(outMeta, AMEDIAFORMAT_KEY_AUDIO_PRESENTATION_INFO, bufData, bufSize);
743 }
744 mbuf->release();
745 return AMEDIA_OK;
746 }
747
appendPESData(unsigned PTS_DTS_flags,uint64_t PTS,uint64_t,const uint8_t * data,size_t size)748 status_t MPEG2PSExtractor::Track::appendPESData(
749 unsigned PTS_DTS_flags,
750 uint64_t PTS, uint64_t /* DTS */,
751 const uint8_t *data, size_t size) {
752 if (mQueue == NULL) {
753 return OK;
754 }
755
756 int64_t timeUs;
757 if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
758 timeUs = (PTS * 100) / 9;
759 } else {
760 timeUs = 0;
761 }
762
763 status_t err = mQueue->appendData(data, size, timeUs);
764
765 if (err != OK) {
766 return err;
767 }
768
769 sp<ABuffer> accessUnit;
770 while ((accessUnit = mQueue->dequeueAccessUnit()) != NULL) {
771 if (mSource == NULL) {
772 sp<MetaData> meta = mQueue->getFormat();
773
774 if (meta != NULL) {
775 ALOGV("Stream ID 0x%02x now has data.", mStreamID);
776
777 mSource = new AnotherPacketSource(meta);
778 mSource->queueAccessUnit(accessUnit);
779 }
780 } else if (mQueue->getFormat() != NULL) {
781 mSource->queueAccessUnit(accessUnit);
782 }
783 }
784
785 return OK;
786 }
787
788 ////////////////////////////////////////////////////////////////////////////////
789
WrappedTrack(MPEG2PSExtractor * extractor,Track * track)790 MPEG2PSExtractor::WrappedTrack::WrappedTrack(
791 MPEG2PSExtractor *extractor, Track *track)
792 : mExtractor(extractor),
793 mTrack(track) {
794 }
795
~WrappedTrack()796 MPEG2PSExtractor::WrappedTrack::~WrappedTrack() {
797 }
798
start()799 media_status_t MPEG2PSExtractor::WrappedTrack::start() {
800 delete mTrack->mBufferGroup;
801 mTrack->mBufferGroup = mBufferGroup;
802 mBufferGroup = nullptr;
803 return mTrack->start();
804 }
805
stop()806 media_status_t MPEG2PSExtractor::WrappedTrack::stop() {
807 return mTrack->stop();
808 }
809
getFormat(AMediaFormat * meta)810 media_status_t MPEG2PSExtractor::WrappedTrack::getFormat(AMediaFormat *meta) {
811 return mTrack->getFormat(meta);
812 }
813
read(MediaBufferHelper ** buffer,const ReadOptions * options)814 media_status_t MPEG2PSExtractor::WrappedTrack::read(
815 MediaBufferHelper **buffer, const ReadOptions *options) {
816 return mTrack->read(buffer, options);
817 }
818
819 ////////////////////////////////////////////////////////////////////////////////
820
SniffMPEG2PS(DataSourceHelper * source,float * confidence)821 bool SniffMPEG2PS(
822 DataSourceHelper *source, float *confidence) {
823 uint8_t header[5];
824 if (source->readAt(0, header, sizeof(header)) < (ssize_t)sizeof(header)) {
825 return false;
826 }
827
828 if (memcmp("\x00\x00\x01\xba", header, 4) || (header[4] >> 6) != 1) {
829 return false;
830 }
831
832 *confidence = 0.25f; // Slightly larger than .mp3 extractor's confidence
833
834 return true;
835 }
836
837 } // namespace android
838