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 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "libavutil/intreadwrite.h"
35 #include "avcodec.h"
36 
37 #define PALETTE_COUNT 256
38 #define CHECK_STREAM_PTR(n) \
39  if ((stream_ptr + n) > s->size ) { \
40  av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
41  stream_ptr + n, s->size); \
42  return; \
43  }
44 
45 typedef struct Msvideo1Context {
46 
49 
50  const unsigned char *buf;
51  int size;
52 
53  int mode_8bit; /* if it's not 8-bit, it's 16-bit */
54 
55  uint32_t pal[256];
57 
59 {
60  Msvideo1Context *s = avctx->priv_data;
61 
62  s->avctx = avctx;
63 
64  /* figure out the colorspace based on the presence of a palette */
65  if (s->avctx->bits_per_coded_sample == 8) {
66  s->mode_8bit = 1;
67  avctx->pix_fmt = PIX_FMT_PAL8;
68  } else {
69  s->mode_8bit = 0;
70  avctx->pix_fmt = PIX_FMT_RGB555;
71  }
72 
73  s->frame.data[0] = NULL;
74 
75  return 0;
76 }
77 
79 {
80  int block_ptr, pixel_ptr;
81  int total_blocks;
82  int pixel_x, pixel_y; /* pixel width and height iterators */
83  int block_x, block_y; /* block width and height iterators */
84  int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
85  int block_inc;
86  int row_dec;
87 
88  /* decoding parameters */
89  int stream_ptr;
90  unsigned char byte_a, byte_b;
91  unsigned short flags;
92  int skip_blocks;
93  unsigned char colors[8];
94  unsigned char *pixels = s->frame.data[0];
95  int stride = s->frame.linesize[0];
96 
97  stream_ptr = 0;
98  skip_blocks = 0;
99  blocks_wide = s->avctx->width / 4;
100  blocks_high = s->avctx->height / 4;
101  total_blocks = blocks_wide * blocks_high;
102  block_inc = 4;
103  row_dec = stride + 4;
104 
105  for (block_y = blocks_high; block_y > 0; block_y--) {
106  block_ptr = ((block_y * 4) - 1) * stride;
107  for (block_x = blocks_wide; block_x > 0; block_x--) {
108  /* check if this block should be skipped */
109  if (skip_blocks) {
110  block_ptr += block_inc;
111  skip_blocks--;
112  total_blocks--;
113  continue;
114  }
115 
116  pixel_ptr = block_ptr;
117 
118  /* get the next two bytes in the encoded data stream */
119  CHECK_STREAM_PTR(2);
120  byte_a = s->buf[stream_ptr++];
121  byte_b = s->buf[stream_ptr++];
122 
123  /* check if the decode is finished */
124  if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
125  return;
126  else if ((byte_b & 0xFC) == 0x84) {
127  /* skip code, but don't count the current block */
128  skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
129  } else if (byte_b < 0x80) {
130  /* 2-color encoding */
131  flags = (byte_b << 8) | byte_a;
132 
133  CHECK_STREAM_PTR(2);
134  colors[0] = s->buf[stream_ptr++];
135  colors[1] = s->buf[stream_ptr++];
136 
137  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
138  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
139  pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
140  pixel_ptr -= row_dec;
141  }
142  } else if (byte_b >= 0x90) {
143  /* 8-color encoding */
144  flags = (byte_b << 8) | byte_a;
145 
146  CHECK_STREAM_PTR(8);
147  memcpy(colors, &s->buf[stream_ptr], 8);
148  stream_ptr += 8;
149 
150  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
151  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
152  pixels[pixel_ptr++] =
153  colors[((pixel_y & 0x2) << 1) +
154  (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
155  pixel_ptr -= row_dec;
156  }
157  } else {
158  /* 1-color encoding */
159  colors[0] = byte_a;
160 
161  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
162  for (pixel_x = 0; pixel_x < 4; pixel_x++)
163  pixels[pixel_ptr++] = colors[0];
164  pixel_ptr -= row_dec;
165  }
166  }
167 
168  block_ptr += block_inc;
169  total_blocks--;
170  }
171  }
172 
173  /* make the palette available on the way out */
174  if (s->avctx->pix_fmt == PIX_FMT_PAL8)
175  memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
176 }
177 
179 {
180  int block_ptr, pixel_ptr;
181  int total_blocks;
182  int pixel_x, pixel_y; /* pixel width and height iterators */
183  int block_x, block_y; /* block width and height iterators */
184  int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
185  int block_inc;
186  int row_dec;
187 
188  /* decoding parameters */
189  int stream_ptr;
190  unsigned char byte_a, byte_b;
191  unsigned short flags;
192  int skip_blocks;
193  unsigned short colors[8];
194  unsigned short *pixels = (unsigned short *)s->frame.data[0];
195  int stride = s->frame.linesize[0] / 2;
196 
197  stream_ptr = 0;
198  skip_blocks = 0;
199  blocks_wide = s->avctx->width / 4;
200  blocks_high = s->avctx->height / 4;
201  total_blocks = blocks_wide * blocks_high;
202  block_inc = 4;
203  row_dec = stride + 4;
204 
205  for (block_y = blocks_high; block_y > 0; block_y--) {
206  block_ptr = ((block_y * 4) - 1) * stride;
207  for (block_x = blocks_wide; block_x > 0; block_x--) {
208  /* check if this block should be skipped */
209  if (skip_blocks) {
210  block_ptr += block_inc;
211  skip_blocks--;
212  total_blocks--;
213  continue;
214  }
215 
216  pixel_ptr = block_ptr;
217 
218  /* get the next two bytes in the encoded data stream */
219  CHECK_STREAM_PTR(2);
220  byte_a = s->buf[stream_ptr++];
221  byte_b = s->buf[stream_ptr++];
222 
223  /* check if the decode is finished */
224  if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
225  return;
226  } else if ((byte_b & 0xFC) == 0x84) {
227  /* skip code, but don't count the current block */
228  skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
229  } else if (byte_b < 0x80) {
230  /* 2- or 8-color encoding modes */
231  flags = (byte_b << 8) | byte_a;
232 
233  CHECK_STREAM_PTR(4);
234  colors[0] = AV_RL16(&s->buf[stream_ptr]);
235  stream_ptr += 2;
236  colors[1] = AV_RL16(&s->buf[stream_ptr]);
237  stream_ptr += 2;
238 
239  if (colors[0] & 0x8000) {
240  /* 8-color encoding */
241  CHECK_STREAM_PTR(12);
242  colors[2] = AV_RL16(&s->buf[stream_ptr]);
243  stream_ptr += 2;
244  colors[3] = AV_RL16(&s->buf[stream_ptr]);
245  stream_ptr += 2;
246  colors[4] = AV_RL16(&s->buf[stream_ptr]);
247  stream_ptr += 2;
248  colors[5] = AV_RL16(&s->buf[stream_ptr]);
249  stream_ptr += 2;
250  colors[6] = AV_RL16(&s->buf[stream_ptr]);
251  stream_ptr += 2;
252  colors[7] = AV_RL16(&s->buf[stream_ptr]);
253  stream_ptr += 2;
254 
255  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
256  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
257  pixels[pixel_ptr++] =
258  colors[((pixel_y & 0x2) << 1) +
259  (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
260  pixel_ptr -= row_dec;
261  }
262  } else {
263  /* 2-color encoding */
264  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
265  for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
266  pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
267  pixel_ptr -= row_dec;
268  }
269  }
270  } else {
271  /* otherwise, it's a 1-color block */
272  colors[0] = (byte_b << 8) | byte_a;
273 
274  for (pixel_y = 0; pixel_y < 4; pixel_y++) {
275  for (pixel_x = 0; pixel_x < 4; pixel_x++)
276  pixels[pixel_ptr++] = colors[0];
277  pixel_ptr -= row_dec;
278  }
279  }
280 
281  block_ptr += block_inc;
282  total_blocks--;
283  }
284  }
285 }
286 
288  void *data, int *data_size,
289  AVPacket *avpkt)
290 {
291  const uint8_t *buf = avpkt->data;
292  int buf_size = avpkt->size;
293  Msvideo1Context *s = avctx->priv_data;
294 
295  s->buf = buf;
296  s->size = buf_size;
297 
298  s->frame.reference = 1;
300  if (avctx->reget_buffer(avctx, &s->frame)) {
301  av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
302  return -1;
303  }
304 
305  if (s->mode_8bit) {
306  const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
307 
308  if (pal) {
309  memcpy(s->pal, pal, AVPALETTE_SIZE);
310  s->frame.palette_has_changed = 1;
311  }
312  }
313 
314  if (s->mode_8bit)
316  else
318 
319  *data_size = sizeof(AVFrame);
320  *(AVFrame*)data = s->frame;
321 
322  /* report that the buffer was completely consumed */
323  return buf_size;
324 }
325 
327 {
328  Msvideo1Context *s = avctx->priv_data;
329 
330  if (s->frame.data[0])
331  avctx->release_buffer(avctx, &s->frame);
332 
333  return 0;
334 }
335 
337  .name = "msvideo1",
338  .type = AVMEDIA_TYPE_VIDEO,
339  .id = CODEC_ID_MSVIDEO1,
340  .priv_data_size = sizeof(Msvideo1Context),
344  .capabilities = CODEC_CAP_DR1,
345  .long_name= NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
346 };