/
matroska-read-common.c
1940 lines (1593 loc) · 53.3 KB
/
matroska-read-common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GStreamer Matroska muxer/demuxer
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
* (c) 2006 Tim-Philipp Müller <tim centricular net>
* (c) 2008 Sebastian Dröge <slomo@circular-chaos.org>
* (c) 2011 Debarshi Ray <rishi@gnu.org>
*
* matroska-read-common.c: shared by matroska file/stream demuxer and parser
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
#ifdef HAVE_BZ2
#include <bzlib.h>
#endif
#include <gst/tag/tag.h>
#include <gst/base/gsttypefindhelper.h>
#include "lzo.h"
#include "ebml-read.h"
#include "matroska-read-common.h"
GST_DEBUG_CATEGORY (matroskareadcommon_debug);
#define GST_CAT_DEFAULT matroskareadcommon_debug
#define DEBUG_ELEMENT_START(common, ebml, element) \
GST_DEBUG_OBJECT (common, "Parsing " element " element at offset %" \
G_GUINT64_FORMAT, gst_ebml_read_get_pos (ebml))
#define DEBUG_ELEMENT_STOP(common, ebml, element, ret) \
GST_DEBUG_OBJECT (common, "Parsing " element " element " \
" finished with '%s'", gst_flow_get_name (ret))
static gboolean
gst_matroska_decompress_data (GstMatroskaTrackEncoding * enc,
guint8 ** data_out, guint * size_out,
GstMatroskaTrackCompressionAlgorithm algo)
{
guint8 *new_data = NULL;
guint new_size = 0;
guint8 *data = *data_out;
guint size = *size_out;
gboolean ret = TRUE;
if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_ZLIB) {
#ifdef HAVE_ZLIB
/* zlib encoded data */
z_stream zstream;
guint orig_size;
int result;
orig_size = size;
zstream.zalloc = (alloc_func) 0;
zstream.zfree = (free_func) 0;
zstream.opaque = (voidpf) 0;
if (inflateInit (&zstream) != Z_OK) {
GST_WARNING ("zlib initialization failed.");
ret = FALSE;
goto out;
}
zstream.next_in = (Bytef *) data;
zstream.avail_in = orig_size;
new_size = orig_size;
new_data = g_malloc (new_size);
zstream.avail_out = new_size;
zstream.next_out = (Bytef *) new_data;
do {
result = inflate (&zstream, Z_NO_FLUSH);
if (result != Z_OK && result != Z_STREAM_END) {
GST_WARNING ("zlib decompression failed.");
g_free (new_data);
inflateEnd (&zstream);
break;
}
new_size += 4000;
new_data = g_realloc (new_data, new_size);
zstream.next_out = (Bytef *) (new_data + zstream.total_out);
zstream.avail_out += 4000;
} while (zstream.avail_in != 0 && result != Z_STREAM_END);
if (result != Z_STREAM_END) {
ret = FALSE;
goto out;
} else {
new_size = zstream.total_out;
inflateEnd (&zstream);
}
#else
GST_WARNING ("zlib encoded tracks not supported.");
ret = FALSE;
goto out;
#endif
} else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_BZLIB) {
#ifdef HAVE_BZ2
/* bzip2 encoded data */
bz_stream bzstream;
guint orig_size;
int result;
bzstream.bzalloc = NULL;
bzstream.bzfree = NULL;
bzstream.opaque = NULL;
orig_size = size;
if (BZ2_bzDecompressInit (&bzstream, 0, 0) != BZ_OK) {
GST_WARNING ("bzip2 initialization failed.");
ret = FALSE;
goto out;
}
bzstream.next_in = (char *) data;
bzstream.avail_in = orig_size;
new_size = orig_size;
new_data = g_malloc (new_size);
bzstream.avail_out = new_size;
bzstream.next_out = (char *) new_data;
do {
result = BZ2_bzDecompress (&bzstream);
if (result != BZ_OK && result != BZ_STREAM_END) {
GST_WARNING ("bzip2 decompression failed.");
g_free (new_data);
BZ2_bzDecompressEnd (&bzstream);
break;
}
new_size += 4000;
new_data = g_realloc (new_data, new_size);
bzstream.next_out = (char *) (new_data + bzstream.total_out_lo32);
bzstream.avail_out += 4000;
} while (bzstream.avail_in != 0 && result != BZ_STREAM_END);
if (result != BZ_STREAM_END) {
ret = FALSE;
goto out;
} else {
new_size = bzstream.total_out_lo32;
BZ2_bzDecompressEnd (&bzstream);
}
#else
GST_WARNING ("bzip2 encoded tracks not supported.");
ret = FALSE;
goto out;
#endif
} else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_LZO1X) {
/* lzo encoded data */
int result;
int orig_size, out_size;
orig_size = size;
out_size = size;
new_size = size;
new_data = g_malloc (new_size);
do {
orig_size = size;
out_size = new_size;
result = lzo1x_decode (new_data, &out_size, data, &orig_size);
if (orig_size > 0) {
new_size += 4000;
new_data = g_realloc (new_data, new_size);
}
} while (orig_size > 0 && result == LZO_OUTPUT_FULL);
new_size -= out_size;
if (result != LZO_OUTPUT_FULL) {
GST_WARNING ("lzo decompression failed");
g_free (new_data);
ret = FALSE;
goto out;
}
} else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_HEADERSTRIP) {
/* header stripped encoded data */
if (enc->comp_settings_length > 0) {
new_data = g_malloc (size + enc->comp_settings_length);
new_size = size + enc->comp_settings_length;
memcpy (new_data, enc->comp_settings, enc->comp_settings_length);
memcpy (new_data + enc->comp_settings_length, data, size);
}
} else {
GST_ERROR ("invalid compression algorithm %d", algo);
ret = FALSE;
}
out:
if (!ret) {
*data_out = NULL;
*size_out = 0;
} else {
*data_out = new_data;
*size_out = new_size;
}
return ret;
}
GstFlowReturn
gst_matroska_decode_content_encodings (GArray * encodings)
{
gint i;
if (encodings == NULL)
return GST_FLOW_OK;
for (i = 0; i < encodings->len; i++) {
GstMatroskaTrackEncoding *enc =
&g_array_index (encodings, GstMatroskaTrackEncoding, i);
guint8 *data = NULL;
guint size;
if ((enc->scope & GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING)
== 0)
continue;
/* Encryption not supported yet */
if (enc->type != 0)
return GST_FLOW_ERROR;
if (i + 1 >= encodings->len)
return GST_FLOW_ERROR;
if (enc->comp_settings_length == 0)
continue;
data = enc->comp_settings;
size = enc->comp_settings_length;
if (!gst_matroska_decompress_data (enc, &data, &size, enc->comp_algo))
return GST_FLOW_ERROR;
g_free (enc->comp_settings);
enc->comp_settings = data;
enc->comp_settings_length = size;
}
return GST_FLOW_OK;
}
gboolean
gst_matroska_decode_data (GArray * encodings, guint8 ** data_out,
guint * size_out, GstMatroskaTrackEncodingScope scope, gboolean free)
{
guint8 *data;
guint size;
gboolean ret = TRUE;
gint i;
g_return_val_if_fail (encodings != NULL, FALSE);
g_return_val_if_fail (data_out != NULL && *data_out != NULL, FALSE);
g_return_val_if_fail (size_out != NULL, FALSE);
data = *data_out;
size = *size_out;
for (i = 0; i < encodings->len; i++) {
GstMatroskaTrackEncoding *enc =
&g_array_index (encodings, GstMatroskaTrackEncoding, i);
guint8 *new_data = NULL;
guint new_size = 0;
if ((enc->scope & scope) == 0)
continue;
/* Encryption not supported yet */
if (enc->type != 0) {
ret = FALSE;
break;
}
new_data = data;
new_size = size;
ret =
gst_matroska_decompress_data (enc, &new_data, &new_size,
enc->comp_algo);
if (!ret)
break;
if ((data == *data_out && free) || (data != *data_out))
g_free (data);
data = new_data;
size = new_size;
}
if (!ret) {
if ((data == *data_out && free) || (data != *data_out))
g_free (data);
*data_out = NULL;
*size_out = 0;
} else {
*data_out = data;
*size_out = size;
}
return ret;
}
static gint
gst_matroska_index_compare (GstMatroskaIndex * i1, GstMatroskaIndex * i2)
{
if (i1->time < i2->time)
return -1;
else if (i1->time > i2->time)
return 1;
else if (i1->block < i2->block)
return -1;
else if (i1->block > i2->block)
return 1;
else
return 0;
}
gint
gst_matroska_index_seek_find (GstMatroskaIndex * i1, GstClockTime * time,
gpointer user_data)
{
if (i1->time < *time)
return -1;
else if (i1->time > *time)
return 1;
else
return 0;
}
GstMatroskaIndex *
gst_matroska_read_common_do_index_seek (GstMatroskaReadCommon * common,
GstMatroskaTrackContext * track, gint64 seek_pos, GArray ** _index,
gint * _entry_index)
{
GstMatroskaIndex *entry = NULL;
GArray *index;
if (!common->index || !common->index->len)
return NULL;
/* find entry just before or at the requested position */
if (track && track->index_table)
index = track->index_table;
else
index = common->index;
entry =
gst_util_array_binary_search (index->data, index->len,
sizeof (GstMatroskaIndex),
(GCompareDataFunc) gst_matroska_index_seek_find, GST_SEARCH_MODE_BEFORE,
&seek_pos, NULL);
if (entry == NULL)
entry = &g_array_index (index, GstMatroskaIndex, 0);
if (_index)
*_index = index;
if (_entry_index)
*_entry_index = entry - (GstMatroskaIndex *) index->data;
return entry;
}
static gint
gst_matroska_read_common_encoding_cmp (GstMatroskaTrackEncoding * a,
GstMatroskaTrackEncoding * b)
{
if (b->order > a->order)
return 1;
else if (b->order < a->order)
return -1;
else
return 0;
}
static gboolean
gst_matroska_read_common_encoding_order_unique (GArray * encodings, guint64
order)
{
gint i;
if (encodings == NULL || encodings->len == 0)
return TRUE;
for (i = 0; i < encodings->len; i++)
if (g_array_index (encodings, GstMatroskaTrackEncoding, i).order == order)
return FALSE;
return TRUE;
}
/* takes ownership of taglist */
void
gst_matroska_read_common_found_global_tag (GstMatroskaReadCommon * common,
GstElement * el, GstTagList * taglist)
{
if (common->global_tags) {
/* nothing sent yet, add to cache */
gst_tag_list_insert (common->global_tags, taglist, GST_TAG_MERGE_APPEND);
gst_tag_list_free (taglist);
} else {
/* hm, already sent, no need to cache and wait anymore */
GST_DEBUG_OBJECT (common, "Sending late global tags %" GST_PTR_FORMAT,
taglist);
gst_element_found_tags (el, taglist);
}
}
gint64
gst_matroska_read_common_get_length (GstMatroskaReadCommon * common)
{
GstFormat fmt = GST_FORMAT_BYTES;
gint64 end = -1;
if (!gst_pad_query_peer_duration (common->sinkpad, &fmt, &end) ||
fmt != GST_FORMAT_BYTES || end < 0)
GST_DEBUG_OBJECT (common, "no upstream length");
return end;
}
/* determine track to seek in */
GstMatroskaTrackContext *
gst_matroska_read_common_get_seek_track (GstMatroskaReadCommon * common,
GstMatroskaTrackContext * track)
{
gint i;
if (track && track->type == GST_MATROSKA_TRACK_TYPE_VIDEO)
return track;
for (i = 0; i < common->src->len; i++) {
GstMatroskaTrackContext *stream;
stream = g_ptr_array_index (common->src, i);
if (stream->type == GST_MATROSKA_TRACK_TYPE_VIDEO && stream->index_table)
track = stream;
}
return track;
}
/* skip unknown or alike element */
GstFlowReturn
gst_matroska_read_common_parse_skip (GstMatroskaReadCommon * common,
GstEbmlRead * ebml, const gchar * parent_name, guint id)
{
if (id == GST_EBML_ID_VOID) {
GST_DEBUG_OBJECT (common, "Skipping EBML Void element");
} else if (id == GST_EBML_ID_CRC32) {
GST_DEBUG_OBJECT (common, "Skipping EBML CRC32 element");
} else {
GST_WARNING_OBJECT (common,
"Unknown %s subelement 0x%x - ignoring", parent_name, id);
}
return gst_ebml_read_skip (ebml);
}
static GstFlowReturn
gst_matroska_read_common_parse_attached_file (GstMatroskaReadCommon * common,
GstEbmlRead * ebml, GstTagList * taglist)
{
guint32 id;
GstFlowReturn ret;
gchar *description = NULL;
gchar *filename = NULL;
gchar *mimetype = NULL;
guint8 *data = NULL;
guint64 datalen = 0;
DEBUG_ELEMENT_START (common, ebml, "AttachedFile");
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
DEBUG_ELEMENT_STOP (common, ebml, "AttachedFile", ret);
return ret;
}
while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
/* read all sub-entries */
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
case GST_MATROSKA_ID_FILEDESCRIPTION:
if (description) {
GST_WARNING_OBJECT (common, "FileDescription can only appear once");
break;
}
ret = gst_ebml_read_utf8 (ebml, &id, &description);
GST_DEBUG_OBJECT (common, "FileDescription: %s",
GST_STR_NULL (description));
break;
case GST_MATROSKA_ID_FILENAME:
if (filename) {
GST_WARNING_OBJECT (common, "FileName can only appear once");
break;
}
ret = gst_ebml_read_utf8 (ebml, &id, &filename);
GST_DEBUG_OBJECT (common, "FileName: %s", GST_STR_NULL (filename));
break;
case GST_MATROSKA_ID_FILEMIMETYPE:
if (mimetype) {
GST_WARNING_OBJECT (common, "FileMimeType can only appear once");
break;
}
ret = gst_ebml_read_ascii (ebml, &id, &mimetype);
GST_DEBUG_OBJECT (common, "FileMimeType: %s", GST_STR_NULL (mimetype));
break;
case GST_MATROSKA_ID_FILEDATA:
if (data) {
GST_WARNING_OBJECT (common, "FileData can only appear once");
break;
}
ret = gst_ebml_read_binary (ebml, &id, &data, &datalen);
GST_DEBUG_OBJECT (common, "FileData of size %" G_GUINT64_FORMAT,
datalen);
break;
default:
ret = gst_matroska_read_common_parse_skip (common, ebml,
"AttachedFile", id);
break;
case GST_MATROSKA_ID_FILEUID:
ret = gst_ebml_read_skip (ebml);
break;
}
}
DEBUG_ELEMENT_STOP (common, ebml, "AttachedFile", ret);
if (filename && mimetype && data && datalen > 0) {
GstTagImageType image_type = GST_TAG_IMAGE_TYPE_NONE;
GstBuffer *tagbuffer = NULL;
GstCaps *caps;
gchar *filename_lc = g_utf8_strdown (filename, -1);
GST_DEBUG_OBJECT (common, "Creating tag for attachment with "
"filename '%s', mimetype '%s', description '%s', "
"size %" G_GUINT64_FORMAT, filename, mimetype,
GST_STR_NULL (description), datalen);
/* TODO: better heuristics for different image types */
if (strstr (filename_lc, "cover")) {
if (strstr (filename_lc, "back"))
image_type = GST_TAG_IMAGE_TYPE_BACK_COVER;
else
image_type = GST_TAG_IMAGE_TYPE_FRONT_COVER;
} else if (g_str_has_prefix (mimetype, "image/") ||
g_str_has_suffix (filename_lc, "png") ||
g_str_has_suffix (filename_lc, "jpg") ||
g_str_has_suffix (filename_lc, "jpeg") ||
g_str_has_suffix (filename_lc, "gif") ||
g_str_has_suffix (filename_lc, "bmp")) {
image_type = GST_TAG_IMAGE_TYPE_UNDEFINED;
}
g_free (filename_lc);
/* First try to create an image tag buffer from this */
if (image_type != GST_TAG_IMAGE_TYPE_NONE) {
tagbuffer =
gst_tag_image_data_to_image_buffer (data, datalen, image_type);
if (!tagbuffer)
image_type = GST_TAG_IMAGE_TYPE_NONE;
}
/* if this failed create an attachment buffer */
if (!tagbuffer) {
tagbuffer = gst_buffer_new_and_alloc (datalen);
memcpy (GST_BUFFER_DATA (tagbuffer), data, datalen);
GST_BUFFER_SIZE (tagbuffer) = datalen;
caps = gst_type_find_helper_for_buffer (NULL, tagbuffer, NULL);
if (caps == NULL)
caps = gst_caps_new_simple (mimetype, NULL);
gst_buffer_set_caps (tagbuffer, caps);
gst_caps_unref (caps);
}
/* Set filename and description on the caps */
caps = GST_BUFFER_CAPS (tagbuffer);
gst_caps_set_simple (caps, "filename", G_TYPE_STRING, filename, NULL);
if (description)
gst_caps_set_simple (caps, "description", G_TYPE_STRING, description,
NULL);
GST_DEBUG_OBJECT (common,
"Created attachment buffer with caps: %" GST_PTR_FORMAT, caps);
/* and append to the tag list */
if (image_type != GST_TAG_IMAGE_TYPE_NONE)
gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, tagbuffer,
NULL);
else
gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_ATTACHMENT,
tagbuffer, NULL);
/* the tag list adds it own ref */
gst_buffer_unref (tagbuffer);
}
g_free (filename);
g_free (mimetype);
g_free (data);
g_free (description);
return ret;
}
GstFlowReturn
gst_matroska_read_common_parse_attachments (GstMatroskaReadCommon * common,
GstElement * el, GstEbmlRead * ebml)
{
guint32 id;
GstFlowReturn ret = GST_FLOW_OK;
GstTagList *taglist;
DEBUG_ELEMENT_START (common, ebml, "Attachments");
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
DEBUG_ELEMENT_STOP (common, ebml, "Attachments", ret);
return ret;
}
taglist = gst_tag_list_new ();
while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
case GST_MATROSKA_ID_ATTACHEDFILE:
ret = gst_matroska_read_common_parse_attached_file (common, ebml,
taglist);
break;
default:
ret = gst_matroska_read_common_parse_skip (common, ebml,
"Attachments", id);
break;
}
}
DEBUG_ELEMENT_STOP (common, ebml, "Attachments", ret);
if (gst_structure_n_fields (GST_STRUCTURE (taglist)) > 0) {
GST_DEBUG_OBJECT (common, "Storing attachment tags");
gst_matroska_read_common_found_global_tag (common, el, taglist);
} else {
GST_DEBUG_OBJECT (common, "No valid attachments found");
gst_tag_list_free (taglist);
}
common->attachments_parsed = TRUE;
return ret;
}
GstFlowReturn
gst_matroska_read_common_parse_chapters (GstMatroskaReadCommon * common,
GstEbmlRead * ebml)
{
guint32 id;
GstFlowReturn ret = GST_FLOW_OK;
GST_WARNING_OBJECT (common, "Parsing of chapters not implemented yet");
/* TODO: implement parsing of chapters */
DEBUG_ELEMENT_START (common, ebml, "Chapters");
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
DEBUG_ELEMENT_STOP (common, ebml, "Chapters", ret);
return ret;
}
while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
default:
ret = gst_ebml_read_skip (ebml);
break;
}
}
DEBUG_ELEMENT_STOP (common, ebml, "Chapters", ret);
return ret;
}
GstFlowReturn
gst_matroska_read_common_parse_header (GstMatroskaReadCommon * common,
GstEbmlRead * ebml)
{
GstFlowReturn ret;
gchar *doctype;
guint version;
guint32 id;
/* this function is the first to be called */
/* default init */
doctype = NULL;
version = 1;
ret = gst_ebml_peek_id (ebml, &id);
if (ret != GST_FLOW_OK)
return ret;
GST_DEBUG_OBJECT (common, "id: %08x", id);
if (id != GST_EBML_ID_HEADER) {
GST_ERROR_OBJECT (common, "Failed to read header");
goto exit;
}
ret = gst_ebml_read_master (ebml, &id);
if (ret != GST_FLOW_OK)
return ret;
while (gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
ret = gst_ebml_peek_id (ebml, &id);
if (ret != GST_FLOW_OK)
return ret;
switch (id) {
/* is our read version uptodate? */
case GST_EBML_ID_EBMLREADVERSION:{
guint64 num;
ret = gst_ebml_read_uint (ebml, &id, &num);
if (ret != GST_FLOW_OK)
return ret;
if (num != GST_EBML_VERSION) {
GST_ERROR_OBJECT (ebml, "Unsupported EBML version %" G_GUINT64_FORMAT,
num);
return GST_FLOW_ERROR;
}
GST_DEBUG_OBJECT (ebml, "EbmlReadVersion: %" G_GUINT64_FORMAT, num);
break;
}
/* we only handle 8 byte lengths at max */
case GST_EBML_ID_EBMLMAXSIZELENGTH:{
guint64 num;
ret = gst_ebml_read_uint (ebml, &id, &num);
if (ret != GST_FLOW_OK)
return ret;
if (num > sizeof (guint64)) {
GST_ERROR_OBJECT (ebml,
"Unsupported EBML maximum size %" G_GUINT64_FORMAT, num);
return GST_FLOW_ERROR;
}
GST_DEBUG_OBJECT (ebml, "EbmlMaxSizeLength: %" G_GUINT64_FORMAT, num);
break;
}
/* we handle 4 byte IDs at max */
case GST_EBML_ID_EBMLMAXIDLENGTH:{
guint64 num;
ret = gst_ebml_read_uint (ebml, &id, &num);
if (ret != GST_FLOW_OK)
return ret;
if (num > sizeof (guint32)) {
GST_ERROR_OBJECT (ebml,
"Unsupported EBML maximum ID %" G_GUINT64_FORMAT, num);
return GST_FLOW_ERROR;
}
GST_DEBUG_OBJECT (ebml, "EbmlMaxIdLength: %" G_GUINT64_FORMAT, num);
break;
}
case GST_EBML_ID_DOCTYPE:{
gchar *text;
ret = gst_ebml_read_ascii (ebml, &id, &text);
if (ret != GST_FLOW_OK)
return ret;
GST_DEBUG_OBJECT (ebml, "EbmlDocType: %s", GST_STR_NULL (text));
if (doctype)
g_free (doctype);
doctype = text;
break;
}
case GST_EBML_ID_DOCTYPEREADVERSION:{
guint64 num;
ret = gst_ebml_read_uint (ebml, &id, &num);
if (ret != GST_FLOW_OK)
return ret;
version = num;
GST_DEBUG_OBJECT (ebml, "EbmlReadVersion: %" G_GUINT64_FORMAT, num);
break;
}
default:
ret = gst_matroska_read_common_parse_skip (common, ebml,
"EBML header", id);
if (ret != GST_FLOW_OK)
return ret;
break;
/* we ignore these two, as they don't tell us anything we care about */
case GST_EBML_ID_EBMLVERSION:
case GST_EBML_ID_DOCTYPEVERSION:
ret = gst_ebml_read_skip (ebml);
if (ret != GST_FLOW_OK)
return ret;
break;
}
}
exit:
if ((doctype != NULL && !strcmp (doctype, GST_MATROSKA_DOCTYPE_MATROSKA)) ||
(doctype != NULL && !strcmp (doctype, GST_MATROSKA_DOCTYPE_WEBM)) ||
(doctype == NULL)) {
if (version <= 2) {
if (doctype) {
GST_INFO_OBJECT (common, "Input is %s version %d", doctype, version);
} else {
GST_WARNING_OBJECT (common, "Input is EBML without doctype, assuming "
"matroska (version %d)", version);
}
ret = GST_FLOW_OK;
} else {
GST_ELEMENT_ERROR (common, STREAM, DEMUX, (NULL),
("Demuxer version (2) is too old to read %s version %d",
GST_STR_NULL (doctype), version));
ret = GST_FLOW_ERROR;
}
g_free (doctype);
} else {
GST_ELEMENT_ERROR (common, STREAM, WRONG_TYPE, (NULL),
("Input is not a matroska stream (doctype=%s)", doctype));
ret = GST_FLOW_ERROR;
g_free (doctype);
}
return ret;
}
static GstFlowReturn
gst_matroska_read_common_parse_index_cuetrack (GstMatroskaReadCommon * common,
GstEbmlRead * ebml, guint * nentries)
{
guint32 id;
GstFlowReturn ret;
GstMatroskaIndex idx;
idx.pos = (guint64) - 1;
idx.track = 0;
idx.time = GST_CLOCK_TIME_NONE;
idx.block = 1;
DEBUG_ELEMENT_START (common, ebml, "CueTrackPositions");
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
DEBUG_ELEMENT_STOP (common, ebml, "CueTrackPositions", ret);
return ret;
}
while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
/* track number */
case GST_MATROSKA_ID_CUETRACK:
{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
idx.track = 0;
GST_WARNING_OBJECT (common, "Invalid CueTrack 0");
break;
}
GST_DEBUG_OBJECT (common, "CueTrack: %" G_GUINT64_FORMAT, num);
idx.track = num;
break;
}
/* position in file */
case GST_MATROSKA_ID_CUECLUSTERPOSITION:
{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num > G_MAXINT64) {
GST_WARNING_OBJECT (common, "CueClusterPosition %" G_GUINT64_FORMAT
" too large", num);
break;
}
idx.pos = num;
break;
}
/* number of block in the cluster */
case GST_MATROSKA_ID_CUEBLOCKNUMBER:
{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (common, "Invalid CueBlockNumber 0");
break;
}
GST_DEBUG_OBJECT (common, "CueBlockNumber: %" G_GUINT64_FORMAT, num);
idx.block = num;
/* mild sanity check, disregard strange cases ... */
if (idx.block > G_MAXUINT16) {
GST_DEBUG_OBJECT (common, "... looks suspicious, ignoring");
idx.block = 1;
}
break;
}
default:
ret = gst_matroska_read_common_parse_skip (common, ebml,
"CueTrackPositions", id);
break;
case GST_MATROSKA_ID_CUECODECSTATE:
case GST_MATROSKA_ID_CUEREFERENCE:
ret = gst_ebml_read_skip (ebml);
break;
}
}
DEBUG_ELEMENT_STOP (common, ebml, "CueTrackPositions", ret);
/* (e.g.) lavf typically creates entries without a block number,
* which is bogus and leads to contradictory information */
if (common->index->len) {
GstMatroskaIndex *last_idx;
last_idx = &g_array_index (common->index, GstMatroskaIndex,
common->index->len - 1);
if (last_idx->block == idx.block && last_idx->pos == idx.pos &&
last_idx->track == idx.track && idx.time > last_idx->time) {
GST_DEBUG_OBJECT (common, "Cue entry refers to same location, "
"but has different time than previous entry; discarding");
idx.track = 0;
}