Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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  } else {
70  s->mode_8bit = 0;
71  avctx->pix_fmt = AV_PIX_FMT_RGB555;
72  }
73 
74  s->frame = av_frame_alloc();
75  if (!s->frame)
76  return AVERROR(ENOMEM);
77 
78  return 0;
79 }
80 
82 {
83  int block_ptr, pixel_ptr;
84  int total_blocks;
85  int pixel_x, pixel_y; /* pixel width and height iterators */
86  int block_x, block_y; /* block width and height iterators */
87  int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
88  int block_inc;
89  int row_dec;
90 
91  /* decoding parameters */
92  int stream_ptr;
93  unsigned char byte_a, byte_b;
94  unsigned short flags;
95  int skip_blocks;
96  unsigned char colors[8];
97  unsigned char *pixels = s->frame->data[0];
98  int stride = s->frame->linesize[0];
99 
100  stream_ptr = 0;
101  skip_blocks = 0;
102  blocks_wide = s->avctx->width / 4;
103  blocks_high = s->avctx->height / 4;
104  total_blocks = blocks_wide * blocks_high;
105  block_inc = 4;
106  row_dec = stride + 4;
107 
108  for (block_y = blocks_high; block_y > 0; block_y--) {
109  block_ptr = ((block_y * 4) - 1) * stride;
110  for (block_x = blocks_wide; block_x > 0; block_x--) {
111  /* check if this block should be skipped */
112  if (skip_blocks) {
113  block_ptr += block_inc;
114  skip_blocks--;
115  total_blocks--;
116  continue;
117  }
118 
119  pixel_ptr = block_ptr;
120 
121  /* get the next two bytes in the encoded data stream */
122  CHECK_STREAM_PTR(2);
123  byte_a = s->buf[stream_ptr++];
124  byte_b = s->buf[stream_ptr++];
125 
126  /* check if the decode is finished */
127  if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
128  return;
129  else if ((byte_b & 0xFC) == 0x84) {
130  /* skip code, but don't count the current block */
131  skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
132  } else if (byte_b < 0x80) {
133  /* 2-color encoding */
134  flags = (byte_b << 8) | byte_a;
135 
136  CHECK_STREAM_PTR(2);
137  colors[0] = s->buf[stream_ptr++];
138  colors[1] = s->buf[stream_ptr++];
139 
140  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
141  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
142  pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
143  pixel_ptr -= row_dec;
144  }
145  } else if (byte_b >= 0x90) {
146  /* 8-color encoding */
147  flags = (byte_b << 8) | byte_a;
148 
149  CHECK_STREAM_PTR(8);
150  memcpy(colors, &s->buf[stream_ptr], 8);
151  stream_ptr += 8;
152 
153  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
154  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
155  pixels[pixel_ptr++] =
156  colors[((pixel_y & 0x2) << 1) +
157  (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
158  pixel_ptr -= row_dec;
159  }
160  } else {
161  /* 1-color encoding */
162  colors[0] = byte_a;
163 
164  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
165  for (pixel_x = 0; pixel_x < 4; pixel_x++)
166  pixels[pixel_ptr++] = colors[0];
167  pixel_ptr -= row_dec;
168  }
169  }
170 
171  block_ptr += block_inc;
172  total_blocks--;
173  }
174  }
175 
176  /* make the palette available on the way out */
177  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
178  memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
179 }
180 
182 {
183  int block_ptr, pixel_ptr;
184  int total_blocks;
185  int pixel_x, pixel_y; /* pixel width and height iterators */
186  int block_x, block_y; /* block width and height iterators */
187  int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
188  int block_inc;
189  int row_dec;
190 
191  /* decoding parameters */
192  int stream_ptr;
193  unsigned char byte_a, byte_b;
194  unsigned short flags;
195  int skip_blocks;
196  unsigned short colors[8];
197  unsigned short *pixels = (unsigned short *)s->frame->data[0];
198  int stride = s->frame->linesize[0] / 2;
199 
200  stream_ptr = 0;
201  skip_blocks = 0;
202  blocks_wide = s->avctx->width / 4;
203  blocks_high = s->avctx->height / 4;
204  total_blocks = blocks_wide * blocks_high;
205  block_inc = 4;
206  row_dec = stride + 4;
207 
208  for (block_y = blocks_high; block_y > 0; block_y--) {
209  block_ptr = ((block_y * 4) - 1) * stride;
210  for (block_x = blocks_wide; block_x > 0; block_x--) {
211  /* check if this block should be skipped */
212  if (skip_blocks) {
213  block_ptr += block_inc;
214  skip_blocks--;
215  total_blocks--;
216  continue;
217  }
218 
219  pixel_ptr = block_ptr;
220 
221  /* get the next two bytes in the encoded data stream */
222  CHECK_STREAM_PTR(2);
223  byte_a = s->buf[stream_ptr++];
224  byte_b = s->buf[stream_ptr++];
225 
226  /* check if the decode is finished */
227  if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
228  return;
229  } else if ((byte_b & 0xFC) == 0x84) {
230  /* skip code, but don't count the current block */
231  skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
232  } else if (byte_b < 0x80) {
233  /* 2- or 8-color encoding modes */
234  flags = (byte_b << 8) | byte_a;
235 
236  CHECK_STREAM_PTR(4);
237  colors[0] = AV_RL16(&s->buf[stream_ptr]);
238  stream_ptr += 2;
239  colors[1] = AV_RL16(&s->buf[stream_ptr]);
240  stream_ptr += 2;
241 
242  if (colors[0] & 0x8000) {
243  /* 8-color encoding */
244  CHECK_STREAM_PTR(12);
245  colors[2] = AV_RL16(&s->buf[stream_ptr]);
246  stream_ptr += 2;
247  colors[3] = AV_RL16(&s->buf[stream_ptr]);
248  stream_ptr += 2;
249  colors[4] = AV_RL16(&s->buf[stream_ptr]);
250  stream_ptr += 2;
251  colors[5] = AV_RL16(&s->buf[stream_ptr]);
252  stream_ptr += 2;
253  colors[6] = AV_RL16(&s->buf[stream_ptr]);
254  stream_ptr += 2;
255  colors[7] = AV_RL16(&s->buf[stream_ptr]);
256  stream_ptr += 2;
257 
258  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
259  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
260  pixels[pixel_ptr++] =
261  colors[((pixel_y & 0x2) << 1) +
262  (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
263  pixel_ptr -= row_dec;
264  }
265  } else {
266  /* 2-color encoding */
267  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
268  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
269  pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
270  pixel_ptr -= row_dec;
271  }
272  }
273  } else {
274  /* otherwise, it's a 1-color block */
275  colors[0] = (byte_b << 8) | byte_a;
276 
277  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
278  for (pixel_x = 0; pixel_x < 4; pixel_x++)
279  pixels[pixel_ptr++] = colors[0];
280  pixel_ptr -= row_dec;
281  }
282  }
283 
284  block_ptr += block_inc;
285  total_blocks--;
286  }
287  }
288 }
289 
291  void *data, int *got_frame,
292  AVPacket *avpkt)
293 {
294  const uint8_t *buf = avpkt->data;
295  int buf_size = avpkt->size;
296  Msvideo1Context *s = avctx->priv_data;
297  int ret;
298 
299  s->buf = buf;
300  s->size = buf_size;
301 
302  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
303  av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
304  return ret;
305  }
306 
307  if (s->mode_8bit) {
309 
310  if (pal) {
311  memcpy(s->pal, pal, AVPALETTE_SIZE);
312  s->frame->palette_has_changed = 1;
313  }
314  }
315 
316  if (s->mode_8bit)
318  else
320 
321  if ((ret = av_frame_ref(data, s->frame)) < 0)
322  return ret;
323 
324  *got_frame = 1;
325 
326  /* report that the buffer was completely consumed */
327  return buf_size;
328 }
329 
331 {
332  Msvideo1Context *s = avctx->priv_data;
333 
334  av_frame_free(&s->frame);
335 
336  return 0;
337 }
338 
340  .name = "msvideo1",
341  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
342  .type = AVMEDIA_TYPE_VIDEO,
343  .id = AV_CODEC_ID_MSVIDEO1,
344  .priv_data_size = sizeof(Msvideo1Context),
346  .close = msvideo1_decode_end,
348  .capabilities = AV_CODEC_CAP_DR1,
349 };
#define AVPALETTE_SIZE
Definition: avcodec.h:3402
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:290
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVCodecContext * avctx
Definition: msvideo1.c:48
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
#define AV_RL16
Definition: intreadwrite.h:42
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
static av_cold int msvideo1_decode_end(AVCodecContext *avctx)
Definition: msvideo1.c:330
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:68
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:70
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:199
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2769
const unsigned char * buf
Definition: msvideo1.c:51
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
AVCodec ff_msvideo1_decoder
Definition: msvideo1.c:339
static void msvideo1_decode_16bit(Msvideo1Context *s)
Definition: msvideo1.c:181
common internal API header
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: utils.c:672
int width
picture width / height.
Definition: avcodec.h:1580
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
NULL
Definition: eval.c:55
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:273
static void msvideo1_decode_8bit(Msvideo1Context *s)
Definition: msvideo1.c:81
#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:146
static av_cold int msvideo1_decode_init(AVCodecContext *avctx)
Definition: msvideo1.c:59
common internal api header.
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:251
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:284
This structure stores compressed data.
Definition: avcodec.h:1323
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
for(j=16;j >0;--j)