FFmpeg
Loading...
Searching...
No Matches
vmdvideo.c
Go to the documentation of this file.
1/*
2 * Sierra VMD video decoder
3 * Copyright (c) 2004 The FFmpeg Project
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * Sierra VMD video decoder
25 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26 * for more information on the Sierra VMD format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 *
29 * The video decoder outputs PAL8 colorspace data. The decoder expects
30 * a 0x330-byte VMD file header to be transmitted via extradata during
31 * codec initialization. Each encoded frame that is sent to this decoder
32 * is expected to be prepended with the appropriate 16-byte frame
33 * information record from the VMD file.
34 */
35
36#include <string.h>
37
38#include "libavutil/common.h"
40#include "libavutil/mem.h"
41
42#include "avcodec.h"
43#include "codec_internal.h"
44#include "decode.h"
45#include "bytestream.h"
46
47#define VMD_HEADER_SIZE 0x330
48#define PALETTE_COUNT 256
49
50typedef struct VmdVideoContext {
51
54
55 const unsigned char *buf;
56 int size;
57
58 unsigned char palette[PALETTE_COUNT * 4];
59 unsigned char *unpack_buffer;
61
64
65#define QUEUE_SIZE 0x1000
66#define QUEUE_MASK 0x0FFF
67
68static int lz_unpack(const unsigned char *src, int src_len,
69 unsigned char *dest, int dest_len)
70{
71 unsigned char *d;
72 unsigned char *d_end;
73 unsigned char queue[QUEUE_SIZE];
74 unsigned int qpos;
75 unsigned int dataleft;
76 unsigned int chainofs;
77 unsigned int chainlen;
78 unsigned int speclen;
79 unsigned char tag;
80 unsigned int i, j;
82
83 bytestream2_init(&gb, src, src_len);
84 d = dest;
85 d_end = d + dest_len;
86 dataleft = bytestream2_get_le32(&gb);
87 memset(queue, 0x20, QUEUE_SIZE);
88 if (bytestream2_get_bytes_left(&gb) < 4)
90 if (bytestream2_peek_le32(&gb) == 0x56781234) {
91 bytestream2_skipu(&gb, 4);
92 qpos = 0x111;
93 speclen = 0xF + 3;
94 } else {
95 qpos = 0xFEE;
96 speclen = 100; /* no speclen */
97 }
98
99 while (dataleft > 0 && bytestream2_get_bytes_left(&gb) > 0) {
100 tag = bytestream2_get_byteu(&gb);
101 if ((tag == 0xFF) && (dataleft > 8)) {
102 if (d_end - d < 8 || bytestream2_get_bytes_left(&gb) < 8)
103 return AVERROR_INVALIDDATA;
104 for (i = 0; i < 8; i++) {
105 queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
106 qpos &= QUEUE_MASK;
107 }
108 dataleft -= 8;
109 } else {
110 for (i = 0; i < 8; i++) {
111 if (dataleft == 0)
112 break;
113 if (tag & 0x01) {
114 if (d_end - d < 1 || bytestream2_get_bytes_left(&gb) < 1)
115 return AVERROR_INVALIDDATA;
116 queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
117 qpos &= QUEUE_MASK;
118 dataleft--;
119 } else {
120 chainofs = bytestream2_get_byte(&gb);
121 chainofs |= ((bytestream2_peek_byte(&gb) & 0xF0) << 4);
122 chainlen = (bytestream2_get_byte(&gb) & 0x0F) + 3;
123 if (chainlen == speclen) {
124 chainlen = bytestream2_get_byte(&gb) + 0xF + 3;
125 }
126 if (d_end - d < chainlen)
127 return AVERROR_INVALIDDATA;
128 for (j = 0; j < chainlen; j++) {
129 *d = queue[chainofs++ & QUEUE_MASK];
130 queue[qpos++] = *d++;
131 qpos &= QUEUE_MASK;
132 }
133 dataleft -= chainlen;
134 }
135 tag >>= 1;
136 }
137 }
138 }
139 return d - dest;
140}
141static int rle_unpack(const unsigned char *src, unsigned char *dest,
142 int src_count, int src_size, int dest_len)
143{
144 unsigned char *pd;
145 int i, l, used = 0;
146 unsigned char *dest_end = dest + dest_len;
148 uint16_t run_val;
149
150 bytestream2_init(&gb, src, src_size);
151 pd = dest;
152 if (src_count & 1) {
153 if (bytestream2_get_bytes_left(&gb) < 1)
154 return 0;
155 *pd++ = bytestream2_get_byteu(&gb);
156 used++;
157 }
158
159 do {
160 if (bytestream2_get_bytes_left(&gb) < 1)
161 break;
162 l = bytestream2_get_byteu(&gb);
163 if (l & 0x80) {
164 l = (l & 0x7F) * 2;
165 if (dest_end - pd < l || bytestream2_get_bytes_left(&gb) < l)
166 return bytestream2_tell(&gb);
167 bytestream2_get_bufferu(&gb, pd, l);
168 pd += l;
169 } else {
170 if (dest_end - pd < 2*l || bytestream2_get_bytes_left(&gb) < 2)
171 return bytestream2_tell(&gb);
172 run_val = bytestream2_get_ne16(&gb);
173 for (i = 0; i < l; i++) {
174 AV_WN16(pd, run_val);
175 pd += 2;
176 }
177 l *= 2;
178 }
179 used += l;
180 } while (used < src_count);
181
182 return bytestream2_tell(&gb);
183}
184
186{
187 int i;
188 unsigned int *palette32;
189 unsigned char r, g, b;
190
192
193 unsigned char meth;
194 unsigned char *dp; /* pointer to current frame */
195 unsigned char *pp; /* pointer to previous frame */
196 unsigned char len;
197 int ofs;
198
199 int frame_x, frame_y, prev_linesize;
200 int frame_width, frame_height;
201
202 frame_x = AV_RL16(&s->buf[6]);
203 frame_y = AV_RL16(&s->buf[8]);
204 frame_width = AV_RL16(&s->buf[10]) - frame_x + 1;
205 frame_height = AV_RL16(&s->buf[12]) - frame_y + 1;
206
207 if ((frame_width == s->avctx->width && frame_height == s->avctx->height) &&
208 (frame_x || frame_y)) {
209
210 s->x_off = frame_x;
211 s->y_off = frame_y;
212 }
213 frame_x -= s->x_off;
214 frame_y -= s->y_off;
215
216 if (frame_x < 0 || frame_width < 0 ||
217 frame_x >= s->avctx->width ||
218 frame_width > s->avctx->width ||
219 frame_x + frame_width > s->avctx->width) {
220 av_log(s->avctx, AV_LOG_ERROR,
221 "Invalid horizontal range %d-%d\n",
222 frame_x, frame_width);
223 return AVERROR_INVALIDDATA;
224 }
225 if (frame_y < 0 || frame_height < 0 ||
226 frame_y >= s->avctx->height ||
227 frame_height > s->avctx->height ||
228 frame_y + frame_height > s->avctx->height) {
229 av_log(s->avctx, AV_LOG_ERROR,
230 "Invalid vertical range %d-%d\n",
231 frame_y, frame_height);
232 return AVERROR_INVALIDDATA;
233 }
234
235 /* if only a certain region will be updated, copy the entire previous
236 * frame before the decode */
237 if (s->prev_frame->data[0] &&
238 (frame_x || frame_y || (frame_width != s->avctx->width) ||
239 (frame_height != s->avctx->height))) {
240
241 memcpy(frame->data[0], s->prev_frame->data[0],
242 s->avctx->height * frame->linesize[0]);
243 }
244
245 /* check if there is a new palette */
246 bytestream2_init(&gb, s->buf + 16, s->size - 16);
247 if (s->buf[15] & 0x02) {
248 bytestream2_skip(&gb, 2);
249 palette32 = (unsigned int *)s->palette;
251 for (i = 0; i < PALETTE_COUNT; i++) {
252 r = bytestream2_get_byteu(&gb) * 4;
253 g = bytestream2_get_byteu(&gb) * 4;
254 b = bytestream2_get_byteu(&gb) * 4;
255 palette32[i] = 0xFFU << 24 | (r << 16) | (g << 8) | (b);
256 palette32[i] |= palette32[i] >> 6 & 0x30303;
257 }
258 } else {
259 av_log(s->avctx, AV_LOG_ERROR, "Incomplete palette\n");
260 return AVERROR_INVALIDDATA;
261 }
262 }
263
264 if (!s->size)
265 return 0;
266
267 /* originally UnpackFrame in VAG's code */
268 if (bytestream2_get_bytes_left(&gb) < 1)
269 return AVERROR_INVALIDDATA;
270 meth = bytestream2_get_byteu(&gb);
271 if (meth & 0x80) {
272 int size;
273 if (!s->unpack_buffer_size) {
274 av_log(s->avctx, AV_LOG_ERROR,
275 "Trying to unpack LZ-compressed frame with no LZ buffer\n");
276 return AVERROR_INVALIDDATA;
277 }
279 s->unpack_buffer, s->unpack_buffer_size);
280 if (size < 0)
281 return size;
282 meth &= 0x7F;
283 bytestream2_init(&gb, s->unpack_buffer, size);
284 }
285
286 dp = &frame->data[0][frame_y * frame->linesize[0] + frame_x];
287 if (s->prev_frame->data[0]) {
288 prev_linesize = s->prev_frame->linesize[0];
289 pp = s->prev_frame->data[0] + frame_y * prev_linesize + frame_x;
290 } else {
291 pp = NULL;
292 prev_linesize = 0;
293 }
294 switch (meth) {
295 case 1:
296 for (i = 0; i < frame_height; i++) {
297 ofs = 0;
298 do {
299 len = bytestream2_get_byte(&gb);
300 if (len & 0x80) {
301 len = (len & 0x7F) + 1;
302 if (ofs + len > frame_width ||
304 return AVERROR_INVALIDDATA;
305 bytestream2_get_bufferu(&gb, &dp[ofs], len);
306 ofs += len;
307 } else {
308 /* interframe pixel copy */
309 if (ofs + len + 1 > frame_width || !pp)
310 return AVERROR_INVALIDDATA;
311 memcpy(&dp[ofs], &pp[ofs], len + 1);
312 ofs += len + 1;
313 }
314 } while (ofs < frame_width);
315 if (ofs > frame_width) {
316 av_log(s->avctx, AV_LOG_ERROR,
317 "offset > width (%d > %d)\n",
318 ofs, frame_width);
319 return AVERROR_INVALIDDATA;
320 }
321 dp += frame->linesize[0];
322 pp = FF_PTR_ADD(pp, prev_linesize);
323 }
324 break;
325
326 case 2:
327 for (i = 0; i < frame_height; i++) {
328 bytestream2_get_buffer(&gb, dp, frame_width);
329 dp += frame->linesize[0];
330 }
331 break;
332
333 case 3:
334 for (i = 0; i < frame_height; i++) {
335 ofs = 0;
336 do {
337 len = bytestream2_get_byte(&gb);
338 if (len & 0x80) {
339 len = (len & 0x7F) + 1;
340 if (bytestream2_peek_byte(&gb) == 0xFF) {
341 int slen = len;
342 bytestream2_get_byte(&gb);
343 len = rle_unpack(gb.buffer, &dp[ofs],
345 frame_width - ofs);
346 ofs += slen;
347 bytestream2_skip(&gb, len);
348 } else {
349 if (ofs + len > frame_width ||
351 return AVERROR_INVALIDDATA;
352 bytestream2_get_buffer(&gb, &dp[ofs], len);
353 ofs += len;
354 }
355 } else {
356 /* interframe pixel copy */
357 if (ofs + len + 1 > frame_width || !pp)
358 return AVERROR_INVALIDDATA;
359 memcpy(&dp[ofs], &pp[ofs], len + 1);
360 ofs += len + 1;
361 }
362 } while (ofs < frame_width);
363 if (ofs > frame_width) {
364 av_log(s->avctx, AV_LOG_ERROR,
365 "offset > width (%d > %d)\n",
366 ofs, frame_width);
367 return AVERROR_INVALIDDATA;
368 }
369 dp += frame->linesize[0];
370 pp = FF_PTR_ADD(pp, prev_linesize);
371 }
372 break;
373 }
374 return 0;
375}
376
378{
379 VmdVideoContext *s = avctx->priv_data;
380
381 av_frame_free(&s->prev_frame);
382 av_freep(&s->unpack_buffer);
383 s->unpack_buffer_size = 0;
384
385 return 0;
386}
387
389{
390 VmdVideoContext *s = avctx->priv_data;
391 int i;
392 unsigned int *palette32;
393 int palette_index = 0;
394 unsigned char r, g, b;
395 unsigned char *vmd_header;
396 unsigned char *raw_palette;
397
398 s->avctx = avctx;
399 avctx->pix_fmt = AV_PIX_FMT_PAL8;
400
401 /* make sure the VMD header made it */
402 if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
403 av_log(s->avctx, AV_LOG_ERROR, "expected extradata size of %d\n",
405 return AVERROR_INVALIDDATA;
406 }
407 vmd_header = (unsigned char *)avctx->extradata;
408
409 s->unpack_buffer_size = AV_RL32(&vmd_header[800]);
410 if (s->unpack_buffer_size) {
411 s->unpack_buffer = av_malloc(s->unpack_buffer_size);
412 if (!s->unpack_buffer)
413 return AVERROR(ENOMEM);
414 }
415
416 /* load up the initial palette */
417 raw_palette = &vmd_header[28];
418 palette32 = (unsigned int *)s->palette;
419 for (i = 0; i < PALETTE_COUNT; i++) {
420 r = raw_palette[palette_index++] * 4;
421 g = raw_palette[palette_index++] * 4;
422 b = raw_palette[palette_index++] * 4;
423 palette32[i] = 0xFFU << 24 | (r << 16) | (g << 8) | (b);
424 palette32[i] |= palette32[i] >> 6 & 0x30303;
425 }
426
427 s->prev_frame = av_frame_alloc();
428 if (!s->prev_frame)
429 return AVERROR(ENOMEM);
430
431 return 0;
432}
433
435 int *got_frame, AVPacket *avpkt)
436{
437 const uint8_t *buf = avpkt->data;
438 int buf_size = avpkt->size;
439 VmdVideoContext *s = avctx->priv_data;
440 int ret;
441
442 s->buf = buf;
443 s->size = buf_size;
444
445 if (buf_size < 16)
446 return AVERROR_INVALIDDATA;
447
448 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
449 return ret;
450
451 if ((ret = vmd_decode(s, frame)) < 0)
452 return ret;
453
454 /* make the palette available on the way out */
455 memcpy(frame->data[1], s->palette, PALETTE_COUNT * 4);
456
457 /* shuffle frames */
458 if ((ret = av_frame_replace(s->prev_frame, frame)) < 0)
459 return ret;
460
461 *got_frame = 1;
462
463 /* report that the buffer was completely consumed */
464 return buf_size;
465}
466
468 .p.name = "vmdvideo",
469 CODEC_LONG_NAME("Sierra VMD video"),
470 .p.type = AVMEDIA_TYPE_VIDEO,
471 .p.id = AV_CODEC_ID_VMDVIDEO,
472 .priv_data_size = sizeof(VmdVideoContext),
476 .p.capabilities = AV_CODEC_CAP_DR1,
477 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
478};
const FFCodec ff_vmdvideo_decoder
Definition vmdvideo.c:467
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:267
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define bytestream2_get_ne16
Definition bytestream.h:119
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
@ AV_CODEC_ID_VMDVIDEO
Definition codec_id.h:102
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition frame.c:376
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define PALETTE_COUNT
Definition idcinvideo.c:57
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RL32(p)
#define AV_RL16(p)
#define AV_WN16(p, v)
#define av_cold
Definition attributes.h:117
#define FF_PTR_ADD(ptr, off)
Definition internal.h:74
Memory handling functions.
uint32_t tag
Definition movenc.c:2073
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
const uint8_t * buffer
Definition bytestream.h:34
AVFrame * prev_frame
Definition vmdvideo.c:53
int unpack_buffer_size
Definition vmdvideo.c:60
AVCodecContext * avctx
Definition vmdvideo.c:52
unsigned char * unpack_buffer
Definition vmdvideo.c:59
unsigned char palette[PALETTE_COUNT *4]
Definition vmdvideo.c:58
const unsigned char * buf
Definition vmdvideo.c:55
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
int size
const char * g
Definition vf_curves.c:128
static int lz_unpack(const unsigned char *src, int src_len, unsigned char *dest, int dest_len)
Definition vmdvideo.c:68
#define QUEUE_SIZE
Definition vmdvideo.c:65
static av_cold int vmdvideo_decode_end(AVCodecContext *avctx)
Definition vmdvideo.c:377
static int rle_unpack(const unsigned char *src, unsigned char *dest, int src_count, int src_size, int dest_len)
Definition vmdvideo.c:141
#define QUEUE_MASK
Definition vmdvideo.c:66
#define VMD_HEADER_SIZE
Definition vmdvideo.c:47
static int vmdvideo_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition vmdvideo.c:434
#define PALETTE_COUNT
Definition vmdvideo.c:48
static int vmd_decode(VmdVideoContext *s, AVFrame *frame)
Definition vmdvideo.c:185
static av_cold int vmdvideo_decode_init(AVCodecContext *avctx)
Definition vmdvideo.c:388
int len