summaryrefslogtreecommitdiffstats
path: root/audio/decode/ad_lavc.c
diff options
context:
space:
mode:
Diffstat (limited to 'audio/decode/ad_lavc.c')
-rw-r--r--audio/decode/ad_lavc.c72
1 files changed, 46 insertions, 26 deletions
diff --git a/audio/decode/ad_lavc.c b/audio/decode/ad_lavc.c
index 8362e918f3..9b5f1fb467 100644
--- a/audio/decode/ad_lavc.c
+++ b/audio/decode/ad_lavc.c
@@ -26,8 +26,11 @@
#include <libavutil/common.h>
#include <libavutil/intreadwrite.h>
+#include "config.h"
+
#include "mpv_talloc.h"
#include "audio/aframe.h"
+#include "audio/chmap_avchannel.h"
#include "audio/fmt-conversion.h"
#include "common/av_common.h"
#include "common/codecs.h"
@@ -41,8 +44,10 @@
#include "options/options.h"
struct priv {
+ struct mp_codec_params *codec;
AVCodecContext *avctx;
AVFrame *avframe;
+ AVPacket *avpkt;
struct mp_chmap force_channel_map;
uint32_t skip_samples, trim_samples;
bool preroll_done;
@@ -56,23 +61,22 @@ struct priv {
#define OPT_BASE_STRUCT struct ad_lavc_params
struct ad_lavc_params {
float ac3drc;
- int downmix;
+ bool downmix;
int threads;
char **avopts;
};
const struct m_sub_options ad_lavc_conf = {
.opts = (const m_option_t[]) {
- OPT_FLOATRANGE("ac3drc", ac3drc, 0, 0, 6),
- OPT_FLAG("downmix", downmix, 0),
- OPT_INTRANGE("threads", threads, 0, 0, 16),
- OPT_KEYVALUELIST("o", avopts, 0),
+ {"ac3drc", OPT_FLOAT(ac3drc), M_RANGE(0, 6)},
+ {"downmix", OPT_BOOL(downmix)},
+ {"threads", OPT_INT(threads), M_RANGE(0, 16)},
+ {"o", OPT_KEYVALUELIST(avopts)},
{0}
},
.size = sizeof(struct ad_lavc_params),
.defaults = &(const struct ad_lavc_params){
.ac3drc = 0,
- .downmix = 1,
.threads = 1,
},
};
@@ -85,7 +89,7 @@ static bool init(struct mp_filter *da, struct mp_codec_params *codec,
struct ad_lavc_params *opts =
mp_get_config_group(ctx, da->global, &ad_lavc_conf);
AVCodecContext *lavc_context;
- AVCodec *lavc_codec;
+ const AVCodec *lavc_codec;
ctx->codec_timebase = mp_get_codec_timebase(codec);
@@ -101,26 +105,37 @@ static bool init(struct mp_filter *da, struct mp_codec_params *codec,
lavc_context = avcodec_alloc_context3(lavc_codec);
ctx->avctx = lavc_context;
ctx->avframe = av_frame_alloc();
+ ctx->avpkt = av_packet_alloc();
+ MP_HANDLE_OOM(ctx->avctx && ctx->avframe && ctx->avpkt);
lavc_context->codec_type = AVMEDIA_TYPE_AUDIO;
lavc_context->codec_id = lavc_codec->id;
-
-#if LIBAVCODEC_VERSION_MICRO >= 100
lavc_context->pkt_timebase = ctx->codec_timebase;
-#endif
if (opts->downmix && mpopts->audio_output_channels.num_chmaps == 1) {
+ const struct mp_chmap *requested_layout =
+ &mpopts->audio_output_channels.chmaps[0];
+#if !HAVE_AV_CHANNEL_LAYOUT
lavc_context->request_channel_layout =
- mp_chmap_to_lavc(&mpopts->audio_output_channels.chmaps[0]);
+ mp_chmap_to_lavc(requested_layout);
+#else
+ AVChannelLayout av_layout = { 0 };
+ mp_chmap_to_av_layout(&av_layout, requested_layout);
+
+ // Always try to set requested output layout - currently only something
+ // supported by AC3, MLP/TrueHD, DTS and the fdk-aac wrapper.
+ av_opt_set_chlayout(lavc_context, "downmix", &av_layout,
+ AV_OPT_SEARCH_CHILDREN);
+
+ av_channel_layout_uninit(&av_layout);
+#endif
}
// Always try to set - option only exists for AC3 at the moment
av_opt_set_double(lavc_context, "drc_scale", opts->ac3drc,
AV_OPT_SEARCH_CHILDREN);
-#if LIBAVCODEC_VERSION_MICRO >= 100
// Let decoder add AV_FRAME_DATA_SKIP_SAMPLES.
av_opt_set(lavc_context, "flags2", "+skip_manual", AV_OPT_SEARCH_CHILDREN);
-#endif
mp_set_avopts(da->log, lavc_context, opts->avopts);
@@ -142,15 +157,16 @@ static bool init(struct mp_filter *da, struct mp_codec_params *codec,
return true;
}
-static void destroy(struct mp_filter *da)
+static void ad_lavc_destroy(struct mp_filter *da)
{
struct priv *ctx = da->priv;
avcodec_free_context(&ctx->avctx);
av_frame_free(&ctx->avframe);
+ mp_free_av_packet(&ctx->avpkt);
}
-static void reset(struct mp_filter *da)
+static void ad_lavc_reset(struct mp_filter *da)
{
struct priv *ctx = da->priv;
@@ -173,10 +189,9 @@ static int send_packet(struct mp_filter *da, struct demux_packet *mpkt)
if (mpkt && priv->next_pts == MP_NOPTS_VALUE)
priv->next_pts = mpkt->pts;
- AVPacket pkt;
- mp_set_av_packet(&pkt, mpkt, &priv->codec_timebase);
+ mp_set_av_packet(priv->avpkt, mpkt, &priv->codec_timebase);
- int ret = avcodec_send_packet(avctx, mpkt ? &pkt : NULL);
+ int ret = avcodec_send_packet(avctx, mpkt ? priv->avpkt : NULL);
if (ret < 0)
MP_ERR(da, "Error decoding audio.\n");
return ret;
@@ -199,14 +214,14 @@ static int receive_frame(struct mp_filter *da, struct mp_frame *out)
MP_ERR(da, "Error decoding audio.\n");
}
-#if LIBAVCODEC_VERSION_MICRO >= 100
if (priv->avframe->flags & AV_FRAME_FLAG_DISCARD)
av_frame_unref(priv->avframe);
-#endif
if (!priv->avframe->buf[0])
return ret;
+ mp_codec_info_from_av(avctx, priv->codec);
+
double out_pts = mp_pts_from_av(priv->avframe->pts, &priv->codec_timebase);
struct mp_aframe *mpframe = mp_aframe_from_avframe(priv->avframe);
@@ -224,7 +239,6 @@ static int receive_frame(struct mp_filter *da, struct mp_frame *out)
priv->next_pts = mp_aframe_end_pts(mpframe);
-#if LIBAVCODEC_VERSION_MICRO >= 100
AVFrameSideData *sd =
av_frame_get_side_data(priv->avframe, AV_FRAME_DATA_SKIP_SAMPLES);
if (sd && sd->size >= 10) {
@@ -232,7 +246,6 @@ static int receive_frame(struct mp_filter *da, struct mp_frame *out)
priv->skip_samples += AV_RL32(d + 0);
priv->trim_samples += AV_RL32(d + 4);
}
-#endif
if (!priv->preroll_done) {
// Skip only if this isn't already handled by AV_FRAME_DATA_SKIP_SAMPLES.
@@ -252,6 +265,9 @@ static int receive_frame(struct mp_filter *da, struct mp_frame *out)
priv->trim_samples -= trim;
}
+ // Strip possibly bogus float values like Infinity, NaN, denormalized
+ mp_aframe_sanitize_float(mpframe);
+
if (mp_aframe_get_size(mpframe) > 0) {
*out = MAKE_FRAME(MP_FRAME_AUDIO, mpframe);
} else {
@@ -263,7 +279,7 @@ static int receive_frame(struct mp_filter *da, struct mp_frame *out)
return ret;
}
-static void process(struct mp_filter *ad)
+static void ad_lavc_process(struct mp_filter *ad)
{
struct priv *priv = ad->priv;
@@ -273,9 +289,9 @@ static void process(struct mp_filter *ad)
static const struct mp_filter_info ad_lavc_filter = {
.name = "ad_lavc",
.priv_size = sizeof(struct priv),
- .process = process,
- .reset = reset,
- .destroy = destroy,
+ .process = ad_lavc_process,
+ .reset = ad_lavc_reset,
+ .destroy = ad_lavc_destroy,
};
static struct mp_decoder *create(struct mp_filter *parent,
@@ -292,12 +308,16 @@ static struct mp_decoder *create(struct mp_filter *parent,
da->log = mp_log_new(da, parent->log, NULL);
struct priv *priv = da->priv;
+ priv->codec = codec;
priv->public.f = da;
if (!init(da, codec, decoder)) {
talloc_free(da);
return NULL;
}
+
+ codec->codec_desc = priv->avctx->codec_descriptor->long_name;
+
return &priv->public;
}