From patchwork Mon Aug 20 21:13:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 10041 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:12c4:0:0:0:0:0 with SMTP id 65-v6csp2968308jap; Mon, 20 Aug 2018 14:14:01 -0700 (PDT) X-Google-Smtp-Source: AA+uWPz8wqT8cq47TQcpa614NjKRB25IyXYayaLve5RMKGVsunxFJXDFiFfNd0KGuVm3NhwCMeWW X-Received: by 2002:a1c:b45:: with SMTP id 66-v6mr26433328wml.45.1534799641195; Mon, 20 Aug 2018 14:14:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534799641; cv=none; d=google.com; s=arc-20160816; b=t9fNJJkHTBOWw4QUfUqcYKag2yvqkp+FRBvy9RjSn4HL22o2TpePHAL9kBE4/mDIj5 DBMN/WY3e9dxrCLlgf6PSe+M7Pv/SgJ1FOOrzLTxKGbyYLsAzkNQeBcLAQVzCTumzXPk AMGfkpy2jSTuGEZ6pufVofV+ti/QLhlIgfSwqxOUSkt4eAe6GOc25DG7saJQX6iM4BOZ YWX4iV6+Ra0GYuUGq/QWxAx3F73dyrn2YxXln5pqj1DVMnaGHTL7Ib+QHX6b5XoTTQeZ umQmWjVUrnE2i91tBE0KD7H27YfCNu7e+SDJktA67sU5kweX93AHd8pOHne94gtKzXcz MjpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=36kfhOD9sc53ya81VYA3ox2CAV0JAM5qyPph25A1SIM=; b=Vwu/oc+5Y9/RslSSrWzVJThpC6gC49WxIve+hSQlnkAJxrTljeiXJAlowMz58v9uqC hgaYNXycHbRbfvpMHLEFKinsMjM4uhpvCMpKltMfccmxhFzRCb7pllX35C4DRG8ngw/6 uWPsyv8V6qb2j9T8t24h3XQxtUr+1jIps/DuDwkKYFVV2lce1Ds2k1DkQmFV58SiQdgh Q8Lsx9pUq3IoMuyao+hEPUaR1AnS1HoH9LCr1/qdUREjuUH4VFj3iZXlpHdtTSDkYtKf nHEvpMpqyUo7fZPCHzTIzZAdAJLDqYNzksl1mycSVPS/Nx4LyXm44HRX9ZqZJsEu/AYx he2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=IPguFidT; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s14-v6si7375934wrt.419.2018.08.20.14.14.00; Mon, 20 Aug 2018 14:14:01 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=IPguFidT; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 82814689BDC; Tue, 21 Aug 2018 00:13:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 11117689253 for ; Tue, 21 Aug 2018 00:13:25 +0300 (EEST) Received: by mail-wr1-f45.google.com with SMTP id n2-v6so3622634wrw.7 for ; Mon, 20 Aug 2018 14:13:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=kyX4AHmxbyPLa4bSSTRuT7lhMdbSyuHGKrgIt0lmCD0=; b=IPguFidTqgdB3GlAXsIk1L1nu1GGmKYeG1MCZiYsghLwlNSEZLiK8dDzE+lPrcb4VT GTqq3rnm5wRa7mK1gxu/5pohtNQdqYe5TaOI18AuSI7hrsOzUGFVsxLlGFarKhbluqWZ e7VP6PAizbI1G+xXBb+lP0uRzTSU1yHeProFqnykcQMoBr/ngUJVGM84Z1C/UB+w1Fnd kogiFZIbylIkY/igUm0FmVshRZDKREWYvCoSnjNzy5MoVDLEykAhHNvutjTDRBwh6RUy e6Erc3hjhsRma1bKi3WGrc8wz1IhqX5LCTJT4Fhfu3uHRgP0y3ioPeG9o+TaesfLaodm 2rjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=kyX4AHmxbyPLa4bSSTRuT7lhMdbSyuHGKrgIt0lmCD0=; b=NeKHrhzu/6rdaq10X+yPUsEsgNf1qXW1ehHioB+u6e4Zrv4lVFsV5PnraoCVrTQ3hb MyCdvAIb7xKndFEhYKscXRQ4vZU6HUu5899I3pgUqO4amD1dTOcnxKWyAQcUTsH3fqLL RvCW/JuV/ZElaqHWkXoGteK1wYi6/NShzbxO29sg5/Geyj5usdxJafmy/l4skzGbDSgO SLO2m5VDDGQ/ewQVgf1KixfZYl1iI7B1F12Fy0WanVkftX69sC2LU5iKgvLo4Bn4Yyt5 /TN9JhIw0Bpp7kr+ZzEf/keObrdmlrBm6VWOYThycWb3N5ALtbnnNM4bCekRf7AapSwq kGVw== X-Gm-Message-State: AOUpUlFPSZmyN4JtegU3LLPG2cVYMp8bSiJOuGIBkAgebvIL6PpNyEBq OGhK6S2BXwQgzOVdshtL7sMxvsxk X-Received: by 2002:adf:8919:: with SMTP id s25-v6mr30263238wrs.89.1534799631432; Mon, 20 Aug 2018 14:13:51 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id 1-v6sm1049770wmf.47.2018.08.20.14.13.50 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Aug 2018 14:13:50 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 20 Aug 2018 23:13:31 +0200 Message-Id: <20180820211332.20871-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec: add IMM4 decoder X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This work is sponsored by VideoLAN. Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 + libavcodec/imm4.c | 501 ++++++++++++++++++++++++++++++++++++++++ libavformat/riff.c | 1 + 6 files changed, 512 insertions(+) create mode 100644 libavcodec/imm4.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 6e40702947..f0c8226283 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -385,6 +385,7 @@ OBJS-$(CONFIG_IDCIN_DECODER) += idcinvideo.o OBJS-$(CONFIG_IDF_DECODER) += bintext.o cga_data.o OBJS-$(CONFIG_IFF_ILBM_DECODER) += iff.o OBJS-$(CONFIG_IMC_DECODER) += imc.o +OBJS-$(CONFIG_IMM4_DECODER) += imm4.o OBJS-$(CONFIG_INDEO2_DECODER) += indeo2.o OBJS-$(CONFIG_INDEO3_DECODER) += indeo3.o OBJS-$(CONFIG_INDEO4_DECODER) += indeo4.o ivi.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index ab3ec04251..fd35fc1d0b 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -153,6 +153,7 @@ extern AVCodec ff_huffyuv_encoder; extern AVCodec ff_huffyuv_decoder; extern AVCodec ff_idcin_decoder; extern AVCodec ff_iff_ilbm_decoder; +extern AVCodec ff_imm4_decoder; extern AVCodec ff_indeo2_decoder; extern AVCodec ff_indeo3_decoder; extern AVCodec ff_indeo4_decoder; diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 56be65bd56..31e50d5a94 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -447,6 +447,7 @@ enum AVCodecID { AV_CODEC_ID_SVG, AV_CODEC_ID_GDV, AV_CODEC_ID_FITS, + AV_CODEC_ID_IMM4, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 1cbb241389..1779149091 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1654,6 +1654,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("FITS (Flexible Image Transport System)"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_IMM4, + .type = AVMEDIA_TYPE_VIDEO, + .name = "imm4", + .long_name = NULL_IF_CONFIG_SMALL("Infinity IMM4"), + .props = AV_CODEC_PROP_LOSSY, + }, /* various PCM "codecs" */ { diff --git a/libavcodec/imm4.c b/libavcodec/imm4.c new file mode 100644 index 0000000000..4769f352e9 --- /dev/null +++ b/libavcodec/imm4.c @@ -0,0 +1,501 @@ +/* + * Infinity IMM4 decoder + * + * Copyright (c) 2018 Paul B Mahol + * + * 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 +#include + +#include "libavutil/thread.h" + +#include "avcodec.h" +#include "bswapdsp.h" +#include "copy_block.h" +#include "get_bits.h" +#include "idctdsp.h" +#include "internal.h" + +typedef struct IMM4Context { + BswapDSPContext bdsp; + GetBitContext gb; + + AVFrame *prev_frame; + uint8_t *bitstream; + int bitstream_size; + + int changed_size; + int factor; + unsigned sindex; + + DECLARE_ALIGNED(32, int16_t, block)[6][64]; + IDCTDSPContext idsp; +} IMM4Context; + +static const uint8_t intra_cb[] = { + 12, 9, 6 +}; + +static const uint8_t inter_cb[] = { + 30, 20, 15 +}; + +static const uint8_t cbplo_symbols[] = { + 3, 4, 19, 20, 35, 36, 51, 52 +}; + +static const uint8_t cbplo_bits[] = { + 1, 4, 3, 6, 3, 6, 3, 6 +}; + +static const uint8_t cbplo_codes[] = { + 1, 1, 1, 1, 2, 2, 3, 3 +}; + +static const uint8_t cbphi_bits[] = { + 4, 5, 5, 4, 5, 4, 6, 4, 5, 6, 4, 4, 4, 4, 4, 2 +}; + +static const uint8_t cbphi_codes[] = { + 3, 5, 4, 9, 3, 7, 2, 11, 2, 3, 5, 10, 4, 8, 6, 3 +}; + +static const uint8_t blktype_symbols[] = { + 0, 1, 2, 3, 4, 16, 17, 18, 19, 20, 32, 33, 34, 35, 48, 50, 51, 52 +}; + +static const uint8_t blktype_bits[] = { + 1, 3, 3, 5, 6, 4, 7, 7, 8, 9, 4, 7, 7, 8, 6, 8, 7, 9 +}; + +static const uint8_t blktype_codes[] = { + 1, 3, 2, 3, 4, 3, 7, 5, 4, 4, 2, 6, 4, 3, 5, 5, 3, 2 +}; + +static const uint16_t block_symbols[] = { + 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0x81, 0x82, 0x83, + 0x84, 0x85, 0x86, 0x101, 0x102, 0x103, 0x104, 0x181, 0x182, 0x183, 0x201, 0x202, + 0x203, 0x281, 0x282, 0x283, 0x301, 0x302, 0x303, 0x381, 0x382, 0x401, 0x402, + 0x481, 0x482, 0x501, 0x502, 0x581, 0x601, 0x681, 0x701, 0x781, 0x801, 0x881, + 0x901, 0x981, 0xA01, 0xA81, 0xB01, 0xB81, 0xC01, 0xC81, 0xD01, 0x4001, 0x4002, + 0x4003, 0x4081, 0x4082, 0x4101, 0x4181, 0x4201, 0x4281, 0x4301, 0x4381, 0x4401, + 0x4481, 0x4501, 0x4581, 0x4601, 0x4681, 0x4701, 0x4781, 0x4801, 0x4881, 0x4901, + 0x4981, 0x4A01, 0x4A81, 0x4B01, 0x4B81, 0x4C01, 0x4C81, 0x4D01, 0x4D81, 0x4E01, + 0x4E81, 0x4F01, 0x4F81, 0x5001, 0x5081, 0x5101, 0x5181, 0x5201, 0x5281, 0x5301, + 0x5381, 0x5401 +}; + +static const uint8_t block_bits[] = { + 7, 2, 4, 6, 7, 8, 9, 9, 10, 10, 11, 11, 11, 3, 6, 8, 10, 11, 12, 4, 8, + 10, 12, 5, 9, 10, 5, 9, 12, 5, 10, 12, 6, 10, 12, 6, 10, 6, 10, 6, + 10, 7, 12, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 11, 11, 12, 12, 4, 9, + 11, 6, 11, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, + 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, + 12, 12 +}; + +static const uint8_t block_codes[] = { + 3, 2, 15, 21, 23, 31, 37, 36, 33, 32, 7, 6, 32, 6, 20, 30, 15, 33, 80, + 14, 29, 14, 81, 13, 35, 13, 12, 34, 82, 11, 12, 83, 19, 11, 84, 18, + 10, 17, 9, 16, 8, 22, 85, 21, 20, 28, 27, 33, 32, 31, 30, 29, 28, + 27, 26, 34, 35, 86, 87, 7, 25, 5, 15, 4, 14, 13, 12, 19, 18, 17, 16, + 26, 25, 24, 23, 22, 21, 20, 19, 24, 23, 22, 21, 20, 19, 18, 17, 7, + 6, 5, 4, 36, 37, 38, 39, 88, 89, 90, 91, 92, 93, 94, 95 +}; + +static VLC cbplo_tab; +static VLC cbphi_tab; +static VLC blktype_tab; +static VLC block_tab; + +static int get_cbphi(GetBitContext *gb, int x) +{ + int value; + + value = get_vlc2(gb, cbphi_tab.table, cbphi_tab.bits, 1); + if (value < 0) + return AVERROR_INVALIDDATA; + + return x ? value : 15 - value; +} + +static int decode_block(AVCodecContext *avctx, GetBitContext *gb, + int block, int factor, int flag) +{ + IMM4Context *s = avctx->priv_data; + int i, last, len, factor2; + + for (i = !flag; i < 64; i++) { + int value; + + value = get_vlc2(gb, block_tab.table, block_tab.bits, 1); + if (value < 0) + return AVERROR_INVALIDDATA; + if (value == 0) { + last = get_bits1(gb); + len = get_bits(gb, 6); + factor2 = get_sbits(gb, 8); + } else { + factor2 = value & 0x7F; + last = (value >> 14) & 1; + len = (value >> 7) & 0x3F; + if (get_bits1(gb)) + factor2 = -factor2; + } + i += len; + if (i >= 64) + break; + s->block[block][i] = factor * factor2; + if (last) + break; + } + + return 0; +} + +static int decode_blocks(AVCodecContext *avctx, GetBitContext *gb, + unsigned cbp, int flag) +{ + IMM4Context *s = avctx->priv_data; + int ret, i; + + memset(s->block, 0, sizeof(s->block)); + + for (i = 0; i < 6; i++) { + if (!flag) { + int x = get_bits(gb, 8); + + if (x == 255) + x = 128; + x *= 8; + + s->block[i][0] = x; + } + + if (cbp & (1 << (5 - i))) { + ret = decode_block(avctx, gb, i, s->factor, flag); + if (ret < 0) + return ret; + } + } + + return 0; +} + +static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame) +{ + IMM4Context *s = avctx->priv_data; + int ret, x, y; + + s->factor = intra_cb[s->sindex] * 2; + + for (y = 0; y < avctx->height; y += 16) { + for (x = 0; x < avctx->width; x += 16) { + unsigned cbphi, cbplo; + + cbplo = get_vlc2(gb, cbplo_tab.table, cbplo_tab.bits, 1) >> 4; + skip_bits1(gb); + + cbphi = get_cbphi(gb, 1); + + ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 0); + if (ret < 0) + return ret; + + s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x, + frame->linesize[0], s->block[0]); + s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8, + frame->linesize[0], s->block[1]); + s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x, + frame->linesize[0], s->block[2]); + s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8, + frame->linesize[0], s->block[3]); + s->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1), + frame->linesize[1], s->block[4]); + s->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1), + frame->linesize[2], s->block[5]); + } + } + + return 0; +} + +static int decode_inter(AVCodecContext *avctx, GetBitContext *gb, + AVFrame *frame, AVFrame *prev) +{ + IMM4Context *s = avctx->priv_data; + int ret, x, y; + + s->factor = inter_cb[s->sindex]; + + for (y = 0; y < avctx->height; y += 16) { + for (x = 0; x < avctx->width; x += 16) { + int reverse, intra_block, value; + unsigned cbphi, cbplo; + + if (get_bits1(gb)) { + copy_block16(frame->data[0] + y * frame->linesize[0] + x, + prev->data[0] + y * prev->linesize[0] + x, + frame->linesize[0], prev->linesize[0], 16); + copy_block8(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1), + prev->data[1] + (y >> 1) * prev->linesize[1] + (x >> 1), + frame->linesize[1], prev->linesize[1], 8); + copy_block8(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1), + prev->data[2] + (y >> 1) * prev->linesize[2] + (x >> 1), + frame->linesize[2], prev->linesize[2], 8); + continue; + } + + value = get_vlc2(gb, blktype_tab.table, blktype_tab.bits, 1); + if (value < 0) + return AVERROR_INVALIDDATA; + + intra_block = value & 0x07; + reverse = intra_block == 3; + if (reverse) + skip_bits1(gb); + + cbplo = value >> 4; + cbphi = get_cbphi(gb, reverse); + if (intra_block) { + ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 0); + if (ret < 0) + return ret; + + s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x, + frame->linesize[0], s->block[0]); + s->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8, + frame->linesize[0], s->block[1]); + s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x, + frame->linesize[0], s->block[2]); + s->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8, + frame->linesize[0], s->block[3]); + s->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1), + frame->linesize[1], s->block[4]); + s->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1), + frame->linesize[2], s->block[5]); + } else { + skip_bits(gb, 2); + ret = decode_blocks(avctx, gb, cbplo | (cbphi << 2), 1); + if (ret < 0) + return ret; + + copy_block16(frame->data[0] + y * frame->linesize[0] + x, + prev->data[0] + y * prev->linesize[0] + x, + frame->linesize[0], prev->linesize[0], 16); + copy_block8(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1), + prev->data[1] + (y >> 1) * prev->linesize[1] + (x >> 1), + frame->linesize[1], prev->linesize[1], 8); + copy_block8(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1), + prev->data[2] + (y >> 1) * prev->linesize[2] + (x >> 1), + frame->linesize[2], prev->linesize[2], 8); + + s->idsp.idct_add(frame->data[0] + y * frame->linesize[0] + x, + frame->linesize[0], s->block[0]); + s->idsp.idct_add(frame->data[0] + y * frame->linesize[0] + x + 8, + frame->linesize[0], s->block[1]); + s->idsp.idct_add(frame->data[0] + (y + 8) * frame->linesize[0] + x, + frame->linesize[0], s->block[2]); + s->idsp.idct_add(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8, + frame->linesize[0], s->block[3]); + s->idsp.idct_add(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1), + frame->linesize[1], s->block[4]); + s->idsp.idct_add(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1), + frame->linesize[2], s->block[5]); + } + } + } + + return 0; +} + +static int decode_frame(AVCodecContext *avctx, void *data, + int *got_frame, AVPacket *avpkt) +{ + IMM4Context *s = avctx->priv_data; + GetBitContext *gb = &s->gb; + AVFrame *frame = data; + unsigned type; + int ret, scaled; + + if (avpkt->size <= 32) + return AVERROR_INVALIDDATA; + + av_fast_padded_malloc(&s->bitstream, &s->bitstream_size, + FFALIGN(avpkt->size, 4)); + if (!s->bitstream) + return AVERROR(ENOMEM); + + s->bdsp.bswap_buf((uint32_t *)s->bitstream, + (uint32_t *)avpkt->data, + (avpkt->size + 3) >> 2); + + if ((ret = init_get_bits8(gb, s->bitstream, FFALIGN(avpkt->size, 4))) < 0) + return ret; + + avctx->pix_fmt = AV_PIX_FMT_YUV420P; + avctx->color_range = AVCOL_RANGE_JPEG; + + scaled = avpkt->data[8]; + if (scaled < 2) { + int width, height; + int mode = avpkt->data[10]; + + switch (mode) { + case 1: + width = 352; + height = 240; + break; + case 2: + width = 704; + height = 240; + break; + case 4: + width = 480; + height = 704; + break; + case 17: + width = 352; + height = 288; + break; + case 18: + width = 704; + height = 288; + break; + default: + width = 704; + height = 576; + break; + } + + if (s->changed_size == 1 && + (avctx->width != width || avctx->height != height)) { + av_log(avctx, AV_LOG_ERROR, "Frame size change is unsupported.\n"); + return AVERROR_INVALIDDATA; + } + avctx->width = width; + avctx->height = height; + } + + s->changed_size = 1; + skip_bits_long(gb, 24 * 8); + type = get_bits_long(gb, 32); + s->sindex = get_bits_long(gb, 32); + if (s->sindex > 2) + return AVERROR_INVALIDDATA; + + switch (type) { + case 0x19781977: + frame->key_frame = 1; + frame->pict_type = AV_PICTURE_TYPE_I; + break; + case 0x12250926: + frame->key_frame = 0; + frame->pict_type = AV_PICTURE_TYPE_P; + break; + default: + avpriv_request_sample(avctx, "type %X", type); + return AVERROR_PATCHWELCOME; + } + + if ((ret = ff_get_buffer(avctx, frame, frame->key_frame ? AV_GET_BUFFER_FLAG_REF : 0)) < 0) + return ret; + + if (frame->key_frame) { + ret = decode_intra(avctx, gb, frame); + if (ret < 0) + return ret; + + av_frame_unref(s->prev_frame); + if ((ret = av_frame_ref(s->prev_frame, frame)) < 0) + return ret; + } else { + if (!s->prev_frame->data[0]) { + av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n"); + return AVERROR_INVALIDDATA; + } + + ret = decode_inter(avctx, gb, frame, s->prev_frame); + if (ret < 0) + return ret; + } + + *got_frame = 1; + + return avpkt->size; +} + +static av_cold void imm4_init_static_data(void) +{ + INIT_VLC_SPARSE_STATIC(&cbplo_tab, 9, FF_ARRAY_ELEMS(cbplo_bits), + cbplo_bits, 1, 1, cbplo_codes, 1, 1, cbplo_symbols, 1, 1, 512); + + INIT_VLC_SPARSE_STATIC(&cbphi_tab, 6, FF_ARRAY_ELEMS(cbphi_bits), + cbphi_bits, 1, 1, cbphi_codes, 1, 1, NULL, 0, 0, 64); + + INIT_VLC_SPARSE_STATIC(&blktype_tab, 9, FF_ARRAY_ELEMS(blktype_bits), + blktype_bits, 1, 1, blktype_codes, 1, 1, blktype_symbols, 1, 1, 512); + + INIT_VLC_SPARSE_STATIC(&block_tab, 12, FF_ARRAY_ELEMS(block_bits), + block_bits, 1, 1, block_codes, 1, 1, block_symbols, 2, 2, 4096); +} + +static av_cold int decode_init(AVCodecContext *avctx) +{ + static AVOnce init_static_once = AV_ONCE_INIT; + IMM4Context *s = avctx->priv_data; + + avctx->idct_algo = FF_IDCT_FAAN; + ff_bswapdsp_init(&s->bdsp); + ff_idctdsp_init(&s->idsp, avctx); + + s->prev_frame = av_frame_alloc(); + if (!s->prev_frame) + return AVERROR(ENOMEM); + + ff_thread_once(&init_static_once, imm4_init_static_data); + + return 0; +} + +static av_cold int decode_close(AVCodecContext *avctx) +{ + IMM4Context *s = avctx->priv_data; + + av_frame_free(&s->prev_frame); + av_freep(&s->bitstream); + s->bitstream_size = 0; + + return 0; +} + +AVCodec ff_imm4_decoder = { + .name = "imm4", + .long_name = NULL_IF_CONFIG_SMALL("Infinity IMM4"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_IMM4, + .priv_data_size = sizeof(IMM4Context), + .init = decode_init, + .close = decode_close, + .decode = decode_frame, + .capabilities = AV_CODEC_CAP_DR1, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | + FF_CODEC_CAP_INIT_CLEANUP, +}; diff --git a/libavformat/riff.c b/libavformat/riff.c index 111e408912..3e14644c46 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -470,6 +470,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { { AV_CODEC_ID_AV1, MKTAG('A', 'V', '0', '1') }, { AV_CODEC_ID_MSCC, MKTAG('M', 'S', 'C', 'C') }, { AV_CODEC_ID_SRGC, MKTAG('S', 'R', 'G', 'C') }, + { AV_CODEC_ID_IMM4, MKTAG('I', 'M', 'M', '4') }, { AV_CODEC_ID_NONE, 0 } };