2
0

vp9_encoder.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Copyright (c) 2010 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 VP9_ENCODER_VP9_ENCODER_H_
  11. #define VP9_ENCODER_VP9_ENCODER_H_
  12. #include <stdio.h>
  13. #include "./vpx_config.h"
  14. #include "vpx/internal/vpx_codec_internal.h"
  15. #include "vpx/vp8cx.h"
  16. #if CONFIG_INTERNAL_STATS
  17. #include "vpx_dsp/ssim.h"
  18. #endif
  19. #include "vpx_dsp/variance.h"
  20. #include "vpx_ports/system_state.h"
  21. #include "vpx_util/vpx_thread.h"
  22. #include "vp9/common/vp9_alloccommon.h"
  23. #include "vp9/common/vp9_ppflags.h"
  24. #include "vp9/common/vp9_entropymode.h"
  25. #include "vp9/common/vp9_thread_common.h"
  26. #include "vp9/common/vp9_onyxc_int.h"
  27. #include "vp9/encoder/vp9_alt_ref_aq.h"
  28. #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
  29. #include "vp9/encoder/vp9_context_tree.h"
  30. #include "vp9/encoder/vp9_encodemb.h"
  31. #include "vp9/encoder/vp9_firstpass.h"
  32. #include "vp9/encoder/vp9_lookahead.h"
  33. #include "vp9/encoder/vp9_mbgraph.h"
  34. #include "vp9/encoder/vp9_mcomp.h"
  35. #include "vp9/encoder/vp9_noise_estimate.h"
  36. #include "vp9/encoder/vp9_quantize.h"
  37. #include "vp9/encoder/vp9_ratectrl.h"
  38. #include "vp9/encoder/vp9_rd.h"
  39. #include "vp9/encoder/vp9_speed_features.h"
  40. #include "vp9/encoder/vp9_svc_layercontext.h"
  41. #include "vp9/encoder/vp9_tokenize.h"
  42. #if CONFIG_VP9_TEMPORAL_DENOISING
  43. #include "vp9/encoder/vp9_denoiser.h"
  44. #endif
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. // vp9 uses 10,000,000 ticks/second as time stamp
  49. #define TICKS_PER_SEC 10000000
  50. typedef struct {
  51. int nmvjointcost[MV_JOINTS];
  52. int nmvcosts[2][MV_VALS];
  53. int nmvcosts_hp[2][MV_VALS];
  54. vpx_prob segment_pred_probs[PREDICTION_PROBS];
  55. unsigned char *last_frame_seg_map_copy;
  56. // 0 = Intra, Last, GF, ARF
  57. signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS];
  58. // 0 = ZERO_MV, MV
  59. signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
  60. FRAME_CONTEXT fc;
  61. } CODING_CONTEXT;
  62. typedef enum {
  63. // encode_breakout is disabled.
  64. ENCODE_BREAKOUT_DISABLED = 0,
  65. // encode_breakout is enabled.
  66. ENCODE_BREAKOUT_ENABLED = 1,
  67. // encode_breakout is enabled with small max_thresh limit.
  68. ENCODE_BREAKOUT_LIMITED = 2
  69. } ENCODE_BREAKOUT_TYPE;
  70. typedef enum {
  71. NORMAL = 0,
  72. FOURFIVE = 1,
  73. THREEFIVE = 2,
  74. ONETWO = 3
  75. } VPX_SCALING;
  76. typedef enum {
  77. // Good Quality Fast Encoding. The encoder balances quality with the amount of
  78. // time it takes to encode the output. Speed setting controls how fast.
  79. GOOD,
  80. // The encoder places priority on the quality of the output over encoding
  81. // speed. The output is compressed at the highest possible quality. This
  82. // option takes the longest amount of time to encode. Speed setting ignored.
  83. BEST,
  84. // Realtime/Live Encoding. This mode is optimized for realtime encoding (for
  85. // example, capturing a television signal or feed from a live camera). Speed
  86. // setting controls how fast.
  87. REALTIME
  88. } MODE;
  89. typedef enum {
  90. FRAMEFLAGS_KEY = 1 << 0,
  91. FRAMEFLAGS_GOLDEN = 1 << 1,
  92. FRAMEFLAGS_ALTREF = 1 << 2,
  93. } FRAMETYPE_FLAGS;
  94. typedef enum {
  95. NO_AQ = 0,
  96. VARIANCE_AQ = 1,
  97. COMPLEXITY_AQ = 2,
  98. CYCLIC_REFRESH_AQ = 3,
  99. EQUATOR360_AQ = 4,
  100. // AQ based on lookahead temporal
  101. // variance (only valid for altref frames)
  102. LOOKAHEAD_AQ = 5,
  103. AQ_MODE_COUNT // This should always be the last member of the enum
  104. } AQ_MODE;
  105. typedef enum {
  106. RESIZE_NONE = 0, // No frame resizing allowed (except for SVC).
  107. RESIZE_FIXED = 1, // All frames are coded at the specified dimension.
  108. RESIZE_DYNAMIC = 2 // Coded size of each frame is determined by the codec.
  109. } RESIZE_TYPE;
  110. typedef struct VP9EncoderConfig {
  111. BITSTREAM_PROFILE profile;
  112. vpx_bit_depth_t bit_depth; // Codec bit-depth.
  113. int width; // width of data passed to the compressor
  114. int height; // height of data passed to the compressor
  115. unsigned int input_bit_depth; // Input bit depth.
  116. double init_framerate; // set to passed in framerate
  117. int64_t target_bandwidth; // bandwidth to be used in bits per second
  118. int noise_sensitivity; // pre processing blur: recommendation 0
  119. int sharpness; // sharpening output: recommendation 0:
  120. int speed;
  121. // maximum allowed bitrate for any intra frame in % of bitrate target.
  122. unsigned int rc_max_intra_bitrate_pct;
  123. // maximum allowed bitrate for any inter frame in % of bitrate target.
  124. unsigned int rc_max_inter_bitrate_pct;
  125. // percent of rate boost for golden frame in CBR mode.
  126. unsigned int gf_cbr_boost_pct;
  127. MODE mode;
  128. int pass;
  129. // Key Framing Operations
  130. int auto_key; // autodetect cut scenes and set the keyframes
  131. int key_freq; // maximum distance to key frame.
  132. int lag_in_frames; // how many frames lag before we start encoding
  133. // ----------------------------------------------------------------
  134. // DATARATE CONTROL OPTIONS
  135. // vbr, cbr, constrained quality or constant quality
  136. enum vpx_rc_mode rc_mode;
  137. // buffer targeting aggressiveness
  138. int under_shoot_pct;
  139. int over_shoot_pct;
  140. // buffering parameters
  141. int64_t starting_buffer_level_ms;
  142. int64_t optimal_buffer_level_ms;
  143. int64_t maximum_buffer_size_ms;
  144. // Frame drop threshold.
  145. int drop_frames_water_mark;
  146. // controlling quality
  147. int fixed_q;
  148. int worst_allowed_q;
  149. int best_allowed_q;
  150. int cq_level;
  151. AQ_MODE aq_mode; // Adaptive Quantization mode
  152. // Special handling of Adaptive Quantization for AltRef frames
  153. int alt_ref_aq;
  154. // Internal frame size scaling.
  155. RESIZE_TYPE resize_mode;
  156. int scaled_frame_width;
  157. int scaled_frame_height;
  158. // Enable feature to reduce the frame quantization every x frames.
  159. int frame_periodic_boost;
  160. // two pass datarate control
  161. int two_pass_vbrbias; // two pass datarate control tweaks
  162. int two_pass_vbrmin_section;
  163. int two_pass_vbrmax_section;
  164. // END DATARATE CONTROL OPTIONS
  165. // ----------------------------------------------------------------
  166. // Spatial and temporal scalability.
  167. int ss_number_layers; // Number of spatial layers.
  168. int ts_number_layers; // Number of temporal layers.
  169. // Bitrate allocation for spatial layers.
  170. int layer_target_bitrate[VPX_MAX_LAYERS];
  171. int ss_target_bitrate[VPX_SS_MAX_LAYERS];
  172. int ss_enable_auto_arf[VPX_SS_MAX_LAYERS];
  173. // Bitrate allocation (CBR mode) and framerate factor, for temporal layers.
  174. int ts_rate_decimator[VPX_TS_MAX_LAYERS];
  175. int enable_auto_arf;
  176. int encode_breakout; // early breakout : for video conf recommend 800
  177. /* Bitfield defining the error resiliency features to enable.
  178. * Can provide decodable frames after losses in previous
  179. * frames and decodable partitions after losses in the same frame.
  180. */
  181. unsigned int error_resilient_mode;
  182. /* Bitfield defining the parallel decoding mode where the
  183. * decoding in successive frames may be conducted in parallel
  184. * just by decoding the frame headers.
  185. */
  186. unsigned int frame_parallel_decoding_mode;
  187. int arnr_max_frames;
  188. int arnr_strength;
  189. int min_gf_interval;
  190. int max_gf_interval;
  191. int tile_columns;
  192. int tile_rows;
  193. int max_threads;
  194. int target_level;
  195. vpx_fixed_buf_t two_pass_stats_in;
  196. struct vpx_codec_pkt_list *output_pkt_list;
  197. #if CONFIG_FP_MB_STATS
  198. vpx_fixed_buf_t firstpass_mb_stats_in;
  199. #endif
  200. vp8e_tuning tuning;
  201. vp9e_tune_content content;
  202. #if CONFIG_VP9_HIGHBITDEPTH
  203. int use_highbitdepth;
  204. #endif
  205. vpx_color_space_t color_space;
  206. vpx_color_range_t color_range;
  207. int render_width;
  208. int render_height;
  209. VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode;
  210. } VP9EncoderConfig;
  211. static INLINE int is_lossless_requested(const VP9EncoderConfig *cfg) {
  212. return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0;
  213. }
  214. // TODO(jingning) All spatially adaptive variables should go to TileDataEnc.
  215. typedef struct TileDataEnc {
  216. TileInfo tile_info;
  217. int thresh_freq_fact[BLOCK_SIZES][MAX_MODES];
  218. int mode_map[BLOCK_SIZES][MAX_MODES];
  219. } TileDataEnc;
  220. typedef struct RD_COUNTS {
  221. vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES];
  222. int64_t comp_pred_diff[REFERENCE_MODES];
  223. int64_t filter_diff[SWITCHABLE_FILTER_CONTEXTS];
  224. int m_search_count;
  225. int ex_search_count;
  226. } RD_COUNTS;
  227. typedef struct ThreadData {
  228. MACROBLOCK mb;
  229. RD_COUNTS rd_counts;
  230. FRAME_COUNTS *counts;
  231. PICK_MODE_CONTEXT *leaf_tree;
  232. PC_TREE *pc_tree;
  233. PC_TREE *pc_root;
  234. } ThreadData;
  235. struct EncWorkerData;
  236. typedef struct ActiveMap {
  237. int enabled;
  238. int update;
  239. unsigned char *map;
  240. } ActiveMap;
  241. typedef enum { Y, U, V, ALL } STAT_TYPE;
  242. typedef struct IMAGE_STAT {
  243. double stat[ALL + 1];
  244. double worst;
  245. } ImageStat;
  246. // Kf noise filtering currently disabled by default in build.
  247. // #define ENABLE_KF_DENOISE 1
  248. #define CPB_WINDOW_SIZE 4
  249. #define FRAME_WINDOW_SIZE 128
  250. #define SAMPLE_RATE_GRACE_P 0.015
  251. #define VP9_LEVELS 14
  252. typedef enum {
  253. LEVEL_UNKNOWN = 0,
  254. LEVEL_1 = 10,
  255. LEVEL_1_1 = 11,
  256. LEVEL_2 = 20,
  257. LEVEL_2_1 = 21,
  258. LEVEL_3 = 30,
  259. LEVEL_3_1 = 31,
  260. LEVEL_4 = 40,
  261. LEVEL_4_1 = 41,
  262. LEVEL_5 = 50,
  263. LEVEL_5_1 = 51,
  264. LEVEL_5_2 = 52,
  265. LEVEL_6 = 60,
  266. LEVEL_6_1 = 61,
  267. LEVEL_6_2 = 62,
  268. LEVEL_MAX = 255
  269. } VP9_LEVEL;
  270. typedef struct {
  271. VP9_LEVEL level;
  272. uint64_t max_luma_sample_rate;
  273. uint32_t max_luma_picture_size;
  274. double average_bitrate; // in kilobits per second
  275. double max_cpb_size; // in kilobits
  276. double compression_ratio;
  277. uint8_t max_col_tiles;
  278. uint32_t min_altref_distance;
  279. uint8_t max_ref_frame_buffers;
  280. } Vp9LevelSpec;
  281. typedef struct {
  282. int64_t ts; // timestamp
  283. uint32_t luma_samples;
  284. uint32_t size; // in bytes
  285. } FrameRecord;
  286. typedef struct {
  287. FrameRecord buf[FRAME_WINDOW_SIZE];
  288. uint8_t start;
  289. uint8_t len;
  290. } FrameWindowBuffer;
  291. typedef struct {
  292. uint8_t seen_first_altref;
  293. uint32_t frames_since_last_altref;
  294. uint64_t total_compressed_size;
  295. uint64_t total_uncompressed_size;
  296. double time_encoded; // in seconds
  297. FrameWindowBuffer frame_window_buffer;
  298. int ref_refresh_map;
  299. } Vp9LevelStats;
  300. typedef struct {
  301. Vp9LevelStats level_stats;
  302. Vp9LevelSpec level_spec;
  303. } Vp9LevelInfo;
  304. typedef struct VP9_COMP {
  305. QUANTS quants;
  306. ThreadData td;
  307. MB_MODE_INFO_EXT *mbmi_ext_base;
  308. DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]);
  309. DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]);
  310. VP9_COMMON common;
  311. VP9EncoderConfig oxcf;
  312. struct lookahead_ctx *lookahead;
  313. struct lookahead_entry *alt_ref_source;
  314. YV12_BUFFER_CONFIG *Source;
  315. YV12_BUFFER_CONFIG *Last_Source; // NULL for first frame and alt_ref frames
  316. YV12_BUFFER_CONFIG *un_scaled_source;
  317. YV12_BUFFER_CONFIG scaled_source;
  318. YV12_BUFFER_CONFIG *unscaled_last_source;
  319. YV12_BUFFER_CONFIG scaled_last_source;
  320. #ifdef ENABLE_KF_DENOISE
  321. YV12_BUFFER_CONFIG raw_unscaled_source;
  322. YV12_BUFFER_CONFIG raw_scaled_source;
  323. #endif
  324. YV12_BUFFER_CONFIG *raw_source_frame;
  325. TileDataEnc *tile_data;
  326. int allocated_tiles; // Keep track of memory allocated for tiles.
  327. // For a still frame, this flag is set to 1 to skip partition search.
  328. int partition_search_skippable_frame;
  329. int scaled_ref_idx[MAX_REF_FRAMES];
  330. int lst_fb_idx;
  331. int gld_fb_idx;
  332. int alt_fb_idx;
  333. int refresh_last_frame;
  334. int refresh_golden_frame;
  335. int refresh_alt_ref_frame;
  336. int ext_refresh_frame_flags_pending;
  337. int ext_refresh_last_frame;
  338. int ext_refresh_golden_frame;
  339. int ext_refresh_alt_ref_frame;
  340. int ext_refresh_frame_context_pending;
  341. int ext_refresh_frame_context;
  342. YV12_BUFFER_CONFIG last_frame_uf;
  343. TOKENEXTRA *tile_tok[4][1 << 6];
  344. uint32_t tok_count[4][1 << 6];
  345. // Ambient reconstruction err target for force key frames
  346. int64_t ambient_err;
  347. RD_OPT rd;
  348. CODING_CONTEXT coding_context;
  349. int *nmvcosts[2];
  350. int *nmvcosts_hp[2];
  351. int *nmvsadcosts[2];
  352. int *nmvsadcosts_hp[2];
  353. int64_t last_time_stamp_seen;
  354. int64_t last_end_time_stamp_seen;
  355. int64_t first_time_stamp_ever;
  356. RATE_CONTROL rc;
  357. double framerate;
  358. int interp_filter_selected[MAX_REF_FRAMES][SWITCHABLE];
  359. struct vpx_codec_pkt_list *output_pkt_list;
  360. MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS];
  361. int mbgraph_n_frames; // number of frames filled in the above
  362. int static_mb_pct; // % forced skip mbs by segmentation
  363. int ref_frame_flags;
  364. SPEED_FEATURES sf;
  365. uint32_t max_mv_magnitude;
  366. int mv_step_param;
  367. int allow_comp_inter_inter;
  368. // Default value is 1. From first pass stats, encode_breakout may be disabled.
  369. ENCODE_BREAKOUT_TYPE allow_encode_breakout;
  370. // Get threshold from external input. A suggested threshold is 800 for HD
  371. // clips, and 300 for < HD clips.
  372. int encode_breakout;
  373. uint8_t *segmentation_map;
  374. // segment threashold for encode breakout
  375. int segment_encode_breakout[MAX_SEGMENTS];
  376. CYCLIC_REFRESH *cyclic_refresh;
  377. ActiveMap active_map;
  378. fractional_mv_step_fp *find_fractional_mv_step;
  379. vp9_full_search_fn_t full_search_sad;
  380. vp9_diamond_search_fn_t diamond_search_sad;
  381. vp9_variance_fn_ptr_t fn_ptr[BLOCK_SIZES];
  382. uint64_t time_receive_data;
  383. uint64_t time_compress_data;
  384. uint64_t time_pick_lpf;
  385. uint64_t time_encode_sb_row;
  386. #if CONFIG_FP_MB_STATS
  387. int use_fp_mb_stats;
  388. #endif
  389. TWO_PASS twopass;
  390. // Force recalculation of segment_ids for each mode info
  391. uint8_t force_update_segmentation;
  392. YV12_BUFFER_CONFIG alt_ref_buffer;
  393. // class responsible for adaptive
  394. // quantization of altref frames
  395. struct ALT_REF_AQ *alt_ref_aq;
  396. #if CONFIG_INTERNAL_STATS
  397. unsigned int mode_chosen_counts[MAX_MODES];
  398. int count;
  399. uint64_t total_sq_error;
  400. uint64_t total_samples;
  401. ImageStat psnr;
  402. uint64_t totalp_sq_error;
  403. uint64_t totalp_samples;
  404. ImageStat psnrp;
  405. double total_blockiness;
  406. double worst_blockiness;
  407. int bytes;
  408. double summed_quality;
  409. double summed_weights;
  410. double summedp_quality;
  411. double summedp_weights;
  412. unsigned int tot_recode_hits;
  413. double worst_ssim;
  414. ImageStat ssimg;
  415. ImageStat fastssim;
  416. ImageStat psnrhvs;
  417. int b_calculate_ssimg;
  418. int b_calculate_blockiness;
  419. int b_calculate_consistency;
  420. double total_inconsistency;
  421. double worst_consistency;
  422. Ssimv *ssim_vars;
  423. Metrics metrics;
  424. #endif
  425. int b_calculate_psnr;
  426. int droppable;
  427. int initial_width;
  428. int initial_height;
  429. int initial_mbs; // Number of MBs in the full-size frame; to be used to
  430. // normalize the firstpass stats. This will differ from the
  431. // number of MBs in the current frame when the frame is
  432. // scaled.
  433. int use_svc;
  434. SVC svc;
  435. // Store frame variance info in SOURCE_VAR_BASED_PARTITION search type.
  436. diff *source_diff_var;
  437. // The threshold used in SOURCE_VAR_BASED_PARTITION search type.
  438. unsigned int source_var_thresh;
  439. int frames_till_next_var_check;
  440. int frame_flags;
  441. search_site_config ss_cfg;
  442. int mbmode_cost[INTRA_MODES];
  443. unsigned int inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
  444. int intra_uv_mode_cost[FRAME_TYPES][INTRA_MODES][INTRA_MODES];
  445. int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
  446. int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
  447. int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
  448. int multi_arf_allowed;
  449. int multi_arf_enabled;
  450. int multi_arf_last_grp_enabled;
  451. #if CONFIG_VP9_TEMPORAL_DENOISING
  452. VP9_DENOISER denoiser;
  453. #endif
  454. int resize_pending;
  455. int resize_state;
  456. int external_resize;
  457. int resize_scale_num;
  458. int resize_scale_den;
  459. int resize_avg_qp;
  460. int resize_buffer_underflow;
  461. int resize_count;
  462. int use_skin_detection;
  463. int target_level;
  464. NOISE_ESTIMATE noise_estimate;
  465. // Count on how many consecutive times a block uses small/zeromv for encoding.
  466. uint8_t *consec_zero_mv;
  467. // VAR_BASED_PARTITION thresholds
  468. // 0 - threshold_64x64; 1 - threshold_32x32;
  469. // 2 - threshold_16x16; 3 - vbp_threshold_8x8;
  470. int64_t vbp_thresholds[4];
  471. int64_t vbp_threshold_minmax;
  472. int64_t vbp_threshold_sad;
  473. BLOCK_SIZE vbp_bsize_min;
  474. // Multi-threading
  475. int num_workers;
  476. VPxWorker *workers;
  477. struct EncWorkerData *tile_thr_data;
  478. VP9LfSync lf_row_sync;
  479. int keep_level_stats;
  480. Vp9LevelInfo level_info;
  481. } VP9_COMP;
  482. void vp9_initialize_enc(void);
  483. struct VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
  484. BufferPool *const pool);
  485. void vp9_remove_compressor(VP9_COMP *cpi);
  486. void vp9_change_config(VP9_COMP *cpi, const VP9EncoderConfig *oxcf);
  487. // receive a frames worth of data. caller can assume that a copy of this
  488. // frame is made and not just a copy of the pointer..
  489. int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
  490. YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
  491. int64_t end_time_stamp);
  492. int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
  493. size_t *size, uint8_t *dest, int64_t *time_stamp,
  494. int64_t *time_end, int flush);
  495. int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
  496. vp9_ppflags_t *flags);
  497. int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags);
  498. void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags);
  499. int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
  500. YV12_BUFFER_CONFIG *sd);
  501. int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
  502. YV12_BUFFER_CONFIG *sd);
  503. int vp9_update_entropy(VP9_COMP *cpi, int update);
  504. int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols);
  505. int vp9_get_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols);
  506. int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
  507. VPX_SCALING vert_mode);
  508. int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
  509. unsigned int height);
  510. void vp9_set_svc(VP9_COMP *cpi, int use_svc);
  511. int vp9_get_quantizer(struct VP9_COMP *cpi);
  512. static INLINE int frame_is_kf_gf_arf(const VP9_COMP *cpi) {
  513. return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame ||
  514. (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref);
  515. }
  516. static INLINE int get_ref_frame_map_idx(const VP9_COMP *cpi,
  517. MV_REFERENCE_FRAME ref_frame) {
  518. if (ref_frame == LAST_FRAME) {
  519. return cpi->lst_fb_idx;
  520. } else if (ref_frame == GOLDEN_FRAME) {
  521. return cpi->gld_fb_idx;
  522. } else {
  523. return cpi->alt_fb_idx;
  524. }
  525. }
  526. static INLINE int get_ref_frame_buf_idx(const VP9_COMP *const cpi,
  527. int ref_frame) {
  528. const VP9_COMMON *const cm = &cpi->common;
  529. const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
  530. return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX;
  531. }
  532. static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer(
  533. VP9_COMP *cpi, MV_REFERENCE_FRAME ref_frame) {
  534. VP9_COMMON *const cm = &cpi->common;
  535. const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
  536. return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf
  537. : NULL;
  538. }
  539. static INLINE int get_token_alloc(int mb_rows, int mb_cols) {
  540. // TODO(JBB): double check we can't exceed this token count if we have a
  541. // 32x32 transform crossing a boundary at a multiple of 16.
  542. // mb_rows, cols are in units of 16 pixels. We assume 3 planes all at full
  543. // resolution. We assume up to 1 token per pixel, and then allow
  544. // a head room of 4.
  545. return mb_rows * mb_cols * (16 * 16 * 3 + 4);
  546. }
  547. // Get the allocated token size for a tile. It does the same calculation as in
  548. // the frame token allocation.
  549. static INLINE int allocated_tokens(TileInfo tile) {
  550. int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 1) >> 1;
  551. int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 1) >> 1;
  552. return get_token_alloc(tile_mb_rows, tile_mb_cols);
  553. }
  554. int64_t vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b);
  555. #if CONFIG_VP9_HIGHBITDEPTH
  556. int64_t vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
  557. const YV12_BUFFER_CONFIG *b);
  558. #endif // CONFIG_VP9_HIGHBITDEPTH
  559. void vp9_scale_references(VP9_COMP *cpi);
  560. void vp9_update_reference_frames(VP9_COMP *cpi);
  561. void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv);
  562. YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(VP9_COMMON *cm,
  563. YV12_BUFFER_CONFIG *unscaled,
  564. YV12_BUFFER_CONFIG *scaled,
  565. YV12_BUFFER_CONFIG *scaled_temp);
  566. YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
  567. YV12_BUFFER_CONFIG *unscaled,
  568. YV12_BUFFER_CONFIG *scaled,
  569. int use_normative_scaler);
  570. void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags);
  571. static INLINE int is_two_pass_svc(const struct VP9_COMP *const cpi) {
  572. return cpi->use_svc && cpi->oxcf.pass != 0;
  573. }
  574. static INLINE int is_one_pass_cbr_svc(const struct VP9_COMP *const cpi) {
  575. return (cpi->use_svc && cpi->oxcf.pass == 0);
  576. }
  577. static INLINE int is_altref_enabled(const VP9_COMP *const cpi) {
  578. return cpi->oxcf.mode != REALTIME && cpi->oxcf.lag_in_frames > 0 &&
  579. (cpi->oxcf.enable_auto_arf &&
  580. (!is_two_pass_svc(cpi) ||
  581. cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id]));
  582. }
  583. static INLINE void set_ref_ptrs(VP9_COMMON *cm, MACROBLOCKD *xd,
  584. MV_REFERENCE_FRAME ref0,
  585. MV_REFERENCE_FRAME ref1) {
  586. xd->block_refs[0] =
  587. &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0];
  588. xd->block_refs[1] =
  589. &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0];
  590. }
  591. static INLINE int get_chessboard_index(const int frame_index) {
  592. return frame_index & 0x1;
  593. }
  594. static INLINE int *cond_cost_list(const struct VP9_COMP *cpi, int *cost_list) {
  595. return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL;
  596. }
  597. VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec);
  598. void vp9_new_framerate(VP9_COMP *cpi, double framerate);
  599. #define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl))
  600. #ifdef __cplusplus
  601. } // extern "C"
  602. #endif
  603. #endif // VP9_ENCODER_VP9_ENCODER_H_