FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/qoienc.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 54 63 85.7%
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 13 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 13 times.
13 const int channels = 3 + (avctx->pix_fmt == AV_PIX_FMT_RGBA);
32 13 uint8_t px_prev[4] = { 0, 0, 0, 255 };
33 13 uint8_t px[4] = { 0, 0, 0, 255 };
34 13 uint8_t index[64][4] = { 0 };
35 int64_t packet_size;
36 uint8_t *buf, *src;
37 13 int ret, run = 0;
38
39 13 packet_size = avctx->width * avctx->height * (channels + 1LL) + 14LL + 8LL;
40
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
41 return ret;
42
43 13 buf = pkt->data;
44 13 src = pict->data[0];
45
46 13 bytestream_put_buffer(&buf, "qoif", 4);
47 13 bytestream_put_be32(&buf, avctx->width);
48 13 bytestream_put_be32(&buf, avctx->height);
49 13 bytestream_put_byte(&buf, channels);
50 13 bytestream_put_byte(&buf, avctx->color_trc == AVCOL_TRC_LINEAR);
51
52
2/2
✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 13 times.
3757 for (int y = 0; y < avctx->height; y++) {
53
2/2
✓ Branch 0 taken 1317888 times.
✓ Branch 1 taken 3744 times.
1321632 for (int x = 0; x < avctx->width; x++) {
54 1317888 memcpy(px, src + x * channels, channels);
55
56
2/2
✓ Branch 0 taken 3467 times.
✓ Branch 1 taken 1314421 times.
1317888 if (!memcmp(px, px_prev, 4)) {
57 3467 run++;
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3467 times.
3467 if (run == 62) {
59 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
60 run = 0;
61 }
62 } else {
63 int index_pos;
64
65
2/2
✓ Branch 0 taken 3466 times.
✓ Branch 1 taken 1310955 times.
1314421 if (run > 0) {
66 3466 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
67 3466 run = 0;
68 }
69
70 1314421 index_pos = QOI_COLOR_HASH(px) & 63;
71
72
2/2
✓ Branch 0 taken 4344 times.
✓ Branch 1 taken 1310077 times.
1314421 if (!memcmp(index[index_pos], px, 4)) {
73 4344 bytestream_put_byte(&buf, QOI_OP_INDEX | index_pos);
74 } else {
75 1310077 memcpy(index[index_pos], px, 4);
76
77
1/2
✓ Branch 0 taken 1310077 times.
✗ Branch 1 not taken.
1310077 if (px[3] == px_prev[3]) {
78 1310077 int8_t vr = px[0] - px_prev[0];
79 1310077 int8_t vg = px[1] - px_prev[1];
80 1310077 int8_t vb = px[2] - px_prev[2];
81
82 1310077 int8_t vg_r = vr - vg;
83 1310077 int8_t vg_b = vb - vg;
84
85
6/6
✓ Branch 0 taken 791027 times.
✓ Branch 1 taken 519050 times.
✓ Branch 2 taken 113068 times.
✓ Branch 3 taken 677959 times.
✓ Branch 4 taken 102561 times.
✓ Branch 5 taken 10507 times.
1310077 if (vr > -3 && vr < 2 &&
86
4/4
✓ Branch 0 taken 10498 times.
✓ Branch 1 taken 92063 times.
✓ Branch 2 taken 9466 times.
✓ Branch 3 taken 1032 times.
102561 vg > -3 && vg < 2 &&
87
2/2
✓ Branch 0 taken 6774 times.
✓ Branch 1 taken 2692 times.
9466 vb > -3 && vb < 2) {
88 6774 bytestream_put_byte(&buf, QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2));
89
6/6
✓ Branch 0 taken 807761 times.
✓ Branch 1 taken 495542 times.
✓ Branch 2 taken 679014 times.
✓ Branch 3 taken 128747 times.
✓ Branch 4 taken 667947 times.
✓ Branch 5 taken 11067 times.
1303303 } else if (vg_r > -9 && vg_r < 8 &&
90
4/4
✓ Branch 0 taken 649620 times.
✓ Branch 1 taken 18327 times.
✓ Branch 2 taken 634110 times.
✓ Branch 3 taken 15510 times.
667947 vg > -33 && vg < 32 &&
91
2/2
✓ Branch 0 taken 616207 times.
✓ Branch 1 taken 17903 times.
634110 vg_b > -9 && vg_b < 8) {
92 616207 bytestream_put_byte(&buf, QOI_OP_LUMA | (vg + 32));
93 616207 bytestream_put_byte(&buf, (vg_r + 8) << 4 | (vg_b + 8));
94 } else {
95 687096 bytestream_put_byte(&buf, QOI_OP_RGB);
96 687096 bytestream_put_byte(&buf, px[0]);
97 687096 bytestream_put_byte(&buf, px[1]);
98 687096 bytestream_put_byte(&buf, px[2]);
99 }
100 } else {
101 bytestream_put_byte(&buf, QOI_OP_RGBA);
102 bytestream_put_byte(&buf, px[0]);
103 bytestream_put_byte(&buf, px[1]);
104 bytestream_put_byte(&buf, px[2]);
105 bytestream_put_byte(&buf, px[3]);
106 }
107 }
108 }
109
110 1317888 memcpy(px_prev, px, 4);
111 }
112
113 3744 src += pict->linesize[0];
114 }
115
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (run)
117 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
118
119 13 bytestream_put_be64(&buf, 0x01);
120
121 13 pkt->size = buf - pkt->data;
122
123 13 *got_packet = 1;
124
125 13 return 0;
126 }
127
128 const FFCodec ff_qoi_encoder = {
129 .p.name = "qoi",
130 .p.long_name = NULL_IF_CONFIG_SMALL("QOI (Quite OK Image format) image"),
131 .p.type = AVMEDIA_TYPE_VIDEO,
132 .p.id = AV_CODEC_ID_QOI,
133 .p.capabilities = AV_CODEC_CAP_FRAME_THREADS,
134 FF_CODEC_ENCODE_CB(qoi_encode_frame),
135 .p.pix_fmts = (const enum AVPixelFormat[]){
136 AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24,
137 AV_PIX_FMT_NONE
138 },
139 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
140 };
141