From 3faa303a47e0c3b59a53988e0f76018930c6cb1a Mon Sep 17 00:00:00 2001 From: Christophe GISQUET Date: Sun, 1 Jan 2012 18:33:22 +0100 Subject: [PATCH] rv34: DC-only inverse transform When decoding coefficients, detect whether the block is DC-only, and take advantage of this knowledge to perform DC-only inverse transform. This is achieved by: - first, changing the 108x4 element modulo_three_table into a 108 element table (kind of base4), and accessing each value using mask and shifts. - then, checking low bits for 0 (as they represent the presence of higher frequency coefficients) Also provide x86 SIMD code for the DC-only inverse transform. Signed-off-by: Kostya Shishkov --- libavcodec/rv34.c | 95 ++++++++++++++++++++++++++----------------- libavcodec/rv34data.h | 44 ++++++++------------ libavcodec/rv34dsp.c | 24 +++++++++++ libavcodec/rv34dsp.h | 2 + libavcodec/x86/Makefile | 6 ++- libavcodec/x86/rv34dsp.asm | 55 +++++++++++++++++++++++++ libavcodec/x86/rv34dsp_init.c | 40 ++++++++++++++++++ 7 files changed, 200 insertions(+), 66 deletions(-) create mode 100644 libavcodec/x86/rv34dsp.asm create mode 100644 libavcodec/x86/rv34dsp_init.c diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c index ec826fd074..6cfefa0e1b 100644 --- a/libavcodec/rv34.c +++ b/libavcodec/rv34.c @@ -204,7 +204,7 @@ static int rv34_decode_cbp(GetBitContext *gb, RV34VLC *vlc, int table) } for(i = 0; i < 4; i++){ - t = modulo_three_table[code][i]; + t = (modulo_three_table[code] >> (6 - 2*i)) & 3; if(t == 1) cbp |= cbp_masks[get_bits1(gb)] << i; if(t == 2) @@ -238,41 +238,42 @@ static inline void decode_coeff(DCTELEM *dst, int coef, int esc, GetBitContext * */ static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc, int q) { - int coeffs[4]; + int flags = modulo_three_table[code]; - coeffs[0] = modulo_three_table[code][0]; - coeffs[1] = modulo_three_table[code][1]; - coeffs[2] = modulo_three_table[code][2]; - coeffs[3] = modulo_three_table[code][3]; - decode_coeff(dst , coeffs[0], 3, gb, vlc, q); + decode_coeff( dst+0, (flags >> 6) , 3, gb, vlc, q); if(is_block2){ - decode_coeff(dst+8, coeffs[1], 2, gb, vlc, q); - decode_coeff(dst+1, coeffs[2], 2, gb, vlc, q); + decode_coeff(dst+8, (flags >> 4) & 3, 2, gb, vlc, q); + decode_coeff(dst+1, (flags >> 2) & 3, 2, gb, vlc, q); }else{ - decode_coeff(dst+1, coeffs[1], 2, gb, vlc, q); - decode_coeff(dst+8, coeffs[2], 2, gb, vlc, q); + decode_coeff(dst+1, (flags >> 4) & 3, 2, gb, vlc, q); + decode_coeff(dst+8, (flags >> 2) & 3, 2, gb, vlc, q); } - decode_coeff(dst+9, coeffs[3], 2, gb, vlc, q); + decode_coeff( dst+9, (flags >> 0) & 3, 2, gb, vlc, q); +} + +/** + * Decode a single coefficient. + */ +static inline void decode_subblock1(DCTELEM *dst, int code, GetBitContext *gb, VLC *vlc, int q) +{ + int coeff = modulo_three_table[code] >> 6; + decode_coeff(dst, coeff, 3, gb, vlc, q); } static inline void decode_subblock3(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc, int q_dc, int q_ac1, int q_ac2) { - int coeffs[4]; + int flags = modulo_three_table[code]; - coeffs[0] = modulo_three_table[code][0]; - coeffs[1] = modulo_three_table[code][1]; - coeffs[2] = modulo_three_table[code][2]; - coeffs[3] = modulo_three_table[code][3]; - decode_coeff(dst , coeffs[0], 3, gb, vlc, q_dc); + decode_coeff( dst+0, (flags >> 6) , 3, gb, vlc, q_dc); if(is_block2){ - decode_coeff(dst+8, coeffs[1], 2, gb, vlc, q_ac1); - decode_coeff(dst+1, coeffs[2], 2, gb, vlc, q_ac1); + decode_coeff(dst+8, (flags >> 4) & 3, 2, gb, vlc, q_ac1); + decode_coeff(dst+1, (flags >> 2) & 3, 2, gb, vlc, q_ac1); }else{ - decode_coeff(dst+1, coeffs[1], 2, gb, vlc, q_ac1); - decode_coeff(dst+8, coeffs[2], 2, gb, vlc, q_ac1); + decode_coeff(dst+1, (flags >> 4) & 3, 2, gb, vlc, q_ac1); + decode_coeff(dst+8, (flags >> 2) & 3, 2, gb, vlc, q_ac1); } - decode_coeff(dst+9, coeffs[3], 2, gb, vlc, q_ac2); + decode_coeff( dst+9, (flags >> 0) & 3, 2, gb, vlc, q_ac2); } /** @@ -286,16 +287,24 @@ static inline void decode_subblock3(DCTELEM *dst, int code, const int is_block2, * o--o */ -static inline void rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc, int q_dc, int q_ac1, int q_ac2) +static inline int rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc, int q_dc, int q_ac1, int q_ac2) { - int code, pattern; + int code, pattern, has_ac = 1; code = get_vlc2(gb, rvlc->first_pattern[fc].table, 9, 2); pattern = code & 0x7; code >>= 3; - decode_subblock3(dst, code, 0, gb, &rvlc->coefficient, q_dc, q_ac1, q_ac2); + + if (modulo_three_table[code] & 0x3F) { + decode_subblock3(dst, code, 0, gb, &rvlc->coefficient, q_dc, q_ac1, q_ac2); + } else { + decode_subblock1(dst, code, gb, &rvlc->coefficient, q_dc); + if (!pattern) + return 0; + has_ac = 0; + } if(pattern & 4){ code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2); @@ -309,7 +318,7 @@ static inline void rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *r code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2); decode_subblock(dst + 8*2+2, code, 0, gb, &rvlc->coefficient, q_ac2); } - + return has_ac || pattern; } /** @@ -1118,7 +1127,7 @@ static int rv34_decode_macroblock(RV34DecContext *r, int8_t *intra_types) MpegEncContext *s = &r->s; GetBitContext *gb = &s->gb; int cbp, cbp2; - int q_dc, q_ac; + int q_dc, q_ac, has_ac; int i, blknum, blkoff; LOCAL_ALIGNED_16(DCTELEM, block16, [64]); int luma_dc_quant; @@ -1157,33 +1166,45 @@ static int rv34_decode_macroblock(RV34DecContext *r, int8_t *intra_types) if(r->is16){ q_dc = rv34_qscale_tab[luma_dc_quant]; q_ac = rv34_qscale_tab[s->qscale]; - memset(block16, 0, 64 * sizeof(*block16)); - rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac); - r->rdsp.rv34_inv_transform_tab[1](block16); + s->dsp.clear_block(block16); + if (rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac)) + r->rdsp.rv34_inv_transform_tab[1](block16); + else + r->rdsp.rv34_inv_transform_dc_tab[1](block16); } q_ac = rv34_qscale_tab[s->qscale]; for(i = 0; i < 16; i++, cbp >>= 1){ + DCTELEM *ptr; if(!r->is16 && !(cbp & 1)) continue; blknum = ((i & 2) >> 1) + ((i & 8) >> 2); blkoff = ((i & 1) << 2) + ((i & 4) << 3); + ptr = s->block[blknum] + blkoff; if(cbp & 1) - rv34_decode_block(s->block[blknum] + blkoff, gb, - r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac); + has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac); + else + has_ac = 0; if(r->is16) //FIXME: optimize - s->block[blknum][blkoff] = block16[(i & 3) | ((i & 0xC) << 1)]; - r->rdsp.rv34_inv_transform_tab[0](s->block[blknum] + blkoff); + ptr[0] = block16[(i & 3) | ((i & 0xC) << 1)]; + if(has_ac) + r->rdsp.rv34_inv_transform_tab[0](ptr); + else + r->rdsp.rv34_inv_transform_dc_tab[0](ptr); } if(r->block_type == RV34_MB_P_MIX16x16) r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1); q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]]; q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]]; for(; i < 24; i++, cbp >>= 1){ + DCTELEM *ptr; if(!(cbp & 1)) continue; blknum = ((i & 4) >> 2) + 4; blkoff = ((i & 1) << 2) + ((i & 2) << 4); - rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->chroma_vlc, 1, q_dc, q_ac, q_ac); - r->rdsp.rv34_inv_transform_tab[0](s->block[blknum] + blkoff); + ptr = s->block[blknum] + blkoff; + if (rv34_decode_block(ptr, gb, r->cur_vlcs, r->chroma_vlc, 1, q_dc, q_ac, q_ac)) + r->rdsp.rv34_inv_transform_tab[0](ptr); + else + r->rdsp.rv34_inv_transform_dc_tab[0](ptr); } if (IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])) rv34_output_macroblock(r, intra_types, cbp2, r->is16); diff --git a/libavcodec/rv34data.h b/libavcodec/rv34data.h index fa41a882a6..41c5b20ad7 100644 --- a/libavcodec/rv34data.h +++ b/libavcodec/rv34data.h @@ -50,34 +50,22 @@ static const uint8_t rv34_cbp_code[16] = { * A lot of four-tuples in RV40 are represented as c0*27+c1*9+c2*3+c3. * This table allows conversion from a value back to a vector. */ -static const uint8_t modulo_three_table[108][4] = { - { 0, 0, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 2 }, { 0, 0, 1, 0 }, - { 0, 0, 1, 1 }, { 0, 0, 1, 2 }, { 0, 0, 2, 0 }, { 0, 0, 2, 1 }, - { 0, 0, 2, 2 }, { 0, 1, 0, 0 }, { 0, 1, 0, 1 }, { 0, 1, 0, 2 }, - { 0, 1, 1, 0 }, { 0, 1, 1, 1 }, { 0, 1, 1, 2 }, { 0, 1, 2, 0 }, - { 0, 1, 2, 1 }, { 0, 1, 2, 2 }, { 0, 2, 0, 0 }, { 0, 2, 0, 1 }, - { 0, 2, 0, 2 }, { 0, 2, 1, 0 }, { 0, 2, 1, 1 }, { 0, 2, 1, 2 }, - { 0, 2, 2, 0 }, { 0, 2, 2, 1 }, { 0, 2, 2, 2 }, { 1, 0, 0, 0 }, - { 1, 0, 0, 1 }, { 1, 0, 0, 2 }, { 1, 0, 1, 0 }, { 1, 0, 1, 1 }, - { 1, 0, 1, 2 }, { 1, 0, 2, 0 }, { 1, 0, 2, 1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, { 3, 2, 1, 0 }, { 3, 2, 1, 1 }, - { 3, 2, 1, 2 }, { 3, 2, 2, 0 }, { 3, 2, 2, 1 }, { 3, 2, 2, 2 }, +static const uint8_t modulo_three_table[108] = { + 0x00, 0x01, 0x02, 0x04, 0x05, 0x06, 0x08, 0x09, 0x0A, + 0x10, 0x11, 0x12, 0x14, 0x15, 0x16, 0x18, 0x19, 0x1A, + 0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x28, 0x29, 0x2A, + + 0x40, 0x41, 0x42, 0x44, 0x45, 0x46, 0x48, 0x49, 0x4A, + 0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x58, 0x59, 0x5A, + 0x60, 0x61, 0x62, 0x64, 0x65, 0x66, 0x68, 0x69, 0x6A, + + 0x80, 0x81, 0x82, 0x84, 0x85, 0x86, 0x88, 0x89, 0x8A, + 0x90, 0x91, 0x92, 0x94, 0x95, 0x96, 0x98, 0x99, 0x9A, + 0xA0, 0xA1, 0xA2, 0xA4, 0xA5, 0xA6, 0xA8, 0xA9, 0xAA, + + 0xC0, 0xC1, 0xC2, 0xC4, 0xC5, 0xC6, 0xC8, 0xC9, 0xCA, + 0xD0, 0xD1, 0xD2, 0xD4, 0xD5, 0xD6, 0xD8, 0xD9, 0xDA, + 0xE0, 0xE1, 0xE2, 0xE4, 0xE5, 0xE6, 0xE8, 0xE9, 0xEA, }; /** diff --git a/libavcodec/rv34dsp.c b/libavcodec/rv34dsp.c index 1f4cea8544..1767be4173 100644 --- a/libavcodec/rv34dsp.c +++ b/libavcodec/rv34dsp.c @@ -97,13 +97,37 @@ static void rv34_inv_transform_noround_c(DCTELEM *block){ } } +static void rv34_inv_transform_dc_c(DCTELEM *block) +{ + DCTELEM dc = (13 * 13 * block[0] + 0x200) >> 10; + int i, j; + + for (i = 0; i < 4; i++, block += 8) + for (j = 0; j < 4; j++) + block[j] = dc; +} + +static void rv34_inv_transform_dc_noround_c(DCTELEM *block) +{ + DCTELEM dc = (13 * 13 * 3 * block[0]) >> 11; + int i, j; + + for (i = 0; i < 4; i++, block += 8) + for (j = 0; j < 4; j++) + block[j] = dc; +} + /** @} */ // transform av_cold void ff_rv34dsp_init(RV34DSPContext *c, DSPContext* dsp) { c->rv34_inv_transform_tab[0] = rv34_inv_transform_c; c->rv34_inv_transform_tab[1] = rv34_inv_transform_noround_c; + c->rv34_inv_transform_dc_tab[0] = rv34_inv_transform_dc_c; + c->rv34_inv_transform_dc_tab[1] = rv34_inv_transform_dc_noround_c; if (HAVE_NEON) ff_rv34dsp_init_neon(c, dsp); + if (HAVE_MMX) + ff_rv34dsp_init_x86(c, dsp); } diff --git a/libavcodec/rv34dsp.h b/libavcodec/rv34dsp.h index f2bc20e911..6f53a09928 100644 --- a/libavcodec/rv34dsp.h +++ b/libavcodec/rv34dsp.h @@ -56,6 +56,7 @@ typedef struct RV34DSPContext { h264_chroma_mc_func avg_chroma_pixels_tab[3]; rv40_weight_func rv40_weight_pixels_tab[2]; rv34_inv_transform_func rv34_inv_transform_tab[2]; + void (*rv34_inv_transform_dc_tab[2])(DCTELEM *block); rv40_weak_loop_filter_func rv40_weak_loop_filter[2]; rv40_strong_loop_filter_func rv40_strong_loop_filter[2]; rv40_loop_filter_strength_func rv40_loop_filter_strength[2]; @@ -66,6 +67,7 @@ void ff_rv34dsp_init(RV34DSPContext *c, DSPContext* dsp); void ff_rv40dsp_init(RV34DSPContext *c, DSPContext* dsp); void ff_rv34dsp_init_neon(RV34DSPContext *c, DSPContext *dsp); +void ff_rv34dsp_init_x86(RV34DSPContext *c, DSPContext *dsp); void ff_rv40dsp_init_x86(RV34DSPContext *c, DSPContext *dsp); void ff_rv40dsp_init_neon(RV34DSPContext *c, DSPContext *dsp); diff --git a/libavcodec/x86/Makefile b/libavcodec/x86/Makefile index 2abe4fbe72..1e88e4a847 100644 --- a/libavcodec/x86/Makefile +++ b/libavcodec/x86/Makefile @@ -24,7 +24,11 @@ YASM-OBJS-$(CONFIG_H264PRED) += x86/h264_intrapred.o \ x86/h264_intrapred_10bit.o MMX-OBJS-$(CONFIG_H264PRED) += x86/h264_intrapred_init.o -MMX-OBJS-$(CONFIG_RV40_DECODER) += x86/rv40dsp.o \ +MMX-OBJS-$(CONFIG_RV30_DECODER) += x86/rv34dsp_init.o +YASM-OBJS-$(CONFIG_RV30_DECODER) += x86/rv34dsp.o +MMX-OBJS-$(CONFIG_RV40_DECODER) += x86/rv34dsp_init.o \ + x86/rv40dsp.o +YASM-OBJS-$(CONFIG_RV40_DECODER) += x86/rv34dsp.o YASM-OBJS-$(CONFIG_VC1_DECODER) += x86/vc1dsp_yasm.o diff --git a/libavcodec/x86/rv34dsp.asm b/libavcodec/x86/rv34dsp.asm new file mode 100644 index 0000000000..58f1af0495 --- /dev/null +++ b/libavcodec/x86/rv34dsp.asm @@ -0,0 +1,55 @@ +;****************************************************************************** +;* MMX/SSE2-optimized functions for the RV30 and RV40 decoders +;* Copyright (C) 2012 Christophe Gisquet +;* +;* This file is part of Libav. +;* +;* Libav is free software; you can redistribute it and/or +;* modify it under the terms of the GNU Lesser General Public +;* License as published by the Free Software Foundation; either +;* version 2.1 of the License, or (at your option) any later version. +;* +;* Libav is distributed in the hope that it will be useful, +;* but WITHOUT ANY WARRANTY; without even the implied warranty of +;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;* Lesser General Public License for more details. +;* +;* You should have received a copy of the GNU Lesser General Public +;* License along with Libav; if not, write to the Free Software +;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +;****************************************************************************** + +%include "x86inc.asm" +%include "x86util.asm" + +SECTION .text + +%macro IDCT_DC_NOROUND 1 + imul %1, 13*13*3 + sar %1, 11 +%endmacro + +%macro IDCT_DC_ROUND 1 + imul %1, 13*13 + add %1, 0x200 + sar %1, 10 +%endmacro + +%macro rv34_idct_dequant4x4_dc 1 +cglobal rv34_idct_dequant4x4_%1_mmx2, 1, 2, 0 + movsx r1, word [r0] + IDCT_DC r1 + movd mm0, r1 + pshufw mm0, mm0, 0 + movq [r0+ 0], mm0 + movq [r0+16], mm0 + movq [r0+32], mm0 + movq [r0+48], mm0 + REP_RET +%endmacro + +INIT_MMX +%define IDCT_DC IDCT_DC_ROUND +rv34_idct_dequant4x4_dc dc +%define IDCT_DC IDCT_DC_NOROUND +rv34_idct_dequant4x4_dc dc_noround diff --git a/libavcodec/x86/rv34dsp_init.c b/libavcodec/x86/rv34dsp_init.c new file mode 100644 index 0000000000..4317e9b23b --- /dev/null +++ b/libavcodec/x86/rv34dsp_init.c @@ -0,0 +1,40 @@ +/* + * RV30/40 MMX/SSE2 optimizations + * Copyright (C) 2012 Christophe Gisquet + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/cpu.h" +#include "libavutil/x86_cpu.h" +#include "libavcodec/dsputil.h" +#include "libavcodec/rv34dsp.h" + +void ff_rv34_idct_dequant4x4_dc_mmx2(DCTELEM *block); +void ff_rv34_idct_dequant4x4_dc_noround_mmx2(DCTELEM *block); + +av_cold void ff_rv34dsp_init_x86(RV34DSPContext* c, DSPContext *dsp) +{ +#if HAVE_YASM + int mm_flags = av_get_cpu_flags(); + + if (mm_flags & AV_CPU_FLAG_MMX2) { + c->rv34_inv_transform_dc_tab[0] = ff_rv34_idct_dequant4x4_dc_mmx2; + c->rv34_inv_transform_dc_tab[1] = ff_rv34_idct_dequant4x4_dc_noround_mmx2; + } +#endif +} -- 2.11.0