1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-28 20:53:54 +02:00

avfilter/ccfifo: remove unnecessary context allocations

This is not public API, no it has no need for an alloc() and free()
functions. The struct can reside on stack.

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2023-05-11 14:34:57 -03:00
parent 86ee031795
commit 7f890b2fbb
13 changed files with 89 additions and 94 deletions

View File

@ -113,7 +113,7 @@ struct decklink_ctx {
/* Capture buffer queue */
DecklinkPacketQueue queue;
AVCCFifo *cc_fifo; ///< closed captions
CCFifo cc_fifo; ///< closed captions
/* Streams present */
int audio;

View File

@ -371,7 +371,7 @@ av_cold int ff_decklink_write_trailer(AVFormatContext *avctx)
klvanc_context_destroy(ctx->vanc_ctx);
#endif
ff_ccfifo_freep(&ctx->cc_fifo);
ff_ccfifo_uninit(&ctx->cc_fifo);
av_freep(&cctx->ctx);
return 0;
@ -527,15 +527,15 @@ out:
that will later be handled by construct_cc... */
static void parse_608subs(AVFormatContext *avctx, struct decklink_ctx *ctx, AVPacket *pkt)
{
size_t cc_size = ff_ccfifo_getoutputsize(ctx->cc_fifo);
size_t cc_size = ff_ccfifo_getoutputsize(&ctx->cc_fifo);
uint8_t *cc_data;
if (!ff_ccfifo_ccdetected(ctx->cc_fifo))
if (!ff_ccfifo_ccdetected(&ctx->cc_fifo))
return;
cc_data = av_packet_new_side_data(pkt, AV_PKT_DATA_A53_CC, cc_size);
if (cc_data)
ff_ccfifo_injectbytes(ctx->cc_fifo, cc_data, cc_size);
ff_ccfifo_injectbytes(&ctx->cc_fifo, cc_data, cc_size);
}
static int decklink_construct_vanc(AVFormatContext *avctx, struct decklink_ctx *ctx,
@ -745,7 +745,7 @@ static int decklink_write_subtitle_packet(AVFormatContext *avctx, AVPacket *pkt)
struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
ff_ccfifo_extractbytes(ctx->cc_fifo, pkt->data, pkt->size);
ff_ccfifo_extractbytes(&ctx->cc_fifo, pkt->data, pkt->size);
return 0;
}
@ -831,9 +831,9 @@ av_cold int ff_decklink_write_header(AVFormatContext *avctx)
avpriv_set_pts_info(st, 64, ctx->bmd_tb_num, ctx->bmd_tb_den);
}
if (!(ctx->cc_fifo = ff_ccfifo_alloc(av_make_q(ctx->bmd_tb_den, ctx->bmd_tb_num), avctx))) {
ret = ff_ccfifo_init(&ctx->cc_fifo, av_make_q(ctx->bmd_tb_den, ctx->bmd_tb_num), avctx);
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
ret = AVERROR(ENOMEM);
goto error;
}

View File

@ -23,18 +23,6 @@
#include "ccfifo.h"
struct AVCCFifo {
AVFifo *cc_608_fifo;
AVFifo *cc_708_fifo;
AVRational framerate;
int expected_cc_count;
int expected_608;
int cc_detected;
int passthrough;
int passthrough_warning;
void *log_ctx;
};
#define MAX_CC_ELEMENTS 128
#define CC_BYTES_PER_ENTRY 3
@ -55,25 +43,18 @@ const static struct cc_lookup cc_lookup_vals[] = {
{ 60000, 1001, 10, 1},
};
void ff_ccfifo_freep(AVCCFifo **ccf)
void ff_ccfifo_uninit(CCFifo *ccf)
{
AVCCFifo *tmp = *ccf;
if (tmp) {
av_fifo_freep2(&tmp->cc_608_fifo);
av_fifo_freep2(&tmp->cc_708_fifo);
}
av_freep(ccf);
av_fifo_freep2(&ccf->cc_608_fifo);
av_fifo_freep2(&ccf->cc_708_fifo);
memset(ccf, 0, sizeof(*ccf));
}
AVCCFifo *ff_ccfifo_alloc(AVRational framerate, void *log_ctx)
int ff_ccfifo_init(CCFifo *ccf, AVRational framerate, void *log_ctx)
{
AVCCFifo *ccf;
int i;
ccf = av_mallocz(sizeof(*ccf));
if (!ccf)
return NULL;
memset(ccf, 0, sizeof(*ccf));
ccf->log_ctx = log_ctx;
ccf->framerate = framerate;
@ -101,24 +82,24 @@ AVCCFifo *ff_ccfifo_alloc(AVRational framerate, void *log_ctx)
ccf->passthrough = 1;
}
return ccf;
return 0;
error:
ff_ccfifo_freep(&ccf);
return NULL;
ff_ccfifo_uninit(ccf);
return AVERROR(ENOMEM);
}
int ff_ccfifo_getoutputsize(AVCCFifo *ccf)
int ff_ccfifo_getoutputsize(CCFifo *ccf)
{
return ccf->expected_cc_count * CC_BYTES_PER_ENTRY;
}
int ff_ccfifo_ccdetected(AVCCFifo *ccf)
int ff_ccfifo_ccdetected(CCFifo *ccf)
{
return ccf->cc_detected;
}
int ff_ccfifo_injectbytes(AVCCFifo *ccf, uint8_t *cc_data, size_t len)
int ff_ccfifo_injectbytes(CCFifo *ccf, uint8_t *cc_data, size_t len)
{
int cc_608_tuples = 0;
int cc_708_tuples = 0;
@ -159,7 +140,7 @@ int ff_ccfifo_injectbytes(AVCCFifo *ccf, uint8_t *cc_data, size_t len)
return 0;
}
int ff_ccfifo_inject(AVCCFifo *ccf, AVFrame *frame)
int ff_ccfifo_inject(CCFifo *ccf, AVFrame *frame)
{
AVFrameSideData *sd;
int ret;
@ -180,7 +161,7 @@ int ff_ccfifo_inject(AVCCFifo *ccf, AVFrame *frame)
return 0;
}
int ff_ccfifo_extractbytes(AVCCFifo *ccf, uint8_t *cc_bytes, size_t len)
int ff_ccfifo_extractbytes(CCFifo *ccf, uint8_t *cc_bytes, size_t len)
{
int cc_count = len / CC_BYTES_PER_ENTRY;
@ -209,7 +190,7 @@ int ff_ccfifo_extractbytes(AVCCFifo *ccf, uint8_t *cc_bytes, size_t len)
/* Read the A53 side data, discard padding, and put 608/708 into
queues so we can ensure they get into the output frames at
the correct rate... */
int ff_ccfifo_extract(AVCCFifo *ccf, AVFrame *frame)
int ff_ccfifo_extract(CCFifo *ccf, AVFrame *frame)
{
AVFrameSideData *side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
if (side_data) {

View File

@ -33,25 +33,33 @@
#include "libavutil/frame.h"
#include "libavutil/fifo.h"
typedef struct AVCCFifo AVCCFifo;
typedef struct CCFifo {
AVFifo *cc_608_fifo;
AVFifo *cc_708_fifo;
AVRational framerate;
int expected_cc_count;
int expected_608;
int cc_detected;
int passthrough;
int passthrough_warning;
void *log_ctx;
} CCFifo;
/**
* Allocate an AVCCFifo.
* Initialize a CCFifo.
*
* @param framerate output framerate
* @param log_ctx used for any av_log() calls
* @return newly allocated AVCCFifo, or NULL on error
* @return Zero on success, or negative AVERROR code on failure.
*/
AVCCFifo *ff_ccfifo_alloc(AVRational framerate, void *log_ctx);
int ff_ccfifo_init(CCFifo *ccf, AVRational framerate, void *log_ctx);
/**
* Free an AVCCFifo
* Free all memory allocated in a CCFifo and clear the context.
*
* @param ccf Pointer to the pointer to the AVCCFifo which should be freed
* @note `*ptr = NULL` is safe and leads to no action.
* @param ccf Pointer to the CCFifo which should be uninitialized
*/
void ff_ccfifo_freep(AVCCFifo **ccf);
void ff_ccfifo_uninit(CCFifo *ccf);
/**
* Extract CC data from an AVFrame
@ -61,17 +69,17 @@ void ff_ccfifo_freep(AVCCFifo **ccf);
* as it will be re-inserted at the appropriate rate later in the
* filter.
*
* @param af AVCCFifo to write to
* @param af CCFifo to write to
* @param frame AVFrame with the video frame to operate on
* @return Zero on success, or negative AVERROR
* code on failure.
*/
int ff_ccfifo_extract(AVCCFifo *ccf, AVFrame *frame);
int ff_ccfifo_extract(CCFifo *ccf, AVFrame *frame);
/**
*Just like ff_ccfifo_extract(), but takes the raw bytes instead of an AVFrame
*/
int ff_ccfifo_extractbytes(AVCCFifo *ccf, uint8_t *data, size_t len);
int ff_ccfifo_extractbytes(CCFifo *ccf, uint8_t *data, size_t len);
/**
* Provide the size in bytes of an output buffer to allocate
@ -80,7 +88,7 @@ int ff_ccfifo_extractbytes(AVCCFifo *ccf, uint8_t *data, size_t len);
* an appropriately sized buffer and pass it to ff_ccfifo_injectbytes()
*
*/
int ff_ccfifo_getoutputsize(AVCCFifo *ccf);
int ff_ccfifo_getoutputsize(CCFifo *ccf);
/**
* Insert CC data from the FIFO into an AVFrame (as side data)
@ -88,23 +96,23 @@ int ff_ccfifo_getoutputsize(AVCCFifo *ccf);
* Dequeue the appropriate number of CC tuples based on the
* frame rate, and insert them into the AVFrame
*
* @param af AVCCFifo to read from
* @param af CCFifo to read from
* @param frame AVFrame with the video frame to operate on
* @return Zero on success, or negative AVERROR
* code on failure.
*/
int ff_ccfifo_inject(AVCCFifo *ccf, AVFrame *frame);
int ff_ccfifo_inject(CCFifo *ccf, AVFrame *frame);
/**
* Just like ff_ccfifo_inject(), but takes the raw bytes to insert the CC data
* int rather than an AVFrame
*/
int ff_ccfifo_injectbytes(AVCCFifo *ccf, uint8_t *data, size_t len);
int ff_ccfifo_injectbytes(CCFifo *ccf, uint8_t *data, size_t len);
/**
* Returns 1 if captions have been found as a prior call
* to ff_ccfifo_extract() or ff_ccfifo_extractbytes()
*/
int ff_ccfifo_ccdetected(AVCCFifo *ccf);
int ff_ccfifo_ccdetected(CCFifo *ccf);
#endif /* AVFILTER_CCFIFO_H */

View File

@ -78,7 +78,7 @@ typedef struct TInterlaceContext {
const AVPixFmtDescriptor *csp;
void (*lowpass_line)(uint8_t *dstp, ptrdiff_t width, const uint8_t *srcp,
ptrdiff_t mref, ptrdiff_t pref, int clip_max);
AVCCFifo *cc_fifo;
CCFifo cc_fifo;
} TInterlaceContext;
void ff_tinterlace_init_x86(TInterlaceContext *interlace);

View File

@ -297,7 +297,7 @@ static av_cold void uninit(AVFilterContext *ctx)
av_frame_free(&yadif->prev);
av_frame_free(&yadif->cur );
av_frame_free(&yadif->next);
ff_ccfifo_freep(&yadif->cc_fifo);
ff_ccfifo_uninit(&yadif->cc_fifo);
}
static const enum AVPixelFormat pix_fmts[] = {
@ -326,6 +326,7 @@ static int config_props(AVFilterLink *link)
AVFilterContext *ctx = link->src;
BWDIFContext *s = link->src->priv;
YADIFContext *yadif = &s->yadif;
int ret;
link->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
link->w = link->src->inputs[0]->w;
@ -336,9 +337,10 @@ static int config_props(AVFilterLink *link)
else
link->frame_rate = ctx->inputs[0]->frame_rate;
if (!(yadif->cc_fifo = ff_ccfifo_alloc(link->frame_rate, ctx))) {
ret = ff_ccfifo_init(&yadif->cc_fifo, link->frame_rate, ctx);
if (ret < 0 ) {
av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
return AVERROR(ENOMEM);
return ret;
}
if (link->w < 3 || link->h < 4) {

View File

@ -37,7 +37,7 @@
typedef struct CCRepackContext
{
const AVClass *class;
AVCCFifo *cc_fifo;
CCFifo cc_fifo;
} CCRepackContext;
static const AVOption ccrepack_options[] = {
@ -50,9 +50,10 @@ static int config_input(AVFilterLink *link)
{
CCRepackContext *ctx = link->dst->priv;
if (!(ctx->cc_fifo = ff_ccfifo_alloc(link->frame_rate, ctx))) {
int ret = ff_ccfifo_init(&ctx->cc_fifo, link->frame_rate, ctx);
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
return AVERROR(ENOMEM);
return ret;
}
return 0;
@ -63,8 +64,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
CCRepackContext *ctx = inlink->dst->priv;
AVFilterLink *outlink = inlink->dst->outputs[0];
ff_ccfifo_extract(ctx->cc_fifo, frame);
ff_ccfifo_inject(ctx->cc_fifo, frame);
ff_ccfifo_extract(&ctx->cc_fifo, frame);
ff_ccfifo_inject(&ctx->cc_fifo, frame);
return ff_filter_frame(outlink, frame);
}
@ -72,7 +73,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static av_cold void uninit(AVFilterContext *ctx)
{
CCRepackContext *s = ctx->priv;
ff_ccfifo_freep(&s->cc_fifo);
ff_ccfifo_uninit(&s->cc_fifo);
}
static const AVFilterPad avfilter_vf_ccrepack_inputs[] = {

View File

@ -86,7 +86,7 @@ typedef struct FPSContext {
AVFrame *frames[2]; ///< buffered frames
int frames_count; ///< number of buffered frames
AVCCFifo *cc_fifo; ///< closed captions
CCFifo cc_fifo; ///< closed captions
int64_t next_pts; ///< pts of the next frame to output
@ -167,7 +167,7 @@ static av_cold void uninit(AVFilterContext *ctx)
frame = shift_frame(ctx, s);
av_frame_free(&frame);
}
ff_ccfifo_freep(&s->cc_fifo);
ff_ccfifo_uninit(&s->cc_fifo);
av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
"%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
@ -213,9 +213,10 @@ static int config_props(AVFilterLink* outlink)
s->in_pts_off, s->out_pts_off, s->start_time);
}
if (!(s->cc_fifo = ff_ccfifo_alloc(outlink->frame_rate, ctx))) {
ret = ff_ccfifo_init(&s->cc_fifo, outlink->frame_rate, ctx);
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
return AVERROR(ENOMEM);
return ret;
}
av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", outlink->frame_rate.num, outlink->frame_rate.den);
@ -250,7 +251,7 @@ static int read_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink,
av_log(ctx, AV_LOG_DEBUG, "Read frame with in pts %"PRId64", out pts %"PRId64"\n",
in_pts, frame->pts);
ff_ccfifo_extract(s->cc_fifo, frame);
ff_ccfifo_extract(&s->cc_fifo, frame);
s->frames[s->frames_count++] = frame;
s->frames_in++;
@ -298,7 +299,7 @@ static int write_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *outlin
if (!frame)
return AVERROR(ENOMEM);
// Make sure Closed Captions will not be duplicated
ff_ccfifo_inject(s->cc_fifo, frame);
ff_ccfifo_inject(&s->cc_fifo, frame);
frame->pts = s->next_pts++;
frame->duration = 1;

View File

@ -203,7 +203,7 @@ static av_cold void uninit(AVFilterContext *ctx)
av_frame_free(&tinterlace->next);
av_freep(&tinterlace->black_data[0][0]);
av_freep(&tinterlace->black_data[1][0]);
ff_ccfifo_freep(&tinterlace->cc_fifo);
ff_ccfifo_uninit(&tinterlace->cc_fifo);
}
static int config_out_props(AVFilterLink *outlink)
@ -212,7 +212,7 @@ static int config_out_props(AVFilterLink *outlink)
AVFilterLink *inlink = outlink->src->inputs[0];
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
TInterlaceContext *tinterlace = ctx->priv;
int i;
int ret, i;
tinterlace->vsub = desc->log2_chroma_h;
outlink->w = inlink->w;
@ -224,7 +224,6 @@ static int config_out_props(AVFilterLink *outlink)
if (tinterlace->mode == MODE_PAD) {
uint8_t black[4] = { 0, 0, 0, 16 };
int ret;
ff_draw_init(&tinterlace->draw, outlink->format, 0);
ff_draw_color(&tinterlace->draw, &tinterlace->color, black);
/* limited range */
@ -292,9 +291,10 @@ static int config_out_props(AVFilterLink *outlink)
#endif
}
if (!(tinterlace->cc_fifo = ff_ccfifo_alloc(outlink->frame_rate, ctx))) {
ret = ff_ccfifo_init(&tinterlace->cc_fifo, outlink->frame_rate, ctx);
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
return AVERROR(ENOMEM);
return ret;
}
av_log(ctx, AV_LOG_VERBOSE, "mode:%d filter:%s h:%d -> h:%d\n", tinterlace->mode,
@ -381,7 +381,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
tinterlace->cur = tinterlace->next;
tinterlace->next = picref;
ff_ccfifo_extract(tinterlace->cc_fifo, picref);
ff_ccfifo_extract(&tinterlace->cc_fifo, picref);
cur = tinterlace->cur;
next = tinterlace->next;
@ -464,7 +464,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
if (!out)
return AVERROR(ENOMEM);
out->pts /= 2; // adjust pts to new framerate
ff_ccfifo_inject(tinterlace->cc_fifo, out);
ff_ccfifo_inject(&tinterlace->cc_fifo, out);
ret = ff_filter_frame(outlink, out);
return ret;
}
@ -514,7 +514,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
out->pts = cur->pts*2;
out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
ff_ccfifo_inject(tinterlace->cc_fifo, out);
ff_ccfifo_inject(&tinterlace->cc_fifo, out);
if ((ret = ff_filter_frame(outlink, out)) < 0)
return ret;
@ -559,7 +559,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
out->pts = av_rescale_q(out->pts, tinterlace->preout_time_base, outlink->time_base);
out->duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
ff_ccfifo_inject(tinterlace->cc_fifo, out);
ff_ccfifo_inject(&tinterlace->cc_fifo, out);
ret = ff_filter_frame(outlink, out);
return ret;

View File

@ -261,7 +261,7 @@ static av_cold void uninit(AVFilterContext *ctx)
av_frame_free(&yadif->prev);
av_frame_free(&yadif->cur );
av_frame_free(&yadif->next);
ff_ccfifo_freep(&yadif->cc_fifo);
ff_ccfifo_uninit(&yadif->cc_fifo);
}
static const enum AVPixelFormat pix_fmts[] = {
@ -286,6 +286,7 @@ static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
YADIFContext *s = ctx->priv;
int ret;
outlink->time_base = av_mul_q(ctx->inputs[0]->time_base, (AVRational){1, 2});
outlink->w = ctx->inputs[0]->w;
@ -297,9 +298,10 @@ static int config_output(AVFilterLink *outlink)
else
outlink->frame_rate = ctx->inputs[0]->frame_rate;
if (!(s->cc_fifo = ff_ccfifo_alloc(outlink->frame_rate, ctx))) {
ret = ff_ccfifo_init(&s->cc_fifo, outlink->frame_rate, ctx);
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
return AVERROR(ENOMEM);
return ret;
}
if (outlink->w < 3 || outlink->h < 3) {

View File

@ -205,7 +205,7 @@ static av_cold void deint_cuda_uninit(AVFilterContext *ctx)
av_frame_free(&y->prev);
av_frame_free(&y->cur);
av_frame_free(&y->next);
ff_ccfifo_freep(&y->cc_fifo);
ff_ccfifo_uninit(&y->cc_fifo);
av_buffer_unref(&s->device_ref);
s->hwctx = NULL;
@ -295,9 +295,9 @@ static int config_output(AVFilterLink *link)
else
link->frame_rate = ctx->inputs[0]->frame_rate;
if (!(y->cc_fifo = ff_ccfifo_alloc(link->frame_rate, ctx))) {
ret = ff_ccfifo_init(&y->cc_fifo, link->frame_rate, ctx);
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
ret = AVERROR(ENOMEM);
goto exit;
}

View File

@ -77,7 +77,7 @@ typedef struct YADIFContext {
int eof;
uint8_t *temp_line;
int temp_line_size;
AVCCFifo *cc_fifo;
CCFifo cc_fifo;
/*
* An algorithm that treats first and/or last fields in a sequence

View File

@ -66,7 +66,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
}
}
ff_ccfifo_inject(yadif->cc_fifo, yadif->out);
ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
ret = ff_filter_frame(ctx->outputs[0], yadif->out);
yadif->frame_pending = (yadif->mode&1) && !is_second;
@ -103,7 +103,7 @@ int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
av_assert0(frame);
ff_ccfifo_extract(yadif->cc_fifo, frame);
ff_ccfifo_extract(&yadif->cc_fifo, frame);
if (yadif->frame_pending)
return_frame(ctx, 1);
@ -146,7 +146,7 @@ int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
if (!yadif->out)
return AVERROR(ENOMEM);
ff_ccfifo_inject(yadif->cc_fifo, yadif->out);
ff_ccfifo_inject(&yadif->cc_fifo, yadif->out);
av_frame_free(&yadif->prev);
if (yadif->out->pts != AV_NOPTS_VALUE)
yadif->out->pts *= 2;