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