309854ab60875e43f141eb0c29d08c6ffd7d8e32
[platform/upstream/gst-plugins-good.git] / ext / flac / gstflacdec.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) <2006> Jan Schmidt <thaytan at mad scientist com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-flacdec
24  * @see_also: #GstFlacEnc
25  *
26  * flacdec decodes FLAC streams.
27  * <ulink url="http://flac.sourceforge.net/">FLAC</ulink>
28  * is a Free Lossless Audio Codec.
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch filesrc location=media/small/dark.441-16-s.flac ! flacdec ! audioconvert ! audioresample ! autoaudiosink
34  * ]|
35  * |[
36  * gst-launch gnomevfssrc location=http://gstreamer.freedesktop.org/media/small/dark.441-16-s.flac ! flacdec ! audioconvert ! audioresample ! queue min-threshold-buffers=10 ! autoaudiosink
37  * ]|
38  * </refsect2>
39  */
40
41 /* TODO: add seeking when operating chain-based with unframed input */
42 /* FIXME: demote/remove granulepos handling and make more time-centric */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 #include <string.h>
48
49 #include "gstflacdec.h"
50 #include <gst/gst-i18n-plugin.h>
51 #include <gst/gsttagsetter.h>
52 #include <gst/base/gsttypefindhelper.h>
53 #include <gst/audio/multichannel.h>
54 #include <gst/tag/tag.h>
55
56 /* Taken from http://flac.sourceforge.net/format.html#frame_header */
57 static const GstAudioChannelPosition channel_positions[8][8] = {
58   {GST_AUDIO_CHANNEL_POSITION_FRONT_MONO},
59   {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
60       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
61         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
62         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
63       GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}, {
64         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
65         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
66         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
67       GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
68         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
69         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
70         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
71         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
72       GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
73         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
74         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
75         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
76         GST_AUDIO_CHANNEL_POSITION_LFE,
77         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
78       GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT},
79   /* FIXME: 7/8 channel layouts are not defined in the FLAC specs */
80   {
81         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
82         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
83         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
84         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
85         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
86         GST_AUDIO_CHANNEL_POSITION_LFE,
87       GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, {
88         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
89         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
90         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
91         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
92         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
93         GST_AUDIO_CHANNEL_POSITION_LFE,
94         GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
95       GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}
96 };
97
98 GST_DEBUG_CATEGORY_STATIC (flacdec_debug);
99 #define GST_CAT_DEFAULT flacdec_debug
100
101 static void gst_flac_dec_finalize (GObject * object);
102 static void gst_flac_dec_loop (GstPad * pad);
103
104 static GstStateChangeReturn gst_flac_dec_change_state (GstElement * element,
105     GstStateChange transition);
106 static const GstQueryType *gst_flac_dec_get_src_query_types (GstPad * pad);
107 static const GstQueryType *gst_flac_dec_get_sink_query_types (GstPad * pad);
108 static gboolean gst_flac_dec_sink_query (GstPad * pad, GstQuery * query);
109 static gboolean gst_flac_dec_src_query (GstPad * pad, GstQuery * query);
110 static gboolean gst_flac_dec_convert_src (GstPad * pad, GstFormat src_format,
111     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
112 static gboolean gst_flac_dec_src_event (GstPad * pad, GstEvent * event);
113 static gboolean gst_flac_dec_sink_activate (GstPad * sinkpad);
114 static gboolean gst_flac_dec_sink_activate_pull (GstPad * sinkpad,
115     gboolean active);
116 static gboolean gst_flac_dec_sink_activate_push (GstPad * sinkpad,
117     gboolean active);
118 static gboolean gst_flac_dec_sink_event (GstPad * pad, GstEvent * event);
119 static GstFlowReturn gst_flac_dec_chain (GstPad * pad, GstBuffer * buf);
120
121 static void gst_flac_dec_reset_decoders (GstFlacDec * flacdec);
122 static void gst_flac_dec_setup_decoder (GstFlacDec * flacdec);
123
124 static FLAC__StreamDecoderReadStatus
125 gst_flac_dec_read_seekable (const FLAC__StreamDecoder * decoder,
126     FLAC__byte buffer[], size_t * bytes, void *client_data);
127 static FLAC__StreamDecoderReadStatus
128 gst_flac_dec_read_stream (const FLAC__StreamDecoder * decoder,
129     FLAC__byte buffer[], size_t * bytes, void *client_data);
130 static FLAC__StreamDecoderSeekStatus
131 gst_flac_dec_seek (const FLAC__StreamDecoder * decoder,
132     FLAC__uint64 position, void *client_data);
133 static FLAC__StreamDecoderTellStatus
134 gst_flac_dec_tell (const FLAC__StreamDecoder * decoder,
135     FLAC__uint64 * position, void *client_data);
136 static FLAC__StreamDecoderLengthStatus
137 gst_flac_dec_length (const FLAC__StreamDecoder * decoder,
138     FLAC__uint64 * length, void *client_data);
139 static FLAC__bool gst_flac_dec_eof (const FLAC__StreamDecoder * decoder,
140     void *client_data);
141 static FLAC__StreamDecoderWriteStatus
142 gst_flac_dec_write_stream (const FLAC__StreamDecoder * decoder,
143     const FLAC__Frame * frame,
144     const FLAC__int32 * const buffer[], void *client_data);
145 static void gst_flac_dec_metadata_cb (const FLAC__StreamDecoder *
146     decoder, const FLAC__StreamMetadata * metadata, void *client_data);
147 static void gst_flac_dec_error_cb (const FLAC__StreamDecoder *
148     decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
149
150 GST_BOILERPLATE (GstFlacDec, gst_flac_dec, GstElement, GST_TYPE_ELEMENT);
151
152 /* FIXME 0.11: Use width=32 for all depths and let audioconvert
153  * handle the conversions instead of doing it ourself.
154  */
155 #define GST_FLAC_DEC_SRC_CAPS                             \
156     "audio/x-raw-int, "                                   \
157     "endianness = (int) BYTE_ORDER, "                     \
158     "signed = (boolean) true, "                           \
159     "width = (int) { 8, 16, 32 }, "                       \
160     "depth = (int) [ 4, 32 ], "                           \
161     "rate = (int) [ 1, 655350 ], "                        \
162     "channels = (int) [ 1, 8 ]"
163
164 static GstStaticPadTemplate flac_dec_src_factory =
165 GST_STATIC_PAD_TEMPLATE ("src",
166     GST_PAD_SRC,
167     GST_PAD_ALWAYS,
168     GST_STATIC_CAPS (GST_FLAC_DEC_SRC_CAPS));
169 static GstStaticPadTemplate flac_dec_sink_factory =
170 GST_STATIC_PAD_TEMPLATE ("sink",
171     GST_PAD_SINK,
172     GST_PAD_ALWAYS,
173     GST_STATIC_CAPS ("audio/x-flac")
174     );
175
176 static void
177 gst_flac_dec_base_init (gpointer g_class)
178 {
179   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
180
181   gst_element_class_add_pad_template (element_class,
182       gst_static_pad_template_get (&flac_dec_src_factory));
183   gst_element_class_add_pad_template (element_class,
184       gst_static_pad_template_get (&flac_dec_sink_factory));
185   gst_element_class_set_details_simple (element_class, "FLAC audio decoder",
186       "Codec/Decoder/Audio",
187       "Decodes FLAC lossless audio streams", "Wim Taymans <wim@fluendo.com>");
188
189   GST_DEBUG_CATEGORY_INIT (flacdec_debug, "flacdec", 0, "flac decoder");
190 }
191
192 static void
193 gst_flac_dec_class_init (GstFlacDecClass * klass)
194 {
195   GstElementClass *gstelement_class;
196   GObjectClass *gobject_class;
197
198   gstelement_class = (GstElementClass *) klass;
199   gobject_class = (GObjectClass *) klass;
200
201   gobject_class->finalize = gst_flac_dec_finalize;
202
203   gstelement_class->change_state =
204       GST_DEBUG_FUNCPTR (gst_flac_dec_change_state);
205 }
206
207 static void
208 gst_flac_dec_init (GstFlacDec * flacdec, GstFlacDecClass * klass)
209 {
210   flacdec->sinkpad =
211       gst_pad_new_from_static_template (&flac_dec_sink_factory, "sink");
212   gst_pad_set_activate_function (flacdec->sinkpad,
213       GST_DEBUG_FUNCPTR (gst_flac_dec_sink_activate));
214   gst_pad_set_activatepull_function (flacdec->sinkpad,
215       GST_DEBUG_FUNCPTR (gst_flac_dec_sink_activate_pull));
216   gst_pad_set_activatepush_function (flacdec->sinkpad,
217       GST_DEBUG_FUNCPTR (gst_flac_dec_sink_activate_push));
218   gst_pad_set_query_type_function (flacdec->sinkpad,
219       GST_DEBUG_FUNCPTR (gst_flac_dec_get_sink_query_types));
220   gst_pad_set_query_function (flacdec->sinkpad,
221       GST_DEBUG_FUNCPTR (gst_flac_dec_sink_query));
222   gst_pad_set_event_function (flacdec->sinkpad,
223       GST_DEBUG_FUNCPTR (gst_flac_dec_sink_event));
224   gst_pad_set_chain_function (flacdec->sinkpad,
225       GST_DEBUG_FUNCPTR (gst_flac_dec_chain));
226   gst_element_add_pad (GST_ELEMENT (flacdec), flacdec->sinkpad);
227
228   flacdec->srcpad =
229       gst_pad_new_from_static_template (&flac_dec_src_factory, "src");
230   gst_pad_set_query_type_function (flacdec->srcpad,
231       GST_DEBUG_FUNCPTR (gst_flac_dec_get_src_query_types));
232   gst_pad_set_query_function (flacdec->srcpad,
233       GST_DEBUG_FUNCPTR (gst_flac_dec_src_query));
234   gst_pad_set_event_function (flacdec->srcpad,
235       GST_DEBUG_FUNCPTR (gst_flac_dec_src_event));
236   gst_pad_use_fixed_caps (flacdec->srcpad);
237   gst_element_add_pad (GST_ELEMENT (flacdec), flacdec->srcpad);
238
239   gst_flac_dec_reset_decoders (flacdec);
240 }
241
242 static void
243 gst_flac_dec_reset_decoders (GstFlacDec * flacdec)
244 {
245   /* Clean up the decoder */
246   if (flacdec->decoder) {
247     FLAC__stream_decoder_delete (flacdec->decoder);
248     flacdec->decoder = NULL;
249   }
250
251   if (flacdec->adapter) {
252     gst_adapter_clear (flacdec->adapter);
253     g_object_unref (flacdec->adapter);
254     flacdec->adapter = NULL;
255   }
256
257   if (flacdec->close_segment) {
258     gst_event_unref (flacdec->close_segment);
259     flacdec->close_segment = NULL;
260   }
261   if (flacdec->start_segment) {
262     gst_event_unref (flacdec->start_segment);
263     flacdec->start_segment = NULL;
264   }
265   if (flacdec->tags) {
266     gst_tag_list_free (flacdec->tags);
267     flacdec->tags = NULL;
268   }
269   if (flacdec->pending) {
270     gst_buffer_unref (flacdec->pending);
271     flacdec->pending = NULL;
272   }
273
274   flacdec->segment.last_stop = 0;
275   flacdec->offset = 0;
276   flacdec->init = TRUE;
277 }
278
279 static void
280 gst_flac_dec_setup_decoder (GstFlacDec * dec)
281 {
282   gst_flac_dec_reset_decoders (dec);
283
284   dec->tags = gst_tag_list_new ();
285   gst_tag_list_add (dec->tags, GST_TAG_MERGE_REPLACE,
286       GST_TAG_AUDIO_CODEC, "FLAC", NULL);
287
288   dec->adapter = gst_adapter_new ();
289
290   dec->decoder = FLAC__stream_decoder_new ();
291
292   /* no point calculating since it's never checked here */
293   FLAC__stream_decoder_set_md5_checking (dec->decoder, false);
294   FLAC__stream_decoder_set_metadata_respond (dec->decoder,
295       FLAC__METADATA_TYPE_VORBIS_COMMENT);
296   FLAC__stream_decoder_set_metadata_respond (dec->decoder,
297       FLAC__METADATA_TYPE_PICTURE);
298 }
299
300 static void
301 gst_flac_dec_finalize (GObject * object)
302 {
303   GstFlacDec *flacdec;
304
305   flacdec = GST_FLAC_DEC (object);
306
307   gst_flac_dec_reset_decoders (flacdec);
308
309   G_OBJECT_CLASS (parent_class)->finalize (object);
310 }
311
312
313 static gboolean
314 gst_flac_dec_update_metadata (GstFlacDec * flacdec,
315     const FLAC__StreamMetadata * metadata)
316 {
317   GstTagList *list;
318   guint num, i;
319
320   if (flacdec->tags)
321     list = flacdec->tags;
322   else
323     flacdec->tags = list = gst_tag_list_new ();
324
325   num = metadata->data.vorbis_comment.num_comments;
326   GST_DEBUG_OBJECT (flacdec, "%u tag(s) found", num);
327
328   for (i = 0; i < num; ++i) {
329     gchar *vc, *name, *value;
330
331     vc = g_strndup ((gchar *) metadata->data.vorbis_comment.comments[i].entry,
332         metadata->data.vorbis_comment.comments[i].length);
333
334     if (gst_tag_parse_extended_comment (vc, &name, NULL, &value, TRUE)) {
335       GST_DEBUG_OBJECT (flacdec, "%s : %s", name, value);
336       if (value && strlen (value))
337         gst_vorbis_tag_add (list, name, value);
338       g_free (name);
339       g_free (value);
340     }
341
342     g_free (vc);
343   }
344
345   return TRUE;
346 }
347
348 /* CRC-8, poly = x^8 + x^2 + x^1 + x^0, init = 0 */
349 static const guint8 crc8_table[256] = {
350   0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
351   0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
352   0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
353   0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
354   0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5,
355   0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
356   0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85,
357   0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
358   0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
359   0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
360   0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2,
361   0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
362   0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32,
363   0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
364   0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
365   0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
366   0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C,
367   0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
368   0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC,
369   0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
370   0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
371   0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
372   0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C,
373   0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
374   0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B,
375   0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
376   0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
377   0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
378   0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
379   0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
380   0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
381   0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
382 };
383
384 static guint8
385 gst_flac_calculate_crc8 (guint8 * data, guint length)
386 {
387   guint8 crc = 0;
388
389   while (length--) {
390     crc = crc8_table[crc ^ *data];
391     ++data;
392   }
393
394   return crc;
395 }
396
397 static gboolean
398 gst_flac_dec_scan_got_frame (GstFlacDec * flacdec, guint8 * data, guint size,
399     gint64 * last_sample_num)
400 {
401   guint headerlen;
402   guint sr_from_end = 0;        /* can be 0, 8 or 16 */
403   guint bs_from_end = 0;        /* can be 0, 8 or 16 */
404   guint32 val = 0;
405   guint8 bs, sr, ca, ss, pb;
406
407   if (size < 10)
408     return FALSE;
409
410   /* sync */
411   if (data[0] != 0xFF || data[1] != 0xF8)
412     return FALSE;
413
414   bs = (data[2] & 0xF0) >> 8;   /* blocksize marker   */
415   sr = (data[2] & 0x0F);        /* samplerate marker  */
416   ca = (data[3] & 0xF0) >> 8;   /* channel assignment */
417   ss = (data[3] & 0x0F) >> 1;   /* sample size marker */
418   pb = (data[3] & 0x01);        /* padding bit        */
419
420   GST_LOG_OBJECT (flacdec,
421       "got sync, bs=%x,sr=%x,ca=%x,ss=%x,pb=%x", bs, sr, ca, ss, pb);
422
423   if (sr == 0x0F || ca >= 0x0B || ss == 0x03 || ss == 0x07) {
424     return FALSE;
425   }
426
427   /* read block size from end of header? */
428   if (bs == 6)
429     bs_from_end = 8;
430   else if (bs == 7)
431     bs_from_end = 16;
432
433   /* read sample rate from end of header? */
434   if (sr == 0x0C)
435     sr_from_end = 8;
436   else if (sr == 0x0D || sr == 0x0E)
437     sr_from_end = 16;
438
439   val = (guint32) g_utf8_get_char_validated ((gchar *) data + 4, -1);
440
441   if (val == (guint32) - 1 || val == (guint32) - 2) {
442     GST_LOG_OBJECT (flacdec, "failed to read sample/frame");
443     return FALSE;
444   }
445
446   headerlen = 4 + g_unichar_to_utf8 ((gunichar) val, NULL) +
447       (bs_from_end / 8) + (sr_from_end / 8);
448
449   if (gst_flac_calculate_crc8 (data, headerlen) != data[headerlen])
450     return FALSE;
451
452   if (flacdec->min_blocksize == flacdec->max_blocksize) {
453     *last_sample_num = (val + 1) * flacdec->min_blocksize;
454   } else {
455     *last_sample_num = val;     /* FIXME: + length of last block in samples */
456   }
457
458   if (flacdec->sample_rate > 0) {
459     GST_DEBUG_OBJECT (flacdec, "last sample %" G_GINT64_FORMAT " = %"
460         GST_TIME_FORMAT, *last_sample_num,
461         GST_TIME_ARGS (*last_sample_num * GST_SECOND / flacdec->sample_rate));
462   }
463
464   return TRUE;
465 }
466
467 #define SCANBLOCK_SIZE  (64*1024)
468
469 static void
470 gst_flac_dec_scan_for_last_block (GstFlacDec * flacdec, gint64 * samples)
471 {
472   GstFormat format = GST_FORMAT_BYTES;
473
474   gint64 file_size, offset;
475
476   GST_INFO_OBJECT (flacdec, "total number of samples unknown, scanning file");
477
478   if (!gst_pad_query_peer_duration (flacdec->sinkpad, &format, &file_size)) {
479     GST_WARNING_OBJECT (flacdec, "failed to query upstream size!");
480     return;
481   }
482
483   GST_DEBUG_OBJECT (flacdec, "upstream size: %" G_GINT64_FORMAT, file_size);
484
485   offset = file_size - 1;
486   while (offset >= MAX (SCANBLOCK_SIZE / 2, file_size / 2)) {
487     GstFlowReturn flow;
488     GstBuffer *buf = NULL;
489     guint8 *data;
490     guint size;
491
492     /* divide by 2 = not very sophisticated way to deal with overlapping */
493     offset -= SCANBLOCK_SIZE / 2;
494     GST_LOG_OBJECT (flacdec, "looking for frame at %" G_GINT64_FORMAT
495         "-%" G_GINT64_FORMAT, offset, offset + SCANBLOCK_SIZE);
496
497     flow = gst_pad_pull_range (flacdec->sinkpad, offset, SCANBLOCK_SIZE, &buf);
498     if (flow != GST_FLOW_OK) {
499       GST_DEBUG_OBJECT (flacdec, "flow = %s", gst_flow_get_name (flow));
500       return;
501     }
502
503     size = GST_BUFFER_SIZE (buf);
504     data = GST_BUFFER_DATA (buf);
505
506     while (size > 16) {
507       if (gst_flac_dec_scan_got_frame (flacdec, data, size, samples)) {
508         GST_DEBUG_OBJECT (flacdec, "frame sync at offset %" G_GINT64_FORMAT,
509             offset + GST_BUFFER_SIZE (buf) - size);
510         gst_buffer_unref (buf);
511         return;
512       }
513       ++data;
514       --size;
515     }
516
517     gst_buffer_unref (buf);
518   }
519 }
520
521 static void
522 gst_flac_extract_picture_buffer (GstFlacDec * dec,
523     const FLAC__StreamMetadata * metadata)
524 {
525   FLAC__StreamMetadata_Picture picture;
526   GstTagList *tags;
527
528   g_return_if_fail (metadata->type == FLAC__METADATA_TYPE_PICTURE);
529
530   GST_LOG_OBJECT (dec, "Got PICTURE block");
531   picture = metadata->data.picture;
532
533   GST_DEBUG_OBJECT (dec, "declared MIME type is: '%s'",
534       GST_STR_NULL (picture.mime_type));
535   GST_DEBUG_OBJECT (dec, "image data is %u bytes", picture.data_length);
536
537   tags = gst_tag_list_new ();
538
539   gst_tag_list_add_id3_image (tags, (guint8 *) picture.data,
540       picture.data_length, picture.type);
541
542   if (!gst_tag_list_is_empty (tags)) {
543     gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, tags);
544   } else {
545     GST_DEBUG_OBJECT (dec, "problem parsing PICTURE block, skipping");
546     gst_tag_list_free (tags);
547   }
548 }
549
550 static void
551 gst_flac_dec_metadata_cb (const FLAC__StreamDecoder * decoder,
552     const FLAC__StreamMetadata * metadata, void *client_data)
553 {
554   GstFlacDec *flacdec = GST_FLAC_DEC (client_data);
555
556   GST_LOG_OBJECT (flacdec, "metadata type: %d", metadata->type);
557
558   switch (metadata->type) {
559     case FLAC__METADATA_TYPE_STREAMINFO:{
560       gint64 samples;
561       guint depth;
562
563       samples = metadata->data.stream_info.total_samples;
564
565       flacdec->min_blocksize = metadata->data.stream_info.min_blocksize;
566       flacdec->max_blocksize = metadata->data.stream_info.max_blocksize;
567       flacdec->sample_rate = metadata->data.stream_info.sample_rate;
568       flacdec->depth = depth = metadata->data.stream_info.bits_per_sample;
569       flacdec->channels = metadata->data.stream_info.channels;
570
571       if (depth < 9)
572         flacdec->width = 8;
573       else if (depth < 17)
574         flacdec->width = 16;
575       else
576         flacdec->width = 32;
577
578       GST_DEBUG_OBJECT (flacdec, "blocksize: min=%u, max=%u",
579           flacdec->min_blocksize, flacdec->max_blocksize);
580       GST_DEBUG_OBJECT (flacdec, "sample rate: %u, channels: %u",
581           flacdec->sample_rate, flacdec->channels);
582       GST_DEBUG_OBJECT (flacdec, "depth: %u, width: %u", flacdec->depth,
583           flacdec->width);
584
585       /* Only scan for last block in pull-mode, since it uses pull_range() */
586       if (samples == 0 && !flacdec->streaming) {
587         gst_flac_dec_scan_for_last_block (flacdec, &samples);
588       }
589
590       GST_DEBUG_OBJECT (flacdec, "total samples = %" G_GINT64_FORMAT, samples);
591
592       /* in framed mode the demuxer/parser upstream has already pushed a
593        * newsegment event in TIME format which we've passed on */
594       if (samples > 0 && !flacdec->framed) {
595         gint64 duration;
596
597         gst_segment_set_duration (&flacdec->segment, GST_FORMAT_DEFAULT,
598             samples);
599
600         /* convert duration to time */
601         duration = gst_util_uint64_scale_int (samples, GST_SECOND,
602             flacdec->sample_rate);
603
604         /* fixme, at this time we could seek to the queued seek event if we have
605          * any */
606         if (flacdec->start_segment)
607           gst_event_unref (flacdec->start_segment);
608         flacdec->start_segment =
609             gst_event_new_new_segment_full (FALSE,
610             flacdec->segment.rate, flacdec->segment.applied_rate,
611             GST_FORMAT_TIME, 0, duration, 0);
612       }
613       break;
614     }
615     case FLAC__METADATA_TYPE_PICTURE:{
616       gst_flac_extract_picture_buffer (flacdec, metadata);
617       break;
618     }
619     case FLAC__METADATA_TYPE_VORBIS_COMMENT:
620       gst_flac_dec_update_metadata (flacdec, metadata);
621       break;
622     default:
623       break;
624   }
625 }
626
627 static void
628 gst_flac_dec_error_cb (const FLAC__StreamDecoder * d,
629     FLAC__StreamDecoderErrorStatus status, void *client_data)
630 {
631   const gchar *error;
632   GstFlacDec *dec;
633
634   dec = GST_FLAC_DEC (client_data);
635
636   switch (status) {
637     case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
638       /* Ignore this error and keep processing */
639       return;
640     case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
641       error = "bad header";
642       break;
643     case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
644       error = "CRC mismatch";
645       break;
646     default:
647       error = "unknown error";
648       break;
649   }
650
651   GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("%s (%d)", error, status));
652   dec->last_flow = GST_FLOW_ERROR;
653 }
654
655 static FLAC__StreamDecoderSeekStatus
656 gst_flac_dec_seek (const FLAC__StreamDecoder * decoder,
657     FLAC__uint64 position, void *client_data)
658 {
659   GstFlacDec *flacdec;
660
661   flacdec = GST_FLAC_DEC (client_data);
662
663   GST_DEBUG_OBJECT (flacdec, "seek %" G_GUINT64_FORMAT, (guint64) position);
664   flacdec->offset = position;
665
666   return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
667 }
668
669 static FLAC__StreamDecoderTellStatus
670 gst_flac_dec_tell (const FLAC__StreamDecoder * decoder,
671     FLAC__uint64 * position, void *client_data)
672 {
673   GstFlacDec *flacdec;
674
675   flacdec = GST_FLAC_DEC (client_data);
676
677   *position = flacdec->offset;
678
679   GST_DEBUG_OBJECT (flacdec, "tell %" G_GINT64_FORMAT, (gint64) * position);
680
681   return FLAC__STREAM_DECODER_TELL_STATUS_OK;
682 }
683
684 static FLAC__StreamDecoderLengthStatus
685 gst_flac_dec_length (const FLAC__StreamDecoder * decoder,
686     FLAC__uint64 * length, void *client_data)
687 {
688   GstFlacDec *flacdec;
689   GstFormat fmt = GST_FORMAT_BYTES;
690   gint64 len;
691   GstPad *peer;
692
693   flacdec = GST_FLAC_DEC (client_data);
694
695   if (!(peer = gst_pad_get_peer (flacdec->sinkpad)))
696     return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
697
698   gst_pad_query_duration (peer, &fmt, &len);
699   gst_object_unref (peer);
700   if (fmt != GST_FORMAT_BYTES || len == -1)
701     return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
702
703   *length = len;
704
705   GST_DEBUG_OBJECT (flacdec, "encoded byte length %" G_GINT64_FORMAT,
706       (gint64) * length);
707
708   return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
709 }
710
711 static FLAC__bool
712 gst_flac_dec_eof (const FLAC__StreamDecoder * decoder, void *client_data)
713 {
714   GstFlacDec *flacdec;
715   GstFormat fmt;
716   GstPad *peer;
717   gboolean ret = FALSE;
718   gint64 len;
719
720   flacdec = GST_FLAC_DEC (client_data);
721
722   if (!(peer = gst_pad_get_peer (flacdec->sinkpad))) {
723     GST_WARNING_OBJECT (flacdec, "no peer pad, returning EOF");
724     return TRUE;
725   }
726
727   fmt = GST_FORMAT_BYTES;
728   if (gst_pad_query_duration (peer, &fmt, &len) && fmt == GST_FORMAT_BYTES &&
729       len != -1 && flacdec->offset >= len) {
730     GST_DEBUG_OBJECT (flacdec,
731         "offset=%" G_GINT64_FORMAT ", len=%" G_GINT64_FORMAT
732         ", returning EOF", flacdec->offset, len);
733     ret = TRUE;
734   }
735
736   gst_object_unref (peer);
737
738   return ret;
739 }
740
741 static FLAC__StreamDecoderReadStatus
742 gst_flac_dec_read_seekable (const FLAC__StreamDecoder * decoder,
743     FLAC__byte buffer[], size_t * bytes, void *client_data)
744 {
745   GstFlowReturn flow;
746   GstFlacDec *flacdec;
747   GstBuffer *buf;
748
749   flacdec = GST_FLAC_DEC (client_data);
750
751   flow = gst_pad_pull_range (flacdec->sinkpad, flacdec->offset, *bytes, &buf);
752
753   GST_PAD_STREAM_LOCK (flacdec->sinkpad);
754   flacdec->pull_flow = flow;
755   GST_PAD_STREAM_UNLOCK (flacdec->sinkpad);
756
757   if (G_UNLIKELY (flow != GST_FLOW_OK)) {
758     GST_INFO_OBJECT (flacdec, "pull_range flow: %s", gst_flow_get_name (flow));
759     if (flow == GST_FLOW_UNEXPECTED)
760       return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
761     else
762       return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
763   }
764
765   GST_DEBUG_OBJECT (flacdec, "Read %d bytes at %" G_GUINT64_FORMAT,
766       GST_BUFFER_SIZE (buf), flacdec->offset);
767   memcpy (buffer, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
768   *bytes = GST_BUFFER_SIZE (buf);
769   gst_buffer_unref (buf);
770   flacdec->offset += *bytes;
771
772   return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
773 }
774
775 static FLAC__StreamDecoderReadStatus
776 gst_flac_dec_read_stream (const FLAC__StreamDecoder * decoder,
777     FLAC__byte buffer[], size_t * bytes, void *client_data)
778 {
779   GstFlacDec *dec = GST_FLAC_DEC (client_data);
780   guint len;
781
782   len = MIN (gst_adapter_available (dec->adapter), *bytes);
783
784   if (len == 0) {
785     GST_LOG_OBJECT (dec, "0 bytes available at the moment");
786     return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
787   }
788
789   GST_LOG_OBJECT (dec, "feeding %u bytes to decoder (available=%u, bytes=%u)",
790       len, gst_adapter_available (dec->adapter), (guint) * bytes);
791   gst_adapter_copy (dec->adapter, buffer, 0, len);
792   *bytes = len;
793
794   gst_adapter_flush (dec->adapter, len);
795
796   return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
797 }
798
799 static FLAC__StreamDecoderWriteStatus
800 gst_flac_dec_write (GstFlacDec * flacdec, const FLAC__Frame * frame,
801     const FLAC__int32 * const buffer[])
802 {
803   GstFlowReturn ret = GST_FLOW_OK;
804   GstBuffer *outbuf;
805   guint depth = frame->header.bits_per_sample;
806   guint width;
807   guint sample_rate = frame->header.sample_rate;
808   guint channels = frame->header.channels;
809   guint samples = frame->header.blocksize;
810   guint j, i;
811   GstClockTime next;
812
813   GST_LOG_OBJECT (flacdec, "samples in frame header: %d", samples);
814
815   /* if a DEFAULT segment is configured, don't send samples past the end
816    * of the segment */
817   if (flacdec->segment.format == GST_FORMAT_DEFAULT &&
818       flacdec->segment.stop != -1 &&
819       flacdec->segment.last_stop >= 0 &&
820       flacdec->segment.last_stop + samples > flacdec->segment.stop) {
821     samples = flacdec->segment.stop - flacdec->segment.last_stop;
822     GST_DEBUG_OBJECT (flacdec,
823         "clipping last buffer to %d samples because of segment", samples);
824   }
825
826   switch (depth) {
827     case 8:
828       width = 8;
829       break;
830     case 12:
831     case 16:
832       width = 16;
833       break;
834     case 20:
835     case 24:
836     case 32:
837       width = 32;
838       break;
839     case 0:
840       if (flacdec->depth < 4 || flacdec->depth > 32) {
841         GST_ERROR_OBJECT (flacdec, "unsupported depth %d from STREAMINFO",
842             flacdec->depth);
843         ret = GST_FLOW_ERROR;
844         goto done;
845       }
846
847       depth = flacdec->depth;
848       if (depth < 9)
849         width = 8;
850       else if (depth < 17)
851         width = 16;
852       else
853         width = 32;
854
855       break;
856     default:
857       GST_ERROR_OBJECT (flacdec, "unsupported depth %d", depth);
858       ret = GST_FLOW_ERROR;
859       goto done;
860   }
861
862   if (sample_rate == 0) {
863     if (flacdec->sample_rate != 0) {
864       sample_rate = flacdec->sample_rate;
865     } else {
866       GST_ERROR_OBJECT (flacdec, "unknown sample rate");
867       ret = GST_FLOW_ERROR;
868       goto done;
869     }
870   }
871
872   if (!GST_PAD_CAPS (flacdec->srcpad)) {
873     GstCaps *caps;
874
875     GST_DEBUG_OBJECT (flacdec, "Negotiating %d Hz @ %d channels",
876         frame->header.sample_rate, channels);
877
878     caps = gst_caps_new_simple ("audio/x-raw-int",
879         "endianness", G_TYPE_INT, G_BYTE_ORDER,
880         "signed", G_TYPE_BOOLEAN, TRUE,
881         "width", G_TYPE_INT, width,
882         "depth", G_TYPE_INT, depth,
883         "rate", G_TYPE_INT, frame->header.sample_rate,
884         "channels", G_TYPE_INT, channels, NULL);
885
886     if (channels > 2) {
887       GstStructure *s = gst_caps_get_structure (caps, 0);
888
889       gst_audio_set_channel_positions (s, channel_positions[channels - 1]);
890     }
891
892     flacdec->depth = depth;
893     flacdec->width = width;
894     flacdec->channels = channels;
895     flacdec->sample_rate = sample_rate;
896
897     gst_pad_set_caps (flacdec->srcpad, caps);
898     gst_caps_unref (caps);
899   }
900
901   if (flacdec->close_segment) {
902     GST_DEBUG_OBJECT (flacdec, "pushing close segment");
903     gst_pad_push_event (flacdec->srcpad, flacdec->close_segment);
904     flacdec->close_segment = NULL;
905   }
906   if (flacdec->start_segment) {
907     GST_DEBUG_OBJECT (flacdec, "pushing start segment");
908     gst_pad_push_event (flacdec->srcpad, flacdec->start_segment);
909     flacdec->start_segment = NULL;
910   }
911
912   if (flacdec->tags) {
913     gst_element_found_tags_for_pad (GST_ELEMENT (flacdec), flacdec->srcpad,
914         flacdec->tags);
915     flacdec->tags = NULL;
916   }
917
918   if (flacdec->pending) {
919     GST_DEBUG_OBJECT (flacdec,
920         "pushing pending samples at offset %" G_GINT64_FORMAT " (%"
921         GST_TIME_FORMAT " + %" GST_TIME_FORMAT ")",
922         GST_BUFFER_OFFSET (flacdec->pending),
923         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (flacdec->pending)),
924         GST_TIME_ARGS (GST_BUFFER_DURATION (flacdec->pending)));
925     /* Pending buffer was always allocated from the seeking thread,
926      * which means it wasn't gst_buffer_alloc'd. Do so now to let
927      * downstream negotiation work on older basetransform */
928     ret = gst_pad_alloc_buffer_and_set_caps (flacdec->srcpad,
929         GST_BUFFER_OFFSET (flacdec->pending),
930         GST_BUFFER_SIZE (flacdec->pending),
931         GST_BUFFER_CAPS (flacdec->pending), &outbuf);
932     if (ret == GST_FLOW_OK) {
933       gst_pad_push (flacdec->srcpad, flacdec->pending);
934       gst_buffer_unref (outbuf);
935     }
936
937     outbuf = flacdec->pending = NULL;
938     flacdec->segment.last_stop += flacdec->pending_samples;
939     flacdec->pending_samples = 0;
940   }
941
942   if (flacdec->seeking) {
943     GST_DEBUG_OBJECT (flacdec, "a pad_alloc would block here, do normal alloc");
944     outbuf = gst_buffer_new_and_alloc (samples * channels * (width / 8));
945     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (flacdec->srcpad));
946     GST_BUFFER_OFFSET (outbuf) = flacdec->segment.last_stop;
947   } else {
948     GST_LOG_OBJECT (flacdec, "alloc_buffer_and_set_caps");
949     ret = gst_pad_alloc_buffer_and_set_caps (flacdec->srcpad,
950         flacdec->segment.last_stop, samples * channels * (width / 8),
951         GST_PAD_CAPS (flacdec->srcpad), &outbuf);
952
953     if (ret != GST_FLOW_OK) {
954       GST_DEBUG_OBJECT (flacdec, "gst_pad_alloc_buffer() returned %s",
955           gst_flow_get_name (ret));
956       goto done;
957     }
958   }
959
960   if (flacdec->cur_granulepos != GST_BUFFER_OFFSET_NONE) {
961     /* this should be fine since it should be one flac frame per ogg packet */
962     flacdec->segment.last_stop = flacdec->cur_granulepos - samples;
963     GST_LOG_OBJECT (flacdec, "granulepos = %" G_GINT64_FORMAT ", samples = %u",
964         flacdec->cur_granulepos, samples);
965   }
966
967   GST_BUFFER_TIMESTAMP (outbuf) =
968       gst_util_uint64_scale_int (flacdec->segment.last_stop, GST_SECOND,
969       frame->header.sample_rate);
970
971   /* get next timestamp to calculate the duration */
972   next = gst_util_uint64_scale_int (flacdec->segment.last_stop + samples,
973       GST_SECOND, frame->header.sample_rate);
974
975   GST_BUFFER_DURATION (outbuf) = next - GST_BUFFER_TIMESTAMP (outbuf);
976
977   if (width == 8) {
978     gint8 *outbuffer = (gint8 *) GST_BUFFER_DATA (outbuf);
979
980     for (i = 0; i < samples; i++) {
981       for (j = 0; j < channels; j++) {
982         *outbuffer++ = (gint8) buffer[j][i];
983       }
984     }
985   } else if (width == 16) {
986     gint16 *outbuffer = (gint16 *) GST_BUFFER_DATA (outbuf);
987
988     for (i = 0; i < samples; i++) {
989       for (j = 0; j < channels; j++) {
990         *outbuffer++ = (gint16) buffer[j][i];
991       }
992     }
993   } else if (width == 32) {
994     gint32 *outbuffer = (gint32 *) GST_BUFFER_DATA (outbuf);
995
996     for (i = 0; i < samples; i++) {
997       for (j = 0; j < channels; j++) {
998         *outbuffer++ = (gint32) buffer[j][i];
999       }
1000     }
1001   } else {
1002     g_assert_not_reached ();
1003   }
1004
1005   if (!flacdec->seeking) {
1006     GST_DEBUG_OBJECT (flacdec, "pushing %d samples at offset %" G_GINT64_FORMAT
1007         " (%" GST_TIME_FORMAT " + %" GST_TIME_FORMAT ")",
1008         samples, GST_BUFFER_OFFSET (outbuf),
1009         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1010         GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
1011
1012     if (flacdec->discont) {
1013       GST_DEBUG_OBJECT (flacdec, "marking discont");
1014       outbuf = gst_buffer_make_metadata_writable (outbuf);
1015       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1016       flacdec->discont = FALSE;
1017     }
1018     ret = gst_pad_push (flacdec->srcpad, outbuf);
1019     GST_DEBUG_OBJECT (flacdec, "returned %s", gst_flow_get_name (ret));
1020     flacdec->segment.last_stop += samples;
1021   } else {
1022     GST_DEBUG_OBJECT (flacdec,
1023         "not pushing %d samples at offset %" G_GINT64_FORMAT
1024         " (in seek)", samples, GST_BUFFER_OFFSET (outbuf));
1025     gst_buffer_replace (&flacdec->pending, outbuf);
1026     gst_buffer_unref (outbuf);
1027     flacdec->pending_samples = samples;
1028     ret = GST_FLOW_OK;
1029   }
1030
1031   if (ret != GST_FLOW_OK) {
1032     GST_DEBUG_OBJECT (flacdec, "gst_pad_push() returned %s",
1033         gst_flow_get_name (ret));
1034   }
1035
1036 done:
1037
1038
1039   /* we act on the flow return value later in the loop function, as we don't
1040    * want to mess up the internal decoder state by returning ABORT when the
1041    * error is in fact non-fatal (like a pad in flushing mode) and we want
1042    * to continue later. So just pretend everything's dandy and act later. */
1043   flacdec->last_flow = ret;
1044
1045   return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
1046 }
1047
1048 static FLAC__StreamDecoderWriteStatus
1049 gst_flac_dec_write_stream (const FLAC__StreamDecoder * decoder,
1050     const FLAC__Frame * frame,
1051     const FLAC__int32 * const buffer[], void *client_data)
1052 {
1053   return gst_flac_dec_write (GST_FLAC_DEC (client_data), frame, buffer);
1054 }
1055
1056 static void
1057 gst_flac_dec_loop (GstPad * sinkpad)
1058 {
1059   GstFlacDec *flacdec;
1060   FLAC__StreamDecoderState s;
1061   FLAC__StreamDecoderInitStatus is;
1062
1063   flacdec = GST_FLAC_DEC (GST_OBJECT_PARENT (sinkpad));
1064
1065   GST_LOG_OBJECT (flacdec, "entering loop");
1066
1067   if (flacdec->init) {
1068     GST_DEBUG_OBJECT (flacdec, "initializing new decoder");
1069     is = FLAC__stream_decoder_init_stream (flacdec->decoder,
1070         gst_flac_dec_read_seekable, gst_flac_dec_seek, gst_flac_dec_tell,
1071         gst_flac_dec_length, gst_flac_dec_eof, gst_flac_dec_write_stream,
1072         gst_flac_dec_metadata_cb, gst_flac_dec_error_cb, flacdec);
1073     if (is != FLAC__STREAM_DECODER_INIT_STATUS_OK)
1074       goto analyze_state;
1075
1076     /*    FLAC__seekable_decoder_process_metadata (flacdec->decoder); */
1077     flacdec->init = FALSE;
1078   }
1079
1080   flacdec->cur_granulepos = GST_BUFFER_OFFSET_NONE;
1081
1082   flacdec->last_flow = GST_FLOW_OK;
1083
1084   GST_LOG_OBJECT (flacdec, "processing single");
1085   FLAC__stream_decoder_process_single (flacdec->decoder);
1086
1087 analyze_state:
1088
1089   GST_LOG_OBJECT (flacdec, "done processing, checking encoder state");
1090   s = FLAC__stream_decoder_get_state (flacdec->decoder);
1091   switch (s) {
1092     case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1093     case FLAC__STREAM_DECODER_READ_METADATA:
1094     case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1095     case FLAC__STREAM_DECODER_READ_FRAME:
1096     {
1097       GST_DEBUG_OBJECT (flacdec, "everything ok");
1098
1099       if (GST_FLOW_IS_FATAL (flacdec->last_flow) ||
1100           flacdec->last_flow == GST_FLOW_NOT_LINKED) {
1101         GST_ELEMENT_ERROR (flacdec, STREAM, FAILED,
1102             (_("Internal data stream error.")),
1103             ("stream stopped, reason %s",
1104                 gst_flow_get_name (flacdec->last_flow)));
1105         goto eos_and_pause;
1106       } else if (flacdec->last_flow != GST_FLOW_OK) {
1107         goto pause;
1108       }
1109
1110       /* check if we're at the end of a configured segment */
1111       if (flacdec->segment.stop != -1 &&
1112           flacdec->segment.last_stop > 0 &&
1113           flacdec->segment.last_stop >= flacdec->segment.stop) {
1114         GST_DEBUG_OBJECT (flacdec, "reached end of the configured segment");
1115
1116         if ((flacdec->segment.flags & GST_SEEK_FLAG_SEGMENT) == 0) {
1117           goto eos_and_pause;
1118         } else {
1119           goto segment_done_and_pause;
1120         }
1121
1122         g_assert_not_reached ();
1123       }
1124
1125       return;
1126     }
1127
1128     case FLAC__STREAM_DECODER_END_OF_STREAM:{
1129       GST_DEBUG_OBJECT (flacdec, "EOS");
1130       FLAC__stream_decoder_reset (flacdec->decoder);
1131
1132       if ((flacdec->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0) {
1133         if (flacdec->segment.duration > 0) {
1134           flacdec->segment.stop = flacdec->segment.duration;
1135         } else {
1136           flacdec->segment.stop = flacdec->segment.last_stop;
1137         }
1138         goto segment_done_and_pause;
1139       }
1140
1141       goto eos_and_pause;
1142     }
1143
1144       /* gst_flac_dec_read_seekable() returned ABORTED */
1145     case FLAC__STREAM_DECODER_ABORTED:
1146     {
1147       GST_INFO_OBJECT (flacdec, "read aborted: last pull_range flow = %s",
1148           gst_flow_get_name (flacdec->pull_flow));
1149       if (!GST_FLOW_IS_FATAL (flacdec->pull_flow)) {
1150         /* it seems we need to flush the decoder here to reset the decoder
1151          * state after the abort for FLAC__stream_decoder_seek_absolute()
1152          * to work properly */
1153         GST_DEBUG_OBJECT (flacdec, "flushing decoder to reset decoder state");
1154         FLAC__stream_decoder_flush (flacdec->decoder);
1155         goto pause;
1156       }
1157       /* fall through */
1158     }
1159     case FLAC__STREAM_DECODER_OGG_ERROR:
1160     case FLAC__STREAM_DECODER_SEEK_ERROR:
1161     case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
1162     case FLAC__STREAM_DECODER_UNINITIALIZED:
1163     default:{
1164       /* fixme: this error sucks -- should try to figure out when/if an more
1165          specific error was already sent via the callback */
1166       GST_ELEMENT_ERROR (flacdec, STREAM, DECODE, (NULL),
1167           ("%s", FLAC__StreamDecoderStateString[s]));
1168       goto eos_and_pause;
1169     }
1170   }
1171
1172   return;
1173
1174 segment_done_and_pause:
1175   {
1176     gint64 stop_time;
1177
1178     stop_time = gst_util_uint64_scale_int (flacdec->segment.stop,
1179         GST_SECOND, flacdec->sample_rate);
1180
1181     GST_DEBUG_OBJECT (flacdec, "posting SEGMENT_DONE message, stop time %"
1182         GST_TIME_FORMAT, GST_TIME_ARGS (stop_time));
1183
1184     gst_element_post_message (GST_ELEMENT (flacdec),
1185         gst_message_new_segment_done (GST_OBJECT (flacdec),
1186             GST_FORMAT_TIME, stop_time));
1187
1188     goto pause;
1189   }
1190 eos_and_pause:
1191   {
1192     GST_DEBUG_OBJECT (flacdec, "sending EOS event");
1193     flacdec->running = FALSE;
1194     gst_pad_push_event (flacdec->srcpad, gst_event_new_eos ());
1195     /* fall through to pause */
1196   }
1197 pause:
1198   {
1199     GST_DEBUG_OBJECT (flacdec, "pausing");
1200     gst_pad_pause_task (sinkpad);
1201     return;
1202   }
1203 }
1204
1205 static gboolean
1206 gst_flac_dec_sink_event (GstPad * pad, GstEvent * event)
1207 {
1208   GstFlacDec *dec;
1209   gboolean res;
1210
1211   dec = GST_FLAC_DEC (gst_pad_get_parent (pad));
1212
1213   switch (GST_EVENT_TYPE (event)) {
1214     case GST_EVENT_FLUSH_STOP:{
1215       if (dec->init == FALSE) {
1216         FLAC__stream_decoder_flush (dec->decoder);
1217         gst_adapter_clear (dec->adapter);
1218       }
1219       res = gst_pad_push_event (dec->srcpad, event);
1220       break;
1221     }
1222     case GST_EVENT_NEWSEGMENT:{
1223       GstFormat fmt;
1224       gboolean update;
1225       gdouble rate, applied_rate;
1226       gint64 cur, stop, time;
1227
1228       gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
1229           &fmt, &cur, &stop, &time);
1230
1231       if (fmt == GST_FORMAT_TIME) {
1232         GstFormat dformat = GST_FORMAT_DEFAULT;
1233
1234         GST_DEBUG_OBJECT (dec, "newsegment event in TIME format => framed");
1235         dec->framed = TRUE;
1236         res = gst_pad_push_event (dec->srcpad, event);
1237
1238         /* this won't work for the first newsegment event though ... */
1239         if (gst_flac_dec_convert_src (dec->srcpad, GST_FORMAT_TIME, cur,
1240                 &dformat, &cur) && cur != -1 &&
1241             gst_flac_dec_convert_src (dec->srcpad, GST_FORMAT_TIME, stop,
1242                 &dformat, &stop) && stop != -1) {
1243           gst_segment_set_newsegment_full (&dec->segment, update, rate,
1244               applied_rate, dformat, cur, stop, time);
1245           GST_DEBUG_OBJECT (dec, "segment %" GST_SEGMENT_FORMAT, &dec->segment);
1246         } else {
1247           GST_WARNING_OBJECT (dec, "couldn't convert time => samples");
1248         }
1249       } else if (fmt == GST_FORMAT_BYTES || TRUE) {
1250         GST_DEBUG_OBJECT (dec, "newsegment event in %s format => not framed",
1251             gst_format_get_name (fmt));
1252         dec->framed = FALSE;
1253
1254         /* prepare generic newsegment event, for some reason our metadata
1255          * callback where we usually set this up is not being called in
1256          * push mode */
1257         dec->start_segment = gst_event_new_new_segment (FALSE, 1.0,
1258             GST_FORMAT_TIME, 0, -1, 0);
1259
1260         gst_event_unref (event);
1261         res = TRUE;
1262       }
1263       break;
1264     }
1265     case GST_EVENT_EOS:{
1266       GST_LOG_OBJECT (dec, "EOS, with %u bytes available in adapter",
1267           gst_adapter_available (dec->adapter));
1268       if (dec->init == FALSE) {
1269         if (gst_adapter_available (dec->adapter) > 0) {
1270           FLAC__stream_decoder_process_until_end_of_stream (dec->decoder);
1271         }
1272         FLAC__stream_decoder_flush (dec->decoder);
1273       }
1274       gst_adapter_clear (dec->adapter);
1275       res = gst_pad_push_event (dec->srcpad, event);
1276       break;
1277     }
1278     default:
1279       res = gst_pad_event_default (pad, event);
1280       break;
1281   }
1282
1283   gst_object_unref (dec);
1284
1285   return res;
1286 }
1287
1288 static GstFlowReturn
1289 gst_flac_dec_chain (GstPad * pad, GstBuffer * buf)
1290 {
1291   FLAC__StreamDecoderInitStatus s;
1292   GstFlacDec *dec;
1293   gboolean got_audio_frame;
1294
1295   dec = GST_FLAC_DEC (GST_PAD_PARENT (pad));
1296
1297   GST_LOG_OBJECT (dec, "buffer with ts=%" GST_TIME_FORMAT ", end_offset=%"
1298       G_GINT64_FORMAT ", size=%u", GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1299       GST_BUFFER_OFFSET_END (buf), GST_BUFFER_SIZE (buf));
1300
1301   if (dec->init) {
1302     GST_DEBUG_OBJECT (dec, "initializing decoder");
1303     s = FLAC__stream_decoder_init_stream (dec->decoder,
1304         gst_flac_dec_read_stream, NULL, NULL, NULL, NULL,
1305         gst_flac_dec_write_stream, gst_flac_dec_metadata_cb,
1306         gst_flac_dec_error_cb, dec);
1307     if (s != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
1308       GST_ELEMENT_ERROR (GST_ELEMENT (dec), LIBRARY, INIT, (NULL), (NULL));
1309       return GST_FLOW_ERROR;
1310     }
1311     GST_DEBUG_OBJECT (dec, "initialized (framed=%d)", dec->framed);
1312     dec->init = FALSE;
1313   } else if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT)) {
1314     /* Clear the adapter and the decoder */
1315     gst_adapter_clear (dec->adapter);
1316     FLAC__stream_decoder_flush (dec->decoder);
1317   }
1318
1319   if (dec->framed) {
1320     gint64 unused;
1321
1322     /* check if this is a flac audio frame (rather than a header or junk) */
1323     got_audio_frame = gst_flac_dec_scan_got_frame (dec, GST_BUFFER_DATA (buf),
1324         GST_BUFFER_SIZE (buf), &unused);
1325
1326     /* oggdemux will set granulepos in OFFSET_END instead of timestamp */
1327     if (G_LIKELY (got_audio_frame)) {
1328       /* old oggdemux for now */
1329       if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1330         dec->cur_granulepos = GST_BUFFER_OFFSET_END (buf);
1331       } else {
1332         GstFormat dformat = GST_FORMAT_DEFAULT;
1333
1334         /* upstream (e.g. demuxer) presents us time,
1335          * convert to default samples */
1336         gst_flac_dec_convert_src (dec->srcpad, GST_FORMAT_TIME,
1337             GST_BUFFER_TIMESTAMP (buf), &dformat, &dec->segment.last_stop);
1338         dec->cur_granulepos = GST_BUFFER_OFFSET_NONE;
1339       }
1340     }
1341   } else {
1342     dec->cur_granulepos = GST_BUFFER_OFFSET_NONE;
1343     got_audio_frame = TRUE;
1344   }
1345
1346   gst_adapter_push (dec->adapter, buf);
1347   buf = NULL;
1348
1349   dec->last_flow = GST_FLOW_OK;
1350
1351   if (!dec->framed) {
1352     /* wait until we have at least 64kB because libflac's StreamDecoder
1353      * interface is a bit dumb it seems (if we don't have as much data as
1354      * it wants it will call our read callback repeatedly and the only
1355      * way to stop that is to error out or EOS, which will affect the
1356      * decoder state). And the decoder seems to always ask for MAX_BLOCK_SIZE
1357      * bytes rather than the max. block size from the header). Requiring
1358      * MAX_BLOCK_SIZE bytes here should make sure it always gets enough data
1359      * to decode at least one block */
1360     while (gst_adapter_available (dec->adapter) >= FLAC__MAX_BLOCK_SIZE &&
1361         dec->last_flow == GST_FLOW_OK) {
1362       GST_LOG_OBJECT (dec, "%u bytes available",
1363           gst_adapter_available (dec->adapter));
1364       if (!FLAC__stream_decoder_process_single (dec->decoder)) {
1365         GST_DEBUG_OBJECT (dec, "process_single failed");
1366         break;
1367       }
1368
1369       if (FLAC__stream_decoder_get_state (dec->decoder) ==
1370           FLAC__STREAM_DECODER_ABORTED) {
1371         GST_WARNING_OBJECT (dec, "Read callback caused internal abort");
1372         dec->last_flow = GST_FLOW_ERROR;
1373         break;
1374       }
1375     }
1376   } else if (dec->framed && got_audio_frame) {
1377     /* framed - there should always be enough data to decode something */
1378     GST_LOG_OBJECT (dec, "%u bytes available",
1379         gst_adapter_available (dec->adapter));
1380     if (!FLAC__stream_decoder_process_single (dec->decoder)) {
1381       GST_DEBUG_OBJECT (dec, "process_single failed");
1382     }
1383   } else {
1384     GST_DEBUG_OBJECT (dec, "don't have all headers yet");
1385   }
1386
1387   return dec->last_flow;
1388 }
1389
1390 static gboolean
1391 gst_flac_dec_convert_sink (GstFlacDec * dec, GstFormat src_format,
1392     gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
1393 {
1394   gboolean res = TRUE;
1395
1396   if (dec->width == 0 || dec->channels == 0 || dec->sample_rate == 0) {
1397     /* no frame decoded yet */
1398     GST_DEBUG_OBJECT (dec, "cannot convert: not set up yet");
1399     return FALSE;
1400   }
1401
1402   switch (src_format) {
1403     case GST_FORMAT_BYTES:{
1404       res = FALSE;
1405       break;
1406     }
1407     case GST_FORMAT_DEFAULT:
1408       switch (*dest_format) {
1409         case GST_FORMAT_BYTES:
1410           res = FALSE;
1411           break;
1412         case GST_FORMAT_TIME:
1413           /* granulepos = sample */
1414           *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
1415               dec->sample_rate);
1416           break;
1417         default:
1418           res = FALSE;
1419           break;
1420       }
1421       break;
1422     case GST_FORMAT_TIME:
1423       switch (*dest_format) {
1424         case GST_FORMAT_BYTES:
1425           res = FALSE;
1426           break;
1427         case GST_FORMAT_DEFAULT:
1428           *dest_value = gst_util_uint64_scale_int (src_value,
1429               dec->sample_rate, GST_SECOND);
1430           break;
1431         default:
1432           res = FALSE;
1433           break;
1434       }
1435       break;
1436     default:
1437       res = FALSE;
1438       break;
1439   }
1440   return res;
1441 }
1442
1443 static const GstQueryType *
1444 gst_flac_dec_get_sink_query_types (GstPad * pad)
1445 {
1446   static const GstQueryType types[] = {
1447     GST_QUERY_CONVERT,
1448     0,
1449   };
1450
1451   return types;
1452 }
1453
1454 static gboolean
1455 gst_flac_dec_sink_query (GstPad * pad, GstQuery * query)
1456 {
1457   GstFlacDec *dec;
1458   gboolean res = FALSE;
1459
1460   dec = GST_FLAC_DEC (gst_pad_get_parent (pad));
1461
1462   GST_LOG_OBJECT (dec, "%s query", GST_QUERY_TYPE_NAME (query));
1463
1464   switch (GST_QUERY_TYPE (query)) {
1465     case GST_QUERY_CONVERT:{
1466       GstFormat src_fmt, dest_fmt;
1467
1468       gint64 src_val, dest_val;
1469
1470       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
1471
1472       res = gst_flac_dec_convert_sink (dec, src_fmt, src_val, &dest_fmt,
1473           &dest_val);
1474
1475       if (res) {
1476         gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1477       }
1478       GST_LOG_OBJECT (dec, "conversion %s", (res) ? "ok" : "FAILED");
1479       break;
1480     }
1481
1482     default:{
1483       res = gst_pad_query_default (pad, query);
1484       break;
1485     }
1486   }
1487
1488   gst_object_unref (dec);
1489   return res;
1490 }
1491
1492 static gboolean
1493 gst_flac_dec_convert_src (GstPad * pad, GstFormat src_format, gint64 src_value,
1494     GstFormat * dest_format, gint64 * dest_value)
1495 {
1496   GstFlacDec *flacdec = GST_FLAC_DEC (GST_PAD_PARENT (pad));
1497   gboolean res = TRUE;
1498   guint bytes_per_sample;
1499   guint scale = 1;
1500
1501   if (flacdec->width == 0 || flacdec->channels == 0 ||
1502       flacdec->sample_rate == 0) {
1503     /* no frame decoded yet */
1504     GST_DEBUG_OBJECT (flacdec, "cannot convert: not set up yet");
1505     return FALSE;
1506   }
1507
1508   bytes_per_sample = flacdec->channels * (flacdec->width / 8);
1509
1510   switch (src_format) {
1511     case GST_FORMAT_BYTES:{
1512       switch (*dest_format) {
1513         case GST_FORMAT_DEFAULT:
1514           *dest_value =
1515               gst_util_uint64_scale_int (src_value, 1, bytes_per_sample);
1516           break;
1517         case GST_FORMAT_TIME:
1518         {
1519           gint byterate = bytes_per_sample * flacdec->sample_rate;
1520
1521           *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
1522               byterate);
1523           break;
1524         }
1525         default:
1526           res = FALSE;
1527       }
1528       break;
1529     }
1530     case GST_FORMAT_DEFAULT:
1531       switch (*dest_format) {
1532         case GST_FORMAT_BYTES:
1533           *dest_value = src_value * bytes_per_sample;
1534           break;
1535         case GST_FORMAT_TIME:
1536           *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
1537               flacdec->sample_rate);
1538           break;
1539         default:
1540           res = FALSE;
1541       }
1542       break;
1543     case GST_FORMAT_TIME:
1544       switch (*dest_format) {
1545         case GST_FORMAT_BYTES:
1546           scale = bytes_per_sample;
1547         case GST_FORMAT_DEFAULT:
1548           *dest_value = gst_util_uint64_scale_int (src_value,
1549               scale * flacdec->sample_rate, GST_SECOND);
1550           break;
1551         default:
1552           res = FALSE;
1553       }
1554       break;
1555     default:
1556       res = FALSE;
1557   }
1558   return res;
1559 }
1560
1561 static const GstQueryType *
1562 gst_flac_dec_get_src_query_types (GstPad * pad)
1563 {
1564   static const GstQueryType types[] = {
1565     GST_QUERY_POSITION,
1566     GST_QUERY_DURATION,
1567     GST_QUERY_CONVERT,
1568     GST_QUERY_SEEKING,
1569     0,
1570   };
1571
1572   return types;
1573 }
1574
1575 static gboolean
1576 gst_flac_dec_src_query (GstPad * pad, GstQuery * query)
1577 {
1578   GstFlacDec *flacdec;
1579   gboolean res = TRUE;
1580   GstPad *peer;
1581
1582   flacdec = GST_FLAC_DEC (gst_pad_get_parent (pad));
1583   peer = gst_pad_get_peer (flacdec->sinkpad);
1584
1585   switch (GST_QUERY_TYPE (query)) {
1586     case GST_QUERY_POSITION:{
1587       GstFormat fmt;
1588       gint64 pos;
1589
1590       gst_query_parse_position (query, &fmt, NULL);
1591
1592       /* there might be a demuxer in front of us who can handle this */
1593       if (fmt == GST_FORMAT_TIME && (res = gst_pad_query (peer, query)))
1594         break;
1595
1596       if (fmt != GST_FORMAT_DEFAULT) {
1597         if (!gst_flac_dec_convert_src (flacdec->srcpad, GST_FORMAT_DEFAULT,
1598                 flacdec->segment.last_stop, &fmt, &pos)) {
1599           GST_DEBUG_OBJECT (flacdec, "failed to convert position into %s "
1600               "format", gst_format_get_name (fmt));
1601           res = FALSE;
1602           goto done;
1603         }
1604       } else {
1605         pos = flacdec->segment.last_stop;
1606       }
1607
1608       gst_query_set_position (query, fmt, pos);
1609
1610       GST_DEBUG_OBJECT (flacdec, "returning position %" G_GUINT64_FORMAT
1611           " (format: %s)", pos, gst_format_get_name (fmt));
1612
1613       res = TRUE;
1614       break;
1615     }
1616
1617     case GST_QUERY_DURATION:{
1618       GstFormat fmt;
1619       gint64 len;
1620
1621       gst_query_parse_duration (query, &fmt, NULL);
1622
1623       /* try any demuxers before us first */
1624       if (fmt == GST_FORMAT_TIME && peer && gst_pad_query (peer, query)) {
1625         gst_query_parse_duration (query, NULL, &len);
1626         GST_DEBUG_OBJECT (flacdec, "peer returned duration %" GST_TIME_FORMAT,
1627             GST_TIME_ARGS (len));
1628         res = TRUE;
1629         goto done;
1630       }
1631
1632       if (flacdec->segment.duration == 0 || flacdec->segment.duration == -1) {
1633         GST_DEBUG_OBJECT (flacdec, "duration not known yet");
1634         res = FALSE;
1635         goto done;
1636       }
1637
1638       /* convert total number of samples to request format */
1639       if (fmt != GST_FORMAT_DEFAULT) {
1640         if (!gst_flac_dec_convert_src (flacdec->srcpad, GST_FORMAT_DEFAULT,
1641                 flacdec->segment.duration, &fmt, &len)) {
1642           GST_DEBUG_OBJECT (flacdec, "failed to convert duration into %s "
1643               "format", gst_format_get_name (fmt));
1644           res = FALSE;
1645           goto done;
1646         }
1647       } else {
1648         len = flacdec->segment.duration;
1649       }
1650
1651       gst_query_set_duration (query, fmt, len);
1652
1653       GST_DEBUG_OBJECT (flacdec, "returning duration %" G_GUINT64_FORMAT
1654           " (format: %s)", len, gst_format_get_name (fmt));
1655
1656       res = TRUE;
1657       break;
1658     }
1659
1660     case GST_QUERY_CONVERT:{
1661       GstFormat src_fmt, dest_fmt;
1662       gint64 src_val, dest_val;
1663
1664       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
1665
1666       res = gst_flac_dec_convert_src (pad, src_fmt, src_val, &dest_fmt,
1667           &dest_val);
1668
1669       if (res) {
1670         gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1671       }
1672
1673       break;
1674     }
1675     case GST_QUERY_SEEKING:{
1676       GstFormat fmt;
1677       gboolean seekable = FALSE;
1678
1679       res = TRUE;
1680       /* If upstream can handle the query we're done */
1681       seekable = gst_pad_peer_query (flacdec->sinkpad, query);
1682       if (seekable)
1683         gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1684       if (seekable)
1685         goto done;
1686
1687       gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1688       if ((fmt != GST_FORMAT_TIME && fmt != GST_FORMAT_DEFAULT) ||
1689           flacdec->streaming) {
1690         gst_query_set_seeking (query, fmt, FALSE, -1, -1);
1691       } else {
1692         gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1);
1693       }
1694       break;
1695     }
1696
1697     default:{
1698       res = gst_pad_query_default (pad, query);
1699       break;
1700     }
1701   }
1702
1703 done:
1704
1705   if (peer)
1706     gst_object_unref (peer);
1707
1708   gst_object_unref (flacdec);
1709
1710   return res;
1711 }
1712
1713 static gboolean
1714 gst_flac_dec_handle_seek_event (GstFlacDec * flacdec, GstEvent * event)
1715 {
1716   FLAC__bool seek_ok;
1717   GstSeekFlags seek_flags;
1718   GstSeekType start_type;
1719   GstSeekType stop_type;
1720   GstSegment segment;
1721   GstFormat seek_format;
1722   gboolean only_update = FALSE;
1723   gboolean flush;
1724   gdouble rate;
1725   gint64 start, last_stop;
1726   gint64 stop;
1727
1728   if (flacdec->streaming) {
1729     GST_DEBUG_OBJECT (flacdec, "seeking in streaming mode not implemented yet");
1730     return FALSE;
1731   }
1732
1733   gst_event_parse_seek (event, &rate, &seek_format, &seek_flags, &start_type,
1734       &start, &stop_type, &stop);
1735
1736   if (seek_format != GST_FORMAT_DEFAULT && seek_format != GST_FORMAT_TIME) {
1737     GST_DEBUG_OBJECT (flacdec,
1738         "seeking is only supported in TIME or DEFAULT format");
1739     return FALSE;
1740   }
1741
1742   if (rate < 0.0) {
1743     GST_DEBUG_OBJECT (flacdec,
1744         "only forward playback supported, rate %f not allowed", rate);
1745     return FALSE;
1746   }
1747
1748   if (seek_format != GST_FORMAT_DEFAULT) {
1749     GstFormat target_format = GST_FORMAT_DEFAULT;
1750
1751     if (start_type != GST_SEEK_TYPE_NONE &&
1752         !gst_flac_dec_convert_src (flacdec->srcpad, seek_format, start,
1753             &target_format, &start)) {
1754       GST_DEBUG_OBJECT (flacdec, "failed to convert start to DEFAULT format");
1755       return FALSE;
1756     }
1757
1758     if (stop_type != GST_SEEK_TYPE_NONE &&
1759         !gst_flac_dec_convert_src (flacdec->srcpad, seek_format, stop,
1760             &target_format, &stop)) {
1761       GST_DEBUG_OBJECT (flacdec, "failed to convert stop to DEFAULT format");
1762       return FALSE;
1763     }
1764   }
1765
1766   flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH);
1767
1768   if (flush) {
1769     /* flushing seek, clear the pipeline of stuff, we need a newsegment after
1770      * this. */
1771     GST_DEBUG_OBJECT (flacdec, "flushing");
1772     gst_pad_push_event (flacdec->sinkpad, gst_event_new_flush_start ());
1773     gst_pad_push_event (flacdec->srcpad, gst_event_new_flush_start ());
1774   } else {
1775     /* non flushing seek, pause the task */
1776     GST_DEBUG_OBJECT (flacdec, "stopping task");
1777     gst_pad_stop_task (flacdec->sinkpad);
1778   }
1779
1780   /* acquire the stream lock, this either happens when the streaming thread
1781    * stopped because of the flush or when the task is paused after the loop
1782    * function finished an iteration, which can never happen when it's blocked
1783    * downstream in PAUSED, for example */
1784   GST_PAD_STREAM_LOCK (flacdec->sinkpad);
1785
1786   /* start seek with clear state to avoid seeking thread pushing segments/data.
1787    * Note current state may have some pending,
1788    * e.g. multi-sink seek leads to immediate subsequent seek events */
1789   if (flacdec->start_segment) {
1790     gst_event_unref (flacdec->start_segment);
1791     flacdec->start_segment = NULL;
1792   }
1793   gst_buffer_replace (&flacdec->pending, NULL);
1794   flacdec->pending_samples = 0;
1795
1796   /* save a segment copy until we know the seek worked. The idea is that
1797    * when the seek fails, we want to restore with what we were doing. */
1798   segment = flacdec->segment;
1799
1800   /* update the segment with the seek values, last_stop will contain the new
1801    * position we should seek to */
1802   gst_segment_set_seek (&flacdec->segment, rate, GST_FORMAT_DEFAULT,
1803       seek_flags, start_type, start, stop_type, stop, &only_update);
1804
1805   GST_DEBUG_OBJECT (flacdec,
1806       "configured segment: [%" G_GINT64_FORMAT "-%" G_GINT64_FORMAT
1807       "] = [%" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "]",
1808       flacdec->segment.start, flacdec->segment.stop,
1809       GST_TIME_ARGS (flacdec->segment.start * GST_SECOND /
1810           flacdec->sample_rate),
1811       GST_TIME_ARGS (flacdec->segment.stop * GST_SECOND /
1812           flacdec->sample_rate));
1813
1814   GST_DEBUG_OBJECT (flacdec, "performing seek to sample %" G_GINT64_FORMAT,
1815       flacdec->segment.last_stop);
1816
1817   /* flush sinkpad again because we need to pull and push buffers while doing
1818    * the seek */
1819   if (flush) {
1820     GST_DEBUG_OBJECT (flacdec, "flushing stop");
1821     gst_pad_push_event (flacdec->sinkpad, gst_event_new_flush_stop ());
1822     gst_pad_push_event (flacdec->srcpad, gst_event_new_flush_stop ());
1823   }
1824
1825   /* mark ourselves as seeking because the above lines will trigger some
1826    * callbacks that need to behave differently when seeking */
1827   flacdec->seeking = TRUE;
1828
1829   GST_LOG_OBJECT (flacdec, "calling seek_absolute");
1830   seek_ok = FLAC__stream_decoder_seek_absolute (flacdec->decoder,
1831       flacdec->segment.last_stop);
1832   GST_LOG_OBJECT (flacdec, "done with seek_absolute, seek_ok=%d", seek_ok);
1833
1834   flacdec->seeking = FALSE;
1835
1836   GST_DEBUG_OBJECT (flacdec, "performed seek to sample %" G_GINT64_FORMAT,
1837       flacdec->segment.last_stop);
1838
1839
1840   if (!seek_ok) {
1841     GST_WARNING_OBJECT (flacdec, "seek failed");
1842     /* seek failed, restore the segment and start streaming again with
1843      * the previous segment values */
1844     flacdec->segment = segment;
1845   } else if (!flush && flacdec->running) {
1846     /* we are running the current segment and doing a non-flushing seek, 
1847      * close the segment first based on the last_stop. */
1848     GST_DEBUG_OBJECT (flacdec, "closing running segment %" G_GINT64_FORMAT
1849         " to %" G_GINT64_FORMAT, segment.start, segment.last_stop);
1850
1851     /* convert the old segment values to time to close the old segment */
1852     start = gst_util_uint64_scale_int (segment.start, GST_SECOND,
1853         flacdec->sample_rate);
1854     last_stop =
1855         gst_util_uint64_scale_int (segment.last_stop, GST_SECOND,
1856         flacdec->sample_rate);
1857
1858     /* queue the segment for sending in the stream thread, start and time are
1859      * always the same. */
1860     if (flacdec->close_segment)
1861       gst_event_unref (flacdec->close_segment);
1862     flacdec->close_segment =
1863         gst_event_new_new_segment_full (TRUE,
1864         segment.rate, segment.applied_rate, GST_FORMAT_TIME,
1865         start, last_stop, start);
1866   }
1867
1868   if (seek_ok) {
1869     /* seek succeeded, flacdec->segment contains the new positions */
1870     GST_DEBUG_OBJECT (flacdec, "seek successful");
1871   }
1872
1873   /* convert the (new) segment values to time, we will need them to generate the
1874    * new segment events. */
1875   start = gst_util_uint64_scale_int (flacdec->segment.start, GST_SECOND,
1876       flacdec->sample_rate);
1877   last_stop = gst_util_uint64_scale_int (flacdec->segment.last_stop, GST_SECOND,
1878       flacdec->sample_rate);
1879
1880   /* for deriving a stop position for the playback segment from the seek
1881    * segment, we must take the duration when the stop is not set */
1882   if (flacdec->segment.stop != -1)
1883     stop = gst_util_uint64_scale_int (flacdec->segment.stop, GST_SECOND,
1884         flacdec->sample_rate);
1885   else
1886     stop = gst_util_uint64_scale_int (flacdec->segment.duration, GST_SECOND,
1887         flacdec->sample_rate);
1888
1889   /* notify start of new segment when we were asked to do so. */
1890   if (flacdec->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1891     /* last_stop contains the position we start from */
1892     gst_element_post_message (GST_ELEMENT (flacdec),
1893         gst_message_new_segment_start (GST_OBJECT (flacdec),
1894             GST_FORMAT_TIME, last_stop));
1895   }
1896
1897   /* if the seek was ok or (when it failed) we are flushing, we need to send out
1898    * a new segment. If we did not flush and the seek failed, we simply do
1899    * nothing here and continue where we were. */
1900   if (seek_ok || flush) {
1901     GST_DEBUG_OBJECT (flacdec, "Creating newsegment from %" GST_TIME_FORMAT
1902         " to %" GST_TIME_FORMAT, GST_TIME_ARGS (last_stop),
1903         GST_TIME_ARGS (stop));
1904     /* now replace the old segment so that we send it in the stream thread the
1905      * next time it is scheduled. */
1906     if (flacdec->start_segment)
1907       gst_event_unref (flacdec->start_segment);
1908     flacdec->start_segment =
1909         gst_event_new_new_segment_full (FALSE,
1910         flacdec->segment.rate, flacdec->segment.applied_rate, GST_FORMAT_TIME,
1911         last_stop, stop, last_stop);
1912   }
1913
1914   /* we'll generate a discont on the next buffer */
1915   flacdec->discont = TRUE;
1916   /* the task is running again now */
1917   flacdec->running = TRUE;
1918   gst_pad_start_task (flacdec->sinkpad,
1919       (GstTaskFunction) gst_flac_dec_loop, flacdec->sinkpad);
1920
1921   GST_PAD_STREAM_UNLOCK (flacdec->sinkpad);
1922
1923   return seek_ok;
1924 }
1925
1926 static gboolean
1927 gst_flac_dec_src_event (GstPad * pad, GstEvent * event)
1928 {
1929   gboolean res = TRUE;
1930   GstFlacDec *flacdec;
1931
1932   flacdec = GST_FLAC_DEC (gst_pad_get_parent (pad));
1933
1934   switch (GST_EVENT_TYPE (event)) {
1935     case GST_EVENT_SEEK:{
1936       GST_DEBUG_OBJECT (flacdec, "received seek event %p", event);
1937       /* first, see if we're before a demuxer that
1938        * might handle the seek for us */
1939       gst_event_ref (event);
1940       res = gst_pad_event_default (pad, event);
1941       /* if not, try to handle it ourselves */
1942       if (!res) {
1943         GST_DEBUG_OBJECT (flacdec, "default failed, handling ourselves");
1944         res = gst_flac_dec_handle_seek_event (flacdec, event);
1945       }
1946       gst_event_unref (event);
1947       break;
1948     }
1949     default:
1950       res = gst_pad_event_default (pad, event);
1951       break;
1952   }
1953
1954   gst_object_unref (flacdec);
1955
1956   return res;
1957 }
1958
1959 static gboolean
1960 gst_flac_dec_sink_activate (GstPad * sinkpad)
1961 {
1962   if (gst_pad_check_pull_range (sinkpad))
1963     return gst_pad_activate_pull (sinkpad, TRUE);
1964
1965   return gst_pad_activate_push (sinkpad, TRUE);
1966 }
1967
1968 static gboolean
1969 gst_flac_dec_sink_activate_push (GstPad * sinkpad, gboolean active)
1970 {
1971   GstFlacDec *dec = GST_FLAC_DEC (GST_OBJECT_PARENT (sinkpad));
1972
1973   if (active) {
1974     gst_flac_dec_setup_decoder (dec);
1975     dec->streaming = TRUE;
1976   }
1977   return TRUE;
1978 }
1979
1980 static gboolean
1981 gst_flac_dec_sink_activate_pull (GstPad * sinkpad, gboolean active)
1982 {
1983   gboolean res;
1984
1985   if (active) {
1986     GstFlacDec *flacdec;
1987
1988     flacdec = GST_FLAC_DEC (GST_PAD_PARENT (sinkpad));
1989
1990     flacdec->offset = 0;
1991     gst_flac_dec_setup_decoder (flacdec);
1992     flacdec->running = TRUE;
1993     flacdec->streaming = FALSE;
1994
1995     res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_flac_dec_loop,
1996         sinkpad);
1997   } else {
1998     res = gst_pad_stop_task (sinkpad);
1999   }
2000   return res;
2001 }
2002
2003 static GstStateChangeReturn
2004 gst_flac_dec_change_state (GstElement * element, GstStateChange transition)
2005 {
2006   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2007   GstFlacDec *flacdec = GST_FLAC_DEC (element);
2008
2009   switch (transition) {
2010     case GST_STATE_CHANGE_READY_TO_PAUSED:
2011       flacdec->seeking = FALSE;
2012       flacdec->channels = 0;
2013       flacdec->depth = 0;
2014       flacdec->width = 0;
2015       flacdec->sample_rate = 0;
2016       gst_segment_init (&flacdec->segment, GST_FORMAT_DEFAULT);
2017       break;
2018     default:
2019       break;
2020   }
2021
2022   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2023   if (ret == GST_STATE_CHANGE_FAILURE)
2024     return ret;
2025
2026   switch (transition) {
2027     case GST_STATE_CHANGE_PAUSED_TO_READY:
2028       gst_segment_init (&flacdec->segment, GST_FORMAT_UNDEFINED);
2029       gst_flac_dec_reset_decoders (flacdec);
2030       break;
2031     default:
2032       break;
2033   }
2034
2035   return ret;
2036 }