FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/imx.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 0 98 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 #if FF_API_PALETTE_HAS_CHANGED
62 FF_DISABLE_DEPRECATION_WARNINGS
63 frame->palette_has_changed = 1;
64 FF_ENABLE_DEPRECATION_WARNINGS
65 #endif
66 frame->flags |= AV_FRAME_FLAG_KEY;
67 } else {
68 frame->flags &= ~AV_FRAME_FLAG_KEY;
69 #if FF_API_PALETTE_HAS_CHANGED
70 FF_DISABLE_DEPRECATION_WARNINGS
71 frame->palette_has_changed = 0;
72 FF_ENABLE_DEPRECATION_WARNINGS
73 #endif
74 }
75
76 bytestream2_init(&gb, avpkt->data, avpkt->size);
77
78 memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE);
79
80 x = 0, y = 0;
81 while (bytestream2_get_bytes_left(&gb) > 0 &&
82 x < 320 && y < 160) {
83 int b = bytestream2_get_byte(&gb);
84 int len = b & 0x3f;
85 int op = b >> 6;
86 int fill;
87
88 switch (op) {
89 case 3:
90 len = len * 64 + bytestream2_get_byte(&gb);
91 case 0:
92 while (len > 0) {
93 x++;
94 len--;
95 if (x >= 320) {
96 x = 0;
97 y++;
98 }
99 if (y >= 160)
100 break;
101 }
102
103 frame->flags &= ~AV_FRAME_FLAG_KEY;
104 break;
105 case 1:
106 if (len == 0) {
107 int offset = bytestream2_get_le16(&gb);
108
109 if (offset < 0 || offset >= 32768)
110 return AVERROR_INVALIDDATA;
111
112 len = bytestream2_get_byte(&gb);
113 while (len > 0 && offset < 32768) {
114 frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++];
115 x++;
116 len--;
117 if (x >= 320) {
118 x = 0;
119 y++;
120 }
121 if (y >= 160)
122 break;
123 }
124
125 frame->flags &= ~AV_FRAME_FLAG_KEY;
126 } else {
127 while (len > 0) {
128 fill = bytestream2_get_byte(&gb);
129 frame->data[0][x + y * frame->linesize[0]] = fill;
130 if (imx->pos < 32768)
131 imx->history[imx->pos++] = fill;
132 x++;
133 len--;
134 if (x >= 320) {
135 x = 0;
136 y++;
137 }
138 if (y >= 160)
139 break;
140 }
141 }
142 break;
143 case 2:
144 fill = bytestream2_get_byte(&gb);
145
146 while (len > 0) {
147 frame->data[0][x + y * frame->linesize[0]] = fill;
148 x++;
149 len--;
150 if (x >= 320) {
151 x = 0;
152 y++;
153 }
154 if (y >= 160)
155 break;
156 }
157 break;
158 }
159 }
160
161 frame->pict_type = (frame->flags & AV_FRAME_FLAG_KEY) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
162
163 if ((ret = av_frame_ref(rframe, frame)) < 0)
164 return ret;
165
166 *got_frame = 1;
167
168 return avpkt->size;
169 }
170
171 static void imx_decode_flush(AVCodecContext *avctx)
172 {
173 SimbiosisIMXContext *imx = avctx->priv_data;
174
175 av_frame_unref(imx->frame);
176 imx->pos = 0;
177 memset(imx->pal, 0, sizeof(imx->pal));
178 memset(imx->history, 0, sizeof(imx->history));
179 }
180
181 static int imx_decode_close(AVCodecContext *avctx)
182 {
183 SimbiosisIMXContext *imx = avctx->priv_data;
184
185 av_frame_free(&imx->frame);
186
187 return 0;
188 }
189
190 const FFCodec ff_simbiosis_imx_decoder = {
191 .p.name = "simbiosis_imx",
192 CODEC_LONG_NAME("Simbiosis Interactive IMX Video"),
193 .p.type = AVMEDIA_TYPE_VIDEO,
194 .p.id = AV_CODEC_ID_SIMBIOSIS_IMX,
195 .priv_data_size = sizeof(SimbiosisIMXContext),
196 .init = imx_decode_init,
197 FF_CODEC_DECODE_CB(imx_decode_frame),
198 .close = imx_decode_close,
199 .flush = imx_decode_flush,
200 .p.capabilities = AV_CODEC_CAP_DR1,
201 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
202 };
203