File: ap_alac.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (234 lines) | stat: -rw-r--r-- 6,325 bytes parent folder | download | duplicates (2)
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
230
231
232
233
234
/*******************************************************************************
*                         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_buffer.h"
#include "ap_packet.h"
#include "ap_event_private.h"
#include "ap_decoder_plugin.h"

#include "alac.h"

namespace ap {


#ifdef HAVE_ALAC

class AlacDecoder : public DecoderPlugin {
protected:
  MemoryBuffer   buffer;
  MemoryBuffer   outbuf;
  alac_file*     handle = nullptr;
  FXlong         stream_position = -1;
protected:
  Packet * out;
protected:
  FXbool getNextFrame(Packet *& packet,FXuchar *& ptr,FXuint & framesize);
public:
  AlacDecoder(DecoderContext*);
  FXuchar codec() const override { return Codec::ALAC; }
  FXbool flush(FXlong offset=0) override;
  FXbool init(ConfigureEvent*) override ;
  FXbool process(Packet*) override;
  ~AlacDecoder();
  };


AlacDecoder::AlacDecoder(DecoderContext * e) : DecoderPlugin(e),handle(nullptr),stream_position(-1),out(NULL) {
  }

AlacDecoder::~AlacDecoder() {
  flush();
  if (handle) {
    dispose_alac(handle);
    handle=nullptr;
    }
  }


FXbool AlacDecoder::init(ConfigureEvent*event) {
  DecoderPlugin::init(event);
  buffer.clear();
  if (handle) {
    dispose_alac(handle);
    handle=nullptr;
    }
  af=event->af;

  // Alac Decoder
  handle=create_alac(event->af.bps(),event->af.channels);
  if (handle==nullptr)
    return false;

  // Make sure we can init the decoder
  DecoderSpecificConfig * dc = dynamic_cast<DecoderSpecificConfig*>(event->dc);
  if (dc==nullptr && dc->config_bytes==0) {
    dispose_alac(handle);
    handle=nullptr;
    return false;
    }

  alac_set_info(handle,(FXchar*)dc->config);
  outbuf.resize(handle->setinfo_max_samples_per_frame*af.framesize());
  stream_position=-1;
  return true;
  }


FXbool AlacDecoder::flush(FXlong offset) {
  DecoderPlugin::flush(offset);
  buffer.clear();
  outbuf.clear();
  if (out) {
    out->unref();
    out=NULL;
    }
  stream_position=-1;
  return true;
  }



FXbool AlacDecoder::getNextFrame(Packet *& packet,FXuchar *& ptr,FXuint & framesize) {
  framesize=0;

  // data in local cache
  if (buffer.size()) {

    // read next framesize
    memcpy(&framesize,buffer.data(),4);

    // get frame from buffer
    if (framesize+4<=buffer.size()){
      buffer.readBytes(4);
      ptr=buffer.data();
      buffer.readBytes(framesize);
      return true;
      }

    // see if packet contains additional data
    if (packet) {

      // get missing data from packet
      FXuint missing = framesize - (buffer.size()-4);
      if (packet->size()>=missing) {
        buffer.append(packet->data(),missing);
        packet->readBytes(missing);
        buffer.readBytes(4);
        ptr=buffer.data();
        buffer.readBytes(framesize);
        if (packet->size()==0) {
          packet->unref();
          packet=nullptr;
          }
        return true;
        }

      // incomplete data in packet
      buffer.append(packet->data(),packet->size());
      packet->unref();
      packet=nullptr;
      }
    return false;
    }

  if (packet) {

    if (packet->size()>=4) {

      // read next framesize
      memcpy(&framesize,packet->data(),4);

      // get frame from packet
      if (framesize+4<=packet->size()){
        packet->readBytes(4);
        ptr=packet->data();
        packet->readBytes(framesize);
        return true;
        }
      }

    // incomplete data in packet
    if (packet->size())
      buffer.append(packet->data(),packet->size());
    packet->unref();
    packet=nullptr;
    return false;
    }
  return false;
  }

FXbool AlacDecoder::process(Packet*packet){
  const FXbool eos = packet->flags&FLAG_EOS;
  const FXlong stream_length = packet->stream_length;
  const FXuint stream_id = packet->stream;

  if (stream_position == -1) {
    stream_position = packet->stream_position;
    }

  FXuchar * inputdata=nullptr;
  FXuint    framesize=0;

  while(getNextFrame(packet,inputdata,framesize)) {
    FXint nframes=outbuf.space();
    decode_frame(handle,inputdata,outbuf.ptr(),&nframes);
    outbuf.wroteBytes(nframes);
    nframes /= af.framesize();
    while(nframes) {

      // Get output packet
      if (out==NULL){
        out = context->get_output_packet();
        if (out==nullptr) return true;
        out->af              = af;
        out->stream          = stream_id;
        out->stream_position = stream_position;
        out->stream_length   = stream_length;
        }

      // copy max frames
      FXuint ncopy = FXMIN(out->availableFrames(),nframes);
      out->appendFrames(outbuf.data(),ncopy);
      outbuf.readBytes(ncopy*af.framesize());
      nframes-=ncopy;
      stream_position+=ncopy;

      // Send to
      if (out->availableFrames()==0) {
        context->post_output_packet(out);
        }
      }
    outbuf.clear();
    }

  if (eos) {
    context->post_output_packet(out,true);
    }
  return true;
  }


DecoderPlugin * ap_alac_decoder(DecoderContext * ctx) {
  return new AlacDecoder(ctx);
  }

#endif
}