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 
41  unsigned int uncompressed_size;
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;
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 = (avctx->width >> 5) * (avctx->height >> 2);
72 
73  if (bytestream2_get_bytes_left(gb) < mask_size)
74  return AVERROR_INVALIDDATA;
75 
76  ret = init_get_bits8(&mask, mask_start, mask_size);
77  if (ret < 0)
78  return ret;
79  bytestream2_skip(gb, mask_size);
80  skip = s->skip;
81 
82  for (int y = 0; y < avctx->height >> 2; y++) {
83  for (int x = 0; x < avctx->width >> 2; x++) {
84  int flag = !get_bits1(&mask);
85 
86  skip[(y*2) *skip_linesize + x*2 ] = flag;
87  skip[(y*2) *skip_linesize + x*2+1] = flag;
88  skip[(y*2+1)*skip_linesize + x*2 ] = flag;
89  skip[(y*2+1)*skip_linesize + x*2+1] = flag;
90  }
91  }
92  }
93 
94  vec = gb->buffer_start + bytestream2_tell(gb);
95  if (bytestream2_get_bytes_left(gb) < nb_vectors * 12)
96  return AVERROR_INVALIDDATA;
97  bytestream2_skip(gb, nb_vectors * 12);
98  if (nb_vectors > 256) {
99  if (bytestream2_get_bytes_left(gb) < (nb_blocks + 7) / 8)
100  return AVERROR_INVALIDDATA;
101  bytestream2_init(&idx9, gb->buffer_start + bytestream2_tell(gb), (nb_blocks + 7) / 8);
102  bytestream2_skip(gb, (nb_blocks + 7) / 8);
103  }
104 
105  skip = s->skip;
106 
107  for (int y = avctx->height - 2; y >= 0; y -= 2) {
108  uint8_t *dsty = frame->data[0] + y * frame->linesize[0];
109  uint8_t *dstu = frame->data[1] + y * frame->linesize[1];
110  uint8_t *dstv = frame->data[2] + y * frame->linesize[2];
111 
112  for (int x = 0; x < avctx->width; x += 2) {
113  int idx;
114 
115  if (!intra_flag && *skip++)
116  continue;
117  if (bytestream2_get_bytes_left(gb) <= 0)
118  return AVERROR_INVALIDDATA;
119  if (nb_vectors <= 256) {
120  idx = bytestream2_get_byte(gb);
121  } else {
122  if (idx9bits == 0) {
123  idx9val = bytestream2_get_byte(&idx9);
124  idx9bits = 8;
125  }
126  idx9bits--;
127  idx = bytestream2_get_byte(gb) | (((idx9val >> (7 - idx9bits)) & 1) << 8);
128  }
129 
130  dsty[x +frame->linesize[0]] = vec[idx * 12 + 0];
131  dsty[x+1+frame->linesize[0]] = vec[idx * 12 + 3];
132  dsty[x] = vec[idx * 12 + 6];
133  dsty[x+1] = vec[idx * 12 + 9];
134 
135  dstu[x +frame->linesize[1]] = vec[idx * 12 + 1];
136  dstu[x+1+frame->linesize[1]] = vec[idx * 12 + 4];
137  dstu[x] = vec[idx * 12 + 7];
138  dstu[x+1] = vec[idx * 12 +10];
139 
140  dstv[x +frame->linesize[2]] = vec[idx * 12 + 2];
141  dstv[x+1+frame->linesize[2]] = vec[idx * 12 + 5];
142  dstv[x] = vec[idx * 12 + 8];
143  dstv[x+1] = vec[idx * 12 +11];
144  }
145  }
146 
147  return intra_flag;
148 }
149 
150 static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size)
151 {
152  uint8_t *dst_start = dst;
153  uint8_t *dst_end = dst + size;
154 
155  for (;bytestream2_get_bytes_left(gb) >= 3;) {
156  int op = bytestream2_get_le16(gb);
157 
158  for (int i = 0; i < 16; i++) {
159  if (op & 1) {
160  int s0 = bytestream2_get_byte(gb);
161  int s1 = bytestream2_get_byte(gb);
162  int offset = ((s0 & 0xF0) << 4) | s1;
163  int length = (s0 & 0xF) + 3;
164 
165  if (dst + length > dst_end ||
166  dst - offset < dst_start)
167  return AVERROR_INVALIDDATA;
168  if (offset > 0) {
169  for (int j = 0; j < length; j++) {
170  dst[j] = dst[j - offset];
171  }
172  }
173  dst += length;
174  } else {
175  if (dst >= dst_end)
176  return AVERROR_INVALIDDATA;
177  *dst++ = bytestream2_get_byte(gb);
178  }
179  op >>= 1;
180  }
181  }
182 
183  return dst - dst_start;
184 }
185 
186 static int decode_frame(AVCodecContext *avctx, void *data,
187  int *got_frame, AVPacket *avpkt)
188 {
189  MidiVidContext *s = avctx->priv_data;
190  GetByteContext *gb = &s->gb;
191  AVFrame *frame = s->frame;
192  int ret, key, uncompressed;
193 
194  if (avpkt->size <= 13)
195  return AVERROR_INVALIDDATA;
196 
197  bytestream2_init(gb, avpkt->data, avpkt->size);
198  bytestream2_skip(gb, 8);
199  uncompressed = bytestream2_get_le32(gb);
200 
201  if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
202  return ret;
203 
204  if (uncompressed) {
205  ret = decode_mvdv(s, avctx, frame);
206  } else {
207  av_fast_padded_malloc(&s->uncompressed, &s->uncompressed_size, 16LL * (avpkt->size - 12));
208  if (!s->uncompressed)
209  return AVERROR(ENOMEM);
210 
211  ret = lzss_uncompress(s, gb, s->uncompressed, s->uncompressed_size);
212  if (ret < 0)
213  return ret;
214  bytestream2_init(gb, s->uncompressed, ret);
215  ret = decode_mvdv(s, avctx, frame);
216  }
217 
218  if (ret < 0)
219  return ret;
220  key = ret;
221 
222  if ((ret = av_frame_ref(data, s->frame)) < 0)
223  return ret;
224 
226  frame->key_frame = key;
227  *got_frame = 1;
228 
229  return avpkt->size;
230 }
231 
233 {
234  MidiVidContext *s = avctx->priv_data;
235  int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
236 
237  if (ret < 0) {
238  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
239  avctx->width, avctx->height);
240  return ret;
241  }
242 
243  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
244 
245  s->frame = av_frame_alloc();
246  if (!s->frame)
247  return AVERROR(ENOMEM);
248  s->skip = av_calloc(avctx->width >> 1, avctx->height >> 1);
249  if (!s->skip)
250  return AVERROR(ENOMEM);
251 
252  return 0;
253 }
254 
255 static void decode_flush(AVCodecContext *avctx)
256 {
257  MidiVidContext *s = avctx->priv_data;
258 
259  av_frame_unref(s->frame);
260 }
261 
263 {
264  MidiVidContext *s = avctx->priv_data;
265 
266  av_frame_free(&s->frame);
267  av_freep(&s->uncompressed);
268  av_freep(&s->skip);
269 
270  return 0;
271 }
272 
274  .name = "mvdv",
275  .long_name = NULL_IF_CONFIG_SMALL("MidiVid VQ"),
276  .type = AVMEDIA_TYPE_VIDEO,
277  .id = AV_CODEC_ID_MVDV,
278  .priv_data_size = sizeof(MidiVidContext),
279  .init = decode_init,
280  .decode = decode_frame,
281  .flush = decode_flush,
282  .close = decode_close,
283  .capabilities = AV_CODEC_CAP_DR1,
284  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
285 };
#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:48
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void flush(AVCodecContext *avctx)
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
Memory handling functions.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1494
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1791
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
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:70
const char * key
AVCodec.
Definition: avcodec.h:3505
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
AVCodec ff_mvdv_decoder
Definition: midivid.c:273
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
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
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:443
uint8_t * data
Definition: avcodec.h:1493
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:100
uint8_t * skip
Definition: midivid.c:42
#define av_log(a,...)
static av_cold int decode_close(AVCodecContext *avctx)
Definition: midivid.c:262
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const uint16_t mask[17]
Definition: lzw.c:38
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
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:2015
#define s0
Definition: regdef.h:37
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
GLsizei GLsizei * length
Definition: opengl_enc.c:114
const char * name
Name of the codec implementation.
Definition: avcodec.h:3512
common internal API header
GetByteContext gb
Definition: midivid.c:38
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:282
uint8_t * uncompressed
Definition: midivid.c:40
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: midivid.c:186
int width
picture width / height.
Definition: avcodec.h:1754
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame * frame
Definition: midivid.c:44
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
main external API structure.
Definition: avcodec.h:1581
static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size)
Definition: midivid.c:150
const uint8_t * buffer_start
Definition: bytestream.h:34
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void decode_flush(AVCodecContext *avctx)
Definition: midivid.c:255
#define s1
Definition: regdef.h:38
static int decode_mvdv(MidiVidContext *s, AVCodecContext *avctx, AVFrame *frame)
Definition: midivid.c:47
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
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:78
common internal api header.
#define flag(name)
Definition: cbs_av1.c:556
static av_cold int decode_init(AVCodecContext *avctx)
Definition: midivid.c:232
void * priv_data
Definition: avcodec.h:1608
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
unsigned int uncompressed_size
Definition: midivid.c:41
#define av_freep(p)
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
This structure stores compressed data.
Definition: avcodec.h:1470
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:987
Predicted.
Definition: avutil.h:275