FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mvcdec.c
Go to the documentation of this file.
1 /*
2  * Silicon Graphics Motion Video Compressor 1 & 2 decoder
3  * Copyright (c) 2012 Peter Ross
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  * Silicon Graphics Motion Video Compressor 1 & 2 decoder
25  */
26 
27 #include "libavutil/intreadwrite.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31 
32 typedef struct MvcContext {
34  int vflip;
35 } MvcContext;
36 
38 {
39  MvcContext *s = avctx->priv_data;
40  int width = avctx->width;
41  int height = avctx->height;
42  int ret;
43 
44  if (avctx->codec_id == AV_CODEC_ID_MVC1) {
45  width += 3;
46  height += 3;
47  }
48  width &= ~3;
49  height &= ~3;
50  if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
51  return ret;
52 
54  s->frame = av_frame_alloc();
55  if (!s->frame)
56  return AVERROR(ENOMEM);
57 
58  s->vflip = avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9);
59  return 0;
60 }
61 
62 static int decode_mvc1(AVCodecContext *avctx, GetByteContext *gb, uint8_t *dst_start, int width, int height, int linesize)
63 {
64  uint8_t *dst;
65  uint16_t v[8];
66  int mask, x, y, i;
67 
68  x = y= 0;
69  while (bytestream2_get_bytes_left(gb) >= 6) {
70  mask = bytestream2_get_be16u(gb);
71  v[0] = bytestream2_get_be16u(gb);
72  v[1] = bytestream2_get_be16u(gb);
73  if ((v[0] & 0x8000)) {
74  if (bytestream2_get_bytes_left(gb) < 12) {
75  av_log(avctx, AV_LOG_WARNING, "buffer overflow\n");
76  return AVERROR_INVALIDDATA;
77  }
78  for (i = 2; i < 8; i++)
79  v[i] = bytestream2_get_be16u(gb);
80  } else {
81  v[2] = v[4] = v[6] = v[0];
82  v[3] = v[5] = v[7] = v[1];
83  }
84 
85 #define PIX16(target, true, false) \
86  i = (mask & target) ? true : false; \
87  AV_WN16A(dst, (v[i] & 0x7C00) | (v[i] & 0x3E0) | (v[i] & 0x1F)); \
88  dst += 2;
89 
90 #define ROW16(row, a1, a0, b1, b0) \
91  dst = dst_start + (y + row) * linesize + x * 2; \
92  PIX16(1 << (row * 4), a1, a0) \
93  PIX16(1 << (row * 4 + 1), a1, a0) \
94  PIX16(1 << (row * 4 + 2), b1, b0) \
95  PIX16(1 << (row * 4 + 3), b1, b0)
96 
97  ROW16(0, 0, 1, 2, 3);
98  ROW16(1, 0, 1, 2, 3);
99  ROW16(2, 4, 5, 6, 7);
100  ROW16(3, 4, 5, 6, 7);
101 
102  x += 4;
103  if (x >= width) {
104  y += 4;
105  if (y >= height) {
106  break;
107  }
108  x = 0;
109  }
110  }
111  return 0;
112 }
113 
114 static void set_4x4_block(uint8_t *dst, int linesize, uint32_t pixel)
115 {
116  int i, j;
117  for (j = 0; j < 4; j++)
118  for (i = 0; i < 4; i++)
119  AV_WN32A(dst + j * linesize + i * 4, pixel);
120 }
121 
122 #define PIX32(target, true, false) \
123  AV_WN32A(dst, (mask & target) ? v[true] : v[false]); \
124  dst += 4;
125 
126 #define ROW32(row, a1, a0, b1, b0) \
127  dst = dst_start + (y + row) * linesize + x * 4; \
128  PIX32(1 << (row * 4), a1, a0) \
129  PIX32(1 << (row * 4 + 1), a1, a0) \
130  PIX32(1 << (row * 4 + 2), b1, b0) \
131  PIX32(1 << (row * 4 + 3), b1, b0)
132 
133 #define MVC2_BLOCK \
134  ROW32(0, 1, 0, 3, 2); \
135  ROW32(1, 1, 0, 3, 2); \
136  ROW32(2, 5, 4, 7, 6); \
137  ROW32(3, 5, 4, 7, 6);
138 
139 static int decode_mvc2(AVCodecContext *avctx, GetByteContext *gb, uint8_t *dst_start, int width, int height, int linesize, int vflip)
140 {
141  uint8_t *dst;
142  uint32_t color[128], v[8];
143  int w, h, nb_colors, i, x, y, p0, p1, mask;
144 
145  if (bytestream2_get_bytes_left(gb) < 6)
146  return AVERROR_INVALIDDATA;
147 
148  w = bytestream2_get_be16u(gb);
149  h = bytestream2_get_be16u(gb);
150  if ((w & ~3) != width || (h & ~3) != height)
151  av_log(avctx, AV_LOG_WARNING, "dimension mismatch\n");
152 
153  if (bytestream2_get_byteu(gb)) {
154  avpriv_request_sample(avctx, "bitmap feature");
155  return AVERROR_PATCHWELCOME;
156  }
157 
158  nb_colors = bytestream2_get_byteu(gb);
159  if (bytestream2_get_bytes_left(gb) < nb_colors * 3)
160  return AVERROR_INVALIDDATA;
161  for (i = 0; i < FFMIN(nb_colors, 128); i++)
162  color[i] = 0xFF000000 | bytestream2_get_be24u(gb);
163  if (nb_colors > 128)
164  bytestream2_skip(gb, (nb_colors - 128) * 3);
165 
166  if (vflip) {
167  dst_start += (height - 1) * linesize;
168  linesize = -linesize;
169  }
170  x = y = 0;
171  while (bytestream2_get_bytes_left(gb) >= 1) {
172  p0 = bytestream2_get_byteu(gb);
173  if ((p0 & 0x80)) {
174  if ((p0 & 0x40)) {
175  p0 &= 0x3F;
176  p0 = (p0 << 2) | (p0 >> 4);
177  set_4x4_block(dst_start + y * linesize + x * 4, linesize, 0xFF000000 | (p0 << 16) | (p0 << 8) | p0);
178  } else {
179  int g, r;
180  p0 &= 0x3F;
181  p0 = (p0 << 2) | (p0 >> 4);
182  if (bytestream2_get_bytes_left(gb) < 2)
183  return AVERROR_INVALIDDATA;
184  g = bytestream2_get_byteu(gb);
185  r = bytestream2_get_byteu(gb);
186  set_4x4_block(dst_start + y * linesize + x * 4, linesize, 0xFF000000 | (r << 16) | (g << 8) | p0);
187  }
188  } else {
189  if (bytestream2_get_bytes_left(gb) < 1)
190  return AVERROR_INVALIDDATA;
191  p1 = bytestream2_get_byteu(gb);
192  if ((p1 & 0x80)) {
193  if ((p0 & 0x7F) == (p1 & 0x7F)) {
194  set_4x4_block(dst_start + y * linesize + x * 4, linesize, color[p0 & 0x7F]);
195  } else {
196  if (bytestream2_get_bytes_left(gb) < 2)
197  return AVERROR_INVALIDDATA;
198  v[0] = v[2] = v[4] = v[6] = color[p0 & 0x7F];
199  v[1] = v[3] = v[5] = v[7] = color[p1 & 0x7F];
200  mask = bytestream2_get_le16u(gb);
201  MVC2_BLOCK
202  }
203  } else {
204  if (bytestream2_get_bytes_left(gb) < 8)
205  return AVERROR_INVALIDDATA;
206  v[0] = color[p0 & 0x7F];
207  v[1] = color[p1 & 0x7F];
208  for (i = 2; i < 8; i++)
209  v[i] = color[bytestream2_get_byteu(gb) & 0x7F];
210  mask = bytestream2_get_le16u(gb);
211  MVC2_BLOCK
212  }
213  }
214 
215  x += 4;
216  if (x >= width) {
217  y += 4;
218  if (y >= height)
219  break;
220  x = 0;
221  }
222  }
223  return 0;
224 }
225 
227  void *data, int *got_frame,
228  AVPacket *avpkt)
229 {
230  MvcContext *s = avctx->priv_data;
231  GetByteContext gb;
232  int ret;
233 
234  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
235  return ret;
236 
237  bytestream2_init(&gb, avpkt->data, avpkt->size);
238  if (avctx->codec_id == AV_CODEC_ID_MVC1)
239  ret = decode_mvc1(avctx, &gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
240  else
241  ret = decode_mvc2(avctx, &gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0], s->vflip);
242  if (ret < 0)
243  return ret;
244 
245  *got_frame = 1;
246  if ((ret = av_frame_ref(data, s->frame)) < 0)
247  return ret;
248 
249  return avpkt->size;
250 }
251 
253 {
254  MvcContext *s = avctx->priv_data;
255 
256  av_frame_free(&s->frame);
257 
258  return 0;
259 }
260 
261 #if CONFIG_MVC1_DECODER
262 AVCodec ff_mvc1_decoder = {
263  .name = "mvc1",
264  .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 1"),
265  .type = AVMEDIA_TYPE_VIDEO,
266  .id = AV_CODEC_ID_MVC1,
267  .priv_data_size = sizeof(MvcContext),
271  .capabilities = CODEC_CAP_DR1,
272 };
273 #endif
274 
275 #if CONFIG_MVC2_DECODER
276 AVCodec ff_mvc2_decoder = {
277  .name = "mvc2",
278  .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 2"),
279  .type = AVMEDIA_TYPE_VIDEO,
280  .id = AV_CODEC_ID_MVC2,
281  .priv_data_size = sizeof(MvcContext),
285  .capabilities = CODEC_CAP_DR1,
286 };
287 #endif