vp9_svc_layercontext.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #ifndef VPX_VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
  11. #define VPX_VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_
  12. #include "vpx/vpx_encoder.h"
  13. #include "vp9/encoder/vp9_ratectrl.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef enum {
  18. // Inter-layer prediction is on on all frames.
  19. INTER_LAYER_PRED_ON,
  20. // Inter-layer prediction is off on all frames.
  21. INTER_LAYER_PRED_OFF,
  22. // Inter-layer prediction is off on non-key frames and non-sync frames.
  23. INTER_LAYER_PRED_OFF_NONKEY,
  24. // Inter-layer prediction is on on all frames, but constrained such
  25. // that any layer S (> 0) can only predict from previous spatial
  26. // layer S-1, from the same superframe.
  27. INTER_LAYER_PRED_ON_CONSTRAINED
  28. } INTER_LAYER_PRED;
  29. typedef struct BUFFER_LONGTERM_REF {
  30. int idx;
  31. int is_used;
  32. } BUFFER_LONGTERM_REF;
  33. typedef struct {
  34. RATE_CONTROL rc;
  35. int target_bandwidth;
  36. int spatial_layer_target_bandwidth; // Target for the spatial layer.
  37. double framerate;
  38. int avg_frame_size;
  39. int max_q;
  40. int min_q;
  41. int scaling_factor_num;
  42. int scaling_factor_den;
  43. TWO_PASS twopass;
  44. vpx_fixed_buf_t rc_twopass_stats_in;
  45. unsigned int current_video_frame_in_layer;
  46. int is_key_frame;
  47. int frames_from_key_frame;
  48. FRAME_TYPE last_frame_type;
  49. struct lookahead_entry *alt_ref_source;
  50. int alt_ref_idx;
  51. int gold_ref_idx;
  52. int has_alt_frame;
  53. size_t layer_size;
  54. struct vpx_psnr_pkt psnr_pkt;
  55. // Cyclic refresh parameters (aq-mode=3), that need to be updated per-frame.
  56. // TODO(jianj/marpan): Is it better to use the full cyclic refresh struct.
  57. int sb_index;
  58. signed char *map;
  59. uint8_t *last_coded_q_map;
  60. uint8_t *consec_zero_mv;
  61. int actual_num_seg1_blocks;
  62. int actual_num_seg2_blocks;
  63. int counter_encode_maxq_scene_change;
  64. uint8_t speed;
  65. } LAYER_CONTEXT;
  66. typedef struct SVC {
  67. int spatial_layer_id;
  68. int temporal_layer_id;
  69. int number_spatial_layers;
  70. int number_temporal_layers;
  71. int spatial_layer_to_encode;
  72. // Workaround for multiple frame contexts
  73. enum { ENCODED = 0, ENCODING, NEED_TO_ENCODE } encode_empty_frame_state;
  74. struct lookahead_entry empty_frame;
  75. int encode_intra_empty_frame;
  76. // Store scaled source frames to be used for temporal filter to generate
  77. // a alt ref frame.
  78. YV12_BUFFER_CONFIG scaled_frames[MAX_LAG_BUFFERS];
  79. // Temp buffer used for 2-stage down-sampling, for real-time mode.
  80. YV12_BUFFER_CONFIG scaled_temp;
  81. int scaled_one_half;
  82. int scaled_temp_is_alloc;
  83. // Layer context used for rate control in one pass temporal CBR mode or
  84. // two pass spatial mode.
  85. LAYER_CONTEXT layer_context[VPX_MAX_LAYERS];
  86. // Indicates what sort of temporal layering is used.
  87. // Currently, this only works for CBR mode.
  88. VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
  89. // Frame flags and buffer indexes for each spatial layer, set by the
  90. // application (external settings).
  91. int ext_frame_flags[VPX_MAX_LAYERS];
  92. int lst_fb_idx[VPX_MAX_LAYERS];
  93. int gld_fb_idx[VPX_MAX_LAYERS];
  94. int alt_fb_idx[VPX_MAX_LAYERS];
  95. int force_zero_mode_spatial_ref;
  96. // Sequence level flag to enable second (long term) temporal reference.
  97. int use_gf_temporal_ref;
  98. // Frame level flag to enable second (long term) temporal reference.
  99. int use_gf_temporal_ref_current_layer;
  100. // Allow second reference for at most 2 top highest resolution layers.
  101. BUFFER_LONGTERM_REF buffer_gf_temporal_ref[2];
  102. int current_superframe;
  103. int non_reference_frame;
  104. int use_base_mv;
  105. int use_partition_reuse;
  106. // Used to control the downscaling filter for source scaling, for 1 pass CBR.
  107. // downsample_filter_phase: = 0 will do sub-sampling (no weighted average),
  108. // = 8 will center the target pixel and get a symmetric averaging filter.
  109. // downsample_filter_type: 4 filters may be used: eighttap_regular,
  110. // eighttap_smooth, eighttap_sharp, and bilinear.
  111. INTERP_FILTER downsample_filter_type[VPX_SS_MAX_LAYERS];
  112. int downsample_filter_phase[VPX_SS_MAX_LAYERS];
  113. BLOCK_SIZE *prev_partition_svc;
  114. int mi_stride[VPX_MAX_LAYERS];
  115. int mi_rows[VPX_MAX_LAYERS];
  116. int mi_cols[VPX_MAX_LAYERS];
  117. int first_layer_denoise;
  118. int skip_enhancement_layer;
  119. int lower_layer_qindex;
  120. int last_layer_dropped[VPX_MAX_LAYERS];
  121. int drop_spatial_layer[VPX_MAX_LAYERS];
  122. int framedrop_thresh[VPX_MAX_LAYERS];
  123. int drop_count[VPX_MAX_LAYERS];
  124. int max_consec_drop;
  125. SVC_LAYER_DROP_MODE framedrop_mode;
  126. INTER_LAYER_PRED disable_inter_layer_pred;
  127. // Flag to indicate scene change and high num of motion blocks at current
  128. // superframe, scene detection is currently checked for each superframe prior
  129. // to encoding, on the full resolution source.
  130. int high_source_sad_superframe;
  131. int high_num_blocks_with_motion;
  132. // Flags used to get SVC pattern info.
  133. int update_buffer_slot[VPX_SS_MAX_LAYERS];
  134. uint8_t reference_last[VPX_SS_MAX_LAYERS];
  135. uint8_t reference_golden[VPX_SS_MAX_LAYERS];
  136. uint8_t reference_altref[VPX_SS_MAX_LAYERS];
  137. // TODO(jianj): Remove these last 3, deprecated.
  138. uint8_t update_last[VPX_SS_MAX_LAYERS];
  139. uint8_t update_golden[VPX_SS_MAX_LAYERS];
  140. uint8_t update_altref[VPX_SS_MAX_LAYERS];
  141. // Keep track of the frame buffer index updated/refreshed on the base
  142. // temporal superframe.
  143. int fb_idx_upd_tl0[VPX_SS_MAX_LAYERS];
  144. // Keep track of the spatial and temporal layer id of the frame that last
  145. // updated the frame buffer index.
  146. uint8_t fb_idx_spatial_layer_id[REF_FRAMES];
  147. uint8_t fb_idx_temporal_layer_id[REF_FRAMES];
  148. int spatial_layer_sync[VPX_SS_MAX_LAYERS];
  149. uint8_t set_intra_only_frame;
  150. uint8_t previous_frame_is_intra_only;
  151. uint8_t superframe_has_layer_sync;
  152. uint8_t fb_idx_base[REF_FRAMES];
  153. int use_set_ref_frame_config;
  154. int temporal_layer_id_per_spatial[VPX_SS_MAX_LAYERS];
  155. int first_spatial_layer_to_encode;
  156. // Parameters for allowing framerate per spatial layer, and buffer
  157. // update based on timestamps.
  158. int64_t duration[VPX_SS_MAX_LAYERS];
  159. int64_t timebase_fac;
  160. int64_t time_stamp_superframe;
  161. int64_t time_stamp_prev[VPX_SS_MAX_LAYERS];
  162. int num_encoded_top_layer;
  163. // Every spatial layer on a superframe whose base is key is key too.
  164. int simulcast_mode;
  165. } SVC;
  166. struct VP9_COMP;
  167. // Initialize layer context data from init_config().
  168. void vp9_init_layer_context(struct VP9_COMP *const cpi);
  169. // Update the layer context from a change_config() call.
  170. void vp9_update_layer_context_change_config(struct VP9_COMP *const cpi,
  171. const int target_bandwidth);
  172. // Prior to encoding the frame, update framerate-related quantities
  173. // for the current temporal layer.
  174. void vp9_update_temporal_layer_framerate(struct VP9_COMP *const cpi);
  175. // Update framerate-related quantities for the current spatial layer.
  176. void vp9_update_spatial_layer_framerate(struct VP9_COMP *const cpi,
  177. double framerate);
  178. // Prior to encoding the frame, set the layer context, for the current layer
  179. // to be encoded, to the cpi struct.
  180. void vp9_restore_layer_context(struct VP9_COMP *const cpi);
  181. // Save the layer context after encoding the frame.
  182. void vp9_save_layer_context(struct VP9_COMP *const cpi);
  183. // Initialize second pass rc for spatial svc.
  184. void vp9_init_second_pass_spatial_svc(struct VP9_COMP *cpi);
  185. void get_layer_resolution(const int width_org, const int height_org,
  186. const int num, const int den, int *width_out,
  187. int *height_out);
  188. // Increment number of video frames in layer
  189. void vp9_inc_frame_in_layer(struct VP9_COMP *const cpi);
  190. // Check if current layer is key frame in spatial upper layer
  191. int vp9_is_upper_layer_key_frame(const struct VP9_COMP *const cpi);
  192. // Get the next source buffer to encode
  193. struct lookahead_entry *vp9_svc_lookahead_pop(struct VP9_COMP *const cpi,
  194. struct lookahead_ctx *ctx,
  195. int drain);
  196. // Start a frame and initialize svc parameters
  197. int vp9_svc_start_frame(struct VP9_COMP *const cpi);
  198. #if CONFIG_VP9_TEMPORAL_DENOISING
  199. int vp9_denoise_svc_non_key(struct VP9_COMP *const cpi);
  200. #endif
  201. void vp9_copy_flags_ref_update_idx(struct VP9_COMP *const cpi);
  202. int vp9_one_pass_cbr_svc_start_layer(struct VP9_COMP *const cpi);
  203. void vp9_free_svc_cyclic_refresh(struct VP9_COMP *const cpi);
  204. void vp9_svc_reset_temporal_layers(struct VP9_COMP *const cpi, int is_key);
  205. void vp9_svc_check_reset_layer_rc_flag(struct VP9_COMP *const cpi);
  206. void vp9_svc_constrain_inter_layer_pred(struct VP9_COMP *const cpi);
  207. void vp9_svc_assert_constraints_pattern(struct VP9_COMP *const cpi);
  208. void vp9_svc_check_spatial_layer_sync(struct VP9_COMP *const cpi);
  209. void vp9_svc_update_ref_frame_buffer_idx(struct VP9_COMP *const cpi);
  210. void vp9_svc_update_ref_frame_key_simulcast(struct VP9_COMP *const cpi);
  211. void vp9_svc_update_ref_frame(struct VP9_COMP *const cpi);
  212. void vp9_svc_adjust_frame_rate(struct VP9_COMP *const cpi);
  213. void vp9_svc_adjust_avg_frame_qindex(struct VP9_COMP *const cpi);
  214. #ifdef __cplusplus
  215. } // extern "C"
  216. #endif
  217. #endif // VPX_VP9_ENCODER_VP9_SVC_LAYERCONTEXT_H_