// Copyright 2011 Google Inc. All Rights Reserved. // // Use of this source code is governed by a BSD-style license // that can be found in the COPYING file in the root of the source // tree. An additional intellectual property rights grant can be found // in the file PATENTS. All contributing project authors may // be found in the AUTHORS file in the root of the source tree. // ----------------------------------------------------------------------------- // // WebP encoder: internal header. // // Author: Skal ([email protected]) #ifndef WEBP_ENC_VP8I_ENC_H_ #define WEBP_ENC_VP8I_ENC_H_ #include <string.h> // for memcpy() #include "src/dec/common_dec.h" #include "src/dsp/dsp.h" #include "src/utils/bit_writer_utils.h" #include "src/utils/thread_utils.h" #include "src/utils/utils.h" #include "src/webp/encode.h" #ifdef __cplusplus extern "C" { #endif //------------------------------------------------------------------------------ // Various defines and enums // version numbers #define ENC_MAJ_VERSION … #define ENC_MIN_VERSION … #define ENC_REV_VERSION … enum { … }; VP8RDLevel; // YUV-cache parameters. Cache is 32-bytes wide (= one cacheline). // The original or reconstructed samples can be accessed using VP8Scan[]. // The predicted blocks can be accessed using offsets to yuv_p_ and // the arrays VP8*ModeOffsets[]. // * YUV Samples area (yuv_in_/yuv_out_/yuv_out2_) // (see VP8Scan[] for accessing the blocks, along with // Y_OFF_ENC/U_OFF_ENC/V_OFF_ENC): // +----+----+ // Y_OFF_ENC |YYYY|UUVV| // U_OFF_ENC |YYYY|UUVV| // V_OFF_ENC |YYYY|....| <- 25% wasted U/V area // |YYYY|....| // +----+----+ // * Prediction area ('yuv_p_', size = PRED_SIZE_ENC) // Intra16 predictions (16x16 block each, two per row): // |I16DC16|I16TM16| // |I16VE16|I16HE16| // Chroma U/V predictions (16x8 block each, two per row): // |C8DC8|C8TM8| // |C8VE8|C8HE8| // Intra 4x4 predictions (4x4 block each) // |I4DC4 I4TM4 I4VE4 I4HE4|I4RD4 I4VR4 I4LD4 I4VL4| // |I4HD4 I4HU4 I4TMP .....|.......................| <- ~31% wasted #define YUV_SIZE_ENC … #define PRED_SIZE_ENC … #define Y_OFF_ENC … #define U_OFF_ENC … #define V_OFF_ENC … extern const uint16_t VP8Scan[16]; extern const uint16_t VP8UVModeOffsets[4]; extern const uint16_t VP8I16ModeOffsets[4]; extern const uint16_t VP8I4ModeOffsets[NUM_BMODES]; // Layout of prediction blocks // intra 16x16 #define I16DC16 … #define I16TM16 … #define I16VE16 … #define I16HE16 … // chroma 8x8, two U/V blocks side by side (hence: 16x8 each) #define C8DC8 … #define C8TM8 … #define C8VE8 … #define C8HE8 … // intra 4x4 #define I4DC4 … #define I4TM4 … #define I4VE4 … #define I4HE4 … #define I4RD4 … #define I4VR4 … #define I4LD4 … #define I4VL4 … #define I4HD4 … #define I4HU4 … #define I4TMP … score_t; // type used for scores, rate, distortion // Note that MAX_COST is not the maximum allowed by sizeof(score_t), // in order to allow overflowing computations. #define MAX_COST … #define QFIX … #define BIAS(b) … // Fun fact: this is the _only_ line where we're actually being lossy and // discarding bits. static WEBP_INLINE int QUANTDIV(uint32_t n, uint32_t iQ, uint32_t B) { … } // Uncomment the following to remove token-buffer code: // #define DISABLE_TOKEN_BUFFER // quality below which error-diffusion is enabled #define ERROR_DIFFUSION_QUALITY … //------------------------------------------------------------------------------ // Headers proba_t; // 16b + 16b ProbaArray; StatsArray; CostArray; CostArrayPtr; // for easy casting CostArrayMap; LFStats; // filter stats VP8Encoder; // segment features VP8EncSegmentHeader; // Struct collecting all frame-persistent probabilities. VP8EncProba; // Filter parameters. Not actually used in the code (we don't perform // the in-loop filtering), but filled from user's config VP8EncFilterHeader; //------------------------------------------------------------------------------ // Informations about the macroblocks. VP8MBInfo; VP8Matrix; VP8SegmentInfo; DError; // Handy transient struct to accumulate score and info during RD-optimization // and mode evaluation. VP8ModeScore; // Iterator structure to iterate through macroblocks, pointing to the // right neighbouring data (samples, predictions, contexts, ...) VP8EncIterator; // in iterator.c // must be called first void VP8IteratorInit(VP8Encoder* const enc, VP8EncIterator* const it); // restart a scan void VP8IteratorReset(VP8EncIterator* const it); // reset iterator position to row 'y' void VP8IteratorSetRow(VP8EncIterator* const it, int y); // set count down (=number of iterations to go) void VP8IteratorSetCountDown(VP8EncIterator* const it, int count_down); // return true if iteration is finished int VP8IteratorIsDone(const VP8EncIterator* const it); // Import uncompressed samples from source. // If tmp_32 is not NULL, import boundary samples too. // tmp_32 is a 32-bytes scratch buffer that must be aligned in memory. void VP8IteratorImport(VP8EncIterator* const it, uint8_t* const tmp_32); // export decimated samples void VP8IteratorExport(const VP8EncIterator* const it); // go to next macroblock. Returns false if not finished. int VP8IteratorNext(VP8EncIterator* const it); // save the yuv_out_ boundary values to top_/left_ arrays for next iterations. void VP8IteratorSaveBoundary(VP8EncIterator* const it); // Report progression based on macroblock rows. Return 0 for user-abort request. int VP8IteratorProgress(const VP8EncIterator* const it, int delta); // Intra4x4 iterations void VP8IteratorStartI4(VP8EncIterator* const it); // returns true if not done. int VP8IteratorRotateI4(VP8EncIterator* const it, const uint8_t* const yuv_out); // Non-zero context setup/teardown void VP8IteratorNzToBytes(VP8EncIterator* const it); void VP8IteratorBytesToNz(VP8EncIterator* const it); // Helper functions to set mode properties void VP8SetIntra16Mode(const VP8EncIterator* const it, int mode); void VP8SetIntra4Mode(const VP8EncIterator* const it, const uint8_t* modes); void VP8SetIntraUVMode(const VP8EncIterator* const it, int mode); void VP8SetSkip(const VP8EncIterator* const it, int skip); void VP8SetSegment(const VP8EncIterator* const it, int segment); //------------------------------------------------------------------------------ // Paginated token buffer VP8Tokens; // struct details in token.c VP8TBuffer; // initialize an empty buffer void VP8TBufferInit(VP8TBuffer* const b, int page_size); void VP8TBufferClear(VP8TBuffer* const b); // de-allocate pages memory #if !defined(DISABLE_TOKEN_BUFFER) // Finalizes bitstream when probabilities are known. // Deletes the allocated token memory if final_pass is true. int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw, const uint8_t* const probas, int final_pass); // record the coding of coefficients without knowing the probabilities yet int VP8RecordCoeffTokens(int ctx, const struct VP8Residual* const res, VP8TBuffer* const tokens); // Estimate the final coded size given a set of 'probas'. size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas); #endif // !DISABLE_TOKEN_BUFFER //------------------------------------------------------------------------------ // VP8Encoder struct VP8Encoder { … }; //------------------------------------------------------------------------------ // internal functions. Not public. // in tree.c extern const uint8_t VP8CoeffsProba0[NUM_TYPES][NUM_BANDS][NUM_CTX][NUM_PROBAS]; extern const uint8_t VP8CoeffsUpdateProba[NUM_TYPES][NUM_BANDS][NUM_CTX][NUM_PROBAS]; // Reset the token probabilities to their initial (default) values void VP8DefaultProbas(VP8Encoder* const enc); // Write the token probabilities void VP8WriteProbas(VP8BitWriter* const bw, const VP8EncProba* const probas); // Writes the partition #0 modes (that is: all intra modes) void VP8CodeIntraModes(VP8Encoder* const enc); // in syntax.c // Generates the final bitstream by coding the partition0 and headers, // and appending an assembly of all the pre-coded token partitions. // Return true if everything is ok. int VP8EncWrite(VP8Encoder* const enc); // Release memory allocated for bit-writing in VP8EncLoop & seq. void VP8EncFreeBitWriters(VP8Encoder* const enc); // in frame.c extern const uint8_t VP8Cat3[]; extern const uint8_t VP8Cat4[]; extern const uint8_t VP8Cat5[]; extern const uint8_t VP8Cat6[]; // Form all the four Intra16x16 predictions in the yuv_p_ cache void VP8MakeLuma16Preds(const VP8EncIterator* const it); // Form all the four Chroma8x8 predictions in the yuv_p_ cache void VP8MakeChroma8Preds(const VP8EncIterator* const it); // Form all the ten Intra4x4 predictions in the yuv_p_ cache // for the 4x4 block it->i4_ void VP8MakeIntra4Preds(const VP8EncIterator* const it); // Rate calculation int VP8GetCostLuma16(VP8EncIterator* const it, const VP8ModeScore* const rd); int VP8GetCostLuma4(VP8EncIterator* const it, const int16_t levels[16]); int VP8GetCostUV(VP8EncIterator* const it, const VP8ModeScore* const rd); // Main coding calls int VP8EncLoop(VP8Encoder* const enc); int VP8EncTokenLoop(VP8Encoder* const enc); // in webpenc.c // Assign an error code to a picture. Return false for convenience. int WebPEncodingSetError(const WebPPicture* const pic, WebPEncodingError error); int WebPReportProgress(const WebPPicture* const pic, int percent, int* const percent_store); // in analysis.c // Main analysis loop. Decides the segmentations and complexity. // Assigns a first guess for Intra16 and uvmode_ prediction modes. int VP8EncAnalyze(VP8Encoder* const enc); // in quant.c // Sets up segment's quantization values, base_quant_ and filter strengths. void VP8SetSegmentParams(VP8Encoder* const enc, float quality); // Pick best modes and fills the levels. Returns true if skipped. int VP8Decimate(VP8EncIterator* WEBP_RESTRICT const it, VP8ModeScore* WEBP_RESTRICT const rd, VP8RDLevel rd_opt); // in alpha.c void VP8EncInitAlpha(VP8Encoder* const enc); // initialize alpha compression int VP8EncStartAlpha(VP8Encoder* const enc); // start alpha coding process int VP8EncFinishAlpha(VP8Encoder* const enc); // finalize compressed data int VP8EncDeleteAlpha(VP8Encoder* const enc); // delete compressed data // autofilter void VP8InitFilter(VP8EncIterator* const it); void VP8StoreFilterStats(VP8EncIterator* const it); void VP8AdjustFilterStrength(VP8EncIterator* const it); // returns the approximate filtering strength needed to smooth a edge // step of 'delta', given a sharpness parameter 'sharpness'. int VP8FilterStrengthFromDelta(int sharpness, int delta); // misc utils for picture_*.c: // Returns true if 'picture' is non-NULL and dimensions/colorspace are within // their valid ranges. If returning false, the 'error_code' in 'picture' is // updated. int WebPValidatePicture(const WebPPicture* const picture); // Remove reference to the ARGB/YUVA buffer (doesn't free anything). void WebPPictureResetBuffers(WebPPicture* const picture); // Allocates ARGB buffer according to set width/height (previous one is // always free'd). Preserves the YUV(A) buffer. Returns false in case of error // (invalid param, out-of-memory). int WebPPictureAllocARGB(WebPPicture* const picture); // Allocates YUVA buffer according to set width/height (previous one is always // free'd). Uses picture->csp to determine whether an alpha buffer is needed. // Preserves the ARGB buffer. // Returns false in case of error (invalid param, out-of-memory). int WebPPictureAllocYUVA(WebPPicture* const picture); // Replace samples that are fully transparent by 'color' to help compressibility // (no guarantee, though). Assumes pic->use_argb is true. void WebPReplaceTransparentPixels(WebPPicture* const pic, uint32_t color); //------------------------------------------------------------------------------ #ifdef __cplusplus } // extern "C" #endif #endif // WEBP_ENC_VP8I_ENC_H_