2012-09-14 15:51:26 +00:00
|
|
|
/*
|
|
|
|
* video encoding using libavformat
|
2015-04-13 07:36:54 +00:00
|
|
|
*
|
2012-09-14 15:51:26 +00:00
|
|
|
* 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
|
|
|
*
|
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>
|
2016-06-24 18:20:32 +00:00
|
|
|
|
2023-11-04 02:55:38 +00:00
|
|
|
#include <libplacebo/utils/libav.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"
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_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 {
|
2018-04-22 17:40:36 +00:00
|
|
|
struct encoder_context *enc;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
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
|
|
|
{
|
2018-04-22 17:40:36 +00:00
|
|
|
struct priv *vc = vo->priv;
|
|
|
|
vc->enc = encoder_context_alloc(vo->encode_lavc_ctx, STREAM_VIDEO, vo->log);
|
|
|
|
if (!vc->enc)
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
2018-04-22 17:40:36 +00:00
|
|
|
talloc_steal(vc, vc->enc);
|
2012-09-14 15:51:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
2018-04-29 00:55:27 +00:00
|
|
|
struct encoder_context *enc = vc->enc;
|
2014-03-08 22:38:53 +00:00
|
|
|
|
2018-04-29 00:55:27 +00:00
|
|
|
if (!vc->shutdown)
|
|
|
|
encoder_encode(enc, NULL); // finish encoding
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-29 17:42:18 +00:00
|
|
|
static void on_ready(void *ptr)
|
|
|
|
{
|
|
|
|
struct vo *vo = ptr;
|
|
|
|
|
|
|
|
vo_event(vo, VO_EVENT_INITIAL_UNBLOCK);
|
|
|
|
}
|
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
static int reconfig2(struct vo *vo, struct mp_image *img)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
2018-04-22 17:40:36 +00:00
|
|
|
AVCodecContext *encoder = vc->enc->encoder;
|
|
|
|
|
|
|
|
struct mp_image_params *params = &img->params;
|
2014-01-24 20:22:25 +00:00
|
|
|
enum AVPixelFormat pix_fmt = imgfmt2pixfmt(params->imgfmt);
|
2015-12-19 19:04:31 +00:00
|
|
|
AVRational aspect = {params->p_w, params->p_h};
|
2018-04-19 17:55:13 +00:00
|
|
|
int width = params->w;
|
|
|
|
int height = params->h;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
if (vc->shutdown)
|
2012-09-14 15:51:26 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
if (avcodec_is_open(encoder)) {
|
|
|
|
if (width == encoder->width && height == encoder->height &&
|
|
|
|
pix_fmt == encoder->pix_fmt)
|
|
|
|
{
|
|
|
|
// consider these changes not critical
|
|
|
|
MP_ERR(vo, "Ignoring mid-stream parameter changes!\n");
|
|
|
|
return 0;
|
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
|
2018-04-22 17:40:36 +00:00
|
|
|
// (due to the avcodec_is_open() check above).
|
2014-11-12 11:16:07 +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
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
encoder->sample_aspect_ratio = aspect;
|
|
|
|
encoder->width = width;
|
|
|
|
encoder->height = height;
|
|
|
|
encoder->pix_fmt = pix_fmt;
|
2023-11-04 02:55:38 +00:00
|
|
|
encoder->colorspace = pl_system_to_av(params->repr.sys);
|
|
|
|
encoder->color_range = pl_levels_to_av(params->repr.levels);
|
2018-04-22 17:40:36 +00:00
|
|
|
|
|
|
|
AVRational tb;
|
|
|
|
|
2018-04-29 00:55:27 +00:00
|
|
|
// we want to handle:
|
|
|
|
// 1/25
|
|
|
|
// 1001/24000
|
|
|
|
// 1001/30000
|
|
|
|
// for this we would need 120000fps...
|
|
|
|
// however, mpeg-4 only allows 16bit values
|
|
|
|
// so let's take 1001/30000 out
|
|
|
|
tb.num = 24000;
|
|
|
|
tb.den = 1;
|
2018-04-22 17:40:36 +00:00
|
|
|
|
|
|
|
const AVRational *rates = encoder->codec->supported_framerates;
|
|
|
|
if (rates && rates[0].den)
|
|
|
|
tb = rates[av_find_nearest_q_idx(tb, rates)];
|
|
|
|
|
|
|
|
encoder->time_base = av_inv_q(tb);
|
|
|
|
|
2023-01-28 20:15:22 +00:00
|
|
|
// Used for rate control, level selection, etc.
|
|
|
|
// Usually it's not too catastrophic if this isn't exactly correct,
|
|
|
|
// as long as it's not off by orders of magnitude.
|
|
|
|
// If we don't set anything, encoders will use the time base,
|
|
|
|
// and 24000 is so high that the output can end up extremely screwy (see #11215),
|
|
|
|
// so we default to 240 if we don't have a real value.
|
|
|
|
if (img->nominal_fps > 0)
|
|
|
|
encoder->framerate = av_d2q(img->nominal_fps, img->nominal_fps * 1001 + 2); // Hopefully give exact results for NTSC rates
|
|
|
|
else
|
|
|
|
encoder->framerate = (AVRational){ 240, 1 };
|
|
|
|
|
2018-04-29 17:42:18 +00:00
|
|
|
if (!encoder_init_codec_and_muxer(vc->enc, on_ready, vo))
|
2012-09-14 15:51:26 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
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
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
enum AVPixelFormat pix_fmt = imgfmt2pixfmt(format);
|
|
|
|
const enum AVPixelFormat *p = vc->enc->encoder->codec->pix_fmts;
|
2016-06-24 18:20:32 +00:00
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
if (!p)
|
|
|
|
return 1;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
while (*p != AV_PIX_FMT_NONE) {
|
|
|
|
if (*p == pix_fmt)
|
|
|
|
return 1;
|
|
|
|
p++;
|
2016-06-24 18:20:32 +00:00
|
|
|
}
|
2018-04-22 17:40:36 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-29 14:07:21 +00:00
|
|
|
static void draw_frame(struct vo *vo, struct vo_frame *voframe)
|
2012-09-14 15:51:26 +00:00
|
|
|
{
|
|
|
|
struct priv *vc = vo->priv;
|
2018-04-22 17:40:36 +00:00
|
|
|
struct encoder_context *enc = vc->enc;
|
|
|
|
struct encode_lavc_context *ectx = enc->encode_lavc_ctx;
|
|
|
|
AVCodecContext *avc = enc->encoder;
|
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
|
|
|
|
2018-04-29 14:07:21 +00:00
|
|
|
if (voframe->redraw || voframe->repeat || voframe->num_frames < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct mp_image *mpi = voframe->frames[0];
|
|
|
|
|
2018-04-29 00:55:27 +00:00
|
|
|
struct mp_osd_res dim = osd_res_from_image_params(vo->params);
|
|
|
|
osd_draw_on_image(vo->osd, dim, mpi->pts, OSD_DRAW_SUB_ONLY, mpi);
|
vo_lavc: remove messy delayed subtitle rendering logic
For some reason vo_lavc's draw_image can buffer the frame and encode it
only later. Also, there is logic for rendering the OSD (i.e. subtitles)
only when needed.
In theory this can lead to subtitles being pruned before it tries to
render them (as the subtitle logic doesn't know that the VO still needs
them later), although this probably never happens in reality.
The worse issue, that actually happened, is that if the last frame gets
buffered, it attempts to render subtitles in the uninit callback. At
this point, the subtitle decoder is already torn down and all subtitles
removed, thus it will draw nothing. This didn't always happen. I'm not
sure why - potentially in the working cases, the frame wasn't buffered.
Since this logic doesn't have much worth, except a minor performance
advantage if frames with subtitles are dropped, just remove it.
Hopefully fixes #4689.
2017-11-07 04:29:26 +00:00
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
if (vc->shutdown)
|
2018-04-29 14:07:21 +00:00
|
|
|
return;
|
2018-04-22 17:40:36 +00:00
|
|
|
|
|
|
|
// Lock for shared timestamp fields.
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_lock(&ectx->lock);
|
2018-04-22 17:40:36 +00:00
|
|
|
|
2018-04-29 00:55:27 +00:00
|
|
|
double pts = mpi->pts;
|
|
|
|
double outpts = pts;
|
|
|
|
if (!enc->options->rawts) {
|
2012-09-25 09:53:29 +00:00
|
|
|
// fix the discontinuity pts offset
|
|
|
|
if (ectx->discontinuity_pts_offset == MP_NOPTS_VALUE) {
|
2018-04-29 00:55:27 +00:00
|
|
|
ectx->discontinuity_pts_offset = ectx->next_in_pts - pts;
|
|
|
|
} else if (fabs(pts + ectx->discontinuity_pts_offset -
|
2018-04-19 18:13:28 +00:00
|
|
|
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",
|
2018-04-29 00:55:27 +00:00
|
|
|
pts + ectx->discontinuity_pts_offset - ectx->next_in_pts);
|
|
|
|
ectx->discontinuity_pts_offset = ectx->next_in_pts - pts;
|
2012-09-25 09:53:29 +00:00
|
|
|
}
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2012-09-25 09:53:29 +00:00
|
|
|
outpts = pts + ectx->discontinuity_pts_offset;
|
|
|
|
}
|
|
|
|
|
2018-04-29 00:55:27 +00:00
|
|
|
if (!enc->options->rawts) {
|
|
|
|
// calculate expected pts of next video frame
|
|
|
|
double timeunit = av_q2d(avc->time_base);
|
|
|
|
double expected_next_pts = pts + timeunit;
|
2012-09-25 09:53:29 +00:00
|
|
|
// set next allowed output pts value
|
2018-04-29 00:55:27 +00:00
|
|
|
double nextpts = expected_next_pts + ectx->discontinuity_pts_offset;
|
2012-09-25 09:53:29 +00:00
|
|
|
if (nextpts > ectx->next_in_pts)
|
|
|
|
ectx->next_in_pts = nextpts;
|
2012-09-14 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_unlock(&ectx->lock);
|
2018-04-22 17:40:36 +00:00
|
|
|
|
2018-04-29 00:55:27 +00:00
|
|
|
AVFrame *frame = mp_image_to_av_frame(mpi);
|
2023-01-10 17:59:21 +00:00
|
|
|
MP_HANDLE_OOM(frame);
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2018-04-29 00:55:27 +00:00
|
|
|
frame->pts = rint(outpts * av_q2d(av_inv_q(avc->time_base)));
|
|
|
|
frame->pict_type = 0; // keep this at unknown/undefined
|
|
|
|
frame->quality = avc->global_quality;
|
|
|
|
encoder_encode(enc, frame);
|
|
|
|
av_frame_free(&frame);
|
2014-06-17 21:05:50 +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)
|
|
|
|
{
|
2015-03-30 21:56:17 +00:00
|
|
|
return VO_NOTIMPL;
|
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",
|
2018-04-29 17:42:18 +00:00
|
|
|
.initially_blocked = true,
|
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,
|
2018-04-22 17:40:36 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
2012-09-14 15:51:26 +00:00
|
|
|
.preinit = preinit,
|
2012-11-04 15:24:18 +00:00
|
|
|
.query_format = query_format,
|
2018-04-22 17:40:36 +00:00
|
|
|
.reconfig2 = reconfig2,
|
2012-09-14 15:51:26 +00:00
|
|
|
.control = control,
|
|
|
|
.uninit = uninit,
|
2018-04-29 14:07:21 +00:00
|
|
|
.draw_frame = draw_frame,
|
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
|