summaryrefslogtreecommitdiff
path: root/src/libspeex/xine_decoder.c
blob: 4092d8dfe6861fa8fcd6e13ad87c553cfe84f51f (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* 
 * Copyright (C) 2000-2003 the xine project
 * 
 * This file is part of xine, a free video player.
 * 
 * xine 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 2 of the License, or
 * (at your option) any later version.
 * 
 * xine 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 * $Id: xine_decoder.c,v 1.11 2004/01/12 17:35:17 miguelfreitas Exp $
 *
 * (ogg/)speex audio decoder plugin (libspeex wrapper) for xine
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#define LOG_MODULE "speex_decoder"
#define LOG_VERBOSE
/*
#define LOG
*/

#include "xine_internal.h"
#include "audio_out.h"
#include "buffer.h"

#include <ogg/ogg.h>
#include <speex.h>
#include <speex_header.h>
#include <speex_callbacks.h>
#include <speex_stereo.h>

#define MAX_FRAME_SIZE 2000

typedef struct {
  audio_decoder_class_t   decoder_class;
} speex_class_t;

typedef struct speex_decoder_s {
  audio_decoder_t   audio_decoder;

  int64_t           pts;

  int               output_sampling_rate;
  int               output_open;
  int               output_mode;

  /* speex stuff */
  void             *st;
  int               frame_size;
  int               rate;
  int               nframes;
  int               channels;
  SpeexBits         bits;
  SpeexStereoState  stereo;
  int               expect_metadata;

  float             output[MAX_FRAME_SIZE];

  int               header_count;

  xine_stream_t    *stream;

} speex_decoder_t;


static void speex_reset (audio_decoder_t *this_gen) {

  speex_decoder_t *this = (speex_decoder_t *) this_gen;

  speex_bits_init (&this->bits);
}

static void speex_discontinuity (audio_decoder_t *this_gen) {

  speex_decoder_t *this = (speex_decoder_t *) this_gen;

  this->pts=0;
}

/* Known speex comment keys from ogg123 sources*/
static struct {
  char *key;         /* includes the '=' for programming convenience */
  int   xine_metainfo_index;
} speex_comment_keys[] = {
  {"ARTIST=", XINE_META_INFO_ARTIST},
  {"ALBUM=", XINE_META_INFO_ALBUM},
  {"TITLE=", XINE_META_INFO_TITLE},
  {"GENRE=", XINE_META_INFO_GENRE},
  {"DESCRIPTION=", XINE_META_INFO_COMMENT},
  {"DATE=", XINE_META_INFO_YEAR},
  {NULL, 0}
};

#define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
                           ((buf[base+2]<<16)&0xff0000)| \
                           ((buf[base+1]<<8)&0xff00)| \
                            (buf[base]&0xff))

static
void read_metadata (speex_decoder_t *this, char * comments, int length)
{
  char * c = comments;
  int len, i, nb_fields;
  char * end;

  _x_meta_info_set(this->stream, XINE_META_INFO_AUDIOCODEC, "speex");

  if (length < 8) {
    xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
    return;
  }

  end = c+length;
  len = readint (c, 0);
  c += 4;

  if (c+len > end) {
    xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
    return;
  }

#ifdef LOG
  /* Encoder */
  printf ("libspeex: ");
  fwrite (c, 1, len, stdout);
  printf ("\n");
#endif

  c += len;

  if (c+4 > end) {
    xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
    return;
  }

  nb_fields = readint (c, 0);
  c += 4;

  for (i = 0; i < nb_fields; i++) {
    if (c+4 > end) {
      xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
      return;
    }

    len = readint (c, 0);
    c += 4;
    if (c+len > end) {
      xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: invalid/corrupted comments\n");
      return;
    }

#ifdef LOG
    printf ("libspeex: ");
    fwrite (c, 1, len, stdout);
    printf ("\n");
#endif

    for (i = 0; speex_comment_keys[i].key != NULL; i++) {

      if ( !strncasecmp (speex_comment_keys[i].key, c,
			 strlen(speex_comment_keys[i].key)) ) {
	int keylen = strlen(speex_comment_keys[i].key);
	char meta_info[(len - keylen) + 1];
	
	lprintf ("known metadata %d %d\n",
		 i, speex_comment_keys[i].xine_metainfo_index);
	
	snprintf(meta_info, (len - keylen), "%s", c + keylen);
	_x_meta_info_set(this->stream, speex_comment_keys[i].xine_metainfo_index, meta_info);
      }
    }

    c += len;
  }
}

static void speex_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {

  speex_decoder_t *this = (speex_decoder_t *) this_gen;

  lprintf ("decode buf=%8p content=%8p flags=%08x\n",
	   buf, buf->content, buf->decoder_flags);

  if ( (buf->decoder_flags & BUF_FLAG_HEADER) &&
       !(buf->decoder_flags & BUF_FLAG_STDHEADER) ) {
    lprintf ("preview buffer, %d headers to go\n", this->header_count);

    if (this->header_count) {

      if (!this->st) {
	SpeexMode * spx_mode;
	SpeexHeader * spx_header;
	int modeID;
	int bitrate;

	speex_bits_init (&this->bits);

	spx_header = speex_packet_to_header (buf->content, buf->size);

	if (!spx_header) {
	  xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: could not read Speex header\n");
	  return;
	}

	modeID = spx_header->mode;
	spx_mode = speex_mode_list[modeID];

	if (spx_mode->bitstream_version != spx_header->mode_bitstream_version) {
	  xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: incompatible Speex mode bitstream version\n");
	  return;
	}

	this->st = speex_decoder_init (spx_mode);
	if (!this->st) {
	  xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: decoder initialization failed\n");
	  return;
	}

	this->rate = spx_header->rate;
	speex_decoder_ctl (this->st, SPEEX_SET_SAMPLING_RATE, &this->rate);
	_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE,
	  this->rate);

	this->channels = spx_header->nb_channels;
	if (this->channels == 2) {
	  SpeexCallback callback;
	
	  callback.callback_id = SPEEX_INBAND_STEREO;
	  callback.func = speex_std_stereo_request_handler;
	  callback.data = &this->stereo;
	  speex_decoder_ctl (this->st, SPEEX_SET_HANDLER, &callback);
	}

	this->nframes = spx_header->frames_per_packet;
	if (!this->nframes) this->nframes = 1;
      
	speex_decoder_ctl (this->st, SPEEX_GET_FRAME_SIZE, &this->frame_size);

	speex_decoder_ctl (this->st, SPEEX_GET_BITRATE, &bitrate);
	if (bitrate <= 1) bitrate = 16000; /* assume 16 kbit */
	_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, bitrate);

	this->header_count += spx_header->extra_headers;
	this->expect_metadata = 1;

	free (spx_header);
      } else if (this->expect_metadata) {
	read_metadata (this, buf->content, buf->size);
      }

      this->header_count--;

      if (!this->header_count) {
	int mode = AO_CAP_MODE_MONO;

	switch (this->channels) {
	case 1: 
	  mode = AO_CAP_MODE_MONO;
	  break;
	case 2: 
	  mode = AO_CAP_MODE_STEREO;
	  break;
	case 4: 
	  mode = AO_CAP_MODE_4CHANNEL;
	  break;
	case 5: 
	  mode = AO_CAP_MODE_5CHANNEL;
	  break;
	case 6: 
	  mode = AO_CAP_MODE_5_1CHANNEL;
	  break;
	default:
	  xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, 
		   "libspeex: help, %d channels ?!\n", this->channels);
	  /* FIXME: handle error */
	}
	
	if (!this->output_open) {
	  this->output_open =
	    this->stream->audio_out->open(this->stream->audio_out, 
					  this->stream,
					  16,
					  this->rate,
					  mode);
	}
      }
    }
    
  } else if (this->output_open) {
    int i, j;

    audio_buffer_t *audio_buffer;

    audio_buffer =
      this->stream->audio_out->get_buffer (this->stream->audio_out);

    speex_bits_read_from (&this->bits, buf->content, buf->size);

    for (j = 0; j < this->nframes; j++) {
      int ret;
      int bitrate;
      ogg_int16_t * ptr = audio_buffer->mem;

      ret = speex_decode (this->st, &this->bits, this->output);

      if (ret==-1)
	break;
      if (ret==-2) {
	xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: Decoding error, corrupted stream?\n");
	break;
      }
      if (speex_bits_remaining(&this->bits)<0) {
	xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "libspeex: Decoding overflow, corrupted stream?\n");
	break;
      }

      if (this->channels == 2) {
	speex_decode_stereo (this->output, this->frame_size, &this->stereo);
      }

      speex_decoder_ctl (this->st, SPEEX_GET_BITRATE, &bitrate);
      if (bitrate <= 1) bitrate = 16000; /* assume 16 kbit */
      _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, bitrate);

      /*PCM saturation (just in case)*/
      for (i=0; i < this->frame_size * this->channels; i++)
	{
	  if (this->output[i]>32000.0)
	    this->output[i]=32000.0;
	  else if (this->output[i]<-32000.0)
	    this->output[i]=-32000.0;
	}

      /*Convert to short and play */	
      for (i=0; i< this->frame_size * this->channels; i++) {
	*ptr++ = (ogg_int16_t)this->output[i];
      }

      audio_buffer->vpts       = this->pts;
      this->pts=0;
      audio_buffer->num_frames = this->frame_size;
	
      this->stream->audio_out->put_buffer (this->stream->audio_out, audio_buffer, this->stream);
	
      buf->pts=0;

    }
  }
  else {
    lprintf ("output not open\n");
  }
}

static void speex_dispose (audio_decoder_t *this_gen) {

  speex_decoder_t *this = (speex_decoder_t *) this_gen; 
  
  if (this->st) {
    speex_decoder_destroy (this->st);
  }
  speex_bits_destroy (&this->bits);

  if (this->output_open) 
    this->stream->audio_out->close (this->stream->audio_out, this->stream);

  free (this_gen);
}

static audio_decoder_t *open_plugin (audio_decoder_class_t *class_gen, 
				     xine_stream_t *stream) {

  speex_decoder_t *this ;
  static SpeexStereoState init_stereo = SPEEX_STEREO_STATE_INIT;

  this = (speex_decoder_t *) xine_xmalloc (sizeof (speex_decoder_t));

  this->audio_decoder.decode_data         = speex_decode_data;
  this->audio_decoder.reset               = speex_reset;
  this->audio_decoder.discontinuity       = speex_discontinuity;
  this->audio_decoder.dispose             = speex_dispose;
  this->stream                            = stream;

  this->output_open     = 0;
  this->header_count    = 2;
  this->expect_metadata = 0;

  this->st = NULL;

  this->channels = 1;

  memcpy (&this->stereo, &init_stereo, sizeof (SpeexStereoState));

  return (audio_decoder_t *) this;
}

/*
 * speex plugin class
 */

static char *get_identifier (audio_decoder_class_t *this) {
  return "speex";
}

static char *get_description (audio_decoder_class_t *this) {
  return "Speex audio decoder plugin";
}

static void dispose_class (audio_decoder_class_t *this) {
  free (this);
}

static void *init_plugin (xine_t *xine, void *data) {

  speex_class_t *this;
  
  this = (speex_class_t *) xine_xmalloc (sizeof (speex_class_t));

  this->decoder_class.open_plugin     = open_plugin;
  this->decoder_class.get_identifier  = get_identifier;
  this->decoder_class.get_description = get_description;
  this->decoder_class.dispose         = dispose_class;

  return this;
}

static uint32_t audio_types[] = { 
  BUF_AUDIO_SPEEX, 0
 };

static decoder_info_t dec_info_audio = {
  audio_types,         /* supported types */
  5                    /* priority        */
};

plugin_info_t xine_plugin_info[] = {
  /* type, API, "name", version, special_info, init_function */  
  { PLUGIN_AUDIO_DECODER, 15, "speex", XINE_VERSION_CODE, &dec_info_audio, init_plugin },
  { PLUGIN_NONE, 0, "", 0, NULL, NULL }
};