xref: /aosp_15_r20/external/libaom/av1/common/seg_common.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_COMMON_SEG_COMMON_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_COMMON_SEG_COMMON_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/prob.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
18*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
19*77c1e3ccSAndroid Build Coastguard Worker #endif
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #define MAX_SEGMENTS 8
22*77c1e3ccSAndroid Build Coastguard Worker #define SEG_TREE_PROBS (MAX_SEGMENTS - 1)
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker #define SEG_TEMPORAL_PRED_CTXS 3
25*77c1e3ccSAndroid Build Coastguard Worker #define SPATIAL_PREDICTION_PROBS 3
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker enum {
28*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_ALT_Q,       // Use alternate Quantizer ....
29*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_ALT_LF_Y_V,  // Use alternate loop filter value on y plane vertical
30*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_ALT_LF_Y_H,  // Use alternate loop filter value on y plane horizontal
31*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_ALT_LF_U,    // Use alternate loop filter value on u plane
32*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_ALT_LF_V,    // Use alternate loop filter value on v plane
33*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_REF_FRAME,   // Optional Segment reference frame
34*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_SKIP,        // Optional Segment (0,0) + skip mode
35*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_GLOBALMV,
36*77c1e3ccSAndroid Build Coastguard Worker   SEG_LVL_MAX
37*77c1e3ccSAndroid Build Coastguard Worker } UENUM1BYTE(SEG_LVL_FEATURES);
38*77c1e3ccSAndroid Build Coastguard Worker 
39*77c1e3ccSAndroid Build Coastguard Worker struct segmentation {
40*77c1e3ccSAndroid Build Coastguard Worker   uint8_t enabled;
41*77c1e3ccSAndroid Build Coastguard Worker   uint8_t update_map;
42*77c1e3ccSAndroid Build Coastguard Worker   uint8_t update_data;
43*77c1e3ccSAndroid Build Coastguard Worker   uint8_t temporal_update;
44*77c1e3ccSAndroid Build Coastguard Worker 
45*77c1e3ccSAndroid Build Coastguard Worker   int16_t feature_data[MAX_SEGMENTS][SEG_LVL_MAX];
46*77c1e3ccSAndroid Build Coastguard Worker   unsigned int feature_mask[MAX_SEGMENTS];
47*77c1e3ccSAndroid Build Coastguard Worker   int last_active_segid;  // The highest numbered segment id that has some
48*77c1e3ccSAndroid Build Coastguard Worker                           // enabled feature.
49*77c1e3ccSAndroid Build Coastguard Worker   uint8_t segid_preskip;  // Whether the segment id will be read before the
50*77c1e3ccSAndroid Build Coastguard Worker                           // skip syntax element.
51*77c1e3ccSAndroid Build Coastguard Worker                           // 1: the segment id will be read first.
52*77c1e3ccSAndroid Build Coastguard Worker                           // 0: the skip syntax element will be read first.
53*77c1e3ccSAndroid Build Coastguard Worker };
54*77c1e3ccSAndroid Build Coastguard Worker 
55*77c1e3ccSAndroid Build Coastguard Worker struct segmentation_probs {
56*77c1e3ccSAndroid Build Coastguard Worker   aom_cdf_prob pred_cdf[SEG_TEMPORAL_PRED_CTXS][CDF_SIZE(2)];
57*77c1e3ccSAndroid Build Coastguard Worker   aom_cdf_prob spatial_pred_seg_cdf[SPATIAL_PREDICTION_PROBS]
58*77c1e3ccSAndroid Build Coastguard Worker                                    [CDF_SIZE(MAX_SEGMENTS)];
59*77c1e3ccSAndroid Build Coastguard Worker };
60*77c1e3ccSAndroid Build Coastguard Worker 
segfeature_active(const struct segmentation * seg,uint8_t segment_id,SEG_LVL_FEATURES feature_id)61*77c1e3ccSAndroid Build Coastguard Worker static inline int segfeature_active(const struct segmentation *seg,
62*77c1e3ccSAndroid Build Coastguard Worker                                     uint8_t segment_id,
63*77c1e3ccSAndroid Build Coastguard Worker                                     SEG_LVL_FEATURES feature_id) {
64*77c1e3ccSAndroid Build Coastguard Worker   return seg->enabled && (seg->feature_mask[segment_id] & (1 << feature_id));
65*77c1e3ccSAndroid Build Coastguard Worker }
66*77c1e3ccSAndroid Build Coastguard Worker 
segfeatures_copy(struct segmentation * dst,const struct segmentation * src)67*77c1e3ccSAndroid Build Coastguard Worker static inline void segfeatures_copy(struct segmentation *dst,
68*77c1e3ccSAndroid Build Coastguard Worker                                     const struct segmentation *src) {
69*77c1e3ccSAndroid Build Coastguard Worker   int i, j;
70*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < MAX_SEGMENTS; i++) {
71*77c1e3ccSAndroid Build Coastguard Worker     dst->feature_mask[i] = src->feature_mask[i];
72*77c1e3ccSAndroid Build Coastguard Worker     for (j = 0; j < SEG_LVL_MAX; j++) {
73*77c1e3ccSAndroid Build Coastguard Worker       dst->feature_data[i][j] = src->feature_data[i][j];
74*77c1e3ccSAndroid Build Coastguard Worker     }
75*77c1e3ccSAndroid Build Coastguard Worker   }
76*77c1e3ccSAndroid Build Coastguard Worker   dst->segid_preskip = src->segid_preskip;
77*77c1e3ccSAndroid Build Coastguard Worker   dst->last_active_segid = src->last_active_segid;
78*77c1e3ccSAndroid Build Coastguard Worker }
79*77c1e3ccSAndroid Build Coastguard Worker 
80*77c1e3ccSAndroid Build Coastguard Worker void av1_clearall_segfeatures(struct segmentation *seg);
81*77c1e3ccSAndroid Build Coastguard Worker 
82*77c1e3ccSAndroid Build Coastguard Worker void av1_enable_segfeature(struct segmentation *seg, int segment_id,
83*77c1e3ccSAndroid Build Coastguard Worker                            SEG_LVL_FEATURES feature_id);
84*77c1e3ccSAndroid Build Coastguard Worker 
85*77c1e3ccSAndroid Build Coastguard Worker void av1_calculate_segdata(struct segmentation *seg);
86*77c1e3ccSAndroid Build Coastguard Worker 
87*77c1e3ccSAndroid Build Coastguard Worker int av1_seg_feature_data_max(SEG_LVL_FEATURES feature_id);
88*77c1e3ccSAndroid Build Coastguard Worker 
89*77c1e3ccSAndroid Build Coastguard Worker int av1_is_segfeature_signed(SEG_LVL_FEATURES feature_id);
90*77c1e3ccSAndroid Build Coastguard Worker 
91*77c1e3ccSAndroid Build Coastguard Worker void av1_set_segdata(struct segmentation *seg, int segment_id,
92*77c1e3ccSAndroid Build Coastguard Worker                      SEG_LVL_FEATURES feature_id, int seg_data);
93*77c1e3ccSAndroid Build Coastguard Worker 
get_segdata(const struct segmentation * seg,int segment_id,SEG_LVL_FEATURES feature_id)94*77c1e3ccSAndroid Build Coastguard Worker static inline int get_segdata(const struct segmentation *seg, int segment_id,
95*77c1e3ccSAndroid Build Coastguard Worker                               SEG_LVL_FEATURES feature_id) {
96*77c1e3ccSAndroid Build Coastguard Worker   return seg->feature_data[segment_id][feature_id];
97*77c1e3ccSAndroid Build Coastguard Worker }
98*77c1e3ccSAndroid Build Coastguard Worker 
set_segment_id(uint8_t * segment_ids,int mi_offset,int x_mis,int y_mis,int mi_stride,uint8_t segment_id)99*77c1e3ccSAndroid Build Coastguard Worker static inline void set_segment_id(uint8_t *segment_ids, int mi_offset,
100*77c1e3ccSAndroid Build Coastguard Worker                                   int x_mis, int y_mis, int mi_stride,
101*77c1e3ccSAndroid Build Coastguard Worker                                   uint8_t segment_id) {
102*77c1e3ccSAndroid Build Coastguard Worker   segment_ids += mi_offset;
103*77c1e3ccSAndroid Build Coastguard Worker   for (int y = 0; y < y_mis; ++y) {
104*77c1e3ccSAndroid Build Coastguard Worker     memset(&segment_ids[y * mi_stride], segment_id,
105*77c1e3ccSAndroid Build Coastguard Worker            x_mis * sizeof(segment_ids[0]));
106*77c1e3ccSAndroid Build Coastguard Worker   }
107*77c1e3ccSAndroid Build Coastguard Worker }
108*77c1e3ccSAndroid Build Coastguard Worker 
109*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
110*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
111*77c1e3ccSAndroid Build Coastguard Worker #endif
112*77c1e3ccSAndroid Build Coastguard Worker 
113*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_COMMON_SEG_COMMON_H_
114