From 2d99f670594e6d14ffdfd23b3ccadb47101d1f00 Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Tue, 2 Feb 2010 15:30:50 +0100 Subject: add method to decode a given frame The method decode(...) will write the decoded audio frame directly to the card via oss or alsa audio interface. --- dxr3audiodecoder.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ dxr3audiodecoder.h | 3 +++ 2 files changed, 56 insertions(+) diff --git a/dxr3audiodecoder.c b/dxr3audiodecoder.c index 6ad774e..d80d235 100644 --- a/dxr3audiodecoder.c +++ b/dxr3audiodecoder.c @@ -86,6 +86,59 @@ void cDxr3AudioDecoder::Init() } } +void cDxr3AudioDecoder::decode(cDxr3PesFrame *frame, iAudio *audio) +{ + int len, out_size; + + const uint8_t *buf = frame->GetPayload(); + int length = frame->GetPayloadLength(); + + if (checkMpegAudioHdr(buf)) { + + // look if Bitrate has changed + if ((buf[2] & 0xf0) != (lastBitrate & 0xf0)) { + dsyslog("[dxr3-audiodecoder] found new audio header"); + + // recalculate used framesize + frameSize = calcFrameSize(buf); + dsyslog("[dxr3-audiodecoder] calculated frame size %d", frameSize); + + // we need now to reinit the deocder and to store the new + // part from the audio header + Init(); + lastBitrate = buf[2]; + } + } + + // setup AVPacket + avpkt.data = const_cast(buf); + avpkt.size = frameSize; + + while (length > 0) { + out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; + +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(51, 29, 0) + len = avcodec_decode_audio(contextAudio, (short *)(&pcmbuf), &out_size, avpkt.data, frameSize); +#elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 26, 0) + len = avcodec_decode_audio2(contextAudio, (short *)(&pcmbuf), &out_size, avpkt.data, frameSize); +#else + len = avcodec_decode_audio3(contextAudio, (short *)(&pcmbuf), &out_size, &avpkt); +#endif + + if (len <= 0) { + esyslog("[dxr3-decoder] failed to decode audio"); + return; + } + + if (out_size) { + audio->write(pcmbuf, out_size); + } + + length -= len; + avpkt.data += len; + } +} + #if 0 // ================================== //! decode given buffer diff --git a/dxr3audiodecoder.h b/dxr3audiodecoder.h index b4d004b..24f5721 100644 --- a/dxr3audiodecoder.h +++ b/dxr3audiodecoder.h @@ -33,6 +33,7 @@ extern "C" { #include "uncopyable.h" class cDxr3PesFrame; +class iAudio; // ================================== // decode audio to mp2 or use DD :) @@ -40,6 +41,8 @@ class cDxr3AudioDecoder : private Uncopyable { public: cDxr3AudioDecoder(); ~cDxr3AudioDecoder(); + + void decode(cDxr3PesFrame *frame, iAudio *audio); /* void Decode(cDxr3PesFrame *frame, uint32_t pts, cDxr3SyncBuffer &aBuf); void DecodeLpcm(cDxr3PesFrame *frame, uint32_t pts, cDxr3SyncBuffer &aBuf); -- cgit v1.2.3