/src/ffmpeg/libavcodec/mss4.c
Line | Count | Source |
1 | | /* |
2 | | * Microsoft Screen 4 (aka Microsoft Expression Encoder Screen) decoder |
3 | | * Copyright (c) 2012 Konstantin Shishkov |
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 | | * Microsoft Screen 4 (aka Microsoft Titanium Screen 2, |
25 | | * aka Microsoft Expression Encoder Screen) decoder |
26 | | */ |
27 | | |
28 | | #include "libavutil/mem.h" |
29 | | #include "libavutil/thread.h" |
30 | | #include "libavutil/imgutils.h" |
31 | | |
32 | | #include "avcodec.h" |
33 | | #include "bytestream.h" |
34 | | #include "codec_internal.h" |
35 | | #include "decode.h" |
36 | | #include "get_bits.h" |
37 | | #include "jpegtables.h" |
38 | | #include "mss34dsp.h" |
39 | | #include "unary.h" |
40 | | |
41 | 134k | #define HEADER_SIZE 8 |
42 | | |
43 | | enum FrameType { |
44 | | INTRA_FRAME = 0, |
45 | | INTER_FRAME, |
46 | | SKIP_FRAME |
47 | | }; |
48 | | |
49 | | enum BlockType { |
50 | | SKIP_BLOCK = 0, |
51 | | DCT_BLOCK, |
52 | | IMAGE_BLOCK, |
53 | | }; |
54 | | |
55 | | enum CachePos { |
56 | | LEFT = 0, |
57 | | TOP_LEFT, |
58 | | TOP, |
59 | | }; |
60 | | |
61 | | static const uint8_t mss4_dc_vlc_lens[2][16] = { |
62 | | { 0, 1, 5, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0 }, |
63 | | { 0, 3, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0 } |
64 | | }; |
65 | | |
66 | | static const uint8_t vec_len_syms[2][4] = { |
67 | | { 4, 2, 3, 1 }, |
68 | | { 4, 1, 2, 3 } |
69 | | }; |
70 | | |
71 | | static const uint8_t mss4_vec_entry_vlc_lens[2][16] = { |
72 | | { 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
73 | | { 0, 1, 5, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } |
74 | | }; |
75 | | |
76 | | static const uint8_t mss4_vec_entry_vlc_syms[2][9] = { |
77 | | { 0, 7, 6, 5, 8, 4, 3, 1, 2 }, |
78 | | { 0, 2, 3, 4, 5, 6, 7, 1, 8 } |
79 | | }; |
80 | | |
81 | | #define MAX_ENTRIES 162 |
82 | | |
83 | | typedef struct MSS4Context { |
84 | | AVFrame *pic; |
85 | | |
86 | | int block[64]; |
87 | | uint8_t imgbuf[3][16 * 16]; |
88 | | |
89 | | int quality; |
90 | | uint16_t quant_mat[2][64]; |
91 | | |
92 | | int *prev_dc[3]; |
93 | | ptrdiff_t dc_stride[3]; |
94 | | int dc_cache[4][4]; |
95 | | |
96 | | int prev_vec[3][4]; |
97 | | } MSS4Context; |
98 | | |
99 | | static VLC dc_vlc[2], ac_vlc[2]; |
100 | | static VLC vec_entry_vlc[2]; |
101 | | |
102 | | static av_cold void mss4_init_vlc(VLC *vlc, unsigned *offset, |
103 | | const uint8_t *lens, const uint8_t *syms) |
104 | 6 | { |
105 | 6 | static VLCElem vlc_buf[2146]; |
106 | 6 | uint8_t bits[MAX_ENTRIES]; |
107 | 6 | int i, j; |
108 | 6 | int idx = 0; |
109 | | |
110 | 102 | for (i = 0; i < 16; i++) { |
111 | 462 | for (j = 0; j < lens[i]; j++) { |
112 | 366 | bits[idx] = i + 1; |
113 | 366 | idx++; |
114 | 366 | } |
115 | 96 | } |
116 | | |
117 | 6 | vlc->table = &vlc_buf[*offset]; |
118 | 6 | vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *offset; |
119 | 6 | ff_vlc_init_from_lengths(vlc, FFMIN(bits[idx - 1], 9), idx, |
120 | 6 | bits, 1, syms, 1, 1, |
121 | 6 | 0, VLC_INIT_STATIC_OVERLONG, NULL); |
122 | 6 | *offset += vlc->table_size; |
123 | 6 | } |
124 | | |
125 | | static av_cold void mss4_init_vlcs(void) |
126 | 1 | { |
127 | 3 | for (unsigned i = 0, offset = 0; i < 2; i++) { |
128 | 2 | mss4_init_vlc(&dc_vlc[i], &offset, mss4_dc_vlc_lens[i], NULL); |
129 | 2 | mss4_init_vlc(&ac_vlc[i], &offset, |
130 | 2 | i ? ff_mjpeg_bits_ac_chrominance + 1 |
131 | 2 | : ff_mjpeg_bits_ac_luminance + 1, |
132 | 2 | i ? ff_mjpeg_val_ac_chrominance |
133 | 2 | : ff_mjpeg_val_ac_luminance); |
134 | 2 | mss4_init_vlc(&vec_entry_vlc[i], &offset, mss4_vec_entry_vlc_lens[i], |
135 | 2 | mss4_vec_entry_vlc_syms[i]); |
136 | 2 | } |
137 | 1 | } |
138 | | |
139 | | /* This function returns values in the range |
140 | | * (-range + 1; -range/2] U [range/2; range - 1) |
141 | | * i.e. |
142 | | * nbits = 0 -> 0 |
143 | | * nbits = 1 -> -1, 1 |
144 | | * nbits = 2 -> -3, -2, 2, 3 |
145 | | */ |
146 | | static av_always_inline int get_coeff_bits(GetBitContext *gb, int nbits) |
147 | 28.5M | { |
148 | 28.5M | int val; |
149 | | |
150 | 28.5M | if (!nbits) |
151 | 474k | return 0; |
152 | | |
153 | 28.0M | val = get_bits(gb, nbits); |
154 | 28.0M | if (val < (1 << (nbits - 1))) |
155 | 16.5M | val -= (1 << nbits) - 1; |
156 | | |
157 | 28.0M | return val; |
158 | 28.5M | } |
159 | | |
160 | | static inline int get_coeff(GetBitContext *gb, const VLC *vlc, |
161 | | int nb_bits, int max_depth) |
162 | 5.39M | { |
163 | 5.39M | int val = get_vlc2(gb, vlc->table, nb_bits, max_depth); |
164 | | |
165 | 5.39M | return get_coeff_bits(gb, val); |
166 | 5.39M | } |
167 | | |
168 | | static int mss4_decode_dct(GetBitContext *gb, VLC *dc_vlc, VLC *ac_vlc, |
169 | | int *block, int *dc_cache, |
170 | | int bx, int by, uint16_t *quant_mat) |
171 | 2.73M | { |
172 | 2.73M | int skip, val, pos = 1, zz_pos, dc; |
173 | | |
174 | 2.73M | memset(block, 0, sizeof(*block) * 64); |
175 | | |
176 | 2.73M | dc = get_coeff(gb, dc_vlc, dc_vlc->bits, 2); |
177 | | // DC prediction is the same as in MSS3 |
178 | 2.73M | if (by) { |
179 | 2.02M | if (bx) { |
180 | 1.91M | int l, tl, t; |
181 | | |
182 | 1.91M | l = dc_cache[LEFT]; |
183 | 1.91M | tl = dc_cache[TOP_LEFT]; |
184 | 1.91M | t = dc_cache[TOP]; |
185 | | |
186 | 1.91M | if (FFABS(t - tl) <= FFABS(l - tl)) |
187 | 956k | dc += l; |
188 | 954k | else |
189 | 954k | dc += t; |
190 | 1.91M | } else { |
191 | 110k | dc += dc_cache[TOP]; |
192 | 110k | } |
193 | 2.02M | } else if (bx) { |
194 | 678k | dc += dc_cache[LEFT]; |
195 | 678k | } |
196 | 2.73M | dc_cache[LEFT] = dc; |
197 | 2.73M | block[0] = dc * quant_mat[0]; |
198 | | |
199 | 25.9M | while (pos < 64) { |
200 | 25.6M | val = get_vlc2(gb, ac_vlc->table, 9, 2); |
201 | 25.6M | if (!val) |
202 | 2.44M | return 0; |
203 | 23.1M | if (val == -1) |
204 | 397 | return -1; |
205 | 23.1M | if (val == 0xF0) { |
206 | 525 | pos += 16; |
207 | 525 | continue; |
208 | 525 | } |
209 | 23.1M | skip = val >> 4; |
210 | 23.1M | val = get_coeff_bits(gb, val & 0xF); |
211 | 23.1M | pos += skip; |
212 | 23.1M | if (pos >= 64) |
213 | 4.38k | return -1; |
214 | | |
215 | 23.1M | zz_pos = ff_zigzag_direct[pos]; |
216 | 23.1M | block[zz_pos] = val * quant_mat[zz_pos]; |
217 | 23.1M | pos++; |
218 | 23.1M | } |
219 | | |
220 | 281k | return pos == 64 ? 0 : -1; |
221 | 2.73M | } |
222 | | |
223 | | static int mss4_decode_dct_block(MSS4Context *c, GetBitContext *gb, |
224 | | uint8_t *dst[3], int mb_x, int mb_y) |
225 | 456k | { |
226 | 456k | int i, j, k, ret; |
227 | 456k | uint8_t *out = dst[0]; |
228 | | |
229 | 1.36M | for (j = 0; j < 2; j++) { |
230 | 2.73M | for (i = 0; i < 2; i++) { |
231 | 1.82M | int xpos = mb_x * 2 + i; |
232 | 1.82M | c->dc_cache[j][TOP_LEFT] = c->dc_cache[j][TOP]; |
233 | 1.82M | c->dc_cache[j][TOP] = c->prev_dc[0][mb_x * 2 + i]; |
234 | 1.82M | ret = mss4_decode_dct(gb, &dc_vlc[0], &ac_vlc[0], c->block, |
235 | 1.82M | c->dc_cache[j], |
236 | 1.82M | xpos, mb_y * 2 + j, c->quant_mat[0]); |
237 | 1.82M | if (ret) |
238 | 483 | return ret; |
239 | 1.82M | c->prev_dc[0][mb_x * 2 + i] = c->dc_cache[j][LEFT]; |
240 | | |
241 | 1.82M | ff_mss34_dct_put(out + xpos * 8, c->pic->linesize[0], |
242 | 1.82M | c->block); |
243 | 1.82M | } |
244 | 912k | out += 8 * c->pic->linesize[0]; |
245 | 912k | } |
246 | | |
247 | 1.36M | for (i = 1; i < 3; i++) { |
248 | 908k | c->dc_cache[i + 1][TOP_LEFT] = c->dc_cache[i + 1][TOP]; |
249 | 908k | c->dc_cache[i + 1][TOP] = c->prev_dc[i][mb_x]; |
250 | 908k | ret = mss4_decode_dct(gb, &dc_vlc[1], &ac_vlc[1], |
251 | 908k | c->block, c->dc_cache[i + 1], mb_x, mb_y, |
252 | 908k | c->quant_mat[1]); |
253 | 908k | if (ret) |
254 | 4.31k | return ret; |
255 | 904k | c->prev_dc[i][mb_x] = c->dc_cache[i + 1][LEFT]; |
256 | | |
257 | 904k | ff_mss34_dct_put(c->imgbuf[i], 8, c->block); |
258 | 904k | out = dst[i] + mb_x * 16; |
259 | | // Since the DCT block is coded as YUV420 and the whole frame as YUV444, |
260 | | // we need to scale chroma. |
261 | 15.3M | for (j = 0; j < 16; j++) { |
262 | 130M | for (k = 0; k < 8; k++) |
263 | 115M | AV_WN16A(out + k * 2, c->imgbuf[i][k + (j & ~1) * 4] * 0x101); |
264 | 14.4M | out += c->pic->linesize[i]; |
265 | 14.4M | } |
266 | 904k | } |
267 | | |
268 | 452k | return 0; |
269 | 456k | } |
270 | | |
271 | | static void read_vec_pos(GetBitContext *gb, int *vec_pos, int *sel_flag, |
272 | | int *sel_len, int *prev) |
273 | 24.9M | { |
274 | 24.9M | int i, y_flag = 0; |
275 | | |
276 | 99.9M | for (i = 2; i >= 0; i--) { |
277 | 74.9M | if (!sel_flag[i]) { |
278 | 24.3M | vec_pos[i] = 0; |
279 | 24.3M | continue; |
280 | 24.3M | } |
281 | 50.6M | if ((!i && !y_flag) || get_bits1(gb)) { |
282 | 25.8M | if (sel_len[i] > 0) { |
283 | 25.6M | int pval = prev[i]; |
284 | 25.6M | vec_pos[i] = get_bits(gb, sel_len[i]); |
285 | 25.6M | if (vec_pos[i] >= pval) |
286 | 14.9M | vec_pos[i]++; |
287 | 25.6M | } else { |
288 | 277k | vec_pos[i] = !prev[i]; |
289 | 277k | } |
290 | 25.8M | y_flag = 1; |
291 | 25.8M | } else { |
292 | 24.7M | vec_pos[i] = prev[i]; |
293 | 24.7M | } |
294 | 50.6M | } |
295 | 24.9M | } |
296 | | |
297 | | static int get_value_cached(GetBitContext *gb, int vec_pos, uint8_t *vec, |
298 | | int vec_size, int component, int shift, int *prev) |
299 | 226M | { |
300 | 226M | if (vec_pos < vec_size) |
301 | 225M | return vec[vec_pos]; |
302 | 1.04M | if (!get_bits1(gb)) |
303 | 611k | return prev[component]; |
304 | 430k | prev[component] = get_bits(gb, 8 - shift) << shift; |
305 | 430k | return prev[component]; |
306 | 1.04M | } |
307 | | |
308 | 103M | #define MKVAL(vals) ((vals)[0] | ((vals)[1] << 3) | ((vals)[2] << 6)) |
309 | | |
310 | | /* Image mode - the hardest to comprehend MSS4 coding mode. |
311 | | * |
312 | | * In this mode all three 16x16 blocks are coded together with a method |
313 | | * remotely similar to the methods employed in MSS1-MSS3. |
314 | | * The idea is that every component has a vector of 1-4 most common symbols |
315 | | * and an escape mode for reading new value from the bitstream. Decoding |
316 | | * consists of retrieving pixel values from the vector or reading new ones |
317 | | * from the bitstream; depending on flags read from the bitstream, these vector |
318 | | * positions can be updated or reused from the state of the previous line |
319 | | * or previous pixel. |
320 | | */ |
321 | | static int mss4_decode_image_block(MSS4Context *ctx, GetBitContext *gb, |
322 | | uint8_t *picdst[3], int mb_x, int mb_y) |
323 | 294k | { |
324 | 294k | uint8_t vec[3][4]; |
325 | 294k | int vec_len[3]; |
326 | 294k | int sel_len[3], sel_flag[3]; |
327 | 294k | int i, j, k, mode, split; |
328 | 294k | int prev_vec1 = 0, prev_split = 0; |
329 | 294k | int vals[3] = { 0 }; |
330 | 294k | int prev_pix[3] = { 0 }; |
331 | 294k | int prev_mode[16] = { 0 }; |
332 | 294k | uint8_t *dst[3]; |
333 | | |
334 | 294k | const int val_shift = ctx->quality == 100 ? 0 : 2; |
335 | | |
336 | 1.17M | for (i = 0; i < 3; i++) |
337 | 883k | dst[i] = ctx->imgbuf[i]; |
338 | | |
339 | 1.17M | for (i = 0; i < 3; i++) { |
340 | 883k | vec_len[i] = vec_len_syms[!!i][get_unary(gb, 0, 3)]; |
341 | 3.54M | for (j = 0; j < vec_len[i]; j++) { |
342 | 2.66M | vec[i][j] = get_coeff(gb, &vec_entry_vlc[!!i], 5, 1); |
343 | 2.66M | vec[i][j] += ctx->prev_vec[i][j]; |
344 | 2.66M | ctx->prev_vec[i][j] = vec[i][j]; |
345 | 2.66M | } |
346 | 883k | sel_flag[i] = vec_len[i] > 1; |
347 | 883k | sel_len[i] = vec_len[i] > 2 ? vec_len[i] - 2 : 0; |
348 | 883k | } |
349 | | |
350 | 5.00M | for (j = 0; j < 16; j++) { |
351 | 4.71M | if (get_bits1(gb)) { |
352 | 3.84M | split = 0; |
353 | 3.84M | if (get_bits1(gb)) { |
354 | 1.76M | prev_mode[0] = 0; |
355 | 1.76M | vals[0] = vals[1] = vals[2] = 0; |
356 | 1.76M | mode = 2; |
357 | 2.08M | } else { |
358 | 2.08M | mode = get_bits1(gb); |
359 | 2.08M | if (mode) |
360 | 370k | split = get_bits(gb, 4); |
361 | 2.08M | } |
362 | 65.4M | for (i = 0; i < 16; i++) { |
363 | 61.5M | if (mode <= 1) { |
364 | 33.3M | vals[0] = prev_mode[i] & 7; |
365 | 33.3M | vals[1] = (prev_mode[i] >> 3) & 7; |
366 | 33.3M | vals[2] = prev_mode[i] >> 6; |
367 | 33.3M | if (mode == 1 && i == split) { |
368 | 370k | read_vec_pos(gb, vals, sel_flag, sel_len, vals); |
369 | 370k | } |
370 | 33.3M | } else if (mode == 2) { |
371 | 28.2M | if (get_bits1(gb)) |
372 | 24.0M | read_vec_pos(gb, vals, sel_flag, sel_len, vals); |
373 | 28.2M | } |
374 | 246M | for (k = 0; k < 3; k++) |
375 | 184M | *dst[k]++ = get_value_cached(gb, vals[k], vec[k], |
376 | 184M | vec_len[k], k, |
377 | 184M | val_shift, prev_pix); |
378 | 61.5M | prev_mode[i] = MKVAL(vals); |
379 | 61.5M | } |
380 | 3.84M | } else { |
381 | 864k | if (get_bits1(gb)) { |
382 | 134k | split = get_bits(gb, 4); |
383 | 134k | if (split >= prev_split) |
384 | 88.8k | split++; |
385 | 134k | prev_split = split; |
386 | 730k | } else { |
387 | 730k | split = prev_split; |
388 | 730k | } |
389 | 864k | if (split) { |
390 | 361k | vals[0] = prev_mode[0] & 7; |
391 | 361k | vals[1] = (prev_mode[0] >> 3) & 7; |
392 | 361k | vals[2] = prev_mode[0] >> 6; |
393 | 1.44M | for (i = 0; i < 3; i++) { |
394 | 10.4M | for (k = 0; k < split; k++) { |
395 | 9.37M | *dst[i]++ = get_value_cached(gb, vals[i], vec[i], |
396 | 9.37M | vec_len[i], i, val_shift, |
397 | 9.37M | prev_pix); |
398 | 9.37M | prev_mode[k] = MKVAL(vals); |
399 | 9.37M | } |
400 | 1.08M | } |
401 | 361k | } |
402 | | |
403 | 864k | if (split != 16) { |
404 | 849k | vals[0] = prev_vec1 & 7; |
405 | 849k | vals[1] = (prev_vec1 >> 3) & 7; |
406 | 849k | vals[2] = prev_vec1 >> 6; |
407 | 849k | if (get_bits1(gb)) { |
408 | 551k | read_vec_pos(gb, vals, sel_flag, sel_len, vals); |
409 | 551k | prev_vec1 = MKVAL(vals); |
410 | 551k | } |
411 | 3.39M | for (i = 0; i < 3; i++) { |
412 | 34.6M | for (k = 0; k < 16 - split; k++) { |
413 | 32.1M | *dst[i]++ = get_value_cached(gb, vals[i], vec[i], |
414 | 32.1M | vec_len[i], i, val_shift, |
415 | 32.1M | prev_pix); |
416 | 32.1M | prev_mode[split + k] = MKVAL(vals); |
417 | 32.1M | } |
418 | 2.54M | } |
419 | 849k | } |
420 | 864k | } |
421 | 4.71M | } |
422 | | |
423 | 1.17M | for (i = 0; i < 3; i++) |
424 | 15.0M | for (j = 0; j < 16; j++) |
425 | 14.1M | memcpy(picdst[i] + mb_x * 16 + j * ctx->pic->linesize[i], |
426 | 14.1M | ctx->imgbuf[i] + j * 16, 16); |
427 | | |
428 | 294k | return 0; |
429 | 294k | } |
430 | | |
431 | | static inline void mss4_update_dc_cache(MSS4Context *c, int mb_x) |
432 | 3.58M | { |
433 | 3.58M | int i; |
434 | | |
435 | 3.58M | c->dc_cache[0][TOP] = c->prev_dc[0][mb_x * 2 + 1]; |
436 | 3.58M | c->dc_cache[0][LEFT] = 0; |
437 | 3.58M | c->dc_cache[1][TOP] = 0; |
438 | 3.58M | c->dc_cache[1][LEFT] = 0; |
439 | | |
440 | 10.7M | for (i = 0; i < 2; i++) |
441 | 7.17M | c->prev_dc[0][mb_x * 2 + i] = 0; |
442 | | |
443 | 10.7M | for (i = 1; i < 3; i++) { |
444 | 7.17M | c->dc_cache[i + 1][TOP] = c->prev_dc[i][mb_x]; |
445 | 7.17M | c->dc_cache[i + 1][LEFT] = 0; |
446 | 7.17M | c->prev_dc[i][mb_x] = 0; |
447 | 7.17M | } |
448 | 3.58M | } |
449 | | |
450 | | static int mss4_decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
451 | | int *got_frame, AVPacket *avpkt) |
452 | 46.6k | { |
453 | 46.6k | const uint8_t *buf = avpkt->data; |
454 | 46.6k | int buf_size = avpkt->size; |
455 | 46.6k | MSS4Context *c = avctx->priv_data; |
456 | 46.6k | GetBitContext gb; |
457 | 46.6k | GetByteContext bc; |
458 | 46.6k | uint8_t *dst[3]; |
459 | 46.6k | int width, height, quality, frame_type; |
460 | 46.6k | int x, y, i, mb_width, mb_height, blk_type; |
461 | 46.6k | int ret; |
462 | | |
463 | 46.6k | if (buf_size < HEADER_SIZE) { |
464 | 16.1k | av_log(avctx, AV_LOG_ERROR, |
465 | 16.1k | "Frame should have at least %d bytes, got %d instead\n", |
466 | 16.1k | HEADER_SIZE, buf_size); |
467 | 16.1k | return AVERROR_INVALIDDATA; |
468 | 16.1k | } |
469 | | |
470 | 30.5k | bytestream2_init(&bc, buf, buf_size); |
471 | 30.5k | width = bytestream2_get_be16(&bc); |
472 | 30.5k | height = bytestream2_get_be16(&bc); |
473 | 30.5k | bytestream2_skip(&bc, 2); |
474 | 30.5k | quality = bytestream2_get_byte(&bc); |
475 | 30.5k | frame_type = bytestream2_get_byte(&bc); |
476 | | |
477 | 30.5k | if (width > avctx->width || |
478 | 28.2k | height != avctx->height) { |
479 | 4.16k | av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\n", |
480 | 4.16k | width, height); |
481 | 4.16k | return AVERROR_INVALIDDATA; |
482 | 4.16k | } |
483 | 26.3k | if (av_image_check_size2(width, height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0) |
484 | 371 | return AVERROR_INVALIDDATA; |
485 | | |
486 | 25.9k | if (quality < 1 || quality > 100) { |
487 | 581 | av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality); |
488 | 581 | return AVERROR_INVALIDDATA; |
489 | 581 | } |
490 | 25.4k | if ((frame_type & ~3) || frame_type == 3) { |
491 | 605 | av_log(avctx, AV_LOG_ERROR, "Invalid frame type %d\n", frame_type); |
492 | 605 | return AVERROR_INVALIDDATA; |
493 | 605 | } |
494 | | |
495 | 24.7k | if (frame_type != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) { |
496 | 222 | av_log(avctx, AV_LOG_ERROR, |
497 | 222 | "Empty frame found but it is not a skip frame.\n"); |
498 | 222 | return AVERROR_INVALIDDATA; |
499 | 222 | } |
500 | 24.5k | mb_width = FFALIGN(width, 16) >> 4; |
501 | 24.5k | mb_height = FFALIGN(height, 16) >> 4; |
502 | | |
503 | 24.5k | if (frame_type != SKIP_FRAME && 8*buf_size < 8*HEADER_SIZE + mb_width*mb_height) |
504 | 515 | return AVERROR_INVALIDDATA; |
505 | | |
506 | 24.0k | if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0) |
507 | 16 | return ret; |
508 | 24.0k | if (frame_type == INTRA_FRAME) |
509 | 892 | c->pic->flags |= AV_FRAME_FLAG_KEY; |
510 | 23.1k | else |
511 | 23.1k | c->pic->flags &= ~AV_FRAME_FLAG_KEY; |
512 | 24.0k | c->pic->pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I |
513 | 24.0k | : AV_PICTURE_TYPE_P; |
514 | 24.0k | if (frame_type == SKIP_FRAME) { |
515 | 225 | *got_frame = 1; |
516 | 225 | if ((ret = av_frame_ref(rframe, c->pic)) < 0) |
517 | 0 | return ret; |
518 | | |
519 | 225 | return buf_size; |
520 | 225 | } |
521 | | |
522 | 23.8k | if (c->quality != quality) { |
523 | 2.59k | c->quality = quality; |
524 | 7.79k | for (i = 0; i < 2; i++) |
525 | 5.19k | ff_mss34_gen_quant_mat(c->quant_mat[i], quality, !i); |
526 | 2.59k | } |
527 | | |
528 | 23.8k | if ((ret = init_get_bits8(&gb, buf + HEADER_SIZE, buf_size - HEADER_SIZE)) < 0) |
529 | 0 | return ret; |
530 | 23.8k | dst[0] = c->pic->data[0]; |
531 | 23.8k | dst[1] = c->pic->data[1]; |
532 | 23.8k | dst[2] = c->pic->data[2]; |
533 | | |
534 | 23.8k | memset(c->prev_vec, 0, sizeof(c->prev_vec)); |
535 | 296k | for (y = 0; y < mb_height; y++) { |
536 | 278k | memset(c->dc_cache, 0, sizeof(c->dc_cache)); |
537 | 4.31M | for (x = 0; x < mb_width; x++) { |
538 | 4.04M | blk_type = decode012(&gb); |
539 | 4.04M | switch (blk_type) { |
540 | 456k | case DCT_BLOCK: |
541 | 456k | if (mss4_decode_dct_block(c, &gb, dst, x, y) < 0) { |
542 | 4.79k | av_log(avctx, AV_LOG_ERROR, |
543 | 4.79k | "Error decoding DCT block %d,%d\n", |
544 | 4.79k | x, y); |
545 | 4.79k | return AVERROR_INVALIDDATA; |
546 | 4.79k | } |
547 | 452k | break; |
548 | 452k | case IMAGE_BLOCK: |
549 | 294k | if (mss4_decode_image_block(c, &gb, dst, x, y) < 0) { |
550 | 0 | av_log(avctx, AV_LOG_ERROR, |
551 | 0 | "Error decoding VQ block %d,%d\n", |
552 | 0 | x, y); |
553 | 0 | return AVERROR_INVALIDDATA; |
554 | 0 | } |
555 | 294k | break; |
556 | 3.29M | case SKIP_BLOCK: |
557 | 3.29M | if (frame_type == INTRA_FRAME) { |
558 | 886 | av_log(avctx, AV_LOG_ERROR, "Skip block in intra frame\n"); |
559 | 886 | return AVERROR_INVALIDDATA; |
560 | 886 | } |
561 | 3.29M | break; |
562 | 4.04M | } |
563 | 4.04M | if (blk_type != DCT_BLOCK) |
564 | 3.58M | mss4_update_dc_cache(c, x); |
565 | 4.04M | } |
566 | 272k | dst[0] += c->pic->linesize[0] * 16; |
567 | 272k | dst[1] += c->pic->linesize[1] * 16; |
568 | 272k | dst[2] += c->pic->linesize[2] * 16; |
569 | 272k | } |
570 | | |
571 | 18.1k | if ((ret = av_frame_ref(rframe, c->pic)) < 0) |
572 | 0 | return ret; |
573 | | |
574 | 18.1k | *got_frame = 1; |
575 | | |
576 | 18.1k | return buf_size; |
577 | 18.1k | } |
578 | | |
579 | | static av_cold int mss4_decode_end(AVCodecContext *avctx) |
580 | 1.23k | { |
581 | 1.23k | MSS4Context * const c = avctx->priv_data; |
582 | 1.23k | int i; |
583 | | |
584 | 1.23k | av_frame_free(&c->pic); |
585 | 4.92k | for (i = 0; i < 3; i++) |
586 | 3.69k | av_freep(&c->prev_dc[i]); |
587 | | |
588 | 1.23k | return 0; |
589 | 1.23k | } |
590 | | |
591 | | static av_cold int mss4_decode_init(AVCodecContext *avctx) |
592 | 1.23k | { |
593 | 1.23k | static AVOnce init_static_once = AV_ONCE_INIT; |
594 | 1.23k | MSS4Context * const c = avctx->priv_data; |
595 | 1.23k | int i; |
596 | | |
597 | 4.92k | for (i = 0; i < 3; i++) { |
598 | 3.69k | c->dc_stride[i] = FFALIGN(avctx->width, 16) >> (2 + !!i); |
599 | 3.69k | c->prev_dc[i] = av_malloc_array(c->dc_stride[i], sizeof(**c->prev_dc)); |
600 | 3.69k | if (!c->prev_dc[i]) { |
601 | 0 | av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n"); |
602 | 0 | return AVERROR(ENOMEM); |
603 | 0 | } |
604 | 3.69k | } |
605 | | |
606 | 1.23k | c->pic = av_frame_alloc(); |
607 | 1.23k | if (!c->pic) |
608 | 0 | return AVERROR(ENOMEM); |
609 | | |
610 | 1.23k | avctx->pix_fmt = AV_PIX_FMT_YUV444P; |
611 | | |
612 | 1.23k | ff_thread_once(&init_static_once, mss4_init_vlcs); |
613 | | |
614 | 1.23k | return 0; |
615 | 1.23k | } |
616 | | |
617 | | const FFCodec ff_mts2_decoder = { |
618 | | .p.name = "mts2", |
619 | | CODEC_LONG_NAME("MS Expression Encoder Screen"), |
620 | | .p.type = AVMEDIA_TYPE_VIDEO, |
621 | | .p.id = AV_CODEC_ID_MTS2, |
622 | | .priv_data_size = sizeof(MSS4Context), |
623 | | .init = mss4_decode_init, |
624 | | .close = mss4_decode_end, |
625 | | FF_CODEC_DECODE_CB(mss4_decode_frame), |
626 | | .p.capabilities = AV_CODEC_CAP_DR1, |
627 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
628 | | }; |