FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/imx.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 0 96 0.0%
Functions: 0 4 0.0%
Branches: 0 55 0.0%

Line Branch Exec Source
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 "codec_internal.h"
25 #include "decode.h"
26
27 typedef struct SimbiosisIMXContext {
28 AVFrame *frame;
29 uint32_t pal[256];
30 uint8_t history[32768];
31 int pos;
32 } SimbiosisIMXContext;
33
34 static av_cold int imx_decode_init(AVCodecContext *avctx)
35 {
36 SimbiosisIMXContext *imx = avctx->priv_data;
37
38 avctx->pix_fmt = AV_PIX_FMT_PAL8;
39 avctx->width = 320;
40 avctx->height = 160;
41
42 imx->frame = av_frame_alloc();
43 if (!imx->frame)
44 return AVERROR(ENOMEM);
45
46 return 0;
47 }
48
49 static int imx_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
50 int *got_frame, AVPacket *avpkt)
51 {
52 SimbiosisIMXContext *imx = avctx->priv_data;
53 int ret, x, y;
54 AVFrame *frame = imx->frame;
55 GetByteContext gb;
56
57 if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
58 return ret;
59
60 if (ff_copy_palette(imx->pal, avpkt, avctx)) {
61 frame->flags |= AV_FRAME_FLAG_KEY;
62 } else {
63 frame->flags &= ~AV_FRAME_FLAG_KEY;
64 }
65
66 bytestream2_init(&gb, avpkt->data, avpkt->size);
67
68 memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE);
69
70 x = 0, y = 0;
71 while (bytestream2_get_bytes_left(&gb) > 0 &&
72 x < 320 && y < 160) {
73 int b = bytestream2_get_byte(&gb);
74 int len = b & 0x3f;
75 int op = b >> 6;
76 int fill;
77
78 switch (op) {
79 case 3:
80 len = len * 64 + bytestream2_get_byte(&gb);
81 case 0:
82 while (len > 0) {
83 x++;
84 len--;
85 if (x >= 320) {
86 x = 0;
87 y++;
88 }
89 if (y >= 160)
90 break;
91 }
92
93 frame->flags &= ~AV_FRAME_FLAG_KEY;
94 break;
95 case 1:
96 if (len == 0) {
97 int offset = bytestream2_get_le16(&gb);
98
99 if (offset < 0 || offset >= 32768)
100 return AVERROR_INVALIDDATA;
101
102 len = bytestream2_get_byte(&gb);
103 while (len > 0 && offset < 32768) {
104 frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++];
105 x++;
106 len--;
107 if (x >= 320) {
108 x = 0;
109 y++;
110 }
111 if (y >= 160)
112 break;
113 }
114
115 frame->flags &= ~AV_FRAME_FLAG_KEY;
116 } else {
117 while (len > 0) {
118 fill = bytestream2_get_byte(&gb);
119 frame->data[0][x + y * frame->linesize[0]] = fill;
120 if (imx->pos < 32768)
121 imx->history[imx->pos++] = fill;
122 x++;
123 len--;
124 if (x >= 320) {
125 x = 0;
126 y++;
127 }
128 if (y >= 160)
129 break;
130 }
131 }
132 break;
133 case 2:
134 fill = bytestream2_get_byte(&gb);
135
136 while (len > 0) {
137 frame->data[0][x + y * frame->linesize[0]] = fill;
138 x++;
139 len--;
140 if (x >= 320) {
141 x = 0;
142 y++;
143 }
144 if (y >= 160)
145 break;
146 }
147 break;
148 }
149 }
150
151 frame->pict_type = (frame->flags & AV_FRAME_FLAG_KEY) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
152
153 if ((ret = av_frame_ref(rframe, frame)) < 0)
154 return ret;
155
156 *got_frame = 1;
157
158 return avpkt->size;
159 }
160
161 static void imx_decode_flush(AVCodecContext *avctx)
162 {
163 SimbiosisIMXContext *imx = avctx->priv_data;
164
165 av_frame_unref(imx->frame);
166 imx->pos = 0;
167 memset(imx->pal, 0, sizeof(imx->pal));
168 memset(imx->history, 0, sizeof(imx->history));
169 }
170
171 static int imx_decode_close(AVCodecContext *avctx)
172 {
173 SimbiosisIMXContext *imx = avctx->priv_data;
174
175 av_frame_free(&imx->frame);
176
177 return 0;
178 }
179
180 const FFCodec ff_simbiosis_imx_decoder = {
181 .p.name = "simbiosis_imx",
182 CODEC_LONG_NAME("Simbiosis Interactive IMX Video"),
183 .p.type = AVMEDIA_TYPE_VIDEO,
184 .p.id = AV_CODEC_ID_SIMBIOSIS_IMX,
185 .priv_data_size = sizeof(SimbiosisIMXContext),
186 .init = imx_decode_init,
187 FF_CODEC_DECODE_CB(imx_decode_frame),
188 .close = imx_decode_close,
189 .flush = imx_decode_flush,
190 .p.capabilities = AV_CODEC_CAP_DR1,
191 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
192 };
193