vp8enci.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // WebP encoder: internal header.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #ifndef WEBP_ENC_VP8ENCI_H_
  14. #define WEBP_ENC_VP8ENCI_H_
  15. #include <string.h> // for memcpy()
  16. #include "../webp/encode.h"
  17. #include "../dsp/dsp.h"
  18. #include "../utils/bit_writer.h"
  19. #include "../utils/thread.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. //------------------------------------------------------------------------------
  24. // Various defines and enums
  25. // version numbers
  26. #define ENC_MAJ_VERSION 0
  27. #define ENC_MIN_VERSION 4
  28. #define ENC_REV_VERSION 2
  29. // intra prediction modes
  30. enum { B_DC_PRED = 0, // 4x4 modes
  31. B_TM_PRED = 1,
  32. B_VE_PRED = 2,
  33. B_HE_PRED = 3,
  34. B_RD_PRED = 4,
  35. B_VR_PRED = 5,
  36. B_LD_PRED = 6,
  37. B_VL_PRED = 7,
  38. B_HD_PRED = 8,
  39. B_HU_PRED = 9,
  40. NUM_BMODES = B_HU_PRED + 1 - B_DC_PRED, // = 10
  41. // Luma16 or UV modes
  42. DC_PRED = B_DC_PRED, V_PRED = B_VE_PRED,
  43. H_PRED = B_HE_PRED, TM_PRED = B_TM_PRED,
  44. NUM_PRED_MODES = 4
  45. };
  46. enum { NUM_MB_SEGMENTS = 4,
  47. MAX_NUM_PARTITIONS = 8,
  48. NUM_TYPES = 4, // 0: i16-AC, 1: i16-DC, 2:chroma-AC, 3:i4-AC
  49. NUM_BANDS = 8,
  50. NUM_CTX = 3,
  51. NUM_PROBAS = 11,
  52. MAX_LF_LEVELS = 64, // Maximum loop filter level
  53. MAX_VARIABLE_LEVEL = 67, // last (inclusive) level with variable cost
  54. MAX_LEVEL = 2047 // max level (note: max codable is 2047 + 67)
  55. };
  56. typedef enum { // Rate-distortion optimization levels
  57. RD_OPT_NONE = 0, // no rd-opt
  58. RD_OPT_BASIC = 1, // basic scoring (no trellis)
  59. RD_OPT_TRELLIS = 2, // perform trellis-quant on the final decision only
  60. RD_OPT_TRELLIS_ALL = 3 // trellis-quant for every scoring (much slower)
  61. } VP8RDLevel;
  62. // YUV-cache parameters. Cache is 16-pixels wide.
  63. // The original or reconstructed samples can be accessed using VP8Scan[]
  64. // The predicted blocks can be accessed using offsets to yuv_p_ and
  65. // the arrays VP8*ModeOffsets[];
  66. // +----+ YUV Samples area. See VP8Scan[] for accessing the blocks.
  67. // Y_OFF |YYYY| <- original samples ('yuv_in_')
  68. // |YYYY|
  69. // |YYYY|
  70. // |YYYY|
  71. // U_OFF |UUVV| V_OFF (=U_OFF + 8)
  72. // |UUVV|
  73. // +----+
  74. // Y_OFF |YYYY| <- compressed/decoded samples ('yuv_out_')
  75. // |YYYY| There are two buffers like this ('yuv_out_'/'yuv_out2_')
  76. // |YYYY|
  77. // |YYYY|
  78. // U_OFF |UUVV| V_OFF
  79. // |UUVV|
  80. // x2 (for yuv_out2_)
  81. // +----+ Prediction area ('yuv_p_', size = PRED_SIZE)
  82. // I16DC16 |YYYY| Intra16 predictions (16x16 block each)
  83. // |YYYY|
  84. // |YYYY|
  85. // |YYYY|
  86. // I16TM16 |YYYY|
  87. // |YYYY|
  88. // |YYYY|
  89. // |YYYY|
  90. // I16VE16 |YYYY|
  91. // |YYYY|
  92. // |YYYY|
  93. // |YYYY|
  94. // I16HE16 |YYYY|
  95. // |YYYY|
  96. // |YYYY|
  97. // |YYYY|
  98. // +----+ Chroma U/V predictions (16x8 block each)
  99. // C8DC8 |UUVV|
  100. // |UUVV|
  101. // C8TM8 |UUVV|
  102. // |UUVV|
  103. // C8VE8 |UUVV|
  104. // |UUVV|
  105. // C8HE8 |UUVV|
  106. // |UUVV|
  107. // +----+ Intra 4x4 predictions (4x4 block each)
  108. // |YYYY| I4DC4 I4TM4 I4VE4 I4HE4
  109. // |YYYY| I4RD4 I4VR4 I4LD4 I4VL4
  110. // |YY..| I4HD4 I4HU4 I4TMP
  111. // +----+
  112. #define BPS 16 // this is the common stride
  113. #define Y_SIZE (BPS * 16)
  114. #define UV_SIZE (BPS * 8)
  115. #define YUV_SIZE (Y_SIZE + UV_SIZE)
  116. #define PRED_SIZE (6 * 16 * BPS + 12 * BPS)
  117. #define Y_OFF (0)
  118. #define U_OFF (Y_SIZE)
  119. #define V_OFF (U_OFF + 8)
  120. #define ALIGN_CST 15
  121. #define DO_ALIGN(PTR) ((uintptr_t)((PTR) + ALIGN_CST) & ~ALIGN_CST)
  122. extern const int VP8Scan[16]; // in quant.c
  123. extern const int VP8UVModeOffsets[4]; // in analyze.c
  124. extern const int VP8I16ModeOffsets[4];
  125. extern const int VP8I4ModeOffsets[NUM_BMODES];
  126. // Layout of prediction blocks
  127. // intra 16x16
  128. #define I16DC16 (0 * 16 * BPS)
  129. #define I16TM16 (1 * 16 * BPS)
  130. #define I16VE16 (2 * 16 * BPS)
  131. #define I16HE16 (3 * 16 * BPS)
  132. // chroma 8x8, two U/V blocks side by side (hence: 16x8 each)
  133. #define C8DC8 (4 * 16 * BPS)
  134. #define C8TM8 (4 * 16 * BPS + 8 * BPS)
  135. #define C8VE8 (5 * 16 * BPS)
  136. #define C8HE8 (5 * 16 * BPS + 8 * BPS)
  137. // intra 4x4
  138. #define I4DC4 (6 * 16 * BPS + 0)
  139. #define I4TM4 (6 * 16 * BPS + 4)
  140. #define I4VE4 (6 * 16 * BPS + 8)
  141. #define I4HE4 (6 * 16 * BPS + 12)
  142. #define I4RD4 (6 * 16 * BPS + 4 * BPS + 0)
  143. #define I4VR4 (6 * 16 * BPS + 4 * BPS + 4)
  144. #define I4LD4 (6 * 16 * BPS + 4 * BPS + 8)
  145. #define I4VL4 (6 * 16 * BPS + 4 * BPS + 12)
  146. #define I4HD4 (6 * 16 * BPS + 8 * BPS + 0)
  147. #define I4HU4 (6 * 16 * BPS + 8 * BPS + 4)
  148. #define I4TMP (6 * 16 * BPS + 8 * BPS + 8)
  149. typedef int64_t score_t; // type used for scores, rate, distortion
  150. // Note that MAX_COST is not the maximum allowed by sizeof(score_t),
  151. // in order to allow overflowing computations.
  152. #define MAX_COST ((score_t)0x7fffffffffffffLL)
  153. #define QFIX 17
  154. #define BIAS(b) ((b) << (QFIX - 8))
  155. // Fun fact: this is the _only_ line where we're actually being lossy and
  156. // discarding bits.
  157. static WEBP_INLINE int QUANTDIV(uint32_t n, uint32_t iQ, uint32_t B) {
  158. return (int)((n * iQ + B) >> QFIX);
  159. }
  160. // size of histogram used by CollectHistogram.
  161. #define MAX_COEFF_THRESH 31
  162. typedef struct VP8Histogram VP8Histogram;
  163. struct VP8Histogram {
  164. // TODO(skal): we only need to store the max_value and last_non_zero actually.
  165. int distribution[MAX_COEFF_THRESH + 1];
  166. };
  167. // Uncomment the following to remove token-buffer code:
  168. // #define DISABLE_TOKEN_BUFFER
  169. //------------------------------------------------------------------------------
  170. // Headers
  171. typedef uint32_t proba_t; // 16b + 16b
  172. typedef uint8_t ProbaArray[NUM_CTX][NUM_PROBAS];
  173. typedef proba_t StatsArray[NUM_CTX][NUM_PROBAS];
  174. typedef uint16_t CostArray[NUM_CTX][MAX_VARIABLE_LEVEL + 1];
  175. typedef double LFStats[NUM_MB_SEGMENTS][MAX_LF_LEVELS]; // filter stats
  176. typedef struct VP8Encoder VP8Encoder;
  177. // segment features
  178. typedef struct {
  179. int num_segments_; // Actual number of segments. 1 segment only = unused.
  180. int update_map_; // whether to update the segment map or not.
  181. // must be 0 if there's only 1 segment.
  182. int size_; // bit-cost for transmitting the segment map
  183. } VP8SegmentHeader;
  184. // Struct collecting all frame-persistent probabilities.
  185. typedef struct {
  186. uint8_t segments_[3]; // probabilities for segment tree
  187. uint8_t skip_proba_; // final probability of being skipped.
  188. ProbaArray coeffs_[NUM_TYPES][NUM_BANDS]; // 1056 bytes
  189. StatsArray stats_[NUM_TYPES][NUM_BANDS]; // 4224 bytes
  190. CostArray level_cost_[NUM_TYPES][NUM_BANDS]; // 13056 bytes
  191. int dirty_; // if true, need to call VP8CalculateLevelCosts()
  192. int use_skip_proba_; // Note: we always use skip_proba for now.
  193. int nb_skip_; // number of skipped blocks
  194. } VP8Proba;
  195. // Filter parameters. Not actually used in the code (we don't perform
  196. // the in-loop filtering), but filled from user's config
  197. typedef struct {
  198. int simple_; // filtering type: 0=complex, 1=simple
  199. int level_; // base filter level [0..63]
  200. int sharpness_; // [0..7]
  201. int i4x4_lf_delta_; // delta filter level for i4x4 relative to i16x16
  202. } VP8FilterHeader;
  203. //------------------------------------------------------------------------------
  204. // Informations about the macroblocks.
  205. typedef struct {
  206. // block type
  207. unsigned int type_:2; // 0=i4x4, 1=i16x16
  208. unsigned int uv_mode_:2;
  209. unsigned int skip_:1;
  210. unsigned int segment_:2;
  211. uint8_t alpha_; // quantization-susceptibility
  212. } VP8MBInfo;
  213. typedef struct VP8Matrix {
  214. uint16_t q_[16]; // quantizer steps
  215. uint16_t iq_[16]; // reciprocals, fixed point.
  216. uint32_t bias_[16]; // rounding bias
  217. uint32_t zthresh_[16]; // value below which a coefficient is zeroed
  218. uint16_t sharpen_[16]; // frequency boosters for slight sharpening
  219. } VP8Matrix;
  220. typedef struct {
  221. VP8Matrix y1_, y2_, uv_; // quantization matrices
  222. int alpha_; // quant-susceptibility, range [-127,127]. Zero is neutral.
  223. // Lower values indicate a lower risk of blurriness.
  224. int beta_; // filter-susceptibility, range [0,255].
  225. int quant_; // final segment quantizer.
  226. int fstrength_; // final in-loop filtering strength
  227. int max_edge_; // max edge delta (for filtering strength)
  228. int min_disto_; // minimum distortion required to trigger filtering record
  229. // reactivities
  230. int lambda_i16_, lambda_i4_, lambda_uv_;
  231. int lambda_mode_, lambda_trellis_, tlambda_;
  232. int lambda_trellis_i16_, lambda_trellis_i4_, lambda_trellis_uv_;
  233. } VP8SegmentInfo;
  234. // Handy transient struct to accumulate score and info during RD-optimization
  235. // and mode evaluation.
  236. typedef struct {
  237. score_t D, SD; // Distortion, spectral distortion
  238. score_t H, R, score; // header bits, rate, score.
  239. int16_t y_dc_levels[16]; // Quantized levels for luma-DC, luma-AC, chroma.
  240. int16_t y_ac_levels[16][16];
  241. int16_t uv_levels[4 + 4][16];
  242. int mode_i16; // mode number for intra16 prediction
  243. uint8_t modes_i4[16]; // mode numbers for intra4 predictions
  244. int mode_uv; // mode number of chroma prediction
  245. uint32_t nz; // non-zero blocks
  246. } VP8ModeScore;
  247. // Iterator structure to iterate through macroblocks, pointing to the
  248. // right neighbouring data (samples, predictions, contexts, ...)
  249. typedef struct {
  250. int x_, y_; // current macroblock
  251. int y_stride_, uv_stride_; // respective strides
  252. uint8_t* yuv_in_; // input samples
  253. uint8_t* yuv_out_; // output samples
  254. uint8_t* yuv_out2_; // secondary buffer swapped with yuv_out_.
  255. uint8_t* yuv_p_; // scratch buffer for prediction
  256. VP8Encoder* enc_; // back-pointer
  257. VP8MBInfo* mb_; // current macroblock
  258. VP8BitWriter* bw_; // current bit-writer
  259. uint8_t* preds_; // intra mode predictors (4x4 blocks)
  260. uint32_t* nz_; // non-zero pattern
  261. uint8_t i4_boundary_[37]; // 32+5 boundary samples needed by intra4x4
  262. uint8_t* i4_top_; // pointer to the current top boundary sample
  263. int i4_; // current intra4x4 mode being tested
  264. int top_nz_[9]; // top-non-zero context.
  265. int left_nz_[9]; // left-non-zero. left_nz[8] is independent.
  266. uint64_t bit_count_[4][3]; // bit counters for coded levels.
  267. uint64_t luma_bits_; // macroblock bit-cost for luma
  268. uint64_t uv_bits_; // macroblock bit-cost for chroma
  269. LFStats* lf_stats_; // filter stats (borrowed from enc_)
  270. int do_trellis_; // if true, perform extra level optimisation
  271. int count_down_; // number of mb still to be processed
  272. int count_down0_; // starting counter value (for progress)
  273. int percent0_; // saved initial progress percent
  274. uint8_t* y_left_; // left luma samples (addressable from index -1 to 15).
  275. uint8_t* u_left_; // left u samples (addressable from index -1 to 7)
  276. uint8_t* v_left_; // left v samples (addressable from index -1 to 7)
  277. uint8_t* y_top_; // top luma samples at position 'x_'
  278. uint8_t* uv_top_; // top u/v samples at position 'x_', packed as 16 bytes
  279. // memory for storing y/u/v_left_ and yuv_in_/out_*
  280. uint8_t yuv_left_mem_[17 + 16 + 16 + 8 + ALIGN_CST]; // memory for *_left_
  281. uint8_t yuv_mem_[3 * YUV_SIZE + PRED_SIZE + ALIGN_CST]; // memory for yuv_*
  282. } VP8EncIterator;
  283. // in iterator.c
  284. // must be called first
  285. void VP8IteratorInit(VP8Encoder* const enc, VP8EncIterator* const it);
  286. // restart a scan
  287. void VP8IteratorReset(VP8EncIterator* const it);
  288. // reset iterator position to row 'y'
  289. void VP8IteratorSetRow(VP8EncIterator* const it, int y);
  290. // set count down (=number of iterations to go)
  291. void VP8IteratorSetCountDown(VP8EncIterator* const it, int count_down);
  292. // return true if iteration is finished
  293. int VP8IteratorIsDone(const VP8EncIterator* const it);
  294. // Import uncompressed samples from source.
  295. // If tmp_32 is not NULL, import boundary samples too.
  296. // tmp_32 is a 32-bytes scratch buffer that must be aligned in memory.
  297. void VP8IteratorImport(VP8EncIterator* const it, uint8_t* tmp_32);
  298. // export decimated samples
  299. void VP8IteratorExport(const VP8EncIterator* const it);
  300. // go to next macroblock. Returns false if not finished.
  301. int VP8IteratorNext(VP8EncIterator* const it);
  302. // save the yuv_out_ boundary values to top_/left_ arrays for next iterations.
  303. void VP8IteratorSaveBoundary(VP8EncIterator* const it);
  304. // Report progression based on macroblock rows. Return 0 for user-abort request.
  305. int VP8IteratorProgress(const VP8EncIterator* const it,
  306. int final_delta_percent);
  307. // Intra4x4 iterations
  308. void VP8IteratorStartI4(VP8EncIterator* const it);
  309. // returns true if not done.
  310. int VP8IteratorRotateI4(VP8EncIterator* const it,
  311. const uint8_t* const yuv_out);
  312. // Non-zero context setup/teardown
  313. void VP8IteratorNzToBytes(VP8EncIterator* const it);
  314. void VP8IteratorBytesToNz(VP8EncIterator* const it);
  315. // Helper functions to set mode properties
  316. void VP8SetIntra16Mode(const VP8EncIterator* const it, int mode);
  317. void VP8SetIntra4Mode(const VP8EncIterator* const it, const uint8_t* modes);
  318. void VP8SetIntraUVMode(const VP8EncIterator* const it, int mode);
  319. void VP8SetSkip(const VP8EncIterator* const it, int skip);
  320. void VP8SetSegment(const VP8EncIterator* const it, int segment);
  321. //------------------------------------------------------------------------------
  322. // Paginated token buffer
  323. typedef struct VP8Tokens VP8Tokens; // struct details in token.c
  324. typedef struct {
  325. #if !defined(DISABLE_TOKEN_BUFFER)
  326. VP8Tokens* pages_; // first page
  327. VP8Tokens** last_page_; // last page
  328. uint16_t* tokens_; // set to (*last_page_)->tokens_
  329. int left_; // how many free tokens left before the page is full
  330. int page_size_; // number of tokens per page
  331. #endif
  332. int error_; // true in case of malloc error
  333. } VP8TBuffer;
  334. // initialize an empty buffer
  335. void VP8TBufferInit(VP8TBuffer* const b, int page_size);
  336. void VP8TBufferClear(VP8TBuffer* const b); // de-allocate pages memory
  337. #if !defined(DISABLE_TOKEN_BUFFER)
  338. // Finalizes bitstream when probabilities are known.
  339. // Deletes the allocated token memory if final_pass is true.
  340. int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,
  341. const uint8_t* const probas, int final_pass);
  342. // record the coding of coefficients without knowing the probabilities yet
  343. int VP8RecordCoeffTokens(int ctx, int coeff_type, int first, int last,
  344. const int16_t* const coeffs,
  345. VP8TBuffer* const tokens);
  346. // Estimate the final coded size given a set of 'probas'.
  347. size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas);
  348. // unused for now
  349. void VP8TokenToStats(const VP8TBuffer* const b, proba_t* const stats);
  350. #endif // !DISABLE_TOKEN_BUFFER
  351. //------------------------------------------------------------------------------
  352. // VP8Encoder
  353. struct VP8Encoder {
  354. const WebPConfig* config_; // user configuration and parameters
  355. WebPPicture* pic_; // input / output picture
  356. // headers
  357. VP8FilterHeader filter_hdr_; // filtering information
  358. VP8SegmentHeader segment_hdr_; // segment information
  359. int profile_; // VP8's profile, deduced from Config.
  360. // dimension, in macroblock units.
  361. int mb_w_, mb_h_;
  362. int preds_w_; // stride of the *preds_ prediction plane (=4*mb_w + 1)
  363. // number of partitions (1, 2, 4 or 8 = MAX_NUM_PARTITIONS)
  364. int num_parts_;
  365. // per-partition boolean decoders.
  366. VP8BitWriter bw_; // part0
  367. VP8BitWriter parts_[MAX_NUM_PARTITIONS]; // token partitions
  368. VP8TBuffer tokens_; // token buffer
  369. int percent_; // for progress
  370. // transparency blob
  371. int has_alpha_;
  372. uint8_t* alpha_data_; // non-NULL if transparency is present
  373. uint32_t alpha_data_size_;
  374. WebPWorker alpha_worker_;
  375. // quantization info (one set of DC/AC dequant factor per segment)
  376. VP8SegmentInfo dqm_[NUM_MB_SEGMENTS];
  377. int base_quant_; // nominal quantizer value. Only used
  378. // for relative coding of segments' quant.
  379. int alpha_; // global susceptibility (<=> complexity)
  380. int uv_alpha_; // U/V quantization susceptibility
  381. // global offset of quantizers, shared by all segments
  382. int dq_y1_dc_;
  383. int dq_y2_dc_, dq_y2_ac_;
  384. int dq_uv_dc_, dq_uv_ac_;
  385. // probabilities and statistics
  386. VP8Proba proba_;
  387. uint64_t sse_[4]; // sum of Y/U/V/A squared errors for all macroblocks
  388. uint64_t sse_count_; // pixel count for the sse_[] stats
  389. int coded_size_;
  390. int residual_bytes_[3][4];
  391. int block_count_[3];
  392. // quality/speed settings
  393. int method_; // 0=fastest, 6=best/slowest.
  394. VP8RDLevel rd_opt_level_; // Deduced from method_.
  395. int max_i4_header_bits_; // partition #0 safeness factor
  396. int thread_level_; // derived from config->thread_level
  397. int do_search_; // derived from config->target_XXX
  398. int use_tokens_; // if true, use token buffer
  399. // Memory
  400. VP8MBInfo* mb_info_; // contextual macroblock infos (mb_w_ + 1)
  401. uint8_t* preds_; // predictions modes: (4*mb_w+1) * (4*mb_h+1)
  402. uint32_t* nz_; // non-zero bit context: mb_w+1
  403. uint8_t* y_top_; // top luma samples.
  404. uint8_t* uv_top_; // top u/v samples.
  405. // U and V are packed into 16 bytes (8 U + 8 V)
  406. LFStats* lf_stats_; // autofilter stats (if NULL, autofilter is off)
  407. };
  408. //------------------------------------------------------------------------------
  409. // internal functions. Not public.
  410. // in tree.c
  411. extern const uint8_t VP8CoeffsProba0[NUM_TYPES][NUM_BANDS][NUM_CTX][NUM_PROBAS];
  412. extern const uint8_t
  413. VP8CoeffsUpdateProba[NUM_TYPES][NUM_BANDS][NUM_CTX][NUM_PROBAS];
  414. // Reset the token probabilities to their initial (default) values
  415. void VP8DefaultProbas(VP8Encoder* const enc);
  416. // Write the token probabilities
  417. void VP8WriteProbas(VP8BitWriter* const bw, const VP8Proba* const probas);
  418. // Writes the partition #0 modes (that is: all intra modes)
  419. void VP8CodeIntraModes(VP8Encoder* const enc);
  420. // in syntax.c
  421. // Generates the final bitstream by coding the partition0 and headers,
  422. // and appending an assembly of all the pre-coded token partitions.
  423. // Return true if everything is ok.
  424. int VP8EncWrite(VP8Encoder* const enc);
  425. // Release memory allocated for bit-writing in VP8EncLoop & seq.
  426. void VP8EncFreeBitWriters(VP8Encoder* const enc);
  427. // in frame.c
  428. extern const uint8_t VP8EncBands[16 + 1];
  429. extern const uint8_t VP8Cat3[];
  430. extern const uint8_t VP8Cat4[];
  431. extern const uint8_t VP8Cat5[];
  432. extern const uint8_t VP8Cat6[];
  433. // Form all the four Intra16x16 predictions in the yuv_p_ cache
  434. void VP8MakeLuma16Preds(const VP8EncIterator* const it);
  435. // Form all the four Chroma8x8 predictions in the yuv_p_ cache
  436. void VP8MakeChroma8Preds(const VP8EncIterator* const it);
  437. // Form all the ten Intra4x4 predictions in the yuv_p_ cache
  438. // for the 4x4 block it->i4_
  439. void VP8MakeIntra4Preds(const VP8EncIterator* const it);
  440. // Rate calculation
  441. int VP8GetCostLuma16(VP8EncIterator* const it, const VP8ModeScore* const rd);
  442. int VP8GetCostLuma4(VP8EncIterator* const it, const int16_t levels[16]);
  443. int VP8GetCostUV(VP8EncIterator* const it, const VP8ModeScore* const rd);
  444. // Main coding calls
  445. int VP8EncLoop(VP8Encoder* const enc);
  446. int VP8EncTokenLoop(VP8Encoder* const enc);
  447. // in webpenc.c
  448. // Assign an error code to a picture. Return false for convenience.
  449. int WebPEncodingSetError(const WebPPicture* const pic, WebPEncodingError error);
  450. int WebPReportProgress(const WebPPicture* const pic,
  451. int percent, int* const percent_store);
  452. // in analysis.c
  453. // Main analysis loop. Decides the segmentations and complexity.
  454. // Assigns a first guess for Intra16 and uvmode_ prediction modes.
  455. int VP8EncAnalyze(VP8Encoder* const enc);
  456. // in quant.c
  457. // Sets up segment's quantization values, base_quant_ and filter strengths.
  458. void VP8SetSegmentParams(VP8Encoder* const enc, float quality);
  459. // Pick best modes and fills the levels. Returns true if skipped.
  460. int VP8Decimate(VP8EncIterator* const it, VP8ModeScore* const rd,
  461. VP8RDLevel rd_opt);
  462. // in alpha.c
  463. void VP8EncInitAlpha(VP8Encoder* const enc); // initialize alpha compression
  464. int VP8EncStartAlpha(VP8Encoder* const enc); // start alpha coding process
  465. int VP8EncFinishAlpha(VP8Encoder* const enc); // finalize compressed data
  466. int VP8EncDeleteAlpha(VP8Encoder* const enc); // delete compressed data
  467. // in filter.c
  468. // SSIM utils
  469. typedef struct {
  470. double w, xm, ym, xxm, xym, yym;
  471. } DistoStats;
  472. void VP8SSIMAddStats(const DistoStats* const src, DistoStats* const dst);
  473. void VP8SSIMAccumulatePlane(const uint8_t* src1, int stride1,
  474. const uint8_t* src2, int stride2,
  475. int W, int H, DistoStats* const stats);
  476. double VP8SSIMGet(const DistoStats* const stats);
  477. double VP8SSIMGetSquaredError(const DistoStats* const stats);
  478. // autofilter
  479. void VP8InitFilter(VP8EncIterator* const it);
  480. void VP8StoreFilterStats(VP8EncIterator* const it);
  481. void VP8AdjustFilterStrength(VP8EncIterator* const it);
  482. // returns the approximate filtering strength needed to smooth a edge
  483. // step of 'delta', given a sharpness parameter 'sharpness'.
  484. int VP8FilterStrengthFromDelta(int sharpness, int delta);
  485. // misc utils for picture_*.c:
  486. // Remove reference to the ARGB/YUVA buffer (doesn't free anything).
  487. void WebPPictureResetBuffers(WebPPicture* const picture);
  488. // Allocates ARGB buffer of given dimension (previous one is always free'd).
  489. // Preserves the YUV(A) buffer. Returns false in case of error (invalid param,
  490. // out-of-memory).
  491. int WebPPictureAllocARGB(WebPPicture* const picture, int width, int height);
  492. // Allocates YUVA buffer of given dimension (previous one is always free'd).
  493. // Uses picture->csp to determine whether an alpha buffer is needed.
  494. // Preserves the ARGB buffer.
  495. // Returns false in case of error (invalid param, out-of-memory).
  496. int WebPPictureAllocYUVA(WebPPicture* const picture, int width, int height);
  497. //------------------------------------------------------------------------------
  498. #if WEBP_ENCODER_ABI_VERSION <= 0x0203
  499. void WebPMemoryWriterClear(WebPMemoryWriter* writer);
  500. #endif
  501. #ifdef __cplusplus
  502. } // extern "C"
  503. #endif
  504. #endif /* WEBP_ENC_VP8ENCI_H_ */