FFmpeg
Loading...
Searching...
No Matches
qtrleenc.c
Go to the documentation of this file.
1/*
2 * Quicktime Animation (RLE) Video Encoder
3 * Copyright (C) 2007 Clemens Fruhwirth
4 * Copyright (C) 2007 Alexis Ballier
5 *
6 * This file is based on flashsvenc.c.
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include "libavutil/imgutils.h"
26#include "libavutil/mem.h"
27#include "avcodec.h"
28#include "bytestream.h"
29#include "codec_internal.h"
30#include "encode.h"
31
32/** Maximum RLE code for bulk copy */
33#define MAX_RLE_BULK 127
34/** Maximum RLE code for repeat */
35#define MAX_RLE_REPEAT 128
36/** Maximum RLE code for skip */
37#define MAX_RLE_SKIP 254
38
39typedef struct QtrleEncContext {
43 unsigned int max_buf_size;
45 /**
46 * This array will contain at ith position the value of the best RLE code
47 * if the line started at pixel i
48 * There can be 3 values :
49 * skip (0) : skip as much as possible pixels because they are equal to the
50 * previous frame ones
51 * repeat (<-1) : repeat that pixel -rle_code times, still as much as
52 * possible
53 * copy (>0) : copy the raw next rle_code pixels */
54 signed char *rlecode_table;
55 /**
56 * This array will contain the length of the best rle encoding of the line
57 * starting at ith pixel */
59 /**
60 * Will contain at ith position the number of consecutive pixels equal to the previous
61 * frame starting from pixel i */
62 uint8_t* skip_table;
63
64 /** Encoded frame is a key frame */
67
69{
70 QtrleEncContext *s = avctx->priv_data;
71
72 av_frame_free(&s->previous_frame);
73 av_free(s->rlecode_table);
74 av_free(s->length_table);
75 av_free(s->skip_table);
76 return 0;
77}
78
80{
81 QtrleEncContext *s = avctx->priv_data;
82
83 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
84 return AVERROR(EINVAL);
85 }
86 s->avctx=avctx;
87 s->logical_width=avctx->width;
88
89 switch (avctx->pix_fmt) {
91 if (avctx->width % 4) {
92 av_log(avctx, AV_LOG_ERROR, "Width not being a multiple of 4 is not supported\n");
93 return AVERROR(EINVAL);
94 }
95 s->logical_width = avctx->width / 4;
96 s->pixel_size = 4;
97 break;
99 s->pixel_size = 2;
100 break;
101 case AV_PIX_FMT_RGB24:
102 s->pixel_size = 3;
103 break;
104 case AV_PIX_FMT_ARGB:
105 s->pixel_size = 4;
106 break;
107 default:
108 av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
109 break;
110 }
111 avctx->bits_per_coded_sample = avctx->pix_fmt == AV_PIX_FMT_GRAY8 ? 40 : s->pixel_size*8;
112
113 s->rlecode_table = av_mallocz(s->logical_width);
114 s->skip_table = av_mallocz(s->logical_width);
115 s->length_table = av_calloc(s->logical_width + 1, sizeof(*s->length_table));
116 if (!s->skip_table || !s->length_table || !s->rlecode_table) {
117 av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
118 return AVERROR(ENOMEM);
119 }
120 s->previous_frame = av_frame_alloc();
121 if (!s->previous_frame) {
122 av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
123 return AVERROR(ENOMEM);
124 }
125
126 s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size*2 /* image base material */
127 + 15 /* header + footer */
128 + s->avctx->height*2 /* skip code+rle end */
129 + s->logical_width/MAX_RLE_BULK + 1 /* rle codes */;
130
131 return 0;
132}
133
134/**
135 * Compute the best RLE sequence for a line
136 */
137static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
138{
139 int width=s->logical_width;
140 int i;
141 signed char rlecode;
142
143 /* This will be the number of pixels equal to the previous frame one's
144 * starting from the ith pixel */
145 unsigned int skipcount;
146 /* This will be the number of consecutive equal pixels in the current
147 * frame, starting from the ith one also */
148 unsigned int av_uninit(repeatcount);
149
150 /* The cost of the three different possibilities */
151 int total_skip_cost;
152 int total_repeat_cost;
153
154 int base_bulk_cost;
155 int lowest_bulk_cost;
156 int lowest_bulk_cost_index;
157 int sec_lowest_bulk_cost;
158 int sec_lowest_bulk_cost_index;
159
160 const uint8_t *this_line = p->data[0] + line * p->linesize[0] + width * s->pixel_size;
161 /* There might be no earlier frame if the current frame is a keyframe.
162 * So just use a pointer to the current frame to avoid a check
163 * to avoid NULL - s->pixel_size (which is undefined behaviour). */
164 const uint8_t *prev_line = s->key_frame ? this_line
165 : s->previous_frame->data[0]
166 + line * s->previous_frame->linesize[0]
167 + width * s->pixel_size;
168
169 s->length_table[width] = 0;
170 skipcount = 0;
171
172 /* Initial values */
173 lowest_bulk_cost = INT_MAX / 2;
174 lowest_bulk_cost_index = width;
175 sec_lowest_bulk_cost = INT_MAX / 2;
176 sec_lowest_bulk_cost_index = width;
177
178 base_bulk_cost = 1 + s->pixel_size;
179
180 for (i = width - 1; i >= 0; i--) {
181
182 int prev_bulk_cost;
183
184 this_line -= s->pixel_size;
185 prev_line -= s->pixel_size;
186
187 /* If our lowest bulk cost index is too far away, replace it
188 * with the next lowest bulk cost */
189 if (FFMIN(width, i + MAX_RLE_BULK) < lowest_bulk_cost_index) {
190 lowest_bulk_cost = sec_lowest_bulk_cost;
191 lowest_bulk_cost_index = sec_lowest_bulk_cost_index;
192
193 sec_lowest_bulk_cost = INT_MAX / 2;
194 sec_lowest_bulk_cost_index = width;
195 }
196
197 /* Deal with the first pixel's bulk cost */
198 if (!i) {
199 base_bulk_cost++;
200 lowest_bulk_cost++;
201 sec_lowest_bulk_cost++;
202 }
203
204 /* Look at the bulk cost of the previous loop and see if it is
205 * a new lower bulk cost */
206 prev_bulk_cost = s->length_table[i + 1] + base_bulk_cost;
207 if (prev_bulk_cost <= sec_lowest_bulk_cost) {
208 /* If it's lower than the 2nd lowest, then it may be lower
209 * than the lowest */
210 if (prev_bulk_cost <= lowest_bulk_cost) {
211
212 /* If we have found a new lowest bulk cost,
213 * then the 2nd lowest bulk cost is now farther than the
214 * lowest bulk cost, and will never be used */
215 sec_lowest_bulk_cost = INT_MAX / 2;
216
217 lowest_bulk_cost = prev_bulk_cost;
218 lowest_bulk_cost_index = i + 1;
219 } else {
220 /* Then it must be the 2nd lowest bulk cost */
221 sec_lowest_bulk_cost = prev_bulk_cost;
222 sec_lowest_bulk_cost_index = i + 1;
223 }
224 }
225
226 if (!s->key_frame && !memcmp(this_line, prev_line, s->pixel_size))
227 skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
228 else
229 skipcount = 0;
230
231 total_skip_cost = s->length_table[i + skipcount] + 2;
232 s->skip_table[i] = skipcount;
233
234
235 if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
236 repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
237 else
238 repeatcount = 1;
239
240 total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
241
242 /* skip code is free for the first pixel, it costs one byte for repeat and bulk copy
243 * so let's make it aware */
244 if (i == 0) {
245 total_skip_cost--;
246 total_repeat_cost++;
247 }
248
249 if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
250 /* repeat is the best */
251 s->length_table[i] = total_repeat_cost;
252 s->rlecode_table[i] = -repeatcount;
253 }
254 else if (skipcount > 0) {
255 /* skip is the best choice here */
256 s->length_table[i] = total_skip_cost;
257 s->rlecode_table[i] = 0;
258 }
259 else {
260 /* We cannot do neither skip nor repeat
261 * thus we use the best bulk copy */
262
263 s->length_table[i] = lowest_bulk_cost;
264 s->rlecode_table[i] = lowest_bulk_cost_index - i;
265
266 }
267
268 /* These bulk costs increase every iteration */
269 lowest_bulk_cost += s->pixel_size;
270 sec_lowest_bulk_cost += s->pixel_size;
271 }
272
273 /* Good! Now we have the best sequence for this line, let's output it. */
274
275 /* We do a special case for the first pixel so that we avoid testing it in
276 * the whole loop */
277
278 i=0;
279 this_line = p-> data[0] + line*p->linesize[0];
280
281 if (s->rlecode_table[0] == 0) {
282 bytestream_put_byte(buf, s->skip_table[0] + 1);
283 i += s->skip_table[0];
284 }
285 else bytestream_put_byte(buf, 1);
286
287
288 while (i < width) {
289 rlecode = s->rlecode_table[i];
290 bytestream_put_byte(buf, rlecode);
291 if (rlecode == 0) {
292 /* Write a skip sequence */
293 bytestream_put_byte(buf, s->skip_table[i] + 1);
294 i += s->skip_table[i];
295 }
296 else if (rlecode > 0) {
297 /* bulk copy */
298 if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
299 int j;
300 // QT grayscale colorspace has 0=white and 255=black, we will
301 // ignore the palette that is included in the AVFrame because
302 // AV_PIX_FMT_GRAY8 has defined color mapping
303 for (j = 0; j < rlecode*s->pixel_size; ++j)
304 bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
305 } else {
306 bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
307 }
308 i += rlecode;
309 }
310 else {
311 /* repeat the bits */
312 if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
313 int j;
314 // QT grayscale colorspace has 0=white and 255=black, ...
315 for (j = 0; j < s->pixel_size; ++j)
316 bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
317 } else {
318 bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
319 }
320 i -= rlecode;
321 }
322 }
323 bytestream_put_byte(buf, -1); // end RLE line
324}
325
326/** Encode frame including header */
327static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
328{
329 int i;
330 int start_line = 0;
331 int end_line = s->avctx->height;
332 uint8_t *orig_buf = buf;
333
334 if (!s->key_frame) {
335 unsigned line_size = s->logical_width * s->pixel_size;
336 for (start_line = 0; start_line < s->avctx->height; start_line++)
337 if (memcmp(p->data[0] + start_line*p->linesize[0],
338 s->previous_frame->data[0] + start_line * s->previous_frame->linesize[0],
339 line_size))
340 break;
341
342 for (end_line=s->avctx->height; end_line > start_line; end_line--)
343 if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
344 s->previous_frame->data[0] + (end_line - 1) * s->previous_frame->linesize[0],
345 line_size))
346 break;
347 }
348
349 bytestream_put_be32(&buf, 0); // CHUNK SIZE, patched later
350
351 if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
352 bytestream_put_be16(&buf, 0); // header
353 else {
354 bytestream_put_be16(&buf, 8); // header
355 bytestream_put_be16(&buf, start_line); // starting line
356 bytestream_put_be16(&buf, 0); // unknown
357 bytestream_put_be16(&buf, end_line - start_line); // lines to update
358 bytestream_put_be16(&buf, 0); // unknown
359 }
360 for (i = start_line; i < end_line; i++)
361 qtrle_encode_line(s, p, i, &buf);
362
363 bytestream_put_byte(&buf, 0); // zero skip code = frame finished
364 AV_WB32(orig_buf, buf - orig_buf); // patch the chunk size
365 return buf - orig_buf;
366}
367
369 const AVFrame *pict, int *got_packet)
370{
371 QtrleEncContext * const s = avctx->priv_data;
372 int ret;
373
374 if ((ret = ff_alloc_packet(avctx, pkt, s->max_buf_size)) < 0)
375 return ret;
376
377 if (avctx->gop_size == 0 || !s->previous_frame->data[0] ||
378 (s->avctx->frame_num % avctx->gop_size) == 0) {
379 /* I-Frame */
380 s->key_frame = 1;
381 } else {
382 /* P-Frame */
383 s->key_frame = 0;
384 }
385
386 pkt->size = encode_frame(s, pict, pkt->data);
387
388 /* save the current frame */
389 ret = av_frame_replace(s->previous_frame, pict);
390 if (ret < 0) {
391 av_log(avctx, AV_LOG_ERROR, "cannot add reference\n");
392 return ret;
393 }
394
395 if (s->key_frame)
396 pkt->flags |= AV_PKT_FLAG_KEY;
397 *got_packet = 1;
398
399 return 0;
400}
401
403 .p.name = "qtrle",
404 CODEC_LONG_NAME("QuickTime Animation (RLE) video"),
405 .p.type = AVMEDIA_TYPE_VIDEO,
406 .p.id = AV_CODEC_ID_QTRLE,
408 .priv_data_size = sizeof(QtrleEncContext),
411 .close = qtrle_encode_end,
414 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
415};
const FFCodec ff_qtrle_encoder
Definition qtrleenc.c:402
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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
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
@ AV_CODEC_ID_QTRLE
Definition codec_id.h:105
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
#define AVERROR(e)
Definition error.h:45
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition frame.c:376
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition imgutils.c:318
misc image utilities
#define AV_WB32(p, v)
#define av_uninit(x)
Definition attributes.h:187
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
const char data[16]
Definition mxf.c:149
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_RGB555BE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition pixfmt.h:114
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
static av_cold int qtrle_encode_end(AVCodecContext *avctx)
Definition qtrleenc.c:68
static av_cold int qtrle_encode_init(AVCodecContext *avctx)
Definition qtrleenc.c:79
#define MAX_RLE_REPEAT
Maximum RLE code for repeat.
Definition qtrleenc.c:35
static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
Compute the best RLE sequence for a line.
Definition qtrleenc.c:137
static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition qtrleenc.c:368
#define MAX_RLE_SKIP
Maximum RLE code for skip.
Definition qtrleenc.c:37
#define MAX_RLE_BULK
Maximum RLE code for bulk copy.
Definition qtrleenc.c:33
static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
Encode frame including header.
Definition qtrleenc.c:327
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
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition avcodec.h:1021
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
unsigned int max_buf_size
Definition qtrleenc.c:43
AVFrame * previous_frame
Definition qtrleenc.c:42
AVCodecContext * avctx
Definition qtrleenc.c:40
uint8_t * skip_table
Will contain at ith position the number of consecutive pixels equal to the previous frame starting fr...
Definition qtrleenc.c:62
int * length_table
This array will contain the length of the best rle encoding of the line starting at ith pixel.
Definition qtrleenc.c:58
signed char * rlecode_table
This array will contain at ith position the value of the best RLE code if the line started at pixel i...
Definition qtrleenc.c:54
int key_frame
Encoded frame is a key frame.
Definition qtrleenc.c:65
#define av_free(p)
#define av_mallocz(s)
#define av_log(a,...)
#define width
Definition dsp.h:89