AOMedia AV1 Codec
temporal_filter.h
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_ENCODER_TEMPORAL_FILTER_H_
13 #define AOM_AV1_ENCODER_TEMPORAL_FILTER_H_
14 
15 #include <stdbool.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
21 struct AV1_COMP;
22 struct AV1EncoderConfig;
23 struct ThreadData;
24 // TODO(wtc): These two variables are only used in avx2, sse2, neon
25 // implementations, where the block size is still hard coded to TF_BLOCK_SIZE.
26 // This should be fixed to align with the c implementation.
27 #define BH 32
28 #define BW 32
29 
30 // Block size used in temporal filtering.
31 #define TF_BLOCK_SIZE BLOCK_32X32
32 
33 // Window size for temporal filtering.
34 #define TF_WINDOW_LENGTH 5
35 
36 // A constant number, sqrt(pi / 2), used for noise estimation.
37 static const double SQRT_PI_BY_2 = 1.25331413732;
38 
39 // Hyper-parameters used to compute filtering weight. These hyper-parameters can
40 // be tuned for a better performance.
41 // 0. A scale factor used in temporal filtering to raise the filter weight from
42 // `double` with range [0, 1] to `int` with range [0, 1000].
43 #define TF_WEIGHT_SCALE 1000
44 // 1. Weight factor used to balance the weighted-average between window error
45 // and block error. The weight is for window error while the weight for block
46 // error is always set as 1.
47 #define TF_WINDOW_BLOCK_BALANCE_WEIGHT 5
48 // 2. Threshold for using q to adjust the filtering weight. Concretely, when
49 // using a small q (high bitrate), we would like to reduce the filtering
50 // strength such that more detailed information can be preserved. Hence, when
51 // q is smaller than this threshold, we will adjust the filtering weight
52 // based on the q-value.
53 #define TF_Q_DECAY_THRESHOLD 20
54 // 3. Normalization factor used to normalize the motion search error. Since the
55 // motion search error can be large and uncontrollable, we will simply
56 // normalize it before using it to compute the filtering weight.
57 #define TF_SEARCH_ERROR_NORM_WEIGHT 20
58 // 4. Threshold for using `arnr_strength` to adjust the filtering strength.
59 // Concretely, users can use `arnr_strength` arguments to control the
60 // strength of temporal filtering. When `arnr_strength` is small enough (
61 // i.e., smaller than this threshold), we will adjust the filtering weight
62 // based on the strength value.
63 #define TF_STRENGTH_THRESHOLD 4
64 // 5. Threshold for using motion search distance to adjust the filtering weight.
65 // Concretely, larger motion search vector leads to a higher probability of
66 // unreliable search. Hence, we would like to reduce the filtering strength
67 // when the distance is large enough. Considering that the distance actually
68 // relies on the frame size, this threshold is also a resolution-based
69 // threshold. Taking 720p videos as an instance, if this field equals to 0.1,
70 // then the actual threshold will be 720 * 0.1 = 72. Similarly, the threshold
71 // for 360p videos will be 360 * 0.1 = 36.
72 #define TF_SEARCH_DISTANCE_THRESHOLD 0.1
73 // 6. Threshold to identify if the q is in a relative high range.
74 // Above this cutoff q, a stronger filtering is applied.
75 // For a high q, the quantization throws away more information, and thus a
76 // stronger filtering is less likely to distort the encoded quality, while a
77 // stronger filtering could reduce bit rates.
78 // Ror a low q, more details are expected to be retained. Filtering is thus
79 // more conservative.
80 #define TF_QINDEX_CUTOFF 128
81 
82 #define NOISE_ESTIMATION_EDGE_THRESHOLD 50
83 
84 // Sum and SSE source vs filtered frame difference returned by
85 // temporal filter.
86 typedef struct {
87  int64_t sum;
88  int64_t sse;
89 } FRAME_DIFF;
90 
96 typedef struct {
100  YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS];
105 
110 
122  struct scale_factors sf;
126  double noise_levels[MAX_MB_PLANE];
130  int num_pels;
134  int mb_rows;
138  int mb_cols;
146  int q_factor;
148 
154 #define TF_INFO_BUF_COUNT 2
155 
159 typedef struct TEMPORAL_FILTER_INFO {
170  YV12_BUFFER_CONFIG tf_buf[TF_INFO_BUF_COUNT];
171 
182  FRAME_DIFF frame_diff[TF_INFO_BUF_COUNT];
186  int tf_buf_gf_index[TF_INFO_BUF_COUNT];
190  int tf_buf_display_index_offset[TF_INFO_BUF_COUNT];
194  int tf_buf_valid[TF_INFO_BUF_COUNT];
196 
202 int av1_is_temporal_filter_on(const struct AV1EncoderConfig *oxcf);
203 
208 void av1_tf_info_alloc(TEMPORAL_FILTER_INFO *tf_info,
209  const struct AV1_COMP *cpi);
210 
214 void av1_tf_info_free(TEMPORAL_FILTER_INFO *tf_info);
215 
219 void av1_tf_info_reset(TEMPORAL_FILTER_INFO *tf_info);
220 
226 void av1_tf_info_filtering(TEMPORAL_FILTER_INFO *tf_info, struct AV1_COMP *cpi,
227  const GF_GROUP *gf_group);
228 
235 YV12_BUFFER_CONFIG *av1_tf_info_get_filtered_buf(TEMPORAL_FILTER_INFO *tf_info,
236  int gf_index,
237  FRAME_DIFF *frame_diff);
238 
241 // Data related to temporal filtering.
242 typedef struct {
243  // Source vs filtered frame error.
244  FRAME_DIFF diff;
245  // Pointer to temporary block info used to store state in temporal filtering
246  // process.
247  MB_MODE_INFO *tmp_mbmi;
248  // Pointer to accumulator buffer used in temporal filtering process.
249  uint32_t *accum;
250  // Pointer to count buffer used in temporal filtering process.
251  uint16_t *count;
252  // Pointer to predictor used in temporal filtering process.
253  uint8_t *pred;
254 } TemporalFilterData;
255 
256 // Data related to temporal filter multi-thread synchronization.
257 typedef struct {
258 #if CONFIG_MULTITHREAD
259  // Mutex lock used for dispatching jobs.
260  pthread_mutex_t *mutex_;
261 #endif // CONFIG_MULTITHREAD
262  // Next temporal filter block row to be filtered.
263  int next_tf_row;
264  // Initialized to false, set to true by the worker thread that encounters an
265  // error in order to abort the processing of other worker threads.
266  bool tf_mt_exit;
267 } AV1TemporalFilterSync;
268 
269 // Estimates noise level from a given frame using a single plane (Y, U, or V).
270 // This is an adaptation of the mehtod in the following paper:
271 // Shen-Chuan Tai, Shih-Ming Yang, "A fast method for image noise
272 // estimation using Laplacian operator and adaptive edge detection",
273 // Proc. 3rd International Symposium on Communications, Control and
274 // Signal Processing, 2008, St Julians, Malta.
275 // Inputs:
276 // frame: Pointer to the frame to estimate noise level from.
277 // noise_level: Pointer to store the estimated noise.
278 // plane_from: Index of the starting plane used for noise estimation.
279 // Commonly, 0 for Y-plane, 1 for U-plane, and 2 for V-plane.
280 // plane_to: Index of the end plane used for noise estimation.
281 // bit_depth: Actual bit-depth instead of the encoding bit-depth of the frame.
282 // edge_thresh: Edge threshold.
283 void av1_estimate_noise_level(const YV12_BUFFER_CONFIG *frame,
284  double *noise_level, int plane_from, int plane_to,
285  int bit_depth, int edge_thresh);
299 void av1_tf_do_filtering_row(struct AV1_COMP *cpi, struct ThreadData *td,
300  int mb_row);
301 
326 void av1_temporal_filter(struct AV1_COMP *cpi,
327  const int filter_frame_lookahead_idx,
328  int gf_frame_index, FRAME_DIFF *frame_diff,
329  YV12_BUFFER_CONFIG *output_frame);
330 
346  const FRAME_DIFF *frame_diff, int q_index,
347  aom_bit_depth_t bit_depth);
348 
350 // Helper function to get `q` used for encoding.
351 int av1_get_q(const struct AV1_COMP *cpi);
352 
353 // Allocates memory for members of TemporalFilterData.
354 // Inputs:
355 // tf_data: Pointer to the structure containing temporal filter related data.
356 // num_pels: Number of pixels in the block across all planes.
357 // is_high_bitdepth: Whether the frame is high-bitdepth or not.
358 // Returns:
359 // True if allocation is successful and false otherwise.
360 static AOM_INLINE bool tf_alloc_and_reset_data(TemporalFilterData *tf_data,
361  int num_pels,
362  int is_high_bitdepth) {
363  tf_data->tmp_mbmi = (MB_MODE_INFO *)aom_calloc(1, sizeof(*tf_data->tmp_mbmi));
364  tf_data->accum =
365  (uint32_t *)aom_memalign(16, num_pels * sizeof(*tf_data->accum));
366  tf_data->count =
367  (uint16_t *)aom_memalign(16, num_pels * sizeof(*tf_data->count));
368  if (is_high_bitdepth)
369  tf_data->pred = CONVERT_TO_BYTEPTR(
370  aom_memalign(32, num_pels * 2 * sizeof(*tf_data->pred)));
371  else
372  tf_data->pred =
373  (uint8_t *)aom_memalign(32, num_pels * sizeof(*tf_data->pred));
374  // In case of an allocation failure, other successfully allocated buffers will
375  // be freed by the tf_dealloc_data() call in encoder_destroy().
376  if (!(tf_data->tmp_mbmi && tf_data->accum && tf_data->count && tf_data->pred))
377  return false;
378  memset(&tf_data->diff, 0, sizeof(tf_data->diff));
379  return true;
380 }
381 
382 // Setup macroblockd params for temporal filtering process.
383 // Inputs:
384 // mbd: Pointer to the block for filtering.
385 // tf_data: Pointer to the structure containing temporal filter related data.
386 // scale: Scaling factor.
387 // Returns:
388 // Nothing will be returned. Contents of mbd will be modified.
389 static AOM_INLINE void tf_setup_macroblockd(MACROBLOCKD *mbd,
390  TemporalFilterData *tf_data,
391  const struct scale_factors *scale) {
392  mbd->block_ref_scale_factors[0] = scale;
393  mbd->block_ref_scale_factors[1] = scale;
394  mbd->mi = &tf_data->tmp_mbmi;
395  mbd->mi[0]->motion_mode = SIMPLE_TRANSLATION;
396 }
397 
398 // Deallocates the memory allocated for members of TemporalFilterData.
399 // Inputs:
400 // tf_data: Pointer to the structure containing temporal filter related data.
401 // is_high_bitdepth: Whether the frame is high-bitdepth or not.
402 // Returns:
403 // Nothing will be returned.
404 static AOM_INLINE void tf_dealloc_data(TemporalFilterData *tf_data,
405  int is_high_bitdepth) {
406  if (is_high_bitdepth)
407  tf_data->pred = (uint8_t *)CONVERT_TO_SHORTPTR(tf_data->pred);
408  aom_free(tf_data->tmp_mbmi);
409  tf_data->tmp_mbmi = NULL;
410  aom_free(tf_data->accum);
411  tf_data->accum = NULL;
412  aom_free(tf_data->count);
413  tf_data->count = NULL;
414  aom_free(tf_data->pred);
415  tf_data->pred = NULL;
416 }
417 
418 // Saves the state prior to temporal filter process.
419 // Inputs:
420 // mbd: Pointer to the block for filtering.
421 // input_mbmi: Backup block info to save input state.
422 // input_buffer: Backup buffer pointer to save input state.
423 // num_planes: Number of planes.
424 // Returns:
425 // Nothing will be returned. Contents of input_mbmi and input_buffer will be
426 // modified.
427 static INLINE void tf_save_state(MACROBLOCKD *mbd, MB_MODE_INFO ***input_mbmi,
428  uint8_t **input_buffer, int num_planes) {
429  for (int i = 0; i < num_planes; i++) {
430  input_buffer[i] = mbd->plane[i].pre[0].buf;
431  }
432  *input_mbmi = mbd->mi;
433 }
434 
435 // Restores the initial state after temporal filter process.
436 // Inputs:
437 // mbd: Pointer to the block for filtering.
438 // input_mbmi: Backup block info from where input state is restored.
439 // input_buffer: Backup buffer pointer from where input state is restored.
440 // num_planes: Number of planes.
441 // Returns:
442 // Nothing will be returned. Contents of mbd will be modified.
443 static INLINE void tf_restore_state(MACROBLOCKD *mbd, MB_MODE_INFO **input_mbmi,
444  uint8_t **input_buffer, int num_planes) {
445  for (int i = 0; i < num_planes; i++) {
446  mbd->plane[i].pre[0].buf = input_buffer[i];
447  }
448  mbd->mi = input_mbmi;
449 }
450 
452 #ifdef __cplusplus
453 } // extern "C"
454 #endif
455 
456 #endif // AOM_AV1_ENCODER_TEMPORAL_FILTER_H_
enum aom_bit_depth aom_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
int av1_check_show_filtered_frame(const YV12_BUFFER_CONFIG *frame, const FRAME_DIFF *frame_diff, int q_index, aom_bit_depth_t bit_depth)
Check whether a filtered frame can be show directly.
void av1_temporal_filter(struct AV1_COMP *cpi, const int filter_frame_lookahead_idx, int gf_frame_index, FRAME_DIFF *frame_diff, YV12_BUFFER_CONFIG *output_frame)
Performs temporal filtering if needed on a source frame. For example to create a filtered alternate r...
void av1_tf_do_filtering_row(struct AV1_COMP *cpi, struct ThreadData *td, int mb_row)
Does temporal filter for a given macroblock row.
Definition: temporal_filter.c:859
Main encoder configuration data structure.
Definition: encoder.h:915
Top level encoder structure.
Definition: encoder.h:2872
Data related to the current GF/ARF group and the individual frames within the group.
Definition: firstpass.h:354
Stores the prediction/txfm mode of the current coding block.
Definition: blockd.h:222
MOTION_MODE motion_mode
The motion mode used by the inter prediction.
Definition: blockd.h:250
Temporal filter info for a gop.
Definition: temporal_filter.h:159
int tf_buf_display_index_offset[2]
Definition: temporal_filter.h:190
YV12_BUFFER_CONFIG tf_buf[2]
Definition: temporal_filter.h:170
int tf_buf_gf_index[2]
Definition: temporal_filter.h:186
FRAME_DIFF frame_diff[2]
Definition: temporal_filter.h:182
int is_temporal_filter_on
Definition: temporal_filter.h:165
int tf_buf_valid[2]
Definition: temporal_filter.h:194
YV12_BUFFER_CONFIG tf_buf_second_arf
Definition: temporal_filter.h:178
Parameters related to temporal filtering.
Definition: temporal_filter.h:96
YV12_BUFFER_CONFIG * output_frame
Definition: temporal_filter.h:109
int q_factor
Definition: temporal_filter.h:146
int num_pels
Definition: temporal_filter.h:130
int num_frames
Definition: temporal_filter.h:104
int compute_frame_diff
Definition: temporal_filter.h:118
int mb_rows
Definition: temporal_filter.h:134
int mb_cols
Definition: temporal_filter.h:138
int is_highbitdepth
Definition: temporal_filter.h:142
int filter_frame_idx
Definition: temporal_filter.h:114
Variables related to current coding block.
Definition: blockd.h:570
struct macroblockd_plane plane[3]
Definition: blockd.h:606
const struct scale_factors * block_ref_scale_factors[2]
Definition: blockd.h:687
MB_MODE_INFO ** mi
Definition: blockd.h:617
YV12 frame buffer data structure.
Definition: yv12config.h:44