Path: blob/master/thirdparty/libwebp/src/dec/quant_dec.c
20850 views
// Copyright 2010 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// Quantizer initialization10//11// Author: Skal ([email protected])1213#include "src/dec/common_dec.h"14#include "src/dec/vp8_dec.h"15#include "src/dec/vp8i_dec.h"16#include "src/utils/bit_reader_utils.h"17#include "src/webp/types.h"1819static WEBP_INLINE int clip(int v, int M) {20return v < 0 ? 0 : v > M ? M : v;21}2223// Paragraph 14.124static const uint8_t kDcTable[128] = {254, 5, 6, 7, 8, 9, 10, 10,2611, 12, 13, 14, 15, 16, 17, 17,2718, 19, 20, 20, 21, 21, 22, 22,2823, 23, 24, 25, 25, 26, 27, 28,2929, 30, 31, 32, 33, 34, 35, 36,3037, 37, 38, 39, 40, 41, 42, 43,3144, 45, 46, 46, 47, 48, 49, 50,3251, 52, 53, 54, 55, 56, 57, 58,3359, 60, 61, 62, 63, 64, 65, 66,3467, 68, 69, 70, 71, 72, 73, 74,3575, 76, 76, 77, 78, 79, 80, 81,3682, 83, 84, 85, 86, 87, 88, 89,3791, 93, 95, 96, 98, 100, 101, 102,38104, 106, 108, 110, 112, 114, 116, 118,39122, 124, 126, 128, 130, 132, 134, 136,40138, 140, 143, 145, 148, 151, 154, 15741};4243static const uint16_t kAcTable[128] = {444, 5, 6, 7, 8, 9, 10, 11,4512, 13, 14, 15, 16, 17, 18, 19,4620, 21, 22, 23, 24, 25, 26, 27,4728, 29, 30, 31, 32, 33, 34, 35,4836, 37, 38, 39, 40, 41, 42, 43,4944, 45, 46, 47, 48, 49, 50, 51,5052, 53, 54, 55, 56, 57, 58, 60,5162, 64, 66, 68, 70, 72, 74, 76,5278, 80, 82, 84, 86, 88, 90, 92,5394, 96, 98, 100, 102, 104, 106, 108,54110, 112, 114, 116, 119, 122, 125, 128,55131, 134, 137, 140, 143, 146, 149, 152,56155, 158, 161, 164, 167, 170, 173, 177,57181, 185, 189, 193, 197, 201, 205, 209,58213, 217, 221, 225, 229, 234, 239, 245,59249, 254, 259, 264, 269, 274, 279, 28460};6162//------------------------------------------------------------------------------63// Paragraph 9.66465void VP8ParseQuant(VP8Decoder* const dec) {66VP8BitReader* const br = &dec->br;67const int base_q0 = VP8GetValue(br, 7, "global-header");68const int dqy1_dc = VP8Get(br, "global-header") ?69VP8GetSignedValue(br, 4, "global-header") : 0;70const int dqy2_dc = VP8Get(br, "global-header") ?71VP8GetSignedValue(br, 4, "global-header") : 0;72const int dqy2_ac = VP8Get(br, "global-header") ?73VP8GetSignedValue(br, 4, "global-header") : 0;74const int dquv_dc = VP8Get(br, "global-header") ?75VP8GetSignedValue(br, 4, "global-header") : 0;76const int dquv_ac = VP8Get(br, "global-header") ?77VP8GetSignedValue(br, 4, "global-header") : 0;7879const VP8SegmentHeader* const hdr = &dec->segment_hdr;80int i;8182for (i = 0; i < NUM_MB_SEGMENTS; ++i) {83int q;84if (hdr->use_segment) {85q = hdr->quantizer[i];86if (!hdr->absolute_delta) {87q += base_q0;88}89} else {90if (i > 0) {91dec->dqm[i] = dec->dqm[0];92continue;93} else {94q = base_q0;95}96}97{98VP8QuantMatrix* const m = &dec->dqm[i];99m->y1_mat[0] = kDcTable[clip(q + dqy1_dc, 127)];100m->y1_mat[1] = kAcTable[clip(q + 0, 127)];101102m->y2_mat[0] = kDcTable[clip(q + dqy2_dc, 127)] * 2;103// For all x in [0..284], x*155/100 is bitwise equal to (x*101581) >> 16.104// The smallest precision for that is '(x*6349) >> 12' but 16 is a good105// word size.106m->y2_mat[1] = (kAcTable[clip(q + dqy2_ac, 127)] * 101581) >> 16;107if (m->y2_mat[1] < 8) m->y2_mat[1] = 8;108109m->uv_mat[0] = kDcTable[clip(q + dquv_dc, 117)];110m->uv_mat[1] = kAcTable[clip(q + dquv_ac, 127)];111112m->uv_quant = q + dquv_ac; // for dithering strength evaluation113}114}115}116117//------------------------------------------------------------------------------118119120