~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/dpcm.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
#include "libavutil/intreadwrite.h"
41
41
#include "avcodec.h"
 
42
#include "bytestream.h"
42
43
 
43
44
typedef struct DPCMContext {
 
45
    AVFrame frame;
44
46
    int channels;
45
 
    short roq_square_array[256];
46
 
    long sample[2];//for SOL_DPCM
47
 
    const int *sol_table;//for SOL_DPCM
 
47
    int16_t roq_square_array[256];
 
48
    int sample[2];                  ///< previous sample (for SOL_DPCM)
 
49
    const int8_t *sol_table;        ///< delta table for SOL_DPCM
48
50
} DPCMContext;
49
51
 
50
 
#define SE_16BIT(x)  if (x & 0x8000) x -= 0x10000;
51
 
 
52
 
static const int interplay_delta_table[] = {
 
52
static const int16_t interplay_delta_table[] = {
53
53
         0,      1,      2,      3,      4,      5,      6,      7,
54
54
         8,      9,     10,     11,     12,     13,     14,     15,
55
55
        16,     17,     18,     19,     20,     21,     22,     23,
85
85
 
86
86
};
87
87
 
88
 
static const int sol_table_old[16] =
89
 
    { 0x0,  0x1,  0x2 , 0x3,  0x6,  0xA,  0xF, 0x15,
90
 
    -0x15, -0xF, -0xA, -0x6, -0x3, -0x2, -0x1, 0x0};
91
 
 
92
 
static const int sol_table_new[16] =
93
 
    { 0x0,  0x1,  0x2,  0x3,  0x6,  0xA,  0xF,  0x15,
94
 
      0x0, -0x1, -0x2, -0x3, -0x6, -0xA, -0xF, -0x15};
95
 
 
96
 
static const int sol_table_16[128] = {
 
88
static const int8_t sol_table_old[16] = {
 
89
      0x0,  0x1,  0x2,  0x3,  0x6,  0xA,  0xF, 0x15,
 
90
    -0x15, -0xF, -0xA, -0x6, -0x3, -0x2, -0x1,  0x0
 
91
};
 
92
 
 
93
static const int8_t sol_table_new[16] = {
 
94
    0x0,  0x1,  0x2,  0x3,  0x6,  0xA,  0xF,  0x15,
 
95
    0x0, -0x1, -0x2, -0x3, -0x6, -0xA, -0xF, -0x15
 
96
};
 
97
 
 
98
static const int16_t sol_table_16[128] = {
97
99
    0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080,
98
100
    0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120,
99
101
    0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0,
110
112
};
111
113
 
112
114
 
113
 
 
114
115
static av_cold int dpcm_decode_init(AVCodecContext *avctx)
115
116
{
116
117
    DPCMContext *s = avctx->priv_data;
117
118
    int i;
118
 
    short square;
 
119
 
 
120
    if (avctx->channels < 1 || avctx->channels > 2) {
 
121
        av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
 
122
        return AVERROR(EINVAL);
 
123
    }
119
124
 
120
125
    s->channels = avctx->channels;
121
126
    s->sample[0] = s->sample[1] = 0;
125
130
    case CODEC_ID_ROQ_DPCM:
126
131
        /* initialize square table */
127
132
        for (i = 0; i < 128; i++) {
128
 
            square = i * i;
129
 
            s->roq_square_array[i] = square;
 
133
            int16_t square = i * i;
 
134
            s->roq_square_array[i      ] =  square;
130
135
            s->roq_square_array[i + 128] = -square;
131
136
        }
132
137
        break;
133
138
 
134
 
 
135
139
    case CODEC_ID_SOL_DPCM:
136
140
        switch(avctx->codec_tag){
137
141
        case 1:
138
 
            s->sol_table=sol_table_old;
 
142
            s->sol_table = sol_table_old;
139
143
            s->sample[0] = s->sample[1] = 0x80;
140
144
            break;
141
145
        case 2:
142
 
            s->sol_table=sol_table_new;
 
146
            s->sol_table = sol_table_new;
143
147
            s->sample[0] = s->sample[1] = 0x80;
144
148
            break;
145
149
        case 3:
146
 
            s->sol_table=sol_table_16;
147
150
            break;
148
151
        default:
149
152
            av_log(avctx, AV_LOG_ERROR, "Unknown SOL subcodec\n");
155
158
        break;
156
159
    }
157
160
 
158
 
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
 
161
    if (avctx->codec->id == CODEC_ID_SOL_DPCM && avctx->codec_tag != 3)
 
162
        avctx->sample_fmt = AV_SAMPLE_FMT_U8;
 
163
    else
 
164
        avctx->sample_fmt = AV_SAMPLE_FMT_S16;
 
165
 
 
166
    avcodec_get_frame_defaults(&s->frame);
 
167
    avctx->coded_frame = &s->frame;
 
168
 
159
169
    return 0;
160
170
}
161
171
 
162
 
static int dpcm_decode_frame(AVCodecContext *avctx,
163
 
                             void *data, int *data_size,
164
 
                             AVPacket *avpkt)
 
172
 
 
173
static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
 
174
                             int *got_frame_ptr, AVPacket *avpkt)
165
175
{
166
176
    const uint8_t *buf = avpkt->data;
167
177
    int buf_size = avpkt->size;
 
178
    const uint8_t *buf_end = buf + buf_size;
168
179
    DPCMContext *s = avctx->priv_data;
169
 
    int in, out = 0;
 
180
    int out = 0, ret;
170
181
    int predictor[2];
171
 
    int channel_number = 0;
172
 
    short *output_samples = data;
173
 
    int shift[2];
174
 
    unsigned char byte;
175
 
    short diff;
176
 
 
177
 
    if (!buf_size)
178
 
        return 0;
179
 
 
180
 
    // almost every DPCM variant expands one byte of data into two
181
 
    if(*data_size/2 < buf_size)
182
 
        return -1;
183
 
 
184
 
    switch(avctx->codec->id) {
185
 
 
186
 
    case CODEC_ID_ROQ_DPCM:
187
 
        if (s->channels == 1)
188
 
            predictor[0] = AV_RL16(&buf[6]);
189
 
        else {
190
 
            predictor[0] = buf[7] << 8;
191
 
            predictor[1] = buf[6] << 8;
 
182
    int ch = 0;
 
183
    int stereo = s->channels - 1;
 
184
    int16_t *output_samples;
 
185
 
 
186
    /* calculate output size */
 
187
    switch(avctx->codec->id) {
 
188
    case CODEC_ID_ROQ_DPCM:
 
189
        out = buf_size - 8;
 
190
        break;
 
191
    case CODEC_ID_INTERPLAY_DPCM:
 
192
        out = buf_size - 6 - s->channels;
 
193
        break;
 
194
    case CODEC_ID_XAN_DPCM:
 
195
        out = buf_size - 2 * s->channels;
 
196
        break;
 
197
    case CODEC_ID_SOL_DPCM:
 
198
        if (avctx->codec_tag != 3)
 
199
            out = buf_size * 2;
 
200
        else
 
201
            out = buf_size;
 
202
        break;
 
203
    }
 
204
    if (out <= 0) {
 
205
        av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
 
206
        return AVERROR(EINVAL);
 
207
    }
 
208
 
 
209
    /* get output buffer */
 
210
    s->frame.nb_samples = out / s->channels;
 
211
    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
 
212
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
 
213
        return ret;
 
214
    }
 
215
    output_samples = (int16_t *)s->frame.data[0];
 
216
 
 
217
    switch(avctx->codec->id) {
 
218
 
 
219
    case CODEC_ID_ROQ_DPCM:
 
220
        buf += 6;
 
221
 
 
222
        if (stereo) {
 
223
            predictor[1] = (int16_t)(bytestream_get_byte(&buf) << 8);
 
224
            predictor[0] = (int16_t)(bytestream_get_byte(&buf) << 8);
 
225
        } else {
 
226
            predictor[0] = (int16_t)bytestream_get_le16(&buf);
192
227
        }
193
 
        SE_16BIT(predictor[0]);
194
 
        SE_16BIT(predictor[1]);
195
228
 
196
229
        /* decode the samples */
197
 
        for (in = 8, out = 0; in < buf_size; in++, out++) {
198
 
            predictor[channel_number] += s->roq_square_array[buf[in]];
199
 
            predictor[channel_number] = av_clip_int16(predictor[channel_number]);
200
 
            output_samples[out] = predictor[channel_number];
 
230
        while (buf < buf_end) {
 
231
            predictor[ch] += s->roq_square_array[*buf++];
 
232
            predictor[ch]  = av_clip_int16(predictor[ch]);
 
233
            *output_samples++ = predictor[ch];
201
234
 
202
235
            /* toggle channel */
203
 
            channel_number ^= s->channels - 1;
 
236
            ch ^= stereo;
204
237
        }
205
238
        break;
206
239
 
207
240
    case CODEC_ID_INTERPLAY_DPCM:
208
 
        in = 6;  /* skip over the stream mask and stream length */
209
 
        predictor[0] = AV_RL16(&buf[in]);
210
 
        in += 2;
211
 
        SE_16BIT(predictor[0])
212
 
        output_samples[out++] = predictor[0];
213
 
        if (s->channels == 2) {
214
 
            predictor[1] = AV_RL16(&buf[in]);
215
 
            in += 2;
216
 
            SE_16BIT(predictor[1])
217
 
            output_samples[out++] = predictor[1];
 
241
        buf += 6;  /* skip over the stream mask and stream length */
 
242
 
 
243
        for (ch = 0; ch < s->channels; ch++) {
 
244
            predictor[ch] = (int16_t)bytestream_get_le16(&buf);
 
245
            *output_samples++ = predictor[ch];
218
246
        }
219
247
 
220
 
        while (in < buf_size) {
221
 
            predictor[channel_number] += interplay_delta_table[buf[in++]];
222
 
            predictor[channel_number] = av_clip_int16(predictor[channel_number]);
223
 
            output_samples[out++] = predictor[channel_number];
 
248
        ch = 0;
 
249
        while (buf < buf_end) {
 
250
            predictor[ch] += interplay_delta_table[*buf++];
 
251
            predictor[ch]  = av_clip_int16(predictor[ch]);
 
252
            *output_samples++ = predictor[ch];
224
253
 
225
254
            /* toggle channel */
226
 
            channel_number ^= s->channels - 1;
 
255
            ch ^= stereo;
227
256
        }
228
 
 
229
257
        break;
230
258
 
231
259
    case CODEC_ID_XAN_DPCM:
232
 
        in = 0;
233
 
        shift[0] = shift[1] = 4;
234
 
        predictor[0] = AV_RL16(&buf[in]);
235
 
        in += 2;
236
 
        SE_16BIT(predictor[0]);
237
 
        if (s->channels == 2) {
238
 
            predictor[1] = AV_RL16(&buf[in]);
239
 
            in += 2;
240
 
            SE_16BIT(predictor[1]);
241
 
        }
242
 
 
243
 
        while (in < buf_size) {
244
 
            byte = buf[in++];
245
 
            diff = (byte & 0xFC) << 8;
246
 
            if ((byte & 0x03) == 3)
247
 
                shift[channel_number]++;
 
260
    {
 
261
        int shift[2] = { 4, 4 };
 
262
 
 
263
        for (ch = 0; ch < s->channels; ch++)
 
264
            predictor[ch] = (int16_t)bytestream_get_le16(&buf);
 
265
 
 
266
        ch = 0;
 
267
        while (buf < buf_end) {
 
268
            uint8_t n = *buf++;
 
269
            int16_t diff = (n & 0xFC) << 8;
 
270
            if ((n & 0x03) == 3)
 
271
                shift[ch]++;
248
272
            else
249
 
                shift[channel_number] -= (2 * (byte & 3));
 
273
                shift[ch] -= (2 * (n & 3));
250
274
            /* saturate the shifter to a lower limit of 0 */
251
 
            if (shift[channel_number] < 0)
252
 
                shift[channel_number] = 0;
253
 
 
254
 
            diff >>= shift[channel_number];
255
 
            predictor[channel_number] += diff;
256
 
 
257
 
            predictor[channel_number] = av_clip_int16(predictor[channel_number]);
258
 
            output_samples[out++] = predictor[channel_number];
 
275
            if (shift[ch] < 0)
 
276
                shift[ch] = 0;
 
277
 
 
278
            diff >>= shift[ch];
 
279
            predictor[ch] += diff;
 
280
 
 
281
            predictor[ch] = av_clip_int16(predictor[ch]);
 
282
            *output_samples++ = predictor[ch];
259
283
 
260
284
            /* toggle channel */
261
 
            channel_number ^= s->channels - 1;
 
285
            ch ^= stereo;
262
286
        }
263
287
        break;
 
288
    }
264
289
    case CODEC_ID_SOL_DPCM:
265
 
        in = 0;
266
290
        if (avctx->codec_tag != 3) {
267
 
            if(*data_size/4 < buf_size)
268
 
                return -1;
269
 
            while (in < buf_size) {
270
 
                int n1, n2;
271
 
                n1 = (buf[in] >> 4) & 0xF;
272
 
                n2 = buf[in++] & 0xF;
273
 
                s->sample[0] += s->sol_table[n1];
274
 
                if (s->sample[0] < 0) s->sample[0] = 0;
275
 
                if (s->sample[0] > 255) s->sample[0] = 255;
276
 
                output_samples[out++] = (s->sample[0] - 128) << 8;
277
 
                s->sample[s->channels - 1] += s->sol_table[n2];
278
 
                if (s->sample[s->channels - 1] < 0) s->sample[s->channels - 1] = 0;
279
 
                if (s->sample[s->channels - 1] > 255) s->sample[s->channels - 1] = 255;
280
 
                output_samples[out++] = (s->sample[s->channels - 1] - 128) << 8;
 
291
            uint8_t *output_samples_u8 = s->frame.data[0];
 
292
            while (buf < buf_end) {
 
293
                uint8_t n = *buf++;
 
294
 
 
295
                s->sample[0] += s->sol_table[n >> 4];
 
296
                s->sample[0]  = av_clip_uint8(s->sample[0]);
 
297
                *output_samples_u8++ = s->sample[0];
 
298
 
 
299
                s->sample[stereo] += s->sol_table[n & 0x0F];
 
300
                s->sample[stereo]  = av_clip_uint8(s->sample[stereo]);
 
301
                *output_samples_u8++ = s->sample[stereo];
281
302
            }
282
303
        } else {
283
 
            while (in < buf_size) {
284
 
                int n;
285
 
                n = buf[in++];
286
 
                if (n & 0x80) s->sample[channel_number] -= s->sol_table[n & 0x7F];
287
 
                else s->sample[channel_number] += s->sol_table[n & 0x7F];
288
 
                s->sample[channel_number] = av_clip_int16(s->sample[channel_number]);
289
 
                output_samples[out++] = s->sample[channel_number];
 
304
            while (buf < buf_end) {
 
305
                uint8_t n = *buf++;
 
306
                if (n & 0x80) s->sample[ch] -= sol_table_16[n & 0x7F];
 
307
                else          s->sample[ch] += sol_table_16[n & 0x7F];
 
308
                s->sample[ch] = av_clip_int16(s->sample[ch]);
 
309
                *output_samples++ = s->sample[ch];
290
310
                /* toggle channel */
291
 
                channel_number ^= s->channels - 1;
 
311
                ch ^= stereo;
292
312
            }
293
313
        }
294
314
        break;
295
315
    }
296
316
 
297
 
    *data_size = out * sizeof(short);
 
317
    *got_frame_ptr   = 1;
 
318
    *(AVFrame *)data = s->frame;
 
319
 
298
320
    return buf_size;
299
321
}
300
322
 
301
 
#define DPCM_DECODER(id, name, long_name_)      \
302
 
AVCodec ff_ ## name ## _decoder = {             \
303
 
    #name,                                      \
304
 
    AVMEDIA_TYPE_AUDIO,                         \
305
 
    id,                                         \
306
 
    sizeof(DPCMContext),                        \
307
 
    dpcm_decode_init,                           \
308
 
    NULL,                                       \
309
 
    NULL,                                       \
310
 
    dpcm_decode_frame,                          \
311
 
    .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
 
323
#define DPCM_DECODER(id_, name_, long_name_)                \
 
324
AVCodec ff_ ## name_ ## _decoder = {                        \
 
325
    .name           = #name_,                               \
 
326
    .type           = AVMEDIA_TYPE_AUDIO,                   \
 
327
    .id             = id_,                                  \
 
328
    .priv_data_size = sizeof(DPCMContext),                  \
 
329
    .init           = dpcm_decode_init,                     \
 
330
    .decode         = dpcm_decode_frame,                    \
 
331
    .capabilities   = CODEC_CAP_DR1,                        \
 
332
    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
312
333
}
313
334
 
314
335
DPCM_DECODER(CODEC_ID_INTERPLAY_DPCM, interplay_dpcm, "DPCM Interplay");
315
 
DPCM_DECODER(CODEC_ID_ROQ_DPCM, roq_dpcm, "DPCM id RoQ");
316
 
DPCM_DECODER(CODEC_ID_SOL_DPCM, sol_dpcm, "DPCM Sol");
317
 
DPCM_DECODER(CODEC_ID_XAN_DPCM, xan_dpcm, "DPCM Xan");
 
336
DPCM_DECODER(CODEC_ID_ROQ_DPCM,       roq_dpcm,       "DPCM id RoQ");
 
337
DPCM_DECODER(CODEC_ID_SOL_DPCM,       sol_dpcm,       "DPCM Sol");
 
338
DPCM_DECODER(CODEC_ID_XAN_DPCM,       xan_dpcm,       "DPCM Xan");