3 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gstflacparse.h"
28 #include <gst/tag/tag.h>
29 #include <gst/audio/audio.h>
31 #include <gst/base/gstbitreader.h>
32 #include <gst/base/gstbytereader.h>
34 GST_DEBUG_CATEGORY_STATIC (flacparse_debug);
35 #define GST_CAT_DEFAULT flacparse_debug
37 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
40 GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) true, "
41 "channels = (int) [ 1, 8 ], " "rate = (int) [ 1, 655350 ]")
44 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) false")
50 static void gst_flac_parse_finalize (GObject * object);
52 static gboolean gst_flac_parse_start (GstBaseParse * parse);
53 static gboolean gst_flac_parse_stop (GstBaseParse * parse);
54 static gboolean gst_flac_parse_check_valid_frame (GstBaseParse * parse,
55 GstBuffer * buffer, guint * framesize, gint * skipsize);
56 static GstFlowReturn gst_flac_parse_parse_frame (GstBaseParse * parse,
59 GST_BOILERPLATE (GstFlacParse, gst_flac_parse, GstBaseParse,
63 gst_flac_parse_base_init (gpointer g_class)
65 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
67 gst_element_class_add_pad_template (element_class,
68 gst_static_pad_template_get (&src_factory));
69 gst_element_class_add_pad_template (element_class,
70 gst_static_pad_template_get (&sink_factory));
72 gst_element_class_set_details_simple (element_class, "FLAC audio parser",
74 "Parses audio with the FLAC lossless audio codec",
75 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
77 GST_DEBUG_CATEGORY_INIT (flacparse_debug, "flacparse", 0,
78 "Flac parser element");
82 gst_flac_parse_class_init (GstFlacParseClass * klass)
84 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85 GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass);
87 gobject_class->finalize = gst_flac_parse_finalize;
89 baseparse_class->start = GST_DEBUG_FUNCPTR (gst_flac_parse_start);
90 baseparse_class->stop = GST_DEBUG_FUNCPTR (gst_flac_parse_stop);
91 baseparse_class->check_valid_frame =
92 GST_DEBUG_FUNCPTR (gst_flac_parse_check_valid_frame);
93 baseparse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_flac_parse_parse_frame);
97 gst_flac_parse_init (GstFlacParse * flacparse, GstFlacParseClass * klass)
102 gst_flac_parse_finalize (GObject * object)
104 GstFlacParse *flacparse = GST_FLAC_PARSE (object);
106 if (flacparse->tags) {
107 gst_tag_list_free (flacparse->tags);
108 flacparse->tags = NULL;
112 g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL);
113 g_list_free (flacparse->headers);
114 flacparse->headers = NULL;
116 G_OBJECT_CLASS (parent_class)->finalize (object);
120 gst_flac_parse_start (GstBaseParse * parse)
122 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
124 flacparse->state = GST_FLAC_PARSE_STATE_INIT;
125 flacparse->min_blocksize = 0;
126 flacparse->max_blocksize = 0;
127 flacparse->min_framesize = 0;
128 flacparse->max_framesize = 0;
130 flacparse->upstream_length = -1;
132 flacparse->samplerate = 0;
133 flacparse->channels = 0;
135 flacparse->total_samples = 0;
137 flacparse->requested_frame_size = 0;
138 flacparse->offset = GST_CLOCK_TIME_NONE;
139 flacparse->blocking_strategy = 0;
140 flacparse->block_size = 0;
141 flacparse->sample_number = 0;
144 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4);
150 gst_flac_parse_stop (GstBaseParse * parse)
152 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
154 if (flacparse->tags) {
155 gst_tag_list_free (flacparse->tags);
156 flacparse->tags = NULL;
159 g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL);
160 g_list_free (flacparse->headers);
161 flacparse->headers = NULL;
167 gst_flac_parse_get_frame_size (GstFlacParse * flacparse, GstBuffer * buffer,
168 guint * framesize_ret)
170 GstBitReader reader = GST_BIT_READER_INIT_FROM_BUFFER (buffer);
174 guint8 channel_assignment = 0;
176 /* Skip 14 bit sync code */
177 if (!gst_bit_reader_skip (&reader, 14))
181 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
186 /* 0 == fixed block size, 1 == variable block size */
187 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->blocking_strategy,
191 /* block size index, calculation of the real blocksize below */
192 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 4))
194 else if (flacparse->block_size == 0)
197 /* sample rate index, calculation of the real samplerate below */
198 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 4))
200 else if (samplerate == 0x0f)
203 /* channel assignment */
204 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 4)) {
206 } else if (tmp < 8) {
207 if (flacparse->channels && tmp + 1 != flacparse->channels)
210 flacparse->channels = tmp + 1;
211 } else if (tmp <= 10) {
212 if (flacparse->channels && 2 != flacparse->channels)
215 flacparse->channels = 2;
217 channel_assignment = 1; /* left-side */
219 channel_assignment = 2; /* right-side */
221 channel_assignment = 3; /* mid-side */
222 } else if (tmp > 10) {
226 /* bits per sample */
227 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 3)) {
229 } else if (tmp == 0x03 || tmp == 0x07) {
231 } else if (tmp == 0 && flacparse->bps == 0) {
232 goto need_streaminfo;
233 } else if (tmp == 0x01 && flacparse->bps != 8) {
234 if (flacparse->bps && flacparse->bps != 8)
238 } else if (tmp == 0x02 && flacparse->bps != 12) {
239 if (flacparse->bps && flacparse->bps != 12)
243 } else if (tmp == 0x04 && flacparse->bps != 16) {
244 if (flacparse->bps && flacparse->bps != 16)
248 } else if (tmp == 0x05 && flacparse->bps != 20) {
249 if (flacparse->bps && flacparse->bps != 20)
253 } else if (tmp == 0x06 && flacparse->bps != 24) {
254 if (flacparse->bps && flacparse->bps != 24)
260 /* reserved, must be 0 */
261 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
266 /* read "utf8" encoded sample/frame number */
272 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
280 flacparse->sample_number = 0;
282 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 7))
284 flacparse->sample_number = tmp;
285 } else if ((flacparse->blocking_strategy == 0 && len > 6) ||
286 (flacparse->blocking_strategy == 1 && len > 7)) {
289 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 8 - len - 1))
292 flacparse->sample_number = tmp;
296 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 2))
298 else if (tmp != 0x02)
301 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 6))
303 flacparse->sample_number <<= 6;
304 flacparse->sample_number |= tmp;
310 /* calculate real blocksize from the blocksize index */
311 if (flacparse->block_size == 1)
312 flacparse->block_size = 192;
313 else if (flacparse->block_size <= 5)
314 flacparse->block_size = 576 * (1 << (flacparse->block_size - 2));
315 else if (flacparse->block_size <= 15)
316 flacparse->block_size = 256 * (1 << (flacparse->block_size - 8));
317 else if (flacparse->block_size == 6) {
318 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 8))
320 flacparse->block_size++;
321 } else if (flacparse->block_size == 7) {
322 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 16))
324 flacparse->block_size++;
327 /* calculate the real samplerate from the samplerate index */
328 if (samplerate == 0 && flacparse->samplerate == 0) {
329 goto need_streaminfo;
330 } else if (samplerate == 1) {
331 if (flacparse->samplerate == 0)
332 flacparse->samplerate = 88200;
333 else if (flacparse->samplerate != 88200)
335 } else if (samplerate == 2) {
336 if (flacparse->samplerate == 0)
337 flacparse->samplerate = 176400;
338 else if (flacparse->samplerate != 176400)
340 } else if (samplerate == 3) {
341 if (flacparse->samplerate == 0)
342 flacparse->samplerate = 192000;
343 else if (flacparse->samplerate != 192000)
345 } else if (samplerate == 4) {
346 if (flacparse->samplerate == 0)
347 flacparse->samplerate = 8000;
348 else if (flacparse->samplerate != 8000)
350 } else if (samplerate == 5) {
351 if (flacparse->samplerate == 0)
352 flacparse->samplerate = 16000;
353 else if (flacparse->samplerate != 16000)
355 } else if (samplerate == 6) {
356 if (flacparse->samplerate == 0)
357 flacparse->samplerate = 22050;
358 else if (flacparse->samplerate != 22050)
360 } else if (samplerate == 7) {
361 if (flacparse->samplerate == 0)
362 flacparse->samplerate = 24000;
363 else if (flacparse->samplerate != 24000)
365 } else if (samplerate == 8) {
366 if (flacparse->samplerate == 0)
367 flacparse->samplerate = 32000;
368 else if (flacparse->samplerate != 32000)
370 } else if (samplerate == 9) {
371 if (flacparse->samplerate == 0)
372 flacparse->samplerate = 44100;
373 else if (flacparse->samplerate != 44100)
375 } else if (samplerate == 10) {
376 if (flacparse->samplerate == 0)
377 flacparse->samplerate = 48000;
378 else if (flacparse->samplerate != 48000)
380 } else if (samplerate == 11) {
381 if (flacparse->samplerate == 0)
382 flacparse->samplerate = 96000;
383 else if (flacparse->samplerate != 96000)
385 } else if (samplerate == 12) {
386 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 8))
389 if (flacparse->samplerate == 0)
390 flacparse->samplerate = samplerate;
391 else if (flacparse->samplerate != samplerate)
393 } else if (samplerate == 13) {
394 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 16))
396 if (flacparse->samplerate == 0)
397 flacparse->samplerate = samplerate;
398 else if (flacparse->samplerate != samplerate)
400 } else if (samplerate == 14) {
401 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 16))
404 if (flacparse->samplerate == 0)
405 flacparse->samplerate = samplerate;
406 else if (flacparse->samplerate != samplerate)
410 /* skip crc-8 for the header */
411 if (!gst_bit_reader_skip (&reader, 8))
414 /* parse subframes, one subframe per channel */
415 for (i = 0; i < flacparse->channels; i++) {
419 cur_bps = flacparse->bps;
421 /* for mid/side, left/side, right/side the "difference" channel
422 * needs and additional bit */
423 if (i == 0 && channel_assignment == 2)
425 else if (i == 1 && (channel_assignment == 1 || channel_assignment == 3))
429 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
435 if (!gst_bit_reader_get_bits_uint8 (&reader, &sf_type, 6))
437 else if (((sf_type & 0xfe) == 0x02) ||
438 ((sf_type & 0xfc) == 0x04) ||
439 ((sf_type & 0xf8) == 0x08 && (sf_type & 0x07) > 4) ||
440 ((sf_type & 0xf0) == 0x10))
443 /* wasted bits per sample, if 1 the value follows unary coded */
444 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1)) {
446 } else if (tmp != 0) {
451 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
459 /* subframe type: constant */
460 if (sf_type == 0x00) {
461 if (!gst_bit_reader_skip (&reader, cur_bps))
463 /* subframe type: verbatim */
464 } else if (sf_type == 0x01) {
465 if (!gst_bit_reader_skip (&reader, cur_bps * flacparse->block_size))
467 /* subframe type: LPC or fixed */
469 guint8 residual_type;
471 guint16 partition_order;
474 /* Skip warm-up samples for fixed subframe and calculate order */
475 if ((sf_type & 0xf8) == 0x08) {
476 order = sf_type & 0x07;
478 g_assert (order <= 4);
480 if (!gst_bit_reader_skip (&reader, cur_bps * order))
482 /* Skip warm-up samples for LPC subframe, get parameters and calculate order */
483 } else if ((sf_type & 0xe0) == 0x20) {
486 order = (sf_type & 0x1f) + 1;
488 /* warm-up samples */
489 if (!gst_bit_reader_skip (&reader, cur_bps * order))
492 /* LPC coefficient precision */
493 if (!gst_bit_reader_get_bits_uint8 (&reader, &prec, 4))
495 else if (prec == 0x0f)
499 /* LPC coefficient shift */
500 if (!gst_bit_reader_skip (&reader, 5))
503 /* LPC coefficients */
504 if (!gst_bit_reader_skip (&reader, order * prec))
507 g_assert_not_reached ();
510 /* residual type: 0 == rice, 1 == rice2 */
511 if (!gst_bit_reader_get_bits_uint8 (&reader, &residual_type, 2))
514 if (residual_type & 0x02)
517 /* partition order */
518 if (!gst_bit_reader_get_bits_uint16 (&reader, &partition_order, 4))
521 partition_order = 1 << partition_order;
523 /* 2^partition_order partitions */
524 for (j = 0; j < partition_order; j++) {
526 guint8 rice_parameter;
528 /* calculate number of samples for the current partition */
529 if (partition_order == 1) {
530 samples = flacparse->block_size - order;
532 samples = flacparse->block_size / partition_order;
534 samples = flacparse->block_size / partition_order - order;
538 if (!gst_bit_reader_get_bits_uint8 (&reader, &rice_parameter,
539 (residual_type == 0) ? 4 : 5))
542 /* if rice parameter has all bits set the samples follow unencoded with the number of bits
543 * per sample in the following 5 bits */
544 if ((residual_type == 0 && rice_parameter == 0x0f)
545 || (residual_type == 1 && rice_parameter == 0x1f)) {
546 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 5))
548 if (!gst_bit_reader_skip (&reader, tmp * samples))
553 /* read the rice encoded samples */
554 for (k = 0; k < samples; k++) {
557 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
560 if (!gst_bit_reader_skip (&reader, rice_parameter))
568 /* zero padding to byte alignment */
569 gst_bit_reader_skip_to_byte (&reader);
571 /* Skip crc-16 for the complete frame */
572 if (!gst_bit_reader_skip (&reader, 16))
575 *framesize_ret = gst_bit_reader_get_pos (&reader) / 8;
577 GST_DEBUG_OBJECT (flacparse, "Parsed frame at offset %" G_GUINT64_FORMAT ":\n"
580 "Sample/Frame number: %" G_GUINT64_FORMAT,
581 flacparse->offset, *framesize_ret,
582 flacparse->block_size, flacparse->sample_number);
590 /* not enough, if that was all available, give up on frame */
591 if (G_UNLIKELY (gst_base_parse_get_drain (GST_BASE_PARSE_CAST (flacparse))))
593 /* otherwise, ask for some more */
594 max = flacparse->max_framesize;
597 flacparse->requested_frame_size
598 = MIN (GST_BUFFER_SIZE (buffer) + 4096, max);
599 if (flacparse->requested_frame_size > GST_BUFFER_SIZE (buffer)) {
600 GST_DEBUG_OBJECT (flacparse, "Requesting %u bytes",
601 flacparse->requested_frame_size);
602 return flacparse->requested_frame_size;
604 GST_DEBUG_OBJECT (flacparse, "Giving up on invalid frame (%d bytes)",
605 GST_BUFFER_SIZE (buffer));
612 GST_ERROR_OBJECT (flacparse, "Need STREAMINFO");
618 GST_WARNING_OBJECT (flacparse, "EOS");
624 GST_WARNING_OBJECT (flacparse, "Invalid frame");
630 gst_flac_parse_check_valid_frame (GstBaseParse * parse, GstBuffer * buffer,
631 guint * framesize, gint * skipsize)
633 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
634 const guint8 *data = GST_BUFFER_DATA (buffer);
636 if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < 4))
639 if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) {
640 if (memcmp (GST_BUFFER_DATA (buffer), "fLaC", 4) == 0) {
641 GST_DEBUG_OBJECT (flacparse, "fLaC marker found");
644 } else if (data[0] == 0xff && (data[1] >> 2) == 0x3e) {
645 GST_DEBUG_OBJECT (flacparse, "Found headerless FLAC");
646 /* Minimal size of a frame header */
647 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 16);
648 flacparse->requested_frame_size = 16;
649 flacparse->state = GST_FLAC_PARSE_STATE_GENERATE_HEADERS;
653 GST_DEBUG_OBJECT (flacparse, "fLaC marker not found");
656 } else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) {
657 guint size = 4 + ((data[1] << 16) | (data[2] << 8) | (data[3]));
659 GST_DEBUG_OBJECT (flacparse, "Found metadata block of size %u", size);
663 if (data[0] == 0xff && (data[1] >> 2) == 0x3e) {
666 flacparse->offset = GST_BUFFER_OFFSET (buffer);
667 flacparse->blocking_strategy = 0;
668 flacparse->block_size = 0;
669 flacparse->sample_number = 0;
671 GST_DEBUG_OBJECT (flacparse, "Found sync code");
672 ret = gst_flac_parse_get_frame_size (flacparse, buffer, framesize);
675 /* if not in sync, also check for next frame header */
676 if (!gst_base_parse_get_sync (parse) &&
677 !gst_base_parse_get_drain (parse)) {
678 GST_DEBUG_OBJECT (flacparse, "Resyncing; checking next sync code");
679 if (GST_BUFFER_SIZE (buffer) >= ret + 2) {
680 if (data[ret] == 0xff && (data[ret + 1] >> 2) == 0x3e) {
681 GST_DEBUG_OBJECT (flacparse, "Found next sync code");
684 GST_DEBUG_OBJECT (flacparse,
685 "No next sync code, rejecting frame");
689 /* request more data for next sync */
690 GST_DEBUG_OBJECT (flacparse, "... but not enough data");
692 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), ret);
693 flacparse->requested_frame_size = ret;
698 } else if (ret == -1) {
700 } else if (ret == -2) {
701 GST_ELEMENT_ERROR (flacparse, STREAM, FORMAT, (NULL),
702 ("Need STREAMINFO for parsing"));
704 } else if (ret > 0) {
706 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), ret);
707 flacparse->requested_frame_size = ret;
711 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
714 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xfffc0000, 0xfff80000,
715 0, GST_BUFFER_SIZE (buffer));
718 GST_DEBUG_OBJECT (parse, "Possible sync at buffer offset %d", off);
722 GST_DEBUG_OBJECT (flacparse, "Sync code not found");
723 *skipsize = GST_BUFFER_SIZE (buffer) - 3;
733 gst_flac_parse_handle_streaminfo (GstFlacParse * flacparse, GstBuffer * buffer)
735 GstBitReader reader = GST_BIT_READER_INIT_FROM_BUFFER (buffer);
737 if (GST_BUFFER_SIZE (buffer) != 4 + 34) {
738 GST_ERROR_OBJECT (flacparse, "Invalid metablock size for STREAMINFO: %u",
739 GST_BUFFER_SIZE (buffer));
743 /* Skip metadata block header */
744 gst_bit_reader_skip (&reader, 32);
746 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->min_blocksize, 16))
748 if (flacparse->min_blocksize < 16) {
749 GST_ERROR_OBJECT (flacparse, "Invalid minimum block size: %u",
750 flacparse->min_blocksize);
754 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->max_blocksize, 16))
756 if (flacparse->max_blocksize < 16) {
757 GST_ERROR_OBJECT (flacparse, "Invalid maximum block size: %u",
758 flacparse->max_blocksize);
762 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->min_framesize, 24))
764 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->max_framesize, 24))
767 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->samplerate, 20))
769 if (flacparse->samplerate == 0) {
770 GST_ERROR_OBJECT (flacparse, "Invalid sample rate 0");
774 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->channels, 3))
776 flacparse->channels++;
777 if (flacparse->channels > 8) {
778 GST_ERROR_OBJECT (flacparse, "Invalid number of channels %u",
779 flacparse->channels);
783 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->bps, 5))
787 if (!gst_bit_reader_get_bits_uint64 (&reader, &flacparse->total_samples, 36))
789 if (flacparse->total_samples)
790 gst_base_parse_set_duration (GST_BASE_PARSE (flacparse), GST_FORMAT_TIME,
791 GST_FRAMES_TO_CLOCK_TIME (flacparse->total_samples,
792 flacparse->samplerate));
794 GST_DEBUG_OBJECT (flacparse, "STREAMINFO:\n"
795 "\tmin/max blocksize: %u/%u,\n"
796 "\tmin/max framesize: %u/%u,\n"
797 "\tsamplerate: %u,\n"
799 "\tbits per sample: %u,\n"
800 "\ttotal samples: %" G_GUINT64_FORMAT,
801 flacparse->min_blocksize, flacparse->max_blocksize,
802 flacparse->min_framesize, flacparse->max_framesize,
803 flacparse->samplerate,
804 flacparse->channels, flacparse->bps, flacparse->total_samples);
809 GST_ERROR_OBJECT (flacparse, "Failed to read data");
814 gst_flac_parse_handle_vorbiscomment (GstFlacParse * flacparse,
817 flacparse->tags = gst_tag_list_from_vorbiscomment_buffer (buffer,
818 GST_BUFFER_DATA (buffer), 4, NULL);
820 if (flacparse->tags == NULL) {
821 GST_ERROR_OBJECT (flacparse, "Invalid vorbiscomment block");
822 } else if (gst_tag_list_is_empty (flacparse->tags)) {
823 gst_tag_list_free (flacparse->tags);
824 flacparse->tags = NULL;
831 gst_flac_parse_handle_picture (GstFlacParse * flacparse, GstBuffer * buffer)
833 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
834 const guint8 *data = GST_BUFFER_DATA (buffer);
835 guint32 img_len, img_type;
836 guint32 img_mimetype_len, img_description_len;
838 if (!gst_byte_reader_get_uint32_be (&reader, &img_type))
841 if (!gst_byte_reader_get_uint32_be (&reader, &img_mimetype_len))
843 if (!gst_byte_reader_skip (&reader, img_mimetype_len))
846 if (!gst_byte_reader_get_uint32_be (&reader, &img_description_len))
848 if (!gst_byte_reader_skip (&reader, img_description_len))
851 if (!gst_byte_reader_skip (&reader, 4 * 4))
854 if (!gst_byte_reader_get_uint32_be (&reader, &img_len))
857 if (!flacparse->tags)
858 flacparse->tags = gst_tag_list_new ();
860 gst_tag_list_add_id3_image (flacparse->tags,
861 data + gst_byte_reader_get_pos (&reader), img_len, img_type);
863 if (gst_tag_list_is_empty (flacparse->tags)) {
864 gst_tag_list_free (flacparse->tags);
865 flacparse->tags = NULL;
871 GST_ERROR_OBJECT (flacparse, "Error reading data");
876 _value_array_append_buffer (GValue * array_val, GstBuffer * buf)
878 GValue value = { 0, };
880 g_value_init (&value, GST_TYPE_BUFFER);
881 /* copy buffer to avoid problems with circular refcounts */
882 buf = gst_buffer_copy (buf);
883 /* again, for good measure */
884 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
885 gst_value_set_buffer (&value, buf);
886 gst_buffer_unref (buf);
887 gst_value_array_append_value (array_val, &value);
888 g_value_unset (&value);
892 gst_flac_parse_handle_headers (GstFlacParse * flacparse)
894 GstBuffer *vorbiscomment = NULL;
895 GstBuffer *streaminfo = NULL;
896 GstBuffer *marker = NULL;
897 GValue array = { 0, };
901 caps = gst_caps_new_simple ("audio/x-flac",
902 "channels", G_TYPE_INT, flacparse->channels,
903 "rate", G_TYPE_INT, flacparse->samplerate, NULL);
905 if (!flacparse->headers)
908 for (l = flacparse->headers; l; l = l->next) {
909 GstBuffer *header = l->data;
910 const guint8 *data = GST_BUFFER_DATA (header);
911 guint size = GST_BUFFER_SIZE (header);
913 GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_IN_CAPS);
915 if (size == 4 && memcmp (data, "fLaC", 4) == 0) {
917 } else if (size > 1 && (data[0] & 0x7f) == 0) {
919 } else if (size > 1 && (data[0] & 0x7f) == 4) {
920 vorbiscomment = header;
924 if (marker == NULL || streaminfo == NULL || vorbiscomment == NULL) {
925 GST_WARNING_OBJECT (flacparse,
926 "missing header %p %p %p, muxing into container "
927 "formats may be broken", marker, streaminfo, vorbiscomment);
931 g_value_init (&array, GST_TYPE_ARRAY);
933 /* add marker including STREAMINFO header */
938 /* minus one for the marker that is merged with streaminfo here */
939 num = g_list_length (flacparse->headers) - 1;
941 buf = gst_buffer_new_and_alloc (13 + GST_BUFFER_SIZE (streaminfo));
942 GST_BUFFER_DATA (buf)[0] = 0x7f;
943 memcpy (GST_BUFFER_DATA (buf) + 1, "FLAC", 4);
944 GST_BUFFER_DATA (buf)[5] = 0x01; /* mapping version major */
945 GST_BUFFER_DATA (buf)[6] = 0x00; /* mapping version minor */
946 GST_BUFFER_DATA (buf)[7] = (num & 0xFF00) >> 8;
947 GST_BUFFER_DATA (buf)[8] = (num & 0x00FF) >> 0;
948 memcpy (GST_BUFFER_DATA (buf) + 9, "fLaC", 4);
949 memcpy (GST_BUFFER_DATA (buf) + 13, GST_BUFFER_DATA (streaminfo),
950 GST_BUFFER_SIZE (streaminfo));
951 _value_array_append_buffer (&array, buf);
952 gst_buffer_unref (buf);
955 /* add VORBISCOMMENT header */
956 _value_array_append_buffer (&array, vorbiscomment);
958 /* add other headers, if there are any */
959 for (l = flacparse->headers; l; l = l->next) {
960 if (GST_BUFFER_CAST (l->data) != marker &&
961 GST_BUFFER_CAST (l->data) != streaminfo &&
962 GST_BUFFER_CAST (l->data) != vorbiscomment) {
963 _value_array_append_buffer (&array, GST_BUFFER_CAST (l->data));
967 gst_structure_set_value (gst_caps_get_structure (caps, 0),
968 "streamheader", &array);
969 g_value_unset (&array);
973 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse)), caps);
974 gst_caps_unref (caps);
976 /* push header buffers; update caps, so when we push the first buffer the
977 * negotiated caps will change to caps that include the streamheader field */
978 for (l = flacparse->headers; l != NULL; l = l->next) {
979 GstBuffer *buf = GST_BUFFER (l->data);
983 gst_buffer_set_caps (buf,
984 GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse))));
986 ret = gst_base_parse_push_buffer (GST_BASE_PARSE (flacparse), buf);
987 if (ret != GST_FLOW_OK)
990 g_list_free (flacparse->headers);
991 flacparse->headers = NULL;
995 gst_element_found_tags (GST_ELEMENT (flacparse),
996 gst_tag_list_copy (flacparse->tags));
1002 gst_flac_parse_generate_headers (GstFlacParse * flacparse)
1004 GstBuffer *marker, *streaminfo, *vorbiscomment;
1007 marker = gst_buffer_new_and_alloc (4);
1008 memcpy (GST_BUFFER_DATA (marker), "fLaC", 4);
1009 GST_BUFFER_TIMESTAMP (marker) = GST_CLOCK_TIME_NONE;
1010 GST_BUFFER_DURATION (marker) = GST_CLOCK_TIME_NONE;
1011 GST_BUFFER_OFFSET (marker) = 0;
1012 GST_BUFFER_OFFSET_END (marker) = 0;
1013 flacparse->headers = g_list_append (flacparse->headers, marker);
1015 streaminfo = gst_buffer_new_and_alloc (4 + 34);
1016 data = GST_BUFFER_DATA (streaminfo);
1017 memset (data, 0, 4 + 34);
1019 /* metadata block header */
1020 data[0] = 0x00; /* is_last = 0; type = 0; */
1021 data[1] = 0x00; /* length = 34; */
1027 data[4] = (flacparse->block_size >> 8) & 0xff; /* min blocksize = blocksize; */
1028 data[5] = (flacparse->block_size) & 0xff;
1029 data[6] = (flacparse->block_size >> 8) & 0xff; /* max blocksize = blocksize; */
1030 data[7] = (flacparse->block_size) & 0xff;
1032 data[8] = 0x00; /* min framesize = 0; */
1035 data[11] = 0x00; /* max framesize = 0; */
1039 data[14] = (flacparse->samplerate >> 12) & 0xff;
1040 data[15] = (flacparse->samplerate >> 4) & 0xff;
1041 data[16] = (flacparse->samplerate >> 0) & 0xf0;
1043 data[16] |= (flacparse->channels - 1) << 1;
1045 data[16] |= ((flacparse->bps - 1) >> 4) & 0x01;
1046 data[17] = (((flacparse->bps - 1)) & 0x0f) << 4;
1050 GstFormat fmt = GST_FORMAT_TIME;
1052 if (gst_pad_query_peer_duration (GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE
1053 (flacparse)), &fmt, &duration) && fmt == GST_FORMAT_TIME) {
1054 duration = GST_CLOCK_TIME_TO_FRAMES (duration, flacparse->samplerate);
1056 data[17] |= (duration >> 32) & 0xff;
1057 data[18] |= (duration >> 24) & 0xff;
1058 data[19] |= (duration >> 16) & 0xff;
1059 data[20] |= (duration >> 8) & 0xff;
1060 data[21] |= (duration >> 0) & 0xff;
1065 GST_BUFFER_TIMESTAMP (streaminfo) = GST_CLOCK_TIME_NONE;
1066 GST_BUFFER_DURATION (streaminfo) = GST_CLOCK_TIME_NONE;
1067 GST_BUFFER_OFFSET (streaminfo) = 0;
1068 GST_BUFFER_OFFSET_END (streaminfo) = 0;
1069 flacparse->headers = g_list_append (flacparse->headers, streaminfo);
1071 /* empty vorbiscomment */
1073 GstTagList *taglist = gst_tag_list_new ();
1077 header[0] = 0x84; /* is_last = 1; type = 4; */
1080 gst_tag_list_to_vorbiscomment_buffer (taglist, header, sizeof (header),
1082 gst_tag_list_free (taglist);
1084 /* Get rid of framing bit */
1085 if (GST_BUFFER_DATA (vorbiscomment)[GST_BUFFER_SIZE (vorbiscomment) - 1] ==
1090 gst_buffer_create_sub (vorbiscomment, 0,
1091 GST_BUFFER_SIZE (vorbiscomment) - 1);
1092 gst_buffer_unref (vorbiscomment);
1093 vorbiscomment = sub;
1096 size = GST_BUFFER_SIZE (vorbiscomment) - 4;
1097 GST_BUFFER_DATA (vorbiscomment)[1] = ((size & 0xFF0000) >> 16);
1098 GST_BUFFER_DATA (vorbiscomment)[2] = ((size & 0x00FF00) >> 8);
1099 GST_BUFFER_DATA (vorbiscomment)[3] = (size & 0x0000FF);
1101 GST_BUFFER_TIMESTAMP (vorbiscomment) = GST_CLOCK_TIME_NONE;
1102 GST_BUFFER_DURATION (vorbiscomment) = GST_CLOCK_TIME_NONE;
1103 GST_BUFFER_OFFSET (vorbiscomment) = 0;
1104 GST_BUFFER_OFFSET_END (vorbiscomment) = 0;
1105 flacparse->headers = g_list_append (flacparse->headers, vorbiscomment);
1111 static GstFlowReturn
1112 gst_flac_parse_parse_frame (GstBaseParse * parse, GstBuffer * buffer)
1114 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
1115 const guint8 *data = GST_BUFFER_DATA (buffer);
1117 if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) {
1118 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
1119 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1120 GST_BUFFER_OFFSET (buffer) = 0;
1121 GST_BUFFER_OFFSET_END (buffer) = 0;
1123 /* 32 bits metadata block */
1124 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4);
1125 flacparse->state = GST_FLAC_PARSE_STATE_HEADERS;
1127 flacparse->headers =
1128 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1130 return GST_BASE_PARSE_FLOW_DROPPED;
1131 } else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) {
1132 gboolean is_last = ((data[0] & 0x80) == 0x80);
1133 guint type = (data[0] & 0x7F);
1136 GST_WARNING_OBJECT (flacparse, "Invalid metadata block type");
1137 return GST_BASE_PARSE_FLOW_DROPPED;
1140 GST_DEBUG_OBJECT (flacparse, "Handling metadata block of type %u", type);
1143 case 0: /* STREAMINFO */
1144 if (!gst_flac_parse_handle_streaminfo (flacparse, buffer))
1145 return GST_FLOW_ERROR;
1147 case 3: /* SEEKTABLE */
1148 /* TODO: handle seektables */
1150 case 4: /* VORBIS_COMMENT */
1151 if (!gst_flac_parse_handle_vorbiscomment (flacparse, buffer))
1152 return GST_FLOW_ERROR;
1154 case 6: /* PICTURE */
1155 if (!gst_flac_parse_handle_picture (flacparse, buffer))
1156 return GST_FLOW_ERROR;
1158 case 1: /* PADDING */
1159 case 2: /* APPLICATION */
1160 case 5: /* CUESHEET */
1161 default: /* RESERVED */
1165 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
1166 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1167 GST_BUFFER_OFFSET (buffer) = 0;
1168 GST_BUFFER_OFFSET_END (buffer) = 0;
1171 flacparse->headers =
1172 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1174 if (!gst_flac_parse_handle_headers (flacparse))
1175 return GST_FLOW_ERROR;
1177 /* Minimal size of a frame header */
1178 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (16,
1179 flacparse->min_framesize));
1180 flacparse->requested_frame_size = MAX (16, flacparse->min_framesize);
1181 flacparse->state = GST_FLAC_PARSE_STATE_DATA;
1183 /* DROPPED because we pushed all headers manually already */
1184 return GST_BASE_PARSE_FLOW_DROPPED;
1186 flacparse->headers =
1187 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1188 return GST_BASE_PARSE_FLOW_DROPPED;
1191 if (flacparse->offset != GST_BUFFER_OFFSET (buffer)) {
1195 flacparse->offset = GST_BUFFER_OFFSET (buffer);
1196 ret = gst_flac_parse_get_frame_size (flacparse, buffer, &framesize);
1198 GST_ERROR_OBJECT (flacparse,
1199 "Baseclass didn't provide a complete frame");
1200 return GST_FLOW_ERROR;
1204 if (flacparse->block_size == 0) {
1205 GST_ERROR_OBJECT (flacparse, "Unparsed frame");
1206 return GST_FLOW_ERROR;
1209 if (flacparse->state == GST_FLAC_PARSE_STATE_GENERATE_HEADERS) {
1210 if (flacparse->blocking_strategy == 1) {
1211 GST_WARNING_OBJECT (flacparse,
1212 "Generating headers for variable blocksize streams not supported");
1214 if (!gst_flac_parse_handle_headers (flacparse))
1215 return GST_FLOW_ERROR;
1217 GST_DEBUG_OBJECT (flacparse, "Generating headers");
1219 if (!gst_flac_parse_generate_headers (flacparse))
1220 return GST_FLOW_ERROR;
1222 if (!gst_flac_parse_handle_headers (flacparse))
1223 return GST_FLOW_ERROR;
1225 flacparse->state = GST_FLAC_PARSE_STATE_DATA;
1228 /* also cater for oggmux metadata */
1229 if (flacparse->blocking_strategy == 0) {
1230 GST_BUFFER_TIMESTAMP (buffer) =
1231 gst_util_uint64_scale (flacparse->sample_number,
1232 flacparse->block_size * GST_SECOND, flacparse->samplerate);
1233 GST_BUFFER_OFFSET_END (buffer) =
1234 flacparse->sample_number * flacparse->block_size +
1235 flacparse->block_size;
1237 GST_BUFFER_TIMESTAMP (buffer) =
1238 gst_util_uint64_scale (flacparse->sample_number, GST_SECOND,
1239 flacparse->samplerate);
1240 GST_BUFFER_OFFSET_END (buffer) =
1241 flacparse->sample_number + flacparse->block_size;
1243 GST_BUFFER_DURATION (buffer) =
1244 GST_FRAMES_TO_CLOCK_TIME (flacparse->block_size, flacparse->samplerate);
1245 GST_BUFFER_OFFSET (buffer) =
1246 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1248 /* Minimal size of a frame header */
1249 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (16,
1250 flacparse->min_framesize));
1251 flacparse->requested_frame_size = MAX (16, flacparse->min_framesize);
1253 flacparse->offset = -1;
1254 flacparse->blocking_strategy = 0;
1255 flacparse->block_size = 0;
1256 flacparse->sample_number = 0;