1 /* GStreamer Matroska muxer/demuxer
2 * (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * (C) 2006 Tim-Philipp Müller <tim centricular net>
5 * matroska-ids.c: matroska track context utility functions
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
27 #include "matroska-ids.h"
32 gst_matroska_track_init_video_context (GstMatroskaTrackContext ** p_context)
34 GstMatroskaTrackVideoContext *video_context;
36 g_assert (p_context != NULL && *p_context != NULL);
38 /* already set up? (track info might come before track type) */
39 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_VIDEO) {
40 GST_LOG ("video context already set up");
44 /* it better not have been set up as some other track type ... */
45 if ((*p_context)->type != 0) {
46 g_return_val_if_reached (FALSE);
49 video_context = g_renew (GstMatroskaTrackVideoContext, *p_context, 1);
50 *p_context = (GstMatroskaTrackContext *) video_context;
53 (*p_context)->type = GST_MATROSKA_TRACK_TYPE_VIDEO;
54 video_context->display_width = 0;
55 video_context->display_height = 0;
56 video_context->pixel_width = 0;
57 video_context->pixel_height = 0;
58 video_context->asr_mode = 0;
59 video_context->fourcc = 0;
60 video_context->default_fps = 0.0;
61 video_context->interlace_mode = GST_MATROSKA_INTERLACE_MODE_UNKNOWN;
62 video_context->field_order = GST_VIDEO_FIELD_ORDER_UNKNOWN;
63 video_context->earliest_time = GST_CLOCK_TIME_NONE;
64 video_context->dirac_unit = NULL;
65 video_context->earliest_time = GST_CLOCK_TIME_NONE;
66 video_context->multiview_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
67 video_context->multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
68 video_context->alpha_mode = FALSE;
69 video_context->colorimetry.range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
70 video_context->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
71 video_context->colorimetry.transfer = GST_VIDEO_TRANSFER_UNKNOWN;
72 video_context->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
73 gst_video_mastering_display_info_init
74 (&video_context->mastering_display_info);
75 video_context->mastering_display_info_present = FALSE;
76 gst_video_content_light_level_init (&video_context->content_light_level);
82 gst_matroska_track_init_audio_context (GstMatroskaTrackContext ** p_context)
84 GstMatroskaTrackAudioContext *audio_context;
86 g_assert (p_context != NULL && *p_context != NULL);
88 /* already set up? (track info might come before track type) */
89 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_AUDIO)
92 /* it better not have been set up as some other track type ... */
93 if ((*p_context)->type != 0) {
94 g_return_val_if_reached (FALSE);
97 audio_context = g_renew (GstMatroskaTrackAudioContext, *p_context, 1);
98 *p_context = (GstMatroskaTrackContext *) audio_context;
101 (*p_context)->type = GST_MATROSKA_TRACK_TYPE_AUDIO;
102 audio_context->channels = 1;
103 audio_context->samplerate = 8000;
104 audio_context->bitdepth = 16;
105 audio_context->wvpk_block_index = 0;
110 gst_matroska_track_init_subtitle_context (GstMatroskaTrackContext ** p_context)
112 GstMatroskaTrackSubtitleContext *subtitle_context;
114 g_assert (p_context != NULL && *p_context != NULL);
116 /* already set up? (track info might come before track type) */
117 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
120 /* it better not have been set up as some other track type ... */
121 if ((*p_context)->type != 0) {
122 g_return_val_if_reached (FALSE);
125 subtitle_context = g_renew (GstMatroskaTrackSubtitleContext, *p_context, 1);
126 *p_context = (GstMatroskaTrackContext *) subtitle_context;
128 (*p_context)->type = GST_MATROSKA_TRACK_TYPE_SUBTITLE;
129 subtitle_context->check_utf8 = TRUE;
130 subtitle_context->invalid_utf8 = FALSE;
131 subtitle_context->check_markup = TRUE;
132 subtitle_context->seen_markup_tag = FALSE;
137 gst_matroska_register_tags (void)
139 /* TODO: register other custom tags */
143 gst_matroska_parse_xiph_stream_headers (gpointer codec_data,
144 gsize codec_data_size)
146 GstBufferList *list = NULL;
147 guint8 *p = codec_data;
148 gint i, offset, num_packets;
151 GST_MEMDUMP ("xiph codec data", codec_data, codec_data_size);
153 if (codec_data == NULL || codec_data_size == 0)
156 /* start of the stream and vorbis audio or theora video, need to
157 * send the codec_priv data as first three packets */
158 num_packets = p[0] + 1;
159 GST_DEBUG ("%u stream headers, total length=%" G_GSIZE_FORMAT " bytes",
160 (guint) num_packets, codec_data_size);
162 length = g_alloca (num_packets * sizeof (guint));
166 /* first packets, read length values */
167 for (i = 0; i < num_packets - 1; i++) {
169 while (offset < codec_data_size) {
170 length[i] += p[offset];
171 if (p[offset++] != 0xff)
176 if (offset + last > codec_data_size)
179 /* last packet is the remaining size */
180 length[i] = codec_data_size - offset - last;
182 list = gst_buffer_list_new ();
184 for (i = 0; i < num_packets; i++) {
187 GST_DEBUG ("buffer %d: %u bytes", i, (guint) length[i]);
189 if (offset + length[i] > codec_data_size)
192 hdr = gst_buffer_new_memdup (p + offset, length[i]);
193 gst_buffer_list_add (list, hdr);
204 gst_buffer_list_unref (list);
210 gst_matroska_parse_speex_stream_headers (gpointer codec_data,
211 gsize codec_data_size)
213 GstBufferList *list = NULL;
215 guint8 *pdata = codec_data;
217 GST_MEMDUMP ("speex codec data", codec_data, codec_data_size);
219 if (codec_data == NULL || codec_data_size < 80) {
220 GST_WARNING ("not enough codec priv data for speex headers");
224 if (memcmp (pdata, "Speex ", 8) != 0) {
225 GST_WARNING ("no Speex marker at start of stream headers");
229 list = gst_buffer_list_new ();
231 hdr = gst_buffer_new_memdup (pdata, 80);
232 gst_buffer_list_add (list, hdr);
234 if (codec_data_size > 80) {
235 hdr = gst_buffer_new_memdup (pdata + 80, codec_data_size - 80);
236 gst_buffer_list_add (list, hdr);
243 gst_matroska_parse_opus_stream_headers (gpointer codec_data,
244 gsize codec_data_size)
246 GstBufferList *list = NULL;
248 guint8 *pdata = codec_data;
250 GST_MEMDUMP ("opus codec data", codec_data, codec_data_size);
252 if (codec_data == NULL || codec_data_size < 19) {
253 GST_WARNING ("not enough codec priv data for opus headers");
257 if (memcmp (pdata, "OpusHead", 8) != 0) {
258 GST_WARNING ("no OpusHead marker at start of stream headers");
262 list = gst_buffer_list_new ();
264 hdr = gst_buffer_new_memdup (pdata, codec_data_size);
265 gst_buffer_list_add (list, hdr);
271 gst_matroska_parse_flac_stream_headers (gpointer codec_data,
272 gsize codec_data_size)
274 GstBufferList *list = NULL;
276 guint8 *pdata = codec_data;
279 GST_MEMDUMP ("flac codec data", codec_data, codec_data_size);
281 /* need at least 'fLaC' marker + STREAMINFO metadata block */
282 if (codec_data == NULL || codec_data_size < ((4) + (4 + 34))) {
283 GST_WARNING ("not enough codec priv data for flac headers");
287 if (memcmp (pdata, "fLaC", 4) != 0) {
288 GST_WARNING ("no flac marker at start of stream headers");
292 list = gst_buffer_list_new ();
294 hdr = gst_buffer_new_memdup (pdata, 4);
295 gst_buffer_list_add (list, hdr);
297 /* skip fLaC marker */
300 while (off < codec_data_size - 3) {
301 len = GST_READ_UINT8 (pdata + off + 1) << 16;
302 len |= GST_READ_UINT8 (pdata + off + 2) << 8;
303 len |= GST_READ_UINT8 (pdata + off + 3);
305 GST_DEBUG ("header packet: len=%u bytes, flags=0x%02x", len, pdata[off]);
307 if (off + len > codec_data_size) {
308 gst_buffer_list_unref (list);
312 hdr = gst_buffer_new_memdup (pdata + off, len + 4);
313 gst_buffer_list_add (list, hdr);
321 gst_matroska_track_get_buffer_timestamp (GstMatroskaTrackContext * track,
324 if (track->dts_only) {
325 return GST_BUFFER_DTS_OR_PTS (buf);
327 return GST_BUFFER_PTS (buf);
332 gst_matroska_track_free (GstMatroskaTrackContext * track)
334 g_free (track->codec_id);
335 g_free (track->codec_name);
336 g_free (track->name);
337 g_free (track->language);
338 g_free (track->codec_priv);
339 g_free (track->codec_state);
340 gst_caps_replace (&track->caps, NULL);
342 if (track->encodings != NULL) {
345 for (i = 0; i < track->encodings->len; ++i) {
346 GstMatroskaTrackEncoding *enc = &g_array_index (track->encodings,
347 GstMatroskaTrackEncoding,
350 g_free (enc->comp_settings);
352 g_array_free (track->encodings, TRUE);
356 gst_tag_list_unref (track->tags);
358 if (track->index_table)
359 g_array_free (track->index_table, TRUE);
361 if (track->stream_headers)
362 gst_buffer_list_unref (track->stream_headers);
364 g_queue_foreach (&track->protection_event_queue, (GFunc) gst_event_unref,
366 g_queue_clear (&track->protection_event_queue);
368 if (track->protection_info)
369 gst_structure_free (track->protection_info);
375 matroska_track_encryption_algorithm_get_type (void)
377 static GType type = 0;
379 static const GEnumValue types[] = {
380 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_NONE, "Not encrypted",
382 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_DES, "DES encryption algorithm",
384 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_3DES, "3DES encryption algorithm",
386 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_TWOFISH,
387 "TwoFish encryption algorithm", "TwoFish"},
388 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_BLOWFISH,
389 "BlowFish encryption algorithm", "BlowFish"},
390 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_AES, "AES encryption algorithm",
396 type = g_enum_register_static ("MatroskaTrackEncryptionAlgorithm", types);
402 matroska_track_encryption_cipher_mode_get_type (void)
404 static GType type = 0;
406 static const GEnumValue types[] = {
407 {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_NONE, "Not defined",
409 {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_CTR, "CTR encryption mode",
415 type = g_enum_register_static ("MatroskaTrackEncryptionCipherMode", types);
421 matroska_track_encoding_scope_get_type (void)
423 static GType type = 0;
425 static const GEnumValue types[] = {
426 {GST_MATROSKA_TRACK_ENCODING_SCOPE_FRAME, "Encoding scope frame", "frame"},
427 {GST_MATROSKA_TRACK_ENCODING_SCOPE_CODEC_DATA, "Encoding scope codec data",
429 {GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING,
430 "Encoding scope next content", "next-content"},
435 type = g_enum_register_static ("MatroskaTrackEncodingScope", types);