From patchwork Tue Jan 22 09:55:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 11823 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id F2B5F44E12E for ; Tue, 22 Jan 2019 11:55:44 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 23F8768AA02; Tue, 22 Jan 2019 11:55:33 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 43F4168A830 for ; Tue, 22 Jan 2019 11:55:27 +0200 (EET) Received: by mail-wr1-f65.google.com with SMTP id p4so26546241wrt.7 for ; Tue, 22 Jan 2019 01:55:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=qzg8gd+KiqPg8N1fEvNz52BUoaaOtxBM37DRUSaXgX4=; b=QWZT41VP4lrkPlM0/9qYLXkma1i6IiB3qT2Xq0l6YziO2dqmAxbsEu6s2NDNP7Xp8a hnOSI1oh8g6QyZtphPSOJNxTwPMjdNYhKm54HNRXHWgFdrF3dzF+MweWEJI5S5nZhosP POIKG6mJtzrO4WsIXBPoEY9y/lkTtklO9SCq9c2DU2Ej3tWXUaz4FkTul7/2CGimFjM0 me632NCzqCFoWVpwjLK+YMom/TYBL337MQ8RbrslCGAAVaVqUhLUs5SRLdtJrQ8+TlPr oTKtu27cLBGKDfHMZ3WV2y9QrA55lXVnOHXvhmtiBZe8mfNZmc/FsOHasL5T+UlQHXKc TWKg== 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=qzg8gd+KiqPg8N1fEvNz52BUoaaOtxBM37DRUSaXgX4=; b=Gqe+kGdbeKWJ5xQ/d6WZgX6P+lnqsmokPOSjUdfVV4g88mGv3mHb8pON+Zr1/iArYb Yx26pRyY83xH26qvrJBW88cOcSLio7sns/zFIJoHacu1g2cy48EVoNCSJvWZQLHOh8+K vFEQ4LQl+Jb4csuj4/aD5MiM6vvoRHJudNmnQ4IWoVhiEyvUQaon4VXamzYxvqPu/nWI C7gyn4kt6sSNThFSGWZtpR5jF6MrHoNvs4ibf8u7L5FRWBst/4dOPjcJX3aFF5dZViOq 68xYsDDjedbCX+rlBbDcWNnVe/RgKjV1q4ZDWIt7aONRtRLP8byLj/pK0gP5NnH9rcMt u/BA== X-Gm-Message-State: AJcUukfsc83g5QkoKby2HSwxwg0TZGLp/k4nSDOl6INNdZTGl7r/jFlh UtJWZRG0Ep5sSbuT971Ymru+pNjo X-Google-Smtp-Source: ALg8bN4LcKBoB+vQh/mFZmRsyYkiRAiNv7qTFR3Ys8GQu1aQPMVLKfzEJxuDiX0SaVscLUd0YPQEiw== X-Received: by 2002:a5d:4250:: with SMTP id s16mr32225485wrr.253.1548150943761; Tue, 22 Jan 2019 01:55:43 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id p139sm107042134wmd.31.2019.01.22.01.55.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Jan 2019 01:55:43 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 22 Jan 2019 10:55:33 +0100 Message-Id: <20190122095533.14932-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avcodec: add ARBC 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" Thanks Kostya for great help in reversing binary. Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/arbc.c | 203 ++++++++++++++++++++++++++++++++++++++++ libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 ++ libavformat/riff.c | 1 + 6 files changed, 214 insertions(+) create mode 100644 libavcodec/arbc.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index bf746c143d..8e240aecf0 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -198,6 +198,7 @@ OBJS-$(CONFIG_APTX_HD_DECODER) += aptx.o OBJS-$(CONFIG_APTX_HD_ENCODER) += aptx.o OBJS-$(CONFIG_APNG_DECODER) += png.o pngdec.o pngdsp.o OBJS-$(CONFIG_APNG_ENCODER) += png.o pngenc.o +OBJS-$(CONFIG_ARBC_DECODER) += arbc.o OBJS-$(CONFIG_SSA_DECODER) += assdec.o ass.o OBJS-$(CONFIG_SSA_ENCODER) += assenc.o ass.o OBJS-$(CONFIG_ASS_DECODER) += assdec.o ass.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index fe0376e27e..5cbb09a5a4 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -41,6 +41,7 @@ extern AVCodec ff_anm_decoder; extern AVCodec ff_ansi_decoder; extern AVCodec ff_apng_encoder; extern AVCodec ff_apng_decoder; +extern AVCodec ff_arbc_decoder; extern AVCodec ff_asv1_encoder; extern AVCodec ff_asv1_decoder; extern AVCodec ff_asv2_encoder; diff --git a/libavcodec/arbc.c b/libavcodec/arbc.c new file mode 100644 index 0000000000..59a1d7bf0a --- /dev/null +++ b/libavcodec/arbc.c @@ -0,0 +1,203 @@ +/* + * Gryphon's Anim Compressor 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/imgutils.h" +#include "libavutil/internal.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/mem.h" + +#include "avcodec.h" +#include "bytestream.h" +#include "internal.h" + +typedef struct ARBCContext { + GetByteContext gb; + + AVFrame *prev_frame; +} ARBCContext; + +static void fill_tile4(AVCodecContext *avctx, uint8_t *color, AVFrame *frame) +{ + ARBCContext *s = avctx->priv_data; + GetByteContext *gb = &s->gb; + int nb_tiles = bytestream2_get_le16(gb); + int h = avctx->height - 1; + + for (int i = 0; i < nb_tiles; i++) { + int y = bytestream2_get_byte(gb); + int x = bytestream2_get_byte(gb); + uint16_t mask = bytestream2_get_le16(gb); + int start_y = y * 4, start_x = x * 4; + int end_y = start_y + 4, end_x = start_x + 4; + + for (int j = start_y; j < end_y; j++) { + for (int k = start_x; k < end_x; k++) { + if (mask & 0x8000) { + if (j >= avctx->height || k >= avctx->width) + continue; + frame->data[0][frame->linesize[0] * (h - j) + 3 * k + 0] = color[0]; + frame->data[0][frame->linesize[0] * (h - j) + 3 * k + 1] = color[1]; + frame->data[0][frame->linesize[0] * (h - j) + 3 * k + 2] = color[2]; + } + mask = mask << 1; + } + } + } +} + +static void fill_tileX(AVCodecContext *avctx, int tile_width, int tile_height, + uint8_t *color, AVFrame *frame) +{ + ARBCContext *s = avctx->priv_data; + GetByteContext *gb = &s->gb; + const int step_h = tile_height / 4; + const int step_w = tile_width / 4; + int nb_tiles = bytestream2_get_le16(gb); + int h = avctx->height - 1; + + for (int i = 0; i < nb_tiles; i++) { + int y = bytestream2_get_byte(gb); + int x = bytestream2_get_byte(gb); + uint16_t mask = bytestream2_get_le16(gb); + int start_y = y * tile_height, start_x = x * tile_width; + int end_y = start_y + tile_height, end_x = start_x + tile_width; + + for (int j = start_y; j < end_y; j += step_h) { + for (int k = start_x; k < end_x; k += step_w) { + if (mask & 0x8000U) { + for (int m = 0; m < step_h; m++) { + for (int n = 0; n < step_w; n++) { + if (j + m >= avctx->height || k + n >= avctx->width) + continue; + frame->data[0][frame->linesize[0] * (h - (j + m)) + 3 * (k + n) + 0] = color[0]; + frame->data[0][frame->linesize[0] * (h - (j + m)) + 3 * (k + n) + 1] = color[1]; + frame->data[0][frame->linesize[0] * (h - (j + m)) + 3 * (k + n) + 2] = color[2]; + } + } + } + mask = mask << 1; + } + } + } +} + +static int decode_frame(AVCodecContext *avctx, void *data, + int *got_frame, AVPacket *avpkt) +{ + ARBCContext *s = avctx->priv_data; + AVFrame *const frame = data; + int ret, nb_segments, keyframe = 1; + + if (avpkt->size < 1) + return AVERROR_INVALIDDATA; + + if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) + return ret; + + if (s->prev_frame->data[0]) { + ret = av_frame_copy(frame, s->prev_frame); + if (ret < 0) + return ret; + } + + bytestream2_init(&s->gb, avpkt->data, avpkt->size); + bytestream2_skip(&s->gb, 8); + nb_segments = bytestream2_get_le16(&s->gb); + if (nb_segments == 0) + keyframe = 0; + + for (int i = 0; i < nb_segments; i++) { + int resolution_flag; + uint8_t fill[3]; + + if (bytestream2_get_bytes_left(&s->gb) <= 0) + return AVERROR_INVALIDDATA; + + fill[0] = bytestream2_get_byte(&s->gb); + bytestream2_skip(&s->gb, 1); + fill[1] = bytestream2_get_byte(&s->gb); + bytestream2_skip(&s->gb, 1); + fill[2] = bytestream2_get_byte(&s->gb); + bytestream2_skip(&s->gb, 1); + resolution_flag = bytestream2_get_byte(&s->gb); + + if (resolution_flag & 0x10) + fill_tileX(avctx, 1024, 1024, fill, frame); + if (resolution_flag & 0x08) + fill_tileX(avctx, 256, 256, fill, frame); + if (resolution_flag & 0x04) + fill_tileX(avctx, 64, 64, fill, frame); + if (resolution_flag & 0x02) + fill_tileX(avctx, 16, 16, fill, frame); + if (resolution_flag & 0x01) + fill_tile4(avctx, fill, frame); + } + + av_frame_unref(s->prev_frame); + if ((ret = av_frame_ref(s->prev_frame, frame)) < 0) + return ret; + + frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; + frame->key_frame = keyframe; + *got_frame = 1; + + return avpkt->size; +} + +static av_cold int decode_init(AVCodecContext *avctx) +{ + ARBCContext *s = avctx->priv_data; + + avctx->pix_fmt = AV_PIX_FMT_RGB24; + + s->prev_frame = av_frame_alloc(); + if (!s->prev_frame) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold int decode_close(AVCodecContext *avctx) +{ + ARBCContext *s = avctx->priv_data; + + av_frame_free(&s->prev_frame); + + return 0; +} + +AVCodec ff_arbc_decoder = { + .name = "arbc", + .long_name = NULL_IF_CONFIG_SMALL("Gryphon's Anim Compressor"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_ARBC, + .priv_data_size = sizeof(ARBCContext), + .init = decode_init, + .decode = decode_frame, + .close = decode_close, + .capabilities = AV_CODEC_CAP_DR1, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | + FF_CODEC_CAP_INIT_CLEANUP, +}; diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 90f9f08289..72dc277dfd 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -453,6 +453,7 @@ enum AVCodecID { AV_CODEC_ID_WCMV, AV_CODEC_ID_RASC, AV_CODEC_ID_HYMT, + AV_CODEC_ID_ARBC, /* 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 10a639101c..7b254c1d45 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1698,6 +1698,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("HuffYUV MT"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_ARBC, + .type = AVMEDIA_TYPE_VIDEO, + .name = "arbc", + .long_name = NULL_IF_CONFIG_SMALL("Gryphon's Anim Compressor"), + .props = AV_CODEC_PROP_LOSSY, + }, /* various PCM "codecs" */ { diff --git a/libavformat/riff.c b/libavformat/riff.c index d5a509c8b9..8f0fd99e22 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -476,6 +476,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { { AV_CODEC_ID_WCMV, MKTAG('W', 'C', 'M', 'V') }, { AV_CODEC_ID_RASC, MKTAG('R', 'A', 'S', 'C') }, { AV_CODEC_ID_HYMT, MKTAG('H', 'Y', 'M', 'T') }, + { AV_CODEC_ID_ARBC, MKTAG('A', 'R', 'B', 'C') }, { AV_CODEC_ID_NONE, 0 } };