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 {
29 uint8_t history[32768];
31 } SimbiosisIMXContext;
33 static av_cold int imx_decode_init(AVCodecContext *avctx)
35 SimbiosisIMXContext *imx = avctx->priv_data;
37 avctx->pix_fmt = AV_PIX_FMT_PAL8;
41 imx->frame = av_frame_alloc();
43 return AVERROR(ENOMEM);
48 static int imx_decode_frame(AVCodecContext *avctx, void *data,
49 int *got_frame, AVPacket *avpkt)
51 SimbiosisIMXContext *imx = avctx->priv_data;
53 buffer_size_t pal_size;
54 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
55 AVFrame *frame = imx->frame;
58 if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
61 if (pal && pal_size == AVPALETTE_SIZE) {
62 memcpy(imx->pal, pal, pal_size);
63 frame->palette_has_changed = 1;
67 frame->palette_has_changed = 0;
70 bytestream2_init(&gb, avpkt->data, avpkt->size);
72 memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE);
75 while (bytestream2_get_bytes_left(&gb) > 0 &&
77 int b = bytestream2_get_byte(&gb);
84 len = len * 64 + bytestream2_get_byte(&gb);
101 int offset = bytestream2_get_le16(&gb);
103 if (offset < 0 || offset >= 32768)
104 return AVERROR_INVALIDDATA;
106 len = bytestream2_get_byte(&gb);
107 while (len > 0 && offset < 32768) {
108 frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++];
119 frame->key_frame = 0;
122 fill = bytestream2_get_byte(&gb);
123 frame->data[0][x + y * frame->linesize[0]] = fill;
124 if (imx->pos < 32768)
125 imx->history[imx->pos++] = fill;
138 fill = bytestream2_get_byte(&gb);
141 frame->data[0][x + y * frame->linesize[0]] = fill;
155 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
157 if ((ret = av_frame_ref(data, frame)) < 0)
165 static void imx_decode_flush(AVCodecContext *avctx)
167 SimbiosisIMXContext *imx = avctx->priv_data;
169 av_frame_unref(imx->frame);
171 memset(imx->pal, 0, sizeof(imx->pal));
172 memset(imx->history, 0, sizeof(imx->history));
175 static int imx_decode_close(AVCodecContext *avctx)
177 SimbiosisIMXContext *imx = avctx->priv_data;
179 av_frame_free(&imx->frame);
184 AVCodec ff_simbiosis_imx_decoder = {
185 .name = "simbiosis_imx",
186 .long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX Video"),
187 .type = AVMEDIA_TYPE_VIDEO,
188 .id = AV_CODEC_ID_SIMBIOSIS_IMX,
189 .priv_data_size = sizeof(SimbiosisIMXContext),
190 .init = imx_decode_init,
191 .decode = imx_decode_frame,
192 .close = imx_decode_close,
193 .flush = imx_decode_flush,
194 .capabilities = AV_CODEC_CAP_DR1,
195 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
196 FF_CODEC_CAP_INIT_CLEANUP,