diff options
Diffstat (limited to 'audio/decode')
-rw-r--r-- | audio/decode/ad_lavc.c | 57 | ||||
-rw-r--r-- | audio/decode/ad_spdif.c | 90 |
2 files changed, 103 insertions, 44 deletions
diff --git a/audio/decode/ad_lavc.c b/audio/decode/ad_lavc.c index eaaf0729e9..9eb308528d 100644 --- a/audio/decode/ad_lavc.c +++ b/audio/decode/ad_lavc.c @@ -17,7 +17,6 @@ #include <stdio.h> #include <stdlib.h> -#include <unistd.h> #include <stdbool.h> #include <assert.h> @@ -26,8 +25,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 +43,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,7 +60,7 @@ struct priv { #define OPT_BASE_STRUCT struct ad_lavc_params struct ad_lavc_params { float ac3drc; - int downmix; + bool downmix; int threads; char **avopts; }; @@ -64,7 +68,7 @@ struct ad_lavc_params { const struct m_sub_options ad_lavc_conf = { .opts = (const m_option_t[]) { {"ac3drc", OPT_FLOAT(ac3drc), M_RANGE(0, 6)}, - {"downmix", OPT_FLAG(downmix)}, + {"downmix", OPT_BOOL(downmix)}, {"threads", OPT_INT(threads), M_RANGE(0, 16)}, {"o", OPT_KEYVALUELIST(avopts)}, {0} @@ -72,7 +76,6 @@ const struct m_sub_options ad_lavc_conf = { .size = sizeof(struct ad_lavc_params), .defaults = &(const struct ad_lavc_params){ .ac3drc = 0, - .downmix = 0, .threads = 1, }, }; @@ -85,7 +88,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,13 +104,24 @@ 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; lavc_context->pkt_timebase = ctx->codec_timebase; if (opts->downmix && mpopts->audio_output_channels.num_chmaps == 1) { - lavc_context->request_channel_layout = - mp_chmap_to_lavc(&mpopts->audio_output_channels.chmaps[0]); + const struct mp_chmap *requested_layout = + &mpopts->audio_output_channels.chmaps[0]; + 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); } // Always try to set - option only exists for AC3 at the moment @@ -137,15 +151,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; @@ -168,10 +183,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; @@ -200,6 +214,9 @@ static int receive_frame(struct mp_filter *da, struct mp_frame *out) if (!priv->avframe->buf[0]) return ret; + mp_codec_info_from_av(avctx, priv->codec); + mp_chmap_from_av_layout(&priv->codec->channels, &avctx->ch_layout); + double out_pts = mp_pts_from_av(priv->avframe->pts, &priv->codec_timebase); struct mp_aframe *mpframe = mp_aframe_from_avframe(priv->avframe); @@ -243,6 +260,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 { @@ -254,7 +274,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; @@ -264,9 +284,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, @@ -283,12 +303,17 @@ 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; + mp_chmap_from_av_layout(&priv->codec->channels, &priv->avctx->ch_layout); + return &priv->public; } diff --git a/audio/decode/ad_spdif.c b/audio/decode/ad_spdif.c index 0b1977769a..6d9ce5e928 100644 --- a/audio/decode/ad_spdif.c +++ b/audio/decode/ad_spdif.c @@ -25,6 +25,7 @@ #include <libavutil/opt.h> #include "audio/aframe.h" +#include "audio/chmap_avchannel.h" #include "audio/format.h" #include "common/av_common.h" #include "common/codecs.h" @@ -39,8 +40,10 @@ struct spdifContext { struct mp_log *log; + struct mp_codec_params *codec; enum AVCodecID codec_id; AVFormatContext *lavf_ctx; + AVPacket *avpkt; int out_buffer_len; uint8_t out_buffer[OUTBUF_SIZE]; bool need_close; @@ -52,7 +55,11 @@ struct spdifContext { struct mp_decoder public; }; +#if LIBAVFORMAT_VERSION_MAJOR < 61 static int write_packet(void *p, uint8_t *buf, int buf_size) +#else +static int write_packet(void *p, const uint8_t *buf, int buf_size) +#endif { struct spdifContext *ctx = p; @@ -68,7 +75,7 @@ static int write_packet(void *p, uint8_t *buf, int buf_size) } // (called on both filter destruction _and_ if lavf fails to init) -static void destroy(struct mp_filter *da) +static void ad_spdif_destroy(struct mp_filter *da) { struct spdifContext *spdif_ctx = da->priv; AVFormatContext *lavf_ctx = spdif_ctx->lavf_ctx; @@ -78,17 +85,18 @@ static void destroy(struct mp_filter *da) av_write_trailer(lavf_ctx); if (lavf_ctx->pb) av_freep(&lavf_ctx->pb->buffer); - av_freep(&lavf_ctx->pb); + avio_context_free(&lavf_ctx->pb); avformat_free_context(lavf_ctx); spdif_ctx->lavf_ctx = NULL; } + mp_free_av_packet(&spdif_ctx->avpkt); } static void determine_codec_params(struct mp_filter *da, AVPacket *pkt, int *out_profile, int *out_rate) { struct spdifContext *spdif_ctx = da->priv; - int profile = FF_PROFILE_UNKNOWN; + int profile = AV_PROFILE_UNKNOWN; AVCodecContext *ctx = NULL; AVFrame *frame = NULL; @@ -105,18 +113,20 @@ static void determine_codec_params(struct mp_filter *da, AVPacket *pkt, uint8_t *d = NULL; int s = 0; - av_parser_parse2(parser, ctx, &d, &s, pkt->data, pkt->size, 0, 0, 0); - *out_profile = profile = ctx->profile; - *out_rate = ctx->sample_rate; + if (av_parser_parse2(parser, ctx, &d, &s, pkt->data, pkt->size, 0, 0, 0) > 0) { + *out_profile = profile = ctx->profile; + *out_rate = ctx->sample_rate; + spdif_ctx->codec->codec_profile = avcodec_profile_name(spdif_ctx->codec_id, profile); + } avcodec_free_context(&ctx); av_parser_close(parser); } - if (profile != FF_PROFILE_UNKNOWN || spdif_ctx->codec_id != AV_CODEC_ID_DTS) + if (profile != AV_PROFILE_UNKNOWN) return; - AVCodec *codec = avcodec_find_decoder(spdif_ctx->codec_id); + const AVCodec *codec = avcodec_find_decoder(spdif_ctx->codec_id); if (!codec) goto done; @@ -139,19 +149,29 @@ static void determine_codec_params(struct mp_filter *da, AVPacket *pkt, *out_profile = profile = ctx->profile; *out_rate = ctx->sample_rate; + struct mp_codec_params *c = spdif_ctx->codec; + c->codec_profile = av_get_profile_name(ctx->codec, ctx->profile); + if (!c->codec_profile) + c->codec_profile = avcodec_profile_name(ctx->codec_id, ctx->profile); + c->codec = ctx->codec_descriptor->name; + c->codec_desc = ctx->codec_descriptor->long_name; + mp_chmap_from_av_layout(&c->channels, &ctx->ch_layout); + done: av_frame_free(&frame); avcodec_free_context(&ctx); - if (profile == FF_PROFILE_UNKNOWN) + if (profile == AV_PROFILE_UNKNOWN) MP_WARN(da, "Failed to parse codec profile.\n"); } -static int init_filter(struct mp_filter *da, AVPacket *pkt) +static int init_filter(struct mp_filter *da) { struct spdifContext *spdif_ctx = da->priv; - int profile = FF_PROFILE_UNKNOWN; + AVPacket *pkt = spdif_ctx->avpkt; + + int profile = AV_PROFILE_UNKNOWN; int c_rate = 0; determine_codec_params(da, pkt, &profile, &c_rate); MP_VERBOSE(da, "In: profile=%d samplerate=%d\n", profile, c_rate); @@ -167,8 +187,7 @@ static int init_filter(struct mp_filter *da, AVPacket *pkt) goto fail; void *buffer = av_mallocz(OUTBUF_SIZE); - if (!buffer) - abort(); + MP_HANDLE_OOM(buffer); lavf_ctx->pb = avio_alloc_context(buffer, OUTBUF_SIZE, 1, spdif_ctx, NULL, write_packet, NULL); if (!lavf_ctx->pb) { @@ -183,7 +202,8 @@ static int init_filter(struct mp_filter *da, AVPacket *pkt) if (!stream) goto fail; - stream->codecpar->codec_id = spdif_ctx->codec_id; + stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + stream->codecpar->codec_id = spdif_ctx->codec_id; AVDictionary *format_opts = NULL; @@ -205,15 +225,15 @@ static int init_filter(struct mp_filter *da, AVPacket *pkt) num_channels = 2; break; case AV_CODEC_ID_DTS: { - bool is_hd = profile == FF_PROFILE_DTS_HD_HRA || - profile == FF_PROFILE_DTS_HD_MA || - profile == FF_PROFILE_UNKNOWN; + bool is_hd = profile == AV_PROFILE_DTS_HD_HRA || + profile == AV_PROFILE_DTS_HD_MA || + profile == AV_PROFILE_UNKNOWN; // Apparently, DTS-HD over SPDIF is specified to be 7.1 (8 channels) // for DTS-HD MA, and stereo (2 channels) for DTS-HD HRA. The bit // streaming rate as well as the signaled channel count are defined // based on this value. - int dts_hd_spdif_channel_count = profile == FF_PROFILE_DTS_HD_HRA ? + int dts_hd_spdif_channel_count = profile == AV_PROFILE_DTS_HD_HRA ? 2 : 8; if (spdif_ctx->use_dts_hd && is_hd) { av_dict_set_int(&format_opts, "dtshd_rate", @@ -223,7 +243,7 @@ static int init_filter(struct mp_filter *da, AVPacket *pkt) num_channels = dts_hd_spdif_channel_count; } else { sample_format = AF_FORMAT_S_DTS; - samplerate = 48000; + samplerate = c_rate > 44100 ? 48000 : 44100; num_channels = 2; } break; @@ -247,6 +267,8 @@ static int init_filter(struct mp_filter *da, AVPacket *pkt) abort(); } + stream->codecpar->sample_rate = samplerate; + struct mp_chmap chmap; mp_chmap_from_channels(&chmap, num_channels); mp_aframe_set_chmap(spdif_ctx->fmt, &chmap); @@ -267,12 +289,12 @@ static int init_filter(struct mp_filter *da, AVPacket *pkt) return 0; fail: - destroy(da); + ad_spdif_destroy(da); mp_filter_internal_mark_failed(da); return -1; } -static void process(struct mp_filter *da) +static void ad_spdif_process(struct mp_filter *da) { struct spdifContext *spdif_ctx = da->priv; @@ -295,15 +317,21 @@ static void process(struct mp_filter *da) struct mp_aframe *out = NULL; double pts = mpkt->pts; - AVPacket pkt; - mp_set_av_packet(&pkt, mpkt, NULL); - pkt.pts = pkt.dts = 0; + if (!spdif_ctx->avpkt) { + spdif_ctx->avpkt = av_packet_alloc(); + MP_HANDLE_OOM(spdif_ctx->avpkt); + } + mp_set_av_packet(spdif_ctx->avpkt, mpkt, NULL); + spdif_ctx->avpkt->pts = spdif_ctx->avpkt->dts = 0; if (!spdif_ctx->lavf_ctx) { - if (init_filter(da, &pkt) < 0) + if (init_filter(da) < 0) goto done; + assert(spdif_ctx->avpkt); + assert(spdif_ctx->lavf_ctx); } + spdif_ctx->out_buffer_len = 0; - int ret = av_write_frame(spdif_ctx->lavf_ctx, &pkt); + int ret = av_write_frame(spdif_ctx->lavf_ctx, spdif_ctx->avpkt); avio_flush(spdif_ctx->lavf_ctx->pb); if (ret < 0) { MP_ERR(da, "spdif mux error: '%s'\n", mp_strerror(AVUNERROR(ret))); @@ -392,8 +420,8 @@ struct mp_decoder_list *select_spdif_codec(const char *codec, const char *pref) static const struct mp_filter_info ad_spdif_filter = { .name = "ad_spdif", .priv_size = sizeof(struct spdifContext), - .process = process, - .destroy = destroy, + .process = ad_spdif_process, + .destroy = ad_spdif_destroy, }; static struct mp_decoder *create(struct mp_filter *parent, @@ -411,6 +439,7 @@ static struct mp_decoder *create(struct mp_filter *parent, struct spdifContext *spdif_ctx = da->priv; spdif_ctx->log = da->log; + spdif_ctx->codec = codec; spdif_ctx->pool = mp_aframe_pool_create(spdif_ctx); spdif_ctx->public.f = da; @@ -424,6 +453,11 @@ static struct mp_decoder *create(struct mp_filter *parent, talloc_free(da); return NULL; } + + const AVCodecDescriptor *desc = avcodec_descriptor_get(spdif_ctx->codec_id); + if (desc) + codec->codec_desc = desc->long_name; + return &spdif_ctx->public; } |