]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp56.c
27b4b8b94444e91ffb6128d465da57e0c0aad670
[ffmpeg] / libavcodec / vp56.c
1 /*
2  * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
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  * @file
23  * VP5 and VP6 compatible video decoder (common features)
24  */
25
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "internal.h"
29 #include "h264chroma.h"
30 #include "vp56.h"
31 #include "vp56data.h"
32
33
34 void ff_vp56_init_dequant(VP56Context *s, int quantizer)
35 {
36     s->quantizer = quantizer;
37     s->dequant_dc = ff_vp56_dc_dequant[quantizer] << 2;
38     s->dequant_ac = ff_vp56_ac_dequant[quantizer] << 2;
39 }
40
41 static int vp56_get_vectors_predictors(VP56Context *s, int row, int col,
42                                        VP56Frame ref_frame)
43 {
44     int nb_pred = 0;
45     VP56mv vect[2] = {{0,0}, {0,0}};
46     int pos, offset;
47     VP56mv mvp;
48
49     for (pos=0; pos<12; pos++) {
50         mvp.x = col + ff_vp56_candidate_predictor_pos[pos][0];
51         mvp.y = row + ff_vp56_candidate_predictor_pos[pos][1];
52         if (mvp.x < 0 || mvp.x >= s->mb_width ||
53             mvp.y < 0 || mvp.y >= s->mb_height)
54             continue;
55         offset = mvp.x + s->mb_width*mvp.y;
56
57         if (ff_vp56_reference_frame[s->macroblocks[offset].type] != ref_frame)
58             continue;
59         if ((s->macroblocks[offset].mv.x == vect[0].x &&
60              s->macroblocks[offset].mv.y == vect[0].y) ||
61             (s->macroblocks[offset].mv.x == 0 &&
62              s->macroblocks[offset].mv.y == 0))
63             continue;
64
65         vect[nb_pred++] = s->macroblocks[offset].mv;
66         if (nb_pred > 1) {
67             nb_pred = -1;
68             break;
69         }
70         s->vector_candidate_pos = pos;
71     }
72
73     s->vector_candidate[0] = vect[0];
74     s->vector_candidate[1] = vect[1];
75
76     return nb_pred+1;
77 }
78
79 static void vp56_parse_mb_type_models(VP56Context *s)
80 {
81     VP56RangeCoder *c = &s->c;
82     VP56Model *model = s->modelp;
83     int i, ctx, type;
84
85     for (ctx=0; ctx<3; ctx++) {
86         if (vp56_rac_get_prob_branchy(c, 174)) {
87             int idx = vp56_rac_gets(c, 4);
88             memcpy(model->mb_types_stats[ctx],
89                    ff_vp56_pre_def_mb_type_stats[idx][ctx],
90                    sizeof(model->mb_types_stats[ctx]));
91         }
92         if (vp56_rac_get_prob_branchy(c, 254)) {
93             for (type=0; type<10; type++) {
94                 for(i=0; i<2; i++) {
95                     if (vp56_rac_get_prob_branchy(c, 205)) {
96                         int delta, sign = vp56_rac_get(c);
97
98                         delta = vp56_rac_get_tree(c, ff_vp56_pmbtm_tree,
99                                                   ff_vp56_mb_type_model_model);
100                         if (!delta)
101                             delta = 4 * vp56_rac_gets(c, 7);
102                         model->mb_types_stats[ctx][type][i] += (delta ^ -sign) + sign;
103                     }
104                 }
105             }
106         }
107     }
108
109     /* compute MB type probability tables based on previous MB type */
110     for (ctx=0; ctx<3; ctx++) {
111         int p[10];
112
113         for (type=0; type<10; type++)
114             p[type] = 100 * model->mb_types_stats[ctx][type][1];
115
116         for (type=0; type<10; type++) {
117             int p02, p34, p0234, p17, p56, p89, p5689, p156789;
118
119             /* conservative MB type probability */
120             model->mb_type[ctx][type][0] = 255 - (255 * model->mb_types_stats[ctx][type][0]) / (1 + model->mb_types_stats[ctx][type][0] + model->mb_types_stats[ctx][type][1]);
121
122             p[type] = 0;    /* same MB type => weight is null */
123
124             /* binary tree parsing probabilities */
125             p02 = p[0] + p[2];
126             p34 = p[3] + p[4];
127             p0234 = p02 + p34;
128             p17 = p[1] + p[7];
129             p56 = p[5] + p[6];
130             p89 = p[8] + p[9];
131             p5689 = p56 + p89;
132             p156789 = p17 + p5689;
133
134             model->mb_type[ctx][type][1] = 1 + 255 * p0234/(1+p0234+p156789);
135             model->mb_type[ctx][type][2] = 1 + 255 * p02  / (1+p0234);
136             model->mb_type[ctx][type][3] = 1 + 255 * p17  / (1+p156789);
137             model->mb_type[ctx][type][4] = 1 + 255 * p[0] / (1+p02);
138             model->mb_type[ctx][type][5] = 1 + 255 * p[3] / (1+p34);
139             model->mb_type[ctx][type][6] = 1 + 255 * p[1] / (1+p17);
140             model->mb_type[ctx][type][7] = 1 + 255 * p56  / (1+p5689);
141             model->mb_type[ctx][type][8] = 1 + 255 * p[5] / (1+p56);
142             model->mb_type[ctx][type][9] = 1 + 255 * p[8] / (1+p89);
143
144             /* restore initial value */
145             p[type] = 100 * model->mb_types_stats[ctx][type][1];
146         }
147     }
148 }
149
150 static VP56mb vp56_parse_mb_type(VP56Context *s,
151                                  VP56mb prev_type, int ctx)
152 {
153     uint8_t *mb_type_model = s->modelp->mb_type[ctx][prev_type];
154     VP56RangeCoder *c = &s->c;
155
156     if (vp56_rac_get_prob_branchy(c, mb_type_model[0]))
157         return prev_type;
158     else
159         return vp56_rac_get_tree(c, ff_vp56_pmbt_tree, mb_type_model);
160 }
161
162 static void vp56_decode_4mv(VP56Context *s, int row, int col)
163 {
164     VP56mv mv = {0,0};
165     int type[4];
166     int b;
167
168     /* parse each block type */
169     for (b=0; b<4; b++) {
170         type[b] = vp56_rac_gets(&s->c, 2);
171         if (type[b])
172             type[b]++;  /* only returns 0, 2, 3 or 4 (all INTER_PF) */
173     }
174
175     /* get vectors */
176     for (b=0; b<4; b++) {
177         switch (type[b]) {
178             case VP56_MB_INTER_NOVEC_PF:
179                 s->mv[b] = (VP56mv) {0,0};
180                 break;
181             case VP56_MB_INTER_DELTA_PF:
182                 s->parse_vector_adjustment(s, &s->mv[b]);
183                 break;
184             case VP56_MB_INTER_V1_PF:
185                 s->mv[b] = s->vector_candidate[0];
186                 break;
187             case VP56_MB_INTER_V2_PF:
188                 s->mv[b] = s->vector_candidate[1];
189                 break;
190         }
191         mv.x += s->mv[b].x;
192         mv.y += s->mv[b].y;
193     }
194
195     /* this is the one selected for the whole MB for prediction */
196     s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
197
198     /* chroma vectors are average luma vectors */
199     s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
200     s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
201 }
202
203 static VP56mb vp56_decode_mv(VP56Context *s, int row, int col)
204 {
205     VP56mv *mv, vect = {0,0};
206     int ctx, b;
207
208     ctx = vp56_get_vectors_predictors(s, row, col, VP56_FRAME_PREVIOUS);
209     s->mb_type = vp56_parse_mb_type(s, s->mb_type, ctx);
210     s->macroblocks[row * s->mb_width + col].type = s->mb_type;
211
212     switch (s->mb_type) {
213         case VP56_MB_INTER_V1_PF:
214             mv = &s->vector_candidate[0];
215             break;
216
217         case VP56_MB_INTER_V2_PF:
218             mv = &s->vector_candidate[1];
219             break;
220
221         case VP56_MB_INTER_V1_GF:
222             vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
223             mv = &s->vector_candidate[0];
224             break;
225
226         case VP56_MB_INTER_V2_GF:
227             vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
228             mv = &s->vector_candidate[1];
229             break;
230
231         case VP56_MB_INTER_DELTA_PF:
232             s->parse_vector_adjustment(s, &vect);
233             mv = &vect;
234             break;
235
236         case VP56_MB_INTER_DELTA_GF:
237             vp56_get_vectors_predictors(s, row, col, VP56_FRAME_GOLDEN);
238             s->parse_vector_adjustment(s, &vect);
239             mv = &vect;
240             break;
241
242         case VP56_MB_INTER_4V:
243             vp56_decode_4mv(s, row, col);
244             return s->mb_type;
245
246         default:
247             mv = &vect;
248             break;
249     }
250
251     s->macroblocks[row*s->mb_width + col].mv = *mv;
252
253     /* same vector for all blocks */
254     for (b=0; b<6; b++)
255         s->mv[b] = *mv;
256
257     return s->mb_type;
258 }
259
260 static VP56mb vp56_conceal_mv(VP56Context *s, int row, int col)
261 {
262     VP56mv *mv, vect = {0,0};
263     int b;
264
265     s->mb_type = VP56_MB_INTER_NOVEC_PF;
266     s->macroblocks[row * s->mb_width + col].type = s->mb_type;
267
268     mv = &vect;
269
270     s->macroblocks[row*s->mb_width + col].mv = *mv;
271
272     /* same vector for all blocks */
273     for (b=0; b<6; b++)
274         s->mv[b] = *mv;
275
276     return s->mb_type;
277 }
278
279 static void vp56_add_predictors_dc(VP56Context *s, VP56Frame ref_frame)
280 {
281     int idx = s->idct_scantable[0];
282     int b;
283
284     for (b=0; b<6; b++) {
285         VP56RefDc *ab = &s->above_blocks[s->above_block_idx[b]];
286         VP56RefDc *lb = &s->left_block[ff_vp56_b6to4[b]];
287         int count = 0;
288         int dc = 0;
289         int i;
290
291         if (ref_frame == lb->ref_frame) {
292             dc += lb->dc_coeff;
293             count++;
294         }
295         if (ref_frame == ab->ref_frame) {
296             dc += ab->dc_coeff;
297             count++;
298         }
299         if (s->avctx->codec->id == AV_CODEC_ID_VP5)
300             for (i=0; i<2; i++)
301                 if (count < 2 && ref_frame == ab[-1+2*i].ref_frame) {
302                     dc += ab[-1+2*i].dc_coeff;
303                     count++;
304                 }
305         if (count == 0)
306             dc = s->prev_dc[ff_vp56_b2p[b]][ref_frame];
307         else if (count == 2)
308             dc /= 2;
309
310         s->block_coeff[b][idx] += dc;
311         s->prev_dc[ff_vp56_b2p[b]][ref_frame] = s->block_coeff[b][idx];
312         ab->dc_coeff = s->block_coeff[b][idx];
313         ab->ref_frame = ref_frame;
314         lb->dc_coeff = s->block_coeff[b][idx];
315         lb->ref_frame = ref_frame;
316         s->block_coeff[b][idx] *= s->dequant_dc;
317     }
318 }
319
320 static void vp56_deblock_filter(VP56Context *s, uint8_t *yuv,
321                                 ptrdiff_t stride, int dx, int dy)
322 {
323     int t = ff_vp56_filter_threshold[s->quantizer];
324     if (dx)  s->vp56dsp.edge_filter_hor(yuv +         10-dx , stride, t);
325     if (dy)  s->vp56dsp.edge_filter_ver(yuv + stride*(10-dy), stride, t);
326 }
327
328 static void vp56_mc(VP56Context *s, int b, int plane, uint8_t *src,
329                     ptrdiff_t stride, int x, int y)
330 {
331     uint8_t *dst = s->frames[VP56_FRAME_CURRENT]->data[plane] + s->block_offset[b];
332     uint8_t *src_block;
333     int src_offset;
334     int overlap_offset = 0;
335     int mask = s->vp56_coord_div[b] - 1;
336     int deblock_filtering = s->deblock_filtering;
337     int dx;
338     int dy;
339
340     if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
341         (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
342          && !s->frames[VP56_FRAME_CURRENT]->key_frame))
343         deblock_filtering = 0;
344
345     dx = s->mv[b].x / s->vp56_coord_div[b];
346     dy = s->mv[b].y / s->vp56_coord_div[b];
347
348     if (b >= 4) {
349         x /= 2;
350         y /= 2;
351     }
352     x += dx - 2;
353     y += dy - 2;
354
355     if (x<0 || x+12>=s->plane_width[plane] ||
356         y<0 || y+12>=s->plane_height[plane]) {
357         s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
358                                  src + s->block_offset[b] + (dy-2)*stride + (dx-2),
359                                  stride, stride,
360                                  12, 12, x, y,
361                                  s->plane_width[plane],
362                                  s->plane_height[plane]);
363         src_block = s->edge_emu_buffer;
364         src_offset = 2 + 2*stride;
365     } else if (deblock_filtering) {
366         /* only need a 12x12 block, but there is no such dsp function, */
367         /* so copy a 16x12 block */
368         s->hdsp.put_pixels_tab[0][0](s->edge_emu_buffer,
369                                      src + s->block_offset[b] + (dy-2)*stride + (dx-2),
370                                      stride, 12);
371         src_block = s->edge_emu_buffer;
372         src_offset = 2 + 2*stride;
373     } else {
374         src_block = src;
375         src_offset = s->block_offset[b] + dy*stride + dx;
376     }
377
378     if (deblock_filtering)
379         vp56_deblock_filter(s, src_block, stride, dx&7, dy&7);
380
381     if (s->mv[b].x & mask)
382         overlap_offset += (s->mv[b].x > 0) ? 1 : -1;
383     if (s->mv[b].y & mask)
384         overlap_offset += (s->mv[b].y > 0) ? stride : -stride;
385
386     if (overlap_offset) {
387         if (s->filter)
388             s->filter(s, dst, src_block, src_offset, src_offset+overlap_offset,
389                       stride, s->mv[b], mask, s->filter_selection, b<4);
390         else
391             s->vp3dsp.put_no_rnd_pixels_l2(dst, src_block+src_offset,
392                                            src_block+src_offset+overlap_offset,
393                                            stride, 8);
394     } else {
395         s->hdsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
396     }
397 }
398
399 static av_always_inline void vp56_render_mb(VP56Context *s, int row, int col, int is_alpha, VP56mb mb_type)
400 {
401     int b, ab, b_max, plane, off;
402     AVFrame *frame_current, *frame_ref;
403     VP56Frame ref_frame = ff_vp56_reference_frame[mb_type];
404
405     vp56_add_predictors_dc(s, ref_frame);
406
407     frame_current = s->frames[VP56_FRAME_CURRENT];
408     frame_ref = s->frames[ref_frame];
409     if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
410         return;
411
412     ab = 6*is_alpha;
413     b_max = 6 - 2*is_alpha;
414
415     switch (mb_type) {
416         case VP56_MB_INTRA:
417             for (b=0; b<b_max; b++) {
418                 plane = ff_vp56_b2p[b+ab];
419                 s->vp3dsp.idct_put(frame_current->data[plane] + s->block_offset[b],
420                                 s->stride[plane], s->block_coeff[b]);
421             }
422             break;
423
424         case VP56_MB_INTER_NOVEC_PF:
425         case VP56_MB_INTER_NOVEC_GF:
426             for (b=0; b<b_max; b++) {
427                 plane = ff_vp56_b2p[b+ab];
428                 off = s->block_offset[b];
429                 s->hdsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
430                                              frame_ref->data[plane] + off,
431                                              s->stride[plane], 8);
432                 s->vp3dsp.idct_add(frame_current->data[plane] + off,
433                                 s->stride[plane], s->block_coeff[b]);
434             }
435             break;
436
437         case VP56_MB_INTER_DELTA_PF:
438         case VP56_MB_INTER_V1_PF:
439         case VP56_MB_INTER_V2_PF:
440         case VP56_MB_INTER_DELTA_GF:
441         case VP56_MB_INTER_4V:
442         case VP56_MB_INTER_V1_GF:
443         case VP56_MB_INTER_V2_GF:
444             for (b=0; b<b_max; b++) {
445                 int x_off = b==1 || b==3 ? 8 : 0;
446                 int y_off = b==2 || b==3 ? 8 : 0;
447                 plane = ff_vp56_b2p[b+ab];
448                 vp56_mc(s, b, plane, frame_ref->data[plane], s->stride[plane],
449                         16*col+x_off, 16*row+y_off);
450                 s->vp3dsp.idct_add(frame_current->data[plane] + s->block_offset[b],
451                                 s->stride[plane], s->block_coeff[b]);
452             }
453             break;
454     }
455
456     if (is_alpha) {
457         s->block_coeff[4][0] = 0;
458         s->block_coeff[5][0] = 0;
459     }
460 }
461
462 static int vp56_decode_mb(VP56Context *s, int row, int col, int is_alpha)
463 {
464     VP56mb mb_type;
465     int ret;
466
467     if (s->frames[VP56_FRAME_CURRENT]->key_frame)
468         mb_type = VP56_MB_INTRA;
469     else
470         mb_type = vp56_decode_mv(s, row, col);
471
472     ret = s->parse_coeff(s);
473     if (ret < 0)
474         return ret;
475
476     vp56_render_mb(s, row, col, is_alpha, mb_type);
477
478     return 0;
479 }
480
481 static int vp56_conceal_mb(VP56Context *s, int row, int col, int is_alpha)
482 {
483     VP56mb mb_type;
484
485     if (s->frames[VP56_FRAME_CURRENT]->key_frame)
486         mb_type = VP56_MB_INTRA;
487     else
488         mb_type = vp56_conceal_mv(s, row, col);
489
490     vp56_render_mb(s, row, col, is_alpha, mb_type);
491
492     return 0;
493 }
494
495 static int vp56_size_changed(VP56Context *s)
496 {
497     AVCodecContext *avctx = s->avctx;
498     int stride = s->frames[VP56_FRAME_CURRENT]->linesize[0];
499     int i;
500
501     s->plane_width[0]  = s->plane_width[3]  = avctx->coded_width;
502     s->plane_width[1]  = s->plane_width[2]  = avctx->coded_width/2;
503     s->plane_height[0] = s->plane_height[3] = avctx->coded_height;
504     s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
505
506     s->have_undamaged_frame = 0;
507
508     for (i=0; i<4; i++)
509         s->stride[i] = s->flip * s->frames[VP56_FRAME_CURRENT]->linesize[i];
510
511     s->mb_width  = (avctx->coded_width +15) / 16;
512     s->mb_height = (avctx->coded_height+15) / 16;
513
514     if (s->mb_width > 1000 || s->mb_height > 1000) {
515         ff_set_dimensions(avctx, 0, 0);
516         av_log(avctx, AV_LOG_ERROR, "picture too big\n");
517         return AVERROR_INVALIDDATA;
518     }
519
520     av_reallocp_array(&s->above_blocks, 4*s->mb_width+6,
521                       sizeof(*s->above_blocks));
522     av_reallocp_array(&s->macroblocks, s->mb_width*s->mb_height,
523                       sizeof(*s->macroblocks));
524     av_free(s->edge_emu_buffer_alloc);
525     s->edge_emu_buffer_alloc = av_malloc(16*stride);
526     s->edge_emu_buffer = s->edge_emu_buffer_alloc;
527     if (!s->above_blocks || !s->macroblocks || !s->edge_emu_buffer_alloc)
528         return AVERROR(ENOMEM);
529     if (s->flip < 0)
530         s->edge_emu_buffer += 15 * stride;
531
532     if (s->alpha_context)
533         return vp56_size_changed(s->alpha_context);
534
535     return 0;
536 }
537
538 static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *, int, int);
539
540 int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
541                          AVPacket *avpkt)
542 {
543     const uint8_t *buf = avpkt->data;
544     VP56Context *s = avctx->priv_data;
545     AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
546     int remaining_buf_size = avpkt->size;
547     int av_uninit(alpha_offset);
548     int i, res;
549     int ret;
550
551     if (s->has_alpha) {
552         if (remaining_buf_size < 3)
553             return AVERROR_INVALIDDATA;
554         alpha_offset = bytestream_get_be24(&buf);
555         remaining_buf_size -= 3;
556         if (remaining_buf_size < alpha_offset)
557             return AVERROR_INVALIDDATA;
558     }
559
560     res = s->parse_header(s, buf, remaining_buf_size);
561     if (res < 0)
562         return res;
563
564     if (res == VP56_SIZE_CHANGE) {
565         for (i = 0; i < 4; i++) {
566             av_frame_unref(s->frames[i]);
567             if (s->alpha_context)
568                 av_frame_unref(s->alpha_context->frames[i]);
569         }
570     }
571
572     ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF);
573     if (ret < 0) {
574         if (res == VP56_SIZE_CHANGE)
575             ff_set_dimensions(avctx, 0, 0);
576         return ret;
577     }
578
579     if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
580         av_frame_unref(s->alpha_context->frames[VP56_FRAME_CURRENT]);
581         if ((ret = av_frame_ref(s->alpha_context->frames[VP56_FRAME_CURRENT], p)) < 0) {
582             av_frame_unref(p);
583             if (res == VP56_SIZE_CHANGE)
584                 ff_set_dimensions(avctx, 0, 0);
585             return ret;
586         }
587     }
588
589     if (res == VP56_SIZE_CHANGE) {
590         if (vp56_size_changed(s)) {
591             av_frame_unref(p);
592             return AVERROR_INVALIDDATA;
593         }
594     }
595
596     if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
597         int bak_w = avctx->width;
598         int bak_h = avctx->height;
599         int bak_cw = avctx->coded_width;
600         int bak_ch = avctx->coded_height;
601         buf += alpha_offset;
602         remaining_buf_size -= alpha_offset;
603
604         res = s->alpha_context->parse_header(s->alpha_context, buf, remaining_buf_size);
605         if (res != 0) {
606             if(res==VP56_SIZE_CHANGE) {
607                 av_log(avctx, AV_LOG_ERROR, "Alpha reconfiguration\n");
608                 avctx->width  = bak_w;
609                 avctx->height = bak_h;
610                 avctx->coded_width  = bak_cw;
611                 avctx->coded_height = bak_ch;
612             }
613             av_frame_unref(p);
614             return AVERROR_INVALIDDATA;
615         }
616     }
617
618     s->discard_frame = 0;
619     avctx->execute2(avctx, ff_vp56_decode_mbs, 0, 0, (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) + 1);
620
621     if (s->discard_frame)
622         return AVERROR_INVALIDDATA;
623
624     if ((res = av_frame_ref(data, p)) < 0)
625         return res;
626     *got_frame = 1;
627
628     return avpkt->size;
629 }
630
631 static int ff_vp56_decode_mbs(AVCodecContext *avctx, void *data,
632                               int jobnr, int threadnr)
633 {
634     VP56Context *s0 = avctx->priv_data;
635     int is_alpha = (jobnr == 1);
636     VP56Context *s = is_alpha ? s0->alpha_context : s0;
637     AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
638     int mb_row, mb_col, mb_row_flip, mb_offset = 0;
639     int block, y, uv;
640     ptrdiff_t stride_y, stride_uv;
641     int res;
642     int damaged = 0;
643
644     if (p->key_frame) {
645         p->pict_type = AV_PICTURE_TYPE_I;
646         s->default_models_init(s);
647         for (block=0; block<s->mb_height*s->mb_width; block++)
648             s->macroblocks[block].type = VP56_MB_INTRA;
649     } else {
650         p->pict_type = AV_PICTURE_TYPE_P;
651         vp56_parse_mb_type_models(s);
652         s->parse_vector_models(s);
653         s->mb_type = VP56_MB_INTER_NOVEC_PF;
654     }
655
656     if (s->parse_coeff_models(s))
657         goto next;
658
659     memset(s->prev_dc, 0, sizeof(s->prev_dc));
660     s->prev_dc[1][VP56_FRAME_CURRENT] = 128;
661     s->prev_dc[2][VP56_FRAME_CURRENT] = 128;
662
663     for (block=0; block < 4*s->mb_width+6; block++) {
664         s->above_blocks[block].ref_frame = VP56_FRAME_NONE;
665         s->above_blocks[block].dc_coeff = 0;
666         s->above_blocks[block].not_null_dc = 0;
667     }
668     s->above_blocks[2*s->mb_width + 2].ref_frame = VP56_FRAME_CURRENT;
669     s->above_blocks[3*s->mb_width + 4].ref_frame = VP56_FRAME_CURRENT;
670
671     stride_y  = p->linesize[0];
672     stride_uv = p->linesize[1];
673
674     if (s->flip < 0)
675         mb_offset = 7;
676
677     /* main macroblocks loop */
678     for (mb_row=0; mb_row<s->mb_height; mb_row++) {
679         if (s->flip < 0)
680             mb_row_flip = s->mb_height - mb_row - 1;
681         else
682             mb_row_flip = mb_row;
683
684         for (block=0; block<4; block++) {
685             s->left_block[block].ref_frame = VP56_FRAME_NONE;
686             s->left_block[block].dc_coeff = 0;
687             s->left_block[block].not_null_dc = 0;
688         }
689         memset(s->coeff_ctx, 0, sizeof(s->coeff_ctx));
690         memset(s->coeff_ctx_last, 24, sizeof(s->coeff_ctx_last));
691
692         s->above_block_idx[0] = 1;
693         s->above_block_idx[1] = 2;
694         s->above_block_idx[2] = 1;
695         s->above_block_idx[3] = 2;
696         s->above_block_idx[4] = 2*s->mb_width + 2 + 1;
697         s->above_block_idx[5] = 3*s->mb_width + 4 + 1;
698
699         s->block_offset[s->frbi] = (mb_row_flip*16 + mb_offset) * stride_y;
700         s->block_offset[s->srbi] = s->block_offset[s->frbi] + 8*stride_y;
701         s->block_offset[1] = s->block_offset[0] + 8;
702         s->block_offset[3] = s->block_offset[2] + 8;
703         s->block_offset[4] = (mb_row_flip*8 + mb_offset) * stride_uv;
704         s->block_offset[5] = s->block_offset[4];
705
706         for (mb_col=0; mb_col<s->mb_width; mb_col++) {
707             if (!damaged) {
708                 int ret = vp56_decode_mb(s, mb_row, mb_col, is_alpha);
709                 if (ret < 0) {
710                     damaged = 1;
711                     if (!s->have_undamaged_frame || !avctx->error_concealment) {
712                         s->discard_frame = 1;
713                         return AVERROR_INVALIDDATA;
714                     }
715                 }
716             }
717             if (damaged)
718                 vp56_conceal_mb(s, mb_row, mb_col, is_alpha);
719
720             for (y=0; y<4; y++) {
721                 s->above_block_idx[y] += 2;
722                 s->block_offset[y] += 16;
723             }
724
725             for (uv=4; uv<6; uv++) {
726                 s->above_block_idx[uv] += 1;
727                 s->block_offset[uv] += 8;
728             }
729         }
730     }
731
732     if (!damaged)
733         s->have_undamaged_frame = 1;
734
735 next:
736     if (p->key_frame || s->golden_frame) {
737         av_frame_unref(s->frames[VP56_FRAME_GOLDEN]);
738         if ((res = av_frame_ref(s->frames[VP56_FRAME_GOLDEN], p)) < 0)
739             return res;
740     }
741
742     av_frame_unref(s->frames[VP56_FRAME_PREVIOUS]);
743     FFSWAP(AVFrame *, s->frames[VP56_FRAME_CURRENT],
744                       s->frames[VP56_FRAME_PREVIOUS]);
745     return 0;
746 }
747
748 av_cold int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
749 {
750     VP56Context *s = avctx->priv_data;
751     return ff_vp56_init_context(avctx, s, flip, has_alpha);
752 }
753
754 av_cold int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
755                                   int flip, int has_alpha)
756 {
757     int i;
758
759     s->avctx = avctx;
760     avctx->pix_fmt = has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
761     if (avctx->skip_alpha) avctx->pix_fmt = AV_PIX_FMT_YUV420P;
762
763     ff_h264chroma_init(&s->h264chroma, 8);
764     ff_hpeldsp_init(&s->hdsp, avctx->flags);
765     ff_videodsp_init(&s->vdsp, 8);
766     ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
767     for (i = 0; i < 64; i++) {
768 #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
769         s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
770 #undef TRANSPOSE
771     }
772
773     for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
774         s->frames[i] = av_frame_alloc();
775         if (!s->frames[i]) {
776             ff_vp56_free(avctx);
777             return AVERROR(ENOMEM);
778         }
779     }
780     s->edge_emu_buffer_alloc = NULL;
781
782     s->above_blocks = NULL;
783     s->macroblocks = NULL;
784     s->quantizer = -1;
785     s->deblock_filtering = 1;
786     s->golden_frame = 0;
787
788     s->filter = NULL;
789
790     s->has_alpha = has_alpha;
791
792     s->modelp = &s->model;
793
794     if (flip) {
795         s->flip = -1;
796         s->frbi = 2;
797         s->srbi = 0;
798     } else {
799         s->flip = 1;
800         s->frbi = 0;
801         s->srbi = 2;
802     }
803
804     return 0;
805 }
806
807 av_cold int ff_vp56_free(AVCodecContext *avctx)
808 {
809     VP56Context *s = avctx->priv_data;
810     return ff_vp56_free_context(s);
811 }
812
813 av_cold int ff_vp56_free_context(VP56Context *s)
814 {
815     int i;
816
817     av_freep(&s->above_blocks);
818     av_freep(&s->macroblocks);
819     av_freep(&s->edge_emu_buffer_alloc);
820
821     for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
822         av_frame_free(&s->frames[i]);
823
824     return 0;
825 }