2012-09-14 15:51:26 +00:00
|
|
|
/*
|
|
|
|
* audio encoding using libavformat
|
2017-06-13 18:22:15 +00:00
|
|
|
*
|
2012-12-28 10:41:30 +00:00
|
|
|
* Copyright (C) 2011-2012 Rudolf Polzer <divVerent@xonotic.org>
|
2012-09-14 15:51:26 +00:00
|
|
|
* NOTE: this file is partially based on ao_pcm.c by Atmosfear
|
|
|
|
*
|
2012-12-28 10:41:30 +00:00
|
|
|
* This file is part of mpv.
|
2012-09-14 15:51:26 +00:00
|
|
|
*
|
2017-06-13 18:22:15 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-09-14 15:51:26 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2012-09-14 15:51:26 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-06-13 18:22:15 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2012-09-14 15:51:26 +00:00
|
|
|
*
|
2017-06-13 18:22:15 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2012-09-14 15:51:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-11-13 18:19:57 +00:00
|
|
|
#include <assert.h>
|
2013-11-16 19:20:11 +00:00
|
|
|
#include <limits.h>
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
2013-11-13 18:19:57 +00:00
|
|
|
#include "audio/fmt-conversion.h"
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/encode_lavc.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
struct priv {
|
|
|
|
AVStream *stream;
|
2016-04-11 18:24:48 +00:00
|
|
|
AVCodecContext *codec;
|
2012-09-14 15:51:26 +00:00
|
|
|
int pcmhack;
|
|
|
|
int aframesize;
|
|
|
|
int aframecount;
|
|
|
|
int64_t savepts;
|
|
|
|
int framecount;
|
|
|
|
int64_t lastpts;
|
|
|
|
int sample_size;
|
|
|
|
const void *sample_padding;
|
2012-09-25 09:53:29 +00:00
|
|
|
double expected_next_pts;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
AVRational worst_time_base;
|
|
|
|
int worst_time_base_is_stream;
|
2014-11-12 11:16:07 +00:00
|
|
|
|
|
|
|
bool shutdown;
|
2012-09-14 15:51:26 +00:00
|
|
|
};
|
|
|
|
|
2015-09-10 21:38:42 +00:00
|
|
|
static bool supports_format(AVCodec *codec, int format)
|
2013-11-16 19:20:11 +00:00
|
|
|
{
|
|
|
|
for (const enum AVSampleFormat *sampleformat = codec->sample_fmts;
|
|
|
|
sampleformat && *sampleformat != AV_SAMPLE_FMT_NONE;
|
|
|
|
++sampleformat)
|
|
|
|
{
|
2015-09-11 07:01:49 +00:00
|
|
|
if (af_from_avformat(*sampleformat) == format)
|
2015-09-10 21:38:42 +00:00
|
|
|
return true;
|
2013-11-16 19:20:11 +00:00
|
|
|
}
|
2015-09-10 21:38:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-11-16 19:20:11 +00:00
|
|
|
|
2015-09-10 21:38:42 +00:00
|
|
|
static void select_format(struct ao *ao, AVCodec *codec)
|
|
|
|
{
|
|
|
|
int formats[AF_FORMAT_COUNT];
|
|
|
|
af_get_best_sample_formats(ao->format, formats);
|
|
|
|
|
2015-09-11 07:01:49 +00:00
|
|
|
for (int n = 0; formats[n]; n++) {
|
2015-09-10 21:38:42 +00:00
|
|
|
if (supports_format(codec, formats[n])) {
|
|
|
|
ao->format = formats[n];
|
|
|
|
break;
|
|
|
|
}
|
2013-11-16 19:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
// open & setup audio device
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
struct priv *ac = talloc_zero(ao, struct priv);
|
|
|
|
AVCodec *codec;
|
|
|
|
|
2013-11-16 19:20:11 +00:00
|
|
|
ao->priv = ac;
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
if (!encode_lavc_available(ao->encode_lavc_ctx)) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "the option --o (output file) must be specified\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_lock(&ao->encode_lavc_ctx->lock);
|
|
|
|
|
2016-04-11 18:24:48 +00:00
|
|
|
if (encode_lavc_alloc_stream(ao->encode_lavc_ctx,
|
|
|
|
AVMEDIA_TYPE_AUDIO,
|
|
|
|
&ac->stream, &ac->codec) < 0) {
|
|
|
|
MP_ERR(ao, "could not get a new audio stream\n");
|
|
|
|
goto fail;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 18:24:48 +00:00
|
|
|
codec = ao->encode_lavc_ctx->ac;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2016-03-17 07:58:07 +00:00
|
|
|
int samplerate = af_select_best_samplerate(ao->samplerate,
|
|
|
|
codec->supported_samplerates);
|
|
|
|
if (samplerate > 0)
|
|
|
|
ao->samplerate = samplerate;
|
|
|
|
|
2014-07-23 14:09:44 +00:00
|
|
|
// TODO: Remove this redundancy with encode_lavc_alloc_stream also
|
|
|
|
// setting the time base.
|
|
|
|
// Using codec->time_bvase is deprecated, but needed for older lavf.
|
|
|
|
ac->stream->time_base.num = 1;
|
|
|
|
ac->stream->time_base.den = ao->samplerate;
|
2016-04-11 18:24:48 +00:00
|
|
|
ac->codec->time_base.num = 1;
|
|
|
|
ac->codec->time_base.den = ao->samplerate;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2016-04-11 18:24:48 +00:00
|
|
|
ac->codec->sample_rate = ao->samplerate;
|
2013-04-05 21:06:22 +00:00
|
|
|
|
2013-05-09 16:06:26 +00:00
|
|
|
struct mp_chmap_sel sel = {0};
|
|
|
|
mp_chmap_sel_add_any(&sel);
|
2016-08-04 18:49:20 +00:00
|
|
|
if (!ao_chmap_sel_adjust2(ao, &sel, &ao->channels, false))
|
2014-03-08 22:38:53 +00:00
|
|
|
goto fail;
|
2013-04-05 21:06:22 +00:00
|
|
|
mp_chmap_reorder_to_lavc(&ao->channels);
|
2016-04-11 18:24:48 +00:00
|
|
|
ac->codec->channels = ao->channels.num;
|
|
|
|
ac->codec->channel_layout = mp_chmap_to_lavc(&ao->channels);
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2016-04-11 18:24:48 +00:00
|
|
|
ac->codec->sample_fmt = AV_SAMPLE_FMT_NONE;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2013-11-16 19:20:11 +00:00
|
|
|
select_format(ao, codec);
|
2012-12-03 19:16:17 +00:00
|
|
|
|
2015-06-26 21:06:37 +00:00
|
|
|
ac->sample_size = af_fmt_to_bytes(ao->format);
|
2016-04-11 18:24:48 +00:00
|
|
|
ac->codec->sample_fmt = af_to_avformat(ao->format);
|
|
|
|
ac->codec->bits_per_raw_sample = ac->sample_size * 8;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2016-04-11 18:24:48 +00:00
|
|
|
if (encode_lavc_open_codec(ao->encode_lavc_ctx, ac->codec) < 0)
|
2014-03-08 22:38:53 +00:00
|
|
|
goto fail;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
ac->pcmhack = 0;
|
2016-04-11 18:24:48 +00:00
|
|
|
if (ac->codec->frame_size <= 1)
|
|
|
|
ac->pcmhack = av_get_bits_per_sample(ac->codec->codec_id) / 8;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2016-06-24 18:20:32 +00:00
|
|
|
if (ac->pcmhack)
|
2012-09-14 15:51:26 +00:00
|
|
|
ac->aframesize = 16384; // "enough"
|
2016-06-24 18:20:32 +00:00
|
|
|
else
|
2016-04-11 18:24:48 +00:00
|
|
|
ac->aframesize = ac->codec->frame_size;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
// enough frames for at least 0.25 seconds
|
|
|
|
ac->framecount = ceil(ao->samplerate * 0.25 / ac->aframesize);
|
|
|
|
// but at least one!
|
|
|
|
ac->framecount = FFMAX(ac->framecount, 1);
|
|
|
|
|
2014-05-10 08:32:23 +00:00
|
|
|
ac->savepts = AV_NOPTS_VALUE;
|
|
|
|
ac->lastpts = AV_NOPTS_VALUE;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
ao->untimed = true;
|
|
|
|
|
2017-06-25 13:57:15 +00:00
|
|
|
ao->period_size = ac->aframesize * ac->framecount;
|
|
|
|
|
2015-10-26 14:54:00 +00:00
|
|
|
if (ao->channels.num > AV_NUM_DATA_POINTERS)
|
|
|
|
goto fail;
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&ao->encode_lavc_ctx->lock);
|
2012-09-14 15:51:26 +00:00
|
|
|
return 0;
|
2014-03-08 22:38:53 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
pthread_mutex_unlock(&ao->encode_lavc_ctx->lock);
|
2014-11-12 11:16:07 +00:00
|
|
|
ac->shutdown = true;
|
2014-03-08 22:38:53 +00:00
|
|
|
return -1;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2016-06-24 18:20:32 +00:00
|
|
|
static void encode(struct ao *ao, double apts, void **data);
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
2013-11-16 17:50:07 +00:00
|
|
|
struct priv *ac = ao->priv;
|
2012-11-01 11:25:50 +00:00
|
|
|
struct encode_lavc_context *ectx = ao->encode_lavc_ctx;
|
|
|
|
|
2014-11-12 11:16:07 +00:00
|
|
|
if (!ac || ac->shutdown)
|
|
|
|
return;
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_lock(&ectx->lock);
|
|
|
|
|
2012-11-01 11:25:50 +00:00
|
|
|
if (!encode_lavc_start(ectx)) {
|
2014-04-10 21:58:12 +00:00
|
|
|
MP_WARN(ao, "not even ready to encode audio at end -> dropped\n");
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&ectx->lock);
|
2012-11-01 11:25:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-24 18:20:32 +00:00
|
|
|
if (ac->stream) {
|
2013-11-16 17:50:07 +00:00
|
|
|
double outpts = ac->expected_next_pts;
|
|
|
|
if (!ectx->options->rawts && ectx->options->copyts)
|
|
|
|
outpts += ectx->discontinuity_pts_offset;
|
2016-04-11 18:24:48 +00:00
|
|
|
outpts += encode_lavc_getoffset(ectx, ac->codec);
|
2016-06-24 18:20:32 +00:00
|
|
|
encode(ao, outpts, NULL);
|
2013-11-16 17:50:07 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&ectx->lock);
|
2014-11-12 11:16:07 +00:00
|
|
|
|
|
|
|
ac->shutdown = true;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 13:57:15 +00:00
|
|
|
// return: how many samples can be played without blocking
|
2012-09-14 15:51:26 +00:00
|
|
|
static int get_space(struct ao *ao)
|
|
|
|
{
|
2013-06-16 17:15:32 +00:00
|
|
|
struct priv *ac = ao->priv;
|
|
|
|
|
2013-11-10 22:24:21 +00:00
|
|
|
return ac->aframesize * ac->framecount;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 18:20:32 +00:00
|
|
|
static void write_packet(struct ao *ao, AVPacket *packet)
|
|
|
|
{
|
|
|
|
// TODO: Can we unify this with the equivalent video code path?
|
|
|
|
struct priv *ac = ao->priv;
|
|
|
|
|
|
|
|
packet->stream_index = ac->stream->index;
|
|
|
|
if (packet->pts != AV_NOPTS_VALUE) {
|
|
|
|
packet->pts = av_rescale_q(packet->pts,
|
|
|
|
ac->codec->time_base,
|
|
|
|
ac->stream->time_base);
|
|
|
|
} else {
|
|
|
|
// Do we need this at all? Better be safe than sorry...
|
|
|
|
MP_WARN(ao, "encoder lost pts, why?\n");
|
|
|
|
if (ac->savepts != MP_NOPTS_VALUE) {
|
|
|
|
packet->pts = av_rescale_q(ac->savepts,
|
|
|
|
ac->codec->time_base,
|
|
|
|
ac->stream->time_base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (packet->dts != AV_NOPTS_VALUE) {
|
|
|
|
packet->dts = av_rescale_q(packet->dts,
|
|
|
|
ac->codec->time_base,
|
|
|
|
ac->stream->time_base);
|
|
|
|
}
|
|
|
|
if (packet->duration > 0) {
|
|
|
|
packet->duration = av_rescale_q(packet->duration,
|
|
|
|
ac->codec->time_base,
|
|
|
|
ac->stream->time_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
ac->savepts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
if (encode_lavc_write_frame(ao->encode_lavc_ctx,
|
|
|
|
ac->stream, packet) < 0) {
|
|
|
|
MP_ERR(ao, "error writing at %d %d/%d\n",
|
|
|
|
(int) packet->pts,
|
|
|
|
ac->stream->time_base.num,
|
|
|
|
ac->stream->time_base.den);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void encode_audio_and_write(struct ao *ao, AVFrame *frame)
|
|
|
|
{
|
|
|
|
// TODO: Can we unify this with the equivalent video code path?
|
|
|
|
struct priv *ac = ao->priv;
|
|
|
|
AVPacket packet = {0};
|
|
|
|
|
|
|
|
int status = avcodec_send_frame(ac->codec, frame);
|
|
|
|
if (status < 0) {
|
|
|
|
MP_ERR(ao, "error encoding at %d %d/%d\n",
|
|
|
|
frame ? (int) frame->pts : -1,
|
|
|
|
ac->codec->time_base.num,
|
|
|
|
ac->codec->time_base.den);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
av_init_packet(&packet);
|
|
|
|
status = avcodec_receive_packet(ac->codec, &packet);
|
|
|
|
if (status == AVERROR(EAGAIN)) { // No more packets for now.
|
|
|
|
if (frame == NULL) {
|
|
|
|
MP_ERR(ao, "sent flush frame, got EAGAIN");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status == AVERROR_EOF) { // No more packets, ever.
|
|
|
|
if (frame != NULL) {
|
|
|
|
MP_ERR(ao, "sent audio frame, got EOF");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status < 0) {
|
|
|
|
MP_ERR(ao, "error encoding at %d %d/%d\n",
|
|
|
|
frame ? (int) frame->pts : -1,
|
|
|
|
ac->codec->time_base.num,
|
|
|
|
ac->codec->time_base.den);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (frame) {
|
|
|
|
if (ac->savepts == AV_NOPTS_VALUE)
|
|
|
|
ac->savepts = frame->pts;
|
|
|
|
}
|
|
|
|
encode_lavc_write_stats(ao->encode_lavc_ctx, ac->codec);
|
|
|
|
write_packet(ao, &packet);
|
|
|
|
av_packet_unref(&packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
// must get exactly ac->aframesize amount of data
|
2016-06-24 18:20:32 +00:00
|
|
|
static void encode(struct ao *ao, double apts, void **data)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
struct priv *ac = ao->priv;
|
|
|
|
struct encode_lavc_context *ectx = ao->encode_lavc_ctx;
|
|
|
|
double realapts = ac->aframecount * (double) ac->aframesize /
|
|
|
|
ao->samplerate;
|
|
|
|
|
|
|
|
ac->aframecount++;
|
|
|
|
|
2012-09-25 09:53:29 +00:00
|
|
|
if (data)
|
2012-09-14 15:51:26 +00:00
|
|
|
ectx->audio_pts_offset = realapts - apts;
|
|
|
|
|
2014-03-16 11:57:14 +00:00
|
|
|
if(data) {
|
|
|
|
AVFrame *frame = av_frame_alloc();
|
2014-03-16 11:58:58 +00:00
|
|
|
frame->format = af_to_avformat(ao->format);
|
2012-09-14 15:51:26 +00:00
|
|
|
frame->nb_samples = ac->aframesize;
|
2012-12-03 19:16:17 +00:00
|
|
|
|
2015-06-26 21:06:37 +00:00
|
|
|
size_t num_planes = af_fmt_is_planar(ao->format) ? ao->channels.num : 1;
|
2014-11-21 09:03:23 +00:00
|
|
|
assert(num_planes <= AV_NUM_DATA_POINTERS);
|
|
|
|
for (int n = 0; n < num_planes; n++)
|
2013-11-13 18:19:57 +00:00
|
|
|
frame->extended_data[n] = data[n];
|
2012-12-03 19:16:17 +00:00
|
|
|
|
2013-11-13 18:19:57 +00:00
|
|
|
frame->linesize[0] = frame->nb_samples * ao->sstride;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2012-09-25 09:53:29 +00:00
|
|
|
if (ectx->options->rawts || ectx->options->copyts) {
|
2012-09-14 15:51:26 +00:00
|
|
|
// real audio pts
|
2016-04-11 18:24:48 +00:00
|
|
|
frame->pts = floor(apts * ac->codec->time_base.den / ac->codec->time_base.num + 0.5);
|
2012-09-14 15:51:26 +00:00
|
|
|
} else {
|
|
|
|
// audio playback time
|
2016-04-11 18:24:48 +00:00
|
|
|
frame->pts = floor(realapts * ac->codec->time_base.den / ac->codec->time_base.num + 0.5);
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 18:24:48 +00:00
|
|
|
int64_t frame_pts = av_rescale_q(frame->pts, ac->codec->time_base, ac->worst_time_base);
|
2014-05-10 08:32:23 +00:00
|
|
|
if (ac->lastpts != AV_NOPTS_VALUE && frame_pts <= ac->lastpts) {
|
2012-09-14 15:51:26 +00:00
|
|
|
// this indicates broken video
|
|
|
|
// (video pts failing to increase fast enough to match audio)
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "audio frame pts went backwards (%d <- %d), autofixed\n",
|
|
|
|
(int)frame->pts, (int)ac->lastpts);
|
2012-09-14 15:51:26 +00:00
|
|
|
frame_pts = ac->lastpts + 1;
|
2016-04-11 18:24:48 +00:00
|
|
|
frame->pts = av_rescale_q(frame_pts, ac->worst_time_base, ac->codec->time_base);
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
ac->lastpts = frame_pts;
|
|
|
|
|
2016-04-11 18:24:48 +00:00
|
|
|
frame->quality = ac->codec->global_quality;
|
2016-06-24 18:20:32 +00:00
|
|
|
encode_audio_and_write(ao, frame);
|
2014-03-16 11:57:14 +00:00
|
|
|
av_frame_free(&frame);
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
else
|
2016-06-24 18:20:32 +00:00
|
|
|
encode_audio_and_write(ao, NULL);
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 18:19:57 +00:00
|
|
|
// this should round samples down to frame sizes
|
2013-11-10 22:24:21 +00:00
|
|
|
// return: number of samples played
|
2013-11-13 18:19:57 +00:00
|
|
|
static int play(struct ao *ao, void **data, int samples, int flags)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
struct priv *ac = ao->priv;
|
|
|
|
struct encode_lavc_context *ectx = ao->encode_lavc_ctx;
|
|
|
|
int bufpos = 0;
|
|
|
|
double nextpts;
|
2012-09-25 09:53:29 +00:00
|
|
|
double outpts;
|
2014-06-12 09:42:00 +00:00
|
|
|
int orig_samples = samples;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_lock(&ectx->lock);
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
if (!encode_lavc_start(ectx)) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "not ready yet for encoding audio\n");
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&ectx->lock);
|
2012-09-14 15:51:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
audio: don't let ao_lavc access frontend internals, change gapless audio
ao_lavc.c accesses ao->buffer, which I consider internal. The access was
done in ao_lavc.c/uninit(), which tried to get the left-over audio in
order to write the last (possibly partial) audio frame. The play()
function didn't accept partial frames, because the AOPLAY_FINAL_CHUNK
flag was not correctly set, and handling it otherwise would require an
internal FIFO.
Fix this by making sure that with gapless audio (used with encoding),
the AOPLAY_FINAL_CHUNK is set only once, instead when each file ends.
Basically, move the hack in ao_lavc's uninit to uninit_player.
One thing can not be entirely correctly handled: if gapless audio is
active, we don't know really whether the AO is closed because the file
ended playing (i.e. we want to send the buffered remainder of the audio
to the AO), or whether the user is quitting the player. (The stop_play
flag is overwritten, fixing that is perhaps not worth it.) Handle this
by adding additional code to drain the AO and the buffers when playback
is quit (see play_current_file() change).
Test case: mpv avdevice://lavfi:sine=441 avdevice://lavfi:sine=441 -length 0.2267 -gapless-audio
2013-11-08 19:00:58 +00:00
|
|
|
|
2014-03-07 14:23:03 +00:00
|
|
|
double pts = ectx->last_audio_in_pts;
|
2014-07-16 12:19:24 +00:00
|
|
|
pts += ectx->samples_since_last_pts / (double)ao->samplerate;
|
2014-03-07 14:23:03 +00:00
|
|
|
|
2015-06-26 21:06:37 +00:00
|
|
|
size_t num_planes = af_fmt_is_planar(ao->format) ? ao->channels.num : 1;
|
2013-11-16 13:10:00 +00:00
|
|
|
|
2014-06-12 09:42:00 +00:00
|
|
|
void *tempdata = NULL;
|
2014-11-21 02:49:22 +00:00
|
|
|
void *padded[MP_NUM_CHANNELS];
|
2014-06-12 09:42:00 +00:00
|
|
|
|
|
|
|
if ((flags & AOPLAY_FINAL_CHUNK) && (samples % ac->aframesize)) {
|
|
|
|
tempdata = talloc_new(NULL);
|
|
|
|
size_t bytelen = samples * ao->sstride;
|
|
|
|
size_t extralen = (ac->aframesize - 1) * ao->sstride;
|
|
|
|
for (int n = 0; n < num_planes; n++) {
|
|
|
|
padded[n] = talloc_size(tempdata, bytelen + extralen);
|
|
|
|
memcpy(padded[n], data[n], bytelen);
|
|
|
|
af_fill_silence((char *)padded[n] + bytelen, extralen, ao->format);
|
|
|
|
}
|
|
|
|
data = padded;
|
|
|
|
samples = (bytelen + extralen) / ao->sstride;
|
audio: don't let ao_lavc access frontend internals, change gapless audio
ao_lavc.c accesses ao->buffer, which I consider internal. The access was
done in ao_lavc.c/uninit(), which tried to get the left-over audio in
order to write the last (possibly partial) audio frame. The play()
function didn't accept partial frames, because the AOPLAY_FINAL_CHUNK
flag was not correctly set, and handling it otherwise would require an
internal FIFO.
Fix this by making sure that with gapless audio (used with encoding),
the AOPLAY_FINAL_CHUNK is set only once, instead when each file ends.
Basically, move the hack in ao_lavc's uninit to uninit_player.
One thing can not be entirely correctly handled: if gapless audio is
active, we don't know really whether the AO is closed because the file
ended playing (i.e. we want to send the buffered remainder of the audio
to the AO), or whether the user is quitting the player. (The stop_play
flag is overwritten, fixing that is perhaps not worth it.) Handle this
by adding additional code to drain the AO and the buffers when playback
is quit (see play_current_file() change).
Test case: mpv avdevice://lavfi:sine=441 avdevice://lavfi:sine=441 -length 0.2267 -gapless-audio
2013-11-08 19:00:58 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 09:53:29 +00:00
|
|
|
if (pts == MP_NOPTS_VALUE) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "frame without pts, please report; synthesizing pts instead\n");
|
2012-09-25 09:53:29 +00:00
|
|
|
// synthesize pts from previous expected next pts
|
|
|
|
pts = ac->expected_next_pts;
|
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
if (ac->worst_time_base.den == 0) {
|
2016-04-11 18:24:48 +00:00
|
|
|
//if (ac->codec->time_base.num / ac->codec->time_base.den >= ac->stream->time_base.num / ac->stream->time_base.den)
|
|
|
|
if (ac->codec->time_base.num * (double) ac->stream->time_base.den >=
|
|
|
|
ac->stream->time_base.num * (double) ac->codec->time_base.den) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_VERBOSE(ao, "NOTE: using codec time base (%d/%d) for pts "
|
|
|
|
"adjustment; the stream base (%d/%d) is not worse.\n",
|
2016-04-11 18:24:48 +00:00
|
|
|
(int)ac->codec->time_base.num,
|
|
|
|
(int)ac->codec->time_base.den,
|
2013-08-22 21:12:35 +00:00
|
|
|
(int)ac->stream->time_base.num,
|
|
|
|
(int)ac->stream->time_base.den);
|
2016-04-11 18:24:48 +00:00
|
|
|
ac->worst_time_base = ac->codec->time_base;
|
2012-09-14 15:51:26 +00:00
|
|
|
ac->worst_time_base_is_stream = 0;
|
|
|
|
} else {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "NOTE: not using codec time base (%d/%d) for pts "
|
|
|
|
"adjustment; the stream base (%d/%d) is worse.\n",
|
2016-04-11 18:24:48 +00:00
|
|
|
(int)ac->codec->time_base.num,
|
|
|
|
(int)ac->codec->time_base.den,
|
2013-08-22 21:12:35 +00:00
|
|
|
(int)ac->stream->time_base.num,
|
|
|
|
(int)ac->stream->time_base.den);
|
2012-09-14 15:51:26 +00:00
|
|
|
ac->worst_time_base = ac->stream->time_base;
|
|
|
|
ac->worst_time_base_is_stream = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: we use the following "axiom" of av_rescale_q:
|
|
|
|
// if time base A is worse than time base B, then
|
|
|
|
// av_rescale_q(av_rescale_q(x, A, B), B, A) == x
|
|
|
|
// this can be proven as long as av_rescale_q rounds to nearest, which
|
|
|
|
// it currently does
|
|
|
|
|
|
|
|
// av_rescale_q(x, A, B) * B = "round x*A to nearest multiple of B"
|
|
|
|
// and:
|
|
|
|
// av_rescale_q(av_rescale_q(x, A, B), B, A) * A
|
|
|
|
// == "round av_rescale_q(x, A, B)*B to nearest multiple of A"
|
|
|
|
// == "round 'round x*A to nearest multiple of B' to nearest multiple of A"
|
|
|
|
//
|
|
|
|
// assume this fails. Then there is a value of x*A, for which the
|
|
|
|
// nearest multiple of B is outside the range [(x-0.5)*A, (x+0.5)*A[.
|
|
|
|
// Absurd, as this range MUST contain at least one multiple of B.
|
|
|
|
}
|
|
|
|
|
2013-11-11 12:03:22 +00:00
|
|
|
// Fix and apply the discontinuity pts offset.
|
2012-09-25 09:53:29 +00:00
|
|
|
if (!ectx->options->rawts && ectx->options->copyts) {
|
|
|
|
// fix the discontinuity pts offset
|
2013-11-11 12:03:22 +00:00
|
|
|
nextpts = pts;
|
2012-09-25 09:53:29 +00:00
|
|
|
if (ectx->discontinuity_pts_offset == MP_NOPTS_VALUE) {
|
|
|
|
ectx->discontinuity_pts_offset = ectx->next_in_pts - nextpts;
|
|
|
|
}
|
|
|
|
else if (fabs(nextpts + ectx->discontinuity_pts_offset - ectx->next_in_pts) > 30) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "detected an unexpected discontinuity (pts jumped by "
|
2012-09-25 09:53:29 +00:00
|
|
|
"%f seconds)\n",
|
|
|
|
nextpts + ectx->discontinuity_pts_offset - ectx->next_in_pts);
|
|
|
|
ectx->discontinuity_pts_offset = ectx->next_in_pts - nextpts;
|
|
|
|
}
|
|
|
|
|
|
|
|
outpts = pts + ectx->discontinuity_pts_offset;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
2013-11-11 12:03:22 +00:00
|
|
|
else {
|
2012-09-25 09:53:29 +00:00
|
|
|
outpts = pts;
|
2013-11-11 12:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shift pts by the pts offset first.
|
2016-04-11 18:24:48 +00:00
|
|
|
outpts += encode_lavc_getoffset(ectx, ac->codec);
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2013-11-13 18:19:57 +00:00
|
|
|
while (samples - bufpos >= ac->aframesize) {
|
2014-11-21 09:03:23 +00:00
|
|
|
void *start[MP_NUM_CHANNELS] = {0};
|
2013-11-16 13:10:00 +00:00
|
|
|
for (int n = 0; n < num_planes; n++)
|
2013-11-13 18:19:57 +00:00
|
|
|
start[n] = (char *)data[n] + bufpos * ao->sstride;
|
|
|
|
encode(ao, outpts + bufpos / (double) ao->samplerate, start);
|
2012-09-14 15:51:26 +00:00
|
|
|
bufpos += ac->aframesize;
|
|
|
|
}
|
|
|
|
|
2013-11-11 12:03:22 +00:00
|
|
|
// Calculate expected pts of next audio frame (input side).
|
|
|
|
ac->expected_next_pts = pts + bufpos / (double) ao->samplerate;
|
2012-09-25 09:53:29 +00:00
|
|
|
|
2013-11-11 12:03:22 +00:00
|
|
|
// Set next allowed input pts value (input side).
|
2012-09-25 09:53:29 +00:00
|
|
|
if (!ectx->options->rawts && ectx->options->copyts) {
|
|
|
|
nextpts = ac->expected_next_pts + ectx->discontinuity_pts_offset;
|
|
|
|
if (nextpts > ectx->next_in_pts)
|
|
|
|
ectx->next_in_pts = nextpts;
|
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2014-06-12 09:42:00 +00:00
|
|
|
talloc_free(tempdata);
|
2014-07-16 13:08:06 +00:00
|
|
|
|
|
|
|
int taken = FFMIN(bufpos, orig_samples);
|
|
|
|
ectx->samples_since_last_pts += taken;
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&ectx->lock);
|
2014-06-12 09:42:00 +00:00
|
|
|
|
|
|
|
if (flags & AOPLAY_FINAL_CHUNK) {
|
|
|
|
if (bufpos < orig_samples) {
|
|
|
|
MP_ERR(ao, "did not write enough data at the end\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (bufpos > orig_samples) {
|
|
|
|
MP_ERR(ao, "audio buffer overflow (should never happen)\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 13:08:06 +00:00
|
|
|
return taken;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void drain(struct ao *ao)
|
|
|
|
{
|
|
|
|
// pretend we support it, so generic code doesn't force a wait
|
|
|
|
}
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
const struct ao_driver audio_out_lavc = {
|
2013-02-06 21:54:03 +00:00
|
|
|
.encode = true,
|
2013-10-23 17:07:27 +00:00
|
|
|
.description = "audio encoding using libavcodec",
|
|
|
|
.name = "lavc",
|
2012-09-14 15:51:26 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.get_space = get_space,
|
|
|
|
.play = play,
|
2014-03-08 23:49:39 +00:00
|
|
|
.drain = drain,
|
2012-09-14 15:51:26 +00:00
|
|
|
};
|
2016-06-24 18:20:32 +00:00
|
|
|
|
|
|
|
// vim: sw=4 ts=4 et tw=80
|