• 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 video_mpeg_compression = { {
594         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x01, 0x02, 0x02,
595     0x01,}
596 };
597 
598 static const MXFUL sound_essence_compression_ac3 = { {
599         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
600     0x03, 0x02, 0x01, 0x00}
601 };
602 
603 static const MXFUL sound_essence_compression_mpeg1_layer1 = { {
604         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
605     0x03, 0x02, 0x04, 0x00}
606 };
607 
608 static const MXFUL sound_essence_compression_mpeg1_layer23 = { {
609         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
610     0x03, 0x02, 0x05, 0x00}
611 };
612 
613 static const MXFUL sound_essence_compression_mpeg1_layer2 = { {
614         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x08, 0x04, 0x02, 0x02, 0x02,
615     0x03, 0x02, 0x05, 0x01}
616 };
617 
618 static const MXFUL sound_essence_compression_mpeg2_layer1 = { {
619         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
620     0x03, 0x02, 0x06, 0x00}
621 };
622 
623 static const MXFUL sound_essence_compression_dts = { {
624         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x02, 0x02, 0x02,
625     0x03, 0x02, 0x1c, 0x00}
626 };
627 
628 static const MXFUL sound_essence_compression_aac = { {
629         0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x03, 0x04, 0x02, 0x02, 0x02,
630     0x03, 0x03, 0x01, 0x00}
631 };
632 
633 static GstCaps *
mxf_mpeg_es_create_caps(MXFMetadataTimelineTrack * track,GstTagList ** tags,gboolean * intra_only,MXFEssenceElementHandleFunc * handler,gpointer * mapping_data,MXFMetadataGenericPictureEssenceDescriptor * p,MXFMetadataGenericSoundEssenceDescriptor * s)634 mxf_mpeg_es_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
635     gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
636     gpointer * mapping_data, MXFMetadataGenericPictureEssenceDescriptor * p,
637     MXFMetadataGenericSoundEssenceDescriptor * s)
638 {
639   GstCaps *caps = NULL;
640   const gchar *codec_name = NULL;
641   MXFMPEGEssenceType t, *mdata;
642   gchar str[48];
643 
644   *mapping_data = g_malloc (sizeof (MXFMPEGEssenceType));
645   mdata = (MXFMPEGEssenceType *) * mapping_data;
646 
647   /* SMPTE RP224 */
648   if (p) {
649     if (mxf_ul_is_zero (&p->picture_essence_coding) ||
650         mxf_ul_is_equal (&p->picture_essence_coding,
651             &p->parent.essence_container)) {
652       GST_WARNING ("No picture essence coding defined, assuming MPEG2");
653       caps =
654           gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 2,
655           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
656       codec_name = "MPEG-2 Video";
657       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
658       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
659       *intra_only = FALSE;
660     } else if (!mxf_ul_is_subclass (&video_mpeg_compression,
661             &p->picture_essence_coding)) {
662       GST_ERROR ("Not MPEG picture essence coding %s",
663           mxf_ul_to_string (&p->picture_essence_coding, str));
664       caps = NULL;
665     } else if (p->picture_essence_coding.u[13] >= 0x01 &&
666         p->picture_essence_coding.u[13] <= 0x08) {
667       caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 2,
668           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
669       codec_name = "MPEG-2 Video";
670       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
671       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
672       *intra_only = FALSE;
673     } else if (p->picture_essence_coding.u[13] == 0x10) {
674       caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 1,
675           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
676       codec_name = "MPEG-1 Video";
677       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
678       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
679       *intra_only = TRUE;
680     } else if (p->picture_essence_coding.u[13] == 0x20) {
681       MXFLocalTag *local_tag =
682           (((MXFMetadataBase *) p)->other_tags) ?
683           g_hash_table_lookup (((MXFMetadataBase *)
684               p)->other_tags, &sony_mpeg4_extradata) : NULL;
685 
686       caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 4,
687           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
688 
689       if (local_tag) {
690         GstMapInfo map;
691         GstBuffer *codec_data = NULL;
692         codec_data = gst_buffer_new_and_alloc (local_tag->size);
693         gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
694         memcpy (map.data, local_tag->data, local_tag->size);
695         gst_buffer_unmap (codec_data, &map);
696         gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, codec_data,
697             NULL);
698         gst_buffer_unref (codec_data);
699       }
700       codec_name = "MPEG-4 Video";
701       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4;
702       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
703       *intra_only = FALSE;
704     } else if ((p->picture_essence_coding.u[13] >> 4) == 0x03) {
705       /* RP 2008 */
706 
707       caps =
708           gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,
709           "byte-stream", NULL);
710       codec_name = "h.264 Video";
711       t = MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC;
712       memcpy (mdata, &t, sizeof (MXFMPEGEssenceType));
713       *intra_only = FALSE;
714     } else {
715       GST_ERROR ("Unsupported MPEG picture essence coding 0x%02x",
716           p->picture_essence_coding.u[13]);
717       caps = NULL;
718     }
719     if (caps)
720       *handler = mxf_mpeg_video_handle_essence_element;
721   } else if (s) {
722     if (mxf_ul_is_zero (&s->sound_essence_compression)) {
723       GST_WARNING ("Zero sound essence compression, assuming MPEG1 audio");
724       caps =
725           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
726           NULL);
727       codec_name = "MPEG-1 Audio";
728     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
729             &sound_essence_compression_ac3)) {
730       caps = gst_caps_new_empty_simple ("audio/x-ac3");
731       codec_name = "AC3 Audio";
732     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
733             &sound_essence_compression_mpeg1_layer1)) {
734       caps =
735           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
736           "layer", G_TYPE_INT, 1, NULL);
737       codec_name = "MPEG-1 Layer 1 Audio";
738     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
739             &sound_essence_compression_mpeg1_layer23)) {
740       caps =
741           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
742           NULL);
743       codec_name = "MPEG-1 Audio";
744     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
745             &sound_essence_compression_mpeg1_layer2)) {
746       caps =
747           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
748           "layer", G_TYPE_INT, 2, NULL);
749       codec_name = "MPEG-1 Layer 2 Audio";
750     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
751             &sound_essence_compression_mpeg2_layer1)) {
752       caps =
753           gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
754           "layer", G_TYPE_INT, 1, "mpegaudioversion", G_TYPE_INT, 2, NULL);
755       codec_name = "MPEG-2 Layer 1 Audio";
756     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
757             &sound_essence_compression_dts)) {
758       caps = gst_caps_new_empty_simple ("audio/x-dts");
759       codec_name = "Dolby DTS Audio";
760     } else if (mxf_ul_is_equal (&s->sound_essence_compression,
761             &sound_essence_compression_aac)) {
762       caps = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
763           2, NULL);
764       codec_name = "MPEG-2 AAC Audio";
765     }
766 
767     if (caps) {
768       mxf_metadata_generic_sound_essence_descriptor_set_caps (s, caps);
769       *handler = mxf_mpeg_audio_handle_essence_element;
770     }
771     *intra_only = TRUE;
772   }
773 
774   if (caps) {
775     if (!*tags)
776       *tags = gst_tag_list_new_empty ();
777     if (codec_name)
778       gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
779           codec_name, NULL);
780 
781     if (p && MXF_IS_METADATA_MPEG_VIDEO_DESCRIPTOR (p)
782         && MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (p)->bitrate) {
783       gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_BITRATE,
784           MXF_METADATA_MPEG_VIDEO_DESCRIPTOR (p)->bitrate, NULL);
785     }
786   }
787 
788   return caps;
789 }
790 
791 static MXFEssenceWrapping
mxf_mpeg_get_track_wrapping(const MXFMetadataTimelineTrack * track)792 mxf_mpeg_get_track_wrapping (const MXFMetadataTimelineTrack * track)
793 {
794   guint i;
795 
796   g_return_val_if_fail (track != NULL, MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING);
797 
798   if (track->parent.descriptor == NULL) {
799     GST_ERROR ("No descriptor found for this track");
800     return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
801   }
802 
803   for (i = 0; i < track->parent.n_descriptor; i++) {
804     if (!track->parent.descriptor[i])
805       continue;
806 
807     if (!MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
808             parent.descriptor[i])
809         && !MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->
810             parent.descriptor[i]))
811       continue;
812 
813     switch (track->parent.descriptor[i]->essence_container.u[15]) {
814       case 0x01:
815         return MXF_ESSENCE_WRAPPING_FRAME_WRAPPING;
816         break;
817       case 0x02:
818         return MXF_ESSENCE_WRAPPING_CLIP_WRAPPING;
819         break;
820       default:
821         return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
822         break;
823     }
824   }
825 
826   return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
827 }
828 
829 static GstCaps *
mxf_mpeg_create_caps(MXFMetadataTimelineTrack * track,GstTagList ** tags,gboolean * intra_only,MXFEssenceElementHandleFunc * handler,gpointer * mapping_data)830 mxf_mpeg_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
831     gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
832     gpointer * mapping_data)
833 {
834   MXFMetadataFileDescriptor *f = NULL;
835   MXFMetadataGenericPictureEssenceDescriptor *p = NULL;
836   MXFMetadataGenericSoundEssenceDescriptor *s = NULL;
837   guint i;
838   GstCaps *caps = NULL;
839 
840   g_return_val_if_fail (track != NULL, NULL);
841 
842   if (track->parent.descriptor == NULL) {
843     GST_ERROR ("No descriptor found for this track");
844     return NULL;
845   }
846 
847   for (i = 0; i < track->parent.n_descriptor; i++) {
848     if (!track->parent.descriptor[i])
849       continue;
850 
851     if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
852             parent.descriptor[i])) {
853       f = track->parent.descriptor[i];
854       p = (MXFMetadataGenericPictureEssenceDescriptor *) track->parent.
855           descriptor[i];
856       break;
857     } else if (MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->
858             parent.descriptor[i])) {
859       f = track->parent.descriptor[i];
860       s = (MXFMetadataGenericSoundEssenceDescriptor *) track->parent.
861           descriptor[i];
862       break;
863     }
864   }
865 
866   if (!f) {
867     GST_ERROR ("No descriptor found for this track");
868     return NULL;
869   }
870 
871   /* SMPTE 381M 7 */
872   if (f->essence_container.u[13] == 0x04) {
873     GST_DEBUG ("Found MPEG ES stream");
874 
875     caps =
876         mxf_mpeg_es_create_caps (track, tags, intra_only, handler, mapping_data,
877         p, s);
878   } else if (f->essence_container.u[13] == 0x07) {
879     GST_ERROR ("MPEG PES streams not supported yet");
880     return NULL;
881   } else if (f->essence_container.u[13] == 0x08) {
882     /* FIXME: get mpeg version somehow */
883     GST_DEBUG ("Found MPEG PS stream");
884     caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 1,
885         "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
886 
887     if (!*tags)
888       *tags = gst_tag_list_new_empty ();
889     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
890         "MPEG PS", NULL);
891     *intra_only = FALSE;
892   } else if (f->essence_container.u[13] == 0x09) {
893     GST_DEBUG ("Found MPEG TS stream");
894     caps = gst_caps_new_empty_simple ("video/mpegts");
895 
896     if (!*tags)
897       *tags = gst_tag_list_new_empty ();
898     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
899         "MPEG TS", NULL);
900     *intra_only = FALSE;
901   } else if (f->essence_container.u[13] == 0x0f) {
902     GST_DEBUG ("Found h264 NAL unit stream");
903     /* RP 2008 */
904     caps =
905         gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,
906         "byte-stream", NULL);
907 
908     if (!*tags)
909       *tags = gst_tag_list_new_empty ();
910     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
911         "h.264 Video", NULL);
912     *intra_only = FALSE;
913   } else if (f->essence_container.u[13] == 0x10) {
914     GST_DEBUG ("Found h264 byte-stream stream");
915     /* RP 2008 */
916     caps =
917         gst_caps_new_simple ("video/x-h264", "stream-format", G_TYPE_STRING,
918         "byte-stream", NULL);
919 
920     if (!*tags)
921       *tags = gst_tag_list_new_empty ();
922     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
923         "h.264 Video", NULL);
924     *intra_only = FALSE;
925   }
926 
927   if (p && caps)
928     mxf_metadata_generic_picture_essence_descriptor_set_caps (p, caps);
929 
930   return caps;
931 }
932 
933 static const MXFEssenceElementHandler mxf_mpeg_essence_element_handler = {
934   mxf_is_mpeg_essence_track,
935   mxf_mpeg_get_track_wrapping,
936   mxf_mpeg_create_caps
937 };
938 
939 typedef struct
940 {
941   guint spf;
942   guint rate;
943 } MPEGAudioMappingData;
944 
945 static GstFlowReturn
mxf_mpeg_audio_write_func(GstBuffer * buffer,gpointer mapping_data,GstAdapter * adapter,GstBuffer ** outbuf,gboolean flush)946 mxf_mpeg_audio_write_func (GstBuffer * buffer,
947     gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
948     gboolean flush)
949 {
950   *outbuf = buffer;
951   return GST_FLOW_OK;
952 }
953 
954 static const guint8 mpeg_essence_container_ul[] = {
955   0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x02,
956   0x0d, 0x01, 0x03, 0x01, 0x02, 0x00, 0x00, 0x01
957 };
958 
959 static MXFMetadataFileDescriptor *
mxf_mpeg_audio_get_descriptor(GstPadTemplate * tmpl,GstCaps * caps,MXFEssenceElementWriteFunc * handler,gpointer * mapping_data)960 mxf_mpeg_audio_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
961     MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
962 {
963   MXFMetadataGenericSoundEssenceDescriptor *ret;
964   GstStructure *s;
965   MPEGAudioMappingData *md = g_new0 (MPEGAudioMappingData, 1);
966   gint rate;
967 
968   md->spf = -1;
969   *mapping_data = md;
970 
971   ret = (MXFMetadataGenericSoundEssenceDescriptor *)
972       g_object_new (MXF_TYPE_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR, NULL);
973 
974   s = gst_caps_get_structure (caps, 0);
975   if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
976     gint mpegversion;
977 
978     if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
979       GST_ERROR ("Invalid caps %" GST_PTR_FORMAT, caps);
980       g_object_unref (ret);
981       return NULL;
982     }
983 
984     if (mpegversion == 1) {
985       gint layer = 0;
986       gint mpegaudioversion = 0;
987 
988       gst_structure_get_int (s, "layer", &layer);
989       gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion);
990 
991       if (mpegaudioversion == 1 && layer == 1)
992         memcpy (&ret->sound_essence_compression,
993             &sound_essence_compression_mpeg1_layer1, 16);
994       else if (mpegaudioversion == 1 && (layer == 2 || layer == 3))
995         memcpy (&ret->sound_essence_compression,
996             &sound_essence_compression_mpeg1_layer23, 16);
997       else if (mpegaudioversion == 2 && layer == 1)
998         memcpy (&ret->sound_essence_compression,
999             &sound_essence_compression_mpeg2_layer1, 16);
1000 
1001       if (layer == 1)
1002         md->spf = 384;
1003       else if (layer == 2 || mpegaudioversion == 1)
1004         md->spf = 1152;
1005       else
1006         md->spf = 576;          /* MPEG-2 or 2.5 */
1007 
1008       /* Otherwise all 0x00, must be some kind of mpeg1 audio */
1009     } else if (mpegversion == 2) {
1010       memcpy (&ret->sound_essence_compression, &sound_essence_compression_aac,
1011           16);
1012       md->spf = 1024;           /* FIXME: is this correct? */
1013     }
1014   } else if (strcmp (gst_structure_get_name (s), "audio/x-ac3") == 0) {
1015     memcpy (&ret->sound_essence_compression, &sound_essence_compression_ac3,
1016         16);
1017     md->spf = 256;              /* FIXME: is this correct? */
1018   } else {
1019     g_assert_not_reached ();
1020   }
1021 
1022   if (!gst_structure_get_int (s, "rate", &rate)) {
1023     GST_ERROR ("Invalid rate");
1024     g_object_unref (ret);
1025     return NULL;
1026   }
1027   md->rate = rate;
1028 
1029   memcpy (&ret->parent.essence_container, &mpeg_essence_container_ul, 16);
1030 
1031   ret->parent.essence_container.u[13] = 0x04;
1032   ret->parent.essence_container.u[14] = 0x40;
1033 
1034   if (!mxf_metadata_generic_sound_essence_descriptor_from_caps (ret, caps)) {
1035     g_object_unref (ret);
1036     return NULL;
1037   }
1038 
1039   *handler = mxf_mpeg_audio_write_func;
1040 
1041   return (MXFMetadataFileDescriptor *) ret;
1042 }
1043 
1044 static void
mxf_mpeg_audio_update_descriptor(MXFMetadataFileDescriptor * d,GstCaps * caps,gpointer mapping_data,GstBuffer * buf)1045 mxf_mpeg_audio_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
1046     gpointer mapping_data, GstBuffer * buf)
1047 {
1048   return;
1049 }
1050 
1051 static void
mxf_mpeg_audio_get_edit_rate(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data,GstBuffer * buf,MXFMetadataSourcePackage * package,MXFMetadataTimelineTrack * track,MXFFraction * edit_rate)1052 mxf_mpeg_audio_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
1053     gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
1054     MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
1055 {
1056   MPEGAudioMappingData *md = mapping_data;
1057 
1058   edit_rate->n = md->rate;
1059   edit_rate->d = md->spf;
1060 }
1061 
1062 static guint32
mxf_mpeg_audio_get_track_number_template(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data)1063 mxf_mpeg_audio_get_track_number_template (MXFMetadataFileDescriptor * a,
1064     GstCaps * caps, gpointer mapping_data)
1065 {
1066   return (0x16 << 24) | (0x05 << 8);
1067 }
1068 
1069 static MXFEssenceElementWriter mxf_mpeg_audio_essence_element_writer = {
1070   mxf_mpeg_audio_get_descriptor,
1071   mxf_mpeg_audio_update_descriptor,
1072   mxf_mpeg_audio_get_edit_rate,
1073   mxf_mpeg_audio_get_track_number_template,
1074   NULL,
1075   {{0,}}
1076 };
1077 
1078 #define MPEG_AUDIO_CAPS \
1079       "audio/mpeg, " \
1080       "mpegversion = (int) 1, " \
1081       "layer = (int) [ 1, 3 ], " \
1082       "rate = (int) [ 8000, 48000 ], " \
1083       "channels = (int) [ 1, 2 ], " \
1084       "parsed = (boolean) TRUE; " \
1085       "audio/x-ac3, " \
1086       "rate = (int) [ 4000, 96000 ], " \
1087       "channels = (int) [ 1, 6 ]; " \
1088       "audio/mpeg, " \
1089       "mpegversion = (int) 2, " \
1090       "rate = (int) [ 8000, 96000 ], " \
1091       "channels = (int) [ 1, 8 ]"
1092 
1093 /* See ISO/IEC 13818-2 for MPEG ES format */
1094 static gboolean
mxf_mpeg_is_mpeg2_frame(GstBuffer * buffer)1095 mxf_mpeg_is_mpeg2_frame (GstBuffer * buffer)
1096 {
1097   GstByteReader reader;
1098   guint32 tmp;
1099   GstMapInfo map;
1100   gboolean ret = FALSE;
1101 
1102   gst_buffer_map (buffer, &map, GST_MAP_READ);
1103   gst_byte_reader_init (&reader, map.data, map.size);
1104 
1105   while (gst_byte_reader_get_remaining (&reader) > 3) {
1106     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
1107       guint8 type = 0;
1108 
1109       /* Found sync code */
1110       gst_byte_reader_skip_unchecked (&reader, 3);
1111 
1112       if (!gst_byte_reader_get_uint8 (&reader, &type))
1113         break;
1114 
1115       /* PICTURE */
1116       if (type == 0x00) {
1117         ret = TRUE;
1118         goto done;
1119       }
1120     } else {
1121       if (gst_byte_reader_skip (&reader, 1) == FALSE)
1122         break;
1123     }
1124   }
1125 
1126 done:
1127   gst_buffer_unmap (buffer, &map);
1128 
1129   return ret;
1130 }
1131 
1132 static gboolean
mxf_mpeg_is_mpeg4_frame(GstBuffer * buffer)1133 mxf_mpeg_is_mpeg4_frame (GstBuffer * buffer)
1134 {
1135   GstByteReader reader;
1136   guint32 tmp;
1137   GstMapInfo map;
1138   gboolean ret = FALSE;
1139 
1140   gst_buffer_map (buffer, &map, GST_MAP_READ);
1141   gst_byte_reader_init (&reader, map.data, map.size);
1142 
1143   while (gst_byte_reader_get_remaining (&reader) > 3) {
1144     if (gst_byte_reader_peek_uint24_be (&reader, &tmp) && tmp == 0x000001) {
1145       guint8 type = 0;
1146 
1147       /* Found sync code */
1148       gst_byte_reader_skip_unchecked (&reader, 3);
1149 
1150       if (!gst_byte_reader_get_uint8 (&reader, &type))
1151         break;
1152 
1153       /* PICTURE */
1154       if (type == 0xb6) {
1155         ret = TRUE;
1156         goto done;
1157       }
1158     } else {
1159       if (gst_byte_reader_skip (&reader, 1) == FALSE)
1160         break;
1161     }
1162   }
1163 
1164 done:
1165   gst_buffer_unmap (buffer, &map);
1166 
1167   return ret;
1168 }
1169 
1170 static GstFlowReturn
mxf_mpeg_video_write_func(GstBuffer * buffer,gpointer mapping_data,GstAdapter * adapter,GstBuffer ** outbuf,gboolean flush)1171 mxf_mpeg_video_write_func (GstBuffer * buffer,
1172     gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
1173     gboolean flush)
1174 {
1175   MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_OTHER;
1176 
1177   if (mapping_data)
1178     type = *((MXFMPEGEssenceType *) mapping_data);
1179 
1180   if (type == MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2) {
1181     if (buffer && !mxf_mpeg_is_mpeg2_frame (buffer)) {
1182       gst_adapter_push (adapter, buffer);
1183       *outbuf = NULL;
1184       return GST_FLOW_OK;
1185     } else if (buffer || gst_adapter_available (adapter)) {
1186       guint av = gst_adapter_available (adapter);
1187       GstBuffer *ret;
1188       GstMapInfo map;
1189 
1190       if (buffer)
1191         ret = gst_buffer_new_and_alloc (gst_buffer_get_size (buffer) + av);
1192       else
1193         ret = gst_buffer_new_and_alloc (av);
1194 
1195       gst_buffer_map (ret, &map, GST_MAP_WRITE);
1196       if (av) {
1197         gconstpointer data = gst_adapter_map (adapter, av);
1198         memcpy (map.data, data, av);
1199         gst_adapter_unmap (adapter);
1200       }
1201 
1202       if (buffer) {
1203         GstMapInfo buffermap;
1204         gst_buffer_map (buffer, &buffermap, GST_MAP_READ);
1205         memcpy (map.data + av, buffermap.data, buffermap.size);
1206         gst_buffer_unmap (buffer, &buffermap);
1207         gst_buffer_unref (buffer);
1208       }
1209 
1210       gst_buffer_unmap (ret, &map);
1211 
1212       *outbuf = ret;
1213       return GST_FLOW_OK;
1214     }
1215   } else if (type == MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4) {
1216     if (buffer && !mxf_mpeg_is_mpeg4_frame (buffer)) {
1217       gst_adapter_push (adapter, buffer);
1218       *outbuf = NULL;
1219       return GST_FLOW_OK;
1220     } else if (buffer || gst_adapter_available (adapter)) {
1221       guint av = gst_adapter_available (adapter);
1222       GstBuffer *ret;
1223       GstMapInfo map;
1224 
1225       if (buffer)
1226         ret = gst_buffer_new_and_alloc (gst_buffer_get_size (buffer) + av);
1227       else
1228         ret = gst_buffer_new_and_alloc (av);
1229 
1230       gst_buffer_map (ret, &map, GST_MAP_WRITE);
1231       if (av) {
1232         gconstpointer data = gst_adapter_map (adapter, av);
1233         memcpy (map.data, data, av);
1234         gst_adapter_unmap (adapter);
1235       }
1236 
1237       if (buffer) {
1238         GstMapInfo buffermap;
1239         gst_buffer_map (buffer, &buffermap, GST_MAP_READ);
1240         memcpy (map.data + av, buffermap.data, buffermap.size);
1241         gst_buffer_unmap (buffer, &buffermap);
1242         gst_buffer_unref (buffer);
1243       }
1244 
1245       gst_buffer_unmap (ret, &map);
1246 
1247       *outbuf = ret;
1248       return GST_FLOW_OK;
1249     }
1250   }
1251 
1252   *outbuf = buffer;
1253   return GST_FLOW_OK;
1254 }
1255 
1256 static const guint8 mpeg_video_picture_essence_compression_ul[] = {
1257   0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x00,
1258   0x04, 0x01, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00
1259 };
1260 
1261 static MXFMetadataFileDescriptor *
mxf_mpeg_video_get_descriptor(GstPadTemplate * tmpl,GstCaps * caps,MXFEssenceElementWriteFunc * handler,gpointer * mapping_data)1262 mxf_mpeg_video_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
1263     MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
1264 {
1265   MXFMetadataMPEGVideoDescriptor *ret;
1266   GstStructure *s;
1267 
1268   ret = (MXFMetadataMPEGVideoDescriptor *)
1269       g_object_new (MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR, NULL);
1270 
1271   s = gst_caps_get_structure (caps, 0);
1272 
1273   memcpy (&ret->parent.parent.parent.essence_container,
1274       &mpeg_essence_container_ul, 16);
1275 
1276   memcpy (&ret->parent.parent.picture_essence_coding,
1277       &mpeg_video_picture_essence_compression_ul, 16);
1278   if (strcmp (gst_structure_get_name (s), "video/mpeg") == 0) {
1279     gint mpegversion;
1280 
1281     if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
1282       GST_ERROR ("Invalid caps %" GST_PTR_FORMAT, caps);
1283       g_object_unref (ret);
1284       return NULL;
1285     }
1286 
1287     if (mpegversion == 1) {
1288       MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
1289 
1290       *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1291       memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1292       ret->parent.parent.picture_essence_coding.u[7] = 0x03;
1293       ret->parent.parent.picture_essence_coding.u[13] = 0x10;
1294       ret->parent.parent.parent.essence_container.u[13] = 0x04;
1295       ret->parent.parent.parent.essence_container.u[14] = 0x60;
1296     } else if (mpegversion == 2) {
1297       MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG2;
1298 
1299       *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1300       memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1301       ret->parent.parent.picture_essence_coding.u[7] = 0x01;
1302       ret->parent.parent.picture_essence_coding.u[13] = 0x01;
1303       ret->parent.parent.parent.essence_container.u[13] = 0x04;
1304       ret->parent.parent.parent.essence_container.u[14] = 0x60;
1305     } else {
1306       const GValue *v;
1307       const GstBuffer *codec_data;
1308       MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_MPEG4;
1309 
1310       *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1311       memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1312 
1313       ret->parent.parent.picture_essence_coding.u[7] = 0x03;
1314       ret->parent.parent.picture_essence_coding.u[13] = 0x20;
1315       ret->parent.parent.parent.essence_container.u[13] = 0x04;
1316       ret->parent.parent.parent.essence_container.u[14] = 0x60;
1317       if ((v = gst_structure_get_value (s, "codec_data"))) {
1318         MXFLocalTag *t = g_slice_new0 (MXFLocalTag);
1319         GstMapInfo map;
1320 
1321         codec_data = gst_value_get_buffer (v);
1322         gst_buffer_map ((GstBuffer *) codec_data, &map, GST_MAP_READ);
1323         t->size = map.size;
1324         t->data = g_memdup2 (map.data, map.size);
1325         gst_buffer_unmap ((GstBuffer *) codec_data, &map);
1326         memcpy (&t->ul, &sony_mpeg4_extradata, 16);
1327         mxf_local_tag_insert (t, &MXF_METADATA_BASE (ret)->other_tags);
1328       }
1329     }
1330   } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
1331     MXFMPEGEssenceType type = MXF_MPEG_ESSENCE_TYPE_VIDEO_AVC;
1332 
1333     *mapping_data = g_new0 (MXFMPEGEssenceType, 1);
1334     memcpy (*mapping_data, &type, sizeof (MXFMPEGEssenceType));
1335     ret->parent.parent.picture_essence_coding.u[7] = 0x0a;
1336     ret->parent.parent.picture_essence_coding.u[13] = 0x30;
1337     ret->parent.parent.parent.essence_container.u[7] = 0x0a;
1338     ret->parent.parent.parent.essence_container.u[13] = 0x10;
1339     ret->parent.parent.parent.essence_container.u[14] = 0x60;
1340   } else {
1341     g_assert_not_reached ();
1342   }
1343 
1344 
1345   if (!mxf_metadata_generic_picture_essence_descriptor_from_caps (&ret->parent.
1346           parent, caps)) {
1347     g_object_unref (ret);
1348     return NULL;
1349   }
1350 
1351   *handler = mxf_mpeg_video_write_func;
1352 
1353   return (MXFMetadataFileDescriptor *) ret;
1354 }
1355 
1356 static void
mxf_mpeg_video_update_descriptor(MXFMetadataFileDescriptor * d,GstCaps * caps,gpointer mapping_data,GstBuffer * buf)1357 mxf_mpeg_video_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
1358     gpointer mapping_data, GstBuffer * buf)
1359 {
1360   return;
1361 }
1362 
1363 static void
mxf_mpeg_video_get_edit_rate(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data,GstBuffer * buf,MXFMetadataSourcePackage * package,MXFMetadataTimelineTrack * track,MXFFraction * edit_rate)1364 mxf_mpeg_video_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
1365     gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
1366     MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
1367 {
1368   (*edit_rate).n = a->sample_rate.n;
1369   (*edit_rate).d = a->sample_rate.d;
1370 }
1371 
1372 static guint32
mxf_mpeg_video_get_track_number_template(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data)1373 mxf_mpeg_video_get_track_number_template (MXFMetadataFileDescriptor * a,
1374     GstCaps * caps, gpointer mapping_data)
1375 {
1376   return (0x15 << 24) | (0x05 << 8);
1377 }
1378 
1379 static MXFEssenceElementWriter mxf_mpeg_video_essence_element_writer = {
1380   mxf_mpeg_video_get_descriptor,
1381   mxf_mpeg_video_update_descriptor,
1382   mxf_mpeg_video_get_edit_rate,
1383   mxf_mpeg_video_get_track_number_template,
1384   NULL,
1385   {{0,}}
1386 };
1387 
1388 #define MPEG_VIDEO_CAPS \
1389 "video/mpeg, " \
1390 "mpegversion = (int) { 1, 2, 4 }, " \
1391 "systemstream = (boolean) FALSE, " \
1392 "width = " GST_VIDEO_SIZE_RANGE ", " \
1393 "height = " GST_VIDEO_SIZE_RANGE ", " \
1394 "framerate = " GST_VIDEO_FPS_RANGE "; " \
1395 "video/x-h264, " \
1396 "stream-format = (string) byte-stream, " \
1397 "width = " GST_VIDEO_SIZE_RANGE ", " \
1398 "height = " GST_VIDEO_SIZE_RANGE ", " \
1399 "framerate = " GST_VIDEO_FPS_RANGE
1400 
1401 void
mxf_mpeg_init(void)1402 mxf_mpeg_init (void)
1403 {
1404   mxf_metadata_register (MXF_TYPE_METADATA_MPEG_VIDEO_DESCRIPTOR);
1405   mxf_essence_element_handler_register (&mxf_mpeg_essence_element_handler);
1406 
1407   mxf_mpeg_audio_essence_element_writer.pad_template =
1408       gst_pad_template_new ("mpeg_audio_sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
1409       gst_caps_from_string (MPEG_AUDIO_CAPS));
1410   memcpy (&mxf_mpeg_audio_essence_element_writer.data_definition,
1411       mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_SOUND_ESSENCE), 16);
1412   mxf_essence_element_writer_register (&mxf_mpeg_audio_essence_element_writer);
1413 
1414   mxf_mpeg_video_essence_element_writer.pad_template =
1415       gst_pad_template_new ("mpeg_video_sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
1416       gst_caps_from_string (MPEG_VIDEO_CAPS));
1417   memcpy (&mxf_mpeg_video_essence_element_writer.data_definition,
1418       mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_PICTURE_ESSENCE),
1419       16);
1420   mxf_essence_element_writer_register (&mxf_mpeg_video_essence_element_writer);
1421 
1422 }
1423