FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/qoienc.c
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 54 63 85.7%
Functions: 1 1 100.0%
Branches: 39 44 88.6%

Line Branch Exec Source
1 /*
2 * QOI image format encoder
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/imgutils.h"
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
25 #include "encode.h"
26 #include "qoi.h"
27
28 38 static int qoi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29 const AVFrame *pict, int *got_packet)
30 {
31
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 const int channels = 3 + (avctx->pix_fmt == AV_PIX_FMT_RGBA);
32 38 uint8_t px_prev[4] = { 0, 0, 0, 255 };
33 38 uint8_t px[4] = { 0, 0, 0, 255 };
34 38 uint8_t index[64][4] = { 0 };
35 int64_t packet_size;
36 uint8_t *buf;
37 const uint8_t *src;
38 38 int ret, run = 0;
39
40 38 packet_size = avctx->width * avctx->height * (channels + 1LL) + 14LL + 8LL;
41
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
38 if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
42 return ret;
43
44 38 buf = pkt->data;
45 38 src = pict->data[0];
46
47 38 bytestream_put_buffer(&buf, "qoif", 4);
48 38 bytestream_put_be32(&buf, avctx->width);
49 38 bytestream_put_be32(&buf, avctx->height);
50 38 bytestream_put_byte(&buf, channels);
51 38 bytestream_put_byte(&buf, avctx->color_trc == AVCOL_TRC_LINEAR);
52
53
2/2
✓ Branch 0 taken 10944 times.
✓ Branch 1 taken 38 times.
10982 for (int y = 0; y < avctx->height; y++) {
54
2/2
✓ Branch 0 taken 3852288 times.
✓ Branch 1 taken 10944 times.
3863232 for (int x = 0; x < avctx->width; x++) {
55 3852288 memcpy(px, src + x * channels, channels);
56
57
2/2
✓ Branch 0 taken 10053 times.
✓ Branch 1 taken 3842235 times.
3852288 if (!memcmp(px, px_prev, 4)) {
58 10053 run++;
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10053 times.
10053 if (run == 62) {
60 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
61 run = 0;
62 }
63 } else {
64 int index_pos;
65
66
2/2
✓ Branch 0 taken 10049 times.
✓ Branch 1 taken 3832186 times.
3842235 if (run > 0) {
67 10049 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
68 10049 run = 0;
69 }
70
71 3842235 index_pos = QOI_COLOR_HASH(px) & 63;
72
73
2/2
✓ Branch 0 taken 12925 times.
✓ Branch 1 taken 3829310 times.
3842235 if (!memcmp(index[index_pos], px, 4)) {
74 12925 bytestream_put_byte(&buf, QOI_OP_INDEX | index_pos);
75 } else {
76 3829310 memcpy(index[index_pos], px, 4);
77
78
1/2
✓ Branch 0 taken 3829310 times.
✗ Branch 1 not taken.
3829310 if (px[3] == px_prev[3]) {
79 3829310 int8_t vr = px[0] - px_prev[0];
80 3829310 int8_t vg = px[1] - px_prev[1];
81 3829310 int8_t vb = px[2] - px_prev[2];
82
83 3829310 int8_t vg_r = vr - vg;
84 3829310 int8_t vg_b = vb - vg;
85
86
6/6
✓ Branch 0 taken 2312114 times.
✓ Branch 1 taken 1517196 times.
✓ Branch 2 taken 328798 times.
✓ Branch 3 taken 1983316 times.
✓ Branch 4 taken 298005 times.
✓ Branch 5 taken 30793 times.
3829310 if (vr > -3 && vr < 2 &&
87
4/4
✓ Branch 0 taken 30487 times.
✓ Branch 1 taken 267518 times.
✓ Branch 2 taken 27481 times.
✓ Branch 3 taken 3006 times.
298005 vg > -3 && vg < 2 &&
88
2/2
✓ Branch 0 taken 19701 times.
✓ Branch 1 taken 7780 times.
27481 vb > -3 && vb < 2) {
89 19701 bytestream_put_byte(&buf, QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2));
90
6/6
✓ Branch 0 taken 2360403 times.
✓ Branch 1 taken 1449206 times.
✓ Branch 2 taken 1984731 times.
✓ Branch 3 taken 375672 times.
✓ Branch 4 taken 1952419 times.
✓ Branch 5 taken 32312 times.
3809609 } else if (vg_r > -9 && vg_r < 8 &&
91
4/4
✓ Branch 0 taken 1898630 times.
✓ Branch 1 taken 53789 times.
✓ Branch 2 taken 1854029 times.
✓ Branch 3 taken 44601 times.
1952419 vg > -33 && vg < 32 &&
92
2/2
✓ Branch 0 taken 1802436 times.
✓ Branch 1 taken 51593 times.
1854029 vg_b > -9 && vg_b < 8) {
93 1802436 bytestream_put_byte(&buf, QOI_OP_LUMA | (vg + 32));
94 1802436 bytestream_put_byte(&buf, (vg_r + 8) << 4 | (vg_b + 8));
95 } else {
96 2007173 bytestream_put_byte(&buf, QOI_OP_RGB);
97 2007173 bytestream_put_byte(&buf, px[0]);
98 2007173 bytestream_put_byte(&buf, px[1]);
99 2007173 bytestream_put_byte(&buf, px[2]);
100 }
101 } else {
102 bytestream_put_byte(&buf, QOI_OP_RGBA);
103 bytestream_put_byte(&buf, px[0]);
104 bytestream_put_byte(&buf, px[1]);
105 bytestream_put_byte(&buf, px[2]);
106 bytestream_put_byte(&buf, px[3]);
107 }
108 }
109 }
110
111 3852288 memcpy(px_prev, px, 4);
112 }
113
114 10944 src += pict->linesize[0];
115 }
116
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (run)
118 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
119
120 38 bytestream_put_be64(&buf, 0x01);
121
122 38 pkt->size = buf - pkt->data;
123
124 38 *got_packet = 1;
125
126 38 return 0;
127 }
128
129 const FFCodec ff_qoi_encoder = {
130 .p.name = "qoi",
131 CODEC_LONG_NAME("QOI (Quite OK Image format) image"),
132 .p.type = AVMEDIA_TYPE_VIDEO,
133 .p.id = AV_CODEC_ID_QOI,
134 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
135 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
136 FF_CODEC_ENCODE_CB(qoi_encode_frame),
137 .p.pix_fmts = (const enum AVPixelFormat[]){
138 AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24,
139 AV_PIX_FMT_NONE
140 },
141 };
142