matroska-ids.c 11.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* GStreamer Matroska muxer/demuxer
 * (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 * (C) 2006 Tim-Philipp Müller <tim centricular net>
 *
 * matroska-ids.c: matroska track context utility functions
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19
20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
21
22
23
24
25
26
27
28
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "matroska-ids.h"

29
30
#include <string.h>

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
gboolean
gst_matroska_track_init_video_context (GstMatroskaTrackContext ** p_context)
{
  GstMatroskaTrackVideoContext *video_context;

  g_assert (p_context != NULL && *p_context != NULL);

  /* already set up? (track info might come before track type) */
  if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_VIDEO) {
    GST_LOG ("video context already set up");
    return TRUE;
  }

  /* it better not have been set up as some other track type ... */
  if ((*p_context)->type != 0) {
    g_return_val_if_reached (FALSE);
  }

  video_context = g_renew (GstMatroskaTrackVideoContext, *p_context, 1);
  *p_context = (GstMatroskaTrackContext *) video_context;

  /* defaults */
  (*p_context)->type = GST_MATROSKA_TRACK_TYPE_VIDEO;
  video_context->display_width = 0;
  video_context->display_height = 0;
  video_context->pixel_width = 0;
  video_context->pixel_height = 0;
  video_context->asr_mode = 0;
  video_context->fourcc = 0;
60
  video_context->default_fps = 0.0;
61
62
63
  video_context->interlace_mode = GST_MATROSKA_INTERLACE_MODE_UNKNOWN;
  video_context->earliest_time = GST_CLOCK_TIME_NONE;
  video_context->dirac_unit = NULL;
64
  video_context->earliest_time = GST_CLOCK_TIME_NONE;
65
66
  video_context->multiview_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
  video_context->multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
67
68
69
70
71
  video_context->colorimetry.range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
  video_context->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
  video_context->colorimetry.transfer = GST_VIDEO_TRANSFER_UNKNOWN;
  video_context->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;

72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  return TRUE;
}

gboolean
gst_matroska_track_init_audio_context (GstMatroskaTrackContext ** p_context)
{
  GstMatroskaTrackAudioContext *audio_context;

  g_assert (p_context != NULL && *p_context != NULL);

  /* already set up? (track info might come before track type) */
  if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_AUDIO)
    return TRUE;

  /* it better not have been set up as some other track type ... */
  if ((*p_context)->type != 0) {
    g_return_val_if_reached (FALSE);
  }

  audio_context = g_renew (GstMatroskaTrackAudioContext, *p_context, 1);
  *p_context = (GstMatroskaTrackContext *) audio_context;

  /* defaults */
  (*p_context)->type = GST_MATROSKA_TRACK_TYPE_AUDIO;
  audio_context->channels = 1;
  audio_context->samplerate = 8000;
99
100
  audio_context->bitdepth = 16;
  audio_context->wvpk_block_index = 0;
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  return TRUE;
}

gboolean
gst_matroska_track_init_subtitle_context (GstMatroskaTrackContext ** p_context)
{
  GstMatroskaTrackSubtitleContext *subtitle_context;

  g_assert (p_context != NULL && *p_context != NULL);

  /* already set up? (track info might come before track type) */
  if ((*p_context)->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
    return TRUE;

  /* it better not have been set up as some other track type ... */
  if ((*p_context)->type != 0) {
    g_return_val_if_reached (FALSE);
  }

  subtitle_context = g_renew (GstMatroskaTrackSubtitleContext, *p_context, 1);
  *p_context = (GstMatroskaTrackContext *) subtitle_context;

  (*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
129
  return TRUE;
}
130
131
132
133
134
135

void
gst_matroska_register_tags (void)
{
  /* TODO: register other custom tags */
}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

GstBufferList *
gst_matroska_parse_xiph_stream_headers (gpointer codec_data,
    gsize codec_data_size)
{
  GstBufferList *list = NULL;
  guint8 *p = codec_data;
  gint i, offset, num_packets;
  guint *length, last;

  GST_MEMDUMP ("xiph codec data", codec_data, codec_data_size);

  if (codec_data == NULL || codec_data_size == 0)
    goto error;

  /* start of the stream and vorbis audio or theora video, need to
   * send the codec_priv data as first three packets */
  num_packets = p[0] + 1;
  GST_DEBUG ("%u stream headers, total length=%" G_GSIZE_FORMAT " bytes",
      (guint) num_packets, codec_data_size);

  length = g_alloca (num_packets * sizeof (guint));
  last = 0;
  offset = 1;

  /* first packets, read length values */
  for (i = 0; i < num_packets - 1; i++) {
    length[i] = 0;
    while (offset < codec_data_size) {
      length[i] += p[offset];
      if (p[offset++] != 0xff)
        break;
    }
    last += length[i];
  }
  if (offset + last > codec_data_size)
    goto error;

  /* last packet is the remaining size */
  length[i] = codec_data_size - offset - last;

  list = gst_buffer_list_new ();

  for (i = 0; i < num_packets; i++) {
    GstBuffer *hdr;

    GST_DEBUG ("buffer %d: %u bytes", i, (guint) length[i]);

    if (offset + length[i] > codec_data_size)
      goto error;

    hdr = gst_buffer_new_wrapped (g_memdup (p + offset, length[i]), length[i]);
    gst_buffer_list_add (list, hdr);

    offset += length[i];
  }

  return list;

/* ERRORS */
error:
  {
    if (list != NULL)
      gst_buffer_list_unref (list);
    return NULL;
  }
}

GstBufferList *
gst_matroska_parse_speex_stream_headers (gpointer codec_data,
    gsize codec_data_size)
{
  GstBufferList *list = NULL;
  GstBuffer *hdr;
  guint8 *pdata = codec_data;

  GST_MEMDUMP ("speex codec data", codec_data, codec_data_size);

  if (codec_data == NULL || codec_data_size < 80) {
    GST_WARNING ("not enough codec priv data for speex headers");
    return NULL;
  }

  if (memcmp (pdata, "Speex   ", 8) != 0) {
    GST_WARNING ("no Speex marker at start of stream headers");
    return NULL;
  }

  list = gst_buffer_list_new ();

  hdr = gst_buffer_new_wrapped (g_memdup (pdata, 80), 80);
  gst_buffer_list_add (list, hdr);

  if (codec_data_size > 80) {
    hdr = gst_buffer_new_wrapped (g_memdup (pdata + 80, codec_data_size - 80),
        codec_data_size - 80);
    gst_buffer_list_add (list, hdr);
  }

  return list;
}

GstBufferList *
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
gst_matroska_parse_opus_stream_headers (gpointer codec_data,
    gsize codec_data_size)
{
  GstBufferList *list = NULL;
  GstBuffer *hdr;
  guint8 *pdata = codec_data;

  GST_MEMDUMP ("opus codec data", codec_data, codec_data_size);

  if (codec_data == NULL || codec_data_size < 19) {
    GST_WARNING ("not enough codec priv data for opus headers");
    return NULL;
  }

  if (memcmp (pdata, "OpusHead", 8) != 0) {
    GST_WARNING ("no OpusHead marker at start of stream headers");
    return NULL;
  }

  list = gst_buffer_list_new ();

  hdr =
      gst_buffer_new_wrapped (g_memdup (pdata, codec_data_size),
      codec_data_size);
  gst_buffer_list_add (list, hdr);

  return list;
}

GstBufferList *
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
gst_matroska_parse_flac_stream_headers (gpointer codec_data,
    gsize codec_data_size)
{
  GstBufferList *list = NULL;
  GstBuffer *hdr;
  guint8 *pdata = codec_data;
  guint len, off;

  GST_MEMDUMP ("flac codec data", codec_data, codec_data_size);

  /* need at least 'fLaC' marker + STREAMINFO metadata block */
  if (codec_data == NULL || codec_data_size < ((4) + (4 + 34))) {
    GST_WARNING ("not enough codec priv data for flac headers");
    return NULL;
  }

  if (memcmp (pdata, "fLaC", 4) != 0) {
    GST_WARNING ("no flac marker at start of stream headers");
    return NULL;
  }

  list = gst_buffer_list_new ();

  hdr = gst_buffer_new_wrapped (g_memdup (pdata, 4), 4);
  gst_buffer_list_add (list, hdr);

  /* skip fLaC marker */
  off = 4;

298
  while (off < codec_data_size - 3) {
299
300
301
302
303
304
    len = GST_READ_UINT8 (pdata + off + 1) << 16;
    len |= GST_READ_UINT8 (pdata + off + 2) << 8;
    len |= GST_READ_UINT8 (pdata + off + 3);

    GST_DEBUG ("header packet: len=%u bytes, flags=0x%02x", len, pdata[off]);

305
306
307
308
309
    if (off + len > codec_data_size) {
      gst_buffer_list_unref (list);
      return NULL;
    }

310
311
312
313
314
315
316
    hdr = gst_buffer_new_wrapped (g_memdup (pdata + off, len + 4), len + 4);
    gst_buffer_list_add (list, hdr);

    off += 4 + len;
  }
  return list;
}
317

318
319
320
321
322
GstClockTime
gst_matroska_track_get_buffer_timestamp (GstMatroskaTrackContext * track,
    GstBuffer * buf)
{
  if (track->dts_only) {
323
    return GST_BUFFER_DTS_OR_PTS (buf);
324
325
326
327
328
  } else {
    return GST_BUFFER_PTS (buf);
  }
}

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
void
gst_matroska_track_free (GstMatroskaTrackContext * track)
{
  g_free (track->codec_id);
  g_free (track->codec_name);
  g_free (track->name);
  g_free (track->language);
  g_free (track->codec_priv);
  g_free (track->codec_state);

  if (track->encodings != NULL) {
    int i;

    for (i = 0; i < track->encodings->len; ++i) {
      GstMatroskaTrackEncoding *enc = &g_array_index (track->encodings,
          GstMatroskaTrackEncoding,
          i);

      g_free (enc->comp_settings);
    }
    g_array_free (track->encodings, TRUE);
  }

352
353
  if (track->tags)
    gst_tag_list_unref (track->tags);
354
355
356
357
358
359
360

  if (track->index_table)
    g_array_free (track->index_table, TRUE);

  if (track->stream_headers)
    gst_buffer_list_unref (track->stream_headers);

361
362
363
364
365
366
367
  g_queue_foreach (&track->protection_event_queue, (GFunc) gst_event_unref,
      NULL);
  g_queue_clear (&track->protection_event_queue);

  if (track->protection_info)
    gst_structure_free (track->protection_info);

368
369
  g_free (track);
}
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435

GType
matroska_track_encryption_algorithm_get_type (void)
{
  static GType type = 0;

  static const GEnumValue types[] = {
    {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_NONE, "Not encrypted",
        "None"},
    {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_DES, "DES encryption algorithm",
        "DES"},
    {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_3DES, "3DES encryption algorithm",
        "3DES"},
    {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_TWOFISH,
        "TwoFish encryption algorithm", "TwoFish"},
    {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_BLOWFISH,
        "BlowFish encryption algorithm", "BlowFish"},
    {GST_MATROSKA_TRACK_ENCRYPTION_ALGORITHM_AES, "AES encryption algorithm",
        "AES"},
    {0, NULL, NULL}
  };

  if (!type) {
    type = g_enum_register_static ("MatroskaTrackEncryptionAlgorithm", types);
  }
  return type;
}

GType
matroska_track_encryption_cipher_mode_get_type (void)
{
  static GType type = 0;

  static const GEnumValue types[] = {
    {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_NONE, "Not defined",
        "None"},
    {GST_MATROSKA_TRACK_ENCRYPTION_CIPHER_MODE_CTR, "CTR encryption mode",
        "CTR"},
    {0, NULL, NULL}
  };

  if (!type) {
    type = g_enum_register_static ("MatroskaTrackEncryptionCipherMode", types);
  }
  return type;
}

GType
matroska_track_encoding_scope_get_type (void)
{
  static GType type = 0;

  static const GEnumValue types[] = {
    {GST_MATROSKA_TRACK_ENCODING_SCOPE_FRAME, "Encoding scope frame", "frame"},
    {GST_MATROSKA_TRACK_ENCODING_SCOPE_CODEC_DATA, "Encoding scope codec data",
        "codec-data"},
    {GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING,
        "Encoding scope next content", "next-content"},
    {0, NULL, NULL}
  };

  if (!type) {
    type = g_enum_register_static ("MatroskaTrackEncodingScope", types);
  }
  return type;
}