FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/qtrleenc.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 151 175 86.3%
Functions: 5 5 100.0%
Branches: 74 95 77.9%

Line Branch Exec Source
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
39 typedef struct QtrleEncContext {
40 AVCodecContext *avctx;
41 int pixel_size;
42 AVFrame *previous_frame;
43 unsigned int max_buf_size;
44 int logical_width;
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 */
58 int *length_table;
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 */
65 int key_frame;
66 } QtrleEncContext;
67
68 7 static av_cold int qtrle_encode_end(AVCodecContext *avctx)
69 {
70 7 QtrleEncContext *s = avctx->priv_data;
71
72 7 av_frame_free(&s->previous_frame);
73 7 av_free(s->rlecode_table);
74 7 av_free(s->length_table);
75 7 av_free(s->skip_table);
76 7 return 0;
77 }
78
79 7 static av_cold int qtrle_encode_init(AVCodecContext *avctx)
80 {
81 7 QtrleEncContext *s = avctx->priv_data;
82
83
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
84 return AVERROR(EINVAL);
85 }
86 7 s->avctx=avctx;
87 7 s->logical_width=avctx->width;
88
89
2/5
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7 switch (avctx->pix_fmt) {
90 3 case AV_PIX_FMT_GRAY8:
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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 3 s->logical_width = avctx->width / 4;
96 3 s->pixel_size = 4;
97 3 break;
98 case AV_PIX_FMT_RGB555BE:
99 s->pixel_size = 2;
100 break;
101 4 case AV_PIX_FMT_RGB24:
102 4 s->pixel_size = 3;
103 4 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
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 avctx->bits_per_coded_sample = avctx->pix_fmt == AV_PIX_FMT_GRAY8 ? 40 : s->pixel_size*8;
112
113 7 s->rlecode_table = av_mallocz(s->logical_width);
114 7 s->skip_table = av_mallocz(s->logical_width);
115 7 s->length_table = av_calloc(s->logical_width + 1, sizeof(*s->length_table));
116
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
7 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 7 s->previous_frame = av_frame_alloc();
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!s->previous_frame) {
122 av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
123 return AVERROR(ENOMEM);
124 }
125
126 7 s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size*2 /* image base material */
127 7 + 15 /* header + footer */
128 7 + s->avctx->height*2 /* skip code+rle end */
129 7 + s->logical_width/MAX_RLE_BULK + 1 /* rle codes */;
130
131 7 return 0;
132 }
133
134 /**
135 * Compute the best RLE sequence for a line
136 */
137 88100 static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
138 {
139 88100 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 88100 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 88100 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 176200 const uint8_t *prev_line = s->key_frame ? this_line
165
2/2
✓ Branch 0 taken 79290 times.
✓ Branch 1 taken 8810 times.
88100 : s->previous_frame->data[0]
166 79290 + line * s->previous_frame->linesize[0]
167 79290 + width * s->pixel_size;
168
169 88100 s->length_table[width] = 0;
170 88100 skipcount = 0;
171
172 /* Initial values */
173 88100 lowest_bulk_cost = INT_MAX / 2;
174 88100 lowest_bulk_cost_index = width;
175 88100 sec_lowest_bulk_cost = INT_MAX / 2;
176 88100 sec_lowest_bulk_cost_index = width;
177
178 88100 base_bulk_cost = 1 + s->pixel_size;
179
180
2/2
✓ Branch 0 taken 19065800 times.
✓ Branch 1 taken 88100 times.
19153900 for (i = width - 1; i >= 0; i--) {
181
182 int prev_bulk_cost;
183
184 19065800 this_line -= s->pixel_size;
185 19065800 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
2/2
✓ Branch 0 taken 26495 times.
✓ Branch 1 taken 19039305 times.
19065800 if (FFMIN(width, i + MAX_RLE_BULK) < lowest_bulk_cost_index) {
190 26495 lowest_bulk_cost = sec_lowest_bulk_cost;
191 26495 lowest_bulk_cost_index = sec_lowest_bulk_cost_index;
192
193 26495 sec_lowest_bulk_cost = INT_MAX / 2;
194 26495 sec_lowest_bulk_cost_index = width;
195 }
196
197 /* Deal with the first pixel's bulk cost */
198
2/2
✓ Branch 0 taken 88100 times.
✓ Branch 1 taken 18977700 times.
19065800 if (!i) {
199 88100 base_bulk_cost++;
200 88100 lowest_bulk_cost++;
201 88100 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 19065800 prev_bulk_cost = s->length_table[i + 1] + base_bulk_cost;
207
1/2
✓ Branch 0 taken 19065800 times.
✗ Branch 1 not taken.
19065800 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
2/2
✓ Branch 0 taken 1250807 times.
✓ Branch 1 taken 17814993 times.
19065800 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 1250807 sec_lowest_bulk_cost = INT_MAX / 2;
216
217 1250807 lowest_bulk_cost = prev_bulk_cost;
218 1250807 lowest_bulk_cost_index = i + 1;
219 } else {
220 /* Then it must be the 2nd lowest bulk cost */
221 17814993 sec_lowest_bulk_cost = prev_bulk_cost;
222 17814993 sec_lowest_bulk_cost_index = i + 1;
223 }
224 }
225
226
4/4
✓ Branch 0 taken 17159220 times.
✓ Branch 1 taken 1906580 times.
✓ Branch 2 taken 310823 times.
✓ Branch 3 taken 16848397 times.
19065800 if (!s->key_frame && !memcmp(this_line, prev_line, s->pixel_size))
227 310823 skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
228 else
229 18754977 skipcount = 0;
230
231 19065800 total_skip_cost = s->length_table[i + skipcount] + 2;
232 19065800 s->skip_table[i] = skipcount;
233
234
235
4/4
✓ Branch 0 taken 18977700 times.
✓ Branch 1 taken 88100 times.
✓ Branch 2 taken 973112 times.
✓ Branch 3 taken 18004588 times.
19065800 if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
236 973112 repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
237 else
238 18092688 repeatcount = 1;
239
240 19065800 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
2/2
✓ Branch 0 taken 88100 times.
✓ Branch 1 taken 18977700 times.
19065800 if (i == 0) {
245 88100 total_skip_cost--;
246 88100 total_repeat_cost++;
247 }
248
249
6/6
✓ Branch 0 taken 973112 times.
✓ Branch 1 taken 18092688 times.
✓ Branch 2 taken 140030 times.
✓ Branch 3 taken 833082 times.
✓ Branch 4 taken 61815 times.
✓ Branch 5 taken 78215 times.
19065800 if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
250 /* repeat is the best */
251 894897 s->length_table[i] = total_repeat_cost;
252 894897 s->rlecode_table[i] = -repeatcount;
253 }
254
2/2
✓ Branch 0 taken 249008 times.
✓ Branch 1 taken 17921895 times.
18170903 else if (skipcount > 0) {
255 /* skip is the best choice here */
256 249008 s->length_table[i] = total_skip_cost;
257 249008 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 17921895 s->length_table[i] = lowest_bulk_cost;
264 17921895 s->rlecode_table[i] = lowest_bulk_cost_index - i;
265
266 }
267
268 /* These bulk costs increase every iteration */
269 19065800 lowest_bulk_cost += s->pixel_size;
270 19065800 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 88100 i=0;
279 88100 this_line = p-> data[0] + line*p->linesize[0];
280
281
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 87486 times.
88100 if (s->rlecode_table[0] == 0) {
282 614 bytestream_put_byte(buf, s->skip_table[0] + 1);
283 614 i += s->skip_table[0];
284 }
285 87486 else bytestream_put_byte(buf, 1);
286
287
288
2/2
✓ Branch 0 taken 1580387 times.
✓ Branch 1 taken 88100 times.
1668487 while (i < width) {
289 1580387 rlecode = s->rlecode_table[i];
290 1580387 bytestream_put_byte(buf, rlecode);
291
2/2
✓ Branch 0 taken 122170 times.
✓ Branch 1 taken 1458217 times.
1580387 if (rlecode == 0) {
292 /* Write a skip sequence */
293 122170 bytestream_put_byte(buf, s->skip_table[i] + 1);
294 122170 i += s->skip_table[i];
295 }
296
2/2
✓ Branch 0 taken 733614 times.
✓ Branch 1 taken 724603 times.
1458217 else if (rlecode > 0) {
297 /* bulk copy */
298
2/2
✓ Branch 0 taken 67951 times.
✓ Branch 1 taken 665663 times.
733614 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
2/2
✓ Branch 0 taken 14970796 times.
✓ Branch 1 taken 67951 times.
15038747 for (j = 0; j < rlecode*s->pixel_size; ++j)
304 14970796 bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
305 } else {
306 665663 bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
307 }
308 733614 i += rlecode;
309 }
310 else {
311 /* repeat the bits */
312
2/2
✓ Branch 0 taken 10722 times.
✓ Branch 1 taken 713881 times.
724603 if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
313 int j;
314 // QT grayscale colorspace has 0=white and 255=black, ...
315
2/2
✓ Branch 0 taken 42888 times.
✓ Branch 1 taken 10722 times.
53610 for (j = 0; j < s->pixel_size; ++j)
316 42888 bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
317 } else {
318 713881 bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
319 }
320 724603 i -= rlecode;
321 }
322 }
323 88100 bytestream_put_byte(buf, -1); // end RLE line
324 88100 }
325
326 /** Encode frame including header */
327 350 static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
328 {
329 int i;
330 350 int start_line = 0;
331 350 int end_line = s->avctx->height;
332 350 uint8_t *orig_buf = buf;
333
334
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 35 times.
350 if (!s->key_frame) {
335 315 unsigned line_size = s->logical_width * s->pixel_size;
336
1/2
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
315 for (start_line = 0; start_line < s->avctx->height; start_line++)
337 315 if (memcmp(p->data[0] + start_line*p->linesize[0],
338
1/2
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
315 s->previous_frame->data[0] + start_line * s->previous_frame->linesize[0],
339 line_size))
340 315 break;
341
342
1/2
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
315 for (end_line=s->avctx->height; end_line > start_line; end_line--)
343 315 if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
344
1/2
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
315 s->previous_frame->data[0] + (end_line - 1) * s->previous_frame->linesize[0],
345 line_size))
346 315 break;
347 }
348
349 350 bytestream_put_be32(&buf, 0); // CHUNK SIZE, patched later
350
351
2/6
✓ Branch 0 taken 350 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 350 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
350 if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
352 350 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
2/2
✓ Branch 0 taken 88100 times.
✓ Branch 1 taken 350 times.
88450 for (i = start_line; i < end_line; i++)
361 88100 qtrle_encode_line(s, p, i, &buf);
362
363 350 bytestream_put_byte(&buf, 0); // zero skip code = frame finished
364 350 AV_WB32(orig_buf, buf - orig_buf); // patch the chunk size
365 350 return buf - orig_buf;
366 }
367
368 350 static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
369 const AVFrame *pict, int *got_packet)
370 {
371 350 QtrleEncContext * const s = avctx->priv_data;
372 int ret;
373
374
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 350 times.
350 if ((ret = ff_alloc_packet(avctx, pkt, s->max_buf_size)) < 0)
375 return ret;
376
377
3/4
✓ Branch 0 taken 350 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 343 times.
✓ Branch 3 taken 7 times.
350 if (avctx->gop_size == 0 || !s->previous_frame->data[0] ||
378
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 315 times.
343 (s->avctx->frame_num % avctx->gop_size) == 0) {
379 /* I-Frame */
380 35 s->key_frame = 1;
381 } else {
382 /* P-Frame */
383 315 s->key_frame = 0;
384 }
385
386 350 pkt->size = encode_frame(s, pict, pkt->data);
387
388 /* save the current frame */
389 350 ret = av_frame_replace(s->previous_frame, pict);
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350 times.
350 if (ret < 0) {
391 av_log(avctx, AV_LOG_ERROR, "cannot add reference\n");
392 return ret;
393 }
394
395
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 315 times.
350 if (s->key_frame)
396 35 pkt->flags |= AV_PKT_FLAG_KEY;
397 350 *got_packet = 1;
398
399 350 return 0;
400 }
401
402 const FFCodec ff_qtrle_encoder = {
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,
407 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
408 .priv_data_size = sizeof(QtrleEncContext),
409 .init = qtrle_encode_init,
410 FF_CODEC_ENCODE_CB(qtrle_encode_frame),
411 .close = qtrle_encode_end,
412 .p.pix_fmts = (const enum AVPixelFormat[]){
413 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB555BE, AV_PIX_FMT_ARGB, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
414 },
415 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
416 };
417