oggvorbis_format.c 9.67 KB
Newer Older
1 2 3 4 5 6 7
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
 * PLEASE READ THESE TERMS BEFORE DISTRIBUTING.                     *
 *                                                                  *
Ralph Giles's avatar
Ralph Giles committed
8
 * THE Ogg123 SOURCE CODE IS (C) COPYRIGHT 2000-2003                *
9 10 11 12 13
 * by Stan Seibert <volsung@xiph.org> AND OTHER CONTRIBUTORS        *
 * http://www.xiph.org/                                             *
 *                                                                  *
 ********************************************************************

Monty's avatar
Monty committed
14
 last mod: $Id$
15 16 17

 ********************************************************************/

18 19 20 21
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Ralph Giles's avatar
Ralph Giles committed
22
#include <stdio.h>
23 24
#include <stdlib.h>
#include <string.h>
25
#include <ctype.h>
26 27 28 29 30
#include <ogg/ogg.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
#include "transport.h"
#include "format.h"
Stan Seibert's avatar
Stan Seibert committed
31
#include "vorbis_comments.h"
32
#include "utf8.h"
33
#include "i18n.h"
34

35 36 37
#ifdef HAVE_OV_READ_FILTER
#include "vgfilter.h"
#endif
38 39 40 41 42 43 44 45 46 47

typedef struct ovf_private_t {
  OggVorbis_File vf;
  vorbis_comment *vc;
  vorbis_info *vi;
  int current_section;

  int bos; /* At beginning of logical bitstream */

  decoder_stats_t stats;
48 49 50
#ifdef HAVE_OV_READ_FILTER
  vgain_state vg;
#endif
51 52 53 54 55 56 57
} ovf_private_t;

/* Forward declarations */
format_t oggvorbis_format;
ov_callbacks vorbisfile_callbacks;


Stan Seibert's avatar
Stan Seibert committed
58 59 60
void print_vorbis_stream_info (decoder_t *decoder);
void print_vorbis_comments (vorbis_comment *vc, decoder_callbacks_t *cb, 
			    void *callback_arg);
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


/* ----------------------------------------------------------- */


int ovf_can_decode (data_source_t *source)
{
  return 1;  /* The file transport is tested last, so always try it */
}


decoder_t* ovf_init (data_source_t *source, ogg123_options_t *ogg123_opts,
		     audio_format_t *audio_fmt,
		     decoder_callbacks_t *callbacks, void *callback_arg)
{
  decoder_t *decoder;
  ovf_private_t *private;
  int ret;


  /* Allocate data source structures */
  decoder = malloc(sizeof(decoder_t));
  private = malloc(sizeof(ovf_private_t));

  if (decoder != NULL && private != NULL) {
    decoder->source = source;
    decoder->actual_fmt = decoder->request_fmt = *audio_fmt;
    decoder->format = &oggvorbis_format;
    decoder->callbacks = callbacks;
    decoder->callback_arg = callback_arg;
    decoder->private = private;

    private->bos = 1;
    private->current_section = -1;

    private->stats.total_time = 0.0;
    private->stats.current_time = 0.0;
    private->stats.instant_bitrate = 0;
    private->stats.avg_bitrate = 0;
100 101 102 103 104

#ifdef HAVE_OV_READ_FILTER
    private->vg.scale_factor = 1.0;
    private->vg.max_scale = 1.0;
#endif
105
  } else {
106
    fprintf(stderr, _("ERROR: Out of memory.\n"));
107 108 109 110
    exit(1);
  }

  /* Initialize vorbisfile decoder */
111

112 113 114 115 116
  ret = ov_open_callbacks (decoder, &private->vf, NULL, 0, 
			   vorbisfile_callbacks);

  if (ret < 0) {
    free(private);
117
/*    free(source);     nope.  caller frees. */
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    return NULL;
  }

  return decoder;
}


int ovf_read (decoder_t *decoder, void *ptr, int nbytes, int *eos,
	      audio_format_t *audio_fmt)
{
  ovf_private_t *priv = decoder->private;
  decoder_callbacks_t *cb = decoder->callbacks;
  int bytes_read = 0;
  int ret;
  int old_section;

  /* Read comments and audio info at the start of a logical bitstream */
  if (priv->bos) {
    priv->vc = ov_comment(&priv->vf, -1);
    priv->vi = ov_info(&priv->vf, -1);

    decoder->actual_fmt.rate = priv->vi->rate;
    decoder->actual_fmt.channels = priv->vi->channels;

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
    switch(decoder->actual_fmt.channels){
    case 1:
      decoder->actual_fmt.matrix="M";
      break;
    case 2:
      decoder->actual_fmt.matrix="L,R";
      break;
    case 3:
      decoder->actual_fmt.matrix="L,C,R";
      break;
    case 4:
      decoder->actual_fmt.matrix="L,R,BL,BR";
      break;
    case 5:
      decoder->actual_fmt.matrix="L,C,R,BL,BR";
      break;
    case 6:
      decoder->actual_fmt.matrix="L,C,R,BL,BR,LFE";
      break;
    case 7:
      decoder->actual_fmt.matrix="L,C,R,SL,SR,BC,LFE";
      break;
    case 8:
      decoder->actual_fmt.matrix="L,C,R,SL,SR,BL,BR,LFE";
      break;
    default:
      decoder->actual_fmt.matrix=NULL;
      break;
    }

172 173 174
#ifdef HAVE_OV_READ_FILTER
    vg_init(&priv->vg, priv->vc);
#endif
175

Stan Seibert's avatar
Stan Seibert committed
176
    print_vorbis_stream_info(decoder);
177
    print_vorbis_comments(priv->vc, cb, decoder->callback_arg);
178 179 180 181 182 183
    priv->bos = 0;
  }

  *audio_fmt = decoder->actual_fmt;

  /* Attempt to read as much audio as is requested */
184
  while (nbytes >= audio_fmt->word_size * audio_fmt->channels) {
185 186

    old_section = priv->current_section;
187 188 189 190 191 192
#ifdef HAVE_OV_READ_FILTER
    ret = ov_read_filter(&priv->vf, ptr, nbytes, audio_fmt->big_endian,
			 audio_fmt->word_size, audio_fmt->signed_sample,
			 &priv->current_section,
			 vg_filter, &priv->vg);
#else
193 194 195
    ret = ov_read(&priv->vf, ptr, nbytes, audio_fmt->big_endian,
		  audio_fmt->word_size, audio_fmt->signed_sample,
		  &priv->current_section);
196
#endif
197 198 199 200 201 202 203 204

    if (ret == 0) {

      /* EOF */
      *eos = 1;
      break;

    } else if (ret == OV_HOLE) {
205

206 207
      if (cb->printf_error != NULL)
	cb->printf_error(decoder->callback_arg, INFO,
208
			   _("--- Hole in the stream; probably harmless\n"));
209

210
    } else if (ret < 0) {
211

212 213
      if (cb->printf_error != NULL)
	cb->printf_error(decoder->callback_arg, ERROR,
214
			 _("=== Vorbis library reported a stream error.\n"));
215

216 217 218
      /* EOF */
      *eos = 1;
      break;
219
    } else {
220

221
      bytes_read += ret;
222
      ptr = (void *)((unsigned char *)ptr + ret);
223 224 225 226 227 228 229 230 231 232 233 234
      nbytes -= ret;

      /* did we enter a new logical bitstream? */
      if (old_section != priv->current_section && old_section != -1) {
	
	*eos = 1;
	priv->bos = 1; /* Read new headers next time through */
	break;
      }
    }

  }
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
  return bytes_read;
}


int ovf_seek (decoder_t *decoder, double offset, int whence)
{
  ovf_private_t *priv = decoder->private;
  int ret;
  double cur;

  if (whence == DECODER_SEEK_CUR) {
    cur = ov_time_tell(&priv->vf);
    if (cur >= 0.0)
      offset += cur;
    else
      return 0;
  }

  ret = ov_time_seek(&priv->vf, offset);
  if (ret == 0)
    return 1;
  else
    return 0;
}


decoder_stats_t *ovf_statistics (decoder_t *decoder)
{
  ovf_private_t *priv = decoder->private;
  long instant_bitrate;
  long avg_bitrate;

  /* ov_time_tell() doesn't work on non-seekable streams, so we use
     ov_pcm_tell()  */
  priv->stats.total_time = (double) ov_pcm_total(&priv->vf, -1) /
    (double) decoder->actual_fmt.rate;
  priv->stats.current_time = (double) ov_pcm_tell(&priv->vf) / 
    (double) decoder->actual_fmt.rate;

  /* vorbisfile returns 0 when no bitrate change has occurred */
  instant_bitrate = ov_bitrate_instant(&priv->vf);
  if (instant_bitrate > 0)
    priv->stats.instant_bitrate = instant_bitrate;

  avg_bitrate = ov_bitrate(&priv->vf, priv->current_section);
  /* Catch error case caused by non-seekable stream */
  priv->stats.avg_bitrate = avg_bitrate > 0 ? avg_bitrate : 0;


  return malloc_decoder_stats(&priv->stats);
}


void ovf_cleanup (decoder_t *decoder)
{
  ovf_private_t *priv = decoder->private;

  ov_clear(&priv->vf);

  free(decoder->private);
  free(decoder);
}


format_t oggvorbis_format = {
  "oggvorbis",
  &ovf_can_decode,
  &ovf_init,
  &ovf_read,
  &ovf_seek,
  &ovf_statistics,
  &ovf_cleanup,
};


/* ------------------- Vorbisfile Callbacks ----------------- */

size_t vorbisfile_cb_read (void *ptr, size_t size, size_t nmemb, void *arg)
{
  decoder_t *decoder = arg;

  return decoder->source->transport->read(decoder->source, ptr, size, nmemb);
}

int vorbisfile_cb_seek (void *arg, ogg_int64_t offset, int whence)
{
  decoder_t *decoder = arg;

  return decoder->source->transport->seek(decoder->source, offset, whence);
}

int vorbisfile_cb_close (void *arg)
{
  return 1; /* Ignore close request so transport can be closed later */
}

long vorbisfile_cb_tell (void *arg)
{
  decoder_t *decoder = arg;

  return decoder->source->transport->tell(decoder->source);
}


ov_callbacks vorbisfile_callbacks = {
  &vorbisfile_cb_read,
  &vorbisfile_cb_seek,
  &vorbisfile_cb_close,
  &vorbisfile_cb_tell
};


/* ------------------- Private functions -------------------- */


Stan Seibert's avatar
Stan Seibert committed
351
void print_vorbis_stream_info (decoder_t *decoder)
352 353 354 355 356 357 358
{
  ovf_private_t *priv = decoder->private;
  decoder_callbacks_t *cb = decoder->callbacks;


  if (cb == NULL || cb->printf_metadata == NULL)
    return;
359

360 361 362 363
  cb->printf_metadata(decoder->callback_arg, 2,
		      _("Ogg Vorbis stream: %d channel, %ld Hz"),
		      priv->vi->channels,
		      priv->vi->rate);
364 365

  cb->printf_metadata(decoder->callback_arg, 3,
366
		      _("Vorbis format: Version %d"), 
367
		      priv->vi->version);
368

369
  cb->printf_metadata(decoder->callback_arg, 3,
370 371
		      _("Bitrate hints: upper=%ld nominal=%ld lower=%ld "
		      "window=%ld"), 
372 373 374 375
		      priv->vi->bitrate_upper,
		      priv->vi->bitrate_nominal,
		      priv->vi->bitrate_lower,
		      priv->vi->bitrate_window);
376

377
  cb->printf_metadata(decoder->callback_arg, 3,
378
		      _("Encoded by: %s"), priv->vc->vendor);
379 380
}

Stan Seibert's avatar
Stan Seibert committed
381 382 383 384 385 386
void print_vorbis_comments (vorbis_comment *vc, decoder_callbacks_t *cb, 
			    void *callback_arg)
{
  int i;
  char *temp = NULL;
  int temp_len = 0;
387

Stan Seibert's avatar
Stan Seibert committed
388
  for (i = 0; i < vc->comments; i++) {
389

Stan Seibert's avatar
Stan Seibert committed
390 391 392 393 394
    /* Gotta null terminate these things */
    if (temp_len < vc->comment_lengths[i] + 1) {
      temp_len = vc->comment_lengths[i] + 1;
      temp = realloc(temp, sizeof(char) * temp_len);
    }
395

Stan Seibert's avatar
Stan Seibert committed
396 397
    strncpy(temp, vc->user_comments[i], vc->comment_lengths[i]);
    temp[vc->comment_lengths[i]] = '\0';
398

Stan Seibert's avatar
Stan Seibert committed
399 400
    print_vorbis_comment(temp, cb, callback_arg);
  }
401

Stan Seibert's avatar
Stan Seibert committed
402 403
  free(temp);
}