ATSParser.cpp revision 6a63a939601645404fd98f58c19cc38ca818d99e
1/*
2 * Copyright (C) 2010 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 "ATSParser"
19#include <utils/Log.h>
20
21#include "ATSParser.h"
22
23#include "AnotherPacketSource.h"
24#include "ESQueue.h"
25#include "include/avc_utils.h"
26
27#include <media/stagefright/foundation/ABitReader.h>
28#include <media/stagefright/foundation/ABuffer.h>
29#include <media/stagefright/foundation/ADebug.h>
30#include <media/stagefright/foundation/AMessage.h>
31#include <media/stagefright/foundation/hexdump.h>
32#include <media/stagefright/MediaDefs.h>
33#include <media/stagefright/MediaErrors.h>
34#include <media/stagefright/MetaData.h>
35#include <utils/KeyedVector.h>
36
37namespace android {
38
39// I want the expression "y" evaluated even if verbose logging is off.
40#define MY_LOGV(x, y) \
41    do { unsigned tmp = y; LOGV(x, tmp); } while (0)
42
43static const size_t kTSPacketSize = 188;
44
45struct ATSParser::Program : public RefBase {
46    Program(unsigned programMapPID);
47
48    bool parsePID(
49            unsigned pid, unsigned payload_unit_start_indicator,
50            ABitReader *br);
51
52    sp<MediaSource> getSource(SourceType type);
53
54private:
55    unsigned mProgramMapPID;
56    KeyedVector<unsigned, sp<Stream> > mStreams;
57
58    void parseProgramMap(ABitReader *br);
59
60    DISALLOW_EVIL_CONSTRUCTORS(Program);
61};
62
63struct ATSParser::Stream : public RefBase {
64    Stream(unsigned elementaryPID, unsigned streamType);
65
66    void parse(
67            unsigned payload_unit_start_indicator,
68            ABitReader *br);
69
70    sp<MediaSource> getSource(SourceType type);
71
72protected:
73    virtual ~Stream();
74
75private:
76    unsigned mElementaryPID;
77    unsigned mStreamType;
78
79    sp<ABuffer> mBuffer;
80    sp<AnotherPacketSource> mSource;
81    bool mPayloadStarted;
82
83    ElementaryStreamQueue mQueue;
84
85    void flush();
86    void parsePES(ABitReader *br);
87
88    void onPayloadData(
89            unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
90            const uint8_t *data, size_t size);
91
92    void extractAACFrames(const sp<ABuffer> &buffer);
93
94    DISALLOW_EVIL_CONSTRUCTORS(Stream);
95};
96
97////////////////////////////////////////////////////////////////////////////////
98
99ATSParser::Program::Program(unsigned programMapPID)
100    : mProgramMapPID(programMapPID) {
101}
102
103bool ATSParser::Program::parsePID(
104        unsigned pid, unsigned payload_unit_start_indicator,
105        ABitReader *br) {
106    if (pid == mProgramMapPID) {
107        if (payload_unit_start_indicator) {
108            unsigned skip = br->getBits(8);
109            br->skipBits(skip * 8);
110        }
111
112        parseProgramMap(br);
113        return true;
114    }
115
116    ssize_t index = mStreams.indexOfKey(pid);
117    if (index < 0) {
118        return false;
119    }
120
121    mStreams.editValueAt(index)->parse(
122            payload_unit_start_indicator, br);
123
124    return true;
125}
126
127void ATSParser::Program::parseProgramMap(ABitReader *br) {
128    unsigned table_id = br->getBits(8);
129    LOGV("  table_id = %u", table_id);
130    CHECK_EQ(table_id, 0x02u);
131
132    unsigned section_syntax_indicator = br->getBits(1);
133    LOGV("  section_syntax_indicator = %u", section_syntax_indicator);
134    CHECK_EQ(section_syntax_indicator, 1u);
135
136    CHECK_EQ(br->getBits(1), 0u);
137    MY_LOGV("  reserved = %u", br->getBits(2));
138
139    unsigned section_length = br->getBits(12);
140    LOGV("  section_length = %u", section_length);
141    CHECK((section_length & 0xc00) == 0);
142    CHECK_LE(section_length, 1021u);
143
144    MY_LOGV("  program_number = %u", br->getBits(16));
145    MY_LOGV("  reserved = %u", br->getBits(2));
146    MY_LOGV("  version_number = %u", br->getBits(5));
147    MY_LOGV("  current_next_indicator = %u", br->getBits(1));
148    MY_LOGV("  section_number = %u", br->getBits(8));
149    MY_LOGV("  last_section_number = %u", br->getBits(8));
150    MY_LOGV("  reserved = %u", br->getBits(3));
151    MY_LOGV("  PCR_PID = 0x%04x", br->getBits(13));
152    MY_LOGV("  reserved = %u", br->getBits(4));
153
154    unsigned program_info_length = br->getBits(12);
155    LOGV("  program_info_length = %u", program_info_length);
156    CHECK((program_info_length & 0xc00) == 0);
157
158    br->skipBits(program_info_length * 8);  // skip descriptors
159
160    // infoBytesRemaining is the number of bytes that make up the
161    // variable length section of ES_infos. It does not include the
162    // final CRC.
163    size_t infoBytesRemaining = section_length - 9 - program_info_length - 4;
164
165    while (infoBytesRemaining > 0) {
166        CHECK_GE(infoBytesRemaining, 5u);
167
168        unsigned streamType = br->getBits(8);
169        LOGV("    stream_type = 0x%02x", streamType);
170
171        MY_LOGV("    reserved = %u", br->getBits(3));
172
173        unsigned elementaryPID = br->getBits(13);
174        LOGV("    elementary_PID = 0x%04x", elementaryPID);
175
176        MY_LOGV("    reserved = %u", br->getBits(4));
177
178        unsigned ES_info_length = br->getBits(12);
179        LOGV("    ES_info_length = %u", ES_info_length);
180        CHECK((ES_info_length & 0xc00) == 0);
181
182        CHECK_GE(infoBytesRemaining - 5, ES_info_length);
183
184#if 0
185        br->skipBits(ES_info_length * 8);  // skip descriptors
186#else
187        unsigned info_bytes_remaining = ES_info_length;
188        while (info_bytes_remaining >= 2) {
189            MY_LOGV("      tag = 0x%02x", br->getBits(8));
190
191            unsigned descLength = br->getBits(8);
192            LOGV("      len = %u", descLength);
193
194            CHECK_GE(info_bytes_remaining, 2 + descLength);
195
196            br->skipBits(descLength * 8);
197
198            info_bytes_remaining -= descLength + 2;
199        }
200        CHECK_EQ(info_bytes_remaining, 0u);
201#endif
202
203        ssize_t index = mStreams.indexOfKey(elementaryPID);
204#if 0  // XXX revisit
205        CHECK_LT(index, 0);
206        mStreams.add(elementaryPID, new Stream(elementaryPID, streamType));
207#else
208        if (index < 0) {
209            mStreams.add(elementaryPID, new Stream(elementaryPID, streamType));
210        }
211#endif
212
213        infoBytesRemaining -= 5 + ES_info_length;
214    }
215
216    CHECK_EQ(infoBytesRemaining, 0u);
217
218    MY_LOGV("  CRC = 0x%08x", br->getBits(32));
219}
220
221sp<MediaSource> ATSParser::Program::getSource(SourceType type) {
222    for (size_t i = 0; i < mStreams.size(); ++i) {
223        sp<MediaSource> source = mStreams.editValueAt(i)->getSource(type);
224        if (source != NULL) {
225            return source;
226        }
227    }
228
229    return NULL;
230}
231
232////////////////////////////////////////////////////////////////////////////////
233
234ATSParser::Stream::Stream(unsigned elementaryPID, unsigned streamType)
235    : mElementaryPID(elementaryPID),
236      mStreamType(streamType),
237      mBuffer(new ABuffer(128 * 1024)),
238      mPayloadStarted(false),
239      mQueue(streamType == 0x1b
240              ? ElementaryStreamQueue::H264 : ElementaryStreamQueue::AAC) {
241    mBuffer->setRange(0, 0);
242}
243
244ATSParser::Stream::~Stream() {
245}
246
247void ATSParser::Stream::parse(
248        unsigned payload_unit_start_indicator, ABitReader *br) {
249    if (payload_unit_start_indicator) {
250        if (mPayloadStarted) {
251            // Otherwise we run the danger of receiving the trailing bytes
252            // of a PES packet that we never saw the start of and assuming
253            // we have a a complete PES packet.
254
255            flush();
256        }
257
258        mPayloadStarted = true;
259    }
260
261    if (!mPayloadStarted) {
262        return;
263    }
264
265    size_t payloadSizeBits = br->numBitsLeft();
266    CHECK((payloadSizeBits % 8) == 0);
267
268    CHECK_LE(mBuffer->size() + payloadSizeBits / 8, mBuffer->capacity());
269
270    memcpy(mBuffer->data() + mBuffer->size(), br->data(), payloadSizeBits / 8);
271    mBuffer->setRange(0, mBuffer->size() + payloadSizeBits / 8);
272}
273
274void ATSParser::Stream::parsePES(ABitReader *br) {
275    unsigned packet_startcode_prefix = br->getBits(24);
276
277    LOGV("packet_startcode_prefix = 0x%08x", packet_startcode_prefix);
278
279    CHECK_EQ(packet_startcode_prefix, 0x000001u);
280
281    unsigned stream_id = br->getBits(8);
282    LOGV("stream_id = 0x%02x", stream_id);
283
284    unsigned PES_packet_length = br->getBits(16);
285    LOGV("PES_packet_length = %u", PES_packet_length);
286
287    if (stream_id != 0xbc  // program_stream_map
288            && stream_id != 0xbe  // padding_stream
289            && stream_id != 0xbf  // private_stream_2
290            && stream_id != 0xf0  // ECM
291            && stream_id != 0xf1  // EMM
292            && stream_id != 0xff  // program_stream_directory
293            && stream_id != 0xf2  // DSMCC
294            && stream_id != 0xf8) {  // H.222.1 type E
295        CHECK_EQ(br->getBits(2), 2u);
296
297        MY_LOGV("PES_scrambling_control = %u", br->getBits(2));
298        MY_LOGV("PES_priority = %u", br->getBits(1));
299        MY_LOGV("data_alignment_indicator = %u", br->getBits(1));
300        MY_LOGV("copyright = %u", br->getBits(1));
301        MY_LOGV("original_or_copy = %u", br->getBits(1));
302
303        unsigned PTS_DTS_flags = br->getBits(2);
304        LOGV("PTS_DTS_flags = %u", PTS_DTS_flags);
305
306        unsigned ESCR_flag = br->getBits(1);
307        LOGV("ESCR_flag = %u", ESCR_flag);
308
309        unsigned ES_rate_flag = br->getBits(1);
310        LOGV("ES_rate_flag = %u", ES_rate_flag);
311
312        unsigned DSM_trick_mode_flag = br->getBits(1);
313        LOGV("DSM_trick_mode_flag = %u", DSM_trick_mode_flag);
314
315        unsigned additional_copy_info_flag = br->getBits(1);
316        LOGV("additional_copy_info_flag = %u", additional_copy_info_flag);
317
318        MY_LOGV("PES_CRC_flag = %u", br->getBits(1));
319        MY_LOGV("PES_extension_flag = %u", br->getBits(1));
320
321        unsigned PES_header_data_length = br->getBits(8);
322        LOGV("PES_header_data_length = %u", PES_header_data_length);
323
324        unsigned optional_bytes_remaining = PES_header_data_length;
325
326        uint64_t PTS = 0, DTS = 0;
327
328        if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
329            CHECK_GE(optional_bytes_remaining, 5u);
330
331            CHECK_EQ(br->getBits(4), PTS_DTS_flags);
332
333            PTS = ((uint64_t)br->getBits(3)) << 30;
334            CHECK_EQ(br->getBits(1), 1u);
335            PTS |= ((uint64_t)br->getBits(15)) << 15;
336            CHECK_EQ(br->getBits(1), 1u);
337            PTS |= br->getBits(15);
338            CHECK_EQ(br->getBits(1), 1u);
339
340            LOGV("PTS = %llu", PTS);
341            // LOGI("PTS = %.2f secs", PTS / 90000.0f);
342
343            optional_bytes_remaining -= 5;
344
345            if (PTS_DTS_flags == 3) {
346                CHECK_GE(optional_bytes_remaining, 5u);
347
348                CHECK_EQ(br->getBits(4), 1u);
349
350                DTS = ((uint64_t)br->getBits(3)) << 30;
351                CHECK_EQ(br->getBits(1), 1u);
352                DTS |= ((uint64_t)br->getBits(15)) << 15;
353                CHECK_EQ(br->getBits(1), 1u);
354                DTS |= br->getBits(15);
355                CHECK_EQ(br->getBits(1), 1u);
356
357                LOGV("DTS = %llu", DTS);
358
359                optional_bytes_remaining -= 5;
360            }
361        }
362
363        if (ESCR_flag) {
364            CHECK_GE(optional_bytes_remaining, 6u);
365
366            br->getBits(2);
367
368            uint64_t ESCR = ((uint64_t)br->getBits(3)) << 30;
369            CHECK_EQ(br->getBits(1), 1u);
370            ESCR |= ((uint64_t)br->getBits(15)) << 15;
371            CHECK_EQ(br->getBits(1), 1u);
372            ESCR |= br->getBits(15);
373            CHECK_EQ(br->getBits(1), 1u);
374
375            LOGV("ESCR = %llu", ESCR);
376            MY_LOGV("ESCR_extension = %u", br->getBits(9));
377
378            CHECK_EQ(br->getBits(1), 1u);
379
380            optional_bytes_remaining -= 6;
381        }
382
383        if (ES_rate_flag) {
384            CHECK_GE(optional_bytes_remaining, 3u);
385
386            CHECK_EQ(br->getBits(1), 1u);
387            MY_LOGV("ES_rate = %u", br->getBits(22));
388            CHECK_EQ(br->getBits(1), 1u);
389
390            optional_bytes_remaining -= 3;
391        }
392
393        br->skipBits(optional_bytes_remaining * 8);
394
395        // ES data follows.
396
397        if (PES_packet_length != 0) {
398            CHECK_GE(PES_packet_length, PES_header_data_length + 3);
399
400            unsigned dataLength =
401                PES_packet_length - 3 - PES_header_data_length;
402
403            CHECK_GE(br->numBitsLeft(), dataLength * 8);
404
405            onPayloadData(
406                    PTS_DTS_flags, PTS, DTS, br->data(), dataLength);
407
408            br->skipBits(dataLength * 8);
409        } else {
410            onPayloadData(
411                    PTS_DTS_flags, PTS, DTS,
412                    br->data(), br->numBitsLeft() / 8);
413
414            size_t payloadSizeBits = br->numBitsLeft();
415            CHECK((payloadSizeBits % 8) == 0);
416
417            LOGV("There's %d bytes of payload.", payloadSizeBits / 8);
418        }
419    } else if (stream_id == 0xbe) {  // padding_stream
420        CHECK_NE(PES_packet_length, 0u);
421        br->skipBits(PES_packet_length * 8);
422    } else {
423        CHECK_NE(PES_packet_length, 0u);
424        br->skipBits(PES_packet_length * 8);
425    }
426}
427
428void ATSParser::Stream::flush() {
429    if (mBuffer->size() == 0) {
430        return;
431    }
432
433    LOGV("flushing stream 0x%04x size = %d", mElementaryPID, mBuffer->size());
434
435    ABitReader br(mBuffer->data(), mBuffer->size());
436    parsePES(&br);
437
438    mBuffer->setRange(0, 0);
439}
440
441void ATSParser::Stream::onPayloadData(
442        unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
443        const uint8_t *data, size_t size) {
444    LOGV("onPayloadData mStreamType=0x%02x", mStreamType);
445
446    CHECK(PTS_DTS_flags == 2 || PTS_DTS_flags == 3);
447    int64_t timeUs = (PTS * 100) / 9;
448
449    status_t err = mQueue.appendData(data, size, timeUs);
450    CHECK_EQ(err, (status_t)OK);
451
452    sp<ABuffer> accessUnit;
453    while ((accessUnit = mQueue.dequeueAccessUnit()) != NULL) {
454        if (mSource == NULL) {
455            sp<MetaData> meta = mQueue.getFormat();
456
457            if (meta != NULL) {
458                LOGV("created source!");
459                mSource = new AnotherPacketSource(meta);
460                mSource->queueAccessUnit(accessUnit);
461            }
462        } else {
463            mSource->queueAccessUnit(accessUnit);
464        }
465    }
466}
467
468sp<MediaSource> ATSParser::Stream::getSource(SourceType type) {
469    if ((type == AVC_VIDEO && mStreamType == 0x1b)
470        || (type == MPEG2ADTS_AUDIO && mStreamType == 0x0f)) {
471        return mSource;
472    }
473
474    return NULL;
475}
476
477////////////////////////////////////////////////////////////////////////////////
478
479ATSParser::ATSParser() {
480}
481
482ATSParser::~ATSParser() {
483}
484
485void ATSParser::feedTSPacket(const void *data, size_t size) {
486    CHECK_EQ(size, kTSPacketSize);
487
488    ABitReader br((const uint8_t *)data, kTSPacketSize);
489    parseTS(&br);
490}
491
492void ATSParser::parseProgramAssociationTable(ABitReader *br) {
493    unsigned table_id = br->getBits(8);
494    LOGV("  table_id = %u", table_id);
495    CHECK_EQ(table_id, 0x00u);
496
497    unsigned section_syntax_indictor = br->getBits(1);
498    LOGV("  section_syntax_indictor = %u", section_syntax_indictor);
499    CHECK_EQ(section_syntax_indictor, 1u);
500
501    CHECK_EQ(br->getBits(1), 0u);
502    MY_LOGV("  reserved = %u", br->getBits(2));
503
504    unsigned section_length = br->getBits(12);
505    LOGV("  section_length = %u", section_length);
506    CHECK((section_length & 0xc00) == 0);
507
508    MY_LOGV("  transport_stream_id = %u", br->getBits(16));
509    MY_LOGV("  reserved = %u", br->getBits(2));
510    MY_LOGV("  version_number = %u", br->getBits(5));
511    MY_LOGV("  current_next_indicator = %u", br->getBits(1));
512    MY_LOGV("  section_number = %u", br->getBits(8));
513    MY_LOGV("  last_section_number = %u", br->getBits(8));
514
515    size_t numProgramBytes = (section_length - 5 /* header */ - 4 /* crc */);
516    CHECK_EQ((numProgramBytes % 4), 0u);
517
518    for (size_t i = 0; i < numProgramBytes / 4; ++i) {
519        unsigned program_number = br->getBits(16);
520        LOGV("    program_number = %u", program_number);
521
522        MY_LOGV("    reserved = %u", br->getBits(3));
523
524        if (program_number == 0) {
525            MY_LOGV("    network_PID = 0x%04x", br->getBits(13));
526        } else {
527            unsigned programMapPID = br->getBits(13);
528
529            LOGV("    program_map_PID = 0x%04x", programMapPID);
530
531            mPrograms.push(new Program(programMapPID));
532        }
533    }
534
535    MY_LOGV("  CRC = 0x%08x", br->getBits(32));
536}
537
538void ATSParser::parsePID(
539        ABitReader *br, unsigned PID,
540        unsigned payload_unit_start_indicator) {
541    if (PID == 0) {
542        if (payload_unit_start_indicator) {
543            unsigned skip = br->getBits(8);
544            br->skipBits(skip * 8);
545        }
546        parseProgramAssociationTable(br);
547        return;
548    }
549
550    bool handled = false;
551    for (size_t i = 0; i < mPrograms.size(); ++i) {
552        if (mPrograms.editItemAt(i)->parsePID(
553                    PID, payload_unit_start_indicator, br)) {
554            handled = true;
555            break;
556        }
557    }
558
559    if (!handled) {
560        LOGV("PID 0x%04x not handled.", PID);
561    }
562}
563
564void ATSParser::parseAdaptationField(ABitReader *br) {
565    unsigned adaptation_field_length = br->getBits(8);
566    if (adaptation_field_length > 0) {
567        br->skipBits(adaptation_field_length * 8);  // XXX
568    }
569}
570
571void ATSParser::parseTS(ABitReader *br) {
572    LOGV("---");
573
574    unsigned sync_byte = br->getBits(8);
575    CHECK_EQ(sync_byte, 0x47u);
576
577    MY_LOGV("transport_error_indicator = %u", br->getBits(1));
578
579    unsigned payload_unit_start_indicator = br->getBits(1);
580    LOGV("payload_unit_start_indicator = %u", payload_unit_start_indicator);
581
582    MY_LOGV("transport_priority = %u", br->getBits(1));
583
584    unsigned PID = br->getBits(13);
585    LOGV("PID = 0x%04x", PID);
586
587    MY_LOGV("transport_scrambling_control = %u", br->getBits(2));
588
589    unsigned adaptation_field_control = br->getBits(2);
590    LOGV("adaptation_field_control = %u", adaptation_field_control);
591
592    unsigned continuity_counter = br->getBits(4);
593    LOGV("continuity_counter = %u", continuity_counter);
594
595    // LOGI("PID = 0x%04x, continuity_counter = %u", PID, continuity_counter);
596
597    if (adaptation_field_control == 2 || adaptation_field_control == 3) {
598        parseAdaptationField(br);
599    }
600
601    if (adaptation_field_control == 1 || adaptation_field_control == 3) {
602        parsePID(br, PID, payload_unit_start_indicator);
603    }
604}
605
606sp<MediaSource> ATSParser::getSource(SourceType type) {
607    for (size_t i = 0; i < mPrograms.size(); ++i) {
608        sp<MediaSource> source = mPrograms.editItemAt(i)->getSource(type);
609
610        if (source != NULL) {
611            return source;
612        }
613    }
614
615    return NULL;
616}
617
618}  // namespace android
619