~ubuntu-branches/ubuntu/trusty/libav/trusty-proposed

« back to all changes in this revision

Viewing changes to libavcodec/dsicinav.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2013-10-22 23:24:08 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: package-import@ubuntu.com-20131022232408-b8tvvn4pyzri9mi3
Tags: 6:9.10-1ubuntu1
* Build all -extra flavors from this source package, as libav got demoted
  from main to universe, cf LP: #1243235
* Simplify debian/rules to follow exactly the code that debian executes
* New upstream (LP: #1180288) fixes lots of security issues (LP: #1242802)
* Merge from unstable, remaining changes:
  - build-depend on libtiff5-dev rather than libtiff4-dev,
    avoids FTBFS caused by imlib
  - follow the regular debian codepaths

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 * Delphine Software International CIN audio/video decoders
25
25
 */
26
26
 
 
27
#include "libavutil/channel_layout.h"
27
28
#include "avcodec.h"
28
29
#include "bytestream.h"
 
30
#include "internal.h"
29
31
#include "mathops.h"
30
32
 
31
33
 
93
95
    unsigned int i;
94
96
 
95
97
    cin->avctx = avctx;
96
 
    avctx->pix_fmt = PIX_FMT_PAL8;
 
98
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
97
99
 
98
100
    cin->frame.data[0] = NULL;
99
101
 
107
109
    return 0;
108
110
}
109
111
 
110
 
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, int size)
 
112
static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
 
113
                                 int size)
111
114
{
112
115
    while (size--)
113
116
        *dst++ += *src++;
114
117
}
115
118
 
116
 
static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
 
119
static int cin_decode_huffman(const unsigned char *src, int src_size,
 
120
                              unsigned char *dst, int dst_size)
117
121
{
118
122
    int b, huff_code = 0;
119
123
    unsigned char huff_code_table[15];
120
 
    unsigned char *dst_cur = dst;
121
 
    unsigned char *dst_end = dst + dst_size;
 
124
    unsigned char *dst_cur       = dst;
 
125
    unsigned char *dst_end       = dst + dst_size;
122
126
    const unsigned char *src_end = src + src_size;
123
127
 
124
 
    memcpy(huff_code_table, src, 15); src += 15; src_size -= 15;
 
128
    memcpy(huff_code_table, src, 15);
 
129
    src += 15;
125
130
 
126
131
    while (src < src_end) {
127
132
        huff_code = *src++;
128
133
        if ((huff_code >> 4) == 15) {
129
 
            b = huff_code << 4;
130
 
            huff_code = *src++;
 
134
            b          = huff_code << 4;
 
135
            huff_code  = *src++;
131
136
            *dst_cur++ = b | (huff_code >> 4);
132
137
        } else
133
138
            *dst_cur++ = huff_code_table[huff_code >> 4];
146
151
    return dst_cur - dst;
147
152
}
148
153
 
149
 
static int cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
 
154
static int cin_decode_lzss(const unsigned char *src, int src_size,
 
155
                           unsigned char *dst, int dst_size)
150
156
{
151
157
    uint16_t cmd;
152
158
    int i, sz, offset, code;
153
 
    unsigned char *dst_end = dst + dst_size, *dst_start = dst;
 
159
    unsigned char *dst_end       = dst + dst_size, *dst_start = dst;
154
160
    const unsigned char *src_end = src + src_size;
155
161
 
156
162
    while (src < src_end && dst < dst_end) {
159
165
            if (code & (1 << i)) {
160
166
                *dst++ = *src++;
161
167
            } else {
162
 
                cmd = AV_RL16(src); src += 2;
 
168
                cmd    = AV_RL16(src);
 
169
                src   += 2;
163
170
                offset = cmd >> 4;
164
 
                if ((int) (dst - dst_start) < offset + 1)
 
171
                if ((int)(dst - dst_start) < offset + 1)
165
172
                    return AVERROR_INVALIDDATA;
166
173
                sz = (cmd & 0xF) + 2;
167
 
                /* don't use memcpy/memmove here as the decoding routine (ab)uses */
168
 
                /* buffer overlappings to repeat bytes in the destination */
 
174
                /* don't use memcpy/memmove here as the decoding routine
 
175
                 * (ab)uses buffer overlappings to repeat bytes in the
 
176
                 * destination */
169
177
                sz = FFMIN(sz, dst_end - dst);
170
178
                while (sz--) {
171
179
                    *dst = *(dst - offset - 1);
178
186
    return 0;
179
187
}
180
188
 
181
 
static void cin_decode_rle(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
 
189
static void cin_decode_rle(const unsigned char *src, int src_size,
 
190
                           unsigned char *dst, int dst_size)
182
191
{
183
192
    int len, code;
184
 
    unsigned char *dst_end = dst + dst_size;
 
193
    unsigned char *dst_end       = dst + dst_size;
185
194
    const unsigned char *src_end = src + src_size;
186
195
 
187
196
    while (src < src_end && dst < dst_end) {
188
197
        code = *src++;
189
198
        if (code & 0x80) {
 
199
            if (src >= src_end)
 
200
                break;
190
201
            len = code - 0x7F;
191
202
            memset(dst, *src++, FFMIN(len, dst_end - dst));
192
203
        } else {
193
204
            len = code + 1;
194
 
            memcpy(dst, src, FFMIN(len, dst_end - dst));
 
205
            memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
195
206
            src += len;
196
207
        }
197
208
        dst += len;
199
210
}
200
211
 
201
212
static int cinvideo_decode_frame(AVCodecContext *avctx,
202
 
                                 void *data, int *data_size,
 
213
                                 void *data, int *got_frame,
203
214
                                 AVPacket *avpkt)
204
215
{
205
 
    const uint8_t *buf = avpkt->data;
206
 
    int buf_size = avpkt->size;
 
216
    const uint8_t *buf   = avpkt->data;
 
217
    int buf_size         = avpkt->size;
207
218
    CinVideoContext *cin = avctx->priv_data;
208
 
    int i, y, palette_type, palette_colors_count, bitmap_frame_type, bitmap_frame_size, res = 0;
 
219
    int i, y, palette_type, palette_colors_count,
 
220
        bitmap_frame_type, bitmap_frame_size, res = 0;
209
221
 
210
 
    palette_type = buf[0];
211
 
    palette_colors_count = AV_RL16(buf+1);
212
 
    bitmap_frame_type = buf[3];
213
 
    buf += 4;
 
222
    palette_type         = buf[0];
 
223
    palette_colors_count = AV_RL16(buf + 1);
 
224
    bitmap_frame_type    = buf[3];
 
225
    buf                 += 4;
214
226
 
215
227
    bitmap_frame_size = buf_size - 4;
216
228
 
221
233
        if (palette_colors_count > 256)
222
234
            return AVERROR_INVALIDDATA;
223
235
        for (i = 0; i < palette_colors_count; ++i) {
224
 
            cin->palette[i] = bytestream_get_le24(&buf);
 
236
            cin->palette[i]    = bytestream_get_le24(&buf);
225
237
            bitmap_frame_size -= 3;
226
238
        }
227
239
    } else {
228
240
        for (i = 0; i < palette_colors_count; ++i) {
229
 
            cin->palette[buf[0]] = AV_RL24(buf+1);
230
 
            buf += 4;
231
 
            bitmap_frame_size -= 4;
 
241
            cin->palette[buf[0]] = AV_RL24(buf + 1);
 
242
            buf                 += 4;
 
243
            bitmap_frame_size   -= 4;
232
244
        }
233
245
    }
234
246
 
235
 
    /* note: the decoding routines below assumes that surface.width = surface.pitch */
 
247
    bitmap_frame_size = FFMIN(cin->bitmap_size, bitmap_frame_size);
 
248
 
 
249
    /* note: the decoding routines below assumes that
 
250
     * surface.width = surface.pitch */
236
251
    switch (bitmap_frame_type) {
237
252
    case 9:
238
253
        cin_decode_rle(buf, bitmap_frame_size,
239
 
          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
 
254
                       cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
240
255
        break;
241
256
    case 34:
242
257
        cin_decode_rle(buf, bitmap_frame_size,
243
 
          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
 
258
                       cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
244
259
        cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
245
 
          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
 
260
                             cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
246
261
        break;
247
262
    case 35:
248
263
        cin_decode_huffman(buf, bitmap_frame_size,
249
 
          cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
 
264
                           cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
250
265
        cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
251
 
          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
 
266
                       cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
252
267
        break;
253
268
    case 36:
254
269
        bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
255
 
          cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
 
270
                                               cin->bitmap_table[CIN_INT_BMP],
 
271
                                               cin->bitmap_size);
256
272
        cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
257
 
          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
 
273
                       cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
258
274
        cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
259
 
          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
 
275
                             cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
260
276
        break;
261
277
    case 37:
262
278
        cin_decode_huffman(buf, bitmap_frame_size,
263
 
          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
 
279
                           cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
264
280
        break;
265
281
    case 38:
266
282
        res = cin_decode_lzss(buf, bitmap_frame_size,
276
292
        if (res < 0)
277
293
            return res;
278
294
        cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
279
 
          cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
 
295
                             cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
280
296
        break;
281
297
    }
282
298
 
283
299
    cin->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
284
 
    if (avctx->reget_buffer(avctx, &cin->frame)) {
285
 
        av_log(cin->avctx, AV_LOG_ERROR, "delphinecinvideo: reget_buffer() failed to allocate a frame\n");
286
 
        return -1;
 
300
    if ((res = avctx->reget_buffer(avctx, &cin->frame)) < 0) {
 
301
        av_log(cin->avctx, AV_LOG_ERROR,
 
302
               "delphinecinvideo: reget_buffer() failed to allocate a frame\n");
 
303
        return res;
287
304
    }
288
305
 
289
306
    memcpy(cin->frame.data[1], cin->palette, sizeof(cin->palette));
290
307
    cin->frame.palette_has_changed = 1;
291
308
    for (y = 0; y < cin->avctx->height; ++y)
292
309
        memcpy(cin->frame.data[0] + (cin->avctx->height - 1 - y) * cin->frame.linesize[0],
293
 
          cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
294
 
          cin->avctx->width);
295
 
 
296
 
    FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_table[CIN_PRE_BMP]);
297
 
 
298
 
    *data_size = sizeof(AVFrame);
 
310
               cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
 
311
               cin->avctx->width);
 
312
 
 
313
    FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
 
314
                      cin->bitmap_table[CIN_PRE_BMP]);
 
315
 
 
316
    *got_frame = 1;
299
317
    *(AVFrame *)data = cin->frame;
300
318
 
301
319
    return buf_size;
319
337
{
320
338
    CinAudioContext *cin = avctx->priv_data;
321
339
 
322
 
    if (avctx->channels != 1) {
323
 
        av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
324
 
        return AVERROR_PATCHWELCOME;
325
 
    }
326
 
 
327
340
    cin->initial_decode_frame = 1;
328
 
    cin->delta = 0;
329
 
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
 
341
    cin->delta                = 0;
 
342
    avctx->sample_fmt         = AV_SAMPLE_FMT_S16;
 
343
    avctx->channels           = 1;
 
344
    avctx->channel_layout     = AV_CH_LAYOUT_MONO;
330
345
 
331
346
    avcodec_get_frame_defaults(&cin->frame);
332
347
    avctx->coded_frame = &cin->frame;
337
352
static int cinaudio_decode_frame(AVCodecContext *avctx, void *data,
338
353
                                 int *got_frame_ptr, AVPacket *avpkt)
339
354
{
340
 
    const uint8_t *buf = avpkt->data;
341
 
    CinAudioContext *cin = avctx->priv_data;
 
355
    const uint8_t *buf     = avpkt->data;
 
356
    CinAudioContext *cin   = avctx->priv_data;
342
357
    const uint8_t *buf_end = buf + avpkt->size;
343
358
    int16_t *samples;
344
359
    int delta, ret;
345
360
 
346
361
    /* get output buffer */
347
362
    cin->frame.nb_samples = avpkt->size - cin->initial_decode_frame;
348
 
    if ((ret = avctx->get_buffer(avctx, &cin->frame)) < 0) {
 
363
    if ((ret = ff_get_buffer(avctx, &cin->frame)) < 0) {
349
364
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
350
365
        return ret;
351
366
    }
354
369
    delta = cin->delta;
355
370
    if (cin->initial_decode_frame) {
356
371
        cin->initial_decode_frame = 0;
357
 
        delta = sign_extend(AV_RL16(buf), 16);
358
 
        buf += 2;
359
 
        *samples++ = delta;
 
372
        delta                     = sign_extend(AV_RL16(buf), 16);
 
373
        buf                      += 2;
 
374
        *samples++                = delta;
360
375
    }
361
376
    while (buf < buf_end) {
362
 
        delta += cinaudio_delta16_table[*buf++];
363
 
        delta = av_clip_int16(delta);
 
377
        delta     += cinaudio_delta16_table[*buf++];
 
378
        delta      = av_clip_int16(delta);
364
379
        *samples++ = delta;
365
380
    }
366
381
    cin->delta = delta;
371
386
    return avpkt->size;
372
387
}
373
388
 
374
 
 
375
389
AVCodec ff_dsicinvideo_decoder = {
376
390
    .name           = "dsicinvideo",
377
391
    .type           = AVMEDIA_TYPE_VIDEO,
378
 
    .id             = CODEC_ID_DSICINVIDEO,
 
392
    .id             = AV_CODEC_ID_DSICINVIDEO,
379
393
    .priv_data_size = sizeof(CinVideoContext),
380
394
    .init           = cinvideo_decode_init,
381
395
    .close          = cinvideo_decode_end,
382
396
    .decode         = cinvideo_decode_frame,
383
397
    .capabilities   = CODEC_CAP_DR1,
384
 
    .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
 
398
    .long_name      = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
385
399
};
386
400
 
387
401
AVCodec ff_dsicinaudio_decoder = {
388
402
    .name           = "dsicinaudio",
389
403
    .type           = AVMEDIA_TYPE_AUDIO,
390
 
    .id             = CODEC_ID_DSICINAUDIO,
 
404
    .id             = AV_CODEC_ID_DSICINAUDIO,
391
405
    .priv_data_size = sizeof(CinAudioContext),
392
406
    .init           = cinaudio_decode_init,
393
407
    .decode         = cinaudio_decode_frame,
394
408
    .capabilities   = CODEC_CAP_DR1,
395
 
    .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN audio"),
 
409
    .long_name      = NULL_IF_CONFIG_SMALL("Delphine Software International CIN audio"),
396
410
};