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 {
48 typedef struct PTable {
49 int value; ///< input value
50 int64_t prob; ///< number of occurences of this value in input
53 typedef struct MagicYUVContext {
67 unsigned slice_pos[4];
70 LLVidEncDSPContext llvidencdsp;
71 void (*predict)(struct MagicYUVContext *s, uint8_t *src, uint8_t *dst,
72 ptrdiff_t stride, int width, int height);
75 static void left_predict(MagicYUVContext *s,
76 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
77 int width, int height)
82 for (i = 0; i < width; i++) {
83 dst[i] = src[i] - prev;
88 for (j = 1; j < height; j++) {
90 for (i = 0; i < width; i++) {
91 dst[i] = src[i] - prev;
99 static void gradient_predict(MagicYUVContext *s,
100 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
101 int width, int height)
103 int left = 0, top, lefttop;
106 for (i = 0; i < width; i++) {
107 dst[i] = src[i] - left;
112 for (j = 1; j < height; j++) {
116 for (i = 1; i < width; i++) {
117 top = src[i - stride];
118 lefttop = src[i - (stride + 1)];
120 dst[i] = (src[i] - top) - left + lefttop;
127 static void median_predict(MagicYUVContext *s,
128 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
129 int width, int height)
131 int left = 0, lefttop;
134 for (i = 0; i < width; i++) {
135 dst[i] = src[i] - left;
140 for (j = 1; j < height; j++) {
141 left = lefttop = src[-stride];
142 s->llvidencdsp.sub_median_pred(dst, src - stride, src, width, &left, &lefttop);
148 static av_cold int magy_encode_init(AVCodecContext *avctx)
150 MagicYUVContext *s = avctx->priv_data;
153 switch (avctx->pix_fmt) {
154 case AV_PIX_FMT_GBRP:
155 avctx->codec_tag = MKTAG('M', '8', 'R', 'G');
159 case AV_PIX_FMT_GBRAP:
160 avctx->codec_tag = MKTAG('M', '8', 'R', 'A');
164 case AV_PIX_FMT_YUV420P:
165 avctx->codec_tag = MKTAG('M', '8', 'Y', '0');
172 case AV_PIX_FMT_YUV422P:
173 avctx->codec_tag = MKTAG('M', '8', 'Y', '2');
178 case AV_PIX_FMT_YUV444P:
179 avctx->codec_tag = MKTAG('M', '8', 'Y', '4');
182 case AV_PIX_FMT_YUVA444P:
183 avctx->codec_tag = MKTAG('M', '8', 'Y', 'A');
186 case AV_PIX_FMT_GRAY8:
187 avctx->codec_tag = MKTAG('M', '8', 'G', '0');
191 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format: %d\n",
193 return AVERROR_INVALIDDATA;
196 ff_llvidencdsp_init(&s->llvidencdsp);
198 s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
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);
206 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer.\n");
207 return AVERROR(ENOMEM);
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;
220 static int magy_huff_cmp_len(const void *a, const void *b)
222 const HuffEntry *aa = a, *bb = b;
223 return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
226 static int huff_cmp_sym(const void *a, const void *b)
228 const HuffEntry *aa = a, *bb = b;
229 return bb->sym - aa->sym;
232 static void calculate_codes(HuffEntry *he)
237 AV_QSORT(he, 256, HuffEntry, magy_huff_cmp_len);
240 for (i = 255; i >= 0; i--) {
241 he[i].code = code >> (32 - he[i].len);
242 code += 0x80000000u >> (he[i].len - 1);
245 AV_QSORT(he, 256, HuffEntry, huff_cmp_sym);
248 static void count_usage(uint8_t *src, int width,
249 int height, PTable *counts)
253 for (j = 0; j < height; j++) {
254 for (i = 0; i < width; i++) {
255 counts[src[i]].prob++;
261 typedef struct PackageMergerList {
262 int nitems; ///< number of items in the list and probability ex. 4
263 int item_idx[515]; ///< index range for each item in items 0, 2, 5, 9, 13
264 int probability[514]; ///< probability of each item 3, 8, 18, 46
265 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
268 static int compare_by_prob(const void *a, const void *b)
270 PTable a_val = *(PTable *)a;
271 PTable b_val = *(PTable *)b;
272 return a_val.prob - b_val.prob;
275 static void magy_huffman_compute_bits(PTable *prob_table, HuffEntry *distincts,
276 int size, int max_length)
278 PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
280 int nbits[257] = {0};
283 av_assert0(max_length > 0);
288 from->item_idx[0] = 0;
289 AV_QSORT(prob_table, size, PTable, compare_by_prob);
291 for (times = 0; times <= max_length; times++) {
298 if (times < max_length) {
301 while (i < size || j + 1 < from->nitems) {
303 to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
305 (j + 1 >= from->nitems ||
307 from->probability[j] + from->probability[j + 1])) {
308 to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
309 to->probability[to->nitems - 1] = prob_table[i].prob;
312 for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
313 to->items[to->item_idx[to->nitems]++] = from->items[k];
315 to->probability[to->nitems - 1] =
316 from->probability[j] + from->probability[j + 1];
325 min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
326 for (i = 0; i < from->item_idx[min]; i++) {
327 nbits[from->items[i]]++;
330 for (i = 0; i < size; i++) {
331 distincts[i].sym = i;
332 distincts[i].len = nbits[i];
336 static int encode_table(AVCodecContext *avctx, uint8_t *dst,
337 int width, int height,
338 PutBitContext *pb, HuffEntry *he)
340 PTable counts[256] = { 0 };
343 count_usage(dst, width, height, counts);
345 for (i = 0; i < 256; i++) {
347 counts[i].value = 255 - i;
350 magy_huffman_compute_bits(counts, he, 256, 16);
354 for (i = 0; i < 256; i++) {
356 put_bits(pb, 7, he[i].len);
362 static int encode_slice(uint8_t *src, uint8_t *dst, int dst_size,
363 int width, int height, HuffEntry *he, int prediction)
369 init_put_bits(&pb, dst, dst_size);
372 put_bits(&pb, 8, prediction);
374 for (j = 0; j < height; j++) {
375 for (i = 0; i < width; i++) {
376 const int idx = src[i];
377 put_bits(&pb, he[idx].len, he[idx].code);
383 count = put_bits_count(&pb) & 0x1F;
386 put_bits(&pb, 32 - count, 0);
388 count = put_bits_count(&pb);
395 static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
396 const AVFrame *frame, int *got_packet)
398 MagicYUVContext *s = avctx->priv_data;
400 const int width = avctx->width, height = avctx->height;
401 int pos, slice, i, j, ret = 0;
403 ret = ff_alloc_packet2(avctx, pkt, (256 + 4 * s->nb_slices + width * height) *
408 bytestream2_init_writer(&pb, pkt->data, pkt->size);
409 bytestream2_put_le32(&pb, MKTAG('M', 'A', 'G', 'Y'));
410 bytestream2_put_le32(&pb, 32);
411 bytestream2_put_byte(&pb, 7);
412 bytestream2_put_byte(&pb, s->format);
413 bytestream2_put_byte(&pb, 0);
414 bytestream2_put_byte(&pb, 0);
415 bytestream2_put_le32(&pb, 0);
417 bytestream2_put_le32(&pb, avctx->width);
418 bytestream2_put_le32(&pb, avctx->height);
419 bytestream2_put_le32(&pb, avctx->width);
420 bytestream2_put_le32(&pb, avctx->height);
421 bytestream2_put_le32(&pb, 0);
423 for (i = 0; i < s->planes; i++) {
424 bytestream2_put_le32(&pb, 0);
425 for (j = 1; j < s->nb_slices; j++) {
426 bytestream2_put_le32(&pb, 0);
430 bytestream2_put_byte(&pb, s->planes);
432 for (i = 0; i < s->planes; i++) {
433 for (slice = 0; slice < s->nb_slices; slice++) {
434 bytestream2_put_byte(&pb, i);
440 AVFrame *p = av_frame_clone(frame);
446 for (i = 0; i < height; i++) {
447 s->llvidencdsp.diff_bytes(b, b, g, width);
448 s->llvidencdsp.diff_bytes(r, r, g, width);
454 FFSWAP(uint8_t*, p->data[0], p->data[1]);
455 FFSWAP(int, p->linesize[0], p->linesize[1]);
457 for (i = 0; i < s->planes; i++) {
458 for (slice = 0; slice < s->nb_slices; slice++) {
459 s->predict(s, p->data[i], s->slices[i], p->linesize[i],
460 p->width, p->height);
466 for (i = 0; i < s->planes; i++) {
467 for (slice = 0; slice < s->nb_slices; slice++) {
468 s->predict(s, frame->data[i], s->slices[i], frame->linesize[i],
469 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
470 AV_CEIL_RSHIFT(frame->height, s->vshift[i]));
475 init_put_bits(&s->pb, pkt->data + bytestream2_tell_p(&pb), bytestream2_get_bytes_left_p(&pb));
477 for (i = 0; i < s->planes; i++) {
478 encode_table(avctx, s->slices[i],
479 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
480 AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
483 s->tables_size = (put_bits_count(&s->pb) + 7) >> 3;
484 bytestream2_skip_p(&pb, s->tables_size);
486 for (i = 0; i < s->planes; i++) {
489 s->slice_pos[i] = bytestream2_tell_p(&pb);
490 slice_size = encode_slice(s->slices[i], pkt->data + bytestream2_tell_p(&pb),
491 bytestream2_get_bytes_left_p(&pb),
492 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
493 AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
494 s->he[i], s->frame_pred);
495 bytestream2_skip_p(&pb, slice_size);
498 pos = bytestream2_tell_p(&pb);
499 bytestream2_seek_p(&pb, 32, SEEK_SET);
500 bytestream2_put_le32(&pb, s->slice_pos[0] - 32);
501 for (i = 0; i < s->planes; i++) {
502 bytestream2_put_le32(&pb, s->slice_pos[i] - 32);
504 bytestream2_seek_p(&pb, pos, SEEK_SET);
506 pkt->size = bytestream2_tell_p(&pb);
507 pkt->flags |= AV_PKT_FLAG_KEY;
514 static av_cold int magy_encode_close(AVCodecContext *avctx)
516 MagicYUVContext *s = avctx->priv_data;
519 for (i = 0; i < s->planes; i++)
520 av_freep(&s->slices[i]);
525 #define OFFSET(x) offsetof(MagicYUVContext, x)
526 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
527 static const AVOption options[] = {
528 { "pred", "Prediction method", OFFSET(frame_pred), AV_OPT_TYPE_INT, {.i64=LEFT}, LEFT, MEDIAN, VE, "pred" },
529 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, 0, 0, VE, "pred" },
530 { "gradient", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = GRADIENT }, 0, 0, VE, "pred" },
531 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MEDIAN }, 0, 0, VE, "pred" },
535 static const AVClass magicyuv_class = {
536 .class_name = "magicyuv",
537 .item_name = av_default_item_name,
539 .version = LIBAVUTIL_VERSION_INT,
542 AVCodec ff_magicyuv_encoder = {
544 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
545 .type = AVMEDIA_TYPE_VIDEO,
546 .id = AV_CODEC_ID_MAGICYUV,
547 .priv_data_size = sizeof(MagicYUVContext),
548 .priv_class = &magicyuv_class,
549 .init = magy_encode_init,
550 .close = magy_encode_close,
551 .encode2 = magy_encode_frame,
552 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
553 .pix_fmts = (const enum AVPixelFormat[]) {
554 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_YUV422P,
555 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_GRAY8,