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->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;
71 
72 
73   return TRUE;
74 }
75 
76 gboolean
gst_matroska_track_init_audio_context(GstMatroskaTrackContext ** p_context)77 gst_matroska_track_init_audio_context (GstMatroskaTrackContext ** p_context)
78 {
79   GstMatroskaTrackAudioContext *audio_context;
80 
81   g_assert (p_context != NULL && *p_context != NULL);
82 
83   /* already set up? (track info might come before track type) */
84   if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_AUDIO)
85     return TRUE;
86 
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);
90   }
91 
92   audio_context = g_renew (GstMatroskaTrackAudioContext, *p_context, 1);
93   *p_context = (GstMatroskaTrackContext *) audio_context;
94 
95   /* defaults */
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;
101   return TRUE;
102 }
103 
104 gboolean
gst_matroska_track_init_subtitle_context(GstMatroskaTrackContext ** p_context)105 gst_matroska_track_init_subtitle_context (GstMatroskaTrackContext ** p_context)
106 {
107   GstMatroskaTrackSubtitleContext *subtitle_context;
108 
109   g_assert (p_context != NULL && *p_context != NULL);
110 
111   /* already set up? (track info might come before track type) */
112   if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
113     return TRUE;
114 
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);
118   }
119 
120   subtitle_context = g_renew (GstMatroskaTrackSubtitleContext, *p_context, 1);
121   *p_context = (GstMatroskaTrackContext *) subtitle_context;
122 
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;
128   return TRUE;
129 }
130 
131 void
gst_matroska_register_tags(void)132 gst_matroska_register_tags (void)
133 {
134   /* TODO: register other custom tags */
135 }
136 
137 GstBufferList *
gst_matroska_parse_xiph_stream_headers(gpointer codec_data,gsize codec_data_size)138 gst_matroska_parse_xiph_stream_headers (gpointer codec_data,
139     gsize codec_data_size)
140 {
141   GstBufferList *list = NULL;
142   guint8 *p = codec_data;
143   gint i, offset, num_packets;
144   guint *length, last;
145 
146   GST_MEMDUMP ("xiph codec data", codec_data, codec_data_size);
147 
148   if (codec_data == NULL || codec_data_size == 0)
149     goto error;
150 
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);
156 
157   length = g_alloca (num_packets * sizeof (guint));
158   last = 0;
159   offset = 1;
160 
161   /* first packets, read length values */
162   for (i = 0; i < num_packets - 1; i++) {
163     length[i] = 0;
164     while (offset < codec_data_size) {
165       length[i] += p[offset];
166       if (p[offset++] != 0xff)
167         break;
168     }
169     last += length[i];
170   }
171   if (offset + last > codec_data_size)
172     goto error;
173 
174   /* last packet is the remaining size */
175   length[i] = codec_data_size - offset - last;
176 
177   list = gst_buffer_list_new ();
178 
179   for (i = 0; i < num_packets; i++) {
180     GstBuffer *hdr;
181 
182     GST_DEBUG ("buffer %d: %u bytes", i, (guint) length[i]);
183 
184     if (offset + length[i] > codec_data_size)
185       goto error;
186 
187     hdr = gst_buffer_new_wrapped (g_memdup (p + offset, length[i]), length[i]);
188     gst_buffer_list_add (list, hdr);
189 
190     offset += length[i];
191   }
192 
193   return list;
194 
195 /* ERRORS */
196 error:
197   {
198     if (list != NULL)
199       gst_buffer_list_unref (list);
200     return NULL;
201   }
202 }
203 
204 GstBufferList *
gst_matroska_parse_speex_stream_headers(gpointer codec_data,gsize codec_data_size)205 gst_matroska_parse_speex_stream_headers (gpointer codec_data,
206     gsize codec_data_size)
207 {
208   GstBufferList *list = NULL;
209   GstBuffer *hdr;
210   guint8 *pdata = codec_data;
211 
212   GST_MEMDUMP ("speex codec data", codec_data, codec_data_size);
213 
214   if (codec_data == NULL || codec_data_size < 80) {
215     GST_WARNING ("not enough codec priv data for speex headers");
216     return NULL;
217   }
218 
219   if (memcmp (pdata, "Speex   ", 8) != 0) {
220     GST_WARNING ("no Speex marker at start of stream headers");
221     return NULL;
222   }
223 
224   list = gst_buffer_list_new ();
225 
226   hdr = gst_buffer_new_wrapped (g_memdup (pdata, 80), 80);
227   gst_buffer_list_add (list, hdr);
228 
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);
233   }
234 
235   return list;
236 }
237 
238 GstBufferList *
gst_matroska_parse_opus_stream_headers(gpointer codec_data,gsize codec_data_size)239 gst_matroska_parse_opus_stream_headers (gpointer codec_data,
240     gsize codec_data_size)
241 {
242   GstBufferList *list = NULL;
243   GstBuffer *hdr;
244   guint8 *pdata = codec_data;
245 
246   GST_MEMDUMP ("opus codec data", codec_data, codec_data_size);
247 
248   if (codec_data == NULL || codec_data_size < 19) {
249     GST_WARNING ("not enough codec priv data for opus headers");
250     return NULL;
251   }
252 
253   if (memcmp (pdata, "OpusHead", 8) != 0) {
254     GST_WARNING ("no OpusHead marker at start of stream headers");
255     return NULL;
256   }
257 
258   list = gst_buffer_list_new ();
259 
260   hdr =
261       gst_buffer_new_wrapped (g_memdup (pdata, codec_data_size),
262       codec_data_size);
263   gst_buffer_list_add (list, hdr);
264 
265   return list;
266 }
267 
268 GstBufferList *
gst_matroska_parse_flac_stream_headers(gpointer codec_data,gsize codec_data_size)269 gst_matroska_parse_flac_stream_headers (gpointer codec_data,
270     gsize codec_data_size)
271 {
272   GstBufferList *list = NULL;
273   GstBuffer *hdr;
274   guint8 *pdata = codec_data;
275   guint len, off;
276 
277   GST_MEMDUMP ("flac codec data", codec_data, codec_data_size);
278 
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");
282     return NULL;
283   }
284 
285   if (memcmp (pdata, "fLaC", 4) != 0) {
286     GST_WARNING ("no flac marker at start of stream headers");
287     return NULL;
288   }
289 
290   list = gst_buffer_list_new ();
291 
292   hdr = gst_buffer_new_wrapped (g_memdup (pdata, 4), 4);
293   gst_buffer_list_add (list, hdr);
294 
295   /* skip fLaC marker */
296   off = 4;
297 
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);
302 
303     GST_DEBUG ("header packet: len=%u bytes, flags=0x%02x", len, pdata[off]);
304 
305     if (off + len > codec_data_size) {
306       gst_buffer_list_unref (list);
307       return NULL;
308     }
309 
310     hdr = gst_buffer_new_wrapped (g_memdup (pdata + off, len + 4), len + 4);
311     gst_buffer_list_add (list, hdr);
312 
313     off += 4 + len;
314   }
315   return list;
316 }
317 
318 GstClockTime
gst_matroska_track_get_buffer_timestamp(GstMatroskaTrackContext * track,GstBuffer * buf)319 gst_matroska_track_get_buffer_timestamp (GstMatroskaTrackContext * track,
320     GstBuffer * buf)
321 {
322   if (track->dts_only) {
323     return GST_BUFFER_DTS_OR_PTS (buf);
324   } else {
325     return GST_BUFFER_PTS (buf);
326   }
327 }
328 
329 void
gst_matroska_track_free(GstMatroskaTrackContext * track)330 gst_matroska_track_free (GstMatroskaTrackContext * track)
331 {
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);
339 
340   if (track->encodings != NULL) {
341     int i;
342 
343     for (i = 0; i < track->encodings->len; ++i) {
344       GstMatroskaTrackEncoding *enc = &g_array_index (track->encodings,
345           GstMatroskaTrackEncoding,
346           i);
347 
348       g_free (enc->comp_settings);
349     }
350     g_array_free (track->encodings, TRUE);
351   }
352 
353   if (track->tags)
354     gst_tag_list_unref (track->tags);
355 
356   if (track->index_table)
357     g_array_free (track->index_table, TRUE);
358 
359   if (track->stream_headers)
360     gst_buffer_list_unref (track->stream_headers);
361 
362   g_queue_foreach (&track->protection_event_queue, (GFunc) gst_event_unref,
363       NULL);
364   g_queue_clear (&track->protection_event_queue);
365 
366   if (track->protection_info)
367     gst_structure_free (track->protection_info);
368 
369   g_free (track);
370 }
371 
372 GType
matroska_track_encryption_algorithm_get_type(void)373 matroska_track_encryption_algorithm_get_type (void)
374 {
375   static GType type = 0;
376 
377   static const GEnumValue types[] = {
378     {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_NONE, "Not encrypted",
379         "None"},
380     {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_DES, "DES encryption algorithm",
381         "DES"},
382     {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_3DES, "3DES encryption algorithm",
383         "3DES"},
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",
389         "AES"},
390     {0, NULL, NULL}
391   };
392 
393   if (!type) {
394     type = g_enum_register_static ("MatroskaTrackEncryptionAlgorithm", types);
395   }
396   return type;
397 }
398 
399 GType
matroska_track_encryption_cipher_mode_get_type(void)400 matroska_track_encryption_cipher_mode_get_type (void)
401 {
402   static GType type = 0;
403 
404   static const GEnumValue types[] = {
405     {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_NONE, "Not defined",
406         "None"},
407     {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_CTR, "CTR encryption mode",
408         "CTR"},
409     {0, NULL, NULL}
410   };
411 
412   if (!type) {
413     type = g_enum_register_static ("MatroskaTrackEncryptionCipherMode", types);
414   }
415   return type;
416 }
417 
418 GType
matroska_track_encoding_scope_get_type(void)419 matroska_track_encoding_scope_get_type (void)
420 {
421   static GType type = 0;
422 
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",
426         "codec-data"},
427     {GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING,
428         "Encoding scope next content", "next-content"},
429     {0, NULL, NULL}
430   };
431 
432   if (!type) {
433     type = g_enum_register_static ("MatroskaTrackEncodingScope", types);
434   }
435   return type;
436 }
437