2012-12-12 22:55:34 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +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-12-12 22:55:34 +00:00
|
|
|
*
|
|
|
|
* mpv 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
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2012-12-12 22:55:34 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +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-12-12 22:55:34 +00:00
|
|
|
*/
|
|
|
|
|
2023-02-28 05:11:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-07-27 22:10:58 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
2012-12-12 22:55:34 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
#include <libavutil/buffer.h>
|
2017-01-12 11:51:55 +00:00
|
|
|
#include <libavutil/hwcontext.h>
|
2023-02-28 05:11:39 +00:00
|
|
|
#if HAVE_VULKAN_INTEROP
|
|
|
|
#include <libavutil/hwcontext_vulkan.h>
|
|
|
|
#endif
|
2017-01-12 13:19:16 +00:00
|
|
|
#include <libavutil/mem.h>
|
2023-02-28 05:11:39 +00:00
|
|
|
#include <libavutil/pixdesc.h>
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2012-12-12 22:55:34 +00:00
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2012-12-12 22:55:34 +00:00
|
|
|
|
2017-01-12 11:51:55 +00:00
|
|
|
#include "fmt-conversion.h"
|
2012-12-12 22:55:34 +00:00
|
|
|
#include "mp_image_pool.h"
|
2023-10-21 02:55:41 +00:00
|
|
|
#include "mp_image.h"
|
|
|
|
#include "osdep/threads.h"
|
2012-12-12 22:55:34 +00:00
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
static mp_static_mutex pool_mutex = MP_STATIC_MUTEX_INITIALIZER;
|
|
|
|
#define pool_lock() mp_mutex_lock(&pool_mutex)
|
|
|
|
#define pool_unlock() mp_mutex_unlock(&pool_mutex)
|
2013-07-27 22:10:58 +00:00
|
|
|
|
|
|
|
// Thread-safety: the pool itself is not thread-safe, but pool-allocated images
|
2013-11-28 18:28:38 +00:00
|
|
|
// can be referenced and unreferenced from other threads. (As long as the image
|
|
|
|
// destructors are thread-safe.)
|
2013-07-27 22:10:58 +00:00
|
|
|
|
|
|
|
struct mp_image_pool {
|
|
|
|
struct mp_image **images;
|
|
|
|
int num_images;
|
2014-03-17 17:21:29 +00:00
|
|
|
|
2018-01-12 00:40:01 +00:00
|
|
|
int fmt, w, h;
|
|
|
|
|
2014-03-17 17:21:29 +00:00
|
|
|
mp_image_allocator allocator;
|
|
|
|
void *allocator_ctx;
|
|
|
|
|
|
|
|
bool use_lru;
|
|
|
|
unsigned int lru_counter;
|
2013-07-27 22:10:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Used to gracefully handle the case when the pool is freed while image
|
|
|
|
// references allocated from the image pool are still held by someone.
|
|
|
|
struct image_flags {
|
|
|
|
// If both of these are false, the image must be freed.
|
|
|
|
bool referenced; // outside mp_image reference exists
|
|
|
|
bool pool_alive; // the mp_image_pool references this
|
2014-03-17 17:21:29 +00:00
|
|
|
unsigned int order; // for LRU allocation (basically a timestamp)
|
2012-12-12 22:55:34 +00:00
|
|
|
};
|
|
|
|
|
2013-10-12 23:16:30 +00:00
|
|
|
static void image_pool_destructor(void *ptr)
|
2012-12-12 22:55:34 +00:00
|
|
|
{
|
|
|
|
struct mp_image_pool *pool = ptr;
|
|
|
|
mp_image_pool_clear(pool);
|
|
|
|
}
|
|
|
|
|
2018-01-12 00:40:01 +00:00
|
|
|
// If tparent!=NULL, set it as talloc parent for the pool.
|
|
|
|
struct mp_image_pool *mp_image_pool_new(void *tparent)
|
2012-12-12 22:55:34 +00:00
|
|
|
{
|
2018-01-12 00:40:01 +00:00
|
|
|
struct mp_image_pool *pool = talloc_ptrtype(tparent, pool);
|
2012-12-12 22:55:34 +00:00
|
|
|
talloc_set_destructor(pool, image_pool_destructor);
|
2018-01-12 00:40:01 +00:00
|
|
|
*pool = (struct mp_image_pool) {0};
|
2012-12-12 22:55:34 +00:00
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_image_pool_clear(struct mp_image_pool *pool)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < pool->num_images; n++) {
|
|
|
|
struct mp_image *img = pool->images[n];
|
2013-07-27 22:10:58 +00:00
|
|
|
struct image_flags *it = img->priv;
|
|
|
|
bool referenced;
|
|
|
|
pool_lock();
|
|
|
|
assert(it->pool_alive);
|
|
|
|
it->pool_alive = false;
|
|
|
|
referenced = it->referenced;
|
|
|
|
pool_unlock();
|
|
|
|
if (!referenced)
|
2012-12-12 22:55:34 +00:00
|
|
|
talloc_free(img);
|
|
|
|
}
|
|
|
|
pool->num_images = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-27 22:10:58 +00:00
|
|
|
// This is the only function that is allowed to run in a different thread.
|
|
|
|
// (Consider passing an image to another thread, which frees it.)
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
static void unref_image(void *opaque, uint8_t *data)
|
2012-12-12 22:55:34 +00:00
|
|
|
{
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
struct mp_image *img = opaque;
|
2013-07-27 22:10:58 +00:00
|
|
|
struct image_flags *it = img->priv;
|
|
|
|
bool alive;
|
|
|
|
pool_lock();
|
|
|
|
assert(it->referenced);
|
|
|
|
it->referenced = false;
|
|
|
|
alive = it->pool_alive;
|
|
|
|
pool_unlock();
|
|
|
|
if (!alive)
|
2012-12-12 22:55:34 +00:00
|
|
|
talloc_free(img);
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:21:29 +00:00
|
|
|
// Return a new image of given format/size. Unlike mp_image_pool_get(), this
|
|
|
|
// returns NULL if there is no free image of this format/size.
|
|
|
|
struct mp_image *mp_image_pool_get_no_alloc(struct mp_image_pool *pool, int fmt,
|
|
|
|
int w, int h)
|
2012-12-12 22:55:34 +00:00
|
|
|
{
|
|
|
|
struct mp_image *new = NULL;
|
2013-07-27 22:10:58 +00:00
|
|
|
pool_lock();
|
2012-12-12 22:55:34 +00:00
|
|
|
for (int n = 0; n < pool->num_images; n++) {
|
|
|
|
struct mp_image *img = pool->images[n];
|
2014-03-17 17:21:29 +00:00
|
|
|
struct image_flags *img_it = img->priv;
|
|
|
|
assert(img_it->pool_alive);
|
|
|
|
if (!img_it->referenced) {
|
2013-07-27 22:10:58 +00:00
|
|
|
if (img->imgfmt == fmt && img->w == w && img->h == h) {
|
2014-03-17 17:21:29 +00:00
|
|
|
if (pool->use_lru) {
|
|
|
|
struct image_flags *new_it = new ? new->priv : NULL;
|
|
|
|
if (!new_it || new_it->order > img_it->order)
|
|
|
|
new = img;
|
|
|
|
} else {
|
|
|
|
new = img;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-27 22:10:58 +00:00
|
|
|
}
|
2012-12-12 22:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-27 22:10:58 +00:00
|
|
|
pool_unlock();
|
2014-03-17 17:21:29 +00:00
|
|
|
if (!new)
|
|
|
|
return NULL;
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
|
|
|
|
// Reference the new image. Since mp_image_pool is not declared thread-safe,
|
|
|
|
// and unreffing images from other threads does not allocate new images,
|
|
|
|
// no synchronization is required here.
|
|
|
|
for (int p = 0; p < MP_MAX_PLANES; p++)
|
|
|
|
assert(!!new->bufs[p] == !p); // only 1 AVBufferRef
|
|
|
|
|
|
|
|
struct mp_image *ref = mp_image_new_dummy_ref(new);
|
|
|
|
|
|
|
|
// This assumes the buffer is at this point exclusively owned by us: we
|
|
|
|
// can't track whether the buffer is unique otherwise.
|
|
|
|
// (av_buffer_is_writable() checks the refcount of the new buffer only.)
|
2017-08-08 14:16:55 +00:00
|
|
|
int flags = av_buffer_is_writable(new->bufs[0]) ? 0 : AV_BUFFER_FLAG_READONLY;
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
ref->bufs[0] = av_buffer_create(new->bufs[0]->data, new->bufs[0]->size,
|
2017-08-08 14:16:55 +00:00
|
|
|
unref_image, new, flags);
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
if (!ref->bufs[0]) {
|
|
|
|
talloc_free(ref);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:21:29 +00:00
|
|
|
struct image_flags *it = new->priv;
|
|
|
|
assert(!it->referenced && it->pool_alive);
|
|
|
|
it->referenced = true;
|
|
|
|
it->order = ++pool->lru_counter;
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
return ref;
|
2014-03-17 17:21:29 +00:00
|
|
|
}
|
2013-07-27 22:10:58 +00:00
|
|
|
|
2016-02-16 00:04:52 +00:00
|
|
|
void mp_image_pool_add(struct mp_image_pool *pool, struct mp_image *new)
|
|
|
|
{
|
|
|
|
struct image_flags *it = talloc_ptrtype(new, it);
|
|
|
|
*it = (struct image_flags) { .pool_alive = true };
|
|
|
|
new->priv = it;
|
|
|
|
MP_TARRAY_APPEND(pool, pool->images, pool->num_images, new);
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:21:29 +00:00
|
|
|
// Return a new image of given format/size. The only difference to
|
|
|
|
// mp_image_alloc() is that there is a transparent mechanism to recycle image
|
|
|
|
// data allocations through this pool.
|
2015-01-22 16:37:40 +00:00
|
|
|
// If pool==NULL, mp_image_alloc() is called (for convenience).
|
2014-03-17 17:21:29 +00:00
|
|
|
// The image can be free'd with talloc_free().
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
// Returns NULL on OOM.
|
2014-03-17 17:21:29 +00:00
|
|
|
struct mp_image *mp_image_pool_get(struct mp_image_pool *pool, int fmt,
|
|
|
|
int w, int h)
|
|
|
|
{
|
2015-01-22 16:37:40 +00:00
|
|
|
if (!pool)
|
|
|
|
return mp_image_alloc(fmt, w, h);
|
2014-03-17 17:21:29 +00:00
|
|
|
struct mp_image *new = mp_image_pool_get_no_alloc(pool, fmt, w, h);
|
2012-12-12 22:55:34 +00:00
|
|
|
if (!new) {
|
2018-01-12 00:40:01 +00:00
|
|
|
if (fmt != pool->fmt || w != pool->w || h != pool->h)
|
2012-12-12 22:55:34 +00:00
|
|
|
mp_image_pool_clear(pool);
|
2018-01-12 00:40:01 +00:00
|
|
|
pool->fmt = fmt;
|
|
|
|
pool->w = w;
|
|
|
|
pool->h = h;
|
2014-03-17 17:21:29 +00:00
|
|
|
if (pool->allocator) {
|
|
|
|
new = pool->allocator(pool->allocator_ctx, fmt, w, h);
|
|
|
|
} else {
|
|
|
|
new = mp_image_alloc(fmt, w, h);
|
|
|
|
}
|
|
|
|
if (!new)
|
|
|
|
return NULL;
|
2016-02-16 00:04:52 +00:00
|
|
|
mp_image_pool_add(pool, new);
|
2014-03-17 17:21:29 +00:00
|
|
|
new = mp_image_pool_get_no_alloc(pool, fmt, w, h);
|
2012-12-12 22:55:34 +00:00
|
|
|
}
|
2014-03-17 17:21:29 +00:00
|
|
|
return new;
|
2012-12-12 22:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Like mp_image_new_copy(), but allocate the image out of the pool.
|
2015-01-22 16:37:40 +00:00
|
|
|
// If pool==NULL, a plain copy is made (for convenience).
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
// Returns NULL on OOM.
|
2012-12-12 22:55:34 +00:00
|
|
|
struct mp_image *mp_image_pool_new_copy(struct mp_image_pool *pool,
|
|
|
|
struct mp_image *img)
|
|
|
|
{
|
|
|
|
struct mp_image *new = mp_image_pool_get(pool, img->imgfmt, img->w, img->h);
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
if (new) {
|
|
|
|
mp_image_copy(new, img);
|
|
|
|
mp_image_copy_attributes(new, img);
|
|
|
|
}
|
2012-12-12 22:55:34 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like mp_image_make_writeable(), but if a copy has to be made, allocate it
|
|
|
|
// out of the pool.
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
// If pool==NULL, mp_image_make_writeable() is called (for convenience).
|
|
|
|
// Returns false on failure (see mp_image_make_writeable()).
|
|
|
|
bool mp_image_pool_make_writeable(struct mp_image_pool *pool,
|
2012-12-12 22:55:34 +00:00
|
|
|
struct mp_image *img)
|
|
|
|
{
|
|
|
|
if (mp_image_is_writeable(img))
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
return true;
|
|
|
|
struct mp_image *new = mp_image_pool_new_copy(pool, img);
|
|
|
|
if (!new)
|
|
|
|
return false;
|
|
|
|
mp_image_steal_data(img, new);
|
2012-12-12 22:55:34 +00:00
|
|
|
assert(mp_image_is_writeable(img));
|
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e.
abort() was called). This was intentional, because it's pretty silly to
degrade playback, and in almost all situations, the OOM will probably
kill you anyway. (And then there's the standard Linux overcommit
behavior, which also will kill you at some point.)
But I changed my opinion, so here we go. This change does not affect
_all_ memory allocations, just image data. Now in most failure cases,
the output will just be skipped. For video filters, this coincidentally
means that failure is treated as EOF (because the playback core assumes
EOF if nothing comes out of the video filter chain). In other
situations, output might be in some way degraded, like skipping frames,
not scaling OSD, and such.
Functions whose return values changed semantics:
mp_image_alloc
mp_image_new_copy
mp_image_new_ref
mp_image_make_writeable
mp_image_setrefp
mp_image_to_av_frame_and_unref
mp_image_from_av_frame
mp_image_new_external_ref
mp_image_new_custom_ref
mp_image_pool_make_writeable
mp_image_pool_get
mp_image_pool_new_copy
mp_vdpau_mixed_frame_create
vf_alloc_out_image
vf_make_out_image_writeable
glGetWindowScreenshot
2014-06-17 20:43:43 +00:00
|
|
|
return true;
|
2012-12-12 22:55:34 +00:00
|
|
|
}
|
2014-03-17 17:21:29 +00:00
|
|
|
|
video: replace our own refcounting with libavutil's
mpv had refcounted frames before libav*, so we were not using
libavutil's facilities. Change this and drop our own code.
Since AVFrames are not actually refcounted, and only the image data
they reference, the semantics change a bit. This affects mainly
mp_image_pool, which was operating on whole images instead of buffers.
While we could work on AVBufferRefs instead (and use AVBufferPool),
this doesn't work for use with hardware decoding, which doesn't
map cleanly to FFmpeg's reference counting. But it worked out. One
weird consequence is that we still need our custom image data
allocation function (for normal image data), because AVFrame's uses
multiple buffers.
There also seems to be a timing-dependent problem with vaapi (the
pool appears to be "leaking" surfaces). I don't know if this is a new
problem, or whether the code changes just happened to cause it more
often. Raising the number of reserved surfaces seemed to fix it, but
since it appears to be timing dependent, and I couldn't find anything
wrong with the code, I'm just going to assume it's not a new bug.
2015-07-05 21:56:00 +00:00
|
|
|
// Call cb(cb_data, fmt, w, h) to allocate an image. Note that the resulting
|
|
|
|
// image must use only 1 AVBufferRef. The returned image must also be owned
|
|
|
|
// exclusively by the image pool, otherwise mp_image_is_writeable() will not
|
|
|
|
// work due to FFmpeg restrictions.
|
2014-03-17 17:21:29 +00:00
|
|
|
void mp_image_pool_set_allocator(struct mp_image_pool *pool,
|
|
|
|
mp_image_allocator cb, void *cb_data)
|
|
|
|
{
|
|
|
|
pool->allocator = cb;
|
|
|
|
pool->allocator_ctx = cb_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put into LRU mode. (Likely better for hwaccel surfaces, but worse for memory.)
|
|
|
|
void mp_image_pool_set_lru(struct mp_image_pool *pool)
|
|
|
|
{
|
|
|
|
pool->use_lru = true;
|
|
|
|
}
|
2017-01-12 11:51:55 +00:00
|
|
|
|
2019-10-02 19:07:14 +00:00
|
|
|
// Return the sw image format mp_image_hw_download() would use. This can be
|
|
|
|
// different from src->params.hw_subfmt in obscure cases.
|
|
|
|
int mp_image_hw_download_get_sw_format(struct mp_image *src)
|
2017-01-12 11:51:55 +00:00
|
|
|
{
|
|
|
|
if (!src->hwctx)
|
2019-10-02 19:07:14 +00:00
|
|
|
return 0;
|
2017-01-12 11:51:55 +00:00
|
|
|
|
|
|
|
// Try to find the first format which we can apparently use.
|
|
|
|
int imgfmt = 0;
|
|
|
|
enum AVPixelFormat *fmts;
|
|
|
|
if (av_hwframe_transfer_get_formats(src->hwctx,
|
|
|
|
AV_HWFRAME_TRANSFER_DIRECTION_FROM, &fmts, 0) < 0)
|
2019-10-02 19:07:14 +00:00
|
|
|
return 0;
|
2017-01-12 11:51:55 +00:00
|
|
|
for (int n = 0; fmts[n] != AV_PIX_FMT_NONE; n++) {
|
|
|
|
imgfmt = pixfmt2imgfmt(fmts[n]);
|
|
|
|
if (imgfmt)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
av_free(fmts);
|
|
|
|
|
2019-10-02 19:07:14 +00:00
|
|
|
return imgfmt;
|
|
|
|
}
|
|
|
|
|
2023-03-27 20:42:17 +00:00
|
|
|
// Copies the contents of the HW surface src to system memory and returns it.
|
2019-10-02 19:07:14 +00:00
|
|
|
// If swpool is not NULL, it's used to allocate the target image.
|
|
|
|
// src must be a hw surface with a AVHWFramesContext attached.
|
|
|
|
// The returned image is cropped as needed.
|
|
|
|
// Returns NULL on failure.
|
|
|
|
struct mp_image *mp_image_hw_download(struct mp_image *src,
|
|
|
|
struct mp_image_pool *swpool)
|
|
|
|
{
|
|
|
|
int imgfmt = mp_image_hw_download_get_sw_format(src);
|
2017-01-12 11:51:55 +00:00
|
|
|
if (!imgfmt)
|
|
|
|
return NULL;
|
|
|
|
|
2019-10-02 19:07:14 +00:00
|
|
|
assert(src->hwctx);
|
|
|
|
AVHWFramesContext *fctx = (void *)src->hwctx->data;
|
|
|
|
|
2017-01-12 11:51:55 +00:00
|
|
|
struct mp_image *dst =
|
|
|
|
mp_image_pool_get(swpool, imgfmt, fctx->width, fctx->height);
|
|
|
|
if (!dst)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Target image must be writable, so unref it.
|
|
|
|
AVFrame *dstav = mp_image_to_av_frame_and_unref(dst);
|
|
|
|
if (!dstav)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
AVFrame *srcav = mp_image_to_av_frame(src);
|
|
|
|
if (!srcav) {
|
|
|
|
av_frame_unref(dstav);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = av_hwframe_transfer_data(dstav, srcav, 0);
|
2017-12-01 06:22:06 +00:00
|
|
|
av_frame_free(&srcav);
|
2017-01-12 11:51:55 +00:00
|
|
|
dst = mp_image_from_av_frame(dstav);
|
2017-12-01 06:22:06 +00:00
|
|
|
av_frame_free(&dstav);
|
2017-02-07 16:06:20 +00:00
|
|
|
if (res >= 0 && dst) {
|
2017-01-12 11:51:55 +00:00
|
|
|
mp_image_set_size(dst, src->w, src->h);
|
|
|
|
mp_image_copy_attributes(dst, src);
|
|
|
|
} else {
|
|
|
|
mp_image_unrefp(&dst);
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
2017-09-29 15:54:21 +00:00
|
|
|
|
|
|
|
bool mp_image_hw_upload(struct mp_image *hw_img, struct mp_image *src)
|
|
|
|
{
|
|
|
|
if (hw_img->w != src->w || hw_img->h != src->h)
|
|
|
|
return false;
|
|
|
|
|
2022-03-25 18:16:21 +00:00
|
|
|
if (!hw_img->hwctx)
|
2017-09-29 15:54:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
AVFrame *dstav = NULL;
|
|
|
|
AVFrame *srcav = NULL;
|
|
|
|
|
|
|
|
// This means the destination image will not be "writable", which would be
|
2024-01-19 18:46:20 +00:00
|
|
|
// a pain if FFmpeg enforced this - fortunately it doesn't care. We can
|
2017-09-29 15:54:21 +00:00
|
|
|
// transfer data to it even if there are multiple refs.
|
|
|
|
dstav = mp_image_to_av_frame(hw_img);
|
|
|
|
if (!dstav)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
srcav = mp_image_to_av_frame(src);
|
|
|
|
if (!srcav)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ok = av_hwframe_transfer_data(dstav, srcav, 0) >= 0;
|
|
|
|
|
|
|
|
done:
|
2023-02-03 05:12:38 +00:00
|
|
|
av_frame_free(&srcav);
|
|
|
|
av_frame_free(&dstav);
|
2017-09-29 15:54:21 +00:00
|
|
|
|
|
|
|
if (ok)
|
|
|
|
mp_image_copy_attributes(hw_img, src);
|
|
|
|
return ok;
|
|
|
|
}
|
2018-01-16 10:46:16 +00:00
|
|
|
|
|
|
|
bool mp_update_av_hw_frames_pool(struct AVBufferRef **hw_frames_ctx,
|
|
|
|
struct AVBufferRef *hw_device_ctx,
|
2023-02-28 05:11:39 +00:00
|
|
|
int imgfmt, int sw_imgfmt, int w, int h,
|
|
|
|
bool disable_multiplane)
|
2018-01-16 10:46:16 +00:00
|
|
|
{
|
|
|
|
enum AVPixelFormat format = imgfmt2pixfmt(imgfmt);
|
|
|
|
enum AVPixelFormat sw_format = imgfmt2pixfmt(sw_imgfmt);
|
|
|
|
|
|
|
|
if (format == AV_PIX_FMT_NONE || sw_format == AV_PIX_FMT_NONE ||
|
|
|
|
!hw_device_ctx || w < 1 || h < 1)
|
|
|
|
{
|
|
|
|
av_buffer_unref(hw_frames_ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*hw_frames_ctx) {
|
|
|
|
AVHWFramesContext *hw_frames = (void *)(*hw_frames_ctx)->data;
|
|
|
|
|
|
|
|
if (hw_frames->device_ref->data != hw_device_ctx->data ||
|
|
|
|
hw_frames->format != format || hw_frames->sw_format != sw_format ||
|
|
|
|
hw_frames->width != w || hw_frames->height != h)
|
|
|
|
av_buffer_unref(hw_frames_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*hw_frames_ctx) {
|
|
|
|
*hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
|
|
|
|
if (!*hw_frames_ctx)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AVHWFramesContext *hw_frames = (void *)(*hw_frames_ctx)->data;
|
|
|
|
hw_frames->format = format;
|
|
|
|
hw_frames->sw_format = sw_format;
|
|
|
|
hw_frames->width = w;
|
|
|
|
hw_frames->height = h;
|
2023-02-28 05:11:39 +00:00
|
|
|
|
|
|
|
#if HAVE_VULKAN_INTEROP
|
|
|
|
if (format == AV_PIX_FMT_VULKAN && disable_multiplane) {
|
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(sw_format);
|
|
|
|
if ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
|
|
|
|
!(desc->flags & AV_PIX_FMT_FLAG_RGB)) {
|
|
|
|
AVVulkanFramesContext *vk_frames = hw_frames->hwctx;
|
|
|
|
vk_frames->flags = AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-16 10:46:16 +00:00
|
|
|
if (av_hwframe_ctx_init(*hw_frames_ctx) < 0) {
|
|
|
|
av_buffer_unref(hw_frames_ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mp_image *mp_av_pool_image_hw_upload(struct AVBufferRef *hw_frames_ctx,
|
|
|
|
struct mp_image *src)
|
|
|
|
{
|
|
|
|
AVFrame *av_frame = av_frame_alloc();
|
|
|
|
if (!av_frame)
|
|
|
|
return NULL;
|
|
|
|
if (av_hwframe_get_buffer(hw_frames_ctx, av_frame, 0) < 0) {
|
|
|
|
av_frame_free(&av_frame);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
struct mp_image *dst = mp_image_from_av_frame(av_frame);
|
|
|
|
av_frame_free(&av_frame);
|
|
|
|
if (!dst)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (dst->w < src->w || dst->h < src->h) {
|
|
|
|
talloc_free(dst);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_image_set_size(dst, src->w, src->h);
|
|
|
|
|
|
|
|
if (!mp_image_hw_upload(dst, src)) {
|
|
|
|
talloc_free(dst);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_image_copy_attributes(dst, src);
|
|
|
|
return dst;
|
|
|
|
}
|
2022-04-04 03:12:50 +00:00
|
|
|
|
|
|
|
struct mp_image *mp_av_pool_image_hw_map(struct AVBufferRef *hw_frames_ctx,
|
|
|
|
struct mp_image *src)
|
|
|
|
{
|
|
|
|
AVFrame *dst_frame = av_frame_alloc();
|
|
|
|
if (!dst_frame)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dst_frame->format = ((AVHWFramesContext*)hw_frames_ctx->data)->format;
|
|
|
|
dst_frame->hw_frames_ctx = av_buffer_ref(hw_frames_ctx);
|
|
|
|
|
|
|
|
AVFrame *src_frame = mp_image_to_av_frame(src);
|
|
|
|
if (av_hwframe_map(dst_frame, src_frame, 0) < 0) {
|
|
|
|
av_frame_free(&src_frame);
|
|
|
|
av_frame_free(&dst_frame);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
av_frame_free(&src_frame);
|
|
|
|
|
|
|
|
struct mp_image *dst = mp_image_from_av_frame(dst_frame);
|
|
|
|
av_frame_free(&dst_frame);
|
|
|
|
if (!dst)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mp_image_copy_attributes(dst, src);
|
|
|
|
return dst;
|
|
|
|
}
|