FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
msvideo1.c
Go to the documentation of this file.
1 /*
2  * Microsoft Video-1 Decoder
3  * Copyright (C) 2003 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  * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
25  * For more information about the MS Video-1 format, visit:
26  * http://www.pcisys.net/~melanson/codecs/
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "libavutil/internal.h"
34 #include "libavutil/intreadwrite.h"
35 #include "avcodec.h"
36 #include "internal.h"
37 
38 #define PALETTE_COUNT 256
39 #define CHECK_STREAM_PTR(n) \
40  if ((stream_ptr + n) > s->size ) { \
41  av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
42  stream_ptr + n, s->size); \
43  return; \
44  }
45 
46 typedef struct Msvideo1Context {
47 
50 
51  const unsigned char *buf;
52  int size;
53 
54  int mode_8bit; /* if it's not 8-bit, it's 16-bit */
55 
56  uint32_t pal[256];
58 
60 {
61  Msvideo1Context *s = avctx->priv_data;
62 
63  s->avctx = avctx;
64 
65  /* figure out the colorspace based on the presence of a palette */
66  if (s->avctx->bits_per_coded_sample == 8) {
67  s->mode_8bit = 1;
68  avctx->pix_fmt = AV_PIX_FMT_PAL8;
69  if (avctx->extradata_size >= AVPALETTE_SIZE)
70  memcpy(s->pal, avctx->extradata, AVPALETTE_SIZE);
71  } else {
72  s->mode_8bit = 0;
73  avctx->pix_fmt = AV_PIX_FMT_RGB555;
74  }
75 
76  s->frame = av_frame_alloc();
77  if (!s->frame)
78  return AVERROR(ENOMEM);
79 
80  return 0;
81 }
82 
84 {
85  int block_ptr, pixel_ptr;
86  int total_blocks;
87  int pixel_x, pixel_y; /* pixel width and height iterators */
88  int block_x, block_y; /* block width and height iterators */
89  int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
90  int block_inc;
91  int row_dec;
92 
93  /* decoding parameters */
94  int stream_ptr;
95  unsigned char byte_a, byte_b;
96  unsigned short flags;
97  int skip_blocks;
98  unsigned char colors[8];
99  unsigned char *pixels = s->frame->data[0];
100  int stride = s->frame->linesize[0];
101 
102  stream_ptr = 0;
103  skip_blocks = 0;
104  blocks_wide = s->avctx->width / 4;
105  blocks_high = s->avctx->height / 4;
106  total_blocks = blocks_wide * blocks_high;
107  block_inc = 4;
108  row_dec = stride + 4;
109 
110  for (block_y = blocks_high; block_y > 0; block_y--) {
111  block_ptr = ((block_y * 4) - 1) * stride;
112  for (block_x = blocks_wide; block_x > 0; block_x--) {
113  /* check if this block should be skipped */
114  if (skip_blocks) {
115  block_ptr += block_inc;
116  skip_blocks--;
117  total_blocks--;
118  continue;
119  }
120 
121  pixel_ptr = block_ptr;
122 
123  /* get the next two bytes in the encoded data stream */
124  CHECK_STREAM_PTR(2);
125  byte_a = s->buf[stream_ptr++];
126  byte_b = s->buf[stream_ptr++];
127 
128  /* check if the decode is finished */
129  if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
130  return;
131  else if ((byte_b & 0xFC) == 0x84) {
132  /* skip code, but don't count the current block */
133  skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
134  } else if (byte_b < 0x80) {
135  /* 2-color encoding */
136  flags = (byte_b << 8) | byte_a;
137 
138  CHECK_STREAM_PTR(2);
139  colors[0] = s->buf[stream_ptr++];
140  colors[1] = s->buf[stream_ptr++];
141 
142  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
143  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
144  pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
145  pixel_ptr -= row_dec;
146  }
147  } else if (byte_b >= 0x90) {
148  /* 8-color encoding */
149  flags = (byte_b << 8) | byte_a;
150 
151  CHECK_STREAM_PTR(8);
152  memcpy(colors, &s->buf[stream_ptr], 8);
153  stream_ptr += 8;
154 
155  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
156  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
157  pixels[pixel_ptr++] =
158  colors[((pixel_y & 0x2) << 1) +
159  (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
160  pixel_ptr -= row_dec;
161  }
162  } else {
163  /* 1-color encoding */
164  colors[0] = byte_a;
165 
166  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
167  for (pixel_x = 0; pixel_x < 4; pixel_x++)
168  pixels[pixel_ptr++] = colors[0];
169  pixel_ptr -= row_dec;
170  }
171  }
172 
173  block_ptr += block_inc;
174  total_blocks--;
175  }
176  }
177 
178  /* make the palette available on the way out */
179  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
180  memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
181 }
182 
184 {
185  int block_ptr, pixel_ptr;
186  int total_blocks;
187  int pixel_x, pixel_y; /* pixel width and height iterators */
188  int block_x, block_y; /* block width and height iterators */
189  int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
190  int block_inc;
191  int row_dec;
192 
193  /* decoding parameters */
194  int stream_ptr;
195  unsigned char byte_a, byte_b;
196  unsigned short flags;
197  int skip_blocks;
198  unsigned short colors[8];
199  unsigned short *pixels = (unsigned short *)s->frame->data[0];
200  int stride = s->frame->linesize[0] / 2;
201 
202  stream_ptr = 0;
203  skip_blocks = 0;
204  blocks_wide = s->avctx->width / 4;
205  blocks_high = s->avctx->height / 4;
206  total_blocks = blocks_wide * blocks_high;
207  block_inc = 4;
208  row_dec = stride + 4;
209 
210  for (block_y = blocks_high; block_y > 0; block_y--) {
211  block_ptr = ((block_y * 4) - 1) * stride;
212  for (block_x = blocks_wide; block_x > 0; block_x--) {
213  /* check if this block should be skipped */
214  if (skip_blocks) {
215  block_ptr += block_inc;
216  skip_blocks--;
217  total_blocks--;
218  continue;
219  }
220 
221  pixel_ptr = block_ptr;
222 
223  /* get the next two bytes in the encoded data stream */
224  CHECK_STREAM_PTR(2);
225  byte_a = s->buf[stream_ptr++];
226  byte_b = s->buf[stream_ptr++];
227 
228  /* check if the decode is finished */
229  if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
230  return;
231  } else if ((byte_b & 0xFC) == 0x84) {
232  /* skip code, but don't count the current block */
233  skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
234  } else if (byte_b < 0x80) {
235  /* 2- or 8-color encoding modes */
236  flags = (byte_b << 8) | byte_a;
237 
238  CHECK_STREAM_PTR(4);
239  colors[0] = AV_RL16(&s->buf[stream_ptr]);
240  stream_ptr += 2;
241  colors[1] = AV_RL16(&s->buf[stream_ptr]);
242  stream_ptr += 2;
243 
244  if (colors[0] & 0x8000) {
245  /* 8-color encoding */
246  CHECK_STREAM_PTR(12);
247  colors[2] = AV_RL16(&s->buf[stream_ptr]);
248  stream_ptr += 2;
249  colors[3] = AV_RL16(&s->buf[stream_ptr]);
250  stream_ptr += 2;
251  colors[4] = AV_RL16(&s->buf[stream_ptr]);
252  stream_ptr += 2;
253  colors[5] = AV_RL16(&s->buf[stream_ptr]);
254  stream_ptr += 2;
255  colors[6] = AV_RL16(&s->buf[stream_ptr]);
256  stream_ptr += 2;
257  colors[7] = AV_RL16(&s->buf[stream_ptr]);
258  stream_ptr += 2;
259 
260  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
261  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
262  pixels[pixel_ptr++] =
263  colors[((pixel_y & 0x2) << 1) +
264  (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
265  pixel_ptr -= row_dec;
266  }
267  } else {
268  /* 2-color encoding */
269  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
270  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
271  pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
272  pixel_ptr -= row_dec;
273  }
274  }
275  } else {
276  /* otherwise, it's a 1-color block */
277  colors[0] = (byte_b << 8) | byte_a;
278 
279  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
280  for (pixel_x = 0; pixel_x < 4; pixel_x++)
281  pixels[pixel_ptr++] = colors[0];
282  pixel_ptr -= row_dec;
283  }
284  }
285 
286  block_ptr += block_inc;
287  total_blocks--;
288  }
289  }
290 }
291 
293  void *data, int *got_frame,
294  AVPacket *avpkt)
295 {
296  const uint8_t *buf = avpkt->data;
297  int buf_size = avpkt->size;
298  Msvideo1Context *s = avctx->priv_data;
299  int ret;
300 
301  s->buf = buf;
302  s->size = buf_size;
303 
304  // Discard frame if its smaller than the minimum frame size
305  if (buf_size < (avctx->width/4) * (avctx->height/4) / 512) {
306  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
307  return AVERROR_INVALIDDATA;
308  }
309 
310  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
311  return ret;
312 
313  if (s->mode_8bit) {
314  int size;
315  const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
316 
317  if (pal && size == AVPALETTE_SIZE) {
318  memcpy(s->pal, pal, AVPALETTE_SIZE);
319  s->frame->palette_has_changed = 1;
320  } else if (pal) {
321  av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
322  }
323  }
324 
325  if (s->mode_8bit)
327  else
329 
330  if ((ret = av_frame_ref(data, s->frame)) < 0)
331  return ret;
332 
333  *got_frame = 1;
334 
335  /* report that the buffer was completely consumed */
336  return buf_size;
337 }
338 
340 {
341  Msvideo1Context *s = avctx->priv_data;
342 
343  av_frame_free(&s->frame);
344 
345  return 0;
346 }
347 
349  .name = "msvideo1",
350  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
351  .type = AVMEDIA_TYPE_VIDEO,
352  .id = AV_CODEC_ID_MSVIDEO1,
353  .priv_data_size = sizeof(Msvideo1Context),
355  .close = msvideo1_decode_end,
357  .capabilities = AV_CODEC_CAP_DR1,
358 };
uint32_t pal[256]
Definition: msvideo1.c:56
static int msvideo1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: msvideo1.c:292
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVCodecContext * avctx
Definition: msvideo1.c:48
int size
Definition: avcodec.h:1680
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
AVCodec.
Definition: avcodec.h:3739
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:1718
static av_cold int msvideo1_decode_end(AVCodecContext *avctx)
Definition: msvideo1.c:339
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
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:395
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1876
uint8_t * data
Definition: avcodec.h:1679
static int flags
Definition: log.c:57
ptrdiff_t size
Definition: opengl_enc.c:101
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3157
#define av_log(a,...)
const unsigned char * buf
Definition: msvideo1.c:51
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: avcodec.h:1411
#define AVERROR(e)
Definition: error.h:43
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
AVCodec ff_msvideo1_decoder
Definition: msvideo1.c:348
static void msvideo1_decode_16bit(Msvideo1Context *s)
Definition: msvideo1.c:183
common internal API header
int width
picture width / height.
Definition: avcodec.h:1948
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
main external API structure.
Definition: avcodec.h:1761
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1877
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:358
static void msvideo1_decode_8bit(Msvideo1Context *s)
Definition: msvideo1.c:83
#define CHECK_STREAM_PTR(n)
Definition: msvideo1.c:39
AVFrame * frame
Definition: msvideo1.c:49
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
static av_cold int msvideo1_decode_init(AVCodecContext *avctx)
Definition: msvideo1.c:59
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
common internal api header.
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:367
void * priv_data
Definition: avcodec.h:1803
int pixels
Definition: avisynth_c.h:429
#define stride
This structure stores compressed data.
Definition: avcodec.h:1656
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:1002
for(j=16;j >0;--j)