This avoids creating unwieldy large packets, which is allowed but does not seem to be a good idea
Showing the changes to libavfilter/af_aresample.c (1 of 1 file). Show all
Unified · Split
| @@ -41,6 +41,7 @@ typedef struct { · 40 unmodified lines | ||
| 41 | 41 | struct SwrContext *swr; |
| 42 | 42 | int64_t next_pts; |
| 43 | 43 | int req_fullfilled; |
| 44 | + int more_data; | |
| 44 | 45 | } AResampleContext; |
| 45 | 46 | |
| 46 | 47 | static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts) |
| @@ -186,7 +187,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref) · 139 unmodified lines | ||
| 186 | 187 | |
| 187 | 188 | delay = swr_get_delay(aresample->swr, outlink->sample_rate); |
| 188 | 189 | if (delay > 0) |
| 189 | − n_out += delay; | |
| 190 | + n_out += FFMIN(delay, FFMAX(4096, n_out)); | |
| 190 | 191 | |
| 191 | 192 | outsamplesref = ff_get_audio_buffer(outlink, n_out); |
| 192 | 193 | |
| @@ -215,6 +216,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref) · 22 unmodified lines | ||
| 215 | 216 | return 0; |
| 216 | 217 | } |
| 217 | 218 | |
| 219 | + aresample->more_data = outsamplesref->nb_samples == n_out; // Indicate that there is probably more data in our buffers | |
| 220 | + | |
| 218 | 221 | outsamplesref->nb_samples = n_out; |
| 219 | 222 | |
| 220 | 223 | ret = ff_filter_frame(outlink, outsamplesref); |
| @@ -261,11 +264,23 @@ static int request_frame(AVFilterLink *outlink) · 40 unmodified lines | ||
| 261 | 264 | AVFilterLink *const inlink = outlink->src->inputs[0]; |
| 262 | 265 | int ret; |
| 263 | 266 | |
| 267 | + // First try to get data from the internal buffers | |
| 268 | + if (aresample->more_data) { | |
| 269 | + AVFrame *outsamplesref; | |
| 270 | + | |
| 271 | + if (flush_frame(outlink, 0, &outsamplesref) >= 0) { | |
| 272 | + return ff_filter_frame(outlink, outsamplesref); | |
| 273 | + } | |
| 274 | + } | |
| 275 | + aresample->more_data = 0; | |
| 276 | + | |
| 277 | + // Second request more data from the input | |
| 264 | 278 | aresample->req_fullfilled = 0; |
| 265 | 279 | do{ |
| 266 | 280 | ret = ff_request_frame(ctx->inputs[0]); |
| 267 | 281 | }while(!aresample->req_fullfilled && ret>=0); |
| 268 | 282 | |
| 283 | + // Third if we hit the end flush | |
| 269 | 284 | if (ret == AVERROR_EOF) { |
| 270 | 285 | AVFrame *outsamplesref; |
| 271 | 286 | |