FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/magicyuvenc.c
Date: 2022-01-27 16:04:32
Exec Total Coverage
Lines: 0 284 0.0%
Branches: 0 150 0.0%

Line Branch Exec Source
1 /*
2 * MagicYUV encoder
3 * Copyright (c) 2017 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/qsort.h"
28
29 #include "avcodec.h"
30 #include "bytestream.h"
31 #include "encode.h"
32 #include "put_bits.h"
33 #include "internal.h"
34 #include "thread.h"
35 #include "lossless_videoencdsp.h"
36
37 #define MAGICYUV_EXTRADATA_SIZE 32
38
39 typedef enum Prediction {
40 LEFT = 1,
41 GRADIENT,
42 MEDIAN,
43 } Prediction;
44
45 typedef struct HuffEntry {
46 uint8_t len;
47 uint32_t code;
48 } HuffEntry;
49
50 typedef struct PTable {
51 int value; ///< input value
52 int64_t prob; ///< number of occurences of this value in input
53 } PTable;
54
55 typedef struct MagicYUVContext {
56 const AVClass *class;
57 int frame_pred;
58 PutBitContext pb;
59 int planes;
60 uint8_t format;
61 AVFrame *p;
62 int slice_height;
63 int nb_slices;
64 int correlate;
65 int hshift[4];
66 int vshift[4];
67 uint8_t *slices[4];
68 unsigned slice_pos[4];
69 unsigned tables_size;
70 HuffEntry he[4][256];
71 LLVidEncDSPContext llvidencdsp;
72 void (*predict)(struct MagicYUVContext *s, uint8_t *src, uint8_t *dst,
73 ptrdiff_t stride, int width, int height);
74 } MagicYUVContext;
75
76 static void left_predict(MagicYUVContext *s,
77 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
78 int width, int height)
79 {
80 uint8_t prev = 0;
81 int i, j;
82
83 for (i = 0; i < width; i++) {
84 dst[i] = src[i] - prev;
85 prev = src[i];
86 }
87 dst += width;
88 src += stride;
89 for (j = 1; j < height; j++) {
90 prev = src[-stride];
91 for (i = 0; i < width; i++) {
92 dst[i] = src[i] - prev;
93 prev = src[i];
94 }
95 dst += width;
96 src += stride;
97 }
98 }
99
100 static void gradient_predict(MagicYUVContext *s,
101 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
102 int width, int height)
103 {
104 int left = 0, top, lefttop;
105 int i, j;
106
107 for (i = 0; i < width; i++) {
108 dst[i] = src[i] - left;
109 left = src[i];
110 }
111 dst += width;
112 src += stride;
113 for (j = 1; j < height; j++) {
114 top = src[-stride];
115 left = src[0] - top;
116 dst[0] = left;
117 for (i = 1; i < width; i++) {
118 top = src[i - stride];
119 lefttop = src[i - (stride + 1)];
120 left = src[i-1];
121 dst[i] = (src[i] - top) - left + lefttop;
122 }
123 dst += width;
124 src += stride;
125 }
126 }
127
128 static void median_predict(MagicYUVContext *s,
129 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
130 int width, int height)
131 {
132 int left = 0, lefttop;
133 int i, j;
134
135 for (i = 0; i < width; i++) {
136 dst[i] = src[i] - left;
137 left = src[i];
138 }
139 dst += width;
140 src += stride;
141 for (j = 1; j < height; j++) {
142 left = lefttop = src[-stride];
143 s->llvidencdsp.sub_median_pred(dst, src - stride, src, width, &left, &lefttop);
144 dst += width;
145 src += stride;
146 }
147 }
148
149 static av_cold int magy_encode_init(AVCodecContext *avctx)
150 {
151 MagicYUVContext *s = avctx->priv_data;
152 PutByteContext pb;
153 int i;
154
155 switch (avctx->pix_fmt) {
156 case AV_PIX_FMT_GBRP:
157 avctx->codec_tag = MKTAG('M', '8', 'R', 'G');
158 s->correlate = 1;
159 s->format = 0x65;
160 break;
161 case AV_PIX_FMT_GBRAP:
162 avctx->codec_tag = MKTAG('M', '8', 'R', 'A');
163 s->correlate = 1;
164 s->format = 0x66;
165 break;
166 case AV_PIX_FMT_YUV420P:
167 avctx->codec_tag = MKTAG('M', '8', 'Y', '0');
168 s->hshift[1] =
169 s->vshift[1] =
170 s->hshift[2] =
171 s->vshift[2] = 1;
172 s->format = 0x69;
173 break;
174 case AV_PIX_FMT_YUV422P:
175 avctx->codec_tag = MKTAG('M', '8', 'Y', '2');
176 s->hshift[1] =
177 s->hshift[2] = 1;
178 s->format = 0x68;
179 break;
180 case AV_PIX_FMT_YUV444P:
181 avctx->codec_tag = MKTAG('M', '8', 'Y', '4');
182 s->format = 0x67;
183 break;
184 case AV_PIX_FMT_YUVA444P:
185 avctx->codec_tag = MKTAG('M', '8', 'Y', 'A');
186 s->format = 0x6a;
187 break;
188 case AV_PIX_FMT_GRAY8:
189 avctx->codec_tag = MKTAG('M', '8', 'G', '0');
190 s->format = 0x6b;
191 break;
192 }
193
194 ff_llvidencdsp_init(&s->llvidencdsp);
195
196 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
197
198 s->nb_slices = 1;
199
200 for (i = 0; i < s->planes; i++) {
201 s->slices[i] = av_malloc(avctx->width * (avctx->height + 2) +
202 AV_INPUT_BUFFER_PADDING_SIZE);
203 if (!s->slices[i]) {
204 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer.\n");
205 return AVERROR(ENOMEM);
206 }
207 }
208
209 switch (s->frame_pred) {
210 case LEFT: s->predict = left_predict; break;
211 case GRADIENT: s->predict = gradient_predict; break;
212 case MEDIAN: s->predict = median_predict; break;
213 }
214
215 avctx->extradata_size = MAGICYUV_EXTRADATA_SIZE;
216
217 avctx->extradata = av_mallocz(avctx->extradata_size +
218 AV_INPUT_BUFFER_PADDING_SIZE);
219
220 if (!avctx->extradata) {
221 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata.\n");
222 return AVERROR(ENOMEM);
223 }
224
225 bytestream2_init_writer(&pb, avctx->extradata, MAGICYUV_EXTRADATA_SIZE);
226 bytestream2_put_le32(&pb, MKTAG('M', 'A', 'G', 'Y'));
227 bytestream2_put_le32(&pb, 32);
228 bytestream2_put_byte(&pb, 7);
229 bytestream2_put_byte(&pb, s->format);
230 bytestream2_put_byte(&pb, 12);
231 bytestream2_put_byte(&pb, 0);
232
233 bytestream2_put_byte(&pb, 0);
234 bytestream2_put_byte(&pb, 0);
235 bytestream2_put_byte(&pb, 32);
236 bytestream2_put_byte(&pb, 0);
237
238 bytestream2_put_le32(&pb, avctx->width);
239 bytestream2_put_le32(&pb, avctx->height);
240 bytestream2_put_le32(&pb, avctx->width);
241 bytestream2_put_le32(&pb, avctx->height);
242
243 return 0;
244 }
245
246 static void calculate_codes(HuffEntry *he, uint16_t codes_count[33])
247 {
248 for (unsigned i = 32, nb_codes = 0; i > 0; i--) {
249 uint16_t curr = codes_count[i]; // # of leafs of length i
250 codes_count[i] = nb_codes / 2; // # of non-leaf nodes on level i
251 nb_codes = codes_count[i] + curr; // # of nodes on level i
252 }
253
254 for (unsigned i = 0; i < 256; i++) {
255 he[i].code = codes_count[he[i].len];
256 codes_count[he[i].len]++;
257 }
258 }
259
260 static void count_usage(uint8_t *src, int width,
261 int height, PTable *counts)
262 {
263 int i, j;
264
265 for (j = 0; j < height; j++) {
266 for (i = 0; i < width; i++) {
267 counts[src[i]].prob++;
268 }
269 src += width;
270 }
271 }
272
273 typedef struct PackageMergerList {
274 int nitems; ///< number of items in the list and probability ex. 4
275 int item_idx[515]; ///< index range for each item in items 0, 2, 5, 9, 13
276 int probability[514]; ///< probability of each item 3, 8, 18, 46
277 int items[257 * 16]; ///< chain of all individual values that make up items A, B, A, B, C, A, B, C, D, C, D, D, E
278 } PackageMergerList;
279
280 static int compare_by_prob(const void *a, const void *b)
281 {
282 const PTable *a2 = a;
283 const PTable *b2 = b;
284 return a2->prob - b2->prob;
285 }
286
287 static void magy_huffman_compute_bits(PTable *prob_table, HuffEntry *distincts,
288 uint16_t codes_counts[33],
289 int size, int max_length)
290 {
291 PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
292 int times, i, j, k;
293 int nbits[257] = {0};
294 int min;
295
296 av_assert0(max_length > 0);
297
298 to->nitems = 0;
299 from->nitems = 0;
300 to->item_idx[0] = 0;
301 from->item_idx[0] = 0;
302 AV_QSORT(prob_table, size, PTable, compare_by_prob);
303
304 for (times = 0; times <= max_length; times++) {
305 to->nitems = 0;
306 to->item_idx[0] = 0;
307
308 j = 0;
309 k = 0;
310
311 if (times < max_length) {
312 i = 0;
313 }
314 while (i < size || j + 1 < from->nitems) {
315 to->nitems++;
316 to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
317 if (i < size &&
318 (j + 1 >= from->nitems ||
319 prob_table[i].prob <
320 from->probability[j] + from->probability[j + 1])) {
321 to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
322 to->probability[to->nitems - 1] = prob_table[i].prob;
323 i++;
324 } else {
325 for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
326 to->items[to->item_idx[to->nitems]++] = from->items[k];
327 }
328 to->probability[to->nitems - 1] =
329 from->probability[j] + from->probability[j + 1];
330 j += 2;
331 }
332 }
333 temp = to;
334 to = from;
335 from = temp;
336 }
337
338 min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
339 for (i = 0; i < from->item_idx[min]; i++) {
340 nbits[from->items[i]]++;
341 }
342
343 for (i = 0; i < size; i++) {
344 distincts[i].len = nbits[i];
345 codes_counts[nbits[i]]++;
346 }
347 }
348
349 static int encode_table(AVCodecContext *avctx, uint8_t *dst,
350 int width, int height,
351 PutBitContext *pb, HuffEntry *he)
352 {
353 PTable counts[256] = { {0} };
354 uint16_t codes_counts[33] = { 0 };
355 int i;
356
357 count_usage(dst, width, height, counts);
358
359 for (i = 0; i < 256; i++) {
360 counts[i].prob++;
361 counts[i].value = i;
362 }
363
364 magy_huffman_compute_bits(counts, he, codes_counts, 256, 12);
365
366 calculate_codes(he, codes_counts);
367
368 for (i = 0; i < 256; i++) {
369 put_bits(pb, 1, 0);
370 put_bits(pb, 7, he[i].len);
371 }
372
373 return 0;
374 }
375
376 static int encode_slice(uint8_t *src, uint8_t *dst, int dst_size,
377 int width, int height, HuffEntry *he, int prediction)
378 {
379 PutBitContext pb;
380 int i, j;
381 int count;
382
383 init_put_bits(&pb, dst, dst_size);
384
385 put_bits(&pb, 8, 0);
386 put_bits(&pb, 8, prediction);
387
388 for (j = 0; j < height; j++) {
389 for (i = 0; i < width; i++) {
390 const int idx = src[i];
391 put_bits(&pb, he[idx].len, he[idx].code);
392 }
393
394 src += width;
395 }
396
397 count = put_bits_count(&pb) & 0x1F;
398
399 if (count)
400 put_bits(&pb, 32 - count, 0);
401
402 flush_put_bits(&pb);
403
404 return put_bytes_output(&pb);
405 }
406
407 static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
408 const AVFrame *frame, int *got_packet)
409 {
410 MagicYUVContext *s = avctx->priv_data;
411 PutByteContext pb;
412 const int width = avctx->width, height = avctx->height;
413 int pos, slice, i, j, ret = 0;
414
415 ret = ff_alloc_packet(avctx, pkt, (256 + 4 * s->nb_slices + width * height) *
416 s->planes + 256);
417 if (ret < 0)
418 return ret;
419
420 bytestream2_init_writer(&pb, pkt->data, pkt->size);
421 bytestream2_put_le32(&pb, MKTAG('M', 'A', 'G', 'Y'));
422 bytestream2_put_le32(&pb, 32); // header size
423 bytestream2_put_byte(&pb, 7); // version
424 bytestream2_put_byte(&pb, s->format);
425 bytestream2_put_byte(&pb, 12); // max huffman length
426 bytestream2_put_byte(&pb, 0);
427
428 bytestream2_put_byte(&pb, 0);
429 bytestream2_put_byte(&pb, 0);
430 bytestream2_put_byte(&pb, 32); // coder type
431 bytestream2_put_byte(&pb, 0);
432
433 bytestream2_put_le32(&pb, avctx->width);
434 bytestream2_put_le32(&pb, avctx->height);
435 bytestream2_put_le32(&pb, avctx->width);
436 bytestream2_put_le32(&pb, avctx->height);
437 bytestream2_put_le32(&pb, 0);
438
439 for (i = 0; i < s->planes; i++) {
440 bytestream2_put_le32(&pb, 0);
441 for (j = 1; j < s->nb_slices; j++) {
442 bytestream2_put_le32(&pb, 0);
443 }
444 }
445
446 bytestream2_put_byte(&pb, s->planes);
447
448 for (i = 0; i < s->planes; i++) {
449 for (slice = 0; slice < s->nb_slices; slice++) {
450 bytestream2_put_byte(&pb, i);
451 }
452 }
453
454 if (s->correlate) {
455 uint8_t *r, *g, *b;
456 AVFrame *p = av_frame_clone(frame);
457
458 g = p->data[0];
459 b = p->data[1];
460 r = p->data[2];
461
462 for (i = 0; i < height; i++) {
463 s->llvidencdsp.diff_bytes(b, b, g, width);
464 s->llvidencdsp.diff_bytes(r, r, g, width);
465 g += p->linesize[0];
466 b += p->linesize[1];
467 r += p->linesize[2];
468 }
469
470 FFSWAP(uint8_t*, p->data[0], p->data[1]);
471 FFSWAP(int, p->linesize[0], p->linesize[1]);
472
473 for (i = 0; i < s->planes; i++) {
474 for (slice = 0; slice < s->nb_slices; slice++) {
475 s->predict(s, p->data[i], s->slices[i], p->linesize[i],
476 p->width, p->height);
477 }
478 }
479
480 av_frame_free(&p);
481 } else {
482 for (i = 0; i < s->planes; i++) {
483 for (slice = 0; slice < s->nb_slices; slice++) {
484 s->predict(s, frame->data[i], s->slices[i], frame->linesize[i],
485 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
486 AV_CEIL_RSHIFT(frame->height, s->vshift[i]));
487 }
488 }
489 }
490
491 init_put_bits(&s->pb, pkt->data + bytestream2_tell_p(&pb), bytestream2_get_bytes_left_p(&pb));
492
493 for (i = 0; i < s->planes; i++) {
494 encode_table(avctx, s->slices[i],
495 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
496 AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
497 &s->pb, s->he[i]);
498 }
499 s->tables_size = put_bytes_count(&s->pb, 1);
500 bytestream2_skip_p(&pb, s->tables_size);
501
502 for (i = 0; i < s->planes; i++) {
503 unsigned slice_size;
504
505 s->slice_pos[i] = bytestream2_tell_p(&pb);
506 slice_size = encode_slice(s->slices[i], pkt->data + bytestream2_tell_p(&pb),
507 bytestream2_get_bytes_left_p(&pb),
508 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
509 AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
510 s->he[i], s->frame_pred);
511 bytestream2_skip_p(&pb, slice_size);
512 }
513
514 pos = bytestream2_tell_p(&pb);
515 bytestream2_seek_p(&pb, 32, SEEK_SET);
516 bytestream2_put_le32(&pb, s->slice_pos[0] - 32);
517 for (i = 0; i < s->planes; i++) {
518 bytestream2_put_le32(&pb, s->slice_pos[i] - 32);
519 }
520 bytestream2_seek_p(&pb, pos, SEEK_SET);
521
522 pkt->size = bytestream2_tell_p(&pb);
523
524 *got_packet = 1;
525
526 return 0;
527 }
528
529 static av_cold int magy_encode_close(AVCodecContext *avctx)
530 {
531 MagicYUVContext *s = avctx->priv_data;
532 int i;
533
534 for (i = 0; i < s->planes; i++)
535 av_freep(&s->slices[i]);
536
537 return 0;
538 }
539
540 #define OFFSET(x) offsetof(MagicYUVContext, x)
541 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
542 static const AVOption options[] = {
543 { "pred", "Prediction method", OFFSET(frame_pred), AV_OPT_TYPE_INT, {.i64=LEFT}, LEFT, MEDIAN, VE, "pred" },
544 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, 0, 0, VE, "pred" },
545 { "gradient", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = GRADIENT }, 0, 0, VE, "pred" },
546 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MEDIAN }, 0, 0, VE, "pred" },
547 { NULL},
548 };
549
550 static const AVClass magicyuv_class = {
551 .class_name = "magicyuv",
552 .item_name = av_default_item_name,
553 .option = options,
554 .version = LIBAVUTIL_VERSION_INT,
555 };
556
557 const AVCodec ff_magicyuv_encoder = {
558 .name = "magicyuv",
559 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
560 .type = AVMEDIA_TYPE_VIDEO,
561 .id = AV_CODEC_ID_MAGICYUV,
562 .priv_data_size = sizeof(MagicYUVContext),
563 .priv_class = &magicyuv_class,
564 .init = magy_encode_init,
565 .close = magy_encode_close,
566 .encode2 = magy_encode_frame,
567 .capabilities = AV_CODEC_CAP_FRAME_THREADS,
568 .pix_fmts = (const enum AVPixelFormat[]) {
569 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_YUV422P,
570 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_GRAY8,
571 AV_PIX_FMT_NONE
572 },
573 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
574 };
575