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;
146
while (dest < dest_end && src < src_end) {
164
bytestream2_init(&ctx, src, src_len);
165
while (dest < dest_end && bytestream2_get_bytes_left(&ctx)) {
166
opcode = bytestream2_get_byte(&ctx);
149
168
if (opcode < 0xe0) {
151
170
if ((opcode & 0x80) == 0) {
152
171
size = opcode & 3;
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) {
176
size = bytestream2_peek_byte(&ctx) >> 6;
159
back = (bytestream_get_be16(&src) & 0x3fff) + 1;
178
back = (bytestream2_get_be16(&ctx) & 0x3fff) + 1;
160
179
size2 = (opcode & 0x3f) + 4;
162
181
size = opcode & 3;
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;
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)
172
memcpy(dest, src, size); dest += size; src += size;
191
bytestream2_get_buffer(&ctx, dest, size);
173
193
av_memcpy_backptr(dest, back, size2);
176
196
int finish = opcode >= 0xfc;
177
197
size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
179
if (dest_end - dest < size || src_end - src < size)
199
if (dest_end - dest < size || bytestream2_get_bytes_left(&ctx) < size)
181
memcpy(dest, src, size); dest += size; src += size;
201
bytestream2_get_buffer(&ctx, dest, size);
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)
195
216
int width = s->avctx->width;
196
217
unsigned char *palette_plane;
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;
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,
219
241
int pixel_count, int motion_x,
230
252
x + motion_x < 0 || x + motion_x >= s->avctx->width)
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;
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);
385
408
/* output a run of pixels from imagedata_buffer */
386
409
if (imagedata_size < size)
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;
396
419
motion_y = sign_extend(vector & 0xF, 4);
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);
496
519
void *data, int *got_frame,
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;
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;
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));
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));
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)];
535
560
*tmpptr++ = (r << 16) | (g << 8) | b;
576
bytestream2_skip(&ctx, size);
555
buf_size = buf_end - buf;
580
buf_size = bytestream2_get_bytes_left(&ctx);
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;
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");
566
s->current_frame.reference = 3;
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;
571
memcpy(s->current_frame.data[1],
595
memcpy(frame->data[1],
572
596
s->palettes + s->cur_palette * AVPALETTE_COUNT, AVPALETTE_SIZE);
575
599
s->size = buf_size;
577
if (xan_wc3_decode_frame(s) < 0)
601
if (xan_wc3_decode_frame(s, frame) < 0)
578
602
return AVERROR_INVALIDDATA;
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)
585
*(AVFrame*)data = s->current_frame;
588
FFSWAP(AVFrame, s->current_frame, s->last_frame);
590
610
/* always report that the buffer was completely consumed */
594
static av_cold int xan_decode_end(AVCodecContext *avctx)
596
XanContext *s = avctx->priv_data;
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);
604
av_freep(&s->buffer1);
605
av_freep(&s->buffer2);
606
av_freep(&s->palettes);
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),