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 {
66 unsigned slice_pos[4];
69 LLVidEncDSPContext llvidencdsp;
70 void (*predict)(struct MagicYUVContext *s, uint8_t *src, uint8_t *dst,
71 ptrdiff_t stride, int width, int height);
74 static void left_predict(MagicYUVContext *s,
75 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
76 int width, int height)
81 for (i = 0; i < width; i++) {
82 dst[i] = src[i] - prev;
87 for (j = 1; j < height; j++) {
89 for (i = 0; i < width; i++) {
90 dst[i] = src[i] - prev;
98 static void gradient_predict(MagicYUVContext *s,
99 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
100 int width, int height)
102 int left = 0, top, lefttop;
105 for (i = 0; i < width; i++) {
106 dst[i] = src[i] - left;
111 for (j = 1; j < height; j++) {
115 for (i = 1; i < width; i++) {
116 top = src[i - stride];
117 lefttop = src[i - (stride + 1)];
119 dst[i] = (src[i] - top) - left + lefttop;
126 static void median_predict(MagicYUVContext *s,
127 uint8_t *src, uint8_t *dst, ptrdiff_t stride,
128 int width, int height)
130 int left = 0, lefttop;
133 for (i = 0; i < width; i++) {
134 dst[i] = src[i] - left;
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);
147 static av_cold int magy_encode_init(AVCodecContext *avctx)
149 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;
217 avctx->extradata_size = 32;
219 avctx->extradata = av_mallocz(avctx->extradata_size +
220 AV_INPUT_BUFFER_PADDING_SIZE);
222 if (!avctx->extradata) {
223 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata.\n");
224 return AVERROR(ENOMEM);
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);
235 bytestream2_put_byte(&pb, 0);
236 bytestream2_put_byte(&pb, 0);
237 bytestream2_put_byte(&pb, 32);
238 bytestream2_put_byte(&pb, 0);
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);
248 static int magy_huff_cmp_len(const void *a, const void *b)
250 const HuffEntry *aa = a, *bb = b;
251 return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
254 static int huff_cmp_sym(const void *a, const void *b)
256 const HuffEntry *aa = a, *bb = b;
257 return bb->sym - aa->sym;
260 static void calculate_codes(HuffEntry *he)
265 AV_QSORT(he, 256, HuffEntry, magy_huff_cmp_len);
268 for (i = 255; i >= 0; i--) {
269 he[i].code = code >> (32 - he[i].len);
270 code += 0x80000000u >> (he[i].len - 1);
273 AV_QSORT(he, 256, HuffEntry, huff_cmp_sym);
276 static void count_usage(uint8_t *src, int width,
277 int height, PTable *counts)
281 for (j = 0; j < height; j++) {
282 for (i = 0; i < width; i++) {
283 counts[src[i]].prob++;
289 typedef struct PackageMergerList {
290 int nitems; ///< number of items in the list and probability ex. 4
291 int item_idx[515]; ///< index range for each item in items 0, 2, 5, 9, 13
292 int probability[514]; ///< probability of each item 3, 8, 18, 46
293 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
296 static int compare_by_prob(const void *a, const void *b)
298 PTable a_val = *(PTable *)a;
299 PTable b_val = *(PTable *)b;
300 return a_val.prob - b_val.prob;
303 static void magy_huffman_compute_bits(PTable *prob_table, HuffEntry *distincts,
304 int size, int max_length)
306 PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
308 int nbits[257] = {0};
311 av_assert0(max_length > 0);
316 from->item_idx[0] = 0;
317 AV_QSORT(prob_table, size, PTable, compare_by_prob);
319 for (times = 0; times <= max_length; times++) {
326 if (times < max_length) {
329 while (i < size || j + 1 < from->nitems) {
331 to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
333 (j + 1 >= from->nitems ||
335 from->probability[j] + from->probability[j + 1])) {
336 to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
337 to->probability[to->nitems - 1] = prob_table[i].prob;
340 for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
341 to->items[to->item_idx[to->nitems]++] = from->items[k];
343 to->probability[to->nitems - 1] =
344 from->probability[j] + from->probability[j + 1];
353 min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
354 for (i = 0; i < from->item_idx[min]; i++) {
355 nbits[from->items[i]]++;
358 for (i = 0; i < size; i++) {
359 distincts[i].sym = i;
360 distincts[i].len = nbits[i];
364 static int encode_table(AVCodecContext *avctx, uint8_t *dst,
365 int width, int height,
366 PutBitContext *pb, HuffEntry *he)
368 PTable counts[256] = { {0} };
371 count_usage(dst, width, height, counts);
373 for (i = 0; i < 256; i++) {
375 counts[i].value = 255 - i;
378 magy_huffman_compute_bits(counts, he, 256, 12);
382 for (i = 0; i < 256; i++) {
384 put_bits(pb, 7, he[i].len);
390 static int encode_slice(uint8_t *src, uint8_t *dst, int dst_size,
391 int width, int height, HuffEntry *he, int prediction)
397 init_put_bits(&pb, dst, dst_size);
400 put_bits(&pb, 8, prediction);
402 for (j = 0; j < height; j++) {
403 for (i = 0; i < width; i++) {
404 const int idx = src[i];
405 put_bits(&pb, he[idx].len, he[idx].code);
411 count = put_bits_count(&pb) & 0x1F;
414 put_bits(&pb, 32 - count, 0);
416 count = put_bits_count(&pb);
423 static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
424 const AVFrame *frame, int *got_packet)
426 MagicYUVContext *s = avctx->priv_data;
428 const int width = avctx->width, height = avctx->height;
429 int pos, slice, i, j, ret = 0;
431 ret = ff_alloc_packet2(avctx, pkt, (256 + 4 * s->nb_slices + width * height) *
436 bytestream2_init_writer(&pb, pkt->data, pkt->size);
437 bytestream2_put_le32(&pb, MKTAG('M', 'A', 'G', 'Y'));
438 bytestream2_put_le32(&pb, 32); // header size
439 bytestream2_put_byte(&pb, 7); // version
440 bytestream2_put_byte(&pb, s->format);
441 bytestream2_put_byte(&pb, 12); // max huffman length
442 bytestream2_put_byte(&pb, 0);
444 bytestream2_put_byte(&pb, 0);
445 bytestream2_put_byte(&pb, 0);
446 bytestream2_put_byte(&pb, 32); // coder type
447 bytestream2_put_byte(&pb, 0);
449 bytestream2_put_le32(&pb, avctx->width);
450 bytestream2_put_le32(&pb, avctx->height);
451 bytestream2_put_le32(&pb, avctx->width);
452 bytestream2_put_le32(&pb, avctx->height);
453 bytestream2_put_le32(&pb, 0);
455 for (i = 0; i < s->planes; i++) {
456 bytestream2_put_le32(&pb, 0);
457 for (j = 1; j < s->nb_slices; j++) {
458 bytestream2_put_le32(&pb, 0);
462 bytestream2_put_byte(&pb, s->planes);
464 for (i = 0; i < s->planes; i++) {
465 for (slice = 0; slice < s->nb_slices; slice++) {
466 bytestream2_put_byte(&pb, i);
472 AVFrame *p = av_frame_clone(frame);
478 for (i = 0; i < height; i++) {
479 s->llvidencdsp.diff_bytes(b, b, g, width);
480 s->llvidencdsp.diff_bytes(r, r, g, width);
486 FFSWAP(uint8_t*, p->data[0], p->data[1]);
487 FFSWAP(int, p->linesize[0], p->linesize[1]);
489 for (i = 0; i < s->planes; i++) {
490 for (slice = 0; slice < s->nb_slices; slice++) {
491 s->predict(s, p->data[i], s->slices[i], p->linesize[i],
492 p->width, p->height);
498 for (i = 0; i < s->planes; i++) {
499 for (slice = 0; slice < s->nb_slices; slice++) {
500 s->predict(s, frame->data[i], s->slices[i], frame->linesize[i],
501 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
502 AV_CEIL_RSHIFT(frame->height, s->vshift[i]));
507 init_put_bits(&s->pb, pkt->data + bytestream2_tell_p(&pb), bytestream2_get_bytes_left_p(&pb));
509 for (i = 0; i < s->planes; i++) {
510 encode_table(avctx, s->slices[i],
511 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
512 AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
515 s->tables_size = (put_bits_count(&s->pb) + 7) >> 3;
516 bytestream2_skip_p(&pb, s->tables_size);
518 for (i = 0; i < s->planes; i++) {
521 s->slice_pos[i] = bytestream2_tell_p(&pb);
522 slice_size = encode_slice(s->slices[i], pkt->data + bytestream2_tell_p(&pb),
523 bytestream2_get_bytes_left_p(&pb),
524 AV_CEIL_RSHIFT(frame->width, s->hshift[i]),
525 AV_CEIL_RSHIFT(frame->height, s->vshift[i]),
526 s->he[i], s->frame_pred);
527 bytestream2_skip_p(&pb, slice_size);
530 pos = bytestream2_tell_p(&pb);
531 bytestream2_seek_p(&pb, 32, SEEK_SET);
532 bytestream2_put_le32(&pb, s->slice_pos[0] - 32);
533 for (i = 0; i < s->planes; i++) {
534 bytestream2_put_le32(&pb, s->slice_pos[i] - 32);
536 bytestream2_seek_p(&pb, pos, SEEK_SET);
538 pkt->size = bytestream2_tell_p(&pb);
539 pkt->flags |= AV_PKT_FLAG_KEY;
546 static av_cold int magy_encode_close(AVCodecContext *avctx)
548 MagicYUVContext *s = avctx->priv_data;
551 for (i = 0; i < s->planes; i++)
552 av_freep(&s->slices[i]);
557 #define OFFSET(x) offsetof(MagicYUVContext, x)
558 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
559 static const AVOption options[] = {
560 { "pred", "Prediction method", OFFSET(frame_pred), AV_OPT_TYPE_INT, {.i64=LEFT}, LEFT, MEDIAN, VE, "pred" },
561 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, 0, 0, VE, "pred" },
562 { "gradient", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = GRADIENT }, 0, 0, VE, "pred" },
563 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MEDIAN }, 0, 0, VE, "pred" },
567 static const AVClass magicyuv_class = {
568 .class_name = "magicyuv",
569 .item_name = av_default_item_name,
571 .version = LIBAVUTIL_VERSION_INT,
574 AVCodec ff_magicyuv_encoder = {
576 .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
577 .type = AVMEDIA_TYPE_VIDEO,
578 .id = AV_CODEC_ID_MAGICYUV,
579 .priv_data_size = sizeof(MagicYUVContext),
580 .priv_class = &magicyuv_class,
581 .init = magy_encode_init,
582 .close = magy_encode_close,
583 .encode2 = magy_encode_frame,
584 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
585 .pix_fmts = (const enum AVPixelFormat[]) {
586 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_YUV422P,
587 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_GRAY8,