/src/ffmpeg/libavcodec/svq3.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2003 The FFmpeg Project |
3 | | * |
4 | | * This file is part of FFmpeg. |
5 | | * |
6 | | * FFmpeg is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * FFmpeg is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with FFmpeg; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | /* |
22 | | * How to use this decoder: |
23 | | * SVQ3 data is transported within Apple Quicktime files. Quicktime files |
24 | | * have stsd atoms to describe media trak properties. A stsd atom for a |
25 | | * video trak contains 1 or more ImageDescription atoms. These atoms begin |
26 | | * with the 4-byte length of the atom followed by the codec fourcc. Some |
27 | | * decoders need information in this atom to operate correctly. Such |
28 | | * is the case with SVQ3. In order to get the best use out of this decoder, |
29 | | * the calling app must make the SVQ3 ImageDescription atom available |
30 | | * via the AVCodecContext's extradata[_size] field: |
31 | | * |
32 | | * AVCodecContext.extradata = pointer to ImageDescription, first characters |
33 | | * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length |
34 | | * AVCodecContext.extradata_size = size of ImageDescription atom memory |
35 | | * buffer (which will be the same as the ImageDescription atom size field |
36 | | * from the QT file, minus 4 bytes since the length is missing) |
37 | | * |
38 | | * You will know you have these parameters passed correctly when the decoder |
39 | | * correctly decodes this file: |
40 | | * http://samples.mplayerhq.hu/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov |
41 | | */ |
42 | | |
43 | | #include <inttypes.h> |
44 | | |
45 | | #include "libavutil/attributes.h" |
46 | | #include "libavutil/crc.h" |
47 | | #include "libavutil/mem.h" |
48 | | #include "libavutil/mem_internal.h" |
49 | | |
50 | | #include "codec_internal.h" |
51 | | #include "decode.h" |
52 | | #include "avcodec.h" |
53 | | #include "mpegutils.h" |
54 | | #include "h264data.h" |
55 | | #include "h264dsp.h" |
56 | | #include "h264pred.h" |
57 | | #include "h264_parse.h" |
58 | | #include "golomb.h" |
59 | | #include "hpeldsp.h" |
60 | | #include "mathops.h" |
61 | | #include "rectangle.h" |
62 | | #include "tpeldsp.h" |
63 | | #include "videodsp.h" |
64 | | |
65 | | #if CONFIG_ZLIB |
66 | | #include <zlib.h> |
67 | | #endif |
68 | | |
69 | | /** |
70 | | * @file |
71 | | * svq3 decoder. |
72 | | */ |
73 | | |
74 | 23.5k | #define NUM_PICS 3 |
75 | | |
76 | | typedef struct SVQ3Frame { |
77 | | AVFrame *f; |
78 | | |
79 | | int16_t (*motion_val[2])[2]; |
80 | | |
81 | | uint32_t *mb_type; |
82 | | } SVQ3Frame; |
83 | | |
84 | | typedef struct SVQ3Context { |
85 | | AVCodecContext *avctx; |
86 | | |
87 | | H264DSPContext h264dsp; |
88 | | H264PredContext hpc; |
89 | | HpelDSPContext hdsp; |
90 | | TpelDSPContext tdsp; |
91 | | VideoDSPContext vdsp; |
92 | | |
93 | | SVQ3Frame *cur_pic; |
94 | | SVQ3Frame *next_pic; |
95 | | SVQ3Frame *last_pic; |
96 | | GetBitContext gb; |
97 | | GetBitContext gb_slice; |
98 | | uint8_t *slice_buf; |
99 | | unsigned slice_buf_size; |
100 | | int halfpel_flag; |
101 | | int thirdpel_flag; |
102 | | int has_watermark; |
103 | | uint32_t watermark_key; |
104 | | int adaptive_quant; |
105 | | int h_edge_pos; |
106 | | int v_edge_pos; |
107 | | int slice_num; |
108 | | int qscale; |
109 | | int cbp; |
110 | | int frame_num; |
111 | | int frame_num_offset; |
112 | | int prev_frame_num_offset; |
113 | | int prev_frame_num; |
114 | | |
115 | | enum AVPictureType pict_type; |
116 | | enum AVPictureType slice_type; |
117 | | int low_delay; |
118 | | |
119 | | int mb_x, mb_y; |
120 | | int mb_xy; |
121 | | int mb_width, mb_height; |
122 | | int mb_stride, mb_num; |
123 | | int b_stride; |
124 | | |
125 | | uint32_t *mb2br_xy; |
126 | | |
127 | | int chroma_pred_mode; |
128 | | int intra16x16_pred_mode; |
129 | | |
130 | | int8_t intra4x4_pred_mode_cache[5 * 8]; |
131 | | int8_t (*intra4x4_pred_mode); |
132 | | |
133 | | unsigned int top_samples_available; |
134 | | unsigned int left_samples_available; |
135 | | |
136 | | uint8_t *edge_emu_buffer; |
137 | | |
138 | | DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2]; |
139 | | DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8]; |
140 | | DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2]; |
141 | | DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2]; |
142 | | DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8]; |
143 | | uint32_t dequant4_coeff[QP_MAX_NUM + 1][16]; |
144 | | int block_offset[2 * (16 * 3)]; |
145 | | SVQ3Frame frames[NUM_PICS]; |
146 | | |
147 | | uint32_t *mb_type_buf; |
148 | | int16_t (*motion_val_buf)[2]; |
149 | | } SVQ3Context; |
150 | | |
151 | 218k | #define FULLPEL_MODE 1 |
152 | 1.28M | #define HALFPEL_MODE 2 |
153 | 735k | #define THIRDPEL_MODE 3 |
154 | 3.15M | #define PREDICT_MODE 4 |
155 | | |
156 | | /* dual scan (from some older H.264 draft) |
157 | | * o-->o-->o o |
158 | | * | /| |
159 | | * o o o / o |
160 | | * | / | |/ | |
161 | | * o o o o |
162 | | * / |
163 | | * o-->o-->o-->o |
164 | | */ |
165 | | static const uint8_t svq3_scan[16] = { |
166 | | 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4, |
167 | | 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, |
168 | | 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4, |
169 | | 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4, |
170 | | }; |
171 | | |
172 | | static const uint8_t luma_dc_zigzag_scan[16] = { |
173 | | 0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64, |
174 | | 3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64, |
175 | | 1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64, |
176 | | 3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64, |
177 | | }; |
178 | | |
179 | | static const uint8_t svq3_pred_0[25][2] = { |
180 | | { 0, 0 }, |
181 | | { 1, 0 }, { 0, 1 }, |
182 | | { 0, 2 }, { 1, 1 }, { 2, 0 }, |
183 | | { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 }, |
184 | | { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 }, |
185 | | { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 }, |
186 | | { 2, 4 }, { 3, 3 }, { 4, 2 }, |
187 | | { 4, 3 }, { 3, 4 }, |
188 | | { 4, 4 } |
189 | | }; |
190 | | |
191 | | static const int8_t svq3_pred_1[6][6][5] = { |
192 | | { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, |
193 | | { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } }, |
194 | | { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 }, |
195 | | { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } }, |
196 | | { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 }, |
197 | | { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } }, |
198 | | { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 }, |
199 | | { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } }, |
200 | | { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 }, |
201 | | { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } }, |
202 | | { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 }, |
203 | | { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } }, |
204 | | }; |
205 | | |
206 | | static const struct { |
207 | | uint8_t run; |
208 | | uint8_t level; |
209 | | } svq3_dct_tables[2][16] = { |
210 | | { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 }, |
211 | | { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } }, |
212 | | { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, |
213 | | { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } } |
214 | | }; |
215 | | |
216 | | static const uint32_t svq3_dequant_coeff[32] = { |
217 | | 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718, |
218 | | 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873, |
219 | | 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683, |
220 | | 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533 |
221 | | }; |
222 | | |
223 | | static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp) |
224 | 27.0k | { |
225 | 27.0k | const unsigned qmul = svq3_dequant_coeff[qp]; |
226 | 513k | #define stride 16 |
227 | 27.0k | int i; |
228 | 27.0k | int temp[16]; |
229 | 27.0k | static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride }; |
230 | | |
231 | 135k | for (i = 0; i < 4; i++) { |
232 | 108k | const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]); |
233 | 108k | const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]); |
234 | 108k | const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3]; |
235 | 108k | const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3]; |
236 | | |
237 | 108k | temp[4 * i + 0] = z0 + z3; |
238 | 108k | temp[4 * i + 1] = z1 + z2; |
239 | 108k | temp[4 * i + 2] = z1 - z2; |
240 | 108k | temp[4 * i + 3] = z0 - z3; |
241 | 108k | } |
242 | | |
243 | 135k | for (i = 0; i < 4; i++) { |
244 | 108k | const int offset = x_offset[i]; |
245 | 108k | const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]); |
246 | 108k | const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]); |
247 | 108k | const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i]; |
248 | 108k | const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i]; |
249 | | |
250 | 108k | output[stride * 0 + offset] = (int)((z0 + z3) * qmul + 0x80000) >> 20; |
251 | 108k | output[stride * 2 + offset] = (int)((z1 + z2) * qmul + 0x80000) >> 20; |
252 | 108k | output[stride * 8 + offset] = (int)((z1 - z2) * qmul + 0x80000) >> 20; |
253 | 108k | output[stride * 10 + offset] = (int)((z0 - z3) * qmul + 0x80000) >> 20; |
254 | 108k | } |
255 | 27.0k | } |
256 | | #undef stride |
257 | | |
258 | | static void svq3_add_idct_c(uint8_t *dst, int16_t *block, |
259 | | int stride, int qp, int dc) |
260 | 774k | { |
261 | 774k | const int qmul = svq3_dequant_coeff[qp]; |
262 | 774k | int i; |
263 | | |
264 | 774k | if (dc) { |
265 | 449k | dc = 13 * 13 * (dc == 1 ? 1538U* block[0] |
266 | 449k | : qmul * (block[0] >> 3) / 2); |
267 | 449k | block[0] = 0; |
268 | 449k | } |
269 | | |
270 | 3.87M | for (i = 0; i < 4; i++) { |
271 | 3.09M | const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]); |
272 | 3.09M | const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]); |
273 | 3.09M | const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i]; |
274 | 3.09M | const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i]; |
275 | | |
276 | 3.09M | block[0 + 4 * i] = z0 + z3; |
277 | 3.09M | block[1 + 4 * i] = z1 + z2; |
278 | 3.09M | block[2 + 4 * i] = z1 - z2; |
279 | 3.09M | block[3 + 4 * i] = z0 - z3; |
280 | 3.09M | } |
281 | | |
282 | 3.87M | for (i = 0; i < 4; i++) { |
283 | 3.09M | const unsigned z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]); |
284 | 3.09M | const unsigned z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]); |
285 | 3.09M | const unsigned z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3]; |
286 | 3.09M | const unsigned z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3]; |
287 | 3.09M | const int rr = (dc + 0x80000u); |
288 | | |
289 | 3.09M | dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((int)((z0 + z3) * qmul + rr) >> 20)); |
290 | 3.09M | dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((int)((z1 + z2) * qmul + rr) >> 20)); |
291 | 3.09M | dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((int)((z1 - z2) * qmul + rr) >> 20)); |
292 | 3.09M | dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((int)((z0 - z3) * qmul + rr) >> 20)); |
293 | 3.09M | } |
294 | | |
295 | 774k | memset(block, 0, 16 * sizeof(int16_t)); |
296 | 774k | } |
297 | | |
298 | | static inline int svq3_decode_block(GetBitContext *gb, int16_t *block, |
299 | | int index, const int type) |
300 | 506k | { |
301 | 506k | static const uint8_t *const scan_patterns[4] = { |
302 | 506k | luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan |
303 | 506k | }; |
304 | | |
305 | 506k | int run, level, sign, limit; |
306 | 506k | unsigned vlc; |
307 | 506k | const int intra = 3 * type >> 2; |
308 | 506k | const uint8_t *const scan = scan_patterns[type]; |
309 | | |
310 | 584k | for (limit = (16 >> intra); index < 16; index = limit, limit += 8) { |
311 | 810k | for (; (vlc = get_interleaved_ue_golomb(gb)) != 0; index++) { |
312 | 268k | if ((int32_t)vlc < 0) |
313 | 0 | return -1; |
314 | | |
315 | 268k | sign = (vlc & 1) ? 0 : -1; |
316 | 268k | vlc = vlc + 1 >> 1; |
317 | | |
318 | 268k | if (type == 3) { |
319 | 16.8k | if (vlc < 3) { |
320 | 10.0k | run = 0; |
321 | 10.0k | level = vlc; |
322 | 10.0k | } else if (vlc < 4) { |
323 | 638 | run = 1; |
324 | 638 | level = 1; |
325 | 6.11k | } else { |
326 | 6.11k | run = vlc & 0x3; |
327 | 6.11k | level = (vlc + 9 >> 2) - run; |
328 | 6.11k | } |
329 | 251k | } else { |
330 | 251k | if (vlc < 16U) { |
331 | 225k | run = svq3_dct_tables[intra][vlc].run; |
332 | 225k | level = svq3_dct_tables[intra][vlc].level; |
333 | 225k | } else if (intra) { |
334 | 1.80k | run = vlc & 0x7; |
335 | 1.80k | level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1))); |
336 | 24.5k | } else { |
337 | 24.5k | run = vlc & 0xF; |
338 | 24.5k | level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0))); |
339 | 24.5k | } |
340 | 251k | } |
341 | | |
342 | | |
343 | 268k | if ((index += run) >= limit) |
344 | 4.00k | return -1; |
345 | | |
346 | 264k | block[scan[index]] = (level ^ sign) - sign; |
347 | 264k | } |
348 | | |
349 | 541k | if (type != 2) { |
350 | 463k | break; |
351 | 463k | } |
352 | 541k | } |
353 | | |
354 | 502k | return 0; |
355 | 506k | } |
356 | | |
357 | | static av_always_inline int |
358 | | svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C, |
359 | | int i, int list, int part_width) |
360 | 555k | { |
361 | 555k | const int topright_ref = s->ref_cache[list][i - 8 + part_width]; |
362 | | |
363 | 555k | if (topright_ref != PART_NOT_AVAILABLE) { |
364 | 304k | *C = s->mv_cache[list][i - 8 + part_width]; |
365 | 304k | return topright_ref; |
366 | 304k | } else { |
367 | 250k | *C = s->mv_cache[list][i - 8 - 1]; |
368 | 250k | return s->ref_cache[list][i - 8 - 1]; |
369 | 250k | } |
370 | 555k | } |
371 | | |
372 | | /** |
373 | | * Get the predicted MV. |
374 | | * @param n the block index |
375 | | * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4) |
376 | | * @param mx the x component of the predicted motion vector |
377 | | * @param my the y component of the predicted motion vector |
378 | | */ |
379 | | static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n, |
380 | | int part_width, int list, |
381 | | int ref, int *const mx, int *const my) |
382 | 555k | { |
383 | 555k | const int index8 = scan8[n]; |
384 | 555k | const int top_ref = s->ref_cache[list][index8 - 8]; |
385 | 555k | const int left_ref = s->ref_cache[list][index8 - 1]; |
386 | 555k | const int16_t *const A = s->mv_cache[list][index8 - 1]; |
387 | 555k | const int16_t *const B = s->mv_cache[list][index8 - 8]; |
388 | 555k | const int16_t *C; |
389 | 555k | int diagonal_ref, match_count; |
390 | | |
391 | | /* mv_cache |
392 | | * B . . A T T T T |
393 | | * U . . L . . , . |
394 | | * U . . L . . . . |
395 | | * U . . L . . , . |
396 | | * . . . L . . . . |
397 | | */ |
398 | | |
399 | 555k | diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width); |
400 | 555k | match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref); |
401 | 555k | if (match_count > 1) { //most common |
402 | 358k | *mx = mid_pred(A[0], B[0], C[0]); |
403 | 358k | *my = mid_pred(A[1], B[1], C[1]); |
404 | 358k | } else if (match_count == 1) { |
405 | 196k | if (left_ref == ref) { |
406 | 196k | *mx = A[0]; |
407 | 196k | *my = A[1]; |
408 | 196k | } else if (top_ref == ref) { |
409 | 0 | *mx = B[0]; |
410 | 0 | *my = B[1]; |
411 | 0 | } else { |
412 | 0 | *mx = C[0]; |
413 | 0 | *my = C[1]; |
414 | 0 | } |
415 | 196k | } else { |
416 | 0 | if (top_ref == PART_NOT_AVAILABLE && |
417 | 0 | diagonal_ref == PART_NOT_AVAILABLE && |
418 | 0 | left_ref != PART_NOT_AVAILABLE) { |
419 | 0 | *mx = A[0]; |
420 | 0 | *my = A[1]; |
421 | 0 | } else { |
422 | 0 | *mx = mid_pred(A[0], B[0], C[0]); |
423 | 0 | *my = mid_pred(A[1], B[1], C[1]); |
424 | 0 | } |
425 | 0 | } |
426 | 555k | } |
427 | | |
428 | | static inline void svq3_mc_dir_part(SVQ3Context *s, |
429 | | int x, int y, int width, int height, |
430 | | int mx, int my, int dxy, |
431 | | int thirdpel, int dir, int avg) |
432 | 2.11M | { |
433 | 2.11M | const SVQ3Frame *pic = (dir == 0) ? s->last_pic : s->next_pic; |
434 | 2.11M | uint8_t *src, *dest; |
435 | 2.11M | int i, emu = 0; |
436 | 2.11M | int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2 |
437 | 2.11M | int linesize = s->cur_pic->f->linesize[0]; |
438 | 2.11M | int uvlinesize = s->cur_pic->f->linesize[1]; |
439 | | |
440 | 2.11M | mx += x; |
441 | 2.11M | my += y; |
442 | | |
443 | 2.11M | if (mx < 0 || mx >= s->h_edge_pos - width - 1 || |
444 | 1.69M | my < 0 || my >= s->v_edge_pos - height - 1) { |
445 | 899k | emu = 1; |
446 | 899k | mx = av_clip(mx, -16, s->h_edge_pos - width + 15); |
447 | 899k | my = av_clip(my, -16, s->v_edge_pos - height + 15); |
448 | 899k | } |
449 | | |
450 | | /* form component predictions */ |
451 | 2.11M | dest = s->cur_pic->f->data[0] + x + y * linesize; |
452 | 2.11M | src = pic->f->data[0] + mx + my * linesize; |
453 | | |
454 | 2.11M | if (emu) { |
455 | 899k | s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, |
456 | 899k | linesize, linesize, |
457 | 899k | width + 1, height + 1, |
458 | 899k | mx, my, s->h_edge_pos, s->v_edge_pos); |
459 | 899k | src = s->edge_emu_buffer; |
460 | 899k | } |
461 | 2.11M | if (thirdpel) |
462 | 66.7k | (avg ? s->tdsp.avg_tpel_pixels_tab |
463 | 66.7k | : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, linesize, |
464 | 66.7k | width, height); |
465 | 2.05M | else |
466 | 2.05M | (avg ? s->hdsp.avg_pixels_tab |
467 | 2.05M | : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, linesize, |
468 | 2.05M | height); |
469 | | |
470 | 2.11M | if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) { |
471 | 2.11M | mx = mx + (mx < (int) x) >> 1; |
472 | 2.11M | my = my + (my < (int) y) >> 1; |
473 | 2.11M | width = width >> 1; |
474 | 2.11M | height = height >> 1; |
475 | 2.11M | blocksize++; |
476 | | |
477 | 6.35M | for (i = 1; i < 3; i++) { |
478 | 4.23M | dest = s->cur_pic->f->data[i] + (x >> 1) + (y >> 1) * uvlinesize; |
479 | 4.23M | src = pic->f->data[i] + mx + my * uvlinesize; |
480 | | |
481 | 4.23M | if (emu) { |
482 | 1.79M | s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, |
483 | 1.79M | uvlinesize, uvlinesize, |
484 | 1.79M | width + 1, height + 1, |
485 | 1.79M | mx, my, (s->h_edge_pos >> 1), |
486 | 1.79M | s->v_edge_pos >> 1); |
487 | 1.79M | src = s->edge_emu_buffer; |
488 | 1.79M | } |
489 | 4.23M | if (thirdpel) |
490 | 133k | (avg ? s->tdsp.avg_tpel_pixels_tab |
491 | 133k | : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, |
492 | 133k | uvlinesize, |
493 | 133k | width, height); |
494 | 4.10M | else |
495 | 4.10M | (avg ? s->hdsp.avg_pixels_tab |
496 | 4.10M | : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, |
497 | 4.10M | uvlinesize, |
498 | 4.10M | height); |
499 | 4.23M | } |
500 | 2.11M | } |
501 | 2.11M | } |
502 | | |
503 | | static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode, |
504 | | int dir, int avg) |
505 | 365k | { |
506 | 365k | int i, j, k, mx, my, dx, dy, x, y; |
507 | | // 0->16x16,1->8x16,2->16x8,3->8x8,4->4x8,5->8x4,6->4x4 |
508 | 365k | const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1); |
509 | 365k | const int part_height = 16 >> ((unsigned)(size + 1) / 3); |
510 | 365k | const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0; |
511 | 365k | const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width; |
512 | 365k | const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width; |
513 | | |
514 | 758k | for (i = 0; i < 16; i += part_height) |
515 | 1.09M | for (j = 0; j < 16; j += part_width) { |
516 | 702k | const int b_xy = (4 * s->mb_x + (j >> 2)) + |
517 | 702k | (4 * s->mb_y + (i >> 2)) * s->b_stride; |
518 | 702k | int dxy; |
519 | 702k | x = 16 * s->mb_x + j; |
520 | 702k | y = 16 * s->mb_y + i; |
521 | 702k | k = (j >> 2 & 1) + (i >> 1 & 2) + |
522 | 702k | (j >> 1 & 4) + (i & 8); |
523 | | |
524 | 702k | if (mode != PREDICT_MODE) { |
525 | 555k | svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my); |
526 | 555k | } else { |
527 | 146k | mx = s->next_pic->motion_val[0][b_xy][0] * 2; |
528 | 146k | my = s->next_pic->motion_val[0][b_xy][1] * 2; |
529 | | |
530 | 146k | if (dir == 0) { |
531 | 73.4k | mx = mx * s->frame_num_offset / |
532 | 73.4k | s->prev_frame_num_offset + 1 >> 1; |
533 | 73.4k | my = my * s->frame_num_offset / |
534 | 73.4k | s->prev_frame_num_offset + 1 >> 1; |
535 | 73.4k | } else { |
536 | 73.4k | mx = mx * (s->frame_num_offset - s->prev_frame_num_offset) / |
537 | 73.4k | s->prev_frame_num_offset + 1 >> 1; |
538 | 73.4k | my = my * (s->frame_num_offset - s->prev_frame_num_offset) / |
539 | 73.4k | s->prev_frame_num_offset + 1 >> 1; |
540 | 73.4k | } |
541 | 146k | } |
542 | | |
543 | | /* clip motion vector prediction to frame border */ |
544 | 702k | mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x); |
545 | 702k | my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y); |
546 | | |
547 | | /* get (optional) motion vector differential */ |
548 | 702k | if (mode == PREDICT_MODE) { |
549 | 146k | dx = dy = 0; |
550 | 555k | } else { |
551 | 555k | dy = get_interleaved_se_golomb(&s->gb_slice); |
552 | 555k | dx = get_interleaved_se_golomb(&s->gb_slice); |
553 | | |
554 | 555k | if (dx != (int16_t)dx || dy != (int16_t)dy) { |
555 | 1.60k | av_log(s->avctx, AV_LOG_ERROR, "invalid MV vlc\n"); |
556 | 1.60k | return -1; |
557 | 1.60k | } |
558 | 555k | } |
559 | | |
560 | | /* compute motion vector */ |
561 | 700k | if (mode == THIRDPEL_MODE) { |
562 | 66.7k | int fx, fy; |
563 | 66.7k | mx = (mx + 1 >> 1) + dx; |
564 | 66.7k | my = (my + 1 >> 1) + dy; |
565 | 66.7k | fx = (unsigned)(mx + 0x30000) / 3 - 0x10000; |
566 | 66.7k | fy = (unsigned)(my + 0x30000) / 3 - 0x10000; |
567 | 66.7k | dxy = (mx - 3 * fx) + 4 * (my - 3 * fy); |
568 | | |
569 | 66.7k | svq3_mc_dir_part(s, x, y, part_width, part_height, |
570 | 66.7k | fx, fy, dxy, 1, dir, avg); |
571 | 66.7k | mx += mx; |
572 | 66.7k | my += my; |
573 | 633k | } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) { |
574 | 179k | mx = (unsigned)(mx + 1 + 0x30000) / 3 + dx - 0x10000; |
575 | 179k | my = (unsigned)(my + 1 + 0x30000) / 3 + dy - 0x10000; |
576 | 179k | dxy = (mx & 1) + 2 * (my & 1); |
577 | | |
578 | 179k | svq3_mc_dir_part(s, x, y, part_width, part_height, |
579 | 179k | mx >> 1, my >> 1, dxy, 0, dir, avg); |
580 | 179k | mx *= 3; |
581 | 179k | my *= 3; |
582 | 454k | } else { |
583 | 454k | mx = (unsigned)(mx + 3 + 0x60000) / 6 + dx - 0x10000; |
584 | 454k | my = (unsigned)(my + 3 + 0x60000) / 6 + dy - 0x10000; |
585 | | |
586 | 454k | svq3_mc_dir_part(s, x, y, part_width, part_height, |
587 | 454k | mx, my, 0, 0, dir, avg); |
588 | 454k | mx *= 6; |
589 | 454k | my *= 6; |
590 | 454k | } |
591 | | |
592 | | /* update mv_cache */ |
593 | 700k | if (mode != PREDICT_MODE) { |
594 | 553k | int32_t mv = pack16to32(mx, my); |
595 | | |
596 | 553k | if (part_height == 8 && i < 8) { |
597 | 18.4k | AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv); |
598 | | |
599 | 18.4k | if (part_width == 8 && j < 8) |
600 | 18.4k | AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv); |
601 | 18.4k | } |
602 | 553k | if (part_width == 8 && j < 8) |
603 | 553k | AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv); |
604 | 553k | if (part_width == 4 || part_height == 4) |
605 | 553k | AV_WN32A(s->mv_cache[dir][scan8[k]], mv); |
606 | 553k | } |
607 | | |
608 | | /* write back motion vectors */ |
609 | 700k | fill_rectangle(s->cur_pic->motion_val[dir][b_xy], |
610 | 700k | part_width >> 2, part_height >> 2, s->b_stride, |
611 | 700k | pack16to32(mx, my), 4); |
612 | 700k | } |
613 | | |
614 | 364k | return 0; |
615 | 365k | } |
616 | | |
617 | | static av_always_inline void hl_decode_mb_idct_luma(SVQ3Context *s, |
618 | | int mb_type, const int *block_offset, |
619 | | int linesize, uint8_t *dest_y) |
620 | 321k | { |
621 | 321k | int i; |
622 | 321k | if (!IS_INTRA4x4(mb_type)) { |
623 | 5.25M | for (i = 0; i < 16; i++) |
624 | 4.94M | if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) { |
625 | 464k | uint8_t *const ptr = dest_y + block_offset[i]; |
626 | 464k | svq3_add_idct_c(ptr, s->mb + i * 16, linesize, |
627 | 464k | s->qscale, IS_INTRA(mb_type) ? 1 : 0); |
628 | 464k | } |
629 | 308k | } |
630 | 321k | } |
631 | | |
632 | | static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s, |
633 | | int mb_type, |
634 | | const int *block_offset, |
635 | | int linesize, |
636 | | uint8_t *dest_y) |
637 | 39.5k | { |
638 | 39.5k | int i; |
639 | 39.5k | int qscale = s->qscale; |
640 | | |
641 | 39.5k | if (IS_INTRA4x4(mb_type)) { |
642 | 213k | for (i = 0; i < 16; i++) { |
643 | 200k | uint8_t *const ptr = dest_y + block_offset[i]; |
644 | 200k | const int dir = s->intra4x4_pred_mode_cache[scan8[i]]; |
645 | | |
646 | 200k | uint8_t *topright; |
647 | 200k | int nnz; |
648 | 200k | if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) { |
649 | 662 | av_assert2(s->mb_y || linesize <= block_offset[i]); |
650 | 662 | topright = ptr + 4 - linesize; |
651 | 662 | } else |
652 | 199k | topright = NULL; |
653 | | |
654 | 200k | s->hpc.pred4x4[dir](ptr, topright, linesize); |
655 | 200k | nnz = s->non_zero_count_cache[scan8[i]]; |
656 | 200k | if (nnz) { |
657 | 192k | svq3_add_idct_c(ptr, s->mb + i * 16, linesize, qscale, 0); |
658 | 192k | } |
659 | 200k | } |
660 | 27.0k | } else { |
661 | 27.0k | s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize); |
662 | 27.0k | svq3_luma_dc_dequant_idct_c(s->mb, s->mb_luma_dc[0], qscale); |
663 | 27.0k | } |
664 | 39.5k | } |
665 | | |
666 | | static void hl_decode_mb(SVQ3Context *s) |
667 | 321k | { |
668 | 321k | const int mb_x = s->mb_x; |
669 | 321k | const int mb_y = s->mb_y; |
670 | 321k | const int mb_xy = s->mb_xy; |
671 | 321k | const int mb_type = s->cur_pic->mb_type[mb_xy]; |
672 | 321k | uint8_t *dest_y, *dest_cb, *dest_cr; |
673 | 321k | int linesize, uvlinesize; |
674 | 321k | int i, j; |
675 | 321k | const int *block_offset = &s->block_offset[0]; |
676 | 321k | const int block_h = 16 >> 1; |
677 | | |
678 | 321k | linesize = s->cur_pic->f->linesize[0]; |
679 | 321k | uvlinesize = s->cur_pic->f->linesize[1]; |
680 | | |
681 | 321k | dest_y = s->cur_pic->f->data[0] + (mb_x + mb_y * linesize) * 16; |
682 | 321k | dest_cb = s->cur_pic->f->data[1] + mb_x * 8 + mb_y * uvlinesize * block_h; |
683 | 321k | dest_cr = s->cur_pic->f->data[2] + mb_x * 8 + mb_y * uvlinesize * block_h; |
684 | | |
685 | 321k | s->vdsp.prefetch(dest_y + (s->mb_x & 3) * 4 * linesize + 64, linesize, 4); |
686 | 321k | s->vdsp.prefetch(dest_cb + (s->mb_x & 7) * uvlinesize + 64, dest_cr - dest_cb, 2); |
687 | | |
688 | 321k | if (IS_INTRA(mb_type)) { |
689 | 39.5k | s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize); |
690 | 39.5k | s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize); |
691 | | |
692 | 39.5k | hl_decode_mb_predict_luma(s, mb_type, block_offset, linesize, dest_y); |
693 | 39.5k | } |
694 | | |
695 | 321k | hl_decode_mb_idct_luma(s, mb_type, block_offset, linesize, dest_y); |
696 | | |
697 | 321k | if (s->cbp & 0x30) { |
698 | 20.2k | uint8_t *dest[2] = { dest_cb, dest_cr }; |
699 | 20.2k | s->h264dsp.chroma_dc_dequant_idct(s->mb + 16 * 16 * 1, |
700 | 20.2k | s->dequant4_coeff[4][0]); |
701 | 20.2k | s->h264dsp.chroma_dc_dequant_idct(s->mb + 16 * 16 * 2, |
702 | 20.2k | s->dequant4_coeff[4][0]); |
703 | 60.8k | for (j = 1; j < 3; j++) { |
704 | 202k | for (i = j * 16; i < j * 16 + 4; i++) |
705 | 162k | if (s->non_zero_count_cache[scan8[i]] || s->mb[i * 16]) { |
706 | 117k | uint8_t *const ptr = dest[j - 1] + block_offset[i]; |
707 | 117k | svq3_add_idct_c(ptr, s->mb + i * 16, |
708 | 117k | uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2); |
709 | 117k | } |
710 | 40.5k | } |
711 | 20.2k | } |
712 | 321k | } |
713 | | |
714 | | static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type) |
715 | 1.76M | { |
716 | 1.76M | int i, j, k, m, dir, mode; |
717 | 1.76M | int cbp = 0; |
718 | 1.76M | uint32_t vlc; |
719 | 1.76M | int8_t *top, *left; |
720 | 1.76M | const int mb_xy = s->mb_xy; |
721 | 1.76M | const int b_xy = 4 * s->mb_x + 4 * s->mb_y * s->b_stride; |
722 | | |
723 | 1.76M | s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF; |
724 | 1.76M | s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF; |
725 | | |
726 | 1.76M | if (mb_type == 0) { /* SKIP */ |
727 | 1.44M | if (s->pict_type == AV_PICTURE_TYPE_P || |
728 | 1.40M | s->next_pic->mb_type[mb_xy] == -1) { |
729 | 1.40M | svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16, |
730 | 1.40M | 0, 0, 0, 0, 0, 0); |
731 | | |
732 | 1.40M | if (s->pict_type == AV_PICTURE_TYPE_B) |
733 | 8.88k | svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16, |
734 | 8.88k | 0, 0, 0, 0, 1, 1); |
735 | | |
736 | 1.40M | mb_type = MB_TYPE_SKIP; |
737 | 1.40M | } else { |
738 | 40.0k | mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6); |
739 | 40.0k | if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0) |
740 | 0 | return -1; |
741 | 40.0k | if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0) |
742 | 0 | return -1; |
743 | | |
744 | 40.0k | mb_type = MB_TYPE_16x16; |
745 | 40.0k | } |
746 | 1.44M | } else if (mb_type < 8) { /* INTER */ |
747 | 272k | if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&s->gb_slice)) |
748 | 34.3k | mode = THIRDPEL_MODE; |
749 | 238k | else if (s->halfpel_flag && |
750 | 236k | s->thirdpel_flag == !get_bits1(&s->gb_slice)) |
751 | 19.5k | mode = HALFPEL_MODE; |
752 | 218k | else |
753 | 218k | mode = FULLPEL_MODE; |
754 | | |
755 | | /* fill caches */ |
756 | | /* note ref_cache should contain here: |
757 | | * ???????? |
758 | | * ???11111 |
759 | | * N??11111 |
760 | | * N??11111 |
761 | | * N??11111 |
762 | | */ |
763 | | |
764 | 335k | for (m = 0; m < 2; m++) { |
765 | 304k | if (s->mb_x > 0 && s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6] != -1) { |
766 | 1.29M | for (i = 0; i < 4; i++) |
767 | 1.03M | AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8], |
768 | 259k | s->cur_pic->motion_val[m][b_xy - 1 + i * s->b_stride]); |
769 | 259k | } else { |
770 | 225k | for (i = 0; i < 4; i++) |
771 | 180k | AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]); |
772 | 45.0k | } |
773 | 304k | if (s->mb_y > 0) { |
774 | 185k | memcpy(s->mv_cache[m][scan8[0] - 1 * 8], |
775 | 185k | s->cur_pic->motion_val[m][b_xy - s->b_stride], |
776 | 185k | 4 * 2 * sizeof(int16_t)); |
777 | 185k | memset(&s->ref_cache[m][scan8[0] - 1 * 8], |
778 | 185k | (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4); |
779 | | |
780 | 185k | if (s->mb_x < s->mb_width - 1) { |
781 | 145k | AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8], |
782 | 145k | s->cur_pic->motion_val[m][b_xy - s->b_stride + 4]); |
783 | 145k | s->ref_cache[m][scan8[0] + 4 - 1 * 8] = |
784 | 145k | (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 || |
785 | 144k | s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1; |
786 | 145k | } else |
787 | 39.4k | s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE; |
788 | 185k | if (s->mb_x > 0) { |
789 | 144k | AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8], |
790 | 144k | s->cur_pic->motion_val[m][b_xy - s->b_stride - 1]); |
791 | 144k | s->ref_cache[m][scan8[0] - 1 - 1 * 8] = |
792 | 144k | (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1; |
793 | 144k | } else |
794 | 40.7k | s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE; |
795 | 185k | } else |
796 | 118k | memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1], |
797 | 118k | PART_NOT_AVAILABLE, 8); |
798 | | |
799 | 304k | if (s->pict_type != AV_PICTURE_TYPE_B) |
800 | 241k | break; |
801 | 304k | } |
802 | | |
803 | | /* decode motion vector(s) and form prediction(s) */ |
804 | 272k | if (s->pict_type == AV_PICTURE_TYPE_P) { |
805 | 241k | if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0) |
806 | 862 | return -1; |
807 | 241k | } else { /* AV_PICTURE_TYPE_B */ |
808 | 31.1k | if (mb_type != 2) { |
809 | 19.6k | if (svq3_mc_dir(s, 0, mode, 0, 0) < 0) |
810 | 444 | return -1; |
811 | 19.6k | } else { |
812 | 57.4k | for (i = 0; i < 4; i++) |
813 | 45.9k | memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride], |
814 | 45.9k | 0, 4 * 2 * sizeof(int16_t)); |
815 | 11.4k | } |
816 | 30.7k | if (mb_type != 1) { |
817 | 24.4k | if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0) |
818 | 301 | return -1; |
819 | 24.4k | } else { |
820 | 31.3k | for (i = 0; i < 4; i++) |
821 | 25.0k | memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride], |
822 | 25.0k | 0, 4 * 2 * sizeof(int16_t)); |
823 | 6.27k | } |
824 | 30.7k | } |
825 | | |
826 | 271k | mb_type = MB_TYPE_16x16; |
827 | 271k | } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */ |
828 | 15.4k | int8_t *i4x4 = s->intra4x4_pred_mode + s->mb2br_xy[s->mb_xy]; |
829 | 15.4k | int8_t *i4x4_cache = s->intra4x4_pred_mode_cache; |
830 | | |
831 | 15.4k | memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t)); |
832 | | |
833 | 15.4k | if (mb_type == 8) { |
834 | 14.3k | if (s->mb_x > 0) { |
835 | 34.5k | for (i = 0; i < 4; i++) |
836 | 27.6k | s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6 - i]; |
837 | 6.91k | if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1) |
838 | 397 | s->left_samples_available = 0x5F5F; |
839 | 6.91k | } |
840 | 14.3k | if (s->mb_y > 0) { |
841 | 6.59k | s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 0]; |
842 | 6.59k | s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 1]; |
843 | 6.59k | s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 2]; |
844 | 6.59k | s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 3]; |
845 | | |
846 | 6.59k | if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1) |
847 | 250 | s->top_samples_available = 0x33FF; |
848 | 6.59k | } |
849 | | |
850 | | /* decode prediction codes for luma blocks */ |
851 | 120k | for (i = 0; i < 16; i += 2) { |
852 | 108k | vlc = get_interleaved_ue_golomb(&s->gb_slice); |
853 | | |
854 | 108k | if (vlc >= 25U) { |
855 | 1.02k | av_log(s->avctx, AV_LOG_ERROR, |
856 | 1.02k | "luma prediction:%"PRIu32"\n", vlc); |
857 | 1.02k | return -1; |
858 | 1.02k | } |
859 | | |
860 | 107k | left = &s->intra4x4_pred_mode_cache[scan8[i] - 1]; |
861 | 107k | top = &s->intra4x4_pred_mode_cache[scan8[i] - 8]; |
862 | | |
863 | 107k | left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]]; |
864 | 107k | left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]]; |
865 | | |
866 | 107k | if (left[1] == -1 || left[2] == -1) { |
867 | 573 | av_log(s->avctx, AV_LOG_ERROR, "weird prediction\n"); |
868 | 573 | return -1; |
869 | 573 | } |
870 | 107k | } |
871 | 14.3k | } else { /* mb_type == 33, DC_128_PRED block type */ |
872 | 5.63k | for (i = 0; i < 4; i++) |
873 | 4.50k | memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4); |
874 | 1.12k | } |
875 | | |
876 | 13.8k | AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4); |
877 | 13.8k | i4x4[4] = i4x4_cache[7 + 8 * 3]; |
878 | 13.8k | i4x4[5] = i4x4_cache[7 + 8 * 2]; |
879 | 13.8k | i4x4[6] = i4x4_cache[7 + 8 * 1]; |
880 | | |
881 | 13.8k | if (mb_type == 8) { |
882 | 12.7k | ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache, |
883 | 12.7k | s->avctx, s->top_samples_available, |
884 | 12.7k | s->left_samples_available); |
885 | | |
886 | 12.7k | s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF; |
887 | 12.7k | s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF; |
888 | 12.7k | } else { |
889 | 5.63k | for (i = 0; i < 4; i++) |
890 | 4.50k | memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4); |
891 | | |
892 | 1.12k | s->top_samples_available = 0x33FF; |
893 | 1.12k | s->left_samples_available = 0x5F5F; |
894 | 1.12k | } |
895 | | |
896 | 13.8k | mb_type = MB_TYPE_INTRA4x4; |
897 | 30.2k | } else { /* INTRA16x16 */ |
898 | 30.2k | dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode; |
899 | 30.2k | dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1; |
900 | | |
901 | 30.2k | if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available, |
902 | 30.2k | s->left_samples_available, dir, 0)) < 0) { |
903 | 1.58k | av_log(s->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n"); |
904 | 1.58k | return s->intra16x16_pred_mode; |
905 | 1.58k | } |
906 | | |
907 | 28.6k | cbp = ff_h264_i_mb_type_info[mb_type - 8].cbp; |
908 | 28.6k | mb_type = MB_TYPE_INTRA16x16; |
909 | 28.6k | } |
910 | | |
911 | 1.76M | if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) { |
912 | 7.11M | for (i = 0; i < 4; i++) |
913 | 5.69M | memset(s->cur_pic->motion_val[0][b_xy + i * s->b_stride], |
914 | 5.69M | 0, 4 * 2 * sizeof(int16_t)); |
915 | 1.42M | if (s->pict_type == AV_PICTURE_TYPE_B) { |
916 | 59.1k | for (i = 0; i < 4; i++) |
917 | 47.3k | memset(s->cur_pic->motion_val[1][b_xy + i * s->b_stride], |
918 | 47.3k | 0, 4 * 2 * sizeof(int16_t)); |
919 | 11.8k | } |
920 | 1.42M | } |
921 | 1.76M | if (!IS_INTRA4x4(mb_type)) { |
922 | 1.74M | memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy], DC_PRED, 8); |
923 | 1.74M | } |
924 | 1.76M | if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) { |
925 | 362k | memset(s->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t)); |
926 | 362k | } |
927 | | |
928 | 1.76M | if (!IS_INTRA16x16(mb_type) && |
929 | 1.73M | (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) { |
930 | 334k | if ((vlc = get_interleaved_ue_golomb(&s->gb_slice)) >= 48U){ |
931 | 911 | av_log(s->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc); |
932 | 911 | return -1; |
933 | 911 | } |
934 | | |
935 | 333k | cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc] |
936 | 333k | : ff_h264_golomb_to_inter_cbp[vlc]; |
937 | 333k | } |
938 | 1.76M | if (IS_INTRA16x16(mb_type) || |
939 | 1.73M | (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) { |
940 | 46.2k | s->qscale += get_interleaved_se_golomb(&s->gb_slice); |
941 | | |
942 | 46.2k | if (s->qscale > 31u) { |
943 | 1.97k | av_log(s->avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale); |
944 | 1.97k | return -1; |
945 | 1.97k | } |
946 | 46.2k | } |
947 | 1.76M | if (IS_INTRA16x16(mb_type)) { |
948 | 27.7k | AV_ZERO128(s->mb_luma_dc[0] + 0); |
949 | 27.7k | AV_ZERO128(s->mb_luma_dc[0] + 8); |
950 | 27.7k | if (svq3_decode_block(&s->gb_slice, s->mb_luma_dc[0], 0, 1)) { |
951 | 559 | av_log(s->avctx, AV_LOG_ERROR, |
952 | 559 | "error while decoding intra luma dc\n"); |
953 | 559 | return -1; |
954 | 559 | } |
955 | 27.7k | } |
956 | | |
957 | 1.75M | if (cbp) { |
958 | 50.0k | const int index = IS_INTRA16x16(mb_type) ? 1 : 0; |
959 | 50.0k | const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1); |
960 | | |
961 | 244k | for (i = 0; i < 4; i++) |
962 | 196k | if ((cbp & (1 << i))) { |
963 | 433k | for (j = 0; j < 4; j++) { |
964 | 347k | k = index ? (1 * (j & 1) + 2 * (i & 1) + |
965 | 6.61k | 2 * (j & 2) + 4 * (i & 2)) |
966 | 347k | : (4 * i + j); |
967 | 347k | s->non_zero_count_cache[scan8[k]] = 1; |
968 | | |
969 | 347k | if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], index, type)) { |
970 | 2.16k | av_log(s->avctx, AV_LOG_ERROR, |
971 | 2.16k | "error while decoding block\n"); |
972 | 2.16k | return -1; |
973 | 2.16k | } |
974 | 347k | } |
975 | 87.8k | } |
976 | | |
977 | 47.9k | if ((cbp & 0x30)) { |
978 | 63.5k | for (i = 1; i < 3; ++i) |
979 | 42.7k | if (svq3_decode_block(&s->gb_slice, &s->mb[16 * 16 * i], 0, 3)) { |
980 | 726 | av_log(s->avctx, AV_LOG_ERROR, |
981 | 726 | "error while decoding chroma dc block\n"); |
982 | 726 | return -1; |
983 | 726 | } |
984 | | |
985 | 20.8k | if ((cbp & 0x20)) { |
986 | 33.0k | for (i = 1; i < 3; i++) { |
987 | 110k | for (j = 0; j < 4; j++) { |
988 | 88.3k | k = 16 * i + j; |
989 | 88.3k | s->non_zero_count_cache[scan8[k]] = 1; |
990 | | |
991 | 88.3k | if (svq3_decode_block(&s->gb_slice, &s->mb[16 * k], 1, 1)) { |
992 | 554 | av_log(s->avctx, AV_LOG_ERROR, |
993 | 554 | "error while decoding chroma ac block\n"); |
994 | 554 | return -1; |
995 | 554 | } |
996 | 88.3k | } |
997 | 22.3k | } |
998 | 11.3k | } |
999 | 20.8k | } |
1000 | 47.9k | } |
1001 | | |
1002 | 1.75M | s->cbp = cbp; |
1003 | 1.75M | s->cur_pic->mb_type[mb_xy] = mb_type; |
1004 | | |
1005 | 1.75M | if (IS_INTRA(mb_type)) |
1006 | 39.5k | s->chroma_pred_mode = ff_h264_check_intra_pred_mode(s->avctx, s->top_samples_available, |
1007 | 39.5k | s->left_samples_available, DC_PRED8x8, 1); |
1008 | | |
1009 | 1.75M | return 0; |
1010 | 1.75M | } |
1011 | | |
1012 | | static int svq3_decode_slice_header(AVCodecContext *avctx) |
1013 | 316k | { |
1014 | 316k | SVQ3Context *s = avctx->priv_data; |
1015 | 316k | const int mb_xy = s->mb_xy; |
1016 | 316k | int i, header; |
1017 | 316k | unsigned slice_id; |
1018 | | |
1019 | 316k | header = get_bits(&s->gb, 8); |
1020 | | |
1021 | 316k | if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) { |
1022 | | /* TODO: what? */ |
1023 | 119k | av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header); |
1024 | 119k | return -1; |
1025 | 197k | } else { |
1026 | 197k | int slice_bits, slice_bytes, slice_length; |
1027 | 197k | int length = header >> 5 & 3; |
1028 | | |
1029 | 197k | slice_length = show_bits(&s->gb, 8 * length); |
1030 | 197k | slice_bits = slice_length * 8; |
1031 | 197k | slice_bytes = slice_length + length - 1; |
1032 | | |
1033 | 197k | skip_bits(&s->gb, 8); |
1034 | | |
1035 | 197k | av_fast_padded_malloc(&s->slice_buf, &s->slice_buf_size, slice_bytes); |
1036 | 197k | if (!s->slice_buf) |
1037 | 0 | return AVERROR(ENOMEM); |
1038 | | |
1039 | 197k | if (slice_bytes * 8LL > get_bits_left(&s->gb)) { |
1040 | 9.49k | av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n"); |
1041 | 9.49k | return AVERROR_INVALIDDATA; |
1042 | 9.49k | } |
1043 | 187k | memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes); |
1044 | | |
1045 | 187k | if (length > 0) { |
1046 | 187k | memmove(s->slice_buf, &s->slice_buf[slice_length], length - 1); |
1047 | 187k | } |
1048 | | |
1049 | 187k | if (s->watermark_key) { |
1050 | 211 | uint32_t header = AV_RL32(&s->slice_buf[1]); |
1051 | 211 | AV_WL32(&s->slice_buf[1], header ^ s->watermark_key); |
1052 | 211 | } |
1053 | 187k | init_get_bits(&s->gb_slice, s->slice_buf, slice_bits); |
1054 | | |
1055 | 187k | skip_bits_long(&s->gb, slice_bytes * 8); |
1056 | 187k | } |
1057 | | |
1058 | 187k | if ((slice_id = get_interleaved_ue_golomb(&s->gb_slice)) >= 3) { |
1059 | 1.72k | av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id); |
1060 | 1.72k | return -1; |
1061 | 1.72k | } |
1062 | | |
1063 | 185k | s->slice_type = ff_h264_golomb_to_pict_type[slice_id]; |
1064 | | |
1065 | 185k | if ((header & 0x9F) == 2) { |
1066 | 167k | i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1)); |
1067 | 167k | get_bits(&s->gb_slice, i); |
1068 | 167k | } else if (get_bits1(&s->gb_slice)) { |
1069 | 307 | avpriv_report_missing_feature(s->avctx, "Media key encryption"); |
1070 | 307 | return AVERROR_PATCHWELCOME; |
1071 | 307 | } |
1072 | | |
1073 | 185k | s->slice_num = get_bits(&s->gb_slice, 8); |
1074 | 185k | s->qscale = get_bits(&s->gb_slice, 5); |
1075 | 185k | s->adaptive_quant = get_bits1(&s->gb_slice); |
1076 | | |
1077 | | /* unknown fields */ |
1078 | 185k | skip_bits1(&s->gb_slice); |
1079 | | |
1080 | 185k | if (s->has_watermark) |
1081 | 1.24k | skip_bits1(&s->gb_slice); |
1082 | | |
1083 | 185k | skip_bits1(&s->gb_slice); |
1084 | 185k | skip_bits(&s->gb_slice, 2); |
1085 | | |
1086 | 185k | if (skip_1stop_8data_bits(&s->gb_slice) < 0) |
1087 | 32.6k | return AVERROR_INVALIDDATA; |
1088 | | |
1089 | | /* reset intra predictors and invalidate motion vector references */ |
1090 | 152k | if (s->mb_x > 0) { |
1091 | 1.70k | memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3, |
1092 | 1.70k | -1, 4 * sizeof(int8_t)); |
1093 | 1.70k | memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x], |
1094 | 1.70k | -1, 8 * sizeof(int8_t) * s->mb_x); |
1095 | 1.70k | } |
1096 | 152k | if (s->mb_y > 0) { |
1097 | 931 | memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride], |
1098 | 931 | -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x)); |
1099 | | |
1100 | 931 | if (s->mb_x > 0) |
1101 | 685 | s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1; |
1102 | 931 | } |
1103 | | |
1104 | 152k | return 0; |
1105 | 185k | } |
1106 | | |
1107 | | static void init_dequant4_coeff_table(SVQ3Context *s) |
1108 | 2.11k | { |
1109 | 2.11k | int q, x; |
1110 | 2.11k | const int max_qp = 51; |
1111 | | |
1112 | 111k | for (q = 0; q < max_qp + 1; q++) { |
1113 | 109k | int shift = ff_h264_quant_div6[q] + 2; |
1114 | 109k | int idx = ff_h264_quant_rem6[q]; |
1115 | 1.86M | for (x = 0; x < 16; x++) |
1116 | 1.75M | s->dequant4_coeff[q][(x >> 2) | ((x << 2) & 0xF)] = |
1117 | 1.75M | ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * 16) << shift; |
1118 | 109k | } |
1119 | 2.11k | } |
1120 | | |
1121 | | static av_cold int svq3_decode_extradata(AVCodecContext *avctx, SVQ3Context *s, |
1122 | | int seqh_offset) |
1123 | 726 | { |
1124 | 726 | const uint8_t *extradata = avctx->extradata + seqh_offset; |
1125 | 726 | unsigned int size = AV_RB32(extradata + 4); |
1126 | 726 | GetBitContext gb; |
1127 | 726 | int ret; |
1128 | | |
1129 | 726 | if (size > avctx->extradata_size - seqh_offset - 8) |
1130 | 35 | return AVERROR_INVALIDDATA; |
1131 | 691 | extradata += 8; |
1132 | 691 | init_get_bits(&gb, extradata, size * 8); |
1133 | | |
1134 | | /* 'frame size code' and optional 'width, height' */ |
1135 | 691 | int frame_size_code = get_bits(&gb, 3); |
1136 | 691 | int w, h; |
1137 | 691 | switch (frame_size_code) { |
1138 | 460 | case 0: |
1139 | 460 | w = 160; |
1140 | 460 | h = 120; |
1141 | 460 | break; |
1142 | 37 | case 1: |
1143 | 37 | w = 128; |
1144 | 37 | h = 96; |
1145 | 37 | break; |
1146 | 15 | case 2: |
1147 | 15 | w = 176; |
1148 | 15 | h = 144; |
1149 | 15 | break; |
1150 | 23 | case 3: |
1151 | 23 | w = 352; |
1152 | 23 | h = 288; |
1153 | 23 | break; |
1154 | 9 | case 4: |
1155 | 9 | w = 704; |
1156 | 9 | h = 576; |
1157 | 9 | break; |
1158 | 11 | case 5: |
1159 | 11 | w = 240; |
1160 | 11 | h = 180; |
1161 | 11 | break; |
1162 | 1 | case 6: |
1163 | 1 | w = 320; |
1164 | 1 | h = 240; |
1165 | 1 | break; |
1166 | 135 | case 7: |
1167 | 135 | w = get_bits(&gb, 12); |
1168 | 135 | h = get_bits(&gb, 12); |
1169 | 135 | break; |
1170 | 691 | } |
1171 | 691 | ret = ff_set_dimensions(avctx, w, h); |
1172 | 691 | if (ret < 0) |
1173 | 1 | return ret; |
1174 | | |
1175 | 690 | s->halfpel_flag = get_bits1(&gb); |
1176 | 690 | s->thirdpel_flag = get_bits1(&gb); |
1177 | | |
1178 | | /* unknown fields */ |
1179 | 690 | int unk0 = get_bits1(&gb); |
1180 | 690 | int unk1 = get_bits1(&gb); |
1181 | 690 | int unk2 = get_bits1(&gb); |
1182 | 690 | int unk3 = get_bits1(&gb); |
1183 | | |
1184 | 690 | s->low_delay = get_bits1(&gb); |
1185 | 690 | avctx->has_b_frames = !s->low_delay; |
1186 | | |
1187 | | /* unknown field */ |
1188 | 690 | int unk4 = get_bits1(&gb); |
1189 | | |
1190 | 690 | av_log(avctx, AV_LOG_DEBUG, "Unknown fields %d %d %d %d %d\n", |
1191 | 690 | unk0, unk1, unk2, unk3, unk4); |
1192 | | |
1193 | 690 | if (skip_1stop_8data_bits(&gb) < 0) |
1194 | 8 | return AVERROR_INVALIDDATA; |
1195 | | |
1196 | 682 | s->has_watermark = get_bits1(&gb); |
1197 | | |
1198 | 682 | if (!s->has_watermark) |
1199 | 86 | return 0; |
1200 | | |
1201 | 596 | #if CONFIG_ZLIB |
1202 | 596 | unsigned watermark_width = get_interleaved_ue_golomb(&gb); |
1203 | 596 | unsigned watermark_height = get_interleaved_ue_golomb(&gb); |
1204 | 596 | int u1 = get_interleaved_ue_golomb(&gb); |
1205 | 596 | int u2 = get_bits(&gb, 8); |
1206 | 596 | int u3 = get_bits(&gb, 2); |
1207 | 596 | int u4 = get_interleaved_ue_golomb(&gb); |
1208 | 596 | unsigned long buf_len = watermark_width * |
1209 | 596 | watermark_height * 4; |
1210 | 596 | int offset = get_bits_count(&gb) + 7 >> 3; |
1211 | | |
1212 | 596 | if (watermark_height <= 0 || |
1213 | 561 | get_bits_left(&gb) <= 0 || |
1214 | 541 | (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height) |
1215 | 82 | return AVERROR_INVALIDDATA; |
1216 | | |
1217 | 514 | av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n", |
1218 | 514 | watermark_width, watermark_height); |
1219 | 514 | av_log(avctx, AV_LOG_DEBUG, |
1220 | 514 | "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n", |
1221 | 514 | u1, u2, u3, u4, offset); |
1222 | | |
1223 | 514 | uint8_t *buf = av_malloc(buf_len); |
1224 | 514 | if (!buf) |
1225 | 17 | return AVERROR(ENOMEM); |
1226 | | |
1227 | 497 | if (uncompress(buf, &buf_len, extradata + offset, |
1228 | 497 | size - offset) != Z_OK) { |
1229 | 465 | av_log(avctx, AV_LOG_ERROR, |
1230 | 465 | "could not uncompress watermark logo\n"); |
1231 | 465 | av_free(buf); |
1232 | 465 | return AVERROR_EXTERNAL; |
1233 | 465 | } |
1234 | 32 | s->watermark_key = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_CCITT), 0, buf, buf_len)); |
1235 | | |
1236 | 32 | s->watermark_key = s->watermark_key << 16 | s->watermark_key; |
1237 | 32 | av_log(avctx, AV_LOG_DEBUG, |
1238 | 32 | "watermark key %#"PRIx32"\n", s->watermark_key); |
1239 | 32 | av_free(buf); |
1240 | | |
1241 | 32 | return 0; |
1242 | | #else |
1243 | | av_log(avctx, AV_LOG_ERROR, |
1244 | | "this svq3 file contains watermark which need zlib support compiled in\n"); |
1245 | | return AVERROR(ENOSYS); |
1246 | | #endif |
1247 | 497 | } |
1248 | | |
1249 | | static av_cold int svq3_decode_init(AVCodecContext *avctx) |
1250 | 2.72k | { |
1251 | 2.72k | SVQ3Context *s = avctx->priv_data; |
1252 | 2.72k | int m, x, y; |
1253 | 2.72k | unsigned char *extradata; |
1254 | 2.72k | int ret; |
1255 | | |
1256 | 2.72k | s->cur_pic = &s->frames[0]; |
1257 | 2.72k | s->last_pic = &s->frames[1]; |
1258 | 2.72k | s->next_pic = &s->frames[2]; |
1259 | | |
1260 | 2.72k | s->cur_pic->f = av_frame_alloc(); |
1261 | 2.72k | s->last_pic->f = av_frame_alloc(); |
1262 | 2.72k | s->next_pic->f = av_frame_alloc(); |
1263 | 2.72k | if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f) |
1264 | 0 | return AVERROR(ENOMEM); |
1265 | | |
1266 | 2.72k | ff_h264dsp_init(&s->h264dsp, 8, 1); |
1267 | 2.72k | ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1); |
1268 | 2.72k | ff_videodsp_init(&s->vdsp, 8); |
1269 | | |
1270 | | |
1271 | 2.72k | avctx->bits_per_raw_sample = 8; |
1272 | | |
1273 | 2.72k | ff_hpeldsp_init(&s->hdsp, avctx->flags); |
1274 | 2.72k | ff_tpeldsp_init(&s->tdsp); |
1275 | | |
1276 | 2.72k | avctx->pix_fmt = AV_PIX_FMT_YUVJ420P; |
1277 | 2.72k | avctx->color_range = AVCOL_RANGE_JPEG; |
1278 | | |
1279 | 2.72k | s->avctx = avctx; |
1280 | 2.72k | s->halfpel_flag = 1; |
1281 | 2.72k | s->thirdpel_flag = 1; |
1282 | 2.72k | s->has_watermark = 0; |
1283 | | |
1284 | | /* prowl for the "SEQH" marker in the extradata */ |
1285 | 2.72k | extradata = (unsigned char *)avctx->extradata; |
1286 | 2.72k | if (extradata) { |
1287 | 1.86M | for (m = 0; m + 8 < avctx->extradata_size; m++) { |
1288 | 1.86M | if (!memcmp(extradata, "SEQH", 4)) { |
1289 | | /* if a match was found, parse the extra data */ |
1290 | 726 | ret = svq3_decode_extradata(avctx, s, m); |
1291 | 726 | if (ret < 0) |
1292 | 608 | return ret; |
1293 | 118 | break; |
1294 | 726 | } |
1295 | 1.86M | extradata++; |
1296 | 1.86M | } |
1297 | 962 | } |
1298 | | |
1299 | 2.11k | s->mb_width = (avctx->width + 15) / 16; |
1300 | 2.11k | s->mb_height = (avctx->height + 15) / 16; |
1301 | 2.11k | s->mb_stride = s->mb_width + 1; |
1302 | 2.11k | s->mb_num = s->mb_width * s->mb_height; |
1303 | 2.11k | s->b_stride = 4 * s->mb_width; |
1304 | 2.11k | s->h_edge_pos = s->mb_width * 16; |
1305 | 2.11k | s->v_edge_pos = s->mb_height * 16; |
1306 | | |
1307 | 2.11k | const unsigned big_mb_num = s->mb_stride * (s->mb_height + 2) + 1; |
1308 | | |
1309 | 2.11k | s->mb_type_buf = av_calloc(big_mb_num, NUM_PICS * sizeof(*s->mb_type_buf)); |
1310 | 2.11k | if (!s->mb_type_buf) |
1311 | 0 | return AVERROR(ENOMEM); |
1312 | 2.11k | uint32_t *mb_type_buf = s->mb_type_buf + 2 * s->mb_stride + 1; |
1313 | | |
1314 | 2.11k | const unsigned b4_stride = s->mb_width * 4 + 1; |
1315 | 2.11k | const unsigned b4_array_size = b4_stride * s->mb_height * 4; |
1316 | 2.11k | const unsigned motion_val_buf_size = b4_array_size + 4; |
1317 | | |
1318 | 2.11k | s->motion_val_buf = av_calloc(motion_val_buf_size, |
1319 | 2.11k | NUM_PICS * 2 * sizeof(*s->motion_val_buf)); |
1320 | 2.11k | if (!s->motion_val_buf) |
1321 | 0 | return AVERROR(ENOMEM); |
1322 | 2.11k | int16_t (*motion_val_buf)[2] = s->motion_val_buf + 4; |
1323 | | |
1324 | 8.45k | for (size_t i = 0; i < NUM_PICS; ++i) { |
1325 | 6.33k | SVQ3Frame *const pic = &s->frames[i]; |
1326 | | |
1327 | 6.33k | pic->mb_type = mb_type_buf; |
1328 | 6.33k | mb_type_buf += big_mb_num; |
1329 | 19.0k | for (size_t j = 0; j < FF_ARRAY_ELEMS(pic->motion_val); ++j) { |
1330 | 12.6k | pic->motion_val[j] = motion_val_buf; |
1331 | 12.6k | motion_val_buf += motion_val_buf_size; |
1332 | 12.6k | } |
1333 | 6.33k | } |
1334 | | |
1335 | 2.11k | s->intra4x4_pred_mode = av_mallocz(s->mb_stride * 2 * 8); |
1336 | 2.11k | if (!s->intra4x4_pred_mode) |
1337 | 0 | return AVERROR(ENOMEM); |
1338 | | |
1339 | 2.11k | s->mb2br_xy = av_mallocz(s->mb_stride * (s->mb_height + 1) * |
1340 | 2.11k | sizeof(*s->mb2br_xy)); |
1341 | 2.11k | if (!s->mb2br_xy) |
1342 | 0 | return AVERROR(ENOMEM); |
1343 | | |
1344 | 4.24M | for (y = 0; y < s->mb_height; y++) |
1345 | 20.9M | for (x = 0; x < s->mb_width; x++) { |
1346 | 16.7M | const int mb_xy = x + y * s->mb_stride; |
1347 | | |
1348 | 16.7M | s->mb2br_xy[mb_xy] = 8 * (mb_xy % (2 * s->mb_stride)); |
1349 | 16.7M | } |
1350 | | |
1351 | 2.11k | init_dequant4_coeff_table(s); |
1352 | | |
1353 | 2.11k | return 0; |
1354 | 2.11k | } |
1355 | | |
1356 | | static int get_buffer(AVCodecContext *avctx, SVQ3Frame *pic) |
1357 | 138k | { |
1358 | 138k | SVQ3Context *s = avctx->priv_data; |
1359 | 138k | int ret = ff_get_buffer(avctx, pic->f, |
1360 | 138k | (s->pict_type != AV_PICTURE_TYPE_B) ? |
1361 | 129k | AV_GET_BUFFER_FLAG_REF : 0); |
1362 | 138k | if (ret < 0) |
1363 | 2.26k | return ret; |
1364 | | |
1365 | 136k | if (!s->edge_emu_buffer) { |
1366 | 1.18k | s->edge_emu_buffer = av_calloc(pic->f->linesize[0], 17); |
1367 | 1.18k | if (!s->edge_emu_buffer) |
1368 | 0 | return AVERROR(ENOMEM); |
1369 | 1.18k | } |
1370 | | |
1371 | 136k | return 0; |
1372 | 136k | } |
1373 | | |
1374 | | static av_cold int alloc_dummy_frame(AVCodecContext *avctx, SVQ3Frame *pic) |
1375 | 1.59k | { |
1376 | 1.59k | av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n"); |
1377 | 1.59k | av_frame_unref(pic->f); |
1378 | 1.59k | int ret = get_buffer(avctx, pic); |
1379 | 1.59k | if (ret < 0) |
1380 | 0 | return ret; |
1381 | | |
1382 | 1.59k | memset(pic->f->data[0], 0, avctx->height * pic->f->linesize[0]); |
1383 | 1.59k | memset(pic->f->data[1], 0x80, (avctx->height / 2) * |
1384 | 1.59k | pic->f->linesize[1]); |
1385 | 1.59k | memset(pic->f->data[2], 0x80, (avctx->height / 2) * |
1386 | 1.59k | pic->f->linesize[2]); |
1387 | | |
1388 | 1.59k | return 0; |
1389 | 1.59k | } |
1390 | | |
1391 | | static int svq3_decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
1392 | | int *got_frame, AVPacket *avpkt) |
1393 | 315k | { |
1394 | 315k | SVQ3Context *s = avctx->priv_data; |
1395 | 315k | int buf_size = avpkt->size; |
1396 | 315k | int left; |
1397 | 315k | int ret, m, i; |
1398 | | |
1399 | | /* special case for last picture */ |
1400 | 315k | if (buf_size == 0) { |
1401 | 2.82k | if (s->next_pic->f->data[0] && !s->low_delay) { |
1402 | 784 | av_frame_move_ref(rframe, s->next_pic->f); |
1403 | 784 | *got_frame = 1; |
1404 | 784 | } |
1405 | 2.82k | return 0; |
1406 | 2.82k | } |
1407 | | |
1408 | 312k | s->mb_x = s->mb_y = s->mb_xy = 0; |
1409 | | |
1410 | 312k | ret = init_get_bits8(&s->gb, avpkt->data, avpkt->size); |
1411 | 312k | if (ret < 0) |
1412 | 0 | return ret; |
1413 | | |
1414 | 312k | ret = svq3_decode_slice_header(avctx); |
1415 | 312k | if (ret < 0) |
1416 | 161k | return ret; |
1417 | | |
1418 | 151k | if (avpkt->size < s->mb_width * s->mb_height / 8) |
1419 | 14.0k | return AVERROR_INVALIDDATA; |
1420 | | |
1421 | 137k | s->pict_type = s->slice_type; |
1422 | | |
1423 | 137k | if (s->pict_type != AV_PICTURE_TYPE_B) |
1424 | 127k | FFSWAP(SVQ3Frame*, s->next_pic, s->last_pic); |
1425 | | |
1426 | 137k | av_frame_unref(s->cur_pic->f); |
1427 | | |
1428 | | /* for skipping the frame */ |
1429 | 137k | s->cur_pic->f->pict_type = s->pict_type; |
1430 | 137k | if (s->pict_type == AV_PICTURE_TYPE_I) |
1431 | 7.41k | s->cur_pic->f->flags |= AV_FRAME_FLAG_KEY; |
1432 | 129k | else |
1433 | 129k | s->cur_pic->f->flags &= ~AV_FRAME_FLAG_KEY; |
1434 | | |
1435 | 137k | ret = get_buffer(avctx, s->cur_pic); |
1436 | 137k | if (ret < 0) |
1437 | 2.26k | return ret; |
1438 | | |
1439 | 2.29M | for (i = 0; i < 16; i++) { |
1440 | 2.15M | s->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3); |
1441 | 2.15M | s->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3); |
1442 | 2.15M | } |
1443 | 2.29M | for (i = 0; i < 16; i++) { |
1444 | 2.15M | s->block_offset[16 + i] = |
1445 | 2.15M | s->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3); |
1446 | 2.15M | s->block_offset[48 + 16 + i] = |
1447 | 2.15M | s->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * s->cur_pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3); |
1448 | 2.15M | } |
1449 | | |
1450 | 134k | if (s->pict_type != AV_PICTURE_TYPE_I) { |
1451 | 128k | if (!s->last_pic->f->data[0]) { |
1452 | 1.31k | ret = alloc_dummy_frame(avctx, s->last_pic); |
1453 | 1.31k | if (ret < 0) |
1454 | 0 | return ret; |
1455 | 1.31k | } |
1456 | | |
1457 | 128k | if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) { |
1458 | 289 | ret = alloc_dummy_frame(avctx, s->next_pic); |
1459 | 289 | if (ret < 0) |
1460 | 0 | return ret; |
1461 | 289 | } |
1462 | 128k | } |
1463 | | |
1464 | 134k | if (avctx->debug & FF_DEBUG_PICT_INFO) |
1465 | 0 | av_log(s->avctx, AV_LOG_DEBUG, |
1466 | 0 | "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n", |
1467 | 0 | av_get_picture_type_char(s->pict_type), |
1468 | 0 | s->halfpel_flag, s->thirdpel_flag, |
1469 | 0 | s->adaptive_quant, s->qscale, s->slice_num); |
1470 | | |
1471 | 134k | if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B || |
1472 | 133k | avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I || |
1473 | 133k | avctx->skip_frame >= AVDISCARD_ALL) |
1474 | 2.60k | return 0; |
1475 | | |
1476 | 132k | if (s->pict_type == AV_PICTURE_TYPE_B) { |
1477 | 7.73k | s->frame_num_offset = s->slice_num - s->prev_frame_num; |
1478 | | |
1479 | 7.73k | if (s->frame_num_offset < 0) |
1480 | 1.39k | s->frame_num_offset += 256; |
1481 | 7.73k | if (s->frame_num_offset == 0 || |
1482 | 7.53k | s->frame_num_offset >= s->prev_frame_num_offset) { |
1483 | 1.63k | av_log(s->avctx, AV_LOG_ERROR, "error in B-frame picture id\n"); |
1484 | 1.63k | return -1; |
1485 | 1.63k | } |
1486 | 124k | } else { |
1487 | 124k | s->prev_frame_num = s->frame_num; |
1488 | 124k | s->frame_num = s->slice_num; |
1489 | 124k | s->prev_frame_num_offset = s->frame_num - s->prev_frame_num; |
1490 | | |
1491 | 124k | if (s->prev_frame_num_offset < 0) |
1492 | 5.57k | s->prev_frame_num_offset += 256; |
1493 | 124k | } |
1494 | | |
1495 | 391k | for (m = 0; m < 2; m++) { |
1496 | 261k | int i; |
1497 | 1.30M | for (i = 0; i < 4; i++) { |
1498 | 1.04M | int j; |
1499 | 6.26M | for (j = -1; j < 4; j++) |
1500 | 5.22M | s->ref_cache[m][scan8[0] + 8 * i + j] = 1; |
1501 | 1.04M | if (i < 3) |
1502 | 783k | s->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE; |
1503 | 1.04M | } |
1504 | 261k | } |
1505 | | |
1506 | 540k | for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { |
1507 | 2.18M | for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) { |
1508 | 1.77M | unsigned mb_type; |
1509 | 1.77M | s->mb_xy = s->mb_x + s->mb_y * s->mb_stride; |
1510 | | |
1511 | 1.77M | if ((get_bits_left(&s->gb_slice)) <= 7) { |
1512 | 136k | if (((get_bits_count(&s->gb_slice) & 7) == 0 || |
1513 | 132k | show_bits(&s->gb_slice, get_bits_left(&s->gb_slice) & 7) == 0)) { |
1514 | | |
1515 | 4.59k | ret = svq3_decode_slice_header(avctx); |
1516 | 4.59k | if (ret < 0) |
1517 | 2.63k | return ret; |
1518 | 4.59k | } |
1519 | 133k | if (s->slice_type != s->pict_type) { |
1520 | 1.21k | avpriv_request_sample(avctx, "non constant slice type"); |
1521 | 1.21k | } |
1522 | | /* TODO: support s->mb_skip_run */ |
1523 | 133k | } |
1524 | | |
1525 | 1.77M | mb_type = get_interleaved_ue_golomb(&s->gb_slice); |
1526 | | |
1527 | 1.77M | if (s->pict_type == AV_PICTURE_TYPE_I) |
1528 | 30.4k | mb_type += 8; |
1529 | 1.74M | else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4) |
1530 | 3.98k | mb_type += 4; |
1531 | 1.77M | if (mb_type > 33 || svq3_decode_mb(s, mb_type)) { |
1532 | 17.0k | av_log(s->avctx, AV_LOG_ERROR, |
1533 | 17.0k | "error while decoding MB %d %d\n", s->mb_x, s->mb_y); |
1534 | 17.0k | return -1; |
1535 | 17.0k | } |
1536 | | |
1537 | 1.75M | if (mb_type != 0 || s->cbp) |
1538 | 321k | hl_decode_mb(s); |
1539 | | |
1540 | 1.75M | if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay) |
1541 | 1.65M | s->cur_pic->mb_type[s->mb_x + s->mb_y * s->mb_stride] = |
1542 | 1.65M | (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1; |
1543 | 1.75M | } |
1544 | | |
1545 | 409k | ff_draw_horiz_band(avctx, s->cur_pic->f, |
1546 | 409k | s->last_pic->f->data[0] ? s->last_pic->f : NULL, |
1547 | 409k | 16 * s->mb_y, 16, PICT_FRAME, 0, |
1548 | 409k | s->low_delay); |
1549 | 409k | } |
1550 | | |
1551 | 110k | left = buf_size*8 - get_bits_count(&s->gb_slice); |
1552 | | |
1553 | 110k | if (s->mb_y != s->mb_height || s->mb_x != s->mb_width) { |
1554 | 0 | av_log(avctx, AV_LOG_INFO, "frame num %"PRId64" incomplete pic x %d y %d left %d\n", avctx->frame_num, s->mb_y, s->mb_x, left); |
1555 | | //av_hex_dump(stderr, buf+buf_size-8, 8); |
1556 | 0 | } |
1557 | | |
1558 | 110k | if (left < 0) { |
1559 | 0 | av_log(avctx, AV_LOG_ERROR, "frame num %"PRId64" left %d\n", avctx->frame_num, left); |
1560 | 0 | return -1; |
1561 | 0 | } |
1562 | | |
1563 | 110k | if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) |
1564 | 900 | ret = av_frame_ref(rframe, s->cur_pic->f); |
1565 | 109k | else if (s->last_pic->f->data[0]) |
1566 | 109k | ret = av_frame_ref(rframe, s->last_pic->f); |
1567 | 110k | if (ret < 0) |
1568 | 0 | return ret; |
1569 | | |
1570 | | /* Do not output the last pic after seeking. */ |
1571 | 110k | if (s->last_pic->f->data[0] || s->low_delay) |
1572 | 110k | *got_frame = 1; |
1573 | | |
1574 | 110k | if (s->pict_type != AV_PICTURE_TYPE_B) { |
1575 | 110k | FFSWAP(SVQ3Frame*, s->cur_pic, s->next_pic); |
1576 | 110k | } else { |
1577 | 338 | av_frame_unref(s->cur_pic->f); |
1578 | 338 | } |
1579 | | |
1580 | 110k | return buf_size; |
1581 | 110k | } |
1582 | | |
1583 | | static av_cold int svq3_decode_end(AVCodecContext *avctx) |
1584 | 2.72k | { |
1585 | 2.72k | SVQ3Context *s = avctx->priv_data; |
1586 | | |
1587 | 10.8k | for (int i = 0; i < NUM_PICS; i++) |
1588 | 8.16k | av_frame_free(&s->frames[i].f); |
1589 | 2.72k | av_freep(&s->motion_val_buf); |
1590 | 2.72k | av_freep(&s->mb_type_buf); |
1591 | 2.72k | av_freep(&s->slice_buf); |
1592 | 2.72k | av_freep(&s->intra4x4_pred_mode); |
1593 | 2.72k | av_freep(&s->edge_emu_buffer); |
1594 | 2.72k | av_freep(&s->mb2br_xy); |
1595 | | |
1596 | 2.72k | return 0; |
1597 | 2.72k | } |
1598 | | |
1599 | | const FFCodec ff_svq3_decoder = { |
1600 | | .p.name = "svq3", |
1601 | | CODEC_LONG_NAME("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"), |
1602 | | .p.type = AVMEDIA_TYPE_VIDEO, |
1603 | | .p.id = AV_CODEC_ID_SVQ3, |
1604 | | .priv_data_size = sizeof(SVQ3Context), |
1605 | | .init = svq3_decode_init, |
1606 | | .close = svq3_decode_end, |
1607 | | FF_CODEC_DECODE_CB(svq3_decode_frame), |
1608 | | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | |
1609 | | AV_CODEC_CAP_DR1 | |
1610 | | AV_CODEC_CAP_DELAY, |
1611 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
1612 | | }; |