vp9_encoder.h 27 KB

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