2 * Copyright (c) 2021 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/common.h"
23 #include "bytestream.h"
26 typedef struct SimbiosisIMXContext {
28 uint8_t history[32768];
30 } SimbiosisIMXContext;
32 static av_cold int imx_decode_init(AVCodecContext *avctx)
34 avctx->pix_fmt = AV_PIX_FMT_PAL8;
40 static int imx_decode_frame(AVCodecContext *avctx, void *data,
41 int *got_frame, AVPacket *avpkt)
43 SimbiosisIMXContext *imx = avctx->priv_data;
44 int ret, x, y, pal_size;
45 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
46 AVFrame *frame = data;
49 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
52 if (pal && pal_size == AVPALETTE_SIZE) {
53 memcpy(imx->pal, pal, pal_size);
54 frame->palette_has_changed = 1;
57 bytestream2_init(&gb, avpkt->data, avpkt->size);
59 memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE);
62 while (bytestream2_get_bytes_left(&gb) > 0 &&
64 int b = bytestream2_get_byte(&gb);
71 len = len * 64 + bytestream2_get_byte(&gb);
86 int offset = bytestream2_get_le16(&gb);
88 if (offset < 0 || offset >= 32768)
89 return AVERROR_INVALIDDATA;
91 len = bytestream2_get_byte(&gb);
92 while (len > 0 && offset < 32768) {
93 frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++];
105 fill = bytestream2_get_byte(&gb);
106 frame->data[0][x + y * frame->linesize[0]] = fill;
107 if (imx->pos < 32768)
108 imx->history[imx->pos++] = fill;
121 fill = bytestream2_get_byte(&gb);
124 frame->data[0][x + y * frame->linesize[0]] = fill;
143 AVCodec ff_simbiosis_imx_decoder = {
144 .name = "simbiosis_imx",
145 .long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX Video"),
146 .type = AVMEDIA_TYPE_VIDEO,
147 .id = AV_CODEC_ID_SIMBIOSIS_IMX,
148 .priv_data_size = sizeof(SimbiosisIMXContext),
149 .init = imx_decode_init,
150 .decode = imx_decode_frame,
151 .capabilities = AV_CODEC_CAP_DR1,
152 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,