summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/decoder/splayPlugin.cpp
blob: 097d6d7935ed86bcdbfdf0ec7ba808eb19de1e6b (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
  splay player plugin
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "splayPlugin.h"
#include "../splay/mpegsound.h"

#include "../splay/splayDecoder.h"
#include "../frame/floatFrame.h"
#include "../frame/pcmFrame.h"
#include "../splay/mpegAudioFrame.h"
#include "../splay/mpegAudioInfo.h"
#include "../input/fileAccessWrapper.h"

#include <iostream>

using namespace std;

#define INPUTBUFFER_SIZE 8192

SplayPlugin::SplayPlugin() {
  pow(6.0,3.0);            // fixes bug in __math.h
  doFloat = false;
  lnoLength=false;
  inputbuffer=new unsigned char[INPUTBUFFER_SIZE];
  pcmFrame=new PCMFrame(MP3FRAMESIZE);
  floatFrame=new FloatFrame(MP3FRAMESIZE);
  audioFrame=new AudioFrame();
  framer=new MpegAudioFrame();
  splay=new SplayDecoder();
  lengthInSec=0;
 
  fileAccess=NULL;
  info=NULL; 
  lOutput=true;
}


SplayPlugin::~SplayPlugin() {
  delete [] inputbuffer;
  delete pcmFrame;
  delete floatFrame;
  delete framer;
  delete splay;
  delete audioFrame;
}


// here we can config our decoder with special flags
void SplayPlugin::config(const char* key,const char* value,void* user_data) {
  if (strcmp(key,"dofloat")==0) {
    doFloat=true;
  }
  if (strcmp(key,"-m")==0) {
    splay->config("m","0",NULL);
  }
  if (strcmp(key,"-2")==0) {
    splay->config("2","1",NULL);
  }
  if (strcmp(key,"-c")==0) {
    lnoLength=true;
  }
  if (strcmp(key,"-d")==0) {
    lOutput=false;
  }

  if (strcmp(key,"decode")==0) {
    if (strcmp(value,"true")==0) {
      lDecode=true;
    } else {
      lDecode=false;
    }
  }    
  DecoderPlugin::config(key,value,user_data);
}



void SplayPlugin::decoder_loop() {

  if (input == NULL) {
    cout << "SplayPlugin::decoder_loop input is NULL"<<endl;
    exit(0);
  }
  if (output == NULL) {
    cout << "SplayPlugin::decoder_loop output is NULL"<<endl;
    exit(0);
  }
  // init decoder
  output->audioInit();

  // start decoding

  fileAccess=new FileAccessWrapper(input);
  info= new MpegAudioInfo(fileAccess);


  
  framer->reset();
  lengthInSec=0;
  resyncCounter=0;

  AudioFrame* audioFrame=pcmFrame;
  if (doFloat) {
    audioFrame=floatFrame;
  }

  output->audioInit();
  while(runCheck()) {

    //
    // check for re-init or for "eof"
    //
    switch(streamState) {
    case _STREAM_STATE_INIT :
      framer->reset();
      resyncCounter=5;
      setStreamState(_STREAM_STATE_PLAY);
      continue;
    case _STREAM_STATE_WAIT_FOR_END:
      // exit while loop
      lDecoderLoop=false;
      continue;
    }
    if (doFrameFind() == true) {
      if (splay->decode(framer->outdata(),framer->len(),audioFrame) == false){
	continue;
      }
      // send data out:

      int rest=framer->restBytes();
      // we have inserted more data already than the 
      // framer has processed. But framer tells us
      // how much he still needs to process.
      long pos=input->getBytePosition();
      TimeStamp* stamp=input->getTimeStamp(pos-rest);
      processStreamState(stamp,audioFrame);

      
      // make this stamp invalid for further synchronisation
      stamp->setPTSFlag(false);
    }
  }

  output->audioFlush();

  delete fileAccess;
  delete info;
  fileAccess=NULL;
  info=NULL;


}


int SplayPlugin::doFrameFind() {

  //
  // fine, we can work on the stream:
  //
  int back=false;
  int framerState=framer->getState();
  switch(framerState) {
  case FRAME_NEED: {
    int bytes=framer->canStore();
    int read=input->read((char*)inputbuffer,bytes);

    if (read <= 0) {
      // read error. reset framer, drop frames
      setStreamState(_STREAM_STATE_INIT);
      break;
    }
    framer->store(inputbuffer,read);
    break;
  }
  case FRAME_WORK:
    back=framer->work();
    break;
  case FRAME_HAS: {
    break;
  }
  default:
    cout << "unknown state in mpeg audio framing"<<endl;
    exit(0);
  }
  return back;
}

void SplayPlugin::audioSetup(AudioFrame* setupFrame) {
  setupFrame->copyFormat(audioFrame);
  output->audioSetup(audioFrame->getFrequenceHZ(),
                     audioFrame->getStereo(),
                     audioFrame->getSigned(),
                     audioFrame->getBigEndian(),
                     audioFrame->getSampleSize()); 
}



void SplayPlugin::processStreamState(TimeStamp* stamp,AudioFrame* playFrame){

  // we always have a frame here, with the correct timestamp here.

  switch(streamState) {
  case _STREAM_STATE_FIRST_INIT :
    output->audioOpen();
   
    audioSetup(playFrame);

    if (lnoLength==false) {
      lengthInSec=getTotalLength();
      pluginInfo->setLength(lengthInSec);
      output->writeInfo(pluginInfo);
    }

    setStreamState(_STREAM_STATE_PLAY);
    // yes, here is no break.
    // we want to send the frame to the output.

  case _STREAM_STATE_PLAY :
    if (resyncCounter > 0) {
      resyncCounter--;
      break;
    }
    if (audioFrame->isFormatEqual(playFrame)==false) {
      audioSetup(playFrame);
    }
    if (lOutput == false) {
      break;
    }
    if(doFloat) {
      FloatFrame* floatFrame=(FloatFrame*)playFrame;
      output->audioPlay(stamp,stamp,
			(char*) floatFrame->getData(),
			playFrame->getLen()*sizeof(float) );
    } else {
      PCMFrame* pcmFrame=(PCMFrame*)playFrame;
      output->audioPlay(stamp,stamp,
			(char*)pcmFrame->getData(),
			playFrame->getLen()*sizeof(short int));
    }
    break;
  default:
    cout << "unknown stream state:"<<streamState<<endl;
  }
}



// splay can seek in streams
int SplayPlugin::seek_impl(int second) {


  if (info != NULL) {
    int pos=info->getSeekPosition(second);
    input->seek(pos);
    setStreamState(_STREAM_STATE_INIT);
  } else {
    cout << "cannot seek, plugin not initialized"<<endl;
  }
  return true;
}


int SplayPlugin::getTotalLength() {
  shutdownLock();
  int back=0;

  if (info->getNeedInit()) {
    long pos=input->getBytePosition();
    if (input->seek(0) == true) {
      int bytes=1024;
      // try reading all info, but not more
      // than 1024 iterations.
      info->reset();
      while(bytes>0) {
	if (info->initialize()==true) {
	  break;
	}
	bytes--;
      }
      input->seek(pos);
    }
    // wheter successful or not, never touch
    // the info thing again.
    info->setNeedInit(false);
  }

  back=info->getLength();
  

  shutdownUnlock();
  return back;
}