xref: /aosp_15_r20/external/libdav1d/src/refmvs.h (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
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 CHECK_SIZE(refmvs_temporal_block, 5);
47 
48 PACKED(typedef union refmvs_refpair {
49     int8_t ref[2]; // [0] = 0: intra=1, [1] = -1: comp=0
50     uint16_t pair;
51 }) ALIGN(refmvs_refpair, 2);
52 CHECK_SIZE(refmvs_refpair, 2);
53 
54 typedef union refmvs_mvpair {
55     mv mv[2];
56     uint64_t n;
57 } refmvs_mvpair;
58 CHECK_SIZE(refmvs_mvpair, 8);
59 
60 PACKED(typedef struct refmvs_block {
61     refmvs_mvpair mv;
62     refmvs_refpair ref;
63     uint8_t bs, mf; // 1 = globalmv+affine, 2 = newmv
64 }) ALIGN(refmvs_block, 4);
65 CHECK_SIZE(refmvs_block, 12);
66 
67 typedef struct refmvs_frame {
68     const Dav1dFrameHeader *frm_hdr;
69     int iw4, ih4, iw8, ih8;
70     int sbsz;
71     int use_ref_frame_mvs;
72     uint8_t sign_bias[7], mfmv_sign[7];
73     int8_t pocdiff[7];
74     uint8_t mfmv_ref[3];
75     int mfmv_ref2cur[3];
76     int mfmv_ref2ref[3][7];
77     int n_mfmvs;
78 
79     int n_blocks;
80     refmvs_temporal_block *rp;
81     /*const*/ refmvs_temporal_block *const *rp_ref;
82     refmvs_temporal_block *rp_proj;
83     ptrdiff_t rp_stride;
84 
85     refmvs_block *r; // 35 x r_stride memory
86     int n_tile_threads, n_frame_threads;
87 } refmvs_frame;
88 
89 typedef struct refmvs_tile {
90     const refmvs_frame *rf;
91     refmvs_block *r[32 + 5];
92     refmvs_temporal_block *rp_proj;
93     struct {
94         int start, end;
95     } tile_col, tile_row;
96 } refmvs_tile;
97 
98 typedef struct refmvs_candidate {
99     refmvs_mvpair mv;
100     int weight;
101 } refmvs_candidate;
102 
103 // initialize temporal MVs; this can be done in any configuration, e.g. one
104 // tile/sbrow at a time, where col_{start,end}8 are the tile boundaries; or
105 // it can just be for the whole frame's sbrow, where col_{start,end}8 are the
106 // frame boundaries. row_{start,end}8 are the superblock row boundaries.
107 #define decl_load_tmvs_fn(name) \
108 void (name)(const refmvs_frame *rf, int tile_row_idx, \
109             int col_start8, int col_end8, int row_start8, int row_end8)
110 typedef decl_load_tmvs_fn(*load_tmvs_fn);
111 
112 #define decl_save_tmvs_fn(name) \
113 void (name)(refmvs_temporal_block *rp, const ptrdiff_t stride, \
114             refmvs_block *const *const rr, const uint8_t *const ref_sign, \
115             int col_end8, int row_end8, int col_start8, int row_start8)
116 typedef decl_save_tmvs_fn(*save_tmvs_fn);
117 
118 #define decl_splat_mv_fn(name) \
119 void (name)(refmvs_block **rr, const refmvs_block *rmv, int bx4, int bw4, int bh4)
120 typedef decl_splat_mv_fn(*splat_mv_fn);
121 
122 typedef struct Dav1dRefmvsDSPContext {
123     load_tmvs_fn load_tmvs;
124     save_tmvs_fn save_tmvs;
125     splat_mv_fn splat_mv;
126 } Dav1dRefmvsDSPContext;
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