1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-28 01:52:19 +00:00

audio/aframe: switch to AVChannelLayout when available

This commit is contained in:
Jan Ekström 2022-06-01 23:57:24 +03:00
parent 1d15a5a059
commit 302acb27c8

View File

@ -18,9 +18,12 @@
#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include "config.h"
#include "common/common.h"
#include "chmap.h"
#include "chmap_avchannel.h"
#include "fmt-conversion.h"
#include "format.h"
#include "aframe.h"
@ -121,6 +124,16 @@ struct mp_aframe *mp_aframe_from_avframe(struct AVFrame *av_frame)
if (!av_frame || av_frame->width > 0 || av_frame->height > 0)
return NULL;
#if HAVE_AV_CHANNEL_LAYOUT
if (!av_channel_layout_check(&av_frame->ch_layout))
return NULL;
struct mp_chmap converted_map = { 0 };
if (!mp_chmap_from_av_layout(&converted_map, &av_frame->ch_layout)) {
return NULL;
}
#endif
int format = af_from_avformat(av_frame->format);
if (!format && av_frame->format != AV_SAMPLE_FMT_NONE)
return NULL;
@ -132,11 +145,15 @@ struct mp_aframe *mp_aframe_from_avframe(struct AVFrame *av_frame)
abort();
frame->format = format;
#if !HAVE_AV_CHANNEL_LAYOUT
mp_chmap_from_lavc(&frame->chmap, frame->av_frame->channel_layout);
// FFmpeg being a stupid POS again
if (frame->chmap.num != frame->av_frame->channels)
mp_chmap_from_channels(&frame->chmap, av_frame->channels);
#else
frame->chmap = converted_map;
#endif
if (av_frame->opaque_ref) {
struct avframe_opaque *op = (void *)av_frame->opaque_ref->data;
@ -204,9 +221,16 @@ void mp_aframe_config_copy(struct mp_aframe *dst, struct mp_aframe *src)
dst->av_frame->sample_rate = src->av_frame->sample_rate;
dst->av_frame->format = src->av_frame->format;
#if !HAVE_AV_CHANNEL_LAYOUT
dst->av_frame->channel_layout = src->av_frame->channel_layout;
// FFmpeg being a stupid POS again
dst->av_frame->channels = src->av_frame->channels;
#else
if (av_channel_layout_copy(&dst->av_frame->ch_layout,
&src->av_frame->ch_layout) < 0)
abort();
#endif
}
// Copy "soft" attributes from src to dst, excluding things which affect
@ -315,13 +339,21 @@ bool mp_aframe_set_chmap(struct mp_aframe *frame, struct mp_chmap *in)
return false;
if (mp_aframe_is_allocated(frame) && in->num != frame->chmap.num)
return false;
#if !HAVE_AV_CHANNEL_LAYOUT
uint64_t lavc_layout = mp_chmap_to_lavc_unchecked(in);
if (!lavc_layout && in->num)
return false;
#endif
frame->chmap = *in;
#if !HAVE_AV_CHANNEL_LAYOUT
frame->av_frame->channel_layout = lavc_layout;
// FFmpeg being a stupid POS again
frame->av_frame->channels = frame->chmap.num;
#else
mp_chmap_to_av_layout(&frame->av_frame->ch_layout, in);
#endif
return true;
}