From 47de4da34feafbe22d8e375c47ad9aaf9a190d77 Mon Sep 17 00:00:00 2001 From: Mark Calderbank Date: Mon, 15 Aug 2005 00:33:36 +0000 Subject: [PATCH] Fix some demuxer bugs --- demuxer.cc | 44 +++++++++++++++++++++++++++++++++++++++++++- demuxer.h | 3 +++ stream.cc | 30 +++++++++++++++++------------- stream.h | 1 - 4 files changed, 63 insertions(+), 15 deletions(-) diff --git a/demuxer.cc b/demuxer.cc index 7401482..6a0c291 100644 --- a/demuxer.cc +++ b/demuxer.cc @@ -157,12 +157,16 @@ int Demuxer::put(UCHAR* buf, int len) case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX: parsed = parse_audio_frame(len, &full); break; + case FRAMETYPE_PRIVATE_1: + parsed = parse_private1_frame(len, &full); + break; } ret += parsed; len -= parsed; if (full) // We have to exit early. break; // out of while loop } -// Log::getInstance()->log("Demuxer", Log::DEBUG, "Put %d; took %d", ret + len, ret); + Log::getInstance()->log( + "Demuxer", Log::DEBUG, "Put %d; took %d", ret + len, ret); return ret; } @@ -207,6 +211,7 @@ int Demuxer::parse_find_frame(int len) break; case FRAMETYPE_VID0 ... FRAMETYPE_VIDMAX: case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX: + case FRAMETYPE_PRIVATE_1: state_frametype = byte; return ret; } @@ -412,6 +417,43 @@ int Demuxer::parse_audio_frame(int len, int* full) return ret; } +int Demuxer::parse_private1_frame(int len, int* full) +{ + int ret = 0; // return number of bytes consumed + int bytes_remaining; + + switch(state_framepos) + { + case 0: // Brand new frame. Set initial states. + // Get MSB of frame length and copy to local frame. + frame_length = *inbuf << 8; + ++inbuf; ++state_framepos; ++ret; --len; + if (len == 0) return ret; + // FALL THROUGH TO NEXT BYTE IN STREAM + case 1: // Get LSB of frame length and copy to local frame. + frame_length += *inbuf; + local_frame[5] = *inbuf; + ++inbuf; ++state_framepos; ++ret; --len; + if (len == 0) return ret; + } + // We are in the frame data + bytes_remaining = 2 + frame_length - state_framepos; + // Temporary - just discard the frame. + if (len >= bytes_remaining) + { + inbuf += bytes_remaining; + ret += bytes_remaining; + state_frametype = state_framepos = 0; + return ret; + } + else + { + inbuf += len; ret += len; + state_framepos += len; + return ret; + } +} + void Demuxer::parse_video_details(UCHAR* buf, int len) { UCHAR byte; diff --git a/demuxer.h b/demuxer.h index 2193c8e..6fe6368 100644 --- a/demuxer.h +++ b/demuxer.h @@ -70,6 +70,7 @@ class Demuxer int parse_find_frame(int len); int parse_video_frame(int len, int* full); int parse_audio_frame(int len, int* full); + int parse_private1_frame(int len, int* full); void parse_video_details(UCHAR* buf, int len); UCHAR* local_frame; @@ -78,6 +79,8 @@ class Demuxer enum FRAMETYPE { + FRAMETYPE_PRIVATE_1 = 0xbd, + FRAMETYPE_AUD0 = 0xc0, FRAMETYPE_AUD1, FRAMETYPE_AUD2, diff --git a/stream.cc b/stream.cc index 116d8ea..5ff40cd 100644 --- a/stream.cc +++ b/stream.cc @@ -44,7 +44,6 @@ int Stream::init(int bufsize) if (!outbuf) return 1; bufferSize = bufsize; bufferHead = bufferTail = 0; - full = 0; initted = 1; return 0; } @@ -52,14 +51,14 @@ int Stream::init(int bufsize) void Stream::flush() { bufferHead = bufferTail = 0; - full = 0; } int Stream::put(UCHAR* inbuf, int len) { int ret = 0; int localTail = bufferTail; - if (len == 0 || (full)) return ret; + if (localTail == 0) localTail = bufferSize; + if (len == 0) return ret; if (bufferHead >= localTail) { @@ -78,13 +77,15 @@ int Stream::put(UCHAR* inbuf, int len) ret += (bufferSize - bufferHead); len -= (bufferSize - bufferHead); bufferHead = 0; - if (localTail == 0) full = 1; - if (len == 0 || (full)) return ret; + if (len == 0) return ret; } } + + if (localTail - bufferHead == 1) return ret; // Full + // We can advance the head up to the tail // If we reach it, we are full. - if (len < (localTail - bufferHead)) + if (len < (localTail - bufferHead - 1)) { // We'll fall short of the tail. memcpy(outbuf + bufferHead, inbuf, len); @@ -93,9 +94,9 @@ int Stream::put(UCHAR* inbuf, int len) } else { // We'll hit the tail. - memcpy(outbuf + bufferHead, inbuf, (localTail - bufferHead)); - ret += (localTail - bufferHead); - bufferHead = localTail; full = 1; + memcpy(outbuf + bufferHead, inbuf, (localTail - bufferHead - 1)); + ret += (localTail - bufferHead - 1); + bufferHead = localTail - 1; return ret; } } @@ -105,21 +106,24 @@ int Stream::drain(int fd) int ret = 0; int localHead = bufferHead; int written; - if (localHead == bufferTail && (!full)) return ret; - if (localHead <= bufferTail) + if (localHead < bufferTail) { // Drain to the end of the buffer written = write(fd, outbuf + bufferTail, (bufferSize - bufferTail)); if (written < 0) return ret; - full = 0; ret += written; if (written == (bufferSize - bufferTail)) bufferTail = 0; else + { bufferTail += written; - if (localHead == 0) return ret; + return ret; + } } + + if (localHead == bufferTail) return ret; // Empty + written = write(fd, outbuf + bufferTail, (localHead - bufferTail)); if (written < 0) return ret; ret += written; diff --git a/stream.h b/stream.h index bb3b572..9abb7c3 100644 --- a/stream.h +++ b/stream.h @@ -44,7 +44,6 @@ class Stream int bufferSize; int bufferHead; int bufferTail; - int full; }; #endif -- 2.39.5