40
40
#include "libavutil/intreadwrite.h"
41
41
#include "avcodec.h"
42
42
#include "bytestream.h"
44
46
typedef struct DPCMContext {
47
48
int16_t roq_square_array[256];
48
49
int sample[2]; ///< previous sample (for SOL_DPCM)
49
50
const int8_t *sol_table; ///< delta table for SOL_DPCM
122
123
return AVERROR(EINVAL);
125
s->channels = avctx->channels;
126
126
s->sample[0] = s->sample[1] = 0;
128
128
switch(avctx->codec->id) {
130
case CODEC_ID_ROQ_DPCM:
130
case AV_CODEC_ID_ROQ_DPCM:
131
131
/* initialize square table */
132
132
for (i = 0; i < 128; i++) {
133
133
int16_t square = i * i;
161
if (avctx->codec->id == CODEC_ID_SOL_DPCM && avctx->codec_tag != 3)
161
if (avctx->codec->id == AV_CODEC_ID_SOL_DPCM && avctx->codec_tag != 3)
162
162
avctx->sample_fmt = AV_SAMPLE_FMT_U8;
164
164
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
173
173
static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
174
174
int *got_frame_ptr, AVPacket *avpkt)
176
const uint8_t *buf = avpkt->data;
177
176
int buf_size = avpkt->size;
178
const uint8_t *buf_end = buf + buf_size;
179
177
DPCMContext *s = avctx->priv_data;
180
178
int out = 0, ret;
181
179
int predictor[2];
183
int stereo = s->channels - 1;
184
int16_t *output_samples;
181
int stereo = avctx->channels - 1;
182
int16_t *output_samples, *samples_end;
186
if (stereo && (buf_size & 1)) {
185
if (stereo && (buf_size & 1))
187
bytestream2_init(&gb, avpkt->data, buf_size);
191
189
/* calculate output size */
192
190
switch(avctx->codec->id) {
193
case CODEC_ID_ROQ_DPCM:
191
case AV_CODEC_ID_ROQ_DPCM:
194
192
out = buf_size - 8;
196
case CODEC_ID_INTERPLAY_DPCM:
197
out = buf_size - 6 - s->channels;
199
case CODEC_ID_XAN_DPCM:
200
out = buf_size - 2 * s->channels;
202
case CODEC_ID_SOL_DPCM:
194
case AV_CODEC_ID_INTERPLAY_DPCM:
195
out = buf_size - 6 - avctx->channels;
197
case AV_CODEC_ID_XAN_DPCM:
198
out = buf_size - 2 * avctx->channels;
200
case AV_CODEC_ID_SOL_DPCM:
203
201
if (avctx->codec_tag != 3)
204
202
out = buf_size * 2;
214
212
/* get output buffer */
215
s->frame.nb_samples = out / s->channels;
216
if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
213
s->frame.nb_samples = out / avctx->channels;
214
if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
217
215
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
220
218
output_samples = (int16_t *)s->frame.data[0];
219
samples_end = output_samples + out;
222
221
switch(avctx->codec->id) {
224
case CODEC_ID_ROQ_DPCM:
223
case AV_CODEC_ID_ROQ_DPCM:
224
bytestream2_skipu(&gb, 6);
228
predictor[1] = (int16_t)(bytestream_get_byte(&buf) << 8);
229
predictor[0] = (int16_t)(bytestream_get_byte(&buf) << 8);
227
predictor[1] = sign_extend(bytestream2_get_byteu(&gb) << 8, 16);
228
predictor[0] = sign_extend(bytestream2_get_byteu(&gb) << 8, 16);
231
predictor[0] = (int16_t)bytestream_get_le16(&buf);
230
predictor[0] = sign_extend(bytestream2_get_le16u(&gb), 16);
234
233
/* decode the samples */
235
while (buf < buf_end) {
236
predictor[ch] += s->roq_square_array[*buf++];
234
while (output_samples < samples_end) {
235
predictor[ch] += s->roq_square_array[bytestream2_get_byteu(&gb)];
237
236
predictor[ch] = av_clip_int16(predictor[ch]);
238
237
*output_samples++ = predictor[ch];
245
case CODEC_ID_INTERPLAY_DPCM:
246
buf += 6; /* skip over the stream mask and stream length */
244
case AV_CODEC_ID_INTERPLAY_DPCM:
245
bytestream2_skipu(&gb, 6); /* skip over the stream mask and stream length */
248
for (ch = 0; ch < s->channels; ch++) {
249
predictor[ch] = (int16_t)bytestream_get_le16(&buf);
247
for (ch = 0; ch < avctx->channels; ch++) {
248
predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
250
249
*output_samples++ = predictor[ch];
254
while (buf < buf_end) {
255
predictor[ch] += interplay_delta_table[*buf++];
253
while (output_samples < samples_end) {
254
predictor[ch] += interplay_delta_table[bytestream2_get_byteu(&gb)];
256
255
predictor[ch] = av_clip_int16(predictor[ch]);
257
256
*output_samples++ = predictor[ch];
264
case CODEC_ID_XAN_DPCM:
263
case AV_CODEC_ID_XAN_DPCM:
266
265
int shift[2] = { 4, 4 };
268
for (ch = 0; ch < s->channels; ch++)
269
predictor[ch] = (int16_t)bytestream_get_le16(&buf);
267
for (ch = 0; ch < avctx->channels; ch++)
268
predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
272
while (buf < buf_end) {
274
int16_t diff = (n & 0xFC) << 8;
271
while (output_samples < samples_end) {
272
int diff = bytestream2_get_byteu(&gb);
278
shift[ch] -= (2 * (n & 3));
278
shift[ch] -= (2 * n);
279
diff = sign_extend((diff &~ 3) << 8, 16);
279
281
/* saturate the shifter to a lower limit of 0 */
280
282
if (shift[ch] < 0)
294
case CODEC_ID_SOL_DPCM:
296
case AV_CODEC_ID_SOL_DPCM:
295
297
if (avctx->codec_tag != 3) {
296
uint8_t *output_samples_u8 = s->frame.data[0];
297
while (buf < buf_end) {
298
uint8_t *output_samples_u8 = s->frame.data[0],
299
*samples_end_u8 = output_samples_u8 + out;
300
while (output_samples_u8 < samples_end_u8) {
301
int n = bytestream2_get_byteu(&gb);
300
303
s->sample[0] += s->sol_table[n >> 4];
301
304
s->sample[0] = av_clip_uint8(s->sample[0]);
306
309
*output_samples_u8++ = s->sample[stereo];
309
while (buf < buf_end) {
312
while (output_samples < samples_end) {
313
int n = bytestream2_get_byteu(&gb);
311
314
if (n & 0x80) s->sample[ch] -= sol_table_16[n & 0x7F];
312
315
else s->sample[ch] += sol_table_16[n & 0x7F];
313
316
s->sample[ch] = av_clip_int16(s->sample[ch]);
337
340
.long_name = NULL_IF_CONFIG_SMALL(long_name_), \
340
DPCM_DECODER(CODEC_ID_INTERPLAY_DPCM, interplay_dpcm, "DPCM Interplay");
341
DPCM_DECODER(CODEC_ID_ROQ_DPCM, roq_dpcm, "DPCM id RoQ");
342
DPCM_DECODER(CODEC_ID_SOL_DPCM, sol_dpcm, "DPCM Sol");
343
DPCM_DECODER(CODEC_ID_XAN_DPCM, xan_dpcm, "DPCM Xan");
343
DPCM_DECODER(AV_CODEC_ID_INTERPLAY_DPCM, interplay_dpcm, "DPCM Interplay");
344
DPCM_DECODER(AV_CODEC_ID_ROQ_DPCM, roq_dpcm, "DPCM id RoQ");
345
DPCM_DECODER(AV_CODEC_ID_SOL_DPCM, sol_dpcm, "DPCM Sol");
346
DPCM_DECODER(AV_CODEC_ID_XAN_DPCM, xan_dpcm, "DPCM Xan");