3 * Copyright (c) 2017 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/qsort.h"
30 #include "bytestream.h"
34 #include "lossless_videoencdsp.h"
36 typedef enum Prediction {
42 typedef struct HuffEntry {
47 typedef struct PTable {
48 int value; ///< input value
49 int64_t prob; ///< number of occurences of this value in input
52 typedef struct MagicYUVContext {
65 unsigned slice_pos[4];
68 LLVidEncDSPContext llvidencdsp;
69 void (*predict)(struct MagicYUVContext *s, uint8_t *src, uint8_t *dst,
70 ptrdiff_t stride, int width, int height);
73 static void left_predict(MagicYUVContext *s,
74 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
75 int width, int height)
80 for (i = 0; i < width; i++) {
81 dst[i] = src[i] - prev;
86 for (j = 1; j < height; j++) {
88 for (i = 0; i < width; i++) {
89 dst[i] = src[i] - prev;
97 static void gradient_predict(MagicYUVContext *s,
98 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
99 int width, int height)
101 int left = 0, top, lefttop;
104 for (i = 0; i < width; i++) {
105 dst[i] = src[i] - left;
110 for (j = 1; j < height; j++) {
114 for (i = 1; i < width; i++) {
115 top = src[i - stride];
116 lefttop = src[i - (stride + 1)];
118 dst[i] = (src[i] - top) - left + lefttop;
125 static void median_predict(MagicYUVContext *s,
126 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
127 int width, int height)
129 int left = 0, lefttop;
132 for (i = 0; i < width; i++) {
133 dst[i] = src[i] - left;
138 for (j = 1; j < height; j++) {
139 left = lefttop = src[-stride];
140 s->llvidencdsp.sub_median_pred(dst, src - stride, src, width, &left, &lefttop);
146 static av_cold int magy_encode_init(AVCodecContext *avctx)
148 MagicYUVContext *s = avctx->priv_data;
152 switch (avctx->pix_fmt) {
153 case AV_PIX_FMT_GBRP:
154 avctx->codec_tag = MKTAG('M', '8', 'R', 'G');
158 case AV_PIX_FMT_GBRAP:
159 avctx->codec_tag = MKTAG('M', '8', 'R', 'A');
163 case AV_PIX_FMT_YUV420P:
164 avctx->codec_tag = MKTAG('M', '8', 'Y', '0');
171 case AV_PIX_FMT_YUV422P:
172 avctx->codec_tag = MKTAG('M', '8', 'Y', '2');
177 case AV_PIX_FMT_YUV444P:
178 avctx->codec_tag = MKTAG('M', '8', 'Y', '4');
181 case AV_PIX_FMT_YUVA444P:
182 avctx->codec_tag = MKTAG('M', '8', 'Y', 'A');
185 case AV_PIX_FMT_GRAY8:
186 avctx->codec_tag = MKTAG('M', '8', 'G', '0');
190 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n",
192 return AVERROR_INVALIDDATA;
195 ff_llvidencdsp_init(&s->llvidencdsp);
197 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
201 for (i = 0; i < s->planes; i++) {
202 s->slices[i] = av_malloc(avctx->width * (avctx->height + 2) +
203 AV_INPUT_BUFFER_PADDING_SIZE);
205 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer.\n");
206 return AVERROR(ENOMEM);
210 switch (s->frame_pred) {
211 case LEFT: s->predict = left_predict; break;
212 case GRADIENT: s->predict = gradient_predict; break;
213 case MEDIAN: s->predict = median_predict; break;
216 avctx->extradata_size = 32;
218 avctx->extradata = av_mallocz(avctx->extradata_size +
219 AV_INPUT_BUFFER_PADDING_SIZE);
221 if (!avctx->extradata) {
222 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata.\n");
223 return AVERROR(ENOMEM);
226 bytestream2_init_writer(&pb, avctx->extradata, avctx->extradata_size);
227 bytestream2_put_le32(&pb, MKTAG('M', 'A', 'G', 'Y'));
228 bytestream2_put_le32(&pb, 32);
229 bytestream2_put_byte(&pb, 7);
230 bytestream2_put_byte(&pb, s->format);
231 bytestream2_put_byte(&pb, 12);
232 bytestream2_put_byte(&pb, 0);
234 bytestream2_put_byte(&pb, 0);
235 bytestream2_put_byte(&pb, 0);
236 bytestream2_put_byte(&pb, 32);
237 bytestream2_put_byte(&pb, 0);
239 bytestream2_put_le32(&pb, avctx->width);
240 bytestream2_put_le32(&pb, avctx->height);
241 bytestream2_put_le32(&pb, avctx->width);
242 bytestream2_put_le32(&pb, avctx->height);
247 static void calculate_codes(HuffEntry *he, uint16_t codes_count[33])
249 for (unsigned i = 32, nb_codes = 0; i > 0; i--) {
250 uint16_t curr = codes_count[i]; // # of leafs of length i
251 codes_count[i] = nb_codes / 2; // # of non-leaf nodes on level i
252 nb_codes = codes_count[i] + curr; // # of nodes on level i
255 for (unsigned i = 0; i < 256; i++) {
256 he[i].code = codes_count[he[i].len];
257 codes_count[he[i].len]++;
261 static void count_usage(uint8_t *src, int width,
262 int height, PTable *counts)
266 for (j = 0; j < height; j++) {
267 for (i = 0; i < width; i++) {
268 counts[src[i]].prob++;
274 typedef struct PackageMergerList {
275 int nitems; ///< number of items in the list and probability ex. 4
276 int item_idx[515]; ///< index range for each item in items 0, 2, 5, 9, 13
277 int probability[514]; ///< probability of each item 3, 8, 18, 46
278 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
281 static int compare_by_prob(const void *a, const void *b)
283 const PTable *a2 = a;
284 const PTable *b2 = b;
285 return a2->prob - b2->prob;
288 static void magy_huffman_compute_bits(PTable *prob_table, HuffEntry *distincts,
289 uint16_t codes_counts[33],
290 int size, int max_length)
292 PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
294 int nbits[257] = {0};
297 av_assert0(max_length > 0);
302 from->item_idx[0] = 0;
303 AV_QSORT(prob_table, size, PTable, compare_by_prob);
305 for (times = 0; times <= max_length; times++) {
312 if (times < max_length) {
315 while (i < size || j + 1 < from->nitems) {
317 to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
319 (j + 1 >= from->nitems ||
321 from->probability[j] + from->probability[j + 1])) {
322 to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
323 to->probability[to->nitems - 1] = prob_table[i].prob;
326 for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
327 to->items[to->item_idx[to->nitems]++] = from->items[k];
329 to->probability[to->nitems - 1] =
330 from->probability[j] + from->probability[j + 1];
339 min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
340 for (i = 0; i < from->item_idx[min]; i++) {
341 nbits[from->items[i]]++;
344 for (i = 0; i < size; i++) {
345 distincts[i].len = nbits[i];
346 codes_counts[nbits[i]]++;
350 static int encode_table(AVCodecContext *avctx, uint8_t *dst,
351 int width, int height,
352 PutBitContext *pb, HuffEntry *he)
354 PTable counts[256] = { {0} };
355 uint16_t codes_counts[33] = { 0 };
358 count_usage(dst, width, height, counts);
360 for (i = 0; i < 256; i++) {
365 magy_huffman_compute_bits(counts, he, codes_counts, 256, 12);
367 calculate_codes(he, codes_counts);
369 for (i = 0; i < 256; i++) {
371 put_bits(pb, 7, he[i].len);
377 static int encode_slice(uint8_t *src, uint8_t *dst, int dst_size,
378 int width, int height, HuffEntry *he, int prediction)
384 init_put_bits(&pb, dst, dst_size);
387 put_bits(&pb, 8, prediction);
389 for (j = 0; j < height; j++) {
390 for (i = 0; i < width; i++) {
391 const int idx = src[i];
392 put_bits(&pb, he[idx].len, he[idx].code);
398 count = put_bits_count(&pb) & 0x1F;
401 put_bits(&pb, 32 - count, 0);
405 return put_bytes_output(&pb);
408 static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
409 const AVFrame *frame, int *got_packet)
411 MagicYUVContext *s = avctx->priv_data;
413 const int width = avctx->width, height = avctx->height;
414 int pos, slice, i, j, ret = 0;
416 ret = ff_alloc_packet2(avctx, pkt, (256 + 4 * s->nb_slices + width * height) *
421 bytestream2_init_writer(&pb, pkt->data, pkt->size);
422 bytestream2_put_le32(&pb, MKTAG('M', 'A', 'G', 'Y'));
423 bytestream2_put_le32(&pb, 32); // header size
424 bytestream2_put_byte(&pb, 7); // version
425 bytestream2_put_byte(&pb, s->format);
426 bytestream2_put_byte(&pb, 12); // max huffman length
427 bytestream2_put_byte(&pb, 0);
429 bytestream2_put_byte(&pb, 0);
430 bytestream2_put_byte(&pb, 0);
431 bytestream2_put_byte(&pb, 32); // coder type
432 bytestream2_put_byte(&pb, 0);
434 bytestream2_put_le32(&pb, avctx->width);
435 bytestream2_put_le32(&pb, avctx->height);
436 bytestream2_put_le32(&pb, avctx->width);
437 bytestream2_put_le32(&pb, avctx->height);
438 bytestream2_put_le32(&pb, 0);
440 for (i = 0; i < s->planes; i++) {
441 bytestream2_put_le32(&pb, 0);
442 for (j = 1; j < s->nb_slices; j++) {
443 bytestream2_put_le32(&pb, 0);
447 bytestream2_put_byte(&pb, s->planes);
449 for (i = 0; i < s->planes; i++) {
450 for (slice = 0; slice < s->nb_slices; slice++) {
451 bytestream2_put_byte(&pb, i);
457 AVFrame *p = av_frame_clone(frame);
463 for (i = 0; i < height; i++) {
464 s->llvidencdsp.diff_bytes(b, b, g, width);
465 s->llvidencdsp.diff_bytes(r, r, g, width);
471 FFSWAP(uint8_t*, p->data[0], p->data[1]);
472 FFSWAP(int, p->linesize[0], p->linesize[1]);
474 for (i = 0; i < s->planes; i++) {
475 for (slice = 0; slice < s->nb_slices; slice++) {
476 s->predict(s, p->data[i], s->slices[i], p->linesize[i],
477 p->width, p->height);
483 for (i = 0; i < s->planes; i++) {
484 for (slice = 0; slice < s->nb_slices; slice++) {
485 s->predict(s, frame->data[i], s->slices[i], frame->linesize[i],
486 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
487 AV_CEIL_RSHIFT(frame->height, s->vshift[i]));
492 init_put_bits(&s->pb, pkt->data + bytestream2_tell_p(&pb), bytestream2_get_bytes_left_p(&pb));
494 for (i = 0; i < s->planes; i++) {
495 encode_table(avctx, s->slices[i],
496 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
497 AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
500 s->tables_size = put_bytes_count(&s->pb, 1);
501 bytestream2_skip_p(&pb, s->tables_size);
503 for (i = 0; i < s->planes; i++) {
506 s->slice_pos[i] = bytestream2_tell_p(&pb);
507 slice_size = encode_slice(s->slices[i], pkt->data + bytestream2_tell_p(&pb),
508 bytestream2_get_bytes_left_p(&pb),
509 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
510 AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
511 s->he[i], s->frame_pred);
512 bytestream2_skip_p(&pb, slice_size);
515 pos = bytestream2_tell_p(&pb);
516 bytestream2_seek_p(&pb, 32, SEEK_SET);
517 bytestream2_put_le32(&pb, s->slice_pos[0] - 32);
518 for (i = 0; i < s->planes; i++) {
519 bytestream2_put_le32(&pb, s->slice_pos[i] - 32);
521 bytestream2_seek_p(&pb, pos, SEEK_SET);
523 pkt->size = bytestream2_tell_p(&pb);
524 pkt->flags |= AV_PKT_FLAG_KEY;
531 static av_cold int magy_encode_close(AVCodecContext *avctx)
533 MagicYUVContext *s = avctx->priv_data;
536 for (i = 0; i < s->planes; i++)
537 av_freep(&s->slices[i]);
542 #define OFFSET(x) offsetof(MagicYUVContext, x)
543 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
544 static const AVOption options[] = {
545 { "pred", "Prediction method", OFFSET(frame_pred), AV_OPT_TYPE_INT, {.i64=LEFT}, LEFT, MEDIAN, VE, "pred" },
546 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, 0, 0, VE, "pred" },
547 { "gradient", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = GRADIENT }, 0, 0, VE, "pred" },
548 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MEDIAN }, 0, 0, VE, "pred" },
552 static const AVClass magicyuv_class = {
553 .class_name = "magicyuv",
554 .item_name = av_default_item_name,
556 .version = LIBAVUTIL_VERSION_INT,
559 AVCodec ff_magicyuv_encoder = {
561 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
562 .type = AVMEDIA_TYPE_VIDEO,
563 .id = AV_CODEC_ID_MAGICYUV,
564 .priv_data_size = sizeof(MagicYUVContext),
565 .priv_class = &magicyuv_class,
566 .init = magy_encode_init,
567 .close = magy_encode_close,
568 .encode2 = magy_encode_frame,
569 .capabilities = AV_CODEC_CAP_FRAME_THREADS,
570 .pix_fmts = (const enum AVPixelFormat[]) {
571 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_YUV422P,
572 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_GRAY8,
575 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,