]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp9mvs.c
lavfi: Drop unused and empty header file
[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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; 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 = b->row, col = b->col, row7 = b->row7;
69     const int8_t (*p)[2] = mv_ref_blk_off[b->bs];
70 #define INVALID_MV 0x80008000U
71     uint32_t mem = 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             clamp_mv(&tmp, &mv, s);             \
103             m = AV_RN32A(&tmp);                 \
104             if (!idx) {                         \
105                 AV_WN32A(pmv, m);               \
106                 return;                         \
107             } else if (mem == INVALID_MV) {     \
108                 mem = m;                        \
109             } else if (m != mem) {              \
110                 AV_WN32A(pmv, m);               \
111                 return;                         \
112             }                                   \
113         } else {                                \
114             uint32_t m = AV_RN32A(&mv);         \
115             if (!idx) {                         \
116                 clamp_mv(pmv, &mv, s);          \
117                 return;                         \
118             } else if (mem == INVALID_MV) {     \
119                 mem = m;                        \
120             } else if (m != mem) {              \
121                 clamp_mv(pmv, &mv, s);          \
122                 return;                         \
123             }                                   \
124         }                                       \
125     } while (0)
126
127         if (row > 0) {
128             VP9MVRefPair *mv = &s->frames[CUR_FRAME].mv[(row - 1) * s->sb_cols * 8 + col];
129
130             if (mv->ref[0] == ref)
131                 RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][0]);
132             else if (mv->ref[1] == ref)
133                 RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][1]);
134         }
135         if (col > s->tiling.tile_col_start) {
136             VP9MVRefPair *mv = &s->frames[CUR_FRAME].mv[row * s->sb_cols * 8 + col - 1];
137
138             if (mv->ref[0] == ref)
139                 RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][0]);
140             else if (mv->ref[1] == ref)
141                 RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][1]);
142         }
143         i = 2;
144     } else {
145         i = 0;
146     }
147
148     // previously coded MVs in the neighborhood, using same reference frame
149     for (; i < 8; i++) {
150         int c = p[i][0] + col, r = p[i][1] + row;
151
152         if (c >= s->tiling.tile_col_start && c < s->cols &&
153             r >= 0 && r < s->rows) {
154             VP9MVRefPair *mv = &s->frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
155
156             if (mv->ref[0] == ref)
157                 RETURN_MV(mv->mv[0]);
158             else if (mv->ref[1] == ref)
159                 RETURN_MV(mv->mv[1]);
160         }
161     }
162
163     // MV at this position in previous frame, using same reference frame
164     if (s->use_last_frame_mvs) {
165         VP9MVRefPair *mv = &s->frames[LAST_FRAME].mv[row * s->sb_cols * 8 + col];
166
167         if (!s->last_uses_2pass)
168             ff_thread_await_progress(&s->frames[LAST_FRAME].tf, row >> 3, 0);
169
170         if (mv->ref[0] == ref)
171             RETURN_MV(mv->mv[0]);
172         else if (mv->ref[1] == ref)
173             RETURN_MV(mv->mv[1]);
174     }
175
176 #define RETURN_SCALE_MV(mv, scale)              \
177     do {                                        \
178         if (scale) {                            \
179             VP56mv mv_temp = { -mv.x, -mv.y };  \
180             RETURN_MV(mv_temp);                 \
181         } else {                                \
182             RETURN_MV(mv);                      \
183         }                                       \
184     } while (0)
185
186     // previously coded MVs in the neighborhood, using different reference frame
187     for (i = 0; i < 8; i++) {
188         int c = p[i][0] + col, r = p[i][1] + row;
189
190         if (c >= s->tiling.tile_col_start && c < s->cols &&
191             r >= 0 && r < s->rows) {
192             VP9MVRefPair *mv = &s->frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
193
194             if (mv->ref[0] != ref && mv->ref[0] >= 0)
195                 RETURN_SCALE_MV(mv->mv[0],
196                                 s->signbias[mv->ref[0]] != s->signbias[ref]);
197             if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
198                 // BUG - libvpx has this condition regardless of whether
199                 // we used the first ref MV and pre-scaling
200                 AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
201                 RETURN_SCALE_MV(mv->mv[1],
202                                 s->signbias[mv->ref[1]] != s->signbias[ref]);
203             }
204         }
205     }
206
207     // MV at this position in previous frame, using different reference frame
208     if (s->use_last_frame_mvs) {
209         VP9MVRefPair *mv = &s->frames[LAST_FRAME].mv[row * s->sb_cols * 8 + col];
210
211         // no need to await_progress, because we already did that above
212         if (mv->ref[0] != ref && mv->ref[0] >= 0)
213             RETURN_SCALE_MV(mv->mv[0],
214                             s->signbias[mv->ref[0]] != s->signbias[ref]);
215         if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
216             // BUG - libvpx has this condition regardless of whether
217             // we used the first ref MV and pre-scaling
218             AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
219             RETURN_SCALE_MV(mv->mv[1],
220                             s->signbias[mv->ref[1]] != s->signbias[ref]);
221         }
222     }
223
224     AV_ZERO32(pmv);
225 #undef INVALID_MV
226 #undef RETURN_MV
227 #undef RETURN_SCALE_MV
228 }
229
230 static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
231 {
232     int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
233     int n, c = vp8_rac_get_tree(&s->c, ff_vp9_mv_class_tree,
234                                 s->prob.p.mv_comp[idx].classes);
235
236     s->counts.mv_comp[idx].sign[sign]++;
237     s->counts.mv_comp[idx].classes[c]++;
238     if (c) {
239         int m;
240
241         for (n = 0, m = 0; m < c; m++) {
242             bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
243             n  |= bit << m;
244             s->counts.mv_comp[idx].bits[m][bit]++;
245         }
246         n <<= 3;
247         bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
248                                s->prob.p.mv_comp[idx].fp);
249         n  |= bit << 1;
250         s->counts.mv_comp[idx].fp[bit]++;
251         if (hp) {
252             bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
253             s->counts.mv_comp[idx].hp[bit]++;
254             n |= bit;
255         } else {
256             n |= 1;
257             // bug in libvpx - we count for bw entropy purposes even if the
258             // bit wasn't coded
259             s->counts.mv_comp[idx].hp[1]++;
260         }
261         n += 8 << c;
262     } else {
263         n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
264         s->counts.mv_comp[idx].class0[n]++;
265         bit = vp8_rac_get_tree(&s->c, ff_vp9_mv_fp_tree,
266                                s->prob.p.mv_comp[idx].class0_fp[n]);
267         s->counts.mv_comp[idx].class0_fp[n][bit]++;
268         n = (n << 3) | (bit << 1);
269         if (hp) {
270             bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
271             s->counts.mv_comp[idx].class0_hp[bit]++;
272             n |= bit;
273         } else {
274             n |= 1;
275             // bug in libvpx - we count for bw entropy purposes even if the
276             // bit wasn't coded
277             s->counts.mv_comp[idx].class0_hp[1]++;
278         }
279     }
280
281     return sign ? -(n + 1) : (n + 1);
282 }
283
284 void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb)
285 {
286     VP9Block *b = s->b;
287
288     if (mode == ZEROMV) {
289         memset(mv, 0, sizeof(*mv) * 2);
290     } else {
291         int hp;
292
293         // FIXME cache this value and reuse for other subblocks
294         find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
295                      mode == NEWMV ? -1 : sb);
296         // FIXME maybe move this code into find_ref_mvs()
297         if ((mode == NEWMV || sb == -1) &&
298             !(hp = s->highprecisionmvs &&
299               abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
300             if (mv[0].y & 1) {
301                 if (mv[0].y < 0)
302                     mv[0].y++;
303                 else
304                     mv[0].y--;
305             }
306             if (mv[0].x & 1) {
307                 if (mv[0].x < 0)
308                     mv[0].x++;
309                 else
310                     mv[0].x--;
311             }
312         }
313         if (mode == NEWMV) {
314             enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
315                                               s->prob.p.mv_joint);
316
317             s->counts.mv_joint[j]++;
318             if (j >= MV_JOINT_V)
319                 mv[0].y += read_mv_component(s, 0, hp);
320             if (j & 1)
321                 mv[0].x += read_mv_component(s, 1, hp);
322         }
323
324         if (b->comp) {
325             // FIXME cache this value and reuse for other subblocks
326             find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
327                          mode == NEWMV ? -1 : sb);
328             if ((mode == NEWMV || sb == -1) &&
329                 !(hp = s->highprecisionmvs &&
330                   abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
331                 if (mv[1].y & 1) {
332                     if (mv[1].y < 0)
333                         mv[1].y++;
334                     else
335                         mv[1].y--;
336                 }
337                 if (mv[1].x & 1) {
338                     if (mv[1].x < 0)
339                         mv[1].x++;
340                     else
341                         mv[1].x--;
342                 }
343             }
344             if (mode == NEWMV) {
345                 enum MVJoint j = vp8_rac_get_tree(&s->c, ff_vp9_mv_joint_tree,
346                                                   s->prob.p.mv_joint);
347
348                 s->counts.mv_joint[j]++;
349                 if (j >= MV_JOINT_V)
350                     mv[1].y += read_mv_component(s, 0, hp);
351                 if (j & 1)
352                     mv[1].x += read_mv_component(s, 1, hp);
353             }
354         }
355     }
356 }