1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +02:00

lavc/pixlet: reduce diff with Libav (cosmetics only)

This commit is contained in:
Clément Bœsch 2017-10-12 20:45:53 +02:00
parent 4b175913be
commit 368fb74831

View File

@ -28,12 +28,14 @@
#include "avcodec.h"
#include "bytestream.h"
#include "get_bits.h"
#include "unary.h"
#include "internal.h"
#include "thread.h"
#include "unary.h"
#define NB_LEVELS 4
#define PIXLET_MAGIC 0xDEADBEEF
#define H 0
#define V 1
@ -47,11 +49,11 @@ typedef struct PixletContext {
AVClass *class;
GetByteContext gb;
GetBitContext gbit;
GetBitContext bc;
int levels;
int depth;
int h, w;
int w, h;
int16_t *filter[2];
int16_t *prediction;
@ -59,38 +61,10 @@ typedef struct PixletContext {
SubBand band[4][NB_LEVELS * 3 + 1];
} PixletContext;
static int init_decoder(AVCodecContext *avctx)
static av_cold int pixlet_init(AVCodecContext *avctx)
{
PixletContext *ctx = avctx->priv_data;
int i, plane;
ctx->filter[0] = av_malloc_array(ctx->h, sizeof(int16_t));
ctx->filter[1] = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t));
ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
return AVERROR(ENOMEM);
for (plane = 0; plane < 3; plane++) {
unsigned shift = plane > 0;
unsigned w = ctx->w >> shift;
unsigned h = ctx->h >> shift;
ctx->band[plane][0].width = w >> NB_LEVELS;
ctx->band[plane][0].height = h >> NB_LEVELS;
ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS);
for (i = 0; i < NB_LEVELS * 3; i++) {
unsigned scale = ctx->levels - (i / 3);
ctx->band[plane][i + 1].width = w >> scale;
ctx->band[plane][i + 1].height = h >> scale;
ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale);
ctx->band[plane][i + 1].x = (w >> scale) * (((i + 1) % 3) != 2);
ctx->band[plane][i + 1].y = (h >> scale) * (((i + 1) % 3) != 1);
}
}
avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
avctx->color_range = AVCOL_RANGE_JPEG;
return 0;
}
@ -112,17 +86,46 @@ static av_cold int pixlet_close(AVCodecContext *avctx)
return 0;
}
static av_cold int pixlet_init(AVCodecContext *avctx)
static int init_decoder(AVCodecContext *avctx)
{
avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
avctx->color_range = AVCOL_RANGE_JPEG;
PixletContext *ctx = avctx->priv_data;
int i, plane;
ctx->filter[0] = av_malloc_array(ctx->h, sizeof(int16_t));
ctx->filter[1] = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t));
ctx->prediction = av_malloc_array((ctx->w >> NB_LEVELS), sizeof(int16_t));
if (!ctx->filter[0] || !ctx->filter[1] || !ctx->prediction)
return AVERROR(ENOMEM);
for (plane = 0; plane < 3; plane++) {
unsigned shift = plane > 0;
unsigned w = ctx->w >> shift;
unsigned h = ctx->h >> shift;
ctx->band[plane][0].width = w >> NB_LEVELS;
ctx->band[plane][0].height = h >> NB_LEVELS;
ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS);
for (i = 0; i < NB_LEVELS * 3; i++) {
unsigned scale = ctx->levels - (i / 3);
ctx->band[plane][i + 1].width = w >> scale;
ctx->band[plane][i + 1].height = h >> scale;
ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale);
ctx->band[plane][i + 1].x = (w >> scale) * (((i + 1) % 3) != 2);
ctx->band[plane][i + 1].y = (h >> scale) * (((i + 1) % 3) != 1);
}
}
return 0;
}
static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size, int width, ptrdiff_t stride)
static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size,
int width, ptrdiff_t stride)
{
PixletContext *ctx = avctx->priv_data;
GetBitContext *b = &ctx->gbit;
GetBitContext *bc = &ctx->bc;
unsigned cnt1, nbits, k, j = 0, i = 0;
int64_t value, state = 3;
int rlen, escape, flag = 0;
@ -130,45 +133,45 @@ static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size, int wi
while (i < size) {
nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14);
cnt1 = get_unary(b, 0, 8);
cnt1 = get_unary(bc, 0, 8);
if (cnt1 < 8) {
value = show_bits(b, nbits);
value = show_bits(bc, nbits);
if (value <= 1) {
skip_bits(b, nbits - 1);
skip_bits(bc, nbits - 1);
escape = ((1 << nbits) - 1) * cnt1;
} else {
skip_bits(b, nbits);
skip_bits(bc, nbits);
escape = value + ((1 << nbits) - 1) * cnt1 - 1;
}
} else {
escape = get_bits(b, 16);
escape = get_bits(bc, 16);
}
value = -((escape + flag) & 1) | 1;
value = -((escape + flag) & 1) | 1;
dst[j++] = value * ((escape + flag + 1) >> 1);
i++;
if (j == width) {
j = 0;
j = 0;
dst += stride;
}
state = 120 * (escape + flag) + state - (120 * state >> 8);
flag = 0;
flag = 0;
if (state * 4ULL > 0xFF || i >= size)
continue;
nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
escape = av_mod_uintp2(16383, nbits);
cnt1 = get_unary(b, 0, 8);
cnt1 = get_unary(bc, 0, 8);
if (cnt1 > 7) {
rlen = get_bits(b, 16);
rlen = get_bits(bc, 16);
} else {
value = show_bits(b, nbits);
value = show_bits(bc, nbits);
if (value > 1) {
skip_bits(b, nbits);
skip_bits(bc, nbits);
rlen = value + escape * cnt1 - 1;
} else {
skip_bits(b, nbits - 1);
skip_bits(bc, nbits - 1);
rlen = escape * cnt1;
}
}
@ -180,31 +183,32 @@ static int read_low_coeffs(AVCodecContext *avctx, int16_t *dst, int size, int wi
for (k = 0; k < rlen; k++) {
dst[j++] = 0;
if (j == width) {
j = 0;
j = 0;
dst += stride;
}
}
state = 0;
flag = rlen < 0xFFFF ? 1 : 0;
flag = rlen < 0xFFFF ? 1 : 0;
}
align_get_bits(b);
return get_bits_count(b) >> 3;
align_get_bits(bc);
return get_bits_count(bc) >> 3;
}
static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, int size,
int c, int a, int d,
static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst,
int size, int c, int a, int d,
int width, ptrdiff_t stride)
{
PixletContext *ctx = avctx->priv_data;
GetBitContext *b = &ctx->gbit;
GetBitContext *bc = &ctx->bc;
unsigned cnt1, shbits, rlen, nbits, length, i = 0, j = 0, k;
int ret, escape, pfx, value, yflag, xflag, flag = 0;
int64_t state = 3, tmp;
if ((ret = init_get_bits8(b, src, bytestream2_get_bytes_left(&ctx->gb))) < 0)
return ret;
ret = init_get_bits8(bc, src, bytestream2_get_bytes_left(&ctx->gb));
if (ret < 0)
return ret;
if (a ^ (a >> 31)) {
nbits = 33 - ff_clz(a ^ (a >> 31));
@ -217,26 +221,24 @@ static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, i
length = 25 - nbits;
while (i < size) {
if (state >> 8 != -3) {
if (state >> 8 != -3)
value = ff_clz((state >> 8) + 3) ^ 0x1F;
} else {
else
value = -1;
}
cnt1 = get_unary(b, 0, length);
cnt1 = get_unary(bc, 0, length);
if (cnt1 >= length) {
cnt1 = get_bits(b, nbits);
cnt1 = get_bits(bc, nbits);
} else {
pfx = 14 + ((((uint64_t)(value - 14)) >> 32) & (value - 14));
if (pfx < 1 || pfx > 25)
return AVERROR_INVALIDDATA;
cnt1 *= (1 << pfx) - 1;
shbits = show_bits(b, pfx);
shbits = show_bits(bc, pfx);
if (shbits <= 1) {
skip_bits(b, pfx - 1);
skip_bits(bc, pfx - 1);
} else {
skip_bits(b, pfx);
skip_bits(bc, pfx);
cnt1 += shbits - 1;
}
}
@ -248,14 +250,14 @@ static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, i
value = 0;
} else {
xflag &= 1u;
tmp = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);
value = xflag + (tmp ^ -xflag);
tmp = (int64_t)c * ((yflag + 1) >> 1) + (c >> 1);
value = xflag + (tmp ^ -xflag);
}
i++;
dst[j++] = value;
if (j == width) {
j = 0;
j = 0;
dst += stride;
}
state += (int64_t)d * (uint64_t)yflag - ((int64_t)(d * (uint64_t)state) >> 8);
@ -265,25 +267,26 @@ static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, i
if ((uint64_t)state > 0xFF / 4 || i >= size)
continue;
pfx = ((state + 8) >> 5) + (state ? ff_clz(state): 32) - 24;
pfx = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24;
escape = av_mod_uintp2(16383, pfx);
cnt1 = get_unary(b, 0, 8);
cnt1 = get_unary(bc, 0, 8);
if (cnt1 < 8) {
if (pfx < 1 || pfx > 25)
return AVERROR_INVALIDDATA;
value = show_bits(b, pfx);
value = show_bits(bc, pfx);
if (value > 1) {
skip_bits(b, pfx);
skip_bits(bc, pfx);
rlen = value + escape * cnt1 - 1;
} else {
skip_bits(b, pfx - 1);
skip_bits(bc, pfx - 1);
rlen = escape * cnt1;
}
} else {
if (get_bits1(b))
value = get_bits(b, 16);
if (get_bits1(bc))
value = get_bits(bc, 16);
else
value = get_bits(b, 8);
value = get_bits(bc, 8);
rlen = value + 8 * escape;
}
@ -295,20 +298,21 @@ static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, i
for (k = 0; k < rlen; k++) {
dst[j++] = 0;
if (j == width) {
j = 0;
j = 0;
dst += stride;
}
}
state = 0;
flag = rlen < 0xFFFF ? 1 : 0;
flag = rlen < 0xFFFF ? 1 : 0;
}
align_get_bits(b);
return get_bits_count(b) >> 3;
align_get_bits(bc);
return get_bits_count(bc) >> 3;
}
static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane, AVFrame *frame)
static int read_highpass(AVCodecContext *avctx, uint8_t *ptr,
int plane, AVFrame *frame)
{
PixletContext *ctx = avctx->priv_data;
ptrdiff_t stride = frame->linesize[plane] / 2;
@ -319,15 +323,16 @@ static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane, AVFrame
int32_t b = bytestream2_get_be32(&ctx->gb);
int32_t c = bytestream2_get_be32(&ctx->gb);
int32_t d = bytestream2_get_be32(&ctx->gb);
int16_t *dest = (int16_t *)frame->data[plane] + ctx->band[plane][i + 1].x +
stride * ctx->band[plane][i + 1].y;
int16_t *dest = (int16_t *)frame->data[plane] +
ctx->band[plane][i + 1].x +
ctx->band[plane][i + 1].y * stride;
unsigned size = ctx->band[plane][i + 1].size;
uint32_t magic;
uint32_t magic = bytestream2_get_be32(&ctx->gb);
magic = bytestream2_get_be32(&ctx->gb);
if (magic != 0xDEADBEEF) {
av_log(avctx, AV_LOG_ERROR, "wrong magic number: 0x%08"PRIX32
" for plane %d, band %d\n", magic, plane, i);
if (magic != PIXLET_MAGIC) {
av_log(avctx, AV_LOG_ERROR,
"wrong magic number: 0x%08"PRIX32" for plane %d, band %d\n",
magic, plane, i);
return AVERROR_INVALIDDATA;
}
@ -338,7 +343,9 @@ static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane, AVFrame
c, (b >= FFABS(a)) ? b : a, d,
ctx->band[plane][i + 1].width, stride);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "error in highpass coefficients for plane %d, band %d\n", plane, i);
av_log(avctx, AV_LOG_ERROR,
"error in highpass coefficients for plane %d, band %d\n",
plane, i);
return ret;
}
bytestream2_skip(&ctx->gb, ret);
@ -347,7 +354,8 @@ static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane, AVFrame
return 0;
}
static void lowpass_prediction(int16_t *dst, int16_t *pred, int width, int height, ptrdiff_t stride)
static void lowpass_prediction(int16_t *dst, int16_t *pred,
int width, int height, ptrdiff_t stride)
{
int16_t val;
int i, j;
@ -373,8 +381,8 @@ static void filterfn(int16_t *dest, int16_t *tmp, unsigned size, int64_t scale)
int64_t value;
hsize = size >> 1;
low = tmp + 4;
high = &low[hsize + 8];
low = tmp + 4;
high = &low[hsize + 8];
memcpy(low, dest, size);
memcpy(high, dest + hsize, size);
@ -411,29 +419,28 @@ static void filterfn(int16_t *dest, int16_t *tmp, unsigned size, int64_t scale)
}
}
static void reconstruction(AVCodecContext *avctx,
int16_t *dest, unsigned width, unsigned height, ptrdiff_t stride, int nb_levels,
int64_t *scaling_H, int64_t *scaling_V)
static void reconstruction(AVCodecContext *avctx, int16_t *dest,
unsigned width, unsigned height, ptrdiff_t stride, int nb_levels,
int64_t *scaling_h, int64_t *scaling_v)
{
PixletContext *ctx = avctx->priv_data;
unsigned scaled_width, scaled_height;
int64_t scale_H, scale_V;
int16_t *ptr, *tmp;
int i, j, k;
scaled_height = height >> nb_levels;
scaled_width = width >> nb_levels;
tmp = ctx->filter[0];
scaled_height = height >> nb_levels;
tmp = ctx->filter[0];
for (i = 0; i < nb_levels; i++) {
int64_t scale_v = scaling_v[i];
int64_t scale_h = scaling_h[i];
scaled_width <<= 1;
scaled_height <<= 1;
scale_H = scaling_H[i];
scale_V = scaling_V[i];
ptr = dest;
for (j = 0; j < scaled_height; j++) {
filterfn(ptr, ctx->filter[1], scaled_width, scale_V);
filterfn(ptr, ctx->filter[1], scaled_width, scale_v);
ptr += stride;
}
@ -441,10 +448,10 @@ static void reconstruction(AVCodecContext *avctx,
ptr = dest + j;
for (k = 0; k < scaled_height; k++) {
tmp[k] = *ptr;
ptr += stride;
ptr += stride;
}
filterfn(tmp, ctx->filter[1], scaled_height, scale_H);
filterfn(tmp, ctx->filter[1], scaled_height, scale_h);
ptr = dest + j;
for (k = 0; k < scaled_height; k++) {
@ -501,11 +508,12 @@ static void postprocess_chroma(AVFrame *frame, int w, int h, int depth)
}
}
static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFrame *frame)
static int decode_plane(AVCodecContext *avctx, int plane,
AVPacket *avpkt, AVFrame *frame)
{
PixletContext *ctx = avctx->priv_data;
ptrdiff_t stride = frame->linesize[plane] / 2;
unsigned shift = plane > 0;
ptrdiff_t stride = frame->linesize[plane] / 2;
unsigned shift = plane > 0;
int16_t *dst;
int i, ret;
@ -522,22 +530,28 @@ static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFra
bytestream2_skip(&ctx->gb, 4);
dst = (int16_t *)frame->data[plane];
dst = (int16_t *)frame->data[plane];
dst[0] = sign_extend(bytestream2_get_be16(&ctx->gb), 16);
if ((ret = init_get_bits8(&ctx->gbit, avpkt->data + bytestream2_tell(&ctx->gb),
bytestream2_get_bytes_left(&ctx->gb))) < 0)
ret = init_get_bits8(&ctx->bc, avpkt->data + bytestream2_tell(&ctx->gb),
bytestream2_get_bytes_left(&ctx->gb));
if (ret < 0)
return ret;
ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1, ctx->band[plane][0].width - 1, 0);
ret = read_low_coeffs(avctx, dst + 1, ctx->band[plane][0].width - 1,
ctx->band[plane][0].width - 1, 0);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, top row\n", plane);
av_log(avctx, AV_LOG_ERROR,
"error in lowpass coefficients for plane %d, top row\n", plane);
return ret;
}
ret = read_low_coeffs(avctx, dst + stride, ctx->band[plane][0].height - 1, 1, stride);
ret = read_low_coeffs(avctx, dst + stride,
ctx->band[plane][0].height - 1, 1, stride);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, left column\n", plane);
av_log(avctx, AV_LOG_ERROR,
"error in lowpass coefficients for plane %d, left column\n",
plane);
return ret;
}
@ -545,7 +559,8 @@ static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFra
(ctx->band[plane][0].width - 1) * (ctx->band[plane][0].height - 1),
ctx->band[plane][0].width - 1, stride);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, rest\n", plane);
av_log(avctx, AV_LOG_ERROR,
"error in lowpass coefficients for plane %d, rest\n", plane);
return ret;
}
@ -559,11 +574,12 @@ static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFra
if (ret < 0)
return ret;
lowpass_prediction(dst, ctx->prediction,
ctx->band[plane][0].width, ctx->band[plane][0].height, stride);
lowpass_prediction(dst, ctx->prediction, ctx->band[plane][0].width,
ctx->band[plane][0].height, stride);
reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift, ctx->h >> shift,
stride, NB_LEVELS, ctx->scaling[plane][H], ctx->scaling[plane][V]);
reconstruction(avctx, (int16_t *)frame->data[plane], ctx->w >> shift,
ctx->h >> shift, stride, NB_LEVELS, ctx->scaling[plane][H],
ctx->scaling[plane][V]);
return 0;
}
@ -664,14 +680,15 @@ static int pixlet_init_thread_copy(AVCodecContext *avctx)
{
PixletContext *ctx = avctx->priv_data;
ctx->filter[0] = NULL;
ctx->filter[1] = NULL;
ctx->filter[0] = NULL;
ctx->filter[1] = NULL;
ctx->prediction = NULL;
ctx->w = ctx->h = 0;
ctx->w = 0;
ctx->h = 0;
return 0;
}
#endif
#endif /* HAVE_THREADS */
AVCodec ff_pixlet_decoder = {
.name = "pixlet",