Browse Source

avcodec: add mvha video decoder

tags/n4.3
Paul B Mahol 6 years ago
parent
commit
ed58f8475f
9 changed files with 324 additions and 1 deletions
  1. +1
    -0
      Changelog
  2. +1
    -0
      configure
  3. +1
    -0
      libavcodec/Makefile
  4. +1
    -0
      libavcodec/allcodecs.c
  5. +1
    -0
      libavcodec/avcodec.h
  6. +7
    -0
      libavcodec/codec_desc.c
  7. +310
    -0
      libavcodec/mvha.c
  8. +1
    -1
      libavcodec/version.h
  9. +1
    -0
      libavformat/riff.c

+ 1
- 0
Changelog View File

@@ -26,6 +26,7 @@ version <next>:
- AV1 Annex B demuxer - AV1 Annex B demuxer
- axcorrelate filter - axcorrelate filter
- mvdv decoder - mvdv decoder
- mvha decoder




version 4.2: version 4.2:


+ 1
- 0
configure View File

@@ -2779,6 +2779,7 @@ msmpeg4v3_decoder_select="h263_decoder"
msmpeg4v3_encoder_select="h263_encoder" msmpeg4v3_encoder_select="h263_encoder"
mss2_decoder_select="mpegvideo qpeldsp vc1_decoder" mss2_decoder_select="mpegvideo qpeldsp vc1_decoder"
mts2_decoder_select="mss34dsp" mts2_decoder_select="mss34dsp"
mvha_decoder_deps="zlib"
mwsc_decoder_deps="zlib" mwsc_decoder_deps="zlib"
mxpeg_decoder_select="mjpeg_decoder" mxpeg_decoder_select="mjpeg_decoder"
nellymoser_decoder_select="mdct sinewin" nellymoser_decoder_select="mdct sinewin"


+ 1
- 0
libavcodec/Makefile View File

@@ -494,6 +494,7 @@ OBJS-$(CONFIG_MTS2_DECODER) += mss4.o
OBJS-$(CONFIG_MVC1_DECODER) += mvcdec.o OBJS-$(CONFIG_MVC1_DECODER) += mvcdec.o
OBJS-$(CONFIG_MVC2_DECODER) += mvcdec.o OBJS-$(CONFIG_MVC2_DECODER) += mvcdec.o
OBJS-$(CONFIG_MVDV_DECODER) += midivid.o OBJS-$(CONFIG_MVDV_DECODER) += midivid.o
OBJS-$(CONFIG_MVHA_DECODER) += mvha.o
OBJS-$(CONFIG_MWSC_DECODER) += mwsc.o OBJS-$(CONFIG_MWSC_DECODER) += mwsc.o
OBJS-$(CONFIG_MXPEG_DECODER) += mxpegdec.o OBJS-$(CONFIG_MXPEG_DECODER) += mxpegdec.o
OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o


+ 1
- 0
libavcodec/allcodecs.c View File

@@ -219,6 +219,7 @@ extern AVCodec ff_mts2_decoder;
extern AVCodec ff_mvc1_decoder; extern AVCodec ff_mvc1_decoder;
extern AVCodec ff_mvc2_decoder; extern AVCodec ff_mvc2_decoder;
extern AVCodec ff_mvdv_decoder; extern AVCodec ff_mvdv_decoder;
extern AVCodec ff_mvha_decoder;
extern AVCodec ff_mwsc_decoder; extern AVCodec ff_mwsc_decoder;
extern AVCodec ff_mxpeg_decoder; extern AVCodec ff_mxpeg_decoder;
extern AVCodec ff_nuv_decoder; extern AVCodec ff_nuv_decoder;


+ 1
- 0
libavcodec/avcodec.h View File

@@ -459,6 +459,7 @@ enum AVCodecID {
AV_CODEC_ID_VP4, AV_CODEC_ID_VP4,
AV_CODEC_ID_IMM5, AV_CODEC_ID_IMM5,
AV_CODEC_ID_MVDV, AV_CODEC_ID_MVDV,
AV_CODEC_ID_MVHA,


/* various PCM "codecs" */ /* various PCM "codecs" */
AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs


+ 7
- 0
libavcodec/codec_desc.c View File

@@ -1740,6 +1740,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("MidiVid VQ"), .long_name = NULL_IF_CONFIG_SMALL("MidiVid VQ"),
.props = AV_CODEC_PROP_LOSSY, .props = AV_CODEC_PROP_LOSSY,
}, },
{
.id = AV_CODEC_ID_MVHA,
.type = AVMEDIA_TYPE_VIDEO,
.name = "mvha",
.long_name = NULL_IF_CONFIG_SMALL("MidiVid Archive Codec"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
},


/* various PCM "codecs" */ /* various PCM "codecs" */
{ {


+ 310
- 0
libavcodec/mvha.c View File

@@ -0,0 +1,310 @@
/*
* MidiVid Archive codec
*
* Copyright (c) 2019 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>

#define CACHED_BITSTREAM_READER !ARCH_X86_32
#include "libavutil/intreadwrite.h"

#include "avcodec.h"
#include "bytestream.h"
#include "get_bits.h"
#include "internal.h"
#include "lossless_videodsp.h"

#include <zlib.h>

typedef struct MVHAContext {
GetBitContext gb;
int nb_symbols;

uint8_t symb[256];
uint32_t prob[256];
VLC vlc;

z_stream zstream;
LLVidDSPContext llviddsp;
} MVHAContext;

typedef struct Node {
int16_t sym;
int16_t n0;
int16_t l, r;
uint32_t count;
} Node;

static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
Node *nodes, int node,
uint32_t pfx, int pl, int *pos)
{
int s;

s = nodes[node].sym;
if (s != -1) {
bits[*pos] = (~pfx) & ((1ULL << FFMAX(pl, 1)) - 1);
lens[*pos] = FFMAX(pl, 1);
xlat[*pos] = s + (pl == 0);
(*pos)++;
} else {
pfx <<= 1;
pl++;
get_tree_codes(bits, lens, xlat, nodes, nodes[node].l, pfx, pl,
pos);
pfx |= 1;
get_tree_codes(bits, lens, xlat, nodes, nodes[node].r, pfx, pl,
pos);
}
}

static int build_vlc(AVCodecContext *avctx, VLC *vlc)
{
MVHAContext *s = avctx->priv_data;
Node nodes[512];
uint32_t bits[256];
int16_t lens[256];
uint8_t xlat[256];
int cur_node, i, j, pos = 0;

ff_free_vlc(vlc);

for (i = 0; i < s->nb_symbols; i++) {
nodes[i].count = s->prob[i];
nodes[i].sym = s->symb[i];
nodes[i].n0 = -2;
nodes[i].l = i;
nodes[i].r = i;
}

cur_node = s->nb_symbols;
j = 0;
do {
for (i = 0; ; i++) {
int new_node = j;
int first_node = cur_node;
int second_node = cur_node;
unsigned nd, st;

nodes[cur_node].count = -1;

do {
int val = nodes[new_node].count;
if (val && (val < nodes[first_node].count)) {
if (val >= nodes[second_node].count) {
first_node = new_node;
} else {
first_node = second_node;
second_node = new_node;
}
}
new_node += 1;
} while (new_node != cur_node);

if (first_node == cur_node)
break;

nd = nodes[second_node].count;
st = nodes[first_node].count;
nodes[second_node].count = 0;
nodes[first_node].count = 0;
if (nd >= UINT32_MAX - st) {
av_log(avctx, AV_LOG_ERROR, "count overflow\n");
return AVERROR_INVALIDDATA;
}
nodes[cur_node].count = nd + st;
nodes[cur_node].sym = -1;
nodes[cur_node].n0 = cur_node;
nodes[cur_node].l = first_node;
nodes[cur_node].r = second_node;
cur_node++;
}
j++;
} while (cur_node - s->nb_symbols == j);

get_tree_codes(bits, lens, xlat, nodes, cur_node - 1, 0, 0, &pos);

return ff_init_vlc_sparse(vlc, 12, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
}

static int decode_frame(AVCodecContext *avctx,
void *data, int *got_frame,
AVPacket *avpkt)
{
MVHAContext *s = avctx->priv_data;
AVFrame *frame = data;
uint32_t type, size;
int ret;

if (avpkt->size <= 8)
return AVERROR_INVALIDDATA;

type = AV_RB32(avpkt->data);
size = AV_RL32(avpkt->data + 4);

if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
return ret;

if (type == MKTAG('L','Z','Y','V')) {
ret = inflateReset(&s->zstream);
if (ret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
return AVERROR_EXTERNAL;
}

s->zstream.next_in = avpkt->data + 8;
s->zstream.avail_in = avpkt->size - 8;

for (int p = 0; p < 3; p++) {
for (int y = 0; y < avctx->height; y++) {
s->zstream.next_out = frame->data[p] + (avctx->height - y - 1) * frame->linesize[p];
s->zstream.avail_out = avctx->width >> (p > 0);

ret = inflate(&s->zstream, Z_SYNC_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
return AVERROR_EXTERNAL;
}
}
}
} else if (type == MKTAG('H','U','F','Y')) {
GetBitContext *gb = &s->gb;
int first_symbol, symbol;

init_get_bits8(gb, avpkt->data + 8, avpkt->size - 8);

skip_bits(gb, 24);

first_symbol = get_bits(gb, 8);
s->nb_symbols = get_bits(gb, 8) + 1;

symbol = first_symbol;
for (int i = 0; i < s->nb_symbols; symbol++) {
int prob;

if (get_bits1(gb)) {
prob = get_bits(gb, 12);
} else {
prob = get_bits(gb, 3);
}

if (prob) {
s->symb[i] = symbol;
s->prob[i] = prob;
i++;
}
}

ret = build_vlc(avctx, &s->vlc);
if (ret < 0)
return ret;

for (int p = 0; p < 3; p++) {
int width = avctx->width >> (p > 0);
ptrdiff_t stride = frame->linesize[p];
uint8_t *dst;

dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
for (int y = 0; y < avctx->height; y++) {
for (int x = 0; x < width; x++) {
int v = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);

if (v < 0)
return AVERROR_INVALIDDATA;

dst[x] = v;
}
dst -= stride;
}
}
} else {
return AVERROR_INVALIDDATA;
}

for (int p = 0; p < 3; p++) {
int left, lefttop;
int width = avctx->width >> (p > 0);
ptrdiff_t stride = frame->linesize[p];
uint8_t *dst;

dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
s->llviddsp.add_left_pred(dst, dst, width, 0);
dst -= stride;
lefttop = left = dst[0];
for (int y = 1; y < avctx->height; y++) {
s->llviddsp.add_median_pred(dst, dst + stride, dst, width, &left, &lefttop);
lefttop = left = dst[0];
dst -= stride;
}
}

frame->pict_type = AV_PICTURE_TYPE_I;
frame->key_frame = 1;
*got_frame = 1;

return avpkt->size;
}

static av_cold int decode_init(AVCodecContext *avctx)
{
MVHAContext *s = avctx->priv_data;
int zret;

avctx->pix_fmt = AV_PIX_FMT_YUV422P;

s->zstream.zalloc = Z_NULL;
s->zstream.zfree = Z_NULL;
s->zstream.opaque = Z_NULL;
zret = inflateInit(&s->zstream);
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
return AVERROR_EXTERNAL;
}

ff_llviddsp_init(&s->llviddsp);

return 0;
}

static av_cold int decode_close(AVCodecContext *avctx)
{
MVHAContext *s = avctx->priv_data;

inflateEnd(&s->zstream);
ff_free_vlc(&s->vlc);

return 0;
}

AVCodec ff_mvha_decoder = {
.name = "mvha",
.long_name = NULL_IF_CONFIG_SMALL("MidiVid Archive Codec"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_MVHA,
.priv_data_size = sizeof(MVHAContext),
.init = decode_init,
.close = decode_close,
.decode = decode_frame,
.capabilities = AV_CODEC_CAP_DR1,
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
FF_CODEC_CAP_INIT_CLEANUP,
};

+ 1
- 1
libavcodec/version.h View File

@@ -28,7 +28,7 @@
#include "libavutil/version.h" #include "libavutil/version.h"


#define LIBAVCODEC_VERSION_MAJOR 58 #define LIBAVCODEC_VERSION_MAJOR 58
#define LIBAVCODEC_VERSION_MINOR 63
#define LIBAVCODEC_VERSION_MINOR 64
#define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_MICRO 100


#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \


+ 1
- 0
libavformat/riff.c View File

@@ -490,6 +490,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
{ AV_CODEC_ID_LSCR, MKTAG('L', 'S', 'C', 'R') }, { AV_CODEC_ID_LSCR, MKTAG('L', 'S', 'C', 'R') },
{ AV_CODEC_ID_IMM5, MKTAG('I', 'M', 'M', '5') }, { AV_CODEC_ID_IMM5, MKTAG('I', 'M', 'M', '5') },
{ AV_CODEC_ID_MVDV, MKTAG('M', 'V', 'D', 'V') }, { AV_CODEC_ID_MVDV, MKTAG('M', 'V', 'D', 'V') },
{ AV_CODEC_ID_MVHA, MKTAG('M', 'V', 'H', 'A') },
{ AV_CODEC_ID_NONE, 0 } { AV_CODEC_ID_NONE, 0 }
}; };




Loading…
Cancel
Save