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->earliest_time = GST_CLOCK_TIME_NONE;
63 video_context->dirac_unit = NULL;
64 video_context->earliest_time = GST_CLOCK_TIME_NONE;
65 video_context->multiview_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
66 video_context->multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
67 video_context->colorimetry.range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
68 video_context->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
69 video_context->colorimetry.transfer = GST_VIDEO_TRANSFER_UNKNOWN;
70 video_context->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
77 gst_matroska_track_init_audio_context (GstMatroskaTrackContext ** p_context)
79 GstMatroskaTrackAudioContext *audio_context;
81 g_assert (p_context != NULL && *p_context != NULL);
83 /* already set up? (track info might come before track type) */
84 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_AUDIO)
87 /* it better not have been set up as some other track type ... */
88 if ((*p_context)->type != 0) {
89 g_return_val_if_reached (FALSE);
92 audio_context = g_renew (GstMatroskaTrackAudioContext, *p_context, 1);
93 *p_context = (GstMatroskaTrackContext *) audio_context;
96 (*p_context)->type = GST_MATROSKA_TRACK_TYPE_AUDIO;
97 audio_context->channels = 1;
98 audio_context->samplerate = 8000;
99 audio_context->bitdepth = 16;
100 audio_context->wvpk_block_index = 0;
105 gst_matroska_track_init_subtitle_context (GstMatroskaTrackContext ** p_context)
107 GstMatroskaTrackSubtitleContext *subtitle_context;
109 g_assert (p_context != NULL && *p_context != NULL);
111 /* already set up? (track info might come before track type) */
112 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
115 /* it better not have been set up as some other track type ... */
116 if ((*p_context)->type != 0) {
117 g_return_val_if_reached (FALSE);
120 subtitle_context = g_renew (GstMatroskaTrackSubtitleContext, *p_context, 1);
121 *p_context = (GstMatroskaTrackContext *) subtitle_context;
123 (*p_context)->type = GST_MATROSKA_TRACK_TYPE_SUBTITLE;
124 subtitle_context->check_utf8 = TRUE;
125 subtitle_context->invalid_utf8 = FALSE;
126 subtitle_context->check_markup = TRUE;
127 subtitle_context->seen_markup_tag = FALSE;
132 gst_matroska_register_tags (void)
134 /* TODO: register other custom tags */
138 gst_matroska_parse_xiph_stream_headers (gpointer codec_data,
139 gsize codec_data_size)
141 GstBufferList *list = NULL;
142 guint8 *p = codec_data;
143 gint i, offset, num_packets;
146 GST_MEMDUMP ("xiph codec data", codec_data, codec_data_size);
148 if (codec_data == NULL || codec_data_size == 0)
151 /* start of the stream and vorbis audio or theora video, need to
152 * send the codec_priv data as first three packets */
153 num_packets = p[0] + 1;
154 GST_DEBUG ("%u stream headers, total length=%" G_GSIZE_FORMAT " bytes",
155 (guint) num_packets, codec_data_size);
157 length = g_alloca (num_packets * sizeof (guint));
161 /* first packets, read length values */
162 for (i = 0; i < num_packets - 1; i++) {
164 while (offset < codec_data_size) {
165 length[i] += p[offset];
166 if (p[offset++] != 0xff)
171 if (offset + last > codec_data_size)
174 /* last packet is the remaining size */
175 length[i] = codec_data_size - offset - last;
177 list = gst_buffer_list_new ();
179 for (i = 0; i < num_packets; i++) {
182 GST_DEBUG ("buffer %d: %u bytes", i, (guint) length[i]);
184 if (offset + length[i] > codec_data_size)
187 hdr = gst_buffer_new_wrapped (g_memdup (p + offset, length[i]), length[i]);
188 gst_buffer_list_add (list, hdr);
199 gst_buffer_list_unref (list);
205 gst_matroska_parse_speex_stream_headers (gpointer codec_data,
206 gsize codec_data_size)
208 GstBufferList *list = NULL;
210 guint8 *pdata = codec_data;
212 GST_MEMDUMP ("speex codec data", codec_data, codec_data_size);
214 if (codec_data == NULL || codec_data_size < 80) {
215 GST_WARNING ("not enough codec priv data for speex headers");
219 if (memcmp (pdata, "Speex ", 8) != 0) {
220 GST_WARNING ("no Speex marker at start of stream headers");
224 list = gst_buffer_list_new ();
226 hdr = gst_buffer_new_wrapped (g_memdup (pdata, 80), 80);
227 gst_buffer_list_add (list, hdr);
229 if (codec_data_size > 80) {
230 hdr = gst_buffer_new_wrapped (g_memdup (pdata + 80, codec_data_size - 80),
231 codec_data_size - 80);
232 gst_buffer_list_add (list, hdr);
239 gst_matroska_parse_opus_stream_headers (gpointer codec_data,
240 gsize codec_data_size)
242 GstBufferList *list = NULL;
244 guint8 *pdata = codec_data;
246 GST_MEMDUMP ("opus codec data", codec_data, codec_data_size);
248 if (codec_data == NULL || codec_data_size < 19) {
249 GST_WARNING ("not enough codec priv data for opus headers");
253 if (memcmp (pdata, "OpusHead", 8) != 0) {
254 GST_WARNING ("no OpusHead marker at start of stream headers");
258 list = gst_buffer_list_new ();
261 gst_buffer_new_wrapped (g_memdup (pdata, codec_data_size),
263 gst_buffer_list_add (list, hdr);
269 gst_matroska_parse_flac_stream_headers (gpointer codec_data,
270 gsize codec_data_size)
272 GstBufferList *list = NULL;
274 guint8 *pdata = codec_data;
277 GST_MEMDUMP ("flac codec data", codec_data, codec_data_size);
279 /* need at least 'fLaC' marker + STREAMINFO metadata block */
280 if (codec_data == NULL || codec_data_size < ((4) + (4 + 34))) {
281 GST_WARNING ("not enough codec priv data for flac headers");
285 if (memcmp (pdata, "fLaC", 4) != 0) {
286 GST_WARNING ("no flac marker at start of stream headers");
290 list = gst_buffer_list_new ();
292 hdr = gst_buffer_new_wrapped (g_memdup (pdata, 4), 4);
293 gst_buffer_list_add (list, hdr);
295 /* skip fLaC marker */
298 while (off < codec_data_size - 3) {
299 len = GST_READ_UINT8 (pdata + off + 1) << 16;
300 len |= GST_READ_UINT8 (pdata + off + 2) << 8;
301 len |= GST_READ_UINT8 (pdata + off + 3);
303 GST_DEBUG ("header packet: len=%u bytes, flags=0x%02x", len, pdata[off]);
305 if (off + len > codec_data_size) {
306 gst_buffer_list_unref (list);
310 hdr = gst_buffer_new_wrapped (g_memdup (pdata + off, len + 4), len + 4);
311 gst_buffer_list_add (list, hdr);
319 gst_matroska_track_get_buffer_timestamp (GstMatroskaTrackContext * track,
322 if (track->dts_only) {
323 return GST_BUFFER_DTS_OR_PTS (buf);
325 return GST_BUFFER_PTS (buf);
330 gst_matroska_track_free (GstMatroskaTrackContext * track)
332 g_free (track->codec_id);
333 g_free (track->codec_name);
334 g_free (track->name);
335 g_free (track->language);
336 g_free (track->codec_priv);
337 g_free (track->codec_state);
338 gst_caps_replace (&track->caps, NULL);
340 if (track->encodings != NULL) {
343 for (i = 0; i < track->encodings->len; ++i) {
344 GstMatroskaTrackEncoding *enc = &g_array_index (track->encodings,
345 GstMatroskaTrackEncoding,
348 g_free (enc->comp_settings);
350 g_array_free (track->encodings, TRUE);
354 gst_tag_list_unref (track->tags);
356 if (track->index_table)
357 g_array_free (track->index_table, TRUE);
359 if (track->stream_headers)
360 gst_buffer_list_unref (track->stream_headers);
362 g_queue_foreach (&track->protection_event_queue, (GFunc) gst_event_unref,
364 g_queue_clear (&track->protection_event_queue);
366 if (track->protection_info)
367 gst_structure_free (track->protection_info);
373 matroska_track_encryption_algorithm_get_type (void)
375 static GType type = 0;
377 static const GEnumValue types[] = {
378 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_NONE, "Not encrypted",
380 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_DES, "DES encryption algorithm",
382 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_3DES, "3DES encryption algorithm",
384 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_TWOFISH,
385 "TwoFish encryption algorithm", "TwoFish"},
386 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_BLOWFISH,
387 "BlowFish encryption algorithm", "BlowFish"},
388 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_AES, "AES encryption algorithm",
394 type = g_enum_register_static ("MatroskaTrackEncryptionAlgorithm", types);
400 matroska_track_encryption_cipher_mode_get_type (void)
402 static GType type = 0;
404 static const GEnumValue types[] = {
405 {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_NONE, "Not defined",
407 {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_CTR, "CTR encryption mode",
413 type = g_enum_register_static ("MatroskaTrackEncryptionCipherMode", types);
419 matroska_track_encoding_scope_get_type (void)
421 static GType type = 0;
423 static const GEnumValue types[] = {
424 {GST_MATROSKA_TRACK_ENCODING_SCOPE_FRAME, "Encoding scope frame", "frame"},
425 {GST_MATROSKA_TRACK_ENCODING_SCOPE_CODEC_DATA, "Encoding scope codec data",
427 {GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING,
428 "Encoding scope next content", "next-content"},
433 type = g_enum_register_static ("MatroskaTrackEncodingScope", types);