Commit c311431e authored by Michael Niedermayer's avatar Michael Niedermayer

apad: add whole_len and pad_len options.

These options allow padding to a specific length or adding a specific amount if silence.
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent c2640146
......@@ -40,6 +40,8 @@ typedef struct {
int64_t next_pts;
int packet_size;
int64_t pad_len;
int64_t whole_len;
} APadContext;
#define OFFSET(x) offsetof(APadContext, x)
......@@ -47,6 +49,8 @@ typedef struct {
static const AVOption apad_options[] = {
{ "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
{ "pad_len", "number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
{ "whole_len", "target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
{ NULL },
};
......@@ -65,6 +69,11 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
if ((ret = av_opt_set_from_string(apad, args, NULL, "=", ":")) < 0)
return ret;
if (apad->whole_len && apad->pad_len) {
av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
return AVERROR(EINVAL);
}
av_opt_free(apad);
return 0;
......@@ -74,6 +83,10 @@ static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
{
AVFilterContext *ctx = inlink->dst;
APadContext *apad = ctx->priv;
if (apad->whole_len)
apad->whole_len -= frame->audio->nb_samples;
apad->next_pts = frame->pts + av_rescale_q(frame->audio->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
return ff_filter_frame(ctx->outputs[0], frame);
}
......@@ -88,7 +101,21 @@ static int request_frame(AVFilterLink *outlink)
if (ret == AVERROR_EOF) {
int n_out = apad->packet_size;
AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
AVFilterBufferRef *outsamplesref;
if (apad->whole_len > 0) {
apad->pad_len = apad->whole_len;
apad->whole_len = 0;
}
if (apad->pad_len > 0) {
n_out = FFMIN(n_out, apad->pad_len);
apad->pad_len -= n_out;
}
if(!n_out)
return AVERROR_EOF;
outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
if (!outsamplesref)
return AVERROR(ENOMEM);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment