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 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 #define decl_splat_mv_fn(name) \
100 void (name)(refmvs_block **rr, const refmvs_block *rmv, int bx4, int bw4, int bh4)
101 typedef decl_splat_mv_fn(*splat_mv_fn);
102 
103 typedef struct Dav1dRefmvsDSPContext {
104     splat_mv_fn splat_mv;
105 } Dav1dRefmvsDSPContext;
106 
107 // call once per frame thread
108 void dav1d_refmvs_init(refmvs_frame *rf);
109 void dav1d_refmvs_clear(refmvs_frame *rf);
110 
111 // call once per frame
112 int dav1d_refmvs_init_frame(refmvs_frame *rf,
113                             const Dav1dSequenceHeader *seq_hdr,
114                             const Dav1dFrameHeader *frm_hdr,
115                             const unsigned ref_poc[7],
116                             refmvs_temporal_block *rp,
117                             const unsigned ref_ref_poc[7][7],
118                             /*const*/ refmvs_temporal_block *const rp_ref[7],
119                             int n_tile_threads, int n_frame_threads);
120 
121 // initialize temporal MVs; this can be done in any configuration, e.g. one
122 // tile/sbrow at a time, where col_{start,end}8 are the tile boundaries; or
123 // it can just be for the whole frame's sbrow, where col_{start,end}8 are the
124 // frame boundaries. row_{start,end}8 are the superblock row boundaries.
125 void dav1d_refmvs_load_tmvs(const refmvs_frame *rf, int tile_row_idx,
126                             int col_start8, int col_end8,
127                             int row_start8, int row_end8);
128 
129 // cache the current tile/sbrow (or frame/sbrow)'s projectable motion vectors
130 // into buffers for use in future frame's temporal MV prediction
131 void dav1d_refmvs_save_tmvs(const refmvs_tile *rt,
132                             int col_start8, int col_end8,
133                             int row_start8, int row_end8);
134 
135 // initialize tile boundaries and refmvs_block pointers for one tile/sbrow
136 void dav1d_refmvs_tile_sbrow_init(refmvs_tile *rt, const refmvs_frame *rf,
137                                   int tile_col_start4, int tile_col_end4,
138                                   int tile_row_start4, int tile_row_end4,
139                                   int sby, int tile_row_idx, int pass);
140 
141 // call for each block
142 void dav1d_refmvs_find(const refmvs_tile *rt,
143                        refmvs_candidate mvstack[8], int *cnt,
144                        int *ctx, const refmvs_refpair ref, enum BlockSize bs,
145                        enum EdgeFlags edge_flags, int by4, int bx4);
146 
147 void dav1d_refmvs_dsp_init(Dav1dRefmvsDSPContext *dsp);
148 void dav1d_refmvs_dsp_init_arm(Dav1dRefmvsDSPContext *dsp);
149 void dav1d_refmvs_dsp_init_x86(Dav1dRefmvsDSPContext *dsp);
150 
151 #endif /* DAV1D_SRC_REF_MVS_H */
152