3 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
4 * Copyright (C) 2009 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
5 * Copyright (C) 2009 Nokia Corporation. All rights reserved.
6 * Contact: Stefan Kost <stefan.kost@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:element-flacparse
26 * @see_also: flacdec, oggdemux, vorbisparse
28 * The flacparse element will parse the header packets of the FLAC
29 * stream and put them as the streamheader in the caps. This is used in the
30 * multifdsink case where you want to stream live FLAC streams to multiple
31 * clients, each client has to receive the streamheaders first before they can
32 * consume the FLAC packets.
34 * This element also makes sure that the buffers that it pushes out are properly
35 * timestamped and that their offset and offset_end are set. The buffers that
36 * flacparse outputs have all of the metadata that oggmux expects to receive,
37 * which allows you to (for example) remux an ogg/flac or convert a native FLAC
38 * format file to an ogg bitstream.
41 * <title>Example pipelines</title>
43 * gst-launch -v filesrc location=sine.flac ! flacparse ! identity \
44 * ! oggmux ! filesink location=sine-remuxed.ogg
45 * ]| This pipeline converts a native FLAC format file to an ogg bitstream.
46 * It also illustrates that the streamheader is set in the caps, and that each
47 * buffer has the timestamp, duration, offset, and offset_end set.
56 #include "gstflacparse.h"
59 #include <gst/tag/tag.h>
60 #include <gst/audio/audio.h>
62 #include <gst/base/gstbitreader.h>
63 #include <gst/base/gstbytereader.h>
65 GST_DEBUG_CATEGORY_STATIC (flacparse_debug);
66 #define GST_CAT_DEFAULT flacparse_debug
68 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
71 GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) true, "
72 "channels = (int) [ 1, 8 ], " "rate = (int) [ 1, 655350 ]")
75 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
78 GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) false")
81 static void gst_flac_parse_finalize (GObject * object);
83 static gboolean gst_flac_parse_start (GstBaseParse * parse);
84 static gboolean gst_flac_parse_stop (GstBaseParse * parse);
85 static gboolean gst_flac_parse_check_valid_frame (GstBaseParse * parse,
86 GstBuffer * buffer, guint * framesize, gint * skipsize);
87 static GstFlowReturn gst_flac_parse_parse_frame (GstBaseParse * parse,
90 GST_BOILERPLATE (GstFlacParse, gst_flac_parse, GstBaseParse,
94 gst_flac_parse_base_init (gpointer g_class)
96 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
98 gst_element_class_add_pad_template (element_class,
99 gst_static_pad_template_get (&src_factory));
100 gst_element_class_add_pad_template (element_class,
101 gst_static_pad_template_get (&sink_factory));
103 gst_element_class_set_details_simple (element_class, "FLAC audio parser",
104 "Codec/Parser/Audio",
105 "Parses audio with the FLAC lossless audio codec",
106 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
108 GST_DEBUG_CATEGORY_INIT (flacparse_debug, "flacparse", 0,
109 "Flac parser element");
113 gst_flac_parse_class_init (GstFlacParseClass * klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass);
118 gobject_class->finalize = gst_flac_parse_finalize;
120 baseparse_class->start = GST_DEBUG_FUNCPTR (gst_flac_parse_start);
121 baseparse_class->stop = GST_DEBUG_FUNCPTR (gst_flac_parse_stop);
122 baseparse_class->check_valid_frame =
123 GST_DEBUG_FUNCPTR (gst_flac_parse_check_valid_frame);
124 baseparse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_flac_parse_parse_frame);
128 gst_flac_parse_init (GstFlacParse * flacparse, GstFlacParseClass * klass)
133 gst_flac_parse_finalize (GObject * object)
135 GstFlacParse *flacparse = GST_FLAC_PARSE (object);
137 if (flacparse->tags) {
138 gst_tag_list_free (flacparse->tags);
139 flacparse->tags = NULL;
143 g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL);
144 g_list_free (flacparse->headers);
145 flacparse->headers = NULL;
147 G_OBJECT_CLASS (parent_class)->finalize (object);
151 gst_flac_parse_start (GstBaseParse * parse)
153 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
155 flacparse->state = GST_FLAC_PARSE_STATE_INIT;
156 flacparse->min_blocksize = 0;
157 flacparse->max_blocksize = 0;
158 flacparse->min_framesize = 0;
159 flacparse->max_framesize = 0;
161 flacparse->upstream_length = -1;
163 flacparse->samplerate = 0;
164 flacparse->channels = 0;
166 flacparse->total_samples = 0;
168 flacparse->requested_frame_size = 0;
169 flacparse->offset = GST_CLOCK_TIME_NONE;
170 flacparse->blocking_strategy = 0;
171 flacparse->block_size = 0;
172 flacparse->sample_number = 0;
175 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4);
181 gst_flac_parse_stop (GstBaseParse * parse)
183 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
185 if (flacparse->tags) {
186 gst_tag_list_free (flacparse->tags);
187 flacparse->tags = NULL;
190 g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL);
191 g_list_free (flacparse->headers);
192 flacparse->headers = NULL;
198 gst_flac_parse_get_frame_size (GstFlacParse * flacparse, GstBuffer * buffer,
199 guint * framesize_ret)
201 GstBitReader reader = GST_BIT_READER_INIT_FROM_BUFFER (buffer);
205 guint8 channel_assignment = 0;
207 /* Skip 14 bit sync code */
208 if (!gst_bit_reader_skip (&reader, 14))
212 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
217 /* 0 == fixed block size, 1 == variable block size */
218 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->blocking_strategy,
222 /* block size index, calculation of the real blocksize below */
223 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 4))
225 else if (flacparse->block_size == 0)
228 /* sample rate index, calculation of the real samplerate below */
229 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 4))
231 else if (samplerate == 0x0f)
234 /* channel assignment */
235 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 4)) {
237 } else if (tmp < 8) {
238 if (flacparse->channels && tmp + 1 != flacparse->channels)
241 flacparse->channels = tmp + 1;
242 } else if (tmp <= 10) {
243 if (flacparse->channels && 2 != flacparse->channels)
246 flacparse->channels = 2;
248 channel_assignment = 1; /* left-side */
250 channel_assignment = 2; /* right-side */
252 channel_assignment = 3; /* mid-side */
253 } else if (tmp > 10) {
257 /* bits per sample */
258 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 3)) {
260 } else if (tmp == 0x03 || tmp == 0x07) {
262 } else if (tmp == 0 && flacparse->bps == 0) {
263 goto need_streaminfo;
264 } else if (tmp == 0x01 && flacparse->bps != 8) {
265 if (flacparse->bps && flacparse->bps != 8)
269 } else if (tmp == 0x02 && flacparse->bps != 12) {
270 if (flacparse->bps && flacparse->bps != 12)
274 } else if (tmp == 0x04 && flacparse->bps != 16) {
275 if (flacparse->bps && flacparse->bps != 16)
279 } else if (tmp == 0x05 && flacparse->bps != 20) {
280 if (flacparse->bps && flacparse->bps != 20)
284 } else if (tmp == 0x06 && flacparse->bps != 24) {
285 if (flacparse->bps && flacparse->bps != 24)
291 /* reserved, must be 0 */
292 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
297 /* read "utf8" encoded sample/frame number */
303 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
311 flacparse->sample_number = 0;
313 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 7))
315 flacparse->sample_number = tmp;
316 } else if ((flacparse->blocking_strategy == 0 && len > 6) ||
317 (flacparse->blocking_strategy == 1 && len > 7)) {
320 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 8 - len - 1))
323 flacparse->sample_number = tmp;
327 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 2))
329 else if (tmp != 0x02)
332 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 6))
334 flacparse->sample_number <<= 6;
335 flacparse->sample_number |= tmp;
341 /* calculate real blocksize from the blocksize index */
342 if (flacparse->block_size == 1)
343 flacparse->block_size = 192;
344 else if (flacparse->block_size <= 5)
345 flacparse->block_size = 576 * (1 << (flacparse->block_size - 2));
346 else if (flacparse->block_size <= 15)
347 flacparse->block_size = 256 * (1 << (flacparse->block_size - 8));
348 else if (flacparse->block_size == 6) {
349 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 8))
351 flacparse->block_size++;
352 } else if (flacparse->block_size == 7) {
353 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 16))
355 flacparse->block_size++;
358 /* calculate the real samplerate from the samplerate index */
359 if (samplerate == 0 && flacparse->samplerate == 0) {
360 goto need_streaminfo;
361 } else if (samplerate == 1) {
362 if (flacparse->samplerate == 0)
363 flacparse->samplerate = 88200;
364 else if (flacparse->samplerate != 88200)
366 } else if (samplerate == 2) {
367 if (flacparse->samplerate == 0)
368 flacparse->samplerate = 176400;
369 else if (flacparse->samplerate != 176400)
371 } else if (samplerate == 3) {
372 if (flacparse->samplerate == 0)
373 flacparse->samplerate = 192000;
374 else if (flacparse->samplerate != 192000)
376 } else if (samplerate == 4) {
377 if (flacparse->samplerate == 0)
378 flacparse->samplerate = 8000;
379 else if (flacparse->samplerate != 8000)
381 } else if (samplerate == 5) {
382 if (flacparse->samplerate == 0)
383 flacparse->samplerate = 16000;
384 else if (flacparse->samplerate != 16000)
386 } else if (samplerate == 6) {
387 if (flacparse->samplerate == 0)
388 flacparse->samplerate = 22050;
389 else if (flacparse->samplerate != 22050)
391 } else if (samplerate == 7) {
392 if (flacparse->samplerate == 0)
393 flacparse->samplerate = 24000;
394 else if (flacparse->samplerate != 24000)
396 } else if (samplerate == 8) {
397 if (flacparse->samplerate == 0)
398 flacparse->samplerate = 32000;
399 else if (flacparse->samplerate != 32000)
401 } else if (samplerate == 9) {
402 if (flacparse->samplerate == 0)
403 flacparse->samplerate = 44100;
404 else if (flacparse->samplerate != 44100)
406 } else if (samplerate == 10) {
407 if (flacparse->samplerate == 0)
408 flacparse->samplerate = 48000;
409 else if (flacparse->samplerate != 48000)
411 } else if (samplerate == 11) {
412 if (flacparse->samplerate == 0)
413 flacparse->samplerate = 96000;
414 else if (flacparse->samplerate != 96000)
416 } else if (samplerate == 12) {
417 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 8))
420 if (flacparse->samplerate == 0)
421 flacparse->samplerate = samplerate;
422 else if (flacparse->samplerate != samplerate)
424 } else if (samplerate == 13) {
425 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 16))
427 if (flacparse->samplerate == 0)
428 flacparse->samplerate = samplerate;
429 else if (flacparse->samplerate != samplerate)
431 } else if (samplerate == 14) {
432 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 16))
435 if (flacparse->samplerate == 0)
436 flacparse->samplerate = samplerate;
437 else if (flacparse->samplerate != samplerate)
441 /* skip crc-8 for the header */
442 if (!gst_bit_reader_skip (&reader, 8))
445 /* parse subframes, one subframe per channel */
446 for (i = 0; i < flacparse->channels; i++) {
450 cur_bps = flacparse->bps;
452 /* for mid/side, left/side, right/side the "difference" channel
453 * needs and additional bit */
454 if (i == 0 && channel_assignment == 2)
456 else if (i == 1 && (channel_assignment == 1 || channel_assignment == 3))
460 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
466 if (!gst_bit_reader_get_bits_uint8 (&reader, &sf_type, 6))
468 else if (((sf_type & 0xfe) == 0x02) ||
469 ((sf_type & 0xfc) == 0x04) ||
470 ((sf_type & 0xf8) == 0x08 && (sf_type & 0x07) > 4) ||
471 ((sf_type & 0xf0) == 0x10))
474 /* wasted bits per sample, if 1 the value follows unary coded */
475 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1)) {
477 } else if (tmp != 0) {
482 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
490 /* subframe type: constant */
491 if (sf_type == 0x00) {
492 if (!gst_bit_reader_skip (&reader, cur_bps))
494 /* subframe type: verbatim */
495 } else if (sf_type == 0x01) {
496 if (!gst_bit_reader_skip (&reader, cur_bps * flacparse->block_size))
498 /* subframe type: LPC or fixed */
500 guint8 residual_type;
502 guint16 partition_order;
505 /* Skip warm-up samples for fixed subframe and calculate order */
506 if ((sf_type & 0xf8) == 0x08) {
507 order = sf_type & 0x07;
509 g_assert (order <= 4);
511 if (!gst_bit_reader_skip (&reader, cur_bps * order))
513 /* Skip warm-up samples for LPC subframe, get parameters and calculate order */
514 } else if ((sf_type & 0xe0) == 0x20) {
517 order = (sf_type & 0x1f) + 1;
519 /* warm-up samples */
520 if (!gst_bit_reader_skip (&reader, cur_bps * order))
523 /* LPC coefficient precision */
524 if (!gst_bit_reader_get_bits_uint8 (&reader, &prec, 4))
526 else if (prec == 0x0f)
530 /* LPC coefficient shift */
531 if (!gst_bit_reader_skip (&reader, 5))
534 /* LPC coefficients */
535 if (!gst_bit_reader_skip (&reader, order * prec))
538 g_assert_not_reached ();
541 /* residual type: 0 == rice, 1 == rice2 */
542 if (!gst_bit_reader_get_bits_uint8 (&reader, &residual_type, 2))
545 if (residual_type & 0x02)
548 /* partition order */
549 if (!gst_bit_reader_get_bits_uint16 (&reader, &partition_order, 4))
552 partition_order = 1 << partition_order;
554 /* 2^partition_order partitions */
555 for (j = 0; j < partition_order; j++) {
557 guint8 rice_parameter;
559 /* calculate number of samples for the current partition */
560 if (partition_order == 1) {
561 samples = flacparse->block_size - order;
563 samples = flacparse->block_size / partition_order;
565 samples = flacparse->block_size / partition_order - order;
569 if (!gst_bit_reader_get_bits_uint8 (&reader, &rice_parameter,
570 (residual_type == 0) ? 4 : 5))
573 /* if rice parameter has all bits set the samples follow unencoded with the number of bits
574 * per sample in the following 5 bits */
575 if ((residual_type == 0 && rice_parameter == 0x0f)
576 || (residual_type == 1 && rice_parameter == 0x1f)) {
577 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 5))
579 if (!gst_bit_reader_skip (&reader, tmp * samples))
584 /* read the rice encoded samples */
585 for (k = 0; k < samples; k++) {
588 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
591 if (!gst_bit_reader_skip (&reader, rice_parameter))
599 /* zero padding to byte alignment */
600 gst_bit_reader_skip_to_byte (&reader);
602 /* Skip crc-16 for the complete frame */
603 if (!gst_bit_reader_skip (&reader, 16))
606 *framesize_ret = gst_bit_reader_get_pos (&reader) / 8;
608 GST_DEBUG_OBJECT (flacparse, "Parsed frame at offset %" G_GUINT64_FORMAT ":\n"
611 "Sample/Frame number: %" G_GUINT64_FORMAT,
612 flacparse->offset, *framesize_ret,
613 flacparse->block_size, flacparse->sample_number);
621 /* not enough, if that was all available, give up on frame */
622 if (G_UNLIKELY (gst_base_parse_get_drain (GST_BASE_PARSE_CAST (flacparse))))
624 /* otherwise, ask for some more */
625 max = flacparse->max_framesize;
628 flacparse->requested_frame_size
629 = MIN (GST_BUFFER_SIZE (buffer) + 4096, max);
630 if (flacparse->requested_frame_size > GST_BUFFER_SIZE (buffer)) {
631 GST_DEBUG_OBJECT (flacparse, "Requesting %u bytes",
632 flacparse->requested_frame_size);
633 return flacparse->requested_frame_size;
635 GST_DEBUG_OBJECT (flacparse, "Giving up on invalid frame (%d bytes)",
636 GST_BUFFER_SIZE (buffer));
643 GST_ERROR_OBJECT (flacparse, "Need STREAMINFO");
649 GST_WARNING_OBJECT (flacparse, "EOS");
655 GST_WARNING_OBJECT (flacparse, "Invalid frame");
661 gst_flac_parse_check_valid_frame (GstBaseParse * parse, GstBuffer * buffer,
662 guint * framesize, gint * skipsize)
664 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
665 const guint8 *data = GST_BUFFER_DATA (buffer);
667 if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < 4))
670 if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) {
671 if (memcmp (GST_BUFFER_DATA (buffer), "fLaC", 4) == 0) {
672 GST_DEBUG_OBJECT (flacparse, "fLaC marker found");
675 } else if (data[0] == 0xff && (data[1] >> 2) == 0x3e) {
676 GST_DEBUG_OBJECT (flacparse, "Found headerless FLAC");
677 /* Minimal size of a frame header */
678 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 16);
679 flacparse->requested_frame_size = 16;
680 flacparse->state = GST_FLAC_PARSE_STATE_GENERATE_HEADERS;
684 GST_DEBUG_OBJECT (flacparse, "fLaC marker not found");
687 } else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) {
688 guint size = 4 + ((data[1] << 16) | (data[2] << 8) | (data[3]));
690 GST_DEBUG_OBJECT (flacparse, "Found metadata block of size %u", size);
694 if (data[0] == 0xff && (data[1] >> 2) == 0x3e) {
697 flacparse->offset = GST_BUFFER_OFFSET (buffer);
698 flacparse->blocking_strategy = 0;
699 flacparse->block_size = 0;
700 flacparse->sample_number = 0;
702 GST_DEBUG_OBJECT (flacparse, "Found sync code");
703 ret = gst_flac_parse_get_frame_size (flacparse, buffer, framesize);
706 /* if not in sync, also check for next frame header */
707 if (!gst_base_parse_get_sync (parse) &&
708 !gst_base_parse_get_drain (parse)) {
709 GST_DEBUG_OBJECT (flacparse, "Resyncing; checking next sync code");
710 if (GST_BUFFER_SIZE (buffer) >= ret + 2) {
711 if (data[ret] == 0xff && (data[ret + 1] >> 2) == 0x3e) {
712 GST_DEBUG_OBJECT (flacparse, "Found next sync code");
715 GST_DEBUG_OBJECT (flacparse,
716 "No next sync code, rejecting frame");
720 /* request more data for next sync */
721 GST_DEBUG_OBJECT (flacparse, "... but not enough data");
723 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), ret);
724 flacparse->requested_frame_size = ret;
729 } else if (ret == -1) {
731 } else if (ret == -2) {
732 GST_ELEMENT_ERROR (flacparse, STREAM, FORMAT, (NULL),
733 ("Need STREAMINFO for parsing"));
735 } else if (ret > 0) {
737 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), ret);
738 flacparse->requested_frame_size = ret;
742 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
745 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xfffc0000, 0xfff80000,
746 0, GST_BUFFER_SIZE (buffer));
749 GST_DEBUG_OBJECT (parse, "Possible sync at buffer offset %d", off);
753 GST_DEBUG_OBJECT (flacparse, "Sync code not found");
754 *skipsize = GST_BUFFER_SIZE (buffer) - 3;
764 gst_flac_parse_handle_streaminfo (GstFlacParse * flacparse, GstBuffer * buffer)
766 GstBitReader reader = GST_BIT_READER_INIT_FROM_BUFFER (buffer);
768 if (GST_BUFFER_SIZE (buffer) != 4 + 34) {
769 GST_ERROR_OBJECT (flacparse, "Invalid metablock size for STREAMINFO: %u",
770 GST_BUFFER_SIZE (buffer));
774 /* Skip metadata block header */
775 gst_bit_reader_skip (&reader, 32);
777 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->min_blocksize, 16))
779 if (flacparse->min_blocksize < 16) {
780 GST_ERROR_OBJECT (flacparse, "Invalid minimum block size: %u",
781 flacparse->min_blocksize);
785 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->max_blocksize, 16))
787 if (flacparse->max_blocksize < 16) {
788 GST_ERROR_OBJECT (flacparse, "Invalid maximum block size: %u",
789 flacparse->max_blocksize);
793 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->min_framesize, 24))
795 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->max_framesize, 24))
798 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->samplerate, 20))
800 if (flacparse->samplerate == 0) {
801 GST_ERROR_OBJECT (flacparse, "Invalid sample rate 0");
805 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->channels, 3))
807 flacparse->channels++;
808 if (flacparse->channels > 8) {
809 GST_ERROR_OBJECT (flacparse, "Invalid number of channels %u",
810 flacparse->channels);
814 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->bps, 5))
818 if (!gst_bit_reader_get_bits_uint64 (&reader, &flacparse->total_samples, 36))
820 if (flacparse->total_samples)
821 gst_base_parse_set_duration (GST_BASE_PARSE (flacparse), GST_FORMAT_TIME,
822 GST_FRAMES_TO_CLOCK_TIME (flacparse->total_samples,
823 flacparse->samplerate));
825 GST_DEBUG_OBJECT (flacparse, "STREAMINFO:\n"
826 "\tmin/max blocksize: %u/%u,\n"
827 "\tmin/max framesize: %u/%u,\n"
828 "\tsamplerate: %u,\n"
830 "\tbits per sample: %u,\n"
831 "\ttotal samples: %" G_GUINT64_FORMAT,
832 flacparse->min_blocksize, flacparse->max_blocksize,
833 flacparse->min_framesize, flacparse->max_framesize,
834 flacparse->samplerate,
835 flacparse->channels, flacparse->bps, flacparse->total_samples);
840 GST_ERROR_OBJECT (flacparse, "Failed to read data");
845 gst_flac_parse_handle_vorbiscomment (GstFlacParse * flacparse,
848 flacparse->tags = gst_tag_list_from_vorbiscomment_buffer (buffer,
849 GST_BUFFER_DATA (buffer), 4, NULL);
851 if (flacparse->tags == NULL) {
852 GST_ERROR_OBJECT (flacparse, "Invalid vorbiscomment block");
853 } else if (gst_tag_list_is_empty (flacparse->tags)) {
854 gst_tag_list_free (flacparse->tags);
855 flacparse->tags = NULL;
862 gst_flac_parse_handle_picture (GstFlacParse * flacparse, GstBuffer * buffer)
864 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
865 const guint8 *data = GST_BUFFER_DATA (buffer);
866 guint32 img_len, img_type;
867 guint32 img_mimetype_len, img_description_len;
869 if (!gst_byte_reader_get_uint32_be (&reader, &img_type))
872 if (!gst_byte_reader_get_uint32_be (&reader, &img_mimetype_len))
874 if (!gst_byte_reader_skip (&reader, img_mimetype_len))
877 if (!gst_byte_reader_get_uint32_be (&reader, &img_description_len))
879 if (!gst_byte_reader_skip (&reader, img_description_len))
882 if (!gst_byte_reader_skip (&reader, 4 * 4))
885 if (!gst_byte_reader_get_uint32_be (&reader, &img_len))
888 if (!flacparse->tags)
889 flacparse->tags = gst_tag_list_new ();
891 gst_tag_list_add_id3_image (flacparse->tags,
892 data + gst_byte_reader_get_pos (&reader), img_len, img_type);
894 if (gst_tag_list_is_empty (flacparse->tags)) {
895 gst_tag_list_free (flacparse->tags);
896 flacparse->tags = NULL;
902 GST_ERROR_OBJECT (flacparse, "Error reading data");
907 _value_array_append_buffer (GValue * array_val, GstBuffer * buf)
909 GValue value = { 0, };
911 g_value_init (&value, GST_TYPE_BUFFER);
912 /* copy buffer to avoid problems with circular refcounts */
913 buf = gst_buffer_copy (buf);
914 /* again, for good measure */
915 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
916 gst_value_set_buffer (&value, buf);
917 gst_buffer_unref (buf);
918 gst_value_array_append_value (array_val, &value);
919 g_value_unset (&value);
923 gst_flac_parse_handle_headers (GstFlacParse * flacparse)
925 GstBuffer *vorbiscomment = NULL;
926 GstBuffer *streaminfo = NULL;
927 GstBuffer *marker = NULL;
928 GValue array = { 0, };
932 caps = gst_caps_new_simple ("audio/x-flac",
933 "channels", G_TYPE_INT, flacparse->channels,
934 "rate", G_TYPE_INT, flacparse->samplerate, NULL);
936 if (!flacparse->headers)
939 for (l = flacparse->headers; l; l = l->next) {
940 GstBuffer *header = l->data;
941 const guint8 *data = GST_BUFFER_DATA (header);
942 guint size = GST_BUFFER_SIZE (header);
944 GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_IN_CAPS);
946 if (size == 4 && memcmp (data, "fLaC", 4) == 0) {
948 } else if (size > 1 && (data[0] & 0x7f) == 0) {
950 } else if (size > 1 && (data[0] & 0x7f) == 4) {
951 vorbiscomment = header;
955 if (marker == NULL || streaminfo == NULL || vorbiscomment == NULL) {
956 GST_WARNING_OBJECT (flacparse,
957 "missing header %p %p %p, muxing into container "
958 "formats may be broken", marker, streaminfo, vorbiscomment);
962 g_value_init (&array, GST_TYPE_ARRAY);
964 /* add marker including STREAMINFO header */
969 /* minus one for the marker that is merged with streaminfo here */
970 num = g_list_length (flacparse->headers) - 1;
972 buf = gst_buffer_new_and_alloc (13 + GST_BUFFER_SIZE (streaminfo));
973 GST_BUFFER_DATA (buf)[0] = 0x7f;
974 memcpy (GST_BUFFER_DATA (buf) + 1, "FLAC", 4);
975 GST_BUFFER_DATA (buf)[5] = 0x01; /* mapping version major */
976 GST_BUFFER_DATA (buf)[6] = 0x00; /* mapping version minor */
977 GST_BUFFER_DATA (buf)[7] = (num & 0xFF00) >> 8;
978 GST_BUFFER_DATA (buf)[8] = (num & 0x00FF) >> 0;
979 memcpy (GST_BUFFER_DATA (buf) + 9, "fLaC", 4);
980 memcpy (GST_BUFFER_DATA (buf) + 13, GST_BUFFER_DATA (streaminfo),
981 GST_BUFFER_SIZE (streaminfo));
982 _value_array_append_buffer (&array, buf);
983 gst_buffer_unref (buf);
986 /* add VORBISCOMMENT header */
987 _value_array_append_buffer (&array, vorbiscomment);
989 /* add other headers, if there are any */
990 for (l = flacparse->headers; l; l = l->next) {
991 if (GST_BUFFER_CAST (l->data) != marker &&
992 GST_BUFFER_CAST (l->data) != streaminfo &&
993 GST_BUFFER_CAST (l->data) != vorbiscomment) {
994 _value_array_append_buffer (&array, GST_BUFFER_CAST (l->data));
998 gst_structure_set_value (gst_caps_get_structure (caps, 0),
999 "streamheader", &array);
1000 g_value_unset (&array);
1004 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse)), caps);
1005 gst_caps_unref (caps);
1007 /* push header buffers; update caps, so when we push the first buffer the
1008 * negotiated caps will change to caps that include the streamheader field */
1009 for (l = flacparse->headers; l != NULL; l = l->next) {
1010 GstBuffer *buf = GST_BUFFER (l->data);
1014 gst_buffer_set_caps (buf,
1015 GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse))));
1017 ret = gst_base_parse_push_buffer (GST_BASE_PARSE (flacparse), buf);
1018 if (ret != GST_FLOW_OK)
1021 g_list_free (flacparse->headers);
1022 flacparse->headers = NULL;
1025 if (flacparse->tags)
1026 gst_element_found_tags (GST_ELEMENT (flacparse),
1027 gst_tag_list_copy (flacparse->tags));
1033 gst_flac_parse_generate_headers (GstFlacParse * flacparse)
1035 GstBuffer *marker, *streaminfo, *vorbiscomment;
1038 marker = gst_buffer_new_and_alloc (4);
1039 memcpy (GST_BUFFER_DATA (marker), "fLaC", 4);
1040 GST_BUFFER_TIMESTAMP (marker) = GST_CLOCK_TIME_NONE;
1041 GST_BUFFER_DURATION (marker) = GST_CLOCK_TIME_NONE;
1042 GST_BUFFER_OFFSET (marker) = 0;
1043 GST_BUFFER_OFFSET_END (marker) = 0;
1044 flacparse->headers = g_list_append (flacparse->headers, marker);
1046 streaminfo = gst_buffer_new_and_alloc (4 + 34);
1047 data = GST_BUFFER_DATA (streaminfo);
1048 memset (data, 0, 4 + 34);
1050 /* metadata block header */
1051 data[0] = 0x00; /* is_last = 0; type = 0; */
1052 data[1] = 0x00; /* length = 34; */
1058 data[4] = (flacparse->block_size >> 8) & 0xff; /* min blocksize = blocksize; */
1059 data[5] = (flacparse->block_size) & 0xff;
1060 data[6] = (flacparse->block_size >> 8) & 0xff; /* max blocksize = blocksize; */
1061 data[7] = (flacparse->block_size) & 0xff;
1063 data[8] = 0x00; /* min framesize = 0; */
1066 data[11] = 0x00; /* max framesize = 0; */
1070 data[14] = (flacparse->samplerate >> 12) & 0xff;
1071 data[15] = (flacparse->samplerate >> 4) & 0xff;
1072 data[16] = (flacparse->samplerate >> 0) & 0xf0;
1074 data[16] |= (flacparse->channels - 1) << 1;
1076 data[16] |= ((flacparse->bps - 1) >> 4) & 0x01;
1077 data[17] = (((flacparse->bps - 1)) & 0x0f) << 4;
1081 GstFormat fmt = GST_FORMAT_TIME;
1083 if (gst_pad_query_peer_duration (GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE
1084 (flacparse)), &fmt, &duration) && fmt == GST_FORMAT_TIME) {
1085 duration = GST_CLOCK_TIME_TO_FRAMES (duration, flacparse->samplerate);
1087 data[17] |= (duration >> 32) & 0xff;
1088 data[18] |= (duration >> 24) & 0xff;
1089 data[19] |= (duration >> 16) & 0xff;
1090 data[20] |= (duration >> 8) & 0xff;
1091 data[21] |= (duration >> 0) & 0xff;
1096 GST_BUFFER_TIMESTAMP (streaminfo) = GST_CLOCK_TIME_NONE;
1097 GST_BUFFER_DURATION (streaminfo) = GST_CLOCK_TIME_NONE;
1098 GST_BUFFER_OFFSET (streaminfo) = 0;
1099 GST_BUFFER_OFFSET_END (streaminfo) = 0;
1100 flacparse->headers = g_list_append (flacparse->headers, streaminfo);
1102 /* empty vorbiscomment */
1104 GstTagList *taglist = gst_tag_list_new ();
1108 header[0] = 0x84; /* is_last = 1; type = 4; */
1111 gst_tag_list_to_vorbiscomment_buffer (taglist, header, sizeof (header),
1113 gst_tag_list_free (taglist);
1115 /* Get rid of framing bit */
1116 if (GST_BUFFER_DATA (vorbiscomment)[GST_BUFFER_SIZE (vorbiscomment) - 1] ==
1121 gst_buffer_create_sub (vorbiscomment, 0,
1122 GST_BUFFER_SIZE (vorbiscomment) - 1);
1123 gst_buffer_unref (vorbiscomment);
1124 vorbiscomment = sub;
1127 size = GST_BUFFER_SIZE (vorbiscomment) - 4;
1128 GST_BUFFER_DATA (vorbiscomment)[1] = ((size & 0xFF0000) >> 16);
1129 GST_BUFFER_DATA (vorbiscomment)[2] = ((size & 0x00FF00) >> 8);
1130 GST_BUFFER_DATA (vorbiscomment)[3] = (size & 0x0000FF);
1132 GST_BUFFER_TIMESTAMP (vorbiscomment) = GST_CLOCK_TIME_NONE;
1133 GST_BUFFER_DURATION (vorbiscomment) = GST_CLOCK_TIME_NONE;
1134 GST_BUFFER_OFFSET (vorbiscomment) = 0;
1135 GST_BUFFER_OFFSET_END (vorbiscomment) = 0;
1136 flacparse->headers = g_list_append (flacparse->headers, vorbiscomment);
1142 static GstFlowReturn
1143 gst_flac_parse_parse_frame (GstBaseParse * parse, GstBuffer * buffer)
1145 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
1146 const guint8 *data = GST_BUFFER_DATA (buffer);
1148 if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) {
1149 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
1150 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1151 GST_BUFFER_OFFSET (buffer) = 0;
1152 GST_BUFFER_OFFSET_END (buffer) = 0;
1154 /* 32 bits metadata block */
1155 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4);
1156 flacparse->state = GST_FLAC_PARSE_STATE_HEADERS;
1158 flacparse->headers =
1159 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1161 return GST_BASE_PARSE_FLOW_DROPPED;
1162 } else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) {
1163 gboolean is_last = ((data[0] & 0x80) == 0x80);
1164 guint type = (data[0] & 0x7F);
1167 GST_WARNING_OBJECT (flacparse, "Invalid metadata block type");
1168 return GST_BASE_PARSE_FLOW_DROPPED;
1171 GST_DEBUG_OBJECT (flacparse, "Handling metadata block of type %u", type);
1174 case 0: /* STREAMINFO */
1175 if (!gst_flac_parse_handle_streaminfo (flacparse, buffer))
1176 return GST_FLOW_ERROR;
1178 case 3: /* SEEKTABLE */
1179 /* TODO: handle seektables */
1181 case 4: /* VORBIS_COMMENT */
1182 if (!gst_flac_parse_handle_vorbiscomment (flacparse, buffer))
1183 return GST_FLOW_ERROR;
1185 case 6: /* PICTURE */
1186 if (!gst_flac_parse_handle_picture (flacparse, buffer))
1187 return GST_FLOW_ERROR;
1189 case 1: /* PADDING */
1190 case 2: /* APPLICATION */
1191 case 5: /* CUESHEET */
1192 default: /* RESERVED */
1196 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
1197 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1198 GST_BUFFER_OFFSET (buffer) = 0;
1199 GST_BUFFER_OFFSET_END (buffer) = 0;
1202 flacparse->headers =
1203 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1205 if (!gst_flac_parse_handle_headers (flacparse))
1206 return GST_FLOW_ERROR;
1208 /* Minimal size of a frame header */
1209 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (16,
1210 flacparse->min_framesize));
1211 flacparse->requested_frame_size = MAX (16, flacparse->min_framesize);
1212 flacparse->state = GST_FLAC_PARSE_STATE_DATA;
1214 /* DROPPED because we pushed all headers manually already */
1215 return GST_BASE_PARSE_FLOW_DROPPED;
1217 flacparse->headers =
1218 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1219 return GST_BASE_PARSE_FLOW_DROPPED;
1222 if (flacparse->offset != GST_BUFFER_OFFSET (buffer)) {
1226 flacparse->offset = GST_BUFFER_OFFSET (buffer);
1227 ret = gst_flac_parse_get_frame_size (flacparse, buffer, &framesize);
1229 GST_ERROR_OBJECT (flacparse,
1230 "Baseclass didn't provide a complete frame");
1231 return GST_FLOW_ERROR;
1235 if (flacparse->block_size == 0) {
1236 GST_ERROR_OBJECT (flacparse, "Unparsed frame");
1237 return GST_FLOW_ERROR;
1240 if (flacparse->state == GST_FLAC_PARSE_STATE_GENERATE_HEADERS) {
1241 if (flacparse->blocking_strategy == 1) {
1242 GST_WARNING_OBJECT (flacparse,
1243 "Generating headers for variable blocksize streams not supported");
1245 if (!gst_flac_parse_handle_headers (flacparse))
1246 return GST_FLOW_ERROR;
1248 GST_DEBUG_OBJECT (flacparse, "Generating headers");
1250 if (!gst_flac_parse_generate_headers (flacparse))
1251 return GST_FLOW_ERROR;
1253 if (!gst_flac_parse_handle_headers (flacparse))
1254 return GST_FLOW_ERROR;
1256 flacparse->state = GST_FLAC_PARSE_STATE_DATA;
1259 /* also cater for oggmux metadata */
1260 if (flacparse->blocking_strategy == 0) {
1261 GST_BUFFER_TIMESTAMP (buffer) =
1262 gst_util_uint64_scale (flacparse->sample_number,
1263 flacparse->block_size * GST_SECOND, flacparse->samplerate);
1264 GST_BUFFER_OFFSET_END (buffer) =
1265 flacparse->sample_number * flacparse->block_size +
1266 flacparse->block_size;
1268 GST_BUFFER_TIMESTAMP (buffer) =
1269 gst_util_uint64_scale (flacparse->sample_number, GST_SECOND,
1270 flacparse->samplerate);
1271 GST_BUFFER_OFFSET_END (buffer) =
1272 flacparse->sample_number + flacparse->block_size;
1274 GST_BUFFER_DURATION (buffer) =
1275 GST_FRAMES_TO_CLOCK_TIME (flacparse->block_size, flacparse->samplerate);
1276 GST_BUFFER_OFFSET (buffer) =
1277 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1279 /* Minimal size of a frame header */
1280 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (16,
1281 flacparse->min_framesize));
1282 flacparse->requested_frame_size = MAX (16, flacparse->min_framesize);
1284 flacparse->offset = -1;
1285 flacparse->blocking_strategy = 0;
1286 flacparse->block_size = 0;
1287 flacparse->sample_number = 0;