1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/*******************************************************************************
* Goggles Audio Player Library *
********************************************************************************
* Copyright (C) 2010-2021 by Sander Jansen. All Rights Reserved *
* --- *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see http://www.gnu.org/licenses. *
********************************************************************************/
#include "ap_defs.h"
#include "ap_opus.h"
#include "ap_packet.h"
#include "ap_ogg_decoder.h"
#include <opus/opus_multistream.h>
namespace ap {
class OpusDecoderPlugin : public OggDecoder{
protected:
OpusMSDecoder* opus;
FXfloat * pcm;
FXfloat gain;
FXushort stream_offset_start;
protected:
FXbool init_decoder(const FXuchar *,FXuint);
// FXbool find_stream_position();
// FXlong find_stream_length();
protected:
public:
OpusDecoderPlugin(DecoderContext*);
FXuchar codec() const override { return Codec::Opus; }
FXbool init(ConfigureEvent*) override;
FXbool process(Packet*) override;
FXbool flush(FXlong) override;
virtual ~OpusDecoderPlugin();
};
OpusDecoderPlugin::OpusDecoderPlugin(DecoderContext * e) : OggDecoder(e),opus(nullptr),pcm(nullptr),gain(0.0f),stream_offset_start(0) {
}
OpusDecoderPlugin::~OpusDecoderPlugin(){
freeElms(pcm);
if (opus) {
opus_multistream_decoder_destroy(opus);
opus=nullptr;
}
}
#define MAX_FRAME_SIZE (960*6)
FXbool OpusDecoderPlugin::init(ConfigureEvent*event) {
OggDecoder::init(event);
af=event->af;
if (opus) {
opus_multistream_decoder_destroy(opus);
opus=nullptr;
gain=0.0f;
}
if (event->dc) {
OpusConfig * opc = dynamic_cast<OpusConfig*>(event->dc);
init_decoder(opc->info,opc->info_bytes);
}
if (pcm)
resizeElms(pcm,MAX_FRAME_SIZE*af.channels*2);
else
allocElms(pcm,MAX_FRAME_SIZE*af.channels*2);
return true;
}
FXbool OpusDecoderPlugin::flush(FXlong offset) {
OggDecoder::flush(offset);
//if (opus) opus_decoder_ctl(opus,OPUS_RESET_STATE);
return true;
}
FXbool OpusDecoderPlugin::init_decoder(const FXuchar * packet,const FXuint size) {
FXASSERT(opus==nullptr);
FXint error;
if (size>=19) {
// Extra check to make sure the reader gave us the header packet
if (FXString::compare((const FXchar*)packet,"OpusHead",8))
return false;
if (packet[18]!=0) {
// Validate stream map size
if (af.channels!=op.bytes-21)
return false;
// Validate stream map
const FXuchar nstreams=packet[19];
const FXuchar ncoupled=packet[20];
for (FXint i=0;i<af.channels;i++) {
if (packet[21+i]>nstreams+ncoupled && packet[21+i]!=255)
return false;
}
opus = opus_multistream_decoder_create(48000,af.channels,nstreams,ncoupled,packet+21,&error);
}
else {
const FXuchar stream_map[2] = {0,1};
opus = opus_multistream_decoder_create(48000,af.channels,1,af.channels>1,stream_map,&error);
}
if (error!=OPUS_OK)
return false;
// Apply any gain
#if FOX_BIGENDIAN == 0
FXshort output_gain = packet[16] | packet[17]<<8;
#else
FXshort output_gain = packet[16]<<8 | packet[17];
#endif
#ifdef OPUS_SET_GAIN
error=opus_multistream_decoder_ctl(opus,OPUS_SET_GAIN(output_gain));
if(error==OPUS_UNIMPLEMENTED)
gain = pow(10,output_gain/(20.0*256));
else
gain = 0.0f;
#else
gain = pow(10,output_gain/(20.0*256));
#endif
return true;
}
return false;
}
FXbool OpusDecoderPlugin::process(Packet * packet) {
OggDecoder::process(packet);
const FXlong stream_begin = FXMAX(stream_offset_start,stream_decode_offset);
const FXlong stream_length = packet->stream_length;
const FXbool eos = packet->flags&FLAG_EOS;
FXlong stream_end = stream_length;
while(get_next_packet(packet)) {
FXint nsamples = opus_multistream_decode_float(opus,(unsigned char*)op.packet,op.bytes,pcm,MAX_FRAME_SIZE,0);
const FXuchar * pcmi = (const FXuchar*)pcm;
// apply output gain
if (gain!=0.0f) {
for (FXint i=0;i<nsamples*af.channels;i++) {
pcm[i]*=gain;
}
}
// Adjust for beginning of stream
if (stream_position<stream_begin) {
FXlong offset = FXMIN(nsamples,stream_begin - stream_position);
GM_DEBUG_PRINT("[opus] stream offset start %ld. Skip %ld at %ld\n",stream_begin,offset,stream_position);
nsamples-=offset;
pcmi+=(offset*af.framesize());
stream_position+=offset;
}
//GM_DEBUG_PRINT("[opus] decoded %d frames\n",nsamples);
if (eos) {
FXlong total = stream_position-stream_offset_start+nsamples;
if (total>stream_end) {
GM_DEBUG_PRINT("adjusting end trimming by %ld\n",(total-stream_end));
nsamples -= (total-stream_end);
}
}
while(nsamples>0) {
/// Get new buffer
if (out==nullptr) {
out = context->get_output_packet();
if (out==nullptr) {
if (packet) packet->unref();
return true;
}
out->stream_position=stream_position - stream_offset_start;
out->stream_length=stream_length;
out->af=af;
}
FXint nw = FXMIN(out->availableFrames(),nsamples);
if (nw>0){
out->appendFrames(pcmi,nw);
pcmi+=(nw*af.framesize());
nsamples-=nw;
stream_position+=nw;
}
if (out->availableFrames()==0) {
context->post_output_packet(out);
}
}
}
if (eos) {
context->post_output_packet(out,true);
}
return true;
}
DecoderPlugin * ap_opus_decoder(DecoderContext * ctx) {
return new OpusDecoderPlugin(ctx);
}
}
|