FFmpeg
Loading...
Searching...
No Matches
qoienc.c
Go to the documentation of this file.
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
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;
37 const uint8_t *src;
38 int ret, run = 0;
39
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;
43
44 buf = pkt->data;
45 src = pict->data[0];
46
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);
52
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);
56
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;
62 }
63 } else {
64 int index_pos;
65
66 if (run > 0) {
67 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
68 run = 0;
69 }
70
71 index_pos = QOI_COLOR_HASH(px) & 63;
72
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);
77
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];
82
83 int8_t vg_r = vr - vg;
84 int8_t vg_b = vb - vg;
85
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]);
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 memcpy(px_prev, px, 4);
112 }
113
114 src += pict->linesize[0];
115 }
116
117 if (run)
118 bytestream_put_byte(&buf, QOI_OP_RUN | (run - 1));
119
120 bytestream_put_be64(&buf, 0x01);
121
122 pkt->size = buf - pkt->data;
123
124 *got_packet = 1;
125
126 return 0;
127}
128
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 |
138};
const FFCodec ff_qoi_encoder
Definition qoienc.c:129
channels
Definition aptx.h:31
Libavcodec external API header.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition bytestream.h:372
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_QOI
Definition codec_id.h:309
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int index
Definition gxfenc.c:90
misc image utilities
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition pixfmt.h:681
#define QOI_OP_RGB
Definition qoi.h:28
#define QOI_OP_INDEX
Definition qoi.h:24
#define QOI_OP_RGBA
Definition qoi.h:29
#define QOI_OP_LUMA
Definition qoi.h:26
#define QOI_OP_DIFF
Definition qoi.h:25
#define QOI_OP_RUN
Definition qoi.h:27
#define QOI_COLOR_HASH(px)
Definition qoi.h:33
static int qoi_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition qoienc.c:28
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
This structure stores compressed data.
Definition packet.h:580
uint8_t run
Definition svq3.c:207
#define src
Definition vp8dsp.c:248