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 /* CRC-8, poly = x^8 + x^2 + x^1 + x^0, init = 0 */
69 static const guint8 crc8_table[256] = {
70 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
71 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
72 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
73 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
74 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5,
75 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
76 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85,
77 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
78 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
79 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
80 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2,
81 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
82 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32,
83 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
84 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
85 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
86 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C,
87 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
88 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC,
89 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
90 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
91 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
92 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C,
93 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
94 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B,
95 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
96 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
97 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
98 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
99 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
100 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
101 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
105 gst_flac_calculate_crc8 (const guint8 * data, guint length)
110 crc = crc8_table[crc ^ *data];
117 /* CRC-16, poly = x^16 + x^15 + x^2 + x^0, init = 0 */
118 static const guint16 crc16_table[256] = {
119 0x0000, 0x8005, 0x800f, 0x000a, 0x801b, 0x001e, 0x0014, 0x8011,
120 0x8033, 0x0036, 0x003c, 0x8039, 0x0028, 0x802d, 0x8027, 0x0022,
121 0x8063, 0x0066, 0x006c, 0x8069, 0x0078, 0x807d, 0x8077, 0x0072,
122 0x0050, 0x8055, 0x805f, 0x005a, 0x804b, 0x004e, 0x0044, 0x8041,
123 0x80c3, 0x00c6, 0x00cc, 0x80c9, 0x00d8, 0x80dd, 0x80d7, 0x00d2,
124 0x00f0, 0x80f5, 0x80ff, 0x00fa, 0x80eb, 0x00ee, 0x00e4, 0x80e1,
125 0x00a0, 0x80a5, 0x80af, 0x00aa, 0x80bb, 0x00be, 0x00b4, 0x80b1,
126 0x8093, 0x0096, 0x009c, 0x8099, 0x0088, 0x808d, 0x8087, 0x0082,
127 0x8183, 0x0186, 0x018c, 0x8189, 0x0198, 0x819d, 0x8197, 0x0192,
128 0x01b0, 0x81b5, 0x81bf, 0x01ba, 0x81ab, 0x01ae, 0x01a4, 0x81a1,
129 0x01e0, 0x81e5, 0x81ef, 0x01ea, 0x81fb, 0x01fe, 0x01f4, 0x81f1,
130 0x81d3, 0x01d6, 0x01dc, 0x81d9, 0x01c8, 0x81cd, 0x81c7, 0x01c2,
131 0x0140, 0x8145, 0x814f, 0x014a, 0x815b, 0x015e, 0x0154, 0x8151,
132 0x8173, 0x0176, 0x017c, 0x8179, 0x0168, 0x816d, 0x8167, 0x0162,
133 0x8123, 0x0126, 0x012c, 0x8129, 0x0138, 0x813d, 0x8137, 0x0132,
134 0x0110, 0x8115, 0x811f, 0x011a, 0x810b, 0x010e, 0x0104, 0x8101,
135 0x8303, 0x0306, 0x030c, 0x8309, 0x0318, 0x831d, 0x8317, 0x0312,
136 0x0330, 0x8335, 0x833f, 0x033a, 0x832b, 0x032e, 0x0324, 0x8321,
137 0x0360, 0x8365, 0x836f, 0x036a, 0x837b, 0x037e, 0x0374, 0x8371,
138 0x8353, 0x0356, 0x035c, 0x8359, 0x0348, 0x834d, 0x8347, 0x0342,
139 0x03c0, 0x83c5, 0x83cf, 0x03ca, 0x83db, 0x03de, 0x03d4, 0x83d1,
140 0x83f3, 0x03f6, 0x03fc, 0x83f9, 0x03e8, 0x83ed, 0x83e7, 0x03e2,
141 0x83a3, 0x03a6, 0x03ac, 0x83a9, 0x03b8, 0x83bd, 0x83b7, 0x03b2,
142 0x0390, 0x8395, 0x839f, 0x039a, 0x838b, 0x038e, 0x0384, 0x8381,
143 0x0280, 0x8285, 0x828f, 0x028a, 0x829b, 0x029e, 0x0294, 0x8291,
144 0x82b3, 0x02b6, 0x02bc, 0x82b9, 0x02a8, 0x82ad, 0x82a7, 0x02a2,
145 0x82e3, 0x02e6, 0x02ec, 0x82e9, 0x02f8, 0x82fd, 0x82f7, 0x02f2,
146 0x02d0, 0x82d5, 0x82df, 0x02da, 0x82cb, 0x02ce, 0x02c4, 0x82c1,
147 0x8243, 0x0246, 0x024c, 0x8249, 0x0258, 0x825d, 0x8257, 0x0252,
148 0x0270, 0x8275, 0x827f, 0x027a, 0x826b, 0x026e, 0x0264, 0x8261,
149 0x0220, 0x8225, 0x822f, 0x022a, 0x823b, 0x023e, 0x0234, 0x8231,
150 0x8213, 0x0216, 0x021c, 0x8219, 0x0208, 0x820d, 0x8207, 0x0202
154 gst_flac_calculate_crc16 (const guint8 * data, guint length)
159 crc = ((crc << 8) ^ crc16_table[(crc >> 8) ^ *data]) & 0xffff;
169 PROP_CHECK_FRAME_CHECKSUMS
172 #define DEFAULT_CHECK_FRAME_CHECKSUMS FALSE
174 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
177 GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) true, "
178 "channels = (int) [ 1, 8 ], " "rate = (int) [ 1, 655350 ]")
181 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
184 GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) false")
187 static void gst_flac_parse_finalize (GObject * object);
188 static void gst_flac_parse_set_property (GObject * object, guint prop_id,
189 const GValue * value, GParamSpec * pspec);
190 static void gst_flac_parse_get_property (GObject * object, guint prop_id,
191 GValue * value, GParamSpec * pspec);
193 static gboolean gst_flac_parse_start (GstBaseParse * parse);
194 static gboolean gst_flac_parse_stop (GstBaseParse * parse);
195 static gboolean gst_flac_parse_check_valid_frame (GstBaseParse * parse,
196 GstBuffer * buffer, guint * framesize, gint * skipsize);
197 static GstFlowReturn gst_flac_parse_parse_frame (GstBaseParse * parse,
199 static gint gst_flac_parse_get_frame_overhead (GstBaseParse * parse,
202 GST_BOILERPLATE (GstFlacParse, gst_flac_parse, GstBaseParse,
203 GST_TYPE_BASE_PARSE);
206 gst_flac_parse_base_init (gpointer g_class)
208 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
210 gst_element_class_add_pad_template (element_class,
211 gst_static_pad_template_get (&src_factory));
212 gst_element_class_add_pad_template (element_class,
213 gst_static_pad_template_get (&sink_factory));
215 gst_element_class_set_details_simple (element_class, "FLAC audio parser",
216 "Codec/Parser/Audio",
217 "Parses audio with the FLAC lossless audio codec",
218 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
220 GST_DEBUG_CATEGORY_INIT (flacparse_debug, "flacparse", 0,
221 "Flac parser element");
225 gst_flac_parse_class_init (GstFlacParseClass * klass)
227 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
228 GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass);
230 gobject_class->finalize = gst_flac_parse_finalize;
231 gobject_class->set_property = gst_flac_parse_set_property;
232 gobject_class->get_property = gst_flac_parse_get_property;
234 g_object_class_install_property (gobject_class, PROP_CHECK_FRAME_CHECKSUMS,
235 g_param_spec_boolean ("check-frame-checksums", "Check Frame Checksums",
236 "Check the overall checksums of every frame",
237 DEFAULT_CHECK_FRAME_CHECKSUMS,
238 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 baseparse_class->start = GST_DEBUG_FUNCPTR (gst_flac_parse_start);
241 baseparse_class->stop = GST_DEBUG_FUNCPTR (gst_flac_parse_stop);
242 baseparse_class->check_valid_frame =
243 GST_DEBUG_FUNCPTR (gst_flac_parse_check_valid_frame);
244 baseparse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_flac_parse_parse_frame);
245 baseparse_class->get_frame_overhead =
246 GST_DEBUG_FUNCPTR (gst_flac_parse_get_frame_overhead);
250 gst_flac_parse_init (GstFlacParse * flacparse, GstFlacParseClass * klass)
252 flacparse->check_frame_checksums = DEFAULT_CHECK_FRAME_CHECKSUMS;
256 gst_flac_parse_set_property (GObject * object, guint prop_id,
257 const GValue * value, GParamSpec * pspec)
259 GstFlacParse *flacparse = GST_FLAC_PARSE (object);
262 case PROP_CHECK_FRAME_CHECKSUMS:
263 flacparse->check_frame_checksums = g_value_get_boolean (value);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 gst_flac_parse_get_property (GObject * object, guint prop_id,
273 GValue * value, GParamSpec * pspec)
275 GstFlacParse *flacparse = GST_FLAC_PARSE (object);
278 case PROP_CHECK_FRAME_CHECKSUMS:
279 g_value_set_boolean (value, flacparse->check_frame_checksums);
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 gst_flac_parse_finalize (GObject * object)
290 GstFlacParse *flacparse = GST_FLAC_PARSE (object);
292 if (flacparse->tags) {
293 gst_tag_list_free (flacparse->tags);
294 flacparse->tags = NULL;
298 g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL);
299 g_list_free (flacparse->headers);
300 flacparse->headers = NULL;
302 G_OBJECT_CLASS (parent_class)->finalize (object);
306 gst_flac_parse_start (GstBaseParse * parse)
308 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
310 flacparse->state = GST_FLAC_PARSE_STATE_INIT;
311 flacparse->min_blocksize = 0;
312 flacparse->max_blocksize = 0;
313 flacparse->min_framesize = 0;
314 flacparse->max_framesize = 0;
316 flacparse->upstream_length = -1;
318 flacparse->samplerate = 0;
319 flacparse->channels = 0;
321 flacparse->total_samples = 0;
323 flacparse->requested_frame_size = 0;
324 flacparse->offset = GST_CLOCK_TIME_NONE;
325 flacparse->blocking_strategy = 0;
326 flacparse->block_size = 0;
327 flacparse->sample_number = 0;
330 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4);
336 gst_flac_parse_stop (GstBaseParse * parse)
338 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
340 if (flacparse->tags) {
341 gst_tag_list_free (flacparse->tags);
342 flacparse->tags = NULL;
345 g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL);
346 g_list_free (flacparse->headers);
347 flacparse->headers = NULL;
353 gst_flac_parse_get_frame_size (GstFlacParse * flacparse, GstBuffer * buffer,
354 guint * framesize_ret)
356 GstBitReader reader = GST_BIT_READER_INIT_FROM_BUFFER (buffer);
360 guint8 channel_assignment = 0;
361 guint8 actual_crc, expected_crc;
363 /* Skip 14 bit sync code */
364 if (!gst_bit_reader_skip (&reader, 14))
368 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
373 /* 0 == fixed block size, 1 == variable block size */
374 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->blocking_strategy,
378 /* block size index, calculation of the real blocksize below */
379 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 4))
381 else if (flacparse->block_size == 0)
384 /* sample rate index, calculation of the real samplerate below */
385 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 4))
387 else if (samplerate == 0x0f)
390 /* channel assignment */
391 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 4)) {
393 } else if (tmp < 8) {
394 if (flacparse->channels && tmp + 1 != flacparse->channels)
397 flacparse->channels = tmp + 1;
398 } else if (tmp <= 10) {
399 if (flacparse->channels && 2 != flacparse->channels)
402 flacparse->channels = 2;
404 channel_assignment = 1; /* left-side */
406 channel_assignment = 2; /* right-side */
408 channel_assignment = 3; /* mid-side */
409 } else if (tmp > 10) {
413 /* bits per sample */
414 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 3)) {
416 } else if (tmp == 0x03 || tmp == 0x07) {
418 } else if (tmp == 0 && flacparse->bps == 0) {
419 goto need_streaminfo;
420 } else if (tmp == 0x01 && flacparse->bps != 8) {
421 if (flacparse->bps && flacparse->bps != 8)
425 } else if (tmp == 0x02 && flacparse->bps != 12) {
426 if (flacparse->bps && flacparse->bps != 12)
430 } else if (tmp == 0x04 && flacparse->bps != 16) {
431 if (flacparse->bps && flacparse->bps != 16)
435 } else if (tmp == 0x05 && flacparse->bps != 20) {
436 if (flacparse->bps && flacparse->bps != 20)
440 } else if (tmp == 0x06 && flacparse->bps != 24) {
441 if (flacparse->bps && flacparse->bps != 24)
447 /* reserved, must be 0 */
448 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
453 /* read "utf8" encoded sample/frame number */
459 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
467 flacparse->sample_number = 0;
469 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 7))
471 flacparse->sample_number = tmp;
472 } else if ((flacparse->blocking_strategy == 0 && len > 6) ||
473 (flacparse->blocking_strategy == 1 && len > 7)) {
476 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 8 - len - 1))
479 flacparse->sample_number = tmp;
483 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 2))
485 else if (tmp != 0x02)
488 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 6))
490 flacparse->sample_number <<= 6;
491 flacparse->sample_number |= tmp;
497 /* calculate real blocksize from the blocksize index */
498 if (flacparse->block_size == 1)
499 flacparse->block_size = 192;
500 else if (flacparse->block_size <= 5)
501 flacparse->block_size = 576 * (1 << (flacparse->block_size - 2));
502 else if (flacparse->block_size <= 15)
503 flacparse->block_size = 256 * (1 << (flacparse->block_size - 8));
504 else if (flacparse->block_size == 6) {
505 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 8))
507 flacparse->block_size++;
508 } else if (flacparse->block_size == 7) {
509 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->block_size, 16))
511 flacparse->block_size++;
514 /* calculate the real samplerate from the samplerate index */
515 if (samplerate == 0 && flacparse->samplerate == 0) {
516 goto need_streaminfo;
517 } else if (samplerate == 1) {
518 if (flacparse->samplerate == 0)
519 flacparse->samplerate = 88200;
520 else if (flacparse->samplerate != 88200)
522 } else if (samplerate == 2) {
523 if (flacparse->samplerate == 0)
524 flacparse->samplerate = 176400;
525 else if (flacparse->samplerate != 176400)
527 } else if (samplerate == 3) {
528 if (flacparse->samplerate == 0)
529 flacparse->samplerate = 192000;
530 else if (flacparse->samplerate != 192000)
532 } else if (samplerate == 4) {
533 if (flacparse->samplerate == 0)
534 flacparse->samplerate = 8000;
535 else if (flacparse->samplerate != 8000)
537 } else if (samplerate == 5) {
538 if (flacparse->samplerate == 0)
539 flacparse->samplerate = 16000;
540 else if (flacparse->samplerate != 16000)
542 } else if (samplerate == 6) {
543 if (flacparse->samplerate == 0)
544 flacparse->samplerate = 22050;
545 else if (flacparse->samplerate != 22050)
547 } else if (samplerate == 7) {
548 if (flacparse->samplerate == 0)
549 flacparse->samplerate = 24000;
550 else if (flacparse->samplerate != 24000)
552 } else if (samplerate == 8) {
553 if (flacparse->samplerate == 0)
554 flacparse->samplerate = 32000;
555 else if (flacparse->samplerate != 32000)
557 } else if (samplerate == 9) {
558 if (flacparse->samplerate == 0)
559 flacparse->samplerate = 44100;
560 else if (flacparse->samplerate != 44100)
562 } else if (samplerate == 10) {
563 if (flacparse->samplerate == 0)
564 flacparse->samplerate = 48000;
565 else if (flacparse->samplerate != 48000)
567 } else if (samplerate == 11) {
568 if (flacparse->samplerate == 0)
569 flacparse->samplerate = 96000;
570 else if (flacparse->samplerate != 96000)
572 } else if (samplerate == 12) {
573 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 8))
576 if (flacparse->samplerate == 0)
577 flacparse->samplerate = samplerate;
578 else if (flacparse->samplerate != samplerate)
580 } else if (samplerate == 13) {
581 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 16))
583 if (flacparse->samplerate == 0)
584 flacparse->samplerate = samplerate;
585 else if (flacparse->samplerate != samplerate)
587 } else if (samplerate == 14) {
588 if (!gst_bit_reader_get_bits_uint16 (&reader, &samplerate, 16))
591 if (flacparse->samplerate == 0)
592 flacparse->samplerate = samplerate;
593 else if (flacparse->samplerate != samplerate)
597 /* check crc-8 for the header */
598 if (!gst_bit_reader_get_bits_uint8 (&reader, &expected_crc, 8))
602 gst_flac_calculate_crc8 (GST_BUFFER_DATA (buffer),
603 (gst_bit_reader_get_pos (&reader) / 8) - 1);
604 if (actual_crc != expected_crc)
607 /* parse subframes, one subframe per channel */
608 for (i = 0; i < flacparse->channels; i++) {
612 cur_bps = flacparse->bps;
614 /* for mid/side, left/side, right/side the "difference" channel
615 * needs and additional bit */
616 if (i == 0 && channel_assignment == 2)
618 else if (i == 1 && (channel_assignment == 1 || channel_assignment == 3))
622 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
628 if (!gst_bit_reader_get_bits_uint8 (&reader, &sf_type, 6))
630 else if (((sf_type & 0xfe) == 0x02) ||
631 ((sf_type & 0xfc) == 0x04) ||
632 ((sf_type & 0xf8) == 0x08 && (sf_type & 0x07) > 4) ||
633 ((sf_type & 0xf0) == 0x10))
636 /* wasted bits per sample, if 1 the value follows unary coded */
637 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1)) {
639 } else if (tmp != 0) {
644 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
652 /* subframe type: constant */
653 if (sf_type == 0x00) {
654 if (!gst_bit_reader_skip (&reader, cur_bps))
656 /* subframe type: verbatim */
657 } else if (sf_type == 0x01) {
658 if (!gst_bit_reader_skip (&reader, cur_bps * flacparse->block_size))
660 /* subframe type: LPC or fixed */
662 guint8 residual_type;
664 guint16 partition_order;
667 /* Skip warm-up samples for fixed subframe and calculate order */
668 if ((sf_type & 0xf8) == 0x08) {
669 order = sf_type & 0x07;
671 g_assert (order <= 4);
673 if (!gst_bit_reader_skip (&reader, cur_bps * order))
675 /* Skip warm-up samples for LPC subframe, get parameters and calculate order */
676 } else if ((sf_type & 0xe0) == 0x20) {
679 order = (sf_type & 0x1f) + 1;
681 /* warm-up samples */
682 if (!gst_bit_reader_skip (&reader, cur_bps * order))
685 /* LPC coefficient precision */
686 if (!gst_bit_reader_get_bits_uint8 (&reader, &prec, 4))
688 else if (prec == 0x0f)
692 /* LPC coefficient shift */
693 if (!gst_bit_reader_skip (&reader, 5))
696 /* LPC coefficients */
697 if (!gst_bit_reader_skip (&reader, order * prec))
700 g_assert_not_reached ();
703 /* residual type: 0 == rice, 1 == rice2 */
704 if (!gst_bit_reader_get_bits_uint8 (&reader, &residual_type, 2))
707 if (residual_type & 0x02)
710 /* partition order */
711 if (!gst_bit_reader_get_bits_uint16 (&reader, &partition_order, 4))
714 partition_order = 1 << partition_order;
716 /* 2^partition_order partitions */
717 for (j = 0; j < partition_order; j++) {
719 guint8 rice_parameter;
721 /* calculate number of samples for the current partition */
722 if (partition_order == 1) {
723 samples = flacparse->block_size - order;
725 samples = flacparse->block_size / partition_order;
727 samples = flacparse->block_size / partition_order - order;
731 if (!gst_bit_reader_get_bits_uint8 (&reader, &rice_parameter,
732 (residual_type == 0) ? 4 : 5))
735 /* if rice parameter has all bits set the samples follow unencoded with the number of bits
736 * per sample in the following 5 bits */
737 if ((residual_type == 0 && rice_parameter == 0x0f)
738 || (residual_type == 1 && rice_parameter == 0x1f)) {
739 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 5))
741 if (!gst_bit_reader_skip (&reader, tmp * samples))
746 /* read the rice encoded samples */
747 for (k = 0; k < samples; k++) {
750 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 1))
753 if (!gst_bit_reader_skip (&reader, rice_parameter))
761 /* zero padding to byte alignment */
762 gst_bit_reader_skip_to_byte (&reader);
764 if (flacparse->check_frame_checksums) {
765 guint16 actual_crc16, expected_crc16;
767 if (!gst_bit_reader_get_bits_uint16 (&reader, &expected_crc16, 16))
771 gst_flac_calculate_crc16 (GST_BUFFER_DATA (buffer),
772 (gst_bit_reader_get_pos (&reader) / 8) - 2);
773 if (actual_crc16 != expected_crc16)
776 /* Skip crc-16 for the complete frame */
777 if (!gst_bit_reader_skip (&reader, 16))
781 *framesize_ret = gst_bit_reader_get_pos (&reader) / 8;
783 GST_DEBUG_OBJECT (flacparse, "Parsed frame at offset %" G_GUINT64_FORMAT ":\n"
786 "Sample/Frame number: %" G_GUINT64_FORMAT,
787 flacparse->offset, *framesize_ret,
788 flacparse->block_size, flacparse->sample_number);
796 /* not enough, if that was all available, give up on frame */
797 if (G_UNLIKELY (gst_base_parse_get_drain (GST_BASE_PARSE_CAST (flacparse))))
799 /* otherwise, ask for some more */
800 max = flacparse->max_framesize;
803 flacparse->requested_frame_size
804 = MIN (GST_BUFFER_SIZE (buffer) + 4096, max);
805 if (flacparse->requested_frame_size > GST_BUFFER_SIZE (buffer)) {
806 GST_DEBUG_OBJECT (flacparse, "Requesting %u bytes",
807 flacparse->requested_frame_size);
808 return flacparse->requested_frame_size;
810 GST_DEBUG_OBJECT (flacparse, "Giving up on invalid frame (%d bytes)",
811 GST_BUFFER_SIZE (buffer));
818 GST_ERROR_OBJECT (flacparse, "Need STREAMINFO");
824 GST_WARNING_OBJECT (flacparse, "EOS");
830 GST_WARNING_OBJECT (flacparse, "Invalid frame");
836 gst_flac_parse_check_valid_frame (GstBaseParse * parse, GstBuffer * buffer,
837 guint * framesize, gint * skipsize)
839 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
840 const guint8 *data = GST_BUFFER_DATA (buffer);
842 if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < 4))
845 if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) {
846 if (memcmp (GST_BUFFER_DATA (buffer), "fLaC", 4) == 0) {
847 GST_DEBUG_OBJECT (flacparse, "fLaC marker found");
850 } else if (data[0] == 0xff && (data[1] >> 2) == 0x3e) {
851 GST_DEBUG_OBJECT (flacparse, "Found headerless FLAC");
852 /* Minimal size of a frame header */
853 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 16);
854 flacparse->requested_frame_size = 16;
855 flacparse->state = GST_FLAC_PARSE_STATE_GENERATE_HEADERS;
859 GST_DEBUG_OBJECT (flacparse, "fLaC marker not found");
862 } else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) {
863 guint size = 4 + ((data[1] << 16) | (data[2] << 8) | (data[3]));
865 GST_DEBUG_OBJECT (flacparse, "Found metadata block of size %u", size);
869 if (data[0] == 0xff && (data[1] >> 2) == 0x3e) {
872 flacparse->offset = GST_BUFFER_OFFSET (buffer);
873 flacparse->blocking_strategy = 0;
874 flacparse->block_size = 0;
875 flacparse->sample_number = 0;
877 GST_DEBUG_OBJECT (flacparse, "Found sync code");
878 ret = gst_flac_parse_get_frame_size (flacparse, buffer, framesize);
881 /* if not in sync, also check for next frame header */
882 if (!gst_base_parse_get_sync (parse) &&
883 !gst_base_parse_get_drain (parse)) {
884 GST_DEBUG_OBJECT (flacparse, "Resyncing; checking next sync code");
885 if (GST_BUFFER_SIZE (buffer) >= ret + 2) {
886 if (data[ret] == 0xff && (data[ret + 1] >> 2) == 0x3e) {
887 GST_DEBUG_OBJECT (flacparse, "Found next sync code");
890 GST_DEBUG_OBJECT (flacparse,
891 "No next sync code, rejecting frame");
895 /* request more data for next sync */
896 GST_DEBUG_OBJECT (flacparse, "... but not enough data");
898 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), ret);
899 flacparse->requested_frame_size = ret;
904 } else if (ret == -1) {
906 } else if (ret == -2) {
907 GST_ELEMENT_ERROR (flacparse, STREAM, FORMAT, (NULL),
908 ("Need STREAMINFO for parsing"));
910 } else if (ret > 0) {
912 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), ret);
913 flacparse->requested_frame_size = ret;
917 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
920 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xfffc0000, 0xfff80000,
921 0, GST_BUFFER_SIZE (buffer));
924 GST_DEBUG_OBJECT (parse, "Possible sync at buffer offset %d", off);
928 GST_DEBUG_OBJECT (flacparse, "Sync code not found");
929 *skipsize = GST_BUFFER_SIZE (buffer) - 3;
939 gst_flac_parse_handle_streaminfo (GstFlacParse * flacparse, GstBuffer * buffer)
941 GstBitReader reader = GST_BIT_READER_INIT_FROM_BUFFER (buffer);
943 if (GST_BUFFER_SIZE (buffer) != 4 + 34) {
944 GST_ERROR_OBJECT (flacparse, "Invalid metablock size for STREAMINFO: %u",
945 GST_BUFFER_SIZE (buffer));
949 /* Skip metadata block header */
950 gst_bit_reader_skip (&reader, 32);
952 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->min_blocksize, 16))
954 if (flacparse->min_blocksize < 16) {
955 GST_ERROR_OBJECT (flacparse, "Invalid minimum block size: %u",
956 flacparse->min_blocksize);
960 if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->max_blocksize, 16))
962 if (flacparse->max_blocksize < 16) {
963 GST_ERROR_OBJECT (flacparse, "Invalid maximum block size: %u",
964 flacparse->max_blocksize);
968 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->min_framesize, 24))
970 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->max_framesize, 24))
973 if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->samplerate, 20))
975 if (flacparse->samplerate == 0) {
976 GST_ERROR_OBJECT (flacparse, "Invalid sample rate 0");
980 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->channels, 3))
982 flacparse->channels++;
983 if (flacparse->channels > 8) {
984 GST_ERROR_OBJECT (flacparse, "Invalid number of channels %u",
985 flacparse->channels);
989 if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->bps, 5))
993 if (!gst_bit_reader_get_bits_uint64 (&reader, &flacparse->total_samples, 36))
995 if (flacparse->total_samples)
996 gst_base_parse_set_duration (GST_BASE_PARSE (flacparse), GST_FORMAT_TIME,
997 GST_FRAMES_TO_CLOCK_TIME (flacparse->total_samples,
998 flacparse->samplerate));
1000 GST_DEBUG_OBJECT (flacparse, "STREAMINFO:\n"
1001 "\tmin/max blocksize: %u/%u,\n"
1002 "\tmin/max framesize: %u/%u,\n"
1003 "\tsamplerate: %u,\n"
1005 "\tbits per sample: %u,\n"
1006 "\ttotal samples: %" G_GUINT64_FORMAT,
1007 flacparse->min_blocksize, flacparse->max_blocksize,
1008 flacparse->min_framesize, flacparse->max_framesize,
1009 flacparse->samplerate,
1010 flacparse->channels, flacparse->bps, flacparse->total_samples);
1015 GST_ERROR_OBJECT (flacparse, "Failed to read data");
1020 gst_flac_parse_handle_vorbiscomment (GstFlacParse * flacparse,
1023 flacparse->tags = gst_tag_list_from_vorbiscomment_buffer (buffer,
1024 GST_BUFFER_DATA (buffer), 4, NULL);
1026 if (flacparse->tags == NULL) {
1027 GST_ERROR_OBJECT (flacparse, "Invalid vorbiscomment block");
1028 } else if (gst_tag_list_is_empty (flacparse->tags)) {
1029 gst_tag_list_free (flacparse->tags);
1030 flacparse->tags = NULL;
1037 gst_flac_parse_handle_picture (GstFlacParse * flacparse, GstBuffer * buffer)
1039 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
1040 const guint8 *data = GST_BUFFER_DATA (buffer);
1041 guint32 img_len = 0, img_type = 0;
1042 guint32 img_mimetype_len = 0, img_description_len = 0;
1044 if (!gst_byte_reader_get_uint32_be (&reader, &img_type))
1047 if (!gst_byte_reader_get_uint32_be (&reader, &img_mimetype_len))
1049 if (!gst_byte_reader_skip (&reader, img_mimetype_len))
1052 if (!gst_byte_reader_get_uint32_be (&reader, &img_description_len))
1054 if (!gst_byte_reader_skip (&reader, img_description_len))
1057 if (!gst_byte_reader_skip (&reader, 4 * 4))
1060 if (!gst_byte_reader_get_uint32_be (&reader, &img_len))
1063 if (!flacparse->tags)
1064 flacparse->tags = gst_tag_list_new ();
1066 gst_tag_list_add_id3_image (flacparse->tags,
1067 data + gst_byte_reader_get_pos (&reader), img_len, img_type);
1069 if (gst_tag_list_is_empty (flacparse->tags)) {
1070 gst_tag_list_free (flacparse->tags);
1071 flacparse->tags = NULL;
1077 GST_ERROR_OBJECT (flacparse, "Error reading data");
1082 _value_array_append_buffer (GValue * array_val, GstBuffer * buf)
1084 GValue value = { 0, };
1086 g_value_init (&value, GST_TYPE_BUFFER);
1087 /* copy buffer to avoid problems with circular refcounts */
1088 buf = gst_buffer_copy (buf);
1089 /* again, for good measure */
1090 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
1091 gst_value_set_buffer (&value, buf);
1092 gst_buffer_unref (buf);
1093 gst_value_array_append_value (array_val, &value);
1094 g_value_unset (&value);
1098 gst_flac_parse_handle_headers (GstFlacParse * flacparse)
1100 GstBuffer *vorbiscomment = NULL;
1101 GstBuffer *streaminfo = NULL;
1102 GstBuffer *marker = NULL;
1103 GValue array = { 0, };
1107 caps = gst_caps_new_simple ("audio/x-flac",
1108 "channels", G_TYPE_INT, flacparse->channels,
1109 "rate", G_TYPE_INT, flacparse->samplerate, NULL);
1111 if (!flacparse->headers)
1114 for (l = flacparse->headers; l; l = l->next) {
1115 GstBuffer *header = l->data;
1116 const guint8 *data = GST_BUFFER_DATA (header);
1117 guint size = GST_BUFFER_SIZE (header);
1119 GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_IN_CAPS);
1121 if (size == 4 && memcmp (data, "fLaC", 4) == 0) {
1123 } else if (size > 1 && (data[0] & 0x7f) == 0) {
1124 streaminfo = header;
1125 } else if (size > 1 && (data[0] & 0x7f) == 4) {
1126 vorbiscomment = header;
1130 if (marker == NULL || streaminfo == NULL || vorbiscomment == NULL) {
1131 GST_WARNING_OBJECT (flacparse,
1132 "missing header %p %p %p, muxing into container "
1133 "formats may be broken", marker, streaminfo, vorbiscomment);
1137 g_value_init (&array, GST_TYPE_ARRAY);
1139 /* add marker including STREAMINFO header */
1144 /* minus one for the marker that is merged with streaminfo here */
1145 num = g_list_length (flacparse->headers) - 1;
1147 buf = gst_buffer_new_and_alloc (13 + GST_BUFFER_SIZE (streaminfo));
1148 GST_BUFFER_DATA (buf)[0] = 0x7f;
1149 memcpy (GST_BUFFER_DATA (buf) + 1, "FLAC", 4);
1150 GST_BUFFER_DATA (buf)[5] = 0x01; /* mapping version major */
1151 GST_BUFFER_DATA (buf)[6] = 0x00; /* mapping version minor */
1152 GST_BUFFER_DATA (buf)[7] = (num & 0xFF00) >> 8;
1153 GST_BUFFER_DATA (buf)[8] = (num & 0x00FF) >> 0;
1154 memcpy (GST_BUFFER_DATA (buf) + 9, "fLaC", 4);
1155 memcpy (GST_BUFFER_DATA (buf) + 13, GST_BUFFER_DATA (streaminfo),
1156 GST_BUFFER_SIZE (streaminfo));
1157 _value_array_append_buffer (&array, buf);
1158 gst_buffer_unref (buf);
1161 /* add VORBISCOMMENT header */
1162 _value_array_append_buffer (&array, vorbiscomment);
1164 /* add other headers, if there are any */
1165 for (l = flacparse->headers; l; l = l->next) {
1166 if (GST_BUFFER_CAST (l->data) != marker &&
1167 GST_BUFFER_CAST (l->data) != streaminfo &&
1168 GST_BUFFER_CAST (l->data) != vorbiscomment) {
1169 _value_array_append_buffer (&array, GST_BUFFER_CAST (l->data));
1173 gst_structure_set_value (gst_caps_get_structure (caps, 0),
1174 "streamheader", &array);
1175 g_value_unset (&array);
1179 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse)), caps);
1180 gst_caps_unref (caps);
1182 /* push header buffers; update caps, so when we push the first buffer the
1183 * negotiated caps will change to caps that include the streamheader field */
1184 for (l = flacparse->headers; l != NULL; l = l->next) {
1185 GstBuffer *buf = GST_BUFFER (l->data);
1189 buf = gst_buffer_make_metadata_writable (buf);
1190 gst_buffer_set_caps (buf,
1191 GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse))));
1193 ret = gst_base_parse_push_buffer (GST_BASE_PARSE (flacparse), buf);
1194 if (ret != GST_FLOW_OK)
1197 g_list_free (flacparse->headers);
1198 flacparse->headers = NULL;
1201 if (flacparse->tags)
1202 gst_element_found_tags (GST_ELEMENT (flacparse),
1203 gst_tag_list_copy (flacparse->tags));
1209 gst_flac_parse_generate_headers (GstFlacParse * flacparse)
1211 GstBuffer *marker, *streaminfo, *vorbiscomment;
1214 marker = gst_buffer_new_and_alloc (4);
1215 memcpy (GST_BUFFER_DATA (marker), "fLaC", 4);
1216 GST_BUFFER_TIMESTAMP (marker) = GST_CLOCK_TIME_NONE;
1217 GST_BUFFER_DURATION (marker) = GST_CLOCK_TIME_NONE;
1218 GST_BUFFER_OFFSET (marker) = 0;
1219 GST_BUFFER_OFFSET_END (marker) = 0;
1220 flacparse->headers = g_list_append (flacparse->headers, marker);
1222 streaminfo = gst_buffer_new_and_alloc (4 + 34);
1223 data = GST_BUFFER_DATA (streaminfo);
1224 memset (data, 0, 4 + 34);
1226 /* metadata block header */
1227 data[0] = 0x00; /* is_last = 0; type = 0; */
1228 data[1] = 0x00; /* length = 34; */
1234 data[4] = (flacparse->block_size >> 8) & 0xff; /* min blocksize = blocksize; */
1235 data[5] = (flacparse->block_size) & 0xff;
1236 data[6] = (flacparse->block_size >> 8) & 0xff; /* max blocksize = blocksize; */
1237 data[7] = (flacparse->block_size) & 0xff;
1239 data[8] = 0x00; /* min framesize = 0; */
1242 data[11] = 0x00; /* max framesize = 0; */
1246 data[14] = (flacparse->samplerate >> 12) & 0xff;
1247 data[15] = (flacparse->samplerate >> 4) & 0xff;
1248 data[16] = (flacparse->samplerate >> 0) & 0xf0;
1250 data[16] |= (flacparse->channels - 1) << 1;
1252 data[16] |= ((flacparse->bps - 1) >> 4) & 0x01;
1253 data[17] = (((flacparse->bps - 1)) & 0x0f) << 4;
1257 GstFormat fmt = GST_FORMAT_TIME;
1259 if (gst_pad_query_peer_duration (GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE
1260 (flacparse)), &fmt, &duration) && fmt == GST_FORMAT_TIME) {
1261 duration = GST_CLOCK_TIME_TO_FRAMES (duration, flacparse->samplerate);
1263 data[17] |= (duration >> 32) & 0xff;
1264 data[18] |= (duration >> 24) & 0xff;
1265 data[19] |= (duration >> 16) & 0xff;
1266 data[20] |= (duration >> 8) & 0xff;
1267 data[21] |= (duration >> 0) & 0xff;
1272 GST_BUFFER_TIMESTAMP (streaminfo) = GST_CLOCK_TIME_NONE;
1273 GST_BUFFER_DURATION (streaminfo) = GST_CLOCK_TIME_NONE;
1274 GST_BUFFER_OFFSET (streaminfo) = 0;
1275 GST_BUFFER_OFFSET_END (streaminfo) = 0;
1276 flacparse->headers = g_list_append (flacparse->headers, streaminfo);
1278 /* empty vorbiscomment */
1280 GstTagList *taglist = gst_tag_list_new ();
1284 header[0] = 0x84; /* is_last = 1; type = 4; */
1287 gst_tag_list_to_vorbiscomment_buffer (taglist, header, sizeof (header),
1289 gst_tag_list_free (taglist);
1291 /* Get rid of framing bit */
1292 if (GST_BUFFER_DATA (vorbiscomment)[GST_BUFFER_SIZE (vorbiscomment) - 1] ==
1297 gst_buffer_create_sub (vorbiscomment, 0,
1298 GST_BUFFER_SIZE (vorbiscomment) - 1);
1299 gst_buffer_unref (vorbiscomment);
1300 vorbiscomment = sub;
1303 size = GST_BUFFER_SIZE (vorbiscomment) - 4;
1304 GST_BUFFER_DATA (vorbiscomment)[1] = ((size & 0xFF0000) >> 16);
1305 GST_BUFFER_DATA (vorbiscomment)[2] = ((size & 0x00FF00) >> 8);
1306 GST_BUFFER_DATA (vorbiscomment)[3] = (size & 0x0000FF);
1308 GST_BUFFER_TIMESTAMP (vorbiscomment) = GST_CLOCK_TIME_NONE;
1309 GST_BUFFER_DURATION (vorbiscomment) = GST_CLOCK_TIME_NONE;
1310 GST_BUFFER_OFFSET (vorbiscomment) = 0;
1311 GST_BUFFER_OFFSET_END (vorbiscomment) = 0;
1312 flacparse->headers = g_list_append (flacparse->headers, vorbiscomment);
1318 static GstFlowReturn
1319 gst_flac_parse_parse_frame (GstBaseParse * parse, GstBuffer * buffer)
1321 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
1322 const guint8 *data = GST_BUFFER_DATA (buffer);
1324 if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) {
1325 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
1326 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1327 GST_BUFFER_OFFSET (buffer) = 0;
1328 GST_BUFFER_OFFSET_END (buffer) = 0;
1330 /* 32 bits metadata block */
1331 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4);
1332 flacparse->state = GST_FLAC_PARSE_STATE_HEADERS;
1334 flacparse->headers =
1335 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1337 return GST_BASE_PARSE_FLOW_DROPPED;
1338 } else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) {
1339 gboolean is_last = ((data[0] & 0x80) == 0x80);
1340 guint type = (data[0] & 0x7F);
1343 GST_WARNING_OBJECT (flacparse, "Invalid metadata block type");
1344 return GST_BASE_PARSE_FLOW_DROPPED;
1347 GST_DEBUG_OBJECT (flacparse, "Handling metadata block of type %u", type);
1350 case 0: /* STREAMINFO */
1351 if (!gst_flac_parse_handle_streaminfo (flacparse, buffer))
1352 return GST_FLOW_ERROR;
1354 case 3: /* SEEKTABLE */
1355 /* TODO: handle seektables */
1357 case 4: /* VORBIS_COMMENT */
1358 if (!gst_flac_parse_handle_vorbiscomment (flacparse, buffer))
1359 return GST_FLOW_ERROR;
1361 case 6: /* PICTURE */
1362 if (!gst_flac_parse_handle_picture (flacparse, buffer))
1363 return GST_FLOW_ERROR;
1365 case 1: /* PADDING */
1366 case 2: /* APPLICATION */
1367 case 5: /* CUESHEET */
1368 default: /* RESERVED */
1372 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
1373 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
1374 GST_BUFFER_OFFSET (buffer) = 0;
1375 GST_BUFFER_OFFSET_END (buffer) = 0;
1378 flacparse->headers =
1379 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1381 if (!gst_flac_parse_handle_headers (flacparse))
1382 return GST_FLOW_ERROR;
1384 /* Minimal size of a frame header */
1385 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (16,
1386 flacparse->min_framesize));
1387 flacparse->requested_frame_size = MAX (16, flacparse->min_framesize);
1388 flacparse->state = GST_FLAC_PARSE_STATE_DATA;
1390 /* DROPPED because we pushed all headers manually already */
1391 return GST_BASE_PARSE_FLOW_DROPPED;
1393 flacparse->headers =
1394 g_list_append (flacparse->headers, gst_buffer_ref (buffer));
1395 return GST_BASE_PARSE_FLOW_DROPPED;
1398 if (flacparse->offset != GST_BUFFER_OFFSET (buffer)) {
1402 flacparse->offset = GST_BUFFER_OFFSET (buffer);
1403 ret = gst_flac_parse_get_frame_size (flacparse, buffer, &framesize);
1405 GST_ERROR_OBJECT (flacparse,
1406 "Baseclass didn't provide a complete frame");
1407 return GST_FLOW_ERROR;
1411 if (flacparse->block_size == 0) {
1412 GST_ERROR_OBJECT (flacparse, "Unparsed frame");
1413 return GST_FLOW_ERROR;
1416 if (flacparse->state == GST_FLAC_PARSE_STATE_GENERATE_HEADERS) {
1417 if (flacparse->blocking_strategy == 1) {
1418 GST_WARNING_OBJECT (flacparse,
1419 "Generating headers for variable blocksize streams not supported");
1421 if (!gst_flac_parse_handle_headers (flacparse))
1422 return GST_FLOW_ERROR;
1424 GST_DEBUG_OBJECT (flacparse, "Generating headers");
1426 if (!gst_flac_parse_generate_headers (flacparse))
1427 return GST_FLOW_ERROR;
1429 if (!gst_flac_parse_handle_headers (flacparse))
1430 return GST_FLOW_ERROR;
1432 flacparse->state = GST_FLAC_PARSE_STATE_DATA;
1435 /* also cater for oggmux metadata */
1436 if (flacparse->blocking_strategy == 0) {
1437 GST_BUFFER_TIMESTAMP (buffer) =
1438 gst_util_uint64_scale (flacparse->sample_number,
1439 flacparse->block_size * GST_SECOND, flacparse->samplerate);
1440 GST_BUFFER_OFFSET_END (buffer) =
1441 flacparse->sample_number * flacparse->block_size +
1442 flacparse->block_size;
1444 GST_BUFFER_TIMESTAMP (buffer) =
1445 gst_util_uint64_scale (flacparse->sample_number, GST_SECOND,
1446 flacparse->samplerate);
1447 GST_BUFFER_OFFSET_END (buffer) =
1448 flacparse->sample_number + flacparse->block_size;
1450 GST_BUFFER_DURATION (buffer) =
1451 GST_FRAMES_TO_CLOCK_TIME (flacparse->block_size, flacparse->samplerate);
1452 GST_BUFFER_OFFSET (buffer) =
1453 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1455 /* Minimal size of a frame header */
1456 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (16,
1457 flacparse->min_framesize));
1458 flacparse->requested_frame_size = MAX (16, flacparse->min_framesize);
1460 flacparse->offset = -1;
1461 flacparse->blocking_strategy = 0;
1462 flacparse->block_size = 0;
1463 flacparse->sample_number = 0;
1469 gst_flac_parse_get_frame_overhead (GstBaseParse * parse, GstBuffer * buffer)
1471 GstFlacParse *flacparse = GST_FLAC_PARSE (parse);
1473 if (flacparse->state != GST_FLAC_PARSE_STATE_DATA)
1476 /* To simplify, we just assume that it's a fixed size header and ignore
1477 * subframe headers. The first could lead us to being off by 88 bits and
1478 * the second even less, so the total inaccuracy is negligible. */