codecparser: mpeg4 type error
[profile/ivi/gst-plugins-bad.git] / gst / mxf / mxfmpeg.c
1 /* GStreamer
2  * Copyright (C) 2008-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /* Implementation of SMPTE 381M - Mapping MPEG streams into the MXF
21  * Generic Container
22  */
23
24 /* TODO:
25  * - Handle PES streams
26  * - Fix TS/PS demuxers to forward timestamps
27  * - h264 support (see SMPTE RP2008)
28  * - AAC support
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <gst/gst.h>
36 #include <gst/video/video.h>
37 #include <string.h>
38
39 #include "mxfmpeg.h"
40 #include "mxfquark.h"
41 #include "mxfessence.h"
42
43 #include <gst/base/gstbytereader.h>
44
45 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
46 #define GST_CAT_DEFAULT mxf_debug
47
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,
51   0x02, 0x00, 0x00
52 };
53
54 static const guint8 _constant_b_frames_ul[] = {
55   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
56   0x03, 0x00, 0x00
57 };
58
59 static const guint8 _coded_content_type_ul[] = {
60   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
61   0x04, 0x00, 0x00
62 };
63
64 static const guint8 _low_delay_ul[] = {
65   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
66   0x05, 0x00, 0x00
67 };
68
69 static const guint8 _closed_gop_ul[] = {
70   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
71   0x06, 0x00, 0x00
72 };
73
74 static const guint8 _identical_gop_ul[] = {
75   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
76   0x07, 0x00, 0x00
77 };
78
79 static const guint8 _max_gop_ul[] = {
80   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
81   0x08, 0x00, 0x00
82 };
83
84 static const guint8 _b_picture_count_ul[] = {
85   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
86   0x09, 0x00, 0x00
87 };
88
89 static const guint8 _bitrate_ul[] = {
90   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
91   0x0b, 0x00, 0x00
92 };
93
94 static const guint8 _profile_and_level_ul[] = {
95   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
96   0x0a, 0x00, 0x00
97 };
98
99 /* SMPTE 381M 8.1 */
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);
109
110 struct _MXFMetadataMPEGVideoDescriptor
111 {
112   MXFMetadataCDCIPictureEssenceDescriptor parent;
113
114   gboolean single_sequence;
115   gboolean const_b_frames;
116   guint8 coded_content_type;
117   gboolean low_delay;
118
119   gboolean closed_gop;
120   gboolean identical_gop;
121   guint16 max_gop;
122
123   guint16 b_picture_count;
124   guint32 bitrate;
125   guint8 profile_and_level;
126 };
127
128 G_DEFINE_TYPE (MXFMetadataMPEGVideoDescriptor,
129     mxf_metadata_mpeg_video_descriptor,
130     MXF_TYPE_METADATA_CDCI_PICTURE_ESSENCE_DESCRIPTOR);
131
132 static gboolean
133 mxf_metadata_mpeg_video_descriptor_handle_tag (MXFMetadataBase * metadata,
134     MXFPrimerPack * primer, guint16 tag, const guint8 * tag_data,
135     guint tag_size)
136 {
137   MXFMetadataMPEGVideoDescriptor *self =
138       MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (metadata);
139   gboolean ret = TRUE;
140   MXFUL *tag_ul = NULL;
141
142   if (!(tag_ul =
143           (MXFUL *) g_hash_table_lookup (primer->mappings,
144               GUINT_TO_POINTER (((guint) tag)))))
145     return FALSE;
146
147   if (memcmp (tag_ul, &_single_sequence_ul, 16) == 0) {
148     if (tag_size != 1)
149       goto error;
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) {
154     if (tag_size != 1)
155       goto error;
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) {
160     if (tag_size != 1)
161       goto error;
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) {
165     if (tag_size != 1)
166       goto error;
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) {
170     if (tag_size != 1)
171       goto error;
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) {
175     if (tag_size != 1)
176       goto error;
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) {
180     if (tag_size != 2)
181       goto error;
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) {
185     if (tag_size != 2)
186       goto error;
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) {
190     if (tag_size != 4)
191       goto error;
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) {
195     if (tag_size != 1)
196       goto error;
197     self->profile_and_level = GST_READ_UINT8 (tag_data);
198     GST_DEBUG ("  profile & level = %u", self->profile_and_level);
199   } else {
200     ret =
201         MXF_METADATA_BASE_CLASS
202         (mxf_metadata_mpeg_video_descriptor_parent_class)->handle_tag (metadata,
203         primer, tag, tag_data, tag_size);
204   }
205
206   return ret;
207
208 error:
209
210   GST_ERROR ("Invalid MPEG video descriptor local tag 0x%04x of size %u", tag,
211       tag_size);
212
213   return FALSE;
214 }
215
216 static GstStructure *
217 mxf_metadata_mpeg_video_descriptor_to_structure (MXFMetadataBase * m)
218 {
219   GstStructure *ret =
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);
223
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,
231       NULL);
232
233   if (self->max_gop)
234     gst_structure_id_set (ret, MXF_QUARK (MAX_GOP), G_TYPE_UINT, self->max_gop,
235         NULL);
236
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);
240
241   if (self->bitrate)
242     gst_structure_id_set (ret, MXF_QUARK (BITRATE), G_TYPE_UINT, self->bitrate,
243         NULL);
244
245   return ret;
246 }
247
248 static GList *
249 mxf_metadata_mpeg_video_descriptor_write_tags (MXFMetadataBase * m,
250     MXFPrimerPack * primer)
251 {
252   MXFMetadataMPEGVideoDescriptor *self = MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (m);
253   GList *ret =
254       MXF_METADATA_BASE_CLASS
255       (mxf_metadata_mpeg_video_descriptor_parent_class)->write_tags (m, primer);
256   MXFLocalTag *t;
257
258   if (self->single_sequence != -1) {
259     t = g_slice_new0 (MXFLocalTag);
260     memcpy (&t->ul, &_single_sequence_ul, 16);
261     t->size = 1;
262     t->data = g_slice_alloc (t->size);
263     t->g_slice = TRUE;
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);
267   }
268
269   if (self->const_b_frames) {
270     t = g_slice_new0 (MXFLocalTag);
271     memcpy (&t->ul, &_constant_b_frames_ul, 16);
272     t->size = 1;
273     t->data = g_slice_alloc (t->size);
274     t->g_slice = TRUE;
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);
278   }
279
280   if (self->coded_content_type) {
281     t = g_slice_new0 (MXFLocalTag);
282     memcpy (&t->ul, &_coded_content_type_ul, 16);
283     t->size = 1;
284     t->data = g_slice_alloc (t->size);
285     t->g_slice = TRUE;
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);
289   }
290
291   if (self->low_delay) {
292     t = g_slice_new0 (MXFLocalTag);
293     memcpy (&t->ul, &_low_delay_ul, 16);
294     t->size = 1;
295     t->data = g_slice_alloc (t->size);
296     t->g_slice = TRUE;
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);
300   }
301
302   if (self->closed_gop) {
303     t = g_slice_new0 (MXFLocalTag);
304     memcpy (&t->ul, &_closed_gop_ul, 16);
305     t->size = 1;
306     t->data = g_slice_alloc (t->size);
307     t->g_slice = TRUE;
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);
311   }
312
313   if (self->identical_gop) {
314     t = g_slice_new0 (MXFLocalTag);
315     memcpy (&t->ul, &_identical_gop_ul, 16);
316     t->size = 1;
317     t->data = g_slice_alloc (t->size);
318     t->g_slice = TRUE;
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);
322   }
323
324   if (self->max_gop) {
325     t = g_slice_new0 (MXFLocalTag);
326     memcpy (&t->ul, &_identical_gop_ul, 16);
327     t->size = 2;
328     t->data = g_slice_alloc (t->size);
329     t->g_slice = TRUE;
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);
333   }
334
335   if (self->b_picture_count) {
336     t = g_slice_new0 (MXFLocalTag);
337     memcpy (&t->ul, &_b_picture_count_ul, 16);
338     t->size = 2;
339     t->data = g_slice_alloc (t->size);
340     t->g_slice = TRUE;
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);
344   }
345
346   if (self->bitrate) {
347     t = g_slice_new0 (MXFLocalTag);
348     memcpy (&t->ul, &_bitrate_ul, 16);
349     t->size = 4;
350     t->data = g_slice_alloc (t->size);
351     t->g_slice = TRUE;
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);
355   }
356
357   if (self->profile_and_level) {
358     t = g_slice_new0 (MXFLocalTag);
359     memcpy (&t->ul, &_profile_and_level_ul, 16);
360     t->size = 1;
361     t->data = g_slice_alloc (t->size);
362     t->g_slice = TRUE;
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);
366   }
367
368   return ret;
369 }
370
371 static void
372 mxf_metadata_mpeg_video_descriptor_init (MXFMetadataMPEGVideoDescriptor * self)
373 {
374   self->single_sequence = -1;
375 }
376
377 static void
378     mxf_metadata_mpeg_video_descriptor_class_init
379     (MXFMetadataMPEGVideoDescriptorClass * klass)
380 {
381   MXFMetadataBaseClass *metadata_base_class = (MXFMetadataBaseClass *) klass;
382   MXFMetadataClass *metadata_class = (MXFMetadataClass *) klass;
383
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;
391
392   metadata_class->type = 0x0151;
393 }
394
395 typedef enum
396 {
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;
402
403 static gboolean
404 mxf_is_mpeg_essence_track (const MXFMetadataTimelineTrack * track)
405 {
406   guint i;
407
408   g_return_val_if_fail (track != NULL, FALSE);
409
410   if (track->parent.descriptor == NULL)
411     return FALSE;
412
413   for (i = 0; i < track->parent.n_descriptor; i++) {
414     MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
415     MXFUL *key;
416
417     if (!d)
418       continue;
419
420     key = &d->essence_container;
421     /* SMPTE 381M 7 */
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))
426       return TRUE;
427   }
428
429   return FALSE;
430 }
431
432 /* See ISO/IEC 13818-2 for MPEG ES format */
433 gboolean
434 mxf_mpeg_is_mpeg2_keyframe (GstBuffer * buffer)
435 {
436   GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
437   guint32 tmp;
438
439   while (gst_byte_reader_get_remaining (&reader) > 3) {
440     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
441       guint8 type = 0;
442
443       /* Found sync code */
444       gst_byte_reader_skip (&reader, 3);
445
446       if (!gst_byte_reader_get_uint8 (&reader, &type))
447         break;
448
449       /* GOP packets are meant as random access markers */
450       if (type == 0xb8) {
451         return TRUE;
452       } else if (type == 0x00) {
453         guint8 pic_type = 0;
454
455         if (!gst_byte_reader_skip (&reader, 5))
456           break;
457
458         if (!gst_byte_reader_get_uint8 (&reader, &pic_type))
459           break;
460
461         pic_type = (pic_type >> 3) & 0x07;
462         if (pic_type == 0x01) {
463           return TRUE;
464         } else {
465           return FALSE;
466         }
467       }
468     } else {
469       gst_byte_reader_skip (&reader, 1);
470     }
471   }
472
473   return FALSE;
474 }
475
476 static gboolean
477 mxf_mpeg_is_mpeg4_keyframe (GstBuffer * buffer)
478 {
479   GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
480   guint32 tmp;
481
482   while (gst_byte_reader_get_remaining (&reader) > 3) {
483     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
484       guint8 type = 0;
485
486       /* Found sync code */
487       gst_byte_reader_skip (&reader, 3);
488
489       if (!gst_byte_reader_get_uint8 (&reader, &type))
490         break;
491
492       if (type == 0xb6) {
493         guint8 pic_type = 0;
494
495         if (!gst_byte_reader_get_uint8 (&reader, &pic_type))
496           break;
497
498         pic_type = (pic_type >> 6) & 0x03;
499         if (pic_type == 0) {
500           return TRUE;
501         } else {
502           return FALSE;
503         }
504       }
505     } else {
506       gst_byte_reader_skip (&reader, 1);
507     }
508   }
509
510   return FALSE;
511 }
512
513 static GstFlowReturn
514 mxf_mpeg_video_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
515     GstCaps * caps, MXFMetadataTimelineTrack * track,
516     gpointer mapping_data, GstBuffer ** outbuf)
517 {
518   MXFMPEGEssenceType type = *((MXFMPEGEssenceType *) mapping_data);
519
520   *outbuf = buffer;
521
522   /* SMPTE 381M 6.1 */
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;
527   }
528
529   switch (type) {
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);
533       else
534         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
535       break;
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);
539       else
540         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
541       break;
542
543     default:
544       break;
545   }
546
547   return GST_FLOW_OK;
548 }
549
550 static GstFlowReturn
551 mxf_mpeg_audio_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
552     GstCaps * caps, MXFMetadataTimelineTrack * track,
553     gpointer mapping_data, GstBuffer ** outbuf)
554 {
555   *outbuf = buffer;
556
557   /* SMPTE 381M 6.2 */
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;
562   }
563
564   return GST_FLOW_OK;
565 }
566
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,
571   0x01, 0x00, 0x00
572 };
573
574 /* RP224 */
575
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}
579 };
580
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}
584 };
585
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}
589 };
590
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}
594 };
595
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}
599 };
600
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}
604 };
605
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}
609 };
610
611 static GstCaps *
612 mxf_mpeg_es_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
613     MXFEssenceElementHandleFunc * handler, gpointer * mapping_data,
614     MXFMetadataGenericPictureEssenceDescriptor * p,
615     MXFMetadataGenericSoundEssenceDescriptor * s)
616 {
617   GstCaps *caps = NULL;
618   const gchar *codec_name = NULL;
619   MXFMPEGEssenceType t, *mdata;
620
621   *mapping_data = g_malloc (sizeof (MXFMPEGEssenceType));
622   mdata = (MXFMPEGEssenceType *) * mapping_data;
623
624   /* SMPTE RP224 */
625   if (p) {
626     if (mxf_ul_is_zero (&p->picture_essence_coding)) {
627       GST_WARNING ("No picture essence coding defined, assuming MPEG2");
628       caps =
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");
647       caps = NULL;
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;
666
667       caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 4,
668           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
669
670       if (local_tag) {
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,
675             NULL);
676         gst_buffer_unref (codec_data);
677       }
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) {
682       /* RP 2008 */
683
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));
689     } else {
690       GST_ERROR ("Unsupported MPEG picture essence coding 0x%02x",
691           p->picture_essence_coding.u[13]);
692       caps = NULL;
693     }
694     if (caps)
695       *handler = mxf_mpeg_video_handle_essence_element;
696   } else if (s) {
697     if (mxf_ul_is_zero (&s->sound_essence_compression)) {
698       GST_WARNING ("Zero sound essence compression, assuming MPEG1 audio");
699       caps =
700           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
701           NULL);
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)) {
709       caps =
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)) {
715       caps =
716           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
717           NULL);
718       codec_name = "MPEG-1 Audio";
719     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
720             &sound_essence_compression_mpeg1_layer2)) {
721       caps =
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)) {
727       caps =
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,
738           2, NULL);
739       codec_name = "MPEG-2 AAC Audio";
740     }
741
742     if (caps) {
743       mxf_metadata_generic_sound_essence_descriptor_set_caps (s, caps);
744       *handler = mxf_mpeg_audio_handle_essence_element;
745     }
746   }
747
748   if (caps) {
749     if (!*tags)
750       *tags = gst_tag_list_new ();
751     if (codec_name)
752       gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
753           codec_name, NULL);
754
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);
759     }
760   }
761
762   return caps;
763 }
764
765 static GstCaps *
766 mxf_mpeg_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
767     MXFEssenceElementHandleFunc * handler, gpointer * mapping_data)
768 {
769   MXFMetadataFileDescriptor *f = NULL;
770   MXFMetadataGenericPictureEssenceDescriptor *p = NULL;
771   MXFMetadataGenericSoundEssenceDescriptor *s = NULL;
772   guint i;
773   GstCaps *caps = NULL;
774
775   g_return_val_if_fail (track != NULL, NULL);
776
777   if (track->parent.descriptor == NULL) {
778     GST_ERROR ("No descriptor found for this track");
779     return NULL;
780   }
781
782   for (i = 0; i < track->parent.n_descriptor; i++) {
783     if (!track->parent.descriptor[i])
784       continue;
785
786     if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->parent.
787             descriptor[i])) {
788       f = track->parent.descriptor[i];
789       p = (MXFMetadataGenericPictureEssenceDescriptor *) track->
790           parent.descriptor[i];
791       break;
792     } else if (MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->parent.
793             descriptor[i])) {
794       f = track->parent.descriptor[i];
795       s = (MXFMetadataGenericSoundEssenceDescriptor *) track->
796           parent.descriptor[i];
797       break;
798     }
799   }
800
801   if (!f) {
802     GST_ERROR ("No descriptor found for this track");
803     return NULL;
804   }
805
806   /* SMPTE 381M 7 */
807   if (f->essence_container.u[13] == 0x04) {
808     GST_DEBUG ("Found MPEG ES stream");
809
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");
813     return NULL;
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);
819
820     if (!*tags)
821       *tags = gst_tag_list_new ();
822     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
823         "MPEG PS", NULL);
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);
827
828     if (!*tags)
829       *tags = gst_tag_list_new ();
830     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
831         "MPEG TS", NULL);
832   } else if (f->essence_container.u[13] == 0x0f) {
833     GST_DEBUG ("Found h264 NAL unit stream");
834     /* RP 2008 */
835     /* TODO: What about codec_data? */
836     caps = gst_caps_new_simple ("video/x-h264", NULL);
837
838     if (!*tags)
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");
844     /* RP 2008 */
845     caps = gst_caps_new_simple ("video/x-h264", NULL);
846
847     if (!*tags)
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);
851   }
852
853   if (p && caps)
854     mxf_metadata_generic_picture_essence_descriptor_set_caps (p, caps);
855
856   return caps;
857 }
858
859 static const MXFEssenceElementHandler mxf_mpeg_essence_element_handler = {
860   mxf_is_mpeg_essence_track,
861   mxf_mpeg_create_caps
862 };
863
864 typedef struct
865 {
866   guint spf;
867   guint rate;
868 } MPEGAudioMappingData;
869
870 static GstFlowReturn
871 mxf_mpeg_audio_write_func (GstBuffer * buffer, GstCaps * caps,
872     gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
873     gboolean flush)
874 {
875   *outbuf = buffer;
876   return GST_FLOW_OK;
877 }
878
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
882 };
883
884 static MXFMetadataFileDescriptor *
885 mxf_mpeg_audio_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
886     MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
887 {
888   MXFMetadataGenericSoundEssenceDescriptor *ret;
889   GstStructure *s;
890   MPEGAudioMappingData *md = g_new0 (MPEGAudioMappingData, 1);
891   gint rate;
892
893   md->spf = -1;
894   *mapping_data = md;
895
896   ret = (MXFMetadataGenericSoundEssenceDescriptor *)
897       gst_mini_object_new (MXF_TYPE_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR);
898
899   s = gst_caps_get_structure (caps, 0);
900   if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
901     gint mpegversion;
902
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));
906       return NULL;
907     }
908
909     if (mpegversion == 1) {
910       gint layer = 0;
911       gint mpegaudioversion = 0;
912
913       gst_structure_get_int (s, "layer", &layer);
914       gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion);
915
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);
925
926       if (layer == 1)
927         md->spf = 384;
928       else if (layer == 2 || mpegversion == 1)
929         md->spf = 1152;
930       else
931         md->spf = 576;          /* MPEG-2 or 2.5 */
932
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,
936           16);
937       md->spf = 1024;           /* FIXME: is this correct? */
938     }
939   } else if (strcmp (gst_structure_get_name (s), "audio/x-ac3") == 0) {
940     memcpy (&ret->sound_essence_compression, &sound_essence_compression_ac3,
941         16);
942     md->spf = 256;              /* FIXME: is this correct? */
943   } else {
944     g_assert_not_reached ();
945   }
946
947   if (!gst_structure_get_int (s, "rate", &rate)) {
948     GST_ERROR ("Invalid rate");
949     gst_mini_object_unref (GST_MINI_OBJECT_CAST (ret));
950     return NULL;
951   }
952   md->rate = rate;
953
954   memcpy (&ret->parent.essence_container, &mpeg_essence_container_ul, 16);
955
956   if (!mxf_metadata_generic_sound_essence_descriptor_from_caps (ret, caps)) {
957     gst_mini_object_unref (GST_MINI_OBJECT_CAST (ret));
958     return NULL;
959   }
960
961   *handler = mxf_mpeg_audio_write_func;
962
963   return (MXFMetadataFileDescriptor *) ret;
964 }
965
966 static void
967 mxf_mpeg_audio_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
968     gpointer mapping_data, GstBuffer * buf)
969 {
970   return;
971 }
972
973 static void
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)
977 {
978   MPEGAudioMappingData *md = mapping_data;
979
980   edit_rate->n = md->rate;
981   edit_rate->d = md->spf;
982 }
983
984 static guint32
985 mxf_mpeg_audio_get_track_number_template (MXFMetadataFileDescriptor * a,
986     GstCaps * caps, gpointer mapping_data)
987 {
988   return (0x16 << 24) | (0x05 << 8);
989 }
990
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,
996   NULL,
997   {{0,}}
998 };
999
1000 #define MPEG_AUDIO_CAPS \
1001       "audio/mpeg, " \
1002       "mpegversion = (int) 1, " \
1003       "layer = (int) [ 1, 3 ], " \
1004       "rate = (int) [ 8000, 48000 ], " \
1005       "channels = (int) [ 1, 2 ], " \
1006       "parsed = (boolean) TRUE; " \
1007       "audio/x-ac3, " \
1008       "rate = (int) [ 4000, 96000 ], " \
1009       "channels = (int) [ 1, 6 ]; " \
1010       "audio/mpeg, " \
1011       "mpegversion = (int) 2, " \
1012       "rate = (int) [ 8000, 96000 ], " \
1013       "channels = (int) [ 1, 8 ]"
1014
1015 /* See ISO/IEC 13818-2 for MPEG ES format */
1016 static gboolean
1017 mxf_mpeg_is_mpeg2_frame (GstBuffer * buffer)
1018 {
1019   GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
1020   guint32 tmp;
1021
1022   while (gst_byte_reader_get_remaining (&reader) > 3) {
1023     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
1024       guint8 type = 0;
1025
1026       /* Found sync code */
1027       gst_byte_reader_skip (&reader, 3);
1028
1029       if (!gst_byte_reader_get_uint8 (&reader, &type))
1030         break;
1031
1032       /* PICTURE */
1033       if (type == 0x00) {
1034         return TRUE;
1035       }
1036     } else {
1037       gst_byte_reader_skip (&reader, 1);
1038     }
1039   }
1040
1041   return FALSE;
1042 }
1043
1044 static gboolean
1045 mxf_mpeg_is_mpeg4_frame (GstBuffer * buffer)
1046 {
1047   GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer);
1048   guint32 tmp;
1049
1050   while (gst_byte_reader_get_remaining (&reader) > 3) {
1051     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
1052       guint8 type = 0;
1053
1054       /* Found sync code */
1055       gst_byte_reader_skip (&reader, 3);
1056
1057       if (!gst_byte_reader_get_uint8 (&reader, &type))
1058         break;
1059
1060       /* PICTURE */
1061       if (type == 0xb6) {
1062         return TRUE;
1063       }
1064     } else {
1065       gst_byte_reader_skip (&reader, 1);
1066     }
1067   }
1068
1069   return FALSE;
1070 }
1071
1072 static GstFlowReturn
1073 mxf_mpeg_video_write_func (GstBuffer * buffer, GstCaps * caps,
1074     gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
1075     gboolean flush)
1076 {
1077   MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_OTHER;
1078
1079   if (mapping_data)
1080     type = *((MXFMPEGEssenceType *) mapping_data);
1081
1082   if (type == MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2) {
1083     if (buffer && !mxf_mpeg_is_mpeg2_frame (buffer)) {
1084       gst_adapter_push (adapter, buffer);
1085       *outbuf = NULL;
1086       return GST_FLOW_OK;
1087     } else if (buffer || gst_adapter_available (adapter)) {
1088       guint av = gst_adapter_available (adapter);
1089       GstBuffer *ret;
1090
1091       if (buffer)
1092         ret = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buffer) + av);
1093       else
1094         ret = gst_buffer_new_and_alloc (av);
1095
1096       if (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);
1100       }
1101
1102       if (buffer) {
1103         memcpy (GST_BUFFER_DATA (ret) + av, GST_BUFFER_DATA (buffer),
1104             GST_BUFFER_SIZE (buffer));
1105         gst_buffer_unref (buffer);
1106       }
1107       *outbuf = ret;
1108       return GST_FLOW_OK;
1109     }
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);
1113       *outbuf = NULL;
1114       return GST_FLOW_OK;
1115     } else if (buffer || gst_adapter_available (adapter)) {
1116       guint av = gst_adapter_available (adapter);
1117       GstBuffer *ret;
1118
1119       if (buffer)
1120         ret = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buffer) + av);
1121       else
1122         ret = gst_buffer_new_and_alloc (av);
1123
1124       if (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);
1128       }
1129
1130       if (buffer) {
1131         memcpy (GST_BUFFER_DATA (ret) + av, GST_BUFFER_DATA (buffer),
1132             GST_BUFFER_SIZE (buffer));
1133         gst_buffer_unref (buffer);
1134       }
1135       *outbuf = ret;
1136       return GST_FLOW_OK;
1137     }
1138   }
1139
1140   *outbuf = buffer;
1141   return GST_FLOW_OK;
1142 }
1143
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
1147 };
1148
1149 static MXFMetadataFileDescriptor *
1150 mxf_mpeg_video_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
1151     MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
1152 {
1153   MXFMetadataMPEGVideoDescriptor *ret;
1154   GstStructure *s;
1155
1156   ret = (MXFMetadataMPEGVideoDescriptor *)
1157       gst_mini_object_new (MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR);
1158
1159   s = gst_caps_get_structure (caps, 0);
1160
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) {
1166     gint mpegversion;
1167
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));
1171       return NULL;
1172     }
1173
1174     if (mpegversion == 1) {
1175       MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
1176
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;
1182
1183       *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1184       memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1185       ret->parent.parent.picture_essence_coding.u[13] = 0x01;
1186     } else {
1187       const GValue *v;
1188       const GstBuffer *codec_data;
1189       MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4;
1190
1191       *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1192       memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1193
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);
1202       }
1203     }
1204   } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
1205     MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC;
1206
1207     *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1208     memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1209     ret->parent.parent.picture_essence_coding.u[13] = 0x30;
1210   } else {
1211     g_assert_not_reached ();
1212   }
1213
1214
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));
1218     return NULL;
1219   }
1220
1221   *handler = mxf_mpeg_video_write_func;
1222
1223   return (MXFMetadataFileDescriptor *) ret;
1224 }
1225
1226 static void
1227 mxf_mpeg_video_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
1228     gpointer mapping_data, GstBuffer * buf)
1229 {
1230   return;
1231 }
1232
1233 static void
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)
1237 {
1238   (*edit_rate).n = a->sample_rate.n;
1239   (*edit_rate).d = a->sample_rate.d;
1240 }
1241
1242 static guint32
1243 mxf_mpeg_video_get_track_number_template (MXFMetadataFileDescriptor * a,
1244     GstCaps * caps, gpointer mapping_data)
1245 {
1246   return (0x15 << 24) | (0x05 << 8);
1247 }
1248
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,
1254   NULL,
1255   {{0,}}
1256 };
1257
1258 #define MPEG_VIDEO_CAPS \
1259 "video/mpeg, " \
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 "; " \
1265 "video/x-h264, " \
1266 "width = " GST_VIDEO_SIZE_RANGE ", " \
1267 "height = " GST_VIDEO_SIZE_RANGE ", " \
1268 "framerate = " GST_VIDEO_FPS_RANGE
1269
1270 void
1271 mxf_mpeg_init (void)
1272 {
1273   mxf_metadata_register (MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR);
1274   mxf_essence_element_handler_register (&mxf_mpeg_essence_element_handler);
1275
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);
1282
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),
1288       16);
1289   mxf_essence_element_writer_register (&mxf_mpeg_video_essence_element_writer);
1290
1291 }