| @@ -0,0 +1,153 @@ |
| | 1 | /* |
| | 2 | * Copyright (c) 2021 Paul B Mahol |
| | 3 | * |
| | 4 | * This file is part of FFmpeg. |
| | 5 | * |
| | 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. |
| | 10 | * |
| | 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. |
| | 15 | * |
| | 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 |
| | 19 | */ |
| | 20 | |
| | 21 | #include "libavutil/common.h" |
| | 22 | #include "avcodec.h" |
| | 23 | #include "bytestream.h" |
| | 24 | #include "internal.h" |
| | 25 | |
| | 26 | typedef struct SimbiosisIMXContext { |
| | 27 | uint32_t pal[256]; |
| | 28 | uint8_t history[32768]; |
| | 29 | int pos; |
| | 30 | } SimbiosisIMXContext; |
| | 31 | |
| | 32 | static av_cold int imx_decode_init(AVCodecContext *avctx) |
| | 33 | { |
| | 34 | avctx->pix_fmt = AV_PIX_FMT_PAL8; |
| | 35 | avctx->width = 320; |
| | 36 | avctx->height = 160; |
| | 37 | return 0; |
| | 38 | } |
| | 39 | |
| | 40 | static int imx_decode_frame(AVCodecContext *avctx, void *data, |
| | 41 | int *got_frame, AVPacket *avpkt) |
| | 42 | { |
| | 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; |
| | 47 | GetByteContext gb; |
| | 48 | |
| | 49 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| | 50 | return ret; |
| | 51 | |
| | 52 | if (pal && pal_size == AVPALETTE_SIZE) { |
| | 53 | memcpy(imx->pal, pal, pal_size); |
| | 54 | frame->palette_has_changed = 1; |
| | 55 | } |
| | 56 | |
| | 57 | bytestream2_init(&gb, avpkt->data, avpkt->size); |
| | 58 | |
| | 59 | memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE); |
| | 60 | |
| | 61 | x = 0, y = 0; |
| | 62 | while (bytestream2_get_bytes_left(&gb) > 0 && |
| | 63 | x < 320 && y < 160) { |
| | 64 | int b = bytestream2_get_byte(&gb); |
| | 65 | int len = b & 0x3f; |
| | 66 | int op = b >> 6; |
| | 67 | int fill; |
| | 68 | |
| | 69 | switch (op) { |
| | 70 | case 3: |
| | 71 | len = len * 64 + bytestream2_get_byte(&gb); |
| | 72 | case 0: |
| | 73 | while (len > 0) { |
| | 74 | x++; |
| | 75 | len--; |
| | 76 | if (x >= 320) { |
| | 77 | x = 0; |
| | 78 | y++; |
| | 79 | } |
| | 80 | if (y >= 160) |
| | 81 | break; |
| | 82 | } |
| | 83 | break; |
| | 84 | case 1: |
| | 85 | if (len == 0) { |
| | 86 | int offset = bytestream2_get_le16(&gb); |
| | 87 | |
| | 88 | if (offset < 0 || offset >= 32768) |
| | 89 | return AVERROR_INVALIDDATA; |
| | 90 | |
| | 91 | len = bytestream2_get_byte(&gb); |
| | 92 | while (len > 0 && offset < 32768) { |
| | 93 | frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++]; |
| | 94 | x++; |
| | 95 | len--; |
| | 96 | if (x >= 320) { |
| | 97 | x = 0; |
| | 98 | y++; |
| | 99 | } |
| | 100 | if (y >= 160) |
| | 101 | break; |
| | 102 | } |
| | 103 | } else { |
| | 104 | while (len > 0) { |
| | 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; |
| | 109 | x++; |
| | 110 | len--; |
| | 111 | if (x >= 320) { |
| | 112 | x = 0; |
| | 113 | y++; |
| | 114 | } |
| | 115 | if (y >= 160) |
| | 116 | break; |
| | 117 | } |
| | 118 | } |
| | 119 | break; |
| | 120 | case 2: |
| | 121 | fill = bytestream2_get_byte(&gb); |
| | 122 | |
| | 123 | while (len > 0) { |
| | 124 | frame->data[0][x + y * frame->linesize[0]] = fill; |
| | 125 | x++; |
| | 126 | len--; |
| | 127 | if (x >= 320) { |
| | 128 | x = 0; |
| | 129 | y++; |
| | 130 | } |
| | 131 | if (y >= 160) |
| | 132 | break; |
| | 133 | } |
| | 134 | break; |
| | 135 | } |
| | 136 | } |
| | 137 | |
| | 138 | *got_frame = 1; |
| | 139 | |
| | 140 | return avpkt->size; |
| | 141 | } |
| | 142 | |
| | 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, |
| | 153 | }; |