gstvorbisdec.c 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GStreamer
 * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de>
 *
 * This library 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; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19
 */

Wim Taymans's avatar
Wim Taymans committed
20 21 22 23 24 25 26
/**
 * SECTION:element-vorbisdec
 * @see_also: vorbisenc, oggdemux
 *
 * This element decodes a Vorbis stream to raw float audio.
 * <ulink url="http://www.vorbis.com/">Vorbis</ulink> is a royalty-free
 * audio codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
27 28 29
 * Foundation</ulink>. As it outputs raw float audio you will often need to
 * put an audioconvert element after it.
 *
30 31
 *
 * <refsect2>
Wim Taymans's avatar
Wim Taymans committed
32
 * <title>Example pipelines</title>
33
 * |[
34
 * gst-launch-1.0 -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! autoaudiosink
35
 * ]| Decode an Ogg/Vorbis. To create an Ogg/Vorbis file refer to the documentation of vorbisenc.
Wim Taymans's avatar
Wim Taymans committed
36 37 38
 * </refsect2>
 */

39 40 41 42
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

43
#include "gstvorbisdec.h"
44
#include <string.h>
45
#include <gst/audio/audio.h>
46 47
#include <gst/tag/tag.h>

48 49
#include "gstvorbiscommon.h"

50
#ifndef TREMOR
51 52
GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
#define GST_CAT_DEFAULT vorbisdec_debug
53 54 55 56
#else
GST_DEBUG_CATEGORY_EXTERN (ivorbisdec_debug);
#define GST_CAT_DEFAULT ivorbisdec_debug
#endif
57

58
static GstStaticPadTemplate vorbis_dec_src_factory =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
59 60 61
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
62
    GST_VORBIS_DEC_SRC_CAPS);
63 64

static GstStaticPadTemplate vorbis_dec_sink_factory =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
65 66 67 68 69
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-vorbis")
    );
70

71
#define gst_vorbis_dec_parent_class parent_class
72
G_DEFINE_TYPE (GstVorbisDec, gst_vorbis_dec, GST_TYPE_AUDIO_DECODER);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
73

74
static void vorbis_dec_finalize (GObject * object);
75 76 77 78 79 80

static gboolean vorbis_dec_start (GstAudioDecoder * dec);
static gboolean vorbis_dec_stop (GstAudioDecoder * dec);
static GstFlowReturn vorbis_dec_handle_frame (GstAudioDecoder * dec,
    GstBuffer * buffer);
static void vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard);
Jan Schmidt's avatar
Jan Schmidt committed
81
static gboolean vorbis_dec_set_format (GstAudioDecoder * dec, GstCaps * caps);
82
static void vorbis_dec_reset (GstAudioDecoder * dec);
83 84

static void
85
gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
86
{
87
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Wim Taymans's avatar
Wim Taymans committed
88
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
Wim Taymans's avatar
Wim Taymans committed
89
  GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
Johan Dahlin's avatar
Johan Dahlin committed
90

91
  gobject_class->finalize = vorbis_dec_finalize;
Johan Dahlin's avatar
Johan Dahlin committed
92

93 94 95 96
  gst_element_class_add_static_pad_template (element_class,
      &vorbis_dec_src_factory);
  gst_element_class_add_static_pad_template (element_class,
      &vorbis_dec_sink_factory);
Wim Taymans's avatar
Wim Taymans committed
97

98
  gst_element_class_set_static_metadata (element_class,
99 100 101
      "Vorbis audio decoder", "Codec/Decoder/Audio",
      GST_VORBIS_DEC_DESCRIPTION,
      "Benjamin Otte <otte@gnome.org>, Chris Lord <chris@openedhand.com>");
102

103 104
  base_class->start = GST_DEBUG_FUNCPTR (vorbis_dec_start);
  base_class->stop = GST_DEBUG_FUNCPTR (vorbis_dec_stop);
Jan Schmidt's avatar
Jan Schmidt committed
105
  base_class->set_format = GST_DEBUG_FUNCPTR (vorbis_dec_set_format);
106 107
  base_class->handle_frame = GST_DEBUG_FUNCPTR (vorbis_dec_handle_frame);
  base_class->flush = GST_DEBUG_FUNCPTR (vorbis_dec_flush);
108 109
}

110
static void
111
gst_vorbis_dec_init (GstVorbisDec * dec)
112
{
113 114 115
  gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
      (dec), TRUE);
  GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
116 117
}

118
static void
119
vorbis_dec_finalize (GObject * object)
120
{
121
  /* Release any possibly allocated libvorbis data.
122 123 124 125
   * _clear functions can safely be called multiple times
   */
  GstVorbisDec *vd = GST_VORBIS_DEC (object);

126
#ifndef USE_TREMOLO
127
  vorbis_block_clear (&vd->vb);
128
#endif
129 130 131
  vorbis_dsp_clear (&vd->vd);
  vorbis_comment_clear (&vd->vc);
  vorbis_info_clear (&vd->vi);
132 133

  G_OBJECT_CLASS (parent_class)->finalize (object);
134 135
}

136
static gboolean
137
vorbis_dec_start (GstAudioDecoder * dec)
138
{
139
  GstVorbisDec *vd = GST_VORBIS_DEC (dec);
140

141 142 143 144
  GST_DEBUG_OBJECT (dec, "start");
  vorbis_info_init (&vd->vi);
  vorbis_comment_init (&vd->vc);
  vd->initialized = FALSE;
145

146
  return TRUE;
147
}
148 149

static gboolean
150
vorbis_dec_stop (GstAudioDecoder * dec)
151
{
152
  GstVorbisDec *vd = GST_VORBIS_DEC (dec);
Wim Taymans's avatar
Wim Taymans committed
153

154 155 156 157 158 159 160 161
  GST_DEBUG_OBJECT (dec, "stop");
  vd->initialized = FALSE;
#ifndef USE_TREMOLO
  vorbis_block_clear (&vd->vb);
#endif
  vorbis_dsp_clear (&vd->vd);
  vorbis_comment_clear (&vd->vc);
  vorbis_info_clear (&vd->vi);
162

163
  return TRUE;
164 165
}

166
static GstFlowReturn
167
vorbis_handle_identification_packet (GstVorbisDec * vd)
168
{
Wim Taymans's avatar
Wim Taymans committed
169
  GstAudioInfo info;
170

171
  switch (vd->vi.channels) {
172 173
    case 1:
    case 2:
174 175 176 177 178
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
Wim Taymans's avatar
Wim Taymans committed
179 180
    case 8:
    {
181 182
      const GstAudioChannelPosition *pos;

183
      pos = gst_vorbis_default_channel_positions[vd->vi.channels - 1];
184 185
      gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate,
          vd->vi.channels, pos);
186
      break;
187 188
    }
    default:{
189
      GstAudioChannelPosition position[64];
190
      gint i, max_pos = MAX (vd->vi.channels, 64);
191

Wim Taymans's avatar
Wim Taymans committed
192
      GST_ELEMENT_WARNING (vd, STREAM, DECODE,
193
          (NULL), ("Using NONE channel layout for more than 8 channels"));
Wim Taymans's avatar
Wim Taymans committed
194
      for (i = 0; i < max_pos; i++)
195 196 197
        position[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
      gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate,
          vd->vi.channels, position);
198
      break;
199
    }
200
  }
201

202
  gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (vd), &info);
203

Wim Taymans's avatar
Wim Taymans committed
204
  vd->info = info;
205 206
  /* select a copy_samples function, this way we can have specialized versions
   * for mono/stereo and avoid the depth switch in tremor case */
207
  vd->copy_samples = gst_vorbis_get_copy_sample_func (info.channels);
208 209 210 211

  return GST_FLOW_OK;
}

212
/* FIXME 0.11: remove tag handling and let container take care of that? */
213 214 215 216 217
static GstFlowReturn
vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
{
  guint bitrate = 0;
  gchar *encoder = NULL;
218
  GstTagList *list;
Wim Taymans's avatar
Wim Taymans committed
219 220
  guint8 *data;
  gsize size;
221 222 223

  GST_DEBUG_OBJECT (vd, "parsing comment packet");

Wim Taymans's avatar
Wim Taymans committed
224 225
  data = gst_ogg_packet_data (packet);
  size = gst_ogg_packet_size (packet);
226

227
  list =
Wim Taymans's avatar
Wim Taymans committed
228
      gst_tag_list_from_vorbiscomment (data, size, (guint8 *) "\003vorbis", 7,
229 230
      &encoder);

231
  if (!list) {
232
    GST_ERROR_OBJECT (vd, "couldn't decode comments");
233
    list = gst_tag_list_new_empty ();
234 235 236
  }

  if (encoder) {
237
    if (encoder[0])
238
      gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
239
          GST_TAG_ENCODER, encoder, NULL);
240 241
    g_free (encoder);
  }
242
  gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
243 244
      GST_TAG_ENCODER_VERSION, vd->vi.version,
      GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
245
  if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
246
    gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
247 248 249
        GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
    bitrate = vd->vi.bitrate_nominal;
  }
250
  if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
251
    gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
252 253 254 255
        GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
    if (!bitrate)
      bitrate = vd->vi.bitrate_upper;
  }
256
  if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
257
    gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
258 259 260 261 262
        GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
    if (!bitrate)
      bitrate = vd->vi.bitrate_lower;
  }
  if (bitrate) {
263
    gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
264 265 266
        GST_TAG_BITRATE, (guint) bitrate, NULL);
  }

267 268
  gst_audio_decoder_merge_tags (GST_AUDIO_DECODER_CAST (vd), list,
      GST_TAG_MERGE_REPLACE);
269
  gst_tag_list_unref (list);
270 271 272 273 274 275 276

  return GST_FLOW_OK;
}

static GstFlowReturn
vorbis_handle_type_packet (GstVorbisDec * vd)
{
277
  gint res;
278

279
  g_assert (!vd->initialized);
280

281 282 283 284
#ifdef USE_TREMOLO
  if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
    goto synthesis_init_error;
#else
285 286 287 288 289
  if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
    goto synthesis_init_error;

  if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
    goto block_init_error;
290
#endif
291

292 293 294
  vd->initialized = TRUE;

  return GST_FLOW_OK;
295 296 297 298 299 300 301 302 303 304 305 306 307 308

  /* ERRORS */
synthesis_init_error:
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
        (NULL), ("couldn't initialize synthesis (%d)", res));
    return GST_FLOW_ERROR;
  }
block_init_error:
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
        (NULL), ("couldn't initialize block (%d)", res));
    return GST_FLOW_ERROR;
  }
309 310
}

311 312 313 314
static GstFlowReturn
vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
{
  GstFlowReturn res;
315
  gint ret;
316

317
  GST_DEBUG_OBJECT (vd, "parsing header packet");
318 319

  /* Packetno = 0 if the first byte is exactly 0x01 */
320
  packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
321

322
#ifdef USE_TREMOLO
323 324
  if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
#else
325
  if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
326
#endif
327 328
    goto header_read_error;

329
  switch ((gst_ogg_packet_data (packet))[0]) {
330
    case 0x01:
331 332
      res = vorbis_handle_identification_packet (vd);
      break;
333
    case 0x03:
334 335
      res = vorbis_handle_comment_packet (vd, packet);
      break;
336
    case 0x05:
337
      res = vorbis_handle_type_packet (vd);
338 339 340
      break;
    default:
      /* ignore */
341
      g_warning ("unknown vorbis header packet found");
342 343 344
      res = GST_FLOW_OK;
      break;
  }
345

346 347 348 349 350 351
  return res;

  /* ERRORS */
header_read_error:
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
352
        (NULL), ("couldn't read header packet (%d)", ret));
353 354 355 356
    return GST_FLOW_ERROR;
  }
}

Wim Taymans's avatar
Wim Taymans committed
357
static GstFlowReturn
358
vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
Wim Taymans's avatar
Wim Taymans committed
359
{
360 361
  ogg_packet *packet;
  ogg_packet_wrapper packet_wrapper;
Wim Taymans's avatar
Wim Taymans committed
362
  GstFlowReturn ret;
Wim Taymans's avatar
Wim Taymans committed
363
  GstMapInfo map;
364

Wim Taymans's avatar
Wim Taymans committed
365
  gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map);
366
  packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
367

Wim Taymans's avatar
Wim Taymans committed
368
  ret = vorbis_handle_header_packet (vd, packet);
369

Wim Taymans's avatar
Wim Taymans committed
370
  gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map);
Wim Taymans's avatar
Wim Taymans committed
371

Wim Taymans's avatar
Wim Taymans committed
372
  return ret;
Wim Taymans's avatar
Wim Taymans committed
373 374
}

375
#define MIN_NUM_HEADERS 3
376
static GstFlowReturn
377
vorbis_dec_handle_header_caps (GstVorbisDec * vd)
378 379
{
  GstFlowReturn result = GST_FLOW_OK;
380 381 382
  GstCaps *caps;
  GstStructure *s = NULL;
  const GValue *array = NULL;
383

Wim Taymans's avatar
Wim Taymans committed
384
  caps = gst_pad_get_current_caps (GST_AUDIO_DECODER_SINK_PAD (vd));
385 386 387 388
  if (caps)
    s = gst_caps_get_structure (caps, 0);
  if (s)
    array = gst_structure_get_value (s, "streamheader");
389

Wim Taymans's avatar
Wim Taymans committed
390 391 392
  if (caps)
    gst_caps_unref (caps);

393 394 395 396
  if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
    const GValue *value = NULL;
    GstBuffer *buf = NULL;
    gint i = 0;
397

398
    while (result == GST_FLOW_OK && i < gst_value_array_get_size (array)) {
399 400 401 402 403 404 405 406 407
      value = gst_value_array_get_value (array, i);
      buf = gst_value_get_buffer (value);
      if (!buf)
        goto null_buffer;
      result = vorbis_dec_handle_header_buffer (vd, buf);
      i++;
    }
  } else
    goto array_error;
408

409 410
done:
  return (result != GST_FLOW_OK ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
411

412 413 414 415 416 417 418 419 420 421 422 423
  /* ERRORS */
array_error:
  {
    GST_WARNING_OBJECT (vd, "streamheader array not found");
    result = GST_FLOW_ERROR;
    goto done;
  }
null_buffer:
  {
    GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
    result = GST_FLOW_ERROR;
    goto done;
424
  }
Wim Taymans's avatar
Wim Taymans committed
425 426
}

427

428
static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
429 430
vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
    GstClockTime timestamp, GstClockTime duration)
431
{
432
#ifdef USE_TREMOLO
433 434
  vorbis_sample_t *pcm;
#else
435
  vorbis_sample_t **pcm;
436
#endif
437
  guint sample_count;
438
  GstBuffer *out = NULL;
439
  GstFlowReturn result;
Wim Taymans's avatar
Wim Taymans committed
440
  GstMapInfo map;
Wim Taymans's avatar
Wim Taymans committed
441
  gsize size;
442

443 444 445 446 447
  if (G_UNLIKELY (!vd->initialized)) {
    result = vorbis_dec_handle_header_caps (vd);
    if (result != GST_FLOW_OK)
      goto not_initialized;
  }
448 449

  /* normal data packet */
450 451
  /* FIXME, we can skip decoding if the packet is outside of the
   * segment, this is however not very trivial as we need a previous
Piotr Fusik's avatar
Piotr Fusik committed
452
   * packet to decode the current one so we must be careful not to
453 454
   * throw away too much. For now we decode everything and clip right
   * before pushing data. */
455

456
#ifdef USE_TREMOLO
457
  if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1)))
458 459
    goto could_not_read;
#else
460
  if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
461 462
    goto could_not_read;

463
  if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
464
    goto not_accepted;
465
#endif
466

467 468
  /* assume all goes well here */
  result = GST_FLOW_OK;
469

470
  /* count samples ready for reading */
471 472 473
#ifdef USE_TREMOLO
  if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
#else
474 475
  if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
    goto done;
476
#endif
477

Wim Taymans's avatar
Wim Taymans committed
478
  size = sample_count * vd->info.bpf;
479 480
  GST_LOG_OBJECT (vd, "%d samples ready for reading, size %" G_GSIZE_FORMAT,
      sample_count, size);
481

482
  /* alloc buffer for it */
483
  out = gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (vd), size);
484

Wim Taymans's avatar
Wim Taymans committed
485
  gst_buffer_map (out, &map, GST_MAP_WRITE);
486
  /* get samples ready for reading now, should be sample_count */
487
#ifdef USE_TREMOLO
Wim Taymans's avatar
Wim Taymans committed
488
  if (G_UNLIKELY (vorbis_dsp_pcmout (&vd->vd, map.data, sample_count) !=
Wim Taymans's avatar
Wim Taymans committed
489
          sample_count))
490
#else
491
  if (G_UNLIKELY (vorbis_synthesis_pcmout (&vd->vd, &pcm) != sample_count))
492
#endif
493
    goto wrong_samples;
494

495 496
#ifdef USE_TREMOLO
  if (vd->info.channels < 9)
Wim Taymans's avatar
Wim Taymans committed
497
    gst_audio_reorder_channels (map.data, map.size, GST_VORBIS_AUDIO_FORMAT,
498 499 500
        vd->info.channels, gst_vorbis_channel_positions[vd->info.channels - 1],
        gst_vorbis_default_channel_positions[vd->info.channels - 1]);
#else
501
  /* copy samples in buffer */
Wim Taymans's avatar
Wim Taymans committed
502
  vd->copy_samples ((vorbis_sample_t *) map.data, pcm,
Wim Taymans's avatar
Wim Taymans committed
503
      sample_count, vd->info.channels);
504
#endif
505

Wim Taymans's avatar
Wim Taymans committed
506 507
  GST_LOG_OBJECT (vd, "have output size of %" G_GSIZE_FORMAT, size);
  gst_buffer_unmap (out, &map);
508

509
done:
510 511 512
  /* whether or not data produced, consume one frame and advance time */
  result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), out, 1);

513 514 515
#ifdef USE_TREMOLO
  vorbis_dsp_read (&vd->vd, sample_count);
#else
516
  vorbis_synthesis_read (&vd->vd, sample_count);
517
#endif
518

519 520 521 522 523 524
  return result;

  /* ERRORS */
not_initialized:
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
525
        (NULL), ("no header sent yet"));
526
    return GST_FLOW_NOT_NEGOTIATED;
527 528 529 530 531 532 533 534 535 536 537 538 539
  }
could_not_read:
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
        (NULL), ("couldn't read data packet"));
    return GST_FLOW_ERROR;
  }
not_accepted:
  {
    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
        (NULL), ("vorbis decoder did not accept data packet"));
    return GST_FLOW_ERROR;
  }
540 541 542 543 544 545 546
wrong_samples:
  {
    gst_buffer_unref (out);
    GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
        (NULL), ("vorbis decoder reported wrong number of samples"));
    return GST_FLOW_ERROR;
  }
547 548
}

549
static GstFlowReturn
550
vorbis_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
551 552 553 554
{
  ogg_packet *packet;
  ogg_packet_wrapper packet_wrapper;
  GstFlowReturn result = GST_FLOW_OK;
Wim Taymans's avatar
Wim Taymans committed
555
  GstMapInfo map;
556
  GstVorbisDec *vd = GST_VORBIS_DEC (dec);
Wim Taymans's avatar
Wim Taymans committed
557

558 559 560
  /* no draining etc */
  if (G_UNLIKELY (!buffer))
    return GST_FLOW_OK;
561

Wim Taymans's avatar
Wim Taymans committed
562
  GST_LOG_OBJECT (vd, "got buffer %p", buffer);
563
  /* make ogg_packet out of the buffer */
Wim Taymans's avatar
Wim Taymans committed
564
  gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map);
565 566 567 568
  packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
  /* set some more stuff */
  packet->granulepos = -1;
  packet->packetno = 0;         /* we don't care */
Wim Taymans's avatar
Wim Taymans committed
569 570
  /* EOS does not matter, it is used in vorbis to implement clipping the last
   * block of samples based on the granulepos. We clip based on segments. */
571
  packet->e_o_s = 0;
572

573
  GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
Wim Taymans's avatar
Wim Taymans committed
574

575
  /* error out on empty header packets, but just skip empty data packets */
576
  if (G_UNLIKELY (packet->bytes == 0)) {
577 578 579 580 581
    if (vd->initialized)
      goto empty_buffer;
    else
      goto empty_header;
  }
582

583
  /* switch depending on packet type */
584
  if ((gst_ogg_packet_data (packet))[0] & 1) {
585
    if (vd->initialized) {
586 587
      GST_INFO_OBJECT (vd, "already initialized, re-init");
      vorbis_dec_reset (dec);
588
    }
589
    result = vorbis_handle_header_packet (vd, packet);
590 591
    if (result != GST_FLOW_OK)
      goto done;
592
    /* consumer header packet/frame */
593
    result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), NULL, 1);
594
  } else {
Wim Taymans's avatar
Wim Taymans committed
595 596 597 598 599
    GstClockTime timestamp, duration;

    timestamp = GST_BUFFER_TIMESTAMP (buffer);
    duration = GST_BUFFER_DURATION (buffer);

600
    result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
601
  }
602

603
done:
Wim Taymans's avatar
Wim Taymans committed
604
  GST_LOG_OBJECT (vd, "unmap buffer %p", buffer);
Wim Taymans's avatar
Wim Taymans committed
605
  gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map);
Wim Taymans's avatar
Wim Taymans committed
606

607
  return result;
608

609
empty_buffer:
610
  {
611 612 613
    /* don't error out here, just ignore the buffer, it's invalid for vorbis
     * but not fatal. */
    GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
614
    result = GST_FLOW_OK;
615 616 617 618 619 620 621 622
    goto done;
  }

/* ERRORS */
empty_header:
  {
    GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
    result = GST_FLOW_ERROR;
623 624
    goto done;
  }
625 626
}

627 628
static void
vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard)
629
{
Thijs Vermeir's avatar
Thijs Vermeir committed
630
#ifdef HAVE_VORBIS_SYNTHESIS_RESTART
631
  GstVorbisDec *vd = GST_VORBIS_DEC (dec);
632

633
  vorbis_synthesis_restart (&vd->vd);
634
#endif
635
}
Jan Schmidt's avatar
Jan Schmidt committed
636

637 638
static void
vorbis_dec_reset (GstAudioDecoder * dec)
Jan Schmidt's avatar
Jan Schmidt committed
639 640 641 642 643 644 645 646 647 648 649 650 651
{
  GstVorbisDec *vd = GST_VORBIS_DEC (dec);

  vd->initialized = FALSE;
#ifndef USE_TREMOLO
  vorbis_block_clear (&vd->vb);
#endif
  vorbis_dsp_clear (&vd->vd);

  vorbis_comment_clear (&vd->vc);
  vorbis_info_clear (&vd->vi);
  vorbis_info_init (&vd->vi);
  vorbis_comment_init (&vd->vc);
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
}

static gboolean
vorbis_dec_set_format (GstAudioDecoder * dec, GstCaps * caps)
{
  GstVorbisDec *vd = GST_VORBIS_DEC (dec);

  GST_DEBUG_OBJECT (vd, "New caps %" GST_PTR_FORMAT " - resetting", caps);

  /* A set_format call implies new data with new header packets */
  if (!vd->initialized)
    return TRUE;

  /* We need to free and re-init libvorbis,
   * or it chokes */
  vorbis_dec_reset (dec);
Jan Schmidt's avatar
Jan Schmidt committed
668 669 670

  return TRUE;
}