/src/ffmpeg/libavcodec/qoidec.c
Line | Count | Source |
1 | | /* |
2 | | * QOI image format |
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 "avcodec.h" |
22 | | #include "bytestream.h" |
23 | | #include "codec_internal.h" |
24 | | #include "decode.h" |
25 | | #include "thread.h" |
26 | | #include "qoi.h" |
27 | | |
28 | | static int qoi_decode_frame(AVCodecContext *avctx, AVFrame *p, |
29 | | int *got_frame, AVPacket *avpkt) |
30 | 798k | { |
31 | 798k | int width, height, channels, space, run = 0; |
32 | 798k | uint8_t index[64][4] = { 0 }; |
33 | 798k | uint8_t px[4] = { 0, 0, 0, 255 }; |
34 | 798k | GetByteContext gb; |
35 | 798k | uint8_t *dst; |
36 | 798k | uint64_t len; |
37 | 798k | int ret; |
38 | | |
39 | 798k | if (avpkt->size < 20) |
40 | 290k | return AVERROR_INVALIDDATA; |
41 | | |
42 | 507k | bytestream2_init(&gb, avpkt->data, avpkt->size); |
43 | 507k | bytestream2_skip(&gb, 4); |
44 | 507k | width = bytestream2_get_be32(&gb); |
45 | 507k | height = bytestream2_get_be32(&gb); |
46 | 507k | channels = bytestream2_get_byte(&gb); |
47 | 507k | space = bytestream2_get_byte(&gb); |
48 | 507k | switch (space) { |
49 | 459k | case 0: break; |
50 | 11.8k | case 1: avctx->color_trc = AVCOL_TRC_LINEAR; break; |
51 | 36.9k | default: return AVERROR_INVALIDDATA; |
52 | 507k | } |
53 | | |
54 | 470k | if ((ret = ff_set_dimensions(avctx, width, height)) < 0) |
55 | 20.9k | return ret; |
56 | | |
57 | 449k | switch (channels) { |
58 | 7.86k | case 3: avctx->pix_fmt = AV_PIX_FMT_RGB24; break; |
59 | 405k | case 4: avctx->pix_fmt = AV_PIX_FMT_RGBA; break; |
60 | 36.6k | default: return AVERROR_INVALIDDATA; |
61 | 449k | } |
62 | | |
63 | 413k | if (avctx->skip_frame >= AVDISCARD_ALL) |
64 | 15.1k | return avpkt->size; |
65 | | |
66 | 398k | if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0) |
67 | 475 | return ret; |
68 | | |
69 | 397k | dst = p->data[0]; |
70 | 397k | len = width * height * channels; |
71 | 16.2M | for (int n = 0, off_x = 0; n < len; n += channels, off_x++) { |
72 | 16.2M | if (off_x >= width) { |
73 | 650k | off_x = 0; |
74 | 650k | dst += p->linesize[0]; |
75 | 650k | } |
76 | 16.2M | if (run > 0) { |
77 | 5.73M | run--; |
78 | 10.5M | } else if (bytestream2_get_bytes_left(&gb) > 4) { |
79 | 10.1M | int chunk = bytestream2_get_byteu(&gb); |
80 | | |
81 | 10.1M | if (chunk == QOI_OP_RGB) { |
82 | 789k | bytestream2_get_bufferu(&gb, px, 3); |
83 | 9.37M | } else if (chunk == QOI_OP_RGBA) { |
84 | 47.5k | bytestream2_get_bufferu(&gb, px, 4); |
85 | 9.32M | } else if ((chunk & QOI_MASK_2) == QOI_OP_INDEX) { |
86 | 6.58M | memcpy(px, index[chunk], 4); |
87 | 6.58M | } else if ((chunk & QOI_MASK_2) == QOI_OP_DIFF) { |
88 | 884k | px[0] += ((chunk >> 4) & 0x03) - 2; |
89 | 884k | px[1] += ((chunk >> 2) & 0x03) - 2; |
90 | 884k | px[2] += ( chunk & 0x03) - 2; |
91 | 1.85M | } else if ((chunk & QOI_MASK_2) == QOI_OP_LUMA) { |
92 | 1.64M | int b2 = bytestream2_get_byteu(&gb); |
93 | 1.64M | int vg = (chunk & 0x3f) - 32; |
94 | 1.64M | px[0] += vg - 8 + ((b2 >> 4) & 0x0f); |
95 | 1.64M | px[1] += vg; |
96 | 1.64M | px[2] += vg - 8 + (b2 & 0x0f); |
97 | 1.64M | } else if ((chunk & QOI_MASK_2) == QOI_OP_RUN) { |
98 | 209k | run = chunk & 0x3f; |
99 | 209k | } |
100 | | |
101 | 10.1M | memcpy(index[QOI_COLOR_HASH(px) & 63], px, 4); |
102 | 10.1M | } else { |
103 | 395k | break; |
104 | 395k | } |
105 | | |
106 | 15.8M | memcpy(&dst[off_x * channels], px, channels); |
107 | 15.8M | } |
108 | | |
109 | 397k | *got_frame = 1; |
110 | | |
111 | 397k | return avpkt->size; |
112 | 398k | } |
113 | | |
114 | | const FFCodec ff_qoi_decoder = { |
115 | | .p.name = "qoi", |
116 | | CODEC_LONG_NAME("QOI (Quite OK Image format) image"), |
117 | | .p.type = AVMEDIA_TYPE_VIDEO, |
118 | | .p.id = AV_CODEC_ID_QOI, |
119 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, |
120 | | .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, |
121 | | FF_CODEC_DECODE_CB(qoi_decode_frame), |
122 | | }; |