aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/qoienc.c
blob: 6d574e0da9e0fe6885367a12f8d6db28f59f585d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * QOI image format encoder
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/imgutils.h"
#include "avcodec.h"
#include "bytestream.h"
#include "codec_internal.h"
#include "encode.h"
#include "qoi.h"

static int qoi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                            const AVFrame *pict, int *got_packet)
{
    const int channels = 3 + (avctx->pix_fmt == AV_PIX_FMT_RGBA);
    uint8_t px_prev[4] = { 0, 0, 0, 255 };
    uint8_t px[4] = { 0, 0, 0, 255 };
    uint8_t index[64][4] = { 0 };
    int64_t packet_size;
    uint8_t *buf;
    const uint8_t *src;
    int ret, run = 0;

    packet_size = avctx->width * avctx->height * (channels + 1LL) + 14LL + 8LL;
    if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
        return ret;

    buf = pkt->data;
    src = pict->data[0];

    bytestream_put_buffer(&buf, "qoif", 4);
    bytestream_put_be32(&buf, avctx->width);
    bytestream_put_be32(&buf, avctx->height);
    bytestream_put_byte(&buf, channels);
    bytestream_put_byte(&buf, avctx->color_trc == AVCOL_TRC_LINEAR);

    for (int y = 0; y < avctx->height; y++) {
        for (int x = 0; x < avctx->width; x++) {
            memcpy(px, src + x * channels, channels);

            if (!memcmp(px, px_prev, 4)) {
                run++;
                if (run == 62) {
                    bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
                    run = 0;
                }
            } else {
                int index_pos;

                if (run > 0) {
                    bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
                    run = 0;
                }

                index_pos = QOI_COLOR_HASH(px) & 63;

                if (!memcmp(index[index_pos], px, 4)) {
                    bytestream_put_byte(&buf, QOI_OP_INDEX | index_pos);
                } else {
                    memcpy(index[index_pos], px, 4);

                    if (px[3] == px_prev[3]) {
                        int8_t vr = px[0] - px_prev[0];
                        int8_t vg = px[1] - px_prev[1];
                        int8_t vb = px[2] - px_prev[2];

                        int8_t vg_r = vr - vg;
                        int8_t vg_b = vb - vg;

                        if (vr > -3 && vr < 2 &&
                            vg > -3 && vg < 2 &&
                            vb > -3 && vb < 2) {
                            bytestream_put_byte(&buf, QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2));
                        } else if (vg_r >  -9 && vg_r <  8 &&
                                   vg   > -33 && vg   < 32 &&
                                   vg_b >  -9 && vg_b <  8) {
                            bytestream_put_byte(&buf, QOI_OP_LUMA     | (vg   + 32));
                            bytestream_put_byte(&buf, (vg_r + 8) << 4 | (vg_b +  8));
                        } else {
                            bytestream_put_byte(&buf, QOI_OP_RGB);
                            bytestream_put_byte(&buf, px[0]);
                            bytestream_put_byte(&buf, px[1]);
                            bytestream_put_byte(&buf, px[2]);
                        }
                    } else {
                        bytestream_put_byte(&buf, QOI_OP_RGBA);
                        bytestream_put_byte(&buf, px[0]);
                        bytestream_put_byte(&buf, px[1]);
                        bytestream_put_byte(&buf, px[2]);
                        bytestream_put_byte(&buf, px[3]);
                    }
                }
            }

            memcpy(px_prev, px, 4);
        }

        src += pict->linesize[0];
    }

    if (run)
        bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));

    bytestream_put_be64(&buf, 0x01);

    pkt->size = buf - pkt->data;

    *got_packet = 1;

    return 0;
}

const FFCodec ff_qoi_encoder = {
    .p.name         = "qoi",
    CODEC_LONG_NAME("QOI (Quite OK Image format) image"),
    .p.type         = AVMEDIA_TYPE_VIDEO,
    .p.id           = AV_CODEC_ID_QOI,
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
    FF_CODEC_ENCODE_CB(qoi_encode_frame),
    .p.pix_fmts     = (const enum AVPixelFormat[]){
        AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24,
        AV_PIX_FMT_NONE
    },
};