2011-10-12 19:23:08 +02:00
|
|
|
/*
|
core: redo how codecs are mapped, remove codecs.conf
Use codec names instead of FourCCs to identify codecs. Rewrite how
codecs are selected and initialized. Now each decoder exports a list
of decoders (and the codec it supports) via add_decoders(). The order
matters, and the first decoder for a given decoder is preferred over
the other decoders. E.g. all ad_mpg123 decoders are preferred over
ad_lavc, because it comes first in the mpcodecs_ad_drivers array.
Likewise, decoders within ad_lavc that are enumerated first by
libavcodec (using av_codec_next()) are preferred. (This is actually
critical to select h264 software decoding by default instead of vdpau.
libavcodec and ffmpeg/avconv use the same method to select decoders by
default, so we hope this is sane.)
The codec names follow libavcodec's codec names as defined by
AVCodecDescriptor.name (see libavcodec/codec_desc.c). Some decoders
have names different from the canonical codec name. The AVCodecDescriptor
API is relatively new, so we need a compatibility layer for older
libavcodec versions for codec names that are referenced internally,
and which are different from the decoder name. (Add a configure check
for that, because checking versions is getting way too messy.)
demux/codec_tags.c is generated from the former codecs.conf (minus
"special" decoders like vdpau, and excluding the mappings that are the
same as the mappings libavformat's exported RIFF tables). It contains
all the mappings from FourCCs to codec name. This is needed for
demux_mkv, demux_mpg, demux_avi and demux_asf. demux_lavf will set the
codec as determined by libavformat, while the other demuxers have to do
this on their own, using the mp_set_audio/video_codec_from_tag()
functions. Note that the sh_audio/video->format members don't uniquely
identify the codec anymore, and sh->codec takes over this role.
Replace the --ac/--vc/--afm/--vfm with new --vd/--ad options, which
provide cover the functionality of the removed switched.
Note: there's no CODECS_FLAG_FLIP flag anymore. This means some obscure
container/video combinations (e.g. the sample Film_200_zygo_pro.mov)
are played flipped. ffplay/avplay doesn't handle this properly either,
so we don't care and blame ffmeg/libav instead.
2013-02-09 15:15:19 +01:00
|
|
|
* Copyright (C) 2012 Naoya OYAMA
|
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2011-10-12 19:23:08 +02:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2011-10-12 19:23:08 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2015-04-13 09:36:54 +02:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2011-10-12 19:23:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct
support for this, and remove the hack that repacked non-interleaved
audio back to packed audio.
Remove the minlen argument from the decoder callback. Instead of
forcing every decoder to have its own decode loop to fill the buffer
until minlen is reached, leave this to the caller. So if a decoder
doesn't return enough data, it's simply called again. (In future, I
even want to change it so that decoders don't read packets directly,
but instead the caller has to pass packets to the decoders. This fits
well with this change, because now the decoder callback typically
decodes at most one packet.)
ad_mpg123.c receives some heavy refactoring. The main problem is that
it wanted to handle format changes when there was no data in the decode
output buffer yet. This sounds reasonable, but actually it would write
data into a buffer prepared for old data, since the caller doesn't know
about the format change yet. (I.e. the best place for a format change
would be _after_ writing the last sample to the output buffer.) It's
possible that this code was not perfectly sane before this commit,
and perhaps lost one frame of data after a format change, but I didn't
confirm this. Trying to fix this, I ended up rewriting the decoding
and also the probing.
2013-11-12 22:27:44 +01:00
|
|
|
#include <assert.h>
|
2011-10-12 19:23:08 +02:00
|
|
|
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavutil/opt.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/msg.h"
|
|
|
|
#include "common/av_common.h"
|
2013-12-17 02:02:25 +01:00
|
|
|
#include "options/options.h"
|
2013-07-22 14:41:56 +02:00
|
|
|
#include "ad.h"
|
2011-10-12 19:23:08 +02:00
|
|
|
|
|
|
|
#define OUTBUF_SIZE 65536
|
2013-11-09 00:32:44 +01:00
|
|
|
|
2011-10-12 19:23:08 +02:00
|
|
|
struct spdifContext {
|
2013-12-21 18:23:59 +01:00
|
|
|
struct mp_log *log;
|
2015-05-19 21:34:30 +02:00
|
|
|
enum AVCodecID codec_id;
|
2011-10-12 19:23:08 +02:00
|
|
|
AVFormatContext *lavf_ctx;
|
|
|
|
int out_buffer_len;
|
2014-07-21 19:29:37 +02:00
|
|
|
uint8_t out_buffer[OUTBUF_SIZE];
|
2013-11-09 00:32:44 +01:00
|
|
|
bool need_close;
|
2015-06-05 22:34:48 +02:00
|
|
|
bool use_dts_hd;
|
2014-11-10 22:01:23 +01:00
|
|
|
struct mp_audio fmt;
|
2016-01-19 21:33:05 +01:00
|
|
|
struct mp_audio_pool *pool;
|
2011-10-12 19:23:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int write_packet(void *p, uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
struct spdifContext *ctx = p;
|
|
|
|
|
2014-07-21 19:29:37 +02:00
|
|
|
int buffer_left = OUTBUF_SIZE - ctx->out_buffer_len;
|
2013-11-09 00:32:44 +01:00
|
|
|
if (buf_size > buffer_left) {
|
2013-12-21 18:23:59 +01:00
|
|
|
MP_ERR(ctx, "spdif packet too large.\n");
|
2013-11-09 00:32:44 +01:00
|
|
|
buf_size = buffer_left;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&ctx->out_buffer[ctx->out_buffer_len], buf, buf_size);
|
|
|
|
ctx->out_buffer_len += buf_size;
|
|
|
|
return buf_size;
|
2011-10-12 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
2013-11-23 21:22:17 +01:00
|
|
|
static void uninit(struct dec_audio *da)
|
2011-10-12 19:23:08 +02:00
|
|
|
{
|
2013-11-23 21:22:17 +01:00
|
|
|
struct spdifContext *spdif_ctx = da->priv;
|
2013-11-09 00:32:44 +01:00
|
|
|
AVFormatContext *lavf_ctx = spdif_ctx->lavf_ctx;
|
|
|
|
|
|
|
|
if (lavf_ctx) {
|
|
|
|
if (spdif_ctx->need_close)
|
|
|
|
av_write_trailer(lavf_ctx);
|
|
|
|
if (lavf_ctx->pb)
|
|
|
|
av_freep(&lavf_ctx->pb->buffer);
|
|
|
|
av_freep(&lavf_ctx->pb);
|
|
|
|
avformat_free_context(lavf_ctx);
|
|
|
|
}
|
2011-10-12 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
2013-11-23 21:22:17 +01:00
|
|
|
static int init(struct dec_audio *da, const char *decoder)
|
2011-10-12 19:23:08 +02:00
|
|
|
{
|
2013-11-09 00:32:44 +01:00
|
|
|
struct spdifContext *spdif_ctx = talloc_zero(NULL, struct spdifContext);
|
2013-11-23 21:22:17 +01:00
|
|
|
da->priv = spdif_ctx;
|
2013-12-21 18:23:59 +01:00
|
|
|
spdif_ctx->log = da->log;
|
2015-06-05 22:34:48 +02:00
|
|
|
spdif_ctx->use_dts_hd = da->opts->dtshd;
|
2016-01-19 21:33:05 +01:00
|
|
|
spdif_ctx->pool = mp_audio_pool_create(spdif_ctx);
|
2015-06-05 22:34:48 +02:00
|
|
|
|
|
|
|
if (strcmp(decoder, "dts-hd") == 0) {
|
|
|
|
decoder = "dts";
|
|
|
|
spdif_ctx->use_dts_hd = true;
|
|
|
|
}
|
2011-10-12 19:23:08 +02:00
|
|
|
|
2015-05-19 21:34:30 +02:00
|
|
|
spdif_ctx->codec_id = mp_codec_to_av_codec_id(decoder);
|
|
|
|
return spdif_ctx->codec_id != AV_CODEC_ID_NONE;
|
|
|
|
}
|
|
|
|
|
2015-05-19 21:35:43 +02:00
|
|
|
static int determine_codec_profile(struct dec_audio *da, AVPacket *pkt)
|
2015-05-19 21:34:30 +02:00
|
|
|
{
|
|
|
|
struct spdifContext *spdif_ctx = da->priv;
|
2015-05-19 21:35:43 +02:00
|
|
|
int profile = FF_PROFILE_UNKNOWN;
|
|
|
|
AVCodecContext *ctx = NULL;
|
|
|
|
AVFrame *frame = NULL;
|
|
|
|
|
|
|
|
AVCodec *codec = avcodec_find_decoder(spdif_ctx->codec_id);
|
|
|
|
if (!codec)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
frame = av_frame_alloc();
|
|
|
|
if (!frame)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ctx = avcodec_alloc_context3(codec);
|
|
|
|
if (!ctx)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (avcodec_open2(ctx, codec, NULL) < 0) {
|
|
|
|
av_free(ctx); // don't attempt to avcodec_close() an unopened ctx
|
|
|
|
ctx = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
int got_frame = 0;
|
|
|
|
if (avcodec_decode_audio4(ctx, frame, &got_frame, pkt) < 1 || !got_frame)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
profile = ctx->profile;
|
|
|
|
|
|
|
|
done:
|
|
|
|
av_frame_free(&frame);
|
|
|
|
if (ctx)
|
|
|
|
avcodec_close(ctx);
|
|
|
|
avcodec_free_context(&ctx);
|
|
|
|
|
|
|
|
if (profile == FF_PROFILE_UNKNOWN)
|
|
|
|
MP_WARN(da, "Failed to parse codec profile.\n");
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init_filter(struct dec_audio *da, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
struct spdifContext *spdif_ctx = da->priv;
|
|
|
|
|
|
|
|
int profile = FF_PROFILE_UNKNOWN;
|
|
|
|
if (spdif_ctx->codec_id == AV_CODEC_ID_DTS)
|
|
|
|
profile = determine_codec_profile(da, pkt);
|
2015-05-19 21:34:30 +02:00
|
|
|
|
2013-11-10 00:52:55 +01:00
|
|
|
AVFormatContext *lavf_ctx = avformat_alloc_context();
|
|
|
|
if (!lavf_ctx)
|
|
|
|
goto fail;
|
|
|
|
|
2015-05-19 21:34:30 +02:00
|
|
|
spdif_ctx->lavf_ctx = lavf_ctx;
|
|
|
|
|
2013-11-10 00:52:55 +01:00
|
|
|
lavf_ctx->oformat = av_guess_format("spdif", NULL, NULL);
|
|
|
|
if (!lavf_ctx->oformat)
|
2011-10-12 19:23:08 +02:00
|
|
|
goto fail;
|
|
|
|
|
2013-11-09 00:32:44 +01:00
|
|
|
void *buffer = av_mallocz(OUTBUF_SIZE);
|
|
|
|
if (!buffer)
|
|
|
|
abort();
|
|
|
|
lavf_ctx->pb = avio_alloc_context(buffer, OUTBUF_SIZE, 1, spdif_ctx, NULL,
|
|
|
|
write_packet, NULL);
|
|
|
|
if (!lavf_ctx->pb) {
|
|
|
|
av_free(buffer);
|
2011-10-12 19:23:08 +02:00
|
|
|
goto fail;
|
2013-11-09 00:32:44 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 00:52:55 +01:00
|
|
|
// Request minimal buffering (not available on Libav)
|
|
|
|
#if LIBAVFORMAT_VERSION_MICRO >= 100
|
2013-11-09 00:32:44 +01:00
|
|
|
lavf_ctx->pb->direct = 1;
|
2013-11-10 00:52:55 +01:00
|
|
|
#endif
|
2013-11-09 00:32:44 +01:00
|
|
|
|
|
|
|
AVStream *stream = avformat_new_stream(lavf_ctx, 0);
|
2011-10-12 19:23:08 +02:00
|
|
|
if (!stream)
|
|
|
|
goto fail;
|
|
|
|
|
2015-05-19 21:34:30 +02:00
|
|
|
stream->codec->codec_id = spdif_ctx->codec_id;
|
2013-11-09 00:32:44 +01:00
|
|
|
|
|
|
|
AVDictionary *format_opts = NULL;
|
2011-10-12 19:23:08 +02:00
|
|
|
|
2013-04-06 22:43:12 +02:00
|
|
|
int num_channels = 0;
|
2013-11-23 21:25:05 +01:00
|
|
|
int sample_format = 0;
|
|
|
|
int samplerate = 0;
|
2015-05-19 21:34:30 +02:00
|
|
|
switch (spdif_ctx->codec_id) {
|
2013-03-09 08:49:56 +01:00
|
|
|
case AV_CODEC_ID_AAC:
|
audio: cleanup spdif format definitions
Before this commit, there was AF_FORMAT_AC3 (the original spdif format,
used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS
and DTS-HD), which was handled as some sort of superset for
AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used
IEC61937-framing, but still was handled as something "separate".
Technically, all of them are pretty similar, but may use different
bitrates. Since digital passthrough pretends to be PCM (just with
special headers that wrap digital packets), this is easily detectable by
the higher samplerate or higher number of channels, so I don't know why
you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs.
AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is
just a mess.
Simplify this by handling all these formats the same way.
AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3).
All AOs just accept all spdif formats now - whether that works or not is
not really clear (seems inconsistent due to earlier attempts to make
DTS-HD work). But on the other hand, enabling spdif requires manual user
interaction, so it doesn't matter much if initialization fails in
slightly less graceful ways if it can't work at all.
At a later point, we will support passthrough with ao_pulse. It seems
the PulseAudio API wants to know the codec type (or maybe not - feeding
it DTS while telling it it's AC3 works), add separate formats for each
codecs. While this reminds of the earlier chaos, it's stricter, and most
code just uses AF_FORMAT_IS_IEC61937().
Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to
include special formats, so that it always describes the fundamental
sample format type. This also ensures valid AF formats are never 0 (this
was probably broken in one of the earlier commits from today).
2014-09-23 22:44:54 +02:00
|
|
|
sample_format = AF_FORMAT_S_AAC;
|
2013-11-23 21:25:05 +01:00
|
|
|
samplerate = 48000;
|
2013-04-06 22:43:12 +02:00
|
|
|
num_channels = 2;
|
2011-10-12 19:23:08 +02:00
|
|
|
break;
|
2013-03-09 08:49:56 +01:00
|
|
|
case AV_CODEC_ID_AC3:
|
audio: cleanup spdif format definitions
Before this commit, there was AF_FORMAT_AC3 (the original spdif format,
used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS
and DTS-HD), which was handled as some sort of superset for
AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used
IEC61937-framing, but still was handled as something "separate".
Technically, all of them are pretty similar, but may use different
bitrates. Since digital passthrough pretends to be PCM (just with
special headers that wrap digital packets), this is easily detectable by
the higher samplerate or higher number of channels, so I don't know why
you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs.
AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is
just a mess.
Simplify this by handling all these formats the same way.
AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3).
All AOs just accept all spdif formats now - whether that works or not is
not really clear (seems inconsistent due to earlier attempts to make
DTS-HD work). But on the other hand, enabling spdif requires manual user
interaction, so it doesn't matter much if initialization fails in
slightly less graceful ways if it can't work at all.
At a later point, we will support passthrough with ao_pulse. It seems
the PulseAudio API wants to know the codec type (or maybe not - feeding
it DTS while telling it it's AC3 works), add separate formats for each
codecs. While this reminds of the earlier chaos, it's stricter, and most
code just uses AF_FORMAT_IS_IEC61937().
Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to
include special formats, so that it always describes the fundamental
sample format type. This also ensures valid AF formats are never 0 (this
was probably broken in one of the earlier commits from today).
2014-09-23 22:44:54 +02:00
|
|
|
sample_format = AF_FORMAT_S_AC3;
|
2013-11-23 21:25:05 +01:00
|
|
|
samplerate = 48000;
|
2013-04-06 22:43:12 +02:00
|
|
|
num_channels = 2;
|
2011-10-12 19:23:08 +02:00
|
|
|
break;
|
2015-05-19 21:35:43 +02:00
|
|
|
case AV_CODEC_ID_DTS: {
|
|
|
|
bool is_hd = profile == FF_PROFILE_DTS_HD_HRA ||
|
2016-01-20 17:18:28 +01:00
|
|
|
profile == FF_PROFILE_DTS_HD_MA ||
|
|
|
|
profile == FF_PROFILE_UNKNOWN;
|
2015-06-05 22:34:48 +02:00
|
|
|
if (spdif_ctx->use_dts_hd && is_hd) {
|
2013-11-09 00:32:44 +01:00
|
|
|
av_dict_set(&format_opts, "dtshd_rate", "768000", 0); // 4*192000
|
2015-05-19 21:34:30 +02:00
|
|
|
sample_format = AF_FORMAT_S_DTSHD;
|
|
|
|
samplerate = 192000;
|
|
|
|
num_channels = 2*4;
|
2013-03-03 18:48:20 +01:00
|
|
|
} else {
|
2015-05-19 21:34:30 +02:00
|
|
|
sample_format = AF_FORMAT_S_DTS;
|
|
|
|
samplerate = 48000;
|
|
|
|
num_channels = 2;
|
2013-03-03 18:48:20 +01:00
|
|
|
}
|
2011-10-12 19:23:08 +02:00
|
|
|
break;
|
2015-05-19 21:35:43 +02:00
|
|
|
}
|
2013-03-09 08:49:56 +01:00
|
|
|
case AV_CODEC_ID_EAC3:
|
audio: cleanup spdif format definitions
Before this commit, there was AF_FORMAT_AC3 (the original spdif format,
used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS
and DTS-HD), which was handled as some sort of superset for
AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used
IEC61937-framing, but still was handled as something "separate".
Technically, all of them are pretty similar, but may use different
bitrates. Since digital passthrough pretends to be PCM (just with
special headers that wrap digital packets), this is easily detectable by
the higher samplerate or higher number of channels, so I don't know why
you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs.
AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is
just a mess.
Simplify this by handling all these formats the same way.
AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3).
All AOs just accept all spdif formats now - whether that works or not is
not really clear (seems inconsistent due to earlier attempts to make
DTS-HD work). But on the other hand, enabling spdif requires manual user
interaction, so it doesn't matter much if initialization fails in
slightly less graceful ways if it can't work at all.
At a later point, we will support passthrough with ao_pulse. It seems
the PulseAudio API wants to know the codec type (or maybe not - feeding
it DTS while telling it it's AC3 works), add separate formats for each
codecs. While this reminds of the earlier chaos, it's stricter, and most
code just uses AF_FORMAT_IS_IEC61937().
Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to
include special formats, so that it always describes the fundamental
sample format type. This also ensures valid AF formats are never 0 (this
was probably broken in one of the earlier commits from today).
2014-09-23 22:44:54 +02:00
|
|
|
sample_format = AF_FORMAT_S_EAC3;
|
2013-11-23 21:25:05 +01:00
|
|
|
samplerate = 192000;
|
2013-04-06 22:43:12 +02:00
|
|
|
num_channels = 2;
|
2011-10-12 19:23:08 +02:00
|
|
|
break;
|
2013-03-09 08:49:56 +01:00
|
|
|
case AV_CODEC_ID_MP3:
|
audio: cleanup spdif format definitions
Before this commit, there was AF_FORMAT_AC3 (the original spdif format,
used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS
and DTS-HD), which was handled as some sort of superset for
AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used
IEC61937-framing, but still was handled as something "separate".
Technically, all of them are pretty similar, but may use different
bitrates. Since digital passthrough pretends to be PCM (just with
special headers that wrap digital packets), this is easily detectable by
the higher samplerate or higher number of channels, so I don't know why
you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs.
AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is
just a mess.
Simplify this by handling all these formats the same way.
AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3).
All AOs just accept all spdif formats now - whether that works or not is
not really clear (seems inconsistent due to earlier attempts to make
DTS-HD work). But on the other hand, enabling spdif requires manual user
interaction, so it doesn't matter much if initialization fails in
slightly less graceful ways if it can't work at all.
At a later point, we will support passthrough with ao_pulse. It seems
the PulseAudio API wants to know the codec type (or maybe not - feeding
it DTS while telling it it's AC3 works), add separate formats for each
codecs. While this reminds of the earlier chaos, it's stricter, and most
code just uses AF_FORMAT_IS_IEC61937().
Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to
include special formats, so that it always describes the fundamental
sample format type. This also ensures valid AF formats are never 0 (this
was probably broken in one of the earlier commits from today).
2014-09-23 22:44:54 +02:00
|
|
|
sample_format = AF_FORMAT_S_MP3;
|
2013-11-23 21:25:05 +01:00
|
|
|
samplerate = 48000;
|
2013-04-06 22:43:12 +02:00
|
|
|
num_channels = 2;
|
2011-10-12 19:23:08 +02:00
|
|
|
break;
|
2013-03-09 08:49:56 +01:00
|
|
|
case AV_CODEC_ID_TRUEHD:
|
audio: cleanup spdif format definitions
Before this commit, there was AF_FORMAT_AC3 (the original spdif format,
used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS
and DTS-HD), which was handled as some sort of superset for
AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used
IEC61937-framing, but still was handled as something "separate".
Technically, all of them are pretty similar, but may use different
bitrates. Since digital passthrough pretends to be PCM (just with
special headers that wrap digital packets), this is easily detectable by
the higher samplerate or higher number of channels, so I don't know why
you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs.
AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is
just a mess.
Simplify this by handling all these formats the same way.
AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3).
All AOs just accept all spdif formats now - whether that works or not is
not really clear (seems inconsistent due to earlier attempts to make
DTS-HD work). But on the other hand, enabling spdif requires manual user
interaction, so it doesn't matter much if initialization fails in
slightly less graceful ways if it can't work at all.
At a later point, we will support passthrough with ao_pulse. It seems
the PulseAudio API wants to know the codec type (or maybe not - feeding
it DTS while telling it it's AC3 works), add separate formats for each
codecs. While this reminds of the earlier chaos, it's stricter, and most
code just uses AF_FORMAT_IS_IEC61937().
Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to
include special formats, so that it always describes the fundamental
sample format type. This also ensures valid AF formats are never 0 (this
was probably broken in one of the earlier commits from today).
2014-09-23 22:44:54 +02:00
|
|
|
sample_format = AF_FORMAT_S_TRUEHD;
|
2013-11-23 21:25:05 +01:00
|
|
|
samplerate = 192000;
|
2013-04-06 22:43:12 +02:00
|
|
|
num_channels = 8;
|
2011-10-12 19:23:08 +02:00
|
|
|
break;
|
|
|
|
default:
|
2013-11-09 00:32:44 +01:00
|
|
|
abort();
|
2011-10-12 19:23:08 +02:00
|
|
|
}
|
2014-11-10 22:01:23 +01:00
|
|
|
mp_audio_set_num_channels(&spdif_ctx->fmt, num_channels);
|
|
|
|
mp_audio_set_format(&spdif_ctx->fmt, sample_format);
|
|
|
|
spdif_ctx->fmt.rate = samplerate;
|
2011-10-12 19:23:08 +02:00
|
|
|
|
2013-11-09 00:32:44 +01:00
|
|
|
if (avformat_write_header(lavf_ctx, &format_opts) < 0) {
|
2013-12-21 18:23:59 +01:00
|
|
|
MP_FATAL(da, "libavformat spdif initialization failed.\n");
|
2013-11-09 00:32:44 +01:00
|
|
|
av_dict_free(&format_opts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
av_dict_free(&format_opts);
|
|
|
|
|
|
|
|
spdif_ctx->need_close = true;
|
|
|
|
|
2015-05-19 21:34:30 +02:00
|
|
|
return 0;
|
2011-10-12 19:23:08 +02:00
|
|
|
|
|
|
|
fail:
|
2013-11-23 21:22:17 +01:00
|
|
|
uninit(da);
|
2015-05-19 21:34:30 +02:00
|
|
|
return -1;
|
2011-10-12 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
2016-01-19 22:24:26 +01:00
|
|
|
static int decode_packet(struct dec_audio *da, struct demux_packet *mpkt,
|
|
|
|
struct mp_audio **out)
|
2011-10-12 19:23:08 +02:00
|
|
|
{
|
2013-11-23 21:22:17 +01:00
|
|
|
struct spdifContext *spdif_ctx = da->priv;
|
2011-10-12 19:23:08 +02:00
|
|
|
|
2013-07-11 19:10:33 +02:00
|
|
|
spdif_ctx->out_buffer_len = 0;
|
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct
support for this, and remove the hack that repacked non-interleaved
audio back to packed audio.
Remove the minlen argument from the decoder callback. Instead of
forcing every decoder to have its own decode loop to fill the buffer
until minlen is reached, leave this to the caller. So if a decoder
doesn't return enough data, it's simply called again. (In future, I
even want to change it so that decoders don't read packets directly,
but instead the caller has to pass packets to the decoders. This fits
well with this change, because now the decoder callback typically
decodes at most one packet.)
ad_mpg123.c receives some heavy refactoring. The main problem is that
it wanted to handle format changes when there was no data in the decode
output buffer yet. This sounds reasonable, but actually it would write
data into a buffer prepared for old data, since the caller doesn't know
about the format change yet. (I.e. the best place for a format change
would be _after_ writing the last sample to the output buffer.) It's
possible that this code was not perfectly sane before this commit,
and perhaps lost one frame of data after a format change, but I didn't
confirm this. Trying to fix this, I ended up rewriting the decoding
and also the probing.
2013-11-12 22:27:44 +01:00
|
|
|
|
|
|
|
if (!mpkt)
|
2016-01-21 22:10:15 +01:00
|
|
|
return 0;
|
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct
support for this, and remove the hack that repacked non-interleaved
audio back to packed audio.
Remove the minlen argument from the decoder callback. Instead of
forcing every decoder to have its own decode loop to fill the buffer
until minlen is reached, leave this to the caller. So if a decoder
doesn't return enough data, it's simply called again. (In future, I
even want to change it so that decoders don't read packets directly,
but instead the caller has to pass packets to the decoders. This fits
well with this change, because now the decoder callback typically
decodes at most one packet.)
ad_mpg123.c receives some heavy refactoring. The main problem is that
it wanted to handle format changes when there was no data in the decode
output buffer yet. This sounds reasonable, but actually it would write
data into a buffer prepared for old data, since the caller doesn't know
about the format change yet. (I.e. the best place for a format change
would be _after_ writing the last sample to the output buffer.) It's
possible that this code was not perfectly sane before this commit,
and perhaps lost one frame of data after a format change, but I didn't
confirm this. Trying to fix this, I ended up rewriting the decoding
and also the probing.
2013-11-12 22:27:44 +01:00
|
|
|
|
2015-11-08 17:22:56 +01:00
|
|
|
double pts = mpkt->pts;
|
|
|
|
|
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct
support for this, and remove the hack that repacked non-interleaved
audio back to packed audio.
Remove the minlen argument from the decoder callback. Instead of
forcing every decoder to have its own decode loop to fill the buffer
until minlen is reached, leave this to the caller. So if a decoder
doesn't return enough data, it's simply called again. (In future, I
even want to change it so that decoders don't read packets directly,
but instead the caller has to pass packets to the decoders. This fits
well with this change, because now the decoder callback typically
decodes at most one packet.)
ad_mpg123.c receives some heavy refactoring. The main problem is that
it wanted to handle format changes when there was no data in the decode
output buffer yet. This sounds reasonable, but actually it would write
data into a buffer prepared for old data, since the caller doesn't know
about the format change yet. (I.e. the best place for a format change
would be _after_ writing the last sample to the output buffer.) It's
possible that this code was not perfectly sane before this commit,
and perhaps lost one frame of data after a format change, but I didn't
confirm this. Trying to fix this, I ended up rewriting the decoding
and also the probing.
2013-11-12 22:27:44 +01:00
|
|
|
AVPacket pkt;
|
av_common: add timebase parameter to mp_set_av_packet()
If the timebase is set, it's used for converting the packet timestamps.
Otherwise, the previous method of reinterpret-casting the mpv style
double timestamps to libavcodec style int64_t timestamps is used.
Also replace the kind of awkward mp_get_av_frame_pkt_ts() function by
mp_pts_from_av(), which simply converts timestamps in a way the old
function did. (Plus it takes a timebase parameter, similar to the
addition to mp_set_av_packet().)
Note that this should not change anything yet. The code in ad_lavc.c and
vd_lavc.c passes NULL for the timebase parameters. We could set
AVCodecContext.pkt_timebase and use that if we want to give libavcodec
"proper" timestamps.
This could be important for ad_lavc.c: some codecs (opus, probably mp3
and aac too) have weird requirements about doing decoding preroll on the
container level, and thus require adjusting the audio start timestamps
in some cases. libavcodec doesn't tell us how much was skipped, so we
either get shifted timestamps (by the length of the skipped data), or we
give it proper timestamps. (Note: libavcodec interprets or changes
timestamps only if pkt_timebase is set, which by default it is not.)
This would require selecting a timebase though, so I feel uncomfortable
with the idea. At least this change paves the way, and will allow some
testing.
2013-12-04 20:12:14 +01:00
|
|
|
mp_set_av_packet(&pkt, mpkt, NULL);
|
2016-01-19 22:24:26 +01:00
|
|
|
mpkt->len = 0; // will be fully consumed
|
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct
support for this, and remove the hack that repacked non-interleaved
audio back to packed audio.
Remove the minlen argument from the decoder callback. Instead of
forcing every decoder to have its own decode loop to fill the buffer
until minlen is reached, leave this to the caller. So if a decoder
doesn't return enough data, it's simply called again. (In future, I
even want to change it so that decoders don't read packets directly,
but instead the caller has to pass packets to the decoders. This fits
well with this change, because now the decoder callback typically
decodes at most one packet.)
ad_mpg123.c receives some heavy refactoring. The main problem is that
it wanted to handle format changes when there was no data in the decode
output buffer yet. This sounds reasonable, but actually it would write
data into a buffer prepared for old data, since the caller doesn't know
about the format change yet. (I.e. the best place for a format change
would be _after_ writing the last sample to the output buffer.) It's
possible that this code was not perfectly sane before this commit,
and perhaps lost one frame of data after a format change, but I didn't
confirm this. Trying to fix this, I ended up rewriting the decoding
and also the probing.
2013-11-12 22:27:44 +01:00
|
|
|
pkt.pts = pkt.dts = 0;
|
2015-05-19 21:34:30 +02:00
|
|
|
if (!spdif_ctx->lavf_ctx) {
|
2015-05-19 21:35:43 +02:00
|
|
|
if (init_filter(da, &pkt) < 0)
|
2016-01-21 22:10:15 +01:00
|
|
|
return -1;
|
2015-05-19 21:34:30 +02:00
|
|
|
}
|
|
|
|
int ret = av_write_frame(spdif_ctx->lavf_ctx, &pkt);
|
|
|
|
avio_flush(spdif_ctx->lavf_ctx->pb);
|
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct
support for this, and remove the hack that repacked non-interleaved
audio back to packed audio.
Remove the minlen argument from the decoder callback. Instead of
forcing every decoder to have its own decode loop to fill the buffer
until minlen is reached, leave this to the caller. So if a decoder
doesn't return enough data, it's simply called again. (In future, I
even want to change it so that decoders don't read packets directly,
but instead the caller has to pass packets to the decoders. This fits
well with this change, because now the decoder callback typically
decodes at most one packet.)
ad_mpg123.c receives some heavy refactoring. The main problem is that
it wanted to handle format changes when there was no data in the decode
output buffer yet. This sounds reasonable, but actually it would write
data into a buffer prepared for old data, since the caller doesn't know
about the format change yet. (I.e. the best place for a format change
would be _after_ writing the last sample to the output buffer.) It's
possible that this code was not perfectly sane before this commit,
and perhaps lost one frame of data after a format change, but I didn't
confirm this. Trying to fix this, I ended up rewriting the decoding
and also the probing.
2013-11-12 22:27:44 +01:00
|
|
|
if (ret < 0)
|
2016-01-21 22:10:15 +01:00
|
|
|
return -1;
|
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct
support for this, and remove the hack that repacked non-interleaved
audio back to packed audio.
Remove the minlen argument from the decoder callback. Instead of
forcing every decoder to have its own decode loop to fill the buffer
until minlen is reached, leave this to the caller. So if a decoder
doesn't return enough data, it's simply called again. (In future, I
even want to change it so that decoders don't read packets directly,
but instead the caller has to pass packets to the decoders. This fits
well with this change, because now the decoder callback typically
decodes at most one packet.)
ad_mpg123.c receives some heavy refactoring. The main problem is that
it wanted to handle format changes when there was no data in the decode
output buffer yet. This sounds reasonable, but actually it would write
data into a buffer prepared for old data, since the caller doesn't know
about the format change yet. (I.e. the best place for a format change
would be _after_ writing the last sample to the output buffer.) It's
possible that this code was not perfectly sane before this commit,
and perhaps lost one frame of data after a format change, but I didn't
confirm this. Trying to fix this, I ended up rewriting the decoding
and also the probing.
2013-11-12 22:27:44 +01:00
|
|
|
|
2014-11-10 22:01:23 +01:00
|
|
|
int samples = spdif_ctx->out_buffer_len / spdif_ctx->fmt.sstride;
|
2016-01-19 21:33:05 +01:00
|
|
|
*out = mp_audio_pool_get(spdif_ctx->pool, &spdif_ctx->fmt, samples);
|
2014-11-10 22:01:23 +01:00
|
|
|
if (!*out)
|
2016-01-21 22:10:15 +01:00
|
|
|
return -1;
|
2014-11-10 22:01:23 +01:00
|
|
|
|
|
|
|
memcpy((*out)->planes[0], spdif_ctx->out_buffer, spdif_ctx->out_buffer_len);
|
2015-11-08 17:22:56 +01:00
|
|
|
(*out)->pts = pts;
|
2014-07-21 19:29:37 +02:00
|
|
|
|
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct
support for this, and remove the hack that repacked non-interleaved
audio back to packed audio.
Remove the minlen argument from the decoder callback. Instead of
forcing every decoder to have its own decode loop to fill the buffer
until minlen is reached, leave this to the caller. So if a decoder
doesn't return enough data, it's simply called again. (In future, I
even want to change it so that decoders don't read packets directly,
but instead the caller has to pass packets to the decoders. This fits
well with this change, because now the decoder callback typically
decodes at most one packet.)
ad_mpg123.c receives some heavy refactoring. The main problem is that
it wanted to handle format changes when there was no data in the decode
output buffer yet. This sounds reasonable, but actually it would write
data into a buffer prepared for old data, since the caller doesn't know
about the format change yet. (I.e. the best place for a format change
would be _after_ writing the last sample to the output buffer.) It's
possible that this code was not perfectly sane before this commit,
and perhaps lost one frame of data after a format change, but I didn't
confirm this. Trying to fix this, I ended up rewriting the decoding
and also the probing.
2013-11-12 22:27:44 +01:00
|
|
|
return 0;
|
2011-10-12 19:23:08 +02:00
|
|
|
}
|
|
|
|
|
2013-11-23 21:22:17 +01:00
|
|
|
static int control(struct dec_audio *da, int cmd, void *arg)
|
2011-10-12 19:23:08 +02:00
|
|
|
{
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2013-11-09 00:32:44 +01:00
|
|
|
static const int codecs[] = {
|
|
|
|
AV_CODEC_ID_AAC,
|
|
|
|
AV_CODEC_ID_AC3,
|
|
|
|
AV_CODEC_ID_DTS,
|
|
|
|
AV_CODEC_ID_EAC3,
|
|
|
|
AV_CODEC_ID_MP3,
|
|
|
|
AV_CODEC_ID_TRUEHD,
|
|
|
|
AV_CODEC_ID_NONE
|
|
|
|
};
|
core: redo how codecs are mapped, remove codecs.conf
Use codec names instead of FourCCs to identify codecs. Rewrite how
codecs are selected and initialized. Now each decoder exports a list
of decoders (and the codec it supports) via add_decoders(). The order
matters, and the first decoder for a given decoder is preferred over
the other decoders. E.g. all ad_mpg123 decoders are preferred over
ad_lavc, because it comes first in the mpcodecs_ad_drivers array.
Likewise, decoders within ad_lavc that are enumerated first by
libavcodec (using av_codec_next()) are preferred. (This is actually
critical to select h264 software decoding by default instead of vdpau.
libavcodec and ffmpeg/avconv use the same method to select decoders by
default, so we hope this is sane.)
The codec names follow libavcodec's codec names as defined by
AVCodecDescriptor.name (see libavcodec/codec_desc.c). Some decoders
have names different from the canonical codec name. The AVCodecDescriptor
API is relatively new, so we need a compatibility layer for older
libavcodec versions for codec names that are referenced internally,
and which are different from the decoder name. (Add a configure check
for that, because checking versions is getting way too messy.)
demux/codec_tags.c is generated from the former codecs.conf (minus
"special" decoders like vdpau, and excluding the mappings that are the
same as the mappings libavformat's exported RIFF tables). It contains
all the mappings from FourCCs to codec name. This is needed for
demux_mkv, demux_mpg, demux_avi and demux_asf. demux_lavf will set the
codec as determined by libavformat, while the other demuxers have to do
this on their own, using the mp_set_audio/video_codec_from_tag()
functions. Note that the sh_audio/video->format members don't uniquely
identify the codec anymore, and sh->codec takes over this role.
Replace the --ac/--vc/--afm/--vfm with new --vd/--ad options, which
provide cover the functionality of the removed switched.
Note: there's no CODECS_FLAG_FLIP flag anymore. This means some obscure
container/video combinations (e.g. the sample Film_200_zygo_pro.mov)
are played flipped. ffplay/avplay doesn't handle this properly either,
so we don't care and blame ffmeg/libav instead.
2013-02-09 15:15:19 +01:00
|
|
|
|
|
|
|
static void add_decoders(struct mp_decoder_list *list)
|
|
|
|
{
|
2013-03-09 08:49:56 +01:00
|
|
|
for (int n = 0; codecs[n] != AV_CODEC_ID_NONE; n++) {
|
core: redo how codecs are mapped, remove codecs.conf
Use codec names instead of FourCCs to identify codecs. Rewrite how
codecs are selected and initialized. Now each decoder exports a list
of decoders (and the codec it supports) via add_decoders(). The order
matters, and the first decoder for a given decoder is preferred over
the other decoders. E.g. all ad_mpg123 decoders are preferred over
ad_lavc, because it comes first in the mpcodecs_ad_drivers array.
Likewise, decoders within ad_lavc that are enumerated first by
libavcodec (using av_codec_next()) are preferred. (This is actually
critical to select h264 software decoding by default instead of vdpau.
libavcodec and ffmpeg/avconv use the same method to select decoders by
default, so we hope this is sane.)
The codec names follow libavcodec's codec names as defined by
AVCodecDescriptor.name (see libavcodec/codec_desc.c). Some decoders
have names different from the canonical codec name. The AVCodecDescriptor
API is relatively new, so we need a compatibility layer for older
libavcodec versions for codec names that are referenced internally,
and which are different from the decoder name. (Add a configure check
for that, because checking versions is getting way too messy.)
demux/codec_tags.c is generated from the former codecs.conf (minus
"special" decoders like vdpau, and excluding the mappings that are the
same as the mappings libavformat's exported RIFF tables). It contains
all the mappings from FourCCs to codec name. This is needed for
demux_mkv, demux_mpg, demux_avi and demux_asf. demux_lavf will set the
codec as determined by libavformat, while the other demuxers have to do
this on their own, using the mp_set_audio/video_codec_from_tag()
functions. Note that the sh_audio/video->format members don't uniquely
identify the codec anymore, and sh->codec takes over this role.
Replace the --ac/--vc/--afm/--vfm with new --vd/--ad options, which
provide cover the functionality of the removed switched.
Note: there's no CODECS_FLAG_FLIP flag anymore. This means some obscure
container/video combinations (e.g. the sample Film_200_zygo_pro.mov)
are played flipped. ffplay/avplay doesn't handle this properly either,
so we don't care and blame ffmeg/libav instead.
2013-02-09 15:15:19 +01:00
|
|
|
const char *format = mp_codec_from_av_codec_id(codecs[n]);
|
|
|
|
if (format) {
|
|
|
|
mp_add_decoder(list, "spdif", format, format,
|
|
|
|
"libavformat/spdifenc audio pass-through decoder");
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 22:34:48 +02:00
|
|
|
mp_add_decoder(list, "spdif", "dts", "dts-hd",
|
|
|
|
"libavformat/spdifenc audio pass-through decoder");
|
core: redo how codecs are mapped, remove codecs.conf
Use codec names instead of FourCCs to identify codecs. Rewrite how
codecs are selected and initialized. Now each decoder exports a list
of decoders (and the codec it supports) via add_decoders(). The order
matters, and the first decoder for a given decoder is preferred over
the other decoders. E.g. all ad_mpg123 decoders are preferred over
ad_lavc, because it comes first in the mpcodecs_ad_drivers array.
Likewise, decoders within ad_lavc that are enumerated first by
libavcodec (using av_codec_next()) are preferred. (This is actually
critical to select h264 software decoding by default instead of vdpau.
libavcodec and ffmpeg/avconv use the same method to select decoders by
default, so we hope this is sane.)
The codec names follow libavcodec's codec names as defined by
AVCodecDescriptor.name (see libavcodec/codec_desc.c). Some decoders
have names different from the canonical codec name. The AVCodecDescriptor
API is relatively new, so we need a compatibility layer for older
libavcodec versions for codec names that are referenced internally,
and which are different from the decoder name. (Add a configure check
for that, because checking versions is getting way too messy.)
demux/codec_tags.c is generated from the former codecs.conf (minus
"special" decoders like vdpau, and excluding the mappings that are the
same as the mappings libavformat's exported RIFF tables). It contains
all the mappings from FourCCs to codec name. This is needed for
demux_mkv, demux_mpg, demux_avi and demux_asf. demux_lavf will set the
codec as determined by libavformat, while the other demuxers have to do
this on their own, using the mp_set_audio/video_codec_from_tag()
functions. Note that the sh_audio/video->format members don't uniquely
identify the codec anymore, and sh->codec takes over this role.
Replace the --ac/--vc/--afm/--vfm with new --vd/--ad options, which
provide cover the functionality of the removed switched.
Note: there's no CODECS_FLAG_FLIP flag anymore. This means some obscure
container/video combinations (e.g. the sample Film_200_zygo_pro.mov)
are played flipped. ffplay/avplay doesn't handle this properly either,
so we don't care and blame ffmeg/libav instead.
2013-02-09 15:15:19 +01:00
|
|
|
}
|
2013-07-22 14:41:56 +02:00
|
|
|
|
|
|
|
const struct ad_functions ad_spdif = {
|
|
|
|
.name = "spdif",
|
|
|
|
.add_decoders = add_decoders,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.control = control,
|
2014-07-21 19:29:37 +02:00
|
|
|
.decode_packet = decode_packet,
|
2013-07-22 14:41:56 +02:00
|
|
|
};
|