2010-01-30 16:57:40 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2010-01-30 16:57:40 +00:00
|
|
|
*
|
video/fmt-conversion, img_format: change license to LGPL
The problem with fmt-conversion.h is that "lucabe", who disagreed with
LGPL, originally wrote it. But it was actually rewritten by "reimar"
later. The original switch statement was replaced with a lookup table.
No code other than the imgfmt2pixfmt() function signature survives.
Neither the format pairs (PIXFMT<->IMGFMT), nor the concept of mapping
them, can be copyrighted.
So changing the license should be fine, because reimar and all other
authors involved with the new code agreed to LGPL.
We also don't consider format pairs added later as copyrightable.
(The direct-mapping idea mentioned in the "Copyright" file seems
attractive, and I might implement in later anyway.)
Likewise, there might be some format names added to img_format.h, which
are not covered by relicensing agreements. These all affect "later"
additions, and they follow either the FFmpeg PIXFMT naming or some other
pre-existing logic, so this should be fine.
2017-06-18 13:12:11 +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.
|
2010-01-30 16:57:40 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2010-01-30 16:57:40 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
video/fmt-conversion, img_format: change license to LGPL
The problem with fmt-conversion.h is that "lucabe", who disagreed with
LGPL, originally wrote it. But it was actually rewritten by "reimar"
later. The original switch statement was replaced with a lookup table.
No code other than the imgfmt2pixfmt() function signature survives.
Neither the format pairs (PIXFMT<->IMGFMT), nor the concept of mapping
them, can be copyrighted.
So changing the license should be fine, because reimar and all other
authors involved with the new code agreed to LGPL.
We also don't consider format pairs added later as copyrightable.
(The direct-mapping idea mentioned in the "Copyright" file seems
attractive, and I might implement in later anyway.)
Likewise, there might be some format names added to img_format.h, which
are not covered by relicensing agreements. These all affect "later"
additions, and they follow either the FFmpeg PIXFMT naming or some other
pre-existing logic, so this should be fine.
2017-06-18 13:12:11 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2010-01-30 16:57:40 +00:00
|
|
|
*
|
video/fmt-conversion, img_format: change license to LGPL
The problem with fmt-conversion.h is that "lucabe", who disagreed with
LGPL, originally wrote it. But it was actually rewritten by "reimar"
later. The original switch statement was replaced with a lookup table.
No code other than the imgfmt2pixfmt() function signature survives.
Neither the format pairs (PIXFMT<->IMGFMT), nor the concept of mapping
them, can be copyrighted.
So changing the license should be fine, because reimar and all other
authors involved with the new code agreed to LGPL.
We also don't consider format pairs added later as copyrightable.
(The direct-mapping idea mentioned in the "Copyright" file seems
attractive, and I might implement in later anyway.)
Likewise, there might be some format names added to img_format.h, which
are not covered by relicensing agreements. These all affect "later"
additions, and they follow either the FFmpeg PIXFMT naming or some other
pre-existing logic, so this should be fine.
2017-06-18 13:12:11 +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/>.
|
2010-01-30 16:57:40 +00:00
|
|
|
*/
|
|
|
|
|
2012-12-31 00:58:25 +00:00
|
|
|
#include <assert.h>
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
#include <string.h>
|
2012-12-31 00:58:25 +00:00
|
|
|
|
vf_scale: replace ancient fallback image format selection
If video output and VO don't support the same format, a conversion
filter needs to be insert. Since a VO can support multiple formats, and
the filter chain also can deal with multiple formats, you basically have
to pick from a huge matrix of possible conversions.
The old MPlayer code had a quite naive algorithm: it first checked
whether any conversion from the list of preferred conversions matched,
and if not, it was falling back on checking a hardcoded list of output
formats (more or less sorted by quality). This had some unintended side-
effects, like not using obvious "replacement" formats, selecting the
wrong colorspace, selecting a bit depth that is too high or too low, and
more.
Use avcodec_find_best_pix_fmt_of_list() provided by FFmpeg instead. This
function was made for this purpose, and should select the "best" format.
Libav provides a similar function, but with a different name - there is
a function with the same name in FFmpeg, but it has different semantics
(I'm not sure if Libav or FFmpeg fucked up here).
This also removes handling of VFCAP_CSP_SUPPORTED vs.
VFCAP_CSP_SUPPORTED_BY_HW, which has no meaning anymore, except possibly
for filter chains with multiple scale filters.
Fixes #1494.
2015-01-21 17:33:47 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
2012-12-31 00:58:25 +00:00
|
|
|
#include <libavutil/pixfmt.h>
|
|
|
|
#include <libavutil/pixdesc.h>
|
|
|
|
|
2015-09-10 20:13:52 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/img_format.h"
|
2012-12-31 00:58:25 +00:00
|
|
|
#include "video/mp_image.h"
|
|
|
|
#include "video/fmt-conversion.h"
|
2001-10-31 22:04:28 +00:00
|
|
|
|
2014-04-14 18:19:44 +00:00
|
|
|
struct mp_imgfmt_entry {
|
|
|
|
const char *name;
|
|
|
|
int fmt;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct mp_imgfmt_entry mp_imgfmt_list[] = {
|
2014-05-22 18:55:17 +00:00
|
|
|
// not in ffmpeg
|
2014-06-14 08:06:23 +00:00
|
|
|
{"vdpau_output", IMGFMT_VDPAU_OUTPUT},
|
2014-04-14 18:19:44 +00:00
|
|
|
// FFmpeg names have an annoying "_vld" suffix
|
2015-07-11 15:21:39 +00:00
|
|
|
{"videotoolbox", IMGFMT_VIDEOTOOLBOX},
|
2014-06-14 08:06:23 +00:00
|
|
|
{"vaapi", IMGFMT_VAAPI},
|
|
|
|
{"none", 0},
|
2012-08-28 21:58:48 +00:00
|
|
|
{0}
|
2012-08-21 17:20:36 +00:00
|
|
|
};
|
|
|
|
|
2014-04-14 18:19:44 +00:00
|
|
|
char **mp_imgfmt_name_list(void)
|
|
|
|
{
|
|
|
|
int count = IMGFMT_END - IMGFMT_START;
|
|
|
|
char **list = talloc_zero_array(NULL, char *, count + 1);
|
|
|
|
int num = 0;
|
|
|
|
for (int n = IMGFMT_START; n < IMGFMT_END; n++) {
|
|
|
|
const char *name = mp_imgfmt_to_name(n);
|
2017-06-18 11:46:34 +00:00
|
|
|
if (strcmp(name, "unknown") != 0)
|
2014-06-14 07:58:48 +00:00
|
|
|
list[num++] = talloc_strdup(list, name);
|
2014-04-14 18:19:44 +00:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-06-18 11:58:42 +00:00
|
|
|
int mp_imgfmt_from_name(bstr name)
|
2012-08-21 17:20:36 +00:00
|
|
|
{
|
2013-01-17 15:10:26 +00:00
|
|
|
int img_fmt = 0;
|
2014-04-14 18:19:44 +00:00
|
|
|
for (const struct mp_imgfmt_entry *p = mp_imgfmt_list; p->name; ++p) {
|
|
|
|
if (bstr_equals0(name, p->name)) {
|
2013-01-17 15:10:26 +00:00
|
|
|
img_fmt = p->fmt;
|
|
|
|
break;
|
2012-08-28 21:58:48 +00:00
|
|
|
}
|
2012-08-21 17:20:36 +00:00
|
|
|
}
|
2013-01-17 15:10:26 +00:00
|
|
|
if (!img_fmt) {
|
|
|
|
char *t = bstrdup0(NULL, name);
|
|
|
|
img_fmt = pixfmt2imgfmt(av_get_pix_fmt(t));
|
|
|
|
talloc_free(t);
|
|
|
|
}
|
|
|
|
return img_fmt;
|
2012-08-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 07:58:48 +00:00
|
|
|
char *mp_imgfmt_to_name_buf(char *buf, size_t buf_size, int fmt)
|
2012-08-21 17:20:36 +00:00
|
|
|
{
|
2014-06-14 07:58:48 +00:00
|
|
|
const char *name = NULL;
|
2014-04-14 18:19:44 +00:00
|
|
|
const struct mp_imgfmt_entry *p = mp_imgfmt_list;
|
|
|
|
for (; p->fmt; p++) {
|
2014-06-14 07:58:48 +00:00
|
|
|
if (p->name && p->fmt == fmt) {
|
|
|
|
name = p->name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!name) {
|
|
|
|
const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(imgfmt2pixfmt(fmt));
|
|
|
|
if (pixdesc)
|
|
|
|
name = pixdesc->name;
|
2012-08-21 17:20:36 +00:00
|
|
|
}
|
2014-06-14 07:58:48 +00:00
|
|
|
if (!name)
|
|
|
|
name = "unknown";
|
|
|
|
snprintf(buf, buf_size, "%s", name);
|
|
|
|
int len = strlen(buf);
|
|
|
|
if (len > 2 && buf[len - 2] == MP_SELECT_LE_BE('l', 'b') && buf[len - 1] == 'e')
|
|
|
|
buf[len - 2] = '\0';
|
|
|
|
return buf;
|
2012-08-21 17:20:36 +00:00
|
|
|
}
|
2012-12-31 00:58:25 +00:00
|
|
|
|
2014-05-22 18:55:17 +00:00
|
|
|
static struct mp_imgfmt_desc mp_only_imgfmt_desc(int mpfmt)
|
|
|
|
{
|
|
|
|
switch (mpfmt) {
|
|
|
|
case IMGFMT_VDPAU_OUTPUT:
|
|
|
|
return (struct mp_imgfmt_desc) {
|
|
|
|
.id = mpfmt,
|
|
|
|
.avformat = AV_PIX_FMT_NONE,
|
2014-11-04 21:26:31 +00:00
|
|
|
.flags = MP_IMGFLAG_BE | MP_IMGFLAG_LE | MP_IMGFLAG_RGB |
|
video: remove d3d11 video processor use from OpenGL interop
We now have a video filter that uses the d3d11 video processor, so it
makes no sense to have one in the VO interop code. The VO uses it for
formats not directly supported by ANGLE (so the video data is converted
to a RGB texture, which ANGLE can take in).
Change this so that the video filter is automatically inserted if
needed. Move the code that maps RGB surfaces to its own inteorp backend.
Add a bunch of new image formats, which are used to enforce the new
constraints, and to automatically insert the filter only when needed.
The added vf mechanism to auto-insert the d3d11vpp filter is very dumb
and primitive, and will work only for this specific purpose. The format
negotiation mechanism in the filter chain is generally not very pretty,
and mostly broken as well. (libavfilter has a different mechanism, and
these mechanisms don't match well, so vf_lavfi uses some sort of hack.
It only works because hwaccel and non-hwaccel formats are strictly
separated.)
The RGB interop is now only used with older ANGLE versions. The only
reason I'm keeping it is because it's relatively isolated (uses only
existing mechanisms and adds no new concepts), and because I want to be
able to compare the behavior of the old code with the new one for
testing. It will be removed eventually.
If ANGLE has NV12 interop, P010 is now handled by converting to NV12
with the video processor, instead of converting it to RGB and using the
old mechanism to import that as a texture.
2016-05-29 15:13:22 +00:00
|
|
|
MP_IMGFLAG_HWACCEL,
|
|
|
|
};
|
2014-05-22 18:55:17 +00:00
|
|
|
}
|
|
|
|
return (struct mp_imgfmt_desc) {0};
|
|
|
|
}
|
|
|
|
|
2013-11-05 20:59:26 +00:00
|
|
|
struct mp_imgfmt_desc mp_imgfmt_get_desc(int mpfmt)
|
2012-12-31 00:58:25 +00:00
|
|
|
{
|
2013-11-29 16:39:57 +00:00
|
|
|
enum AVPixelFormat fmt = imgfmt2pixfmt(mpfmt);
|
|
|
|
const AVPixFmtDescriptor *pd = av_pix_fmt_desc_get(fmt);
|
video: passthrough unknown AVPixelFormats
This is a rather radical change: instead of maintaining a whitelist of
FFmpeg formats we support, we automatically support all formats.
In general, a format which doesn't have an explicit IMGFMT_* name will
be converted to a known format through libswscale, or will be handled
by code which can treat pixel formats in a generic way using the pixel
format description, like vo_opengl.
AV_PIX_FMT_UYYVYY411 is a special-case. It's packed YUV with chroma
subsampling by 4 in both directions. Its component order is documented
as "Cb Y0 Y1 Cr Y2 Y3", meaning there's one UV sample for 4 Y samples.
This means each pixel uses 1.5 bytes (4 pixels have 1 UV sample, so
4 bytes + 2 bytes). FFmpeg can actually handle this format with its
generic mechanism in an extremely awkward way, but it doesn't work for
us. Blacklist it, and hope no similar formats will be added in the
future.
Currently, the AV_PIX_FMT_*s allowed are limited to a numeric value of
500. More is not allowed, and there are some fixed size arrays that need
to contain any possible format (look for IMGFMT_END dependencies).
We could have this simpler by replacing IMGFMT_* with AV_PIX_FMT_*
through the whole codebase. But for now, this is better, because we
can compensate for formats missing in Libav or older FFmpeg versions,
like AV_PIX_FMT_RGB0 and others.
2014-11-04 22:32:02 +00:00
|
|
|
if (!pd || pd->nb_components > 4 || fmt == AV_PIX_FMT_NONE ||
|
|
|
|
fmt == AV_PIX_FMT_UYYVYY411)
|
2014-05-22 18:55:17 +00:00
|
|
|
return mp_only_imgfmt_desc(mpfmt);
|
2017-08-15 15:00:35 +00:00
|
|
|
enum mp_component_type is_uint =
|
2017-09-30 12:19:34 +00:00
|
|
|
mp_imgfmt_get_component_type(mpfmt) == MP_COMPONENT_TYPE_UINT;
|
2013-11-05 20:59:26 +00:00
|
|
|
|
2012-12-31 00:58:25 +00:00
|
|
|
struct mp_imgfmt_desc desc = {
|
|
|
|
.id = mpfmt,
|
|
|
|
.avformat = fmt,
|
|
|
|
.chroma_xs = pd->log2_chroma_w,
|
|
|
|
.chroma_ys = pd->log2_chroma_h,
|
|
|
|
};
|
|
|
|
|
|
|
|
int planedepth[4] = {0};
|
2013-12-08 22:38:35 +00:00
|
|
|
int el_size = (pd->flags & AV_PIX_FMT_FLAG_BITSTREAM) ? 1 : 8;
|
2014-11-04 22:32:02 +00:00
|
|
|
bool need_endian = false; // single component is spread over >1 bytes
|
2016-01-07 15:00:38 +00:00
|
|
|
int shift = -1; // shift for all components, or -1 if not uniform
|
2012-12-31 00:58:25 +00:00
|
|
|
for (int c = 0; c < pd->nb_components; c++) {
|
|
|
|
AVComponentDescriptor d = pd->comp[c];
|
|
|
|
// multiple components per plane -> Y is definitive, ignore chroma
|
|
|
|
if (!desc.bpp[d.plane])
|
2016-12-07 18:44:29 +00:00
|
|
|
desc.bpp[d.plane] = d.step * el_size;
|
|
|
|
planedepth[d.plane] += d.depth;
|
|
|
|
need_endian |= (d.depth + d.shift) > 8;
|
2015-09-10 20:13:52 +00:00
|
|
|
if (c == 0)
|
2016-12-07 18:44:29 +00:00
|
|
|
desc.component_bits = d.depth;
|
|
|
|
if (d.depth != desc.component_bits)
|
2015-01-21 18:29:18 +00:00
|
|
|
desc.component_bits = 0;
|
2016-01-07 15:00:38 +00:00
|
|
|
if (c == 0)
|
|
|
|
shift = d.shift;
|
|
|
|
if (shift != d.shift)
|
|
|
|
shift = -1;
|
2012-12-31 00:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int p = 0; p < 4; p++) {
|
|
|
|
if (desc.bpp[p])
|
|
|
|
desc.num_planes++;
|
|
|
|
}
|
|
|
|
|
2014-11-04 22:32:02 +00:00
|
|
|
desc.plane_bits = planedepth[0];
|
|
|
|
|
|
|
|
// Check whether any components overlap other components (per plane).
|
|
|
|
// We're cheating/simplifying here: we assume that this happens if a shift
|
|
|
|
// is set - which is wrong in general (could be needed for padding, instead
|
2016-01-07 15:21:26 +00:00
|
|
|
// of overlapping bits of another component - use the "< 8" test to exclude
|
|
|
|
// "normal" formats which use this for padding, like p010).
|
|
|
|
// Needed for rgb444le/be.
|
2014-11-04 22:32:02 +00:00
|
|
|
bool component_byte_overlap = false;
|
|
|
|
for (int c = 0; c < pd->nb_components; c++) {
|
|
|
|
AVComponentDescriptor d = pd->comp[c];
|
2016-01-07 15:21:26 +00:00
|
|
|
component_byte_overlap |= d.shift > 0 && planedepth[d.plane] > 8 &&
|
|
|
|
desc.component_bits < 8;
|
2014-11-04 22:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If every component sits in its own byte, or all components are within
|
|
|
|
// a single byte, no endian-dependent access is needed. If components
|
|
|
|
// stride bytes (like with packed 2 byte RGB formats), endian-dependent
|
|
|
|
// access is needed.
|
|
|
|
need_endian |= component_byte_overlap;
|
|
|
|
|
|
|
|
if (!need_endian) {
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
desc.flags |= MP_IMGFLAG_LE | MP_IMGFLAG_BE;
|
|
|
|
} else {
|
2013-12-08 22:38:35 +00:00
|
|
|
desc.flags |= (pd->flags & AV_PIX_FMT_FLAG_BE)
|
|
|
|
? MP_IMGFLAG_BE : MP_IMGFLAG_LE;
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
}
|
2012-12-31 00:58:25 +00:00
|
|
|
|
2017-02-21 09:35:38 +00:00
|
|
|
if ((pd->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
|
|
|
|
desc.flags |= MP_IMGFLAG_HWACCEL;
|
|
|
|
} else if (fmt == AV_PIX_FMT_XYZ12LE || fmt == AV_PIX_FMT_XYZ12BE) {
|
2013-05-01 21:58:48 +00:00
|
|
|
desc.flags |= MP_IMGFLAG_XYZ;
|
2013-12-08 22:38:35 +00:00
|
|
|
} else if (!(pd->flags & AV_PIX_FMT_FLAG_RGB) &&
|
|
|
|
fmt != AV_PIX_FMT_MONOBLACK &&
|
|
|
|
fmt != AV_PIX_FMT_PAL8)
|
2012-12-31 00:58:25 +00:00
|
|
|
{
|
|
|
|
desc.flags |= MP_IMGFLAG_YUV;
|
2012-12-24 00:10:57 +00:00
|
|
|
} else {
|
|
|
|
desc.flags |= MP_IMGFLAG_RGB;
|
2012-12-31 00:58:25 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 22:38:35 +00:00
|
|
|
if (pd->flags & AV_PIX_FMT_FLAG_ALPHA)
|
2012-12-31 00:58:25 +00:00
|
|
|
desc.flags |= MP_IMGFLAG_ALPHA;
|
|
|
|
|
2013-11-05 20:59:26 +00:00
|
|
|
if (mpfmt >= IMGFMT_RGB0_START && mpfmt <= IMGFMT_RGB0_END)
|
|
|
|
desc.flags &= ~MP_IMGFLAG_ALPHA;
|
|
|
|
|
2012-12-26 22:12:30 +00:00
|
|
|
if (desc.num_planes == pd->nb_components)
|
2012-12-31 00:58:25 +00:00
|
|
|
desc.flags |= MP_IMGFLAG_PLANAR;
|
|
|
|
|
2013-12-08 22:38:35 +00:00
|
|
|
if (!(pd->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
|
|
|
|
!(pd->flags & AV_PIX_FMT_FLAG_BITSTREAM))
|
|
|
|
{
|
2013-01-14 17:37:17 +00:00
|
|
|
desc.flags |= MP_IMGFLAG_BYTE_ALIGNED;
|
|
|
|
for (int p = 0; p < desc.num_planes; p++)
|
|
|
|
desc.bytes[p] = desc.bpp[p] / 8;
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:01:07 +00:00
|
|
|
if (pd->flags & AV_PIX_FMT_FLAG_PAL)
|
2013-12-01 19:45:44 +00:00
|
|
|
desc.flags |= MP_IMGFLAG_PAL;
|
|
|
|
|
2015-10-18 16:37:24 +00:00
|
|
|
if ((desc.flags & (MP_IMGFLAG_YUV | MP_IMGFLAG_RGB))
|
2015-11-01 13:11:43 +00:00
|
|
|
&& (desc.flags & MP_IMGFLAG_BYTE_ALIGNED)
|
2016-01-07 15:00:38 +00:00
|
|
|
&& !(pd->flags & AV_PIX_FMT_FLAG_PAL)
|
2016-01-07 15:21:26 +00:00
|
|
|
&& !component_byte_overlap
|
2017-08-15 15:00:35 +00:00
|
|
|
&& shift >= 0 && is_uint)
|
2013-01-14 17:37:17 +00:00
|
|
|
{
|
2012-12-31 00:58:25 +00:00
|
|
|
bool same_depth = true;
|
|
|
|
for (int p = 0; p < desc.num_planes; p++) {
|
|
|
|
same_depth &= planedepth[p] == planedepth[0] &&
|
|
|
|
desc.bpp[p] == desc.bpp[0];
|
|
|
|
}
|
2015-10-18 16:37:24 +00:00
|
|
|
if (same_depth && pd->nb_components == desc.num_planes) {
|
|
|
|
if (desc.flags & MP_IMGFLAG_YUV) {
|
|
|
|
desc.flags |= MP_IMGFLAG_YUV_P;
|
|
|
|
} else {
|
|
|
|
desc.flags |= MP_IMGFLAG_RGB_P;
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 15:21:26 +00:00
|
|
|
if (pd->nb_components == 3 && desc.num_planes == 2 &&
|
|
|
|
planedepth[1] == planedepth[0] * 2 &&
|
|
|
|
desc.bpp[1] == desc.bpp[0] * 2 &&
|
|
|
|
(desc.flags & MP_IMGFLAG_YUV))
|
|
|
|
{
|
|
|
|
|
|
|
|
desc.flags |= MP_IMGFLAG_YUV_NV;
|
2016-01-07 15:54:01 +00:00
|
|
|
if (fmt == AV_PIX_FMT_NV21)
|
2016-01-07 15:21:26 +00:00
|
|
|
desc.flags |= MP_IMGFLAG_YUV_NV_SWAP;
|
|
|
|
}
|
|
|
|
if (desc.flags & (MP_IMGFLAG_YUV_P | MP_IMGFLAG_RGB_P | MP_IMGFLAG_YUV_NV))
|
|
|
|
desc.component_bits += shift;
|
2012-12-31 00:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int p = 0; p < desc.num_planes; p++) {
|
|
|
|
desc.xs[p] = (p == 1 || p == 2) ? desc.chroma_xs : 0;
|
|
|
|
desc.ys[p] = (p == 1 || p == 2) ? desc.chroma_ys : 0;
|
|
|
|
}
|
|
|
|
|
2012-12-25 21:29:49 +00:00
|
|
|
desc.align_x = 1 << desc.chroma_xs;
|
|
|
|
desc.align_y = 1 << desc.chroma_ys;
|
|
|
|
|
|
|
|
if ((desc.bpp[0] % 8) != 0)
|
|
|
|
desc.align_x = 8 / desc.bpp[0]; // expect power of 2
|
|
|
|
|
2017-02-21 09:35:38 +00:00
|
|
|
if (desc.flags & MP_IMGFLAG_HWACCEL) {
|
|
|
|
desc.component_bits = 0;
|
|
|
|
desc.plane_bits = 0;
|
2015-01-10 18:11:28 +00:00
|
|
|
}
|
2014-11-04 21:26:31 +00:00
|
|
|
|
2012-12-31 00:58:25 +00:00
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:51:37 +00:00
|
|
|
static bool validate_regular_imgfmt(const struct mp_regular_imgfmt *fmt)
|
|
|
|
{
|
|
|
|
bool present[MP_NUM_COMPONENTS] = {0};
|
|
|
|
int n_comp = 0;
|
|
|
|
|
|
|
|
for (int n = 0; n < fmt->num_planes; n++) {
|
|
|
|
const struct mp_regular_imgfmt_plane *plane = &fmt->planes[n];
|
|
|
|
n_comp += plane->num_components;
|
|
|
|
if (n_comp > MP_NUM_COMPONENTS)
|
|
|
|
return false;
|
|
|
|
if (!plane->num_components)
|
|
|
|
return false; // no empty planes in between allowed
|
|
|
|
|
|
|
|
bool pad_only = true;
|
|
|
|
int chroma_luma = 0; // luma: 1, chroma: 2, both: 3
|
|
|
|
for (int i = 0; i < plane->num_components; i++) {
|
|
|
|
int comp = plane->components[i];
|
|
|
|
if (comp > MP_NUM_COMPONENTS)
|
|
|
|
return false;
|
|
|
|
if (comp == 0)
|
|
|
|
continue;
|
|
|
|
pad_only = false;
|
|
|
|
if (present[comp - 1])
|
|
|
|
return false; // no duplicates
|
|
|
|
present[comp - 1] = true;
|
|
|
|
chroma_luma |= (comp == 2 || comp == 3) ? 2 : 1;
|
|
|
|
}
|
|
|
|
if (pad_only)
|
|
|
|
return false; // no planes with only padding allowed
|
|
|
|
if ((fmt->chroma_w > 1 || fmt->chroma_h > 1) && chroma_luma == 3)
|
|
|
|
return false; // separate chroma/luma planes required
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(present[0] || present[3]) || // at least component 1 or alpha needed
|
|
|
|
(present[1] && !present[0]) || // component 2 requires component 1
|
|
|
|
(present[2] && !present[1])) // component 3 requires component 2
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum mp_csp mp_imgfmt_get_forced_csp(int imgfmt)
|
|
|
|
{
|
2017-10-09 13:15:53 +00:00
|
|
|
enum AVPixelFormat pixfmt = imgfmt2pixfmt(imgfmt);
|
|
|
|
const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(pixfmt);
|
2017-06-29 18:51:37 +00:00
|
|
|
|
|
|
|
// FFmpeg does not provide a flag for XYZ, so this is the best we can do.
|
|
|
|
if (pixdesc && strncmp(pixdesc->name, "xyz", 3) == 0)
|
|
|
|
return MP_CSP_XYZ;
|
|
|
|
|
|
|
|
if (pixdesc && (pixdesc->flags & AV_PIX_FMT_FLAG_RGB))
|
|
|
|
return MP_CSP_RGB;
|
|
|
|
|
2017-10-09 13:15:53 +00:00
|
|
|
if (pixfmt == AV_PIX_FMT_PAL8 || pixfmt == AV_PIX_FMT_MONOBLACK)
|
|
|
|
return MP_CSP_RGB;
|
|
|
|
|
2017-06-29 18:51:37 +00:00
|
|
|
return MP_CSP_AUTO;
|
|
|
|
}
|
|
|
|
|
2017-08-15 15:00:35 +00:00
|
|
|
enum mp_component_type mp_imgfmt_get_component_type(int imgfmt)
|
|
|
|
{
|
|
|
|
const AVPixFmtDescriptor *pixdesc =
|
|
|
|
av_pix_fmt_desc_get(imgfmt2pixfmt(imgfmt));
|
|
|
|
|
|
|
|
if (!pixdesc)
|
|
|
|
return MP_COMPONENT_TYPE_UNKNOWN;
|
|
|
|
|
2017-11-06 16:14:01 +00:00
|
|
|
#if LIBAVUTIL_VERSION_MICRO >= 100
|
2017-08-15 15:00:35 +00:00
|
|
|
if (pixdesc->flags & AV_PIX_FMT_FLAG_FLOAT)
|
|
|
|
return MP_COMPONENT_TYPE_FLOAT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return MP_COMPONENT_TYPE_UINT;
|
|
|
|
}
|
|
|
|
|
2017-06-29 18:51:37 +00:00
|
|
|
static bool is_native_endian(const AVPixFmtDescriptor *pixdesc)
|
|
|
|
{
|
|
|
|
enum AVPixelFormat pixfmt = av_pix_fmt_desc_get_id(pixdesc);
|
|
|
|
enum AVPixelFormat other = av_pix_fmt_swap_endianness(pixfmt);
|
|
|
|
if (other == AV_PIX_FMT_NONE || other == pixfmt)
|
|
|
|
return true; // no endian nonsense
|
|
|
|
bool is_le = *(char *)&(uint32_t){1};
|
|
|
|
return pixdesc && (is_le != !!(pixdesc->flags & AV_PIX_FMT_FLAG_BE));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_get_regular_imgfmt(struct mp_regular_imgfmt *dst, int imgfmt)
|
|
|
|
{
|
|
|
|
struct mp_regular_imgfmt res = {0};
|
|
|
|
|
|
|
|
const AVPixFmtDescriptor *pixdesc =
|
|
|
|
av_pix_fmt_desc_get(imgfmt2pixfmt(imgfmt));
|
|
|
|
|
|
|
|
if (!pixdesc || (pixdesc->flags & AV_PIX_FMT_FLAG_BITSTREAM) ||
|
|
|
|
(pixdesc->flags & AV_PIX_FMT_FLAG_HWACCEL) ||
|
|
|
|
(pixdesc->flags & AV_PIX_FMT_FLAG_PAL) ||
|
|
|
|
pixdesc->nb_components < 1 ||
|
|
|
|
pixdesc->nb_components > MP_NUM_COMPONENTS ||
|
|
|
|
!is_native_endian(pixdesc))
|
|
|
|
return false;
|
|
|
|
|
2017-08-15 15:00:35 +00:00
|
|
|
res.component_type = mp_imgfmt_get_component_type(imgfmt);
|
|
|
|
if (!res.component_type)
|
|
|
|
return false;
|
|
|
|
|
2017-06-29 18:51:37 +00:00
|
|
|
const AVComponentDescriptor *comp0 = &pixdesc->comp[0];
|
|
|
|
|
|
|
|
int depth = comp0->depth + comp0->shift;
|
|
|
|
if (depth < 1 || depth > 64)
|
|
|
|
return false;
|
|
|
|
res.component_size = (depth + 7) / 8;
|
|
|
|
|
|
|
|
for (int n = 0; n < pixdesc->nb_components; n++) {
|
|
|
|
const AVComponentDescriptor *comp = &pixdesc->comp[n];
|
|
|
|
|
|
|
|
if (comp->plane < 0 || comp->plane >= MP_MAX_PLANES)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
res.num_planes = MPMAX(res.num_planes, comp->plane + 1);
|
|
|
|
|
|
|
|
// We support uniform depth only.
|
|
|
|
if (comp->depth != comp0->depth || comp->shift != comp0->shift)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Uniform component size; even the padding must have same size.
|
|
|
|
int ncomp = comp->step / res.component_size;
|
|
|
|
if (!ncomp || ncomp * res.component_size != comp->step)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct mp_regular_imgfmt_plane *plane = &res.planes[comp->plane];
|
|
|
|
|
|
|
|
if (plane->num_components && plane->num_components != ncomp)
|
|
|
|
return false;
|
|
|
|
plane->num_components = ncomp;
|
|
|
|
|
|
|
|
int pos = comp->offset / res.component_size;
|
|
|
|
if (pos < 0 || pos >= ncomp || ncomp > MP_NUM_COMPONENTS)
|
|
|
|
return false;
|
|
|
|
if (plane->components[pos])
|
|
|
|
return false;
|
|
|
|
plane->components[pos] = n + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure alpha is always component 4.
|
|
|
|
if (pixdesc->nb_components == 2 && (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA)) {
|
|
|
|
for (int n = 0; n < res.num_planes; n++) {
|
|
|
|
for (int i = 0; i < res.planes[n].num_components; i++) {
|
|
|
|
if (res.planes[n].components[i] == 2)
|
|
|
|
res.planes[n].components[i] = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.component_pad = comp0->depth - res.component_size * 8;
|
|
|
|
if (comp0->shift) {
|
|
|
|
// We support padding only on 1 side.
|
|
|
|
if (comp0->shift + comp0->depth != res.component_size * 8)
|
|
|
|
return false;
|
|
|
|
res.component_pad = -res.component_pad;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.chroma_w = 1 << pixdesc->log2_chroma_w;
|
|
|
|
res.chroma_h = 1 << pixdesc->log2_chroma_h;
|
|
|
|
|
2017-11-06 16:14:01 +00:00
|
|
|
#if LIBAVUTIL_VERSION_MICRO >= 100
|
2017-08-15 15:03:35 +00:00
|
|
|
if (pixdesc->flags & AV_PIX_FMT_FLAG_BAYER)
|
|
|
|
return false; // it's satan himself
|
|
|
|
#endif
|
2017-06-29 18:51:37 +00:00
|
|
|
|
|
|
|
if (!validate_regular_imgfmt(&res))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*dst = res;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-24 15:42:21 +00:00
|
|
|
// Find a format that has the given flags set with the following configuration.
|
|
|
|
int mp_imgfmt_find(int xs, int ys, int planes, int component_bits, int flags)
|
2012-12-25 13:54:42 +00:00
|
|
|
{
|
|
|
|
for (int n = IMGFMT_START + 1; n < IMGFMT_END; n++) {
|
|
|
|
struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(n);
|
2015-12-24 15:42:21 +00:00
|
|
|
if (desc.id && ((desc.flags & flags) == flags)) {
|
2012-12-25 13:54:42 +00:00
|
|
|
if (desc.num_planes == planes && desc.chroma_xs == xs &&
|
|
|
|
desc.chroma_ys == ys && desc.plane_bits == component_bits &&
|
|
|
|
(desc.flags & MP_IMGFLAG_NE))
|
|
|
|
return desc.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-04 22:52:29 +00:00
|
|
|
|
vf_scale: replace ancient fallback image format selection
If video output and VO don't support the same format, a conversion
filter needs to be insert. Since a VO can support multiple formats, and
the filter chain also can deal with multiple formats, you basically have
to pick from a huge matrix of possible conversions.
The old MPlayer code had a quite naive algorithm: it first checked
whether any conversion from the list of preferred conversions matched,
and if not, it was falling back on checking a hardcoded list of output
formats (more or less sorted by quality). This had some unintended side-
effects, like not using obvious "replacement" formats, selecting the
wrong colorspace, selecting a bit depth that is too high or too low, and
more.
Use avcodec_find_best_pix_fmt_of_list() provided by FFmpeg instead. This
function was made for this purpose, and should select the "best" format.
Libav provides a similar function, but with a different name - there is
a function with the same name in FFmpeg, but it has different semantics
(I'm not sure if Libav or FFmpeg fucked up here).
This also removes handling of VFCAP_CSP_SUPPORTED vs.
VFCAP_CSP_SUPPORTED_BY_HW, which has no meaning anymore, except possibly
for filter chains with multiple scale filters.
Fixes #1494.
2015-01-21 17:33:47 +00:00
|
|
|
#if LIBAVUTIL_VERSION_MICRO < 100
|
|
|
|
#define avcodec_find_best_pix_fmt_of_list avcodec_find_best_pix_fmt2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Compare the dst image formats, and return the one which can carry more data
|
|
|
|
// (e.g. higher depth, more color components, lower chroma subsampling, etc.),
|
|
|
|
// with respect to what is required to keep most of the src format.
|
|
|
|
// Returns the imgfmt, or 0 on error.
|
|
|
|
int mp_imgfmt_select_best(int dst1, int dst2, int src)
|
|
|
|
{
|
|
|
|
enum AVPixelFormat dst1pxf = imgfmt2pixfmt(dst1);
|
|
|
|
enum AVPixelFormat dst2pxf = imgfmt2pixfmt(dst2);
|
|
|
|
enum AVPixelFormat srcpxf = imgfmt2pixfmt(src);
|
|
|
|
enum AVPixelFormat dstlist[] = {dst1pxf, dst2pxf, AV_PIX_FMT_NONE};
|
2015-01-21 20:49:15 +00:00
|
|
|
return pixfmt2imgfmt(avcodec_find_best_pix_fmt_of_list(dstlist, srcpxf, 1, 0));
|
vf_scale: replace ancient fallback image format selection
If video output and VO don't support the same format, a conversion
filter needs to be insert. Since a VO can support multiple formats, and
the filter chain also can deal with multiple formats, you basically have
to pick from a huge matrix of possible conversions.
The old MPlayer code had a quite naive algorithm: it first checked
whether any conversion from the list of preferred conversions matched,
and if not, it was falling back on checking a hardcoded list of output
formats (more or less sorted by quality). This had some unintended side-
effects, like not using obvious "replacement" formats, selecting the
wrong colorspace, selecting a bit depth that is too high or too low, and
more.
Use avcodec_find_best_pix_fmt_of_list() provided by FFmpeg instead. This
function was made for this purpose, and should select the "best" format.
Libav provides a similar function, but with a different name - there is
a function with the same name in FFmpeg, but it has different semantics
(I'm not sure if Libav or FFmpeg fucked up here).
This also removes handling of VFCAP_CSP_SUPPORTED vs.
VFCAP_CSP_SUPPORTED_BY_HW, which has no meaning anymore, except possibly
for filter chains with multiple scale filters.
Fixes #1494.
2015-01-21 17:33:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 10:37:21 +00:00
|
|
|
// Same as mp_imgfmt_select_best(), but with a list of dst formats.
|
|
|
|
int mp_imgfmt_select_best_list(int *dst, int num_dst, int src)
|
|
|
|
{
|
|
|
|
int best = 0;
|
|
|
|
for (int n = 0; n < num_dst; n++)
|
|
|
|
best = best ? mp_imgfmt_select_best(best, dst[n], src) : dst[n];
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2014-11-04 22:52:29 +00:00
|
|
|
#if 0
|
|
|
|
|
|
|
|
#include <libavutil/frame.h>
|
|
|
|
#include "sws_utils.h"
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const AVPixFmtDescriptor *avd = av_pix_fmt_desc_next(NULL);
|
|
|
|
for (; avd; avd = av_pix_fmt_desc_next(avd)) {
|
|
|
|
enum AVPixelFormat fmt = av_pix_fmt_desc_get_id(avd);
|
|
|
|
if (fmt == AV_PIX_FMT_YUVJ420P || fmt == AV_PIX_FMT_YUVJ422P ||
|
|
|
|
fmt == AV_PIX_FMT_YUVJ444P || fmt == AV_PIX_FMT_YUVJ440P)
|
|
|
|
continue;
|
|
|
|
printf("%s (%d)", avd->name, (int)fmt);
|
|
|
|
int mpfmt = pixfmt2imgfmt(fmt);
|
|
|
|
bool generic = mpfmt >= IMGFMT_AVPIXFMT_START &&
|
|
|
|
mpfmt < IMGFMT_AVPIXFMT_END;
|
|
|
|
printf(" mp=%d%s\n ", mpfmt, generic ? " [GENERIC]" : "");
|
|
|
|
struct mp_imgfmt_desc d = mp_imgfmt_get_desc(mpfmt);
|
|
|
|
if (d.id)
|
|
|
|
assert(d.avformat == fmt);
|
|
|
|
#define FLAG(t, c) if (d.flags & (t)) printf("[%s]", c);
|
|
|
|
FLAG(MP_IMGFLAG_BYTE_ALIGNED, "BA")
|
|
|
|
FLAG(MP_IMGFLAG_ALPHA, "a")
|
|
|
|
FLAG(MP_IMGFLAG_PLANAR, "P")
|
|
|
|
FLAG(MP_IMGFLAG_YUV_P, "YUVP")
|
2016-01-07 15:21:26 +00:00
|
|
|
FLAG(MP_IMGFLAG_YUV_NV, "NV")
|
|
|
|
FLAG(MP_IMGFLAG_YUV_NV_SWAP, "NVSWAP")
|
2014-11-04 22:52:29 +00:00
|
|
|
FLAG(MP_IMGFLAG_YUV, "yuv")
|
|
|
|
FLAG(MP_IMGFLAG_RGB, "rgb")
|
|
|
|
FLAG(MP_IMGFLAG_XYZ, "xyz")
|
|
|
|
FLAG(MP_IMGFLAG_LE, "le")
|
|
|
|
FLAG(MP_IMGFLAG_BE, "be")
|
|
|
|
FLAG(MP_IMGFLAG_PAL, "pal")
|
|
|
|
FLAG(MP_IMGFLAG_HWACCEL, "hw")
|
2017-06-29 18:51:37 +00:00
|
|
|
int fcsp = mp_imgfmt_get_forced_csp(mpfmt);
|
|
|
|
if (fcsp)
|
|
|
|
printf(" fcsp=%d", fcsp);
|
2017-08-15 15:00:35 +00:00
|
|
|
printf(" ctype=%d", mp_imgfmt_get_component_type(mpfmt));
|
2014-11-04 22:52:29 +00:00
|
|
|
printf("\n");
|
2015-01-21 18:29:18 +00:00
|
|
|
printf(" planes=%d, chroma=%d:%d align=%d:%d bits=%d cbits=%d\n",
|
|
|
|
d.num_planes, d.chroma_xs, d.chroma_ys, d.align_x, d.align_y,
|
|
|
|
d.plane_bits, d.component_bits);
|
2017-06-30 16:38:23 +00:00
|
|
|
printf(" planes=%d, chroma=%d:%d align=%d:%d bits=%d cbits=%d\n",
|
2016-01-07 15:21:26 +00:00
|
|
|
d.num_planes, d.chroma_xs, d.chroma_ys, d.align_x, d.align_y,
|
2017-06-30 16:38:23 +00:00
|
|
|
d.plane_bits, d.component_bits);
|
2014-11-04 22:52:29 +00:00
|
|
|
printf(" {");
|
|
|
|
for (int n = 0; n < MP_MAX_PLANES; n++)
|
|
|
|
printf("%d/%d/[%d:%d] ", d.bytes[n], d.bpp[n], d.xs[n], d.ys[n]);
|
|
|
|
printf("}\n");
|
|
|
|
if (mpfmt && !(d.flags & MP_IMGFLAG_HWACCEL) && fmt != AV_PIX_FMT_UYYVYY411)
|
|
|
|
{
|
|
|
|
AVFrame *fr = av_frame_alloc();
|
|
|
|
fr->format = fmt;
|
|
|
|
fr->width = 128;
|
|
|
|
fr->height = 128;
|
video: generally try to align image data on 64 bytes
Generally, using x86 SIMD efficiently (or crash-free) requires aligning
all data on boundaries of 16, 32, or 64 (depending on instruction set
used). 64 bytes is needed or AVX-512, 32 for old AVX, 16 for SSE. Both
FFmpeg and zimg usually require aligned data for this reason.
FFmpeg is very unclear about alignment. Yes, it requires you to align
data pointers and strides. No, it doesn't tell you how much, except
sometimes (libavcodec has a legacy-looking avcodec_align_dimensions2()
API function, that requires a heavy-weight AVCodecContext as argument).
Sometimes, FFmpeg will take a shit on YOUR and ITS OWN alignment. For
example, vf_crop will randomly reduce alignment of data pointers,
depending on the crop parameters. On the other hand, some libavfilter
filters or libavcodec encoders may randomly crash if they get the wrong
alignment. I have no idea how this thing works at all.
FFmpeg usually doesn't seem to signal alignment internal anywhere, and
usually leaves it to av_malloc() etc. to allocate with proper alignment.
libavutil/mem.c currently has a ALIGN define, which is set to 64 if
FFmpeg is built with AVX-512 support, or as low as 16 if built without
any AVX support. The really funny thing is that a normal FFmpeg build
will e.g. align tiny string allocations to 64 bytes, even if the machine
does not support AVX at all.
For zimg use (in a later commit), we also want guaranteed alignment.
Modern x86 should actually not be much slower at unaligned accesses, but
that doesn't help. zimg's dumb intrinsic code apparently randomly
chooses between aligned or unaligned accesses (depending on compiler, I
guess), and on some CPUs these can even cause crashes. So just treat the
requirement to align as a fact of life.
All this means that we should probably make sure our own allocations are
64 bit aligned. This still doesn't guarantee alignment in all cases, but
it's slightly better than before.
This also makes me wonder whether we should always override libavcodec's
buffer pool, just so we have a guaranteed alignment. Currently, we only
do that if --vd-lavc-dr is used (and if that actually works). On the
other hand, it always uses DR on my machine, so who cares.
2019-07-15 01:06:47 +00:00
|
|
|
int err = av_frame_get_buffer(fr, MP_IMAGE_BYTE_ALIGN);
|
2014-11-04 22:52:29 +00:00
|
|
|
assert(err >= 0);
|
|
|
|
struct mp_image *mpi = mp_image_alloc(mpfmt, fr->width, fr->height);
|
|
|
|
assert(mpi);
|
|
|
|
// A rather fuzzy test, which might fail even if there's no bug.
|
|
|
|
for (int n = 0; n < 4; n++) {
|
|
|
|
assert(!!mpi->planes[n] == !!fr->data[n]);
|
|
|
|
assert(mpi->stride[n] == fr->linesize[n]);
|
|
|
|
}
|
|
|
|
talloc_free(mpi);
|
|
|
|
av_frame_free(&fr);
|
|
|
|
}
|
2017-06-29 18:51:37 +00:00
|
|
|
struct mp_regular_imgfmt reg;
|
|
|
|
if (mp_get_regular_imgfmt(®, mpfmt)) {
|
|
|
|
printf(" Regular: %d planes, %d bytes per comp., %d bit-pad "
|
|
|
|
"%dx%d chroma\n",
|
|
|
|
reg.num_planes, reg.component_size, reg.component_pad,
|
|
|
|
reg.chroma_w, reg.chroma_h);
|
|
|
|
for (int n = 0; n < reg.num_planes; n++) {
|
|
|
|
struct mp_regular_imgfmt_plane *plane = ®.planes[n];
|
|
|
|
printf(" %d: {", n);
|
|
|
|
for (int i = 0; i < plane->num_components; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
printf(", ");
|
|
|
|
printf("%d", plane->components[i]);
|
|
|
|
}
|
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 22:52:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|