| 1 |
/* GStreamer |
| 2 |
* |
| 3 |
* Copyright (C) 2008 Sebastian Dröge <[email protected]>. |
| 4 |
* Copyright (C) 2009 Mark Nauwelaerts <[email protected]> |
| 5 |
* Copyright (C) 2009 Nokia Corporation. All rights reserved. |
| 6 |
* Contact: Stefan Kost <[email protected]> |
| 7 |
* |
| 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. |
| 12 |
* |
| 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. |
| 17 |
* |
| 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., 51 Franklin St, Fifth Floor, |
| 21 |
* Boston, MA 02110-1301, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
/** |
| 25 |
* SECTION:element-flacparse |
| 26 |
* @title: flacparse |
| 27 |
* @see_also: flacdec, oggdemux, vorbisparse |
| 28 |
* |
| 29 |
* The flacparse element will parse the header packets of the FLAC |
| 30 |
* stream and put them as the streamheader in the caps. This is used in the |
| 31 |
* multifdsink case where you want to stream live FLAC streams to multiple |
| 32 |
* clients, each client has to receive the streamheaders first before they can |
| 33 |
* consume the FLAC packets. |
| 34 |
* |
| 35 |
* This element also makes sure that the buffers that it pushes out are properly |
| 36 |
* timestamped and that their offset and offset_end are set. The buffers that |
| 37 |
* flacparse outputs have all of the metadata that oggmux expects to receive, |
| 38 |
* which allows you to (for example) remux an ogg/flac or convert a native FLAC |
| 39 |
* format file to an ogg bitstream. |
| 40 |
* |
| 41 |
* ## Example pipelines |
| 42 |
* |[ |
| 43 |
* gst-launch-1.0 -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. |
| 48 |
* |
| 49 |
*/ |
| 50 |
|
| 51 |
#ifdef HAVE_CONFIG_H |
| 52 |
#include "config.h" |
| 53 |
#endif |
| 54 |
|
| 55 |
#include "gstaudioparserselements.h" |
| 56 |
#include "gstflacparse.h" |
| 57 |
|
| 58 |
#include <string.h> |
| 59 |
#include <gst/tag/tag.h> |
| 60 |
#include <gst/audio/audio.h> |
| 61 |
#include <gst/base/base.h> |
| 62 |
#include <gst/pbutils/pbutils.h> |
| 63 |
|
| 64 |
GST_DEBUG_CATEGORY_STATIC (flacparse_debug); |
| 65 |
#define GST_CAT_DEFAULT flacparse_debug |
| 66 |
|
| 67 |
typedef struct |
| 68 |
{ |
| 69 |
guint16 block_size; |
| 70 |
guint32 samplerate; |
| 71 |
guint8 bps; |
| 72 |
guint8 blocking_strategy; |
| 73 |
guint8 channels; |
| 74 |
guint64 sample_number; |
| 75 |
} FlacFrameHeader; |
| 76 |
|
| 77 |
/* CRC-8, poly = x^8 + x^2 + x^1 + x^0, init = 0 */ |
| 78 |
static const guint8 crc8_table[256] = { |
| 79 |
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, |
| 80 |
0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, |
| 81 |
0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, |
| 82 |
0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, |
| 83 |
0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, |
| 84 |
0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, |
| 85 |
0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, |
| 86 |
0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, |
| 87 |
0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, |
| 88 |
0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, |
| 89 |
0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, |
| 90 |
0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, |
| 91 |
0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, |
| 92 |
0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, |
| 93 |
0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, |
| 94 |
0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, |
| 95 |
0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, |
| 96 |
0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, |
| 97 |
0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, |
| 98 |
0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, |
| 99 |
0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, |
| 100 |
0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, |
| 101 |
0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, |
| 102 |
0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, |
| 103 |
0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, |
| 104 |
0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, |
| 105 |
0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, |
| 106 |
0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, |
| 107 |
0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, |
| 108 |
0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, |
| 109 |
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, |
| 110 |
0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 |
| 111 |
}; |
| 112 |
|
| 113 |
static guint8 |
| 114 |
gst_flac_calculate_crc8 (const guint8 * data, guint length) |
| 115 |
{ |
| 116 |
guint8 crc = 0; |
| 117 |
|
| 118 |
while (length--) { |
| 119 |
crc = crc8_table[crc ^ *data]; |
| 120 |
++data; |
| 121 |
} |
| 122 |
|
| 123 |
return crc; |
| 124 |
} |
| 125 |
|
| 126 |
/* CRC-16, poly = x^16 + x^15 + x^2 + x^0, init = 0 */ |
| 127 |
static const guint16 crc16_table[256] = { |
| 128 |
0x0000, 0x8005, 0x800f, 0x000a, 0x801b, 0x001e, 0x0014, 0x8011, |
| 129 |
0x8033, 0x0036, 0x003c, 0x8039, 0x0028, 0x802d, 0x8027, 0x0022, |
| 130 |
0x8063, 0x0066, 0x006c, 0x8069, 0x0078, 0x807d, 0x8077, 0x0072, |
| 131 |
0x0050, 0x8055, 0x805f, 0x005a, 0x804b, 0x004e, 0x0044, 0x8041, |
| 132 |
0x80c3, 0x00c6, 0x00cc, 0x80c9, 0x00d8, 0x80dd, 0x80d7, 0x00d2, |
| 133 |
0x00f0, 0x80f5, 0x80ff, 0x00fa, 0x80eb, 0x00ee, 0x00e4, 0x80e1, |
| 134 |
0x00a0, 0x80a5, 0x80af, 0x00aa, 0x80bb, 0x00be, 0x00b4, 0x80b1, |
| 135 |
0x8093, 0x0096, 0x009c, 0x8099, 0x0088, 0x808d, 0x8087, 0x0082, |
| 136 |
0x8183, 0x0186, 0x018c, 0x8189, 0x0198, 0x819d, 0x8197, 0x0192, |
| 137 |
0x01b0, 0x81b5, 0x81bf, 0x01ba, 0x81ab, 0x01ae, 0x01a4, 0x81a1, |
| 138 |
0x01e0, 0x81e5, 0x81ef, 0x01ea, 0x81fb, 0x01fe, 0x01f4, 0x81f1, |
| 139 |
0x81d3, 0x01d6, 0x01dc, 0x81d9, 0x01c8, 0x81cd, 0x81c7, 0x01c2, |
| 140 |
0x0140, 0x8145, 0x814f, 0x014a, 0x815b, 0x015e, 0x0154, 0x8151, |
| 141 |
0x8173, 0x0176, 0x017c, 0x8179, 0x0168, 0x816d, 0x8167, 0x0162, |
| 142 |
0x8123, 0x0126, 0x012c, 0x8129, 0x0138, 0x813d, 0x8137, 0x0132, |
| 143 |
0x0110, 0x8115, 0x811f, 0x011a, 0x810b, 0x010e, 0x0104, 0x8101, |
| 144 |
0x8303, 0x0306, 0x030c, 0x8309, 0x0318, 0x831d, 0x8317, 0x0312, |
| 145 |
0x0330, 0x8335, 0x833f, 0x033a, 0x832b, 0x032e, 0x0324, 0x8321, |
| 146 |
0x0360, 0x8365, 0x836f, 0x036a, 0x837b, 0x037e, 0x0374, 0x8371, |
| 147 |
0x8353, 0x0356, 0x035c, 0x8359, 0x0348, 0x834d, 0x8347, 0x0342, |
| 148 |
0x03c0, 0x83c5, 0x83cf, 0x03ca, 0x83db, 0x03de, 0x03d4, 0x83d1, |
| 149 |
0x83f3, 0x03f6, 0x03fc, 0x83f9, 0x03e8, 0x83ed, 0x83e7, 0x03e2, |
| 150 |
0x83a3, 0x03a6, 0x03ac, 0x83a9, 0x03b8, 0x83bd, 0x83b7, 0x03b2, |
| 151 |
0x0390, 0x8395, 0x839f, 0x039a, 0x838b, 0x038e, 0x0384, 0x8381, |
| 152 |
0x0280, 0x8285, 0x828f, 0x028a, 0x829b, 0x029e, 0x0294, 0x8291, |
| 153 |
0x82b3, 0x02b6, 0x02bc, 0x82b9, 0x02a8, 0x82ad, 0x82a7, 0x02a2, |
| 154 |
0x82e3, 0x02e6, 0x02ec, 0x82e9, 0x02f8, 0x82fd, 0x82f7, 0x02f2, |
| 155 |
0x02d0, 0x82d5, 0x82df, 0x02da, 0x82cb, 0x02ce, 0x02c4, 0x82c1, |
| 156 |
0x8243, 0x0246, 0x024c, 0x8249, 0x0258, 0x825d, 0x8257, 0x0252, |
| 157 |
0x0270, 0x8275, 0x827f, 0x027a, 0x826b, 0x026e, 0x0264, 0x8261, |
| 158 |
0x0220, 0x8225, 0x822f, 0x022a, 0x823b, 0x023e, 0x0234, 0x8231, |
| 159 |
0x8213, 0x0216, 0x021c, 0x8219, 0x0208, 0x820d, 0x8207, 0x0202 |
| 160 |
}; |
| 161 |
|
| 162 |
static guint16 |
| 163 |
gst_flac_calculate_crc16 (const guint8 * data, guint length) |
| 164 |
{ |
| 165 |
guint16 crc = 0; |
| 166 |
|
| 167 |
while (length--) { |
| 168 |
crc = ((crc << 8) ^ crc16_table[(crc >> 8) ^ *data]) & 0xffff; |
| 169 |
data++; |
| 170 |
} |
| 171 |
|
| 172 |
return crc; |
| 173 |
} |
| 174 |
|
| 175 |
enum |
| 176 |
{ |
| 177 |
PROP_0, |
| 178 |
PROP_CHECK_FRAME_CHECKSUMS |
| 179 |
}; |
| 180 |
|
| 181 |
#define DEFAULT_CHECK_FRAME_CHECKSUMS FALSE |
| 182 |
|
| 183 |
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", |
| 184 |
GST_PAD_SRC, |
| 185 |
GST_PAD_ALWAYS, |
| 186 |
GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) true, " |
| 187 |
"channels = (int) [ 1, 8 ], " "rate = (int) [ 1, 655350 ]") |
| 188 |
); |
| 189 |
|
| 190 |
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", |
| 191 |
GST_PAD_SINK, |
| 192 |
GST_PAD_ALWAYS, |
| 193 |
GST_STATIC_CAPS ("audio/x-flac") |
| 194 |
); |
| 195 |
|
| 196 |
static GstBuffer *gst_flac_parse_generate_vorbiscomment (GstFlacParse * |
| 197 |
flacparse, gboolean is_last); |
| 198 |
|
| 199 |
static inline void gst_flac_parse_reset_buffer_time_and_offset (GstBuffer * |
| 200 |
buffer); |
| 201 |
static void gst_flac_parse_reset (GstFlacParse * parser); |
| 202 |
static gboolean gst_flac_parse_handle_block_type (GstFlacParse * flacparse, |
| 203 |
guint type, GstBuffer * sbuffer); |
| 204 |
static void gst_flac_parse_finalize (GObject * object); |
| 205 |
static void gst_flac_parse_set_property (GObject * object, guint prop_id, |
| 206 |
const GValue * value, GParamSpec * pspec); |
| 207 |
static void gst_flac_parse_get_property (GObject * object, guint prop_id, |
| 208 |
GValue * value, GParamSpec * pspec); |
| 209 |
|
| 210 |
static gboolean gst_flac_parse_start (GstBaseParse * parse); |
| 211 |
static gboolean gst_flac_parse_stop (GstBaseParse * parse); |
| 212 |
static GstFlowReturn gst_flac_parse_handle_frame (GstBaseParse * parse, |
| 213 |
GstBaseParseFrame * frame, gint * skipsize); |
| 214 |
static GstFlowReturn gst_flac_parse_parse_frame (GstBaseParse * parse, |
| 215 |
GstBaseParseFrame * frame, gint size); |
| 216 |
static GstFlowReturn gst_flac_parse_pre_push_frame (GstBaseParse * parse, |
| 217 |
GstBaseParseFrame * frame); |
| 218 |
static gboolean gst_flac_parse_convert (GstBaseParse * parse, |
| 219 |
GstFormat src_format, gint64 src_value, GstFormat dest_format, |
| 220 |
gint64 * dest_value); |
| 221 |
static gboolean gst_flac_parse_src_event (GstBaseParse * parse, |
| 222 |
GstEvent * event); |
| 223 |
static GstCaps *gst_flac_parse_get_sink_caps (GstBaseParse * parse, |
| 224 |
GstCaps * filter); |
| 225 |
static gboolean gst_flac_parse_set_sink_caps (GstBaseParse * parse, |
| 226 |
GstCaps * caps); |
| 227 |
|
| 228 |
#define gst_flac_parse_parent_class parent_class |
| 229 |
G_DEFINE_TYPE (GstFlacParse, gst_flac_parse, GST_TYPE_BASE_PARSE); |
| 230 |
GST_ELEMENT_REGISTER_DEFINE (flacparse, "flacparse", |
| 231 |
GST_RANK_PRIMARY + 1, GST_TYPE_FLAC_PARSE); |
| 232 |
|
| 233 |
static void |
| 234 |
gst_flac_parse_class_init (GstFlacParseClass * klass) |
| 235 |
{ |
| 236 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 237 |
GstElementClass *element_class = GST_ELEMENT_CLASS (klass); |
| 238 |
GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass); |
| 239 |
|
| 240 |
GST_DEBUG_CATEGORY_INIT (flacparse_debug, "flacparse", 0, |
| 241 |
"Flac parser element"); |
| 242 |
|
| 243 |
gobject_class->finalize = gst_flac_parse_finalize; |
| 244 |
gobject_class->set_property = gst_flac_parse_set_property; |
| 245 |
gobject_class->get_property = gst_flac_parse_get_property; |
| 246 |
|
| 247 |
g_object_class_install_property (gobject_class, PROP_CHECK_FRAME_CHECKSUMS, |
| 248 |
g_param_spec_boolean ("check-frame-checksums", "Check Frame Checksums", |
| 249 |
"Check the overall checksums of every frame", |
| 250 |
DEFAULT_CHECK_FRAME_CHECKSUMS, |
| 251 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 252 |
|
| 253 |
baseparse_class->start = GST_DEBUG_FUNCPTR (gst_flac_parse_start); |
| 254 |
baseparse_class->stop = GST_DEBUG_FUNCPTR (gst_flac_parse_stop); |
| 255 |
baseparse_class->handle_frame = |
| 256 |
GST_DEBUG_FUNCPTR (gst_flac_parse_handle_frame); |
| 257 |
baseparse_class->pre_push_frame = |
| 258 |
GST_DEBUG_FUNCPTR (gst_flac_parse_pre_push_frame); |
| 259 |
baseparse_class->convert = GST_DEBUG_FUNCPTR (gst_flac_parse_convert); |
| 260 |
baseparse_class->src_event = GST_DEBUG_FUNCPTR (gst_flac_parse_src_event); |
| 261 |
baseparse_class->get_sink_caps = |
| 262 |
GST_DEBUG_FUNCPTR (gst_flac_parse_get_sink_caps); |
| 263 |
baseparse_class->set_sink_caps = |
| 264 |
GST_DEBUG_FUNCPTR (gst_flac_parse_set_sink_caps); |
| 265 |
|
| 266 |
gst_element_class_add_static_pad_template (element_class, &src_factory); |
| 267 |
gst_element_class_add_static_pad_template (element_class, &sink_factory); |
| 268 |
|
| 269 |
gst_element_class_set_static_metadata (element_class, "FLAC audio parser", |
| 270 |
"Codec/Parser/Audio", |
| 271 |
"Parses audio with the FLAC lossless audio codec", |
| 272 |
"Sebastian Dröge <[email protected]>"); |
| 273 |
} |
| 274 |
|
| 275 |
static void |
| 276 |
gst_flac_parse_init (GstFlacParse * flacparse) |
| 277 |
{ |
| 278 |
flacparse->check_frame_checksums = DEFAULT_CHECK_FRAME_CHECKSUMS; |
| 279 |
GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (flacparse)); |
| 280 |
GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (flacparse)); |
| 281 |
} |
| 282 |
|
| 283 |
static void |
| 284 |
gst_flac_parse_set_property (GObject * object, guint prop_id, |
| 285 |
const GValue * value, GParamSpec * pspec) |
| 286 |
{ |
| 287 |
GstFlacParse *flacparse = GST_FLAC_PARSE (object); |
| 288 |
|
| 289 |
switch (prop_id) { |
| 290 |
case PROP_CHECK_FRAME_CHECKSUMS: |
| 291 |
flacparse->check_frame_checksums = g_value_get_boolean (value); |
| 292 |
break; |
| 293 |
default: |
| 294 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
static void |
| 300 |
gst_flac_parse_get_property (GObject * object, guint prop_id, |
| 301 |
GValue * value, GParamSpec * pspec) |
| 302 |
{ |
| 303 |
GstFlacParse *flacparse = GST_FLAC_PARSE (object); |
| 304 |
|
| 305 |
switch (prop_id) { |
| 306 |
case PROP_CHECK_FRAME_CHECKSUMS: |
| 307 |
g_value_set_boolean (value, flacparse->check_frame_checksums); |
| 308 |
break; |
| 309 |
default: |
| 310 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 311 |
break; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
static void |
| 316 |
gst_flac_parse_reset (GstFlacParse * parser) |
| 317 |
{ |
| 318 |
if (parser->tags) { |
| 319 |
gst_tag_list_unref (parser->tags); |
| 320 |
parser->tags = NULL; |
| 321 |
} |
| 322 |
if (parser->toc) { |
| 323 |
gst_toc_unref (parser->toc); |
| 324 |
parser->toc = NULL; |
| 325 |
} |
| 326 |
if (parser->seektable) { |
| 327 |
gst_buffer_unref (parser->seektable); |
| 328 |
parser->seektable = NULL; |
| 329 |
} |
| 330 |
|
| 331 |
g_list_foreach (parser->headers, (GFunc) gst_mini_object_unref, NULL); |
| 332 |
g_list_free (parser->headers); |
| 333 |
parser->headers = NULL; |
| 334 |
} |
| 335 |
|
| 336 |
static void |
| 337 |
gst_flac_parse_finalize (GObject * object) |
| 338 |
{ |
| 339 |
GstFlacParse *flacparse = GST_FLAC_PARSE (object); |
| 340 |
|
| 341 |
gst_flac_parse_reset (flacparse); |
| 342 |
G_OBJECT_CLASS (parent_class)->finalize (object); |
| 343 |
} |
| 344 |
|
| 345 |
static gboolean |
| 346 |
gst_flac_parse_start (GstBaseParse * parse) |
| 347 |
{ |
| 348 |
GstFlacParse *flacparse = GST_FLAC_PARSE (parse); |
| 349 |
|
| 350 |
flacparse->state = GST_FLAC_PARSE_STATE_INIT; |
| 351 |
flacparse->min_blocksize = 0; |
| 352 |
flacparse->max_blocksize = 0; |
| 353 |
flacparse->min_framesize = 0; |
| 354 |
flacparse->max_framesize = 0; |
| 355 |
|
| 356 |
flacparse->upstream_length = -1; |
| 357 |
|
| 358 |
flacparse->samplerate = 0; |
| 359 |
flacparse->channels = 0; |
| 360 |
flacparse->bps = 0; |
| 361 |
flacparse->total_samples = 0; |
| 362 |
|
| 363 |
flacparse->offset = GST_CLOCK_TIME_NONE; |
| 364 |
flacparse->blocking_strategy = 0; |
| 365 |
flacparse->block_size = 0; |
| 366 |
flacparse->fixed_block_size = 0; |
| 367 |
flacparse->sample_number = 0; |
| 368 |
flacparse->strategy_checked = FALSE; |
| 369 |
|
| 370 |
flacparse->sent_codec_tag = FALSE; |
| 371 |
|
| 372 |
/* "fLaC" marker */ |
| 373 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4); |
| 374 |
|
| 375 |
/* inform baseclass we can come up with ts, based on counters in packets */ |
| 376 |
gst_base_parse_set_has_timing_info (GST_BASE_PARSE_CAST (flacparse), TRUE); |
| 377 |
gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (flacparse), TRUE); |
| 378 |
|
| 379 |
return TRUE; |
| 380 |
} |
| 381 |
|
| 382 |
static gboolean |
| 383 |
gst_flac_parse_stop (GstBaseParse * parse) |
| 384 |
{ |
| 385 |
GstFlacParse *flacparse = GST_FLAC_PARSE (parse); |
| 386 |
|
| 387 |
gst_flac_parse_reset (flacparse); |
| 388 |
return TRUE; |
| 389 |
} |
| 390 |
|
| 391 |
static const guint8 sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 32 }; |
| 392 |
|
| 393 |
static const guint16 blocksize_table[16] = { |
| 394 |
0, 192, 576 << 0, 576 << 1, 576 << 2, 576 << 3, 0, 0, |
| 395 |
256 << 0, 256 << 1, 256 << 2, 256 << 3, 256 << 4, 256 << 5, 256 << 6, |
| 396 |
256 << 7, |
| 397 |
}; |
| 398 |
|
| 399 |
static const guint32 sample_rate_table[16] = { |
| 400 |
0, |
| 401 |
88200, 176400, 192000, |
| 402 |
8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, |
| 403 |
0, 0, 0, 0, |
| 404 |
}; |
| 405 |
|
| 406 |
typedef enum |
| 407 |
{ |
| 408 |
FRAME_HEADER_VALID, |
| 409 |
FRAME_HEADER_INVALID, |
| 410 |
FRAME_HEADER_MORE_DATA |
| 411 |
} FrameHeaderCheckReturn; |
| 412 |
|
| 413 |
static FrameHeaderCheckReturn |
| 414 |
gst_flac_parse_frame_header_is_valid (GstFlacParse * flacparse, |
| 415 |
const guint8 * data, guint size, guint64 offset, |
| 416 |
FlacFrameHeader * header_ret) |
| 417 |
{ |
| 418 |
GstBitReader reader = GST_BIT_READER_INIT (data, size); |
| 419 |
guint8 blocking_strategy; |
| 420 |
guint16 block_size; |
| 421 |
guint32 samplerate = 0; |
| 422 |
guint64 sample_number; |
| 423 |
guint8 channels, bps; |
| 424 |
guint8 tmp = 0; |
| 425 |
guint8 actual_crc, expected_crc = 0; |
| 426 |
|
| 427 |
/* Skip 14 bit sync code */ |
| 428 |
gst_bit_reader_skip_unchecked (&reader, 14); |
| 429 |
|
| 430 |
/* Must be 0 */ |
| 431 |
if (gst_bit_reader_get_bits_uint8_unchecked (&reader, 1) != 0) |
| 432 |
goto error; |
| 433 |
|
| 434 |
/* 0 == fixed block size, 1 == variable block size */ |
| 435 |
blocking_strategy = gst_bit_reader_get_bits_uint8_unchecked (&reader, 1); |
| 436 |
|
| 437 |
/* block size index, calculation of the real blocksize below */ |
| 438 |
block_size = gst_bit_reader_get_bits_uint16_unchecked (&reader, 4); |
| 439 |
if (block_size == 0) |
| 440 |
goto error; |
| 441 |
|
| 442 |
/* sample rate index, calculation of the real samplerate below */ |
| 443 |
samplerate = gst_bit_reader_get_bits_uint16_unchecked (&reader, 4); |
| 444 |
if (samplerate == 0x0f) |
| 445 |
goto error; |
| 446 |
|
| 447 |
/* channel assignment */ |
| 448 |
channels = gst_bit_reader_get_bits_uint8_unchecked (&reader, 4); |
| 449 |
if (channels < 8) { |
| 450 |
channels++; |
| 451 |
} else if (channels <= 10) { |
| 452 |
channels = 2; |
| 453 |
} else if (channels > 10) { |
| 454 |
goto error; |
| 455 |
} |
| 456 |
|
| 457 |
/* bits per sample */ |
| 458 |
bps = gst_bit_reader_get_bits_uint8_unchecked (&reader, 3); |
| 459 |
if (bps == 0x03) { |
| 460 |
goto error; |
| 461 |
} else if (bps == 0 && flacparse->bps == 0) { |
| 462 |
goto need_streaminfo; |
| 463 |
} else if (bps == 0 && flacparse->bps != 0) { |
| 464 |
bps = flacparse->bps; |
| 465 |
} else { |
| 466 |
bps = sample_size_table[bps]; |
| 467 |
} |
| 468 |
|
| 469 |
/* reserved, must be 0 */ |
| 470 |
if (gst_bit_reader_get_bits_uint8_unchecked (&reader, 1) != 0) |
| 471 |
goto error; |
| 472 |
|
| 473 |
/* read "utf8" encoded sample/frame number */ |
| 474 |
{ |
| 475 |
gint len = 0; |
| 476 |
|
| 477 |
len = gst_bit_reader_get_bits_uint8_unchecked (&reader, 8); |
| 478 |
|
| 479 |
/* This is slightly faster than a loop */ |
| 480 |
if (!(len & 0x80)) { |
| 481 |
sample_number = len; |
| 482 |
len = 0; |
| 483 |
} else if ((len & 0xc0) && !(len & 0x20)) { |
| 484 |
sample_number = len & 0x1f; |
| 485 |
len = 1; |
| 486 |
} else if ((len & 0xe0) && !(len & 0x10)) { |
| 487 |
sample_number = len & 0x0f; |
| 488 |
len = 2; |
| 489 |
} else if ((len & 0xf0) && !(len & 0x08)) { |
| 490 |
sample_number = len & 0x07; |
| 491 |
len = 3; |
| 492 |
} else if ((len & 0xf8) && !(len & 0x04)) { |
| 493 |
sample_number = len & 0x03; |
| 494 |
len = 4; |
| 495 |
} else if ((len & 0xfc) && !(len & 0x02)) { |
| 496 |
sample_number = len & 0x01; |
| 497 |
len = 5; |
| 498 |
} else if ((len & 0xfe) && !(len & 0x01)) { |
| 499 |
sample_number = len & 0x0; |
| 500 |
len = 6; |
| 501 |
} else { |
| 502 |
goto error; |
| 503 |
} |
| 504 |
|
| 505 |
if ((blocking_strategy == 0 && len > 5) || |
| 506 |
(blocking_strategy == 1 && len > 6)) |
| 507 |
goto error; |
| 508 |
|
| 509 |
while (len > 0) { |
| 510 |
if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 8)) |
| 511 |
goto need_more_data; |
| 512 |
|
| 513 |
if ((tmp & 0xc0) != 0x80) |
| 514 |
goto error; |
| 515 |
|
| 516 |
sample_number <<= 6; |
| 517 |
sample_number |= (tmp & 0x3f); |
| 518 |
len--; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
/* calculate real blocksize from the blocksize index */ |
| 523 |
if (block_size == 6) { |
| 524 |
if (!gst_bit_reader_get_bits_uint16 (&reader, &block_size, 8)) |
| 525 |
goto need_more_data; |
| 526 |
block_size++; |
| 527 |
} else if (block_size == 7) { |
| 528 |
if (!gst_bit_reader_get_bits_uint16 (&reader, &block_size, 16)) |
| 529 |
goto need_more_data; |
| 530 |
block_size++; |
| 531 |
} else { |
| 532 |
block_size = blocksize_table[block_size]; |
| 533 |
} |
| 534 |
|
| 535 |
/* calculate the real samplerate from the samplerate index */ |
| 536 |
if (samplerate == 0 && flacparse->samplerate == 0) { |
| 537 |
goto need_streaminfo; |
| 538 |
} else if (samplerate == 0 && flacparse->samplerate != 0) { |
| 539 |
samplerate = flacparse->samplerate; |
| 540 |
} else if (samplerate < 12) { |
| 541 |
samplerate = sample_rate_table[samplerate]; |
| 542 |
} else if (samplerate == 12) { |
| 543 |
if (!gst_bit_reader_get_bits_uint32 (&reader, &samplerate, 8)) |
| 544 |
goto need_more_data; |
| 545 |
samplerate *= 1000; |
| 546 |
} else if (samplerate == 13) { |
| 547 |
if (!gst_bit_reader_get_bits_uint32 (&reader, &samplerate, 16)) |
| 548 |
goto need_more_data; |
| 549 |
} else if (samplerate == 14) { |
| 550 |
if (!gst_bit_reader_get_bits_uint32 (&reader, &samplerate, 16)) |
| 551 |
goto need_more_data; |
| 552 |
samplerate *= 10; |
| 553 |
} |
| 554 |
|
| 555 |
/* check crc-8 for the header */ |
| 556 |
if (!gst_bit_reader_get_bits_uint8 (&reader, &expected_crc, 8)) |
| 557 |
goto need_more_data; |
| 558 |
|
| 559 |
actual_crc = |
| 560 |
gst_flac_calculate_crc8 (data, |
| 561 |
(gst_bit_reader_get_pos (&reader) / 8) - 1); |
| 562 |
if (actual_crc != expected_crc) { |
| 563 |
GST_DEBUG_OBJECT (flacparse, |
| 564 |
"Checksum mismatch. Header CRC was '%d' but frame has '%d'", |
| 565 |
expected_crc, actual_crc); |
| 566 |
goto error; |
| 567 |
} |
| 568 |
|
| 569 |
GST_TRACE_OBJECT (flacparse, |
| 570 |
"Parsed frame at offset %" G_GUINT64_FORMAT ":\n" "Block size: %u\n" |
| 571 |
"Samplerate %u\nbps: %u\nBlocking strategy: %u\nChannels: %u\n" |
| 572 |
"Sample/Frame number: %" G_GUINT64_FORMAT, offset, |
| 573 |
block_size, samplerate, bps, blocking_strategy, channels, sample_number); |
| 574 |
|
| 575 |
if (header_ret) { |
| 576 |
header_ret->block_size = block_size; |
| 577 |
header_ret->samplerate = samplerate; |
| 578 |
header_ret->bps = bps; |
| 579 |
header_ret->blocking_strategy = blocking_strategy; |
| 580 |
header_ret->channels = channels; |
| 581 |
header_ret->sample_number = sample_number; |
| 582 |
} |
| 583 |
|
| 584 |
return FRAME_HEADER_VALID; |
| 585 |
|
| 586 |
need_streaminfo: |
| 587 |
GST_ERROR_OBJECT (flacparse, "Need STREAMINFO metadata. Bits per sample " |
| 588 |
"or sample rate not in frame header"); |
| 589 |
error: |
| 590 |
return FRAME_HEADER_INVALID; |
| 591 |
|
| 592 |
need_more_data: |
| 593 |
return FRAME_HEADER_MORE_DATA; |
| 594 |
} |
| 595 |
|
| 596 |
static void |
| 597 |
gst_flac_parse_frame_header_update (GstFlacParse * flacparse, |
| 598 |
const FlacFrameHeader * header) |
| 599 |
{ |
| 600 |
|
| 601 |
if (flacparse->samplerate != header->samplerate |
| 602 |
|| flacparse->channels != header->channels) { |
| 603 |
GstCaps *caps; |
| 604 |
|
| 605 |
GST_DEBUG_OBJECT (flacparse, |
| 606 |
"Configuring caps with sample rate %d and %d channels", |
| 607 |
header->samplerate, header->channels); |
| 608 |
|
| 609 |
/* Do not include the headers as they would contain an invalid samplerate / |
| 610 |
* channel count now */ |
| 611 |
caps = gst_caps_new_simple ("audio/x-flac", |
| 612 |
"channels", G_TYPE_INT, header->channels, |
| 613 |
"framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, header->samplerate, |
| 614 |
NULL); |
| 615 |
|
| 616 |
gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse)), |
| 617 |
caps); |
| 618 |
gst_caps_unref (caps); |
| 619 |
} |
| 620 |
|
| 621 |
flacparse->block_size = header->block_size; |
| 622 |
flacparse->samplerate = header->samplerate; |
| 623 |
flacparse->bps = header->bps; |
| 624 |
flacparse->blocking_strategy = header->blocking_strategy; |
| 625 |
flacparse->channels = header->channels; |
| 626 |
flacparse->sample_number = header->sample_number; |
| 627 |
|
| 628 |
/* Remember the initial fixed block size */ |
| 629 |
if (flacparse->blocking_strategy == 0 && flacparse->fixed_block_size == 0) |
| 630 |
flacparse->fixed_block_size = header->block_size; |
| 631 |
} |
| 632 |
|
| 633 |
static gboolean |
| 634 |
gst_flac_parse_frame_is_valid (GstFlacParse * flacparse, |
| 635 |
const guint8 * data, gsize size, guint * ret) |
| 636 |
{ |
| 637 |
guint max; |
| 638 |
guint i, search_start, search_end; |
| 639 |
FrameHeaderCheckReturn header_ret; |
| 640 |
FlacFrameHeader header_start, header_end = { 0, }; |
| 641 |
gboolean suspect_start; |
| 642 |
|
| 643 |
/* minimum frame header size is 5 bytes and we read that much |
| 644 |
* without checking the size below */ |
| 645 |
if (size < MAX (flacparse->min_framesize, 5)) |
| 646 |
goto need_more; |
| 647 |
|
| 648 |
header_ret = |
| 649 |
gst_flac_parse_frame_header_is_valid (flacparse, data, size, |
| 650 |
flacparse->offset, &header_start); |
| 651 |
if (header_ret == FRAME_HEADER_INVALID) { |
| 652 |
*ret = 0; |
| 653 |
return FALSE; |
| 654 |
} |
| 655 |
if (header_ret == FRAME_HEADER_MORE_DATA) |
| 656 |
goto need_more; |
| 657 |
|
| 658 |
suspect_start = (flacparse->channels |
| 659 |
&& flacparse->channels != header_start.channels) || (flacparse->bps |
| 660 |
&& flacparse->bps != header_start.bps) || (flacparse->samplerate |
| 661 |
&& flacparse->samplerate != header_start.samplerate) |
| 662 |
|| (flacparse->block_size && !flacparse->force_variable_block_size |
| 663 |
&& flacparse->blocking_strategy != header_start.blocking_strategy) |
| 664 |
|| (flacparse->block_size && !flacparse->force_variable_block_size |
| 665 |
&& header_start.blocking_strategy == 0 |
| 666 |
&& flacparse->block_size != header_start.block_size); |
| 667 |
|
| 668 |
/* mind unknown framesize */ |
| 669 |
search_start = MAX (2, flacparse->min_framesize); |
| 670 |
|
| 671 |
/* at minimum 5 bytes are read below so stop 5 bytes before the end. |
| 672 |
* we also checked above that at least 5 bytes are available. */ |
| 673 |
search_end = size - 5; |
| 674 |
if (flacparse->max_framesize) |
| 675 |
search_end = MIN (search_end, flacparse->max_framesize + 9 + 2); |
| 676 |
|
| 677 |
for (i = search_start; i < search_end; i++) { |
| 678 |
|
| 679 |
if ((GST_READ_UINT16_BE (data + i) & 0xfffe) != 0xfff8) |
| 680 |
continue; |
| 681 |
|
| 682 |
GST_LOG_OBJECT (flacparse, "possible frame end at offset %d", i); |
| 683 |
header_ret = |
| 684 |
gst_flac_parse_frame_header_is_valid (flacparse, data + i, |
| 685 |
size - i, flacparse->offset + i, &header_end); |
| 686 |
if (header_ret == FRAME_HEADER_VALID) { |
| 687 |
gboolean suspect_end; |
| 688 |
|
| 689 |
suspect_end = (header_start.channels != header_end.channels) || |
| 690 |
(header_start.bps != header_end.bps) || |
| 691 |
(header_start.samplerate != header_end.samplerate) || |
| 692 |
(!flacparse->force_variable_block_size |
| 693 |
&& header_start.blocking_strategy != header_end.blocking_strategy) |
| 694 |
|| (!flacparse->force_variable_block_size |
| 695 |
&& header_start.blocking_strategy == 0 |
| 696 |
&& header_start.block_size != header_end.block_size); |
| 697 |
|
| 698 |
if (flacparse->check_frame_checksums || suspect_start || suspect_end) { |
| 699 |
guint16 actual_crc = gst_flac_calculate_crc16 (data, i - 2); |
| 700 |
guint16 expected_crc = GST_READ_UINT16_BE (data + i - 2); |
| 701 |
|
| 702 |
GST_LOG_OBJECT (flacparse, |
| 703 |
"Found possible frame (%d, %d). Checking for CRC match", |
| 704 |
suspect_start, suspect_end); |
| 705 |
if (actual_crc != expected_crc) { |
| 706 |
GST_DEBUG_OBJECT (flacparse, |
| 707 |
"Checksum mismatch. Header CRC was '%d' but frame has '%d'", |
| 708 |
expected_crc, actual_crc); |
| 709 |
continue; |
| 710 |
} |
| 711 |
} |
| 712 |
*ret = i; |
| 713 |
goto valid_frame; |
| 714 |
} else if (header_ret == FRAME_HEADER_MORE_DATA) { |
| 715 |
goto need_more; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
/* For the last frame output everything to the end */ |
| 720 |
if (G_UNLIKELY (GST_BASE_PARSE_DRAINING (flacparse))) { |
| 721 |
if (flacparse->check_frame_checksums) { |
| 722 |
guint16 actual_crc = gst_flac_calculate_crc16 (data, size - 2); |
| 723 |
guint16 expected_crc = GST_READ_UINT16_BE (data + size - 2); |
| 724 |
|
| 725 |
if (actual_crc == expected_crc) { |
| 726 |
*ret = size; |
| 727 |
goto valid_frame; |
| 728 |
} |
| 729 |
} else { |
| 730 |
*ret = size; |
| 731 |
goto valid_frame; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
/* so we searched to expected end and found nothing, |
| 736 |
* give up on this frame (start) */ |
| 737 |
if (flacparse->max_framesize && i > 2 * flacparse->max_framesize) { |
| 738 |
GST_LOG_OBJECT (flacparse, |
| 739 |
"could not determine valid frame end, discarding frame (start)"); |
| 740 |
*ret = 1; |
| 741 |
return FALSE; |
| 742 |
} |
| 743 |
|
| 744 |
need_more: |
| 745 |
max = flacparse->max_framesize + 16; |
| 746 |
if (max == 16) |
| 747 |
max = 1 << 24; |
| 748 |
*ret = MIN (size + 4096, max); |
| 749 |
return TRUE; |
| 750 |
|
| 751 |
valid_frame: |
| 752 |
if (!flacparse->strategy_checked) { |
| 753 |
GST_INFO_OBJECT (flacparse, "First sample number is %" G_GUINT64_FORMAT, |
| 754 |
header_start.sample_number); |
| 755 |
flacparse->first_sample_number = header_start.sample_number; |
| 756 |
} |
| 757 |
|
| 758 |
/* Sanity check sample number against blocking strategy, as it seems |
| 759 |
some files claim fixed block size but supply sample numbers, |
| 760 |
rather than block numbers. */ |
| 761 |
if (!flacparse->strategy_checked) { |
| 762 |
if (header_start.blocking_strategy == 0 && header_end.blocking_strategy == 0 |
| 763 |
&& header_end.block_size != 0) { |
| 764 |
if (header_end.block_size == header_end.sample_number) { |
| 765 |
GST_WARNING_OBJECT (flacparse, "This file claims fixed block size, " |
| 766 |
"but seems to be lying: assuming variable block size"); |
| 767 |
flacparse->force_variable_block_size = TRUE; |
| 768 |
header_start.blocking_strategy = 1; |
| 769 |
} |
| 770 |
} |
| 771 |
flacparse->strategy_checked = TRUE; |
| 772 |
} |
| 773 |
|
| 774 |
if (flacparse->force_variable_block_size) |
| 775 |
header_start.blocking_strategy = 1; |
| 776 |
|
| 777 |
if (flacparse->channels && flacparse->channels != header_start.channels) { |
| 778 |
GST_WARNING_OBJECT (flacparse, "channels are not constant (%d -> %d)", |
| 779 |
flacparse->channels, header_start.channels); |
| 780 |
} |
| 781 |
|
| 782 |
if (flacparse->bps && header_start.bps != flacparse->bps) { |
| 783 |
GST_WARNING_OBJECT (flacparse, "bps is not constant (%d -> %d)", |
| 784 |
flacparse->bps, header_start.bps); |
| 785 |
} |
| 786 |
|
| 787 |
if (flacparse->samplerate && flacparse->samplerate != header_start.samplerate) { |
| 788 |
GST_WARNING_OBJECT (flacparse, "samplerate is not constant (%d -> %d)", |
| 789 |
flacparse->samplerate, header_start.samplerate); |
| 790 |
} |
| 791 |
|
| 792 |
/* documentation says: |
| 793 |
* The "blocking strategy" bit must be the same throughout the entire stream. */ |
| 794 |
if (flacparse->block_size |
| 795 |
&& flacparse->blocking_strategy != header_start.blocking_strategy) { |
| 796 |
GST_WARNING_OBJECT (flacparse, |
| 797 |
"blocking strategy is not constant (%d -> %d)", |
| 798 |
flacparse->blocking_strategy, header_start.blocking_strategy); |
| 799 |
|
| 800 |
/* Reset if switching to a fixed block size at this point */ |
| 801 |
if (header_start.blocking_strategy == 0) |
| 802 |
flacparse->fixed_block_size = 0; |
| 803 |
} |
| 804 |
|
| 805 |
/* |
| 806 |
The FLAC format documentation says: |
| 807 |
The "blocking strategy" bit determines how to calculate the sample number |
| 808 |
of the first sample in the frame. If the bit is 0 (fixed-blocksize), the |
| 809 |
frame header encodes the frame number as above, and the frame's starting |
| 810 |
sample number will be the frame number times the blocksize. If it is 1 |
| 811 |
(variable-blocksize), the frame header encodes the frame's starting |
| 812 |
sample number itself. (In the case of a fixed-blocksize stream, only the |
| 813 |
last block may be shorter than the stream blocksize; its starting sample |
| 814 |
number will be calculated as the frame number times the previous frame's |
| 815 |
blocksize, or zero if it is the first frame). |
| 816 |
|
| 817 |
Therefore, when in fixed block size mode, we only update the block size |
| 818 |
the first time, then reuse that block size for subsequent calls. |
| 819 |
This will also fix a timestamp problem with the last block's timestamp |
| 820 |
being miscalculated by scaling the block number by a "wrong" block size. |
| 821 |
*/ |
| 822 |
if (flacparse->block_size && header_start.blocking_strategy == 0) { |
| 823 |
/* Only check if we're not draining, i.e. block size of the next frame could |
| 824 |
* be read successfully */ |
| 825 |
if (header_end.block_size |
| 826 |
&& flacparse->block_size != header_start.block_size) { |
| 827 |
GST_WARNING_OBJECT (flacparse, "Block size is not constant (%d -> %d)", |
| 828 |
flacparse->block_size, header_start.block_size); |
| 829 |
header_start.block_size = flacparse->fixed_block_size; |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
gst_flac_parse_frame_header_update (flacparse, &header_start); |
| 834 |
|
| 835 |
GST_TRACE_OBJECT (flacparse, "Found valid frame"); |
| 836 |
|
| 837 |
return TRUE; |
| 838 |
} |
| 839 |
|
| 840 |
static GstFlowReturn |
| 841 |
gst_flac_parse_handle_frame (GstBaseParse * parse, |
| 842 |
GstBaseParseFrame * frame, gint * skipsize) |
| 843 |
{ |
| 844 |
GstFlacParse *flacparse = GST_FLAC_PARSE (parse); |
| 845 |
GstBuffer *buffer = frame->buffer; |
| 846 |
GstMapInfo map; |
| 847 |
gboolean ret = FALSE; |
| 848 |
guint framesize = 0; |
| 849 |
|
| 850 |
gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 851 |
|
| 852 |
*skipsize = 1; |
| 853 |
|
| 854 |
if (G_UNLIKELY (map.size < 4)) { |
| 855 |
goto cleanup; |
| 856 |
} |
| 857 |
|
| 858 |
if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) { |
| 859 |
if (memcmp (map.data, "fLaC", 4) == 0) { |
| 860 |
GST_DEBUG_OBJECT (flacparse, "fLaC marker found"); |
| 861 |
framesize = 4; |
| 862 |
ret = TRUE; |
| 863 |
goto cleanup; |
| 864 |
} |
| 865 |
if (map.data[0] == 0xff && (map.data[1] >> 2) == 0x3e) { |
| 866 |
GST_DEBUG_OBJECT (flacparse, "Found headerless FLAC"); |
| 867 |
/* Minimal size of a frame header */ |
| 868 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 9); |
| 869 |
flacparse->state = GST_FLAC_PARSE_STATE_GENERATE_HEADERS; |
| 870 |
*skipsize = 0; |
| 871 |
goto cleanup; |
| 872 |
} |
| 873 |
GST_DEBUG_OBJECT (flacparse, "fLaC marker not found"); |
| 874 |
goto cleanup; |
| 875 |
} |
| 876 |
|
| 877 |
if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) { |
| 878 |
guint size = 4 + ((map.data[1] << 16) | (map.data[2] << 8) | (map.data[3])); |
| 879 |
|
| 880 |
GST_DEBUG_OBJECT (flacparse, "Found metadata block of size %u", size); |
| 881 |
ret = TRUE; |
| 882 |
framesize = size; |
| 883 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), framesize); |
| 884 |
goto cleanup; |
| 885 |
} |
| 886 |
|
| 887 |
if ((GST_READ_UINT16_BE (map.data) & 0xfffe) == 0xfff8) { |
| 888 |
guint next = 0; |
| 889 |
|
| 890 |
flacparse->offset = GST_BUFFER_OFFSET (buffer); |
| 891 |
flacparse->blocking_strategy = 0; |
| 892 |
flacparse->sample_number = 0; |
| 893 |
|
| 894 |
GST_DEBUG_OBJECT (flacparse, "Found sync code"); |
| 895 |
ret = gst_flac_parse_frame_is_valid (flacparse, map.data, map.size, &next); |
| 896 |
if (ret) { |
| 897 |
framesize = next; |
| 898 |
goto cleanup; |
| 899 |
} |
| 900 |
|
| 901 |
/* If we're at EOS and the frame was not valid, drop it! */ |
| 902 |
if (G_UNLIKELY (GST_BASE_PARSE_DRAINING (flacparse))) { |
| 903 |
GST_WARNING_OBJECT (flacparse, "EOS"); |
| 904 |
goto cleanup; |
| 905 |
} |
| 906 |
|
| 907 |
if (next == 0) { |
| 908 |
ret = TRUE; |
| 909 |
} else if (next > map.size) { |
| 910 |
GST_DEBUG_OBJECT (flacparse, "Requesting %u bytes", next); |
| 911 |
*skipsize = 0; |
| 912 |
gst_base_parse_set_min_frame_size (parse, next); |
| 913 |
goto cleanup; |
| 914 |
} else { |
| 915 |
GST_ERROR_OBJECT (flacparse, |
| 916 |
"Giving up on invalid frame (%" G_GSIZE_FORMAT " bytes)", map.size); |
| 917 |
goto cleanup; |
| 918 |
} |
| 919 |
} else { |
| 920 |
GstByteReader reader; |
| 921 |
gint off; |
| 922 |
|
| 923 |
gst_byte_reader_init (&reader, map.data, map.size); |
| 924 |
off = |
| 925 |
gst_byte_reader_masked_scan_uint32 (&reader, 0xfffc0000, 0xfff80000, |
| 926 |
0, map.size); |
| 927 |
|
| 928 |
if (off > 0) { |
| 929 |
GST_DEBUG_OBJECT (parse, "Possible sync at buffer offset %d", off); |
| 930 |
*skipsize = off; |
| 931 |
goto cleanup; |
| 932 |
} |
| 933 |
|
| 934 |
GST_DEBUG_OBJECT (flacparse, "Sync code not found"); |
| 935 |
*skipsize = map.size - 3; |
| 936 |
goto cleanup; |
| 937 |
} |
| 938 |
|
| 939 |
cleanup: |
| 940 |
if (ret) { |
| 941 |
*skipsize = 0; |
| 942 |
/* found a whole frame */ |
| 943 |
ret = framesize <= map.size; |
| 944 |
} |
| 945 |
|
| 946 |
gst_buffer_unmap (buffer, &map); |
| 947 |
|
| 948 |
if (ret) { |
| 949 |
GstFlowReturn flow_ret = |
| 950 |
gst_flac_parse_parse_frame (parse, frame, framesize); |
| 951 |
if (flow_ret == GST_BASE_PARSE_FLOW_DROPPED) { |
| 952 |
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_DROP; |
| 953 |
} else if (flow_ret != GST_FLOW_OK) { |
| 954 |
return flow_ret; |
| 955 |
} |
| 956 |
|
| 957 |
return gst_base_parse_finish_frame (parse, frame, framesize); |
| 958 |
} |
| 959 |
|
| 960 |
return GST_FLOW_OK; |
| 961 |
} |
| 962 |
|
| 963 |
static gboolean |
| 964 |
gst_flac_parse_handle_streaminfo (GstFlacParse * flacparse, GstBuffer * buffer) |
| 965 |
{ |
| 966 |
GstBitReader reader; |
| 967 |
GstMapInfo map; |
| 968 |
|
| 969 |
gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 970 |
gst_bit_reader_init (&reader, map.data, map.size); |
| 971 |
|
| 972 |
if (map.size != 4 + 34) { |
| 973 |
GST_ERROR_OBJECT (flacparse, |
| 974 |
"Invalid metablock size for STREAMINFO: %" G_GSIZE_FORMAT "", map.size); |
| 975 |
goto failure; |
| 976 |
} |
| 977 |
|
| 978 |
/* Skip metadata block header */ |
| 979 |
if (!gst_bit_reader_skip (&reader, 32)) |
| 980 |
goto error; |
| 981 |
|
| 982 |
if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->min_blocksize, 16)) |
| 983 |
goto error; |
| 984 |
if (flacparse->min_blocksize < 16) { |
| 985 |
GST_WARNING_OBJECT (flacparse, "Invalid minimum block size: %u", |
| 986 |
flacparse->min_blocksize); |
| 987 |
} |
| 988 |
|
| 989 |
if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->max_blocksize, 16)) |
| 990 |
goto error; |
| 991 |
if (flacparse->max_blocksize < 16) { |
| 992 |
GST_WARNING_OBJECT (flacparse, "Invalid maximum block size: %u", |
| 993 |
flacparse->max_blocksize); |
| 994 |
} |
| 995 |
|
| 996 |
if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->min_framesize, 24)) |
| 997 |
goto error; |
| 998 |
if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->max_framesize, 24)) |
| 999 |
goto error; |
| 1000 |
|
| 1001 |
if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->samplerate, 20)) |
| 1002 |
goto error; |
| 1003 |
if (flacparse->samplerate == 0) { |
| 1004 |
GST_ERROR_OBJECT (flacparse, "Invalid sample rate 0"); |
| 1005 |
goto failure; |
| 1006 |
} |
| 1007 |
|
| 1008 |
if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->channels, 3)) |
| 1009 |
goto error; |
| 1010 |
flacparse->channels++; |
| 1011 |
if (flacparse->channels > 8) { |
| 1012 |
GST_ERROR_OBJECT (flacparse, "Invalid number of channels %u", |
| 1013 |
flacparse->channels); |
| 1014 |
goto failure; |
| 1015 |
} |
| 1016 |
|
| 1017 |
if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->bps, 5)) |
| 1018 |
goto error; |
| 1019 |
flacparse->bps++; |
| 1020 |
|
| 1021 |
if (!gst_bit_reader_get_bits_uint64 (&reader, &flacparse->total_samples, 36)) |
| 1022 |
goto error; |
| 1023 |
if (flacparse->total_samples) { |
| 1024 |
gst_base_parse_set_duration (GST_BASE_PARSE (flacparse), |
| 1025 |
GST_FORMAT_DEFAULT, flacparse->total_samples, 0); |
| 1026 |
} |
| 1027 |
|
| 1028 |
gst_buffer_unmap (buffer, &map); |
| 1029 |
|
| 1030 |
GST_DEBUG_OBJECT (flacparse, "STREAMINFO:\n" |
| 1031 |
"\tmin/max blocksize: %u/%u,\n" |
| 1032 |
"\tmin/max framesize: %u/%u,\n" |
| 1033 |
"\tsamplerate: %u,\n" |
| 1034 |
"\tchannels: %u,\n" |
| 1035 |
"\tbits per sample: %u,\n" |
| 1036 |
"\ttotal samples: %" G_GUINT64_FORMAT, |
| 1037 |
flacparse->min_blocksize, flacparse->max_blocksize, |
| 1038 |
flacparse->min_framesize, flacparse->max_framesize, |
| 1039 |
flacparse->samplerate, |
| 1040 |
flacparse->channels, flacparse->bps, flacparse->total_samples); |
| 1041 |
|
| 1042 |
return TRUE; |
| 1043 |
|
| 1044 |
error: |
| 1045 |
GST_ERROR_OBJECT (flacparse, "Failed to read data"); |
| 1046 |
failure: |
| 1047 |
gst_buffer_unmap (buffer, &map); |
| 1048 |
return FALSE; |
| 1049 |
} |
| 1050 |
|
| 1051 |
static gboolean |
| 1052 |
gst_flac_parse_handle_vorbiscomment (GstFlacParse * flacparse, |
| 1053 |
GstBuffer * buffer) |
| 1054 |
{ |
| 1055 |
GstTagList *tags; |
| 1056 |
GstMapInfo map; |
| 1057 |
|
| 1058 |
gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 1059 |
|
| 1060 |
tags = |
| 1061 |
gst_tag_list_from_vorbiscomment (map.data, map.size, map.data, 4, NULL); |
| 1062 |
gst_buffer_unmap (buffer, &map); |
| 1063 |
|
| 1064 |
if (tags == NULL) { |
| 1065 |
GST_ERROR_OBJECT (flacparse, "Invalid vorbiscomment block"); |
| 1066 |
} else if (gst_tag_list_is_empty (tags)) { |
| 1067 |
gst_tag_list_unref (tags); |
| 1068 |
} else if (flacparse->tags == NULL) { |
| 1069 |
flacparse->tags = tags; |
| 1070 |
} else { |
| 1071 |
gst_tag_list_insert (flacparse->tags, tags, GST_TAG_MERGE_APPEND); |
| 1072 |
gst_tag_list_unref (tags); |
| 1073 |
} |
| 1074 |
|
| 1075 |
return TRUE; |
| 1076 |
} |
| 1077 |
|
| 1078 |
static gboolean |
| 1079 |
gst_flac_parse_handle_cuesheet (GstFlacParse * flacparse, GstBuffer * buffer) |
| 1080 |
{ |
| 1081 |
GstByteReader reader; |
| 1082 |
GstMapInfo map; |
| 1083 |
guint i, j; |
| 1084 |
guint8 n_tracks, track_num, index; |
| 1085 |
guint64 offset; |
| 1086 |
gint64 start, stop; |
| 1087 |
gchar *id; |
| 1088 |
gchar isrc[13]; |
| 1089 |
GstTagList *tags; |
| 1090 |
GstToc *toc; |
| 1091 |
GstTocEntry *cur_entry = NULL, *prev_entry = NULL; |
| 1092 |
|
| 1093 |
gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 1094 |
gst_byte_reader_init (&reader, map.data, map.size); |
| 1095 |
|
| 1096 |
toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL); |
| 1097 |
|
| 1098 |
/* skip 4 bytes METADATA_BLOCK_HEADER */ |
| 1099 |
/* https://xiph.org/flac/format.html#metadata_block_header */ |
| 1100 |
if (!gst_byte_reader_skip (&reader, 4)) |
| 1101 |
goto error; |
| 1102 |
|
| 1103 |
/* skip 395 bytes from METADATA_BLOCK_CUESHEET */ |
| 1104 |
/* https://xiph.org/flac/format.html#metadata_block_cuesheet */ |
| 1105 |
if (!gst_byte_reader_skip (&reader, 395)) |
| 1106 |
goto error; |
| 1107 |
|
| 1108 |
if (!gst_byte_reader_get_uint8 (&reader, &n_tracks)) |
| 1109 |
goto error; |
| 1110 |
|
| 1111 |
/* CUESHEET_TRACK */ |
| 1112 |
/* https://xiph.org/flac/format.html#cuesheet_track */ |
| 1113 |
for (i = 0; i < n_tracks; i++) { |
| 1114 |
if (!gst_byte_reader_get_uint64_be (&reader, &offset)) |
| 1115 |
goto error; |
| 1116 |
if (!gst_byte_reader_get_uint8 (&reader, &track_num)) |
| 1117 |
goto error; |
| 1118 |
|
| 1119 |
if (gst_byte_reader_get_remaining (&reader) < 12) |
| 1120 |
goto error; |
| 1121 |
memcpy (isrc, map.data + gst_byte_reader_get_pos (&reader), 12); |
| 1122 |
/* \0-terminate the string */ |
| 1123 |
isrc[12] = '\0'; |
| 1124 |
if (!gst_byte_reader_skip (&reader, 12)) |
| 1125 |
goto error; |
| 1126 |
|
| 1127 |
/* skip 14 bytes from CUESHEET_TRACK */ |
| 1128 |
if (!gst_byte_reader_skip (&reader, 14)) |
| 1129 |
goto error; |
| 1130 |
if (!gst_byte_reader_get_uint8 (&reader, &index)) |
| 1131 |
goto error; |
| 1132 |
/* add tracks in TOC */ |
| 1133 |
/* lead-out tack has number 170 or 255 */ |
| 1134 |
if (track_num != 170 && track_num != 255) { |
| 1135 |
prev_entry = cur_entry; |
| 1136 |
/* previous track stop time = current track start time */ |
| 1137 |
if (prev_entry != NULL) { |
| 1138 |
gst_toc_entry_get_start_stop_times (prev_entry, &start, NULL); |
| 1139 |
stop = |
| 1140 |
gst_util_uint64_scale_round (offset, GST_SECOND, |
| 1141 |
flacparse->samplerate); |
| 1142 |
gst_toc_entry_set_start_stop_times (prev_entry, start, stop); |
| 1143 |
} |
| 1144 |
id = g_strdup_printf ("%08x", track_num); |
| 1145 |
cur_entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id); |
| 1146 |
g_free (id); |
| 1147 |
start = |
| 1148 |
gst_util_uint64_scale_round (offset, GST_SECOND, |
| 1149 |
flacparse->samplerate); |
| 1150 |
gst_toc_entry_set_start_stop_times (cur_entry, start, -1); |
| 1151 |
/* add ISRC as tag in track */ |
| 1152 |
if (strlen (isrc) != 0) { |
| 1153 |
tags = gst_tag_list_new_empty (); |
| 1154 |
gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ISRC, isrc, NULL); |
| 1155 |
gst_toc_entry_set_tags (cur_entry, tags); |
| 1156 |
} |
| 1157 |
gst_toc_append_entry (toc, cur_entry); |
| 1158 |
/* CUESHEET_TRACK_INDEX */ |
| 1159 |
/* https://xiph.org/flac/format.html#cuesheet_track_index */ |
| 1160 |
for (j = 0; j < index; j++) { |
| 1161 |
if (!gst_byte_reader_skip (&reader, 12)) |
| 1162 |
goto error; |
| 1163 |
} |
| 1164 |
} else { |
| 1165 |
/* set stop time in last track */ |
| 1166 |
stop = |
| 1167 |
gst_util_uint64_scale_round (offset, GST_SECOND, |
| 1168 |
flacparse->samplerate); |
| 1169 |
gst_toc_entry_set_start_stop_times (cur_entry, start, stop); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* send data as TOC */ |
| 1174 |
if (!flacparse->toc) |
| 1175 |
flacparse->toc = toc; |
| 1176 |
|
| 1177 |
gst_buffer_unmap (buffer, &map); |
| 1178 |
return TRUE; |
| 1179 |
|
| 1180 |
error: |
| 1181 |
GST_ERROR_OBJECT (flacparse, "Error reading data"); |
| 1182 |
gst_buffer_unmap (buffer, &map); |
| 1183 |
return FALSE; |
| 1184 |
} |
| 1185 |
|
| 1186 |
static gboolean |
| 1187 |
gst_flac_parse_handle_picture (GstFlacParse * flacparse, GstBuffer * buffer) |
| 1188 |
{ |
| 1189 |
GstByteReader reader; |
| 1190 |
GstMapInfo map; |
| 1191 |
guint32 img_len = 0, img_type = 0; |
| 1192 |
guint32 img_mimetype_len = 0, img_description_len = 0; |
| 1193 |
const guint8 *img_data; |
| 1194 |
|
| 1195 |
gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 1196 |
gst_byte_reader_init (&reader, map.data, map.size); |
| 1197 |
|
| 1198 |
if (!gst_byte_reader_skip (&reader, 4)) |
| 1199 |
goto error; |
| 1200 |
|
| 1201 |
if (!gst_byte_reader_get_uint32_be (&reader, &img_type)) |
| 1202 |
goto error; |
| 1203 |
|
| 1204 |
if (!gst_byte_reader_get_uint32_be (&reader, &img_mimetype_len)) |
| 1205 |
goto error; |
| 1206 |
if (!gst_byte_reader_skip (&reader, img_mimetype_len)) |
| 1207 |
goto error; |
| 1208 |
|
| 1209 |
if (!gst_byte_reader_get_uint32_be (&reader, &img_description_len)) |
| 1210 |
goto error; |
| 1211 |
if (!gst_byte_reader_skip (&reader, img_description_len)) |
| 1212 |
goto error; |
| 1213 |
|
| 1214 |
if (!gst_byte_reader_skip (&reader, 4 * 4)) |
| 1215 |
goto error; |
| 1216 |
|
| 1217 |
if (!gst_byte_reader_get_uint32_be (&reader, &img_len)) |
| 1218 |
goto error; |
| 1219 |
|
| 1220 |
if (!gst_byte_reader_get_data (&reader, img_len, &img_data)) |
| 1221 |
goto error; |
| 1222 |
|
| 1223 |
GST_INFO_OBJECT (flacparse, "Got image of %d bytes", img_len); |
| 1224 |
|
| 1225 |
if (img_len > 0) { |
| 1226 |
if (flacparse->tags == NULL) |
| 1227 |
flacparse->tags = gst_tag_list_new_empty (); |
| 1228 |
|
| 1229 |
gst_tag_list_add_id3_image (flacparse->tags, img_data, img_len, img_type); |
| 1230 |
} |
| 1231 |
|
| 1232 |
gst_buffer_unmap (buffer, &map); |
| 1233 |
return TRUE; |
| 1234 |
|
| 1235 |
error: |
| 1236 |
GST_ERROR_OBJECT (flacparse, "Error reading data"); |
| 1237 |
gst_buffer_unmap (buffer, &map); |
| 1238 |
return FALSE; |
| 1239 |
} |
| 1240 |
|
| 1241 |
static gboolean |
| 1242 |
gst_flac_parse_handle_seektable (GstFlacParse * flacparse, GstBuffer * buffer) |
| 1243 |
{ |
| 1244 |
|
| 1245 |
GST_DEBUG_OBJECT (flacparse, "storing seektable"); |
| 1246 |
/* only store for now; |
| 1247 |
* offset of the first frame is needed to get real info */ |
| 1248 |
if (flacparse->seektable) |
| 1249 |
gst_buffer_unref (flacparse->seektable); |
| 1250 |
flacparse->seektable = gst_buffer_ref (buffer); |
| 1251 |
|
| 1252 |
return TRUE; |
| 1253 |
} |
| 1254 |
|
| 1255 |
static void |
| 1256 |
gst_flac_parse_process_seektable (GstFlacParse * flacparse, gint64 boffset) |
| 1257 |
{ |
| 1258 |
GstByteReader br; |
| 1259 |
gint64 offset = 0, samples = 0; |
| 1260 |
GstMapInfo map; |
| 1261 |
|
| 1262 |
GST_DEBUG_OBJECT (flacparse, |
| 1263 |
"parsing seektable; base offset %" G_GINT64_FORMAT, boffset); |
| 1264 |
|
| 1265 |
if (boffset <= 0) |
| 1266 |
goto exit; |
| 1267 |
|
| 1268 |
gst_buffer_map (flacparse->seektable, &map, GST_MAP_READ); |
| 1269 |
gst_byte_reader_init (&br, map.data, map.size); |
| 1270 |
|
| 1271 |
/* skip header */ |
| 1272 |
if (!gst_byte_reader_skip (&br, 4)) |
| 1273 |
goto done; |
| 1274 |
|
| 1275 |
/* seekpoints */ |
| 1276 |
while (gst_byte_reader_get_remaining (&br)) { |
| 1277 |
if (!gst_byte_reader_get_int64_be (&br, &samples)) |
| 1278 |
break; |
| 1279 |
if (!gst_byte_reader_get_int64_be (&br, &offset)) |
| 1280 |
break; |
| 1281 |
if (!gst_byte_reader_skip (&br, 2)) |
| 1282 |
break; |
| 1283 |
|
| 1284 |
GST_LOG_OBJECT (flacparse, "samples %" G_GINT64_FORMAT " -> offset %" |
| 1285 |
G_GINT64_FORMAT, samples, offset); |
| 1286 |
|
| 1287 |
/* sanity check */ |
| 1288 |
if (G_LIKELY (offset > 0 && samples > 0)) { |
| 1289 |
gst_base_parse_add_index_entry (GST_BASE_PARSE (flacparse), |
| 1290 |
boffset + offset, gst_util_uint64_scale (samples, GST_SECOND, |
| 1291 |
flacparse->samplerate), TRUE, FALSE); |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
done: |
| 1296 |
gst_buffer_unmap (flacparse->seektable, &map); |
| 1297 |
exit: |
| 1298 |
gst_buffer_unref (flacparse->seektable); |
| 1299 |
flacparse->seektable = NULL; |
| 1300 |
} |
| 1301 |
|
| 1302 |
static void |
| 1303 |
_value_array_append_buffer (GValue * array_val, GstBuffer * buf) |
| 1304 |
{ |
| 1305 |
GValue value = { 0, }; |
| 1306 |
|
| 1307 |
g_value_init (&value, GST_TYPE_BUFFER); |
| 1308 |
/* copy buffer to avoid problems with circular refcounts */ |
| 1309 |
buf = gst_buffer_copy (buf); |
| 1310 |
/* again, for good measure */ |
| 1311 |
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER); |
| 1312 |
gst_value_set_buffer (&value, buf); |
| 1313 |
gst_buffer_unref (buf); |
| 1314 |
gst_value_array_append_value (array_val, &value); |
| 1315 |
g_value_unset (&value); |
| 1316 |
} |
| 1317 |
|
| 1318 |
static GstFlowReturn |
| 1319 |
gst_flac_parse_handle_headers (GstFlacParse * flacparse) |
| 1320 |
{ |
| 1321 |
GstBuffer *vorbiscomment = NULL; |
| 1322 |
GstBuffer *streaminfo = NULL; |
| 1323 |
GstBuffer *marker = NULL; |
| 1324 |
GValue array = { 0, }; |
| 1325 |
GstCaps *caps; |
| 1326 |
GList *l; |
| 1327 |
GstFlowReturn res = GST_FLOW_OK; |
| 1328 |
gboolean is_streaminfo_last = FALSE; |
| 1329 |
|
| 1330 |
caps = gst_caps_new_simple ("audio/x-flac", |
| 1331 |
"channels", G_TYPE_INT, flacparse->channels, |
| 1332 |
"framed", G_TYPE_BOOLEAN, TRUE, |
| 1333 |
"rate", G_TYPE_INT, flacparse->samplerate, NULL); |
| 1334 |
|
| 1335 |
if (!flacparse->headers) |
| 1336 |
goto push_headers; |
| 1337 |
|
| 1338 |
for (l = flacparse->headers; l; l = l->next) { |
| 1339 |
GstBuffer *header = l->data; |
| 1340 |
GstMapInfo map; |
| 1341 |
|
| 1342 |
gst_buffer_map (header, &map, GST_MAP_READ); |
| 1343 |
|
| 1344 |
GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_HEADER); |
| 1345 |
|
| 1346 |
if (map.size == 4 && memcmp (map.data, "fLaC", 4) == 0) { |
| 1347 |
marker = header; |
| 1348 |
} else if (map.size > 1 && (map.data[0] & 0x7f) == 0) { |
| 1349 |
streaminfo = header; |
| 1350 |
is_streaminfo_last = (map.data[0] & 0x80) != 0; |
| 1351 |
} else if (map.size > 1 && (map.data[0] & 0x7f) == 4) { |
| 1352 |
vorbiscomment = header; |
| 1353 |
} |
| 1354 |
|
| 1355 |
gst_buffer_unmap (header, &map); |
| 1356 |
} |
| 1357 |
|
| 1358 |
/* at least this one we can generate easily |
| 1359 |
* to provide full headers downstream */ |
| 1360 |
if (vorbiscomment == NULL && streaminfo != NULL) { |
| 1361 |
GST_DEBUG_OBJECT (flacparse, |
| 1362 |
"missing vorbiscomment header; generating dummy"); |
| 1363 |
/* this vorbiscomment header is inserted after streaminfo and inherits its last-metadata-block flag */ |
| 1364 |
vorbiscomment = |
| 1365 |
gst_flac_parse_generate_vorbiscomment (flacparse, is_streaminfo_last); |
| 1366 |
flacparse->headers = |
| 1367 |
g_list_insert (flacparse->headers, vorbiscomment, |
| 1368 |
g_list_index (flacparse->headers, streaminfo) + 1); |
| 1369 |
} |
| 1370 |
|
| 1371 |
if (marker == NULL || streaminfo == NULL || vorbiscomment == NULL) { |
| 1372 |
GST_WARNING_OBJECT (flacparse, |
| 1373 |
"missing header %p %p %p, muxing into container " |
| 1374 |
"formats may be broken", marker, streaminfo, vorbiscomment); |
| 1375 |
goto push_headers; |
| 1376 |
} |
| 1377 |
|
| 1378 |
g_value_init (&array, GST_TYPE_ARRAY); |
| 1379 |
|
| 1380 |
/* add marker including STREAMINFO header */ |
| 1381 |
{ |
| 1382 |
GstBuffer *buf; |
| 1383 |
guint16 num; |
| 1384 |
GstMapInfo sinfomap, writemap; |
| 1385 |
|
| 1386 |
gst_buffer_map (streaminfo, &sinfomap, GST_MAP_READ); |
| 1387 |
|
| 1388 |
/* minus one for the marker that is merged with streaminfo here */ |
| 1389 |
num = g_list_length (flacparse->headers) - 1; |
| 1390 |
|
| 1391 |
buf = gst_buffer_new_and_alloc (13 + sinfomap.size); |
| 1392 |
gst_buffer_map (buf, &writemap, GST_MAP_WRITE); |
| 1393 |
|
| 1394 |
writemap.data[0] = 0x7f; |
| 1395 |
memcpy (writemap.data + 1, "FLAC", 4); |
| 1396 |
writemap.data[5] = 0x01; /* mapping version major */ |
| 1397 |
writemap.data[6] = 0x00; /* mapping version minor */ |
| 1398 |
writemap.data[7] = (num & 0xFF00) >> 8; |
| 1399 |
writemap.data[8] = (num & 0x00FF) >> 0; |
| 1400 |
memcpy (writemap.data + 9, "fLaC", 4); |
| 1401 |
memcpy (writemap.data + 13, sinfomap.data, sinfomap.size); |
| 1402 |
/* clear the last-metadata-block flag because a VORBISCOMMENT always follows */ |
| 1403 |
writemap.data[13] = 0x00; /* is_last = 0; type = 0; */ |
| 1404 |
_value_array_append_buffer (&array, buf); |
| 1405 |
|
| 1406 |
gst_buffer_unmap (streaminfo, &sinfomap); |
| 1407 |
gst_buffer_unmap (buf, &writemap); |
| 1408 |
gst_buffer_unref (buf); |
| 1409 |
} |
| 1410 |
|
| 1411 |
/* add other headers, including VORBISCOMMENT */ |
| 1412 |
for (l = flacparse->headers; l; l = l->next) { |
| 1413 |
if (GST_BUFFER_CAST (l->data) != marker && |
| 1414 |
GST_BUFFER_CAST (l->data) != streaminfo) { |
| 1415 |
_value_array_append_buffer (&array, GST_BUFFER_CAST (l->data)); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
|
| 1419 |
gst_structure_set_value (gst_caps_get_structure (caps, 0), |
| 1420 |
"streamheader", &array); |
| 1421 |
g_value_unset (&array); |
| 1422 |
|
| 1423 |
push_headers: |
| 1424 |
|
| 1425 |
gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse)), caps); |
| 1426 |
gst_caps_unref (caps); |
| 1427 |
|
| 1428 |
/* push header buffers; update caps, so when we push the first buffer the |
| 1429 |
* negotiated caps will change to caps that include the streamheader field */ |
| 1430 |
while (flacparse->headers) { |
| 1431 |
GstBuffer *buf = GST_BUFFER (flacparse->headers->data); |
| 1432 |
GstBaseParseFrame frame; |
| 1433 |
|
| 1434 |
flacparse->headers = |
| 1435 |
g_list_delete_link (flacparse->headers, flacparse->headers); |
| 1436 |
buf = gst_buffer_make_writable (buf); |
| 1437 |
|
| 1438 |
/* init, set and give away frame */ |
| 1439 |
gst_base_parse_frame_init (&frame); |
| 1440 |
frame.buffer = buf; |
| 1441 |
frame.overhead = -1; |
| 1442 |
res = gst_base_parse_push_frame (GST_BASE_PARSE (flacparse), &frame); |
| 1443 |
gst_base_parse_frame_free (&frame); |
| 1444 |
if (res != GST_FLOW_OK) |
| 1445 |
break; |
| 1446 |
} |
| 1447 |
g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL); |
| 1448 |
g_list_free (flacparse->headers); |
| 1449 |
flacparse->headers = NULL; |
| 1450 |
|
| 1451 |
return res; |
| 1452 |
} |
| 1453 |
|
| 1454 |
/* empty vorbiscomment */ |
| 1455 |
static GstBuffer * |
| 1456 |
gst_flac_parse_generate_vorbiscomment (GstFlacParse * flacparse, |
| 1457 |
gboolean is_last) |
| 1458 |
{ |
| 1459 |
GstTagList *taglist = gst_tag_list_new_empty (); |
| 1460 |
guchar header[4]; |
| 1461 |
guint size; |
| 1462 |
GstBuffer *vorbiscomment; |
| 1463 |
GstMapInfo map; |
| 1464 |
|
| 1465 |
header[0] = (is_last ? 0x80 : 0x00) | 0x04; /* is_last may vary; type = 4; */ |
| 1466 |
|
| 1467 |
vorbiscomment = |
| 1468 |
gst_tag_list_to_vorbiscomment_buffer (taglist, header, |
| 1469 |
sizeof (header), NULL); |
| 1470 |
gst_tag_list_unref (taglist); |
| 1471 |
|
| 1472 |
gst_buffer_map (vorbiscomment, &map, GST_MAP_WRITE); |
| 1473 |
|
| 1474 |
/* Get rid of framing bit */ |
| 1475 |
if (map.data[map.size - 1] == 1) { |
| 1476 |
GstBuffer *sub; |
| 1477 |
|
| 1478 |
sub = |
| 1479 |
gst_buffer_copy_region (vorbiscomment, GST_BUFFER_COPY_ALL, 0, |
| 1480 |
map.size - 1); |
| 1481 |
gst_buffer_unmap (vorbiscomment, &map); |
| 1482 |
gst_buffer_unref (vorbiscomment); |
| 1483 |
vorbiscomment = sub; |
| 1484 |
gst_buffer_map (vorbiscomment, &map, GST_MAP_WRITE); |
| 1485 |
} |
| 1486 |
|
| 1487 |
size = map.size - 4; |
| 1488 |
map.data[1] = ((size & 0xFF0000) >> 16); |
| 1489 |
map.data[2] = ((size & 0x00FF00) >> 8); |
| 1490 |
map.data[3] = (size & 0x0000FF); |
| 1491 |
gst_buffer_unmap (vorbiscomment, &map); |
| 1492 |
gst_flac_parse_reset_buffer_time_and_offset (vorbiscomment); |
| 1493 |
|
| 1494 |
return vorbiscomment; |
| 1495 |
} |
| 1496 |
|
| 1497 |
static gboolean |
| 1498 |
gst_flac_parse_generate_headers (GstFlacParse * flacparse) |
| 1499 |
{ |
| 1500 |
GstBuffer *marker, *streaminfo; |
| 1501 |
GstMapInfo map; |
| 1502 |
|
| 1503 |
marker = gst_buffer_new_and_alloc (4); |
| 1504 |
gst_buffer_map (marker, &map, GST_MAP_WRITE); |
| 1505 |
memcpy (map.data, "fLaC", 4); |
| 1506 |
gst_buffer_unmap (marker, &map); |
| 1507 |
gst_flac_parse_reset_buffer_time_and_offset (marker); |
| 1508 |
flacparse->headers = g_list_append (flacparse->headers, marker); |
| 1509 |
|
| 1510 |
streaminfo = gst_buffer_new_and_alloc (4 + 34); |
| 1511 |
gst_buffer_map (streaminfo, &map, GST_MAP_WRITE); |
| 1512 |
memset (map.data, 0, 4 + 34); |
| 1513 |
|
| 1514 |
/* metadata block header */ |
| 1515 |
map.data[0] = 0x00; /* is_last = 0; type = 0; */ |
| 1516 |
map.data[1] = 0x00; /* length = 34; */ |
| 1517 |
map.data[2] = 0x00; |
| 1518 |
map.data[3] = 0x22; |
| 1519 |
|
| 1520 |
/* streaminfo */ |
| 1521 |
|
| 1522 |
map.data[4] = (flacparse->block_size >> 8) & 0xff; /* min blocksize = blocksize; */ |
| 1523 |
map.data[5] = (flacparse->block_size) & 0xff; |
| 1524 |
map.data[6] = (flacparse->block_size >> 8) & 0xff; /* max blocksize = blocksize; */ |
| 1525 |
map.data[7] = (flacparse->block_size) & 0xff; |
| 1526 |
|
| 1527 |
map.data[8] = 0x00; /* min framesize = 0; */ |
| 1528 |
map.data[9] = 0x00; |
| 1529 |
map.data[10] = 0x00; |
| 1530 |
map.data[11] = 0x00; /* max framesize = 0; */ |
| 1531 |
map.data[12] = 0x00; |
| 1532 |
map.data[13] = 0x00; |
| 1533 |
|
| 1534 |
map.data[14] = (flacparse->samplerate >> 12) & 0xff; |
| 1535 |
map.data[15] = (flacparse->samplerate >> 4) & 0xff; |
| 1536 |
map.data[16] = (flacparse->samplerate >> 0) & 0xf0; |
| 1537 |
|
| 1538 |
map.data[16] |= (flacparse->channels - 1) << 1; |
| 1539 |
|
| 1540 |
map.data[16] |= ((flacparse->bps - 1) >> 4) & 0x01; |
| 1541 |
map.data[17] = (((flacparse->bps - 1)) & 0x0f) << 4; |
| 1542 |
|
| 1543 |
{ |
| 1544 |
gint64 duration; |
| 1545 |
|
| 1546 |
if (gst_pad_peer_query_duration (GST_BASE_PARSE_SINK_PAD (flacparse), |
| 1547 |
GST_FORMAT_TIME, &duration) && duration != -1) { |
| 1548 |
duration = GST_CLOCK_TIME_TO_FRAMES (duration, flacparse->samplerate); |
| 1549 |
|
| 1550 |
map.data[17] |= (duration >> 32) & 0xff; |
| 1551 |
map.data[18] |= (duration >> 24) & 0xff; |
| 1552 |
map.data[19] |= (duration >> 16) & 0xff; |
| 1553 |
map.data[20] |= (duration >> 8) & 0xff; |
| 1554 |
map.data[21] |= (duration >> 0) & 0xff; |
| 1555 |
} |
| 1556 |
} |
| 1557 |
/* MD5 = 0; */ |
| 1558 |
|
| 1559 |
gst_buffer_unmap (streaminfo, &map); |
| 1560 |
gst_flac_parse_reset_buffer_time_and_offset (streaminfo); |
| 1561 |
flacparse->headers = g_list_append (flacparse->headers, streaminfo); |
| 1562 |
|
| 1563 |
flacparse->headers = g_list_append (flacparse->headers, |
| 1564 |
gst_flac_parse_generate_vorbiscomment (flacparse, TRUE)); |
| 1565 |
|
| 1566 |
return TRUE; |
| 1567 |
} |
| 1568 |
|
| 1569 |
static inline void |
| 1570 |
gst_flac_parse_reset_buffer_time_and_offset (GstBuffer * buffer) |
| 1571 |
{ |
| 1572 |
GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE; |
| 1573 |
GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE; |
| 1574 |
GST_BUFFER_OFFSET (buffer) = 0; |
| 1575 |
GST_BUFFER_OFFSET_END (buffer) = 0; |
| 1576 |
} |
| 1577 |
|
| 1578 |
/* Type 127 is invalid for a metadata block header & should |
| 1579 |
* be discarded _before_ calling this function */ |
| 1580 |
static gboolean |
| 1581 |
gst_flac_parse_handle_block_type (GstFlacParse * flacparse, guint type, |
| 1582 |
GstBuffer * sbuffer) |
| 1583 |
{ |
| 1584 |
gboolean ret = TRUE; |
| 1585 |
|
| 1586 |
switch (type) { |
| 1587 |
case 0: /* STREAMINFO */ |
| 1588 |
GST_INFO_OBJECT (flacparse, "STREAMINFO header"); |
| 1589 |
ret = gst_flac_parse_handle_streaminfo (flacparse, sbuffer); |
| 1590 |
break; |
| 1591 |
case 3: /* SEEKTABLE */ |
| 1592 |
GST_INFO_OBJECT (flacparse, "SEEKTABLE header"); |
| 1593 |
ret = gst_flac_parse_handle_seektable (flacparse, sbuffer); |
| 1594 |
break; |
| 1595 |
case 4: /* VORBIS_COMMENT */ |
| 1596 |
GST_INFO_OBJECT (flacparse, "VORBISCOMMENT header"); |
| 1597 |
ret = gst_flac_parse_handle_vorbiscomment (flacparse, sbuffer); |
| 1598 |
break; |
| 1599 |
case 5: /* CUESHEET */ |
| 1600 |
GST_INFO_OBJECT (flacparse, "CUESHEET header"); |
| 1601 |
ret = gst_flac_parse_handle_cuesheet (flacparse, sbuffer); |
| 1602 |
break; |
| 1603 |
case 6: /* PICTURE */ |
| 1604 |
GST_INFO_OBJECT (flacparse, "PICTURE header"); |
| 1605 |
ret = gst_flac_parse_handle_picture (flacparse, sbuffer); |
| 1606 |
break; |
| 1607 |
case 1: /* PADDING */ |
| 1608 |
GST_INFO_OBJECT (flacparse, "PADDING header"); |
| 1609 |
break; |
| 1610 |
case 2: /* APPLICATION */ |
| 1611 |
GST_INFO_OBJECT (flacparse, "APPLICATION header"); |
| 1612 |
break; |
| 1613 |
default: /* RESERVED */ |
| 1614 |
GST_INFO_OBJECT (flacparse, "Unhandled metadata header type '%u'", type); |
| 1615 |
GST_FIXME_OBJECT (flacparse, "FLAC version might not be fully supported"); |
| 1616 |
break; |
| 1617 |
} |
| 1618 |
|
| 1619 |
return ret; |
| 1620 |
} |
| 1621 |
|
| 1622 |
static GstFlowReturn |
| 1623 |
gst_flac_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame, |
| 1624 |
gint size) |
| 1625 |
{ |
| 1626 |
GstFlacParse *flacparse = GST_FLAC_PARSE (parse); |
| 1627 |
GstBuffer *buffer = frame->buffer, *sbuffer; |
| 1628 |
GstMapInfo map; |
| 1629 |
GstFlowReturn res = GST_FLOW_ERROR; |
| 1630 |
guint64 relative_sample_number; |
| 1631 |
|
| 1632 |
gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 1633 |
|
| 1634 |
if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) { |
| 1635 |
sbuffer = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0, size); |
| 1636 |
gst_flac_parse_reset_buffer_time_and_offset (sbuffer); |
| 1637 |
|
| 1638 |
/* 32 bits metadata block */ |
| 1639 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4); |
| 1640 |
flacparse->state = GST_FLAC_PARSE_STATE_HEADERS; |
| 1641 |
|
| 1642 |
flacparse->headers = g_list_append (flacparse->headers, sbuffer); |
| 1643 |
|
| 1644 |
res = GST_BASE_PARSE_FLOW_DROPPED; |
| 1645 |
} else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) { |
| 1646 |
gboolean is_last = map.data[0] >> 7; |
| 1647 |
guint type = (map.data[0] & 0x7F); |
| 1648 |
|
| 1649 |
if (type == 127) { |
| 1650 |
GST_WARNING_OBJECT (flacparse, "Invalid metadata block type 127"); |
| 1651 |
res = GST_BASE_PARSE_FLOW_DROPPED; |
| 1652 |
goto cleanup; |
| 1653 |
} |
| 1654 |
|
| 1655 |
GST_DEBUG_OBJECT (flacparse, "Handling metadata block of type %u", type); |
| 1656 |
|
| 1657 |
sbuffer = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0, size); |
| 1658 |
|
| 1659 |
if (gst_flac_parse_handle_block_type (flacparse, type, sbuffer)) { |
| 1660 |
gst_flac_parse_reset_buffer_time_and_offset (sbuffer); |
| 1661 |
flacparse->headers = g_list_append (flacparse->headers, sbuffer); |
| 1662 |
} else { |
| 1663 |
GST_WARNING_OBJECT (parse, "failed to parse header of type %u", type); |
| 1664 |
GST_MEMDUMP_OBJECT (parse, "bad header data", map.data, size); |
| 1665 |
|
| 1666 |
gst_buffer_unref (sbuffer); |
| 1667 |
|
| 1668 |
/* error out unless we have a STREAMINFO header */ |
| 1669 |
if (flacparse->samplerate == 0 || flacparse->bps == 0) |
| 1670 |
goto header_parsing_error; |
| 1671 |
|
| 1672 |
/* .. in which case just stop header parsing and try to find audio */ |
| 1673 |
is_last = TRUE; |
| 1674 |
} |
| 1675 |
|
| 1676 |
if (is_last) { |
| 1677 |
res = gst_flac_parse_handle_headers (flacparse); |
| 1678 |
|
| 1679 |
/* Minimal size of a frame header */ |
| 1680 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (9, |
| 1681 |
flacparse->min_framesize)); |
| 1682 |
flacparse->state = GST_FLAC_PARSE_STATE_DATA; |
| 1683 |
|
| 1684 |
if (res != GST_FLOW_OK) |
| 1685 |
goto cleanup; |
| 1686 |
} else { |
| 1687 |
/* Header length */ |
| 1688 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4); |
| 1689 |
} |
| 1690 |
|
| 1691 |
/* DROPPED because we pushed already or will push all headers manually */ |
| 1692 |
res = GST_BASE_PARSE_FLOW_DROPPED; |
| 1693 |
} else { |
| 1694 |
if (flacparse->offset != GST_BUFFER_OFFSET (buffer)) { |
| 1695 |
FrameHeaderCheckReturn ret; |
| 1696 |
FlacFrameHeader header; |
| 1697 |
|
| 1698 |
GST_DEBUG_OBJECT (flacparse, |
| 1699 |
"Unexpected offset %" G_GUINT64_FORMAT ", expected %" |
| 1700 |
G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer), flacparse->offset); |
| 1701 |
|
| 1702 |
flacparse->offset = GST_BUFFER_OFFSET (buffer); |
| 1703 |
ret = |
| 1704 |
gst_flac_parse_frame_header_is_valid (flacparse, |
| 1705 |
map.data, map.size, flacparse->offset, &header); |
| 1706 |
if (ret != FRAME_HEADER_VALID) { |
| 1707 |
GST_ERROR_OBJECT (flacparse, |
| 1708 |
"Baseclass didn't provide a complete frame"); |
| 1709 |
goto cleanup; |
| 1710 |
} |
| 1711 |
|
| 1712 |
/* We don't do any sanity checks here but assume that this frame is valid |
| 1713 |
* if we somehow got a complete frame from an unexpected offset. */ |
| 1714 |
gst_flac_parse_frame_header_update (flacparse, &header); |
| 1715 |
} |
| 1716 |
|
| 1717 |
if (flacparse->block_size == 0) { |
| 1718 |
GST_ERROR_OBJECT (flacparse, "Unparsed frame"); |
| 1719 |
goto cleanup; |
| 1720 |
} |
| 1721 |
|
| 1722 |
if (flacparse->seektable) |
| 1723 |
gst_flac_parse_process_seektable (flacparse, GST_BUFFER_OFFSET (buffer)); |
| 1724 |
|
| 1725 |
if (flacparse->state == GST_FLAC_PARSE_STATE_GENERATE_HEADERS) { |
| 1726 |
if (flacparse->blocking_strategy == 1) { |
| 1727 |
GST_WARNING_OBJECT (flacparse, |
| 1728 |
"Generating headers for variable blocksize streams not supported"); |
| 1729 |
|
| 1730 |
res = gst_flac_parse_handle_headers (flacparse); |
| 1731 |
} else { |
| 1732 |
GST_DEBUG_OBJECT (flacparse, "Generating headers"); |
| 1733 |
|
| 1734 |
if (!gst_flac_parse_generate_headers (flacparse)) |
| 1735 |
goto cleanup; |
| 1736 |
|
| 1737 |
res = gst_flac_parse_handle_headers (flacparse); |
| 1738 |
} |
| 1739 |
flacparse->state = GST_FLAC_PARSE_STATE_DATA; |
| 1740 |
if (res != GST_FLOW_OK) |
| 1741 |
goto cleanup; |
| 1742 |
} |
| 1743 |
|
| 1744 |
/* also cater for oggmux metadata */ |
| 1745 |
relative_sample_number = |
| 1746 |
flacparse->sample_number - flacparse->first_sample_number; |
| 1747 |
if (flacparse->blocking_strategy == 0) { |
| 1748 |
GST_BUFFER_PTS (buffer) = |
| 1749 |
gst_util_uint64_scale (relative_sample_number, |
| 1750 |
flacparse->fixed_block_size * GST_SECOND, flacparse->samplerate); |
| 1751 |
GST_BUFFER_OFFSET_END (buffer) = |
| 1752 |
relative_sample_number * flacparse->fixed_block_size + |
| 1753 |
flacparse->block_size; |
| 1754 |
} else { |
| 1755 |
GST_BUFFER_PTS (buffer) = |
| 1756 |
gst_util_uint64_scale (relative_sample_number, GST_SECOND, |
| 1757 |
flacparse->samplerate); |
| 1758 |
GST_BUFFER_OFFSET_END (buffer) = |
| 1759 |
relative_sample_number + flacparse->block_size; |
| 1760 |
} |
| 1761 |
|
| 1762 |
GST_BUFFER_DTS (buffer) = GST_BUFFER_PTS (buffer); |
| 1763 |
GST_BUFFER_OFFSET (buffer) = |
| 1764 |
gst_util_uint64_scale (GST_BUFFER_OFFSET_END (buffer), GST_SECOND, |
| 1765 |
flacparse->samplerate); |
| 1766 |
GST_BUFFER_DURATION (buffer) = |
| 1767 |
GST_BUFFER_OFFSET (buffer) - GST_BUFFER_PTS (buffer); |
| 1768 |
|
| 1769 |
/* To simplify, we just assume that it's a fixed size header and ignore |
| 1770 |
* subframe headers. The first could lead us to be off by 88 bits and |
| 1771 |
* the second even less, so the total inaccuracy is negligible. */ |
| 1772 |
frame->overhead = 7; |
| 1773 |
|
| 1774 |
/* Minimal size of a frame header */ |
| 1775 |
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (9, |
| 1776 |
flacparse->min_framesize)); |
| 1777 |
|
| 1778 |
flacparse->offset = -1; |
| 1779 |
flacparse->blocking_strategy = 0; |
| 1780 |
flacparse->sample_number = 0; |
| 1781 |
res = GST_FLOW_OK; |
| 1782 |
} |
| 1783 |
|
| 1784 |
cleanup: |
| 1785 |
gst_buffer_unmap (buffer, &map); |
| 1786 |
return res; |
| 1787 |
|
| 1788 |
header_parsing_error: |
| 1789 |
GST_ELEMENT_ERROR (flacparse, STREAM, DECODE, (NULL), |
| 1790 |
("Failed to parse headers")); |
| 1791 |
goto cleanup; |
| 1792 |
} |
| 1793 |
|
| 1794 |
static GstFlowReturn |
| 1795 |
gst_flac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 1796 |
{ |
| 1797 |
GstFlacParse *flacparse = GST_FLAC_PARSE (parse); |
| 1798 |
|
| 1799 |
if (!flacparse->sent_codec_tag) { |
| 1800 |
GstCaps *caps; |
| 1801 |
|
| 1802 |
if (flacparse->tags == NULL) |
| 1803 |
flacparse->tags = gst_tag_list_new_empty (); |
| 1804 |
|
| 1805 |
/* codec tag */ |
| 1806 |
caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse)); |
| 1807 |
if (G_UNLIKELY (caps == NULL)) { |
| 1808 |
if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) { |
| 1809 |
GST_INFO_OBJECT (parse, "Src pad is flushing"); |
| 1810 |
return GST_FLOW_FLUSHING; |
| 1811 |
} |
| 1812 |
GST_INFO_OBJECT (parse, "Src pad is not negotiated!"); |
| 1813 |
return GST_FLOW_NOT_NEGOTIATED; |
| 1814 |
} |
| 1815 |
gst_pb_utils_add_codec_description_to_tag_list (flacparse->tags, |
| 1816 |
GST_TAG_AUDIO_CODEC, caps); |
| 1817 |
gst_caps_unref (caps); |
| 1818 |
|
| 1819 |
/* Announce our pending tags */ |
| 1820 |
gst_base_parse_merge_tags (parse, flacparse->tags, GST_TAG_MERGE_REPLACE); |
| 1821 |
|
| 1822 |
/* also signals the end of first-frame processing */ |
| 1823 |
flacparse->sent_codec_tag = TRUE; |
| 1824 |
} |
| 1825 |
|
| 1826 |
/* Push toc */ |
| 1827 |
if (flacparse->toc) { |
| 1828 |
gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (flacparse), |
| 1829 |
gst_event_new_toc (flacparse->toc, FALSE)); |
| 1830 |
} |
| 1831 |
|
| 1832 |
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP; |
| 1833 |
|
| 1834 |
return GST_FLOW_OK; |
| 1835 |
} |
| 1836 |
|
| 1837 |
static gboolean |
| 1838 |
gst_flac_parse_convert (GstBaseParse * parse, |
| 1839 |
GstFormat src_format, gint64 src_value, GstFormat dest_format, |
| 1840 |
gint64 * dest_value) |
| 1841 |
{ |
| 1842 |
GstFlacParse *flacparse = GST_FLAC_PARSE (parse); |
| 1843 |
|
| 1844 |
if (flacparse->samplerate > 0) { |
| 1845 |
if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) { |
| 1846 |
if (src_value != -1) |
| 1847 |
*dest_value = |
| 1848 |
gst_util_uint64_scale (src_value, GST_SECOND, |
| 1849 |
flacparse->samplerate); |
| 1850 |
else |
| 1851 |
*dest_value = -1; |
| 1852 |
return TRUE; |
| 1853 |
} else if (src_format == GST_FORMAT_TIME && |
| 1854 |
dest_format == GST_FORMAT_DEFAULT) { |
| 1855 |
if (src_value != -1) |
| 1856 |
*dest_value = |
| 1857 |
gst_util_uint64_scale (src_value, flacparse->samplerate, |
| 1858 |
GST_SECOND); |
| 1859 |
else |
| 1860 |
*dest_value = -1; |
| 1861 |
return TRUE; |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
return GST_BASE_PARSE_CLASS (parent_class)->convert (parse, src_format, |
| 1866 |
src_value, dest_format, dest_value); |
| 1867 |
} |
| 1868 |
|
| 1869 |
static gboolean |
| 1870 |
gst_flac_parse_src_event (GstBaseParse * parse, GstEvent * event) |
| 1871 |
{ |
| 1872 |
GstFlacParse *flacparse = GST_FLAC_PARSE (parse); |
| 1873 |
gboolean res = FALSE; |
| 1874 |
|
| 1875 |
switch (GST_EVENT_TYPE (event)) { |
| 1876 |
case GST_EVENT_TOC_SELECT: |
| 1877 |
{ |
| 1878 |
GstTocEntry *entry = NULL; |
| 1879 |
GstEvent *seek_event; |
| 1880 |
GstToc *toc = NULL; |
| 1881 |
gint64 start_pos; |
| 1882 |
gchar *uid = NULL; |
| 1883 |
|
| 1884 |
/* FIXME: some locking would be good */ |
| 1885 |
if (flacparse->toc) |
| 1886 |
toc = gst_toc_ref (flacparse->toc); |
| 1887 |
|
| 1888 |
if (toc != NULL) { |
| 1889 |
gst_event_parse_toc_select (event, &uid); |
| 1890 |
if (uid != NULL) { |
| 1891 |
entry = gst_toc_find_entry (toc, uid); |
| 1892 |
if (entry != NULL) { |
| 1893 |
gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL); |
| 1894 |
|
| 1895 |
/* FIXME: use segment rate here instead? */ |
| 1896 |
seek_event = gst_event_new_seek (1.0, |
| 1897 |
GST_FORMAT_TIME, |
| 1898 |
GST_SEEK_FLAG_FLUSH, |
| 1899 |
GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_NONE, -1); |
| 1900 |
|
| 1901 |
res = |
| 1902 |
GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, |
| 1903 |
seek_event); |
| 1904 |
|
| 1905 |
} else { |
| 1906 |
GST_WARNING_OBJECT (parse, "no TOC entry with given UID: %s", uid); |
| 1907 |
} |
| 1908 |
g_free (uid); |
| 1909 |
} |
| 1910 |
gst_toc_unref (toc); |
| 1911 |
} else { |
| 1912 |
GST_DEBUG_OBJECT (flacparse, "no TOC to select"); |
| 1913 |
} |
| 1914 |
gst_event_unref (event); |
| 1915 |
break; |
| 1916 |
} |
| 1917 |
default: |
| 1918 |
res = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event); |
| 1919 |
break; |
| 1920 |
} |
| 1921 |
return res; |
| 1922 |
} |
| 1923 |
|
| 1924 |
static void |
| 1925 |
remove_fields (GstCaps * caps) |
| 1926 |
{ |
| 1927 |
guint i, n; |
| 1928 |
|
| 1929 |
n = gst_caps_get_size (caps); |
| 1930 |
for (i = 0; i < n; i++) { |
| 1931 |
GstStructure *s = gst_caps_get_structure (caps, i); |
| 1932 |
|
| 1933 |
gst_structure_remove_field (s, "framed"); |
| 1934 |
} |
| 1935 |
} |
| 1936 |
|
| 1937 |
static GstCaps * |
| 1938 |
gst_flac_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter) |
| 1939 |
{ |
| 1940 |
GstCaps *peercaps, *templ; |
| 1941 |
GstCaps *res; |
| 1942 |
|
| 1943 |
templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse)); |
| 1944 |
if (filter) { |
| 1945 |
GstCaps *fcopy = gst_caps_copy (filter); |
| 1946 |
/* Remove the fields we convert */ |
| 1947 |
remove_fields (fcopy); |
| 1948 |
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy); |
| 1949 |
gst_caps_unref (fcopy); |
| 1950 |
} else |
| 1951 |
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL); |
| 1952 |
|
| 1953 |
if (peercaps) { |
| 1954 |
/* Remove the framed field */ |
| 1955 |
peercaps = gst_caps_make_writable (peercaps); |
| 1956 |
remove_fields (peercaps); |
| 1957 |
|
| 1958 |
res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST); |
| 1959 |
gst_caps_unref (peercaps); |
| 1960 |
gst_caps_unref (templ); |
| 1961 |
} else { |
| 1962 |
res = templ; |
| 1963 |
} |
| 1964 |
|
| 1965 |
if (filter) { |
| 1966 |
GstCaps *intersection; |
| 1967 |
|
| 1968 |
intersection = |
| 1969 |
gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST); |
| 1970 |
gst_caps_unref (res); |
| 1971 |
res = intersection; |
| 1972 |
} |
| 1973 |
|
| 1974 |
return res; |
| 1975 |
} |
| 1976 |
|
| 1977 |
static gboolean |
| 1978 |
gst_flac_parse_set_sink_caps (GstBaseParse * parse, GstCaps * caps) |
| 1979 |
{ |
| 1980 |
GstCaps *current_caps; |
| 1981 |
GstFlacParse *flacparse = GST_FLAC_PARSE (parse); |
| 1982 |
|
| 1983 |
/* If caps are changing, drain any pending frames we have so that afterwards |
| 1984 |
* we can potentially accept a new stream that is starting with the FLAC |
| 1985 |
* headers again. If headers appear in the middle of the stream we can't |
| 1986 |
* detect them |
| 1987 |
*/ |
| 1988 |
gst_base_parse_drain (parse); |
| 1989 |
|
| 1990 |
/* If the caps did really change we need to reset the parser */ |
| 1991 |
current_caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (parse)); |
| 1992 |
if (current_caps) { |
| 1993 |
if (!gst_caps_is_strictly_equal (caps, current_caps)) { |
| 1994 |
GST_DEBUG_OBJECT (flacparse, "Reset parser on sink pad caps change"); |
| 1995 |
gst_flac_parse_stop (parse); |
| 1996 |
gst_flac_parse_start (parse); |
| 1997 |
} |
| 1998 |
gst_caps_unref (current_caps); |
| 1999 |
} |
| 2000 |
|
| 2001 |
return TRUE; |
| 2002 |
} |