avcodec/snow: Split ff_snow_get_buffer()

The part of said function that is common to both encoder and decoder
is negligible since c954cf1e1b
and more than offset by the costs of "Am I an encoder?" checks.

So move allocating the frames to the encoder and decoder directly.
Also rename ff_snow_frame_start() to ff_snow_frames_prepare(),
because a frame without a buffer has not been properly started.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2023-09-25 18:21:55 +02:00
parent 0228e27ded
commit 418332e01c
4 changed files with 41 additions and 46 deletions

View File

@ -22,7 +22,6 @@
#include "libavutil/thread.h"
#include "avcodec.h"
#include "decode.h"
#include "encode.h"
#include "snow_dwt.h"
#include "snow.h"
#include "snowdata.h"
@ -61,36 +60,6 @@ void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_
}
}
int ff_snow_get_buffer(SnowContext *s, AVFrame *frame)
{
int ret, i;
int edges_needed = av_codec_is_encoder(s->avctx->codec);
frame->width = s->avctx->width ;
frame->height = s->avctx->height;
if (edges_needed) {
frame->width += 2 * EDGE_WIDTH;
frame->height += 2 * EDGE_WIDTH;
ret = ff_encode_alloc_frame(s->avctx, frame);
} else
ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF);
if (ret < 0)
return ret;
if (edges_needed) {
for (i = 0; frame->data[i]; i++) {
int offset = (EDGE_WIDTH >> (i ? s->chroma_v_shift : 0)) *
frame->linesize[i] +
(EDGE_WIDTH >> (i ? s->chroma_h_shift : 0));
frame->data[i] += offset;
}
frame->width = s->avctx->width;
frame->height = s->avctx->height;
}
return 0;
}
void ff_snow_reset_contexts(SnowContext *s){ //FIXME better initial contexts
int plane_index, level, orientation;
@ -589,20 +558,21 @@ void ff_snow_release_buffer(AVCodecContext *avctx)
}
}
int ff_snow_frame_start(SnowContext *s){
int ff_snow_frames_prepare(SnowContext *s)
{
AVFrame *tmp;
int i, ret;
ff_snow_release_buffer(s->avctx);
tmp= s->last_picture[s->max_ref_frames-1];
for(i=s->max_ref_frames-1; i>0; i--)
for (int i = s->max_ref_frames - 1; i > 0; i--)
s->last_picture[i] = s->last_picture[i-1];
s->last_picture[0] = s->current_picture;
s->current_picture = tmp;
if(s->keyframe){
s->ref_frames= 0;
s->current_picture->flags |= AV_FRAME_FLAG_KEY;
}else{
int i;
for(i=0; i<s->max_ref_frames && s->last_picture[i]->data[0]; i++)
@ -613,14 +583,8 @@ int ff_snow_frame_start(SnowContext *s){
av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
return AVERROR_INVALIDDATA;
}
}
if ((ret = ff_snow_get_buffer(s, s->current_picture)) < 0)
return ret;
if (s->keyframe)
s->current_picture->flags |= AV_FRAME_FLAG_KEY;
else
s->current_picture->flags &= ~AV_FRAME_FLAG_KEY;
}
return 0;
}

View File

@ -245,11 +245,10 @@ void ff_snow_common_end(SnowContext *s);
void ff_snow_release_buffer(AVCodecContext *avctx);
void ff_snow_reset_contexts(SnowContext *s);
int ff_snow_alloc_blocks(SnowContext *s);
int ff_snow_frame_start(SnowContext *s);
int ff_snow_frames_prepare(SnowContext *s);
void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride,
int sx, int sy, int b_w, int b_h, const BlockNode *block,
int plane_index, int w, int h);
int ff_snow_get_buffer(SnowContext *s, AVFrame *frame);
/* common inline functions */
//XXX doublecheck all of them should stay inlined

View File

@ -24,6 +24,7 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "codec_internal.h"
#include "decode.h"
#include "snow_dwt.h"
#include "snow.h"
@ -475,7 +476,13 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
ff_snow_alloc_blocks(s);
if((res = ff_snow_frame_start(s)) < 0)
if ((res = ff_snow_frames_prepare(s)) < 0)
return res;
s->current_picture->width = s->avctx->width;
s->current_picture->height = s->avctx->height;
res = ff_get_buffer(s->avctx, s->current_picture, AV_GET_BUFFER_FLAG_REF);
if (res < 0)
return res;
s->current_picture->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;

View File

@ -39,6 +39,28 @@
#include "mpegvideo.h"
#include "h263enc.h"
static int get_encode_buffer(SnowContext *s, AVFrame *frame)
{
int ret;
frame->width = s->avctx->width + 2 * EDGE_WIDTH;
frame->height = s->avctx->height + 2 * EDGE_WIDTH;
ret = ff_encode_alloc_frame(s->avctx, frame);
if (ret < 0)
return ret;
for (int i = 0; frame->data[i]; i++) {
int offset = (EDGE_WIDTH >> (i ? s->chroma_v_shift : 0)) *
frame->linesize[i] +
(EDGE_WIDTH >> (i ? s->chroma_h_shift : 0));
frame->data[i] += offset;
}
frame->width = s->avctx->width;
frame->height = s->avctx->height;
return 0;
}
static av_cold int encode_init(AVCodecContext *avctx)
{
SnowContext *s = avctx->priv_data;
@ -140,7 +162,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
if (!s->input_picture)
return AVERROR(ENOMEM);
if ((ret = ff_snow_get_buffer(s, s->input_picture)) < 0)
if ((ret = get_encode_buffer(s, s->input_picture)) < 0)
return ret;
if(s->motion_est == FF_ME_ITER){
@ -1659,7 +1681,10 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
emms_c();
}
ff_snow_frame_start(s);
ff_snow_frames_prepare(s);
ret = get_encode_buffer(s, s->current_picture);
if (ret < 0)
return ret;
s->m.current_picture_ptr= &s->m.current_picture;
s->m.current_picture.f = s->current_picture;