diff options
author | scop <scop> | 2005-04-19 18:19:34 +0000 |
---|---|---|
committer | scop <scop> | 2005-04-19 18:19:34 +0000 |
commit | f9c59e544ada17055c3ed15b3d80f0d285c3928d (patch) | |
tree | cd7b038c17b040beb435e0a16aab02346854c9ad /dxr3audiodecoder.c | |
parent | dea137eddfe1017e9ff85a8c221aee2bca4647ad (diff) | |
download | vdr-plugin-dxr3-f9c59e544ada17055c3ed15b3d80f0d285c3928d.tar.gz vdr-plugin-dxr3-f9c59e544ada17055c3ed15b3d80f0d285c3928d.tar.bz2 |
Mass indentation/whitespace cleanup.
Diffstat (limited to 'dxr3audiodecoder.c')
-rw-r--r-- | dxr3audiodecoder.c | 396 |
1 files changed, 206 insertions, 190 deletions
diff --git a/dxr3audiodecoder.c b/dxr3audiodecoder.c index 19371a2..17ea0fe 100644 --- a/dxr3audiodecoder.c +++ b/dxr3audiodecoder.c @@ -22,11 +22,10 @@ /* -ToDo: - - cDxr3AudioDecoder::Init: Why are we always reinit the codec? + ToDo: + - cDxr3AudioDecoder::Init: Why are we always reinit the codec? */ - #include <stdio.h> #include "dxr3audiodecoder.h" #include "dxr3pesframe.h" @@ -36,12 +35,12 @@ const int LPCM_HEADER_LENGTH = 7; // ================================== //! constructor -cDxr3AudioDecoder::cDxr3AudioDecoder() : rbuf(50000), ac3dtsDecoder(&rbuf) +cDxr3AudioDecoder::cDxr3AudioDecoder() : rbuf(50000), ac3dtsDecoder(&rbuf) { decoderOpened = false; audioSynched = false; volume = 255; - Codec.id = CODEC_ID_MP2; + Codec.id = CODEC_ID_MP2; Init(); @@ -51,245 +50,262 @@ cDxr3AudioDecoder::cDxr3AudioDecoder() : rbuf(50000), ac3dtsDecoder(&rbuf) // ================================== //! deconst. -cDxr3AudioDecoder::~cDxr3AudioDecoder() +cDxr3AudioDecoder::~cDxr3AudioDecoder() { - // close codec, if it is open - cDxr3Ffmepg::Instance().CloseCodec(Codec); + // close codec, if it is open + cDxr3Ffmepg::Instance().CloseCodec(Codec); }; // ================================== //! (re)init ffmpeg codec -void cDxr3AudioDecoder::Init() +void cDxr3AudioDecoder::Init() { - // (re)init codec - cDxr3Ffmepg::Instance().CloseCodec(Codec); - if (cDxr3Ffmepg::Instance().FindCodec(Codec)) - { - cDxr3Ffmepg::Instance().OpenCodec(Codec); - rate = channels = -1; - frameSize = Codec.codec_context.frame_size; - decoderOpened = true; - foundHeader = false; - decodeAudio = true; - - //lastHeader[0] = 0xFF; - //lastHeader[1] = lastHeader[2] = lastHeader[3] = 0; - } - else - { - decoderOpened = false; - } + // (re)init codec + cDxr3Ffmepg::Instance().CloseCodec(Codec); + if (cDxr3Ffmepg::Instance().FindCodec(Codec)) + { + cDxr3Ffmepg::Instance().OpenCodec(Codec); + rate = channels = -1; + frameSize = Codec.codec_context.frame_size; + decoderOpened = true; + foundHeader = false; + decodeAudio = true; + + //lastHeader[0] = 0xFF; + //lastHeader[1] = lastHeader[2] = lastHeader[3] = 0; + } + else + { + decoderOpened = false; + } } // ================================== //! decode given buffer -void cDxr3AudioDecoder::Decode(const uint8_t* buf, int length, uint32_t pts, cDxr3SyncBuffer &aBuf) +void cDxr3AudioDecoder::Decode(const uint8_t* buf, int length, uint32_t pts, + cDxr3SyncBuffer &aBuf) { - if (!decoderOpened) - { - // No decoder is open, so it - // is better to stop here. - return; - } + if (!decoderOpened) + { + // No decoder is open, so it + // is better to stop here. + return; + } int len; int out_size; - enum audioException - { - WRONG_LENGTH, - UNEXPECTED_PARAMETER_CHANGE + enum audioException + { + WRONG_LENGTH, + UNEXPECTED_PARAMETER_CHANGE }; int i = 0; - for (i = 0; i < length-4 && !foundHeader; i++) + for (i = 0; i < length-4 && !foundHeader; i++) + { + unsigned int tempHead = *((unsigned int*)(buf+i)); + if (HeadCheck(tempHead)) { - unsigned int tempHead = *((unsigned int*)(buf+i)); - if (HeadCheck(tempHead)) - { - if ((buf[i+2] & 0xFC) != (lastHeader[2] & 0xFC)) - { - cLog::Instance() << "cDxr3AudioDecoder::Decode Found different audio header -> init\n"; - cLog::Instance() << "cDxr3AudioDecoder::Decode Old header 0x" << std::hex << *((uint32_t*) lastHeader) << " new header 0x" << *((uint32_t*) (buf+i)) << std::dec << "\n"; - - Init(); - lastHeader[0] = buf[i]; - lastHeader[1] = buf[i+1]; - lastHeader[2] = buf[i+2]; - lastHeader[3] = buf[i+3]; - } - foundHeader = true; - } + if ((buf[i+2] & 0xFC) != (lastHeader[2] & 0xFC)) + { + cLog::Instance() << "cDxr3AudioDecoder::Decode Found different audio header -> init\n"; + cLog::Instance() << "cDxr3AudioDecoder::Decode Old header 0x" + << std::hex << *((uint32_t*) lastHeader) + << " new header 0x" << *((uint32_t*) (buf+i)) + << std::dec << "\n"; + + Init(); + lastHeader[0] = buf[i]; + lastHeader[1] = buf[i+1]; + lastHeader[2] = buf[i+2]; + lastHeader[3] = buf[i+3]; + } + foundHeader = true; + } } - if (audioSynched) - { - // no header found - decodeAudio = true; - } - else + if (audioSynched) + { + // no header found + decodeAudio = true; + } + else + { + if (foundHeader && pts) { - if (foundHeader && pts) - { - decodeAudio = true; - audioSynched = true; - } + decodeAudio = true; + audioSynched = true; + } } - try + try + { + while (length > 0 && decodeAudio) { - while (length > 0 && decodeAudio) - { - len = avcodec_decode_audio(&Codec.codec_context, (short *)(&pcmbuf), &out_size, - const_cast<uint8_t *>(buf), length); - if (len < 0 || out_size < 0) throw WRONG_LENGTH; - - if (Codec.codec_context.sample_rate != rate) - { - cLog::Instance() << "cDxr3AudioDecoder::Decode Sample rate = " << Codec.codec_context.sample_rate << "\n"; - if (rate != -1) throw UNEXPECTED_PARAMETER_CHANGE; - rate = Codec.codec_context.sample_rate; - } - if (Codec.codec_context.channels != channels+1 ) - { - if (channels != -1) throw UNEXPECTED_PARAMETER_CHANGE; - channels = (Codec.codec_context.channels == 2) ? 1 : 0; - cLog::Instance() << "cDxr3AudioDecoder::Decode channels = " << Codec.codec_context.channels << "\n"; - } - if (out_size) - { - cFixedLengthFrame* pTempFrame = aBuf.Push(pcmbuf, out_size, pts); - pTempFrame->SetChannelCount(channels); - pTempFrame->SetDataRate(rate); - } - length -= len; - buf += len; - } - } - catch (audioException ex) + len = avcodec_decode_audio(&Codec.codec_context, + (short *)(&pcmbuf), &out_size, + const_cast<uint8_t *>(buf), length); + if (len < 0 || out_size < 0) + throw WRONG_LENGTH; + + if (Codec.codec_context.sample_rate != rate) + { + cLog::Instance() << "cDxr3AudioDecoder::Decode Sample rate = " + << Codec.codec_context.sample_rate << "\n"; + if (rate != -1) throw UNEXPECTED_PARAMETER_CHANGE; + rate = Codec.codec_context.sample_rate; + } + if (Codec.codec_context.channels != channels + 1) + { + if (channels != -1) + throw UNEXPECTED_PARAMETER_CHANGE; + channels = (Codec.codec_context.channels == 2) ? 1 : 0; + cLog::Instance() << "cDxr3AudioDecoder::Decode channels = " + << Codec.codec_context.channels << "\n"; + } + if (out_size) + { + cFixedLengthFrame* pTempFrame = aBuf.Push(pcmbuf, + out_size, pts); + pTempFrame->SetChannelCount(channels); + pTempFrame->SetDataRate(rate); + } + length -= len; + buf += len; + } + } + catch (audioException ex) + { + switch (ex) { - switch (ex) - { - case WRONG_LENGTH: - cLog::Instance() << "cDxr3AudioDecoder::Decode wrong length\n"; - break; - - case UNEXPECTED_PARAMETER_CHANGE: - cLog::Instance() << "cDxr3AudioDecoder::Decode unexpected parameter change\n"; - break; - - default: - cLog::Instance() << "cDxr3AudioDecoder::Decode unexpeced exception\n"; - break; - } - dsyslog("cDxr3AudioDecoder::Decode skipping %d broken data bytes", length); - - Init(); + case WRONG_LENGTH: + cLog::Instance() << "cDxr3AudioDecoder::Decode wrong length\n"; + break; + + case UNEXPECTED_PARAMETER_CHANGE: + cLog::Instance() << "cDxr3AudioDecoder::Decode unexpected parameter change\n"; + break; + + default: + cLog::Instance() << "cDxr3AudioDecoder::Decode unexpeced exception\n"; + break; + } + dsyslog("cDxr3AudioDecoder::Decode skipping %d broken data bytes", + length); + + Init(); } } // ================================== //! decode lpcm -void cDxr3AudioDecoder::DecodeLpcm(const uint8_t* buf, int length, uint32_t pts, cDxr3SyncBuffer &aBuf) +void cDxr3AudioDecoder::DecodeLpcm(const uint8_t* buf, int length, + uint32_t pts, cDxr3SyncBuffer &aBuf) { - if (length > (LPCM_HEADER_LENGTH + 2)) + if (length > (LPCM_HEADER_LENGTH + 2)) + { + uint8_t* pFrame = new uint8_t[length - LPCM_HEADER_LENGTH]; + // only even number of bytes are allowed + assert(!((length - LPCM_HEADER_LENGTH) % 2)); + + for (int i = LPCM_HEADER_LENGTH; i < length; i += 2) { - uint8_t* pFrame = new uint8_t[length - LPCM_HEADER_LENGTH]; - assert(!((length - LPCM_HEADER_LENGTH) % 2)); // only even number of bytes are allowed - - for (int i = LPCM_HEADER_LENGTH; i < length; i += 2) - { - pFrame[i - LPCM_HEADER_LENGTH] = buf[i + 1]; - pFrame[i - LPCM_HEADER_LENGTH + 1] = buf[i]; - } - - int codedSpeed = (buf[5] >> 4) & 0x03; - int speed = 0; - - switch (codedSpeed) - { - case 1: - speed = 96000; - break; - - case 2: - speed = 44100; - break; - - case 3: - speed = 32000; - break; - - default: - speed = 48000; - break; - } - - cFixedLengthFrame* pTempFrame = aBuf.Push(pFrame, length - LPCM_HEADER_LENGTH, pts); - pTempFrame->SetChannelCount(1); - pTempFrame->SetDataRate(speed); - - delete[] pFrame; + pFrame[i - LPCM_HEADER_LENGTH] = buf[i + 1]; + pFrame[i - LPCM_HEADER_LENGTH + 1] = buf[i]; + } + + int codedSpeed = (buf[5] >> 4) & 0x03; + int speed = 0; + + switch (codedSpeed) + { + case 1: + speed = 96000; + break; + + case 2: + speed = 44100; + break; + + case 3: + speed = 32000; + break; + + default: + speed = 48000; + break; + } + + cFixedLengthFrame* pTempFrame = aBuf.Push(pFrame, + length - LPCM_HEADER_LENGTH, + pts); + pTempFrame->SetChannelCount(1); + pTempFrame->SetDataRate(speed); + + delete[] pFrame; } } // ================================== //! decode ac3 -void cDxr3AudioDecoder::DecodeAc3Dts(const uint8_t* pPes, const uint8_t* buf, int length, uint32_t pts, cDxr3SyncBuffer &aBuf) +void cDxr3AudioDecoder::DecodeAc3Dts(const uint8_t* pPes, const uint8_t* buf, + int length, uint32_t pts, + cDxr3SyncBuffer &aBuf) { int headerLength = (int) (buf - pPes); uint8_t* pBuf = (uint8_t*) pPes; ac3dtsDecoder.Check(pBuf + headerLength, length, pBuf); ac3dtsDecoder.Encapsulate(pBuf + headerLength, length); - + cFrame* pFrame = 0; - while ((pFrame = rbuf.Get())) + while ((pFrame = rbuf.Get())) + { + if (pFrame && pFrame->Count()) { - if (pFrame && pFrame->Count()) - { - cDxr3PesFrame tempPes; - tempPes.ExtractNextFrame(pFrame->Data(), pFrame->Count()); - int pesHeaderLength = (int) (tempPes.GetEsStart() - tempPes.GetPesStart()); - uint8_t* pData = pFrame->Data() + pesHeaderLength + LPCM_HEADER_LENGTH; - - for (int i = 0; i < pFrame->Count() - pesHeaderLength - LPCM_HEADER_LENGTH; i += 2) - { - std::swap(pData[i], pData[i + 1]); - } - - aBuf.Push(pFrame->Data() + pesHeaderLength + LPCM_HEADER_LENGTH, pFrame->Count() - pesHeaderLength - 7, tempPes.GetPts()); - if (pFrame) rbuf.Drop(pFrame); - } + cDxr3PesFrame tempPes; + tempPes.ExtractNextFrame(pFrame->Data(), pFrame->Count()); + int pesHeaderLength = (int) (tempPes.GetEsStart() - tempPes.GetPesStart()); + uint8_t* pData = pFrame->Data() + pesHeaderLength + LPCM_HEADER_LENGTH; + + for (int i = 0; i < pFrame->Count() - pesHeaderLength - LPCM_HEADER_LENGTH; i += 2) + { + std::swap(pData[i], pData[i + 1]); + } + + aBuf.Push(pFrame->Data() + pesHeaderLength + LPCM_HEADER_LENGTH, pFrame->Count() - pesHeaderLength - 7, tempPes.GetPts()); + if (pFrame) rbuf.Drop(pFrame); + } } -} +} // ================================== //! checking routine -bool cDxr3AudioDecoder::HeadCheck(unsigned long head) +bool cDxr3AudioDecoder::HeadCheck(unsigned long head) { bool retval = false; - + uint8_t* phead = (uint8_t*) (&head); - if (phead[0] != 0xFF) - { - retval = false; - } - else if (phead[1] != 0xFC && phead[1] != 0xFE) - { - retval = false; - } - else if ((phead[2] & 0xF0) == 0xF0) - { - retval = false; - } - else if ((phead[2] & 0xC) == 0xC) - { - retval = false; + if (phead[0] != 0xFF) + { + retval = false; } - else - { - retval = true; + else if (phead[1] != 0xFC && phead[1] != 0xFE) + { + retval = false; + } + else if ((phead[2] & 0xF0) == 0xF0) + { + retval = false; + } + else if ((phead[2] & 0xC) == 0xC) + { + retval = false; + } + else + { + retval = true; } return retval; |