avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / qoienc.c
blobb9efdc2fa524bf67876a55029ee9593d525e0ba9
1 /*
2 * QOI image format encoder
4 * This file is part of FFmpeg.
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.
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.
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
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"
28 static int qoi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29 const AVFrame *pict, int *got_packet)
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;
37 const uint8_t *src;
38 int ret, run = 0;
40 packet_size = avctx->width * avctx->height * (channels + 1LL) + 14LL + 8LL;
41 if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
42 return ret;
44 buf = pkt->data;
45 src = pict->data[0];
47 bytestream_put_buffer(&buf, "qoif", 4);
48 bytestream_put_be32(&buf, avctx->width);
49 bytestream_put_be32(&buf, avctx->height);
50 bytestream_put_byte(&buf, channels);
51 bytestream_put_byte(&buf, avctx->color_trc == AVCOL_TRC_LINEAR);
53 for (int y = 0; y < avctx->height; y++) {
54 for (int x = 0; x < avctx->width; x++) {
55 memcpy(px, src + x * channels, channels);
57 if (!memcmp(px, px_prev, 4)) {
58 run++;
59 if (run == 62) {
60 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
61 run = 0;
63 } else {
64 int index_pos;
66 if (run > 0) {
67 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
68 run = 0;
71 index_pos = QOI_COLOR_HASH(px) & 63;
73 if (!memcmp(index[index_pos], px, 4)) {
74 bytestream_put_byte(&buf, QOI_OP_INDEX | index_pos);
75 } else {
76 memcpy(index[index_pos], px, 4);
78 if (px[3] == px_prev[3]) {
79 int8_t vr = px[0] - px_prev[0];
80 int8_t vg = px[1] - px_prev[1];
81 int8_t vb = px[2] - px_prev[2];
83 int8_t vg_r = vr - vg;
84 int8_t vg_b = vb - vg;
86 if (vr > -3 && vr < 2 &&
87 vg > -3 && vg < 2 &&
88 vb > -3 && vb < 2) {
89 bytestream_put_byte(&buf, QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2));
90 } else if (vg_r > -9 && vg_r < 8 &&
91 vg > -33 && vg < 32 &&
92 vg_b > -9 && vg_b < 8) {
93 bytestream_put_byte(&buf, QOI_OP_LUMA | (vg + 32));
94 bytestream_put_byte(&buf, (vg_r + 8) << 4 | (vg_b + 8));
95 } else {
96 bytestream_put_byte(&buf, QOI_OP_RGB);
97 bytestream_put_byte(&buf, px[0]);
98 bytestream_put_byte(&buf, px[1]);
99 bytestream_put_byte(&buf, px[2]);
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]);
111 memcpy(px_prev, px, 4);
114 src += pict->linesize[0];
117 if (run)
118 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
120 bytestream_put_be64(&buf, 0x01);
122 pkt->size = buf - pkt->data;
124 *got_packet = 1;
126 return 0;
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