221
gst_matroska_track_free (GstMatroskaTrackContext * track)
223
g_free (track->codec_id);
224
g_free (track->codec_name);
225
g_free (track->name);
226
g_free (track->language);
227
g_free (track->codec_priv);
229
if (track->encodings != NULL) {
232
for (i = 0; i < track->encodings->len; ++i) {
233
GstMatroskaTrackEncoding *enc = &g_array_index (track->encodings,
234
GstMatroskaTrackEncoding,
237
g_free (enc->comp_settings);
239
g_array_free (track->encodings, TRUE);
216
246
gst_matroska_demux_reset (GstElement * element)
218
248
GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
317
gst_matroska_demux_encoding_cmp (gconstpointer a, gconstpointer b)
319
const GstMatroskaTrackEncoding *enc_a;
320
const GstMatroskaTrackEncoding *enc_b;
322
enc_a = (const GstMatroskaTrackEncoding *) a;
323
enc_b = (const GstMatroskaTrackEncoding *) b;
325
return (gint) enc_b->order - (gint) enc_a->order;
329
gst_matroska_demux_read_track_encodings (GstEbmlRead * ebml,
330
GstMatroskaDemux * demux, GstMatroskaTrackContext * context)
335
if (!gst_ebml_read_master (ebml, &id))
339
g_array_sized_new (FALSE, FALSE, sizeof (GstMatroskaTrackEncoding), 1);
342
if (!gst_ebml_peek_id (ebml, &demux->level_up, &id)) {
345
} else if (demux->level_up > 0) {
351
case GST_MATROSKA_ID_CONTENTENCODING:{
352
GstMatroskaTrackEncoding enc = { 0, };
354
if (!gst_ebml_read_master (ebml, &id)) {
360
if (!gst_ebml_peek_id (ebml, &demux->level_up, &id)) {
363
} else if (demux->level_up > 0) {
369
case GST_MATROSKA_ID_CONTENTENCODINGORDER:{
372
if (!gst_ebml_read_uint (ebml, &id, &num)) {
379
case GST_MATROSKA_ID_CONTENTENCODINGSCOPE:{
382
if (!gst_ebml_read_uint (ebml, &id, &num)) {
387
GST_WARNING ("Unknown scope value in contents encoding.");
392
case GST_MATROSKA_ID_CONTENTENCODINGTYPE:{
395
if (!gst_ebml_read_uint (ebml, &id, &num)) {
400
GST_WARNING ("Unknown type value in contents encoding.");
405
case GST_MATROSKA_ID_CONTENTCOMPRESSION:{
407
if (!gst_ebml_read_master (ebml, &id)) {
414
if (!gst_ebml_peek_id (ebml, &demux->level_up, &id)) {
417
} else if (demux->level_up > 0) {
423
case GST_MATROSKA_ID_CONTENTCOMPALGO:{
426
if (!gst_ebml_read_uint (ebml, &id, &num)) {
431
GST_WARNING ("Unknown scope value in encoding compalgo.");
436
case GST_MATROSKA_ID_CONTENTCOMPSETTINGS:{
441
if (!gst_ebml_read_binary (ebml, &id, &data, &size)) {
445
enc.comp_settings = data;
446
enc.comp_settings_length = size;
450
GST_WARNING ("Unknown track compression header entry 0x%x"
452
if (!gst_ebml_read_skip (ebml))
457
if (demux->level_up) {
465
case GST_MATROSKA_ID_CONTENTENCRYPTION:
466
GST_WARNING ("Encrypted tracks not yet supported");
470
("Unknown track encoding header entry 0x%x - ignoring", id);
471
if (!gst_ebml_read_skip (ebml))
476
if (demux->level_up) {
482
g_array_append_val (context->encodings, enc);
487
GST_WARNING ("Unknown track encodings header entry 0x%x - ignoring",
489
if (!gst_ebml_read_skip (ebml))
494
if (demux->level_up) {
500
/* Sort encodings according to their order */
501
g_array_sort (context->encodings, gst_matroska_demux_encoding_cmp);
292
507
gst_matroska_demux_add_stream (GstMatroskaDemux * demux)
2043
2265
static gboolean
2266
gst_matroska_demux_push_hdr_buf (GstMatroskaDemux * demux,
2267
GstMatroskaTrackContext * stream, guint8 * data, guint len)
2270
GstBuffer *header_buf = NULL;
2272
flow = gst_pad_alloc_buffer_and_set_caps (stream->pad,
2273
GST_BUFFER_OFFSET_NONE, len, stream->caps, &header_buf);
2275
if (flow == GST_FLOW_OK) {
2276
memcpy (GST_BUFFER_DATA (header_buf), data, len);
2277
flow = gst_pad_push (stream->pad, header_buf);
2280
if (flow != GST_FLOW_OK && flow != GST_FLOW_NOT_LINKED)
2287
gst_matroska_demux_push_flac_codec_priv_data (GstMatroskaDemux * demux,
2288
GstMatroskaTrackContext * stream)
2293
GST_LOG_OBJECT (demux, "priv data size = %u", stream->codec_priv_size);
2295
pdata = (guint8 *) stream->codec_priv;
2297
/* need at least 'fLaC' marker + STREAMINFO metadata block */
2298
if (stream->codec_priv_size < ((4) + (4 + 34))) {
2299
GST_WARNING_OBJECT (demux, "not enough codec priv data for flac headers");
2303
if (memcmp (pdata, "fLaC", 4) != 0) {
2304
GST_WARNING_OBJECT (demux, "no flac marker at start of stream headers");
2308
if (!gst_matroska_demux_push_hdr_buf (demux, stream, pdata, 4))
2311
off = 4; /* skip fLaC marker */
2312
while (off < stream->codec_priv_size) {
2313
len = GST_READ_UINT8 (pdata + off + 1) << 16;
2314
len |= GST_READ_UINT8 (pdata + off + 2) << 8;
2315
len |= GST_READ_UINT8 (pdata + off + 3);
2317
GST_DEBUG_OBJECT (demux, "header packet: len=%u bytes, flags=0x%02x",
2318
len, (guint) pdata[off]);
2320
if (!gst_matroska_demux_push_hdr_buf (demux, stream, pdata + off, len))
2044
2330
gst_matroska_demux_push_xiph_codec_priv_data (GstMatroskaDemux * demux,
2045
2331
GstMatroskaTrackContext * stream)
2049
guint32 offset, length;
2333
guint8 *p = (guint8 *) stream->codec_priv;
2334
gint i, offset, length, num_packets;
2053
2336
/* start of the stream and vorbis audio or theora video, need to
2054
2337
* send the codec_priv data as first three packets */
2055
p = (guchar *) stream->codec_priv;
2058
for (i = 0; i < 2; i++) {
2338
num_packets = p[0] + 1;
2339
GST_DEBUG_OBJECT (demux, "%u stream headers, total length=%u bytes",
2340
(guint) num_packets, stream->codec_priv_size);
2342
offset = num_packets; /* offset to data of first packet */
2344
for (i = 0; i < num_packets - 1; i++) {
2059
2345
length = p[i + 1];
2060
if (gst_pad_alloc_buffer_and_set_caps (stream->pad, GST_BUFFER_OFFSET_NONE,
2061
length, stream->caps, &priv) == GST_FLOW_OK) {
2062
memcpy (GST_BUFFER_DATA (priv), &p[offset], length);
2064
ret = gst_pad_push (stream->pad, priv);
2065
if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
2347
GST_DEBUG_OBJECT (demux, "buffer %d: length=%u bytes", i, (guint) length);
2348
if (offset + length > stream->codec_priv_size)
2351
if (!gst_matroska_demux_push_hdr_buf (demux, stream, p + offset, length))
2068
2354
offset += length;
2070
2357
length = stream->codec_priv_size - offset;
2071
if (gst_pad_alloc_buffer_and_set_caps (stream->pad, GST_BUFFER_OFFSET_NONE,
2072
length, stream->caps, &priv) == GST_FLOW_OK) {
2073
memcpy (GST_BUFFER_DATA (priv), &p[offset], length);
2074
ret = gst_pad_push (stream->pad, priv);
2075
if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
2358
GST_DEBUG_OBJECT (demux, "buffer %d: length=%u bytes", i, (guint) length);
2359
if (!gst_matroska_demux_push_hdr_buf (demux, stream, p + offset, length))
2495
gst_matroska_decode_buffer (GstMatroskaTrackContext * context, GstBuffer * buf)
2499
g_assert (context->encodings != NULL);
2501
for (i = 0; i < context->encodings->len; i++) {
2502
GstMatroskaTrackEncoding *enc;
2503
guint8 *new_data = NULL;
2507
enc = &g_array_index (context->encodings, GstMatroskaTrackEncoding, i);
2509
/* Currently only compression is supported */
2513
/* FIXME: use enc->scope ? */
2515
if (enc->comp_algo == 0) {
2517
/* zlib encoded track */
2522
orig_size = GST_BUFFER_SIZE (buf);
2523
zstream.zalloc = (alloc_func) 0;
2524
zstream.zfree = (free_func) 0;
2525
zstream.opaque = (voidpf) 0;
2526
if (inflateInit (&zstream) != Z_OK) {
2527
GST_WARNING ("zlib initialization failed.");
2530
zstream.next_in = (Bytef *) GST_BUFFER_DATA (buf);
2531
zstream.avail_in = orig_size;
2532
new_size = orig_size;
2533
new_data = g_malloc (new_size);
2534
zstream.avail_out = new_size;
2537
new_data = g_realloc (new_data, new_size);
2538
zstream.next_out = (Bytef *) (new_data + zstream.total_out);
2539
result = inflate (&zstream, Z_NO_FLUSH);
2540
if (result != Z_OK && result != Z_STREAM_END) {
2541
GST_WARNING ("zlib decompression failed.");
2543
inflateEnd (&zstream);
2546
zstream.avail_out += 4000;
2547
} while (zstream.avail_out == 4000 &&
2548
zstream.avail_in != 0 && result != Z_STREAM_END);
2550
new_size = zstream.total_out;
2551
inflateEnd (&zstream);
2553
GST_WARNING ("GZIP encoded tracks not supported.");
2556
} else if (enc->comp_algo == 1) {
2557
GST_WARNING ("BZIP encoded tracks not supported.");
2559
} else if (enc->comp_algo == 2) {
2560
GST_WARNING ("LZO encoded tracks not supported.");
2562
} else if (enc->comp_algo == 3) {
2563
GST_WARNING ("Header-stripped tracks not supported.");
2566
g_assert_not_reached ();
2569
g_assert (new_data != NULL);
2571
new_buf = gst_buffer_new ();
2572
GST_BUFFER_MALLOCDATA (new_buf) = (guint8 *) new_data;
2573
GST_BUFFER_DATA (new_buf) = (guint8 *) new_data;
2574
GST_BUFFER_SIZE (new_buf) = new_size;
2575
gst_buffer_stamp (new_buf, buf);
2577
gst_buffer_unref (buf);
2210
2584
static gboolean
2211
2585
gst_matroska_demux_parse_blockgroup_or_simpleblock (GstMatroskaDemux * demux,
2212
2586
guint64 cluster_time, gboolean is_simpleblock)
3232
3617
videocontext->display_height * videocontext->pixel_width, NULL);
3235
if (context->default_duration > 0) {
3236
GValue fps_double = { 0 };
3237
GValue fps_fraction = { 0 };
3239
g_value_init (&fps_double, G_TYPE_DOUBLE);
3240
g_value_init (&fps_fraction, GST_TYPE_FRACTION);
3241
g_value_set_double (&fps_double,
3242
gst_guint64_to_gdouble (GST_SECOND / context->default_duration));
3620
if (videocontext->default_fps > 0.0) {
3621
GValue fps_double = { 0, };
3622
GValue fps_fraction = { 0, };
3624
g_value_init (&fps_double, G_TYPE_DOUBLE);
3625
g_value_init (&fps_fraction, GST_TYPE_FRACTION);
3626
g_value_set_double (&fps_double, videocontext->default_fps);
3627
g_value_transform (&fps_double, &fps_fraction);
3629
gst_structure_set_value (structure, "framerate", &fps_fraction);
3630
g_value_unset (&fps_double);
3631
g_value_unset (&fps_fraction);
3632
} else if (context->default_duration > 0) {
3633
GValue fps_double = { 0, };
3634
GValue fps_fraction = { 0, };
3636
g_value_init (&fps_double, G_TYPE_DOUBLE);
3637
g_value_init (&fps_fraction, GST_TYPE_FRACTION);
3638
g_value_set_double (&fps_double, (gdouble) GST_SECOND * 1.0 /
3639
gst_guint64_to_gdouble (context->default_duration));
3243
3640
g_value_transform (&fps_double, &fps_fraction);
3245
3642
gst_structure_set_value (structure, "framerate", &fps_fraction);
3396
3799
caps = gst_riff_create_audio_caps (auds->format, NULL, auds, NULL,
3397
3800
NULL, codec_name);
3399
} else if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG2,
3400
strlen (GST_MATROSKA_CODEC_ID_AUDIO_MPEG2)) ||
3401
!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG4,
3402
strlen (GST_MATROSKA_CODEC_ID_AUDIO_MPEG4))) {
3802
} else if (g_str_has_prefix (codec_id, "A_AAC")) {
3403
3803
GstBuffer *priv = NULL;
3404
3804
gint mpegversion = -1;
3405
3805
gint rate_idx, profile;
3408
/* make up decoderspecificdata */
3409
priv = gst_buffer_new_and_alloc (5);
3410
data = GST_BUFFER_DATA (priv);
3411
rate_idx = aac_rate_idx (audiocontext->samplerate);
3412
profile = aac_profile_idx (codec_id);
3414
data[0] = ((profile + 1) << 3) | ((rate_idx & 0xE) >> 1);
3415
data[1] = ((rate_idx & 0x1) << 7) | (audiocontext->channels << 3);
3416
GST_BUFFER_SIZE (priv) = 2;
3806
guint8 *data = NULL;
3808
/* unspecified AAC profile with opaque private codec data */
3809
if (strcmp (codec_id, "A_AAC") == 0) {
3810
if (context->codec_priv_size >= 2) {
3811
guint obj_type, freq_index, explicit_freq_bytes = 0;
3813
codec_id = GST_MATROSKA_CODEC_ID_AUDIO_MPEG4;
3814
freq_index = (GST_READ_UINT16_BE (context->codec_priv) & 0x780) >> 7;
3815
obj_type = (GST_READ_UINT16_BE (context->codec_priv) & 0xF800) >> 11;
3816
if (freq_index == 15)
3817
explicit_freq_bytes = 3;
3818
GST_DEBUG ("obj_type = %u, freq_index = %u", obj_type, freq_index);
3819
priv = gst_buffer_new_and_alloc (context->codec_priv_size);
3820
memcpy (GST_BUFFER_DATA (priv), context->codec_priv,
3821
context->codec_priv_size);
3822
/* assume SBR if samplerate <= 24kHz */
3823
if (obj_type == 5 || (freq_index >= 6 && freq_index != 15) ||
3824
(context->codec_priv_size == (5 + explicit_freq_bytes))) {
3825
audiocontext->samplerate *= 2;
3828
GST_WARNING ("Opaque A_AAC codec ID, but no codec private data");
3829
/* just try this and see what happens ... */
3830
codec_id = GST_MATROSKA_CODEC_ID_AUDIO_MPEG4;
3834
/* make up decoder-specific data if it is not supplied */
3836
priv = gst_buffer_new_and_alloc (5);
3837
data = GST_BUFFER_DATA (priv);
3838
rate_idx = aac_rate_idx (audiocontext->samplerate);
3839
profile = aac_profile_idx (codec_id);
3841
data[0] = ((profile + 1) << 3) | ((rate_idx & 0xE) >> 1);
3842
data[1] = ((rate_idx & 0x1) << 7) | (audiocontext->channels << 3);
3843
GST_BUFFER_SIZE (priv) = 2;
3418
3846
if (!strncmp (codec_id, GST_MATROSKA_CODEC_ID_AUDIO_MPEG2,
3419
3847
strlen (GST_MATROSKA_CODEC_ID_AUDIO_MPEG2))) {