2 * Copyright (C) 2008-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* Implementation of SMPTE 386M - Mapping Type-D10 essence data into the MXF
34 #include "mxfessence.h"
36 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
37 #define GST_CAT_DEFAULT mxf_debug
41 guint width, channels;
42 } MXFD10AudioMappingData;
45 mxf_is_d10_essence_track (const MXFMetadataTimelineTrack * track)
49 g_return_val_if_fail (track != NULL, FALSE);
51 if (track->parent.descriptor == NULL)
54 for (i = 0; i < track->parent.n_descriptor; i++) {
55 MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
61 key = &d->essence_container;
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))
74 mxf_d10_picture_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
76 MXFMetadataTimelineTrack * track,
77 gpointer mapping_data, GstBuffer ** outbuf)
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;
87 if (mxf_mpeg_is_mpeg2_keyframe (buffer))
88 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
90 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
96 mxf_d10_sound_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
98 MXFMetadataTimelineTrack * track,
99 gpointer mapping_data, GstBuffer ** outbuf)
101 guint i, j, nsamples;
102 const guint8 *indata;
104 MXFD10AudioMappingData *data = mapping_data;
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);
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;
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;
122 nsamples = ((GST_BUFFER_SIZE (buffer) - 4) / 4) / 8;
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);
129 indata = GST_BUFFER_DATA (buffer);
130 outdata = GST_BUFFER_DATA (*outbuf);
132 /* Skip 32 bit header */
135 for (i = 0; i < nsamples; i++) {
136 for (j = 0; j < data->channels; j++) {
137 guint32 in = GST_READ_UINT32_LE (indata);
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);
150 outdata += data->width;
152 /* There are always 8 channels but only the first
153 * ones contain valid data, skip the others */
154 indata += 4 * (8 - data->channels);
157 gst_buffer_unref (buffer);
163 mxf_d10_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
164 MXFEssenceElementHandleFunc * handler, gpointer * mapping_data)
166 MXFMetadataGenericPictureEssenceDescriptor *p = NULL;
167 MXFMetadataGenericSoundEssenceDescriptor *s = NULL;
169 GstCaps *caps = NULL;
171 g_return_val_if_fail (track != NULL, NULL);
173 if (track->parent.descriptor == NULL) {
174 GST_ERROR ("No descriptor found for this track");
178 for (i = 0; i < track->parent.n_descriptor; i++) {
179 if (!track->parent.descriptor[i])
182 if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->parent.
184 p = (MXFMetadataGenericPictureEssenceDescriptor *) track->
185 parent.descriptor[i];
187 } else if (MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->parent.
189 s = (MXFMetadataGenericSoundEssenceDescriptor *) track->
190 parent.descriptor[i];
196 GST_ERROR ("No descriptor found for this track");
201 *tags = gst_tag_list_new ();
204 MXFD10AudioMappingData *data;
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");
213 if (s->quantization_bits != 16 && s->quantization_bits != 24) {
214 GST_ERROR ("Invalid width %u", s->quantization_bits);
218 /* FIXME: set channel layout */
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);
226 mxf_metadata_generic_sound_essence_descriptor_set_caps (s, caps);
228 *handler = mxf_d10_sound_handle_essence_element;
230 data = g_new0 (MXFD10AudioMappingData, 1);
231 data->width = s->quantization_bits / 8;
232 data->channels = s->channel_count;
233 *mapping_data = data;
235 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
236 "SMPTE D-10 Audio", NULL);
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);
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);
251 static const MXFEssenceElementHandler mxf_d10_essence_element_handler = {
252 mxf_is_d10_essence_track,
259 mxf_essence_element_handler_register (&mxf_d10_essence_element_handler);