codecparser: mpeg4 type error
[profile/ivi/gst-plugins-bad.git] / gst / mxf / mxfd10.c
1 /* GStreamer
2  * Copyright (C) 2008-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /* Implementation of SMPTE 386M - Mapping Type-D10 essence data into the MXF
21  * Generic Container
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29 #include <string.h>
30
31 #include "mxfd10.h"
32
33 #include "mxfmpeg.h"
34 #include "mxfessence.h"
35
36 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
37 #define GST_CAT_DEFAULT mxf_debug
38
39 typedef struct
40 {
41   guint width, channels;
42 } MXFD10AudioMappingData;
43
44 static gboolean
45 mxf_is_d10_essence_track (const MXFMetadataTimelineTrack * track)
46 {
47   guint i;
48
49   g_return_val_if_fail (track != NULL, FALSE);
50
51   if (track->parent.descriptor == NULL)
52     return FALSE;
53
54   for (i = 0; i < track->parent.n_descriptor; i++) {
55     MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
56     MXFUL *key;
57
58     if (!d)
59       continue;
60
61     key = &d->essence_container;
62     /* SMPTE 386M 5.1 */
63     if (mxf_is_generic_container_essence_container_label (key) &&
64         key->u[12] == 0x02 && key->u[13] == 0x01 &&
65         (key->u[14] >= 0x01 && key->u[14] <= 0x06) &&
66         (key->u[15] == 0x01 || key->u[15] == 0x02))
67       return TRUE;
68   }
69
70   return FALSE;
71 }
72
73 static GstFlowReturn
74 mxf_d10_picture_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
75     GstCaps * caps,
76     MXFMetadataTimelineTrack * track,
77     gpointer mapping_data, GstBuffer ** outbuf)
78 {
79   *outbuf = buffer;
80
81   /* SMPTE 386M 5.2.1 */
82   if (key->u[12] != 0x05 || key->u[13] != 0x01 || key->u[14] != 0x01) {
83     GST_ERROR ("Invalid D10 picture essence element");
84     return GST_FLOW_ERROR;
85   }
86
87   if (mxf_mpeg_is_mpeg2_keyframe (buffer))
88     GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
89   else
90     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
91
92   return GST_FLOW_OK;
93 }
94
95 static GstFlowReturn
96 mxf_d10_sound_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
97     GstCaps * caps,
98     MXFMetadataTimelineTrack * track,
99     gpointer mapping_data, GstBuffer ** outbuf)
100 {
101   guint i, j, nsamples;
102   const guint8 *indata;
103   guint8 *outdata;
104   MXFD10AudioMappingData *data = mapping_data;
105
106   g_return_val_if_fail (data != NULL, GST_FLOW_ERROR);
107   g_return_val_if_fail (data->channels != 0
108       && data->width != 0, GST_FLOW_ERROR);
109
110   /* SMPTE 386M 5.3.1 */
111   if (key->u[12] != 0x06 || key->u[13] != 0x01 || key->u[14] != 0x10) {
112     GST_ERROR ("Invalid D10 sound essence element");
113     return GST_FLOW_ERROR;
114   }
115
116   /* Now transform raw AES3 into raw audio, see SMPTE 331M */
117   if ((GST_BUFFER_SIZE (buffer) - 4) % 32 != 0) {
118     GST_ERROR ("Invalid D10 sound essence buffer size");
119     return GST_FLOW_ERROR;
120   }
121
122   nsamples = ((GST_BUFFER_SIZE (buffer) - 4) / 4) / 8;
123
124   *outbuf = gst_buffer_new_and_alloc (nsamples * data->width * data->channels);
125   gst_buffer_copy_metadata (*outbuf, buffer,
126       GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
127       GST_BUFFER_COPY_CAPS);
128
129   indata = GST_BUFFER_DATA (buffer);
130   outdata = GST_BUFFER_DATA (*outbuf);
131
132   /* Skip 32 bit header */
133   indata += 4;
134
135   for (i = 0; i < nsamples; i++) {
136     for (j = 0; j < data->channels; j++) {
137       guint32 in = GST_READ_UINT32_LE (indata);
138
139       /* Remove first 4 and last 4 bits as they only
140        * contain status data. Shift the 24 bit samples
141        * to the correct width afterwards. */
142       if (data->width == 2) {
143         in = (in >> 12) & 0xffff;
144         GST_WRITE_UINT16_LE (outdata, in);
145       } else if (data->width == 3) {
146         in = (in >> 4) & 0xffffff;
147         GST_WRITE_UINT24_LE (outdata, in);
148       }
149       indata += 4;
150       outdata += data->width;
151     }
152     /* There are always 8 channels but only the first
153      * ones contain valid data, skip the others */
154     indata += 4 * (8 - data->channels);
155   }
156
157   gst_buffer_unref (buffer);
158
159   return GST_FLOW_OK;
160 }
161
162 static GstCaps *
163 mxf_d10_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
164     MXFEssenceElementHandleFunc * handler, gpointer * mapping_data)
165 {
166   MXFMetadataGenericPictureEssenceDescriptor *p = NULL;
167   MXFMetadataGenericSoundEssenceDescriptor *s = NULL;
168   guint i;
169   GstCaps *caps = NULL;
170
171   g_return_val_if_fail (track != NULL, NULL);
172
173   if (track->parent.descriptor == NULL) {
174     GST_ERROR ("No descriptor found for this track");
175     return NULL;
176   }
177
178   for (i = 0; i < track->parent.n_descriptor; i++) {
179     if (!track->parent.descriptor[i])
180       continue;
181
182     if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->parent.
183             descriptor[i])) {
184       p = (MXFMetadataGenericPictureEssenceDescriptor *) track->
185           parent.descriptor[i];
186       break;
187     } else if (MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->parent.
188             descriptor[i])) {
189       s = (MXFMetadataGenericSoundEssenceDescriptor *) track->
190           parent.descriptor[i];
191       break;
192     }
193   }
194
195   if (!s && !p) {
196     GST_ERROR ("No descriptor found for this track");
197     return NULL;
198   }
199
200   if (!*tags)
201     *tags = gst_tag_list_new ();
202
203   if (s) {
204     MXFD10AudioMappingData *data;
205
206     if (s->channel_count == 0 ||
207         s->quantization_bits == 0 ||
208         s->audio_sampling_rate.n == 0 || s->audio_sampling_rate.d == 0) {
209       GST_ERROR ("Invalid descriptor");
210       return NULL;
211     }
212
213     if (s->quantization_bits != 16 && s->quantization_bits != 24) {
214       GST_ERROR ("Invalid width %u", s->quantization_bits);
215       return NULL;
216     }
217
218     /* FIXME: set channel layout */
219
220     caps = gst_caps_new_simple ("audio/x-raw-int",
221         "signed", G_TYPE_BOOLEAN,
222         (s->quantization_bits != 8), "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
223         "depth", G_TYPE_INT, s->quantization_bits, "width", G_TYPE_INT,
224         s->quantization_bits, NULL);
225
226     mxf_metadata_generic_sound_essence_descriptor_set_caps (s, caps);
227
228     *handler = mxf_d10_sound_handle_essence_element;
229
230     data = g_new0 (MXFD10AudioMappingData, 1);
231     data->width = s->quantization_bits / 8;
232     data->channels = s->channel_count;
233     *mapping_data = data;
234
235     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
236         "SMPTE D-10 Audio", NULL);
237   } else if (p) {
238     caps =
239         gst_caps_new_simple ("video/mpeg", "systemstream", G_TYPE_BOOLEAN,
240         FALSE, "mpegversion", G_TYPE_INT, 2, NULL);
241     mxf_metadata_generic_picture_essence_descriptor_set_caps (p, caps);
242
243     *handler = mxf_d10_picture_handle_essence_element;
244     gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
245         "SMPTE D-10 Video", NULL);
246   }
247
248   return caps;
249 }
250
251 static const MXFEssenceElementHandler mxf_d10_essence_element_handler = {
252   mxf_is_d10_essence_track,
253   mxf_d10_create_caps
254 };
255
256 void
257 mxf_d10_init (void)
258 {
259   mxf_essence_element_handler_register (&mxf_d10_essence_element_handler);
260 }