3 * Copyright (c) 2019 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mem.h"
31 #define BITSTREAM_READER_LE
34 #include "bytestream.h"
37 typedef struct MidiVidContext {
40 uint8_t *uncompressed;
41 unsigned int uncompressed_size;
47 static int decode_mvdv(MidiVidContext *s, AVCodecContext *avctx, AVFrame *frame)
49 GetByteContext *gb = &s->gb;
52 uint16_t nb_vectors, intra_flag;
54 const uint8_t *mask_start;
61 nb_vectors = bytestream2_get_le16(gb);
62 intra_flag = !!bytestream2_get_le16(gb);
64 nb_blocks = (avctx->width / 2) * (avctx->height / 2);
66 int ret, skip_linesize, padding;
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;
74 if (bytestream2_get_bytes_left(gb) < mask_size)
75 return AVERROR_INVALIDDATA;
77 ret = init_get_bits8(&mask, mask_start, mask_size);
80 bytestream2_skip(gb, mask_size);
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);
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;
92 skip_bits_long(&mask, padding);
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);
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];
114 for (int x = 0; x < avctx->width; x += 2) {
117 if (!intra_flag && *skip++)
119 if (bytestream2_get_bytes_left(gb) <= 0)
120 return AVERROR_INVALIDDATA;
121 if (nb_vectors <= 256) {
122 idx = bytestream2_get_byte(gb);
125 idx9val = bytestream2_get_byte(&idx9);
129 idx = bytestream2_get_byte(gb) | (((idx9val >> (7 - idx9bits)) & 1) << 8);
131 if (idx >= nb_vectors)
132 return AVERROR_INVALIDDATA;
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];
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];
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];
154 static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size)
156 uint8_t *dst_start = dst;
157 uint8_t *dst_end = dst + size;
159 for (;bytestream2_get_bytes_left(gb) >= 3;) {
160 int op = bytestream2_get_le16(gb);
162 for (int i = 0; i < 16; i++) {
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;
169 if (dst + length > dst_end ||
170 dst - offset < dst_start)
171 return AVERROR_INVALIDDATA;
173 for (int j = 0; j < length; j++) {
174 dst[j] = dst[j - offset];
180 return AVERROR_INVALIDDATA;
181 *dst++ = bytestream2_get_byte(gb);
187 return dst - dst_start;
190 static int decode_frame(AVCodecContext *avctx, void *data,
191 int *got_frame, AVPacket *avpkt)
193 MidiVidContext *s = avctx->priv_data;
194 GetByteContext *gb = &s->gb;
195 AVFrame *frame = s->frame;
196 int ret, key, uncompressed;
198 if (avpkt->size <= 13)
199 return AVERROR_INVALIDDATA;
201 bytestream2_init(gb, avpkt->data, avpkt->size);
202 bytestream2_skip(gb, 8);
203 uncompressed = bytestream2_get_le32(gb);
205 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
209 ret = decode_mvdv(s, avctx, frame);
211 av_fast_padded_malloc(&s->uncompressed, &s->uncompressed_size, 16LL * (avpkt->size - 12));
212 if (!s->uncompressed)
213 return AVERROR(ENOMEM);
215 ret = lzss_uncompress(s, gb, s->uncompressed, s->uncompressed_size);
218 bytestream2_init(gb, s->uncompressed, ret);
219 ret = decode_mvdv(s, avctx, frame);
226 if ((ret = av_frame_ref(data, s->frame)) < 0)
229 frame->pict_type = key ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
230 frame->key_frame = key;
236 static av_cold int decode_init(AVCodecContext *avctx)
238 MidiVidContext *s = avctx->priv_data;
239 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
241 if (avctx->width & 3 || avctx->height & 3)
242 ret = AVERROR_INVALIDDATA;
245 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
246 avctx->width, avctx->height);
250 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
252 s->frame = av_frame_alloc();
254 return AVERROR(ENOMEM);
255 s->skip = av_calloc(avctx->width >> 1, avctx->height >> 1);
257 return AVERROR(ENOMEM);
262 static void decode_flush(AVCodecContext *avctx)
264 MidiVidContext *s = avctx->priv_data;
266 av_frame_unref(s->frame);
269 static av_cold int decode_close(AVCodecContext *avctx)
271 MidiVidContext *s = avctx->priv_data;
273 av_frame_free(&s->frame);
274 av_freep(&s->uncompressed);
280 AVCodec ff_mvdv_decoder = {
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),
287 .decode = decode_frame,
288 .flush = decode_flush,
289 .close = decode_close,
290 .capabilities = AV_CODEC_CAP_DR1,
291 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,