1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec/cfhdenc: refactor DSP code for CFHD encoder

This is needed to implement x86 SIMD.
This commit is contained in:
Paul B Mahol 2021-02-09 19:17:27 +01:00
parent 767d3f7dcf
commit d69f4be1c6
4 changed files with 151 additions and 91 deletions

View File

@ -258,7 +258,7 @@ OBJS-$(CONFIG_CDGRAPHICS_DECODER) += cdgraphics.o
OBJS-$(CONFIG_CDTOONS_DECODER) += cdtoons.o OBJS-$(CONFIG_CDTOONS_DECODER) += cdtoons.o
OBJS-$(CONFIG_CDXL_DECODER) += cdxl.o OBJS-$(CONFIG_CDXL_DECODER) += cdxl.o
OBJS-$(CONFIG_CFHD_DECODER) += cfhd.o cfhddata.o cfhddsp.o OBJS-$(CONFIG_CFHD_DECODER) += cfhd.o cfhddata.o cfhddsp.o
OBJS-$(CONFIG_CFHD_ENCODER) += cfhdenc.o cfhddata.o OBJS-$(CONFIG_CFHD_ENCODER) += cfhdenc.o cfhddata.o cfhdencdsp.o
OBJS-$(CONFIG_CINEPAK_DECODER) += cinepak.o OBJS-$(CONFIG_CINEPAK_DECODER) += cinepak.o
OBJS-$(CONFIG_CINEPAK_ENCODER) += cinepakenc.o elbg.o OBJS-$(CONFIG_CINEPAK_ENCODER) += cinepakenc.o elbg.o
OBJS-$(CONFIG_CLEARVIDEO_DECODER) += clearvideo.o OBJS-$(CONFIG_CLEARVIDEO_DECODER) += clearvideo.o

View File

@ -33,6 +33,7 @@
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h" #include "bytestream.h"
#include "cfhd.h" #include "cfhd.h"
#include "cfhdencdsp.h"
#include "put_bits.h" #include "put_bits.h"
#include "internal.h" #include "internal.h"
#include "thread.h" #include "thread.h"
@ -239,6 +240,8 @@ typedef struct CFHDEncContext {
Runbook rb[321]; Runbook rb[321];
Codebook cb[513]; Codebook cb[513];
int16_t *alpha; int16_t *alpha;
CFHDEncDSPContext dsp;
} CFHDEncContext; } CFHDEncContext;
static av_cold int cfhd_encode_init(AVCodecContext *avctx) static av_cold int cfhd_encode_init(AVCodecContext *avctx)
@ -359,6 +362,8 @@ static av_cold int cfhd_encode_init(AVCodecContext *avctx)
s->lut[i] = last; s->lut[i] = last;
} }
ff_cfhdencdsp_init(&s->dsp);
if (s->planes != 4) if (s->planes != 4)
return 0; return 0;
@ -369,42 +374,6 @@ static av_cold int cfhd_encode_init(AVCodecContext *avctx)
return 0; return 0;
} }
static av_always_inline void filter(int16_t *input, ptrdiff_t in_stride,
int16_t *low, ptrdiff_t low_stride,
int16_t *high, ptrdiff_t high_stride,
int len)
{
low[(0>>1) * low_stride] = av_clip_int16(input[0*in_stride] + input[1*in_stride]);
high[(0>>1) * high_stride] = av_clip_int16((5 * input[0*in_stride] - 11 * input[1*in_stride] +
4 * input[2*in_stride] + 4 * input[3*in_stride] -
1 * input[4*in_stride] - 1 * input[5*in_stride] + 4) >> 3);
for (int i = 2; i < len - 2; i += 2) {
low[(i>>1) * low_stride] = av_clip_int16(input[i*in_stride] + input[(i+1)*in_stride]);
high[(i>>1) * high_stride] = av_clip_int16(((-input[(i-2)*in_stride] - input[(i-1)*in_stride] +
input[(i+2)*in_stride] + input[(i+3)*in_stride] + 4) >> 3) +
input[(i+0)*in_stride] - input[(i+1)*in_stride]);
}
low[((len-2)>>1) * low_stride] = av_clip_int16(input[((len-2)+0)*in_stride] + input[((len-2)+1)*in_stride]);
high[((len-2)>>1) * high_stride] = av_clip_int16((11* input[((len-2)+0)*in_stride] - 5 * input[((len-2)+1)*in_stride] -
4 * input[((len-2)-1)*in_stride] - 4 * input[((len-2)-2)*in_stride] +
1 * input[((len-2)-3)*in_stride] + 1 * input[((len-2)-4)*in_stride] + 4) >> 3);
}
static void horiz_filter(int16_t *input, int16_t *low, int16_t *high,
int width)
{
filter(input, 1, low, 1, high, 1, width);
}
static void vert_filter(int16_t *input, ptrdiff_t in_stride,
int16_t *low, ptrdiff_t low_stride,
int16_t *high, ptrdiff_t high_stride, int len)
{
filter(input, in_stride, low, low_stride, high, high_stride, len);
}
static void quantize_band(int16_t *input, int width, int a_width, static void quantize_band(int16_t *input, int width, int a_width,
int height, unsigned quantization) int height, unsigned quantization)
{ {
@ -454,6 +423,7 @@ static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *frame, int *got_packet) const AVFrame *frame, int *got_packet)
{ {
CFHDEncContext *s = avctx->priv_data; CFHDEncContext *s = avctx->priv_data;
CFHDEncDSPContext *dsp = &s->dsp;
PutByteContext *pby = &s->pby; PutByteContext *pby = &s->pby;
PutBitContext *pb = &s->pb; PutBitContext *pb = &s->pb;
const Codebook *const cb = s->cb; const Codebook *const cb = s->cb;
@ -480,12 +450,9 @@ static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
in_stride = avctx->width; in_stride = avctx->width;
} }
for (int i = 0; i < height * 2; i++) { dsp->horiz_filter(input, low, high,
horiz_filter(input, low, high, width * 2); in_stride, a_width, a_width,
input += in_stride; width * 2, height * 2);
low += a_width;
high += a_width;
}
input = s->plane[plane].l_h[7]; input = s->plane[plane].l_h[7];
low = s->plane[plane].subband[7]; low = s->plane[plane].subband[7];
@ -493,23 +460,17 @@ static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
high = s->plane[plane].subband[9]; high = s->plane[plane].subband[9];
high_stride = s->plane[plane].band[2][0].a_width; high_stride = s->plane[plane].band[2][0].a_width;
for (int i = 0; i < width; i++) { dsp->vert_filter(input, low, high,
vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); a_width, low_stride, high_stride,
input++; width, height * 2);
low++;
high++;
}
input = s->plane[plane].l_h[6]; input = s->plane[plane].l_h[6];
low = s->plane[plane].l_h[7]; low = s->plane[plane].l_h[7];
high = s->plane[plane].subband[8]; high = s->plane[plane].subband[8];
for (int i = 0; i < width; i++) { dsp->vert_filter(input, low, high,
vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); a_width, low_stride, high_stride,
input++; width, height * 2);
low++;
high++;
}
a_width = s->plane[plane].band[1][0].a_width; a_width = s->plane[plane].band[1][0].a_width;
width = s->plane[plane].band[1][0].width; width = s->plane[plane].band[1][0].width;
@ -527,34 +488,25 @@ static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
} }
input = s->plane[plane].l_h[7]; input = s->plane[plane].l_h[7];
for (int i = 0; i < height * 2; i++) { dsp->horiz_filter(input, low, high,
horiz_filter(input, low, high, width * 2); a_width * 2, low_stride, high_stride,
input += a_width * 2; width * 2, height * 2);
low += low_stride;
high += high_stride;
}
input = s->plane[plane].l_h[4]; input = s->plane[plane].l_h[4];
low = s->plane[plane].subband[4]; low = s->plane[plane].subband[4];
high = s->plane[plane].subband[6]; high = s->plane[plane].subband[6];
for (int i = 0; i < width; i++) { dsp->vert_filter(input, low, high,
vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); a_width, low_stride, high_stride,
input++; width, height * 2);
low++;
high++;
}
input = s->plane[plane].l_h[3]; input = s->plane[plane].l_h[3];
low = s->plane[plane].l_h[4]; low = s->plane[plane].l_h[4];
high = s->plane[plane].subband[5]; high = s->plane[plane].subband[5];
for (int i = 0; i < width; i++) { dsp->vert_filter(input, low, high,
vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); a_width, low_stride, high_stride,
input++; width, height * 2);
low++;
high++;
}
a_width = s->plane[plane].band[0][0].a_width; a_width = s->plane[plane].band[0][0].a_width;
width = s->plane[plane].band[0][0].width; width = s->plane[plane].band[0][0].width;
@ -574,34 +526,25 @@ static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
} }
input = s->plane[plane].l_h[4]; input = s->plane[plane].l_h[4];
for (int i = 0; i < height * 2; i++) { dsp->horiz_filter(input, low, high,
horiz_filter(input, low, high, width * 2); a_width * 2, low_stride, high_stride,
input += a_width * 2; width * 2, height * 2);
low += low_stride;
high += high_stride;
}
low = s->plane[plane].subband[1]; low = s->plane[plane].subband[1];
high = s->plane[plane].subband[3]; high = s->plane[plane].subband[3];
input = s->plane[plane].l_h[1]; input = s->plane[plane].l_h[1];
for (int i = 0; i < width; i++) { dsp->vert_filter(input, low, high,
vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); a_width, low_stride, high_stride,
input++; width, height * 2);
low++;
high++;
}
low = s->plane[plane].subband[0]; low = s->plane[plane].subband[0];
high = s->plane[plane].subband[2]; high = s->plane[plane].subband[2];
input = s->plane[plane].l_h[0]; input = s->plane[plane].l_h[0];
for (int i = 0; i < width; i++) { dsp->vert_filter(input, low, high,
vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2); a_width, low_stride, high_stride,
input++; width, height * 2);
low++;
high++;
}
} }
ret = ff_alloc_packet2(avctx, pkt, 64LL + s->planes * (2LL * avctx->width * avctx->height + 1000LL), 0); ret = ff_alloc_packet2(avctx, pkt, 64LL + s->planes * (2LL * avctx->width * avctx->height + 1000LL), 0);

76
libavcodec/cfhdencdsp.c Normal file
View File

@ -0,0 +1,76 @@
/*
* Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
*
* 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 "libavutil/attributes.h"
#include "libavutil/common.h"
#include "libavutil/avassert.h"
#include "cfhdencdsp.h"
static av_always_inline void filter(int16_t *input, ptrdiff_t in_stride,
int16_t *low, ptrdiff_t low_stride,
int16_t *high, ptrdiff_t high_stride,
int len)
{
low[(0>>1) * low_stride] = av_clip_int16(input[0*in_stride] + input[1*in_stride]);
high[(0>>1) * high_stride] = av_clip_int16((5 * input[0*in_stride] - 11 * input[1*in_stride] +
4 * input[2*in_stride] + 4 * input[3*in_stride] -
1 * input[4*in_stride] - 1 * input[5*in_stride] + 4) >> 3);
for (int i = 2; i < len - 2; i += 2) {
low[(i>>1) * low_stride] = av_clip_int16(input[i*in_stride] + input[(i+1)*in_stride]);
high[(i>>1) * high_stride] = av_clip_int16(((-input[(i-2)*in_stride] - input[(i-1)*in_stride] +
input[(i+2)*in_stride] + input[(i+3)*in_stride] + 4) >> 3) +
input[(i+0)*in_stride] - input[(i+1)*in_stride]);
}
low[((len-2)>>1) * low_stride] = av_clip_int16(input[((len-2)+0)*in_stride] + input[((len-2)+1)*in_stride]);
high[((len-2)>>1) * high_stride] = av_clip_int16((11* input[((len-2)+0)*in_stride] - 5 * input[((len-2)+1)*in_stride] -
4 * input[((len-2)-1)*in_stride] - 4 * input[((len-2)-2)*in_stride] +
1 * input[((len-2)-3)*in_stride] + 1 * input[((len-2)-4)*in_stride] + 4) >> 3);
}
static void horiz_filter(int16_t *input, int16_t *low, int16_t *high,
ptrdiff_t in_stride, ptrdiff_t low_stride,
ptrdiff_t high_stride,
int width, int height)
{
for (int i = 0; i < height; i++) {
filter(input, 1, low, 1, high, 1, width);
input += in_stride;
low += low_stride;
high += high_stride;
}
}
static void vert_filter(int16_t *input, int16_t *low, int16_t *high,
ptrdiff_t in_stride, ptrdiff_t low_stride,
ptrdiff_t high_stride,
int width, int height)
{
for (int i = 0; i < width; i++)
filter(&input[i], in_stride, &low[i], low_stride, &high[i], high_stride, height);
}
av_cold void ff_cfhdencdsp_init(CFHDEncDSPContext *c)
{
c->horiz_filter = horiz_filter;
c->vert_filter = vert_filter;
}

41
libavcodec/cfhdencdsp.h Normal file
View File

@ -0,0 +1,41 @@
/*
* 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
*/
#ifndef AVCODEC_CFHDENCDSP_H
#define AVCODEC_CFHDENCDSP_H
#include <stddef.h>
#include <stdint.h>
typedef struct CFHDEncDSPContext {
void (*horiz_filter)(int16_t *input, int16_t *low, int16_t *high,
ptrdiff_t in_stride, ptrdiff_t low_stride,
ptrdiff_t high_stride,
int width, int height);
void (*vert_filter)(int16_t *input, int16_t *low, int16_t *high,
ptrdiff_t in_stride, ptrdiff_t low_stride,
ptrdiff_t high_stride,
int width, int height);
} CFHDEncDSPContext;
void ff_cfhdencdsp_init(CFHDEncDSPContext *c);
void ff_cfhdencdsp_init_x86(CFHDEncDSPContext *c);
#endif /* AVCODEC_CFHDENCDSP_H */