audio/aframe: reuse data buffer if less than 8 channels

This fixes audio encoding crashing under ASan.
When extended_data != data, FFmpeg copies more pointers from
extended_data (= the number of channels) than there really
are for non-planar formats (= exactly 1), but that's not our fault.
Regardless, this commit makes it work in all common cases.
This commit is contained in:
sfan5 2021-04-06 07:44:03 +02:00 committed by Jan Ekström
parent 5117fa70af
commit 3c34e6fec4
1 changed files with 12 additions and 6 deletions

View File

@ -635,18 +635,24 @@ int mp_aframe_pool_allocate(struct mp_aframe_pool *pool, struct mp_aframe *frame
AVFrame *av_frame = frame->av_frame;
if (av_frame->extended_data != av_frame->data)
av_freep(&av_frame->extended_data); // sigh
av_frame->extended_data =
av_mallocz_array(planes, sizeof(av_frame->extended_data[0]));
if (!av_frame->extended_data)
abort();
if (planes > AV_NUM_DATA_POINTERS) {
av_frame->extended_data =
av_mallocz_array(planes, sizeof(av_frame->extended_data[0]));
if (!av_frame->extended_data)
abort();
} else {
av_frame->extended_data = av_frame->data;
}
av_frame->buf[0] = av_buffer_pool_get(pool->avpool);
if (!av_frame->buf[0])
return -1;
av_frame->linesize[0] = samples * sstride;
for (int n = 0; n < planes; n++)
av_frame->extended_data[n] = av_frame->buf[0]->data + n * plane_size;
for (int n = 0; n < MPMIN(planes, AV_NUM_DATA_POINTERS); n++)
av_frame->data[n] = av_frame->extended_data[n];
if (planes > AV_NUM_DATA_POINTERS) {
for (int n = 0; n < AV_NUM_DATA_POINTERS; n++)
av_frame->data[n] = av_frame->extended_data[n];
}
av_frame->nb_samples = samples;
return 0;