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 
qoi_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)28 static int qoi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29                             const AVFrame *pict, int *got_packet)
30 {
31     const int channels = 3 + (avctx->pix_fmt == AV_PIX_FMT_RGBA);
32     uint8_t px_prev[4] = { 0, 0, 0, 255 };
33     uint8_t px[4] = { 0, 0, 0, 255 };
34     uint8_t index[64][4] = { 0 };
35     int64_t packet_size;
36     uint8_t *buf, *src;
37     int ret, run = 0;
38 
39     packet_size = avctx->width * avctx->height * (channels + 1LL) + 14LL + 8LL;
40     if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
41         return ret;
42 
43     buf = pkt->data;
44     src = pict->data[0];
45 
46     bytestream_put_buffer(&buf, "qoif", 4);
47     bytestream_put_be32(&buf, avctx->width);
48     bytestream_put_be32(&buf, avctx->height);
49     bytestream_put_byte(&buf, channels);
50     bytestream_put_byte(&buf, avctx->color_trc == AVCOL_TRC_LINEAR);
51 
52     for (int y = 0; y < avctx->height; y++) {
53         for (int x = 0; x < avctx->width; x++) {
54             memcpy(px, src + x * channels, channels);
55 
56             if (!memcmp(px, px_prev, 4)) {
57                 run++;
58                 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                 if (run > 0) {
66                     bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
67                     run = 0;
68                 }
69 
70                 index_pos = QOI_COLOR_HASH(px) & 63;
71 
72                 if (!memcmp(index[index_pos], px, 4)) {
73                     bytestream_put_byte(&buf, QOI_OP_INDEX | index_pos);
74                 } else {
75                     memcpy(index[index_pos], px, 4);
76 
77                     if (px[3] == px_prev[3]) {
78                         int8_t vr = px[0] - px_prev[0];
79                         int8_t vg = px[1] - px_prev[1];
80                         int8_t vb = px[2] - px_prev[2];
81 
82                         int8_t vg_r = vr - vg;
83                         int8_t vg_b = vb - vg;
84 
85                         if (vr > -3 && vr < 2 &&
86                             vg > -3 && vg < 2 &&
87                             vb > -3 && vb < 2) {
88                             bytestream_put_byte(&buf, QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2));
89                         } else if (vg_r >  -9 && vg_r <  8 &&
90                                    vg   > -33 && vg   < 32 &&
91                                    vg_b >  -9 && vg_b <  8) {
92                             bytestream_put_byte(&buf, QOI_OP_LUMA     | (vg   + 32));
93                             bytestream_put_byte(&buf, (vg_r + 8) << 4 | (vg_b +  8));
94                         } else {
95                             bytestream_put_byte(&buf, QOI_OP_RGB);
96                             bytestream_put_byte(&buf, px[0]);
97                             bytestream_put_byte(&buf, px[1]);
98                             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             memcpy(px_prev, px, 4);
111         }
112 
113         src += pict->linesize[0];
114     }
115 
116     if (run)
117         bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
118 
119     bytestream_put_be64(&buf, 0x01);
120 
121     pkt->size = buf - pkt->data;
122 
123     *got_packet = 1;
124 
125     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