1/*
2 *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
12#define VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
13
14#include "vpx/vpx_encoder.h"
15
16#include "vp9/encoder/vp9_ratectrl.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22typedef struct {
23  RATE_CONTROL rc;
24  int target_bandwidth;
25  int spatial_layer_target_bandwidth;  // Target for the spatial layer.
26  double framerate;
27  int avg_frame_size;
28  int max_q;
29  int min_q;
30  int scaling_factor_num;
31  int scaling_factor_den;
32  TWO_PASS twopass;
33  vpx_fixed_buf_t rc_twopass_stats_in;
34  unsigned int current_video_frame_in_layer;
35  int is_key_frame;
36  int frames_from_key_frame;
37  FRAME_TYPE last_frame_type;
38  struct lookahead_entry *alt_ref_source;
39  int alt_ref_idx;
40  int gold_ref_idx;
41  int has_alt_frame;
42  size_t layer_size;
43  struct vpx_psnr_pkt psnr_pkt;
44  // Cyclic refresh parameters (aq-mode=3), that need to be updated per-frame.
45  int sb_index;
46  signed char *map;
47  uint8_t *last_coded_q_map;
48  uint8_t *consec_zero_mv;
49  uint8_t speed;
50} LAYER_CONTEXT;
51
52typedef struct SVC {
53  int spatial_layer_id;
54  int temporal_layer_id;
55  int number_spatial_layers;
56  int number_temporal_layers;
57
58  int spatial_layer_to_encode;
59  int first_spatial_layer_to_encode;
60  int rc_drop_superframe;
61
62  // Workaround for multiple frame contexts
63  enum { ENCODED = 0, ENCODING, NEED_TO_ENCODE } encode_empty_frame_state;
64  struct lookahead_entry empty_frame;
65  int encode_intra_empty_frame;
66
67  // Store scaled source frames to be used for temporal filter to generate
68  // a alt ref frame.
69  YV12_BUFFER_CONFIG scaled_frames[MAX_LAG_BUFFERS];
70  // Temp buffer used for 2-stage down-sampling, for real-time mode.
71  YV12_BUFFER_CONFIG scaled_temp;
72  int scaled_one_half;
73  int scaled_temp_is_alloc;
74
75  // Layer context used for rate control in one pass temporal CBR mode or
76  // two pass spatial mode.
77  LAYER_CONTEXT layer_context[VPX_MAX_LAYERS];
78  // Indicates what sort of temporal layering is used.
79  // Currently, this only works for CBR mode.
80  VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
81  // Frame flags and buffer indexes for each spatial layer, set by the
82  // application (external settings).
83  int ext_frame_flags[VPX_MAX_LAYERS];
84  int ext_lst_fb_idx[VPX_MAX_LAYERS];
85  int ext_gld_fb_idx[VPX_MAX_LAYERS];
86  int ext_alt_fb_idx[VPX_MAX_LAYERS];
87  int ref_frame_index[REF_FRAMES];
88  int force_zero_mode_spatial_ref;
89  int current_superframe;
90  int non_reference_frame;
91  int use_base_mv;
92  // Used to control the downscaling filter for source scaling, for 1 pass CBR.
93  // downsample_filter_phase: = 0 will do sub-sampling (no weighted average),
94  // = 8 will center the target pixel and get a symmetric averaging filter.
95  // downsample_filter_type: 4 filters may be used: eighttap_regular,
96  // eighttap_smooth, eighttap_sharp, and bilinear.
97  INTERP_FILTER downsample_filter_type[VPX_SS_MAX_LAYERS];
98  int downsample_filter_phase[VPX_SS_MAX_LAYERS];
99
100  BLOCK_SIZE *prev_partition_svc;
101  int mi_stride[VPX_MAX_LAYERS];
102
103  int first_layer_denoise;
104} SVC;
105
106struct VP9_COMP;
107
108// Initialize layer context data from init_config().
109void vp9_init_layer_context(struct VP9_COMP *const cpi);
110
111// Update the layer context from a change_config() call.
112void vp9_update_layer_context_change_config(struct VP9_COMP *const cpi,
113                                            const int target_bandwidth);
114
115// Prior to encoding the frame, update framerate-related quantities
116// for the current temporal layer.
117void vp9_update_temporal_layer_framerate(struct VP9_COMP *const cpi);
118
119// Update framerate-related quantities for the current spatial layer.
120void vp9_update_spatial_layer_framerate(struct VP9_COMP *const cpi,
121                                        double framerate);
122
123// Prior to encoding the frame, set the layer context, for the current layer
124// to be encoded, to the cpi struct.
125void vp9_restore_layer_context(struct VP9_COMP *const cpi);
126
127// Save the layer context after encoding the frame.
128void vp9_save_layer_context(struct VP9_COMP *const cpi);
129
130// Initialize second pass rc for spatial svc.
131void vp9_init_second_pass_spatial_svc(struct VP9_COMP *cpi);
132
133void get_layer_resolution(const int width_org, const int height_org,
134                          const int num, const int den, int *width_out,
135                          int *height_out);
136
137// Increment number of video frames in layer
138void vp9_inc_frame_in_layer(struct VP9_COMP *const cpi);
139
140// Check if current layer is key frame in spatial upper layer
141int vp9_is_upper_layer_key_frame(const struct VP9_COMP *const cpi);
142
143// Get the next source buffer to encode
144struct lookahead_entry *vp9_svc_lookahead_pop(struct VP9_COMP *const cpi,
145                                              struct lookahead_ctx *ctx,
146                                              int drain);
147
148// Start a frame and initialize svc parameters
149int vp9_svc_start_frame(struct VP9_COMP *const cpi);
150
151int vp9_one_pass_cbr_svc_start_layer(struct VP9_COMP *const cpi);
152
153void vp9_free_svc_cyclic_refresh(struct VP9_COMP *const cpi);
154
155void vp9_svc_reset_key_frame(struct VP9_COMP *const cpi);
156
157void vp9_svc_check_reset_layer_rc_flag(struct VP9_COMP *const cpi);
158
159#ifdef __cplusplus
160}  // extern "C"
161#endif
162
163#endif  // VP9_ENCODER_VP9_SVC_LAYERCONTEXT_
164