From d26198ada3279e9d7ef5d3d2cca13b0eff5144f0 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Wed, 10 Feb 2021 00:20:26 +0100 Subject: [PATCH] avcodec/g2meet, mjpegdec: Factor out common VLC initialization code While just at it, remove the nb_codes parameter: It is redundant (the number of codes is implicitly contained in the array containing how many entries of a specific size there are) and for this reason it might even be wrong, so it is better to check what is actually used instead. Signed-off-by: Andreas Rheinhardt --- libavcodec/Makefile | 4 +-- libavcodec/g2meet.c | 38 ++++++------------------ libavcodec/mjpegdec.c | 48 +++++------------------------- libavcodec/mjpegdec.h | 2 ++ libavcodec/mjpegdec_common.c | 57 ++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 72 deletions(-) create mode 100644 libavcodec/mjpegdec_common.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index db8e4fd254..379c91af8c 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -342,7 +342,7 @@ OBJS-$(CONFIG_FMVC_DECODER) += fmvc.o OBJS-$(CONFIG_FOURXM_DECODER) += 4xm.o OBJS-$(CONFIG_FRAPS_DECODER) += fraps.o OBJS-$(CONFIG_FRWU_DECODER) += frwu.o -OBJS-$(CONFIG_G2M_DECODER) += g2meet.o elsdec.o +OBJS-$(CONFIG_G2M_DECODER) += g2meet.o elsdec.o mjpegdec_common.o OBJS-$(CONFIG_G723_1_DECODER) += g723_1dec.o g723_1.o \ acelp_vectors.o celp_filters.o celp_math.o OBJS-$(CONFIG_G723_1_ENCODER) += g723_1enc.o g723_1.o \ @@ -451,7 +451,7 @@ OBJS-$(CONFIG_METASOUND_DECODER) += metasound.o metasound_data.o \ twinvq.o OBJS-$(CONFIG_MICRODVD_DECODER) += microdvddec.o ass.o OBJS-$(CONFIG_MIMIC_DECODER) += mimic.o -OBJS-$(CONFIG_MJPEG_DECODER) += mjpegdec.o +OBJS-$(CONFIG_MJPEG_DECODER) += mjpegdec.o mjpegdec_common.o OBJS-$(CONFIG_MJPEG_QSV_DECODER) += qsvdec.o OBJS-$(CONFIG_MJPEG_ENCODER) += mjpegenc.o mjpegenc_common.o \ mjpegenc_huffman.o diff --git a/libavcodec/g2meet.c b/libavcodec/g2meet.c index 70dc8c72a3..68b1b9dc74 100644 --- a/libavcodec/g2meet.c +++ b/libavcodec/g2meet.c @@ -41,6 +41,7 @@ #include "internal.h" #include "jpegtables.h" #include "mjpeg.h" +#include "mjpegdec.h" #define EPIC_PIX_STACK_SIZE 1024 #define EPIC_PIX_STACK_MAX (EPIC_PIX_STACK_SIZE - 1) @@ -159,45 +160,24 @@ typedef struct G2MContext { int cursor_hot_x, cursor_hot_y; } G2MContext; -static av_cold int build_vlc(VLC *vlc, const uint8_t *bits_table, - const uint8_t *val_table, int nb_codes, - int is_ac) -{ - uint8_t huff_size[256] = { 0 }; - uint16_t huff_code[256]; - uint16_t huff_sym[256]; - int i; - - ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); - - for (i = 0; i < 256; i++) - huff_sym[i] = i + 16 * is_ac; - - if (is_ac) - huff_sym[0] = 16 * 256; - - return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1, - huff_code, 2, 2, huff_sym, 2, 2, 0); -} - static av_cold int jpg_init(AVCodecContext *avctx, JPGContext *c) { int ret; - ret = build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance, - avpriv_mjpeg_val_dc, 12, 0); + ret = ff_mjpeg_build_vlc(&c->dc_vlc[0], avpriv_mjpeg_bits_dc_luminance, + avpriv_mjpeg_val_dc, 0, avctx); if (ret) return ret; - ret = build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance, - avpriv_mjpeg_val_dc, 12, 0); + ret = ff_mjpeg_build_vlc(&c->dc_vlc[1], avpriv_mjpeg_bits_dc_chrominance, + avpriv_mjpeg_val_dc, 0, avctx); if (ret) return ret; - ret = build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance, - avpriv_mjpeg_val_ac_luminance, 251, 1); + ret = ff_mjpeg_build_vlc(&c->ac_vlc[0], avpriv_mjpeg_bits_ac_luminance, + avpriv_mjpeg_val_ac_luminance, 1, avctx); if (ret) return ret; - ret = build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance, - avpriv_mjpeg_val_ac_chrominance, 251, 1); + ret = ff_mjpeg_build_vlc(&c->ac_vlc[1], avpriv_mjpeg_bits_ac_chrominance, + avpriv_mjpeg_val_ac_chrominance, 1, avctx); if (ret) return ret; diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c index 66dc79529c..256393a68f 100644 --- a/libavcodec/mjpegdec.c +++ b/libavcodec/mjpegdec.c @@ -51,40 +51,6 @@ #include "bytestream.h" -static void build_huffman_codes(uint8_t *huff_size, const uint8_t *bits_table) -{ - for (int i = 1, k = 0; i <= 16; i++) { - int nb = bits_table[i]; - for (int j = 0; j < nb;j++) { - huff_size[k] = i; - k++; - } - } -} - -static int build_vlc(VLC *vlc, const uint8_t *bits_table, - const uint8_t *val_table, int nb_codes, - int is_ac, void *logctx) -{ - uint8_t huff_size[256]; - uint16_t huff_sym[256]; - int i; - - av_assert0(nb_codes <= 256); - - build_huffman_codes(huff_size, bits_table); - - for (i = 0; i < nb_codes; i++) { - huff_sym[i] = val_table[i] + 16 * is_ac; - - if (is_ac && !val_table[i]) - huff_sym[i] = 16 * 256; - } - - return ff_init_vlc_from_lengths(vlc, 9, nb_codes, huff_size, 1, - huff_sym, 2, 2, 0, 0, logctx); -} - static int init_default_huffman_tables(MJpegDecodeContext *s) { static const struct { @@ -110,9 +76,9 @@ static int init_default_huffman_tables(MJpegDecodeContext *s) int i, ret; for (i = 0; i < FF_ARRAY_ELEMS(ht); i++) { - ret = build_vlc(&s->vlcs[ht[i].class][ht[i].index], - ht[i].bits, ht[i].values, ht[i].length, - ht[i].class == 1, s->avctx); + ret = ff_mjpeg_build_vlc(&s->vlcs[ht[i].class][ht[i].index], + ht[i].bits, ht[i].values, + ht[i].class == 1, s->avctx); if (ret < 0) return ret; @@ -307,14 +273,14 @@ int ff_mjpeg_decode_dht(MJpegDecodeContext *s) ff_free_vlc(&s->vlcs[class][index]); av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n", class, index, n); - if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table, - n, class > 0, s->avctx)) < 0) + if ((ret = ff_mjpeg_build_vlc(&s->vlcs[class][index], bits_table, + val_table, class > 0, s->avctx)) < 0) return ret; if (class > 0) { ff_free_vlc(&s->vlcs[2][index]); - if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table, - n, 0, s->avctx)) < 0) + if ((ret = ff_mjpeg_build_vlc(&s->vlcs[2][index], bits_table, + val_table, 0, s->avctx)) < 0) return ret; } diff --git a/libavcodec/mjpegdec.h b/libavcodec/mjpegdec.h index a824b5029c..732aeab994 100644 --- a/libavcodec/mjpegdec.h +++ b/libavcodec/mjpegdec.h @@ -161,6 +161,8 @@ typedef struct MJpegDecodeContext { void *hwaccel_picture_private; } MJpegDecodeContext; +int ff_mjpeg_build_vlc(VLC *vlc, const uint8_t *bits_table, + const uint8_t *val_table, int is_ac, void *logctx); int ff_mjpeg_decode_init(AVCodecContext *avctx); int ff_mjpeg_decode_end(AVCodecContext *avctx); int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame); diff --git a/libavcodec/mjpegdec_common.c b/libavcodec/mjpegdec_common.c new file mode 100644 index 0000000000..701ddfec06 --- /dev/null +++ b/libavcodec/mjpegdec_common.c @@ -0,0 +1,57 @@ +/* + * MJPEG decoder VLC code + * Copyright (c) 2000, 2001 Fabrice Bellard + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include "libavutil/avassert.h" +#include "mjpegdec.h" +#include "vlc.h" + +static int build_huffman_codes(uint8_t *huff_size, const uint8_t *bits_table) +{ + int nb_codes = 0; + for (int i = 1, j = 0; i <= 16; i++) { + nb_codes += bits_table[i]; + av_assert1(nb_codes <= 256); + for (; j < nb_codes; j++) + huff_size[j] = i; + } + return nb_codes; +} + +int ff_mjpeg_build_vlc(VLC *vlc, const uint8_t *bits_table, + const uint8_t *val_table, int is_ac, void *logctx) +{ + uint8_t huff_size[256]; + uint16_t huff_sym[256]; + int nb_codes = build_huffman_codes(huff_size, bits_table); + + for (int i = 0; i < nb_codes; i++) { + huff_sym[i] = val_table[i] + 16 * is_ac; + + if (is_ac && !val_table[i]) + huff_sym[i] = 16 * 256; + } + + return ff_init_vlc_from_lengths(vlc, 9, nb_codes, huff_size, 1, + huff_sym, 2, 2, 0, 0, logctx); +}