docs: remove outdated and pointless 'Last reviewed' lines from docs
[platform/upstream/gst-plugins-base.git] / ext / vorbis / gstvorbisdec.c
1 /* GStreamer
2  * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-vorbisdec
22  * @see_also: vorbisenc, oggdemux
23  *
24  * This element decodes a Vorbis stream to raw float audio.
25  * <ulink url="http://www.vorbis.com/">Vorbis</ulink> is a royalty-free
26  * audio codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
27  * Foundation</ulink>.
28  *
29  * <refsect2>
30  * <title>Example pipelines</title>
31  * |[
32  * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! alsasink
33  * ]| Decode an Ogg/Vorbis. To create an Ogg/Vorbis file refer to the documentation of vorbisenc.
34  * </refsect2>
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #  include "config.h"
39 #endif
40
41 #include "gstvorbisdec.h"
42 #include <string.h>
43 #include <gst/audio/audio.h>
44 #include <gst/tag/tag.h>
45
46 #include "gstvorbiscommon.h"
47
48 #ifndef TREMOR
49 GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
50 #define GST_CAT_DEFAULT vorbisdec_debug
51 #else
52 GST_DEBUG_CATEGORY_EXTERN (ivorbisdec_debug);
53 #define GST_CAT_DEFAULT ivorbisdec_debug
54 #endif
55
56 static GstStaticPadTemplate vorbis_dec_src_factory =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_VORBIS_DEC_SRC_CAPS);
61
62 static GstStaticPadTemplate vorbis_dec_sink_factory =
63 GST_STATIC_PAD_TEMPLATE ("sink",
64     GST_PAD_SINK,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("audio/x-vorbis")
67     );
68
69 #define gst_vorbis_dec_parent_class parent_class
70 G_DEFINE_TYPE (GstVorbisDec, gst_vorbis_dec, GST_TYPE_AUDIO_DECODER);
71
72 static void vorbis_dec_finalize (GObject * object);
73
74 static gboolean vorbis_dec_start (GstAudioDecoder * dec);
75 static gboolean vorbis_dec_stop (GstAudioDecoder * dec);
76 static GstFlowReturn vorbis_dec_handle_frame (GstAudioDecoder * dec,
77     GstBuffer * buffer);
78 static void vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard);
79
80 static void
81 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
82 {
83   GstPadTemplate *src_template, *sink_template;
84   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
86   GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
87
88   gobject_class->finalize = vorbis_dec_finalize;
89
90   src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
91   gst_element_class_add_pad_template (element_class, src_template);
92
93   sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
94   gst_element_class_add_pad_template (element_class, sink_template);
95
96   gst_element_class_set_static_metadata (element_class,
97       "Vorbis audio decoder", "Codec/Decoder/Audio",
98       GST_VORBIS_DEC_DESCRIPTION,
99       "Benjamin Otte <otte@gnome.org>, Chris Lord <chris@openedhand.com>");
100
101   base_class->start = GST_DEBUG_FUNCPTR (vorbis_dec_start);
102   base_class->stop = GST_DEBUG_FUNCPTR (vorbis_dec_stop);
103   base_class->handle_frame = GST_DEBUG_FUNCPTR (vorbis_dec_handle_frame);
104   base_class->flush = GST_DEBUG_FUNCPTR (vorbis_dec_flush);
105 }
106
107 static void
108 gst_vorbis_dec_init (GstVorbisDec * dec)
109 {
110 }
111
112 static void
113 vorbis_dec_finalize (GObject * object)
114 {
115   /* Release any possibly allocated libvorbis data.
116    * _clear functions can safely be called multiple times
117    */
118   GstVorbisDec *vd = GST_VORBIS_DEC (object);
119
120 #ifndef USE_TREMOLO
121   vorbis_block_clear (&vd->vb);
122 #endif
123   vorbis_dsp_clear (&vd->vd);
124   vorbis_comment_clear (&vd->vc);
125   vorbis_info_clear (&vd->vi);
126
127   G_OBJECT_CLASS (parent_class)->finalize (object);
128 }
129
130 static gboolean
131 vorbis_dec_start (GstAudioDecoder * dec)
132 {
133   GstVorbisDec *vd = GST_VORBIS_DEC (dec);
134
135   GST_DEBUG_OBJECT (dec, "start");
136   vorbis_info_init (&vd->vi);
137   vorbis_comment_init (&vd->vc);
138   vd->initialized = FALSE;
139
140   return TRUE;
141 }
142
143 static gboolean
144 vorbis_dec_stop (GstAudioDecoder * dec)
145 {
146   GstVorbisDec *vd = GST_VORBIS_DEC (dec);
147
148   GST_DEBUG_OBJECT (dec, "stop");
149   vd->initialized = FALSE;
150 #ifndef USE_TREMOLO
151   vorbis_block_clear (&vd->vb);
152 #endif
153   vorbis_dsp_clear (&vd->vd);
154   vorbis_comment_clear (&vd->vc);
155   vorbis_info_clear (&vd->vi);
156
157   return TRUE;
158 }
159
160 static GstFlowReturn
161 vorbis_handle_identification_packet (GstVorbisDec * vd)
162 {
163   GstAudioInfo info;
164
165   switch (vd->vi.channels) {
166     case 1:
167     case 2:
168     case 3:
169     case 4:
170     case 5:
171     case 6:
172     case 7:
173     case 8:
174     {
175       const GstAudioChannelPosition *pos;
176
177       pos = gst_vorbis_default_channel_positions[vd->vi.channels - 1];
178       gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate,
179           vd->vi.channels, pos);
180       break;
181     }
182     default:{
183       GstAudioChannelPosition position[64];
184       gint i, max_pos = MAX (vd->vi.channels, 64);
185
186       GST_ELEMENT_WARNING (vd, STREAM, DECODE,
187           (NULL), ("Using NONE channel layout for more than 8 channels"));
188       for (i = 0; i < max_pos; i++)
189         position[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
190       gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate,
191           vd->vi.channels, position);
192       break;
193     }
194   }
195
196   gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (vd), &info);
197
198   vd->info = info;
199   /* select a copy_samples function, this way we can have specialized versions
200    * for mono/stereo and avoid the depth switch in tremor case */
201   vd->copy_samples = gst_vorbis_get_copy_sample_func (info.channels);
202
203   return GST_FLOW_OK;
204 }
205
206 /* FIXME 0.11: remove tag handling and let container take care of that? */
207 static GstFlowReturn
208 vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
209 {
210   guint bitrate = 0;
211   gchar *encoder = NULL;
212   GstTagList *list;
213   guint8 *data;
214   gsize size;
215
216   GST_DEBUG_OBJECT (vd, "parsing comment packet");
217
218   data = gst_ogg_packet_data (packet);
219   size = gst_ogg_packet_size (packet);
220
221   list =
222       gst_tag_list_from_vorbiscomment (data, size, (guint8 *) "\003vorbis", 7,
223       &encoder);
224
225   if (!list) {
226     GST_ERROR_OBJECT (vd, "couldn't decode comments");
227     list = gst_tag_list_new_empty ();
228   }
229
230   if (encoder) {
231     if (encoder[0])
232       gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
233           GST_TAG_ENCODER, encoder, NULL);
234     g_free (encoder);
235   }
236   gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
237       GST_TAG_ENCODER_VERSION, vd->vi.version,
238       GST_TAG_AUDIO_CODEC, "Vorbis", NULL);
239   if (vd->vi.bitrate_nominal > 0 && vd->vi.bitrate_nominal <= 0x7FFFFFFF) {
240     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
241         GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
242     bitrate = vd->vi.bitrate_nominal;
243   }
244   if (vd->vi.bitrate_upper > 0 && vd->vi.bitrate_upper <= 0x7FFFFFFF) {
245     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
246         GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
247     if (!bitrate)
248       bitrate = vd->vi.bitrate_upper;
249   }
250   if (vd->vi.bitrate_lower > 0 && vd->vi.bitrate_lower <= 0x7FFFFFFF) {
251     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
252         GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
253     if (!bitrate)
254       bitrate = vd->vi.bitrate_lower;
255   }
256   if (bitrate) {
257     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
258         GST_TAG_BITRATE, (guint) bitrate, NULL);
259   }
260
261   gst_audio_decoder_merge_tags (GST_AUDIO_DECODER_CAST (vd), list,
262       GST_TAG_MERGE_REPLACE);
263   gst_tag_list_unref (list);
264
265   return GST_FLOW_OK;
266 }
267
268 static GstFlowReturn
269 vorbis_handle_type_packet (GstVorbisDec * vd)
270 {
271   gint res;
272
273   g_assert (vd->initialized == FALSE);
274
275 #ifdef USE_TREMOLO
276   if (G_UNLIKELY ((res = vorbis_dsp_init (&vd->vd, &vd->vi))))
277     goto synthesis_init_error;
278 #else
279   if (G_UNLIKELY ((res = vorbis_synthesis_init (&vd->vd, &vd->vi))))
280     goto synthesis_init_error;
281
282   if (G_UNLIKELY ((res = vorbis_block_init (&vd->vd, &vd->vb))))
283     goto block_init_error;
284 #endif
285
286   vd->initialized = TRUE;
287
288   return GST_FLOW_OK;
289
290   /* ERRORS */
291 synthesis_init_error:
292   {
293     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
294         (NULL), ("couldn't initialize synthesis (%d)", res));
295     return GST_FLOW_ERROR;
296   }
297 block_init_error:
298   {
299     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
300         (NULL), ("couldn't initialize block (%d)", res));
301     return GST_FLOW_ERROR;
302   }
303 }
304
305 static GstFlowReturn
306 vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
307 {
308   GstFlowReturn res;
309   gint ret;
310
311   GST_DEBUG_OBJECT (vd, "parsing header packet");
312
313   /* Packetno = 0 if the first byte is exactly 0x01 */
314   packet->b_o_s = ((gst_ogg_packet_data (packet))[0] == 0x1) ? 1 : 0;
315
316 #ifdef USE_TREMOLO
317   if ((ret = vorbis_dsp_headerin (&vd->vi, &vd->vc, packet)))
318 #else
319   if ((ret = vorbis_synthesis_headerin (&vd->vi, &vd->vc, packet)))
320 #endif
321     goto header_read_error;
322
323   switch ((gst_ogg_packet_data (packet))[0]) {
324     case 0x01:
325       res = vorbis_handle_identification_packet (vd);
326       break;
327     case 0x03:
328       res = vorbis_handle_comment_packet (vd, packet);
329       break;
330     case 0x05:
331       res = vorbis_handle_type_packet (vd);
332       break;
333     default:
334       /* ignore */
335       g_warning ("unknown vorbis header packet found");
336       res = GST_FLOW_OK;
337       break;
338   }
339
340   return res;
341
342   /* ERRORS */
343 header_read_error:
344   {
345     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
346         (NULL), ("couldn't read header packet (%d)", ret));
347     return GST_FLOW_ERROR;
348   }
349 }
350
351 static GstFlowReturn
352 vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
353 {
354   ogg_packet *packet;
355   ogg_packet_wrapper packet_wrapper;
356   GstFlowReturn ret;
357   GstMapInfo map;
358
359   gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map);
360   packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
361
362   ret = vorbis_handle_header_packet (vd, packet);
363
364   gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map);
365
366   return ret;
367 }
368
369 #define MIN_NUM_HEADERS 3
370 static GstFlowReturn
371 vorbis_dec_handle_header_caps (GstVorbisDec * vd)
372 {
373   GstFlowReturn result = GST_FLOW_OK;
374   GstCaps *caps;
375   GstStructure *s = NULL;
376   const GValue *array = NULL;
377
378   caps = gst_pad_get_current_caps (GST_AUDIO_DECODER_SINK_PAD (vd));
379   if (caps)
380     s = gst_caps_get_structure (caps, 0);
381   if (s)
382     array = gst_structure_get_value (s, "streamheader");
383
384   if (caps)
385     gst_caps_unref (caps);
386
387   if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
388     const GValue *value = NULL;
389     GstBuffer *buf = NULL;
390     gint i = 0;
391
392     while (result == GST_FLOW_OK && i < gst_value_array_get_size (array)) {
393       value = gst_value_array_get_value (array, i);
394       buf = gst_value_get_buffer (value);
395       if (!buf)
396         goto null_buffer;
397       result = vorbis_dec_handle_header_buffer (vd, buf);
398       i++;
399     }
400   } else
401     goto array_error;
402
403 done:
404   return (result != GST_FLOW_OK ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
405
406   /* ERRORS */
407 array_error:
408   {
409     GST_WARNING_OBJECT (vd, "streamheader array not found");
410     result = GST_FLOW_ERROR;
411     goto done;
412   }
413 null_buffer:
414   {
415     GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
416     result = GST_FLOW_ERROR;
417     goto done;
418   }
419 }
420
421
422 static GstFlowReturn
423 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
424     GstClockTime timestamp, GstClockTime duration)
425 {
426 #ifdef USE_TREMOLO
427   vorbis_sample_t *pcm;
428 #else
429   vorbis_sample_t **pcm;
430 #endif
431   guint sample_count;
432   GstBuffer *out = NULL;
433   GstFlowReturn result;
434   GstMapInfo map;
435   gsize size;
436
437   if (G_UNLIKELY (!vd->initialized)) {
438     result = vorbis_dec_handle_header_caps (vd);
439     if (result != GST_FLOW_OK)
440       goto not_initialized;
441   }
442
443   /* normal data packet */
444   /* FIXME, we can skip decoding if the packet is outside of the
445    * segment, this is however not very trivial as we need a previous
446    * packet to decode the current one so we must be careful not to
447    * throw away too much. For now we decode everything and clip right
448    * before pushing data. */
449
450 #ifdef USE_TREMOLO
451   if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1)))
452     goto could_not_read;
453 #else
454   if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet)))
455     goto could_not_read;
456
457   if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0))
458     goto not_accepted;
459 #endif
460
461   /* assume all goes well here */
462   result = GST_FLOW_OK;
463
464   /* count samples ready for reading */
465 #ifdef USE_TREMOLO
466   if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
467 #else
468   if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
469     goto done;
470 #endif
471
472   size = sample_count * vd->info.bpf;
473   GST_LOG_OBJECT (vd, "%d samples ready for reading, size %" G_GSIZE_FORMAT,
474       sample_count, size);
475
476   /* alloc buffer for it */
477   out = gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (vd), size);
478
479   gst_buffer_map (out, &map, GST_MAP_WRITE);
480   /* get samples ready for reading now, should be sample_count */
481 #ifdef USE_TREMOLO
482   if (G_UNLIKELY (vorbis_dsp_pcmout (&vd->vd, map.data, sample_count) !=
483           sample_count))
484 #else
485   if (G_UNLIKELY (vorbis_synthesis_pcmout (&vd->vd, &pcm) != sample_count))
486 #endif
487     goto wrong_samples;
488
489 #ifdef USE_TREMOLO
490   if (vd->info.channels < 9)
491     gst_audio_reorder_channels (map.data, map.size, GST_VORBIS_AUDIO_FORMAT,
492         vd->info.channels, gst_vorbis_channel_positions[vd->info.channels - 1],
493         gst_vorbis_default_channel_positions[vd->info.channels - 1]);
494 #else
495   /* copy samples in buffer */
496   vd->copy_samples ((vorbis_sample_t *) map.data, pcm,
497       sample_count, vd->info.channels);
498 #endif
499
500   GST_LOG_OBJECT (vd, "have output size of %" G_GSIZE_FORMAT, size);
501   gst_buffer_unmap (out, &map);
502
503 done:
504   /* whether or not data produced, consume one frame and advance time */
505   result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), out, 1);
506
507 #ifdef USE_TREMOLO
508   vorbis_dsp_read (&vd->vd, sample_count);
509 #else
510   vorbis_synthesis_read (&vd->vd, sample_count);
511 #endif
512
513   return result;
514
515   /* ERRORS */
516 not_initialized:
517   {
518     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
519         (NULL), ("no header sent yet"));
520     return GST_FLOW_NOT_NEGOTIATED;
521   }
522 could_not_read:
523   {
524     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
525         (NULL), ("couldn't read data packet"));
526     return GST_FLOW_ERROR;
527   }
528 not_accepted:
529   {
530     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
531         (NULL), ("vorbis decoder did not accept data packet"));
532     return GST_FLOW_ERROR;
533   }
534 wrong_samples:
535   {
536     gst_buffer_unref (out);
537     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
538         (NULL), ("vorbis decoder reported wrong number of samples"));
539     return GST_FLOW_ERROR;
540   }
541 }
542
543 static GstFlowReturn
544 vorbis_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
545 {
546   ogg_packet *packet;
547   ogg_packet_wrapper packet_wrapper;
548   GstFlowReturn result = GST_FLOW_OK;
549   GstMapInfo map;
550   GstVorbisDec *vd = GST_VORBIS_DEC (dec);
551
552   /* no draining etc */
553   if (G_UNLIKELY (!buffer))
554     return GST_FLOW_OK;
555
556   GST_LOG_OBJECT (vd, "got buffer %p", buffer);
557   /* make ogg_packet out of the buffer */
558   gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map);
559   packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
560   /* set some more stuff */
561   packet->granulepos = -1;
562   packet->packetno = 0;         /* we don't care */
563   /* EOS does not matter, it is used in vorbis to implement clipping the last
564    * block of samples based on the granulepos. We clip based on segments. */
565   packet->e_o_s = 0;
566
567   GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes);
568
569   /* error out on empty header packets, but just skip empty data packets */
570   if (G_UNLIKELY (packet->bytes == 0)) {
571     if (vd->initialized)
572       goto empty_buffer;
573     else
574       goto empty_header;
575   }
576
577   /* switch depending on packet type */
578   if ((gst_ogg_packet_data (packet))[0] & 1) {
579     if (vd->initialized) {
580       GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet");
581       goto done;
582     }
583     result = vorbis_handle_header_packet (vd, packet);
584     if (result != GST_FLOW_OK)
585       goto done;
586     /* consumer header packet/frame */
587     result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), NULL, 1);
588   } else {
589     GstClockTime timestamp, duration;
590
591     timestamp = GST_BUFFER_TIMESTAMP (buffer);
592     duration = GST_BUFFER_DURATION (buffer);
593
594     result = vorbis_handle_data_packet (vd, packet, timestamp, duration);
595   }
596
597 done:
598   GST_LOG_OBJECT (vd, "unmap buffer %p", buffer);
599   gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map);
600
601   return result;
602
603 empty_buffer:
604   {
605     /* don't error out here, just ignore the buffer, it's invalid for vorbis
606      * but not fatal. */
607     GST_WARNING_OBJECT (vd, "empty buffer received, ignoring");
608     result = GST_FLOW_OK;
609     goto done;
610   }
611
612 /* ERRORS */
613 empty_header:
614   {
615     GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
616     result = GST_FLOW_ERROR;
617     goto done;
618   }
619 }
620
621 static void
622 vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard)
623 {
624 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
625   GstVorbisDec *vd = GST_VORBIS_DEC (dec);
626
627   vorbis_synthesis_restart (&vd->vd);
628 #endif
629 }