FFmpeg
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 "config_components.h"
28 
29 #include "libavutil/intreadwrite.h"
30 
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "codec_internal.h"
34 #include "decode.h"
35 
36 typedef struct MvcContext {
37  int vflip;
38 } MvcContext;
39 
41 {
42  MvcContext *s = avctx->priv_data;
43  int width = avctx->width;
44  int height = avctx->height;
45  int ret;
46 
47  if (avctx->codec_id == AV_CODEC_ID_MVC1) {
48  width += 3;
49  height += 3;
50  }
51  width &= ~3;
52  height &= ~3;
53  if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
54  return ret;
55 
56  avctx->pix_fmt = (avctx->codec_id == AV_CODEC_ID_MVC1) ? AV_PIX_FMT_RGB555
58  s->vflip = avctx->extradata_size >= 9 &&
59  !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9);
60  return 0;
61 }
62 
63 static int decode_mvc1(AVCodecContext *avctx, GetByteContext *gb,
64  uint8_t *dst_start, int width, int height, int linesize)
65 {
66  uint8_t *dst;
67  uint16_t v[8];
68  int mask, x, y, i;
69 
70  for (y = 0; y < height; y += 4) {
71  for (x = 0; x < width; x += 4) {
72  if (bytestream2_get_bytes_left(gb) < 6)
73  return 0;
74 
75  mask = bytestream2_get_be16u(gb);
76  v[0] = bytestream2_get_be16u(gb);
77  v[1] = bytestream2_get_be16u(gb);
78  if ((v[0] & 0x8000)) {
79  if (bytestream2_get_bytes_left(gb) < 12) {
80  av_log(avctx, AV_LOG_WARNING, "buffer overflow\n");
81  return AVERROR_INVALIDDATA;
82  }
83  for (i = 2; i < 8; i++)
84  v[i] = bytestream2_get_be16u(gb);
85  } else {
86  v[2] = v[4] = v[6] = v[0];
87  v[3] = v[5] = v[7] = v[1];
88  }
89 
90 #define PIX16(target, true, false) \
91  i = (mask & target) ? true : false; \
92  AV_WN16A(dst, v[i] & 0x7FFF); \
93  dst += 2;
94 
95 #define ROW16(row, a1, a0, b1, b0) \
96  dst = dst_start + (y + row) * linesize + x * 2; \
97  PIX16(1 << (row * 4), a1, a0) \
98  PIX16(1 << (row * 4 + 1), a1, a0) \
99  PIX16(1 << (row * 4 + 2), b1, b0) \
100  PIX16(1 << (row * 4 + 3), b1, b0)
101 
102  ROW16(0, 0, 1, 2, 3);
103  ROW16(1, 0, 1, 2, 3);
104  ROW16(2, 4, 5, 6, 7);
105  ROW16(3, 4, 5, 6, 7);
106  }
107  }
108  return 0;
109 }
110 
111 static void set_4x4_block(uint8_t *dst, int linesize, uint32_t pixel)
112 {
113  int i, j;
114  for (j = 0; j < 4; j++)
115  for (i = 0; i < 4; i++)
116  AV_WN32A(dst + j * linesize + i * 4, pixel);
117 }
118 
119 #define PIX32(target, true, false) \
120  AV_WN32A(dst, (mask & target) ? v[true] : v[false]); \
121  dst += 4;
122 
123 #define ROW32(row, a1, a0, b1, b0) \
124  dst = dst_start + (y + row) * linesize + x * 4; \
125  PIX32(1 << (row * 4), a1, a0) \
126  PIX32(1 << (row * 4 + 1), a1, a0) \
127  PIX32(1 << (row * 4 + 2), b1, b0) \
128  PIX32(1 << (row * 4 + 3), b1, b0)
129 
130 #define MVC2_BLOCK \
131  ROW32(0, 1, 0, 3, 2); \
132  ROW32(1, 1, 0, 3, 2); \
133  ROW32(2, 5, 4, 7, 6); \
134  ROW32(3, 5, 4, 7, 6);
135 
137  uint8_t *dst_start, int width, int height,
138  int linesize, int vflip)
139 {
140  uint8_t *dst;
141  uint32_t color[128], v[8];
142  int w, h, nb_colors, i, x, y, p0, p1, mask;
143 
144  if (bytestream2_get_bytes_left(gb) < 6)
145  return AVERROR_INVALIDDATA;
146 
147  w = bytestream2_get_be16u(gb);
148  h = bytestream2_get_be16u(gb);
149  if ((w & ~3) != width || (h & ~3) != height)
150  av_log(avctx, AV_LOG_WARNING, "dimension mismatch\n");
151 
152  if (bytestream2_get_byteu(gb)) {
153  avpriv_request_sample(avctx, "bitmap feature");
154  return AVERROR_PATCHWELCOME;
155  }
156 
157  nb_colors = bytestream2_get_byteu(gb);
158  if (bytestream2_get_bytes_left(gb) < nb_colors * 3)
159  return AVERROR_INVALIDDATA;
160  for (i = 0; i < FFMIN(nb_colors, 128); i++)
161  color[i] = 0xFF000000 | bytestream2_get_be24u(gb);
162  if (nb_colors > 128)
163  bytestream2_skip(gb, (nb_colors - 128) * 3);
164 
165  if (vflip) {
166  dst_start += (height - 1) * linesize;
167  linesize = -linesize;
168  }
169  x = y = 0;
170  while (bytestream2_get_bytes_left(gb) >= 1) {
171  p0 = bytestream2_get_byteu(gb);
172  if ((p0 & 0x80)) {
173  if ((p0 & 0x40)) {
174  p0 &= 0x3F;
175  p0 = (p0 << 2) | (p0 >> 4);
176  set_4x4_block(dst_start + y * linesize + x * 4, linesize,
177  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,
187  0xFF000000 | (r << 16) | (g << 8) | p0);
188  }
189  } else {
190  if (bytestream2_get_bytes_left(gb) < 1)
191  return AVERROR_INVALIDDATA;
192  p1 = bytestream2_get_byteu(gb);
193  if ((p1 & 0x80)) {
194  if ((p0 & 0x7F) == (p1 & 0x7F)) {
195  set_4x4_block(dst_start + y * linesize + x * 4, linesize,
196  color[p0 & 0x7F]);
197  } else {
198  if (bytestream2_get_bytes_left(gb) < 2)
199  return AVERROR_INVALIDDATA;
200  v[0] = v[2] = v[4] = v[6] = color[p0 & 0x7F];
201  v[1] = v[3] = v[5] = v[7] = color[p1 & 0x7F];
202  mask = bytestream2_get_le16u(gb);
203  MVC2_BLOCK
204  }
205  } else {
206  if (bytestream2_get_bytes_left(gb) < 8)
207  return AVERROR_INVALIDDATA;
208  v[0] = color[p0 & 0x7F];
209  v[1] = color[p1 & 0x7F];
210  for (i = 2; i < 8; i++)
211  v[i] = color[bytestream2_get_byteu(gb) & 0x7F];
212  mask = bytestream2_get_le16u(gb);
213  MVC2_BLOCK
214  }
215  }
216 
217  x += 4;
218  if (x >= width) {
219  y += 4;
220  if (y >= height)
221  break;
222  x = 0;
223  }
224  }
225  return 0;
226 }
227 
229  int *got_frame, AVPacket *avpkt)
230 {
231  MvcContext *s = avctx->priv_data;
232  GetByteContext gb;
233  int ret;
234 
235  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
236  return ret;
237 
238  bytestream2_init(&gb, avpkt->data, avpkt->size);
239  if (avctx->codec_id == AV_CODEC_ID_MVC1)
240  ret = decode_mvc1(avctx, &gb, frame->data[0],
241  avctx->width, avctx->height, frame->linesize[0]);
242  else
243  ret = decode_mvc2(avctx, &gb, frame->data[0],
244  avctx->width, avctx->height, frame->linesize[0],
245  s->vflip);
246  if (ret < 0)
247  return ret;
248 
249  *got_frame = 1;
250 
251  return avpkt->size;
252 }
253 
254 #if CONFIG_MVC1_DECODER
255 const FFCodec ff_mvc1_decoder = {
256  .p.name = "mvc1",
257  CODEC_LONG_NAME("Silicon Graphics Motion Video Compressor 1"),
258  .p.type = AVMEDIA_TYPE_VIDEO,
259  .p.id = AV_CODEC_ID_MVC1,
260  .priv_data_size = sizeof(MvcContext),
263  .p.capabilities = AV_CODEC_CAP_DR1,
264 };
265 #endif
266 
267 #if CONFIG_MVC2_DECODER
268 const FFCodec ff_mvc2_decoder = {
269  .p.name = "mvc2",
270  CODEC_LONG_NAME("Silicon Graphics Motion Video Compressor 2"),
271  .p.type = AVMEDIA_TYPE_VIDEO,
272  .p.id = AV_CODEC_ID_MVC2,
273  .priv_data_size = sizeof(MvcContext),
276  .p.capabilities = AV_CODEC_CAP_DR1,
277 };
278 #endif
MVC2_BLOCK
#define MVC2_BLOCK
Definition: mvcdec.c:130
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
r
const char * r
Definition: vf_curves.c:127
color
Definition: vf_paletteuse.c:513
mvc_decode_init
static av_cold int mvc_decode_init(AVCodecContext *avctx)
Definition: mvcdec.c:40
GetByteContext
Definition: bytestream.h:33
AV_CODEC_ID_MVC2
@ AV_CODEC_ID_MVC2
Definition: codec_id.h:239
mask
int mask
Definition: mediacodecdec_common.c:154
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:539
FFCodec
Definition: codec_internal.h:127
AV_WN32A
#define AV_WN32A(p, v)
Definition: intreadwrite.h:534
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:94
decode_mvc2
static int decode_mvc2(AVCodecContext *avctx, GetByteContext *gb, uint8_t *dst_start, int width, int height, int linesize, int vflip)
Definition: mvcdec.c:136
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
MvcContext
Definition: mvcdec.c:36
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:530
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
g
const char * g
Definition: vf_curves.c:128
decode_mvc1
static int decode_mvc1(AVCodecContext *avctx, GetByteContext *gb, uint8_t *dst_start, int width, int height, int linesize)
Definition: mvcdec.c:63
mvc_decode_frame
static int mvc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: mvcdec.c:228
decode.h
set_4x4_block
static void set_4x4_block(uint8_t *dst, int linesize, uint32_t pixel)
Definition: mvcdec.c:111
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:461
ROW16
#define ROW16(row, a1, a0, b1, b0)
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
pixel
uint8_t pixel
Definition: tiny_ssim.c:41
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
height
#define height
Definition: dsp.h:85
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:475
ff_mvc1_decoder
const FFCodec ff_mvc1_decoder
ff_mvc2_decoder
const FFCodec ff_mvc2_decoder
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:529
AV_PIX_FMT_RGB555
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:490
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
AV_CODEC_ID_MVC1
@ AV_CODEC_ID_MVC1
Definition: codec_id.h:238
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2070
MvcContext::vflip
int vflip
Definition: mvcdec.c:37
width
#define width
Definition: dsp.h:85