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->earliest_time = GST_CLOCK_TIME_NONE;
62 video_context->multiview_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
63 video_context->multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
69 gst_matroska_track_init_audio_context (GstMatroskaTrackContext ** p_context)
71 GstMatroskaTrackAudioContext *audio_context;
73 g_assert (p_context != NULL && *p_context != NULL);
75 /* already set up? (track info might come before track type) */
76 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_AUDIO)
79 /* it better not have been set up as some other track type ... */
80 if ((*p_context)->type != 0) {
81 g_return_val_if_reached (FALSE);
84 audio_context = g_renew (GstMatroskaTrackAudioContext, *p_context, 1);
85 *p_context = (GstMatroskaTrackContext *) audio_context;
88 (*p_context)->type = GST_MATROSKA_TRACK_TYPE_AUDIO;
89 audio_context->channels = 1;
90 audio_context->samplerate = 8000;
95 gst_matroska_track_init_subtitle_context (GstMatroskaTrackContext ** p_context)
97 GstMatroskaTrackSubtitleContext *subtitle_context;
99 g_assert (p_context != NULL && *p_context != NULL);
101 /* already set up? (track info might come before track type) */
102 if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
105 /* it better not have been set up as some other track type ... */
106 if ((*p_context)->type != 0) {
107 g_return_val_if_reached (FALSE);
110 subtitle_context = g_renew (GstMatroskaTrackSubtitleContext, *p_context, 1);
111 *p_context = (GstMatroskaTrackContext *) subtitle_context;
113 (*p_context)->type = GST_MATROSKA_TRACK_TYPE_SUBTITLE;
114 subtitle_context->invalid_utf8 = FALSE;
115 subtitle_context->seen_markup_tag = FALSE;
120 gst_matroska_register_tags (void)
122 /* TODO: register other custom tags */
126 gst_matroska_parse_xiph_stream_headers (gpointer codec_data,
127 gsize codec_data_size)
129 GstBufferList *list = NULL;
130 guint8 *p = codec_data;
131 gint i, offset, num_packets;
134 GST_MEMDUMP ("xiph codec data", codec_data, codec_data_size);
136 if (codec_data == NULL || codec_data_size == 0)
139 /* start of the stream and vorbis audio or theora video, need to
140 * send the codec_priv data as first three packets */
141 num_packets = p[0] + 1;
142 GST_DEBUG ("%u stream headers, total length=%" G_GSIZE_FORMAT " bytes",
143 (guint) num_packets, codec_data_size);
145 length = g_alloca (num_packets * sizeof (guint));
149 /* first packets, read length values */
150 for (i = 0; i < num_packets - 1; i++) {
152 while (offset < codec_data_size) {
153 length[i] += p[offset];
154 if (p[offset++] != 0xff)
159 if (offset + last > codec_data_size)
162 /* last packet is the remaining size */
163 length[i] = codec_data_size - offset - last;
165 list = gst_buffer_list_new ();
167 for (i = 0; i < num_packets; i++) {
170 GST_DEBUG ("buffer %d: %u bytes", i, (guint) length[i]);
172 if (offset + length[i] > codec_data_size)
175 hdr = gst_buffer_new_wrapped (g_memdup (p + offset, length[i]), length[i]);
176 gst_buffer_list_add (list, hdr);
187 gst_buffer_list_unref (list);
193 gst_matroska_parse_speex_stream_headers (gpointer codec_data,
194 gsize codec_data_size)
196 GstBufferList *list = NULL;
198 guint8 *pdata = codec_data;
200 GST_MEMDUMP ("speex codec data", codec_data, codec_data_size);
202 if (codec_data == NULL || codec_data_size < 80) {
203 GST_WARNING ("not enough codec priv data for speex headers");
207 if (memcmp (pdata, "Speex ", 8) != 0) {
208 GST_WARNING ("no Speex marker at start of stream headers");
212 list = gst_buffer_list_new ();
214 hdr = gst_buffer_new_wrapped (g_memdup (pdata, 80), 80);
215 gst_buffer_list_add (list, hdr);
217 if (codec_data_size > 80) {
218 hdr = gst_buffer_new_wrapped (g_memdup (pdata + 80, codec_data_size - 80),
219 codec_data_size - 80);
220 gst_buffer_list_add (list, hdr);
227 gst_matroska_parse_opus_stream_headers (gpointer codec_data,
228 gsize codec_data_size)
230 GstBufferList *list = NULL;
232 guint8 *pdata = codec_data;
234 GST_MEMDUMP ("opus codec data", codec_data, codec_data_size);
236 if (codec_data == NULL || codec_data_size < 19) {
237 GST_WARNING ("not enough codec priv data for opus headers");
241 if (memcmp (pdata, "OpusHead", 8) != 0) {
242 GST_WARNING ("no OpusHead marker at start of stream headers");
246 list = gst_buffer_list_new ();
249 gst_buffer_new_wrapped (g_memdup (pdata, codec_data_size),
251 gst_buffer_list_add (list, hdr);
257 gst_matroska_parse_flac_stream_headers (gpointer codec_data,
258 gsize codec_data_size)
260 GstBufferList *list = NULL;
262 guint8 *pdata = codec_data;
265 GST_MEMDUMP ("flac codec data", codec_data, codec_data_size);
267 /* need at least 'fLaC' marker + STREAMINFO metadata block */
268 if (codec_data == NULL || codec_data_size < ((4) + (4 + 34))) {
269 GST_WARNING ("not enough codec priv data for flac headers");
273 if (memcmp (pdata, "fLaC", 4) != 0) {
274 GST_WARNING ("no flac marker at start of stream headers");
278 list = gst_buffer_list_new ();
280 hdr = gst_buffer_new_wrapped (g_memdup (pdata, 4), 4);
281 gst_buffer_list_add (list, hdr);
283 /* skip fLaC marker */
286 while (off < codec_data_size - 3) {
287 len = GST_READ_UINT8 (pdata + off + 1) << 16;
288 len |= GST_READ_UINT8 (pdata + off + 2) << 8;
289 len |= GST_READ_UINT8 (pdata + off + 3);
291 GST_DEBUG ("header packet: len=%u bytes, flags=0x%02x", len, pdata[off]);
293 if (off + len > codec_data_size) {
294 gst_buffer_list_unref (list);
298 hdr = gst_buffer_new_wrapped (g_memdup (pdata + off, len + 4), len + 4);
299 gst_buffer_list_add (list, hdr);
307 gst_matroska_track_get_buffer_timestamp (GstMatroskaTrackContext * track,
310 if (track->dts_only) {
311 return GST_BUFFER_DTS_OR_PTS (buf);
313 return GST_BUFFER_PTS (buf);
318 gst_matroska_track_free (GstMatroskaTrackContext * track)
320 g_free (track->codec_id);
321 g_free (track->codec_name);
322 g_free (track->name);
323 g_free (track->language);
324 g_free (track->codec_priv);
325 g_free (track->codec_state);
327 if (track->encodings != NULL) {
330 for (i = 0; i < track->encodings->len; ++i) {
331 GstMatroskaTrackEncoding *enc = &g_array_index (track->encodings,
332 GstMatroskaTrackEncoding,
335 g_free (enc->comp_settings);
337 g_array_free (track->encodings, TRUE);
341 gst_tag_list_unref (track->tags);
343 if (track->index_table)
344 g_array_free (track->index_table, TRUE);
346 if (track->stream_headers)
347 gst_buffer_list_unref (track->stream_headers);