Commit | Line | Data |
---|---|---|
8b82a956 | 1 | /* |
2912e87a | 2 | * Copyright (c) 2003 The Libav Project |
8b82a956 | 3 | * |
2912e87a | 4 | * This file is part of Libav. |
b78e7197 | 5 | * |
2912e87a | 6 | * Libav is free software; you can redistribute it and/or |
8b82a956 MN |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either | |
b78e7197 | 9 | * version 2.1 of the License, or (at your option) any later version. |
8b82a956 | 10 | * |
2912e87a | 11 | * Libav is distributed in the hope that it will be useful, |
8b82a956 MN |
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 | |
2912e87a | 17 | * License along with Libav; if not, write to the Free Software |
5509bffa | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
e5a389a1 DB |
19 | */ |
20 | ||
21 | /* | |
8b82a956 MN |
22 | * How to use this decoder: |
23 | * SVQ3 data is transported within Apple Quicktime files. Quicktime files | |
89a79364 MM |
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 | |
8b82a956 MN |
30 | * via the AVCodecContext's extradata[_size] field: |
31 | * | |
115329f1 | 32 | * AVCodecContext.extradata = pointer to ImageDescription, first characters |
89a79364 | 33 | * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length |
115329f1 DB |
34 | * AVCodecContext.extradata_size = size of ImageDescription atom memory |
35 | * buffer (which will be the same as the ImageDescription atom size field | |
89a79364 MM |
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: | |
f0a41afd | 40 | * http://samples.libav.org/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov |
8b82a956 | 41 | */ |
bd8ac882 | 42 | |
cc8163e1 DB |
43 | #include <inttypes.h> |
44 | ||
bd8ac882 | 45 | #include "libavutil/attributes.h" |
903d58f6 | 46 | #include "internal.h" |
903d58f6 | 47 | #include "avcodec.h" |
e0c16e4e | 48 | #include "mpegutils.h" |
903d58f6 MN |
49 | #include "h264.h" |
50 | ||
a7d2861d | 51 | #include "h264data.h" // FIXME FIXME FIXME |
903d58f6 | 52 | |
188d3c51 | 53 | #include "h264_mvpred.h" |
903d58f6 | 54 | #include "golomb.h" |
2f6bc5f7 | 55 | #include "hpeldsp.h" |
903d58f6 | 56 | #include "rectangle.h" |
57f09608 | 57 | #include "tpeldsp.h" |
903d58f6 | 58 | |
b250f9c6 | 59 | #if CONFIG_ZLIB |
c4864924 BC |
60 | #include <zlib.h> |
61 | #endif | |
62 | ||
2be3fe39 | 63 | #include "svq1.h" |
75d5156a | 64 | #include "svq3.h" |
2be3fe39 | 65 | |
8b82a956 | 66 | /** |
ba87f080 | 67 | * @file |
8b82a956 MN |
68 | * svq3 decoder. |
69 | */ | |
70 | ||
7f9f771e | 71 | typedef struct SVQ3Context { |
8dfc6d1f | 72 | H264Context h; |
2f6bc5f7 | 73 | HpelDSPContext hdsp; |
57f09608 | 74 | TpelDSPContext tdsp; |
9b749c82 VG |
75 | H264Picture *cur_pic; |
76 | H264Picture *next_pic; | |
77 | H264Picture *last_pic; | |
1098f5c0 LB |
78 | GetBitContext gb; |
79 | uint8_t *slice_buf; | |
80 | int slice_size; | |
8dfc6d1f BC |
81 | int halfpel_flag; |
82 | int thirdpel_flag; | |
83 | int unknown_flag; | |
8dfc6d1f | 84 | uint32_t watermark_key; |
2c541554 AK |
85 | int adaptive_quant; |
86 | int next_p_frame_damaged; | |
87 | int h_edge_pos; | |
88 | int v_edge_pos; | |
89 | int last_frame_output; | |
8dfc6d1f BC |
90 | } SVQ3Context; |
91 | ||
115329f1 DB |
92 | #define FULLPEL_MODE 1 |
93 | #define HALFPEL_MODE 2 | |
94d44f45 | 94 | #define THIRDPEL_MODE 3 |
2e26c8d2 | 95 | #define PREDICT_MODE 4 |
115329f1 | 96 | |
f7a8c179 | 97 | /* dual scan (from some older h264 draft) |
a7d2861d DB |
98 | * o-->o-->o o |
99 | * | /| | |
100 | * o o o / o | |
101 | * | / | |/ | | |
102 | * o o o o | |
103 | * / | |
104 | * o-->o-->o-->o | |
105 | */ | |
76de302d | 106 | static const uint8_t svq3_scan[16] = { |
a7d2861d DB |
107 | 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4, |
108 | 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, | |
109 | 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4, | |
110 | 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4, | |
8b82a956 MN |
111 | }; |
112 | ||
c4e43560 DB |
113 | static const uint8_t luma_dc_zigzag_scan[16] = { |
114 | 0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64, | |
115 | 3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64, | |
116 | 1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64, | |
117 | 3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64, | |
118 | }; | |
119 | ||
8b82a956 | 120 | static const uint8_t svq3_pred_0[25][2] = { |
76de302d DB |
121 | { 0, 0 }, |
122 | { 1, 0 }, { 0, 1 }, | |
123 | { 0, 2 }, { 1, 1 }, { 2, 0 }, | |
124 | { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 }, | |
125 | { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 }, | |
126 | { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 }, | |
127 | { 2, 4 }, { 3, 3 }, { 4, 2 }, | |
128 | { 4, 3 }, { 3, 4 }, | |
129 | { 4, 4 } | |
8b82a956 MN |
130 | }; |
131 | ||
132 | static const int8_t svq3_pred_1[6][6][5] = { | |
a7d2861d DB |
133 | { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, |
134 | { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } }, | |
135 | { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 }, | |
136 | { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } }, | |
137 | { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 }, | |
138 | { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } }, | |
139 | { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 }, | |
140 | { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } }, | |
141 | { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 }, | |
142 | { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } }, | |
143 | { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 }, | |
144 | { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } }, | |
8b82a956 MN |
145 | }; |
146 | ||
a7d2861d DB |
147 | static const struct { |
148 | uint8_t run; | |
149 | uint8_t level; | |
150 | } svq3_dct_tables[2][16] = { | |
76de302d DB |
151 | { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 }, |
152 | { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } }, | |
153 | { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, | |
154 | { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } } | |
8b82a956 MN |
155 | }; |
156 | ||
157 | static const uint32_t svq3_dequant_coeff[32] = { | |
a7d2861d DB |
158 | 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718, |
159 | 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873, | |
160 | 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683, | |
161 | 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533 | |
8b82a956 MN |
162 | }; |
163 | ||
88bd7fdc | 164 | void ff_svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp) |
a7d2861d | 165 | { |
76de302d | 166 | const int qmul = svq3_dequant_coeff[qp]; |
8b82a956 MN |
167 | #define stride 16 |
168 | int i; | |
169 | int temp[16]; | |
a7d2861d DB |
170 | static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride }; |
171 | ||
172 | for (i = 0; i < 4; i++) { | |
173 | const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]); | |
174 | const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]); | |
175 | const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3]; | |
176 | const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3]; | |
177 | ||
178 | temp[4 * i + 0] = z0 + z3; | |
179 | temp[4 * i + 1] = z1 + z2; | |
180 | temp[4 * i + 2] = z1 - z2; | |
181 | temp[4 * i + 3] = z0 - z3; | |
8b82a956 MN |
182 | } |
183 | ||
a7d2861d DB |
184 | for (i = 0; i < 4; i++) { |
185 | const int offset = x_offset[i]; | |
186 | const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]); | |
187 | const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]); | |
188 | const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i]; | |
189 | const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i]; | |
190 | ||
af1ede06 DB |
191 | output[stride * 0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20; |
192 | output[stride * 2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20; | |
193 | output[stride * 8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20; | |
194 | output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20; | |
8b82a956 MN |
195 | } |
196 | } | |
197 | #undef stride | |
198 | ||
88bd7fdc | 199 | void ff_svq3_add_idct_c(uint8_t *dst, int16_t *block, |
a7d2861d | 200 | int stride, int qp, int dc) |
7f8205da | 201 | { |
76de302d | 202 | const int qmul = svq3_dequant_coeff[qp]; |
8b82a956 | 203 | int i; |
8b82a956 MN |
204 | |
205 | if (dc) { | |
af1ede06 DB |
206 | dc = 13 * 13 * (dc == 1 ? 1538 * block[0] |
207 | : qmul * (block[0] >> 3) / 2); | |
8b82a956 MN |
208 | block[0] = 0; |
209 | } | |
210 | ||
76de302d | 211 | for (i = 0; i < 4; i++) { |
a7d2861d DB |
212 | const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]); |
213 | const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]); | |
214 | const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i]; | |
215 | const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i]; | |
216 | ||
217 | block[0 + 4 * i] = z0 + z3; | |
218 | block[1 + 4 * i] = z1 + z2; | |
219 | block[2 + 4 * i] = z1 - z2; | |
220 | block[3 + 4 * i] = z0 - z3; | |
8b82a956 MN |
221 | } |
222 | ||
76de302d | 223 | for (i = 0; i < 4; i++) { |
a7d2861d DB |
224 | const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]); |
225 | const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]); | |
226 | const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3]; | |
227 | const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3]; | |
76de302d DB |
228 | const int rr = (dc + 0x80000); |
229 | ||
af1ede06 DB |
230 | dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20)); |
231 | dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20)); | |
232 | dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20)); | |
233 | dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20)); | |
8b82a956 | 234 | } |
62844c3f RB |
235 | |
236 | memset(block, 0, 16 * sizeof(int16_t)); | |
8b82a956 MN |
237 | } |
238 | ||
88bd7fdc | 239 | static inline int svq3_decode_block(GetBitContext *gb, int16_t *block, |
7f8205da DB |
240 | int index, const int type) |
241 | { | |
76de302d DB |
242 | static const uint8_t *const scan_patterns[4] = |
243 | { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan }; | |
8b82a956 | 244 | |
9a2e7911 JG |
245 | int run, level, limit; |
246 | unsigned vlc; | |
af1ede06 | 247 | const int intra = 3 * type >> 2; |
76de302d | 248 | const uint8_t *const scan = scan_patterns[type]; |
8b82a956 | 249 | |
76de302d DB |
250 | for (limit = (16 >> intra); index < 16; index = limit, limit += 8) { |
251 | for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) { | |
9a2e7911 JG |
252 | int sign = (vlc & 1) ? 0 : -1; |
253 | vlc = vlc + 1 >> 1; | |
a7d2861d DB |
254 | |
255 | if (type == 3) { | |
256 | if (vlc < 3) { | |
257 | run = 0; | |
258 | level = vlc; | |
259 | } else if (vlc < 4) { | |
260 | run = 1; | |
261 | level = 1; | |
262 | } else { | |
af1ede06 DB |
263 | run = vlc & 0x3; |
264 | level = (vlc + 9 >> 2) - run; | |
a7d2861d DB |
265 | } |
266 | } else { | |
267 | if (vlc < 16) { | |
268 | run = svq3_dct_tables[intra][vlc].run; | |
269 | level = svq3_dct_tables[intra][vlc].level; | |
270 | } else if (intra) { | |
af1ede06 | 271 | run = vlc & 0x7; |
a7d2861d DB |
272 | level = (vlc >> 3) + |
273 | ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1))); | |
274 | } else { | |
af1ede06 | 275 | run = vlc & 0xF; |
a7d2861d DB |
276 | level = (vlc >> 4) + |
277 | ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0))); | |
278 | } | |
279 | } | |
8b82a956 | 280 | |
a7d2861d DB |
281 | if ((index += run) >= limit) |
282 | return -1; | |
283 | ||
284 | block[scan[index]] = (level ^ sign) - sign; | |
76de302d | 285 | } |
8b82a956 | 286 | |
76de302d DB |
287 | if (type != 2) { |
288 | break; | |
289 | } | |
8b82a956 | 290 | } |
8b82a956 | 291 | |
76de302d | 292 | return 0; |
8b82a956 MN |
293 | } |
294 | ||
2c541554 | 295 | static inline void svq3_mc_dir_part(SVQ3Context *s, |
7f8205da DB |
296 | int x, int y, int width, int height, |
297 | int mx, int my, int dxy, | |
298 | int thirdpel, int dir, int avg) | |
299 | { | |
9b749c82 | 300 | H264Context *h = &s->h; |
36d04801 | 301 | H264SliceContext *sl = &h->slice_ctx[0]; |
9b749c82 | 302 | const H264Picture *pic = (dir == 0) ? s->last_pic : s->next_pic; |
76de302d DB |
303 | uint8_t *src, *dest; |
304 | int i, emu = 0; | |
a7d2861d | 305 | int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2 |
76de302d DB |
306 | |
307 | mx += x; | |
308 | my += y; | |
8b82a956 | 309 | |
af1ede06 DB |
310 | if (mx < 0 || mx >= s->h_edge_pos - width - 1 || |
311 | my < 0 || my >= s->v_edge_pos - height - 1) { | |
fae6fd5b | 312 | emu = 1; |
af1ede06 DB |
313 | mx = av_clip(mx, -16, s->h_edge_pos - width + 15); |
314 | my = av_clip(my, -16, s->v_edge_pos - height + 15); | |
8b82a956 MN |
315 | } |
316 | ||
76de302d | 317 | /* form component predictions */ |
a0f29460 AK |
318 | dest = h->cur_pic.f->data[0] + x + y * sl->linesize; |
319 | src = pic->f->data[0] + mx + my * sl->linesize; | |
76de302d DB |
320 | |
321 | if (emu) { | |
36d04801 | 322 | h->vdsp.emulated_edge_mc(sl->edge_emu_buffer, src, |
c28ed1d7 | 323 | sl->linesize, sl->linesize, |
8c53d39e RB |
324 | width + 1, height + 1, |
325 | mx, my, s->h_edge_pos, s->v_edge_pos); | |
36d04801 | 326 | src = sl->edge_emu_buffer; |
8b82a956 | 327 | } |
76de302d | 328 | if (thirdpel) |
57f09608 | 329 | (avg ? s->tdsp.avg_tpel_pixels_tab |
c28ed1d7 | 330 | : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, sl->linesize, |
57f09608 | 331 | width, height); |
76de302d | 332 | else |
2f6bc5f7 | 333 | (avg ? s->hdsp.avg_pixels_tab |
c28ed1d7 | 334 | : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, sl->linesize, |
2f6bc5f7 | 335 | height); |
76de302d | 336 | |
7c6eb0a1 | 337 | if (!(h->flags & AV_CODEC_FLAG_GRAY)) { |
af1ede06 DB |
338 | mx = mx + (mx < (int) x) >> 1; |
339 | my = my + (my < (int) y) >> 1; | |
340 | width = width >> 1; | |
341 | height = height >> 1; | |
76de302d DB |
342 | blocksize++; |
343 | ||
344 | for (i = 1; i < 3; i++) { | |
a0f29460 AK |
345 | dest = h->cur_pic.f->data[i] + (x >> 1) + (y >> 1) * sl->uvlinesize; |
346 | src = pic->f->data[i] + mx + my * sl->uvlinesize; | |
76de302d DB |
347 | |
348 | if (emu) { | |
36d04801 | 349 | h->vdsp.emulated_edge_mc(sl->edge_emu_buffer, src, |
c28ed1d7 | 350 | sl->uvlinesize, sl->uvlinesize, |
8c53d39e RB |
351 | width + 1, height + 1, |
352 | mx, my, (s->h_edge_pos >> 1), | |
353 | s->v_edge_pos >> 1); | |
36d04801 | 354 | src = sl->edge_emu_buffer; |
76de302d DB |
355 | } |
356 | if (thirdpel) | |
57f09608 DB |
357 | (avg ? s->tdsp.avg_tpel_pixels_tab |
358 | : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, | |
c28ed1d7 | 359 | sl->uvlinesize, |
57f09608 | 360 | width, height); |
76de302d | 361 | else |
2f6bc5f7 RB |
362 | (avg ? s->hdsp.avg_pixels_tab |
363 | : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, | |
c28ed1d7 | 364 | sl->uvlinesize, |
2f6bc5f7 | 365 | height); |
76de302d DB |
366 | } |
367 | } | |
8b82a956 MN |
368 | } |
369 | ||
2c541554 | 370 | static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode, |
a7d2861d | 371 | int dir, int avg) |
7f8205da | 372 | { |
76de302d | 373 | int i, j, k, mx, my, dx, dy, x, y; |
2c541554 | 374 | H264Context *h = &s->h; |
8b00f4df | 375 | H264SliceContext *sl = &h->slice_ctx[0]; |
a7d2861d DB |
376 | const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1); |
377 | const int part_height = 16 >> ((unsigned)(size + 1) / 3); | |
378 | const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0; | |
379 | const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width; | |
380 | const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width; | |
381 | ||
382 | for (i = 0; i < 16; i += part_height) | |
76de302d | 383 | for (j = 0; j < 16; j += part_width) { |
d4d9068c AK |
384 | const int b_xy = (4 * sl->mb_x + (j >> 2)) + |
385 | (4 * sl->mb_y + (i >> 2)) * h->b_stride; | |
76de302d | 386 | int dxy; |
d4d9068c AK |
387 | x = 16 * sl->mb_x + j; |
388 | y = 16 * sl->mb_y + i; | |
af1ede06 DB |
389 | k = (j >> 2 & 1) + (i >> 1 & 2) + |
390 | (j >> 1 & 4) + (i & 8); | |
76de302d DB |
391 | |
392 | if (mode != PREDICT_MODE) { | |
8b00f4df | 393 | pred_motion(h, sl, k, part_width >> 2, dir, 1, &mx, &my); |
76de302d | 394 | } else { |
759001c5 AK |
395 | mx = s->next_pic->motion_val[0][b_xy][0] << 1; |
396 | my = s->next_pic->motion_val[0][b_xy][1] << 1; | |
76de302d DB |
397 | |
398 | if (dir == 0) { | |
af1ede06 DB |
399 | mx = mx * h->frame_num_offset / |
400 | h->prev_frame_num_offset + 1 >> 1; | |
401 | my = my * h->frame_num_offset / | |
402 | h->prev_frame_num_offset + 1 >> 1; | |
76de302d | 403 | } else { |
af1ede06 DB |
404 | mx = mx * (h->frame_num_offset - h->prev_frame_num_offset) / |
405 | h->prev_frame_num_offset + 1 >> 1; | |
406 | my = my * (h->frame_num_offset - h->prev_frame_num_offset) / | |
407 | h->prev_frame_num_offset + 1 >> 1; | |
76de302d DB |
408 | } |
409 | } | |
410 | ||
411 | /* clip motion vector prediction to frame border */ | |
a7d2861d DB |
412 | mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x); |
413 | my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y); | |
76de302d DB |
414 | |
415 | /* get (optional) motion vector differential */ | |
416 | if (mode == PREDICT_MODE) { | |
417 | dx = dy = 0; | |
418 | } else { | |
2c541554 AK |
419 | dy = svq3_get_se_golomb(&h->gb); |
420 | dx = svq3_get_se_golomb(&h->gb); | |
76de302d DB |
421 | |
422 | if (dx == INVALID_VLC || dy == INVALID_VLC) { | |
2c541554 | 423 | av_log(h->avctx, AV_LOG_ERROR, "invalid MV vlc\n"); |
76de302d DB |
424 | return -1; |
425 | } | |
426 | } | |
427 | ||
428 | /* compute motion vector */ | |
429 | if (mode == THIRDPEL_MODE) { | |
430 | int fx, fy; | |
af1ede06 DB |
431 | mx = (mx + 1 >> 1) + dx; |
432 | my = (my + 1 >> 1) + dy; | |
433 | fx = (unsigned)(mx + 0x3000) / 3 - 0x1000; | |
434 | fy = (unsigned)(my + 0x3000) / 3 - 0x1000; | |
a7d2861d DB |
435 | dxy = (mx - 3 * fx) + 4 * (my - 3 * fy); |
436 | ||
437 | svq3_mc_dir_part(s, x, y, part_width, part_height, | |
438 | fx, fy, dxy, 1, dir, avg); | |
76de302d DB |
439 | mx += mx; |
440 | my += my; | |
441 | } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) { | |
af1ede06 DB |
442 | mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000; |
443 | my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000; | |
a7d2861d | 444 | dxy = (mx & 1) + 2 * (my & 1); |
76de302d | 445 | |
a7d2861d DB |
446 | svq3_mc_dir_part(s, x, y, part_width, part_height, |
447 | mx >> 1, my >> 1, dxy, 0, dir, avg); | |
76de302d DB |
448 | mx *= 3; |
449 | my *= 3; | |
450 | } else { | |
af1ede06 DB |
451 | mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000; |
452 | my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000; | |
76de302d | 453 | |
a7d2861d DB |
454 | svq3_mc_dir_part(s, x, y, part_width, part_height, |
455 | mx, my, 0, 0, dir, avg); | |
76de302d DB |
456 | mx *= 6; |
457 | my *= 6; | |
458 | } | |
459 | ||
460 | /* update mv_cache */ | |
461 | if (mode != PREDICT_MODE) { | |
a7d2861d | 462 | int32_t mv = pack16to32(mx, my); |
76de302d DB |
463 | |
464 | if (part_height == 8 && i < 8) { | |
e6287f07 | 465 | AV_WN32A(sl->mv_cache[dir][scan8[k] + 1 * 8], mv); |
76de302d | 466 | |
a7d2861d | 467 | if (part_width == 8 && j < 8) |
e6287f07 | 468 | AV_WN32A(sl->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv); |
76de302d | 469 | } |
a7d2861d | 470 | if (part_width == 8 && j < 8) |
e6287f07 | 471 | AV_WN32A(sl->mv_cache[dir][scan8[k] + 1], mv); |
a7d2861d | 472 | if (part_width == 4 || part_height == 4) |
e6287f07 | 473 | AV_WN32A(sl->mv_cache[dir][scan8[k]], mv); |
76de302d DB |
474 | } |
475 | ||
476 | /* write back motion vectors */ | |
759001c5 | 477 | fill_rectangle(h->cur_pic.motion_val[dir][b_xy], |
657ccb5a DB |
478 | part_width >> 2, part_height >> 2, h->b_stride, |
479 | pack16to32(mx, my), 4); | |
bb270c08 | 480 | } |
2e26c8d2 | 481 | |
76de302d | 482 | return 0; |
2e26c8d2 MM |
483 | } |
484 | ||
2c541554 | 485 | static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type) |
7f8205da | 486 | { |
2c541554 | 487 | H264Context *h = &s->h; |
d231e84b | 488 | H264SliceContext *sl = &h->slice_ctx[0]; |
76de302d DB |
489 | int i, j, k, m, dir, mode; |
490 | int cbp = 0; | |
491 | uint32_t vlc; | |
492 | int8_t *top, *left; | |
0edbe6fa | 493 | const int mb_xy = sl->mb_xy; |
d4d9068c | 494 | const int b_xy = 4 * sl->mb_x + 4 * sl->mb_y * h->b_stride; |
76de302d | 495 | |
d4d9068c AK |
496 | sl->top_samples_available = (sl->mb_y == 0) ? 0x33FF : 0xFFFF; |
497 | sl->left_samples_available = (sl->mb_x == 0) ? 0x5F5F : 0xFFFF; | |
64c81b2c | 498 | sl->topright_samples_available = 0xFFFF; |
76de302d DB |
499 | |
500 | if (mb_type == 0) { /* SKIP */ | |
2c541554 | 501 | if (h->pict_type == AV_PICTURE_TYPE_P || |
759001c5 | 502 | s->next_pic->mb_type[mb_xy] == -1) { |
d4d9068c | 503 | svq3_mc_dir_part(s, 16 * sl->mb_x, 16 * sl->mb_y, 16, 16, |
a7d2861d | 504 | 0, 0, 0, 0, 0, 0); |
76de302d | 505 | |
2c541554 | 506 | if (h->pict_type == AV_PICTURE_TYPE_B) |
d4d9068c | 507 | svq3_mc_dir_part(s, 16 * sl->mb_x, 16 * sl->mb_y, 16, 16, |
a7d2861d | 508 | 0, 0, 0, 0, 1, 1); |
76de302d DB |
509 | |
510 | mb_type = MB_TYPE_SKIP; | |
511 | } else { | |
759001c5 | 512 | mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6); |
2c541554 | 513 | if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0) |
76de302d | 514 | return -1; |
2c541554 | 515 | if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0) |
76de302d | 516 | return -1; |
8b82a956 | 517 | |
76de302d | 518 | mb_type = MB_TYPE_16x16; |
bb270c08 | 519 | } |
76de302d | 520 | } else if (mb_type < 8) { /* INTER */ |
2c541554 | 521 | if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&h->gb)) |
76de302d | 522 | mode = THIRDPEL_MODE; |
2c541554 AK |
523 | else if (s->halfpel_flag && |
524 | s->thirdpel_flag == !get_bits1(&h->gb)) | |
76de302d | 525 | mode = HALFPEL_MODE; |
a7d2861d | 526 | else |
76de302d | 527 | mode = FULLPEL_MODE; |
8b82a956 | 528 | |
76de302d DB |
529 | /* fill caches */ |
530 | /* note ref_cache should contain here: | |
a7d2861d DB |
531 | * ???????? |
532 | * ???11111 | |
533 | * N??11111 | |
534 | * N??11111 | |
535 | * N??11111 | |
536 | */ | |
76de302d DB |
537 | |
538 | for (m = 0; m < 2; m++) { | |
d4d9068c | 539 | if (sl->mb_x > 0 && sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) { |
a7d2861d | 540 | for (i = 0; i < 4; i++) |
e6287f07 | 541 | AV_COPY32(sl->mv_cache[m][scan8[0] - 1 + i * 8], |
759001c5 | 542 | h->cur_pic.motion_val[m][b_xy - 1 + i * h->b_stride]); |
76de302d | 543 | } else { |
a7d2861d | 544 | for (i = 0; i < 4; i++) |
e6287f07 | 545 | AV_ZERO32(sl->mv_cache[m][scan8[0] - 1 + i * 8]); |
76de302d | 546 | } |
d4d9068c | 547 | if (sl->mb_y > 0) { |
e6287f07 | 548 | memcpy(sl->mv_cache[m][scan8[0] - 1 * 8], |
759001c5 | 549 | h->cur_pic.motion_val[m][b_xy - h->b_stride], |
a7d2861d | 550 | 4 * 2 * sizeof(int16_t)); |
e6287f07 | 551 | memset(&sl->ref_cache[m][scan8[0] - 1 * 8], |
7d8154ed | 552 | (sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4); |
76de302d | 553 | |
d4d9068c | 554 | if (sl->mb_x < h->mb_width - 1) { |
e6287f07 | 555 | AV_COPY32(sl->mv_cache[m][scan8[0] + 4 - 1 * 8], |
759001c5 | 556 | h->cur_pic.motion_val[m][b_xy - h->b_stride + 4]); |
e6287f07 | 557 | sl->ref_cache[m][scan8[0] + 4 - 1 * 8] = |
7d8154ed AK |
558 | (sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride + 1] + 6] == -1 || |
559 | sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1; | |
a7d2861d | 560 | } else |
e6287f07 | 561 | sl->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE; |
d4d9068c | 562 | if (sl->mb_x > 0) { |
e6287f07 | 563 | AV_COPY32(sl->mv_cache[m][scan8[0] - 1 - 1 * 8], |
759001c5 | 564 | h->cur_pic.motion_val[m][b_xy - h->b_stride - 1]); |
e6287f07 | 565 | sl->ref_cache[m][scan8[0] - 1 - 1 * 8] = |
7d8154ed | 566 | (sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1; |
a7d2861d | 567 | } else |
e6287f07 | 568 | sl->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE; |
a7d2861d | 569 | } else |
e6287f07 | 570 | memset(&sl->ref_cache[m][scan8[0] - 1 * 8 - 1], |
a7d2861d | 571 | PART_NOT_AVAILABLE, 8); |
76de302d | 572 | |
2c541554 | 573 | if (h->pict_type != AV_PICTURE_TYPE_B) |
76de302d | 574 | break; |
bb270c08 | 575 | } |
8b82a956 | 576 | |
76de302d | 577 | /* decode motion vector(s) and form prediction(s) */ |
2c541554 AK |
578 | if (h->pict_type == AV_PICTURE_TYPE_P) { |
579 | if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0) | |
76de302d | 580 | return -1; |
975a1447 | 581 | } else { /* AV_PICTURE_TYPE_B */ |
7f3624dc | 582 | if (mb_type != 2) { |
2c541554 | 583 | if (svq3_mc_dir(s, 0, mode, 0, 0) < 0) |
76de302d | 584 | return -1; |
7f3624dc | 585 | } else { |
a7d2861d | 586 | for (i = 0; i < 4; i++) |
759001c5 | 587 | memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride], |
a7d2861d | 588 | 0, 4 * 2 * sizeof(int16_t)); |
7f3624dc MH |
589 | } |
590 | if (mb_type != 1) { | |
2c541554 | 591 | if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0) |
76de302d | 592 | return -1; |
7f3624dc | 593 | } else { |
a7d2861d | 594 | for (i = 0; i < 4; i++) |
759001c5 | 595 | memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride], |
a7d2861d | 596 | 0, 4 * 2 * sizeof(int16_t)); |
7f3624dc | 597 | } |
bb270c08 | 598 | } |
8b82a956 | 599 | |
76de302d DB |
600 | mb_type = MB_TYPE_16x16; |
601 | } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */ | |
7d8154ed | 602 | memset(sl->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t)); |
76de302d DB |
603 | |
604 | if (mb_type == 8) { | |
d4d9068c | 605 | if (sl->mb_x > 0) { |
a7d2861d | 606 | for (i = 0; i < 4; i++) |
7d8154ed AK |
607 | sl->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i]; |
608 | if (sl->intra4x4_pred_mode_cache[scan8[0] - 1] == -1) | |
64c81b2c | 609 | sl->left_samples_available = 0x5F5F; |
76de302d | 610 | } |
d4d9068c | 611 | if (sl->mb_y > 0) { |
7d8154ed AK |
612 | sl->intra4x4_pred_mode_cache[4 + 8 * 0] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 0]; |
613 | sl->intra4x4_pred_mode_cache[5 + 8 * 0] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 1]; | |
614 | sl->intra4x4_pred_mode_cache[6 + 8 * 0] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 2]; | |
615 | sl->intra4x4_pred_mode_cache[7 + 8 * 0] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 3]; | |
76de302d | 616 | |
7d8154ed | 617 | if (sl->intra4x4_pred_mode_cache[4 + 8 * 0] == -1) |
64c81b2c | 618 | sl->top_samples_available = 0x33FF; |
76de302d DB |
619 | } |
620 | ||
621 | /* decode prediction codes for luma blocks */ | |
a7d2861d | 622 | for (i = 0; i < 16; i += 2) { |
2c541554 | 623 | vlc = svq3_get_ue_golomb(&h->gb); |
76de302d | 624 | |
a7d2861d | 625 | if (vlc >= 25) { |
cc8163e1 DB |
626 | av_log(h->avctx, AV_LOG_ERROR, |
627 | "luma prediction:%"PRIu32"\n", vlc); | |
76de302d DB |
628 | return -1; |
629 | } | |
630 | ||
7d8154ed AK |
631 | left = &sl->intra4x4_pred_mode_cache[scan8[i] - 1]; |
632 | top = &sl->intra4x4_pred_mode_cache[scan8[i] - 8]; | |
76de302d DB |
633 | |
634 | left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]]; | |
635 | left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]]; | |
636 | ||
a7d2861d | 637 | if (left[1] == -1 || left[2] == -1) { |
2c541554 | 638 | av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n"); |
76de302d DB |
639 | return -1; |
640 | } | |
641 | } | |
642 | } else { /* mb_type == 33, DC_128_PRED block type */ | |
a7d2861d | 643 | for (i = 0; i < 4; i++) |
7d8154ed | 644 | memset(&sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4); |
884182b3 | 645 | } |
8b82a956 | 646 | |
7d8154ed | 647 | write_back_intra_pred_mode(h, sl); |
8b82a956 | 648 | |
76de302d | 649 | if (mb_type == 8) { |
7d8154ed | 650 | ff_h264_check_intra4x4_pred_mode(h, sl); |
da3b9756 | 651 | |
d4d9068c AK |
652 | sl->top_samples_available = (sl->mb_y == 0) ? 0x33FF : 0xFFFF; |
653 | sl->left_samples_available = (sl->mb_x == 0) ? 0x5F5F : 0xFFFF; | |
76de302d | 654 | } else { |
a7d2861d | 655 | for (i = 0; i < 4; i++) |
7d8154ed | 656 | memset(&sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4); |
da3b9756 | 657 | |
64c81b2c AK |
658 | sl->top_samples_available = 0x33FF; |
659 | sl->left_samples_available = 0x5F5F; | |
76de302d | 660 | } |
da3b9756 | 661 | |
76de302d DB |
662 | mb_type = MB_TYPE_INTRA4x4; |
663 | } else { /* INTRA16x16 */ | |
664 | dir = i_mb_type_info[mb_type - 8].pred_mode; | |
a7d2861d | 665 | dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1; |
8b82a956 | 666 | |
64c81b2c | 667 | if ((sl->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, sl, dir, 0)) < 0) { |
1115689d | 668 | av_log(h->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n"); |
4bd5ac20 | 669 | return sl->intra16x16_pred_mode; |
76de302d | 670 | } |
8b82a956 | 671 | |
a7d2861d | 672 | cbp = i_mb_type_info[mb_type - 8].cbp; |
76de302d | 673 | mb_type = MB_TYPE_INTRA16x16; |
884182b3 | 674 | } |
8b82a956 | 675 | |
2c541554 | 676 | if (!IS_INTER(mb_type) && h->pict_type != AV_PICTURE_TYPE_I) { |
a7d2861d | 677 | for (i = 0; i < 4; i++) |
759001c5 | 678 | memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride], |
a7d2861d | 679 | 0, 4 * 2 * sizeof(int16_t)); |
2c541554 | 680 | if (h->pict_type == AV_PICTURE_TYPE_B) { |
a7d2861d | 681 | for (i = 0; i < 4; i++) |
759001c5 | 682 | memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride], |
a7d2861d | 683 | 0, 4 * 2 * sizeof(int16_t)); |
76de302d | 684 | } |
8b82a956 | 685 | } |
76de302d | 686 | if (!IS_INTRA4x4(mb_type)) { |
7d8154ed | 687 | memset(sl->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8); |
da3b9756 | 688 | } |
2c541554 | 689 | if (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B) { |
f69574cf | 690 | memset(sl->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t)); |
884182b3 | 691 | } |
2e26c8d2 | 692 | |
a7d2861d | 693 | if (!IS_INTRA16x16(mb_type) && |
2c541554 AK |
694 | (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B)) { |
695 | if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48) { | |
cc8163e1 | 696 | av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc); |
76de302d DB |
697 | return -1; |
698 | } | |
8b82a956 | 699 | |
a7d2861d DB |
700 | cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] |
701 | : golomb_to_inter_cbp[vlc]; | |
884182b3 | 702 | } |
a7d2861d | 703 | if (IS_INTRA16x16(mb_type) || |
2c541554 | 704 | (h->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) { |
d231e84b | 705 | sl->qscale += svq3_get_se_golomb(&h->gb); |
8b82a956 | 706 | |
d231e84b AK |
707 | if (sl->qscale > 31u) { |
708 | av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", sl->qscale); | |
bb270c08 | 709 | return -1; |
bb270c08 | 710 | } |
8b82a956 | 711 | } |
76de302d | 712 | if (IS_INTRA16x16(mb_type)) { |
bf03a878 AK |
713 | AV_ZERO128(sl->mb_luma_dc[0] + 0); |
714 | AV_ZERO128(sl->mb_luma_dc[0] + 8); | |
715 | if (svq3_decode_block(&h->gb, sl->mb_luma_dc[0], 0, 1)) { | |
2c541554 | 716 | av_log(h->avctx, AV_LOG_ERROR, |
a7d2861d | 717 | "error while decoding intra luma dc\n"); |
76de302d | 718 | return -1; |
884182b3 | 719 | } |
76de302d | 720 | } |
8b82a956 | 721 | |
76de302d DB |
722 | if (cbp) { |
723 | const int index = IS_INTRA16x16(mb_type) ? 1 : 0; | |
d231e84b | 724 | const int type = ((sl->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1); |
76de302d | 725 | |
a7d2861d | 726 | for (i = 0; i < 4; i++) |
76de302d DB |
727 | if ((cbp & (1 << i))) { |
728 | for (j = 0; j < 4; j++) { | |
a7d2861d DB |
729 | k = index ? (1 * (j & 1) + 2 * (i & 1) + |
730 | 2 * (j & 2) + 4 * (i & 2)) | |
731 | : (4 * i + j); | |
f69574cf | 732 | sl->non_zero_count_cache[scan8[k]] = 1; |
a7d2861d | 733 | |
bf03a878 | 734 | if (svq3_decode_block(&h->gb, &sl->mb[16 * k], index, type)) { |
2c541554 | 735 | av_log(h->avctx, AV_LOG_ERROR, |
a7d2861d | 736 | "error while decoding block\n"); |
76de302d DB |
737 | return -1; |
738 | } | |
739 | } | |
740 | } | |
8b82a956 | 741 | |
76de302d | 742 | if ((cbp & 0x30)) { |
a7d2861d | 743 | for (i = 1; i < 3; ++i) |
bf03a878 | 744 | if (svq3_decode_block(&h->gb, &sl->mb[16 * 16 * i], 0, 3)) { |
2c541554 | 745 | av_log(h->avctx, AV_LOG_ERROR, |
a7d2861d DB |
746 | "error while decoding chroma dc block\n"); |
747 | return -1; | |
748 | } | |
76de302d DB |
749 | |
750 | if ((cbp & 0x20)) { | |
11177a4d JGG |
751 | for (i = 1; i < 3; i++) { |
752 | for (j = 0; j < 4; j++) { | |
a7d2861d | 753 | k = 16 * i + j; |
f69574cf | 754 | sl->non_zero_count_cache[scan8[k]] = 1; |
11177a4d | 755 | |
bf03a878 | 756 | if (svq3_decode_block(&h->gb, &sl->mb[16 * k], 1, 1)) { |
2c541554 | 757 | av_log(h->avctx, AV_LOG_ERROR, |
a7d2861d | 758 | "error while decoding chroma ac block\n"); |
11177a4d JGG |
759 | return -1; |
760 | } | |
76de302d DB |
761 | } |
762 | } | |
763 | } | |
bb270c08 | 764 | } |
8b82a956 | 765 | } |
8b82a956 | 766 | |
e7226984 | 767 | sl->cbp = cbp; |
759001c5 | 768 | h->cur_pic.mb_type[mb_xy] = mb_type; |
8b82a956 | 769 | |
a7d2861d | 770 | if (IS_INTRA(mb_type)) |
64c81b2c | 771 | sl->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, sl, DC_PRED8x8, 1); |
8b82a956 | 772 | |
76de302d | 773 | return 0; |
8b82a956 MN |
774 | } |
775 | ||
8dfc6d1f | 776 | static int svq3_decode_slice_header(AVCodecContext *avctx) |
7f8205da | 777 | { |
2c541554 AK |
778 | SVQ3Context *s = avctx->priv_data; |
779 | H264Context *h = &s->h; | |
d231e84b | 780 | H264SliceContext *sl = &h->slice_ctx[0]; |
0edbe6fa | 781 | const int mb_xy = sl->mb_xy; |
76de302d | 782 | int i, header; |
288bb3da | 783 | unsigned slice_id; |
da3b9756 | 784 | |
1098f5c0 | 785 | header = get_bits(&s->gb, 8); |
da3b9756 | 786 | |
76de302d DB |
787 | if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) { |
788 | /* TODO: what? */ | |
8dfc6d1f | 789 | av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header); |
76de302d DB |
790 | return -1; |
791 | } else { | |
1098f5c0 | 792 | int slice_bits, slice_bytes, slice_length; |
af1ede06 | 793 | int length = header >> 5 & 3; |
da3b9756 | 794 | |
1098f5c0 LB |
795 | slice_length = show_bits(&s->gb, 8 * length); |
796 | slice_bits = slice_length * 8; | |
797 | slice_bytes = slice_length + length - 1; | |
da3b9756 | 798 | |
1098f5c0 | 799 | if (slice_bytes > get_bits_left(&s->gb)) { |
8dfc6d1f | 800 | av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n"); |
76de302d | 801 | return -1; |
a7d2861d | 802 | } |
da3b9756 | 803 | |
1098f5c0 LB |
804 | skip_bits(&s->gb, 8); |
805 | ||
806 | av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE); | |
807 | if (!s->slice_buf) | |
808 | return AVERROR(ENOMEM); | |
809 | ||
810 | memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes); | |
811 | ||
812 | init_get_bits(&h->gb, s->slice_buf, slice_bits); | |
da3b9756 | 813 | |
2c541554 | 814 | if (s->watermark_key) { |
1098f5c0 LB |
815 | uint32_t header = AV_RL32(&h->gb.buffer[1]); |
816 | AV_WL32(&h->gb.buffer[1], header ^ s->watermark_key); | |
76de302d DB |
817 | } |
818 | if (length > 0) { | |
1098f5c0 | 819 | memcpy(s->slice_buf, &s->slice_buf[slice_length], length - 1); |
76de302d | 820 | } |
1098f5c0 | 821 | skip_bits_long(&s->gb, slice_bytes * 8); |
da3b9756 | 822 | } |
da3b9756 | 823 | |
2c541554 | 824 | if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) { |
cc8163e1 | 825 | av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id); |
76de302d DB |
826 | return -1; |
827 | } | |
da3b9756 | 828 | |
56febc99 | 829 | sl->slice_type = golomb_to_pict_type[slice_id]; |
da3b9756 | 830 | |
76de302d | 831 | if ((header & 0x9F) == 2) { |
2c541554 | 832 | i = (h->mb_num < 64) ? 6 : (1 + av_log2(h->mb_num - 1)); |
47a0d393 | 833 | sl->mb_skip_run = get_bits(&h->gb, i) - |
d4d9068c | 834 | (sl->mb_y * h->mb_width + sl->mb_x); |
76de302d | 835 | } else { |
2c541554 | 836 | skip_bits1(&h->gb); |
47a0d393 | 837 | sl->mb_skip_run = 0; |
76de302d | 838 | } |
da3b9756 | 839 | |
56febc99 AK |
840 | sl->slice_num = get_bits(&h->gb, 8); |
841 | sl->qscale = get_bits(&h->gb, 5); | |
2c541554 | 842 | s->adaptive_quant = get_bits1(&h->gb); |
da3b9756 | 843 | |
76de302d | 844 | /* unknown fields */ |
2c541554 | 845 | skip_bits1(&h->gb); |
da3b9756 | 846 | |
2c541554 AK |
847 | if (s->unknown_flag) |
848 | skip_bits1(&h->gb); | |
da3b9756 | 849 | |
2c541554 AK |
850 | skip_bits1(&h->gb); |
851 | skip_bits(&h->gb, 2); | |
da3b9756 | 852 | |
2c541554 AK |
853 | while (get_bits1(&h->gb)) |
854 | skip_bits(&h->gb, 8); | |
da3b9756 | 855 | |
76de302d | 856 | /* reset intra predictors and invalidate motion vector references */ |
d4d9068c | 857 | if (sl->mb_x > 0) { |
7d8154ed | 858 | memset(sl->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3, |
a7d2861d | 859 | -1, 4 * sizeof(int8_t)); |
d4d9068c AK |
860 | memset(sl->intra4x4_pred_mode + h->mb2br_xy[mb_xy - sl->mb_x], |
861 | -1, 8 * sizeof(int8_t) * sl->mb_x); | |
76de302d | 862 | } |
d4d9068c | 863 | if (sl->mb_y > 0) { |
7d8154ed | 864 | memset(sl->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_stride], |
d4d9068c | 865 | -1, 8 * sizeof(int8_t) * (h->mb_width - sl->mb_x)); |
76de302d | 866 | |
d4d9068c | 867 | if (sl->mb_x > 0) |
7d8154ed | 868 | sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] = -1; |
da3b9756 | 869 | } |
da3b9756 | 870 | |
76de302d | 871 | return 0; |
da3b9756 MM |
872 | } |
873 | ||
5ef251e5 | 874 | static av_cold int svq3_decode_init(AVCodecContext *avctx) |
7f8205da | 875 | { |
2c541554 AK |
876 | SVQ3Context *s = avctx->priv_data; |
877 | H264Context *h = &s->h; | |
07c5ca55 | 878 | H264SliceContext *sl; |
f4cca718 | 879 | int m; |
76de302d | 880 | unsigned char *extradata; |
9e1db721 | 881 | unsigned char *extradata_end; |
76de302d | 882 | unsigned int size; |
9e1db721 | 883 | int marker_found = 0; |
76de302d | 884 | |
2c541554 AK |
885 | s->cur_pic = av_mallocz(sizeof(*s->cur_pic)); |
886 | s->last_pic = av_mallocz(sizeof(*s->last_pic)); | |
887 | s->next_pic = av_mallocz(sizeof(*s->next_pic)); | |
888 | if (!s->next_pic || !s->last_pic || !s->cur_pic) { | |
889 | av_freep(&s->cur_pic); | |
890 | av_freep(&s->last_pic); | |
891 | av_freep(&s->next_pic); | |
892 | return AVERROR(ENOMEM); | |
893 | } | |
894 | ||
a0f29460 AK |
895 | s->cur_pic->f = av_frame_alloc(); |
896 | s->last_pic->f = av_frame_alloc(); | |
897 | s->next_pic->f = av_frame_alloc(); | |
898 | if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f) | |
899 | return AVERROR(ENOMEM); | |
900 | ||
903d58f6 | 901 | if (ff_h264_decode_init(avctx) < 0) |
f4cca718 BC |
902 | return -1; |
903 | ||
a0f29460 AK |
904 | // we will overwrite it later during decoding |
905 | av_frame_free(&h->cur_pic.f); | |
906 | ||
249796e2 AK |
907 | ff_h264dsp_init(&h->h264dsp, 8, 1); |
908 | ff_h264chroma_init(&h->h264chroma, 8); | |
909 | ff_h264qpel_init(&h->h264qpel, 8); | |
910 | ff_h264_pred_init(&h->hpc, AV_CODEC_ID_SVQ3, 8, 1); | |
911 | ff_videodsp_init(&h->vdsp, 8); | |
912 | ||
913 | memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t)); | |
914 | memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t)); | |
915 | ||
916 | h->sps.bit_depth_luma = 8; | |
917 | h->chroma_format_idc = 1; | |
918 | ||
2f6bc5f7 | 919 | ff_hpeldsp_init(&s->hdsp, avctx->flags); |
57f09608 DB |
920 | ff_tpeldsp_init(&s->tdsp); |
921 | ||
07c5ca55 AK |
922 | sl = h->slice_ctx; |
923 | ||
2c541554 | 924 | h->flags = avctx->flags; |
07c5ca55 | 925 | sl->is_complex = 1; |
2c541554 | 926 | h->picture_structure = PICT_FRAME; |
3795ec68 VG |
927 | avctx->pix_fmt = AV_PIX_FMT_YUVJ420P; |
928 | avctx->color_range = AVCOL_RANGE_JPEG; | |
76de302d | 929 | |
d231e84b | 930 | h->slice_ctx[0].chroma_qp[0] = h->slice_ctx[0].chroma_qp[1] = 4; |
2c541554 | 931 | h->chroma_x_shift = h->chroma_y_shift = 1; |
d9ebb00d | 932 | |
2c541554 AK |
933 | s->halfpel_flag = 1; |
934 | s->thirdpel_flag = 1; | |
935 | s->unknown_flag = 0; | |
d9ebb00d AK |
936 | |
937 | /* prowl for the "SEQH" marker in the extradata */ | |
938 | extradata = (unsigned char *)avctx->extradata; | |
939 | extradata_end = avctx->extradata + avctx->extradata_size; | |
940 | if (extradata) { | |
941 | for (m = 0; m + 8 < avctx->extradata_size; m++) { | |
942 | if (!memcmp(extradata, "SEQH", 4)) { | |
943 | marker_found = 1; | |
944 | break; | |
9e1db721 | 945 | } |
d9ebb00d | 946 | extradata++; |
76de302d | 947 | } |
d9ebb00d | 948 | } |
8b82a956 | 949 | |
d9ebb00d AK |
950 | /* if a match was found, parse the extra data */ |
951 | if (marker_found) { | |
952 | GetBitContext gb; | |
953 | int frame_size_code; | |
954 | ||
955 | size = AV_RB32(&extradata[4]); | |
956 | if (size > extradata_end - extradata - 8) | |
957 | return AVERROR_INVALIDDATA; | |
958 | init_get_bits(&gb, extradata + 8, size * 8); | |
959 | ||
960 | /* 'frame size code' and optional 'width, height' */ | |
961 | frame_size_code = get_bits(&gb, 3); | |
962 | switch (frame_size_code) { | |
963 | case 0: | |
964 | avctx->width = 160; | |
965 | avctx->height = 120; | |
966 | break; | |
967 | case 1: | |
968 | avctx->width = 128; | |
969 | avctx->height = 96; | |
970 | break; | |
971 | case 2: | |
972 | avctx->width = 176; | |
973 | avctx->height = 144; | |
974 | break; | |
975 | case 3: | |
976 | avctx->width = 352; | |
977 | avctx->height = 288; | |
978 | break; | |
979 | case 4: | |
980 | avctx->width = 704; | |
981 | avctx->height = 576; | |
982 | break; | |
983 | case 5: | |
984 | avctx->width = 240; | |
985 | avctx->height = 180; | |
986 | break; | |
987 | case 6: | |
988 | avctx->width = 320; | |
989 | avctx->height = 240; | |
990 | break; | |
991 | case 7: | |
992 | avctx->width = get_bits(&gb, 12); | |
993 | avctx->height = get_bits(&gb, 12); | |
994 | break; | |
995 | } | |
8b82a956 | 996 | |
2c541554 AK |
997 | s->halfpel_flag = get_bits1(&gb); |
998 | s->thirdpel_flag = get_bits1(&gb); | |
8b82a956 | 999 | |
d9ebb00d AK |
1000 | /* unknown fields */ |
1001 | skip_bits1(&gb); | |
1002 | skip_bits1(&gb); | |
1003 | skip_bits1(&gb); | |
1004 | skip_bits1(&gb); | |
8b82a956 | 1005 | |
2c541554 | 1006 | h->low_delay = get_bits1(&gb); |
1e002b60 | 1007 | |
d9ebb00d AK |
1008 | /* unknown field */ |
1009 | skip_bits1(&gb); | |
76de302d | 1010 | |
d9ebb00d AK |
1011 | while (get_bits1(&gb)) |
1012 | skip_bits(&gb, 8); | |
76de302d | 1013 | |
2c541554 AK |
1014 | s->unknown_flag = get_bits1(&gb); |
1015 | avctx->has_b_frames = !h->low_delay; | |
1016 | if (s->unknown_flag) { | |
b250f9c6 | 1017 | #if CONFIG_ZLIB |
d9ebb00d AK |
1018 | unsigned watermark_width = svq3_get_ue_golomb(&gb); |
1019 | unsigned watermark_height = svq3_get_ue_golomb(&gb); | |
1020 | int u1 = svq3_get_ue_golomb(&gb); | |
1021 | int u2 = get_bits(&gb, 8); | |
1022 | int u3 = get_bits(&gb, 2); | |
1023 | int u4 = svq3_get_ue_golomb(&gb); | |
1024 | unsigned long buf_len = watermark_width * | |
1025 | watermark_height * 4; | |
1026 | int offset = get_bits_count(&gb) + 7 >> 3; | |
1027 | uint8_t *buf; | |
1028 | ||
601c2015 MS |
1029 | if (watermark_height > 0 && |
1030 | (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height) | |
d9ebb00d | 1031 | return -1; |
76de302d | 1032 | |
d9ebb00d | 1033 | buf = av_malloc(buf_len); |
cc8163e1 | 1034 | av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n", |
d9ebb00d AK |
1035 | watermark_width, watermark_height); |
1036 | av_log(avctx, AV_LOG_DEBUG, | |
1037 | "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n", | |
1038 | u1, u2, u3, u4, offset); | |
1039 | if (uncompress(buf, &buf_len, extradata + 8 + offset, | |
1040 | size - offset) != Z_OK) { | |
a7d2861d | 1041 | av_log(avctx, AV_LOG_ERROR, |
d9ebb00d AK |
1042 | "could not uncompress watermark logo\n"); |
1043 | av_free(buf); | |
76de302d | 1044 | return -1; |
76de302d | 1045 | } |
2c541554 AK |
1046 | s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0); |
1047 | s->watermark_key = s->watermark_key << 16 | s->watermark_key; | |
d9ebb00d | 1048 | av_log(avctx, AV_LOG_DEBUG, |
cc8163e1 | 1049 | "watermark key %#"PRIx32"\n", s->watermark_key); |
d9ebb00d AK |
1050 | av_free(buf); |
1051 | #else | |
1052 | av_log(avctx, AV_LOG_ERROR, | |
1053 | "this svq3 file contains watermark which need zlib support compiled in\n"); | |
1054 | return -1; | |
1055 | #endif | |
76de302d | 1056 | } |
d9ebb00d | 1057 | } |
cdca7c37 | 1058 | |
2c541554 AK |
1059 | h->width = avctx->width; |
1060 | h->height = avctx->height; | |
1061 | h->mb_width = (h->width + 15) / 16; | |
1062 | h->mb_height = (h->height + 15) / 16; | |
1063 | h->mb_stride = h->mb_width + 1; | |
1064 | h->mb_num = h->mb_width * h->mb_height; | |
1065 | h->b_stride = 4 * h->mb_width; | |
1066 | s->h_edge_pos = h->mb_width * 16; | |
1067 | s->v_edge_pos = h->mb_height * 16; | |
cdca7c37 | 1068 | |
d9ebb00d AK |
1069 | if (ff_h264_alloc_tables(h) < 0) { |
1070 | av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n"); | |
1071 | return AVERROR(ENOMEM); | |
da3b9756 | 1072 | } |
76de302d | 1073 | |
f4cca718 BC |
1074 | return 0; |
1075 | } | |
1076 | ||
9b749c82 | 1077 | static void free_picture(AVCodecContext *avctx, H264Picture *pic) |
759001c5 AK |
1078 | { |
1079 | int i; | |
1080 | for (i = 0; i < 2; i++) { | |
1081 | av_buffer_unref(&pic->motion_val_buf[i]); | |
1082 | av_buffer_unref(&pic->ref_index_buf[i]); | |
1083 | } | |
1084 | av_buffer_unref(&pic->mb_type_buf); | |
1085 | ||
a0f29460 | 1086 | av_frame_unref(pic->f); |
759001c5 AK |
1087 | } |
1088 | ||
9b749c82 | 1089 | static int get_buffer(AVCodecContext *avctx, H264Picture *pic) |
2c541554 AK |
1090 | { |
1091 | SVQ3Context *s = avctx->priv_data; | |
1092 | H264Context *h = &s->h; | |
36d04801 | 1093 | H264SliceContext *sl = &h->slice_ctx[0]; |
2c541554 AK |
1094 | const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1; |
1095 | const int mb_array_size = h->mb_stride * h->mb_height; | |
1096 | const int b4_stride = h->mb_width * 4 + 1; | |
1097 | const int b4_array_size = b4_stride * h->mb_height * 4; | |
1098 | int ret; | |
1099 | ||
759001c5 | 1100 | if (!pic->motion_val_buf[0]) { |
2c541554 AK |
1101 | int i; |
1102 | ||
759001c5 AK |
1103 | pic->mb_type_buf = av_buffer_allocz((big_mb_num + h->mb_stride) * sizeof(uint32_t)); |
1104 | if (!pic->mb_type_buf) | |
2c541554 | 1105 | return AVERROR(ENOMEM); |
759001c5 | 1106 | pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1; |
2c541554 AK |
1107 | |
1108 | for (i = 0; i < 2; i++) { | |
759001c5 AK |
1109 | pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t)); |
1110 | pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size); | |
1111 | if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) { | |
1112 | ret = AVERROR(ENOMEM); | |
1113 | goto fail; | |
1114 | } | |
2c541554 | 1115 | |
759001c5 AK |
1116 | pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4; |
1117 | pic->ref_index[i] = pic->ref_index_buf[i]->data; | |
2c541554 AK |
1118 | } |
1119 | } | |
759001c5 AK |
1120 | pic->reference = !(h->pict_type == AV_PICTURE_TYPE_B); |
1121 | ||
a0f29460 | 1122 | ret = ff_get_buffer(avctx, pic->f, |
759001c5 AK |
1123 | pic->reference ? AV_GET_BUFFER_FLAG_REF : 0); |
1124 | if (ret < 0) | |
1125 | goto fail; | |
2c541554 | 1126 | |
36d04801 | 1127 | if (!sl->edge_emu_buffer) { |
a0f29460 | 1128 | sl->edge_emu_buffer = av_mallocz(pic->f->linesize[0] * 17); |
36d04801 | 1129 | if (!sl->edge_emu_buffer) |
fae6fd5b RB |
1130 | return AVERROR(ENOMEM); |
1131 | } | |
2c541554 | 1132 | |
a0f29460 AK |
1133 | sl->linesize = pic->f->linesize[0]; |
1134 | sl->uvlinesize = pic->f->linesize[1]; | |
2c541554 | 1135 | |
759001c5 AK |
1136 | return 0; |
1137 | fail: | |
1138 | free_picture(avctx, pic); | |
2c541554 AK |
1139 | return ret; |
1140 | } | |
1141 | ||
a7d2861d | 1142 | static int svq3_decode_frame(AVCodecContext *avctx, void *data, |
df9b9567 | 1143 | int *got_frame, AVPacket *avpkt) |
f4cca718 | 1144 | { |
7a00bbad | 1145 | const uint8_t *buf = avpkt->data; |
2c541554 AK |
1146 | SVQ3Context *s = avctx->priv_data; |
1147 | H264Context *h = &s->h; | |
e6287f07 | 1148 | H264SliceContext *sl = &h->slice_ctx[0]; |
a7d2861d | 1149 | int buf_size = avpkt->size; |
2c541554 | 1150 | int ret, m, i; |
f4cca718 | 1151 | |
76de302d DB |
1152 | /* special case for last picture */ |
1153 | if (buf_size == 0) { | |
a0f29460 AK |
1154 | if (s->next_pic->f->data[0] && !h->low_delay && !s->last_frame_output) { |
1155 | ret = av_frame_ref(data, s->next_pic->f); | |
759001c5 AK |
1156 | if (ret < 0) |
1157 | return ret; | |
2c541554 | 1158 | s->last_frame_output = 1; |
df9b9567 | 1159 | *got_frame = 1; |
76de302d DB |
1160 | } |
1161 | return 0; | |
da3b9756 | 1162 | } |
8b82a956 | 1163 | |
1098f5c0 LB |
1164 | ret = init_get_bits(&s->gb, buf, 8 * buf_size); |
1165 | if (ret < 0) | |
1166 | return ret; | |
8b82a956 | 1167 | |
d4d9068c | 1168 | sl->mb_x = sl->mb_y = sl->mb_xy = 0; |
8b82a956 | 1169 | |
8dfc6d1f | 1170 | if (svq3_decode_slice_header(avctx)) |
76de302d | 1171 | return -1; |
8b82a956 | 1172 | |
56febc99 | 1173 | h->pict_type = sl->slice_type; |
8b82a956 | 1174 | |
2c541554 | 1175 | if (h->pict_type != AV_PICTURE_TYPE_B) |
9b749c82 | 1176 | FFSWAP(H264Picture*, s->next_pic, s->last_pic); |
2c541554 | 1177 | |
a0f29460 | 1178 | av_frame_unref(s->cur_pic->f); |
8b82a956 | 1179 | |
8ed2ae09 | 1180 | /* for skipping the frame */ |
a0f29460 AK |
1181 | s->cur_pic->f->pict_type = h->pict_type; |
1182 | s->cur_pic->f->key_frame = (h->pict_type == AV_PICTURE_TYPE_I); | |
76de302d | 1183 | |
2c541554 AK |
1184 | ret = get_buffer(avctx, s->cur_pic); |
1185 | if (ret < 0) | |
1186 | return ret; | |
1187 | ||
1188 | h->cur_pic_ptr = s->cur_pic; | |
1189 | h->cur_pic = *s->cur_pic; | |
1190 | ||
1191 | for (i = 0; i < 16; i++) { | |
c28ed1d7 AK |
1192 | h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * sl->linesize * ((scan8[i] - scan8[0]) >> 3); |
1193 | h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * sl->linesize * ((scan8[i] - scan8[0]) >> 3); | |
2c541554 AK |
1194 | } |
1195 | for (i = 0; i < 16; i++) { | |
1196 | h->block_offset[16 + i] = | |
c28ed1d7 | 1197 | h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * sl->uvlinesize * ((scan8[i] - scan8[0]) >> 3); |
2c541554 | 1198 | h->block_offset[48 + 16 + i] = |
c28ed1d7 | 1199 | h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * sl->uvlinesize * ((scan8[i] - scan8[0]) >> 3); |
2c541554 AK |
1200 | } |
1201 | ||
1202 | if (h->pict_type != AV_PICTURE_TYPE_I) { | |
a0f29460 | 1203 | if (!s->last_pic->f->data[0]) { |
2c541554 AK |
1204 | av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n"); |
1205 | ret = get_buffer(avctx, s->last_pic); | |
1206 | if (ret < 0) | |
1207 | return ret; | |
a0f29460 AK |
1208 | memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]); |
1209 | memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) * | |
1210 | s->last_pic->f->linesize[1]); | |
1211 | memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) * | |
1212 | s->last_pic->f->linesize[2]); | |
2c541554 AK |
1213 | } |
1214 | ||
a0f29460 | 1215 | if (h->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) { |
2c541554 AK |
1216 | av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n"); |
1217 | ret = get_buffer(avctx, s->next_pic); | |
1218 | if (ret < 0) | |
1219 | return ret; | |
a0f29460 AK |
1220 | memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]); |
1221 | memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) * | |
1222 | s->next_pic->f->linesize[1]); | |
1223 | memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) * | |
1224 | s->next_pic->f->linesize[2]); | |
2c541554 AK |
1225 | } |
1226 | } | |
1227 | ||
1228 | if (avctx->debug & FF_DEBUG_PICT_INFO) | |
1229 | av_log(h->avctx, AV_LOG_DEBUG, | |
1230 | "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n", | |
1231 | av_get_picture_type_char(h->pict_type), | |
1232 | s->halfpel_flag, s->thirdpel_flag, | |
56febc99 | 1233 | s->adaptive_quant, h->slice_ctx[0].qscale, sl->slice_num); |
2c541554 AK |
1234 | |
1235 | if (avctx->skip_frame >= AVDISCARD_NONREF && h->pict_type == AV_PICTURE_TYPE_B || | |
1236 | avctx->skip_frame >= AVDISCARD_NONKEY && h->pict_type != AV_PICTURE_TYPE_I || | |
af1ede06 | 1237 | avctx->skip_frame >= AVDISCARD_ALL) |
76de302d DB |
1238 | return 0; |
1239 | ||
1240 | if (s->next_p_frame_damaged) { | |
2c541554 | 1241 | if (h->pict_type == AV_PICTURE_TYPE_B) |
76de302d DB |
1242 | return 0; |
1243 | else | |
1244 | s->next_p_frame_damaged = 0; | |
1245 | } | |
da3b9756 | 1246 | |
2c541554 | 1247 | if (h->pict_type == AV_PICTURE_TYPE_B) { |
56febc99 | 1248 | h->frame_num_offset = sl->slice_num - h->prev_frame_num; |
8b82a956 | 1249 | |
a7d2861d | 1250 | if (h->frame_num_offset < 0) |
76de302d | 1251 | h->frame_num_offset += 256; |
a7d2861d DB |
1252 | if (h->frame_num_offset == 0 || |
1253 | h->frame_num_offset >= h->prev_frame_num_offset) { | |
2c541554 | 1254 | av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n"); |
76de302d DB |
1255 | return -1; |
1256 | } | |
1257 | } else { | |
a7d2861d | 1258 | h->prev_frame_num = h->frame_num; |
56febc99 | 1259 | h->frame_num = sl->slice_num; |
af1ede06 | 1260 | h->prev_frame_num_offset = h->frame_num - h->prev_frame_num; |
da3b9756 | 1261 | |
a7d2861d | 1262 | if (h->prev_frame_num_offset < 0) |
76de302d | 1263 | h->prev_frame_num_offset += 256; |
da3b9756 | 1264 | } |
da3b9756 | 1265 | |
a7d2861d | 1266 | for (m = 0; m < 2; m++) { |
76de302d | 1267 | int i; |
a7d2861d | 1268 | for (i = 0; i < 4; i++) { |
76de302d DB |
1269 | int j; |
1270 | for (j = -1; j < 4; j++) | |
e6287f07 | 1271 | sl->ref_cache[m][scan8[0] + 8 * i + j] = 1; |
76de302d | 1272 | if (i < 3) |
e6287f07 | 1273 | sl->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE; |
76de302d | 1274 | } |
da3b9756 | 1275 | } |
da3b9756 | 1276 | |
d4d9068c AK |
1277 | for (sl->mb_y = 0; sl->mb_y < h->mb_height; sl->mb_y++) { |
1278 | for (sl->mb_x = 0; sl->mb_x < h->mb_width; sl->mb_x++) { | |
9a2e7911 | 1279 | unsigned mb_type; |
d4d9068c | 1280 | sl->mb_xy = sl->mb_x + sl->mb_y * h->mb_stride; |
da3b9756 | 1281 | |
1098f5c0 LB |
1282 | if ((get_bits_left(&h->gb)) <= 7) { |
1283 | if (((get_bits_count(&h->gb) & 7) == 0 || | |
1284 | show_bits(&h->gb, get_bits_left(&h->gb) & 7) == 0)) { | |
da3b9756 | 1285 | |
1098f5c0 LB |
1286 | if (svq3_decode_slice_header(avctx)) |
1287 | return -1; | |
1288 | } | |
76de302d DB |
1289 | /* TODO: support s->mb_skip_run */ |
1290 | } | |
da3b9756 | 1291 | |
2c541554 | 1292 | mb_type = svq3_get_ue_golomb(&h->gb); |
da3b9756 | 1293 | |
2c541554 | 1294 | if (h->pict_type == AV_PICTURE_TYPE_I) |
76de302d | 1295 | mb_type += 8; |
2c541554 | 1296 | else if (h->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4) |
76de302d | 1297 | mb_type += 4; |
2c541554 AK |
1298 | if (mb_type > 33 || svq3_decode_mb(s, mb_type)) { |
1299 | av_log(h->avctx, AV_LOG_ERROR, | |
d4d9068c | 1300 | "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y); |
76de302d DB |
1301 | return -1; |
1302 | } | |
8b82a956 | 1303 | |
a7d2861d | 1304 | if (mb_type != 0) |
92c6c2a6 | 1305 | ff_h264_hl_decode_mb(h, &h->slice_ctx[0]); |
8b82a956 | 1306 | |
2c541554 | 1307 | if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay) |
d4d9068c | 1308 | h->cur_pic.mb_type[sl->mb_x + sl->mb_y * h->mb_stride] = |
2c541554 | 1309 | (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1; |
76de302d | 1310 | } |
2e26c8d2 | 1311 | |
a0f29460 AK |
1312 | ff_draw_horiz_band(avctx, s->cur_pic->f, |
1313 | s->last_pic->f->data[0] ? s->last_pic->f : NULL, | |
d4d9068c | 1314 | 16 * sl->mb_y, 16, h->picture_structure, 0, |
54b2ce74 | 1315 | h->low_delay); |
8b82a956 | 1316 | } |
4c701ac8 | 1317 | |
2c541554 | 1318 | if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay) |
a0f29460 AK |
1319 | ret = av_frame_ref(data, s->cur_pic->f); |
1320 | else if (s->last_pic->f->data[0]) | |
1321 | ret = av_frame_ref(data, s->last_pic->f); | |
759001c5 AK |
1322 | if (ret < 0) |
1323 | return ret; | |
da3b9756 | 1324 | |
76de302d | 1325 | /* Do not output the last pic after seeking. */ |
a0f29460 | 1326 | if (s->last_pic->f->data[0] || h->low_delay) |
df9b9567 | 1327 | *got_frame = 1; |
da3b9756 | 1328 | |
2c541554 | 1329 | if (h->pict_type != AV_PICTURE_TYPE_B) { |
9b749c82 | 1330 | FFSWAP(H264Picture*, s->cur_pic, s->next_pic); |
759001c5 | 1331 | } else { |
a0f29460 | 1332 | av_frame_unref(s->cur_pic->f); |
2c541554 AK |
1333 | } |
1334 | ||
76de302d | 1335 | return buf_size; |
8b82a956 MN |
1336 | } |
1337 | ||
bd8ac882 | 1338 | static av_cold int svq3_decode_end(AVCodecContext *avctx) |
8dfc6d1f | 1339 | { |
2c541554 AK |
1340 | SVQ3Context *s = avctx->priv_data; |
1341 | H264Context *h = &s->h; | |
8dfc6d1f | 1342 | |
2c541554 AK |
1343 | free_picture(avctx, s->cur_pic); |
1344 | free_picture(avctx, s->next_pic); | |
1345 | free_picture(avctx, s->last_pic); | |
a0f29460 AK |
1346 | av_frame_free(&s->cur_pic->f); |
1347 | av_frame_free(&s->next_pic->f); | |
1348 | av_frame_free(&s->last_pic->f); | |
759001c5 AK |
1349 | av_freep(&s->cur_pic); |
1350 | av_freep(&s->next_pic); | |
1351 | av_freep(&s->last_pic); | |
1098f5c0 | 1352 | av_freep(&s->slice_buf); |
759001c5 | 1353 | |
a0f29460 | 1354 | memset(&h->cur_pic, 0, sizeof(h->cur_pic)); |
8dfc6d1f | 1355 | |
2c541554 | 1356 | ff_h264_free_context(h); |
8dfc6d1f BC |
1357 | |
1358 | return 0; | |
1359 | } | |
8b82a956 | 1360 | |
d36beb3f | 1361 | AVCodec ff_svq3_decoder = { |
ec6402b7 | 1362 | .name = "svq3", |
b2bed932 | 1363 | .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"), |
ec6402b7 | 1364 | .type = AVMEDIA_TYPE_VIDEO, |
36ef5369 | 1365 | .id = AV_CODEC_ID_SVQ3, |
ec6402b7 AK |
1366 | .priv_data_size = sizeof(SVQ3Context), |
1367 | .init = svq3_decode_init, | |
1368 | .close = svq3_decode_end, | |
1369 | .decode = svq3_decode_frame, | |
def97856 VG |
1370 | .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | |
1371 | AV_CODEC_CAP_DR1 | | |
1372 | AV_CODEC_CAP_DELAY, | |
a7d2861d DB |
1373 | .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, |
1374 | AV_PIX_FMT_NONE}, | |
8b82a956 | 1375 | }; |