1 /* GStreamer
2 * Copyright (C) 2009 Pioneers of the Inevitable <songbird@songbirdnest.com>
3 *
4 * Authors: Peter van Hardenberg <pvh@songbirdnest.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /* Based on ADPCM encoders in libsndfile,
23 Copyright (C) 1999-2002 Erik de Castro Lopo <erikd@zip.com.au
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <gst/gst.h>
31 #include <gst/audio/gstaudioencoder.h>
32
33 #define GST_TYPE_ADPCM_ENC \
34 (adpcmenc_get_type ())
35
36 #define GST_TYPE_ADPCMENC_LAYOUT \
37 (adpcmenc_layout_get_type ())
38
39 #define GST_ADPCM_ENC(obj) \
40 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_ADPCM_ENC, ADPCMEnc))
41
42 #define GST_CAT_DEFAULT adpcmenc_debug
43 GST_DEBUG_CATEGORY_STATIC (adpcmenc_debug);
44
45 static GstStaticPadTemplate adpcmenc_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_PAD_SINK,
48 GST_PAD_ALWAYS,
49 GST_STATIC_CAPS ("audio/x-raw, "
50 "format = (string) " GST_AUDIO_NE (S16) ", "
51 "layout = (string) interleaved, "
52 "rate = (int) [1, MAX], channels = (int) [1,2]")
53 );
54
55 static GstStaticPadTemplate adpcmenc_src_template =
56 GST_STATIC_PAD_TEMPLATE ("src",
57 GST_PAD_SRC,
58 GST_PAD_ALWAYS,
59 GST_STATIC_CAPS ("audio/x-adpcm, "
60 " layout=(string)dvi, "
61 " block_align = (int) [64, 8192], "
62 " rate = (int)[ 1, MAX ], " "channels = (int)[1,2];")
63 );
64
65 #define MIN_ADPCM_BLOCK_SIZE 64
66 #define MAX_ADPCM_BLOCK_SIZE 8192
67 #define DEFAULT_ADPCM_BLOCK_SIZE 1024
68 #define DEFAULT_ADPCM_LAYOUT LAYOUT_ADPCM_DVI
69
70 static const int ima_indx_adjust[16] = {
71 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8,
72 };
73
74 static const int ima_step_size[89] = {
75 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
76 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230,
77 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
78 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
79 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442,
80 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
81 32767
82 };
83
84
85 enum adpcm_properties
86 {
87 PROP_0,
88 PROP_BLOCK_SIZE,
89 PROP_LAYOUT
90 };
91
92 enum adpcm_layout
93 {
94 LAYOUT_ADPCM_DVI
95 };
96
97 static GType
adpcmenc_layout_get_type(void)98 adpcmenc_layout_get_type (void)
99 {
100 static GType adpcmenc_layout_type = 0;
101
102 if (!adpcmenc_layout_type) {
103 static const GEnumValue layout_types[] = {
104 {LAYOUT_ADPCM_DVI, "DVI/IMA APDCM", "dvi"},
105 {0, NULL, NULL},
106 };
107
108 adpcmenc_layout_type = g_enum_register_static ("GstADPCMEncLayout",
109 layout_types);
110 }
111
112 return adpcmenc_layout_type;
113 }
114
115 typedef struct _ADPCMEncClass
116 {
117 GstAudioEncoderClass parent_class;
118 } ADPCMEncClass;
119
120 typedef struct _ADPCMEnc
121 {
122 GstAudioEncoder parent;
123
124 enum adpcm_layout layout;
125 int rate;
126 int channels;
127 int blocksize;
128 int samples_per_block;
129
130 guint8 step_index[2];
131
132 } ADPCMEnc;
133
134 GType adpcmenc_get_type (void);
135 GST_ELEMENT_REGISTER_DECLARE (adpcmenc);
136 G_DEFINE_TYPE_WITH_CODE (ADPCMEnc, adpcmenc, GST_TYPE_AUDIO_ENCODER,
137 GST_DEBUG_CATEGORY_INIT (adpcmenc_debug, "adpcmenc", 0, "ADPCM Encoders");
138 );
139 GST_ELEMENT_REGISTER_DEFINE (adpcmenc, "adpcmenc", GST_RANK_PRIMARY,
140 GST_TYPE_ADPCM_ENC);
141
142 static gboolean
adpcmenc_setup(ADPCMEnc * enc)143 adpcmenc_setup (ADPCMEnc * enc)
144 {
145 const int DVI_IMA_HEADER_SIZE = 4;
146 const int ADPCM_SAMPLES_PER_BYTE = 2;
147 guint64 sample_bytes;
148 const char *layout;
149 GstCaps *caps;
150 gboolean ret;
151
152 switch (enc->layout) {
153 case LAYOUT_ADPCM_DVI:
154 layout = "dvi";
155 /* IMA ADPCM includes a 4-byte header per channel, */
156 sample_bytes = enc->blocksize - (DVI_IMA_HEADER_SIZE * enc->channels);
157 /* two samples per byte, plus a single sample in the header. */
158 enc->samples_per_block =
159 ((sample_bytes * ADPCM_SAMPLES_PER_BYTE) / enc->channels) + 1;
160 break;
161 default:
162 GST_WARNING_OBJECT (enc, "Invalid layout");
163 return FALSE;
164 }
165
166 caps = gst_caps_new_simple ("audio/x-adpcm",
167 "rate", G_TYPE_INT, enc->rate,
168 "channels", G_TYPE_INT, enc->channels,
169 "layout", G_TYPE_STRING, layout,
170 "block_align", G_TYPE_INT, enc->blocksize, NULL);
171
172 ret = gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (enc), caps);
173 gst_caps_unref (caps);
174
175 /* Step index state is carried between blocks. */
176 enc->step_index[0] = 0;
177 enc->step_index[1] = 0;
178
179 return ret;
180 }
181
182 static gboolean
adpcmenc_set_format(GstAudioEncoder * benc,GstAudioInfo * info)183 adpcmenc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
184 {
185 ADPCMEnc *enc = (ADPCMEnc *) (benc);
186
187 enc->rate = GST_AUDIO_INFO_RATE (info);
188 enc->channels = GST_AUDIO_INFO_CHANNELS (info);
189
190 if (!adpcmenc_setup (enc))
191 return FALSE;
192
193 /* report needs to base class */
194 gst_audio_encoder_set_frame_samples_min (benc, enc->samples_per_block);
195 gst_audio_encoder_set_frame_samples_max (benc, enc->samples_per_block);
196 gst_audio_encoder_set_frame_max (benc, 1);
197
198 return TRUE;
199 }
200
201 static void
adpcmenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)202 adpcmenc_set_property (GObject * object,
203 guint prop_id, const GValue * value, GParamSpec * pspec)
204 {
205 ADPCMEnc *enc = GST_ADPCM_ENC (object);
206
207 switch (prop_id) {
208 case PROP_BLOCK_SIZE:
209 enc->blocksize = g_value_get_int (value);
210 break;
211 case PROP_LAYOUT:
212 enc->layout = g_value_get_enum (value);
213 break;
214 default:
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 break;
217 }
218 }
219
220 static void
adpcmenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)221 adpcmenc_get_property (GObject * object,
222 guint prop_id, GValue * value, GParamSpec * pspec)
223 {
224 ADPCMEnc *enc = GST_ADPCM_ENC (object);
225
226 switch (prop_id) {
227 case PROP_BLOCK_SIZE:
228 g_value_set_int (value, enc->blocksize);
229 break;
230 case PROP_LAYOUT:
231 g_value_set_enum (value, enc->layout);
232 break;
233 default:
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 break;
236 }
237 }
238
239 static guint8
adpcmenc_encode_ima_sample(gint16 sample,gint16 * prev_sample,guint8 * stepindex)240 adpcmenc_encode_ima_sample (gint16 sample, gint16 * prev_sample,
241 guint8 * stepindex)
242 {
243 const int NEGATIVE_SIGN_BIT = 0x8;
244 int diff, vpdiff, mask, step;
245 int bytecode = 0x0;
246 diff = sample - *prev_sample;
247 step = ima_step_size[*stepindex];
248 vpdiff = step >> 3;
249
250 if (diff < 0) {
251 diff = -diff;
252 bytecode = NEGATIVE_SIGN_BIT;
253 }
254
255 mask = 0x4;
256 while (mask > 0) {
257 if (diff >= step) {
258 bytecode |= mask;
259 diff -= step;
260 vpdiff += step;
261 }
262 step >>= 1;
263 mask >>= 1;
264 }
265
266 if (bytecode & 8) {
267 vpdiff = -vpdiff;
268 }
269
270 *prev_sample = CLAMP (*prev_sample + vpdiff, G_MININT16, G_MAXINT16);
271 *stepindex = CLAMP (*stepindex + ima_indx_adjust[bytecode], 0, 88);
272
273 return bytecode;
274 }
275
276 static gboolean
adpcmenc_encode_ima_block(ADPCMEnc * enc,const gint16 * samples,guint8 * outbuf)277 adpcmenc_encode_ima_block (ADPCMEnc * enc, const gint16 * samples,
278 guint8 * outbuf)
279 {
280 const int HEADER_SIZE = 4;
281 gint16 prev_sample[2] = { 0, 0 };
282 guint32 write_pos = 0;
283 guint32 read_pos = 0;
284 guint8 channel = 0;
285
286 /* Write a header for each channel.
287 * The header consists of a sixteen-bit predicted sound value,
288 * and an eight bit step_index, carried forward from any previous block.
289 * These allow seeking within the file.
290 */
291 for (channel = 0; channel < enc->channels; channel++) {
292 write_pos = channel * HEADER_SIZE;
293 outbuf[write_pos + 0] = (samples[channel] & 0xFF);
294 outbuf[write_pos + 1] = (samples[channel] >> 8) & 0xFF;
295 outbuf[write_pos + 2] = enc->step_index[channel];
296 outbuf[write_pos + 3] = 0;
297 prev_sample[channel] = samples[channel];
298 }
299
300 /* raw-audio looks like this for a stereo stream:
301 * [ L, R, L, R, L, R ... ]
302 * encoded audio is in eight-sample blocks, two samples to a byte thusly:
303 * [ LL, LL, LL, LL, RR, RR, RR, RR ... ]
304 */
305 write_pos = HEADER_SIZE * enc->channels;
306 read_pos = enc->channels; /* the first sample is in the header. */
307 while (write_pos < enc->blocksize) {
308 gint8 CHANNEL_CHUNK_SIZE = 8;
309 for (channel = 0; channel < enc->channels; channel++) {
310 /* convert eight samples (four bytes) per channel, then swap */
311 guint32 channel_chunk_base = read_pos + channel;
312 gint8 chunk;
313 for (chunk = 0; chunk < CHANNEL_CHUNK_SIZE; chunk++) {
314 guint8 packed_byte = 0, encoded_sample;
315 encoded_sample =
316 adpcmenc_encode_ima_sample (samples[channel_chunk_base +
317 (chunk * enc->channels)], &prev_sample[channel],
318 &enc->step_index[channel]);
319 packed_byte |= encoded_sample & 0x0F;
320
321 chunk++;
322
323 encoded_sample =
324 adpcmenc_encode_ima_sample (samples[channel_chunk_base +
325 (chunk * enc->channels)], &prev_sample[channel],
326 &enc->step_index[channel]);
327 packed_byte |= encoded_sample << 4 & 0xF0;
328
329 outbuf[write_pos++] = packed_byte;
330 }
331 }
332 /* advance to the next block of 8 samples per channel */
333 read_pos += CHANNEL_CHUNK_SIZE * enc->channels;
334 if (read_pos > enc->samples_per_block * enc->channels) {
335 GST_LOG ("Ran past the end. (Reading %i of %i.)", read_pos,
336 enc->samples_per_block);
337 }
338 }
339
340 return TRUE;
341 }
342
343 static GstBuffer *
adpcmenc_encode_block(ADPCMEnc * enc,const gint16 * samples,int blocksize)344 adpcmenc_encode_block (ADPCMEnc * enc, const gint16 * samples, int blocksize)
345 {
346 gboolean res = FALSE;
347 GstBuffer *outbuf = NULL;
348 GstMapInfo omap;
349
350 if (enc->layout == LAYOUT_ADPCM_DVI) {
351 outbuf = gst_buffer_new_and_alloc (enc->blocksize);
352 gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
353 res = adpcmenc_encode_ima_block (enc, samples, omap.data);
354 gst_buffer_unmap (outbuf, &omap);
355 } else {
356 /* should not happen afaics */
357 g_assert_not_reached ();
358 GST_WARNING_OBJECT (enc, "Unknown layout");
359 res = FALSE;
360 }
361
362 if (!res) {
363 if (outbuf)
364 gst_buffer_unref (outbuf);
365 outbuf = NULL;
366 GST_WARNING_OBJECT (enc, "Encode of block failed");
367 }
368
369 return outbuf;
370 }
371
372 static GstFlowReturn
adpcmenc_handle_frame(GstAudioEncoder * benc,GstBuffer * buffer)373 adpcmenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
374 {
375 ADPCMEnc *enc = (ADPCMEnc *) (benc);
376 GstFlowReturn ret = GST_FLOW_OK;
377 gint16 *samples;
378 GstBuffer *outbuf;
379 int input_bytes_per_block;
380 const int BYTES_PER_SAMPLE = 2;
381 GstMapInfo map;
382
383 /* we don't deal with squeezing remnants, so simply discard those */
384 if (G_UNLIKELY (buffer == NULL)) {
385 GST_DEBUG_OBJECT (benc, "no data");
386 goto done;
387 }
388
389 input_bytes_per_block =
390 enc->samples_per_block * BYTES_PER_SAMPLE * enc->channels;
391
392 gst_buffer_map (buffer, &map, GST_MAP_READ);
393 if (G_UNLIKELY (map.size < input_bytes_per_block)) {
394 GST_DEBUG_OBJECT (enc, "discarding trailing data %d", (gint) map.size);
395 gst_buffer_unmap (buffer, &map);
396 ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
397 goto done;
398 }
399
400 samples = (gint16 *) map.data;
401 outbuf = adpcmenc_encode_block (enc, samples, enc->blocksize);
402 gst_buffer_unmap (buffer, &map);
403
404 ret = gst_audio_encoder_finish_frame (benc, outbuf, enc->samples_per_block);
405
406 done:
407 return ret;
408 }
409
410 static gboolean
adpcmenc_start(GstAudioEncoder * enc)411 adpcmenc_start (GstAudioEncoder * enc)
412 {
413 GST_DEBUG_OBJECT (enc, "start");
414
415 return TRUE;
416 }
417
418 static gboolean
adpcmenc_stop(GstAudioEncoder * enc)419 adpcmenc_stop (GstAudioEncoder * enc)
420 {
421 GST_DEBUG_OBJECT (enc, "stop");
422
423 return TRUE;
424 }
425
426 static void
adpcmenc_init(ADPCMEnc * enc)427 adpcmenc_init (ADPCMEnc * enc)
428 {
429 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (enc));
430
431 /* Set defaults. */
432 enc->blocksize = DEFAULT_ADPCM_BLOCK_SIZE;
433 enc->layout = DEFAULT_ADPCM_LAYOUT;
434 }
435
436 static void
adpcmenc_class_init(ADPCMEncClass * klass)437 adpcmenc_class_init (ADPCMEncClass * klass)
438 {
439 GObjectClass *gobjectclass = (GObjectClass *) klass;
440 GstElementClass *element_class = (GstElementClass *) klass;
441 GstAudioEncoderClass *base_class = (GstAudioEncoderClass *) klass;
442
443 gobjectclass->set_property = adpcmenc_set_property;
444 gobjectclass->get_property = adpcmenc_get_property;
445
446 gst_element_class_add_static_pad_template (element_class,
447 &adpcmenc_sink_template);
448 gst_element_class_add_static_pad_template (element_class,
449 &adpcmenc_src_template);
450 gst_element_class_set_static_metadata (element_class, "ADPCM encoder",
451 "Codec/Encoder/Audio", "Encode ADPCM audio",
452 "Pioneers of the Inevitable <songbird@songbirdnest.com>");
453
454 base_class->start = GST_DEBUG_FUNCPTR (adpcmenc_start);
455 base_class->stop = GST_DEBUG_FUNCPTR (adpcmenc_stop);
456 base_class->set_format = GST_DEBUG_FUNCPTR (adpcmenc_set_format);
457 base_class->handle_frame = GST_DEBUG_FUNCPTR (adpcmenc_handle_frame);
458
459 g_object_class_install_property (gobjectclass, PROP_LAYOUT,
460 g_param_spec_enum ("layout", "Layout",
461 "Layout for output stream",
462 GST_TYPE_ADPCMENC_LAYOUT, DEFAULT_ADPCM_LAYOUT,
463 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
464
465 g_object_class_install_property (gobjectclass, PROP_BLOCK_SIZE,
466 g_param_spec_int ("blockalign", "Block Align",
467 "Block size for output stream",
468 MIN_ADPCM_BLOCK_SIZE, MAX_ADPCM_BLOCK_SIZE,
469 DEFAULT_ADPCM_BLOCK_SIZE,
470 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
471
472 gst_type_mark_as_plugin_api (GST_TYPE_ADPCMENC_LAYOUT, 0);
473 }
474
475 static gboolean
plugin_init(GstPlugin * plugin)476 plugin_init (GstPlugin * plugin)
477 {
478 return GST_ELEMENT_REGISTER (adpcmenc, plugin);
479 }
480
481 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, adpcmenc,
482 "ADPCM encoder", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
483 GST_PACKAGE_ORIGIN);
484