1 /* GStreamer Matroska muxer/demuxer
2 * (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * (C) 2006 Tim-Philipp Müller <tim centricular net>
4 *
5 * matroska-ids.c: matroska track context utility functions
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "matroska-ids.h"
28
29 #include <string.h>
30
31 gboolean
gst_matroska_track_init_video_context(GstMatroskaTrackContext ** p_context)32 gst_matroska_track_init_video_context (GstMatroskaTrackContext ** p_context)
33 {
34 GstMatroskaTrackVideoContext *video_context;
35
36 g_assert (p_context != NULL && *p_context != NULL);
37
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");
41 return TRUE;
42 }
43
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);
47 }
48
49 video_context = g_renew (GstMatroskaTrackVideoContext, *p_context, 1);
50 *p_context = (GstMatroskaTrackContext *) video_context;
51
52 /* defaults */
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);
77
78 return TRUE;
79 }
80
81 gboolean
gst_matroska_track_init_audio_context(GstMatroskaTrackContext ** p_context)82 gst_matroska_track_init_audio_context (GstMatroskaTrackContext ** p_context)
83 {
84 GstMatroskaTrackAudioContext *audio_context;
85
86 g_assert (p_context != NULL && *p_context != NULL);
87
88 /* already set up? (track info might come before track type) */
89 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_AUDIO)
90 return TRUE;
91
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);
95 }
96
97 audio_context = g_renew (GstMatroskaTrackAudioContext, *p_context, 1);
98 *p_context = (GstMatroskaTrackContext *) audio_context;
99
100 /* defaults */
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;
106 return TRUE;
107 }
108
109 gboolean
gst_matroska_track_init_subtitle_context(GstMatroskaTrackContext ** p_context)110 gst_matroska_track_init_subtitle_context (GstMatroskaTrackContext ** p_context)
111 {
112 GstMatroskaTrackSubtitleContext *subtitle_context;
113
114 g_assert (p_context != NULL && *p_context != NULL);
115
116 /* already set up? (track info might come before track type) */
117 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
118 return TRUE;
119
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);
123 }
124
125 subtitle_context = g_renew (GstMatroskaTrackSubtitleContext, *p_context, 1);
126 *p_context = (GstMatroskaTrackContext *) subtitle_context;
127
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;
133 return TRUE;
134 }
135
136 void
gst_matroska_register_tags(void)137 gst_matroska_register_tags (void)
138 {
139 /* TODO: register other custom tags */
140 }
141
142 GstBufferList *
gst_matroska_parse_xiph_stream_headers(gpointer codec_data,gsize codec_data_size)143 gst_matroska_parse_xiph_stream_headers (gpointer codec_data,
144 gsize codec_data_size)
145 {
146 GstBufferList *list = NULL;
147 guint8 *p = codec_data;
148 gint i, offset, num_packets;
149 guint *length, last;
150
151 GST_MEMDUMP ("xiph codec data", codec_data, codec_data_size);
152
153 if (codec_data == NULL || codec_data_size == 0)
154 goto error;
155
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);
161
162 length = g_alloca (num_packets * sizeof (guint));
163 last = 0;
164 offset = 1;
165
166 /* first packets, read length values */
167 for (i = 0; i < num_packets - 1; i++) {
168 length[i] = 0;
169 while (offset < codec_data_size) {
170 length[i] += p[offset];
171 if (p[offset++] != 0xff)
172 break;
173 }
174 last += length[i];
175 }
176 if (offset + last > codec_data_size)
177 goto error;
178
179 /* last packet is the remaining size */
180 length[i] = codec_data_size - offset - last;
181
182 list = gst_buffer_list_new ();
183
184 for (i = 0; i < num_packets; i++) {
185 GstBuffer *hdr;
186
187 GST_DEBUG ("buffer %d: %u bytes", i, (guint) length[i]);
188
189 if (offset + length[i] > codec_data_size)
190 goto error;
191
192 hdr = gst_buffer_new_memdup (p + offset, length[i]);
193 gst_buffer_list_add (list, hdr);
194
195 offset += length[i];
196 }
197
198 return list;
199
200 /* ERRORS */
201 error:
202 {
203 if (list != NULL)
204 gst_buffer_list_unref (list);
205 return NULL;
206 }
207 }
208
209 GstBufferList *
gst_matroska_parse_speex_stream_headers(gpointer codec_data,gsize codec_data_size)210 gst_matroska_parse_speex_stream_headers (gpointer codec_data,
211 gsize codec_data_size)
212 {
213 GstBufferList *list = NULL;
214 GstBuffer *hdr;
215 guint8 *pdata = codec_data;
216
217 GST_MEMDUMP ("speex codec data", codec_data, codec_data_size);
218
219 if (codec_data == NULL || codec_data_size < 80) {
220 GST_WARNING ("not enough codec priv data for speex headers");
221 return NULL;
222 }
223
224 if (memcmp (pdata, "Speex ", 8) != 0) {
225 GST_WARNING ("no Speex marker at start of stream headers");
226 return NULL;
227 }
228
229 list = gst_buffer_list_new ();
230
231 hdr = gst_buffer_new_memdup (pdata, 80);
232 gst_buffer_list_add (list, hdr);
233
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);
237 }
238
239 return list;
240 }
241
242 GstBufferList *
gst_matroska_parse_opus_stream_headers(gpointer codec_data,gsize codec_data_size)243 gst_matroska_parse_opus_stream_headers (gpointer codec_data,
244 gsize codec_data_size)
245 {
246 GstBufferList *list = NULL;
247 GstBuffer *hdr;
248 guint8 *pdata = codec_data;
249
250 GST_MEMDUMP ("opus codec data", codec_data, codec_data_size);
251
252 if (codec_data == NULL || codec_data_size < 19) {
253 GST_WARNING ("not enough codec priv data for opus headers");
254 return NULL;
255 }
256
257 if (memcmp (pdata, "OpusHead", 8) != 0) {
258 GST_WARNING ("no OpusHead marker at start of stream headers");
259 return NULL;
260 }
261
262 list = gst_buffer_list_new ();
263
264 hdr = gst_buffer_new_memdup (pdata, codec_data_size);
265 gst_buffer_list_add (list, hdr);
266
267 return list;
268 }
269
270 GstBufferList *
gst_matroska_parse_flac_stream_headers(gpointer codec_data,gsize codec_data_size)271 gst_matroska_parse_flac_stream_headers (gpointer codec_data,
272 gsize codec_data_size)
273 {
274 GstBufferList *list = NULL;
275 GstBuffer *hdr;
276 guint8 *pdata = codec_data;
277 guint len, off;
278
279 GST_MEMDUMP ("flac codec data", codec_data, codec_data_size);
280
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");
284 return NULL;
285 }
286
287 if (memcmp (pdata, "fLaC", 4) != 0) {
288 GST_WARNING ("no flac marker at start of stream headers");
289 return NULL;
290 }
291
292 list = gst_buffer_list_new ();
293
294 hdr = gst_buffer_new_memdup (pdata, 4);
295 gst_buffer_list_add (list, hdr);
296
297 /* skip fLaC marker */
298 off = 4;
299
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);
304
305 GST_DEBUG ("header packet: len=%u bytes, flags=0x%02x", len, pdata[off]);
306
307 if (off + len > codec_data_size) {
308 gst_buffer_list_unref (list);
309 return NULL;
310 }
311
312 hdr = gst_buffer_new_memdup (pdata + off, len + 4);
313 gst_buffer_list_add (list, hdr);
314
315 off += 4 + len;
316 }
317 return list;
318 }
319
320 GstClockTime
gst_matroska_track_get_buffer_timestamp(GstMatroskaTrackContext * track,GstBuffer * buf)321 gst_matroska_track_get_buffer_timestamp (GstMatroskaTrackContext * track,
322 GstBuffer * buf)
323 {
324 if (track->dts_only) {
325 return GST_BUFFER_DTS_OR_PTS (buf);
326 } else {
327 return GST_BUFFER_PTS (buf);
328 }
329 }
330
331 void
gst_matroska_track_free(GstMatroskaTrackContext * track)332 gst_matroska_track_free (GstMatroskaTrackContext * track)
333 {
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);
341
342 if (track->encodings != NULL) {
343 int i;
344
345 for (i = 0; i < track->encodings->len; ++i) {
346 GstMatroskaTrackEncoding *enc = &g_array_index (track->encodings,
347 GstMatroskaTrackEncoding,
348 i);
349
350 g_free (enc->comp_settings);
351 }
352 g_array_unref (track->encodings);
353 }
354
355 if (track->tags)
356 gst_tag_list_unref (track->tags);
357
358 if (track->index_table)
359 g_array_unref (track->index_table);
360
361 if (track->stream_headers)
362 gst_buffer_list_unref (track->stream_headers);
363
364 g_queue_foreach (&track->protection_event_queue, (GFunc) gst_event_unref,
365 NULL);
366 g_queue_clear (&track->protection_event_queue);
367
368 if (track->protection_info)
369 gst_structure_free (track->protection_info);
370
371 g_free (track);
372 }
373
374 GType
matroska_track_encryption_algorithm_get_type(void)375 matroska_track_encryption_algorithm_get_type (void)
376 {
377 static GType type = 0;
378
379 static const GEnumValue types[] = {
380 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_NONE, "Not encrypted",
381 "None"},
382 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_DES, "DES encryption algorithm",
383 "DES"},
384 {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_3DES, "3DES encryption algorithm",
385 "3DES"},
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",
391 "AES"},
392 {0, NULL, NULL}
393 };
394
395 if (!type) {
396 type = g_enum_register_static ("MatroskaTrackEncryptionAlgorithm", types);
397 }
398 return type;
399 }
400
401 GType
matroska_track_encryption_cipher_mode_get_type(void)402 matroska_track_encryption_cipher_mode_get_type (void)
403 {
404 static GType type = 0;
405
406 static const GEnumValue types[] = {
407 {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_NONE, "Not defined",
408 "None"},
409 {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_CTR, "CTR encryption mode",
410 "CTR"},
411 {0, NULL, NULL}
412 };
413
414 if (!type) {
415 type = g_enum_register_static ("MatroskaTrackEncryptionCipherMode", types);
416 }
417 return type;
418 }
419
420 GType
matroska_track_encoding_scope_get_type(void)421 matroska_track_encoding_scope_get_type (void)
422 {
423 static GType type = 0;
424
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",
428 "codec-data"},
429 {GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING,
430 "Encoding scope next content", "next-content"},
431 {0, NULL, NULL}
432 };
433
434 if (!type) {
435 type = g_enum_register_static ("MatroskaTrackEncodingScope", types);
436 }
437 return type;
438 }
439