1 /* GStreamer AIFF muxer
2 * Copyright (C) 2009 Robert Swain <robert.swain@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 * Alternatively, the contents of this file may be used under the
23 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
24 * which case the following provisions apply instead of the ones
25 * mentioned above:
26 *
27 * This library is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU Library General Public
29 * License as published by the Free Software Foundation; either
30 * version 2 of the License, or (at your option) any later version.
31 *
32 * This library is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 * Library General Public License for more details.
36 *
37 * You should have received a copy of the GNU Library General Public
38 * License along with this library; if not, write to the
39 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
40 * Boston, MA 02110-1301, USA.
41 */
42
43 /**
44 * SECTION:element-aiffmux
45 * @title: aiffmux
46 *
47 * Format an audio stream into the Audio Interchange File Format
48 *
49 */
50
51 #ifdef HAVE_CONFIG_H
52 # include <config.h>
53 #endif
54
55 #include <string.h>
56 #include <math.h>
57 #include <gst/gst.h>
58 #include <gst/base/gstbytewriter.h>
59
60 #include "aiffelements.h"
61 #include "aiffmux.h"
62
63 GST_DEBUG_CATEGORY (aiffmux_debug);
64 #define GST_CAT_DEFAULT aiffmux_debug
65
66 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
67 GST_PAD_SINK,
68 GST_PAD_ALWAYS,
69 GST_STATIC_CAPS ("audio/x-raw, "
70 "format = { S8, S16BE, S24BE, S32BE },"
71 "channels = (int) [ 1, MAX ], " "rate = (int) [ 1, MAX ]")
72 );
73
74 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
75 GST_PAD_SRC,
76 GST_PAD_ALWAYS,
77 GST_STATIC_CAPS ("audio/x-aiff")
78 );
79
80 #define gst_aiff_mux_parent_class parent_class
81 G_DEFINE_TYPE_WITH_CODE (GstAiffMux, gst_aiff_mux, GST_TYPE_ELEMENT,
82 GST_DEBUG_CATEGORY_INIT (aiffmux_debug, "aiffmux", 0, "AIFF muxer"));
83 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (aiffmux, "aiffmux", GST_RANK_PRIMARY,
84 GST_TYPE_AIFF_MUX, aiff_element_init (plugin));
85
86 static GstStateChangeReturn
gst_aiff_mux_change_state(GstElement * element,GstStateChange transition)87 gst_aiff_mux_change_state (GstElement * element, GstStateChange transition)
88 {
89 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
90 GstAiffMux *aiffmux = GST_AIFF_MUX (element);
91
92 switch (transition) {
93 case GST_STATE_CHANGE_READY_TO_PAUSED:
94 gst_audio_info_init (&aiffmux->info);
95 aiffmux->length = 0;
96 aiffmux->sent_header = FALSE;
97 aiffmux->overflow = FALSE;
98 break;
99 default:
100 break;
101 }
102
103 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
104 if (ret != GST_STATE_CHANGE_SUCCESS)
105 return ret;
106
107 return ret;
108 }
109
110 static void
gst_aiff_mux_class_init(GstAiffMuxClass * klass)111 gst_aiff_mux_class_init (GstAiffMuxClass * klass)
112 {
113 GstElementClass *gstelement_class;
114
115 gstelement_class = (GstElementClass *) klass;
116
117 gst_element_class_set_static_metadata (gstelement_class,
118 "AIFF audio muxer", "Muxer/Audio", "Multiplex raw audio into AIFF",
119 "Robert Swain <robert.swain@gmail.com>");
120
121 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
122 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
123
124 gstelement_class->change_state =
125 GST_DEBUG_FUNCPTR (gst_aiff_mux_change_state);
126 }
127
128 #define AIFF_FORM_HEADER_LEN 8 + 4
129 #define AIFF_COMM_HEADER_LEN 8 + 18
130 #define AIFF_SSND_HEADER_LEN 8 + 8
131 #define AIFF_HEADER_LEN \
132 (AIFF_FORM_HEADER_LEN + AIFF_COMM_HEADER_LEN + AIFF_SSND_HEADER_LEN)
133
134 static void
gst_aiff_mux_write_form_header(GstAiffMux * aiffmux,guint32 audio_data_size,GstByteWriter * writer)135 gst_aiff_mux_write_form_header (GstAiffMux * aiffmux, guint32 audio_data_size,
136 GstByteWriter * writer)
137 {
138 guint64 cur_size;
139
140 /* ckID == 'FORM' */
141 gst_byte_writer_put_uint32_le_unchecked (writer,
142 GST_MAKE_FOURCC ('F', 'O', 'R', 'M'));
143
144 /* AIFF chunks must be even aligned */
145 cur_size = AIFF_HEADER_LEN - 8 + audio_data_size;
146 if ((cur_size & 1) && cur_size + 1 < G_MAXUINT32) {
147 cur_size += 1;
148 }
149
150 gst_byte_writer_put_uint32_be_unchecked (writer, cur_size);
151 /* formType == 'AIFF' */
152 gst_byte_writer_put_uint32_le_unchecked (writer,
153 GST_MAKE_FOURCC ('A', 'I', 'F', 'F'));
154 }
155
156 /*
157 * BEGIN: Code borrowed from FFmpeg's libavutil/intfloat_readwrite.{c,h}
158 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
159 */
160
161 /* IEEE 80 bits extended float */
162 typedef struct AVExtFloat
163 {
164 guint8 exponent[2];
165 guint8 mantissa[8];
166 } AVExtFloat;
167
168 /* Courtesy http://www.devx.com/tips/Tip/42853 */
169 static inline gint
gst_aiff_mux_isinf(gdouble x)170 gst_aiff_mux_isinf (gdouble x)
171 {
172 volatile gdouble temp = x;
173 if ((temp == x) && ((temp - x) != 0.0))
174 return (x < 0.0 ? -1 : 1);
175 else
176 return 0;
177 }
178
179 static void
gst_aiff_mux_write_ext(GstByteWriter * writer,double d)180 gst_aiff_mux_write_ext (GstByteWriter * writer, double d)
181 {
182 struct AVExtFloat ext = { {0} };
183 gint e, i;
184 gdouble f;
185 guint64 m;
186
187 f = fabs (frexp (d, &e));
188 if (f >= 0.5 && f < 1) {
189 e += 16382;
190 ext.exponent[0] = e >> 8;
191 ext.exponent[1] = e;
192 m = (guint64) ldexp (f, 64);
193 for (i = 0; i < 8; i++)
194 ext.mantissa[i] = m >> (56 - (i << 3));
195 } else if (f != 0.0) {
196 ext.exponent[0] = 0x7f;
197 ext.exponent[1] = 0xff;
198 if (!gst_aiff_mux_isinf (f))
199 ext.mantissa[0] = ~0;
200 }
201 if (d < 0)
202 ext.exponent[0] |= 0x80;
203
204 gst_byte_writer_put_data_unchecked (writer, ext.exponent, 2);
205 gst_byte_writer_put_data_unchecked (writer, ext.mantissa, 8);
206 }
207
208 /*
209 * END: Code borrowed from FFmpeg's libavutil/intfloat_readwrite.{c,h}
210 */
211
212 static void
gst_aiff_mux_write_comm_header(GstAiffMux * aiffmux,guint32 audio_data_size,GstByteWriter * writer)213 gst_aiff_mux_write_comm_header (GstAiffMux * aiffmux, guint32 audio_data_size,
214 GstByteWriter * writer)
215 {
216 guint16 channels;
217 guint16 width, depth;
218 gdouble rate;
219
220 channels = GST_AUDIO_INFO_CHANNELS (&aiffmux->info);
221 width = GST_AUDIO_INFO_WIDTH (&aiffmux->info);
222 depth = GST_AUDIO_INFO_DEPTH (&aiffmux->info);
223 rate = GST_AUDIO_INFO_RATE (&aiffmux->info);
224
225 gst_byte_writer_put_uint32_le_unchecked (writer,
226 GST_MAKE_FOURCC ('C', 'O', 'M', 'M'));
227 gst_byte_writer_put_uint32_be_unchecked (writer, 18);
228 gst_byte_writer_put_uint16_be_unchecked (writer, channels);
229 /* numSampleFrames value will be overwritten when known */
230 gst_byte_writer_put_uint32_be_unchecked (writer,
231 audio_data_size / (width / 8 * channels));
232 gst_byte_writer_put_uint16_be_unchecked (writer, depth);
233 gst_aiff_mux_write_ext (writer, rate);
234 }
235
236 static void
gst_aiff_mux_write_ssnd_header(GstAiffMux * aiffmux,guint32 audio_data_size,GstByteWriter * writer)237 gst_aiff_mux_write_ssnd_header (GstAiffMux * aiffmux, guint32 audio_data_size,
238 GstByteWriter * writer)
239 {
240 gst_byte_writer_put_uint32_le_unchecked (writer,
241 GST_MAKE_FOURCC ('S', 'S', 'N', 'D'));
242 /* ckSize will be overwritten when known */
243 gst_byte_writer_put_uint32_be_unchecked (writer,
244 audio_data_size + AIFF_SSND_HEADER_LEN - 8);
245 /* offset and blockSize are set to 0 as we don't support block-aligned sample data yet */
246 gst_byte_writer_put_uint32_be_unchecked (writer, 0);
247 gst_byte_writer_put_uint32_be_unchecked (writer, 0);
248 }
249
250 static GstFlowReturn
gst_aiff_mux_push_header(GstAiffMux * aiffmux,guint32 audio_data_size)251 gst_aiff_mux_push_header (GstAiffMux * aiffmux, guint32 audio_data_size)
252 {
253 GstFlowReturn ret;
254 GstBuffer *outbuf;
255 GstByteWriter writer;
256 GstSegment seg;
257
258 /* seek to beginning of file */
259 gst_segment_init (&seg, GST_FORMAT_BYTES);
260
261 if (gst_pad_push_event (aiffmux->srcpad,
262 gst_event_new_segment (&seg)) == FALSE) {
263 GST_ELEMENT_WARNING (aiffmux, STREAM, MUX,
264 ("An output stream seeking error occurred when multiplexing."),
265 ("Failed to seek to beginning of stream to write header."));
266 }
267
268 GST_DEBUG_OBJECT (aiffmux, "writing header with datasize=%u",
269 audio_data_size);
270
271 gst_byte_writer_init_with_size (&writer, AIFF_HEADER_LEN, TRUE);
272
273 gst_aiff_mux_write_form_header (aiffmux, audio_data_size, &writer);
274 gst_aiff_mux_write_comm_header (aiffmux, audio_data_size, &writer);
275 gst_aiff_mux_write_ssnd_header (aiffmux, audio_data_size, &writer);
276
277 outbuf = gst_byte_writer_reset_and_get_buffer (&writer);
278
279 ret = gst_pad_push (aiffmux->srcpad, outbuf);
280
281 if (ret != GST_FLOW_OK) {
282 GST_WARNING_OBJECT (aiffmux, "push header failed: flow = %s",
283 gst_flow_get_name (ret));
284 }
285
286 return ret;
287 }
288
289 static GstFlowReturn
gst_aiff_mux_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)290 gst_aiff_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
291 {
292 GstAiffMux *aiffmux = GST_AIFF_MUX (parent);
293 GstFlowReturn flow = GST_FLOW_OK;
294 guint64 cur_size;
295 gsize buf_size;
296
297 if (!GST_AUDIO_INFO_CHANNELS (&aiffmux->info))
298 goto not_negotiated;
299
300 if (G_UNLIKELY (aiffmux->overflow))
301 goto overflow;
302
303 if (!aiffmux->sent_header) {
304 /* use bogus size initially, we'll write the real
305 * header when we get EOS and know the exact length */
306 flow = gst_aiff_mux_push_header (aiffmux, 0x7FFF0000);
307 if (flow != GST_FLOW_OK)
308 goto flow_error;
309
310 GST_DEBUG_OBJECT (aiffmux, "wrote dummy header");
311 aiffmux->sent_header = TRUE;
312 }
313
314 /* AIFF has an audio data size limit of slightly under 4 GB.
315 A value of audiosize + AIFF_HEADER_LEN - 8 is written, so
316 I'll error out if writing data that makes this overflow. */
317 cur_size = aiffmux->length + AIFF_HEADER_LEN - 8;
318 buf_size = gst_buffer_get_size (buf);
319
320 if (G_UNLIKELY (cur_size + buf_size >= G_MAXUINT32)) {
321 GST_ERROR_OBJECT (aiffmux, "AIFF only supports about 4 GB worth of "
322 "audio data, dropping any further data on the floor");
323 GST_ELEMENT_WARNING (aiffmux, STREAM, MUX, ("AIFF has a 4GB size limit"),
324 ("AIFF only supports about 4 GB worth of audio data, "
325 "dropping any further data on the floor"));
326 aiffmux->overflow = TRUE;
327 goto overflow;
328 }
329
330 GST_LOG_OBJECT (aiffmux,
331 "pushing %" G_GSIZE_FORMAT " bytes raw audio, ts=%" GST_TIME_FORMAT,
332 buf_size, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
333
334 buf = gst_buffer_make_writable (buf);
335
336 GST_BUFFER_OFFSET (buf) = AIFF_HEADER_LEN + aiffmux->length;
337 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
338
339 aiffmux->length += buf_size;
340
341 flow = gst_pad_push (aiffmux->srcpad, buf);
342
343 return flow;
344
345 not_negotiated:
346 {
347 GST_WARNING_OBJECT (aiffmux, "no input format negotiated");
348 gst_buffer_unref (buf);
349 return GST_FLOW_NOT_NEGOTIATED;
350 }
351 overflow:
352 {
353 GST_WARNING_OBJECT (aiffmux, "output file too large, dropping buffer");
354 gst_buffer_unref (buf);
355 return GST_FLOW_OK;
356 }
357 flow_error:
358 {
359 GST_DEBUG_OBJECT (aiffmux, "got flow error %s", gst_flow_get_name (flow));
360 gst_buffer_unref (buf);
361 return flow;
362 }
363 }
364
365 static gboolean
gst_aiff_mux_set_caps(GstAiffMux * aiffmux,GstCaps * caps)366 gst_aiff_mux_set_caps (GstAiffMux * aiffmux, GstCaps * caps)
367 {
368 GstCaps *outcaps;
369 GstAudioInfo info;
370
371 if (aiffmux->sent_header) {
372 GST_WARNING_OBJECT (aiffmux, "cannot change format mid-stream");
373 return FALSE;
374 }
375
376 GST_DEBUG_OBJECT (aiffmux, "got caps: %" GST_PTR_FORMAT, caps);
377
378 if (!gst_audio_info_from_caps (&info, caps)) {
379 GST_WARNING_OBJECT (aiffmux, "caps incomplete");
380 return FALSE;
381 }
382
383 aiffmux->info = info;
384
385 GST_LOG_OBJECT (aiffmux,
386 "accepted caps: chans=%d depth=%d rate=%d",
387 GST_AUDIO_INFO_CHANNELS (&info), GST_AUDIO_INFO_DEPTH (&info),
388 GST_AUDIO_INFO_RATE (&info));
389
390 outcaps = gst_static_pad_template_get_caps (&src_factory);
391 gst_pad_push_event (aiffmux->srcpad, gst_event_new_caps (outcaps));
392 gst_caps_unref (outcaps);
393
394 return TRUE;
395 }
396
397
398 static gboolean
gst_aiff_mux_event(GstPad * pad,GstObject * parent,GstEvent * event)399 gst_aiff_mux_event (GstPad * pad, GstObject * parent, GstEvent * event)
400 {
401 gboolean res = TRUE;
402 GstAiffMux *aiffmux;
403
404 aiffmux = GST_AIFF_MUX (parent);
405
406 switch (GST_EVENT_TYPE (event)) {
407 case GST_EVENT_EOS:{
408 guint64 cur_size;
409 GST_DEBUG_OBJECT (aiffmux, "got EOS");
410
411 cur_size = aiffmux->length + AIFF_HEADER_LEN - 8;
412
413 /* ID3 chunk must be even aligned */
414 if ((aiffmux->length & 1) && cur_size + 1 < G_MAXUINT32) {
415 GstFlowReturn ret;
416 guint8 *data = g_new0 (guint8, 1);
417 GstBuffer *buffer = gst_buffer_new_wrapped (data, 1);
418 GST_BUFFER_OFFSET (buffer) = AIFF_HEADER_LEN + aiffmux->length;
419 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
420 ret = gst_pad_push (aiffmux->srcpad, buffer);
421 if (ret != GST_FLOW_OK) {
422 GST_WARNING_OBJECT (aiffmux, "failed to push padding byte: %s",
423 gst_flow_get_name (ret));
424 }
425 }
426
427 /* write header with correct length values */
428 gst_aiff_mux_push_header (aiffmux, aiffmux->length);
429
430 /* and forward the EOS event */
431 res = gst_pad_event_default (pad, parent, event);
432 break;
433 }
434 case GST_EVENT_CAPS:
435 {
436 GstCaps *caps;
437
438 gst_event_parse_caps (event, &caps);
439 res = gst_aiff_mux_set_caps (aiffmux, caps);
440 gst_event_unref (event);
441 break;
442 }
443 case GST_EVENT_SEGMENT:
444 /* Just drop it, it's probably in TIME format
445 * anyway. We'll send our own newsegment event */
446 gst_event_unref (event);
447 break;
448 default:
449 res = gst_pad_event_default (pad, parent, event);
450 break;
451 }
452 return res;
453 }
454
455 static void
gst_aiff_mux_init(GstAiffMux * aiffmux)456 gst_aiff_mux_init (GstAiffMux * aiffmux)
457 {
458 aiffmux->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
459 gst_pad_set_chain_function (aiffmux->sinkpad,
460 GST_DEBUG_FUNCPTR (gst_aiff_mux_chain));
461 gst_pad_set_event_function (aiffmux->sinkpad,
462 GST_DEBUG_FUNCPTR (gst_aiff_mux_event));
463 gst_element_add_pad (GST_ELEMENT (aiffmux), aiffmux->sinkpad);
464
465 aiffmux->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
466 gst_pad_use_fixed_caps (aiffmux->srcpad);
467 gst_element_add_pad (GST_ELEMENT (aiffmux), aiffmux->srcpad);
468 }
469