FFmpeg
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 |
137  .p.pix_fmts = (const enum AVPixelFormat[]){
140  },
141 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
QOI_OP_LUMA
#define QOI_OP_LUMA
Definition: qoi.h:26
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:567
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:995
AVPacket::data
uint8_t * data
Definition: packet.h:374
encode.h
FFCodec
Definition: codec_internal.h:127
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
QOI_OP_RUN
#define QOI_OP_RUN
Definition: qoi.h:27
QOI_OP_INDEX
#define QOI_OP_INDEX
Definition: qoi.h:24
qoi_encode_frame
static int qoi_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: qoienc.c:28
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:315
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#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:156
channels
channels
Definition: aptx.h:31
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:107
run
uint8_t run
Definition: svq3.c:203
qoi.h
index
int index
Definition: gxfenc.c:89
ff_qoi_encoder
const FFCodec ff_qoi_encoder
Definition: qoienc.c:129
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
codec_internal.h
AV_CODEC_ID_QOI
@ AV_CODEC_ID_QOI
Definition: codec_id.h:317
QOI_OP_RGBA
#define QOI_OP_RGBA
Definition: qoi.h:29
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
AVCodecContext::height
int height
Definition: avcodec.h:598
QOI_OP_RGB
#define QOI_OP_RGB
Definition: qoi.h:28
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:635
avcodec.h
ret
ret
Definition: filter_design.txt:187
QOI_COLOR_HASH
#define QOI_COLOR_HASH(px)
Definition: qoi.h:33
AVCodecContext
main external API structure.
Definition: avcodec.h:426
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
QOI_OP_DIFF
#define QOI_OP_DIFF
Definition: qoi.h:25
AVPacket
This structure stores compressed data.
Definition: packet.h:351
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:598
bytestream.h
imgutils.h
AVFrame::linesize
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:375
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:35