2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
25 /*#define DEBUG_ENABLED */
26 #include "gstflacdec.h"
27 #include <gst/gsttaginterface.h>
29 #include <gst/tag/tag.h>
31 #include "flac_compat.h"
33 static GstPadTemplate *src_template, *sink_template;
35 /* elementfactory information */
36 GstElementDetails flacdec_details = {
38 "Codec/Decoder/Audio",
39 "Decodes FLAC lossless audio streams",
40 "Wim Taymans <wim.taymans@chello.be>",
43 /* FlacDec signals and args */
56 static void gst_flacdec_base_init (gpointer g_class);
57 static void gst_flacdec_class_init (FlacDecClass * klass);
58 static void gst_flacdec_init (FlacDec * flacdec);
60 static void gst_flacdec_loop (GstElement * element);
61 static GstElementStateReturn gst_flacdec_change_state (GstElement * element);
62 static const GstFormat *gst_flacdec_get_src_formats (GstPad * pad);
63 static gboolean gst_flacdec_convert_src (GstPad * pad, GstFormat src_format,
64 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
65 static const GstQueryType *gst_flacdec_get_src_query_types (GstPad * pad);
66 static gboolean gst_flacdec_src_query (GstPad * pad, GstQueryType type,
67 GstFormat * format, gint64 * value);
68 static const GstEventMask *gst_flacdec_get_src_event_masks (GstPad * pad);
69 static gboolean gst_flacdec_src_event (GstPad * pad, GstEvent * event);
71 static FLAC__SeekableStreamDecoderReadStatus
72 gst_flacdec_read (const FLAC__SeekableStreamDecoder * decoder,
73 FLAC__byte buffer[], unsigned *bytes, void *client_data);
74 static FLAC__SeekableStreamDecoderSeekStatus
75 gst_flacdec_seek (const FLAC__SeekableStreamDecoder * decoder,
76 FLAC__uint64 position, void *client_data);
77 static FLAC__SeekableStreamDecoderTellStatus
78 gst_flacdec_tell (const FLAC__SeekableStreamDecoder * decoder,
79 FLAC__uint64 * position, void *client_data);
80 static FLAC__SeekableStreamDecoderLengthStatus
81 gst_flacdec_length (const FLAC__SeekableStreamDecoder * decoder,
82 FLAC__uint64 * length, void *client_data);
83 static FLAC__bool gst_flacdec_eof (const FLAC__SeekableStreamDecoder * decoder,
85 static FLAC__StreamDecoderWriteStatus
86 gst_flacdec_write (const FLAC__SeekableStreamDecoder * decoder,
87 const FLAC__Frame * frame,
88 const FLAC__int32 * const buffer[], void *client_data);
89 static void gst_flacdec_metadata_callback (const FLAC__SeekableStreamDecoder *
90 decoder, const FLAC__StreamMetadata * metadata, void *client_data);
91 static void gst_flacdec_error_callback (const FLAC__SeekableStreamDecoder *
92 decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
94 static GstElementClass *parent_class = NULL;
96 /*static guint gst_flacdec_signals[LAST_SIGNAL] = { 0 }; */
99 flacdec_get_type (void)
101 static GType flacdec_type = 0;
104 static const GTypeInfo flacdec_info = {
105 sizeof (FlacDecClass),
106 gst_flacdec_base_init,
108 (GClassInitFunc) gst_flacdec_class_init,
113 (GInstanceInitFunc) gst_flacdec_init,
116 g_type_register_static (GST_TYPE_ELEMENT, "FlacDec", &flacdec_info, 0);
122 flac_caps_factory (void)
124 return gst_caps_new_simple ("audio/x-flac", NULL);
125 /* "rate", GST_PROPS_INT_RANGE (11025, 48000),
126 * "channels", GST_PROPS_INT_RANGE (1, 2), */
130 raw_caps_factory (void)
132 return gst_caps_new_simple ("audio/x-raw-int",
133 "endianness", G_TYPE_INT, G_BYTE_ORDER,
134 "signed", G_TYPE_BOOLEAN, TRUE,
135 "width", G_TYPE_INT, 16,
136 "depth", G_TYPE_INT, 16,
137 "rate", GST_TYPE_INT_RANGE, 11025, 48000,
138 "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
142 gst_flacdec_base_init (gpointer g_class)
144 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
145 GstCaps *raw_caps, *flac_caps;
147 raw_caps = raw_caps_factory ();
148 flac_caps = flac_caps_factory ();
150 sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
151 GST_PAD_ALWAYS, flac_caps);
152 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
153 GST_PAD_ALWAYS, raw_caps);
154 gst_element_class_add_pad_template (element_class, sink_template);
155 gst_element_class_add_pad_template (element_class, src_template);
156 gst_element_class_set_details (element_class, &flacdec_details);
160 gst_flacdec_class_init (FlacDecClass * klass)
162 GstElementClass *gstelement_class;
163 GObjectClass *gobject_class;
165 gstelement_class = (GstElementClass *) klass;
166 gobject_class = (GObjectClass *) klass;
168 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
170 gstelement_class->change_state = gst_flacdec_change_state;
174 gst_flacdec_init (FlacDec * flacdec)
176 flacdec->sinkpad = gst_pad_new_from_template (sink_template, "sink");
177 gst_element_add_pad (GST_ELEMENT (flacdec), flacdec->sinkpad);
178 gst_pad_set_convert_function (flacdec->sinkpad, NULL);
180 gst_element_set_loop_function (GST_ELEMENT (flacdec), gst_flacdec_loop);
181 flacdec->srcpad = gst_pad_new_from_template (src_template, "src");
182 gst_element_add_pad (GST_ELEMENT (flacdec), flacdec->srcpad);
183 gst_pad_set_formats_function (flacdec->srcpad, gst_flacdec_get_src_formats);
184 gst_pad_set_convert_function (flacdec->srcpad, gst_flacdec_convert_src);
185 gst_pad_set_query_type_function (flacdec->srcpad,
186 gst_flacdec_get_src_query_types);
187 gst_pad_set_query_function (flacdec->srcpad, gst_flacdec_src_query);
188 gst_pad_set_event_mask_function (flacdec->srcpad,
189 gst_flacdec_get_src_event_masks);
190 gst_pad_set_event_function (flacdec->srcpad, gst_flacdec_src_event);
191 gst_pad_use_explicit_caps (flacdec->srcpad);
193 flacdec->decoder = FLAC__seekable_stream_decoder_new ();
194 flacdec->total_samples = 0;
195 flacdec->init = TRUE;
196 flacdec->eos = FALSE;
197 flacdec->seek_pending = FALSE;
199 FLAC__seekable_stream_decoder_set_read_callback (flacdec->decoder,
201 FLAC__seekable_stream_decoder_set_seek_callback (flacdec->decoder,
203 FLAC__seekable_stream_decoder_set_tell_callback (flacdec->decoder,
205 FLAC__seekable_stream_decoder_set_length_callback (flacdec->decoder,
207 FLAC__seekable_stream_decoder_set_eof_callback (flacdec->decoder,
209 #if FLAC_VERSION >= 0x010003
210 FLAC__seekable_stream_decoder_set_write_callback (flacdec->decoder,
213 FLAC__seekable_stream_decoder_set_write_callback (flacdec->decoder,
214 (FLAC__StreamDecoderWriteStatus (*)
215 (const FLAC__SeekableStreamDecoder * decoder,
216 const FLAC__Frame * frame,
217 const FLAC__int32 * buffer[], void *client_data))
218 (gst_flacdec_write));
220 FLAC__seekable_stream_decoder_set_metadata_respond (flacdec->decoder,
221 FLAC__METADATA_TYPE_VORBIS_COMMENT);
222 FLAC__seekable_stream_decoder_set_metadata_callback (flacdec->decoder,
223 gst_flacdec_metadata_callback);
224 FLAC__seekable_stream_decoder_set_error_callback (flacdec->decoder,
225 gst_flacdec_error_callback);
226 FLAC__seekable_stream_decoder_set_client_data (flacdec->decoder, flacdec);
230 gst_flacdec_update_metadata (FlacDec * flacdec,
231 const FLAC__StreamMetadata * metadata)
234 guint32 number_of_comments, cursor, str_len;
235 gchar *p_value, *value, *name, *str_ptr;
237 list = gst_tag_list_new ();
242 number_of_comments = metadata->data.vorbis_comment.num_comments;
244 GST_DEBUG ("%d tag(s) found", number_of_comments);
245 for (cursor = 0; cursor < number_of_comments; cursor++) {
246 str_ptr = metadata->data.vorbis_comment.comments[cursor].entry;
247 str_len = metadata->data.vorbis_comment.comments[cursor].length;
248 p_value = g_strstr_len (str_ptr, str_len, "=");
250 name = g_strndup (str_ptr, p_value - str_ptr);
251 value = g_strndup (p_value + 1, str_ptr + str_len - p_value - 1);
253 GST_DEBUG ("%s : %s", name, value);
254 gst_vorbis_tag_add (list, name, value);
261 gst_element_found_tags (GST_ELEMENT (flacdec), list);
262 if (GST_PAD_IS_USABLE (flacdec->srcpad)) {
263 gst_pad_push (flacdec->srcpad, GST_DATA (gst_event_new_tag (list)));
270 gst_flacdec_metadata_callback (const FLAC__SeekableStreamDecoder * decoder,
271 const FLAC__StreamMetadata * metadata, void *client_data)
275 flacdec = GST_FLACDEC (client_data);
277 switch (metadata->type) {
278 case FLAC__METADATA_TYPE_STREAMINFO:
279 flacdec->stream_samples = metadata->data.stream_info.total_samples;
281 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
282 gst_flacdec_update_metadata (flacdec, metadata);
290 gst_flacdec_error_callback (const FLAC__SeekableStreamDecoder * decoder,
291 FLAC__StreamDecoderErrorStatus status, void *client_data)
296 flacdec = GST_FLACDEC (client_data);
299 case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
302 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
303 error = "bad header";
305 case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
306 error = "CRC mismatch";
309 error = "unknown error";
313 GST_ELEMENT_ERROR (flacdec, STREAM, DECODE, (NULL), (error));
316 static FLAC__SeekableStreamDecoderSeekStatus
317 gst_flacdec_seek (const FLAC__SeekableStreamDecoder * decoder,
318 FLAC__uint64 position, void *client_data)
322 flacdec = GST_FLACDEC (client_data);
324 GST_DEBUG ("seek %" G_GINT64_FORMAT, position);
325 if (!gst_bytestream_seek (flacdec->bs, position, GST_SEEK_METHOD_SET)) {
326 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
328 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
331 static FLAC__SeekableStreamDecoderTellStatus
332 gst_flacdec_tell (const FLAC__SeekableStreamDecoder * decoder,
333 FLAC__uint64 * position, void *client_data)
337 flacdec = GST_FLACDEC (client_data);
339 *position = gst_bytestream_tell (flacdec->bs);
341 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
343 GST_DEBUG ("tell %" G_GINT64_FORMAT, *position);
345 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
348 static FLAC__SeekableStreamDecoderLengthStatus
349 gst_flacdec_length (const FLAC__SeekableStreamDecoder * decoder,
350 FLAC__uint64 * length, void *client_data)
354 flacdec = GST_FLACDEC (client_data);
356 *length = gst_bytestream_length (flacdec->bs);
358 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
360 GST_DEBUG ("length %" G_GINT64_FORMAT, *length);
362 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
366 gst_flacdec_eof (const FLAC__SeekableStreamDecoder * decoder, void *client_data)
370 flacdec = GST_FLACDEC (client_data);
371 GST_DEBUG ("eof %d", flacdec->eos);
376 static FLAC__SeekableStreamDecoderReadStatus
377 gst_flacdec_read (const FLAC__SeekableStreamDecoder * decoder,
378 FLAC__byte buffer[], unsigned *bytes, void *client_data)
384 flacdec = GST_FLACDEC (client_data);
386 //g_print ("read %u\n", *bytes);
388 while (insize == 0) {
389 insize = gst_bytestream_peek_bytes (flacdec->bs, &indata, *bytes);
390 if (insize < *bytes) {
394 gst_bytestream_get_status (flacdec->bs, &avail, &event);
396 switch (GST_EVENT_TYPE (event)) {
400 gst_event_unref (event);
405 case GST_EVENT_DISCONTINUOUS:
406 GST_DEBUG ("discont");
408 /* we are not yet sending the discont, we'll do that in the next write operation */
409 flacdec->need_discont = TRUE;
410 gst_event_unref (event);
413 gst_pad_event_default (flacdec->sinkpad, event);
417 insize = gst_bytestream_peek_bytes (flacdec->bs, &indata, avail);
423 memcpy (buffer, indata, insize);
425 gst_bytestream_flush_fast (flacdec->bs, insize);
427 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
430 static FLAC__StreamDecoderWriteStatus
431 gst_flacdec_write (const FLAC__SeekableStreamDecoder * decoder,
432 const FLAC__Frame * frame,
433 const FLAC__int32 * const buffer[], void *client_data)
437 guint depth = frame->header.bits_per_sample;
438 guint channels = frame->header.channels;
439 guint samples = frame->header.blocksize;
442 flacdec = GST_FLACDEC (client_data);
444 if (flacdec->need_discont) {
445 gint64 time = 0, bytes = 0;
449 flacdec->need_discont = FALSE;
451 if (!GST_PAD_CAPS (flacdec->srcpad)) {
452 if (flacdec->seek_pending) {
453 flacdec->total_samples = flacdec->seek_value;
456 if (GST_PAD_IS_USABLE (flacdec->srcpad)) {
457 GST_DEBUG ("send discont");
459 format = GST_FORMAT_TIME;
460 gst_pad_convert (flacdec->srcpad, GST_FORMAT_DEFAULT,
461 flacdec->total_samples, &format, &time);
462 format = GST_FORMAT_BYTES;
463 gst_pad_convert (flacdec->srcpad, GST_FORMAT_DEFAULT,
464 flacdec->total_samples, &format, &bytes);
465 discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, time,
466 GST_FORMAT_BYTES, bytes,
467 GST_FORMAT_DEFAULT, flacdec->total_samples, NULL);
469 gst_pad_push (flacdec->srcpad, GST_DATA (discont));
474 if (!GST_PAD_CAPS (flacdec->srcpad)) {
475 gst_pad_set_explicit_caps (flacdec->srcpad,
476 gst_caps_new_simple ("audio/x-raw-int",
477 "endianness", G_TYPE_INT, G_BYTE_ORDER,
478 "signed", G_TYPE_BOOLEAN, TRUE,
479 "width", G_TYPE_INT, depth,
480 "depth", G_TYPE_INT, depth,
481 "rate", G_TYPE_INT, frame->header.sample_rate,
482 "channels", G_TYPE_INT, channels, NULL));
484 flacdec->depth = depth;
485 flacdec->channels = channels;
486 flacdec->frequency = frame->header.sample_rate;
489 if (GST_PAD_IS_USABLE (flacdec->srcpad)) {
490 outbuf = gst_buffer_new ();
491 GST_BUFFER_SIZE (outbuf) = samples * channels * ((depth + 7) >> 3);
492 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
493 GST_BUFFER_TIMESTAMP (outbuf) =
494 flacdec->total_samples * GST_SECOND / frame->header.sample_rate;
497 guint8 *outbuffer = (guint8 *) GST_BUFFER_DATA (outbuf);
499 for (i = 0; i < samples; i++) {
500 for (j = 0; j < channels; j++) {
501 *outbuffer++ = (guint8) buffer[j][i];
504 } else if (depth == 16) {
505 guint16 *outbuffer = (guint16 *) GST_BUFFER_DATA (outbuf);
507 for (i = 0; i < samples; i++) {
508 for (j = 0; j < channels; j++) {
509 *outbuffer++ = (guint16) buffer[j][i];
513 g_warning ("flacdec: invalid depth %d found\n", depth);
514 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
516 gst_pad_push (flacdec->srcpad, GST_DATA (outbuf));
518 flacdec->total_samples += samples;
520 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
524 gst_flacdec_loop (GstElement * element)
529 flacdec = GST_FLACDEC (element);
531 GST_DEBUG ("flacdec: entering loop");
533 FLAC__StreamDecoderState res;
535 GST_DEBUG ("flacdec: initializing decoder");
536 res = FLAC__seekable_stream_decoder_init (flacdec->decoder);
537 if (res != FLAC__SEEKABLE_STREAM_DECODER_OK) {
538 GST_ELEMENT_ERROR (flacdec, LIBRARY, INIT, (NULL),
539 (FLAC__SeekableStreamDecoderStateString[res]));
542 /* FLAC__seekable_stream_decoder_process_metadata (flacdec->decoder); */
543 flacdec->init = FALSE;
546 if (flacdec->seek_pending) {
547 GST_DEBUG ("perform seek to sample %" G_GINT64_FORMAT, flacdec->seek_value);
549 if (FLAC__seekable_stream_decoder_seek_absolute (flacdec->decoder,
550 flacdec->seek_value)) {
551 flacdec->total_samples = flacdec->seek_value;
552 GST_DEBUG ("seek done");
554 GST_DEBUG ("seek failed");
556 flacdec->seek_pending = FALSE;
559 GST_DEBUG ("flacdec: processing single");
560 res = FLAC__seekable_stream_decoder_process_single (flacdec->decoder);
561 GST_DEBUG ("flacdec: checking for EOS");
562 if (FLAC__seekable_stream_decoder_get_state (flacdec->decoder) ==
563 FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM) {
566 GST_DEBUG ("flacdec: sending EOS event");
567 FLAC__seekable_stream_decoder_reset (flacdec->decoder);
569 if (GST_PAD_IS_USABLE (flacdec->srcpad)) {
570 event = gst_event_new (GST_EVENT_EOS);
571 gst_pad_push (flacdec->srcpad, GST_DATA (event));
573 gst_element_set_eos (element);
575 GST_DEBUG ("flacdec: _loop end");
579 static const GstFormat *
580 gst_flacdec_get_src_formats (GstPad * pad)
582 static const GstFormat formats[] = {
592 gst_flacdec_convert_src (GstPad * pad, GstFormat src_format, gint64 src_value,
593 GstFormat * dest_format, gint64 * dest_value)
596 FlacDec *flacdec = GST_FLACDEC (gst_pad_get_parent (pad));
598 gint bytes_per_sample;
600 bytes_per_sample = flacdec->channels * ((flacdec->depth + 7) >> 3);
602 switch (src_format) {
603 case GST_FORMAT_BYTES:
604 switch (*dest_format) {
605 case GST_FORMAT_DEFAULT:
606 if (bytes_per_sample == 0)
608 *dest_value = src_value / bytes_per_sample;
610 case GST_FORMAT_TIME:
612 gint byterate = bytes_per_sample * flacdec->frequency;
616 *dest_value = src_value * GST_SECOND / byterate;
623 case GST_FORMAT_DEFAULT:
624 switch (*dest_format) {
625 case GST_FORMAT_BYTES:
626 *dest_value = src_value * bytes_per_sample;
628 case GST_FORMAT_TIME:
629 if (flacdec->frequency == 0)
631 *dest_value = src_value * GST_SECOND / flacdec->frequency;
637 case GST_FORMAT_TIME:
638 switch (*dest_format) {
639 case GST_FORMAT_BYTES:
640 scale = bytes_per_sample;
641 case GST_FORMAT_DEFAULT:
642 *dest_value = src_value * scale * flacdec->frequency / GST_SECOND;
654 static const GstQueryType *
655 gst_flacdec_get_src_query_types (GstPad * pad)
657 static const GstQueryType types[] = {
666 gst_flacdec_src_query (GstPad * pad, GstQueryType type,
667 GstFormat * format, gint64 * value)
670 FlacDec *flacdec = GST_FLACDEC (gst_pad_get_parent (pad));
673 case GST_QUERY_TOTAL:
677 if (flacdec->stream_samples == 0)
678 samples = flacdec->total_samples;
680 samples = flacdec->stream_samples;
682 gst_pad_convert (flacdec->srcpad,
683 GST_FORMAT_DEFAULT, samples, format, value);
686 case GST_QUERY_POSITION:
687 gst_pad_convert (flacdec->srcpad,
688 GST_FORMAT_DEFAULT, flacdec->total_samples, format, value);
698 static const GstEventMask *
699 gst_flacdec_get_src_event_masks (GstPad * pad)
701 static const GstEventMask masks[] = {
702 {GST_EVENT_SEEK, GST_SEEK_FLAG_ACCURATE},
709 gst_flacdec_src_event (GstPad * pad, GstEvent * event)
712 FlacDec *flacdec = GST_FLACDEC (gst_pad_get_parent (pad));
715 switch (GST_EVENT_TYPE (event)) {
717 format = GST_FORMAT_DEFAULT;
719 if (gst_pad_convert (flacdec->srcpad,
720 GST_EVENT_SEEK_FORMAT (event),
721 GST_EVENT_SEEK_OFFSET (event), &format, &flacdec->seek_value))
722 flacdec->seek_pending = TRUE;
730 gst_event_unref (event);
734 static GstElementStateReturn
735 gst_flacdec_change_state (GstElement * element)
737 FlacDec *flacdec = GST_FLACDEC (element);
739 switch (GST_STATE_TRANSITION (element)) {
740 case GST_STATE_NULL_TO_READY:
741 case GST_STATE_READY_TO_PAUSED:
742 flacdec->bs = gst_bytestream_new (flacdec->sinkpad);
743 flacdec->seek_pending = FALSE;
744 flacdec->total_samples = 0;
745 flacdec->eos = FALSE;
746 if (flacdec->init == FALSE) {
747 FLAC__seekable_stream_decoder_reset (flacdec->decoder);
750 case GST_STATE_PAUSED_TO_PLAYING:
751 flacdec->eos = FALSE;
752 case GST_STATE_PLAYING_TO_PAUSED:
754 case GST_STATE_PAUSED_TO_READY:
755 gst_bytestream_destroy (flacdec->bs);
757 case GST_STATE_READY_TO_NULL:
762 if (GST_ELEMENT_CLASS (parent_class)->change_state)
763 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
765 return GST_STATE_SUCCESS;