Way, way, way too many files: Remove crack comment from the 2000 era.
[platform/upstream/gst-plugins-good.git] / ext / speex / gstspeexdec.c
1 /* GStreamer
2  * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "gstspeexdec.h"
25 #include <string.h>
26 #include <gst/tag/tag.h>
27
28 GST_DEBUG_CATEGORY (speexdec_debug);
29 #define GST_CAT_DEFAULT speexdec_debug
30
31 static GstElementDetails speex_dec_details = {
32   "SpeexDec",
33   "Codec/Decoder/Audio",
34   "decode speex streams to audio",
35   "Wim Taymans <wim@fluendo.com>",
36 };
37
38 /* Filter signals and args */
39 enum
40 {
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 #define DEFAULT_ENH             TRUE
46
47 enum
48 {
49   ARG_0,
50   ARG_ENH
51 };
52
53 static GstStaticPadTemplate speex_dec_src_factory =
54 GST_STATIC_PAD_TEMPLATE ("src",
55     GST_PAD_SRC,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("audio/x-raw-int, "
58         "rate = (int) [ 6000, 48000 ], "
59         "channels = (int) [ 1, 2 ], "
60         "endianness = (int) BYTE_ORDER, "
61         "signed = (boolean) true, " "width = (int) 16, " "depth = (int) 16")
62     );
63
64 static GstStaticPadTemplate speex_dec_sink_factory =
65 GST_STATIC_PAD_TEMPLATE ("sink",
66     GST_PAD_SINK,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS ("audio/x-speex")
69     );
70
71 GST_BOILERPLATE (GstSpeexDec, gst_speex_dec, GstElement, GST_TYPE_ELEMENT);
72
73 static void speex_dec_chain (GstPad * pad, GstData * data);
74 static GstElementStateReturn speex_dec_change_state (GstElement * element);
75 static const GstFormat *speex_dec_get_formats (GstPad * pad);
76
77 static gboolean speex_dec_src_event (GstPad * pad, GstEvent * event);
78 static gboolean speex_dec_src_query (GstPad * pad,
79     GstQueryType query, GstFormat * format, gint64 * value);
80 static gboolean speex_dec_convert (GstPad * pad,
81     GstFormat src_format, gint64 src_value,
82     GstFormat * dest_format, gint64 * dest_value);
83
84 static void gst_speexdec_get_property (GObject * object, guint prop_id,
85     GValue * value, GParamSpec * pspec);
86 static void gst_speexdec_set_property (GObject * object, guint prop_id,
87     const GValue * value, GParamSpec * pspec);
88
89 static void
90 gst_speex_dec_base_init (gpointer g_class)
91 {
92   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
93
94   gst_element_class_add_pad_template (element_class,
95       gst_static_pad_template_get (&speex_dec_src_factory));
96   gst_element_class_add_pad_template (element_class,
97       gst_static_pad_template_get (&speex_dec_sink_factory));
98   gst_element_class_set_details (element_class, &speex_dec_details);
99 }
100
101 static void
102 gst_speex_dec_class_init (GstSpeexDecClass * klass)
103 {
104   GObjectClass *gobject_class;
105   GstElementClass *gstelement_class;
106
107   gobject_class = (GObjectClass *) klass;
108   gstelement_class = (GstElementClass *) klass;
109
110   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
111       g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
112           DEFAULT_ENH, G_PARAM_READWRITE));
113
114   gstelement_class->change_state = speex_dec_change_state;
115
116   gobject_class->set_property = gst_speexdec_set_property;
117   gobject_class->get_property = gst_speexdec_get_property;
118
119   GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
120       "speex decoding element");
121 }
122
123 static const GstFormat *
124 speex_dec_get_formats (GstPad * pad)
125 {
126   static GstFormat src_formats[] = {
127     GST_FORMAT_BYTES,
128     GST_FORMAT_DEFAULT,         /* samples in the audio case */
129     GST_FORMAT_TIME,
130     0
131   };
132   static GstFormat sink_formats[] = {
133     GST_FORMAT_BYTES,
134     GST_FORMAT_TIME,
135     GST_FORMAT_DEFAULT,         /* samples */
136     0
137   };
138
139   return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
140 }
141
142 static const GstEventMask *
143 speex_get_event_masks (GstPad * pad)
144 {
145   static const GstEventMask speex_dec_src_event_masks[] = {
146     {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
147     {0,}
148   };
149
150   return speex_dec_src_event_masks;
151 }
152
153 static const GstQueryType *
154 speex_get_query_types (GstPad * pad)
155 {
156   static const GstQueryType speex_dec_src_query_types[] = {
157     GST_QUERY_TOTAL,
158     GST_QUERY_POSITION,
159     0
160   };
161
162   return speex_dec_src_query_types;
163 }
164
165 static void
166 gst_speex_dec_init (GstSpeexDec * dec)
167 {
168   dec->sinkpad =
169       gst_pad_new_from_template (gst_static_pad_template_get
170       (&speex_dec_sink_factory), "sink");
171   gst_pad_set_chain_function (dec->sinkpad, speex_dec_chain);
172   gst_pad_set_formats_function (dec->sinkpad, speex_dec_get_formats);
173   gst_pad_set_convert_function (dec->sinkpad, speex_dec_convert);
174   gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
175
176   dec->srcpad =
177       gst_pad_new_from_template (gst_static_pad_template_get
178       (&speex_dec_src_factory), "src");
179   gst_pad_use_explicit_caps (dec->srcpad);
180   gst_pad_set_event_mask_function (dec->srcpad, speex_get_event_masks);
181   gst_pad_set_event_function (dec->srcpad, speex_dec_src_event);
182   gst_pad_set_query_type_function (dec->srcpad, speex_get_query_types);
183   gst_pad_set_query_function (dec->srcpad, speex_dec_src_query);
184   gst_pad_set_formats_function (dec->srcpad, speex_dec_get_formats);
185   gst_pad_set_convert_function (dec->srcpad, speex_dec_convert);
186   gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
187
188   dec->enh = DEFAULT_ENH;
189
190   GST_FLAG_SET (dec, GST_ELEMENT_EVENT_AWARE);
191 }
192
193 static gboolean
194 speex_dec_convert (GstPad * pad,
195     GstFormat src_format, gint64 src_value,
196     GstFormat * dest_format, gint64 * dest_value)
197 {
198   gboolean res = TRUE;
199   GstSpeexDec *dec;
200   guint64 scale = 1;
201
202   dec = GST_SPEEXDEC (gst_pad_get_parent (pad));
203
204   if (dec->packetno < 1)
205     return FALSE;
206
207   if (pad == dec->sinkpad &&
208       (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
209     return FALSE;
210
211   switch (src_format) {
212     case GST_FORMAT_TIME:
213       switch (*dest_format) {
214         case GST_FORMAT_BYTES:
215           scale = sizeof (float) * dec->header->nb_channels;
216         case GST_FORMAT_DEFAULT:
217           *dest_value = scale * (src_value * dec->header->rate / GST_SECOND);
218           break;
219         default:
220           res = FALSE;
221       }
222       break;
223     case GST_FORMAT_DEFAULT:
224       switch (*dest_format) {
225         case GST_FORMAT_BYTES:
226           *dest_value = src_value * sizeof (float) * dec->header->nb_channels;
227           break;
228         case GST_FORMAT_TIME:
229           *dest_value = src_value * GST_SECOND / dec->header->rate;
230           break;
231         default:
232           res = FALSE;
233       }
234       break;
235     case GST_FORMAT_BYTES:
236       switch (*dest_format) {
237         case GST_FORMAT_DEFAULT:
238           *dest_value = src_value / (sizeof (float) * dec->header->nb_channels);
239           break;
240         case GST_FORMAT_TIME:
241           *dest_value = src_value * GST_SECOND /
242               (dec->header->rate * sizeof (float) * dec->header->nb_channels);
243           break;
244         default:
245           res = FALSE;
246       }
247       break;
248     default:
249       res = FALSE;
250   }
251
252   return res;
253 }
254
255 static gboolean
256 speex_dec_src_query (GstPad * pad, GstQueryType query, GstFormat * format,
257     gint64 * value)
258 {
259   gint64 samples_out = 0;
260   GstSpeexDec *dec = GST_SPEEXDEC (gst_pad_get_parent (pad));
261   GstFormat my_format = GST_FORMAT_DEFAULT;
262
263   if (query == GST_QUERY_POSITION) {
264     samples_out = dec->samples_out;
265   } else {
266     /* query peer in default format */
267     if (!gst_pad_query (GST_PAD_PEER (dec->sinkpad), query, &my_format,
268             &samples_out))
269       return FALSE;
270   }
271
272   /* and convert to the final format */
273   if (!gst_pad_convert (pad, GST_FORMAT_DEFAULT, samples_out, format, value))
274     return FALSE;
275
276   GST_LOG_OBJECT (dec,
277       "query %u: peer returned samples_out: %llu - we return %llu (format %u)\n",
278       query, samples_out, *value, *format);
279   return TRUE;
280 }
281
282 static gboolean
283 speex_dec_src_event (GstPad * pad, GstEvent * event)
284 {
285   gboolean res = TRUE;
286   GstSpeexDec *dec = GST_SPEEXDEC (gst_pad_get_parent (pad));
287
288   switch (GST_EVENT_TYPE (event)) {
289     case GST_EVENT_SEEK:{
290       guint64 value;
291       GstFormat my_format = GST_FORMAT_DEFAULT;
292
293       /* convert to samples_out */
294       res = speex_dec_convert (pad, GST_EVENT_SEEK_FORMAT (event),
295           GST_EVENT_SEEK_OFFSET (event), &my_format, &value);
296       if (res) {
297         GstEvent *real_seek = gst_event_new_seek (
298             (GST_EVENT_SEEK_TYPE (event) & ~GST_SEEK_FORMAT_MASK) |
299             GST_FORMAT_DEFAULT,
300             value);
301
302         res = gst_pad_send_event (GST_PAD_PEER (dec->sinkpad), real_seek);
303       }
304       gst_event_unref (event);
305       break;
306     }
307     default:
308       res = gst_pad_event_default (pad, event);
309       break;
310   }
311
312   return res;
313 }
314
315 static void
316 speex_dec_event (GstSpeexDec * dec, GstEvent * event)
317 {
318   guint64 value, time, bytes;
319
320   GST_LOG_OBJECT (dec, "handling event");
321   switch (GST_EVENT_TYPE (event)) {
322     case GST_EVENT_DISCONTINUOUS:
323       if (gst_event_discont_get_value (event, GST_FORMAT_DEFAULT,
324               (gint64 *) & value)) {
325         dec->samples_out = value;
326         GST_DEBUG_OBJECT (dec,
327             "setting samples_out to %" G_GUINT64_FORMAT " after discont",
328             value);
329       } else {
330         GST_WARNING_OBJECT (dec,
331             "discont event didn't include offset, we might set it wrong now");
332       }
333       if (dec->packetno < 2) {
334         if (dec->samples_out != 0)
335           GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
336               ("can't handle discont before parsing first 2 packets"));
337         dec->packetno = 0;
338         gst_pad_push (dec->srcpad, GST_DATA (gst_event_new_discontinuous (FALSE,
339                     GST_FORMAT_TIME, (guint64) 0, GST_FORMAT_DEFAULT,
340                     (guint64) 0, GST_FORMAT_BYTES, (guint64) 0, 0)));
341       } else {
342         GstFormat time_format, default_format, bytes_format;
343
344         time_format = GST_FORMAT_TIME;
345         default_format = GST_FORMAT_DEFAULT;
346         bytes_format = GST_FORMAT_BYTES;
347
348         dec->packetno = 2;
349         /* if one of them works, all of them work */
350         if (speex_dec_convert (dec->srcpad, GST_FORMAT_DEFAULT,
351                 dec->samples_out, &time_format, &time)
352             && speex_dec_convert (dec->srcpad, GST_FORMAT_DEFAULT,
353                 dec->samples_out, &bytes_format, &bytes)) {
354           gst_pad_push (dec->srcpad,
355               GST_DATA (gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME,
356                       time, GST_FORMAT_DEFAULT, dec->samples_out,
357                       GST_FORMAT_BYTES, bytes, 0)));
358         } else {
359           GST_ERROR_OBJECT (dec,
360               "failed to parse data for DISCONT event, not sending any");
361         }
362       }
363       gst_data_unref (GST_DATA (event));
364       break;
365     default:
366       gst_pad_event_default (dec->sinkpad, event);
367       break;
368   }
369 }
370
371 static void
372 speex_dec_chain (GstPad * pad, GstData * data)
373 {
374   GstBuffer *buf;
375   GstSpeexDec *dec;
376
377   dec = GST_SPEEXDEC (gst_pad_get_parent (pad));
378   if (GST_IS_EVENT (data)) {
379     speex_dec_event (dec, GST_EVENT (data));
380     return;
381   }
382
383   buf = GST_BUFFER (data);
384
385   if (dec->packetno == 0) {
386     GstCaps *caps;
387
388     /* get the header */
389     dec->header =
390         speex_packet_to_header (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
391     gst_data_unref (data);
392     if (!dec->header) {
393       GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
394           (NULL), ("couldn't read header"));
395       return;
396     }
397     if (dec->header->mode >= SPEEX_NB_MODES) {
398       GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
399           (NULL),
400           ("Mode number %d does not (yet/any longer) exist in this version",
401               dec->header->mode));
402       return;
403     }
404
405     dec->mode = (SpeexMode *) speex_mode_list[dec->header->mode];
406
407     /* initialize the decoder */
408     dec->state = speex_decoder_init (dec->mode);
409     if (!dec->state) {
410       GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
411           (NULL), ("couldn't initialize decoder"));
412       gst_data_unref (data);
413       return;
414     }
415
416     speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
417     speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
418
419     if (dec->header->nb_channels != 1) {
420       dec->callback.callback_id = SPEEX_INBAND_STEREO;
421       dec->callback.func = speex_std_stereo_request_handler;
422       dec->callback.data = &dec->stereo;
423       speex_decoder_ctl (dec->state, SPEEX_SET_HANDLER, &dec->callback);
424     }
425
426     speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
427
428     speex_bits_init (&dec->bits);
429
430     /* set caps */
431     caps = gst_caps_new_simple ("audio/x-raw-int",
432         "rate", G_TYPE_INT, dec->header->rate,
433         "channels", G_TYPE_INT, dec->header->nb_channels,
434         "signed", G_TYPE_BOOLEAN, TRUE,
435         "endianness", G_TYPE_INT, G_BYTE_ORDER,
436         "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
437
438     if (!gst_pad_set_explicit_caps (dec->srcpad, caps)) {
439       gst_caps_free (caps);
440       return;
441     }
442     gst_caps_free (caps);
443   } else if (dec->packetno == 1) {
444     gchar *encoder = NULL;
445
446     /* FIXME parse comments */
447     GstTagList *list = gst_tag_list_from_vorbiscomment_buffer (buf, "", 1,
448         &encoder);
449
450     gst_data_unref (data);
451
452     if (!list) {
453       GST_WARNING_OBJECT (dec, "couldn't decode comments");
454       list = gst_tag_list_new ();
455     }
456     if (encoder) {
457       gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
458           GST_TAG_ENCODER, encoder, NULL);
459       g_free (encoder);
460     }
461     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
462         GST_TAG_AUDIO_CODEC, "Speex", NULL);
463     /*
464        gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
465        GST_TAG_ENCODER_VERSION, dec->vi.version, NULL);
466
467        if (dec->vi.bitrate_upper > 0)
468        gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
469        GST_TAG_MAXIMUM_BITRATE, (guint) vd->vi.bitrate_upper, NULL);
470        if (vd->vi.bitrate_nominal > 0)
471        gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
472        GST_TAG_NOMINAL_BITRATE, (guint) vd->vi.bitrate_nominal, NULL);
473        if (vd->vi.bitrate_lower > 0)
474        gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
475        GST_TAG_MINIMUM_BITRATE, (guint) vd->vi.bitrate_lower, NULL);
476      */
477     gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, 0, list);
478   } else {
479     gint i;
480
481     /* send data to the bitstream */
482     speex_bits_read_from (&dec->bits, GST_BUFFER_DATA (buf),
483         GST_BUFFER_SIZE (buf));
484     gst_data_unref (data);
485
486     /* now decode each frame */
487     for (i = 0; i < dec->header->frames_per_packet; i++) {
488       gint ret;
489       GstBuffer *outbuf;
490       gint16 *out_data;
491
492       ret = speex_decode (dec->state, &dec->bits, dec->output);
493       if (ret == -1) {
494         /* uh? end of stream */
495         GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
496         break;
497       } else if (ret == -2) {
498         GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
499         break;
500       }
501       if (speex_bits_remaining (&dec->bits) < 0) {
502         GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
503         break;
504       }
505       if (dec->header->nb_channels == 2)
506         speex_decode_stereo (dec->output, dec->frame_size, &dec->stereo);
507
508       outbuf = gst_pad_alloc_buffer (dec->srcpad, GST_BUFFER_OFFSET_NONE,
509           dec->frame_size * dec->header->nb_channels * 2);
510       out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
511
512       /*PCM saturation (just in case) */
513       for (i = 0; i < dec->frame_size * dec->header->nb_channels; i++) {
514         if (dec->output[i] > 32767.0)
515           out_data[i] = 32767;
516         else if (dec->output[i] < -32768.0)
517           out_data[i] = -32768;
518         else
519           out_data[i] = (gint16) dec->output[i];
520       }
521
522       GST_BUFFER_OFFSET (outbuf) = dec->samples_out;
523       GST_BUFFER_OFFSET_END (outbuf) = dec->samples_out + dec->frame_size;
524       GST_BUFFER_TIMESTAMP (outbuf) =
525           dec->samples_out * GST_SECOND / dec->header->rate;
526       GST_BUFFER_DURATION (outbuf) =
527           dec->frame_size * GST_SECOND / dec->header->rate;
528       gst_pad_push (dec->srcpad, GST_DATA (outbuf));
529       dec->samples_out += dec->frame_size;
530     }
531   }
532   dec->packetno++;
533 }
534
535 static void
536 gst_speexdec_get_property (GObject * object, guint prop_id,
537     GValue * value, GParamSpec * pspec)
538 {
539   GstSpeexDec *speexdec;
540
541   g_return_if_fail (GST_IS_SPEEXDEC (object));
542
543   speexdec = GST_SPEEXDEC (object);
544
545   switch (prop_id) {
546     case ARG_ENH:
547       g_value_set_boolean (value, speexdec->enh);
548       break;
549     default:
550       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
551       break;
552   }
553 }
554
555 static void
556 gst_speexdec_set_property (GObject * object, guint prop_id,
557     const GValue * value, GParamSpec * pspec)
558 {
559   GstSpeexDec *speexdec;
560
561   g_return_if_fail (GST_IS_SPEEXDEC (object));
562
563   speexdec = GST_SPEEXDEC (object);
564
565   switch (prop_id) {
566     case ARG_ENH:
567       speexdec->enh = g_value_get_boolean (value);
568       break;
569     default:
570       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
571       break;
572   }
573 }
574
575
576 static GstElementStateReturn
577 speex_dec_change_state (GstElement * element)
578 {
579   GstSpeexDec *vd = GST_SPEEXDEC (element);
580
581   switch (GST_STATE_TRANSITION (element)) {
582     case GST_STATE_NULL_TO_READY:
583       break;
584     case GST_STATE_READY_TO_PAUSED:
585       break;
586     case GST_STATE_PAUSED_TO_PLAYING:
587       break;
588     case GST_STATE_PLAYING_TO_PAUSED:
589       break;
590     case GST_STATE_PAUSED_TO_READY:
591       vd->packetno = 0;
592       vd->samples_out = 0;
593       break;
594     case GST_STATE_READY_TO_NULL:
595       break;
596     default:
597       break;
598   }
599
600   return parent_class->change_state (element);
601 }