FFmpeg coverage


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