~ubuntu-branches/ubuntu/trusty/gst-libav1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavcodec/dpcm.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-09-24 17:07:00 UTC
  • mfrom: (1.1.17) (7.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130924170700-4dg62s3pwl0pdakz
Tags: 1.2.0-1
* New upstream stable release:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include "libavutil/intreadwrite.h"
41
41
#include "avcodec.h"
42
42
#include "bytestream.h"
 
43
#include "internal.h"
 
44
#include "mathops.h"
43
45
 
44
46
typedef struct DPCMContext {
45
47
    AVFrame frame;
46
 
    int channels;
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);
123
124
    }
124
125
 
125
 
    s->channels = avctx->channels;
126
126
    s->sample[0] = s->sample[1] = 0;
127
127
 
128
128
    switch(avctx->codec->id) {
129
129
 
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;
136
136
        }
137
137
        break;
138
138
 
139
 
    case CODEC_ID_SOL_DPCM:
 
139
    case AV_CODEC_ID_SOL_DPCM:
140
140
        switch(avctx->codec_tag){
141
141
        case 1:
142
142
            s->sol_table = sol_table_old;
158
158
        break;
159
159
    }
160
160
 
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;
163
163
    else
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)
175
175
{
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];
182
180
    int ch = 0;
183
 
    int stereo = s->channels - 1;
184
 
    int16_t *output_samples;
 
181
    int stereo = avctx->channels - 1;
 
182
    int16_t *output_samples, *samples_end;
 
183
    GetByteContext gb;
185
184
 
186
 
    if (stereo && (buf_size & 1)) {
 
185
    if (stereo && (buf_size & 1))
187
186
        buf_size--;
188
 
        buf_end--;
189
 
    }
 
187
    bytestream2_init(&gb, avpkt->data, buf_size);
190
188
 
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;
195
193
        break;
196
 
    case CODEC_ID_INTERPLAY_DPCM:
197
 
        out = buf_size - 6 - s->channels;
198
 
        break;
199
 
    case CODEC_ID_XAN_DPCM:
200
 
        out = buf_size - 2 * s->channels;
201
 
        break;
202
 
    case CODEC_ID_SOL_DPCM:
 
194
    case AV_CODEC_ID_INTERPLAY_DPCM:
 
195
        out = buf_size - 6 - avctx->channels;
 
196
        break;
 
197
    case AV_CODEC_ID_XAN_DPCM:
 
198
        out = buf_size - 2 * avctx->channels;
 
199
        break;
 
200
    case AV_CODEC_ID_SOL_DPCM:
203
201
        if (avctx->codec_tag != 3)
204
202
            out = buf_size * 2;
205
203
        else
212
210
    }
213
211
 
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");
218
216
        return ret;
219
217
    }
220
218
    output_samples = (int16_t *)s->frame.data[0];
 
219
    samples_end = output_samples + out;
221
220
 
222
221
    switch(avctx->codec->id) {
223
222
 
224
 
    case CODEC_ID_ROQ_DPCM:
225
 
        buf += 6;
 
223
    case AV_CODEC_ID_ROQ_DPCM:
 
224
        bytestream2_skipu(&gb, 6);
226
225
 
227
226
        if (stereo) {
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);
230
229
        } else {
231
 
            predictor[0] = (int16_t)bytestream_get_le16(&buf);
 
230
            predictor[0] = sign_extend(bytestream2_get_le16u(&gb), 16);
232
231
        }
233
232
 
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];
239
238
 
242
241
        }
243
242
        break;
244
243
 
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 */
247
246
 
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];
251
250
        }
252
251
 
253
252
        ch = 0;
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];
258
257
 
261
260
        }
262
261
        break;
263
262
 
264
 
    case CODEC_ID_XAN_DPCM:
 
263
    case AV_CODEC_ID_XAN_DPCM:
265
264
    {
266
265
        int shift[2] = { 4, 4 };
267
266
 
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);
270
269
 
271
270
        ch = 0;
272
 
        while (buf < buf_end) {
273
 
            uint8_t n = *buf++;
274
 
            int16_t diff = (n & 0xFC) << 8;
275
 
            if ((n & 0x03) == 3)
 
271
        while (output_samples < samples_end) {
 
272
            int diff = bytestream2_get_byteu(&gb);
 
273
            int n    = diff & 3;
 
274
 
 
275
            if (n == 3)
276
276
                shift[ch]++;
277
277
            else
278
 
                shift[ch] -= (2 * (n & 3));
 
278
                shift[ch] -= (2 * n);
 
279
            diff = sign_extend((diff &~ 3) << 8, 16);
 
280
 
279
281
            /* saturate the shifter to a lower limit of 0 */
280
282
            if (shift[ch] < 0)
281
283
                shift[ch] = 0;
291
293
        }
292
294
        break;
293
295
    }
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 n = *buf++;
 
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);
299
302
 
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];
307
310
            }
308
311
        } else {
309
 
            while (buf < buf_end) {
310
 
                uint8_t n = *buf++;
 
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_),     \
338
341
}
339
342
 
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");