2012-09-14 15:51:26 +00:00
|
|
|
/*
|
|
|
|
* audio encoding using libavformat
|
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
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <libavutil/common.h>
|
|
|
|
#include <libavutil/audioconvert.h>
|
|
|
|
|
2012-11-14 01:31:57 +00:00
|
|
|
#include "compat/libav.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
#include "config.h"
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/options.h"
|
|
|
|
#include "mpvcore/mp_common.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
|
|
|
#include "audio/reorder_ch.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
#include "talloc.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "ao.h"
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/mp_msg.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/encode_lavc.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
static const char *sample_padding_signed = "\x00\x00\x00\x00";
|
|
|
|
static const char *sample_padding_u8 = "\x80";
|
|
|
|
static const char *sample_padding_float = "\x00\x00\x00\x00";
|
|
|
|
|
|
|
|
struct priv {
|
|
|
|
uint8_t *buffer;
|
|
|
|
size_t buffer_size;
|
|
|
|
AVStream *stream;
|
2012-12-03 19:16:17 +00:00
|
|
|
bool planarize;
|
2012-09-14 15:51:26 +00:00
|
|
|
int pcmhack;
|
|
|
|
int aframesize;
|
|
|
|
int aframecount;
|
|
|
|
int offset;
|
|
|
|
int offset_left;
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
const enum AVSampleFormat *sampleformat;
|
|
|
|
AVCodec *codec;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ac->stream = encode_lavc_alloc_stream(ao->encode_lavc_ctx,
|
|
|
|
AVMEDIA_TYPE_AUDIO);
|
|
|
|
|
|
|
|
if (!ac->stream) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "could not get a new audio stream\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
codec = encode_lavc_get_codec(ao->encode_lavc_ctx, ac->stream);
|
|
|
|
|
|
|
|
// ac->stream->time_base.num = 1;
|
|
|
|
// ac->stream->time_base.den = ao->samplerate;
|
|
|
|
// doing this breaks mpeg2ts in ffmpeg
|
|
|
|
// which doesn't properly force the time base to be 90000
|
|
|
|
// furthermore, ffmpeg.c doesn't do this either and works
|
|
|
|
|
|
|
|
ac->stream->codec->time_base.num = 1;
|
|
|
|
ac->stream->codec->time_base.den = ao->samplerate;
|
|
|
|
|
|
|
|
ac->stream->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);
|
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
|
|
|
|
return -1;
|
2013-04-05 21:06:22 +00:00
|
|
|
mp_chmap_reorder_to_lavc(&ao->channels);
|
|
|
|
ac->stream->codec->channels = ao->channels.num;
|
|
|
|
ac->stream->codec->channel_layout = mp_chmap_to_lavc(&ao->channels);
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
ac->stream->codec->sample_fmt = AV_SAMPLE_FMT_NONE;
|
|
|
|
|
|
|
|
{
|
|
|
|
// first check if the selected format is somewhere in the list of
|
|
|
|
// supported formats by the codec
|
|
|
|
for (sampleformat = codec->sample_fmts;
|
|
|
|
sampleformat && *sampleformat != AV_SAMPLE_FMT_NONE;
|
|
|
|
++sampleformat) {
|
|
|
|
switch (*sampleformat) {
|
|
|
|
case AV_SAMPLE_FMT_U8:
|
2012-12-03 19:16:17 +00:00
|
|
|
case AV_SAMPLE_FMT_U8P:
|
2012-09-14 15:51:26 +00:00
|
|
|
if (ao->format == AF_FORMAT_U8)
|
|
|
|
goto out_search;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S16:
|
2012-12-03 19:16:17 +00:00
|
|
|
case AV_SAMPLE_FMT_S16P:
|
2012-09-14 15:51:26 +00:00
|
|
|
if (ao->format == AF_FORMAT_S16_BE)
|
|
|
|
goto out_search;
|
|
|
|
if (ao->format == AF_FORMAT_S16_LE)
|
|
|
|
goto out_search;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S32:
|
2012-12-03 19:16:17 +00:00
|
|
|
case AV_SAMPLE_FMT_S32P:
|
2012-09-14 15:51:26 +00:00
|
|
|
if (ao->format == AF_FORMAT_S32_BE)
|
|
|
|
goto out_search;
|
|
|
|
if (ao->format == AF_FORMAT_S32_LE)
|
|
|
|
goto out_search;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLT:
|
2012-12-03 19:16:17 +00:00
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
2012-09-14 15:51:26 +00:00
|
|
|
if (ao->format == AF_FORMAT_FLOAT_BE)
|
|
|
|
goto out_search;
|
|
|
|
if (ao->format == AF_FORMAT_FLOAT_LE)
|
|
|
|
goto out_search;
|
|
|
|
break;
|
2012-12-03 19:16:17 +00:00
|
|
|
// FIXME do we need support for AV_SAMPLE_FORMAT_DBL/DBLP?
|
2012-09-14 15:51:26 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_search:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sampleformat || *sampleformat == AV_SAMPLE_FMT_NONE) {
|
|
|
|
// if the selected format is not supported, we have to pick the first
|
|
|
|
// one we CAN support
|
|
|
|
// note: not needing to select endianness here, as the switch() below
|
|
|
|
// does that anyway for us
|
|
|
|
for (sampleformat = codec->sample_fmts;
|
|
|
|
sampleformat && *sampleformat != AV_SAMPLE_FMT_NONE;
|
|
|
|
++sampleformat) {
|
|
|
|
switch (*sampleformat) {
|
|
|
|
case AV_SAMPLE_FMT_U8:
|
2012-12-03 19:16:17 +00:00
|
|
|
case AV_SAMPLE_FMT_U8P:
|
2012-09-14 15:51:26 +00:00
|
|
|
ao->format = AF_FORMAT_U8;
|
|
|
|
goto out_takefirst;
|
|
|
|
case AV_SAMPLE_FMT_S16:
|
2012-12-03 19:16:17 +00:00
|
|
|
case AV_SAMPLE_FMT_S16P:
|
2012-09-14 15:51:26 +00:00
|
|
|
ao->format = AF_FORMAT_S16_NE;
|
|
|
|
goto out_takefirst;
|
|
|
|
case AV_SAMPLE_FMT_S32:
|
2012-12-03 19:16:17 +00:00
|
|
|
case AV_SAMPLE_FMT_S32P:
|
2012-09-14 15:51:26 +00:00
|
|
|
ao->format = AF_FORMAT_S32_NE;
|
|
|
|
goto out_takefirst;
|
|
|
|
case AV_SAMPLE_FMT_FLT:
|
2012-12-03 19:16:17 +00:00
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
2012-09-14 15:51:26 +00:00
|
|
|
ao->format = AF_FORMAT_FLOAT_NE;
|
|
|
|
goto out_takefirst;
|
2012-12-03 19:16:17 +00:00
|
|
|
// FIXME do we need support for AV_SAMPLE_FORMAT_DBL/DBLP?
|
2012-09-14 15:51:26 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_takefirst:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ao->format) {
|
|
|
|
// now that we have chosen a format, set up the fields for it, boldly
|
|
|
|
// switching endianness if needed (mplayer code will convert for us
|
|
|
|
// anyway, but ffmpeg always expects native endianness)
|
|
|
|
case AF_FORMAT_U8:
|
|
|
|
ac->stream->codec->sample_fmt = AV_SAMPLE_FMT_U8;
|
|
|
|
ac->sample_size = 1;
|
|
|
|
ac->sample_padding = sample_padding_u8;
|
|
|
|
ao->format = AF_FORMAT_U8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case AF_FORMAT_S16_BE:
|
|
|
|
case AF_FORMAT_S16_LE:
|
|
|
|
ac->stream->codec->sample_fmt = AV_SAMPLE_FMT_S16;
|
|
|
|
ac->sample_size = 2;
|
|
|
|
ac->sample_padding = sample_padding_signed;
|
|
|
|
ao->format = AF_FORMAT_S16_NE;
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_S32_BE:
|
|
|
|
case AF_FORMAT_S32_LE:
|
|
|
|
ac->stream->codec->sample_fmt = AV_SAMPLE_FMT_S32;
|
|
|
|
ac->sample_size = 4;
|
|
|
|
ac->sample_padding = sample_padding_signed;
|
|
|
|
ao->format = AF_FORMAT_S32_NE;
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_FLOAT_BE:
|
|
|
|
case AF_FORMAT_FLOAT_LE:
|
|
|
|
ac->stream->codec->sample_fmt = AV_SAMPLE_FMT_FLT;
|
|
|
|
ac->sample_size = 4;
|
|
|
|
ac->sample_padding = sample_padding_float;
|
|
|
|
ao->format = AF_FORMAT_FLOAT_NE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-03 19:16:17 +00:00
|
|
|
// detect if we have to planarize
|
|
|
|
ac->planarize = false;
|
|
|
|
{
|
|
|
|
bool found_format = false;
|
2012-12-13 11:58:16 +00:00
|
|
|
bool found_planar_format = false;
|
2012-12-03 19:16:17 +00:00
|
|
|
for (sampleformat = codec->sample_fmts;
|
|
|
|
sampleformat && *sampleformat != AV_SAMPLE_FMT_NONE;
|
|
|
|
++sampleformat) {
|
|
|
|
if (*sampleformat == ac->stream->codec->sample_fmt)
|
|
|
|
found_format = true;
|
|
|
|
if (*sampleformat ==
|
2012-12-13 11:58:16 +00:00
|
|
|
av_get_planar_sample_fmt(ac->stream->codec->sample_fmt))
|
|
|
|
found_planar_format = true;
|
2012-12-03 19:16:17 +00:00
|
|
|
}
|
2012-12-13 11:58:16 +00:00
|
|
|
if (!found_format && found_planar_format) {
|
2012-12-03 19:16:17 +00:00
|
|
|
ac->stream->codec->sample_fmt =
|
2012-12-13 11:58:16 +00:00
|
|
|
av_get_planar_sample_fmt(ac->stream->codec->sample_fmt);
|
2012-12-03 19:16:17 +00:00
|
|
|
ac->planarize = true;
|
|
|
|
}
|
2012-12-13 11:58:16 +00:00
|
|
|
if (!found_format && !found_planar_format) {
|
2012-12-03 19:16:17 +00:00
|
|
|
// shouldn't happen
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "sample format not found\n");
|
2012-12-03 19:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
ac->stream->codec->bits_per_raw_sample = ac->sample_size * 8;
|
|
|
|
|
|
|
|
if (encode_lavc_open_codec(ao->encode_lavc_ctx, ac->stream) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ac->pcmhack = 0;
|
|
|
|
if (ac->stream->codec->frame_size <= 1)
|
|
|
|
ac->pcmhack = av_get_bits_per_sample(ac->stream->codec->codec_id) / 8;
|
|
|
|
|
|
|
|
if (ac->pcmhack) {
|
|
|
|
ac->aframesize = 16384; // "enough"
|
2013-04-05 21:06:22 +00:00
|
|
|
ac->buffer_size =
|
|
|
|
ac->aframesize * ac->pcmhack * ao->channels.num * 2 + 200;
|
2012-09-14 15:51:26 +00:00
|
|
|
} else {
|
|
|
|
ac->aframesize = ac->stream->codec->frame_size;
|
2013-04-05 21:06:22 +00:00
|
|
|
ac->buffer_size =
|
|
|
|
ac->aframesize * ac->sample_size * ao->channels.num * 2 + 200;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
if (ac->buffer_size < FF_MIN_BUFFER_SIZE)
|
|
|
|
ac->buffer_size = FF_MIN_BUFFER_SIZE;
|
|
|
|
ac->buffer = talloc_size(ac, ac->buffer_size);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
ac->savepts = MP_NOPTS_VALUE;
|
|
|
|
ac->lastpts = MP_NOPTS_VALUE;
|
|
|
|
ac->offset = ac->stream->codec->sample_rate *
|
|
|
|
encode_lavc_getoffset(ao->encode_lavc_ctx, ac->stream);
|
|
|
|
ac->offset_left = ac->offset;
|
|
|
|
|
|
|
|
ao->untimed = true;
|
|
|
|
ao->priv = ac;
|
|
|
|
|
2012-12-03 19:16:17 +00:00
|
|
|
if (ac->planarize)
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "need to planarize audio data\n");
|
2012-12-03 19:16:17 +00:00
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fill_with_padding(void *buf, int cnt, int sz, const void *padding)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (sz == 1) {
|
|
|
|
memset(buf, cnt, *(char *)padding);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i = 0; i < cnt; ++i)
|
|
|
|
memcpy((char *) buf + i * sz, padding, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2012-09-25 09:53:29 +00:00
|
|
|
static int encode(struct ao *ao, double apts, void *data);
|
2013-04-28 09:39:30 +00:00
|
|
|
static int play(struct ao *ao, void *data, int len, int flags);
|
2012-09-14 15:51:26 +00:00
|
|
|
static void uninit(struct ao *ao, bool cut_audio)
|
|
|
|
{
|
|
|
|
struct priv *ac = ao->priv;
|
2012-11-01 11:25:50 +00:00
|
|
|
struct encode_lavc_context *ectx = ao->encode_lavc_ctx;
|
|
|
|
|
|
|
|
if (!encode_lavc_start(ectx)) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "not even ready to encode audio at end -> dropped");
|
2012-11-01 11:25:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
if (ac->buffer) {
|
|
|
|
if (ao->buffer.len > 0) {
|
2013-04-28 09:39:30 +00:00
|
|
|
// TRICK: append aframesize-1 samples to the end, then play() will
|
|
|
|
// encode all it can
|
|
|
|
size_t extralen =
|
2013-05-12 19:47:55 +00:00
|
|
|
(ac->aframesize - 1) * ao->channels.num * ac->sample_size;
|
2013-04-28 09:39:30 +00:00
|
|
|
void *paddingbuf = talloc_size(ao, ao->buffer.len + extralen);
|
2012-09-14 15:51:26 +00:00
|
|
|
memcpy(paddingbuf, ao->buffer.start, ao->buffer.len);
|
|
|
|
fill_with_padding((char *) paddingbuf + ao->buffer.len,
|
2013-04-28 09:39:30 +00:00
|
|
|
extralen / ac->sample_size,
|
2012-09-14 15:51:26 +00:00
|
|
|
ac->sample_size, ac->sample_padding);
|
2013-04-28 09:39:30 +00:00
|
|
|
int written = play(ao, paddingbuf, ao->buffer.len + extralen, 0);
|
|
|
|
if (written < ao->buffer.len) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "did not write enough data at the end\n");
|
2013-04-28 09:39:30 +00:00
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
talloc_free(paddingbuf);
|
|
|
|
ao->buffer.len = 0;
|
|
|
|
}
|
2013-04-28 09:39:30 +00:00
|
|
|
|
|
|
|
double outpts = ac->expected_next_pts;
|
|
|
|
if (!ectx->options->rawts && ectx->options->copyts)
|
|
|
|
outpts += ectx->discontinuity_pts_offset;
|
|
|
|
outpts += encode_lavc_getoffset(ectx, ac->stream);
|
|
|
|
|
|
|
|
while (encode(ao, outpts, NULL) > 0) ;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ao->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return: how many bytes can be played without blocking
|
|
|
|
static int get_space(struct ao *ao)
|
|
|
|
{
|
2013-06-16 17:15:32 +00:00
|
|
|
struct priv *ac = ao->priv;
|
|
|
|
|
|
|
|
return ac->aframesize * ac->sample_size * ao->channels.num * ac->framecount;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// must get exactly ac->aframesize amount of data
|
2012-09-25 09:53:29 +00:00
|
|
|
static int encode(struct ao *ao, double apts, void *data)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
AVFrame *frame;
|
|
|
|
AVPacket packet;
|
|
|
|
struct priv *ac = ao->priv;
|
|
|
|
struct encode_lavc_context *ectx = ao->encode_lavc_ctx;
|
|
|
|
double realapts = ac->aframecount * (double) ac->aframesize /
|
|
|
|
ao->samplerate;
|
|
|
|
int status, gotpacket;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
av_init_packet(&packet);
|
|
|
|
packet.data = ac->buffer;
|
|
|
|
packet.size = ac->buffer_size;
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
frame = avcodec_alloc_frame();
|
|
|
|
frame->nb_samples = ac->aframesize;
|
2012-12-03 19:16:17 +00:00
|
|
|
|
|
|
|
if (ac->planarize) {
|
2013-04-05 21:06:22 +00:00
|
|
|
void *data2 = talloc_size(ao, ac->aframesize * ao->channels.num *
|
|
|
|
ac->sample_size);
|
|
|
|
reorder_to_planar(data2, data, ac->sample_size, ao->channels.num,
|
|
|
|
ac->aframesize);
|
2012-12-03 19:16:17 +00:00
|
|
|
data = data2;
|
|
|
|
}
|
|
|
|
|
2013-04-05 21:06:22 +00:00
|
|
|
size_t audiolen = ac->aframesize * ao->channels.num * ac->sample_size;
|
|
|
|
if (avcodec_fill_audio_frame(frame, ao->channels.num,
|
|
|
|
ac->stream->codec->sample_fmt, data,
|
|
|
|
audiolen, 1))
|
|
|
|
{
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "error filling\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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
|
2012-09-25 09:53:29 +00:00
|
|
|
frame->pts = floor(apts * ac->stream->codec->time_base.den / ac->stream->codec->time_base.num + 0.5);
|
2012-09-14 15:51:26 +00:00
|
|
|
} else {
|
|
|
|
// audio playback time
|
|
|
|
frame->pts = floor(realapts * ac->stream->codec->time_base.den / ac->stream->codec->time_base.num + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t frame_pts = av_rescale_q(frame->pts, ac->stream->codec->time_base, ac->worst_time_base);
|
|
|
|
if (ac->lastpts != MP_NOPTS_VALUE && frame_pts <= ac->lastpts) {
|
|
|
|
// 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;
|
|
|
|
frame->pts = av_rescale_q(frame_pts, ac->worst_time_base, ac->stream->codec->time_base);
|
|
|
|
}
|
|
|
|
ac->lastpts = frame_pts;
|
|
|
|
|
|
|
|
frame->quality = ac->stream->codec->global_quality;
|
|
|
|
status = avcodec_encode_audio2(ac->stream->codec, &packet, frame, &gotpacket);
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
if (ac->savepts == MP_NOPTS_VALUE)
|
|
|
|
ac->savepts = frame->pts;
|
|
|
|
}
|
|
|
|
|
2012-11-03 17:06:23 +00:00
|
|
|
avcodec_free_frame(&frame);
|
2012-12-03 19:16:17 +00:00
|
|
|
|
|
|
|
if (ac->planarize) {
|
|
|
|
talloc_free(data);
|
|
|
|
data = NULL;
|
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = avcodec_encode_audio2(ac->stream->codec, &packet, NULL, &gotpacket);
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:12:35 +00:00
|
|
|
if(status) {
|
|
|
|
MP_ERR(ao, "error encoding\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!gotpacket)
|
|
|
|
return 0;
|
|
|
|
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_DBG(ao, "got pts %f (playback time: %f); out size: %d\n",
|
2012-09-14 15:51:26 +00:00
|
|
|
apts, realapts, packet.size);
|
|
|
|
|
|
|
|
encode_lavc_write_stats(ao->encode_lavc_ctx, ac->stream);
|
|
|
|
|
2012-09-29 13:04:40 +00:00
|
|
|
packet.stream_index = ac->stream->index;
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
// Do we need this at all? Better be safe than sorry...
|
|
|
|
if (packet.pts == AV_NOPTS_VALUE) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "encoder lost pts, why?\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
if (ac->savepts != MP_NOPTS_VALUE)
|
|
|
|
packet.pts = ac->savepts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet.pts != AV_NOPTS_VALUE)
|
|
|
|
packet.pts = av_rescale_q(packet.pts, ac->stream->codec->time_base,
|
|
|
|
ac->stream->time_base);
|
|
|
|
|
|
|
|
if (packet.dts != AV_NOPTS_VALUE)
|
|
|
|
packet.dts = av_rescale_q(packet.dts, ac->stream->codec->time_base,
|
|
|
|
ac->stream->time_base);
|
|
|
|
|
|
|
|
if(packet.duration > 0)
|
|
|
|
packet.duration = av_rescale_q(packet.duration, ac->stream->codec->time_base,
|
|
|
|
ac->stream->time_base);
|
|
|
|
|
|
|
|
ac->savepts = MP_NOPTS_VALUE;
|
|
|
|
|
|
|
|
if (encode_lavc_write_frame(ao->encode_lavc_ctx, &packet) < 0) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "error writing at %f %f/%f\n",
|
2012-09-14 15:51:26 +00:00
|
|
|
realapts, (double) ac->stream->time_base.num,
|
|
|
|
(double) ac->stream->time_base.den);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return packet.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// plays 'len' bytes of 'data'
|
2013-06-16 17:15:32 +00:00
|
|
|
// it should round it down to frame sizes
|
2012-09-14 15:51:26 +00:00
|
|
|
// return: number of bytes played
|
|
|
|
static int play(struct ao *ao, void *data, int len, int flags)
|
|
|
|
{
|
|
|
|
struct priv *ac = ao->priv;
|
|
|
|
struct encode_lavc_context *ectx = ao->encode_lavc_ctx;
|
|
|
|
int bufpos = 0;
|
|
|
|
int64_t ptsoffset;
|
|
|
|
void *paddingbuf = NULL;
|
|
|
|
double nextpts;
|
2012-09-25 09:53:29 +00:00
|
|
|
double pts = ao->pts;
|
|
|
|
double outpts;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2013-04-05 21:06:22 +00:00
|
|
|
len /= ac->sample_size * ao->channels.num;
|
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");
|
2012-09-14 15:51:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
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) {
|
|
|
|
//if (ac->stream->codec->time_base.num / ac->stream->codec->time_base.den >= ac->stream->time_base.num / ac->stream->time_base.den)
|
|
|
|
if (ac->stream->codec->time_base.num * (double) ac->stream->time_base.den >=
|
|
|
|
ac->stream->time_base.num * (double) ac->stream->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",
|
|
|
|
(int)ac->stream->codec->time_base.num,
|
|
|
|
(int)ac->stream->codec->time_base.den,
|
|
|
|
(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->codec->time_base;
|
|
|
|
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",
|
|
|
|
(int)ac->stream->codec->time_base.num,
|
|
|
|
(int)ac->stream->codec->time_base.den,
|
|
|
|
(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.
|
|
|
|
}
|
|
|
|
|
|
|
|
ptsoffset = ac->offset;
|
|
|
|
// this basically just edits ao->apts for syncing purposes
|
|
|
|
|
|
|
|
if (ectx->options->copyts || ectx->options->rawts) {
|
|
|
|
// we do not send time sync data to the video side,
|
|
|
|
// but we always need the exact pts, even if zero
|
|
|
|
} else {
|
|
|
|
// here we must "simulate" the pts editing
|
|
|
|
// 1. if we have to skip stuff, we skip it
|
|
|
|
// 2. if we have to add samples, we add them
|
|
|
|
// 3. we must still adjust ptsoffset appropriately for AV sync!
|
|
|
|
// invariant:
|
|
|
|
// if no partial skipping is done, the first frame gets ao->apts passed as pts!
|
|
|
|
|
|
|
|
if (ac->offset_left < 0) {
|
|
|
|
if (ac->offset_left <= -len) {
|
|
|
|
// skip whole frame
|
|
|
|
ac->offset_left += len;
|
2013-04-05 21:06:22 +00:00
|
|
|
return len * ac->sample_size * ao->channels.num;
|
2012-09-14 15:51:26 +00:00
|
|
|
} else {
|
|
|
|
// skip part of this frame, buffer/encode the rest
|
|
|
|
bufpos -= ac->offset_left;
|
|
|
|
ptsoffset += ac->offset_left;
|
|
|
|
ac->offset_left = 0;
|
|
|
|
}
|
|
|
|
} else if (ac->offset_left > 0) {
|
|
|
|
// make a temporary buffer, filled with zeroes at the start
|
|
|
|
// (don't worry, only happens once)
|
|
|
|
|
2013-04-05 21:06:22 +00:00
|
|
|
paddingbuf = talloc_size(ac, ac->sample_size * ao->channels.num *
|
2012-09-14 15:51:26 +00:00
|
|
|
(ac->offset_left + len));
|
|
|
|
fill_with_padding(paddingbuf, ac->offset_left, ac->sample_size,
|
|
|
|
ac->sample_padding);
|
2013-04-05 21:06:22 +00:00
|
|
|
data = (char *) paddingbuf + ac->sample_size * ao->channels.num *
|
2012-09-14 15:51:26 +00:00
|
|
|
ac->offset_left;
|
|
|
|
bufpos -= ac->offset_left; // yes, negative!
|
|
|
|
ptsoffset += ac->offset_left;
|
|
|
|
ac->offset_left = 0;
|
|
|
|
|
|
|
|
// now adjust the bufpos so the final value of bufpos is positive!
|
|
|
|
/*
|
|
|
|
int cnt = (len - bufpos) / ac->aframesize;
|
|
|
|
int finalbufpos = bufpos + cnt * ac->aframesize;
|
|
|
|
*/
|
|
|
|
int finalbufpos = len - (len - bufpos) % ac->aframesize;
|
|
|
|
if (finalbufpos < 0) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "cannot attain the "
|
2012-09-14 15:51:26 +00:00
|
|
|
"exact requested audio sync; shifting by %d frames\n",
|
|
|
|
-finalbufpos);
|
|
|
|
bufpos -= finalbufpos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-25 09:53:29 +00:00
|
|
|
if (!ectx->options->rawts && ectx->options->copyts) {
|
|
|
|
// fix the discontinuity pts offset
|
|
|
|
nextpts = pts + ptsoffset / (double) ao->samplerate;
|
|
|
|
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
|
|
|
}
|
2012-09-25 09:53:29 +00:00
|
|
|
else
|
|
|
|
outpts = pts;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
while (len - bufpos >= ac->aframesize) {
|
2012-09-25 09:53:29 +00:00
|
|
|
encode(ao,
|
|
|
|
outpts + (bufpos + ptsoffset) / (double) ao->samplerate + encode_lavc_getoffset(ectx, ac->stream),
|
2013-04-05 21:06:22 +00:00
|
|
|
(char *) data + ac->sample_size * bufpos * ao->channels.num);
|
2012-09-14 15:51:26 +00:00
|
|
|
bufpos += ac->aframesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(paddingbuf);
|
|
|
|
|
2012-09-25 09:53:29 +00:00
|
|
|
// calculate expected pts of next audio frame
|
|
|
|
ac->expected_next_pts = pts + (bufpos + ptsoffset) / (double) ao->samplerate;
|
|
|
|
|
|
|
|
if (!ectx->options->rawts && ectx->options->copyts) {
|
|
|
|
// set next allowed output pts value
|
|
|
|
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
|
|
|
|
2013-04-05 21:06:22 +00:00
|
|
|
return bufpos * ac->sample_size * ao->channels.num;
|
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,
|
2012-09-14 15:51:26 +00:00
|
|
|
.info = &(const struct ao_info) {
|
|
|
|
"audio encoding using libavcodec",
|
|
|
|
"lavc",
|
|
|
|
"Rudolf Polzer <divVerent@xonotic.org>",
|
|
|
|
""
|
|
|
|
},
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.get_space = get_space,
|
|
|
|
.play = play,
|
|
|
|
};
|