Mercurial > libavcodec.hg
annotate vp56.c @ 3715:cdaee53c5da1 libavcodec
replaces back some C code by dsp.put_pixels_tab[0][0] (revert r6231)
but this time ensure proper alignment using av_malloc instead of av_realloc
author | aurel |
---|---|
date | Wed, 13 Sep 2006 20:16:33 +0000 |
parents | fb73a0142544 |
children | c8c591fe26f8 |
rev | line source |
---|---|
3695 | 1 /** |
2 * @file vp56.c | |
3 * VP5 and VP6 compatible video decoder (common features) | |
4 * | |
5 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> | |
6 * | |
7 * This library is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * This library is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with this library; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 #include "avcodec.h" | |
23 | |
24 #include "vp56.h" | |
25 #include "vp56data.h" | |
26 | |
27 | |
28 void vp56_init_dequant(vp56_context_t *s, int quantizer) | |
29 { | |
30 s->quantizer = quantizer; | |
31 s->dequant_dc = vp56_dc_dequant[quantizer] << 2; | |
32 s->dequant_ac = vp56_ac_dequant[quantizer] << 2; | |
33 } | |
34 | |
35 static int vp56_get_vectors_predictors(vp56_context_t *s, int row, int col, | |
36 vp56_frame_t ref_frame) | |
37 { | |
38 int nb_pred = 0; | |
3697 | 39 vp56_mv_t vect[2] = {{0,0}, {0,0}}; |
3695 | 40 int pos, offset; |
41 vp56_mv_t mvp; | |
42 | |
43 for (pos=0; pos<12; pos++) { | |
44 mvp.x = col + vp56_candidate_predictor_pos[pos][0]; | |
45 mvp.y = row + vp56_candidate_predictor_pos[pos][1]; | |
46 if (mvp.x < 0 || mvp.x >= s->mb_width || | |
47 mvp.y < 0 || mvp.y >= s->mb_height) | |
48 continue; | |
49 offset = mvp.x + s->mb_width*mvp.y; | |
50 | |
51 if (vp56_reference_frame[s->macroblocks[offset].type] != ref_frame) | |
52 continue; | |
3697 | 53 if ((s->macroblocks[offset].mv.x == vect[0].x && |
54 s->macroblocks[offset].mv.y == vect[0].y) || | |
3695 | 55 (s->macroblocks[offset].mv.x == 0 && |
56 s->macroblocks[offset].mv.y == 0)) | |
57 continue; | |
58 | |
3697 | 59 vect[nb_pred++] = s->macroblocks[offset].mv; |
3695 | 60 if (nb_pred > 1) { |
61 nb_pred = -1; | |
62 break; | |
63 } | |
64 s->vector_candidate_pos = pos; | |
65 } | |
66 | |
3697 | 67 s->vector_candidate[0] = vect[0]; |
68 s->vector_candidate[1] = vect[1]; | |
3695 | 69 |
70 return nb_pred+1; | |
71 } | |
72 | |
73 static void vp56_parse_mb_type_models(vp56_context_t *s) | |
74 { | |
75 vp56_range_coder_t *c = &s->c; | |
76 int i, ctx, type; | |
77 | |
78 for (ctx=0; ctx<3; ctx++) { | |
79 if (vp56_rac_get_prob(c, 174)) { | |
80 int idx = vp56_rac_gets(c, 4); | |
81 memcpy(s->mb_types_stats[ctx],vp56_pre_def_mb_type_stats[idx][ctx], | |
82 sizeof(s->mb_types_stats[ctx])); | |
83 } | |
84 if (vp56_rac_get_prob(c, 254)) { | |
85 for (type=0; type<10; type++) { | |
86 for(i=0; i<2; i++) { | |
87 if (vp56_rac_get_prob(c, 205)) { | |
88 int delta, sign = vp56_rac_get(c); | |
89 | |
90 delta = vp56_rac_get_tree(c, vp56_pmbtm_tree, | |
91 vp56_mb_type_model_model); | |
92 if (!delta) | |
93 delta = 4 * vp56_rac_gets(c, 7); | |
94 s->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign; | |
95 } | |
96 } | |
97 } | |
98 } | |
99 } | |
100 | |
101 /* compute MB type probability tables based on previous MB type */ | |
102 for (ctx=0; ctx<3; ctx++) { | |
103 int p[10]; | |
104 | |
105 for (type=0; type<10; type++) | |
106 p[type] = 100 * s->mb_types_stats[ctx][type][1]; | |
107 | |
108 for (type=0; type<10; type++) { | |
109 int p02, p34, p0234, p17, p56, p89, p5689, p156789; | |
110 | |
111 /* conservative MB type probability */ | |
112 s->mb_type_model[ctx][type][0] = 255 - (255 * s->mb_types_stats[ctx][type][0]) / (1 + s->mb_types_stats[ctx][type][0] + s->mb_types_stats[ctx][type][1]); | |
113 | |
114 p[type] = 0; /* same MB type => weight is null */ | |
115 | |
116 /* binary tree parsing probabilities */ | |
117 p02 = p[0] + p[2]; | |
118 p34 = p[3] + p[4]; | |
119 p0234 = p02 + p34; | |
120 p17 = p[1] + p[7]; | |
121 p56 = p[5] + p[6]; | |
122 p89 = p[8] + p[9]; | |
123 p5689 = p56 + p89; | |
124 p156789 = p17 + p5689; | |
125 | |
126 s->mb_type_model[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789); | |
127 s->mb_type_model[ctx][type][2] = 1 + 255 * p02 / (1+p0234); | |
128 s->mb_type_model[ctx][type][3] = 1 + 255 * p17 / (1+p156789); | |
129 s->mb_type_model[ctx][type][4] = 1 + 255 * p[0] / (1+p02); | |
130 s->mb_type_model[ctx][type][5] = 1 + 255 * p[3] / (1+p34); | |
131 s->mb_type_model[ctx][type][6] = 1 + 255 * p[1] / (1+p17); | |
132 s->mb_type_model[ctx][type][7] = 1 + 255 * p56 / (1+p5689); | |
133 s->mb_type_model[ctx][type][8] = 1 + 255 * p[5] / (1+p56); | |
134 s->mb_type_model[ctx][type][9] = 1 + 255 * p[8] / (1+p89); | |
135 | |
136 /* restore initial value */ | |
137 p[type] = 100 * s->mb_types_stats[ctx][type][1]; | |
138 } | |
139 } | |
140 } | |
141 | |
142 static vp56_mb_t vp56_parse_mb_type(vp56_context_t *s, | |
143 vp56_mb_t prev_type, int ctx) | |
144 { | |
145 uint8_t *mb_type_model = s->mb_type_model[ctx][prev_type]; | |
146 vp56_range_coder_t *c = &s->c; | |
147 | |
148 if (vp56_rac_get_prob(c, mb_type_model[0])) | |
149 return prev_type; | |
150 else | |
151 return vp56_rac_get_tree(c, vp56_pmbt_tree, mb_type_model); | |
152 } | |
153 | |
154 static void vp56_decode_4mv(vp56_context_t *s, int row, int col) | |
155 { | |
156 vp56_mv_t mv = {0,0}; | |
157 int type[4]; | |
158 int b; | |
159 | |
160 /* parse each block type */ | |
161 for (b=0; b<4; b++) { | |
162 type[b] = vp56_rac_gets(&s->c, 2); | |
163 if (type[b]) | |
164 type[b]++; /* only returns 0, 2, 3 or 4 (all INTER_PF) */ | |
165 } | |
166 | |
167 /* get vectors */ | |
168 for (b=0; b<4; b++) { | |
169 switch (type[b]) { | |
170 case VP56_MB_INTER_NOVEC_PF: | |
171 s->mv[b] = (vp56_mv_t) {0,0}; | |
172 break; | |
173 case VP56_MB_INTER_DELTA_PF: | |
174 s->parse_vector_adjustment(s, &s->mv[b]); | |
175 break; | |
176 case VP56_MB_INTER_V1_PF: | |
177 s->mv[b] = s->vector_candidate[0]; | |
178 break; | |
179 case VP56_MB_INTER_V2_PF: | |
180 s->mv[b] = s->vector_candidate[1]; | |
181 break; | |
182 } | |
183 mv.x += s->mv[b].x; | |
184 mv.y += s->mv[b].y; | |
185 } | |
186 | |
187 /* this is the one selected for the whole MB for prediction */ | |
188 s->macroblocks[row * s->mb_width + col].mv = s->mv[3]; | |
189 | |
190 /* chroma vectors are average luma vectors */ | |
191 if (s->avctx->codec->id == CODEC_ID_VP5) { | |
192 s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2); | |
193 s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2); | |
194 } else { | |
195 s->mv[4] = s->mv[5] = (vp56_mv_t) {mv.x/4, mv.y/4}; | |
196 } | |
197 } | |
198 | |
199 static vp56_mb_t vp56_decode_mv(vp56_context_t *s, int row, int col) | |
200 { | |
3697 | 201 vp56_mv_t *mv, vect = {0,0}; |
3695 | 202 int ctx, b; |
203 | |
204 ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS); | |
205 s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx); | |
206 s->macroblocks[row * s->mb_width + col].type = s->mb_type; | |
207 | |
208 switch (s->mb_type) { | |
209 case VP56_MB_INTER_V1_PF: | |
210 mv = &s->vector_candidate[0]; | |
211 break; | |
212 | |
213 case VP56_MB_INTER_V2_PF: | |
214 mv = &s->vector_candidate[1]; | |
215 break; | |
216 | |
217 case VP56_MB_INTER_V1_GF: | |
218 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN); | |
219 mv = &s->vector_candidate[0]; | |
220 break; | |
221 | |
222 case VP56_MB_INTER_V2_GF: | |
223 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN); | |
224 mv = &s->vector_candidate[1]; | |
225 break; | |
226 | |
227 case VP56_MB_INTER_DELTA_PF: | |
3697 | 228 s->parse_vector_adjustment(s, &vect); |
229 mv = &vect; | |
3695 | 230 break; |
231 | |
232 case VP56_MB_INTER_DELTA_GF: | |
233 vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN); | |
3697 | 234 s->parse_vector_adjustment(s, &vect); |
235 mv = &vect; | |
3695 | 236 break; |
237 | |
238 case VP56_MB_INTER_4V: | |
239 vp56_decode_4mv(s, row, col); | |
240 return s->mb_type; | |
241 | |
242 default: | |
3697 | 243 mv = &vect; |
3695 | 244 break; |
245 } | |
246 | |
247 s->macroblocks[row*s->mb_width + col].mv = *mv; | |
248 | |
249 /* same vector for all blocks */ | |
250 for (b=0; b<6; b++) | |
251 s->mv[b] = *mv; | |
252 | |
253 return s->mb_type; | |
254 } | |
255 | |
256 static void vp56_add_predictors_dc(vp56_context_t *s, vp56_frame_t ref_frame) | |
257 { | |
258 int idx = s->scantable.permutated[0]; | |
259 int i; | |
260 | |
261 for (i=0; i<6; i++) { | |
262 vp56_ref_dc_t *ab = &s->above_blocks[s->above_block_idx[i]]; | |
263 vp56_ref_dc_t *lb = &s->left_block[vp56_b6to4[i]]; | |
264 int count = 0; | |
265 int dc = 0; | |
266 | |
267 if (ref_frame == lb->ref_frame) { | |
268 dc += lb->dc_coeff; | |
269 count++; | |
270 } | |
271 if (ref_frame == ab->ref_frame) { | |
272 dc += ab->dc_coeff; | |
273 count++; | |
274 } | |
275 if (s->avctx->codec->id == CODEC_ID_VP5) { | |
276 if (count < 2 && ref_frame == ab[-1].ref_frame) { | |
277 dc += ab[-1].dc_coeff; | |
278 count++; | |
279 } | |
280 if (count < 2 && ref_frame == ab[1].ref_frame) { | |
281 dc += ab[1].dc_coeff; | |
282 count++; | |
283 } | |
284 } | |
285 if (count == 0) | |
286 dc = s->prev_dc[vp56_b6to3[i]][ref_frame]; | |
287 else if (count == 2) | |
288 dc /= 2; | |
289 | |
290 s->block_coeff[i][idx] += dc; | |
291 s->prev_dc[vp56_b6to3[i]][ref_frame] = s->block_coeff[i][idx]; | |
292 ab->dc_coeff = s->block_coeff[i][idx]; | |
293 ab->ref_frame = ref_frame; | |
294 lb->dc_coeff = s->block_coeff[i][idx]; | |
295 lb->ref_frame = ref_frame; | |
296 s->block_coeff[i][idx] *= s->dequant_dc; | |
297 } | |
298 } | |
299 | |
300 static void vp56_edge_filter(vp56_context_t *s, uint8_t *yuv, | |
301 int pix_inc, int line_inc, int t) | |
302 { | |
303 int pix2_inc = 2 * pix_inc; | |
304 int i, v; | |
305 | |
306 for (i=0; i<12; i++) { | |
307 v = (yuv[-pix2_inc] + 3*(yuv[0]-yuv[-pix_inc]) - yuv[pix_inc] + 4) >>3; | |
308 v = s->adjust(v, t); | |
309 yuv[-pix_inc] = clip_uint8(yuv[-pix_inc] + v); | |
310 yuv[0] = clip_uint8(yuv[0] - v); | |
311 yuv += line_inc; | |
312 } | |
313 } | |
314 | |
315 static void vp56_deblock_filter(vp56_context_t *s, uint8_t *yuv, | |
316 int stride, int dx, int dy) | |
317 { | |
318 int t = vp56_filter_threshold[s->quantizer]; | |
319 if (dx) vp56_edge_filter(s, yuv + 10-dx , 1, stride, t); | |
320 if (dy) vp56_edge_filter(s, yuv + stride*(10-dy), stride, 1, t); | |
321 } | |
322 | |
323 static void vp56_mc(vp56_context_t *s, int b, uint8_t *src, | |
324 int stride, int x, int y) | |
325 { | |
326 int plane = vp56_b6to3[b]; | |
327 uint8_t *dst= s->frames[VP56_FRAME_CURRENT].data[plane]+s->block_offset[b]; | |
328 uint8_t *src_block; | |
329 int src_offset; | |
330 int overlap_offset = 0; | |
331 int mask = s->vp56_coord_div[b] - 1; | |
332 int deblock_filtering = s->deblock_filtering; | |
333 int dx; | |
334 int dy; | |
335 | |
336 if (s->avctx->skip_loop_filter >= AVDISCARD_ALL || | |
337 (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY | |
338 && !s->frames[VP56_FRAME_CURRENT].key_frame)) | |
339 deblock_filtering = 0; | |
340 | |
341 dx = s->mv[b].x / s->vp56_coord_div[b]; | |
342 dy = s->mv[b].y / s->vp56_coord_div[b]; | |
343 | |
344 if (b >= 4) { | |
345 x /= 2; | |
346 y /= 2; | |
347 } | |
348 x += dx - 2; | |
349 y += dy - 2; | |
350 | |
351 if (x<0 || x+12>=s->plane_width[plane] || | |
352 y<0 || y+12>=s->plane_height[plane]) { | |
353 ff_emulated_edge_mc(s->edge_emu_buffer, | |
354 src + s->block_offset[b] + (dy-2)*stride + (dx-2), | |
355 stride, 12, 12, x, y, | |
356 s->plane_width[plane], | |
357 s->plane_height[plane]); | |
358 src_block = s->edge_emu_buffer; | |
359 src_offset = 2 + 2*stride; | |
360 } else if (deblock_filtering) { | |
3715
cdaee53c5da1
replaces back some C code by dsp.put_pixels_tab[0][0] (revert r6231)
aurel
parents:
3706
diff
changeset
|
361 /* only need a 12x12 block, but there is no such dsp function, */ |
cdaee53c5da1
replaces back some C code by dsp.put_pixels_tab[0][0] (revert r6231)
aurel
parents:
3706
diff
changeset
|
362 /* so copy a 16x12 block */ |
cdaee53c5da1
replaces back some C code by dsp.put_pixels_tab[0][0] (revert r6231)
aurel
parents:
3706
diff
changeset
|
363 s->dsp.put_pixels_tab[0][0](s->edge_emu_buffer, |
cdaee53c5da1
replaces back some C code by dsp.put_pixels_tab[0][0] (revert r6231)
aurel
parents:
3706
diff
changeset
|
364 src + s->block_offset[b] + (dy-2)*stride + (dx-2), |
cdaee53c5da1
replaces back some C code by dsp.put_pixels_tab[0][0] (revert r6231)
aurel
parents:
3706
diff
changeset
|
365 stride, 12); |
3695 | 366 src_block = s->edge_emu_buffer; |
367 src_offset = 2 + 2*stride; | |
368 } else { | |
369 src_block = src; | |
370 src_offset = s->block_offset[b] + dy*stride + dx; | |
371 } | |
372 | |
373 if (deblock_filtering) | |
374 vp56_deblock_filter(s, src_block, stride, dx&7, dy&7); | |
375 | |
376 if (s->mv[b].x & mask) | |
377 overlap_offset += (s->mv[b].x > 0) ? 1 : -1; | |
378 if (s->mv[b].y & mask) | |
379 overlap_offset += (s->mv[b].y > 0) ? stride : -stride; | |
380 | |
381 if (overlap_offset) { | |
382 if (s->filter) | |
383 s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset, | |
384 stride, s->mv[b], mask, s->filter_selection, b<4); | |
385 else | |
386 s->dsp.put_no_rnd_pixels_l2[1](dst, src_block+src_offset, | |
387 src_block+src_offset+overlap_offset, | |
388 stride, 8); | |
389 } else { | |
390 s->dsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8); | |
391 } | |
392 } | |
393 | |
394 static void vp56_decode_mb(vp56_context_t *s, int row, int col) | |
395 { | |
396 AVFrame *frame_current, *frame_ref; | |
397 vp56_mb_t mb_type; | |
398 vp56_frame_t ref_frame; | |
399 int b, plan, off; | |
400 | |
401 if (s->frames[VP56_FRAME_CURRENT].key_frame) | |
402 mb_type = VP56_MB_INTRA; | |
403 else | |
404 mb_type = vp56_decode_mv(s, row, col); | |
405 ref_frame = vp56_reference_frame[mb_type]; | |
406 | |
407 memset(s->block_coeff, 0, sizeof(s->block_coeff)); | |
408 | |
409 s->parse_coeff(s); | |
410 | |
411 vp56_add_predictors_dc(s, ref_frame); | |
412 | |
413 frame_current = &s->frames[VP56_FRAME_CURRENT]; | |
414 frame_ref = &s->frames[ref_frame]; | |
415 | |
416 switch (mb_type) { | |
417 case VP56_MB_INTRA: | |
418 for (b=0; b<6; b++) { | |
419 plan = vp56_b6to3[b]; | |
420 s->dsp.idct_put(frame_current->data[plan] + s->block_offset[b], | |
421 s->stride[plan], s->block_coeff[b]); | |
422 } | |
423 break; | |
424 | |
425 case VP56_MB_INTER_NOVEC_PF: | |
426 case VP56_MB_INTER_NOVEC_GF: | |
427 for (b=0; b<6; b++) { | |
428 plan = vp56_b6to3[b]; | |
429 off = s->block_offset[b]; | |
430 s->dsp.put_pixels_tab[1][0](frame_current->data[plan] + off, | |
431 frame_ref->data[plan] + off, | |
432 s->stride[plan], 8); | |
433 s->dsp.idct_add(frame_current->data[plan] + off, | |
434 s->stride[plan], s->block_coeff[b]); | |
435 } | |
436 break; | |
437 | |
438 case VP56_MB_INTER_DELTA_PF: | |
439 case VP56_MB_INTER_V1_PF: | |
440 case VP56_MB_INTER_V2_PF: | |
441 case VP56_MB_INTER_DELTA_GF: | |
442 case VP56_MB_INTER_4V: | |
443 case VP56_MB_INTER_V1_GF: | |
444 case VP56_MB_INTER_V2_GF: | |
445 for (b=0; b<6; b++) { | |
446 int x_off = b==1 || b==3 ? 8 : 0; | |
447 int y_off = b==2 || b==3 ? 8 : 0; | |
448 plan = vp56_b6to3[b]; | |
449 vp56_mc(s, b, frame_ref->data[plan], s->stride[plan], | |
450 16*col+x_off, 16*row+y_off); | |
451 s->dsp.idct_add(frame_current->data[plan] + s->block_offset[b], | |
452 s->stride[plan], s->block_coeff[b]); | |
453 } | |
454 break; | |
455 } | |
456 } | |
457 | |
458 static int vp56_size_changed(AVCodecContext *avctx, vp56_context_t *s) | |
459 { | |
460 int stride = s->frames[VP56_FRAME_CURRENT].linesize[0]; | |
461 int i; | |
462 | |
463 s->plane_width[0] = s->avctx->width; | |
464 s->plane_width[1] = s->plane_width[2] = s->avctx->width/2; | |
465 s->plane_height[0] = s->avctx->height; | |
466 s->plane_height[1] = s->plane_height[2] = s->avctx->height/2; | |
467 | |
468 for (i=0; i<3; i++) | |
469 s->stride[i] = s->flip * s->frames[VP56_FRAME_CURRENT].linesize[i]; | |
470 | |
471 s->mb_width = (s->avctx->width+15) / 16; | |
472 s->mb_height = (s->avctx->height+15) / 16; | |
473 | |
474 if (s->mb_width > 1000 || s->mb_height > 1000) { | |
475 av_log(avctx, AV_LOG_ERROR, "picture too big\n"); | |
476 return -1; | |
477 } | |
478 | |
479 s->above_blocks = av_realloc(s->above_blocks, | |
480 (4*s->mb_width+6) * sizeof(*s->above_blocks)); | |
481 s->macroblocks = av_realloc(s->macroblocks, | |
482 s->mb_width*s->mb_height*sizeof(*s->macroblocks)); | |
3715
cdaee53c5da1
replaces back some C code by dsp.put_pixels_tab[0][0] (revert r6231)
aurel
parents:
3706
diff
changeset
|
483 av_free(s->edge_emu_buffer_alloc); |
cdaee53c5da1
replaces back some C code by dsp.put_pixels_tab[0][0] (revert r6231)
aurel
parents:
3706
diff
changeset
|
484 s->edge_emu_buffer_alloc = av_malloc(16*stride); |
3695 | 485 s->edge_emu_buffer = s->edge_emu_buffer_alloc; |
486 if (s->flip < 0) | |
487 s->edge_emu_buffer += 15 * stride; | |
488 | |
489 return 0; | |
490 } | |
491 | |
492 int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size, | |
493 uint8_t *buf, int buf_size) | |
494 { | |
495 vp56_context_t *s = avctx->priv_data; | |
496 AVFrame *const p = &s->frames[VP56_FRAME_CURRENT]; | |
497 AVFrame *picture = data; | |
498 int mb_row, mb_col, mb_row_flip, mb_offset = 0; | |
499 int block, y, uv, stride_y, stride_uv; | |
500 int golden_frame = 0; | |
501 int res; | |
502 | |
503 res = s->parse_header(s, buf, buf_size, &golden_frame); | |
504 if (!res) | |
505 return -1; | |
506 | |
507 p->reference = 1; | |
508 if (avctx->get_buffer(avctx, p) < 0) { | |
509 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
510 return -1; | |
511 } | |
512 | |
513 if (res == 2) | |
514 if (vp56_size_changed(avctx, s)) { | |
515 avctx->release_buffer(avctx, p); | |
516 return -1; | |
517 } | |
518 | |
519 if (p->key_frame) { | |
520 p->pict_type = FF_I_TYPE; | |
521 s->default_models_init(s); | |
522 for (block=0; block<s->mb_height*s->mb_width; block++) | |
523 s->macroblocks[block].type = VP56_MB_INTRA; | |
524 } else { | |
525 p->pict_type = FF_P_TYPE; | |
526 vp56_parse_mb_type_models(s); | |
527 s->parse_vector_models(s); | |
528 s->mb_type = VP56_MB_INTER_NOVEC_PF; | |
529 } | |
530 | |
531 s->parse_coeff_models(s); | |
532 | |
533 memset(s->prev_dc, 0, sizeof(s->prev_dc)); | |
534 s->prev_dc[1][VP56_FRAME_CURRENT] = 128; | |
535 s->prev_dc[2][VP56_FRAME_CURRENT] = 128; | |
536 | |
537 for (block=0; block < 4*s->mb_width+6; block++) { | |
538 s->above_blocks[block].ref_frame = -1; | |
539 s->above_blocks[block].dc_coeff = 0; | |
540 s->above_blocks[block].not_null_dc = 0; | |
541 } | |
542 s->above_blocks[2*s->mb_width + 2].ref_frame = 0; | |
543 s->above_blocks[3*s->mb_width + 4].ref_frame = 0; | |
544 | |
545 stride_y = p->linesize[0]; | |
546 stride_uv = p->linesize[1]; | |
547 | |
548 if (s->flip < 0) | |
549 mb_offset = 7; | |
550 | |
551 /* main macroblocks loop */ | |
552 for (mb_row=0; mb_row<s->mb_height; mb_row++) { | |
553 if (s->flip < 0) | |
554 mb_row_flip = s->mb_height - mb_row - 1; | |
555 else | |
556 mb_row_flip = mb_row; | |
557 | |
558 for (block=0; block<4; block++) { | |
559 s->left_block[block].ref_frame = -1; | |
560 s->left_block[block].dc_coeff = 0; | |
561 s->left_block[block].not_null_dc = 0; | |
562 memset(s->coeff_ctx[block], 0, 64*sizeof(s->coeff_ctx[block][0])); | |
563 } | |
564 memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last)); | |
565 | |
566 s->above_block_idx[0] = 1; | |
567 s->above_block_idx[1] = 2; | |
568 s->above_block_idx[2] = 1; | |
569 s->above_block_idx[3] = 2; | |
570 s->above_block_idx[4] = 2*s->mb_width + 2 + 1; | |
571 s->above_block_idx[5] = 3*s->mb_width + 4 + 1; | |
572 | |
573 s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y; | |
574 s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y; | |
575 s->block_offset[1] = s->block_offset[0] + 8; | |
576 s->block_offset[3] = s->block_offset[2] + 8; | |
577 s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv; | |
578 s->block_offset[5] = s->block_offset[4]; | |
579 | |
580 for (mb_col=0; mb_col<s->mb_width; mb_col++) { | |
581 vp56_decode_mb(s, mb_row, mb_col); | |
582 | |
583 for (y=0; y<4; y++) { | |
584 s->above_block_idx[y] += 2; | |
585 s->block_offset[y] += 16; | |
586 } | |
587 | |
588 for (uv=4; uv<6; uv++) { | |
589 s->above_block_idx[uv] += 1; | |
590 s->block_offset[uv] += 8; | |
591 } | |
592 } | |
593 } | |
594 | |
595 if (s->frames[VP56_FRAME_PREVIOUS].data[0] | |
596 && (s->frames[VP56_FRAME_PREVIOUS].data[0] | |
597 != s->frames[VP56_FRAME_GOLDEN].data[0])) { | |
598 avctx->release_buffer(avctx, &s->frames[VP56_FRAME_PREVIOUS]); | |
599 } | |
600 if (p->key_frame || golden_frame) { | |
601 if (s->frames[VP56_FRAME_GOLDEN].data[0]) | |
602 avctx->release_buffer(avctx, &s->frames[VP56_FRAME_GOLDEN]); | |
603 s->frames[VP56_FRAME_GOLDEN] = *p; | |
604 } | |
605 s->frames[VP56_FRAME_PREVIOUS] = *p; | |
606 | |
607 *picture = *p; | |
608 *data_size = sizeof(AVPicture); | |
609 | |
610 return buf_size; | |
611 } | |
612 | |
613 void vp56_init(vp56_context_t *s, AVCodecContext *avctx, int flip) | |
614 { | |
615 int i; | |
616 | |
617 s->avctx = avctx; | |
618 avctx->pix_fmt = PIX_FMT_YUV420P; | |
619 | |
620 if (s->avctx->idct_algo == FF_IDCT_AUTO) | |
621 s->avctx->idct_algo = FF_IDCT_VP3; | |
622 dsputil_init(&s->dsp, s->avctx); | |
623 ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct); | |
624 | |
625 avcodec_set_dimensions(s->avctx, 0, 0); | |
626 | |
627 for (i=0; i<3; i++) | |
628 s->frames[i].data[0] = NULL; | |
629 s->edge_emu_buffer_alloc = NULL; | |
630 | |
631 s->above_blocks = NULL; | |
632 s->macroblocks = NULL; | |
633 s->quantizer = -1; | |
634 s->deblock_filtering = 1; | |
635 | |
636 s->filter = NULL; | |
637 | |
638 if (flip) { | |
639 s->flip = -1; | |
640 s->frbi = 2; | |
641 s->srbi = 0; | |
642 } else { | |
643 s->flip = 1; | |
644 s->frbi = 0; | |
645 s->srbi = 2; | |
646 } | |
647 } | |
648 | |
649 int vp56_free(AVCodecContext *avctx) | |
650 { | |
651 vp56_context_t *s = avctx->priv_data; | |
652 | |
653 av_free(s->above_blocks); | |
654 av_free(s->macroblocks); | |
655 av_free(s->edge_emu_buffer_alloc); | |
656 if (s->frames[VP56_FRAME_GOLDEN].data[0] | |
657 && (s->frames[VP56_FRAME_PREVIOUS].data[0] | |
658 != s->frames[VP56_FRAME_GOLDEN].data[0])) | |
659 avctx->release_buffer(avctx, &s->frames[VP56_FRAME_GOLDEN]); | |
660 if (s->frames[VP56_FRAME_PREVIOUS].data[0]) | |
661 avctx->release_buffer(avctx, &s->frames[VP56_FRAME_PREVIOUS]); | |
662 return 0; | |
663 } |