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