From db0b829fbd0a2c1c8109cee146496d0b804ba664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B9=20=D0=93=D1=83=D0=B1=D0=B8=D1=87?= Date: Mon, 14 Jan 2019 17:50:41 +0300 Subject: [PATCH] still no good, but a bit cleaned up --- decoder.cpp | 113 ++++++++++++++++------------------------------------ decoder.h | 5 ++- index.html | 3 +- 3 files changed, 41 insertions(+), 80 deletions(-) diff --git a/decoder.cpp b/decoder.cpp index 7485b4b..1a18e70 100644 --- a/decoder.cpp +++ b/decoder.cpp @@ -8,7 +8,9 @@ channels(0), cachedLength(0), samplesPerFrame(0), glue(new uint8_t[GLUE_LENGTH]), -lastSync(-1), +cachedNext(NULL), +cachedThis(NULL), +cachedError(MAD_ERROR_NONE), cached(false), synth(), stream(), @@ -107,7 +109,6 @@ emscripten::val Decoder::decode(uint32_t count) int res = mad_frame_decode(&frame, &stream); if (res == 0) { - lastSync = stream.bufend - stream.this_frame; ++success; } else { if (MAD_RECOVERABLE(stream.error)) { @@ -153,7 +154,6 @@ bool Decoder::hasMore() const uint32_t Decoder::framesLeft(uint32_t max) { if (state == empty || state == onGlueHalf) { - cached = true; return 0; } @@ -178,13 +178,16 @@ uint32_t Decoder::framesLeft(uint32_t max) ++cachedLength; } } else { - std::cout << "framesLeft::" << mad_stream_errorstr(&probe) << std::endl; + std::cout << "framesLeft error: " << mad_stream_errorstr(&probe) << std::endl; if (!MAD_RECOVERABLE(probe.error)) { break; } } } + cachedNext = probe.next_frame; + cachedThis = probe.this_frame; + cachedError = probe.error; mad_header_finish(&ph); mad_stream_finish(&probe); std::cout << cachedLength << " frames are available for decoding" << std::endl; @@ -196,13 +199,12 @@ uint32_t Decoder::framesLeft(uint32_t max) void Decoder::pullBuffer() { - mad_header dummy; - mad_header_init(&dummy); - while(stream.error == 0 || MAD_RECOVERABLE(stream.error)) { - mad_header_decode(&dummy, &stream); - std::cout << "pullBuffer:: " << mad_stream_errorstr(&stream) << std::endl; + if (cached == false) { + std::cout << "Error in pullBuffer method!" << std::endl; } - mad_header_finish(&dummy); + stream.this_frame = cachedThis; + stream.next_frame = cachedNext; + stream.error = cachedError; } void Decoder::changeBuffer() @@ -224,39 +226,8 @@ void Decoder::changeBuffer() break; case onGlueFull: std::cout << "Having another fragment " << pending[0].length << " bytes long" << std::endl; - left = (uint64_t)stream.bufend - (uint64_t)stream.next_frame; -// std::cout << "MD_LENGTH_BEFORE: " << stream.anc_bitlen << std::endl; - mad_stream_buffer(&stream, pending[0].ptr, pending[0].length); - - //mad_stream_skip(&stream, left); - stream.error = MAD_ERROR_NONE; - if (lastSync != -1) { - if (lastSync > GLUE_LENGTH / 2) { - std::cout << "Error: bytes to read in the buffer are more then glue buffer can fit LASTSYNC (" << lastSync << ")" << std::endl; - throw 1; - } - uint64_t target = (uint64_t)stream.buffer + GLUE_LENGTH / 2 - lastSync; - while ((uint64_t)(stream.this_frame) < target) { -// std::cout << "pulling" << std::endl; - mad_frame_decode(&frame, &stream); - } - stream.error = MAD_ERROR_NONE; - -// stream.this_frame = stream.buffer + GLUE_LENGTH / 2 - lastSync; -// stream.next_frame = stream.buffer + GLUE_LENGTH / 2 - left; -// stream.sync = 1; - - lastSync = -1; - -// mad_bitptr next_frame; -// mad_bit_init(&next_frame, stream.next_frame); -// stream.anc_ptr = stream.ptr; -// stream.anc_bitlen = mad_bit_length(&stream.ptr, &next_frame); -// mad_bit_finish(&next_frame); -// -// std::cout << ", MD_LENGTH_AFTER: " << stream.anc_bitlen << std::endl; - } + switchBuffer(pending[0].ptr, pending[0].length); for (int i = 0; i < GLUE_LENGTH/2; ++i) { glue[i] = pending[0].ptr[pending[0].length - GLUE_LENGTH/2 + i]; @@ -317,46 +288,32 @@ void Decoder::initializeProbe(mad_stream& probe) void Decoder::switchToGlue() { std::cout << "Switching to glue" << std::endl; - uint32_t left = (uint64_t)stream.bufend - (uint64_t)stream.next_frame; - if (left > GLUE_LENGTH / 2) { - std::cout << "Error: bytes to read in the buffer are more then glue buffer can fit (" << left << ")" << std::endl; - throw 1; - } -// std::cout << "MD_LENGTH_BEFORE: " << stream.anc_bitlen << std::endl; - mad_stream_buffer(&stream, glue, GLUE_LENGTH); - - if (lastSync != -1) { - if (lastSync > GLUE_LENGTH / 2) { - std::cout << "Error: bytes to read in the buffer are more then glue buffer can fit LASTSYNC (" << lastSync << ")" << std::endl; - throw 1; - } - uint64_t target = (uint64_t)stream.bufend - GLUE_LENGTH / 2 - lastSync; - while ((uint64_t)(stream.this_frame) < target) { -// std::cout << "pulling" << std::endl; - mad_frame_decode(&frame, &stream); - } - stream.error = MAD_ERROR_NONE; -// stream.this_frame = stream.bufend - GLUE_LENGTH / 2 - lastSync; -// stream.next_frame = stream.bufend - GLUE_LENGTH / 2 - left; -// stream.sync = 1; - -// mad_bitptr next_frame; -// mad_bit_init(&next_frame, stream.next_frame); -// stream.anc_ptr = stream.ptr; -// stream.anc_bitlen = mad_bit_length(&stream.ptr, &next_frame); -// mad_bit_finish(&next_frame); -// -// std::cout << ", MD_LENGTH_AFTER: " << stream.anc_bitlen << std::endl; - - lastSync = -1; - } - //mad_stream_skip(&stream, left); + switchBuffer(glue, GLUE_LENGTH); stream.error = MAD_ERROR_NONE; - //std::cout << "SKIPLEN (" << stream.skiplen << ")" << std::endl; - std::cout << "Freeing the drained fragment" << std::endl; free(pending[0].ptr); pending.pop_front(); } + +void Decoder::switchBuffer(uint8_t* bufferPtr, uint32_t length) +{ + uint32_t left; + if (stream.error != MAD_ERROR_BUFLEN) { + std::cout << "WARNING: Switching buffers while the previous one is not drained, error: " << mad_stream_errorstr(&stream) << std::endl; + } + if (stream.next_frame != NULL) { + left = stream.bufend - stream.next_frame; + } else { + std::cout << "WARNING: not supposed to happen" << std::endl; + } + + if (left > GLUE_LENGTH / 2) { + std::cout << "Error: bytes to read in the buffer are more then glue buffer can fit (" << left << ")" << std::endl; + throw 1; + } + mad_stream_buffer(&stream, bufferPtr + GLUE_LENGTH / 2 - left, length - (GLUE_LENGTH / 2 - left)); + + stream.error = MAD_ERROR_NONE; +} diff --git a/decoder.h b/decoder.h index 159fd34..f6e88e7 100644 --- a/decoder.h +++ b/decoder.h @@ -45,7 +45,9 @@ private: uint32_t cachedLength; uint16_t samplesPerFrame; uint8_t* glue; - int32_t lastSync; + uint8_t const* cachedNext; + uint8_t const* cachedThis; + mad_error cachedError; bool cached; mad_synth synth; @@ -61,6 +63,7 @@ private: void prepareNextBuffer(); void initializeProbe(mad_stream& probe); void switchToGlue(); + void switchBuffer(uint8_t* bufferPtr, uint32_t length); }; #endif // DECODER_H diff --git a/index.html b/index.html index 10fbcc7..a3a3ce4 100644 --- a/index.html +++ b/index.html @@ -30,7 +30,8 @@ createFileFragments(reader.result); scheduleData(); - ctx.resume(); + + setTimeout(.bind(ctx), 1000); } reader.readAsArrayBuffer(file);