FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/qoidec.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 52 61 85.2%
Branches: 22 32 68.8%

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 <stdlib.h>
22
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "internal.h"
26 #include "bytestream.h"
27 #include "codec_internal.h"
28 #include "thread.h"
29 #include "qoi.h"
30
31 14 static int qoi_decode_frame(AVCodecContext *avctx, AVFrame *p,
32 int *got_frame, AVPacket *avpkt)
33 {
34 14 const uint8_t *buf = avpkt->data;
35 14 int ret, buf_size = avpkt->size;
36 14 int width, height, channels, space, run = 0;
37 14 uint8_t index[64][4] = { 0 };
38 14 uint8_t px[4] = { 0, 0, 0, 255 };
39 GetByteContext gb;
40 uint8_t *dst;
41 uint64_t len;
42
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (buf_size < 20)
44 return AVERROR_INVALIDDATA;
45
46 14 bytestream2_init(&gb, buf, buf_size);
47 14 bytestream2_skip(&gb, 4);
48 14 width = bytestream2_get_be32(&gb);
49 14 height = bytestream2_get_be32(&gb);
50 14 channels = bytestream2_get_byte(&gb);
51 14 space = bytestream2_get_byte(&gb);
52
1/3
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
14 switch (space) {
53 14 case 0: break;
54 case 1: avctx->color_trc = AVCOL_TRC_LINEAR; break;
55 default: return AVERROR_INVALIDDATA;
56 }
57
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
59 return ret;
60
61
1/3
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
14 switch (channels) {
62 14 case 3: avctx->pix_fmt = AV_PIX_FMT_RGB24; break;
63 case 4: avctx->pix_fmt = AV_PIX_FMT_RGBA; break;
64 default: return AVERROR_INVALIDDATA;
65 }
66
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
68 return ret;
69
70 14 dst = p->data[0];
71 14 len = width * height * channels;
72
2/2
✓ Branch 0 taken 1419264 times.
✓ Branch 1 taken 14 times.
1419278 for (int n = 0, off_x = 0; n < len; n += channels, off_x++) {
73
2/2
✓ Branch 0 taken 4018 times.
✓ Branch 1 taken 1415246 times.
1419264 if (off_x >= width) {
74 4018 off_x = 0;
75 4018 dst += p->linesize[0];
76 }
77
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1419263 times.
1419264 if (run > 0) {
78 1 run--;
79
1/2
✓ Branch 1 taken 1419263 times.
✗ Branch 2 not taken.
1419263 } else if (bytestream2_get_bytes_left(&gb) > 4) {
80 1419263 int chunk = bytestream2_get_byteu(&gb);
81
82
2/2
✓ Branch 0 taken 740019 times.
✓ Branch 1 taken 679244 times.
1419263 if (chunk == QOI_OP_RGB) {
83 740019 bytestream2_get_bufferu(&gb, px, 3);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 679244 times.
679244 } else if (chunk == QOI_OP_RGBA) {
85 bytestream2_get_bufferu(&gb, px, 4);
86
2/2
✓ Branch 0 taken 4691 times.
✓ Branch 1 taken 674553 times.
679244 } else if ((chunk & QOI_MASK_2) == QOI_OP_INDEX) {
87 4691 memcpy(px, index[chunk], 4);
88
2/2
✓ Branch 0 taken 7255 times.
✓ Branch 1 taken 667298 times.
674553 } else if ((chunk & QOI_MASK_2) == QOI_OP_DIFF) {
89 7255 px[0] += ((chunk >> 4) & 0x03) - 2;
90 7255 px[1] += ((chunk >> 2) & 0x03) - 2;
91 7255 px[2] += ( chunk & 0x03) - 2;
92
2/2
✓ Branch 0 taken 663569 times.
✓ Branch 1 taken 3729 times.
667298 } else if ((chunk & QOI_MASK_2) == QOI_OP_LUMA) {
93 663569 int b2 = bytestream2_get_byteu(&gb);
94 663569 int vg = (chunk & 0x3f) - 32;
95 663569 px[0] += vg - 8 + ((b2 >> 4) & 0x0f);
96 663569 px[1] += vg;
97 663569 px[2] += vg - 8 + (b2 & 0x0f);
98
1/2
✓ Branch 0 taken 3729 times.
✗ Branch 1 not taken.
3729 } else if ((chunk & QOI_MASK_2) == QOI_OP_RUN) {
99 3729 run = chunk & 0x3f;
100 }
101
102 1419263 memcpy(index[QOI_COLOR_HASH(px) & 63], px, 4);
103 } else {
104 break;
105 }
106
107 1419264 memcpy(&dst[off_x * channels], px, channels);
108 }
109
110 14 p->key_frame = 1;
111 14 p->pict_type = AV_PICTURE_TYPE_I;
112
113 14 *got_frame = 1;
114
115 14 return buf_size;
116 }
117
118 const FFCodec ff_qoi_decoder = {
119 .p.name = "qoi",
120 .p.long_name = NULL_IF_CONFIG_SMALL("QOI (Quite OK Image format) image"),
121 .p.type = AVMEDIA_TYPE_VIDEO,
122 .p.id = AV_CODEC_ID_QOI,
123 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
124 FF_CODEC_DECODE_CB(qoi_decode_frame),
125 };
126