Line |
Branch |
Exec |
Source |
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 "libavutil/attributes.h" |
23 |
|
|
#include "libavutil/imgutils.h" |
24 |
|
|
#include "libavutil/internal.h" |
25 |
|
|
#include "libavutil/mem.h" |
26 |
|
|
|
27 |
|
|
#define BITSTREAM_READER_LE |
28 |
|
|
#include "avcodec.h" |
29 |
|
|
#include "get_bits.h" |
30 |
|
|
#include "bytestream.h" |
31 |
|
|
#include "codec_internal.h" |
32 |
|
|
#include "decode.h" |
33 |
|
|
|
34 |
|
|
typedef struct MidiVidContext { |
35 |
|
|
GetByteContext gb; |
36 |
|
|
|
37 |
|
|
uint8_t *uncompressed; |
38 |
|
|
unsigned int uncompressed_size; |
39 |
|
|
uint8_t *skip; |
40 |
|
|
|
41 |
|
|
AVFrame *frame; |
42 |
|
|
} MidiVidContext; |
43 |
|
|
|
44 |
|
✗ |
static int decode_mvdv(MidiVidContext *s, AVCodecContext *avctx, AVFrame *frame) |
45 |
|
|
{ |
46 |
|
✗ |
GetByteContext *gb = &s->gb; |
47 |
|
|
GetBitContext mask; |
48 |
|
|
GetByteContext idx9; |
49 |
|
|
uint16_t nb_vectors, intra_flag; |
50 |
|
|
const uint8_t *vec; |
51 |
|
|
const uint8_t *mask_start; |
52 |
|
|
uint8_t *skip; |
53 |
|
|
uint32_t mask_size; |
54 |
|
✗ |
int idx9bits = 0; |
55 |
|
✗ |
int idx9val = 0; |
56 |
|
|
uint32_t nb_blocks; |
57 |
|
|
|
58 |
|
✗ |
nb_vectors = bytestream2_get_le16(gb); |
59 |
|
✗ |
intra_flag = !!bytestream2_get_le16(gb); |
60 |
|
✗ |
if (intra_flag) { |
61 |
|
✗ |
nb_blocks = (avctx->width / 2) * (avctx->height / 2); |
62 |
|
|
} else { |
63 |
|
|
int ret, skip_linesize, padding; |
64 |
|
|
|
65 |
|
✗ |
nb_blocks = bytestream2_get_le32(gb); |
66 |
|
✗ |
skip_linesize = avctx->width >> 1; |
67 |
|
✗ |
mask_start = gb->buffer_start + bytestream2_tell(gb); |
68 |
|
✗ |
mask_size = (FFALIGN(avctx->width, 32) >> 2) * (avctx->height >> 2) >> 3; |
69 |
|
✗ |
padding = (FFALIGN(avctx->width, 32) - avctx->width) >> 2; |
70 |
|
|
|
71 |
|
✗ |
if (bytestream2_get_bytes_left(gb) < mask_size) |
72 |
|
✗ |
return AVERROR_INVALIDDATA; |
73 |
|
|
|
74 |
|
✗ |
ret = init_get_bits8(&mask, mask_start, mask_size); |
75 |
|
✗ |
if (ret < 0) |
76 |
|
✗ |
return ret; |
77 |
|
✗ |
bytestream2_skip(gb, mask_size); |
78 |
|
✗ |
skip = s->skip; |
79 |
|
|
|
80 |
|
✗ |
for (int y = 0; y < avctx->height >> 2; y++) { |
81 |
|
✗ |
for (int x = 0; x < avctx->width >> 2; x++) { |
82 |
|
✗ |
int flag = !get_bits1(&mask); |
83 |
|
|
|
84 |
|
✗ |
skip[(y*2) *skip_linesize + x*2 ] = flag; |
85 |
|
✗ |
skip[(y*2) *skip_linesize + x*2+1] = flag; |
86 |
|
✗ |
skip[(y*2+1)*skip_linesize + x*2 ] = flag; |
87 |
|
✗ |
skip[(y*2+1)*skip_linesize + x*2+1] = flag; |
88 |
|
|
} |
89 |
|
✗ |
skip_bits_long(&mask, padding); |
90 |
|
|
} |
91 |
|
|
} |
92 |
|
|
|
93 |
|
✗ |
vec = gb->buffer_start + bytestream2_tell(gb); |
94 |
|
✗ |
if (bytestream2_get_bytes_left(gb) < nb_vectors * 12) |
95 |
|
✗ |
return AVERROR_INVALIDDATA; |
96 |
|
✗ |
bytestream2_skip(gb, nb_vectors * 12); |
97 |
|
✗ |
if (nb_vectors > 256) { |
98 |
|
✗ |
if (bytestream2_get_bytes_left(gb) < (nb_blocks + 7 * !intra_flag) / 8) |
99 |
|
✗ |
return AVERROR_INVALIDDATA; |
100 |
|
✗ |
bytestream2_init(&idx9, gb->buffer_start + bytestream2_tell(gb), (nb_blocks + 7 * !intra_flag) / 8); |
101 |
|
✗ |
bytestream2_skip(gb, (nb_blocks + 7 * !intra_flag) / 8); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
✗ |
skip = s->skip; |
105 |
|
|
|
106 |
|
✗ |
for (int y = avctx->height - 2; y >= 0; y -= 2) { |
107 |
|
✗ |
uint8_t *dsty = frame->data[0] + y * frame->linesize[0]; |
108 |
|
✗ |
uint8_t *dstu = frame->data[1] + y * frame->linesize[1]; |
109 |
|
✗ |
uint8_t *dstv = frame->data[2] + y * frame->linesize[2]; |
110 |
|
|
|
111 |
|
✗ |
for (int x = 0; x < avctx->width; x += 2) { |
112 |
|
|
int idx; |
113 |
|
|
|
114 |
|
✗ |
if (!intra_flag && *skip++) |
115 |
|
✗ |
continue; |
116 |
|
✗ |
if (bytestream2_get_bytes_left(gb) <= 0) |
117 |
|
✗ |
return AVERROR_INVALIDDATA; |
118 |
|
✗ |
if (nb_vectors <= 256) { |
119 |
|
✗ |
idx = bytestream2_get_byte(gb); |
120 |
|
|
} else { |
121 |
|
✗ |
if (idx9bits == 0) { |
122 |
|
✗ |
idx9val = bytestream2_get_byte(&idx9); |
123 |
|
✗ |
idx9bits = 8; |
124 |
|
|
} |
125 |
|
✗ |
idx9bits--; |
126 |
|
✗ |
idx = bytestream2_get_byte(gb) | (((idx9val >> (7 - idx9bits)) & 1) << 8); |
127 |
|
|
} |
128 |
|
✗ |
if (idx >= nb_vectors) |
129 |
|
✗ |
return AVERROR_INVALIDDATA; |
130 |
|
|
|
131 |
|
✗ |
dsty[x +frame->linesize[0]] = vec[idx * 12 + 0]; |
132 |
|
✗ |
dsty[x+1+frame->linesize[0]] = vec[idx * 12 + 3]; |
133 |
|
✗ |
dsty[x] = vec[idx * 12 + 6]; |
134 |
|
✗ |
dsty[x+1] = vec[idx * 12 + 9]; |
135 |
|
|
|
136 |
|
✗ |
dstu[x +frame->linesize[1]] = vec[idx * 12 + 1]; |
137 |
|
✗ |
dstu[x+1+frame->linesize[1]] = vec[idx * 12 + 4]; |
138 |
|
✗ |
dstu[x] = vec[idx * 12 + 7]; |
139 |
|
✗ |
dstu[x+1] = vec[idx * 12 +10]; |
140 |
|
|
|
141 |
|
✗ |
dstv[x +frame->linesize[2]] = vec[idx * 12 + 2]; |
142 |
|
✗ |
dstv[x+1+frame->linesize[2]] = vec[idx * 12 + 5]; |
143 |
|
✗ |
dstv[x] = vec[idx * 12 + 8]; |
144 |
|
✗ |
dstv[x+1] = vec[idx * 12 +11]; |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
|
148 |
|
✗ |
return intra_flag; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
✗ |
static ptrdiff_t lzss_uncompress(MidiVidContext *s, GetByteContext *gb, uint8_t *dst, unsigned int size) |
152 |
|
|
{ |
153 |
|
✗ |
uint8_t *dst_start = dst; |
154 |
|
✗ |
uint8_t *dst_end = dst + size; |
155 |
|
|
|
156 |
|
✗ |
for (;bytestream2_get_bytes_left(gb) >= 3;) { |
157 |
|
✗ |
int op = bytestream2_get_le16(gb); |
158 |
|
|
|
159 |
|
✗ |
for (int i = 0; i < 16; i++) { |
160 |
|
✗ |
if (op & 1) { |
161 |
|
✗ |
int s0 = bytestream2_get_byte(gb); |
162 |
|
✗ |
int s1 = bytestream2_get_byte(gb); |
163 |
|
✗ |
int offset = ((s0 & 0xF0) << 4) | s1; |
164 |
|
✗ |
int length = (s0 & 0xF) + 3; |
165 |
|
|
|
166 |
|
✗ |
if (dst + length > dst_end || |
167 |
|
✗ |
dst - offset < dst_start) |
168 |
|
✗ |
return AVERROR_INVALIDDATA; |
169 |
|
✗ |
if (offset > 0) { |
170 |
|
✗ |
for (int j = 0; j < length; j++) { |
171 |
|
✗ |
dst[j] = dst[j - offset]; |
172 |
|
|
} |
173 |
|
|
} |
174 |
|
✗ |
dst += length; |
175 |
|
|
} else { |
176 |
|
✗ |
if (dst >= dst_end) |
177 |
|
✗ |
return AVERROR_INVALIDDATA; |
178 |
|
✗ |
*dst++ = bytestream2_get_byte(gb); |
179 |
|
|
} |
180 |
|
✗ |
op >>= 1; |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
|
184 |
|
✗ |
return dst - dst_start; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
✗ |
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
188 |
|
|
int *got_frame, AVPacket *avpkt) |
189 |
|
|
{ |
190 |
|
✗ |
MidiVidContext *s = avctx->priv_data; |
191 |
|
✗ |
GetByteContext *gb = &s->gb; |
192 |
|
✗ |
AVFrame *frame = s->frame; |
193 |
|
|
int ret, key, uncompressed; |
194 |
|
|
|
195 |
|
✗ |
if (avpkt->size <= 13) |
196 |
|
✗ |
return AVERROR_INVALIDDATA; |
197 |
|
|
|
198 |
|
✗ |
bytestream2_init(gb, avpkt->data, avpkt->size); |
199 |
|
✗ |
bytestream2_skip(gb, 8); |
200 |
|
✗ |
uncompressed = bytestream2_get_le32(gb); |
201 |
|
|
|
202 |
|
✗ |
if (!uncompressed) { |
203 |
|
✗ |
av_fast_padded_malloc(&s->uncompressed, &s->uncompressed_size, 16LL * (avpkt->size - 12)); |
204 |
|
✗ |
if (!s->uncompressed) |
205 |
|
✗ |
return AVERROR(ENOMEM); |
206 |
|
|
|
207 |
|
✗ |
ret = lzss_uncompress(s, gb, s->uncompressed, s->uncompressed_size); |
208 |
|
✗ |
if (ret < 0) |
209 |
|
✗ |
return ret; |
210 |
|
✗ |
bytestream2_init(gb, s->uncompressed, ret); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
✗ |
if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0) |
214 |
|
✗ |
return ret; |
215 |
|
|
|
216 |
|
✗ |
ret = decode_mvdv(s, avctx, frame); |
217 |
|
|
|
218 |
|
✗ |
if (ret < 0) |
219 |
|
✗ |
return ret; |
220 |
|
✗ |
key = ret; |
221 |
|
|
|
222 |
|
✗ |
if ((ret = av_frame_ref(rframe, s->frame)) < 0) |
223 |
|
✗ |
return ret; |
224 |
|
|
|
225 |
|
✗ |
frame->pict_type = key ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; |
226 |
|
✗ |
if (key) |
227 |
|
✗ |
frame->flags |= AV_FRAME_FLAG_KEY; |
228 |
|
|
else |
229 |
|
✗ |
frame->flags &= ~AV_FRAME_FLAG_KEY; |
230 |
|
✗ |
*got_frame = 1; |
231 |
|
|
|
232 |
|
✗ |
return avpkt->size; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
✗ |
static av_cold int decode_init(AVCodecContext *avctx) |
236 |
|
|
{ |
237 |
|
✗ |
MidiVidContext *s = avctx->priv_data; |
238 |
|
✗ |
int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx); |
239 |
|
|
|
240 |
|
✗ |
if (avctx->width & 3 || avctx->height & 3) |
241 |
|
✗ |
ret = AVERROR_INVALIDDATA; |
242 |
|
|
|
243 |
|
✗ |
if (ret < 0) { |
244 |
|
✗ |
av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", |
245 |
|
|
avctx->width, avctx->height); |
246 |
|
✗ |
return ret; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
✗ |
avctx->pix_fmt = AV_PIX_FMT_YUV444P; |
250 |
|
|
|
251 |
|
✗ |
s->frame = av_frame_alloc(); |
252 |
|
✗ |
if (!s->frame) |
253 |
|
✗ |
return AVERROR(ENOMEM); |
254 |
|
✗ |
s->skip = av_calloc(avctx->width >> 1, avctx->height >> 1); |
255 |
|
✗ |
if (!s->skip) |
256 |
|
✗ |
return AVERROR(ENOMEM); |
257 |
|
|
|
258 |
|
✗ |
return 0; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
✗ |
static av_cold void decode_flush(AVCodecContext *avctx) |
262 |
|
|
{ |
263 |
|
✗ |
MidiVidContext *s = avctx->priv_data; |
264 |
|
|
|
265 |
|
✗ |
av_frame_unref(s->frame); |
266 |
|
✗ |
} |
267 |
|
|
|
268 |
|
✗ |
static av_cold int decode_close(AVCodecContext *avctx) |
269 |
|
|
{ |
270 |
|
✗ |
MidiVidContext *s = avctx->priv_data; |
271 |
|
|
|
272 |
|
✗ |
av_frame_free(&s->frame); |
273 |
|
✗ |
av_freep(&s->uncompressed); |
274 |
|
✗ |
av_freep(&s->skip); |
275 |
|
|
|
276 |
|
✗ |
return 0; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
const FFCodec ff_mvdv_decoder = { |
280 |
|
|
.p.name = "mvdv", |
281 |
|
|
CODEC_LONG_NAME("MidiVid VQ"), |
282 |
|
|
.p.type = AVMEDIA_TYPE_VIDEO, |
283 |
|
|
.p.id = AV_CODEC_ID_MVDV, |
284 |
|
|
.priv_data_size = sizeof(MidiVidContext), |
285 |
|
|
.init = decode_init, |
286 |
|
|
FF_CODEC_DECODE_CB(decode_frame), |
287 |
|
|
.flush = decode_flush, |
288 |
|
|
.close = decode_close, |
289 |
|
|
.p.capabilities = AV_CODEC_CAP_DR1, |
290 |
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
291 |
|
|
}; |
292 |
|
|
|