Path: blob/master/3rdparty/libwebp/src/enc/iterator_enc.c
16354 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// VP8Iterator: block iterator10//11// Author: Skal ([email protected])1213#include <string.h>1415#include "src/enc/vp8i_enc.h"1617//------------------------------------------------------------------------------18// VP8Iterator19//------------------------------------------------------------------------------2021static void InitLeft(VP8EncIterator* const it) {22it->y_left_[-1] = it->u_left_[-1] = it->v_left_[-1] =23(it->y_ > 0) ? 129 : 127;24memset(it->y_left_, 129, 16);25memset(it->u_left_, 129, 8);26memset(it->v_left_, 129, 8);27it->left_nz_[8] = 0;28if (it->top_derr_ != NULL) {29memset(&it->left_derr_, 0, sizeof(it->left_derr_));30}31}3233static void InitTop(VP8EncIterator* const it) {34const VP8Encoder* const enc = it->enc_;35const size_t top_size = enc->mb_w_ * 16;36memset(enc->y_top_, 127, 2 * top_size);37memset(enc->nz_, 0, enc->mb_w_ * sizeof(*enc->nz_));38if (enc->top_derr_ != NULL) {39memset(enc->top_derr_, 0, enc->mb_w_ * sizeof(*enc->top_derr_));40}41}4243void VP8IteratorSetRow(VP8EncIterator* const it, int y) {44VP8Encoder* const enc = it->enc_;45it->x_ = 0;46it->y_ = y;47it->bw_ = &enc->parts_[y & (enc->num_parts_ - 1)];48it->preds_ = enc->preds_ + y * 4 * enc->preds_w_;49it->nz_ = enc->nz_;50it->mb_ = enc->mb_info_ + y * enc->mb_w_;51it->y_top_ = enc->y_top_;52it->uv_top_ = enc->uv_top_;53InitLeft(it);54}5556void VP8IteratorReset(VP8EncIterator* const it) {57VP8Encoder* const enc = it->enc_;58VP8IteratorSetRow(it, 0);59VP8IteratorSetCountDown(it, enc->mb_w_ * enc->mb_h_); // default60InitTop(it);61memset(it->bit_count_, 0, sizeof(it->bit_count_));62it->do_trellis_ = 0;63}6465void VP8IteratorSetCountDown(VP8EncIterator* const it, int count_down) {66it->count_down_ = it->count_down0_ = count_down;67}6869int VP8IteratorIsDone(const VP8EncIterator* const it) {70return (it->count_down_ <= 0);71}7273void VP8IteratorInit(VP8Encoder* const enc, VP8EncIterator* const it) {74it->enc_ = enc;75it->yuv_in_ = (uint8_t*)WEBP_ALIGN(it->yuv_mem_);76it->yuv_out_ = it->yuv_in_ + YUV_SIZE_ENC;77it->yuv_out2_ = it->yuv_out_ + YUV_SIZE_ENC;78it->yuv_p_ = it->yuv_out2_ + YUV_SIZE_ENC;79it->lf_stats_ = enc->lf_stats_;80it->percent0_ = enc->percent_;81it->y_left_ = (uint8_t*)WEBP_ALIGN(it->yuv_left_mem_ + 1);82it->u_left_ = it->y_left_ + 16 + 16;83it->v_left_ = it->u_left_ + 16;84it->top_derr_ = enc->top_derr_;85VP8IteratorReset(it);86}8788int VP8IteratorProgress(const VP8EncIterator* const it, int delta) {89VP8Encoder* const enc = it->enc_;90if (delta && enc->pic_->progress_hook != NULL) {91const int done = it->count_down0_ - it->count_down_;92const int percent = (it->count_down0_ <= 0)93? it->percent0_94: it->percent0_ + delta * done / it->count_down0_;95return WebPReportProgress(enc->pic_, percent, &enc->percent_);96}97return 1;98}99100//------------------------------------------------------------------------------101// Import the source samples into the cache. Takes care of replicating102// boundary pixels if necessary.103104static WEBP_INLINE int MinSize(int a, int b) { return (a < b) ? a : b; }105106static void ImportBlock(const uint8_t* src, int src_stride,107uint8_t* dst, int w, int h, int size) {108int i;109for (i = 0; i < h; ++i) {110memcpy(dst, src, w);111if (w < size) {112memset(dst + w, dst[w - 1], size - w);113}114dst += BPS;115src += src_stride;116}117for (i = h; i < size; ++i) {118memcpy(dst, dst - BPS, size);119dst += BPS;120}121}122123static void ImportLine(const uint8_t* src, int src_stride,124uint8_t* dst, int len, int total_len) {125int i;126for (i = 0; i < len; ++i, src += src_stride) dst[i] = *src;127for (; i < total_len; ++i) dst[i] = dst[len - 1];128}129130void VP8IteratorImport(VP8EncIterator* const it, uint8_t* tmp_32) {131const VP8Encoder* const enc = it->enc_;132const int x = it->x_, y = it->y_;133const WebPPicture* const pic = enc->pic_;134const uint8_t* const ysrc = pic->y + (y * pic->y_stride + x) * 16;135const uint8_t* const usrc = pic->u + (y * pic->uv_stride + x) * 8;136const uint8_t* const vsrc = pic->v + (y * pic->uv_stride + x) * 8;137const int w = MinSize(pic->width - x * 16, 16);138const int h = MinSize(pic->height - y * 16, 16);139const int uv_w = (w + 1) >> 1;140const int uv_h = (h + 1) >> 1;141142ImportBlock(ysrc, pic->y_stride, it->yuv_in_ + Y_OFF_ENC, w, h, 16);143ImportBlock(usrc, pic->uv_stride, it->yuv_in_ + U_OFF_ENC, uv_w, uv_h, 8);144ImportBlock(vsrc, pic->uv_stride, it->yuv_in_ + V_OFF_ENC, uv_w, uv_h, 8);145146if (tmp_32 == NULL) return;147148// Import source (uncompressed) samples into boundary.149if (x == 0) {150InitLeft(it);151} else {152if (y == 0) {153it->y_left_[-1] = it->u_left_[-1] = it->v_left_[-1] = 127;154} else {155it->y_left_[-1] = ysrc[- 1 - pic->y_stride];156it->u_left_[-1] = usrc[- 1 - pic->uv_stride];157it->v_left_[-1] = vsrc[- 1 - pic->uv_stride];158}159ImportLine(ysrc - 1, pic->y_stride, it->y_left_, h, 16);160ImportLine(usrc - 1, pic->uv_stride, it->u_left_, uv_h, 8);161ImportLine(vsrc - 1, pic->uv_stride, it->v_left_, uv_h, 8);162}163164it->y_top_ = tmp_32 + 0;165it->uv_top_ = tmp_32 + 16;166if (y == 0) {167memset(tmp_32, 127, 32 * sizeof(*tmp_32));168} else {169ImportLine(ysrc - pic->y_stride, 1, tmp_32, w, 16);170ImportLine(usrc - pic->uv_stride, 1, tmp_32 + 16, uv_w, 8);171ImportLine(vsrc - pic->uv_stride, 1, tmp_32 + 16 + 8, uv_w, 8);172}173}174175//------------------------------------------------------------------------------176// Copy back the compressed samples into user space if requested.177178static void ExportBlock(const uint8_t* src, uint8_t* dst, int dst_stride,179int w, int h) {180while (h-- > 0) {181memcpy(dst, src, w);182dst += dst_stride;183src += BPS;184}185}186187void VP8IteratorExport(const VP8EncIterator* const it) {188const VP8Encoder* const enc = it->enc_;189if (enc->config_->show_compressed) {190const int x = it->x_, y = it->y_;191const uint8_t* const ysrc = it->yuv_out_ + Y_OFF_ENC;192const uint8_t* const usrc = it->yuv_out_ + U_OFF_ENC;193const uint8_t* const vsrc = it->yuv_out_ + V_OFF_ENC;194const WebPPicture* const pic = enc->pic_;195uint8_t* const ydst = pic->y + (y * pic->y_stride + x) * 16;196uint8_t* const udst = pic->u + (y * pic->uv_stride + x) * 8;197uint8_t* const vdst = pic->v + (y * pic->uv_stride + x) * 8;198int w = (pic->width - x * 16);199int h = (pic->height - y * 16);200201if (w > 16) w = 16;202if (h > 16) h = 16;203204// Luma plane205ExportBlock(ysrc, ydst, pic->y_stride, w, h);206207{ // U/V planes208const int uv_w = (w + 1) >> 1;209const int uv_h = (h + 1) >> 1;210ExportBlock(usrc, udst, pic->uv_stride, uv_w, uv_h);211ExportBlock(vsrc, vdst, pic->uv_stride, uv_w, uv_h);212}213}214}215216//------------------------------------------------------------------------------217// Non-zero contexts setup/teardown218219// Nz bits:220// 0 1 2 3 Y221// 4 5 6 7222// 8 9 10 11223// 12 13 14 15224// 16 17 U225// 18 19226// 20 21 V227// 22 23228// 24 DC-intra16229230// Convert packed context to byte array231#define BIT(nz, n) (!!((nz) & (1 << (n))))232233void VP8IteratorNzToBytes(VP8EncIterator* const it) {234const int tnz = it->nz_[0], lnz = it->nz_[-1];235int* const top_nz = it->top_nz_;236int* const left_nz = it->left_nz_;237238// Top-Y239top_nz[0] = BIT(tnz, 12);240top_nz[1] = BIT(tnz, 13);241top_nz[2] = BIT(tnz, 14);242top_nz[3] = BIT(tnz, 15);243// Top-U244top_nz[4] = BIT(tnz, 18);245top_nz[5] = BIT(tnz, 19);246// Top-V247top_nz[6] = BIT(tnz, 22);248top_nz[7] = BIT(tnz, 23);249// DC250top_nz[8] = BIT(tnz, 24);251252// left-Y253left_nz[0] = BIT(lnz, 3);254left_nz[1] = BIT(lnz, 7);255left_nz[2] = BIT(lnz, 11);256left_nz[3] = BIT(lnz, 15);257// left-U258left_nz[4] = BIT(lnz, 17);259left_nz[5] = BIT(lnz, 19);260// left-V261left_nz[6] = BIT(lnz, 21);262left_nz[7] = BIT(lnz, 23);263// left-DC is special, iterated separately264}265266void VP8IteratorBytesToNz(VP8EncIterator* const it) {267uint32_t nz = 0;268const int* const top_nz = it->top_nz_;269const int* const left_nz = it->left_nz_;270// top271nz |= (top_nz[0] << 12) | (top_nz[1] << 13);272nz |= (top_nz[2] << 14) | (top_nz[3] << 15);273nz |= (top_nz[4] << 18) | (top_nz[5] << 19);274nz |= (top_nz[6] << 22) | (top_nz[7] << 23);275nz |= (top_nz[8] << 24); // we propagate the _top_ bit, esp. for intra4276// left277nz |= (left_nz[0] << 3) | (left_nz[1] << 7);278nz |= (left_nz[2] << 11);279nz |= (left_nz[4] << 17) | (left_nz[6] << 21);280281*it->nz_ = nz;282}283284#undef BIT285286//------------------------------------------------------------------------------287// Advance to the next position, doing the bookkeeping.288289void VP8IteratorSaveBoundary(VP8EncIterator* const it) {290VP8Encoder* const enc = it->enc_;291const int x = it->x_, y = it->y_;292const uint8_t* const ysrc = it->yuv_out_ + Y_OFF_ENC;293const uint8_t* const uvsrc = it->yuv_out_ + U_OFF_ENC;294if (x < enc->mb_w_ - 1) { // left295int i;296for (i = 0; i < 16; ++i) {297it->y_left_[i] = ysrc[15 + i * BPS];298}299for (i = 0; i < 8; ++i) {300it->u_left_[i] = uvsrc[7 + i * BPS];301it->v_left_[i] = uvsrc[15 + i * BPS];302}303// top-left (before 'top'!)304it->y_left_[-1] = it->y_top_[15];305it->u_left_[-1] = it->uv_top_[0 + 7];306it->v_left_[-1] = it->uv_top_[8 + 7];307}308if (y < enc->mb_h_ - 1) { // top309memcpy(it->y_top_, ysrc + 15 * BPS, 16);310memcpy(it->uv_top_, uvsrc + 7 * BPS, 8 + 8);311}312}313314int VP8IteratorNext(VP8EncIterator* const it) {315if (++it->x_ == it->enc_->mb_w_) {316VP8IteratorSetRow(it, ++it->y_);317} else {318it->preds_ += 4;319it->mb_ += 1;320it->nz_ += 1;321it->y_top_ += 16;322it->uv_top_ += 16;323}324return (0 < --it->count_down_);325}326327//------------------------------------------------------------------------------328// Helper function to set mode properties329330void VP8SetIntra16Mode(const VP8EncIterator* const it, int mode) {331uint8_t* preds = it->preds_;332int y;333for (y = 0; y < 4; ++y) {334memset(preds, mode, 4);335preds += it->enc_->preds_w_;336}337it->mb_->type_ = 1;338}339340void VP8SetIntra4Mode(const VP8EncIterator* const it, const uint8_t* modes) {341uint8_t* preds = it->preds_;342int y;343for (y = 4; y > 0; --y) {344memcpy(preds, modes, 4 * sizeof(*modes));345preds += it->enc_->preds_w_;346modes += 4;347}348it->mb_->type_ = 0;349}350351void VP8SetIntraUVMode(const VP8EncIterator* const it, int mode) {352it->mb_->uv_mode_ = mode;353}354355void VP8SetSkip(const VP8EncIterator* const it, int skip) {356it->mb_->skip_ = skip;357}358359void VP8SetSegment(const VP8EncIterator* const it, int segment) {360it->mb_->segment_ = segment;361}362363//------------------------------------------------------------------------------364// Intra4x4 sub-blocks iteration365//366// We store and update the boundary samples into an array of 37 pixels. They367// are updated as we iterate and reconstructs each intra4x4 blocks in turn.368// The position of the samples has the following snake pattern:369//370// 16|17 18 19 20|21 22 23 24|25 26 27 28|29 30 31 32|33 34 35 36 <- Top-right371// --+-----------+-----------+-----------+-----------+372// 15| 19| 23| 27| 31|373// 14| 18| 22| 26| 30|374// 13| 17| 21| 25| 29|375// 12|13 14 15 16|17 18 19 20|21 22 23 24|25 26 27 28|376// --+-----------+-----------+-----------+-----------+377// 11| 15| 19| 23| 27|378// 10| 14| 18| 22| 26|379// 9| 13| 17| 21| 25|380// 8| 9 10 11 12|13 14 15 16|17 18 19 20|21 22 23 24|381// --+-----------+-----------+-----------+-----------+382// 7| 11| 15| 19| 23|383// 6| 10| 14| 18| 22|384// 5| 9| 13| 17| 21|385// 4| 5 6 7 8| 9 10 11 12|13 14 15 16|17 18 19 20|386// --+-----------+-----------+-----------+-----------+387// 3| 7| 11| 15| 19|388// 2| 6| 10| 14| 18|389// 1| 5| 9| 13| 17|390// 0| 1 2 3 4| 5 6 7 8| 9 10 11 12|13 14 15 16|391// --+-----------+-----------+-----------+-----------+392393// Array to record the position of the top sample to pass to the prediction394// functions in dsp.c.395static const uint8_t VP8TopLeftI4[16] = {39617, 21, 25, 29,39713, 17, 21, 25,3989, 13, 17, 21,3995, 9, 13, 17400};401402void VP8IteratorStartI4(VP8EncIterator* const it) {403const VP8Encoder* const enc = it->enc_;404int i;405406it->i4_ = 0; // first 4x4 sub-block407it->i4_top_ = it->i4_boundary_ + VP8TopLeftI4[0];408409// Import the boundary samples410for (i = 0; i < 17; ++i) { // left411it->i4_boundary_[i] = it->y_left_[15 - i];412}413for (i = 0; i < 16; ++i) { // top414it->i4_boundary_[17 + i] = it->y_top_[i];415}416// top-right samples have a special case on the far right of the picture417if (it->x_ < enc->mb_w_ - 1) {418for (i = 16; i < 16 + 4; ++i) {419it->i4_boundary_[17 + i] = it->y_top_[i];420}421} else { // else, replicate the last valid pixel four times422for (i = 16; i < 16 + 4; ++i) {423it->i4_boundary_[17 + i] = it->i4_boundary_[17 + 15];424}425}426VP8IteratorNzToBytes(it); // import the non-zero context427}428429int VP8IteratorRotateI4(VP8EncIterator* const it,430const uint8_t* const yuv_out) {431const uint8_t* const blk = yuv_out + VP8Scan[it->i4_];432uint8_t* const top = it->i4_top_;433int i;434435// Update the cache with 7 fresh samples436for (i = 0; i <= 3; ++i) {437top[-4 + i] = blk[i + 3 * BPS]; // store future top samples438}439if ((it->i4_ & 3) != 3) { // if not on the right sub-blocks #3, #7, #11, #15440for (i = 0; i <= 2; ++i) { // store future left samples441top[i] = blk[3 + (2 - i) * BPS];442}443} else { // else replicate top-right samples, as says the specs.444for (i = 0; i <= 3; ++i) {445top[i] = top[i + 4];446}447}448// move pointers to next sub-block449++it->i4_;450if (it->i4_ == 16) { // we're done451return 0;452}453454it->i4_top_ = it->i4_boundary_ + VP8TopLeftI4[it->i4_];455return 1;456}457458//------------------------------------------------------------------------------459460461