~siretart/ubuntu/utopic/libav/libav10

« back to all changes in this revision

Viewing changes to libavcodec/xan.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2014-05-11 12:28:45 UTC
  • mfrom: (1.1.22) (2.1.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140511122845-gxvpts83i958y0i5
Tags: 6:10.1-1
* New upstream release 10:
   - pcm-dvd: Fix 20bit decoding (bug/592)
   - avi: Improve non-interleaved detection (bug/666)
   - arm: hpeldsp: fix put_pixels8_y2_{,no_rnd_}armv6
   - arm: hpeldsp: prevent overreads in armv6 asm (bug/646)
   - avfilter: Add missing emms_c when needed
   - rtmpproto: Check the buffer sizes when copying app/playpath strings
   - swscale: Fix an undefined behaviour
   - vp9: Read the frame size as unsigned
   - dcadec: Use correct channel count in stereo downmix check
   - dcadec: Do not decode the XCh extension when downmixing to stereo
   - matroska: add the Opus mapping
   - matroskadec: read the CodecDelay element
   - rtmpproto: Make sure to pass on the error code if read_connect failed
   - lavr: allocate the resampling buffer with a positive size
   - mp3enc: Properly write bitrate value in XING header (Closes: #736088)
   - golomb: Fix the implementation of get_se_golomb_long
* Drop debian/libav-tools.maintscript. ffserver is no longer found in
  stable, and this seems to cause other problems today (Closes: #742676)

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
typedef struct XanContext {
53
53
 
54
54
    AVCodecContext *avctx;
55
 
    AVFrame last_frame;
56
 
    AVFrame current_frame;
 
55
    AVFrame *last_frame;
57
56
 
58
57
    const unsigned char *buf;
59
58
    int size;
72
71
 
73
72
} XanContext;
74
73
 
 
74
static av_cold int xan_decode_end(AVCodecContext *avctx)
 
75
{
 
76
    XanContext *s = avctx->priv_data;
 
77
 
 
78
    av_frame_free(&s->last_frame);
 
79
 
 
80
    av_freep(&s->buffer1);
 
81
    av_freep(&s->buffer2);
 
82
    av_freep(&s->palettes);
 
83
 
 
84
    return 0;
 
85
}
 
86
 
75
87
static av_cold int xan_decode_init(AVCodecContext *avctx)
76
88
{
77
89
    XanContext *s = avctx->priv_data;
92
104
        return AVERROR(ENOMEM);
93
105
    }
94
106
 
 
107
    s->last_frame = av_frame_alloc();
 
108
    if (!s->last_frame) {
 
109
        xan_decode_end(avctx);
 
110
        return AVERROR(ENOMEM);
 
111
    }
 
112
 
95
113
    return 0;
96
114
}
97
115
 
115
133
    while (val != 0x16) {
116
134
        unsigned idx = val - 0x17 + get_bits1(&gb) * byte;
117
135
        if (idx >= 2 * byte)
118
 
            return -1;
 
136
            return AVERROR_INVALIDDATA;
119
137
        val = src[idx];
120
138
 
121
139
        if (val < 0x16) {
141
159
    int size;
142
160
    unsigned char *dest_org = dest;
143
161
    unsigned char *dest_end = dest + dest_len;
144
 
    const unsigned char *src_end = src + src_len;
 
162
    GetByteContext ctx;
145
163
 
146
 
    while (dest < dest_end && src < src_end) {
147
 
        opcode = *src++;
 
164
    bytestream2_init(&ctx, src, src_len);
 
165
    while (dest < dest_end && bytestream2_get_bytes_left(&ctx)) {
 
166
        opcode = bytestream2_get_byte(&ctx);
148
167
 
149
168
        if (opcode < 0xe0) {
150
169
            int size2, back;
151
170
            if ((opcode & 0x80) == 0) {
152
171
                size = opcode & 3;
153
172
 
154
 
                back  = ((opcode & 0x60) << 3) + *src++ + 1;
 
173
                back  = ((opcode & 0x60) << 3) + bytestream2_get_byte(&ctx) + 1;
155
174
                size2 = ((opcode & 0x1c) >> 2) + 3;
156
175
            } else if ((opcode & 0x40) == 0) {
157
 
                size = *src >> 6;
 
176
                size = bytestream2_peek_byte(&ctx) >> 6;
158
177
 
159
 
                back  = (bytestream_get_be16(&src) & 0x3fff) + 1;
 
178
                back  = (bytestream2_get_be16(&ctx) & 0x3fff) + 1;
160
179
                size2 = (opcode & 0x3f) + 4;
161
180
            } else {
162
181
                size = opcode & 3;
163
182
 
164
 
                back  = ((opcode & 0x10) << 12) + bytestream_get_be16(&src) + 1;
165
 
                size2 = ((opcode & 0x0c) <<  6) + *src++ + 5;
 
183
                back  = ((opcode & 0x10) << 12) + bytestream2_get_be16(&ctx) + 1;
 
184
                size2 = ((opcode & 0x0c) <<  6) + bytestream2_get_byte(&ctx) + 5;
166
185
            }
167
186
 
168
187
            if (dest_end - dest < size + size2 ||
169
188
                dest + size - dest_org < back ||
170
 
                src_end - src < size)
 
189
                bytestream2_get_bytes_left(&ctx) < size)
171
190
                return;
172
 
            memcpy(dest, src, size);  dest += size;  src += size;
 
191
            bytestream2_get_buffer(&ctx, dest, size);
 
192
            dest += size;
173
193
            av_memcpy_backptr(dest, back, size2);
174
194
            dest += size2;
175
195
        } else {
176
196
            int finish = opcode >= 0xfc;
177
197
            size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
178
198
 
179
 
            if (dest_end - dest < size || src_end - src < size)
 
199
            if (dest_end - dest < size || bytestream2_get_bytes_left(&ctx) < size)
180
200
                return;
181
 
            memcpy(dest, src, size);  dest += size;  src += size;
 
201
            bytestream2_get_buffer(&ctx, dest, size);
 
202
            dest += size;
182
203
            if (finish)
183
204
                return;
184
205
        }
185
206
    }
186
207
}
187
208
 
188
 
static inline void xan_wc3_output_pixel_run(XanContext *s,
 
209
static inline void xan_wc3_output_pixel_run(XanContext *s, AVFrame *frame,
189
210
    const unsigned char *pixel_buffer, int x, int y, int pixel_count)
190
211
{
191
212
    int stride;
195
216
    int width = s->avctx->width;
196
217
    unsigned char *palette_plane;
197
218
 
198
 
    palette_plane = s->current_frame.data[0];
199
 
    stride = s->current_frame.linesize[0];
 
219
    palette_plane = frame->data[0];
 
220
    stride = frame->linesize[0];
200
221
    line_inc = stride - width;
201
222
    index = y * stride + x;
202
223
    current_x = x;
215
236
    }
216
237
}
217
238
 
218
 
static inline void xan_wc3_copy_pixel_run(XanContext *s, int x, int y,
 
239
static inline void xan_wc3_copy_pixel_run(XanContext *s, AVFrame *frame,
 
240
                                          int x, int y,
219
241
                                          int pixel_count, int motion_x,
220
242
                                          int motion_y)
221
243
{
230
252
        x + motion_x < 0 || x + motion_x >= s->avctx->width)
231
253
        return;
232
254
 
233
 
    palette_plane = s->current_frame.data[0];
234
 
    prev_palette_plane = s->last_frame.data[0];
 
255
    palette_plane = frame->data[0];
 
256
    prev_palette_plane = s->last_frame->data[0];
235
257
    if (!prev_palette_plane)
236
258
        prev_palette_plane = palette_plane;
237
 
    stride = s->current_frame.linesize[0];
 
259
    stride = frame->linesize[0];
238
260
    line_inc = stride - width;
239
261
    curframe_index = y * stride + x;
240
262
    curframe_x = x;
266
288
    }
267
289
}
268
290
 
269
 
static int xan_wc3_decode_frame(XanContext *s) {
 
291
static int xan_wc3_decode_frame(XanContext *s, AVFrame *frame)
 
292
{
270
293
 
271
294
    int width  = s->avctx->width;
272
295
    int height = s->avctx->height;
380
403
            flag ^= 1;
381
404
            if (flag) {
382
405
                /* run of (size) pixels is unchanged from last frame */
383
 
                xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
 
406
                xan_wc3_copy_pixel_run(s, frame, x, y, size, 0, 0);
384
407
            } else {
385
408
                /* output a run of pixels from imagedata_buffer */
386
409
                if (imagedata_size < size)
387
410
                    break;
388
 
                xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
 
411
                xan_wc3_output_pixel_run(s, frame, imagedata_buffer, x, y, size);
389
412
                imagedata_buffer += size;
390
413
                imagedata_size -= size;
391
414
            }
396
419
            motion_y = sign_extend(vector & 0xF, 4);
397
420
 
398
421
            /* copy a run of pixels from the previous frame */
399
 
            xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
 
422
            xan_wc3_copy_pixel_run(s, frame, x, y, size, motion_x, motion_y);
400
423
 
401
424
            flag = 0;
402
425
        }
496
519
                            void *data, int *got_frame,
497
520
                            AVPacket *avpkt)
498
521
{
 
522
    AVFrame *frame = data;
499
523
    const uint8_t *buf = avpkt->data;
500
524
    int ret, buf_size = avpkt->size;
501
525
    XanContext *s = avctx->priv_data;
502
 
    const uint8_t *buf_end = buf + buf_size;
 
526
    GetByteContext ctx;
503
527
    int tag = 0;
504
528
 
505
 
    while (buf_end - buf > 8 && tag != VGA__TAG) {
 
529
    bytestream2_init(&ctx, buf, buf_size);
 
530
    while (bytestream2_get_bytes_left(&ctx) > 8 && tag != VGA__TAG) {
506
531
        unsigned *tmpptr;
507
532
        uint32_t new_pal;
508
533
        int size;
509
534
        int i;
510
 
        tag  = bytestream_get_le32(&buf);
511
 
        size = bytestream_get_be32(&buf);
512
 
        size = FFMIN(size, buf_end - buf);
 
535
        tag  = bytestream2_get_le32(&ctx);
 
536
        size = bytestream2_get_be32(&ctx);
 
537
        size = FFMIN(size, bytestream2_get_bytes_left(&ctx));
513
538
        switch (tag) {
514
539
        case PALT_TAG:
515
540
            if (size < PALETTE_SIZE)
524
549
            tmpptr += s->palettes_count * AVPALETTE_COUNT;
525
550
            for (i = 0; i < PALETTE_COUNT; i++) {
526
551
#if RUNTIME_GAMMA
527
 
                int r = gamma_corr(*buf++);
528
 
                int g = gamma_corr(*buf++);
529
 
                int b = gamma_corr(*buf++);
 
552
                int r = gamma_corr(bytestream2_get_byteu(&ctx));
 
553
                int g = gamma_corr(bytestream2_get_byteu(&ctx));
 
554
                int b = gamma_corr(bytestream2_get_byteu(&ctx));
530
555
#else
531
 
                int r = gamma_lookup[*buf++];
532
 
                int g = gamma_lookup[*buf++];
533
 
                int b = gamma_lookup[*buf++];
 
556
                int r = gamma_lookup[bytestream2_get_byteu(&ctx)];
 
557
                int g = gamma_lookup[bytestream2_get_byteu(&ctx)];
 
558
                int b = gamma_lookup[bytestream2_get_byteu(&ctx)];
534
559
#endif
535
560
                *tmpptr++ = (r << 16) | (g << 8) | b;
536
561
            }
539
564
        case SHOT_TAG:
540
565
            if (size < 4)
541
566
                return AVERROR_INVALIDDATA;
542
 
            new_pal = bytestream_get_le32(&buf);
 
567
            new_pal = bytestream2_get_le32(&ctx);
543
568
            if (new_pal < s->palettes_count) {
544
569
                s->cur_palette = new_pal;
545
570
            } else
548
573
        case VGA__TAG:
549
574
            break;
550
575
        default:
551
 
            buf += size;
 
576
            bytestream2_skip(&ctx, size);
552
577
            break;
553
578
        }
554
579
    }
555
 
    buf_size = buf_end - buf;
 
580
    buf_size = bytestream2_get_bytes_left(&ctx);
556
581
 
557
582
    if (s->palettes_count <= 0) {
558
583
        av_log(s->avctx, AV_LOG_ERROR, "No palette found\n");
559
584
        return AVERROR_INVALIDDATA;
560
585
    }
561
586
 
562
 
    if ((ret = ff_get_buffer(avctx, &s->current_frame))) {
 
587
    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF))) {
563
588
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
564
589
        return ret;
565
590
    }
566
 
    s->current_frame.reference = 3;
567
591
 
568
592
    if (!s->frame_size)
569
 
        s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
 
593
        s->frame_size = frame->linesize[0] * s->avctx->height;
570
594
 
571
 
    memcpy(s->current_frame.data[1],
 
595
    memcpy(frame->data[1],
572
596
           s->palettes + s->cur_palette * AVPALETTE_COUNT, AVPALETTE_SIZE);
573
597
 
574
 
    s->buf = buf;
 
598
    s->buf = ctx.buffer;
575
599
    s->size = buf_size;
576
600
 
577
 
    if (xan_wc3_decode_frame(s) < 0)
 
601
    if (xan_wc3_decode_frame(s, frame) < 0)
578
602
        return AVERROR_INVALIDDATA;
579
603
 
580
 
    /* release the last frame if it is allocated */
581
 
    if (s->last_frame.data[0])
582
 
        avctx->release_buffer(avctx, &s->last_frame);
 
604
    av_frame_unref(s->last_frame);
 
605
    if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
 
606
        return ret;
583
607
 
584
608
    *got_frame = 1;
585
 
    *(AVFrame*)data = s->current_frame;
586
 
 
587
 
    /* shuffle frames */
588
 
    FFSWAP(AVFrame, s->current_frame, s->last_frame);
589
609
 
590
610
    /* always report that the buffer was completely consumed */
591
611
    return buf_size;
592
612
}
593
613
 
594
 
static av_cold int xan_decode_end(AVCodecContext *avctx)
595
 
{
596
 
    XanContext *s = avctx->priv_data;
597
 
 
598
 
    /* release the frames */
599
 
    if (s->last_frame.data[0])
600
 
        avctx->release_buffer(avctx, &s->last_frame);
601
 
    if (s->current_frame.data[0])
602
 
        avctx->release_buffer(avctx, &s->current_frame);
603
 
 
604
 
    av_freep(&s->buffer1);
605
 
    av_freep(&s->buffer2);
606
 
    av_freep(&s->palettes);
607
 
 
608
 
    return 0;
609
 
}
610
 
 
611
614
AVCodec ff_xan_wc3_decoder = {
612
615
    .name           = "xan_wc3",
 
616
    .long_name      = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
613
617
    .type           = AVMEDIA_TYPE_VIDEO,
614
618
    .id             = AV_CODEC_ID_XAN_WC3,
615
619
    .priv_data_size = sizeof(XanContext),
617
621
    .close          = xan_decode_end,
618
622
    .decode         = xan_decode_frame,
619
623
    .capabilities   = CODEC_CAP_DR1,
620
 
    .long_name      = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
621
624
};