Libav
vmdvideo.c
Go to the documentation of this file.
1 /*
2  * Sierra VMD video decoder
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
35 #include <string.h>
36 
37 #include "libavutil/common.h"
38 #include "libavutil/intreadwrite.h"
39 
40 #include "avcodec.h"
41 #include "internal.h"
42 #include "bytestream.h"
43 
44 #define VMD_HEADER_SIZE 0x330
45 #define PALETTE_COUNT 256
46 
47 typedef struct VmdVideoContext {
48 
51 
52  const unsigned char *buf;
53  int size;
54 
55  unsigned char palette[PALETTE_COUNT * 4];
56  unsigned char *unpack_buffer;
58 
59  int x_off, y_off;
61 
62 #define QUEUE_SIZE 0x1000
63 #define QUEUE_MASK 0x0FFF
64 
65 static void lz_unpack(const unsigned char *src, int src_len,
66  unsigned char *dest, int dest_len)
67 {
68  unsigned char *d;
69  unsigned char *d_end;
70  unsigned char queue[QUEUE_SIZE];
71  unsigned int qpos;
72  unsigned int dataleft;
73  unsigned int chainofs;
74  unsigned int chainlen;
75  unsigned int speclen;
76  unsigned char tag;
77  unsigned int i, j;
78  GetByteContext gb;
79 
80  bytestream2_init(&gb, src, src_len);
81  d = dest;
82  d_end = d + dest_len;
83  dataleft = bytestream2_get_le32(&gb);
84  memset(queue, 0x20, QUEUE_SIZE);
85  if (bytestream2_get_bytes_left(&gb) < 4)
86  return;
87  if (bytestream2_peek_le32(&gb) == 0x56781234) {
88  bytestream2_get_le32(&gb);
89  qpos = 0x111;
90  speclen = 0xF + 3;
91  } else {
92  qpos = 0xFEE;
93  speclen = 100; /* no speclen */
94  }
95 
96  while (dataleft > 0 && bytestream2_get_bytes_left(&gb) > 0) {
97  tag = bytestream2_get_byteu(&gb);
98  if ((tag == 0xFF) && (dataleft > 8)) {
99  if (d + 8 > d_end || bytestream2_get_bytes_left(&gb) < 8)
100  return;
101  for (i = 0; i < 8; i++) {
102  queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
103  qpos &= QUEUE_MASK;
104  }
105  dataleft -= 8;
106  } else {
107  for (i = 0; i < 8; i++) {
108  if (dataleft == 0)
109  break;
110  if (tag & 0x01) {
111  if (d + 1 > d_end || bytestream2_get_bytes_left(&gb) < 1)
112  return;
113  queue[qpos++] = *d++ = bytestream2_get_byte(&gb);
114  qpos &= QUEUE_MASK;
115  dataleft--;
116  } else {
117  chainofs = bytestream2_get_byte(&gb);
118  chainofs |= ((bytestream2_peek_byte(&gb) & 0xF0) << 4);
119  chainlen = (bytestream2_get_byte(&gb) & 0x0F) + 3;
120  if (chainlen == speclen) {
121  chainlen = bytestream2_get_byte(&gb) + 0xF + 3;
122  }
123  if (d + chainlen > d_end)
124  return;
125  for (j = 0; j < chainlen; j++) {
126  *d = queue[chainofs++ & QUEUE_MASK];
127  queue[qpos++] = *d++;
128  qpos &= QUEUE_MASK;
129  }
130  dataleft -= chainlen;
131  }
132  tag >>= 1;
133  }
134  }
135  }
136 }
137 
138 static int rle_unpack(const unsigned char *src, unsigned char *dest,
139  int src_count, int src_size, int dest_len)
140 {
141  unsigned char *pd;
142  int i, l, used = 0;
143  unsigned char *dest_end = dest + dest_len;
144  GetByteContext gb;
145  uint16_t run_val;
146 
147  bytestream2_init(&gb, src, src_size);
148  pd = dest;
149  if (src_count & 1) {
150  if (bytestream2_get_bytes_left(&gb) < 1)
151  return 0;
152  *pd++ = bytestream2_get_byteu(&gb);
153  used++;
154  }
155 
156  do {
157  if (bytestream2_get_bytes_left(&gb) < 1)
158  break;
159  l = bytestream2_get_byteu(&gb);
160  if (l & 0x80) {
161  l = (l & 0x7F) * 2;
162  if (pd + l > dest_end || bytestream2_get_bytes_left(&gb) < l)
163  return bytestream2_tell(&gb);
164  bytestream2_get_buffer(&gb, pd, l);
165  pd += l;
166  } else {
167  if (pd + l > dest_end || bytestream2_get_bytes_left(&gb) < 2)
168  return bytestream2_tell(&gb);
169  run_val = bytestream2_get_ne16(&gb);
170  for (i = 0; i < l; i++) {
171  AV_WN16(pd, run_val);
172  pd += 2;
173  }
174  l *= 2;
175  }
176  used += l;
177  } while (used < src_count);
178 
179  return bytestream2_tell(&gb);
180 }
181 
182 static int vmd_decode(VmdVideoContext *s, AVFrame *frame)
183 {
184  int i;
185  unsigned int *palette32;
186  unsigned char r, g, b;
187 
188  GetByteContext gb;
189 
190  unsigned char meth;
191  unsigned char *dp; /* pointer to current frame */
192  unsigned char *pp; /* pointer to previous frame */
193  unsigned char len;
194  int ofs;
195 
196  int frame_x, frame_y;
197  int frame_width, frame_height;
198 
199  frame_x = AV_RL16(&s->buf[6]);
200  frame_y = AV_RL16(&s->buf[8]);
201  frame_width = AV_RL16(&s->buf[10]) - frame_x + 1;
202  frame_height = AV_RL16(&s->buf[12]) - frame_y + 1;
203  if (frame_x < 0 || frame_width < 0 ||
204  frame_x >= s->avctx->width ||
205  frame_width > s->avctx->width ||
206  frame_x + frame_width > s->avctx->width) {
208  "Invalid horizontal range %d-%d\n",
209  frame_x, frame_width);
210  return AVERROR_INVALIDDATA;
211  }
212  if (frame_y < 0 || frame_height < 0 ||
213  frame_y >= s->avctx->height ||
214  frame_height > s->avctx->height ||
215  frame_y + frame_height > s->avctx->height) {
217  "Invalid vertical range %d-%d\n",
218  frame_x, frame_width);
219  return AVERROR_INVALIDDATA;
220  }
221 
222  if ((frame_width == s->avctx->width && frame_height == s->avctx->height) &&
223  (frame_x || frame_y)) {
224 
225  s->x_off = frame_x;
226  s->y_off = frame_y;
227  }
228  frame_x -= s->x_off;
229  frame_y -= s->y_off;
230 
231  /* if only a certain region will be updated, copy the entire previous
232  * frame before the decode */
233  if (s->prev_frame->data[0] &&
234  (frame_x || frame_y || (frame_width != s->avctx->width) ||
235  (frame_height != s->avctx->height))) {
236 
237  memcpy(frame->data[0], s->prev_frame->data[0],
238  s->avctx->height * frame->linesize[0]);
239  }
240 
241  /* check if there is a new palette */
242  bytestream2_init(&gb, s->buf + 16, s->size - 16);
243  if (s->buf[15] & 0x02) {
244  bytestream2_skip(&gb, 2);
245  palette32 = (unsigned int *)s->palette;
247  for (i = 0; i < PALETTE_COUNT; i++) {
248  r = bytestream2_get_byteu(&gb) * 4;
249  g = bytestream2_get_byteu(&gb) * 4;
250  b = bytestream2_get_byteu(&gb) * 4;
251  palette32[i] = (r << 16) | (g << 8) | (b);
252  }
253  } else {
254  av_log(s->avctx, AV_LOG_ERROR, "Incomplete palette\n");
255  return AVERROR_INVALIDDATA;
256  }
257  s->size -= PALETTE_COUNT * 3 + 2;
258  }
259 
260  if (!s->size)
261  return 0;
262 
263  /* originally UnpackFrame in VAG's code */
264  if (bytestream2_get_bytes_left(&gb) < 1)
265  return AVERROR_INVALIDDATA;
266  meth = bytestream2_get_byteu(&gb);
267  if (meth & 0x80) {
268  if (!s->unpack_buffer_size) {
270  "Trying to unpack LZ-compressed frame with no LZ buffer\n");
271  return AVERROR_INVALIDDATA;
272  }
275  meth &= 0x7F;
277  }
278 
279  dp = &frame->data[0][frame_y * frame->linesize[0] + frame_x];
280  pp = &s->prev_frame->data[0][frame_y * s->prev_frame->linesize[0] + frame_x];
281  switch (meth) {
282  case 1:
283  for (i = 0; i < frame_height; i++) {
284  ofs = 0;
285  do {
286  len = bytestream2_get_byte(&gb);
287  if (len & 0x80) {
288  len = (len & 0x7F) + 1;
289  if (ofs + len > frame_width ||
291  return AVERROR_INVALIDDATA;
292  bytestream2_get_buffer(&gb, &dp[ofs], len);
293  ofs += len;
294  } else {
295  /* interframe pixel copy */
296  if (ofs + len + 1 > frame_width || !s->prev_frame->data[0])
297  return AVERROR_INVALIDDATA;
298  memcpy(&dp[ofs], &pp[ofs], len + 1);
299  ofs += len + 1;
300  }
301  } while (ofs < frame_width);
302  if (ofs > frame_width) {
304  "VMD video: offset > width (%d > %d)\n",
305  ofs, frame_width);
306  return AVERROR_INVALIDDATA;
307  }
308  dp += frame->linesize[0];
309  pp += s->prev_frame->linesize[0];
310  }
311  break;
312 
313  case 2:
314  for (i = 0; i < frame_height; i++) {
315  bytestream2_get_buffer(&gb, dp, frame_width);
316  dp += frame->linesize[0];
317  pp += s->prev_frame->linesize[0];
318  }
319  break;
320 
321  case 3:
322  for (i = 0; i < frame_height; i++) {
323  ofs = 0;
324  do {
325  len = bytestream2_get_byte(&gb);
326  if (len & 0x80) {
327  len = (len & 0x7F) + 1;
328  if (bytestream2_peek_byte(&gb) == 0xFF) {
329  int slen = len;
330  bytestream2_get_byte(&gb);
331  len = rle_unpack(gb.buffer, &dp[ofs],
332  len, bytestream2_get_bytes_left(&gb),
333  frame_width - ofs);
334  ofs += slen;
335  bytestream2_skip(&gb, len);
336  } else {
337  bytestream2_get_buffer(&gb, &dp[ofs], len);
338  ofs += len;
339  }
340  } else {
341  /* interframe pixel copy */
342  if (ofs + len + 1 > frame_width || !s->prev_frame->data[0])
343  return AVERROR_INVALIDDATA;
344  memcpy(&dp[ofs], &pp[ofs], len + 1);
345  ofs += len + 1;
346  }
347  } while (ofs < frame_width);
348  if (ofs > frame_width) {
350  "VMD video: offset > width (%d > %d)\n",
351  ofs, frame_width);
352  return AVERROR_INVALIDDATA;
353  }
354  dp += frame->linesize[0];
355  pp += s->prev_frame->linesize[0];
356  }
357  break;
358  }
359  return 0;
360 }
361 
363 {
364  VmdVideoContext *s = avctx->priv_data;
365 
368 
369  return 0;
370 }
371 
373 {
374  VmdVideoContext *s = avctx->priv_data;
375  int i;
376  unsigned int *palette32;
377  int palette_index = 0;
378  unsigned char r, g, b;
379  unsigned char *vmd_header;
380  unsigned char *raw_palette;
381 
382  s->avctx = avctx;
383  avctx->pix_fmt = AV_PIX_FMT_PAL8;
384 
385  /* make sure the VMD header made it */
386  if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
387  av_log(s->avctx, AV_LOG_ERROR, "VMD video: expected extradata size of %d\n",
389  return -1;
390  }
391  vmd_header = (unsigned char *)avctx->extradata;
392 
393  s->unpack_buffer_size = AV_RL32(&vmd_header[800]);
394  if (s->unpack_buffer_size) {
396  if (!s->unpack_buffer)
397  return AVERROR(ENOMEM);
398  }
399 
400  /* load up the initial palette */
401  raw_palette = &vmd_header[28];
402  palette32 = (unsigned int *)s->palette;
403  for (i = 0; i < PALETTE_COUNT; i++) {
404  r = raw_palette[palette_index++] * 4;
405  g = raw_palette[palette_index++] * 4;
406  b = raw_palette[palette_index++] * 4;
407  palette32[i] = (r << 16) | (g << 8) | (b);
408  }
409 
410  s->prev_frame = av_frame_alloc();
411  if (!s->prev_frame) {
412  vmdvideo_decode_end(avctx);
413  return AVERROR(ENOMEM);
414  }
415 
416  return 0;
417 }
418 
420  void *data, int *got_frame,
421  AVPacket *avpkt)
422 {
423  const uint8_t *buf = avpkt->data;
424  int buf_size = avpkt->size;
425  VmdVideoContext *s = avctx->priv_data;
426  AVFrame *frame = data;
427  int ret;
428 
429  s->buf = buf;
430  s->size = buf_size;
431 
432  if (buf_size < 16)
433  return AVERROR_INVALIDDATA;
434 
435  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
436  av_log(s->avctx, AV_LOG_ERROR, "VMD Video: get_buffer() failed\n");
437  return ret;
438  }
439 
440  if ((ret = vmd_decode(s, frame)) < 0)
441  return ret;
442 
443  /* make the palette available on the way out */
444  memcpy(frame->data[1], s->palette, PALETTE_COUNT * 4);
445 
446  /* shuffle frames */
448  if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
449  return ret;
450 
451  *got_frame = 1;
452 
453  /* report that the buffer was completely consumed */
454  return buf_size;
455 }
456 
458  .name = "vmdvideo",
459  .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD video"),
460  .type = AVMEDIA_TYPE_VIDEO,
461  .id = AV_CODEC_ID_VMDVIDEO,
462  .priv_data_size = sizeof(VmdVideoContext),
466  .capabilities = CODEC_CAP_DR1,
467 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVFrame * prev_frame
Definition: vmdvideo.c:50
int size
Definition: avcodec.h:968
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1248
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
#define AV_RL16
Definition: intreadwrite.h:42
AVCodec.
Definition: avcodec.h:2790
AVCodecContext * avctx
Definition: vmdvideo.c:49
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define b
Definition: input.c:52
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:188
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1158
AVCodec ff_vmdvideo_decoder
Definition: vmdvideo.c:457
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:967
const uint8_t * buffer
Definition: bytestream.h:33
unsigned char * unpack_buffer
Definition: vmdvideo.c:56
uint32_t tag
Definition: movenc.c:844
static int vmdvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vmdvideo.c:419
unsigned char palette[PALETTE_COUNT *4]
Definition: vmdvideo.c:55
#define r
Definition: input.c:51
static av_cold int vmdvideo_decode_end(AVCodecContext *avctx)
Definition: vmdvideo.c:362
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define bytestream2_get_ne16
Definition: bytestream.h:112
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
g
Definition: yuv2rgb.c:535
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:258
int unpack_buffer_size
Definition: vmdvideo.c:57
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
const unsigned char * buf
Definition: vmdvideo.c:52
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2797
static int vmd_decode(VmdVideoContext *s, AVFrame *frame)
Definition: vmdvideo.c:182
static av_cold int vmdvideo_decode_init(AVCodecContext *avctx)
Definition: vmdvideo.c:372
#define PALETTE_COUNT
Definition: vmdvideo.c:45
int width
picture width / height.
Definition: avcodec.h:1218
#define AV_RL32
Definition: intreadwrite.h:146
#define QUEUE_MASK
Definition: vmdvideo.c:63
if(ac->has_optimized_func)
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:183
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1044
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:603
#define QUEUE_SIZE
Definition: vmdvideo.c:62
int extradata_size
Definition: avcodec.h:1159
static void lz_unpack(const unsigned char *src, int src_len, unsigned char *dest, int dest_len)
Definition: vmdvideo.c:65
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:283
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
common internal api header.
common internal and external API header
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1086
int len
#define AV_WN16(p, v)
Definition: intreadwrite.h:334
static int rle_unpack(const unsigned char *src, unsigned char *dest, int src_count, int src_size, int dest_len)
Definition: vmdvideo.c:138
This structure stores compressed data.
Definition: avcodec.h:944
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:850
for(j=16;j >0;--j)
#define VMD_HEADER_SIZE
Definition: vmdvideo.c:44