]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp9mvs.c
Merge commit 'ecd2ec69ce10e13f6ede353d2def7ce9f45c1a7d'
[ffmpeg] / libavcodec / vp9mvs.c
1 /*
2  * VP9 compatible video decoder
3  *
4  * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5  * Copyright (C) 2013 Clément Bœsch <u pkh me>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "internal.h"
25 #include "vp56.h"
26 #include "vp9.h"
27 #include "vp9data.h"
28
29 static av_always_inline void clamp_mv(VP56mv *dst, const VP56mv *src,
30                                       VP9Context *s)
31 {
32     dst->x = av_clip(src->x, s->min_mv.x, s->max_mv.x);
33     dst->y = av_clip(src->y, s->min_mv.y, s->max_mv.y);
34 }
35
36 static void find_ref_mvs(VP9Context *s,
37                          VP56mv *pmv, int ref, int z, int idx, int sb)
38 {
39     static const int8_t mv_ref_blk_off[N_BS_SIZES][8][2] = {
40         [BS_64x64] = { {  3, -1 }, { -1,  3 }, {  4, -1 }, { -1,  4 },
41                        { -1, -1 }, {  0, -1 }, { -1,  0 }, {  6, -1 } },
42         [BS_64x32] = { {  0, -1 }, { -1,  0 }, {  4, -1 }, { -1,  2 },
43                        { -1, -1 }, {  0, -3 }, { -3,  0 }, {  2, -1 } },
44         [BS_32x64] = { { -1,  0 }, {  0, -1 }, { -1,  4 }, {  2, -1 },
45                        { -1, -1 }, { -3,  0 }, {  0, -3 }, { -1,  2 } },
46         [BS_32x32] = { {  1, -1 }, { -1,  1 }, {  2, -1 }, { -1,  2 },
47                        { -1, -1 }, {  0, -3 }, { -3,  0 }, { -3, -3 } },
48         [BS_32x16] = { {  0, -1 }, { -1,  0 }, {  2, -1 }, { -1, -1 },
49                        { -1,  1 }, {  0, -3 }, { -3,  0 }, { -3, -3 } },
50         [BS_16x32] = { { -1,  0 }, {  0, -1 }, { -1,  2 }, { -1, -1 },
51                        {  1, -1 }, { -3,  0 }, {  0, -3 }, { -3, -3 } },
52         [BS_16x16] = { {  0, -1 }, { -1,  0 }, {  1, -1 }, { -1,  1 },
53                        { -1, -1 }, {  0, -3 }, { -3,  0 }, { -3, -3 } },
54         [BS_16x8]  = { {  0, -1 }, { -1,  0 }, {  1, -1 }, { -1, -1 },
55                        {  0, -2 }, { -2,  0 }, { -2, -1 }, { -1, -2 } },
56         [BS_8x16]  = { { -1,  0 }, {  0, -1 }, { -1,  1 }, { -1, -1 },
57                        { -2,  0 }, {  0, -2 }, { -1, -2 }, { -2, -1 } },
58         [BS_8x8]   = { {  0, -1 }, { -1,  0 }, { -1, -1 }, {  0, -2 },
59                        { -2,  0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
60         [BS_8x4]   = { {  0, -1 }, { -1,  0 }, { -1, -1 }, {  0, -2 },
61                        { -2,  0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
62         [BS_4x8]   = { {  0, -1 }, { -1,  0 }, { -1, -1 }, {  0, -2 },
63                        { -2,  0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
64         [BS_4x4]   = { {  0, -1 }, { -1,  0 }, { -1, -1 }, {  0, -2 },
65                        { -2,  0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
66     };
67     VP9Block *b = s->b;
68     int row = s->row, col = s->col, row7 = s->row7;
69     const int8_t (*p)[2] = mv_ref_blk_off[b->bs];
70 #define INVALID_MV 0x80008000U
71     uint32_t mem = INVALID_MV, mem_sub8x8 = INVALID_MV;
72     int i;
73
74 #define RETURN_DIRECT_MV(mv)                    \
75     do {                                        \
76         uint32_t m = AV_RN32A(&mv);             \
77         if (!idx) {                             \
78             AV_WN32A(pmv, m);                   \
79             return;                             \
80         } else if (mem == INVALID_MV) {         \
81             mem = m;                            \
82         } else if (m != mem) {                  \
83             AV_WN32A(pmv, m);                   \
84             return;                             \
85         }                                       \
86     } while (0)
87
88     if (sb >= 0) {
89         if (sb == 2 || sb == 1) {
90             RETURN_DIRECT_MV(b->mv[0][z]);
91         } else if (sb == 3) {
92             RETURN_DIRECT_MV(b->mv[2][z]);
93             RETURN_DIRECT_MV(b->mv[1][z]);
94             RETURN_DIRECT_MV(b->mv[0][z]);
95         }
96
97 #define RETURN_MV(mv)                                                  \
98     do {                                                               \
99         if (sb > 0) {                                                  \
100             VP56mv tmp;                                                \
101             uint32_t m;                                                \
102             av_assert2(idx == 1);                                      \
103             av_assert2(mem != INVALID_MV);                             \
104             if (mem_sub8x8 == INVALID_MV) {                            \
105                 clamp_mv(&tmp, &mv, s);                                \
106                 m = AV_RN32A(&tmp);                                    \
107                 if (m != mem) {                                        \
108                     AV_WN32A(pmv, m);                                  \
109                     return;                                            \
110                 }                                                      \
111                 mem_sub8x8 = AV_RN32A(&mv);                            \
112             } else if (mem_sub8x8 != AV_RN32A(&mv)) {                  \
113                 clamp_mv(&tmp, &mv, s);                                \
114                 m = AV_RN32A(&tmp);                                    \
115                 if (m != mem) {                                        \
116                     AV_WN32A(pmv, m);                                  \
117                 } else {                                               \
118                     /* BUG I'm pretty sure this isn't the intention */ \
119                     AV_WN32A(pmv, 0);                                  \
120                 }                                                      \
121                 return;                                                \
122             }                                                          \
123         } else {                                                       \
124             uint32_t m = AV_RN32A(&mv);                                \
125             if (!idx) {                                                \
126                 clamp_mv(pmv, &mv, s);                                 \
127                 return;                                                \
128             } else if (mem == INVALID_MV) {                            \
129                 mem = m;                                               \
130             } else if (m != mem) {                                     \
131                 clamp_mv(pmv, &mv, s);                                 \
132                 return;                                                \
133             }                                                          \
134         }                                                              \
135     } while (0)
136
137         if (row > 0) {
138             VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[(row - 1) * s->sb_cols * 8 + col];
139             if (mv->ref[0] == ref)
140                 RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][0]);
141             else if (mv->ref[1] == ref)
142                 RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][1]);
143         }
144         if (col > s->tile_col_start) {
145             VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[row * s->sb_cols * 8 + col - 1];
146             if (mv->ref[0] == ref)
147                 RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][0]);
148             else if (mv->ref[1] == ref)
149                 RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][1]);
150         }
151         i = 2;
152     } else {
153         i = 0;
154     }
155
156     // previously coded MVs in this neighborhood, using same reference frame
157     for (; i < 8; i++) {
158         int c = p[i][0] + col, r = p[i][1] + row;
159
160         if (c >= s->tile_col_start && c < s->cols &&
161             r >= 0 && r < s->rows) {
162             VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
163
164             if (mv->ref[0] == ref)
165                 RETURN_MV(mv->mv[0]);
166             else if (mv->ref[1] == ref)
167                 RETURN_MV(mv->mv[1]);
168         }
169     }
170
171     // MV at this position in previous frame, using same reference frame
172     if (s->s.h.use_last_frame_mvs) {
173         VP9mvrefPair *mv = &s->s.frames[REF_FRAME_MVPAIR].mv[row * s->sb_cols * 8 + col];
174
175         if (!s->s.frames[REF_FRAME_MVPAIR].uses_2pass)
176             ff_thread_await_progress(&s->s.frames[REF_FRAME_MVPAIR].tf, row >> 3, 0);
177         if (mv->ref[0] == ref)
178             RETURN_MV(mv->mv[0]);
179         else if (mv->ref[1] == ref)
180             RETURN_MV(mv->mv[1]);
181     }
182
183 #define RETURN_SCALE_MV(mv, scale)              \
184     do {                                        \
185         if (scale) {                            \
186             VP56mv mv_temp = { -mv.x, -mv.y };  \
187             RETURN_MV(mv_temp);                 \
188         } else {                                \
189             RETURN_MV(mv);                      \
190         }                                       \
191     } while (0)
192
193     // previously coded MVs in this neighborhood, using different reference frame
194     for (i = 0; i < 8; i++) {
195         int c = p[i][0] + col, r = p[i][1] + row;
196
197         if (c >= s->tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
198             VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
199
200             if (mv->ref[0] != ref && mv->ref[0] >= 0)
201                 RETURN_SCALE_MV(mv->mv[0],
202                                 s->s.h.signbias[mv->ref[0]] != s->s.h.signbias[ref]);
203             if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
204                 // BUG - libvpx has this condition regardless of whether
205                 // we used the first ref MV and pre-scaling
206                 AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
207                 RETURN_SCALE_MV(mv->mv[1], s->s.h.signbias[mv->ref[1]] != s->s.h.signbias[ref]);
208             }
209         }
210     }
211
212     // MV at this position in previous frame, using different reference frame
213     if (s->s.h.use_last_frame_mvs) {
214         VP9mvrefPair *mv = &s->s.frames[REF_FRAME_MVPAIR].mv[row * s->sb_cols * 8 + col];
215
216         // no need to await_progress, because we already did that above
217         if (mv->ref[0] != ref && mv->ref[0] >= 0)
218             RETURN_SCALE_MV(mv->mv[0], s->s.h.signbias[mv->ref[0]] != s->s.h.signbias[ref]);
219         if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
220             // BUG - libvpx has this condition regardless of whether
221             // we used the first ref MV and pre-scaling
222             AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
223             RETURN_SCALE_MV(mv->mv[1], s->s.h.signbias[mv->ref[1]] != s->s.h.signbias[ref]);
224         }
225     }
226
227     AV_ZERO32(pmv);
228     clamp_mv(pmv, pmv, s);
229 #undef INVALID_MV
230 #undef RETURN_MV
231 #undef RETURN_SCALE_MV
232 }
233
234 static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
235 {
236     int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
237     int n, c = vp8_rac_get_tree(&s->c, ff_vp9_mv_class_tree,
238                                 s->prob.p.mv_comp[idx].classes);
239
240     s->counts.mv_comp[idx].sign[sign]++;
241     s->counts.mv_comp[idx].classes[c]++;
242     if (c) {
243         int m;
244
245         for (n = 0, m = 0; m < c; m++) {
246             bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
247             n |= bit << m;
248             s->counts.mv_comp[idx].bits[m][bit]++;
249         }
250         n <<= 3;
251         bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
252                                s->prob.p.mv_comp[idx].fp);
253         n  |= bit << 1;
254         s->counts.mv_comp[idx].fp[bit]++;
255         if (hp) {
256             bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
257             s->counts.mv_comp[idx].hp[bit]++;
258             n |= bit;
259         } else {
260             n |= 1;
261             // bug in libvpx - we count for bw entropy purposes even if the
262             // bit wasn't coded
263             s->counts.mv_comp[idx].hp[1]++;
264         }
265         n += 8 << c;
266     } else {
267         n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
268         s->counts.mv_comp[idx].class0[n]++;
269         bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
270                                s->prob.p.mv_comp[idx].class0_fp[n]);
271         s->counts.mv_comp[idx].class0_fp[n][bit]++;
272         n = (n << 3) | (bit << 1);
273         if (hp) {
274             bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
275             s->counts.mv_comp[idx].class0_hp[bit]++;
276             n |= bit;
277         } else {
278             n |= 1;
279             // bug in libvpx - we count for bw entropy purposes even if the
280             // bit wasn't coded
281             s->counts.mv_comp[idx].class0_hp[1]++;
282         }
283     }
284
285     return sign ? -(n + 1) : (n + 1);
286 }
287
288 void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb)
289 {
290     VP9Block *b = s->b;
291
292     if (mode == ZEROMV) {
293         AV_ZERO64(mv);
294     } else {
295         int hp;
296
297         // FIXME cache this value and reuse for other subblocks
298         find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
299                      mode == NEWMV ? -1 : sb);
300         // FIXME maybe move this code into find_ref_mvs()
301         if ((mode == NEWMV || sb == -1) &&
302             !(hp = s->s.h.highprecisionmvs &&
303               abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
304             if (mv[0].y & 1) {
305                 if (mv[0].y < 0)
306                     mv[0].y++;
307                 else
308                     mv[0].y--;
309             }
310             if (mv[0].x & 1) {
311                 if (mv[0].x < 0)
312                     mv[0].x++;
313                 else
314                     mv[0].x--;
315             }
316         }
317         if (mode == NEWMV) {
318             enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
319                                               s->prob.p.mv_joint);
320
321             s->counts.mv_joint[j]++;
322             if (j >= MV_JOINT_V)
323                 mv[0].y += read_mv_component(s, 0, hp);
324             if (j & 1)
325                 mv[0].x += read_mv_component(s, 1, hp);
326         }
327
328         if (b->comp) {
329             // FIXME cache this value and reuse for other subblocks
330             find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
331                          mode == NEWMV ? -1 : sb);
332             if ((mode == NEWMV || sb == -1) &&
333                 !(hp = s->s.h.highprecisionmvs &&
334                   abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
335                 if (mv[1].y & 1) {
336                     if (mv[1].y < 0)
337                         mv[1].y++;
338                     else
339                         mv[1].y--;
340                 }
341                 if (mv[1].x & 1) {
342                     if (mv[1].x < 0)
343                         mv[1].x++;
344                     else
345                         mv[1].x--;
346                 }
347             }
348             if (mode == NEWMV) {
349                 enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
350                                                   s->prob.p.mv_joint);
351
352                 s->counts.mv_joint[j]++;
353                 if (j >= MV_JOINT_V)
354                     mv[1].y += read_mv_component(s, 0, hp);
355                 if (j & 1)
356                     mv[1].x += read_mv_component(s, 1, hp);
357             }
358         }
359     }
360 }