Hi, patch attached.
From f9f538cc6253f892bb04b7c9e46189194b6f9be4 Mon Sep 17 00:00:00 2001 From: Paul B Mahol <one...@gmail.com> Date: Sun, 22 May 2016 15:18:30 +0200 Subject: [PATCH] avocdec: add MagicYUV decoder
Signed-off-by: Paul B Mahol <one...@gmail.com> --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 + libavcodec/magicyuv.c | 437 ++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/riff.c | 1 + 6 files changed, 448 insertions(+) create mode 100644 libavcodec/magicyuv.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 224ccf7..733a82d 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -348,6 +348,7 @@ OBJS-$(CONFIG_LOCO_DECODER) += loco.o OBJS-$(CONFIG_M101_DECODER) += m101.o OBJS-$(CONFIG_MACE3_DECODER) += mace.o OBJS-$(CONFIG_MACE6_DECODER) += mace.o +OBJS-$(CONFIG_MAGICYUV_DECODER) += magicyuv.o OBJS-$(CONFIG_MDEC_DECODER) += mdec.o mpeg12.o mpeg12data.o OBJS-$(CONFIG_METASOUND_DECODER) += metasound.o metasound_data.o \ twinvq.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 6d0a7e7..7aa54ee 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -219,6 +219,7 @@ void avcodec_register_all(void) REGISTER_ENCODER(LJPEG, ljpeg); REGISTER_DECODER(LOCO, loco); REGISTER_DECODER(M101, m101); + REGISTER_DECODER(MAGICYUV, magicyuv); REGISTER_DECODER(MDEC, mdec); REGISTER_DECODER(MIMIC, mimic); REGISTER_ENCDEC (MJPEG, mjpeg); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 00d0ef8..339a1b2 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -406,6 +406,7 @@ enum AVCodecID { AV_CODEC_ID_CFHD, AV_CODEC_ID_TRUEMOTION2RT, AV_CODEC_ID_M101, + AV_CODEC_ID_MAGICYUV, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 077b2d4..5fd946e 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1542,6 +1542,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("Duck TrueMotion 2.0 Real Time"), .props = AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_MAGICYUV, + .type = AVMEDIA_TYPE_VIDEO, + .name = "magicyuv", + .long_name = NULL_IF_CONFIG_SMALL("MagicYUV Lossless Video"), + .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, + }, /* various PCM "codecs" */ { diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c new file mode 100644 index 0000000..2a161d9 --- /dev/null +++ b/libavcodec/magicyuv.c @@ -0,0 +1,437 @@ +/* + * MagicYUV decoder + * Copyright (c) 2016 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "avcodec.h" +#include "bytestream.h" +#include "get_bits.h" +#include "huffyuvdsp.h" +#include "internal.h" +#include "thread.h" + +typedef struct Slice { + uint32_t start; + uint32_t size; +} Slice; + +typedef enum Prediction { + LEFT = 1, + GRADIENT, + MEDIAN, +} Prediction; + +typedef struct MagicYUVContext { + AVFrame *p; + int slice_height; + int nb_slices; + int planes; + uint8_t *buf; + int hshift[4]; + int vshift[4]; + Slice *slices[4]; + int slices_size[4]; + uint8_t freq[4][256]; + VLC vlc[4]; + HuffYUVDSPContext hdsp; +} MagicYUVContext; + +static av_cold int decode_init(AVCodecContext *avctx) +{ + MagicYUVContext *s = avctx->priv_data; + ff_huffyuvdsp_init(&s->hdsp); + return 0; +} + +typedef struct HuffEntry { + uint8_t sym; + uint8_t len; + uint32_t code; +} HuffEntry; + +static int ff_magy_huff_cmp_len(const void *a, const void *b) +{ + const HuffEntry *aa = a, *bb = b; + return (aa->len - bb->len) * 256 + aa->sym - bb->sym; +} + +static int build_huff(VLC *vlc, uint8_t *freq) +{ + HuffEntry he[256]; + uint32_t codes[256]; + uint8_t bits[256]; + uint8_t syms[256]; + uint32_t code; + int i, last; + + for (i = 0; i < 256; i++) { + he[i].sym = 255 - i; + he[i].len = freq[i]; + } + qsort(he, 256, sizeof(*he), ff_magy_huff_cmp_len); + + last = 255; + while (he[last].len == 255 && last) + last--; + + code = 1; + for (i = last; i >= 0; i--) { + codes[i] = code >> (32 - he[i].len); + bits[i] = he[i].len; + syms[i] = he[i].sym; + code += 0x80000000u >> (he[i].len - 1); + } + + ff_free_vlc(vlc); + return ff_init_vlc_sparse(vlc, 12, 256, + bits, sizeof(*bits), sizeof(*bits), + codes, sizeof(*codes), sizeof(*codes), + syms, sizeof(*syms), sizeof(*syms), 0); +} + +static int decode_slice(AVCodecContext *avctx, void *tdata, + int j, int threadnr) +{ + MagicYUVContext *s = avctx->priv_data; + AVFrame *p = s->p; + int i, k, x, ret; + GetBitContext b; + uint8_t *dst; + + for (i = 0; i < s->planes; i++) { + int stride = p->linesize[i]; + int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]); + int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]); + int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]); + int pred; + + if ((ret = init_get_bits8(&b, s->buf + s->slices[i][j].start, s->slices[i][j].size)) < 0) + return ret; + + if (get_bits(&b, 8)) { + return AVERROR_INVALIDDATA; + } + pred = get_bits(&b, 8); + dst = p->data[i] + j * sheight * stride; + for (k = 0; k < height; k++) { + for (x = 0; x < width; x++) { + int pix; + if (get_bits_left(&b) <= 0) { + return AVERROR_INVALIDDATA; + } + pix = get_vlc2(&b, s->vlc[i].table, s->vlc[i].bits, 3); + if (pix < 0) { + return AVERROR_INVALIDDATA; + } + dst[x] = 255 - pix; + } + dst += stride; + } + + if (pred == LEFT) { + dst = p->data[i] + j * sheight * stride; + s->hdsp.add_hfyu_left_pred(dst, dst, width, 0); + dst += stride; + for (k = 1; k < height; k++) { + s->hdsp.add_hfyu_left_pred(dst, dst, width, dst[-stride]); + dst += stride; + } + } else if (pred == GRADIENT) { + int left, lefttop, top; + + dst = p->data[i] + j * sheight * stride; + s->hdsp.add_hfyu_left_pred(dst, dst, width, 0); + left = lefttop = 0; + dst += stride; + for (k = 1; k < height; k++) { + top = dst[-stride]; + left = top + dst[0]; + dst[0] = left; + for (x = 1; x < width; x++) { + top = dst[x - stride]; + lefttop = dst[x - (stride + 1)]; + left += top - lefttop + dst[x]; + dst[x] = left; + } + dst += stride; + } + } else if (pred == MEDIAN) { + int left, lefttop; + + dst = p->data[i] + j * sheight * stride; + lefttop = left = dst[0]; + s->hdsp.add_hfyu_left_pred(dst, dst, width, 0); + dst += stride; + for (k = 1; k < height; k++) { + s->hdsp.add_hfyu_median_pred(dst, dst - stride, dst, width, &left, &lefttop); + lefttop = left = dst[0]; + dst += stride; + } + } else { + avpriv_request_sample(avctx, "unknown prediction: %d", pred); + return AVERROR_PATCHWELCOME; + } + } + + return 0; +} + +static int decode_frame(AVCodecContext *avctx, + void *data, int *got_frame, + AVPacket *avpkt) +{ + int i, j, k, x, header_size, slice_width; + int ret, format, version, table_size, decorrelate = 0; + uint32_t first_offset, offset, next_offset; + MagicYUVContext *s = avctx->priv_data; + ThreadFrame frame = { .f = data }; + AVFrame *p = data; + GetByteContext gb; + GetBitContext b; + + bytestream2_init(&gb, avpkt->data, avpkt->size); + if (bytestream2_get_le32(&gb) != MKTAG('M','A','G','Y')) + return AVERROR_INVALIDDATA; + + header_size = bytestream2_get_le32(&gb); + if (header_size < 32 || header_size >= avpkt->size) + return AVERROR_INVALIDDATA; + + version = bytestream2_get_byte(&gb); + if (version != 7) { + avpriv_request_sample(avctx, "unsupported version: %d", version); + return AVERROR_PATCHWELCOME; + } + + s->hshift[1] = s->vshift[1] = 0; + s->hshift[2] = s->vshift[2] = 0; + + format = bytestream2_get_byte(&gb); + switch (format) { + case 0x65: + avctx->pix_fmt = AV_PIX_FMT_GBRP; + decorrelate = 1; + s->planes = 3; + break; + case 0x66: + avctx->pix_fmt = AV_PIX_FMT_GBRAP; + decorrelate = 1; + s->planes = 4; + break; + case 0x67: + avctx->pix_fmt = AV_PIX_FMT_YUV444P; + s->planes = 3; + break; + case 0x68: + avctx->pix_fmt = AV_PIX_FMT_YUV422P; + s->planes = 3; + s->hshift[1] = s->hshift[2] = 1; + break; + case 0x69: + avctx->pix_fmt = AV_PIX_FMT_YUV420P; + s->planes = 3; + s->hshift[1] = s->vshift[1] = 1; + s->hshift[2] = s->vshift[2] = 1; + break; + case 0x6b: + avctx->pix_fmt = AV_PIX_FMT_GRAY8; + s->planes = 1; + break; + default: + avpriv_request_sample(avctx, "unsupported format: 0x%X", format); + return AVERROR_PATCHWELCOME; + } + + bytestream2_skip(&gb, 6); + + avctx->coded_width = bytestream2_get_le32(&gb); + avctx->coded_height = bytestream2_get_le32(&gb); + slice_width = bytestream2_get_le32(&gb); + if (slice_width != avctx->coded_width) { + avpriv_request_sample(avctx, "unsupported slice width: %d", slice_width); + return AVERROR_PATCHWELCOME; + } + s->slice_height = bytestream2_get_le32(&gb); + bytestream2_skip(&gb, 4); + + s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height; + if (s->nb_slices > INT_MAX / sizeof(Slice)) { + av_log(avctx, AV_LOG_ERROR, "invalid number of slices\n"); + return AVERROR_INVALIDDATA; + } + + for (i = 0; i < s->planes; i++) { + av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice)); + if (!s->slices[i]) + return AVERROR(ENOMEM); + + offset = bytestream2_get_le32(&gb); + if (offset > avpkt->size - header_size) + return AVERROR_INVALIDDATA; + + if (i == 0) + first_offset = offset; + + for (j = 0; j < s->nb_slices - 1; j++) { + s->slices[i][j].start = offset + header_size; + next_offset = bytestream2_get_le32(&gb); + s->slices[i][j].size = next_offset - offset; + offset = next_offset; + + if (offset > avpkt->size - header_size) + return AVERROR_INVALIDDATA; + } + + s->slices[i][j].start = offset + header_size; + s->slices[i][j].size = avpkt->size - offset; + } + + if (bytestream2_get_byte(&gb) != s->planes) + return AVERROR_INVALIDDATA; + + bytestream2_skip(&gb, s->nb_slices * s->planes); + + table_size = header_size + first_offset - bytestream2_tell(&gb); + if (table_size < 2) + return AVERROR_INVALIDDATA; + + if ((ret = init_get_bits8(&b, avpkt->data + bytestream2_tell(&gb), table_size)) < 0) + return ret; + + memset(s->freq, 0, sizeof(s->freq)); + j = i = 0; + while (get_bits_left(&b) >= 8) { + int l = get_bits(&b, 4); + int x = get_bits(&b, 4); + int L = get_bitsz(&b, l) + 1; + + for (k = 0; k < L; k++) { + if (j + k < 256) + s->freq[i][j + k] = x; + } + + j += L; + if (j == 256) { + j = 0; + if (build_huff(&s->vlc[i], s->freq[i])) { + av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n"); + return AVERROR_INVALIDDATA; + } + i++; + if (i == s->planes) { + break; + } + } else if (j > 256) { + return AVERROR_INVALIDDATA; + } + } + + p->pict_type = AV_PICTURE_TYPE_I; + p->key_frame = 1; + + if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) + return ret; + + s->buf = avpkt->data; + s->p = p; + avctx->execute2(avctx, decode_slice, NULL, NULL, s->nb_slices); + + if (decorrelate) { + uint8_t *b = p->data[0]; + uint8_t *g = p->data[1]; + uint8_t *r = p->data[2]; + + for (i = 0; i < p->height; i++) { + for (x = 0; x < p->width; x++) { + b[x] += g[x]; + r[x] += g[x]; + } + b += p->linesize[0]; + g += p->linesize[1]; + r += p->linesize[2]; + } + } + + if (avctx->pix_fmt == AV_PIX_FMT_GBRP || + avctx->pix_fmt == AV_PIX_FMT_GBRAP) { + FFSWAP(uint8_t*, p->data[0], p->data[1]); + FFSWAP(int, p->linesize[0], p->linesize[1]); + } + + *got_frame = 1; + + if (ret < 0) + return ret; + return avpkt->size; +} + +#if HAVE_THREADS +static int decode_init_thread_copy(AVCodecContext *avctx) +{ + MagicYUVContext *s = avctx->priv_data; + + s->slices[0] = 0; + s->slices[1] = 0; + s->slices[2] = 0; + s->slices[3] = 0; + s->slices_size[0] = 0; + s->slices_size[1] = 0; + s->slices_size[2] = 0; + s->slices_size[3] = 0; + + return 0; +} +#endif + +static av_cold int decode_end(AVCodecContext *avctx) +{ + MagicYUVContext * const s = avctx->priv_data; + + av_freep(&s->slices[0]); + av_freep(&s->slices[1]); + av_freep(&s->slices[2]); + av_freep(&s->slices[3]); + s->slices_size[0] = 0; + s->slices_size[1] = 0; + s->slices_size[2] = 0; + s->slices_size[3] = 0; + ff_free_vlc(&s->vlc[0]); + ff_free_vlc(&s->vlc[1]); + ff_free_vlc(&s->vlc[2]); + ff_free_vlc(&s->vlc[3]); + + return 0; +} + +AVCodec ff_magicyuv_decoder = { + .name = "magicyuv", + .long_name = NULL_IF_CONFIG_SMALL("MagicYUV Lossless Video"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_MAGICYUV, + .priv_data_size = sizeof(MagicYUVContext), + .init = decode_init, + .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy), + .close = decode_end, + .decode = decode_frame, + .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS, +}; diff --git a/libavformat/riff.c b/libavformat/riff.c index 6280e90..5311b54 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -421,6 +421,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { { AV_CODEC_ID_CFHD, MKTAG('C', 'F', 'H', 'D') }, { AV_CODEC_ID_M101, MKTAG('M', '1', '0', '1') }, { AV_CODEC_ID_M101, MKTAG('M', '1', '0', '2') }, + { AV_CODEC_ID_MAGICYUV, MKTAG('M', 'A', 'G', 'Y') }, { AV_CODEC_ID_NONE, 0 } }; -- 2.5.0
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel