summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/decoder/vorbisPlugin.cpp
blob: b32ba93fc52c84ed85e1d2f0664c2ded08047afb (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
/*
  vorbis player plugin
  Copyright (C) 2000  Martin Vogt

  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.

  For more information look at the file COPYRIGHT in this package

 */


#include "vorbisPlugin.h"

#include <iostream>

using namespace std;

#ifdef OGG_VORBIS

size_t fread_func(void *ptr, size_t size, size_t nmemb, void *stream) {
  InputStream* input=((VorbisPlugin*)stream)->getInputStream();
  int bytes=input->read((char*)ptr,size*nmemb);
  // workaround for RC3, report success during seek
  /*
  if (((VorbisPlugin*)stream)->vorbis_seek_bug_active == true) {
    errno=0;
    return 0;
  }
  */
  // error on read and no "seek workaround"
  if (bytes == 0) {
    //
    // If different thread close the input we signal
    // a read error to vorbis
    //
    if (input->isOpen() == false) {
      // note: errno is in this thread another variable, than in 
      // the thread which closed the file.
      // here we "fake" the errno var.
      errno=EBADF;
      return 0;
    }
  }
  // successful read
  return bytes;
}


int fseek_func(void *stream, ogg_int64_t offset, int whence) {
  int ret=-1;
  InputStream* input=((VorbisPlugin*)stream)->getInputStream();

  switch(whence) {
  case SEEK_SET:
    ret=input->seek(offset);
    break;
  case SEEK_CUR:
    ret=input->seek(input->getBytePosition()+offset);
    break;
  case SEEK_END:
    ret=input->seek(input->getByteLength());
    break;
  default:
    cout << "fseek_func VorbisPlugn strange call"<<endl;
  }

  if (ret == false) {
    // vorbis does not handle errors in seek at all
    // and if they are handled vorbis segfaults. (RC3)
    // here we always "success" but set an seek_error_mark
    // 
    ret=-1;
  }

  return ret;

}


int fclose_func (void *) {
  //InputStream* input=(InputStream*) stream;

  // its handled different in kmpg
  // we close the stream if the decoder signals eof.
  return true;

}


long ftell_func  (void *stream) {
  InputStream* input=((VorbisPlugin*)stream)->getInputStream();
  return input->getBytePosition();
}


VorbisPlugin::VorbisPlugin() {
  

  memset(&vf, 0, sizeof(vf));
  timeDummy=new TimeStamp();
  pcmout=new char[4096];
  lnoLength=false;
  lshutdown=true;

}


VorbisPlugin::~VorbisPlugin() {
  delete timeDummy;
  delete pcmout;
}


// here we can config our decoder with special flags
void VorbisPlugin::config(const char* key,const char* value,void* user_data) {

  if (strcmp(key,"-c")==0) {
    lnoLength=true;
  }
  DecoderPlugin::config(key,value,user_data);
}


int VorbisPlugin::init() {
  ov_callbacks callbacks;

  callbacks.read_func = fread_func;
  callbacks.seek_func = fseek_func;
  callbacks.close_func = fclose_func;
  callbacks.tell_func = ftell_func;

  // here is the hack to pass the pointer to
  // our streaming interface.
  
  if(ov_open_callbacks(this, &vf, NULL, 0, callbacks) < 0) {
    return false;
  }

  return true;
}


// called by decoder thread
int VorbisPlugin::processVorbis(vorbis_info* vi,vorbis_comment* comment) {

  // decode
  int ret;
  int current_section=-1; /* A vorbis physical bitstream may
			     consist of many logical sections
			     (information for each of which may be
			     fetched from the vf structure).  This
			     value is filled in by ov_read to alert
			     us what section we're currently
			     decoding in case we need to change
			     playback settings at a section
			     boundary */
  ret=ov_read(&vf,pcmout,4096,0,2,1,&current_section);
  switch(ret){
  case 0:
    /* EOF */
    lDecoderLoop=false;
    break;
  case -1:
    /* error in the stream.  Not a problem, just reporting it in
       case we (the app) cares.  In this case, we don't. */
    cout << "error found"<<endl;
    break;  
  default:
    if(current_section!=last_section){
      vi=ov_info(&vf,-1); /* The info struct is different in each
			     section.  vf holds them all for the
			     given bitstream.  This requests the
			     current one */
      
      double timeoffset=ov_time_tell(&vf);
      
      comment = ov_comment(&vf, -1);
      if(comment) {
	cout << "we have a comment:"<<timeoffset<<endl;
      }
    }  
      last_section=current_section;
      output->audioPlay(timeDummy,timeDummy,pcmout,ret);
      break;
    }
  return true;
}


void VorbisPlugin::decoder_loop() {
  vorbis_info *vi=NULL;
  vorbis_comment *comment=NULL;
  last_section=0;
  current_section=0;
       


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

  /********** Decode setup ************/
  // start decoding
  lshutdown=false;
  /** 
      Vorbis RC3 introducted a new bug:
          seek on a closed stream segfaults vorbis.
	  The bugfix in vorbis is (RC3) around line 1190 should be:
                seek_error:
		  // the caller of this goto may not set a return code 
                  ret=OV_ENOSEEK;
      The workaround is to check if we are in a seek operation
      and always "fake" successful reads.
  */
  vorbis_seek_bug_active=false;


  while(runCheck()) {

    switch(streamState) {
    case _STREAM_STATE_FIRST_INIT :
      if (init()== false) {
	// total failure. exit decoding
	lDecoderLoop=false;
	break;
      }	
      // now init stream
      vi=ov_info(&vf,-1);
      if (lnoLength==false) {
	pluginInfo->setLength(getTotalLength());
	output->writeInfo(pluginInfo);
      }
      output->audioOpen();
      output->audioSetup(vi->rate,vi->channels-1,1,0,16);
  

      lhasLength=true;
      setStreamState(_STREAM_STATE_PLAY);
      break;
    case _STREAM_STATE_INIT :
    case _STREAM_STATE_PLAY :
      processVorbis(vi,comment);
      break;
    case _STREAM_STATE_WAIT_FOR_END:
      // exit while loop
      lDecoderLoop=false;
      usleep(2000000);
      break;
    default:
      cout << "unknown stream state vorbis decoder:"<<streamState<<endl;
    }

  }

  lshutdown=true;
  ov_clear(&vf); /* ov_clear closes the stream if its open.  Safe to
		    call on an uninitialized structure as long as
		    we've zeroed it */
  memset(&vf, 0, sizeof(vf));

  output->audioFlush();
}

// vorbis can seek in streams
int VorbisPlugin::seek_impl(int second) {
  vorbis_seek_bug_active=true;
  ov_time_seek(&vf,(double) second);
  vorbis_seek_bug_active=false;
  return true;
}



int VorbisPlugin::getTotalLength() {
  int back=0;
  int byteLen=input->getByteLength();
  if (byteLen == 0) {
    return 0;
  }
  /* Retrieve the length in second*/
  shutdownLock();
  if (lshutdown==false) {
      back = (int) ov_time_total(&vf, -1);
  }
  shutdownUnlock();
  
  return back;
}




#endif
//OGG_VORBIS