1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
2 /* GStreamer
3 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) <2006> Nokia Corporation, Stefan Kost <stefan.kost@nokia.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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:element-wavparse
24 *
25 * Parse a .wav file into raw or compressed audio.
26 *
27 * Wavparse supports both push and pull mode operations, making it possible to
28 * stream from a network source.
29 *
30 * <refsect2>
31 * <title>Example launch line</title>
32 * |[
33 * gst-launch-1.0 filesrc location=sine.wav ! wavparse ! audioconvert ! alsasink
34 * ]| Read a wav file and output to the soundcard using the ALSA element. The
35 * wav file is assumed to contain raw uncompressed samples.
36 * |[
37 * gst-launch-1.0 gnomevfssrc location=http://www.example.org/sine.wav ! queue ! wavparse ! audioconvert ! alsasink
38 * ]| Stream data from a network url.
39 * </refsect2>
40 */
41
42 /*
43 * TODO:
44 * http://replaygain.hydrogenaudio.org/file_format_wav.html
45 */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include <string.h>
52 #include <math.h>
53
54 #include "gstwavparse.h"
55 #include "gst/riff/riff-media.h"
56 #include <gst/base/gsttypefindhelper.h>
57 #include <gst/pbutils/descriptions.h>
58 #include <gst/gst-i18n-plugin.h>
59
60 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
61 #define GST_CAT_DEFAULT (wavparse_debug)
62
63 /* Data size chunk of RF64,
64 * see http://tech.ebu.ch/docs/tech/tech3306-2009.pdf */
65 #define GST_RS64_TAG_DS64 GST_MAKE_FOURCC ('d','s','6','4')
66
67 static void gst_wavparse_dispose (GObject * object);
68
69 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
70 GstObject * parent);
71 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
72 GstObject * parent, GstPadMode mode, gboolean active);
73 static gboolean gst_wavparse_send_event (GstElement * element,
74 GstEvent * event);
75 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
76 GstStateChange transition);
77
78 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
79 GstQuery * query);
80 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
81 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
82
83 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
84 GstBuffer * buf);
85 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
86 GstEvent * event);
87 static void gst_wavparse_loop (GstPad * pad);
88 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
89 GstEvent * event);
90
91 static void gst_wavparse_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_wavparse_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
95
96 #define DEFAULT_IGNORE_LENGTH FALSE
97
98 enum
99 {
100 PROP_0,
101 PROP_IGNORE_LENGTH,
102 };
103
104 static GstStaticPadTemplate sink_template_factory =
105 GST_STATIC_PAD_TEMPLATE ("sink",
106 GST_PAD_SINK,
107 GST_PAD_ALWAYS,
108 GST_STATIC_CAPS ("audio/x-wav;audio/x-rf64")
109 );
110
111 #define DEBUG_INIT \
112 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
113
114 #define gst_wavparse_parent_class parent_class
115 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
116 DEBUG_INIT);
117
118 typedef struct
119 {
120 /* Offset Size Description Value
121 * 0x00 4 ID unique identification value
122 * 0x04 4 Position play order position
123 * 0x08 4 Data Chunk ID RIFF ID of corresponding data chunk
124 * 0x0c 4 Chunk Start Byte Offset of Data Chunk *
125 * 0x10 4 Block Start Byte Offset to sample of First Channel
126 * 0x14 4 Sample Offset Byte Offset to sample byte of First Channel
127 */
128 guint32 id;
129 guint32 position;
130 guint32 data_chunk_id;
131 guint32 chunk_start;
132 guint32 block_start;
133 guint32 sample_offset;
134 } GstWavParseCue;
135
136 typedef struct
137 {
138 /* Offset Size Description Value
139 * 0x08 4 Cue Point ID 0 - 0xFFFFFFFF
140 * 0x0c Text
141 */
142 guint32 cue_point_id;
143 gchar *text;
144 } GstWavParseLabl, GstWavParseNote;
145
146 static void
gst_wavparse_class_init(GstWavParseClass * klass)147 gst_wavparse_class_init (GstWavParseClass * klass)
148 {
149 GstElementClass *gstelement_class;
150 GObjectClass *object_class;
151 GstPadTemplate *src_template;
152
153 gstelement_class = (GstElementClass *) klass;
154 object_class = (GObjectClass *) klass;
155
156 parent_class = g_type_class_peek_parent (klass);
157
158 object_class->dispose = gst_wavparse_dispose;
159
160 object_class->set_property = gst_wavparse_set_property;
161 object_class->get_property = gst_wavparse_get_property;
162
163 /**
164 * GstWavParse:ignore-length:
165 *
166 * This selects whether the length found in a data chunk
167 * should be ignored. This may be useful for streamed audio
168 * where the length is unknown until the end of streaming,
169 * and various software/hardware just puts some random value
170 * in there and hopes it doesn't break too much.
171 */
172 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
173 g_param_spec_boolean ("ignore-length",
174 "Ignore length",
175 "Ignore length from the Wave header",
176 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
177 );
178
179 gstelement_class->change_state = gst_wavparse_change_state;
180 gstelement_class->send_event = gst_wavparse_send_event;
181
182 /* register pads */
183 gst_element_class_add_static_pad_template (gstelement_class,
184 &sink_template_factory);
185
186 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
187 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
188 gst_element_class_add_pad_template (gstelement_class, src_template);
189
190 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
191 "Codec/Demuxer/Audio",
192 "Parse a .wav file into raw audio",
193 "Erik Walthinsen <omega@cse.ogi.edu>");
194 }
195
196 static void
gst_wavparse_notes_free(GstWavParseNote * note)197 gst_wavparse_notes_free (GstWavParseNote * note)
198 {
199 if (note)
200 g_free (note->text);
201 g_free (note);
202 }
203
204 static void
gst_wavparse_labls_free(GstWavParseLabl * labl)205 gst_wavparse_labls_free (GstWavParseLabl * labl)
206 {
207 if (labl)
208 g_free (labl->text);
209 g_free (labl);
210 }
211
212 static void
gst_wavparse_reset(GstWavParse * wav)213 gst_wavparse_reset (GstWavParse * wav)
214 {
215 wav->state = GST_WAVPARSE_START;
216
217 /* These will all be set correctly in the fmt chunk */
218 wav->depth = 0;
219 wav->rate = 0;
220 wav->width = 0;
221 wav->channels = 0;
222 wav->blockalign = 0;
223 wav->bps = 0;
224 wav->fact = 0;
225 wav->offset = 0;
226 wav->end_offset = 0;
227 wav->dataleft = 0;
228 wav->datasize = 0;
229 wav->datastart = 0;
230 wav->chunk_size = 0;
231 wav->duration = 0;
232 wav->got_fmt = FALSE;
233 wav->first = TRUE;
234
235 if (wav->seek_event)
236 gst_event_unref (wav->seek_event);
237 wav->seek_event = NULL;
238 if (wav->adapter) {
239 gst_adapter_clear (wav->adapter);
240 g_object_unref (wav->adapter);
241 wav->adapter = NULL;
242 }
243 if (wav->tags)
244 gst_tag_list_unref (wav->tags);
245 wav->tags = NULL;
246 if (wav->toc)
247 gst_toc_unref (wav->toc);
248 wav->toc = NULL;
249 if (wav->cues)
250 g_list_free_full (wav->cues, g_free);
251 wav->cues = NULL;
252 if (wav->labls)
253 g_list_free_full (wav->labls, (GDestroyNotify) gst_wavparse_labls_free);
254 wav->labls = NULL;
255 if (wav->notes)
256 g_list_free_full (wav->notes, (GDestroyNotify) gst_wavparse_notes_free);
257 wav->notes = NULL;
258 if (wav->caps)
259 gst_caps_unref (wav->caps);
260 wav->caps = NULL;
261 if (wav->start_segment)
262 gst_event_unref (wav->start_segment);
263 wav->start_segment = NULL;
264 }
265
266 static void
gst_wavparse_dispose(GObject * object)267 gst_wavparse_dispose (GObject * object)
268 {
269 GstWavParse *wav = GST_WAVPARSE (object);
270
271 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
272 gst_wavparse_reset (wav);
273
274 G_OBJECT_CLASS (parent_class)->dispose (object);
275 }
276
277 static void
gst_wavparse_init(GstWavParse * wavparse)278 gst_wavparse_init (GstWavParse * wavparse)
279 {
280 gst_wavparse_reset (wavparse);
281
282 /* sink */
283 wavparse->sinkpad =
284 gst_pad_new_from_static_template (&sink_template_factory, "sink");
285 gst_pad_set_activate_function (wavparse->sinkpad,
286 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
287 gst_pad_set_activatemode_function (wavparse->sinkpad,
288 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
289 gst_pad_set_chain_function (wavparse->sinkpad,
290 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
291 gst_pad_set_event_function (wavparse->sinkpad,
292 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
293 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
294
295 /* src */
296 wavparse->srcpad =
297 gst_pad_new_from_template (gst_element_class_get_pad_template
298 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
299 gst_pad_use_fixed_caps (wavparse->srcpad);
300 gst_pad_set_query_function (wavparse->srcpad,
301 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
302 gst_pad_set_event_function (wavparse->srcpad,
303 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
304 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
305 }
306
307 static gboolean
gst_wavparse_parse_file_header(GstElement * element,GstBuffer * buf)308 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
309 {
310 guint32 doctype;
311
312 if (!gst_riff_parse_file_header (element, buf, &doctype))
313 return FALSE;
314
315 if (doctype != GST_RIFF_RIFF_WAVE)
316 goto not_wav;
317
318 return TRUE;
319
320 /* ERRORS */
321 not_wav:
322 {
323 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
324 ("File is not a WAVE file: 0x%" G_GINT32_MODIFIER "x", doctype));
325 return FALSE;
326 }
327 }
328
329 static GstFlowReturn
gst_wavparse_stream_init(GstWavParse * wav)330 gst_wavparse_stream_init (GstWavParse * wav)
331 {
332 GstFlowReturn res;
333 GstBuffer *buf = NULL;
334
335 if ((res = gst_pad_pull_range (wav->sinkpad,
336 wav->offset, 12, &buf)) != GST_FLOW_OK)
337 return res;
338 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
339 return GST_FLOW_ERROR;
340
341 wav->offset += 12;
342
343 return GST_FLOW_OK;
344 }
345
346 static gboolean
gst_wavparse_time_to_bytepos(GstWavParse * wav,gint64 ts,gint64 * bytepos)347 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
348 {
349 /* -1 always maps to -1 */
350 if (ts == -1) {
351 *bytepos = -1;
352 return TRUE;
353 }
354
355 /* 0 always maps to 0 */
356 if (ts == 0) {
357 *bytepos = 0;
358 return TRUE;
359 }
360
361 if (wav->bps > 0) {
362 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
363 return TRUE;
364 } else if (wav->fact) {
365 guint64 bps = gst_util_uint64_scale (wav->datasize, wav->rate, wav->fact);
366 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
367 return TRUE;
368 }
369
370 return FALSE;
371 }
372
373 /* This function is used to perform seeks on the element.
374 *
375 * It also works when event is NULL, in which case it will just
376 * start from the last configured segment. This technique is
377 * used when activating the element and to perform the seek in
378 * READY.
379 */
380 static gboolean
gst_wavparse_perform_seek(GstWavParse * wav,GstEvent * event)381 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
382 {
383 gboolean res;
384 gdouble rate;
385 GstFormat format, bformat;
386 GstSeekFlags flags;
387 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
388 gint64 cur, stop, upstream_size;
389 gboolean flush;
390 gboolean update;
391 GstSegment seeksegment = { 0, };
392 gint64 last_stop;
393 guint32 seqnum = GST_SEQNUM_INVALID;
394
395 if (event) {
396 GST_DEBUG_OBJECT (wav, "doing seek with event");
397
398 gst_event_parse_seek (event, &rate, &format, &flags,
399 &cur_type, &cur, &stop_type, &stop);
400 seqnum = gst_event_get_seqnum (event);
401
402 /* no negative rates yet */
403 if (rate < 0.0)
404 goto negative_rate;
405
406 if (format != wav->segment.format) {
407 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
408 gst_format_get_name (format),
409 gst_format_get_name (wav->segment.format));
410 res = TRUE;
411 if (cur_type != GST_SEEK_TYPE_NONE)
412 res =
413 gst_pad_query_convert (wav->srcpad, format, cur,
414 wav->segment.format, &cur);
415 if (res && stop_type != GST_SEEK_TYPE_NONE)
416 res =
417 gst_pad_query_convert (wav->srcpad, format, stop,
418 wav->segment.format, &stop);
419 if (!res)
420 goto no_format;
421
422 format = wav->segment.format;
423 }
424 } else {
425 GST_DEBUG_OBJECT (wav, "doing seek without event");
426 flags = 0;
427 rate = 1.0;
428 cur_type = GST_SEEK_TYPE_SET;
429 stop_type = GST_SEEK_TYPE_SET;
430 }
431
432 /* in push mode, we must delegate to upstream */
433 if (wav->streaming) {
434 gboolean res = FALSE;
435
436 /* if streaming not yet started; only prepare initial newsegment */
437 if (!event || wav->state != GST_WAVPARSE_DATA) {
438 if (wav->start_segment)
439 gst_event_unref (wav->start_segment);
440 wav->start_segment = gst_event_new_segment (&wav->segment);
441 res = TRUE;
442 } else {
443 /* convert seek positions to byte positions in data sections */
444 if (format == GST_FORMAT_TIME) {
445 /* should not fail */
446 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
447 goto no_position;
448 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
449 goto no_position;
450 }
451 /* mind sample boundary and header */
452 if (cur >= 0) {
453 cur -= (cur % wav->bytes_per_sample);
454 cur += wav->datastart;
455 }
456 if (stop >= 0) {
457 stop -= (stop % wav->bytes_per_sample);
458 stop += wav->datastart;
459 }
460 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
461 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
462 stop);
463 /* BYTE seek event */
464 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
465 stop_type, stop);
466 if (seqnum != GST_SEQNUM_INVALID)
467 gst_event_set_seqnum (event, seqnum);
468 res = gst_pad_push_event (wav->sinkpad, event);
469 }
470 return res;
471 }
472
473 /* get flush flag */
474 flush = flags & GST_SEEK_FLAG_FLUSH;
475
476 /* now we need to make sure the streaming thread is stopped. We do this by
477 * either sending a FLUSH_START event downstream which will cause the
478 * streaming thread to stop with a WRONG_STATE.
479 * For a non-flushing seek we simply pause the task, which will happen as soon
480 * as it completes one iteration (and thus might block when the sink is
481 * blocking in preroll). */
482 if (flush) {
483 GstEvent *fevent;
484 GST_DEBUG_OBJECT (wav, "sending flush start");
485
486 fevent = gst_event_new_flush_start ();
487 if (seqnum != GST_SEQNUM_INVALID)
488 gst_event_set_seqnum (fevent, seqnum);
489 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
490 gst_pad_push_event (wav->srcpad, fevent);
491 } else {
492 gst_pad_pause_task (wav->sinkpad);
493 }
494
495 /* we should now be able to grab the streaming thread because we stopped it
496 * with the above flush/pause code */
497 GST_PAD_STREAM_LOCK (wav->sinkpad);
498
499 /* save current position */
500 last_stop = wav->segment.position;
501
502 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
503
504 /* copy segment, we need this because we still need the old
505 * segment when we close the current segment. */
506 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
507
508 /* configure the seek parameters in the seeksegment. We will then have the
509 * right values in the segment to perform the seek */
510 if (event) {
511 GST_DEBUG_OBJECT (wav, "configuring seek");
512 gst_segment_do_seek (&seeksegment, rate, format, flags,
513 cur_type, cur, stop_type, stop, &update);
514 }
515
516 /* figure out the last position we need to play. If it's configured (stop !=
517 * -1), use that, else we play until the total duration of the file */
518 if ((stop = seeksegment.stop) == -1)
519 stop = seeksegment.duration;
520
521 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
522 if ((cur_type != GST_SEEK_TYPE_NONE)) {
523 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
524 * we can just copy the last_stop. If not, we use the bps to convert TIME to
525 * bytes. */
526 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
527 (gint64 *) & wav->offset))
528 wav->offset = seeksegment.position;
529 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
530 wav->offset -= (wav->offset % wav->bytes_per_sample);
531 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
532 wav->offset += wav->datastart;
533 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
534 } else {
535 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
536 wav->offset);
537 }
538
539 if (stop_type != GST_SEEK_TYPE_NONE) {
540 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
541 wav->end_offset = stop;
542 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
543 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
544 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
545 wav->end_offset += wav->datastart;
546 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
547 } else {
548 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
549 wav->end_offset);
550 }
551
552 /* make sure filesize is not exceeded due to rounding errors or so,
553 * same precaution as in _stream_headers */
554 bformat = GST_FORMAT_BYTES;
555 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
556 wav->end_offset = MIN (wav->end_offset, upstream_size);
557
558 if (wav->datasize > 0 && wav->end_offset > wav->datastart + wav->datasize)
559 wav->end_offset = wav->datastart + wav->datasize;
560
561 /* this is the range of bytes we will use for playback */
562 wav->offset = MIN (wav->offset, wav->end_offset);
563 wav->dataleft = wav->end_offset - wav->offset;
564
565 GST_DEBUG_OBJECT (wav,
566 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
567 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
568 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
569
570 /* prepare for streaming again */
571 if (flush) {
572 GstEvent *fevent;
573
574 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
575 GST_DEBUG_OBJECT (wav, "sending flush stop");
576
577 fevent = gst_event_new_flush_stop (TRUE);
578 if (seqnum != GST_SEQNUM_INVALID)
579 gst_event_set_seqnum (fevent, seqnum);
580 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
581 gst_pad_push_event (wav->srcpad, fevent);
582 }
583
584 /* now we did the seek and can activate the new segment values */
585 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
586
587 /* if we're doing a segment seek, post a SEGMENT_START message */
588 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
589 gst_element_post_message (GST_ELEMENT_CAST (wav),
590 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
591 wav->segment.format, wav->segment.position));
592 }
593
594 /* now create the newsegment */
595 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
596 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
597
598 /* store the newsegment event so it can be sent from the streaming thread. */
599 if (wav->start_segment)
600 gst_event_unref (wav->start_segment);
601 wav->start_segment = gst_event_new_segment (&wav->segment);
602 if (seqnum != GST_SEQNUM_INVALID)
603 gst_event_set_seqnum (wav->start_segment, seqnum);
604
605 /* mark discont if we are going to stream from another position. */
606 if (last_stop != wav->segment.position) {
607 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
608 wav->discont = TRUE;
609 }
610
611 /* and start the streaming task again */
612 if (!wav->streaming) {
613 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
614 wav->sinkpad, NULL);
615 }
616
617 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
618
619 return TRUE;
620
621 /* ERRORS */
622 negative_rate:
623 {
624 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
625 return FALSE;
626 }
627 no_format:
628 {
629 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
630 return FALSE;
631 }
632 no_position:
633 {
634 GST_DEBUG_OBJECT (wav,
635 "Could not determine byte position for desired time");
636 return FALSE;
637 }
638 }
639
640 /*
641 * gst_wavparse_peek_chunk_info:
642 * @wav Wavparse object
643 * @tag holder for tag
644 * @size holder for tag size
645 *
646 * Peek next chunk info (tag and size)
647 *
648 * Returns: %TRUE when the chunk info (header) is available
649 */
650 static gboolean
gst_wavparse_peek_chunk_info(GstWavParse * wav,guint32 * tag,guint32 * size)651 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
652 {
653 const guint8 *data = NULL;
654
655 if (gst_adapter_available (wav->adapter) < 8)
656 return FALSE;
657
658 data = gst_adapter_map (wav->adapter, 8);
659 *tag = GST_READ_UINT32_LE (data);
660 *size = GST_READ_UINT32_LE (data + 4);
661 gst_adapter_unmap (wav->adapter);
662
663 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
664 GST_FOURCC_ARGS (*tag));
665
666 return TRUE;
667 }
668
669 /*
670 * gst_wavparse_peek_chunk:
671 * @wav Wavparse object
672 * @tag holder for tag
673 * @size holder for tag size
674 *
675 * Peek enough data for one full chunk
676 *
677 * Returns: %TRUE when the full chunk is available
678 */
679 static gboolean
gst_wavparse_peek_chunk(GstWavParse * wav,guint32 * tag,guint32 * size)680 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
681 {
682 guint32 peek_size = 0;
683 guint available;
684
685 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
686 return FALSE;
687
688 /* size 0 -> empty data buffer would surprise most callers,
689 * large size -> do not bother trying to squeeze that into adapter,
690 * so we throw poor man's exception, which can be caught if caller really
691 * wants to handle 0 size chunk */
692 if (!(*size) || (*size) >= (1 << 30)) {
693 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
694 *size, GST_FOURCC_ARGS (*tag));
695 /* chain should give up */
696 wav->abort_buffering = TRUE;
697 return FALSE;
698 }
699 peek_size = (*size + 1) & ~1;
700 available = gst_adapter_available (wav->adapter);
701
702 if (available >= (8 + peek_size)) {
703 return TRUE;
704 } else {
705 GST_LOG ("but only %u bytes available now", available);
706 return FALSE;
707 }
708 }
709
710 /*
711 * gst_wavparse_calculate_duration:
712 * @wav: wavparse object
713 *
714 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
715 * fallback.
716 *
717 * Returns: %TRUE if duration is available.
718 */
719 static gboolean
gst_wavparse_calculate_duration(GstWavParse * wav)720 gst_wavparse_calculate_duration (GstWavParse * wav)
721 {
722 if (wav->duration > 0)
723 return TRUE;
724
725 if (wav->bps > 0) {
726 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
727 wav->duration =
728 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
729 (guint64) wav->bps);
730 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
731 GST_TIME_ARGS (wav->duration));
732 return TRUE;
733 } else if (wav->fact) {
734 wav->duration =
735 gst_util_uint64_scale_ceil (GST_SECOND, wav->fact, wav->rate);
736 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
737 GST_TIME_ARGS (wav->duration));
738 return TRUE;
739 }
740 return FALSE;
741 }
742
743 static gboolean
gst_waveparse_ignore_chunk(GstWavParse * wav,GstBuffer * buf,guint32 tag,guint32 size)744 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
745 guint32 size)
746 {
747 guint flush;
748
749 if (wav->streaming) {
750 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
751 return FALSE;
752 }
753 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
754 GST_FOURCC_ARGS (tag));
755 flush = 8 + ((size + 1) & ~1);
756 wav->offset += flush;
757 if (wav->streaming) {
758 gst_adapter_flush (wav->adapter, flush);
759 } else {
760 gst_buffer_unref (buf);
761 }
762
763 return TRUE;
764 }
765
766 /*
767 * gst_wavparse_cue_chunk:
768 * @wav GstWavParse object
769 * @data holder for data
770 * @size holder for data size
771 *
772 * Parse cue chunk from @data to wav->cues.
773 *
774 * Returns: %TRUE when cue chunk is available
775 */
776 static gboolean
gst_wavparse_cue_chunk(GstWavParse * wav,const guint8 * data,guint32 size)777 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
778 {
779 guint32 i, ncues;
780 GList *cues = NULL;
781 GstWavParseCue *cue;
782
783 if (wav->cues) {
784 GST_WARNING_OBJECT (wav, "found another cue's");
785 return TRUE;
786 }
787
788 ncues = GST_READ_UINT32_LE (data);
789
790 if (size < 4 + ncues * 24) {
791 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
792 return FALSE;
793 }
794
795 /* parse data */
796 data += 4;
797 for (i = 0; i < ncues; i++) {
798 cue = g_new0 (GstWavParseCue, 1);
799 cue->id = GST_READ_UINT32_LE (data);
800 cue->position = GST_READ_UINT32_LE (data + 4);
801 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
802 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
803 cue->block_start = GST_READ_UINT32_LE (data + 16);
804 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
805 cues = g_list_append (cues, cue);
806 data += 24;
807 }
808
809 wav->cues = cues;
810
811 return TRUE;
812 }
813
814 /*
815 * gst_wavparse_labl_chunk:
816 * @wav GstWavParse object
817 * @data holder for data
818 * @size holder for data size
819 *
820 * Parse labl from @data to wav->labls.
821 *
822 * Returns: %TRUE when labl chunk is available
823 */
824 static gboolean
gst_wavparse_labl_chunk(GstWavParse * wav,const guint8 * data,guint32 size)825 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
826 {
827 GstWavParseLabl *labl;
828
829 if (size < 5)
830 return FALSE;
831
832 labl = g_new0 (GstWavParseLabl, 1);
833
834 /* parse data */
835 data += 8;
836 labl->cue_point_id = GST_READ_UINT32_LE (data);
837 labl->text = g_memdup (data + 4, size - 4);
838
839 wav->labls = g_list_append (wav->labls, labl);
840
841 return TRUE;
842 }
843
844 /*
845 * gst_wavparse_note_chunk:
846 * @wav GstWavParse object
847 * @data holder for data
848 * @size holder for data size
849 *
850 * Parse note from @data to wav->notes.
851 *
852 * Returns: %TRUE when note chunk is available
853 */
854 static gboolean
gst_wavparse_note_chunk(GstWavParse * wav,const guint8 * data,guint32 size)855 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
856 {
857 GstWavParseNote *note;
858
859 if (size < 5)
860 return FALSE;
861
862 note = g_new0 (GstWavParseNote, 1);
863
864 /* parse data */
865 data += 8;
866 note->cue_point_id = GST_READ_UINT32_LE (data);
867 note->text = g_memdup (data + 4, size - 4);
868
869 wav->notes = g_list_append (wav->notes, note);
870
871 return TRUE;
872 }
873
874 /*
875 * gst_wavparse_smpl_chunk:
876 * @wav GstWavParse object
877 * @data holder for data
878 * @size holder for data size
879 *
880 * Parse smpl chunk from @data.
881 *
882 * Returns: %TRUE when cue chunk is available
883 */
884 static gboolean
gst_wavparse_smpl_chunk(GstWavParse * wav,const guint8 * data,guint32 size)885 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
886 {
887 guint32 note_number;
888
889 /*
890 manufacturer_id = GST_READ_UINT32_LE (data);
891 product_id = GST_READ_UINT32_LE (data + 4);
892 sample_period = GST_READ_UINT32_LE (data + 8);
893 */
894 note_number = GST_READ_UINT32_LE (data + 12);
895 /*
896 pitch_fraction = GST_READ_UINT32_LE (data + 16);
897 SMPTE_format = GST_READ_UINT32_LE (data + 20);
898 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
899 num_sample_loops = GST_READ_UINT32_LE (data + 28);
900 List of Sample Loops, 24 bytes each
901 */
902
903 if (!wav->tags)
904 wav->tags = gst_tag_list_new_empty ();
905 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
906 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
907 return TRUE;
908 }
909
910 /*
911 * gst_wavparse_adtl_chunk:
912 * @wav GstWavParse object
913 * @data holder for data
914 * @size holder for data size
915 *
916 * Parse adtl from @data.
917 *
918 * Returns: %TRUE when adtl chunk is available
919 */
920 static gboolean
gst_wavparse_adtl_chunk(GstWavParse * wav,const guint8 * data,guint32 size)921 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
922 {
923 guint32 ltag, lsize, offset = 0;
924
925 while (size >= 8) {
926 ltag = GST_READ_UINT32_LE (data + offset);
927 lsize = GST_READ_UINT32_LE (data + offset + 4);
928
929 if (lsize + 8 > size) {
930 GST_WARNING_OBJECT (wav, "Invalid adtl size: %u + 8 > %u", lsize, size);
931 return FALSE;
932 }
933
934 switch (ltag) {
935 case GST_RIFF_TAG_labl:
936 gst_wavparse_labl_chunk (wav, data + offset, size);
937 break;
938 case GST_RIFF_TAG_note:
939 gst_wavparse_note_chunk (wav, data + offset, size);
940 break;
941 default:
942 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
943 GST_FOURCC_ARGS (ltag));
944 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
945 break;
946 }
947 offset += 8 + GST_ROUND_UP_2 (lsize);
948 size -= 8 + GST_ROUND_UP_2 (lsize);
949 }
950
951 return TRUE;
952 }
953
954 static GstTagList *
gst_wavparse_get_tags_toc_entry(GstToc * toc,gchar * id)955 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
956 {
957 GstTagList *tags = NULL;
958 GstTocEntry *entry = NULL;
959
960 entry = gst_toc_find_entry (toc, id);
961 if (entry != NULL) {
962 tags = gst_toc_entry_get_tags (entry);
963 if (tags == NULL) {
964 tags = gst_tag_list_new_empty ();
965 gst_toc_entry_set_tags (entry, tags);
966 }
967 }
968
969 return tags;
970 }
971
972 /*
973 * gst_wavparse_create_toc:
974 * @wav GstWavParse object
975 *
976 * Create TOC from wav->cues and wav->labls.
977 */
978 static gboolean
gst_wavparse_create_toc(GstWavParse * wav)979 gst_wavparse_create_toc (GstWavParse * wav)
980 {
981 gint64 start, stop;
982 gchar *id;
983 GList *list;
984 GstWavParseCue *cue;
985 GstWavParseLabl *labl;
986 GstWavParseNote *note;
987 GstTagList *tags;
988 GstToc *toc;
989 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
990
991 GST_OBJECT_LOCK (wav);
992 if (wav->toc) {
993 GST_OBJECT_UNLOCK (wav);
994 GST_WARNING_OBJECT (wav, "found another TOC");
995 return FALSE;
996 }
997
998 if (!wav->cues) {
999 GST_OBJECT_UNLOCK (wav);
1000 return TRUE;
1001 }
1002
1003 /* FIXME: send CURRENT scope toc too */
1004 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
1005
1006 /* add cue edition */
1007 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
1008 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
1009 gst_toc_append_entry (toc, entry);
1010
1011 /* add tracks in cue edition */
1012 list = wav->cues;
1013 while (list) {
1014 cue = list->data;
1015 prev_subentry = cur_subentry;
1016 /* previous track stop time = current track start time */
1017 if (prev_subentry != NULL) {
1018 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
1019 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1020 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
1021 }
1022 id = g_strdup_printf ("%08x", cue->id);
1023 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
1024 g_free (id);
1025 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1026 stop = wav->duration;
1027 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
1028 gst_toc_entry_append_sub_entry (entry, cur_subentry);
1029 list = g_list_next (list);
1030 }
1031
1032 /* add tags in tracks */
1033 list = wav->labls;
1034 while (list) {
1035 labl = list->data;
1036 id = g_strdup_printf ("%08x", labl->cue_point_id);
1037 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1038 g_free (id);
1039 if (tags != NULL) {
1040 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1041 NULL);
1042 }
1043 list = g_list_next (list);
1044 }
1045 list = wav->notes;
1046 while (list) {
1047 note = list->data;
1048 id = g_strdup_printf ("%08x", note->cue_point_id);
1049 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1050 g_free (id);
1051 if (tags != NULL) {
1052 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1053 note->text, NULL);
1054 }
1055 list = g_list_next (list);
1056 }
1057
1058 /* send data as TOC */
1059 wav->toc = toc;
1060
1061 /* send TOC event */
1062 if (wav->toc) {
1063 GST_OBJECT_UNLOCK (wav);
1064 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1065 }
1066
1067 return TRUE;
1068 }
1069
1070 #define MAX_BUFFER_SIZE 4096
1071
1072 static gboolean
parse_ds64(GstWavParse * wav,GstBuffer * buf)1073 parse_ds64 (GstWavParse * wav, GstBuffer * buf)
1074 {
1075 GstMapInfo map;
1076 guint32 dataSizeLow, dataSizeHigh;
1077 guint32 sampleCountLow, sampleCountHigh;
1078
1079 gst_buffer_map (buf, &map, GST_MAP_READ);
1080 dataSizeLow = GST_READ_UINT32_LE (map.data + 2 * 4);
1081 dataSizeHigh = GST_READ_UINT32_LE (map.data + 3 * 4);
1082 sampleCountLow = GST_READ_UINT32_LE (map.data + 4 * 4);
1083 sampleCountHigh = GST_READ_UINT32_LE (map.data + 5 * 4);
1084 gst_buffer_unmap (buf, &map);
1085 if (dataSizeHigh != 0xFFFFFFFF && dataSizeLow != 0xFFFFFFFF) {
1086 wav->datasize = ((guint64) dataSizeHigh << 32) | dataSizeLow;
1087 }
1088 if (sampleCountHigh != 0xFFFFFFFF && sampleCountLow != 0xFFFFFFFF) {
1089 wav->fact = ((guint64) sampleCountHigh << 32) | sampleCountLow;
1090 }
1091
1092 GST_DEBUG_OBJECT (wav, "Got 'ds64' TAG, datasize : %" G_GINT64_FORMAT
1093 " fact: %" G_GINT64_FORMAT, wav->datasize, wav->fact);
1094 return TRUE;
1095 }
1096
1097 static GstFlowReturn
gst_wavparse_stream_headers(GstWavParse * wav)1098 gst_wavparse_stream_headers (GstWavParse * wav)
1099 {
1100 GstFlowReturn res = GST_FLOW_OK;
1101 GstBuffer *buf = NULL;
1102 gst_riff_strf_auds *header = NULL;
1103 guint32 tag, size;
1104 gboolean gotdata = FALSE;
1105 GstCaps *caps = NULL;
1106 gchar *codec_name = NULL;
1107 gint64 upstream_size = 0;
1108 GstStructure *s;
1109
1110 /* search for "_fmt" chunk, which must be before "data" */
1111 while (!wav->got_fmt) {
1112 GstBuffer *extra;
1113
1114 if (wav->streaming) {
1115 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1116 return res;
1117
1118 gst_adapter_flush (wav->adapter, 8);
1119 wav->offset += 8;
1120
1121 if (size) {
1122 buf = gst_adapter_take_buffer (wav->adapter, size);
1123 if (size & 1)
1124 gst_adapter_flush (wav->adapter, 1);
1125 wav->offset += GST_ROUND_UP_2 (size);
1126 } else {
1127 buf = gst_buffer_new ();
1128 }
1129 } else {
1130 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1131 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1132 return res;
1133 }
1134
1135 if (tag == GST_RS64_TAG_DS64) {
1136 if (!parse_ds64 (wav, buf))
1137 goto fail;
1138 else
1139 continue;
1140 }
1141
1142 if (tag != GST_RIFF_TAG_fmt) {
1143 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1144 GST_FOURCC_ARGS (tag));
1145 gst_buffer_unref (buf);
1146 buf = NULL;
1147 continue;
1148 }
1149
1150 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1151 &extra)))
1152 goto parse_header_error;
1153
1154 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1155
1156 /* do sanity checks of header fields */
1157 if (header->channels == 0)
1158 goto no_channels;
1159 if (header->rate == 0)
1160 goto no_rate;
1161
1162 GST_DEBUG_OBJECT (wav, "creating the caps");
1163
1164 /* Note: gst_riff_create_audio_caps might need to fix values in
1165 * the header header depending on the format, so call it first */
1166 /* FIXME: Need to handle the channel reorder map */
1167 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1168 NULL, &codec_name, NULL);
1169
1170 if (extra)
1171 gst_buffer_unref (extra);
1172
1173 if (!caps)
1174 goto unknown_format;
1175
1176 /* If we got raw audio from upstream, we remove the codec_data field,
1177 * which may have been added if the wav header included an extended
1178 * chunk. We want to keep it for non raw audio.
1179 */
1180 s = gst_caps_get_structure (caps, 0);
1181 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1182 gst_structure_remove_field (s, "codec_data");
1183 }
1184
1185 /* do more sanity checks of header fields
1186 * (these can be sanitized by gst_riff_create_audio_caps()
1187 */
1188 wav->format = header->format;
1189 wav->rate = header->rate;
1190 wav->channels = header->channels;
1191 wav->blockalign = header->blockalign;
1192 wav->depth = header->bits_per_sample;
1193 wav->av_bps = header->av_bps;
1194 wav->vbr = FALSE;
1195
1196 g_free (header);
1197 header = NULL;
1198
1199 /* do format specific handling */
1200 switch (wav->format) {
1201 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1202 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1203 {
1204 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1205 * bitrate inside the mpeg stream */
1206 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1207 wav->bps = 0;
1208 break;
1209 }
1210 case GST_RIFF_WAVE_FORMAT_PCM:
1211 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1212 goto invalid_blockalign;
1213 /* fall through */
1214 default:
1215 if (wav->av_bps > wav->blockalign * wav->rate)
1216 goto invalid_bps;
1217 /* use the configured bps */
1218 wav->bps = wav->av_bps;
1219 break;
1220 }
1221
1222 wav->width = (wav->blockalign * 8) / wav->channels;
1223 wav->bytes_per_sample = wav->channels * wav->width / 8;
1224
1225 if (wav->bytes_per_sample <= 0)
1226 goto no_bytes_per_sample;
1227
1228 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1229 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1230 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1231 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1232 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1233 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1234 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1235
1236 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1237 * formats). This will make the element output a BYTE format segment and
1238 * will not timestamp the outgoing buffers.
1239 */
1240 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1241
1242 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1243
1244 /* create pad later so we can sniff the first few bytes
1245 * of the real data and correct our caps if necessary */
1246 gst_caps_replace (&wav->caps, caps);
1247 gst_caps_replace (&caps, NULL);
1248
1249 wav->got_fmt = TRUE;
1250
1251 if (wav->tags == NULL)
1252 wav->tags = gst_tag_list_new_empty ();
1253
1254 {
1255 GstCaps *templ_caps = gst_pad_get_pad_template_caps (wav->sinkpad);
1256 gst_pb_utils_add_codec_description_to_tag_list (wav->tags,
1257 GST_TAG_CONTAINER_FORMAT, templ_caps);
1258 gst_caps_unref (templ_caps);
1259 }
1260
1261 /* If bps is nonzero, then we do have a valid bitrate that can be
1262 * announced in a tag list. */
1263 if (wav->bps) {
1264 guint bitrate = wav->bps * 8;
1265 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1266 GST_TAG_BITRATE, bitrate, NULL);
1267 }
1268
1269 if (codec_name) {
1270 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1271 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1272
1273 g_free (codec_name);
1274 codec_name = NULL;
1275 }
1276
1277 }
1278
1279 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1280 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1281
1282 /* loop headers until we get data */
1283 while (!gotdata) {
1284 if (wav->streaming) {
1285 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1286 goto exit;
1287 } else {
1288 GstMapInfo map;
1289
1290 buf = NULL;
1291 if ((res =
1292 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1293 &buf)) != GST_FLOW_OK)
1294 goto header_read_error;
1295 gst_buffer_map (buf, &map, GST_MAP_READ);
1296 tag = GST_READ_UINT32_LE (map.data);
1297 size = GST_READ_UINT32_LE (map.data + 4);
1298 gst_buffer_unmap (buf, &map);
1299 }
1300
1301 GST_INFO_OBJECT (wav,
1302 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT ", size %"
1303 G_GUINT32_FORMAT, GST_FOURCC_ARGS (tag), wav->offset, size);
1304
1305 /* Maximum valid size is INT_MAX */
1306 if (size & 0x80000000) {
1307 GST_WARNING_OBJECT (wav, "Invalid size, clipping to 0x7fffffff");
1308 size = 0x7fffffff;
1309 }
1310
1311 /* Clip to upstream size if known */
1312 if (upstream_size > 0 && size + wav->offset > upstream_size) {
1313 GST_WARNING_OBJECT (wav, "Clipping chunk size to file size");
1314 g_assert (upstream_size >= wav->offset);
1315 size = upstream_size - wav->offset;
1316 }
1317
1318 /* wav is a st00pid format, we don't know for sure where data starts.
1319 * So we have to go bit by bit until we find the 'data' header
1320 */
1321 switch (tag) {
1322 case GST_RIFF_TAG_data:{
1323 guint64 size64;
1324
1325 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1326 size64 = size;
1327 if (wav->ignore_length) {
1328 GST_DEBUG_OBJECT (wav, "Ignoring length");
1329 size64 = 0;
1330 }
1331 if (wav->streaming) {
1332 gst_adapter_flush (wav->adapter, 8);
1333 gotdata = TRUE;
1334 } else {
1335 gst_buffer_unref (buf);
1336 }
1337 wav->offset += 8;
1338 wav->datastart = wav->offset;
1339 /* use size from ds64 chunk if available */
1340 if (size64 == -1 && wav->datasize > 0) {
1341 GST_DEBUG_OBJECT (wav, "Using ds64 datasize");
1342 size64 = wav->datasize;
1343 }
1344 wav->chunk_size = size64;
1345
1346 /* If size is zero, then the data chunk probably actually extends to
1347 the end of the file */
1348 if (size64 == 0 && upstream_size) {
1349 size64 = upstream_size - wav->datastart;
1350 }
1351 /* Or the file might be truncated */
1352 else if (upstream_size) {
1353 size64 = MIN (size64, (upstream_size - wav->datastart));
1354 }
1355 wav->datasize = size64;
1356 wav->dataleft = size64;
1357 wav->end_offset = size64 + wav->datastart;
1358 if (!wav->streaming) {
1359 /* We will continue parsing tags 'till end */
1360 wav->offset += size64;
1361 }
1362 GST_DEBUG_OBJECT (wav, "datasize = %" G_GUINT64_FORMAT, size64);
1363 break;
1364 }
1365 case GST_RIFF_TAG_fact:{
1366 if (wav->fact == 0 &&
1367 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1368 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1369 const guint data_size = 4;
1370
1371 GST_INFO_OBJECT (wav, "Have fact chunk");
1372 if (size < data_size) {
1373 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1374 /* need more data */
1375 goto exit;
1376 }
1377 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1378 data_size, size);
1379 break;
1380 }
1381 /* number of samples (for compressed formats) */
1382 if (wav->streaming) {
1383 const guint8 *data = NULL;
1384
1385 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1386 goto exit;
1387 }
1388 gst_adapter_flush (wav->adapter, 8);
1389 data = gst_adapter_map (wav->adapter, data_size);
1390 wav->fact = GST_READ_UINT32_LE (data);
1391 gst_adapter_unmap (wav->adapter);
1392 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1393 } else {
1394 gst_buffer_unref (buf);
1395 buf = NULL;
1396 if ((res =
1397 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1398 data_size, &buf)) != GST_FLOW_OK)
1399 goto header_read_error;
1400 gst_buffer_extract (buf, 0, &wav->fact, 4);
1401 wav->fact = GUINT32_FROM_LE (wav->fact);
1402 gst_buffer_unref (buf);
1403 }
1404 GST_DEBUG_OBJECT (wav, "have fact %" G_GUINT64_FORMAT, wav->fact);
1405 wav->offset += 8 + GST_ROUND_UP_2 (size);
1406 break;
1407 } else {
1408 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1409 /* need more data */
1410 goto exit;
1411 }
1412 }
1413 break;
1414 }
1415 case GST_RIFF_TAG_acid:{
1416 const gst_riff_acid *acid = NULL;
1417 const guint data_size = sizeof (gst_riff_acid);
1418 gfloat tempo;
1419
1420 GST_INFO_OBJECT (wav, "Have acid chunk");
1421 if (size < data_size) {
1422 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1423 /* need more data */
1424 goto exit;
1425 }
1426 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1427 data_size, size);
1428 break;
1429 }
1430 if (wav->streaming) {
1431 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1432 goto exit;
1433 }
1434 gst_adapter_flush (wav->adapter, 8);
1435 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1436 data_size);
1437 tempo = acid->tempo;
1438 gst_adapter_unmap (wav->adapter);
1439 } else {
1440 GstMapInfo map;
1441 gst_buffer_unref (buf);
1442 buf = NULL;
1443 if ((res =
1444 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1445 size, &buf)) != GST_FLOW_OK)
1446 goto header_read_error;
1447 gst_buffer_map (buf, &map, GST_MAP_READ);
1448 acid = (const gst_riff_acid *) map.data;
1449 tempo = acid->tempo;
1450 gst_buffer_unmap (buf, &map);
1451 }
1452 /* send data as tags */
1453 if (!wav->tags)
1454 wav->tags = gst_tag_list_new_empty ();
1455 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1456 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1457
1458 size = GST_ROUND_UP_2 (size);
1459 if (wav->streaming) {
1460 gst_adapter_flush (wav->adapter, size);
1461 } else {
1462 gst_buffer_unref (buf);
1463 }
1464 wav->offset += 8 + size;
1465 break;
1466 }
1467 /* FIXME: all list tags after data are ignored in streaming mode */
1468 case GST_RIFF_TAG_LIST:{
1469 guint32 ltag;
1470
1471 if (wav->streaming) {
1472 const guint8 *data = NULL;
1473
1474 if (gst_adapter_available (wav->adapter) < 12) {
1475 goto exit;
1476 }
1477 data = gst_adapter_map (wav->adapter, 12);
1478 ltag = GST_READ_UINT32_LE (data + 8);
1479 gst_adapter_unmap (wav->adapter);
1480 } else {
1481 gst_buffer_unref (buf);
1482 buf = NULL;
1483 if ((res =
1484 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1485 &buf)) != GST_FLOW_OK)
1486 goto header_read_error;
1487 gst_buffer_extract (buf, 8, <ag, 4);
1488 ltag = GUINT32_FROM_LE (ltag);
1489 }
1490 switch (ltag) {
1491 case GST_RIFF_LIST_INFO:{
1492 const gint data_size = size - 4;
1493 GstTagList *new;
1494
1495 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1496 if (wav->streaming) {
1497 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1498 goto exit;
1499 }
1500 gst_adapter_flush (wav->adapter, 12);
1501 wav->offset += 12;
1502 if (data_size > 0) {
1503 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1504 if (data_size & 1)
1505 gst_adapter_flush (wav->adapter, 1);
1506 }
1507 } else {
1508 wav->offset += 12;
1509 gst_buffer_unref (buf);
1510 buf = NULL;
1511 if (data_size > 0) {
1512 if ((res =
1513 gst_pad_pull_range (wav->sinkpad, wav->offset,
1514 data_size, &buf)) != GST_FLOW_OK)
1515 goto header_read_error;
1516 }
1517 }
1518 if (data_size > 0) {
1519 /* parse tags */
1520 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1521 if (new) {
1522 GstTagList *old = wav->tags;
1523 wav->tags =
1524 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1525 if (old)
1526 gst_tag_list_unref (old);
1527 gst_tag_list_unref (new);
1528 }
1529 gst_buffer_unref (buf);
1530 wav->offset += GST_ROUND_UP_2 (data_size);
1531 }
1532 break;
1533 }
1534 case GST_RIFF_LIST_adtl:{
1535 const gint data_size = size - 4;
1536
1537 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1538 if (wav->streaming) {
1539 const guint8 *data = NULL;
1540
1541 gst_adapter_flush (wav->adapter, 12);
1542 wav->offset += 12;
1543 data = gst_adapter_map (wav->adapter, data_size);
1544 gst_wavparse_adtl_chunk (wav, data, data_size);
1545 gst_adapter_unmap (wav->adapter);
1546 } else {
1547 GstMapInfo map;
1548
1549 gst_buffer_unref (buf);
1550 buf = NULL;
1551 wav->offset += 12;
1552 if ((res =
1553 gst_pad_pull_range (wav->sinkpad, wav->offset,
1554 data_size, &buf)) != GST_FLOW_OK)
1555 goto header_read_error;
1556 gst_buffer_map (buf, &map, GST_MAP_READ);
1557 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1558 data_size);
1559 gst_buffer_unmap (buf, &map);
1560 }
1561 wav->offset += GST_ROUND_UP_2 (data_size);
1562 break;
1563 }
1564 default:
1565 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1566 GST_FOURCC_ARGS (ltag));
1567 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1568 /* need more data */
1569 goto exit;
1570 break;
1571 }
1572 break;
1573 }
1574 case GST_RIFF_TAG_cue:{
1575 const guint data_size = size;
1576
1577 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1578 if (wav->streaming) {
1579 const guint8 *data = NULL;
1580
1581 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1582 goto exit;
1583 }
1584 gst_adapter_flush (wav->adapter, 8);
1585 wav->offset += 8;
1586 data = gst_adapter_map (wav->adapter, data_size);
1587 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1588 goto header_read_error;
1589 }
1590 gst_adapter_unmap (wav->adapter);
1591 } else {
1592 GstMapInfo map;
1593
1594 wav->offset += 8;
1595 gst_buffer_unref (buf);
1596 buf = NULL;
1597 if ((res =
1598 gst_pad_pull_range (wav->sinkpad, wav->offset,
1599 data_size, &buf)) != GST_FLOW_OK)
1600 goto header_read_error;
1601 gst_buffer_map (buf, &map, GST_MAP_READ);
1602 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1603 data_size)) {
1604 goto header_read_error;
1605 }
1606 gst_buffer_unmap (buf, &map);
1607 }
1608 size = GST_ROUND_UP_2 (size);
1609 if (wav->streaming) {
1610 gst_adapter_flush (wav->adapter, size);
1611 } else {
1612 gst_buffer_unref (buf);
1613 }
1614 size = GST_ROUND_UP_2 (size);
1615 wav->offset += size;
1616 break;
1617 }
1618 case GST_RIFF_TAG_smpl:{
1619 const gint data_size = size;
1620
1621 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1622 if (wav->streaming) {
1623 const guint8 *data = NULL;
1624
1625 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1626 goto exit;
1627 }
1628 gst_adapter_flush (wav->adapter, 8);
1629 wav->offset += 8;
1630 data = gst_adapter_map (wav->adapter, data_size);
1631 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1632 goto header_read_error;
1633 }
1634 gst_adapter_unmap (wav->adapter);
1635 } else {
1636 GstMapInfo map;
1637
1638 wav->offset += 8;
1639 gst_buffer_unref (buf);
1640 buf = NULL;
1641 if ((res =
1642 gst_pad_pull_range (wav->sinkpad, wav->offset,
1643 data_size, &buf)) != GST_FLOW_OK)
1644 goto header_read_error;
1645 gst_buffer_map (buf, &map, GST_MAP_READ);
1646 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1647 data_size)) {
1648 goto header_read_error;
1649 }
1650 gst_buffer_unmap (buf, &map);
1651 }
1652 size = GST_ROUND_UP_2 (size);
1653 if (wav->streaming) {
1654 gst_adapter_flush (wav->adapter, size);
1655 } else {
1656 gst_buffer_unref (buf);
1657 }
1658 size = GST_ROUND_UP_2 (size);
1659 wav->offset += size;
1660 break;
1661 }
1662 default:
1663 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1664 GST_FOURCC_ARGS (tag));
1665 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1666 /* need more data */
1667 goto exit;
1668 break;
1669 }
1670
1671 if (upstream_size && (wav->offset >= upstream_size)) {
1672 /* Now we are gone through the whole file */
1673 gotdata = TRUE;
1674 }
1675 }
1676
1677 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1678
1679 if (wav->bps <= 0 && wav->fact) {
1680 #if 0
1681 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1682 wav->bps =
1683 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1684 (guint64) wav->fact);
1685 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1686 #endif
1687 wav->vbr = TRUE;
1688 }
1689
1690 if (gst_wavparse_calculate_duration (wav)) {
1691 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1692 if (!wav->ignore_length)
1693 wav->segment.duration = wav->duration;
1694 if (!wav->toc)
1695 gst_wavparse_create_toc (wav);
1696 } else {
1697 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1698 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1699 if (!wav->ignore_length)
1700 wav->segment.duration = wav->datasize;
1701 }
1702
1703 /* now we have all the info to perform a pending seek if any, if no
1704 * event, this will still do the right thing and it will also send
1705 * the right newsegment event downstream. */
1706 gst_wavparse_perform_seek (wav, wav->seek_event);
1707 /* remove pending event */
1708 gst_event_replace (&wav->seek_event, NULL);
1709
1710 /* we just started, we are discont */
1711 wav->discont = TRUE;
1712
1713 wav->state = GST_WAVPARSE_DATA;
1714
1715 /* determine reasonable max buffer size,
1716 * that is, buffers not too small either size or time wise
1717 * so we do not end up with too many of them */
1718 /* var abuse */
1719 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1720 wav->max_buf_size = upstream_size;
1721 else
1722 wav->max_buf_size = 0;
1723 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1724 if (wav->blockalign > 0)
1725 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1726
1727 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1728
1729 return GST_FLOW_OK;
1730
1731 /* ERROR */
1732 exit:
1733 {
1734 g_free (codec_name);
1735 g_free (header);
1736 if (caps)
1737 gst_caps_unref (caps);
1738 return res;
1739 }
1740 fail:
1741 {
1742 res = GST_FLOW_ERROR;
1743 goto exit;
1744 }
1745 parse_header_error:
1746 {
1747 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1748 ("Couldn't parse audio header"));
1749 goto fail;
1750 }
1751 no_channels:
1752 {
1753 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1754 ("Stream claims to contain no channels - invalid data"));
1755 goto fail;
1756 }
1757 no_rate:
1758 {
1759 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1760 ("Stream with sample_rate == 0 - invalid data"));
1761 goto fail;
1762 }
1763 invalid_blockalign:
1764 {
1765 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1766 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1767 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1768 goto fail;
1769 }
1770 invalid_bps:
1771 {
1772 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1773 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1774 wav->av_bps, wav->blockalign * wav->rate));
1775 goto fail;
1776 }
1777 no_bytes_per_sample:
1778 {
1779 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1780 ("Could not caluclate bytes per sample - invalid data"));
1781 goto fail;
1782 }
1783 unknown_format:
1784 {
1785 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1786 ("No caps found for format 0x%x, %u channels, %u Hz",
1787 wav->format, wav->channels, wav->rate));
1788 goto fail;
1789 }
1790 header_read_error:
1791 {
1792 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1793 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1794 goto fail;
1795 }
1796 }
1797
1798 /*
1799 * Read WAV file tag when streaming
1800 */
1801 static GstFlowReturn
gst_wavparse_parse_stream_init(GstWavParse * wav)1802 gst_wavparse_parse_stream_init (GstWavParse * wav)
1803 {
1804 if (gst_adapter_available (wav->adapter) >= 12) {
1805 GstBuffer *tmp;
1806
1807 /* _take flushes the data */
1808 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1809
1810 GST_DEBUG ("Parsing wav header");
1811 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1812 return GST_FLOW_ERROR;
1813
1814 wav->offset += 12;
1815 /* Go to next state */
1816 wav->state = GST_WAVPARSE_HEADER;
1817 }
1818 return GST_FLOW_OK;
1819 }
1820
1821 /* handle an event sent directly to the element.
1822 *
1823 * This event can be sent either in the READY state or the
1824 * >READY state. The only event of interest really is the seek
1825 * event.
1826 *
1827 * In the READY state we can only store the event and try to
1828 * respect it when going to PAUSED. We assume we are in the
1829 * READY state when our parsing state != GST_WAVPARSE_DATA.
1830 *
1831 * When we are steaming, we can simply perform the seek right
1832 * away.
1833 */
1834 static gboolean
gst_wavparse_send_event(GstElement * element,GstEvent * event)1835 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1836 {
1837 GstWavParse *wav = GST_WAVPARSE (element);
1838 gboolean res = FALSE;
1839
1840 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1841
1842 switch (GST_EVENT_TYPE (event)) {
1843 case GST_EVENT_SEEK:
1844 if (wav->state == GST_WAVPARSE_DATA) {
1845 /* we can handle the seek directly when streaming data */
1846 res = gst_wavparse_perform_seek (wav, event);
1847 } else {
1848 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1849
1850 gst_event_replace (&wav->seek_event, event);
1851
1852 /* we always return true */
1853 res = TRUE;
1854 }
1855 break;
1856 default:
1857 break;
1858 }
1859 gst_event_unref (event);
1860 return res;
1861 }
1862
1863 static gboolean
gst_wavparse_have_dts_caps(const GstCaps * caps,GstTypeFindProbability prob)1864 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1865 {
1866 GstStructure *s;
1867
1868 s = gst_caps_get_structure (caps, 0);
1869 if (!gst_structure_has_name (s, "audio/x-dts"))
1870 return FALSE;
1871 /* typefind behavior for DTS:
1872 * MAXIMUM: multiple frame syncs detected, certainly DTS
1873 * LIKELY: single frame sync at offset 0. Maybe DTS?
1874 * POSSIBLE: single frame sync, not at offset 0. Highly unlikely
1875 * to be DTS. */
1876 if (prob > GST_TYPE_FIND_LIKELY)
1877 return TRUE;
1878 if (prob <= GST_TYPE_FIND_POSSIBLE)
1879 return FALSE;
1880 /* for maybe, check for at least a valid-looking rate and channels */
1881 if (!gst_structure_has_field (s, "channels"))
1882 return FALSE;
1883 /* and for extra assurance we could also check the rate from the DTS frame
1884 * against the one in the wav header, but for now let's not do that */
1885 return gst_structure_has_field (s, "rate");
1886 }
1887
1888 static GstTagList *
gst_wavparse_get_upstream_tags(GstWavParse * wav,GstTagScope scope)1889 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1890 {
1891 GstTagList *tags = NULL;
1892 GstEvent *ev;
1893 gint i;
1894
1895 i = 0;
1896 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1897 gst_event_parse_tag (ev, &tags);
1898 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1899 tags = gst_tag_list_copy (tags);
1900 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1901 gst_event_unref (ev);
1902 break;
1903 }
1904 tags = NULL;
1905 gst_event_unref (ev);
1906 }
1907 return tags;
1908 }
1909
1910 static void
gst_wavparse_add_src_pad(GstWavParse * wav,GstBuffer * buf)1911 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1912 {
1913 GstStructure *s;
1914 GstTagList *tags, *utags;
1915
1916 GST_DEBUG_OBJECT (wav, "adding src pad");
1917
1918 g_assert (wav->caps != NULL);
1919
1920 s = gst_caps_get_structure (wav->caps, 0);
1921 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1922 GstTypeFindProbability prob;
1923 GstCaps *tf_caps;
1924
1925 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1926 if (tf_caps != NULL) {
1927 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1928 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1929 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1930 gst_caps_unref (wav->caps);
1931 wav->caps = tf_caps;
1932
1933 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1934 GST_TAG_AUDIO_CODEC, "dts", NULL);
1935 } else {
1936 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1937 "marked as raw PCM audio, but ignoring for now", tf_caps);
1938 gst_caps_unref (tf_caps);
1939 }
1940 }
1941 }
1942
1943 gst_pad_set_caps (wav->srcpad, wav->caps);
1944
1945 if (wav->start_segment) {
1946 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1947 gst_pad_push_event (wav->srcpad, wav->start_segment);
1948 wav->start_segment = NULL;
1949 }
1950
1951 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1952 * that there'll be only one scope/type of tag list from upstream, if any */
1953 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1954 if (utags == NULL)
1955 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1956
1957 /* if there's a tag upstream it's probably been added to override the
1958 * tags from inside the wav header, so keep upstream tags if in doubt */
1959 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1960
1961 if (wav->tags != NULL) {
1962 gst_tag_list_unref (wav->tags);
1963 wav->tags = NULL;
1964 }
1965
1966 if (utags != NULL)
1967 gst_tag_list_unref (utags);
1968
1969 /* send tags downstream, if any */
1970 if (tags != NULL)
1971 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1972 }
1973
1974 static GstFlowReturn
gst_wavparse_stream_data(GstWavParse * wav,gboolean flushing)1975 gst_wavparse_stream_data (GstWavParse * wav, gboolean flushing)
1976 {
1977 GstBuffer *buf = NULL;
1978 GstFlowReturn res = GST_FLOW_OK;
1979 guint64 desired, obtained;
1980 GstClockTime timestamp, next_timestamp, duration;
1981 guint64 pos, nextpos;
1982
1983 iterate_adapter:
1984 GST_LOG_OBJECT (wav,
1985 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1986 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1987
1988 if ((wav->dataleft == 0 || wav->dataleft < wav->blockalign)) {
1989 /* In case chunk size is not declared in the begining get size from the
1990 * file size directly */
1991 if (wav->chunk_size == 0) {
1992 gint64 upstream_size = 0;
1993
1994 /* Get the size of the file */
1995 if (!gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES,
1996 &upstream_size))
1997 goto found_eos;
1998
1999 if (upstream_size < wav->offset + wav->datastart)
2000 goto found_eos;
2001
2002 /* If file has updated since the beggining continue reading the file */
2003 wav->dataleft = upstream_size - wav->offset - wav->datastart;
2004 wav->end_offset = upstream_size;
2005
2006 /* Get the next n bytes and output them, if we can */
2007 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
2008 goto found_eos;
2009 } else {
2010 goto found_eos;
2011 }
2012 }
2013
2014 /* scale the amount of data by the segment rate so we get equal
2015 * amounts of data regardless of the playback rate */
2016 desired =
2017 MIN (gst_guint64_to_gdouble (wav->dataleft),
2018 wav->max_buf_size * ABS (wav->segment.rate));
2019
2020 if (desired >= wav->blockalign && wav->blockalign > 0)
2021 desired -= (desired % wav->blockalign);
2022
2023 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
2024 "from the sinkpad", desired);
2025
2026 if (wav->streaming) {
2027 guint avail = gst_adapter_available (wav->adapter);
2028 guint extra;
2029
2030 /* flush some bytes if evil upstream sends segment that starts
2031 * before data or does is not send sample aligned segment */
2032 if (G_LIKELY (wav->offset >= wav->datastart)) {
2033 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
2034 } else {
2035 extra = wav->datastart - wav->offset;
2036 }
2037
2038 if (G_UNLIKELY (extra)) {
2039 extra = wav->bytes_per_sample - extra;
2040 if (extra <= avail) {
2041 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
2042 gst_adapter_flush (wav->adapter, extra);
2043 wav->offset += extra;
2044 wav->dataleft -= extra;
2045 goto iterate_adapter;
2046 } else {
2047 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
2048 gst_adapter_clear (wav->adapter);
2049 wav->offset += avail;
2050 wav->dataleft -= avail;
2051 return GST_FLOW_OK;
2052 }
2053 }
2054
2055 if (avail < desired) {
2056 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
2057
2058 /* If we are at the end of the stream, we need to flush whatever we have left */
2059 if (avail > 0 && flushing) {
2060 if (avail >= wav->blockalign && wav->blockalign > 0) {
2061 avail -= (avail % wav->blockalign);
2062 buf = gst_adapter_take_buffer (wav->adapter, avail);
2063 } else {
2064 return GST_FLOW_OK;
2065 }
2066 } else {
2067 return GST_FLOW_OK;
2068 }
2069 } else {
2070 buf = gst_adapter_take_buffer (wav->adapter, desired);
2071 }
2072 } else {
2073 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
2074 desired, &buf)) != GST_FLOW_OK)
2075 goto pull_error;
2076
2077 /* we may get a short buffer at the end of the file */
2078 if (gst_buffer_get_size (buf) < desired) {
2079 gsize size = gst_buffer_get_size (buf);
2080
2081 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2082 if (size >= wav->blockalign) {
2083 if (wav->blockalign > 0) {
2084 buf = gst_buffer_make_writable (buf);
2085 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2086 }
2087 } else {
2088 gst_buffer_unref (buf);
2089 goto found_eos;
2090 }
2091 }
2092 }
2093
2094 obtained = gst_buffer_get_size (buf);
2095
2096 /* our positions in bytes */
2097 pos = wav->offset - wav->datastart;
2098 nextpos = pos + obtained;
2099
2100 /* update offsets, does not overflow. */
2101 buf = gst_buffer_make_writable (buf);
2102 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2103 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2104
2105 /* first chunk of data? create the source pad. We do this only here so
2106 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2107 if (G_UNLIKELY (wav->first)) {
2108 wav->first = FALSE;
2109 /* this will also push the segment events */
2110 gst_wavparse_add_src_pad (wav, buf);
2111 } else {
2112 /* If we have a pending start segment, send it now. */
2113 if (G_UNLIKELY (wav->start_segment != NULL)) {
2114 gst_pad_push_event (wav->srcpad, wav->start_segment);
2115 wav->start_segment = NULL;
2116 }
2117 }
2118
2119 if (wav->bps > 0) {
2120 /* and timestamps if we have a bitrate, be careful for overflows */
2121 timestamp =
2122 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2123 next_timestamp =
2124 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2125 duration = next_timestamp - timestamp;
2126
2127 /* update current running segment position */
2128 if (G_LIKELY (next_timestamp >= wav->segment.start))
2129 wav->segment.position = next_timestamp;
2130 } else if (wav->fact) {
2131 guint64 bps =
2132 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2133 /* and timestamps if we have a bitrate, be careful for overflows */
2134 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2135 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2136 duration = next_timestamp - timestamp;
2137 } else {
2138 /* no bitrate, all we know is that the first sample has timestamp 0, all
2139 * other positions and durations have unknown timestamp. */
2140 if (pos == 0)
2141 timestamp = 0;
2142 else
2143 timestamp = GST_CLOCK_TIME_NONE;
2144 duration = GST_CLOCK_TIME_NONE;
2145 /* update current running segment position with byte offset */
2146 if (G_LIKELY (nextpos >= wav->segment.start))
2147 wav->segment.position = nextpos;
2148 }
2149 if ((pos > 0) && wav->vbr) {
2150 /* don't set timestamps for VBR files if it's not the first buffer */
2151 timestamp = GST_CLOCK_TIME_NONE;
2152 duration = GST_CLOCK_TIME_NONE;
2153 }
2154 if (wav->discont) {
2155 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2156 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2157 wav->discont = FALSE;
2158 }
2159
2160 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2161 GST_BUFFER_DURATION (buf) = duration;
2162
2163 GST_LOG_OBJECT (wav,
2164 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2165 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2166 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2167
2168 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2169 goto push_error;
2170
2171 if (obtained < wav->dataleft) {
2172 wav->offset += obtained;
2173 wav->dataleft -= obtained;
2174 } else {
2175 wav->offset += wav->dataleft;
2176 wav->dataleft = 0;
2177 }
2178
2179 /* Iterate until need more data, so adapter size won't grow */
2180 if (wav->streaming) {
2181 GST_LOG_OBJECT (wav,
2182 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2183 wav->end_offset);
2184 goto iterate_adapter;
2185 }
2186 return res;
2187
2188 /* ERROR */
2189 found_eos:
2190 {
2191 GST_DEBUG_OBJECT (wav, "found EOS");
2192 return GST_FLOW_EOS;
2193 }
2194 pull_error:
2195 {
2196 /* check if we got EOS */
2197 if (res == GST_FLOW_EOS)
2198 goto found_eos;
2199
2200 GST_WARNING_OBJECT (wav,
2201 "Error getting %" G_GINT64_FORMAT " bytes from the "
2202 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2203 return res;
2204 }
2205 push_error:
2206 {
2207 GST_INFO_OBJECT (wav,
2208 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2209 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2210 gst_pad_is_linked (wav->srcpad));
2211 return res;
2212 }
2213 }
2214
2215 static void
gst_wavparse_loop(GstPad * pad)2216 gst_wavparse_loop (GstPad * pad)
2217 {
2218 GstFlowReturn ret;
2219 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2220 GstEvent *event;
2221 gchar *stream_id;
2222
2223 GST_LOG_OBJECT (wav, "process data");
2224
2225 switch (wav->state) {
2226 case GST_WAVPARSE_START:
2227 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2228 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2229 goto pause;
2230
2231 stream_id =
2232 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2233 event = gst_event_new_stream_start (stream_id);
2234 gst_event_set_group_id (event, gst_util_group_id_next ());
2235 gst_pad_push_event (wav->srcpad, event);
2236 g_free (stream_id);
2237
2238 wav->state = GST_WAVPARSE_HEADER;
2239 /* fall-through */
2240
2241 case GST_WAVPARSE_HEADER:
2242 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2243 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2244 goto pause;
2245
2246 wav->state = GST_WAVPARSE_DATA;
2247 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2248 /* fall-through */
2249
2250 case GST_WAVPARSE_DATA:
2251 if ((ret = gst_wavparse_stream_data (wav, FALSE)) != GST_FLOW_OK)
2252 goto pause;
2253 break;
2254 default:
2255 g_assert_not_reached ();
2256 }
2257 return;
2258
2259 /* ERRORS */
2260 pause:
2261 {
2262 const gchar *reason = gst_flow_get_name (ret);
2263
2264 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2265 gst_pad_pause_task (pad);
2266
2267 if (ret == GST_FLOW_EOS) {
2268 /* handle end-of-stream/segment */
2269 /* so align our position with the end of it, if there is one
2270 * this ensures a subsequent will arrive at correct base/acc time */
2271 if (wav->segment.format == GST_FORMAT_TIME) {
2272 if (wav->segment.rate > 0.0 &&
2273 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2274 wav->segment.position = wav->segment.stop;
2275 else if (wav->segment.rate < 0.0)
2276 wav->segment.position = wav->segment.start;
2277 }
2278 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2279 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2280 ("No valid input found before end of stream"));
2281 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2282 } else {
2283 /* add pad before we perform EOS */
2284 if (G_UNLIKELY (wav->first)) {
2285 wav->first = FALSE;
2286 gst_wavparse_add_src_pad (wav, NULL);
2287 }
2288
2289 /* perform EOS logic */
2290 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2291 GstClockTime stop;
2292
2293 if ((stop = wav->segment.stop) == -1)
2294 stop = wav->segment.duration;
2295
2296 gst_element_post_message (GST_ELEMENT_CAST (wav),
2297 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2298 wav->segment.format, stop));
2299 gst_pad_push_event (wav->srcpad,
2300 gst_event_new_segment_done (wav->segment.format, stop));
2301 } else {
2302 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2303 }
2304 }
2305 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2306 /* for fatal errors we post an error message, post the error
2307 * first so the app knows about the error first. */
2308 GST_ELEMENT_FLOW_ERROR (wav, ret);
2309 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2310 }
2311 return;
2312 }
2313 }
2314
2315 static GstFlowReturn
gst_wavparse_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)2316 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2317 {
2318 GstFlowReturn ret;
2319 GstWavParse *wav = GST_WAVPARSE (parent);
2320
2321 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2322 gst_buffer_get_size (buf));
2323
2324 gst_adapter_push (wav->adapter, buf);
2325
2326 switch (wav->state) {
2327 case GST_WAVPARSE_START:
2328 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2329 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2330 goto done;
2331
2332 if (wav->state != GST_WAVPARSE_HEADER)
2333 break;
2334
2335 /* otherwise fall-through */
2336 case GST_WAVPARSE_HEADER:
2337 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2338 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2339 goto done;
2340
2341 if (!wav->got_fmt || wav->datastart == 0)
2342 break;
2343
2344 wav->state = GST_WAVPARSE_DATA;
2345 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2346
2347 /* fall-through */
2348 case GST_WAVPARSE_DATA:
2349 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2350 wav->discont = TRUE;
2351 if ((ret = gst_wavparse_stream_data (wav, FALSE)) != GST_FLOW_OK)
2352 goto done;
2353 break;
2354 default:
2355 g_return_val_if_reached (GST_FLOW_ERROR);
2356 }
2357 done:
2358 if (G_UNLIKELY (wav->abort_buffering)) {
2359 wav->abort_buffering = FALSE;
2360 ret = GST_FLOW_ERROR;
2361 /* sort of demux/parse error */
2362 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2363 }
2364
2365 return ret;
2366 }
2367
2368 static GstFlowReturn
gst_wavparse_flush_data(GstWavParse * wav)2369 gst_wavparse_flush_data (GstWavParse * wav)
2370 {
2371 GstFlowReturn ret = GST_FLOW_OK;
2372 guint av;
2373
2374 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2375 ret = gst_wavparse_stream_data (wav, TRUE);
2376 }
2377
2378 return ret;
2379 }
2380
2381 static gboolean
gst_wavparse_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)2382 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2383 {
2384 GstWavParse *wav = GST_WAVPARSE (parent);
2385 gboolean ret = TRUE;
2386
2387 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2388
2389 switch (GST_EVENT_TYPE (event)) {
2390 case GST_EVENT_CAPS:
2391 {
2392 /* discard, we'll come up with proper src caps */
2393 gst_event_unref (event);
2394 break;
2395 }
2396 case GST_EVENT_SEGMENT:
2397 {
2398 gint64 start, stop, offset = 0, end_offset = -1;
2399 GstSegment segment;
2400
2401 /* some debug output */
2402 gst_event_copy_segment (event, &segment);
2403 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2404 &segment);
2405
2406 if (wav->state != GST_WAVPARSE_DATA) {
2407 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2408 goto exit;
2409 }
2410
2411 /* now we are either committed to TIME or BYTE format,
2412 * and we only expect a BYTE segment, e.g. following a seek */
2413 if (segment.format == GST_FORMAT_BYTES) {
2414 /* handle (un)signed issues */
2415 start = segment.start;
2416 stop = segment.stop;
2417 if (start > 0) {
2418 offset = start;
2419 start -= wav->datastart;
2420 start = MAX (start, 0);
2421 }
2422 if (stop > 0) {
2423 end_offset = stop;
2424 stop -= wav->datastart;
2425 stop = MAX (stop, 0);
2426 }
2427 if (wav->segment.format == GST_FORMAT_TIME) {
2428 guint64 bps = wav->bps;
2429
2430 /* operating in format TIME, so we can convert */
2431 if (!bps && wav->fact)
2432 bps =
2433 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2434 if (bps) {
2435 if (start >= 0)
2436 start =
2437 gst_util_uint64_scale_ceil (start, GST_SECOND,
2438 (guint64) wav->bps);
2439 if (stop >= 0)
2440 stop =
2441 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2442 (guint64) wav->bps);
2443 }
2444 }
2445 } else {
2446 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2447 goto exit;
2448 }
2449
2450 segment.start = start;
2451 segment.stop = stop;
2452
2453 /* accept upstream's notion of segment and distribute along */
2454 segment.format = wav->segment.format;
2455 segment.time = segment.position = segment.start;
2456 segment.duration = wav->segment.duration;
2457 segment.base = gst_segment_to_running_time (&wav->segment,
2458 GST_FORMAT_TIME, wav->segment.position);
2459
2460 gst_segment_copy_into (&segment, &wav->segment);
2461
2462 /* also store the newsegment event for the streaming thread */
2463 if (wav->start_segment)
2464 gst_event_unref (wav->start_segment);
2465 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2466 wav->start_segment = gst_event_new_segment (&segment);
2467
2468 /* stream leftover data in current segment */
2469 gst_wavparse_flush_data (wav);
2470 /* and set up streaming thread for next one */
2471 wav->offset = offset;
2472 wav->end_offset = end_offset;
2473
2474 if (wav->datasize > 0 && (wav->end_offset == -1
2475 || wav->end_offset > wav->datastart + wav->datasize))
2476 wav->end_offset = wav->datastart + wav->datasize;
2477
2478 if (wav->end_offset != -1) {
2479 wav->dataleft = wav->end_offset - wav->offset;
2480 } else {
2481 /* infinity; upstream will EOS when done */
2482 wav->dataleft = G_MAXUINT64;
2483 }
2484 exit:
2485 gst_event_unref (event);
2486 break;
2487 }
2488 case GST_EVENT_EOS:
2489 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2490 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2491 ("No valid input found before end of stream"));
2492 } else {
2493 /* add pad if needed so EOS is seen downstream */
2494 if (G_UNLIKELY (wav->first)) {
2495 wav->first = FALSE;
2496 gst_wavparse_add_src_pad (wav, NULL);
2497 }
2498
2499 /* stream leftover data in current segment */
2500 gst_wavparse_flush_data (wav);
2501 }
2502
2503 /* fall-through */
2504 case GST_EVENT_FLUSH_STOP:
2505 {
2506 GstClockTime dur;
2507
2508 if (wav->adapter)
2509 gst_adapter_clear (wav->adapter);
2510 wav->discont = TRUE;
2511 dur = wav->segment.duration;
2512 gst_segment_init (&wav->segment, wav->segment.format);
2513 wav->segment.duration = dur;
2514 /* fall-through */
2515 }
2516 default:
2517 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2518 break;
2519 }
2520
2521 return ret;
2522 }
2523
2524 #if 0
2525 /* convert and query stuff */
2526 static const GstFormat *
2527 gst_wavparse_get_formats (GstPad * pad)
2528 {
2529 static const GstFormat formats[] = {
2530 GST_FORMAT_TIME,
2531 GST_FORMAT_BYTES,
2532 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2533 0
2534 };
2535
2536 return formats;
2537 }
2538 #endif
2539
2540 static gboolean
gst_wavparse_pad_convert(GstPad * pad,GstFormat src_format,gint64 src_value,GstFormat * dest_format,gint64 * dest_value)2541 gst_wavparse_pad_convert (GstPad * pad,
2542 GstFormat src_format, gint64 src_value,
2543 GstFormat * dest_format, gint64 * dest_value)
2544 {
2545 GstWavParse *wavparse;
2546 gboolean res = TRUE;
2547
2548 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2549
2550 if (*dest_format == src_format) {
2551 *dest_value = src_value;
2552 return TRUE;
2553 }
2554
2555 if ((wavparse->bps == 0) && !wavparse->fact)
2556 goto no_bps_fact;
2557
2558 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2559 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2560
2561 switch (src_format) {
2562 case GST_FORMAT_BYTES:
2563 switch (*dest_format) {
2564 case GST_FORMAT_DEFAULT:
2565 *dest_value = src_value / wavparse->bytes_per_sample;
2566 /* make sure we end up on a sample boundary */
2567 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2568 break;
2569 case GST_FORMAT_TIME:
2570 /* src_value + datastart = offset */
2571 GST_INFO_OBJECT (wavparse,
2572 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2573 wavparse->offset);
2574 if (wavparse->bps > 0)
2575 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2576 (guint64) wavparse->bps);
2577 else if (wavparse->fact) {
2578 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2579 wavparse->rate, wavparse->fact);
2580
2581 *dest_value =
2582 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2583 } else {
2584 res = FALSE;
2585 }
2586 break;
2587 default:
2588 res = FALSE;
2589 goto done;
2590 }
2591 break;
2592
2593 case GST_FORMAT_DEFAULT:
2594 switch (*dest_format) {
2595 case GST_FORMAT_BYTES:
2596 *dest_value = src_value * wavparse->bytes_per_sample;
2597 break;
2598 case GST_FORMAT_TIME:
2599 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2600 (guint64) wavparse->rate);
2601 break;
2602 default:
2603 res = FALSE;
2604 goto done;
2605 }
2606 break;
2607
2608 case GST_FORMAT_TIME:
2609 switch (*dest_format) {
2610 case GST_FORMAT_BYTES:
2611 if (wavparse->bps > 0)
2612 *dest_value = gst_util_uint64_scale (src_value,
2613 (guint64) wavparse->bps, GST_SECOND);
2614 else {
2615 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2616 wavparse->rate, wavparse->fact);
2617
2618 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2619 }
2620 /* make sure we end up on a sample boundary */
2621 *dest_value -= *dest_value % wavparse->blockalign;
2622 break;
2623 case GST_FORMAT_DEFAULT:
2624 *dest_value = gst_util_uint64_scale (src_value,
2625 (guint64) wavparse->rate, GST_SECOND);
2626 break;
2627 default:
2628 res = FALSE;
2629 goto done;
2630 }
2631 break;
2632
2633 default:
2634 res = FALSE;
2635 goto done;
2636 }
2637
2638 done:
2639 return res;
2640
2641 /* ERRORS */
2642 no_bps_fact:
2643 {
2644 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2645 res = FALSE;
2646 goto done;
2647 }
2648 }
2649
2650 /* handle queries for location and length in requested format */
2651 static gboolean
gst_wavparse_pad_query(GstPad * pad,GstObject * parent,GstQuery * query)2652 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2653 {
2654 gboolean res = TRUE;
2655 GstWavParse *wav = GST_WAVPARSE (parent);
2656
2657 /* only if we know */
2658 if (wav->state != GST_WAVPARSE_DATA) {
2659 return FALSE;
2660 }
2661
2662 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2663
2664 switch (GST_QUERY_TYPE (query)) {
2665 case GST_QUERY_POSITION:
2666 {
2667 gint64 curb;
2668 gint64 cur;
2669 GstFormat format;
2670
2671 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2672 curb = wav->offset - wav->datastart;
2673 gst_query_parse_position (query, &format, NULL);
2674 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2675
2676 switch (format) {
2677 case GST_FORMAT_BYTES:
2678 format = GST_FORMAT_BYTES;
2679 cur = curb;
2680 break;
2681 default:
2682 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2683 &format, &cur);
2684 break;
2685 }
2686 if (res)
2687 gst_query_set_position (query, format, cur);
2688 break;
2689 }
2690 case GST_QUERY_DURATION:
2691 {
2692 gint64 duration = 0;
2693 GstFormat format;
2694
2695 if (wav->ignore_length) {
2696 res = FALSE;
2697 break;
2698 }
2699
2700 gst_query_parse_duration (query, &format, NULL);
2701
2702 switch (format) {
2703 case GST_FORMAT_BYTES:{
2704 format = GST_FORMAT_BYTES;
2705 duration = wav->datasize;
2706 break;
2707 }
2708 case GST_FORMAT_TIME:
2709 if ((res = gst_wavparse_calculate_duration (wav))) {
2710 duration = wav->duration;
2711 }
2712 break;
2713 default:
2714 res = FALSE;
2715 break;
2716 }
2717 if (res)
2718 gst_query_set_duration (query, format, duration);
2719 break;
2720 }
2721 case GST_QUERY_CONVERT:
2722 {
2723 gint64 srcvalue, dstvalue;
2724 GstFormat srcformat, dstformat;
2725
2726 gst_query_parse_convert (query, &srcformat, &srcvalue,
2727 &dstformat, &dstvalue);
2728 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2729 &dstformat, &dstvalue);
2730 if (res)
2731 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2732 break;
2733 }
2734 case GST_QUERY_SEEKING:{
2735 GstFormat fmt;
2736 gboolean seekable = FALSE;
2737
2738 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2739 if (fmt == wav->segment.format) {
2740 if (wav->streaming) {
2741 GstQuery *q;
2742
2743 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2744 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2745 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2746 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2747 }
2748 gst_query_unref (q);
2749 } else {
2750 GST_LOG_OBJECT (wav, "looping => seekable");
2751 seekable = TRUE;
2752 res = TRUE;
2753 }
2754 } else if (fmt == GST_FORMAT_TIME) {
2755 res = TRUE;
2756 }
2757 if (res) {
2758 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2759 }
2760 break;
2761 }
2762 case GST_QUERY_SEGMENT:
2763 {
2764 GstFormat format;
2765 gint64 start, stop;
2766
2767 format = wav->segment.format;
2768
2769 start =
2770 gst_segment_to_stream_time (&wav->segment, format,
2771 wav->segment.start);
2772 if ((stop = wav->segment.stop) == -1)
2773 stop = wav->segment.duration;
2774 else
2775 stop = gst_segment_to_stream_time (&wav->segment, format, stop);
2776
2777 gst_query_set_segment (query, wav->segment.rate, format, start, stop);
2778 res = TRUE;
2779 break;
2780 }
2781 default:
2782 res = gst_pad_query_default (pad, parent, query);
2783 break;
2784 }
2785 return res;
2786 }
2787
2788 static gboolean
gst_wavparse_srcpad_event(GstPad * pad,GstObject * parent,GstEvent * event)2789 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2790 {
2791 GstWavParse *wavparse = GST_WAVPARSE (parent);
2792 gboolean res = FALSE;
2793
2794 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2795
2796 switch (GST_EVENT_TYPE (event)) {
2797 case GST_EVENT_SEEK:
2798 /* can only handle events when we are in the data state */
2799 if (wavparse->state == GST_WAVPARSE_DATA) {
2800 res = gst_wavparse_perform_seek (wavparse, event);
2801 }
2802 gst_event_unref (event);
2803 break;
2804
2805 case GST_EVENT_TOC_SELECT:
2806 {
2807 char *uid = NULL;
2808 GstTocEntry *entry = NULL;
2809 GstEvent *seek_event;
2810 gint64 start_pos;
2811
2812 if (!wavparse->toc) {
2813 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2814 return FALSE;
2815 } else {
2816 gst_event_parse_toc_select (event, &uid);
2817 if (uid != NULL) {
2818 GST_OBJECT_LOCK (wavparse);
2819 entry = gst_toc_find_entry (wavparse->toc, uid);
2820 if (entry == NULL) {
2821 GST_OBJECT_UNLOCK (wavparse);
2822 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2823 uid);
2824 res = FALSE;
2825 } else {
2826 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2827 GST_OBJECT_UNLOCK (wavparse);
2828 seek_event = gst_event_new_seek (1.0,
2829 GST_FORMAT_TIME,
2830 GST_SEEK_FLAG_FLUSH,
2831 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2832 res = gst_wavparse_perform_seek (wavparse, seek_event);
2833 gst_event_unref (seek_event);
2834 }
2835 g_free (uid);
2836 } else {
2837 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2838 res = FALSE;
2839 }
2840 }
2841 gst_event_unref (event);
2842 break;
2843 }
2844
2845 default:
2846 res = gst_pad_push_event (wavparse->sinkpad, event);
2847 break;
2848 }
2849 return res;
2850 }
2851
2852 static gboolean
gst_wavparse_sink_activate(GstPad * sinkpad,GstObject * parent)2853 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2854 {
2855 GstWavParse *wav = GST_WAVPARSE (parent);
2856 GstQuery *query;
2857 gboolean pull_mode;
2858
2859 if (wav->adapter) {
2860 gst_adapter_clear (wav->adapter);
2861 g_object_unref (wav->adapter);
2862 wav->adapter = NULL;
2863 }
2864
2865 query = gst_query_new_scheduling ();
2866
2867 if (!gst_pad_peer_query (sinkpad, query)) {
2868 gst_query_unref (query);
2869 goto activate_push;
2870 }
2871
2872 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2873 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2874 gst_query_unref (query);
2875
2876 if (!pull_mode)
2877 goto activate_push;
2878
2879 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2880 wav->streaming = FALSE;
2881 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2882
2883 activate_push:
2884 {
2885 GST_DEBUG_OBJECT (sinkpad, "activating push");
2886 wav->streaming = TRUE;
2887 wav->adapter = gst_adapter_new ();
2888 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2889 }
2890 }
2891
2892
2893 static gboolean
gst_wavparse_sink_activate_mode(GstPad * sinkpad,GstObject * parent,GstPadMode mode,gboolean active)2894 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2895 GstPadMode mode, gboolean active)
2896 {
2897 gboolean res;
2898
2899 switch (mode) {
2900 case GST_PAD_MODE_PUSH:
2901 res = TRUE;
2902 break;
2903 case GST_PAD_MODE_PULL:
2904 if (active) {
2905 /* if we have a scheduler we can start the task */
2906 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2907 sinkpad, NULL);
2908 } else {
2909 res = gst_pad_stop_task (sinkpad);
2910 }
2911 break;
2912 default:
2913 res = FALSE;
2914 break;
2915 }
2916 return res;
2917 }
2918
2919 static GstStateChangeReturn
gst_wavparse_change_state(GstElement * element,GstStateChange transition)2920 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2921 {
2922 GstStateChangeReturn ret;
2923 GstWavParse *wav = GST_WAVPARSE (element);
2924
2925 switch (transition) {
2926 case GST_STATE_CHANGE_NULL_TO_READY:
2927 break;
2928 case GST_STATE_CHANGE_READY_TO_PAUSED:
2929 gst_wavparse_reset (wav);
2930 break;
2931 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2932 break;
2933 default:
2934 break;
2935 }
2936
2937 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2938
2939 switch (transition) {
2940 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2941 break;
2942 case GST_STATE_CHANGE_PAUSED_TO_READY:
2943 gst_wavparse_reset (wav);
2944 break;
2945 case GST_STATE_CHANGE_READY_TO_NULL:
2946 break;
2947 default:
2948 break;
2949 }
2950 return ret;
2951 }
2952
2953 static void
gst_wavparse_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)2954 gst_wavparse_set_property (GObject * object, guint prop_id,
2955 const GValue * value, GParamSpec * pspec)
2956 {
2957 GstWavParse *self;
2958
2959 g_return_if_fail (GST_IS_WAVPARSE (object));
2960 self = GST_WAVPARSE (object);
2961
2962 switch (prop_id) {
2963 case PROP_IGNORE_LENGTH:
2964 self->ignore_length = g_value_get_boolean (value);
2965 break;
2966 default:
2967 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2968 }
2969
2970 }
2971
2972 static void
gst_wavparse_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)2973 gst_wavparse_get_property (GObject * object, guint prop_id,
2974 GValue * value, GParamSpec * pspec)
2975 {
2976 GstWavParse *self;
2977
2978 g_return_if_fail (GST_IS_WAVPARSE (object));
2979 self = GST_WAVPARSE (object);
2980
2981 switch (prop_id) {
2982 case PROP_IGNORE_LENGTH:
2983 g_value_set_boolean (value, self->ignore_length);
2984 break;
2985 default:
2986 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2987 }
2988 }
2989
2990 static gboolean
plugin_init(GstPlugin * plugin)2991 plugin_init (GstPlugin * plugin)
2992 {
2993 gst_riff_init ();
2994
2995 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2996 GST_TYPE_WAVPARSE);
2997 }
2998
2999 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
3000 GST_VERSION_MINOR,
3001 wavparse,
3002 "Parse a .wav file into raw audio",
3003 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
3004