diff options
-rw-r--r-- | include/media/stagefright/MPEG2TSWriter.h | 3 | ||||
-rw-r--r-- | media/libstagefright/MPEG2TSWriter.cpp | 43 |
2 files changed, 40 insertions, 6 deletions
diff --git a/include/media/stagefright/MPEG2TSWriter.h b/include/media/stagefright/MPEG2TSWriter.h index 98fcf1d..2e2922e 100644 --- a/include/media/stagefright/MPEG2TSWriter.h +++ b/include/media/stagefright/MPEG2TSWriter.h @@ -71,6 +71,7 @@ private: int64_t mNumTSPacketsBeforeMeta; int mPATContinuityCounter; int mPMTContinuityCounter; + uint32_t mCrcTable[256]; void init(); @@ -78,6 +79,8 @@ private: void writeProgramAssociationTable(); void writeProgramMap(); void writeAccessUnit(int32_t sourceIndex, const sp<ABuffer> &buffer); + void initCrcTable(); + uint32_t crc32(const uint8_t *start, size_t length); ssize_t internalWrite(const void *data, size_t size); status_t reset(); diff --git a/media/libstagefright/MPEG2TSWriter.cpp b/media/libstagefright/MPEG2TSWriter.cpp index 62c8aab..7de1304 100644 --- a/media/libstagefright/MPEG2TSWriter.cpp +++ b/media/libstagefright/MPEG2TSWriter.cpp @@ -28,6 +28,7 @@ #include <media/stagefright/MediaSource.h> #include <media/stagefright/MetaData.h> #include <media/stagefright/Utils.h> +#include <arpa/inet.h> #include "include/ESDS.h" @@ -508,6 +509,8 @@ MPEG2TSWriter::MPEG2TSWriter( void MPEG2TSWriter::init() { CHECK(mFile != NULL || mWriteFunc != NULL); + initCrcTable(); + mLooper = new ALooper; mLooper->setName("MPEG2TSWriter"); @@ -735,7 +738,7 @@ void MPEG2TSWriter::writeProgramAssociationTable() { }; sp<ABuffer> buffer = new ABuffer(188); - memset(buffer->data(), 0, buffer->size()); + memset(buffer->data(), 0xff, buffer->size()); memcpy(buffer->data(), kData, sizeof(kData)); if (++mPATContinuityCounter == 16) { @@ -743,6 +746,9 @@ void MPEG2TSWriter::writeProgramAssociationTable() { } buffer->data()[3] |= mPATContinuityCounter; + uint32_t crc = htonl(crc32(&buffer->data()[5], 12)); + memcpy(&buffer->data()[17], &crc, sizeof(crc)); + CHECK_EQ(internalWrite(buffer->data(), buffer->size()), buffer->size()); } @@ -789,7 +795,7 @@ void MPEG2TSWriter::writeProgramMap() { }; sp<ABuffer> buffer = new ABuffer(188); - memset(buffer->data(), 0, buffer->size()); + memset(buffer->data(), 0xff, buffer->size()); memcpy(buffer->data(), kData, sizeof(kData)); if (++mPMTContinuityCounter == 16) { @@ -816,10 +822,8 @@ void MPEG2TSWriter::writeProgramMap() { *ptr++ = 0x00; } - *ptr++ = 0x00; - *ptr++ = 0x00; - *ptr++ = 0x00; - *ptr++ = 0x00; + uint32_t crc = htonl(crc32(&buffer->data()[5], 12+mSources.size()*5)); + memcpy(&buffer->data()[17+mSources.size()*5], &crc, sizeof(crc)); CHECK_EQ(internalWrite(buffer->data(), buffer->size()), buffer->size()); } @@ -966,6 +970,33 @@ void MPEG2TSWriter::writeTS() { } } +void MPEG2TSWriter::initCrcTable() { + uint32_t poly = 0x04C11DB7; + + for (int i = 0; i < 256; i++) { + uint32_t crc = i << 24; + for (int j = 0; j < 8; j++) { + crc = (crc << 1) ^ ((crc & 0x80000000) ? (poly) : 0); + } + mCrcTable[i] = crc; + } +} + +/** + * Compute CRC32 checksum for buffer starting at offset start and for length + * bytes. + */ +uint32_t MPEG2TSWriter::crc32(const uint8_t *p_start, size_t length) { + uint32_t crc = 0xFFFFFFFF; + const uint8_t *p; + + for (p = p_start; p < p_start + length; p++) { + crc = (crc << 8) ^ mCrcTable[((crc >> 24) ^ *p) & 0xFF]; + } + + return crc; +} + ssize_t MPEG2TSWriter::internalWrite(const void *data, size_t size) { if (mFile != NULL) { return fwrite(data, 1, size, mFile); |