2012-09-14 15:51:26 +00:00
|
|
|
/*
|
|
|
|
* video encoding using libavformat
|
|
|
|
* Copyright (C) 2010 Nicolas George <george@nsup.org>
|
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
|
|
|
*
|
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>
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/fmt-conversion.h"
|
|
|
|
#include "video/mp_image.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
#include "talloc.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "vo.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
|
|
|
|
2013-11-24 11:58:06 +00:00
|
|
|
#include "sub/osd.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
struct priv {
|
|
|
|
uint8_t *buffer;
|
|
|
|
size_t buffer_size;
|
|
|
|
AVStream *stream;
|
|
|
|
int have_first_packet;
|
|
|
|
|
|
|
|
int harddup;
|
|
|
|
|
|
|
|
double lastpts;
|
|
|
|
int64_t lastipts;
|
|
|
|
int64_t lastframeipts;
|
2013-06-09 14:31:09 +00:00
|
|
|
int64_t lastencodedipts;
|
2013-06-09 13:37:28 +00:00
|
|
|
int64_t mindeltapts;
|
2012-09-25 09:53:29 +00:00
|
|
|
double expected_next_pts;
|
2012-09-14 15:51:26 +00:00
|
|
|
mp_image_t *lastimg;
|
2012-10-24 17:25:24 +00:00
|
|
|
int lastimg_wants_osd;
|
2012-09-14 15:51:26 +00:00
|
|
|
int lastdisplaycount;
|
|
|
|
|
|
|
|
AVRational worst_time_base;
|
|
|
|
int worst_time_base_is_stream;
|
|
|
|
|
2014-03-28 23:25:08 +00:00
|
|
|
struct mp_image_params real_colorspace;
|
2014-11-12 11:16:07 +00:00
|
|
|
|
|
|
|
bool shutdown;
|
2012-09-14 15:51:26 +00:00
|
|
|
};
|
|
|
|
|
2013-07-22 20:52:42 +00:00
|
|
|
static int preinit(struct vo *vo)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
struct priv *vc;
|
|
|
|
if (!encode_lavc_available(vo->encode_lavc_ctx)) {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_ERR(vo, "the option --o (output file) must be specified\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
vo->priv = talloc_zero(vo, struct priv);
|
|
|
|
vc = vo->priv;
|
|
|
|
vc->harddup = vo->encode_lavc_ctx->options->harddup;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi);
|
2012-09-14 15:51:26 +00:00
|
|
|
static void uninit(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
2014-11-12 11:16:07 +00:00
|
|
|
if (!vc || vc->shutdown)
|
2012-09-14 15:51:26 +00:00
|
|
|
return;
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_lock(&vo->encode_lavc_ctx->lock);
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
if (vc->lastipts >= 0 && vc->stream)
|
2014-03-08 22:38:53 +00:00
|
|
|
draw_image_unlocked(vo, NULL);
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2012-12-22 20:46:28 +00:00
|
|
|
mp_image_unrefp(&vc->lastimg);
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&vo->encode_lavc_ctx->lock);
|
2014-11-12 11:16:07 +00:00
|
|
|
|
|
|
|
vc->shutdown = true;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 20:22:25 +00:00
|
|
|
static int reconfig(struct vo *vo, struct mp_image_params *params, int flags)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
2014-01-24 20:22:25 +00:00
|
|
|
enum AVPixelFormat pix_fmt = imgfmt2pixfmt(params->imgfmt);
|
2012-09-14 15:51:26 +00:00
|
|
|
AVRational display_aspect_ratio, image_aspect_ratio;
|
|
|
|
AVRational aspect;
|
2014-01-24 20:22:25 +00:00
|
|
|
uint32_t width = params->w;
|
|
|
|
uint32_t height = params->h;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2014-11-12 11:16:07 +00:00
|
|
|
if (!vc || vc->shutdown)
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_lock(&vo->encode_lavc_ctx->lock);
|
|
|
|
|
2014-01-24 20:22:25 +00:00
|
|
|
display_aspect_ratio.num = params->d_w;
|
|
|
|
display_aspect_ratio.den = params->d_h;
|
2012-09-14 15:51:26 +00:00
|
|
|
image_aspect_ratio.num = width;
|
|
|
|
image_aspect_ratio.den = height;
|
|
|
|
aspect = av_div_q(display_aspect_ratio, image_aspect_ratio);
|
|
|
|
|
|
|
|
if (vc->stream) {
|
|
|
|
/* NOTE:
|
|
|
|
* in debug builds we get a "comparison between signed and unsigned"
|
|
|
|
* warning here. We choose to ignore that; just because ffmpeg currently
|
|
|
|
* uses a plain 'int' for these struct fields, it doesn't mean it always
|
|
|
|
* will */
|
|
|
|
if (width == vc->stream->codec->width &&
|
|
|
|
height == vc->stream->codec->height) {
|
|
|
|
if (aspect.num != vc->stream->codec->sample_aspect_ratio.num ||
|
|
|
|
aspect.den != vc->stream->codec->sample_aspect_ratio.den) {
|
|
|
|
/* aspect-only changes are not critical */
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_WARN(vo, "unsupported pixel aspect ratio change from %d:%d to %d:%d\n",
|
2012-09-14 15:51:26 +00:00
|
|
|
vc->stream->codec->sample_aspect_ratio.num,
|
|
|
|
vc->stream->codec->sample_aspect_ratio.den,
|
|
|
|
aspect.num, aspect.den);
|
|
|
|
}
|
2014-03-08 22:38:53 +00:00
|
|
|
goto done;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME Is it possible with raw video? */
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_ERR(vo, "resolution changes not supported.\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:16:07 +00:00
|
|
|
// When we get here, this must be the first call to reconfigure(). Thus, we
|
|
|
|
// can rely on no existing data in vc having been allocated yet.
|
|
|
|
// Reason:
|
|
|
|
// - Second calls after reconfigure() already failed once fail (due to the
|
|
|
|
// vc->shutdown check above).
|
|
|
|
// - Second calls after reconfigure() already succeeded once return early
|
|
|
|
// (due to the vc->stream check above).
|
|
|
|
|
2014-05-10 08:32:23 +00:00
|
|
|
vc->lastipts = AV_NOPTS_VALUE;
|
|
|
|
vc->lastframeipts = AV_NOPTS_VALUE;
|
|
|
|
vc->lastencodedipts = AV_NOPTS_VALUE;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2013-12-21 17:04:16 +00:00
|
|
|
if (pix_fmt == AV_PIX_FMT_NONE) {
|
|
|
|
MP_FATAL(vo, "Format %s not supported by lavc.\n",
|
2014-01-24 20:22:25 +00:00
|
|
|
mp_imgfmt_to_name(params->imgfmt));
|
2013-12-21 17:04:16 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
vc->stream = encode_lavc_alloc_stream(vo->encode_lavc_ctx,
|
|
|
|
AVMEDIA_TYPE_VIDEO);
|
|
|
|
vc->stream->sample_aspect_ratio = vc->stream->codec->sample_aspect_ratio =
|
|
|
|
aspect;
|
|
|
|
vc->stream->codec->width = width;
|
|
|
|
vc->stream->codec->height = height;
|
|
|
|
vc->stream->codec->pix_fmt = pix_fmt;
|
|
|
|
|
2014-03-28 23:25:08 +00:00
|
|
|
encode_lavc_set_csp(vo->encode_lavc_ctx, vc->stream, params->colorspace);
|
|
|
|
encode_lavc_set_csp_levels(vo->encode_lavc_ctx, vc->stream, params->colorlevels);
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
if (encode_lavc_open_codec(vo->encode_lavc_ctx, vc->stream) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2014-03-28 23:25:08 +00:00
|
|
|
vc->real_colorspace.colorspace =
|
|
|
|
encode_lavc_get_csp(vo->encode_lavc_ctx, vc->stream);
|
|
|
|
vc->real_colorspace.colorlevels =
|
|
|
|
encode_lavc_get_csp_levels(vo->encode_lavc_ctx, vc->stream);
|
2012-10-24 17:25:24 +00:00
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
vc->buffer_size = 6 * width * height + 200;
|
|
|
|
if (vc->buffer_size < FF_MIN_BUFFER_SIZE)
|
|
|
|
vc->buffer_size = FF_MIN_BUFFER_SIZE;
|
|
|
|
if (vc->buffer_size < sizeof(AVPicture))
|
|
|
|
vc->buffer_size = sizeof(AVPicture);
|
|
|
|
|
|
|
|
vc->buffer = talloc_size(vc, vc->buffer_size);
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
done:
|
|
|
|
pthread_mutex_unlock(&vo->encode_lavc_ctx->lock);
|
2012-09-14 15:51:26 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&vo->encode_lavc_ctx->lock);
|
2014-11-12 11:16:07 +00:00
|
|
|
vc->shutdown = true;
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-21 21:08:24 +00:00
|
|
|
static int query_format(struct vo *vo, int format)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
2013-11-29 16:39:57 +00:00
|
|
|
enum AVPixelFormat pix_fmt = imgfmt2pixfmt(format);
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
if (!vo->encode_lavc_ctx)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_lock(&vo->encode_lavc_ctx->lock);
|
|
|
|
int flags = 0;
|
|
|
|
if (encode_lavc_supports_pixfmt(vo->encode_lavc_ctx, pix_fmt))
|
2015-01-21 21:08:24 +00:00
|
|
|
flags = 1;
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&vo->encode_lavc_ctx->lock);
|
|
|
|
return flags;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_packet(struct vo *vo, int size, AVPacket *packet)
|
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
|
|
|
|
|
|
|
if (size < 0) {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_ERR(vo, "error encoding\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
packet->stream_index = vc->stream->index;
|
|
|
|
if (packet->pts != AV_NOPTS_VALUE) {
|
|
|
|
packet->pts = av_rescale_q(packet->pts,
|
|
|
|
vc->stream->codec->time_base,
|
|
|
|
vc->stream->time_base);
|
|
|
|
} else {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_VERBOSE(vo, "codec did not provide pts\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
packet->pts = av_rescale_q(vc->lastipts, vc->worst_time_base,
|
|
|
|
vc->stream->time_base);
|
|
|
|
}
|
|
|
|
if (packet->dts != AV_NOPTS_VALUE) {
|
|
|
|
packet->dts = av_rescale_q(packet->dts,
|
|
|
|
vc->stream->codec->time_base,
|
|
|
|
vc->stream->time_base);
|
|
|
|
}
|
|
|
|
if (packet->duration > 0) {
|
|
|
|
packet->duration = av_rescale_q(packet->duration,
|
|
|
|
vc->stream->codec->time_base,
|
|
|
|
vc->stream->time_base);
|
|
|
|
} else {
|
|
|
|
// HACK: libavformat calculates dts wrong if the initial packet
|
|
|
|
// duration is not set, but ONLY if the time base is "high" and if we
|
|
|
|
// have b-frames!
|
|
|
|
if (!packet->duration)
|
|
|
|
if (!vc->have_first_packet)
|
|
|
|
if (vc->stream->codec->has_b_frames
|
|
|
|
|| vc->stream->codec->max_b_frames)
|
|
|
|
if (vc->stream->time_base.num * 1000LL <=
|
|
|
|
vc->stream->time_base.den)
|
|
|
|
packet->duration = FFMAX(1, av_rescale_q(1,
|
|
|
|
vc->stream->codec->time_base, vc->stream->time_base));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encode_lavc_write_frame(vo->encode_lavc_ctx, packet) < 0) {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_ERR(vo, "error writing\n");
|
2012-09-14 15:51:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vc->have_first_packet = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int encode_video(struct vo *vo, AVFrame *frame, AVPacket *packet)
|
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
|
|
|
if (encode_lavc_oformat_flags(vo->encode_lavc_ctx) & AVFMT_RAWPICTURE) {
|
|
|
|
if (!frame)
|
|
|
|
return 0;
|
|
|
|
memcpy(vc->buffer, frame, sizeof(AVPicture));
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_DBG(vo, "got pts %f\n",
|
2012-09-14 15:51:26 +00:00
|
|
|
frame->pts * (double) vc->stream->codec->time_base.num /
|
|
|
|
(double) vc->stream->codec->time_base.den);
|
|
|
|
packet->size = sizeof(AVPicture);
|
|
|
|
return packet->size;
|
|
|
|
} else {
|
|
|
|
int got_packet = 0;
|
|
|
|
int status = avcodec_encode_video2(vc->stream->codec, packet,
|
|
|
|
frame, &got_packet);
|
|
|
|
int size = (status < 0) ? status : got_packet ? packet->size : 0;
|
|
|
|
|
|
|
|
if (frame)
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_DBG(vo, "got pts %f; out size: %d\n",
|
2012-09-14 15:51:26 +00:00
|
|
|
frame->pts * (double) vc->stream->codec->time_base.num /
|
|
|
|
(double) vc->stream->codec->time_base.den, size);
|
|
|
|
|
2014-04-10 22:08:32 +00:00
|
|
|
if (got_packet)
|
|
|
|
encode_lavc_write_stats(vo->encode_lavc_ctx, vc->stream);
|
2012-09-14 15:51:26 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
|
|
|
struct encode_lavc_context *ectx = vo->encode_lavc_ctx;
|
2013-06-18 10:22:25 +00:00
|
|
|
int size;
|
2012-09-14 15:51:26 +00:00
|
|
|
AVCodecContext *avc;
|
|
|
|
int64_t frameipts;
|
|
|
|
double nextpts;
|
|
|
|
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
double pts = mpi ? mpi->pts : MP_NOPTS_VALUE;
|
|
|
|
|
2014-11-12 11:16:07 +00:00
|
|
|
if (!vc || vc->shutdown)
|
2014-06-17 21:05:50 +00:00
|
|
|
goto done;
|
2012-09-14 15:51:26 +00:00
|
|
|
if (!encode_lavc_start(ectx)) {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_WARN(vo, "NOTE: skipped initial video frame (probably because audio is not there yet)\n");
|
2014-06-17 21:05:50 +00:00
|
|
|
goto done;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
2012-09-25 09:53:29 +00:00
|
|
|
if (pts == MP_NOPTS_VALUE) {
|
|
|
|
if (mpi)
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_WARN(vo, "frame without pts, please report; synthesizing pts instead\n");
|
2012-09-25 09:53:29 +00:00
|
|
|
pts = vc->expected_next_pts;
|
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
avc = vc->stream->codec;
|
|
|
|
|
|
|
|
if (vc->worst_time_base.den == 0) {
|
|
|
|
//if (avc->time_base.num / avc->time_base.den >= vc->stream->time_base.num / vc->stream->time_base.den)
|
|
|
|
if (avc->time_base.num * (double) vc->stream->time_base.den >=
|
|
|
|
vc->stream->time_base.num * (double) avc->time_base.den) {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_VERBOSE(vo, "NOTE: using codec time base "
|
|
|
|
"(%d/%d) for frame dropping; the stream base (%d/%d) is "
|
|
|
|
"not worse.\n", (int)avc->time_base.num,
|
|
|
|
(int)avc->time_base.den, (int)vc->stream->time_base.num,
|
|
|
|
(int)vc->stream->time_base.den);
|
2012-09-14 15:51:26 +00:00
|
|
|
vc->worst_time_base = avc->time_base;
|
|
|
|
vc->worst_time_base_is_stream = 0;
|
|
|
|
} else {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_WARN(vo, "NOTE: not using codec time base (%d/%d) for frame "
|
|
|
|
"dropping; the stream base (%d/%d) is worse.\n",
|
|
|
|
(int)avc->time_base.num, (int)avc->time_base.den,
|
|
|
|
(int)vc->stream->time_base.num, (int)vc->stream->time_base.den);
|
2012-09-14 15:51:26 +00:00
|
|
|
vc->worst_time_base = vc->stream->time_base;
|
|
|
|
vc->worst_time_base_is_stream = 1;
|
|
|
|
}
|
2013-06-09 13:37:28 +00:00
|
|
|
if (ectx->options->maxfps)
|
|
|
|
vc->mindeltapts = ceil(vc->worst_time_base.den /
|
|
|
|
(vc->worst_time_base.num * ectx->options->maxfps));
|
|
|
|
else
|
|
|
|
vc->mindeltapts = 0;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
|
|
|
|
double timeunit = (double)vc->worst_time_base.num / vc->worst_time_base.den;
|
|
|
|
|
2012-09-25 09:53:29 +00:00
|
|
|
double outpts;
|
|
|
|
if (ectx->options->rawts)
|
|
|
|
outpts = pts;
|
|
|
|
else if (ectx->options->copyts) {
|
|
|
|
// fix the discontinuity pts offset
|
2012-09-14 15:51:26 +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-09-20 14:32:24 +00:00
|
|
|
MP_WARN(vo, "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;
|
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2012-09-25 09:53:29 +00:00
|
|
|
outpts = pts + ectx->discontinuity_pts_offset;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// adjust pts by knowledge of audio pts vs audio playback time
|
|
|
|
double duration = 0;
|
|
|
|
if (ectx->last_video_in_pts != MP_NOPTS_VALUE)
|
|
|
|
duration = pts - ectx->last_video_in_pts;
|
|
|
|
if (duration < 0)
|
|
|
|
duration = timeunit; // XXX warn about discontinuity?
|
|
|
|
outpts = vc->lastpts + duration;
|
|
|
|
if (ectx->audio_pts_offset != MP_NOPTS_VALUE) {
|
|
|
|
double adj = outpts - pts - ectx->audio_pts_offset;
|
|
|
|
adj = FFMIN(adj, duration * 0.1);
|
|
|
|
adj = FFMAX(adj, -duration * 0.1);
|
|
|
|
outpts -= adj;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
2012-09-25 09:53:29 +00:00
|
|
|
}
|
|
|
|
vc->lastpts = outpts;
|
|
|
|
ectx->last_video_in_pts = pts;
|
|
|
|
frameipts = floor((outpts + encode_lavc_getoffset(ectx, vc->stream))
|
|
|
|
/ timeunit + 0.5);
|
|
|
|
|
|
|
|
// calculate expected pts of next video frame
|
|
|
|
vc->expected_next_pts = pts + timeunit;
|
|
|
|
|
|
|
|
if (!ectx->options->rawts && ectx->options->copyts) {
|
|
|
|
// set next allowed output pts value
|
|
|
|
nextpts = vc->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
|
|
|
}
|
|
|
|
|
|
|
|
// never-drop mode
|
2013-06-09 14:37:13 +00:00
|
|
|
if (ectx->options->neverdrop) {
|
2013-06-09 14:31:09 +00:00
|
|
|
int64_t step = vc->mindeltapts ? vc->mindeltapts : 1;
|
2013-06-09 14:37:13 +00:00
|
|
|
if (frameipts < vc->lastipts + step) {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_INFO(vo, "--oneverdrop increased pts by %d\n",
|
|
|
|
(int) (vc->lastipts - frameipts + step));
|
2013-06-09 14:37:13 +00:00
|
|
|
frameipts = vc->lastipts + step;
|
|
|
|
vc->lastpts = frameipts * timeunit - encode_lavc_getoffset(ectx, vc->stream);
|
|
|
|
}
|
2013-06-09 13:37:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-10 08:32:23 +00:00
|
|
|
if (vc->lastipts != AV_NOPTS_VALUE) {
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
// we have a valid image in lastimg
|
2014-11-11 16:59:03 +00:00
|
|
|
while (vc->lastimg && vc->lastipts < frameipts) {
|
2012-09-14 15:51:26 +00:00
|
|
|
int64_t thisduration = vc->harddup ? 1 : (frameipts - vc->lastipts);
|
|
|
|
AVPacket packet;
|
|
|
|
|
2013-06-09 14:31:09 +00:00
|
|
|
// we will ONLY encode this frame if it can be encoded at at least
|
|
|
|
// vc->mindeltapts after the last encoded frame!
|
|
|
|
int64_t skipframes =
|
2014-05-10 08:32:23 +00:00
|
|
|
(vc->lastencodedipts == AV_NOPTS_VALUE)
|
2013-11-11 12:02:34 +00:00
|
|
|
? 0
|
|
|
|
: vc->lastencodedipts + vc->mindeltapts - vc->lastipts;
|
2013-06-09 14:31:09 +00:00
|
|
|
if (skipframes < 0)
|
|
|
|
skipframes = 0;
|
|
|
|
|
|
|
|
if (thisduration > skipframes) {
|
2014-03-16 11:57:14 +00:00
|
|
|
AVFrame *frame = av_frame_alloc();
|
2013-06-09 14:31:09 +00:00
|
|
|
|
|
|
|
// this is a nop, unless the worst time base is the STREAM time base
|
|
|
|
frame->pts = av_rescale_q(vc->lastipts + skipframes,
|
|
|
|
vc->worst_time_base, avc->time_base);
|
|
|
|
|
2013-06-17 15:33:50 +00:00
|
|
|
enum AVPictureType savetype = frame->pict_type;
|
2013-06-17 06:56:55 +00:00
|
|
|
mp_image_copy_fields_to_av_frame(frame, vc->lastimg);
|
2013-06-17 15:33:50 +00:00
|
|
|
frame->pict_type = savetype;
|
|
|
|
// keep this at avcodec_get_frame_defaults default
|
|
|
|
|
2013-06-09 14:31:09 +00:00
|
|
|
frame->quality = avc->global_quality;
|
|
|
|
|
|
|
|
av_init_packet(&packet);
|
|
|
|
packet.data = vc->buffer;
|
|
|
|
packet.size = vc->buffer_size;
|
|
|
|
size = encode_video(vo, frame, &packet);
|
|
|
|
write_packet(vo, size, &packet);
|
|
|
|
++vc->lastdisplaycount;
|
|
|
|
vc->lastencodedipts = vc->lastipts + skipframes;
|
2014-03-16 11:57:14 +00:00
|
|
|
|
|
|
|
av_frame_free(&frame);
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vc->lastipts += thisduration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mpi) {
|
|
|
|
// finish encoding
|
|
|
|
do {
|
|
|
|
AVPacket packet;
|
|
|
|
av_init_packet(&packet);
|
|
|
|
packet.data = vc->buffer;
|
|
|
|
packet.size = vc->buffer_size;
|
|
|
|
size = encode_video(vo, NULL, &packet);
|
|
|
|
write_packet(vo, size, &packet);
|
|
|
|
} while (size > 0);
|
|
|
|
} else {
|
|
|
|
if (frameipts >= vc->lastframeipts) {
|
2014-05-10 08:32:23 +00:00
|
|
|
if (vc->lastframeipts != AV_NOPTS_VALUE && vc->lastdisplaycount != 1)
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_INFO(vo, "Frame at pts %d got displayed %d times\n",
|
|
|
|
(int) vc->lastframeipts, vc->lastdisplaycount);
|
2014-06-17 21:05:50 +00:00
|
|
|
talloc_free(vc->lastimg);
|
|
|
|
vc->lastimg = mpi;
|
|
|
|
mpi = NULL;
|
2012-10-24 17:25:24 +00:00
|
|
|
vc->lastimg_wants_osd = true;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
vc->lastframeipts = vc->lastipts = frameipts;
|
|
|
|
if (ectx->options->rawts && vc->lastipts < 0) {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_ERR(vo, "why does this happen? DEBUG THIS! vc->lastipts = %lld\n", (long long) vc->lastipts);
|
2012-09-14 15:51:26 +00:00
|
|
|
vc->lastipts = -1;
|
|
|
|
}
|
|
|
|
vc->lastdisplaycount = 0;
|
2012-10-24 17:25:24 +00:00
|
|
|
} else {
|
2013-09-20 14:32:24 +00:00
|
|
|
MP_INFO(vo, "Frame at pts %d got dropped "
|
|
|
|
"entirely because pts went backwards\n", (int) frameipts);
|
2012-10-24 17:25:24 +00:00
|
|
|
vc->lastimg_wants_osd = false;
|
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 22:43:54 +00:00
|
|
|
if (vc->lastimg && vc->lastimg_wants_osd && vo->params) {
|
|
|
|
struct mp_osd_res dim = osd_res_from_image_params(vo->params);
|
2012-10-18 15:31:00 +00:00
|
|
|
|
2014-06-17 21:05:50 +00:00
|
|
|
osd_draw_on_image(vo->osd, dim, vc->lastimg->pts, OSD_DRAW_SUB_ONLY,
|
2014-01-18 00:19:20 +00:00
|
|
|
vc->lastimg);
|
2012-10-18 15:31:00 +00:00
|
|
|
}
|
2014-06-17 21:05:50 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
talloc_free(mpi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_image(struct vo *vo, mp_image_t *mpi)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&vo->encode_lavc_ctx->lock);
|
|
|
|
draw_image_unlocked(vo, mpi);
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&vo->encode_lavc_ctx->lock);
|
2012-10-18 15:31:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-20 13:14:43 +00:00
|
|
|
static void flip_page(struct vo *vo)
|
2014-06-15 18:46:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
static int control(struct vo *vo, uint32_t request, void *data)
|
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
2014-03-08 22:38:53 +00:00
|
|
|
int r = VO_NOTIMPL;
|
|
|
|
pthread_mutex_lock(&vo->encode_lavc_ctx->lock);
|
2012-09-14 15:51:26 +00:00
|
|
|
switch (request) {
|
2014-03-28 23:25:08 +00:00
|
|
|
case VOCTRL_GET_COLORSPACE:
|
|
|
|
*(struct mp_image_params *)data = vc->real_colorspace;
|
2014-03-08 22:38:53 +00:00
|
|
|
r = 1;
|
|
|
|
break;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
2014-03-08 22:38:53 +00:00
|
|
|
pthread_mutex_unlock(&vo->encode_lavc_ctx->lock);
|
|
|
|
return r;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct vo_driver video_out_lavc = {
|
2013-02-06 21:54:03 +00:00
|
|
|
.encode = true,
|
2013-10-23 17:06:14 +00:00
|
|
|
.description = "video encoding using libavcodec",
|
|
|
|
.name = "lavc",
|
video: move display and timing to a separate thread
The VO is run inside its own thread. It also does most of video timing.
The playloop hands the image data and a realtime timestamp to the VO,
and the VO does the rest.
In particular, this allows the playloop to do other things, instead of
blocking for video redraw. But if anything accesses the VO during video
timing, it will block.
This also fixes vo_sdl.c event handling; but that is only a side-effect,
since reimplementing the broken way would require more effort.
Also drop --softsleep. In theory, this option helps if the kernel's
sleeping mechanism is too inaccurate for video timing. In practice, I
haven't ever encountered a situation where it helps, and it just burns
CPU cycles. On the other hand it's probably actively harmful, because
it prevents the libavcodec decoder threads from doing real work.
Side note:
Originally, I intended that multiple frames can be queued to the VO. But
this is not done, due to problems with OSD and other certain features.
OSD in particular is simply designed in a way that it can be neither
timed nor copied, so you do have to render it into the video frame
before you can draw the next frame. (Subtitles have no such restriction.
sd_lavc was even updated to fix this.) It seems the right solution to
queuing multiple VO frames is rendering on VO-backed framebuffers, like
vo_vdpau.c does. This requires VO driver support, and is out of scope
of this commit.
As consequence, the VO has a queue size of 1. The existing video queue
is just needed to compute frame duration, and will be moved out in the
next commit.
2014-08-12 21:02:08 +00:00
|
|
|
.untimed = true,
|
2012-09-14 15:51:26 +00:00
|
|
|
.preinit = preinit,
|
2012-11-04 15:24:18 +00:00
|
|
|
.query_format = query_format,
|
2014-01-24 20:22:25 +00:00
|
|
|
.reconfig = reconfig,
|
2012-09-14 15:51:26 +00:00
|
|
|
.control = control,
|
|
|
|
.uninit = uninit,
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
.draw_image = draw_image,
|
2014-09-20 13:14:43 +00:00
|
|
|
.flip_page = flip_page,
|
2012-09-14 15:51:26 +00:00
|
|
|
};
|
|
|
|
|
2012-10-24 17:25:24 +00:00
|
|
|
// vim: sw=4 ts=4 et tw=80
|