FFmpeg
midivid.c
Go to the documentation of this file.
1 /*
2  * MidiVid decoder
3  * Copyright (c) 2019 Paul B Mahol
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 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mem.h"
30 
31 #define BITSTREAM_READER_LE
32 #include "avcodec.h"
33 #include "get_bits.h"
34 #include "bytestream.h"
35 #include "internal.h"
36 
37 typedef struct MidiVidContext {
39 
40  uint8_t *uncompressed;
41  unsigned int uncompressed_size;
42  uint8_t *skip;
43 
46 
48 {
49  GetByteContext *gb = &s->gb;
51  GetByteContext idx9;
52  uint16_t nb_vectors, intra_flag;
53  const uint8_t *vec;
54  const uint8_t *mask_start;
55  uint8_t *skip;
56  uint32_t mask_size;
57  int idx9bits = 0;
58  int idx9val = 0;
59  uint32_t nb_blocks;
60 
61  nb_vectors = bytestream2_get_le16(gb);
62  intra_flag = !!bytestream2_get_le16(gb);
63  if (intra_flag) {
64  nb_blocks = (avctx->width / 2) * (avctx->height / 2);
65  } else {
66  int ret, skip_linesize, padding;
67 
68  nb_blocks = bytestream2_get_le32(gb);
69  skip_linesize = avctx->width >> 1;
70  mask_start = gb->buffer_start + bytestream2_tell(gb);
71  mask_size = (FFALIGN(avctx->width, 32) >> 2) * (avctx->height >> 2) >> 3;
72  padding = (FFALIGN(avctx->width, 32) - avctx->width) >> 2;
73 
74  if (bytestream2_get_bytes_left(gb) < mask_size)
75  return AVERROR_INVALIDDATA;
76 
77  ret = init_get_bits8(&mask, mask_start, mask_size);
78  if (ret < 0)
79  return ret;
80  bytestream2_skip(gb, mask_size);
81  skip = s->skip;
82 
83  for (int y = 0; y < avctx->height >> 2; y++) {
84  for (int x = 0; x < avctx->width >> 2; x++) {
85  int flag = !get_bits1(&mask);
86 
87  skip[(y*2) *skip_linesize + x*2 ] = flag;
88  skip[(y*2) *skip_linesize + x*2+1] = flag;
89  skip[(y*2+1)*skip_linesize + x*2 ] = flag;
90  skip[(y*2+1)*skip_linesize + x*2+1] = flag;
91  }
92  skip_bits_long(&mask, padding);
93  }
94  }
95 
96  vec = gb->buffer_start + bytestream2_tell(gb);
97  if (bytestream2_get_bytes_left(gb) < nb_vectors * 12)
98  return AVERROR_INVALIDDATA;
99  bytestream2_skip(gb, nb_vectors * 12);
100  if (nb_vectors > 256) {
101  if (bytestream2_get_bytes_left(gb) < (nb_blocks + 7 * !intra_flag) / 8)
102  return AVERROR_INVALIDDATA;
103  bytestream2_init(&idx9, gb->buffer_start + bytestream2_tell(gb), (nb_blocks + 7 * !intra_flag) / 8);
104  bytestream2_skip(gb, (nb_blocks + 7 * !intra_flag) / 8);
105  }
106 
107  skip = s->skip;
108 
109  for (int y = avctx->height - 2; y >= 0; y -= 2) {
110  uint8_t *dsty = frame->data[0] + y * frame->linesize[0];
111  uint8_t *dstu = frame->data[1] + y * frame->linesize[1];
112  uint8_t *dstv = frame->data[2] + y * frame->linesize[2];
113 
114  for (int x = 0; x < avctx->width; x += 2) {
115  int idx;
116 
117  if (!intra_flag && *skip++)
118  continue;
119  if (bytestream2_get_bytes_left(gb) <= 0)
120  return AVERROR_INVALIDDATA;
121  if (nb_vectors <= 256) {
122  idx = bytestream2_get_byte(gb);
123  } else {
124  if (idx9bits == 0) {
125  idx9val = bytestream2_get_byte(&idx9);
126  idx9bits = 8;
127  }
128  idx9bits--;
129  idx = bytestream2_get_byte(gb) | (((idx9val >> (7 - idx9bits)) & 1) << 8);
130  }
131  if (idx >= nb_vectors)
132  return AVERROR_INVALIDDATA;
133 
134  dsty[x +frame->linesize[0]] = vec[idx * 12 + 0];
135  dsty[x+1+frame->linesize[0]] = vec[idx * 12 + 3];
136  dsty[x] = vec[idx * 12 + 6];
137  dsty[x+1] = vec[idx * 12 + 9];
138 
139  dstu[x +frame->linesize[1]] = vec[idx * 12 + 1];
140  dstu[x+1+frame->linesize[1]] = vec[idx * 12 + 4];
141  dstu[x] = vec[idx * 12 + 7];
142  dstu[x+1] = vec[idx * 12 +10];
143 
144  dstv[x +frame->linesize[2]] = vec[idx * 12 + 2];
145  dstv[x+1+frame->linesize[2]] = vec[idx * 12 + 5];
146  dstv[x] = vec[idx * 12 + 8];
147  dstv[x+1] = vec[idx * 12 +11];
148  }
149  }
150 
151  return intra_flag;
152 }
153 
154 static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size)
155 {
156  uint8_t *dst_start = dst;
157  uint8_t *dst_end = dst + size;
158 
159  for (;bytestream2_get_bytes_left(gb) >= 3;) {
160  int op = bytestream2_get_le16(gb);
161 
162  for (int i = 0; i < 16; i++) {
163  if (op & 1) {
164  int s0 = bytestream2_get_byte(gb);
165  int s1 = bytestream2_get_byte(gb);
166  int offset = ((s0 & 0xF0) << 4) | s1;
167  int length = (s0 & 0xF) + 3;
168 
169  if (dst + length > dst_end ||
170  dst - offset < dst_start)
171  return AVERROR_INVALIDDATA;
172  if (offset > 0) {
173  for (int j = 0; j < length; j++) {
174  dst[j] = dst[j - offset];
175  }
176  }
177  dst += length;
178  } else {
179  if (dst >= dst_end)
180  return AVERROR_INVALIDDATA;
181  *dst++ = bytestream2_get_byte(gb);
182  }
183  op >>= 1;
184  }
185  }
186 
187  return dst - dst_start;
188 }
189 
190 static int decode_frame(AVCodecContext *avctx, void *data,
191  int *got_frame, AVPacket *avpkt)
192 {
193  MidiVidContext *s = avctx->priv_data;
194  GetByteContext *gb = &s->gb;
195  AVFrame *frame = s->frame;
196  int ret, key, uncompressed;
197 
198  if (avpkt->size <= 13)
199  return AVERROR_INVALIDDATA;
200 
201  bytestream2_init(gb, avpkt->data, avpkt->size);
202  bytestream2_skip(gb, 8);
203  uncompressed = bytestream2_get_le32(gb);
204 
205  if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
206  return ret;
207 
208  if (uncompressed) {
209  ret = decode_mvdv(s, avctx, frame);
210  } else {
211  av_fast_padded_malloc(&s->uncompressed, &s->uncompressed_size, 16LL * (avpkt->size - 12));
212  if (!s->uncompressed)
213  return AVERROR(ENOMEM);
214 
215  ret = lzss_uncompress(s, gb, s->uncompressed, s->uncompressed_size);
216  if (ret < 0)
217  return ret;
218  bytestream2_init(gb, s->uncompressed, ret);
219  ret = decode_mvdv(s, avctx, frame);
220  }
221 
222  if (ret < 0)
223  return ret;
224  key = ret;
225 
226  if ((ret = av_frame_ref(data, s->frame)) < 0)
227  return ret;
228 
229  frame->pict_type = key ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
230  frame->key_frame = key;
231  *got_frame = 1;
232 
233  return avpkt->size;
234 }
235 
237 {
238  MidiVidContext *s = avctx->priv_data;
239  int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
240 
241  if (avctx->width & 3 || avctx->height & 3)
243 
244  if (ret < 0) {
245  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
246  avctx->width, avctx->height);
247  return ret;
248  }
249 
250  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
251 
252  s->frame = av_frame_alloc();
253  if (!s->frame)
254  return AVERROR(ENOMEM);
255  s->skip = av_calloc(avctx->width >> 1, avctx->height >> 1);
256  if (!s->skip)
257  return AVERROR(ENOMEM);
258 
259  return 0;
260 }
261 
262 static void decode_flush(AVCodecContext *avctx)
263 {
264  MidiVidContext *s = avctx->priv_data;
265 
266  av_frame_unref(s->frame);
267 }
268 
270 {
271  MidiVidContext *s = avctx->priv_data;
272 
273  av_frame_free(&s->frame);
274  av_freep(&s->uncompressed);
275  av_freep(&s->skip);
276 
277  return 0;
278 }
279 
281  .name = "mvdv",
282  .long_name = NULL_IF_CONFIG_SMALL("MidiVid VQ"),
283  .type = AVMEDIA_TYPE_VIDEO,
284  .id = AV_CODEC_ID_MVDV,
285  .priv_data_size = sizeof(MidiVidContext),
286  .init = decode_init,
287  .decode = decode_frame,
288  .flush = decode_flush,
289  .close = decode_close,
290  .capabilities = AV_CODEC_CAP_DR1,
292 };
AVCodec
AVCodec.
Definition: codec.h:202
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:292
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
GetByteContext::buffer_start
const uint8_t * buffer_start
Definition: bytestream.h:34
GetByteContext
Definition: bytestream.h:33
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
data
const char data[16]
Definition: mxf.c:143
MidiVidContext
Definition: midivid.c:37
MidiVidContext::uncompressed
uint8_t * uncompressed
Definition: midivid.c:40
init
static int init
Definition: av_tx.c:47
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: midivid.c:269
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
GetBitContext
Definition: get_bits.h:62
MidiVidContext::frame
AVFrame * frame
Definition: midivid.c:44
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:97
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
mask
static const uint16_t mask[17]
Definition: lzw.c:38
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
op
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:75
s1
#define s1
Definition: regdef.h:38
get_bits.h
key
const char * key
Definition: hwcontext_opencl.c:168
MidiVidContext::gb
GetByteContext gb
Definition: midivid.c:38
ff_mvdv_decoder
const AVCodec ff_mvdv_decoder
Definition: midivid.c:280
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
AV_CODEC_ID_MVDV
@ AV_CODEC_ID_MVDV
Definition: codec_id.h:297
decode_frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: midivid.c:190
MidiVidContext::skip
uint8_t * skip
Definition: midivid.c:42
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:499
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
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:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_frame_ref
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:325
size
int size
Definition: twinvq_data.h:10344
lzss_uncompress
static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size)
Definition: midivid.c:154
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
flag
#define flag(name)
Definition: cbs_av1.c:553
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:50
internal.h
av_fast_padded_malloc
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:50
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:435
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
avcodec.h
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1759
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
decode_mvdv
static int decode_mvdv(MidiVidContext *s, AVCodecContext *avctx, AVFrame *frame)
Definition: midivid.c:47
AVCodecContext
main external API structure.
Definition: avcodec.h:383
decode_flush
static void decode_flush(AVCodecContext *avctx)
Definition: midivid.c:262
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
s0
#define s0
Definition: regdef.h:37
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: midivid.c:236
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
bytestream.h
imgutils.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:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
av_image_check_size
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:318
MidiVidContext::uncompressed_size
unsigned int uncompressed_size
Definition: midivid.c:41