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 388M - Mapping A-Law coded audio 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 "mxfalaw.h"
32 #include "mxfessence.h"
33
34 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
35 #define GST_CAT_DEFAULT mxf_debug
36
37 static gboolean
mxf_is_alaw_essence_track(const MXFMetadataTimelineTrack * track)38 mxf_is_alaw_essence_track (const MXFMetadataTimelineTrack * track)
39 {
40 guint i;
41
42 g_return_val_if_fail (track != NULL, FALSE);
43
44 if (track->parent.descriptor == NULL)
45 return FALSE;
46
47 for (i = 0; i < track->parent.n_descriptor; i++) {
48 MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
49 MXFUL *key;
50
51 if (!d)
52 continue;
53
54 key = &d->essence_container;
55 /* SMPTE 388M 6.1 */
56 if (mxf_is_generic_container_essence_container_label (key) &&
57 key->u[12] == 0x02 && key->u[13] == 0x0a &&
58 (key->u[14] == 0x01 || key->u[14] == 0x02 || key->u[14] == 0x03))
59 return TRUE;
60 }
61
62 return FALSE;
63 }
64
65 static GstFlowReturn
mxf_alaw_handle_essence_element(const MXFUL * key,GstBuffer * buffer,GstCaps * caps,MXFMetadataTimelineTrack * track,gpointer mapping_data,GstBuffer ** outbuf)66 mxf_alaw_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
67 GstCaps * caps,
68 MXFMetadataTimelineTrack * track,
69 gpointer mapping_data, GstBuffer ** outbuf)
70 {
71 *outbuf = buffer;
72
73 /* SMPTE 388M 5.1 */
74 if (key->u[12] != 0x16 || (key->u[14] != 0x08 && key->u[14] != 0x09
75 && key->u[14] != 0x0a)) {
76 GST_ERROR ("Invalid A-Law essence element");
77 return GST_FLOW_ERROR;
78 }
79
80 return GST_FLOW_OK;
81 }
82
83 static MXFEssenceWrapping
mxf_alaw_get_track_wrapping(const MXFMetadataTimelineTrack * track)84 mxf_alaw_get_track_wrapping (const MXFMetadataTimelineTrack * track)
85 {
86 guint i;
87
88 g_return_val_if_fail (track != NULL, MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING);
89
90 if (track->parent.descriptor == NULL) {
91 GST_ERROR ("No descriptor found for this track");
92 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
93 }
94
95 for (i = 0; i < track->parent.n_descriptor; i++) {
96 if (!track->parent.descriptor[i])
97 continue;
98
99 if (!MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->parent.
100 descriptor[i]))
101 continue;
102
103 switch (track->parent.descriptor[i]->essence_container.u[14]) {
104 case 0x01:
105 return MXF_ESSENCE_WRAPPING_FRAME_WRAPPING;
106 break;
107 case 0x02:
108 return MXF_ESSENCE_WRAPPING_CLIP_WRAPPING;
109 break;
110 case 0x03:
111 default:
112 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
113 break;
114 }
115 }
116
117 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
118 }
119
120 static GstCaps *
mxf_alaw_create_caps(MXFMetadataTimelineTrack * track,GstTagList ** tags,gboolean * intra_only,MXFEssenceElementHandleFunc * handler,gpointer * mapping_data)121 mxf_alaw_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
122 gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
123 gpointer * mapping_data)
124 {
125 MXFMetadataGenericSoundEssenceDescriptor *s = NULL;
126 guint i;
127 GstCaps *caps = NULL;
128
129 g_return_val_if_fail (track != NULL, NULL);
130
131 if (track->parent.descriptor == NULL) {
132 GST_ERROR ("No descriptor found for this track");
133 return NULL;
134 }
135
136 for (i = 0; i < track->parent.n_descriptor; i++) {
137 if (!track->parent.descriptor[i])
138 continue;
139
140 if (MXF_IS_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR (track->parent.
141 descriptor[i])) {
142 s = (MXFMetadataGenericSoundEssenceDescriptor *) track->parent.
143 descriptor[i];
144 break;
145 }
146 }
147
148 if (!s) {
149 GST_ERROR ("No generic sound essence descriptor found for this track");
150 return NULL;
151 }
152
153 *handler = mxf_alaw_handle_essence_element;
154
155 if (s && s->audio_sampling_rate.n != 0 && s->audio_sampling_rate.d != 0 &&
156 s->channel_count != 0) {
157
158 caps = gst_caps_new_empty_simple ("audio/x-alaw");
159 mxf_metadata_generic_sound_essence_descriptor_set_caps (s, caps);
160
161 /* TODO: Handle channel layout somehow?
162 * Or is alaw limited to two channels? */
163 if (!*tags)
164 *tags = gst_tag_list_new_empty ();
165
166 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_AUDIO_CODEC,
167 "A-law encoded audio", NULL);
168
169 }
170
171 *intra_only = TRUE;
172
173 return caps;
174 }
175
176 static const MXFEssenceElementHandler mxf_alaw_essence_element_handler = {
177 mxf_is_alaw_essence_track,
178 mxf_alaw_get_track_wrapping,
179 mxf_alaw_create_caps
180 };
181
182 typedef struct
183 {
184 guint64 error;
185 gint rate, channels;
186 MXFFraction edit_rate;
187 } ALawMappingData;
188
189 static GstFlowReturn
mxf_alaw_write_func(GstBuffer * buffer,gpointer mapping_data,GstAdapter * adapter,GstBuffer ** outbuf,gboolean flush)190 mxf_alaw_write_func (GstBuffer * buffer, gpointer mapping_data,
191 GstAdapter * adapter, GstBuffer ** outbuf, gboolean flush)
192 {
193 ALawMappingData *md = mapping_data;
194 guint bytes;
195 guint64 speu =
196 gst_util_uint64_scale (md->rate, md->edit_rate.d, md->edit_rate.n);
197
198 md->error += (md->edit_rate.d * md->rate) % (md->edit_rate.n);
199 if (md->error >= md->edit_rate.n) {
200 md->error = 0;
201 speu += 1;
202 }
203
204 bytes = speu * md->channels;
205
206 if (buffer)
207 gst_adapter_push (adapter, buffer);
208
209 if (gst_adapter_available (adapter) == 0)
210 return GST_FLOW_OK;
211
212 if (flush)
213 bytes = MIN (gst_adapter_available (adapter), bytes);
214
215 if (gst_adapter_available (adapter) >= bytes) {
216 *outbuf = gst_adapter_take_buffer (adapter, bytes);
217 }
218
219 if (gst_adapter_available (adapter) >= bytes)
220 return GST_FLOW_CUSTOM_SUCCESS;
221 else
222 return GST_FLOW_OK;
223 }
224
225 static const guint8 alaw_essence_container_ul[] = {
226 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x03,
227 0x0d, 0x01, 0x03, 0x01, 0x02, 0x0a, 0x01, 0x00
228 };
229
230 static const MXFUL mxf_sound_essence_compression_alaw =
231 { {0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x03, 0x04, 0x02, 0x02, 0x02,
232 0x03, 0x01, 0x01, 0x00}
233 };
234
235 static MXFMetadataFileDescriptor *
mxf_alaw_get_descriptor(GstPadTemplate * tmpl,GstCaps * caps,MXFEssenceElementWriteFunc * handler,gpointer * mapping_data)236 mxf_alaw_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
237 MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
238 {
239 MXFMetadataGenericSoundEssenceDescriptor *ret;
240 GstStructure *s;
241 ALawMappingData *md;
242 gint rate, channels;
243
244 s = gst_caps_get_structure (caps, 0);
245 if (strcmp (gst_structure_get_name (s), "audio/x-alaw") != 0 ||
246 !gst_structure_get_int (s, "rate", &rate) ||
247 !gst_structure_get_int (s, "channels", &channels)) {
248 GST_ERROR ("Invalid caps %" GST_PTR_FORMAT, caps);
249 return NULL;
250 }
251
252 ret = (MXFMetadataGenericSoundEssenceDescriptor *)
253 g_object_new (MXF_TYPE_METADATA_GENERIC_SOUND_ESSENCE_DESCRIPTOR, NULL);
254
255 memcpy (&ret->parent.essence_container, &alaw_essence_container_ul, 16);
256 memcpy (&ret->sound_essence_compression, &mxf_sound_essence_compression_alaw,
257 16);
258
259 if (!mxf_metadata_generic_sound_essence_descriptor_from_caps (ret, caps)) {
260 g_object_unref (ret);
261 return NULL;
262 }
263
264 *handler = mxf_alaw_write_func;
265
266 md = g_new0 (ALawMappingData, 1);
267 md->rate = rate;
268 md->channels = channels;
269 *mapping_data = md;
270
271 return (MXFMetadataFileDescriptor *) ret;
272 }
273
274 static void
mxf_alaw_update_descriptor(MXFMetadataFileDescriptor * d,GstCaps * caps,gpointer mapping_data,GstBuffer * buf)275 mxf_alaw_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
276 gpointer mapping_data, GstBuffer * buf)
277 {
278 return;
279 }
280
281 static void
mxf_alaw_get_edit_rate(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data,GstBuffer * buf,MXFMetadataSourcePackage * package,MXFMetadataTimelineTrack * track,MXFFraction * edit_rate)282 mxf_alaw_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
283 gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
284 MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
285 {
286 guint i;
287 gdouble min = G_MAXDOUBLE;
288 ALawMappingData *md = mapping_data;
289
290 for (i = 0; i < package->parent.n_tracks; i++) {
291 MXFMetadataTimelineTrack *tmp;
292
293 if (!MXF_IS_METADATA_TIMELINE_TRACK (package->parent.tracks[i]) ||
294 package->parent.tracks[i] == (MXFMetadataTrack *) track)
295 continue;
296
297 tmp = MXF_METADATA_TIMELINE_TRACK (package->parent.tracks[i]);
298 if (((gdouble) tmp->edit_rate.n) / ((gdouble) tmp->edit_rate.d) < min) {
299 min = ((gdouble) tmp->edit_rate.n) / ((gdouble) tmp->edit_rate.d);
300 memcpy (edit_rate, &tmp->edit_rate, sizeof (MXFFraction));
301 }
302 }
303
304 if (min == G_MAXDOUBLE) {
305 /* 100ms edit units */
306 edit_rate->n = 10;
307 edit_rate->d = 1;
308 }
309
310 memcpy (&md->edit_rate, edit_rate, sizeof (MXFFraction));
311 }
312
313 static guint32
mxf_alaw_get_track_number_template(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data)314 mxf_alaw_get_track_number_template (MXFMetadataFileDescriptor * a,
315 GstCaps * caps, gpointer mapping_data)
316 {
317 return (0x16 << 24) | (0x08 << 8);
318 }
319
320 static MXFEssenceElementWriter mxf_alaw_essence_element_writer = {
321 mxf_alaw_get_descriptor,
322 mxf_alaw_update_descriptor,
323 mxf_alaw_get_edit_rate,
324 mxf_alaw_get_track_number_template,
325 NULL,
326 {{0,}}
327 };
328
329 #define ALAW_CAPS \
330 "audio/x-alaw, " \
331 "rate = (int) [ 8000, 192000 ], " \
332 "channels = (int) [ 1, 2 ]"
333
334 void
mxf_alaw_init(void)335 mxf_alaw_init (void)
336 {
337 mxf_essence_element_handler_register (&mxf_alaw_essence_element_handler);
338
339 mxf_alaw_essence_element_writer.pad_template =
340 gst_pad_template_new ("alaw_audio_sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
341 gst_caps_from_string (ALAW_CAPS));
342 memcpy (&mxf_alaw_essence_element_writer.data_definition,
343 mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_SOUND_ESSENCE), 16);
344 mxf_essence_element_writer_register (&mxf_alaw_essence_element_writer);
345 }
346