History

avcodec: GEM Raster image decoder

Peter Ross committed 5 years ago

Commit 60a9d371 parent 314289c8 files · +357 −1Browse files

Unified · Split

8 files
  1. Changelog +1 −0
  2. doc/general_contents.texi +2 −0
  3. libavcodec/Makefile +1 −0
  4. libavcodec/allcodecs.c +1 −0
  5. libavcodec/codec_desc.c +7 −0
  6. libavcodec/codec_id.h +1 −0
  7. libavcodec/gemdec.c +343 −0
  8. libavcodec/version.h +1 −1
Changelog+1−0
@@ -23,6 +23,7 @@ version <next>: · 22 unmodified lines
23- morpho video filter23- morpho video filter
24- amr parser24- amr parser
25- (a)latency filters25- (a)latency filters
26- GEM Raster image decoder
26 27
27 28
28version 4.4:29version 4.4:
doc/general_contents.texi+2−0
@@ -741,6 +741,8 @@ following image formats are supported: · 740 unmodified lines
741 @tab OpenEXR741 @tab OpenEXR
742@item FITS @tab X @tab X742@item FITS @tab X @tab X
743 @tab Flexible Image Transport System743 @tab Flexible Image Transport System
744@item IMG @tab @tab X
745 @tab GEM Raster image
744@item JPEG @tab X @tab X746@item JPEG @tab X @tab X
745 @tab Progressive JPEG is not supported.747 @tab Progressive JPEG is not supported.
746@item JPEG 2000 @tab X @tab X748@item JPEG 2000 @tab X @tab X
libavcodec/Makefile+1−0
@@ -348,6 +348,7 @@ OBJS-$(CONFIG_G723_1_ENCODER) += g723_1enc.o g723_1.o \ · 347 unmodified lines
348 acelp_vectors.o celp_filters.o celp_math.o348 acelp_vectors.o celp_filters.o celp_math.o
349OBJS-$(CONFIG_G729_DECODER) += g729dec.o lsp.o celp_math.o celp_filters.o acelp_filters.o acelp_pitch_delay.o acelp_vectors.o g729postfilter.o349OBJS-$(CONFIG_G729_DECODER) += g729dec.o lsp.o celp_math.o celp_filters.o acelp_filters.o acelp_pitch_delay.o acelp_vectors.o g729postfilter.o
350OBJS-$(CONFIG_GDV_DECODER) += gdv.o350OBJS-$(CONFIG_GDV_DECODER) += gdv.o
351OBJS-$(CONFIG_GEM_DECODER) += gemdec.o
351OBJS-$(CONFIG_GIF_DECODER) += gifdec.o lzw.o352OBJS-$(CONFIG_GIF_DECODER) += gifdec.o lzw.o
352OBJS-$(CONFIG_GIF_ENCODER) += gif.o lzwenc.o353OBJS-$(CONFIG_GIF_ENCODER) += gif.o lzwenc.o
353OBJS-$(CONFIG_GREMLIN_DPCM_DECODER) += dpcm.o354OBJS-$(CONFIG_GREMLIN_DPCM_DECODER) += dpcm.o
libavcodec/allcodecs.c+1−0
@@ -134,6 +134,7 @@ extern const AVCodec ff_fraps_decoder; · 133 unmodified lines
134extern const AVCodec ff_frwu_decoder;134extern const AVCodec ff_frwu_decoder;
135extern const AVCodec ff_g2m_decoder;135extern const AVCodec ff_g2m_decoder;
136extern const AVCodec ff_gdv_decoder;136extern const AVCodec ff_gdv_decoder;
137extern const AVCodec ff_gem_decoder;
137extern const AVCodec ff_gif_encoder;138extern const AVCodec ff_gif_encoder;
138extern const AVCodec ff_gif_decoder;139extern const AVCodec ff_gif_decoder;
139extern const AVCodec ff_h261_encoder;140extern const AVCodec ff_h261_encoder;
libavcodec/codec_desc.c+7−0
@@ -1855,6 +1855,13 @@ static const AVCodecDescriptor codec_descriptors[] = { · 1854 unmodified lines
1855 .long_name = NULL_IF_CONFIG_SMALL("Digital Pictures SGA Video"),1855 .long_name = NULL_IF_CONFIG_SMALL("Digital Pictures SGA Video"),
1856 .props = AV_CODEC_PROP_LOSSY,1856 .props = AV_CODEC_PROP_LOSSY,
1857 },1857 },
1858 {
1859 .id = AV_CODEC_ID_GEM,
1860 .type = AVMEDIA_TYPE_VIDEO,
1861 .name = "gem",
1862 .long_name = NULL_IF_CONFIG_SMALL("GEM Raster image"),
1863 .props = AV_CODEC_PROP_LOSSY,
1864 },
1858 1865
1859 /* various PCM "codecs" */1866 /* various PCM "codecs" */
1860 {1867 {
libavcodec/codec_id.h+1−0
@@ -307,6 +307,7 @@ enum AVCodecID { · 306 unmodified lines
307 AV_CODEC_ID_CRI,307 AV_CODEC_ID_CRI,
308 AV_CODEC_ID_SIMBIOSIS_IMX,308 AV_CODEC_ID_SIMBIOSIS_IMX,
309 AV_CODEC_ID_SGA_VIDEO,309 AV_CODEC_ID_SGA_VIDEO,
310 AV_CODEC_ID_GEM,
310 311
311 /* various PCM "codecs" */312 /* various PCM "codecs" */
312 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs313 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
libavcodec/gemdec.c+343−0
@@ -0,0 +1,343 @@
1/*
2 * GEM Raster image decoder
3 * Copyright (c) 2021 Peter Ross (pross@xvid.org)
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * GEM Raster image decoder
25 */
26
27#include "avcodec.h"
28#include "bytestream.h"
29#include "internal.h"
30
31static const uint32_t gem_color_palette[16]={
32 0xFFFFFFFF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFF00,
33 0xFF0000FF, 0xFFFF00FF, 0xFF00FFFF, 0xFFAEAEAE,
34 0xFF555555, 0xFFAE0000, 0xFF00AE00, 0xFFAEAE00,
35 0xFF0000AE, 0xFFAE00AE, 0xFF00AEAE, 0xFF000000,
36};
37
38static const uint8_t gem_gray[256]={
39 0xFF, 0x7F, 0xBF, 0x3F, 0xDF, 0x5F, 0x9F, 0x1F, 0xEF, 0x6F, 0xAF, 0x2F, 0xCF, 0x4F, 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C, 0x7C, 0xBC, 0x3C, 0xDC, 0x5C, 0x9C, 0x1C, 0xEC, 0x6C, 0xAC, 0x2C, 0xCC, 0x4C, 0x8C, 0x0C,
52 0xF4, 0x74, 0xB4, 0x34, 0xD4, 0x54, 0x94, 0x14, 0xE4, 0x64, 0xA4, 0x24, 0xC4, 0x44, 0x84, 0x04,
53 0xF8, 0x78, 0xB8, 0x38, 0xD8, 0x58, 0x98, 0x18, 0xE8, 0x68, 0xA8, 0x28, 0xC8, 0x48, 0x88, 0x08,
54 0xF0, 0x70, 0xB0, 0x30, 0xD0, 0x50, 0x90, 0x10, 0xE0, 0x60, 0xA0, 0x20, 0xC0, 0x40, 0x80, 0x00,
55};
56
57typedef struct {
58 int y, pl, x, vdup;
59} State;
60
61static void put_lines_bits(AVCodecContext *avctx, int planes, int row_width, int pixel_size, State * state, uint8_t * row, AVFrame *p)
62{
63 int pl_byte = state->pl / 8;
64 int pl_bit = state->pl & 7;
65 for (int y = 0; y < state->vdup && (state->y + y) < avctx->height; y++)
66 for (int x = 0; x < row_width; x++)
67 for (int i = 7; i >= 0 && x * 8 + 7 - i < avctx->width; i--)
68 p->data[0][ (state->y + y) * p->linesize[0] + (x * 8 + 7 - i) * pixel_size + pl_byte] |= !!(row[x] & (1 << i)) << pl_bit;
69
70 state->pl++;
71 if (state->pl >= planes) {
72 state->pl = 0;
73 state->y += state->vdup;
74 state->vdup = 1;
75 }
76}
77
78static void put_lines_bytes(AVCodecContext *avctx, int planes, int row_width, int pixel_size, State * state, uint8_t * row, AVFrame *p)
79{
80 for (int y = 0; y < state->vdup && (state->y + y) < avctx->height; y++)
81 memcpy(p->data[0] + (state->y + y) * p->linesize[0], row, avctx->width * pixel_size);
82
83 state->y += state->vdup;
84 state->vdup = 1;
85}
86
87static int gem_decode_frame(AVCodecContext *avctx,
88 void *data, int *got_frame,
89 AVPacket *avpkt)
90{
91 const uint8_t *buf = avpkt->data;
92 int buf_size = avpkt->size;
93 const uint8_t *buf_end = buf + buf_size;
94 AVFrame *p = data;
95 int header_size, planes, pattern_size, tag = 0, count_scalar = 1, ret;
96 unsigned int x, count, v;
97 GetByteContext gb;
98 uint32_t *palette;
99 const uint8_t * b;
100 uint8_t * row;
101 int row_width, pixel_size;
102 State state = {.y = 0, .pl = 0, .x = 0, .vdup = 1};
103 void (*put_lines)(AVCodecContext *avctx, int planes, int row_width, int pixel_size, State * state, uint8_t * row, AVFrame *p);
104
105 if (buf_size <= 16)
106 return AVERROR_INVALIDDATA;
107
108 bytestream2_init(&gb, buf + 2, buf_size - 2);
109 header_size = bytestream2_get_be16(&gb);
110 if (header_size < 8 || buf_size <= header_size * 2)
111 return AVERROR_INVALIDDATA;
112
113 planes = bytestream2_get_be16(&gb);
114 pattern_size = bytestream2_get_be16(&gb);
115 avctx->sample_aspect_ratio.num = bytestream2_get_be16(&gb);
116 avctx->sample_aspect_ratio.den = bytestream2_get_be16(&gb);
117 avctx->width = bytestream2_get_be16(&gb);
118 avctx->height = bytestream2_get_be16(&gb);
119
120 row_width = (avctx->width + 7) / 8;
121 put_lines = put_lines_bits;
122
123 if (header_size == 9) {
124 count_scalar = bytestream2_get_be16(&gb);
125 if (count_scalar != 3) {
126 avpriv_request_sample(avctx, "count_scalar=%d", count_scalar);
127 return AVERROR_PATCHWELCOME;
128 }
129 planes = 24;
130 avctx->pix_fmt = AV_PIX_FMT_BGR24;
131 pixel_size = 3;
132 } else if (planes == 15) {
133#if HAVE_BIGENDIAN
134 avctx->pix_fmt = AV_PIX_FMT_BGR555BE;
135#else
136 avctx->pix_fmt = AV_PIX_FMT_BGR555LE;
137#endif
138 pixel_size = 2;
139 } else if (planes == 16) {
140 avctx->pix_fmt = AV_PIX_FMT_RGB565BE;
141 pixel_size = 2;
142 } else if (planes == 24) {
143 avctx->pix_fmt = AV_PIX_FMT_RGB24;
144 pixel_size = 3;
145 } else if (planes == 32) {
146 avctx->pix_fmt = AV_PIX_FMT_0RGB;
147 pixel_size = 4;
148 } else {
149 avctx->pix_fmt = AV_PIX_FMT_PAL8;
150 pixel_size = 1;
151 }
152
153 if (header_size >= 11)
154 tag = bytestream2_peek_be32(&gb);
155
156 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
157 return ret;
158
159 p->pict_type = AV_PICTURE_TYPE_I;
160 p->key_frame = 1;
161 p->palette_has_changed = 1;
162 palette = (uint32_t *)p->data[1];
163
164 if (tag == AV_RB32("STTT")) {
165 bytestream2_skip(&gb, 6);
166 if (planes == 4) {
167 for (int i = 0; i < (1 << planes); i++) {
168 int v = bytestream2_get_be16(&gb);
169 int r = ((v >> 8) & 0x7) << 5;
170 int g = ((v >> 4) & 0x7) << 5;
171 int b = ((v ) & 0x7) << 5;
172 palette[i] = 0xFF000000 | r << 16 | g << 8 | b;
173 }
174 } else {
175 avpriv_request_sample(avctx, "STTT planes=%d", planes);
176 return AVERROR_PATCHWELCOME;
177 }
178 } else if (tag == AV_RB32("TIMG")) {
179 bytestream2_skip(&gb, 4);
180 if (planes != 15) {
181 avpriv_request_sample(avctx, "TIMG planes=%d", planes);
182 return AVERROR_PATCHWELCOME;
183 }
184 } else if (tag == AV_RB32("XIMG")) {
185 bytestream2_skip(&gb, 6);
186 if (planes == 1 || planes == 2 || planes == 4 || planes == 8) {
187 for (int i = 0; i < (1 << planes); i++) {
188 int r = (bytestream2_get_be16(&gb) * 51 + 100) / 200;
189 int g = (bytestream2_get_be16(&gb) * 51 + 100) / 200;
190 int b = (bytestream2_get_be16(&gb) * 51 + 100) / 200;
191 palette[i] = 0xFF000000 | r << 16 | g << 8 | b;
192 }
193 } else if (planes == 16) {
194 planes = 1;
195 row_width = ((avctx->width + 7)/8)*8 * pixel_size;
196 put_lines = put_lines_bytes;
197 } else if (planes == 24) {
198 planes = 1;
199 row_width = ((avctx->width + 15)/16)*16 * pixel_size;
200 put_lines = put_lines_bytes;
201 } else if (planes == 32) {
202 planes = 1;
203 row_width = avctx->width * pixel_size;
204 put_lines = put_lines_bytes;
205 } else {
206 avpriv_request_sample(avctx, "XIMG planes=%d", planes);
207 return AVERROR_PATCHWELCOME;
208 }
209 } else if (planes == 1) {
210 palette[0] = 0xFFFFFFFF;
211 palette[1] = 0xFF000000;
212 } else if (planes == 2 || planes == 3 || planes == 4) {
213 if (header_size == 9 + (1 << planes)) {
214 bytestream2_skip(&gb, 2);
215 for (int i = 0; i < (1 << planes); i++) {
216 int v = bytestream2_get_be16(&gb);
217 int r = ((v >> 8) & 0x7) << 5;
218 int g = ((v >> 4) & 0x7) << 5;
219 int b = ((v ) & 0x7) << 5;
220 palette[i] = 0xFF000000 | r << 16 | g << 8 | b;
221 }
222 } else
223 memcpy(palette, gem_color_palette, sizeof(gem_color_palette));
224 } else if (planes == 8) {
225 for (int i = 0; i < 256; i++)
226 palette[i] = 0xFF000000 | (gem_gray[i]<<16) | (gem_gray[i]<<8) | gem_gray[i];
227 } else if (planes == 16) {
228 planes = 1;
229 row_width = avctx->width * pixel_size;
230 put_lines = put_lines_bytes;
231 } else if (planes == 24) {
232 planes = 1;
233 row_width = avctx->width * pixel_size;
234 put_lines = put_lines_bytes;
235 } else {
236 avpriv_request_sample(avctx, "planes=%d", planes);
237 return AVERROR_PATCHWELCOME;
238 }
239
240 ret = av_reallocp_array(&avctx->priv_data, planes, row_width);
241 if (ret < 0)
242 return ret;
243 row = avctx->priv_data;
244
245 memset(p->data[0], 0, avctx->height * p->linesize[0]);
246 b = buf + header_size * 2;
247 x = 0;
248
249#define SKIP \
250do { \
251 x++; \
252 if (x >= row_width) { \
253 put_lines(avctx, planes, row_width, pixel_size, &state, row + state.pl * row_width, p); \
254 if (state.y >= avctx->height) goto abort; \
255 x = 0; \
256 } \
257} while(0)
258
259#define PUT(v) \
260do { \
261 row[state.pl * row_width + x++] = v; \
262 if (x >= row_width) { \
263 put_lines(avctx, planes, row_width, pixel_size, &state, row + state.pl * row_width, p); \
264 if (state.y >= avctx->height) goto abort; \
265 x = 0; \
266 } \
267} while(0)
268
269 while(b < buf_end) {
270 int opcode = *b++;
271 if (opcode == 0x80) { /* copy */
272 if (b >= buf_end)
273 goto abort;
274 count = *b++;
275 if (!count)
276 count = 256;
277 count *= count_scalar;
278 for (int j = 0; j < count; j++) {
279 if (b >= buf_end)
280 goto abort;
281 PUT(*b++);
282 }
283 } else if (opcode) { /* run */
284 count = opcode & 0x7f;
285 if (!count)
286 count = 256;
287 count *= count_scalar;
288 v = opcode & 0x80 ? 0xFF : 0x00;
289 for (int i = 0; i < count; i++)
290 PUT(v);
291 } else {
292 if (b >= buf_end)
293 goto abort;
294 count = *b++;
295 if (count) { /* pattern */
296 if (b > buf_end - pattern_size)
297 goto abort;
298
299 count *= count_scalar;
300 for (int j = 0; j < count; j++)
301 for (int k = 0; k < pattern_size; k++)
302 PUT(b[k]);
303
304 b += pattern_size;
305 } else {
306 if (b >= buf_end)
307 goto abort;
308 count = *b++;
309 if (count == 0xFF) { /* vertical duplication */
310 if (b >= buf_end)
311 goto abort;
312 state.vdup = *b++;
313 if (!state.vdup)
314 state.vdup = 256;
315 } else { /* horizontal duplication */
316 for (int i = 0; i < count + 1; i++)
317 SKIP;
318 }
319 }
320 }
321 }
322
323abort:
324
325 *got_frame = 1;
326 return buf_size;
327}
328
329static av_cold int gem_close(AVCodecContext *avctx)
330{
331 av_freep(&avctx->priv_data);
332 return 0;
333}
334
335const AVCodec ff_gem_decoder = {
336 .name = "gem",
337 .long_name = NULL_IF_CONFIG_SMALL("GEM Raster image"),
338 .type = AVMEDIA_TYPE_VIDEO,
339 .id = AV_CODEC_ID_GEM,
340 .decode = gem_decode_frame,
341 .close = gem_close,
342 .capabilities = AV_CODEC_CAP_DR1,
343};
libavcodec/version.h+1−1
@@ -28,7 +28,7 @@ · 27 unmodified lines
28#include "libavutil/version.h"28#include "libavutil/version.h"
29 29
30#define LIBAVCODEC_VERSION_MAJOR 5930#define LIBAVCODEC_VERSION_MAJOR 59
31#define LIBAVCODEC_VERSION_MINOR 1031#define LIBAVCODEC_VERSION_MINOR 11
32#define LIBAVCODEC_VERSION_MICRO 10032#define LIBAVCODEC_VERSION_MICRO 100
33 33
34#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \34#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t