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