Line | Branch | Exec | 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 | 40 | static int qoi_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
29 | int *got_frame, AVPacket *avpkt) | ||
30 | { | ||
31 | 40 | int width, height, channels, space, run = 0; | |
32 | 40 | uint8_t index[64][4] = { 0 }; | |
33 | 40 | uint8_t px[4] = { 0, 0, 0, 255 }; | |
34 | GetByteContext gb; | ||
35 | uint8_t *dst; | ||
36 | uint64_t len; | ||
37 | int ret; | ||
38 | |||
39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (avpkt->size < 20) |
40 | ✗ | return AVERROR_INVALIDDATA; | |
41 | |||
42 | 40 | bytestream2_init(&gb, avpkt->data, avpkt->size); | |
43 | 40 | bytestream2_skip(&gb, 4); | |
44 | 40 | width = bytestream2_get_be32(&gb); | |
45 | 40 | height = bytestream2_get_be32(&gb); | |
46 | 40 | channels = bytestream2_get_byte(&gb); | |
47 | 40 | space = bytestream2_get_byte(&gb); | |
48 |
1/3✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
40 | switch (space) { |
49 | 40 | case 0: break; | |
50 | ✗ | case 1: avctx->color_trc = AVCOL_TRC_LINEAR; break; | |
51 | ✗ | default: return AVERROR_INVALIDDATA; | |
52 | } | ||
53 | |||
54 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
|
40 | if ((ret = ff_set_dimensions(avctx, width, height)) < 0) |
55 | ✗ | return ret; | |
56 | |||
57 |
1/3✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
40 | switch (channels) { |
58 | 40 | case 3: avctx->pix_fmt = AV_PIX_FMT_RGB24; break; | |
59 | ✗ | case 4: avctx->pix_fmt = AV_PIX_FMT_RGBA; break; | |
60 | ✗ | default: return AVERROR_INVALIDDATA; | |
61 | } | ||
62 | |||
63 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
|
40 | if (avctx->skip_frame >= AVDISCARD_ALL) |
64 | 2 | return avpkt->size; | |
65 | |||
66 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
|
38 | if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0) |
67 | ✗ | return ret; | |
68 | |||
69 | 38 | dst = p->data[0]; | |
70 | 38 | len = width * height * channels; | |
71 |
2/2✓ Branch 0 taken 3852288 times.
✓ Branch 1 taken 38 times.
|
3852326 | for (int n = 0, off_x = 0; n < len; n += channels, off_x++) { |
72 |
2/2✓ Branch 0 taken 10906 times.
✓ Branch 1 taken 3841382 times.
|
3852288 | if (off_x >= width) { |
73 | 10906 | off_x = 0; | |
74 | 10906 | dst += p->linesize[0]; | |
75 | } | ||
76 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3852284 times.
|
3852288 | if (run > 0) { |
77 | 4 | run--; | |
78 |
1/2✓ Branch 1 taken 3852284 times.
✗ Branch 2 not taken.
|
3852284 | } else if (bytestream2_get_bytes_left(&gb) > 4) { |
79 | 3852284 | int chunk = bytestream2_get_byteu(&gb); | |
80 | |||
81 |
2/2✓ Branch 0 taken 2007173 times.
✓ Branch 1 taken 1845111 times.
|
3852284 | if (chunk == QOI_OP_RGB) { |
82 | 2007173 | bytestream2_get_bufferu(&gb, px, 3); | |
83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1845111 times.
|
1845111 | } else if (chunk == QOI_OP_RGBA) { |
84 | ✗ | bytestream2_get_bufferu(&gb, px, 4); | |
85 |
2/2✓ Branch 0 taken 12925 times.
✓ Branch 1 taken 1832186 times.
|
1845111 | } else if ((chunk & QOI_MASK_2) == QOI_OP_INDEX) { |
86 | 12925 | memcpy(px, index[chunk], 4); | |
87 |
2/2✓ Branch 0 taken 19701 times.
✓ Branch 1 taken 1812485 times.
|
1832186 | } else if ((chunk & QOI_MASK_2) == QOI_OP_DIFF) { |
88 | 19701 | px[0] += ((chunk >> 4) & 0x03) - 2; | |
89 | 19701 | px[1] += ((chunk >> 2) & 0x03) - 2; | |
90 | 19701 | px[2] += ( chunk & 0x03) - 2; | |
91 |
2/2✓ Branch 0 taken 1802436 times.
✓ Branch 1 taken 10049 times.
|
1812485 | } else if ((chunk & QOI_MASK_2) == QOI_OP_LUMA) { |
92 | 1802436 | int b2 = bytestream2_get_byteu(&gb); | |
93 | 1802436 | int vg = (chunk & 0x3f) - 32; | |
94 | 1802436 | px[0] += vg - 8 + ((b2 >> 4) & 0x0f); | |
95 | 1802436 | px[1] += vg; | |
96 | 1802436 | px[2] += vg - 8 + (b2 & 0x0f); | |
97 |
1/2✓ Branch 0 taken 10049 times.
✗ Branch 1 not taken.
|
10049 | } else if ((chunk & QOI_MASK_2) == QOI_OP_RUN) { |
98 | 10049 | run = chunk & 0x3f; | |
99 | } | ||
100 | |||
101 | 3852284 | memcpy(index[QOI_COLOR_HASH(px) & 63], px, 4); | |
102 | } else { | ||
103 | ✗ | break; | |
104 | } | ||
105 | |||
106 | 3852288 | memcpy(&dst[off_x * channels], px, channels); | |
107 | } | ||
108 | |||
109 | 38 | *got_frame = 1; | |
110 | |||
111 | 38 | return avpkt->size; | |
112 | } | ||
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 | }; | ||
123 |