1 /*
2 * Copyright © 2020, VideoLAN and dav1d authors
3 * Copyright © 2020, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef DAV1D_SRC_REF_MVS_H
29 #define DAV1D_SRC_REF_MVS_H
30
31 #include <stdint.h>
32
33 #include "dav1d/headers.h"
34
35 #include "common/intops.h"
36
37 #include "src/intra_edge.h"
38 #include "src/tables.h"
39
40 #define INVALID_MV 0x80008000
41
42 PACKED(typedef struct refmvs_temporal_block {
43 mv mv;
44 int8_t ref;
45 }) refmvs_temporal_block;
46
47 typedef union refmvs_refpair {
48 int8_t ref[2]; // [0] = 0: intra=1, [1] = -1: comp=0
49 uint16_t pair;
50 } refmvs_refpair;
51
52 typedef union refmvs_mvpair {
53 mv mv[2];
54 uint64_t n;
55 } refmvs_mvpair;
56
57 PACKED(typedef struct refmvs_block {
58 refmvs_mvpair mv;
59 refmvs_refpair ref;
60 uint8_t bs, mf; // 1 = globalmv+affine, 2 = newmv
61 }) ALIGN(refmvs_block, 4);
62
63 typedef struct refmvs_frame {
64 const Dav1dFrameHeader *frm_hdr;
65 int iw4, ih4, iw8, ih8;
66 int sbsz;
67 int use_ref_frame_mvs;
68 uint8_t sign_bias[7], mfmv_sign[7];
69 int8_t pocdiff[7];
70 uint8_t mfmv_ref[3];
71 int mfmv_ref2cur[3];
72 int mfmv_ref2ref[3][7];
73 int n_mfmvs;
74
75 refmvs_temporal_block *rp;
76 /*const*/ refmvs_temporal_block *const *rp_ref;
77 refmvs_temporal_block *rp_proj;
78 ptrdiff_t rp_stride;
79
80 refmvs_block *r; // 35 x r_stride memory
81 ptrdiff_t r_stride;
82 int n_tile_rows, n_tile_threads, n_frame_threads;
83 } refmvs_frame;
84
85 typedef struct refmvs_tile {
86 const refmvs_frame *rf;
87 refmvs_block *r[32 + 5];
88 refmvs_temporal_block *rp_proj;
89 struct {
90 int start, end;
91 } tile_col, tile_row;
92 } refmvs_tile;
93
94 typedef struct refmvs_candidate {
95 refmvs_mvpair mv;
96 int weight;
97 } refmvs_candidate;
98
99 // initialize temporal MVs; this can be done in any configuration, e.g. one
100 // tile/sbrow at a time, where col_{start,end}8 are the tile boundaries; or
101 // it can just be for the whole frame's sbrow, where col_{start,end}8 are the
102 // frame boundaries. row_{start,end}8 are the superblock row boundaries.
103 #define decl_load_tmvs_fn(name) \
104 void (name)(const refmvs_frame *rf, int tile_row_idx, \
105 int col_start8, int col_end8, int row_start8, int row_end8)
106 typedef decl_load_tmvs_fn(*load_tmvs_fn);
107
108 #define decl_save_tmvs_fn(name) \
109 void (name)(refmvs_temporal_block *rp, const ptrdiff_t stride, \
110 refmvs_block *const *const rr, const uint8_t *const ref_sign, \
111 int col_end8, int row_end8, int col_start8, int row_start8)
112 typedef decl_save_tmvs_fn(*save_tmvs_fn);
113
114 #define decl_splat_mv_fn(name) \
115 void (name)(refmvs_block **rr, const refmvs_block *rmv, int bx4, int bw4, int bh4)
116 typedef decl_splat_mv_fn(*splat_mv_fn);
117
118 typedef struct Dav1dRefmvsDSPContext {
119 load_tmvs_fn load_tmvs;
120 save_tmvs_fn save_tmvs;
121 splat_mv_fn splat_mv;
122 } Dav1dRefmvsDSPContext;
123
124 // call once per frame thread
125 void dav1d_refmvs_init(refmvs_frame *rf);
126 void dav1d_refmvs_clear(refmvs_frame *rf);
127
128 // call once per frame
129 int dav1d_refmvs_init_frame(refmvs_frame *rf,
130 const Dav1dSequenceHeader *seq_hdr,
131 const Dav1dFrameHeader *frm_hdr,
132 const unsigned ref_poc[7],
133 refmvs_temporal_block *rp,
134 const unsigned ref_ref_poc[7][7],
135 /*const*/ refmvs_temporal_block *const rp_ref[7],
136 int n_tile_threads, int n_frame_threads);
137
138 // cache the current tile/sbrow (or frame/sbrow)'s projectable motion vectors
139 // into buffers for use in future frame's temporal MV prediction
dav1d_refmvs_save_tmvs(const Dav1dRefmvsDSPContext * const dsp,const refmvs_tile * const rt,const int col_start8,int col_end8,const int row_start8,int row_end8)140 static inline void dav1d_refmvs_save_tmvs(const Dav1dRefmvsDSPContext *const dsp,
141 const refmvs_tile *const rt,
142 const int col_start8, int col_end8,
143 const int row_start8, int row_end8)
144 {
145 const refmvs_frame *const rf = rt->rf;
146
147 assert(row_start8 >= 0);
148 assert((unsigned) (row_end8 - row_start8) <= 16U);
149 row_end8 = imin(row_end8, rf->ih8);
150 col_end8 = imin(col_end8, rf->iw8);
151
152 const ptrdiff_t stride = rf->rp_stride;
153 const uint8_t *const ref_sign = rf->mfmv_sign;
154 refmvs_temporal_block *rp = &rf->rp[row_start8 * stride];
155
156 dsp->save_tmvs(rp, stride, rt->r + 6, ref_sign,
157 col_end8, row_end8, col_start8, row_start8);
158 }
159
160 // initialize tile boundaries and refmvs_block pointers for one tile/sbrow
161 void dav1d_refmvs_tile_sbrow_init(refmvs_tile *rt, const refmvs_frame *rf,
162 int tile_col_start4, int tile_col_end4,
163 int tile_row_start4, int tile_row_end4,
164 int sby, int tile_row_idx, int pass);
165
166 // call for each block
167 void dav1d_refmvs_find(const refmvs_tile *rt,
168 refmvs_candidate mvstack[8], int *cnt,
169 int *ctx, const refmvs_refpair ref, enum BlockSize bs,
170 enum EdgeFlags edge_flags, int by4, int bx4);
171
172 void dav1d_refmvs_dsp_init(Dav1dRefmvsDSPContext *dsp);
173 void dav1d_refmvs_dsp_init_arm(Dav1dRefmvsDSPContext *dsp);
174 void dav1d_refmvs_dsp_init_loongarch(Dav1dRefmvsDSPContext *dsp);
175 void dav1d_refmvs_dsp_init_x86(Dav1dRefmvsDSPContext *dsp);
176
177 #endif /* DAV1D_SRC_REF_MVS_H */
178