• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /* Implementation of SMPTE 381M - Mapping MPEG streams into the MXF
21  * Generic Container
22  *
23  * RP 2008 - Mapping AVC Streams into the MXF Generic Container
24  *
25  */
26 
27 /* TODO:
28  * - Handle PES streams
29  * - Fix TS/PS demuxers to forward timestamps
30  * - AAC support
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <gst/gst.h>
38 #include <gst/video/video.h>
39 #include <string.h>
40 
41 #include "mxfmpeg.h"
42 #include "mxfquark.h"
43 #include "mxfessence.h"
44 
45 #include <gst/base/gstbytereader.h>
46 
47 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
48 #define GST_CAT_DEFAULT mxf_debug
49 
50 /* SMPTE 381M 8.1 - ULs of local tags */
51 static const guint8 _single_sequence_ul[] = {
52   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
53   0x02, 0x00, 0x00
54 };
55 
56 static const guint8 _constant_b_frames_ul[] = {
57   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
58   0x03, 0x00, 0x00
59 };
60 
61 static const guint8 _coded_content_type_ul[] = {
62   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
63   0x04, 0x00, 0x00
64 };
65 
66 static const guint8 _low_delay_ul[] = {
67   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
68   0x05, 0x00, 0x00
69 };
70 
71 static const guint8 _closed_gop_ul[] = {
72   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
73   0x06, 0x00, 0x00
74 };
75 
76 static const guint8 _identical_gop_ul[] = {
77   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
78   0x07, 0x00, 0x00
79 };
80 
81 static const guint8 _max_gop_ul[] = {
82   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
83   0x08, 0x00, 0x00
84 };
85 
86 static const guint8 _b_picture_count_ul[] = {
87   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
88   0x09, 0x00, 0x00
89 };
90 
91 static const guint8 _bitrate_ul[] = {
92   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
93   0x0b, 0x00, 0x00
94 };
95 
96 static const guint8 _profile_and_level_ul[] = {
97   0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x05, 0x04, 0x01, 0x06, 0x02, 0x01,
98   0x0a, 0x00, 0x00
99 };
100 
101 /* SMPTE 381M 8.1 */
102 #define MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR \
103   (mxf_metadata_mpeg_video_descriptor_get_type())
104 #define MXF_METADATA_MPEG_VIDEO_DESCRIPTOR(obj) \
105   (G_TYPE_CHECK_INSTANCE_CAST((obj),MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR, MXFMetadataMPEGVideoDescriptor))
106 #define MXF_IS_METADATA_MPEG_VIDEO_DESCRIPTOR(obj) \
107   (G_TYPE_CHECK_INSTANCE_TYPE((obj),MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR))
108 typedef struct _MXFMetadataMPEGVideoDescriptor MXFMetadataMPEGVideoDescriptor;
109 typedef MXFMetadataClass MXFMetadataMPEGVideoDescriptorClass;
110 GType mxf_metadata_mpeg_video_descriptor_get_type (void);
111 
112 struct _MXFMetadataMPEGVideoDescriptor
113 {
114   MXFMetadataCDCIPictureEssenceDescriptor parent;
115 
116   gboolean single_sequence;
117   gboolean const_b_frames;
118   guint8 coded_content_type;
119   gboolean low_delay;
120 
121   gboolean closed_gop;
122   gboolean identical_gop;
123   guint16 max_gop;
124 
125   guint16 b_picture_count;
126   guint32 bitrate;
127   guint8 profile_and_level;
128 };
129 
130 G_DEFINE_TYPE (MXFMetadataMPEGVideoDescriptor,
131     mxf_metadata_mpeg_video_descriptor,
132     MXF_TYPE_METADATA_CDCI_PICTURE_ESSENCE_DESCRIPTOR);
133 
134 static gboolean
mxf_metadata_mpeg_video_descriptor_handle_tag(MXFMetadataBase * metadata,MXFPrimerPack * primer,guint16 tag,const guint8 * tag_data,guint tag_size)135 mxf_metadata_mpeg_video_descriptor_handle_tag (MXFMetadataBase * metadata,
136     MXFPrimerPack * primer, guint16 tag, const guint8 * tag_data,
137     guint tag_size)
138 {
139   MXFMetadataMPEGVideoDescriptor *self =
140       MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (metadata);
141   gboolean ret = TRUE;
142   MXFUL *tag_ul = NULL;
143 
144   if (!(tag_ul =
145           (MXFUL *) g_hash_table_lookup (primer->mappings,
146               GUINT_TO_POINTER (((guint) tag)))))
147     return FALSE;
148 
149   if (memcmp (tag_ul, &_single_sequence_ul, 16) == 0) {
150     if (tag_size != 1)
151       goto error;
152     self->single_sequence = GST_READ_UINT8 (tag_data);
153     GST_DEBUG ("  single sequence = %s",
154         (self->single_sequence) ? "yes" : "no");
155   } else if (memcmp (tag_ul, &_constant_b_frames_ul, 16) == 0) {
156     if (tag_size != 1)
157       goto error;
158     self->const_b_frames = GST_READ_UINT8 (tag_data);
159     GST_DEBUG ("  constant b frames = %s",
160         (self->single_sequence) ? "yes" : "no");
161   } else if (memcmp (tag_ul, &_coded_content_type_ul, 16) == 0) {
162     if (tag_size != 1)
163       goto error;
164     self->coded_content_type = GST_READ_UINT8 (tag_data);
165     GST_DEBUG ("  coded content type = %u", self->coded_content_type);
166   } else if (memcmp (tag_ul, &_low_delay_ul, 16) == 0) {
167     if (tag_size != 1)
168       goto error;
169     self->low_delay = GST_READ_UINT8 (tag_data);
170     GST_DEBUG ("  low delay = %s", (self->low_delay) ? "yes" : "no");
171   } else if (memcmp (tag_ul, &_closed_gop_ul, 16) == 0) {
172     if (tag_size != 1)
173       goto error;
174     self->closed_gop = GST_READ_UINT8 (tag_data);
175     GST_DEBUG ("  closed gop = %s", (self->closed_gop) ? "yes" : "no");
176   } else if (memcmp (tag_ul, &_identical_gop_ul, 16) == 0) {
177     if (tag_size != 1)
178       goto error;
179     self->identical_gop = GST_READ_UINT8 (tag_data);
180     GST_DEBUG ("  identical gop = %s", (self->identical_gop) ? "yes" : "no");
181   } else if (memcmp (tag_ul, &_max_gop_ul, 16) == 0) {
182     if (tag_size != 2)
183       goto error;
184     self->max_gop = GST_READ_UINT16_BE (tag_data);
185     GST_DEBUG ("  max gop = %u", self->max_gop);
186   } else if (memcmp (tag_ul, &_b_picture_count_ul, 16) == 0) {
187     if (tag_size != 2)
188       goto error;
189     self->b_picture_count = GST_READ_UINT16_BE (tag_data);
190     GST_DEBUG ("  b picture count = %u", self->b_picture_count);
191   } else if (memcmp (tag_ul, &_bitrate_ul, 16) == 0) {
192     if (tag_size != 4)
193       goto error;
194     self->bitrate = GST_READ_UINT32_BE (tag_data);
195     GST_DEBUG ("  bitrate = %u", self->bitrate);
196   } else if (memcmp (tag_ul, &_profile_and_level_ul, 16) == 0) {
197     if (tag_size != 1)
198       goto error;
199     self->profile_and_level = GST_READ_UINT8 (tag_data);
200     GST_DEBUG ("  profile & level = %u", self->profile_and_level);
201   } else {
202     ret =
203         MXF_METADATA_BASE_CLASS
204         (mxf_metadata_mpeg_video_descriptor_parent_class)->handle_tag (metadata,
205         primer, tag, tag_data, tag_size);
206   }
207 
208   return ret;
209 
210 error:
211 
212   GST_ERROR ("Invalid MPEG video descriptor local tag 0x%04x of size %u", tag,
213       tag_size);
214 
215   return FALSE;
216 }
217 
218 static GstStructure *
mxf_metadata_mpeg_video_descriptor_to_structure(MXFMetadataBase * m)219 mxf_metadata_mpeg_video_descriptor_to_structure (MXFMetadataBase * m)
220 {
221   GstStructure *ret =
222       MXF_METADATA_BASE_CLASS
223       (mxf_metadata_mpeg_video_descriptor_parent_class)->to_structure (m);
224   MXFMetadataMPEGVideoDescriptor *self = MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (m);
225 
226   gst_structure_id_set (ret, MXF_QUARK (SINGLE_SEQUENCE), G_TYPE_BOOLEAN,
227       self->single_sequence, MXF_QUARK (CONST_B_FRAMES), G_TYPE_BOOLEAN,
228       self->const_b_frames, MXF_QUARK (CODED_CONTENT_TYPE), G_TYPE_UCHAR,
229       self->coded_content_type, MXF_QUARK (LOW_DELAY), G_TYPE_BOOLEAN,
230       self->low_delay, MXF_QUARK (CLOSED_GOP), G_TYPE_BOOLEAN, self->closed_gop,
231       MXF_QUARK (IDENTICAL_GOP), G_TYPE_BOOLEAN, self->identical_gop,
232       MXF_QUARK (PROFILE_AND_LEVEL), G_TYPE_UCHAR, self->profile_and_level,
233       NULL);
234 
235   if (self->max_gop)
236     gst_structure_id_set (ret, MXF_QUARK (MAX_GOP), G_TYPE_UINT, self->max_gop,
237         NULL);
238 
239   if (self->b_picture_count)
240     gst_structure_id_set (ret, MXF_QUARK (B_PICTURE_COUNT), G_TYPE_UINT,
241         self->b_picture_count, NULL);
242 
243   if (self->bitrate)
244     gst_structure_id_set (ret, MXF_QUARK (BITRATE), G_TYPE_UINT, self->bitrate,
245         NULL);
246 
247   return ret;
248 }
249 
250 static GList *
mxf_metadata_mpeg_video_descriptor_write_tags(MXFMetadataBase * m,MXFPrimerPack * primer)251 mxf_metadata_mpeg_video_descriptor_write_tags (MXFMetadataBase * m,
252     MXFPrimerPack * primer)
253 {
254   MXFMetadataMPEGVideoDescriptor *self = MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (m);
255   GList *ret =
256       MXF_METADATA_BASE_CLASS
257       (mxf_metadata_mpeg_video_descriptor_parent_class)->write_tags (m, primer);
258   MXFLocalTag *t;
259 
260   if (self->single_sequence != -1) {
261     t = g_slice_new0 (MXFLocalTag);
262     memcpy (&t->ul, &_single_sequence_ul, 16);
263     t->size = 1;
264     t->data = g_slice_alloc (t->size);
265     t->g_slice = TRUE;
266     GST_WRITE_UINT8 (t->data, (self->single_sequence) ? 1 : 0);
267     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
268     ret = g_list_prepend (ret, t);
269   }
270 
271   if (self->const_b_frames) {
272     t = g_slice_new0 (MXFLocalTag);
273     memcpy (&t->ul, &_constant_b_frames_ul, 16);
274     t->size = 1;
275     t->data = g_slice_alloc (t->size);
276     t->g_slice = TRUE;
277     GST_WRITE_UINT8 (t->data, (self->const_b_frames) ? 1 : 0);
278     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
279     ret = g_list_prepend (ret, t);
280   }
281 
282   if (self->coded_content_type) {
283     t = g_slice_new0 (MXFLocalTag);
284     memcpy (&t->ul, &_coded_content_type_ul, 16);
285     t->size = 1;
286     t->data = g_slice_alloc (t->size);
287     t->g_slice = TRUE;
288     GST_WRITE_UINT8 (t->data, self->coded_content_type);
289     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
290     ret = g_list_prepend (ret, t);
291   }
292 
293   if (self->low_delay) {
294     t = g_slice_new0 (MXFLocalTag);
295     memcpy (&t->ul, &_low_delay_ul, 16);
296     t->size = 1;
297     t->data = g_slice_alloc (t->size);
298     t->g_slice = TRUE;
299     GST_WRITE_UINT8 (t->data, (self->low_delay) ? 1 : 0);
300     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
301     ret = g_list_prepend (ret, t);
302   }
303 
304   if (self->closed_gop) {
305     t = g_slice_new0 (MXFLocalTag);
306     memcpy (&t->ul, &_closed_gop_ul, 16);
307     t->size = 1;
308     t->data = g_slice_alloc (t->size);
309     t->g_slice = TRUE;
310     GST_WRITE_UINT8 (t->data, (self->closed_gop) ? 1 : 0);
311     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
312     ret = g_list_prepend (ret, t);
313   }
314 
315   if (self->identical_gop) {
316     t = g_slice_new0 (MXFLocalTag);
317     memcpy (&t->ul, &_identical_gop_ul, 16);
318     t->size = 1;
319     t->data = g_slice_alloc (t->size);
320     t->g_slice = TRUE;
321     GST_WRITE_UINT8 (t->data, (self->identical_gop) ? 1 : 0);
322     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
323     ret = g_list_prepend (ret, t);
324   }
325 
326   if (self->max_gop) {
327     t = g_slice_new0 (MXFLocalTag);
328     memcpy (&t->ul, &_identical_gop_ul, 16);
329     t->size = 2;
330     t->data = g_slice_alloc (t->size);
331     t->g_slice = TRUE;
332     GST_WRITE_UINT16_BE (t->data, self->max_gop);
333     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
334     ret = g_list_prepend (ret, t);
335   }
336 
337   if (self->b_picture_count) {
338     t = g_slice_new0 (MXFLocalTag);
339     memcpy (&t->ul, &_b_picture_count_ul, 16);
340     t->size = 2;
341     t->data = g_slice_alloc (t->size);
342     t->g_slice = TRUE;
343     GST_WRITE_UINT16_BE (t->data, self->b_picture_count);
344     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
345     ret = g_list_prepend (ret, t);
346   }
347 
348   if (self->bitrate) {
349     t = g_slice_new0 (MXFLocalTag);
350     memcpy (&t->ul, &_bitrate_ul, 16);
351     t->size = 4;
352     t->data = g_slice_alloc (t->size);
353     t->g_slice = TRUE;
354     GST_WRITE_UINT32_BE (t->data, self->bitrate);
355     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
356     ret = g_list_prepend (ret, t);
357   }
358 
359   if (self->profile_and_level) {
360     t = g_slice_new0 (MXFLocalTag);
361     memcpy (&t->ul, &_profile_and_level_ul, 16);
362     t->size = 1;
363     t->data = g_slice_alloc (t->size);
364     t->g_slice = TRUE;
365     GST_WRITE_UINT8 (t->data, self->profile_and_level);
366     mxf_primer_pack_add_mapping (primer, 0, &t->ul);
367     ret = g_list_prepend (ret, t);
368   }
369 
370   return ret;
371 }
372 
373 static void
mxf_metadata_mpeg_video_descriptor_init(MXFMetadataMPEGVideoDescriptor * self)374 mxf_metadata_mpeg_video_descriptor_init (MXFMetadataMPEGVideoDescriptor * self)
375 {
376   self->single_sequence = -1;
377 }
378 
379 static void
mxf_metadata_mpeg_video_descriptor_class_init(MXFMetadataMPEGVideoDescriptorClass * klass)380     mxf_metadata_mpeg_video_descriptor_class_init
381     (MXFMetadataMPEGVideoDescriptorClass * klass)
382 {
383   MXFMetadataBaseClass *metadata_base_class = (MXFMetadataBaseClass *) klass;
384   MXFMetadataClass *metadata_class = (MXFMetadataClass *) klass;
385 
386   metadata_base_class->handle_tag =
387       mxf_metadata_mpeg_video_descriptor_handle_tag;
388   metadata_base_class->name_quark = MXF_QUARK (MPEG_VIDEO_DESCRIPTOR);
389   metadata_base_class->to_structure =
390       mxf_metadata_mpeg_video_descriptor_to_structure;
391   metadata_base_class->write_tags =
392       mxf_metadata_mpeg_video_descriptor_write_tags;
393 
394   metadata_class->type = 0x0151;
395 }
396 
397 typedef enum
398 {
399   MXF_MPEG_ESSENCE_TYPE_OTHER = 0,
400   MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2,
401   MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4,
402   MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC
403 } MXFMPEGEssenceType;
404 
405 static gboolean
mxf_is_mpeg_essence_track(const MXFMetadataTimelineTrack * track)406 mxf_is_mpeg_essence_track (const MXFMetadataTimelineTrack * track)
407 {
408   guint i;
409 
410   g_return_val_if_fail (track != NULL, FALSE);
411 
412   if (track->parent.descriptor == NULL)
413     return FALSE;
414 
415   for (i = 0; i < track->parent.n_descriptor; i++) {
416     MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
417     MXFUL *key;
418 
419     if (!d)
420       continue;
421 
422     key = &d->essence_container;
423     /* SMPTE 381M 7 */
424     /* SMPTE RP2008 8.1 */
425     if (mxf_is_generic_container_essence_container_label (key) &&
426         key->u[12] == 0x02 &&
427         (key->u[13] == 0x04 ||
428             key->u[13] == 0x07 || key->u[13] == 0x08 || key->u[13] == 0x09 ||
429             key->u[13] == 0x0f || key->u[13] == 0x10))
430       return TRUE;
431   }
432 
433   return FALSE;
434 }
435 
436 /* See ISO/IEC 13818-2 for MPEG ES format */
437 gboolean
mxf_mpeg_is_mpeg2_keyframe(GstBuffer * buffer)438 mxf_mpeg_is_mpeg2_keyframe (GstBuffer * buffer)
439 {
440   GstMapInfo map;
441   GstByteReader reader;
442   guint32 tmp;
443   gboolean ret = FALSE;
444 
445   gst_buffer_map (buffer, &map, GST_MAP_READ);
446   gst_byte_reader_init (&reader, map.data, map.size);
447 
448   while (gst_byte_reader_get_remaining (&reader) > 3) {
449     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
450       guint8 type = 0;
451 
452       /* Found sync code */
453       gst_byte_reader_skip_unchecked (&reader, 3);
454 
455       if (!gst_byte_reader_get_uint8 (&reader, &type))
456         break;
457 
458       /* GOP packets are meant as random access markers */
459       if (type == 0xb8) {
460         ret = TRUE;
461         goto done;
462       } else if (type == 0x00) {
463         guint8 pic_type = 0;
464 
465         if (!gst_byte_reader_skip (&reader, 5))
466           break;
467 
468         if (!gst_byte_reader_get_uint8 (&reader, &pic_type))
469           break;
470 
471         pic_type = (pic_type >> 3) & 0x07;
472         if (pic_type == 0x01) {
473           ret = TRUE;
474         }
475         goto done;
476       }
477     } else if (gst_byte_reader_skip (&reader, 1) == FALSE)
478       break;
479   }
480 
481 done:
482   gst_buffer_unmap (buffer, &map);
483 
484   return ret;
485 }
486 
487 static gboolean
mxf_mpeg_is_mpeg4_keyframe(GstBuffer * buffer)488 mxf_mpeg_is_mpeg4_keyframe (GstBuffer * buffer)
489 {
490   GstMapInfo map;
491   GstByteReader reader;
492   guint32 tmp;
493   gboolean ret = FALSE;
494 
495   gst_buffer_map (buffer, &map, GST_MAP_READ);
496   gst_byte_reader_init (&reader, map.data, map.size);
497 
498   while (gst_byte_reader_get_remaining (&reader) > 3) {
499     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
500       guint8 type = 0;
501 
502       /* Found sync code */
503       gst_byte_reader_skip_unchecked (&reader, 3);
504 
505       if (!gst_byte_reader_get_uint8 (&reader, &type))
506         break;
507 
508       if (type == 0xb6) {
509         guint8 pic_type = 0;
510 
511         if (!gst_byte_reader_get_uint8 (&reader, &pic_type))
512           break;
513 
514         pic_type = (pic_type >> 6) & 0x03;
515         if (pic_type == 0) {
516           ret = TRUE;
517         }
518         goto done;
519       }
520     } else if (gst_byte_reader_skip (&reader, 1) == FALSE)
521       break;
522   }
523 
524 done:
525   gst_buffer_unmap (buffer, &map);
526 
527   return ret;
528 }
529 
530 static GstFlowReturn
mxf_mpeg_video_handle_essence_element(const MXFUL * key,GstBuffer * buffer,GstCaps * caps,MXFMetadataTimelineTrack * track,gpointer mapping_data,GstBuffer ** outbuf)531 mxf_mpeg_video_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
532     GstCaps * caps, MXFMetadataTimelineTrack * track,
533     gpointer mapping_data, GstBuffer ** outbuf)
534 {
535   MXFMPEGEssenceType type = *((MXFMPEGEssenceType *) mapping_data);
536 
537   *outbuf = buffer;
538 
539   /* SMPTE 381M 6.1 */
540   if (key->u[12] != 0x15 || (key->u[14] != 0x05 && key->u[14] != 0x06
541           && key->u[14] != 0x07)) {
542     GST_ERROR ("Invalid MPEG video essence element");
543     return GST_FLOW_ERROR;
544   }
545 
546   switch (type) {
547     case MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2:
548       if (mxf_mpeg_is_mpeg2_keyframe (buffer))
549         GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
550       else
551         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
552       break;
553     case MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4:
554       if (mxf_mpeg_is_mpeg4_keyframe (buffer))
555         GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
556       else
557         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
558       break;
559 
560     default:
561       break;
562   }
563 
564   return GST_FLOW_OK;
565 }
566 
567 static GstFlowReturn
mxf_mpeg_audio_handle_essence_element(const MXFUL * key,GstBuffer * buffer,GstCaps * caps,MXFMetadataTimelineTrack * track,gpointer mapping_data,GstBuffer ** outbuf)568 mxf_mpeg_audio_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
569     GstCaps * caps, MXFMetadataTimelineTrack * track,
570     gpointer mapping_data, GstBuffer ** outbuf)
571 {
572   *outbuf = buffer;
573 
574   /* SMPTE 381M 6.2 */
575   if (key->u[12] != 0x16 || (key->u[14] != 0x05 && key->u[14] != 0x06
576           && key->u[14] != 0x07)) {
577     GST_ERROR ("Invalid MPEG audio essence element");
578     return GST_FLOW_ERROR;
579   }
580 
581   return GST_FLOW_OK;
582 }
583 
584 /* Private uid used by SONY C0023S01.mxf,
585  * taken from the ffmpeg mxf demuxer */
586 static const guint8 sony_mpeg4_extradata[] = {
587   0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0e, 0x06, 0x06, 0x02, 0x02,
588   0x01, 0x00, 0x00
589 };
590 
591 /* RP224 */
592 
593 static const MXFUL sound_essence_compression_ac3 = { {
594         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
595     0x03, 0x02, 0x01, 0x00}
596 };
597 
598 static const MXFUL sound_essence_compression_mpeg1_layer1 = { {
599         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
600     0x03, 0x02, 0x04, 0x00}
601 };
602 
603 static const MXFUL sound_essence_compression_mpeg1_layer23 = { {
604         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
605     0x03, 0x02, 0x05, 0x00}
606 };
607 
608 static const MXFUL sound_essence_compression_mpeg1_layer2 = { {
609         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x08, 0x04, 0x02, 0x02, 0x02,
610     0x03, 0x02, 0x05, 0x01}
611 };
612 
613 static const MXFUL sound_essence_compression_mpeg2_layer1 = { {
614         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
615     0x03, 0x02, 0x06, 0x00}
616 };
617 
618 static const MXFUL sound_essence_compression_dts = { {
619         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
620     0x03, 0x02, 0x1c, 0x00}
621 };
622 
623 static const MXFUL sound_essence_compression_aac = { {
624         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x03, 0x04, 0x02, 0x02, 0x02,
625     0x03, 0x03, 0x01, 0x00}
626 };
627 
628 static GstCaps *
mxf_mpeg_es_create_caps(MXFMetadataTimelineTrack * track,GstTagList ** tags,gboolean * intra_only,MXFEssenceElementHandleFunc * handler,gpointer * mapping_data,MXFMetadataGenericPictureEssenceDescriptor * p,MXFMetadataGenericSoundEssenceDescriptor * s)629 mxf_mpeg_es_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
630     gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
631     gpointer * mapping_data, MXFMetadataGenericPictureEssenceDescriptor * p,
632     MXFMetadataGenericSoundEssenceDescriptor * s)
633 {
634   GstCaps *caps = NULL;
635   const gchar *codec_name = NULL;
636   MXFMPEGEssenceType t, *mdata;
637 
638   *mapping_data = g_malloc (sizeof (MXFMPEGEssenceType));
639   mdata = (MXFMPEGEssenceType *) * mapping_data;
640 
641   /* SMPTE RP224 */
642   if (p) {
643     if (mxf_ul_is_zero (&p->picture_essence_coding)) {
644       GST_WARNING ("No picture essence coding defined, assuming MPEG2");
645       caps =
646           gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 2,
647           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
648       codec_name = "MPEG-2 Video";
649       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
650       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
651       *intra_only = FALSE;
652     } else if (p->picture_essence_coding.u[0] != 0x06
653         || p->picture_essence_coding.u[1] != 0x0e
654         || p->picture_essence_coding.u[2] != 0x2b
655         || p->picture_essence_coding.u[3] != 0x34
656         || p->picture_essence_coding.u[4] != 0x04
657         || p->picture_essence_coding.u[5] != 0x01
658         || p->picture_essence_coding.u[6] != 0x01
659         || p->picture_essence_coding.u[8] != 0x04
660         || p->picture_essence_coding.u[9] != 0x01
661         || p->picture_essence_coding.u[10] != 0x02
662         || p->picture_essence_coding.u[11] != 0x02
663         || p->picture_essence_coding.u[12] != 0x01) {
664       GST_ERROR ("No MPEG picture essence coding");
665       caps = NULL;
666     } else if (p->picture_essence_coding.u[13] >= 0x01 &&
667         p->picture_essence_coding.u[13] <= 0x08) {
668       caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 2,
669           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
670       codec_name = "MPEG-2 Video";
671       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
672       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
673       *intra_only = FALSE;
674     } else if (p->picture_essence_coding.u[13] == 0x10) {
675       caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 1,
676           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
677       codec_name = "MPEG-1 Video";
678       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
679       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
680       *intra_only = TRUE;
681     } else if (p->picture_essence_coding.u[13] == 0x20) {
682       MXFLocalTag *local_tag =
683           (((MXFMetadataBase *) p)->other_tags) ?
684           g_hash_table_lookup (((MXFMetadataBase *)
685               p)->other_tags, &sony_mpeg4_extradata) : NULL;
686 
687       caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 4,
688           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
689 
690       if (local_tag) {
691         GstMapInfo map;
692         GstBuffer *codec_data = NULL;
693         codec_data = gst_buffer_new_and_alloc (local_tag->size);
694         gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
695         memcpy (map.data, local_tag->data, local_tag->size);
696         gst_buffer_unmap (codec_data, &map);
697         gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, codec_data,
698             NULL);
699         gst_buffer_unref (codec_data);
700       }
701       codec_name = "MPEG-4 Video";
702       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4;
703       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
704       *intra_only = FALSE;
705     } else if ((p->picture_essence_coding.u[13] >> 4) == 0x03) {
706       /* RP 2008 */
707 
708       caps =
709           gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,
710           "byte-stream", NULL);
711       codec_name = "h.264 Video";
712       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC;
713       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
714       *intra_only = FALSE;
715     } else {
716       GST_ERROR ("Unsupported MPEG picture essence coding 0x%02x",
717           p->picture_essence_coding.u[13]);
718       caps = NULL;
719     }
720     if (caps)
721       *handler = mxf_mpeg_video_handle_essence_element;
722   } else if (s) {
723     if (mxf_ul_is_zero (&s->sound_essence_compression)) {
724       GST_WARNING ("Zero sound essence compression, assuming MPEG1 audio");
725       caps =
726           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
727           NULL);
728       codec_name = "MPEG-1 Audio";
729     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
730             &sound_essence_compression_ac3)) {
731       caps = gst_caps_new_empty_simple ("audio/x-ac3");
732       codec_name = "AC3 Audio";
733     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
734             &sound_essence_compression_mpeg1_layer1)) {
735       caps =
736           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
737           "layer", G_TYPE_INT, 1, NULL);
738       codec_name = "MPEG-1 Layer 1 Audio";
739     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
740             &sound_essence_compression_mpeg1_layer23)) {
741       caps =
742           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
743           NULL);
744       codec_name = "MPEG-1 Audio";
745     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
746             &sound_essence_compression_mpeg1_layer2)) {
747       caps =
748           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
749           "layer", G_TYPE_INT, 2, NULL);
750       codec_name = "MPEG-1 Layer 2 Audio";
751     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
752             &sound_essence_compression_mpeg2_layer1)) {
753       caps =
754           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
755           "layer", G_TYPE_INT, 1, "mpegaudioversion", G_TYPE_INT, 2, NULL);
756       codec_name = "MPEG-2 Layer 1 Audio";
757     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
758             &sound_essence_compression_dts)) {
759       caps = gst_caps_new_empty_simple ("audio/x-dts");
760       codec_name = "Dolby DTS Audio";
761     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
762             &sound_essence_compression_aac)) {
763       caps = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
764           2, NULL);
765       codec_name = "MPEG-2 AAC Audio";
766     }
767 
768     if (caps) {
769       mxf_metadata_generic_sound_essence_descriptor_set_caps (s, caps);
770       *handler = mxf_mpeg_audio_handle_essence_element;
771     }
772     *intra_only = TRUE;
773   }
774 
775   if (caps) {
776     if (!*tags)
777       *tags = gst_tag_list_new_empty ();
778     if (codec_name)
779       gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
780           codec_name, NULL);
781 
782     if (p && MXF_IS_METADATA_MPEG_VIDEO_DESCRIPTOR (p)
783         && MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (p)->bitrate) {
784       gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_BITRATE,
785           MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (p)->bitrate, NULL);
786     }
787   }
788 
789   return caps;
790 }
791 
792 static MXFEssenceWrapping
mxf_mpeg_get_track_wrapping(const MXFMetadataTimelineTrack * track)793 mxf_mpeg_get_track_wrapping (const MXFMetadataTimelineTrack * track)
794 {
795   guint i;
796 
797   g_return_val_if_fail (track != NULL, MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING);
798 
799   if (track->parent.descriptor == NULL) {
800     GST_ERROR ("No descriptor found for this track");
801     return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
802   }
803 
804   for (i = 0; i < track->parent.n_descriptor; i++) {
805     if (!track->parent.descriptor[i])
806       continue;
807 
808     if (!MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
809             parent.descriptor[i])
810         && !MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->
811             parent.descriptor[i]))
812       continue;
813 
814     switch (track->parent.descriptor[i]->essence_container.u[15]) {
815       case 0x01:
816         return MXF_ESSENCE_WRAPPING_FRAME_WRAPPING;
817         break;
818       case 0x02:
819         return MXF_ESSENCE_WRAPPING_CLIP_WRAPPING;
820         break;
821       default:
822         return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
823         break;
824     }
825   }
826 
827   return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
828 }
829 
830 static GstCaps *
mxf_mpeg_create_caps(MXFMetadataTimelineTrack * track,GstTagList ** tags,gboolean * intra_only,MXFEssenceElementHandleFunc * handler,gpointer * mapping_data)831 mxf_mpeg_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
832     gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
833     gpointer * mapping_data)
834 {
835   MXFMetadataFileDescriptor *f = NULL;
836   MXFMetadataGenericPictureEssenceDescriptor *p = NULL;
837   MXFMetadataGenericSoundEssenceDescriptor *s = NULL;
838   guint i;
839   GstCaps *caps = NULL;
840 
841   g_return_val_if_fail (track != NULL, NULL);
842 
843   if (track->parent.descriptor == NULL) {
844     GST_ERROR ("No descriptor found for this track");
845     return NULL;
846   }
847 
848   for (i = 0; i < track->parent.n_descriptor; i++) {
849     if (!track->parent.descriptor[i])
850       continue;
851 
852     if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
853             parent.descriptor[i])) {
854       f = track->parent.descriptor[i];
855       p = (MXFMetadataGenericPictureEssenceDescriptor *) track->parent.
856           descriptor[i];
857       break;
858     } else if (MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->
859             parent.descriptor[i])) {
860       f = track->parent.descriptor[i];
861       s = (MXFMetadataGenericSoundEssenceDescriptor *) track->parent.
862           descriptor[i];
863       break;
864     }
865   }
866 
867   if (!f) {
868     GST_ERROR ("No descriptor found for this track");
869     return NULL;
870   }
871 
872   /* SMPTE 381M 7 */
873   if (f->essence_container.u[13] == 0x04) {
874     GST_DEBUG ("Found MPEG ES stream");
875 
876     caps =
877         mxf_mpeg_es_create_caps (track, tags, intra_only, handler, mapping_data,
878         p, s);
879   } else if (f->essence_container.u[13] == 0x07) {
880     GST_ERROR ("MPEG PES streams not supported yet");
881     return NULL;
882   } else if (f->essence_container.u[13] == 0x08) {
883     /* FIXME: get mpeg version somehow */
884     GST_DEBUG ("Found MPEG PS stream");
885     caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 1,
886         "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
887 
888     if (!*tags)
889       *tags = gst_tag_list_new_empty ();
890     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
891         "MPEG PS", NULL);
892     *intra_only = FALSE;
893   } else if (f->essence_container.u[13] == 0x09) {
894     GST_DEBUG ("Found MPEG TS stream");
895     caps = gst_caps_new_empty_simple ("video/mpegts");
896 
897     if (!*tags)
898       *tags = gst_tag_list_new_empty ();
899     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
900         "MPEG TS", NULL);
901     *intra_only = FALSE;
902   } else if (f->essence_container.u[13] == 0x0f) {
903     GST_DEBUG ("Found h264 NAL unit stream");
904     /* RP 2008 */
905     caps =
906         gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,
907         "byte-stream", NULL);
908 
909     if (!*tags)
910       *tags = gst_tag_list_new_empty ();
911     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
912         "h.264 Video", NULL);
913     *intra_only = FALSE;
914   } else if (f->essence_container.u[13] == 0x10) {
915     GST_DEBUG ("Found h264 byte-stream stream");
916     /* RP 2008 */
917     caps =
918         gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,
919         "byte-stream", NULL);
920 
921     if (!*tags)
922       *tags = gst_tag_list_new_empty ();
923     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
924         "h.264 Video", NULL);
925     *intra_only = FALSE;
926   }
927 
928   if (p && caps)
929     mxf_metadata_generic_picture_essence_descriptor_set_caps (p, caps);
930 
931   return caps;
932 }
933 
934 static const MXFEssenceElementHandler mxf_mpeg_essence_element_handler = {
935   mxf_is_mpeg_essence_track,
936   mxf_mpeg_get_track_wrapping,
937   mxf_mpeg_create_caps
938 };
939 
940 typedef struct
941 {
942   guint spf;
943   guint rate;
944 } MPEGAudioMappingData;
945 
946 static GstFlowReturn
mxf_mpeg_audio_write_func(GstBuffer * buffer,gpointer mapping_data,GstAdapter * adapter,GstBuffer ** outbuf,gboolean flush)947 mxf_mpeg_audio_write_func (GstBuffer * buffer,
948     gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
949     gboolean flush)
950 {
951   *outbuf = buffer;
952   return GST_FLOW_OK;
953 }
954 
955 static const guint8 mpeg_essence_container_ul[] = {
956   0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x02,
957   0x0d, 0x01, 0x03, 0x01, 0x02, 0x00, 0x00, 0x01
958 };
959 
960 static MXFMetadataFileDescriptor *
mxf_mpeg_audio_get_descriptor(GstPadTemplate * tmpl,GstCaps * caps,MXFEssenceElementWriteFunc * handler,gpointer * mapping_data)961 mxf_mpeg_audio_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
962     MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
963 {
964   MXFMetadataGenericSoundEssenceDescriptor *ret;
965   GstStructure *s;
966   MPEGAudioMappingData *md = g_new0 (MPEGAudioMappingData, 1);
967   gint rate;
968 
969   md->spf = -1;
970   *mapping_data = md;
971 
972   ret = (MXFMetadataGenericSoundEssenceDescriptor *)
973       g_object_new (MXF_TYPE_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR, NULL);
974 
975   s = gst_caps_get_structure (caps, 0);
976   if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
977     gint mpegversion;
978 
979     if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
980       GST_ERROR ("Invalid caps %" GST_PTR_FORMAT, caps);
981       g_object_unref (ret);
982       return NULL;
983     }
984 
985     if (mpegversion == 1) {
986       gint layer = 0;
987       gint mpegaudioversion = 0;
988 
989       gst_structure_get_int (s, "layer", &layer);
990       gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion);
991 
992       if (mpegaudioversion == 1 && layer == 1)
993         memcpy (&ret->sound_essence_compression,
994             &sound_essence_compression_mpeg1_layer1, 16);
995       else if (mpegaudioversion == 1 && (layer == 2 || layer == 3))
996         memcpy (&ret->sound_essence_compression,
997             &sound_essence_compression_mpeg1_layer23, 16);
998       else if (mpegaudioversion == 2 && layer == 1)
999         memcpy (&ret->sound_essence_compression,
1000             &sound_essence_compression_mpeg2_layer1, 16);
1001 
1002       if (layer == 1)
1003         md->spf = 384;
1004       else if (layer == 2 || mpegaudioversion == 1)
1005         md->spf = 1152;
1006       else
1007         md->spf = 576;          /* MPEG-2 or 2.5 */
1008 
1009       /* Otherwise all 0x00, must be some kind of mpeg1 audio */
1010     } else if (mpegversion == 2) {
1011       memcpy (&ret->sound_essence_compression, &sound_essence_compression_aac,
1012           16);
1013       md->spf = 1024;           /* FIXME: is this correct? */
1014     }
1015   } else if (strcmp (gst_structure_get_name (s), "audio/x-ac3") == 0) {
1016     memcpy (&ret->sound_essence_compression, &sound_essence_compression_ac3,
1017         16);
1018     md->spf = 256;              /* FIXME: is this correct? */
1019   } else {
1020     g_assert_not_reached ();
1021   }
1022 
1023   if (!gst_structure_get_int (s, "rate", &rate)) {
1024     GST_ERROR ("Invalid rate");
1025     g_object_unref (ret);
1026     return NULL;
1027   }
1028   md->rate = rate;
1029 
1030   memcpy (&ret->parent.essence_container, &mpeg_essence_container_ul, 16);
1031 
1032   ret->parent.essence_container.u[13] = 0x04;
1033   ret->parent.essence_container.u[14] = 0x40;
1034 
1035   if (!mxf_metadata_generic_sound_essence_descriptor_from_caps (ret, caps)) {
1036     g_object_unref (ret);
1037     return NULL;
1038   }
1039 
1040   *handler = mxf_mpeg_audio_write_func;
1041 
1042   return (MXFMetadataFileDescriptor *) ret;
1043 }
1044 
1045 static void
mxf_mpeg_audio_update_descriptor(MXFMetadataFileDescriptor * d,GstCaps * caps,gpointer mapping_data,GstBuffer * buf)1046 mxf_mpeg_audio_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
1047     gpointer mapping_data, GstBuffer * buf)
1048 {
1049   return;
1050 }
1051 
1052 static void
mxf_mpeg_audio_get_edit_rate(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data,GstBuffer * buf,MXFMetadataSourcePackage * package,MXFMetadataTimelineTrack * track,MXFFraction * edit_rate)1053 mxf_mpeg_audio_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
1054     gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
1055     MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
1056 {
1057   MPEGAudioMappingData *md = mapping_data;
1058 
1059   edit_rate->n = md->rate;
1060   edit_rate->d = md->spf;
1061 }
1062 
1063 static guint32
mxf_mpeg_audio_get_track_number_template(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data)1064 mxf_mpeg_audio_get_track_number_template (MXFMetadataFileDescriptor * a,
1065     GstCaps * caps, gpointer mapping_data)
1066 {
1067   return (0x16 << 24) | (0x05 << 8);
1068 }
1069 
1070 static MXFEssenceElementWriter mxf_mpeg_audio_essence_element_writer = {
1071   mxf_mpeg_audio_get_descriptor,
1072   mxf_mpeg_audio_update_descriptor,
1073   mxf_mpeg_audio_get_edit_rate,
1074   mxf_mpeg_audio_get_track_number_template,
1075   NULL,
1076   {{0,}}
1077 };
1078 
1079 #define MPEG_AUDIO_CAPS \
1080       "audio/mpeg, " \
1081       "mpegversion = (int) 1, " \
1082       "layer = (int) [ 1, 3 ], " \
1083       "rate = (int) [ 8000, 48000 ], " \
1084       "channels = (int) [ 1, 2 ], " \
1085       "parsed = (boolean) TRUE; " \
1086       "audio/x-ac3, " \
1087       "rate = (int) [ 4000, 96000 ], " \
1088       "channels = (int) [ 1, 6 ]; " \
1089       "audio/mpeg, " \
1090       "mpegversion = (int) 2, " \
1091       "rate = (int) [ 8000, 96000 ], " \
1092       "channels = (int) [ 1, 8 ]"
1093 
1094 /* See ISO/IEC 13818-2 for MPEG ES format */
1095 static gboolean
mxf_mpeg_is_mpeg2_frame(GstBuffer * buffer)1096 mxf_mpeg_is_mpeg2_frame (GstBuffer * buffer)
1097 {
1098   GstByteReader reader;
1099   guint32 tmp;
1100   GstMapInfo map;
1101   gboolean ret = FALSE;
1102 
1103   gst_buffer_map (buffer, &map, GST_MAP_READ);
1104   gst_byte_reader_init (&reader, map.data, map.size);
1105 
1106   while (gst_byte_reader_get_remaining (&reader) > 3) {
1107     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
1108       guint8 type = 0;
1109 
1110       /* Found sync code */
1111       gst_byte_reader_skip_unchecked (&reader, 3);
1112 
1113       if (!gst_byte_reader_get_uint8 (&reader, &type))
1114         break;
1115 
1116       /* PICTURE */
1117       if (type == 0x00) {
1118         ret = TRUE;
1119         goto done;
1120       }
1121     } else {
1122       if (gst_byte_reader_skip (&reader, 1) == FALSE)
1123         break;
1124     }
1125   }
1126 
1127 done:
1128   gst_buffer_unmap (buffer, &map);
1129 
1130   return ret;
1131 }
1132 
1133 static gboolean
mxf_mpeg_is_mpeg4_frame(GstBuffer * buffer)1134 mxf_mpeg_is_mpeg4_frame (GstBuffer * buffer)
1135 {
1136   GstByteReader reader;
1137   guint32 tmp;
1138   GstMapInfo map;
1139   gboolean ret = FALSE;
1140 
1141   gst_buffer_map (buffer, &map, GST_MAP_READ);
1142   gst_byte_reader_init (&reader, map.data, map.size);
1143 
1144   while (gst_byte_reader_get_remaining (&reader) > 3) {
1145     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
1146       guint8 type = 0;
1147 
1148       /* Found sync code */
1149       gst_byte_reader_skip_unchecked (&reader, 3);
1150 
1151       if (!gst_byte_reader_get_uint8 (&reader, &type))
1152         break;
1153 
1154       /* PICTURE */
1155       if (type == 0xb6) {
1156         ret = TRUE;
1157         goto done;
1158       }
1159     } else {
1160       if (gst_byte_reader_skip (&reader, 1) == FALSE)
1161         break;
1162     }
1163   }
1164 
1165 done:
1166   gst_buffer_unmap (buffer, &map);
1167 
1168   return ret;
1169 }
1170 
1171 static GstFlowReturn
mxf_mpeg_video_write_func(GstBuffer * buffer,gpointer mapping_data,GstAdapter * adapter,GstBuffer ** outbuf,gboolean flush)1172 mxf_mpeg_video_write_func (GstBuffer * buffer,
1173     gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
1174     gboolean flush)
1175 {
1176   MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_OTHER;
1177 
1178   if (mapping_data)
1179     type = *((MXFMPEGEssenceType *) mapping_data);
1180 
1181   if (type == MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2) {
1182     if (buffer && !mxf_mpeg_is_mpeg2_frame (buffer)) {
1183       gst_adapter_push (adapter, buffer);
1184       *outbuf = NULL;
1185       return GST_FLOW_OK;
1186     } else if (buffer || gst_adapter_available (adapter)) {
1187       guint av = gst_adapter_available (adapter);
1188       GstBuffer *ret;
1189       GstMapInfo map;
1190 
1191       if (buffer)
1192         ret = gst_buffer_new_and_alloc (gst_buffer_get_size (buffer) + av);
1193       else
1194         ret = gst_buffer_new_and_alloc (av);
1195 
1196       gst_buffer_map (ret, &map, GST_MAP_WRITE);
1197       if (av) {
1198         gconstpointer data = gst_adapter_map (adapter, av);
1199         memcpy (map.data, data, av);
1200         gst_adapter_unmap (adapter);
1201       }
1202 
1203       if (buffer) {
1204         GstMapInfo buffermap;
1205         gst_buffer_map (buffer, &buffermap, GST_MAP_READ);
1206         memcpy (map.data + av, buffermap.data, buffermap.size);
1207         gst_buffer_unmap (buffer, &buffermap);
1208         gst_buffer_unref (buffer);
1209       }
1210 
1211       gst_buffer_unmap (ret, &map);
1212 
1213       *outbuf = ret;
1214       return GST_FLOW_OK;
1215     }
1216   } else if (type == MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4) {
1217     if (buffer && !mxf_mpeg_is_mpeg4_frame (buffer)) {
1218       gst_adapter_push (adapter, buffer);
1219       *outbuf = NULL;
1220       return GST_FLOW_OK;
1221     } else if (buffer || gst_adapter_available (adapter)) {
1222       guint av = gst_adapter_available (adapter);
1223       GstBuffer *ret;
1224       GstMapInfo map;
1225 
1226       if (buffer)
1227         ret = gst_buffer_new_and_alloc (gst_buffer_get_size (buffer) + av);
1228       else
1229         ret = gst_buffer_new_and_alloc (av);
1230 
1231       gst_buffer_map (ret, &map, GST_MAP_WRITE);
1232       if (av) {
1233         gconstpointer data = gst_adapter_map (adapter, av);
1234         memcpy (map.data, data, av);
1235         gst_adapter_unmap (adapter);
1236       }
1237 
1238       if (buffer) {
1239         GstMapInfo buffermap;
1240         gst_buffer_map (buffer, &buffermap, GST_MAP_READ);
1241         memcpy (map.data + av, buffermap.data, buffermap.size);
1242         gst_buffer_unmap (buffer, &buffermap);
1243         gst_buffer_unref (buffer);
1244       }
1245 
1246       gst_buffer_unmap (ret, &map);
1247 
1248       *outbuf = ret;
1249       return GST_FLOW_OK;
1250     }
1251   }
1252 
1253   *outbuf = buffer;
1254   return GST_FLOW_OK;
1255 }
1256 
1257 static const guint8 mpeg_video_picture_essence_compression_ul[] = {
1258   0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x00,
1259   0x04, 0x01, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00
1260 };
1261 
1262 static MXFMetadataFileDescriptor *
mxf_mpeg_video_get_descriptor(GstPadTemplate * tmpl,GstCaps * caps,MXFEssenceElementWriteFunc * handler,gpointer * mapping_data)1263 mxf_mpeg_video_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
1264     MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
1265 {
1266   MXFMetadataMPEGVideoDescriptor *ret;
1267   GstStructure *s;
1268 
1269   ret = (MXFMetadataMPEGVideoDescriptor *)
1270       g_object_new (MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR, NULL);
1271 
1272   s = gst_caps_get_structure (caps, 0);
1273 
1274   memcpy (&ret->parent.parent.parent.essence_container,
1275       &mpeg_essence_container_ul, 16);
1276 
1277   memcpy (&ret->parent.parent.picture_essence_coding,
1278       &mpeg_video_picture_essence_compression_ul, 16);
1279   if (strcmp (gst_structure_get_name (s), "video/mpeg") == 0) {
1280     gint mpegversion;
1281 
1282     if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
1283       GST_ERROR ("Invalid caps %" GST_PTR_FORMAT, caps);
1284       g_object_unref (ret);
1285       return NULL;
1286     }
1287 
1288     if (mpegversion == 1) {
1289       MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
1290 
1291       *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1292       memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1293       ret->parent.parent.picture_essence_coding.u[7] = 0x03;
1294       ret->parent.parent.picture_essence_coding.u[13] = 0x10;
1295       ret->parent.parent.parent.essence_container.u[13] = 0x04;
1296       ret->parent.parent.parent.essence_container.u[14] = 0x60;
1297     } else if (mpegversion == 2) {
1298       MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
1299 
1300       *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1301       memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1302       ret->parent.parent.picture_essence_coding.u[7] = 0x01;
1303       ret->parent.parent.picture_essence_coding.u[13] = 0x01;
1304       ret->parent.parent.parent.essence_container.u[13] = 0x04;
1305       ret->parent.parent.parent.essence_container.u[14] = 0x60;
1306     } else {
1307       const GValue *v;
1308       const GstBuffer *codec_data;
1309       MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4;
1310 
1311       *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1312       memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1313 
1314       ret->parent.parent.picture_essence_coding.u[7] = 0x03;
1315       ret->parent.parent.picture_essence_coding.u[13] = 0x20;
1316       ret->parent.parent.parent.essence_container.u[13] = 0x04;
1317       ret->parent.parent.parent.essence_container.u[14] = 0x60;
1318       if ((v = gst_structure_get_value (s, "codec_data"))) {
1319         MXFLocalTag *t = g_slice_new0 (MXFLocalTag);
1320         GstMapInfo map;
1321 
1322         codec_data = gst_value_get_buffer (v);
1323         gst_buffer_map ((GstBuffer *) codec_data, &map, GST_MAP_READ);
1324         t->size = map.size;
1325         t->data = g_memdup (map.data, map.size);
1326         gst_buffer_unmap ((GstBuffer *) codec_data, &map);
1327         memcpy (&t->ul, &sony_mpeg4_extradata, 16);
1328         mxf_local_tag_insert (t, &MXF_METADATA_BASE (ret)->other_tags);
1329       }
1330     }
1331   } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
1332     MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC;
1333 
1334     *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1335     memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1336     ret->parent.parent.picture_essence_coding.u[7] = 0x0a;
1337     ret->parent.parent.picture_essence_coding.u[13] = 0x30;
1338     ret->parent.parent.parent.essence_container.u[7] = 0x0a;
1339     ret->parent.parent.parent.essence_container.u[13] = 0x10;
1340     ret->parent.parent.parent.essence_container.u[14] = 0x60;
1341   } else {
1342     g_assert_not_reached ();
1343   }
1344 
1345 
1346   if (!mxf_metadata_generic_picture_essence_descriptor_from_caps (&ret->parent.
1347           parent, caps)) {
1348     g_object_unref (ret);
1349     return NULL;
1350   }
1351 
1352   *handler = mxf_mpeg_video_write_func;
1353 
1354   return (MXFMetadataFileDescriptor *) ret;
1355 }
1356 
1357 static void
mxf_mpeg_video_update_descriptor(MXFMetadataFileDescriptor * d,GstCaps * caps,gpointer mapping_data,GstBuffer * buf)1358 mxf_mpeg_video_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
1359     gpointer mapping_data, GstBuffer * buf)
1360 {
1361   return;
1362 }
1363 
1364 static void
mxf_mpeg_video_get_edit_rate(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data,GstBuffer * buf,MXFMetadataSourcePackage * package,MXFMetadataTimelineTrack * track,MXFFraction * edit_rate)1365 mxf_mpeg_video_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
1366     gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
1367     MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
1368 {
1369   (*edit_rate).n = a->sample_rate.n;
1370   (*edit_rate).d = a->sample_rate.d;
1371 }
1372 
1373 static guint32
mxf_mpeg_video_get_track_number_template(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data)1374 mxf_mpeg_video_get_track_number_template (MXFMetadataFileDescriptor * a,
1375     GstCaps * caps, gpointer mapping_data)
1376 {
1377   return (0x15 << 24) | (0x05 << 8);
1378 }
1379 
1380 static MXFEssenceElementWriter mxf_mpeg_video_essence_element_writer = {
1381   mxf_mpeg_video_get_descriptor,
1382   mxf_mpeg_video_update_descriptor,
1383   mxf_mpeg_video_get_edit_rate,
1384   mxf_mpeg_video_get_track_number_template,
1385   NULL,
1386   {{0,}}
1387 };
1388 
1389 #define MPEG_VIDEO_CAPS \
1390 "video/mpeg, " \
1391 "mpegversion = (int) { 1, 2, 4 }, " \
1392 "systemstream = (boolean) FALSE, " \
1393 "width = " GST_VIDEO_SIZE_RANGE ", " \
1394 "height = " GST_VIDEO_SIZE_RANGE ", " \
1395 "framerate = " GST_VIDEO_FPS_RANGE "; " \
1396 "video/x-h264, " \
1397 "stream-format = (string) byte-stream, " \
1398 "width = " GST_VIDEO_SIZE_RANGE ", " \
1399 "height = " GST_VIDEO_SIZE_RANGE ", " \
1400 "framerate = " GST_VIDEO_FPS_RANGE
1401 
1402 void
mxf_mpeg_init(void)1403 mxf_mpeg_init (void)
1404 {
1405   mxf_metadata_register (MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR);
1406   mxf_essence_element_handler_register (&mxf_mpeg_essence_element_handler);
1407 
1408   mxf_mpeg_audio_essence_element_writer.pad_template =
1409       gst_pad_template_new ("mpeg_audio_sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
1410       gst_caps_from_string (MPEG_AUDIO_CAPS));
1411   memcpy (&mxf_mpeg_audio_essence_element_writer.data_definition,
1412       mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_SOUND_ESSENCE), 16);
1413   mxf_essence_element_writer_register (&mxf_mpeg_audio_essence_element_writer);
1414 
1415   mxf_mpeg_video_essence_element_writer.pad_template =
1416       gst_pad_template_new ("mpeg_video_sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
1417       gst_caps_from_string (MPEG_VIDEO_CAPS));
1418   memcpy (&mxf_mpeg_video_essence_element_writer.data_definition,
1419       mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_PICTURE_ESSENCE),
1420       16);
1421   mxf_essence_element_writer_register (&mxf_mpeg_video_essence_element_writer);
1422 
1423 }
1424