2 * Copyright (C) 2008-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* Implementation of SMPTE 381M - Mapping MPEG streams into the MXF
25 * - Handle PES streams
26 * - Fix TS/PS demuxers to forward timestamps
27 * - h264 support (see SMPTE RP2008)
36 #include <gst/video/video.h>
41 #include "mxfessence.h"
43 #include <gst/base/gstbytereader.h>
45 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
46 #define GST_CAT_DEFAULT mxf_debug
48 /* SMPTE 381M 8.1 - ULs of local tags */
49 static const guint8 _single_sequence_ul[] = {
50 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
54 static const guint8 _constant_b_frames_ul[] = {
55 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
59 static const guint8 _coded_content_type_ul[] = {
60 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
64 static const guint8 _low_delay_ul[] = {
65 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
69 static const guint8 _closed_gop_ul[] = {
70 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
74 static const guint8 _identical_gop_ul[] = {
75 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
79 static const guint8 _max_gop_ul[] = {
80 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
84 static const guint8 _b_picture_count_ul[] = {
85 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
89 static const guint8 _bitrate_ul[] = {
90 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
94 static const guint8 _profile_and_level_ul[] = {
95 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
100 #define MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR \
101 (mxf_metadata_mpeg_video_descriptor_get_type())
102 #define MXF_METADATA_MPEG_VIDEO_DESCRIPTOR(obj) \
103 (G_TYPE_CHECK_INSTANCE_CAST((obj),MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR, MXFMetadataMPEGVideoDescriptor))
104 #define MXF_IS_METADATA_MPEG_VIDEO_DESCRIPTOR(obj) \
105 (G_TYPE_CHECK_INSTANCE_TYPE((obj),MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR))
106 typedef struct _MXFMetadataMPEGVideoDescriptor MXFMetadataMPEGVideoDescriptor;
107 typedef MXFMetadataClass MXFMetadataMPEGVideoDescriptorClass;
108 GType mxf_metadata_mpeg_video_descriptor_get_type (void);
110 struct _MXFMetadataMPEGVideoDescriptor
112 MXFMetadataCDCIPictureEssenceDescriptor parent;
114 gboolean single_sequence;
115 gboolean const_b_frames;
116 guint8 coded_content_type;
120 gboolean identical_gop;
123 guint16 b_picture_count;
125 guint8 profile_and_level;
128 G_DEFINE_TYPE (MXFMetadataMPEGVideoDescriptor,
129 mxf_metadata_mpeg_video_descriptor,
130 MXF_TYPE_METADATA_CDCI_PICTURE_ESSENCE_DESCRIPTOR);
133 mxf_metadata_mpeg_video_descriptor_handle_tag (MXFMetadataBase * metadata,
134 MXFPrimerPack * primer, guint16 tag, const guint8 * tag_data,
137 MXFMetadataMPEGVideoDescriptor *self =
138 MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (metadata);
140 MXFUL *tag_ul = NULL;
143 (MXFUL *) g_hash_table_lookup (primer->mappings,
144 GUINT_TO_POINTER (((guint) tag)))))
147 if (memcmp (tag_ul, &_single_sequence_ul, 16) == 0) {
150 self->single_sequence = GST_READ_UINT8 (tag_data);
151 GST_DEBUG (" single sequence = %s",
152 (self->single_sequence) ? "yes" : "no");
153 } else if (memcmp (tag_ul, &_constant_b_frames_ul, 16) == 0) {
156 self->const_b_frames = GST_READ_UINT8 (tag_data);
157 GST_DEBUG (" constant b frames = %s",
158 (self->single_sequence) ? "yes" : "no");
159 } else if (memcmp (tag_ul, &_coded_content_type_ul, 16) == 0) {
162 self->coded_content_type = GST_READ_UINT8 (tag_data);
163 GST_DEBUG (" coded content type = %u", self->coded_content_type);
164 } else if (memcmp (tag_ul, &_low_delay_ul, 16) == 0) {
167 self->low_delay = GST_READ_UINT8 (tag_data);
168 GST_DEBUG (" low delay = %s", (self->low_delay) ? "yes" : "no");
169 } else if (memcmp (tag_ul, &_closed_gop_ul, 16) == 0) {
172 self->closed_gop = GST_READ_UINT8 (tag_data);
173 GST_DEBUG (" closed gop = %s", (self->closed_gop) ? "yes" : "no");
174 } else if (memcmp (tag_ul, &_identical_gop_ul, 16) == 0) {
177 self->identical_gop = GST_READ_UINT8 (tag_data);
178 GST_DEBUG (" identical gop = %s", (self->identical_gop) ? "yes" : "no");
179 } else if (memcmp (tag_ul, &_max_gop_ul, 16) == 0) {
182 self->max_gop = GST_READ_UINT16_BE (tag_data);
183 GST_DEBUG (" max gop = %u", self->max_gop);
184 } else if (memcmp (tag_ul, &_b_picture_count_ul, 16) == 0) {
187 self->b_picture_count = GST_READ_UINT16_BE (tag_data);
188 GST_DEBUG (" b picture count = %u", self->b_picture_count);
189 } else if (memcmp (tag_ul, &_bitrate_ul, 16) == 0) {
192 self->bitrate = GST_READ_UINT32_BE (tag_data);
193 GST_DEBUG (" bitrate = %u", self->bitrate);
194 } else if (memcmp (tag_ul, &_profile_and_level_ul, 16) == 0) {
197 self->profile_and_level = GST_READ_UINT8 (tag_data);
198 GST_DEBUG (" profile & level = %u", self->profile_and_level);
201 MXF_METADATA_BASE_CLASS
202 (mxf_metadata_mpeg_video_descriptor_parent_class)->handle_tag (metadata,
203 primer, tag, tag_data, tag_size);
210 GST_ERROR ("Invalid MPEG video descriptor local tag 0x%04x of size %u", tag,
216 static GstStructure *
217 mxf_metadata_mpeg_video_descriptor_to_structure (MXFMetadataBase * m)
220 MXF_METADATA_BASE_CLASS
221 (mxf_metadata_mpeg_video_descriptor_parent_class)->to_structure (m);
222 MXFMetadataMPEGVideoDescriptor *self = MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (m);
224 gst_structure_id_set (ret, MXF_QUARK (SINGLE_SEQUENCE), G_TYPE_BOOLEAN,
225 self->single_sequence, MXF_QUARK (CONST_B_FRAMES), G_TYPE_BOOLEAN,
226 self->const_b_frames, MXF_QUARK (CODED_CONTENT_TYPE), G_TYPE_UCHAR,
227 self->coded_content_type, MXF_QUARK (LOW_DELAY), G_TYPE_BOOLEAN,
228 self->low_delay, MXF_QUARK (CLOSED_GOP), G_TYPE_BOOLEAN, self->closed_gop,
229 MXF_QUARK (IDENTICAL_GOP), G_TYPE_BOOLEAN, self->identical_gop,
230 MXF_QUARK (PROFILE_AND_LEVEL), G_TYPE_UCHAR, self->profile_and_level,
234 gst_structure_id_set (ret, MXF_QUARK (MAX_GOP), G_TYPE_UINT, self->max_gop,
237 if (self->b_picture_count)
238 gst_structure_id_set (ret, MXF_QUARK (B_PICTURE_COUNT), G_TYPE_UINT,
239 self->b_picture_count, NULL);
242 gst_structure_id_set (ret, MXF_QUARK (BITRATE), G_TYPE_UINT, self->bitrate,
249 mxf_metadata_mpeg_video_descriptor_write_tags (MXFMetadataBase * m,
250 MXFPrimerPack * primer)
252 MXFMetadataMPEGVideoDescriptor *self = MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (m);
254 MXF_METADATA_BASE_CLASS
255 (mxf_metadata_mpeg_video_descriptor_parent_class)->write_tags (m, primer);
258 if (self->single_sequence != -1) {
259 t = g_slice_new0 (MXFLocalTag);
260 memcpy (&t->ul, &_single_sequence_ul, 16);
262 t->data = g_slice_alloc (t->size);
264 GST_WRITE_UINT8 (t->data, (self->single_sequence) ? 1 : 0);
265 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
266 ret = g_list_prepend (ret, t);
269 if (self->const_b_frames) {
270 t = g_slice_new0 (MXFLocalTag);
271 memcpy (&t->ul, &_constant_b_frames_ul, 16);
273 t->data = g_slice_alloc (t->size);
275 GST_WRITE_UINT8 (t->data, (self->const_b_frames) ? 1 : 0);
276 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
277 ret = g_list_prepend (ret, t);
280 if (self->coded_content_type) {
281 t = g_slice_new0 (MXFLocalTag);
282 memcpy (&t->ul, &_coded_content_type_ul, 16);
284 t->data = g_slice_alloc (t->size);
286 GST_WRITE_UINT8 (t->data, self->coded_content_type);
287 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
288 ret = g_list_prepend (ret, t);
291 if (self->low_delay) {
292 t = g_slice_new0 (MXFLocalTag);
293 memcpy (&t->ul, &_low_delay_ul, 16);
295 t->data = g_slice_alloc (t->size);
297 GST_WRITE_UINT8 (t->data, (self->low_delay) ? 1 : 0);
298 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
299 ret = g_list_prepend (ret, t);
302 if (self->closed_gop) {
303 t = g_slice_new0 (MXFLocalTag);
304 memcpy (&t->ul, &_closed_gop_ul, 16);
306 t->data = g_slice_alloc (t->size);
308 GST_WRITE_UINT8 (t->data, (self->closed_gop) ? 1 : 0);
309 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
310 ret = g_list_prepend (ret, t);
313 if (self->identical_gop) {
314 t = g_slice_new0 (MXFLocalTag);
315 memcpy (&t->ul, &_identical_gop_ul, 16);
317 t->data = g_slice_alloc (t->size);
319 GST_WRITE_UINT8 (t->data, (self->identical_gop) ? 1 : 0);
320 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
321 ret = g_list_prepend (ret, t);
325 t = g_slice_new0 (MXFLocalTag);
326 memcpy (&t->ul, &_identical_gop_ul, 16);
328 t->data = g_slice_alloc (t->size);
330 GST_WRITE_UINT16_BE (t->data, self->max_gop);
331 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
332 ret = g_list_prepend (ret, t);
335 if (self->b_picture_count) {
336 t = g_slice_new0 (MXFLocalTag);
337 memcpy (&t->ul, &_b_picture_count_ul, 16);
339 t->data = g_slice_alloc (t->size);
341 GST_WRITE_UINT16_BE (t->data, self->b_picture_count);
342 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
343 ret = g_list_prepend (ret, t);
347 t = g_slice_new0 (MXFLocalTag);
348 memcpy (&t->ul, &_bitrate_ul, 16);
350 t->data = g_slice_alloc (t->size);
352 GST_WRITE_UINT32_BE (t->data, self->bitrate);
353 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
354 ret = g_list_prepend (ret, t);
357 if (self->profile_and_level) {
358 t = g_slice_new0 (MXFLocalTag);
359 memcpy (&t->ul, &_profile_and_level_ul, 16);
361 t->data = g_slice_alloc (t->size);
363 GST_WRITE_UINT8 (t->data, self->profile_and_level);
364 mxf_primer_pack_add_mapping (primer, 0, &t->ul);
365 ret = g_list_prepend (ret, t);
372 mxf_metadata_mpeg_video_descriptor_init (MXFMetadataMPEGVideoDescriptor * self)
374 self->single_sequence = -1;
378 mxf_metadata_mpeg_video_descriptor_class_init
379 (MXFMetadataMPEGVideoDescriptorClass * klass)
381 MXFMetadataBaseClass *metadata_base_class = (MXFMetadataBaseClass *) klass;
382 MXFMetadataClass *metadata_class = (MXFMetadataClass *) klass;
384 metadata_base_class->handle_tag =
385 mxf_metadata_mpeg_video_descriptor_handle_tag;
386 metadata_base_class->name_quark = MXF_QUARK (MPEG_VIDEO_DESCRIPTOR);
387 metadata_base_class->to_structure =
388 mxf_metadata_mpeg_video_descriptor_to_structure;
389 metadata_base_class->write_tags =
390 mxf_metadata_mpeg_video_descriptor_write_tags;
392 metadata_class->type = 0x0151;
397 MXF_MPEG_ESSENCE_TYPE_OTHER = 0,
398 MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2,
399 MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4,
400 MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC
401 } MXFMPEGEssenceType;
404 mxf_is_mpeg_essence_track (const MXFMetadataTimelineTrack * track)
408 g_return_val_if_fail (track != NULL, FALSE);
410 if (track->parent.descriptor == NULL)
413 for (i = 0; i < track->parent.n_descriptor; i++) {
414 MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
420 key = &d->essence_container;
422 if (mxf_is_generic_container_essence_container_label (key) &&
423 key->u[12] == 0x02 &&
424 (key->u[13] == 0x04 ||
425 key->u[13] == 0x07 || key->u[13] == 0x08 || key->u[13] == 0x09))
432 /* See ISO/IEC 13818-2 for MPEG ES format */
434 mxf_mpeg_is_mpeg2_keyframe (GstBuffer * buffer)
436 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
439 while (gst_byte_reader_get_remaining (&reader) > 3) {
440 if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
443 /* Found sync code */
444 gst_byte_reader_skip (&reader, 3);
446 if (!gst_byte_reader_get_uint8 (&reader, &type))
449 /* GOP packets are meant as random access markers */
452 } else if (type == 0x00) {
455 if (!gst_byte_reader_skip (&reader, 5))
458 if (!gst_byte_reader_get_uint8 (&reader, &pic_type))
461 pic_type = (pic_type >> 3) & 0x07;
462 if (pic_type == 0x01) {
469 gst_byte_reader_skip (&reader, 1);
477 mxf_mpeg_is_mpeg4_keyframe (GstBuffer * buffer)
479 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
482 while (gst_byte_reader_get_remaining (&reader) > 3) {
483 if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
486 /* Found sync code */
487 gst_byte_reader_skip (&reader, 3);
489 if (!gst_byte_reader_get_uint8 (&reader, &type))
495 if (!gst_byte_reader_get_uint8 (&reader, &pic_type))
498 pic_type = (pic_type >> 6) & 0x03;
506 gst_byte_reader_skip (&reader, 1);
514 mxf_mpeg_video_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
515 GstCaps * caps, MXFMetadataTimelineTrack * track,
516 gpointer mapping_data, GstBuffer ** outbuf)
518 MXFMPEGEssenceType type = *((MXFMPEGEssenceType *) mapping_data);
523 if (key->u[12] != 0x15 || (key->u[14] != 0x05 && key->u[14] != 0x06
524 && key->u[14] != 0x07)) {
525 GST_ERROR ("Invalid MPEG video essence element");
526 return GST_FLOW_ERROR;
530 case MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2:
531 if (mxf_mpeg_is_mpeg2_keyframe (buffer))
532 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
534 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
536 case MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4:
537 if (mxf_mpeg_is_mpeg4_keyframe (buffer))
538 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
540 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
551 mxf_mpeg_audio_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
552 GstCaps * caps, MXFMetadataTimelineTrack * track,
553 gpointer mapping_data, GstBuffer ** outbuf)
558 if (key->u[12] != 0x16 || (key->u[14] != 0x05 && key->u[14] != 0x06
559 && key->u[14] != 0x07)) {
560 GST_ERROR ("Invalid MPEG audio essence element");
561 return GST_FLOW_ERROR;
567 /* Private uid used by SONY C0023S01.mxf,
568 * taken from the ffmpeg mxf demuxer */
569 static const guint8 sony_mpeg4_extradata[] = {
570 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0e, 0x06, 0x06, 0x02, 0x02,
576 static const MXFUL sound_essence_compression_ac3 = { {
577 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
578 0x03, 0x02, 0x01, 0x00}
581 static const MXFUL sound_essence_compression_mpeg1_layer1 = { {
582 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
583 0x03, 0x02, 0x04, 0x00}
586 static const MXFUL sound_essence_compression_mpeg1_layer12 = { {
587 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
588 0x03, 0x02, 0x05, 0x00}
591 static const MXFUL sound_essence_compression_mpeg1_layer2 = { {
592 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x08, 0x04, 0x02, 0x02, 0x02,
593 0x03, 0x02, 0x05, 0x01}
596 static const MXFUL sound_essence_compression_mpeg2_layer1 = { {
597 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
598 0x03, 0x02, 0x06, 0x00}
601 static const MXFUL sound_essence_compression_dts = { {
602 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
603 0x03, 0x02, 0x1c, 0x00}
606 static const MXFUL sound_essence_compression_aac = { {
607 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x03, 0x04, 0x02, 0x02, 0x02,
608 0x03, 0x03, 0x01, 0x00}
612 mxf_mpeg_es_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
613 MXFEssenceElementHandleFunc * handler, gpointer * mapping_data,
614 MXFMetadataGenericPictureEssenceDescriptor * p,
615 MXFMetadataGenericSoundEssenceDescriptor * s)
617 GstCaps *caps = NULL;
618 const gchar *codec_name = NULL;
619 MXFMPEGEssenceType t, *mdata;
621 *mapping_data = g_malloc (sizeof (MXFMPEGEssenceType));
622 mdata = (MXFMPEGEssenceType *) * mapping_data;
626 if (mxf_ul_is_zero (&p->picture_essence_coding)) {
627 GST_WARNING ("No picture essence coding defined, assuming MPEG2");
629 gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 2,
630 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
631 codec_name = "MPEG-2 Video";
632 t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
633 memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
634 } else if (p->picture_essence_coding.u[0] != 0x06
635 || p->picture_essence_coding.u[1] != 0x0e
636 || p->picture_essence_coding.u[2] != 0x2b
637 || p->picture_essence_coding.u[3] != 0x34
638 || p->picture_essence_coding.u[4] != 0x04
639 || p->picture_essence_coding.u[5] != 0x01
640 || p->picture_essence_coding.u[6] != 0x01
641 || p->picture_essence_coding.u[8] != 0x04
642 || p->picture_essence_coding.u[9] != 0x01
643 || p->picture_essence_coding.u[10] != 0x02
644 || p->picture_essence_coding.u[11] != 0x02
645 || p->picture_essence_coding.u[12] != 0x01) {
646 GST_ERROR ("No MPEG picture essence coding");
648 } else if (p->picture_essence_coding.u[13] >= 0x01 &&
649 p->picture_essence_coding.u[13] <= 0x08) {
650 caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 2,
651 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
652 codec_name = "MPEG-2 Video";
653 t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
654 memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
655 } else if (p->picture_essence_coding.u[13] == 0x10) {
656 caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 1,
657 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
658 codec_name = "MPEG-1 Video";
659 t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
660 memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
661 } else if (p->picture_essence_coding.u[13] == 0x20) {
662 MXFLocalTag *local_tag =
663 (((MXFMetadataBase *) p)->other_tags) ?
664 g_hash_table_lookup (((MXFMetadataBase *)
665 p)->other_tags, &sony_mpeg4_extradata) : NULL;
667 caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 4,
668 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
671 GstBuffer *codec_data = NULL;
672 codec_data = gst_buffer_new_and_alloc (local_tag->size);
673 memcpy (GST_BUFFER_DATA (codec_data), local_tag->data, local_tag->size);
674 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, codec_data,
676 gst_buffer_unref (codec_data);
678 codec_name = "MPEG-4 Video";
679 t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4;
680 memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
681 } else if ((p->picture_essence_coding.u[13] >> 4) == 0x03) {
684 /* TODO: What about codec_data for AVC1 streams? */
685 caps = gst_caps_new_simple ("video/x-h264", NULL);
686 codec_name = "h.264 Video";
687 t = MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC;
688 memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
690 GST_ERROR ("Unsupported MPEG picture essence coding 0x%02x",
691 p->picture_essence_coding.u[13]);
695 *handler = mxf_mpeg_video_handle_essence_element;
697 if (mxf_ul_is_zero (&s->sound_essence_compression)) {
698 GST_WARNING ("Zero sound essence compression, assuming MPEG1 audio");
700 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
702 codec_name = "MPEG-1 Audio";
703 } else if (mxf_ul_is_equal (&s->sound_essence_compression,
704 &sound_essence_compression_ac3)) {
705 caps = gst_caps_new_simple ("audio/x-ac3", NULL);
706 codec_name = "AC3 Audio";
707 } else if (mxf_ul_is_equal (&s->sound_essence_compression,
708 &sound_essence_compression_mpeg1_layer1)) {
710 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
711 "layer", G_TYPE_INT, 1, NULL);
712 codec_name = "MPEG-1 Layer 1 Audio";
713 } else if (mxf_ul_is_equal (&s->sound_essence_compression,
714 &sound_essence_compression_mpeg1_layer12)) {
716 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
718 codec_name = "MPEG-1 Audio";
719 } else if (mxf_ul_is_equal (&s->sound_essence_compression,
720 &sound_essence_compression_mpeg1_layer2)) {
722 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
723 "layer", G_TYPE_INT, 2, NULL);
724 codec_name = "MPEG-1 Layer 2 Audio";
725 } else if (mxf_ul_is_equal (&s->sound_essence_compression,
726 &sound_essence_compression_mpeg2_layer1)) {
728 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
729 "layer", G_TYPE_INT, 1, "mpegaudioversion", G_TYPE_INT, 2, NULL);
730 codec_name = "MPEG-2 Layer 1 Audio";
731 } else if (mxf_ul_is_equal (&s->sound_essence_compression,
732 &sound_essence_compression_dts)) {
733 caps = gst_caps_new_simple ("audio/x-dts", NULL);
734 codec_name = "Dolby DTS Audio";
735 } else if (mxf_ul_is_equal (&s->sound_essence_compression,
736 &sound_essence_compression_aac)) {
737 caps = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
739 codec_name = "MPEG-2 AAC Audio";
743 mxf_metadata_generic_sound_essence_descriptor_set_caps (s, caps);
744 *handler = mxf_mpeg_audio_handle_essence_element;
750 *tags = gst_tag_list_new ();
752 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
755 if (p && MXF_IS_METADATA_MPEG_VIDEO_DESCRIPTOR (p)
756 && MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (p)->bitrate) {
757 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_BITRATE,
758 MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (p)->bitrate, NULL);
766 mxf_mpeg_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
767 MXFEssenceElementHandleFunc * handler, gpointer * mapping_data)
769 MXFMetadataFileDescriptor *f = NULL;
770 MXFMetadataGenericPictureEssenceDescriptor *p = NULL;
771 MXFMetadataGenericSoundEssenceDescriptor *s = NULL;
773 GstCaps *caps = NULL;
775 g_return_val_if_fail (track != NULL, NULL);
777 if (track->parent.descriptor == NULL) {
778 GST_ERROR ("No descriptor found for this track");
782 for (i = 0; i < track->parent.n_descriptor; i++) {
783 if (!track->parent.descriptor[i])
786 if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->parent.
788 f = track->parent.descriptor[i];
789 p = (MXFMetadataGenericPictureEssenceDescriptor *) track->
790 parent.descriptor[i];
792 } else if (MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->parent.
794 f = track->parent.descriptor[i];
795 s = (MXFMetadataGenericSoundEssenceDescriptor *) track->
796 parent.descriptor[i];
802 GST_ERROR ("No descriptor found for this track");
807 if (f->essence_container.u[13] == 0x04) {
808 GST_DEBUG ("Found MPEG ES stream");
810 caps = mxf_mpeg_es_create_caps (track, tags, handler, mapping_data, p, s);
811 } else if (f->essence_container.u[13] == 0x07) {
812 GST_ERROR ("MPEG PES streams not supported yet");
814 } else if (f->essence_container.u[13] == 0x08) {
815 /* FIXME: get mpeg version somehow */
816 GST_DEBUG ("Found MPEG PS stream");
817 caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 1,
818 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
821 *tags = gst_tag_list_new ();
822 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
824 } else if (f->essence_container.u[13] == 0x09) {
825 GST_DEBUG ("Found MPEG TS stream");
826 caps = gst_caps_new_simple ("video/mpegts", NULL);
829 *tags = gst_tag_list_new ();
830 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
832 } else if (f->essence_container.u[13] == 0x0f) {
833 GST_DEBUG ("Found h264 NAL unit stream");
835 /* TODO: What about codec_data? */
836 caps = gst_caps_new_simple ("video/x-h264", NULL);
839 *tags = gst_tag_list_new ();
840 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
841 "h.264 Video", NULL);
842 } else if (f->essence_container.u[13] == 0x10) {
843 GST_DEBUG ("Found h264 byte stream stream");
845 caps = gst_caps_new_simple ("video/x-h264", NULL);
848 *tags = gst_tag_list_new ();
849 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
850 "h.264 Video", NULL);
854 mxf_metadata_generic_picture_essence_descriptor_set_caps (p, caps);
859 static const MXFEssenceElementHandler mxf_mpeg_essence_element_handler = {
860 mxf_is_mpeg_essence_track,
868 } MPEGAudioMappingData;
871 mxf_mpeg_audio_write_func (GstBuffer * buffer, GstCaps * caps,
872 gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
879 static const guint8 mpeg_essence_container_ul[] = {
880 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x02,
881 0x0d, 0x01, 0x03, 0x01, 0x02, 0x04, 0x01, 0x01
884 static MXFMetadataFileDescriptor *
885 mxf_mpeg_audio_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
886 MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
888 MXFMetadataGenericSoundEssenceDescriptor *ret;
890 MPEGAudioMappingData *md = g_new0 (MPEGAudioMappingData, 1);
896 ret = (MXFMetadataGenericSoundEssenceDescriptor *)
897 gst_mini_object_new (MXF_TYPE_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR);
899 s = gst_caps_get_structure (caps, 0);
900 if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
903 if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
904 GST_ERROR ("Invalid caps %" GST_PTR_FORMAT, caps);
905 gst_mini_object_unref (GST_MINI_OBJECT_CAST (ret));
909 if (mpegversion == 1) {
911 gint mpegaudioversion = 0;
913 gst_structure_get_int (s, "layer", &layer);
914 gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion);
916 if (mpegaudioversion == 1 && layer == 1)
917 memcpy (&ret->sound_essence_compression,
918 &sound_essence_compression_mpeg1_layer1, 16);
919 else if (mpegaudioversion == 1 && layer == 2)
920 memcpy (&ret->sound_essence_compression,
921 &sound_essence_compression_mpeg1_layer12, 16);
922 else if (mpegaudioversion == 2 && layer == 1)
923 memcpy (&ret->sound_essence_compression,
924 &sound_essence_compression_mpeg2_layer1, 16);
928 else if (layer == 2 || mpegversion == 1)
931 md->spf = 576; /* MPEG-2 or 2.5 */
933 /* Otherwise all 0x00, must be some kind of mpeg1 audio */
934 } else if (mpegversion == 2) {
935 memcpy (&ret->sound_essence_compression, &sound_essence_compression_aac,
937 md->spf = 1024; /* FIXME: is this correct? */
939 } else if (strcmp (gst_structure_get_name (s), "audio/x-ac3") == 0) {
940 memcpy (&ret->sound_essence_compression, &sound_essence_compression_ac3,
942 md->spf = 256; /* FIXME: is this correct? */
944 g_assert_not_reached ();
947 if (!gst_structure_get_int (s, "rate", &rate)) {
948 GST_ERROR ("Invalid rate");
949 gst_mini_object_unref (GST_MINI_OBJECT_CAST (ret));
954 memcpy (&ret->parent.essence_container, &mpeg_essence_container_ul, 16);
956 if (!mxf_metadata_generic_sound_essence_descriptor_from_caps (ret, caps)) {
957 gst_mini_object_unref (GST_MINI_OBJECT_CAST (ret));
961 *handler = mxf_mpeg_audio_write_func;
963 return (MXFMetadataFileDescriptor *) ret;
967 mxf_mpeg_audio_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
968 gpointer mapping_data, GstBuffer * buf)
974 mxf_mpeg_audio_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
975 gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
976 MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
978 MPEGAudioMappingData *md = mapping_data;
980 edit_rate->n = md->rate;
981 edit_rate->d = md->spf;
985 mxf_mpeg_audio_get_track_number_template (MXFMetadataFileDescriptor * a,
986 GstCaps * caps, gpointer mapping_data)
988 return (0x16 << 24) | (0x05 << 8);
991 static MXFEssenceElementWriter mxf_mpeg_audio_essence_element_writer = {
992 mxf_mpeg_audio_get_descriptor,
993 mxf_mpeg_audio_update_descriptor,
994 mxf_mpeg_audio_get_edit_rate,
995 mxf_mpeg_audio_get_track_number_template,
1000 #define MPEG_AUDIO_CAPS \
1002 "mpegversion = (int) 1, " \
1003 "layer = (int) [ 1, 3 ], " \
1004 "rate = (int) [ 8000, 48000 ], " \
1005 "channels = (int) [ 1, 2 ], " \
1006 "parsed = (boolean) TRUE; " \
1008 "rate = (int) [ 4000, 96000 ], " \
1009 "channels = (int) [ 1, 6 ]; " \
1011 "mpegversion = (int) 2, " \
1012 "rate = (int) [ 8000, 96000 ], " \
1013 "channels = (int) [ 1, 8 ]"
1015 /* See ISO/IEC 13818-2 for MPEG ES format */
1017 mxf_mpeg_is_mpeg2_frame (GstBuffer * buffer)
1019 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
1022 while (gst_byte_reader_get_remaining (&reader) > 3) {
1023 if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
1026 /* Found sync code */
1027 gst_byte_reader_skip (&reader, 3);
1029 if (!gst_byte_reader_get_uint8 (&reader, &type))
1037 gst_byte_reader_skip (&reader, 1);
1045 mxf_mpeg_is_mpeg4_frame (GstBuffer * buffer)
1047 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
1050 while (gst_byte_reader_get_remaining (&reader) > 3) {
1051 if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
1054 /* Found sync code */
1055 gst_byte_reader_skip (&reader, 3);
1057 if (!gst_byte_reader_get_uint8 (&reader, &type))
1065 gst_byte_reader_skip (&reader, 1);
1072 static GstFlowReturn
1073 mxf_mpeg_video_write_func (GstBuffer * buffer, GstCaps * caps,
1074 gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
1077 MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_OTHER;
1080 type = *((MXFMPEGEssenceType *) mapping_data);
1082 if (type == MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2) {
1083 if (buffer && !mxf_mpeg_is_mpeg2_frame (buffer)) {
1084 gst_adapter_push (adapter, buffer);
1087 } else if (buffer || gst_adapter_available (adapter)) {
1088 guint av = gst_adapter_available (adapter);
1092 ret = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buffer) + av);
1094 ret = gst_buffer_new_and_alloc (av);
1097 GstBuffer *tmp = gst_adapter_take_buffer (adapter, av);
1098 memcpy (GST_BUFFER_DATA (ret), GST_BUFFER_DATA (tmp), av);
1099 gst_buffer_unref (tmp);
1103 memcpy (GST_BUFFER_DATA (ret) + av, GST_BUFFER_DATA (buffer),
1104 GST_BUFFER_SIZE (buffer));
1105 gst_buffer_unref (buffer);
1110 } else if (type == MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4) {
1111 if (buffer && !mxf_mpeg_is_mpeg4_frame (buffer)) {
1112 gst_adapter_push (adapter, buffer);
1115 } else if (buffer || gst_adapter_available (adapter)) {
1116 guint av = gst_adapter_available (adapter);
1120 ret = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buffer) + av);
1122 ret = gst_buffer_new_and_alloc (av);
1125 GstBuffer *tmp = gst_adapter_take_buffer (adapter, av);
1126 memcpy (GST_BUFFER_DATA (ret), GST_BUFFER_DATA (tmp), av);
1127 gst_buffer_unref (tmp);
1131 memcpy (GST_BUFFER_DATA (ret) + av, GST_BUFFER_DATA (buffer),
1132 GST_BUFFER_SIZE (buffer));
1133 gst_buffer_unref (buffer);
1144 static const guint8 mpeg_video_picture_essence_compression_ul[] = {
1145 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x00,
1146 0x04, 0x01, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00
1149 static MXFMetadataFileDescriptor *
1150 mxf_mpeg_video_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
1151 MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
1153 MXFMetadataMPEGVideoDescriptor *ret;
1156 ret = (MXFMetadataMPEGVideoDescriptor *)
1157 gst_mini_object_new (MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR);
1159 s = gst_caps_get_structure (caps, 0);
1161 memcpy (&ret->parent.parent.parent.essence_container,
1162 &mpeg_essence_container_ul, 16);
1163 memcpy (&ret->parent.parent.picture_essence_coding,
1164 &mpeg_video_picture_essence_compression_ul, 16);
1165 if (strcmp (gst_structure_get_name (s), "video/mpeg") == 0) {
1168 if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
1169 GST_ERROR ("Invalid caps %" GST_PTR_FORMAT, caps);
1170 gst_mini_object_unref (GST_MINI_OBJECT_CAST (ret));
1174 if (mpegversion == 1) {
1175 MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
1177 *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1178 memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1179 ret->parent.parent.picture_essence_coding.u[13] = 0x10;
1180 } else if (mpegversion == 2) {
1181 MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
1183 *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1184 memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1185 ret->parent.parent.picture_essence_coding.u[13] = 0x01;
1188 const GstBuffer *codec_data;
1189 MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4;
1191 *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1192 memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1194 ret->parent.parent.picture_essence_coding.u[13] = 0x20;
1195 if ((v = gst_structure_get_value (s, "codec_data"))) {
1196 MXFLocalTag *t = g_slice_new0 (MXFLocalTag);
1197 codec_data = gst_value_get_buffer (v);
1198 t->size = GST_BUFFER_SIZE (codec_data);
1199 t->data = g_memdup (GST_BUFFER_DATA (codec_data), t->size);
1200 memcpy (&t->ul, &sony_mpeg4_extradata, 16);
1201 mxf_local_tag_insert (t, &MXF_METADATA_BASE (ret)->other_tags);
1204 } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
1205 MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC;
1207 *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1208 memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1209 ret->parent.parent.picture_essence_coding.u[13] = 0x30;
1211 g_assert_not_reached ();
1215 if (!mxf_metadata_generic_picture_essence_descriptor_from_caps (&ret->
1216 parent.parent, caps)) {
1217 gst_mini_object_unref (GST_MINI_OBJECT_CAST (ret));
1221 *handler = mxf_mpeg_video_write_func;
1223 return (MXFMetadataFileDescriptor *) ret;
1227 mxf_mpeg_video_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
1228 gpointer mapping_data, GstBuffer * buf)
1234 mxf_mpeg_video_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
1235 gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
1236 MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
1238 (*edit_rate).n = a->sample_rate.n;
1239 (*edit_rate).d = a->sample_rate.d;
1243 mxf_mpeg_video_get_track_number_template (MXFMetadataFileDescriptor * a,
1244 GstCaps * caps, gpointer mapping_data)
1246 return (0x15 << 24) | (0x05 << 8);
1249 static MXFEssenceElementWriter mxf_mpeg_video_essence_element_writer = {
1250 mxf_mpeg_video_get_descriptor,
1251 mxf_mpeg_video_update_descriptor,
1252 mxf_mpeg_video_get_edit_rate,
1253 mxf_mpeg_video_get_track_number_template,
1258 #define MPEG_VIDEO_CAPS \
1260 "mpegversion = (int) { 1, 2, 4 }, " \
1261 "systemstream = (boolean) FALSE, " \
1262 "width = " GST_VIDEO_SIZE_RANGE ", " \
1263 "height = " GST_VIDEO_SIZE_RANGE ", " \
1264 "framerate = " GST_VIDEO_FPS_RANGE "; " \
1266 "width = " GST_VIDEO_SIZE_RANGE ", " \
1267 "height = " GST_VIDEO_SIZE_RANGE ", " \
1268 "framerate = " GST_VIDEO_FPS_RANGE
1271 mxf_mpeg_init (void)
1273 mxf_metadata_register (MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR);
1274 mxf_essence_element_handler_register (&mxf_mpeg_essence_element_handler);
1276 mxf_mpeg_audio_essence_element_writer.pad_template =
1277 gst_pad_template_new ("mpeg_audio_sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
1278 gst_caps_from_string (MPEG_AUDIO_CAPS));
1279 memcpy (&mxf_mpeg_audio_essence_element_writer.data_definition,
1280 mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_SOUND_ESSENCE), 16);
1281 mxf_essence_element_writer_register (&mxf_mpeg_audio_essence_element_writer);
1283 mxf_mpeg_video_essence_element_writer.pad_template =
1284 gst_pad_template_new ("mpeg_video_sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
1285 gst_caps_from_string (MPEG_VIDEO_CAPS));
1286 memcpy (&mxf_mpeg_video_essence_element_writer.data_definition,
1287 mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_PICTURE_ESSENCE),
1289 mxf_essence_element_writer_register (&mxf_mpeg_video_essence_element_writer);