Coverage Report

Created: 2026-11-30 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/video/img_format.c
Line
Count
Source
1
/*
2
 * This file is part of mpv.
3
 *
4
 * mpv is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * mpv is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include <assert.h>
19
#include <string.h>
20
21
#include <libavcodec/avcodec.h>
22
#include <libavutil/imgutils.h>
23
#include <libavutil/pixfmt.h>
24
#include <libavutil/pixdesc.h>
25
26
#include "video/img_format.h"
27
#include "video/mp_image.h"
28
#include "video/fmt-conversion.h"
29
30
struct mp_imgfmt_entry {
31
    const char *name;
32
    // Valid if flags!=0.
33
    // This can be incomplete, and missing fields are filled in:
34
    //  - sets num_planes and bpp[], derived from comps[] (rounds to bytes)
35
    //  - sets MP_IMGFLAG_GRAY, derived from comps[]
36
    //  - sets MP_IMGFLAG_ALPHA, derived from comps[]
37
    //  - sets align_x/y if 0, derived from chroma shift
38
    //  - sets xs[]/ys[] always, derived from num_planes/chroma_shift
39
    //  - sets MP_IMGFLAG_HAS_COMPS|MP_IMGFLAG_NE if num_planes>0
40
    //  - sets MP_IMGFLAG_TYPE_UINT if no other type set
41
    //  - sets id to mp_imgfmt_list[] implied format
42
    struct mp_imgfmt_desc desc;
43
};
44
45
#define FRINGE_GBRP(def, dname, b)                                          \
46
    [def - IMGFMT_CUST_BASE] = {                                            \
47
        .name = dname,                                                      \
48
        .desc = { .flags = MP_IMGFLAG_COLOR_RGB,                            \
49
                  .comps = { {2, 0, 8, (b) - 8}, {0, 0, 8, (b) - 8},        \
50
                             {1, 0, 8, (b) - 8}, }, }}
51
52
#define FLOAT_YUV(def, dname, xs, ys, a)                                    \
53
    [def - IMGFMT_CUST_BASE] = {                                            \
54
        .name = dname,                                                      \
55
        .desc = { .flags = MP_IMGFLAG_COLOR_YUV | MP_IMGFLAG_TYPE_FLOAT,    \
56
                   .chroma_xs = xs, .chroma_ys = ys,                        \
57
                   .comps = { {0, 0, 32}, {1, 0, 32}, {2, 0, 32},           \
58
                              {3 * (a), 0, 32 * (a)} }, }}
59
60
static const struct mp_imgfmt_entry mp_imgfmt_list[] = {
61
    // not in ffmpeg
62
    [IMGFMT_VDPAU_OUTPUT - IMGFMT_CUST_BASE] = {
63
        .name = "vdpau_output",
64
        .desc = {
65
            .flags = MP_IMGFLAG_NE | MP_IMGFLAG_RGB | MP_IMGFLAG_HWACCEL,
66
        },
67
    },
68
    [IMGFMT_YAP8 - IMGFMT_CUST_BASE] = {
69
        .name = "yap8",
70
        .desc = {
71
            .flags = MP_IMGFLAG_COLOR_YUV,
72
            .comps = { {0, 0, 8}, {0}, {0}, {1, 0, 8} },
73
        },
74
    },
75
    [IMGFMT_YAP16 - IMGFMT_CUST_BASE] = {
76
        .name = "yap16",
77
        .desc = {
78
            .flags = MP_IMGFLAG_COLOR_YUV,
79
            .comps = { {0, 0, 16}, {0}, {0}, {1, 0, 16} },
80
        },
81
    },
82
    [IMGFMT_Y1 - IMGFMT_CUST_BASE] = {
83
        .name = "y1",
84
        .desc = {
85
            .flags = MP_IMGFLAG_COLOR_RGB,
86
            .comps = { {0, 0, 8, -7} },
87
        },
88
    },
89
    [IMGFMT_YAPF - IMGFMT_CUST_BASE] = {
90
        .name = "grayaf32", // try to mimic ffmpeg naming convention
91
        .desc = {
92
            .flags = MP_IMGFLAG_COLOR_YUV | MP_IMGFLAG_TYPE_FLOAT,
93
            .comps = { {0, 0, 32}, {0}, {0}, {1, 0, 32} },
94
        },
95
    },
96
    FLOAT_YUV(IMGFMT_444PF,  "yuv444pf",  0, 0, 0),
97
    FLOAT_YUV(IMGFMT_444APF, "yuva444pf", 0, 0, 1),
98
    FLOAT_YUV(IMGFMT_420PF,  "yuv420pf",  1, 1, 0),
99
    FLOAT_YUV(IMGFMT_420APF, "yuva420pf", 1, 1, 1),
100
    FLOAT_YUV(IMGFMT_422PF,  "yuv422pf",  1, 0, 0),
101
    FLOAT_YUV(IMGFMT_422APF, "yuva422pf", 1, 0, 1),
102
    FLOAT_YUV(IMGFMT_440PF,  "yuv440pf",  0, 1, 0),
103
    FLOAT_YUV(IMGFMT_440APF, "yuva440pf", 0, 1, 1),
104
    FLOAT_YUV(IMGFMT_410PF,  "yuv410pf",  2, 2, 0),
105
    FLOAT_YUV(IMGFMT_410APF, "yuva410pf", 2, 2, 1),
106
    FLOAT_YUV(IMGFMT_411PF,  "yuv411pf",  2, 0, 0),
107
    FLOAT_YUV(IMGFMT_411APF, "yuva411pf", 2, 0, 1),
108
    FRINGE_GBRP(IMGFMT_GBRP1, "gbrp1", 1),
109
    FRINGE_GBRP(IMGFMT_GBRP2, "gbrp2", 2),
110
    FRINGE_GBRP(IMGFMT_GBRP3, "gbrp3", 3),
111
    FRINGE_GBRP(IMGFMT_GBRP4, "gbrp4", 4),
112
    FRINGE_GBRP(IMGFMT_GBRP5, "gbrp5", 5),
113
    FRINGE_GBRP(IMGFMT_GBRP6, "gbrp6", 6),
114
    // in FFmpeg, but FFmpeg names have an annoying "_vld" suffix
115
    [IMGFMT_VIDEOTOOLBOX - IMGFMT_CUST_BASE] = {
116
        .name = "videotoolbox",
117
    },
118
    [IMGFMT_VAAPI - IMGFMT_CUST_BASE] = {
119
        .name = "vaapi",
120
    },
121
};
122
123
static const struct mp_imgfmt_entry *get_mp_desc(int imgfmt)
124
1.92M
{
125
1.92M
    if (imgfmt < IMGFMT_CUST_BASE)
126
1.24M
        return NULL;
127
681k
    int index = imgfmt - IMGFMT_CUST_BASE;
128
681k
    if (index >= MP_ARRAY_SIZE(mp_imgfmt_list))
129
647k
        return NULL;
130
33.4k
    const struct mp_imgfmt_entry *e = &mp_imgfmt_list[index];
131
33.4k
    return e->name ? e : NULL;
132
681k
}
133
134
char **mp_imgfmt_name_list(void)
135
0
{
136
0
    int count = IMGFMT_END - IMGFMT_START;
137
0
    char **list = talloc_zero_array(NULL, char *, count + 1);
138
0
    int num = 0;
139
0
    for (int n = IMGFMT_START; n < IMGFMT_END; n++) {
140
0
        const char *name = mp_imgfmt_to_name(n);
141
0
        if (strcmp(name, "unknown") != 0)
142
0
            list[num++] = talloc_strdup(list, name);
143
0
    }
144
0
    return list;
145
0
}
146
147
int mp_imgfmt_from_name(bstr name)
148
3.27k
{
149
3.27k
    if (bstr_equals0(name, "none"))
150
0
        return 0;
151
3.27k
    static const struct { bstr old, new; } deprecated_fmt_names[] = {
152
3.27k
        {bstr0_lit("rgb30"), bstr0_lit("x2rgb10")},
153
3.27k
    };
154
6.54k
    for (int n = 0; n < MP_ARRAY_SIZE(deprecated_fmt_names); n++) {
155
3.27k
        if (bstr_equals(name, deprecated_fmt_names[n].old)) {
156
0
            name = deprecated_fmt_names[n].new;
157
0
            break;
158
0
        }
159
3.27k
    }
160
88.3k
    for (int n = 0; n < MP_ARRAY_SIZE(mp_imgfmt_list); n++) {
161
85.1k
        const struct mp_imgfmt_entry *p = &mp_imgfmt_list[n];
162
85.1k
        if (p->name && bstr_equals0(name, p->name))
163
0
            return IMGFMT_CUST_BASE + n;
164
85.1k
    }
165
3.27k
    return pixfmt2imgfmt(av_get_pix_fmt(mp_tprintf(80, "%.*s", BSTR_P(name))));
166
3.27k
}
167
168
char *mp_imgfmt_to_name_buf(char *buf, size_t buf_size, int fmt)
169
235k
{
170
235k
    const struct mp_imgfmt_entry *p = get_mp_desc(fmt);
171
235k
    const char *name = p ? p->name : NULL;
172
235k
    if (!name) {
173
235k
        const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(imgfmt2pixfmt(fmt));
174
235k
        if (pixdesc)
175
226k
            name = pixdesc->name;
176
235k
    }
177
235k
    if (!name)
178
9.71k
        name = "unknown";
179
235k
    snprintf(buf, buf_size, "%s", name);
180
235k
    int len = strlen(buf);
181
235k
    if (len > 2 && buf[len - 2] == MP_SELECT_LE_BE('l', 'b') && buf[len - 1] == 'e')
182
0
        buf[len - 2] = '\0';
183
235k
    return buf;
184
235k
}
185
186
static void fill_pixdesc_layout(struct mp_imgfmt_desc *desc,
187
                                enum AVPixelFormat fmt,
188
                                const AVPixFmtDescriptor *pd)
189
1.31M
{
190
1.31M
    if (pd->flags & AV_PIX_FMT_FLAG_PAL ||
191
1.31M
        pd->flags & AV_PIX_FMT_FLAG_HWACCEL)
192
23.1k
        goto fail;
193
194
1.31M
    bool has_alpha = pd->flags & AV_PIX_FMT_FLAG_ALPHA;
195
1.29M
    if (pd->nb_components != 1 + has_alpha &&
196
1.25M
        pd->nb_components != 3 + has_alpha)
197
0
        goto fail;
198
199
    // Very convenient: we assume we're always on little endian, and FFmpeg
200
    // explicitly marks big endian formats => don't need to guess whether a
201
    // format is little endian, or not affected by byte order.
202
1.29M
    bool is_be = pd->flags & AV_PIX_FMT_FLAG_BE;
203
1.29M
    bool is_ne = MP_SELECT_LE_BE(false, true) == is_be;
204
205
    // Packed sub-sampled YUV is very... special.
206
1.29M
    bool is_packed_ss_yuv = pd->log2_chroma_w && !pd->log2_chroma_h &&
207
54.0k
        pd->comp[1].plane == 0 && pd->comp[2].plane == 0 &&
208
12.8k
        pd->nb_components == 3;
209
210
1.29M
    if (is_packed_ss_yuv)
211
12.8k
        desc->bpp[0] = pd->comp[1].step * 8;
212
213
    // Determine if there are any byte overlaps => relevant for determining
214
    // access unit for endian, since pixdesc does not expose this, and assumes
215
    // a weird model where you do separate memory fetches for each component.
216
1.29M
    bool any_shared_bytes = !!(pd->flags & AV_PIX_FMT_FLAG_BITSTREAM);
217
5.19M
    for (int c = 0; c < pd->nb_components; c++) {
218
7.94M
        for (int i = 0; i < c; i++) {
219
4.04M
            const AVComponentDescriptor *d1 = &pd->comp[c];
220
4.04M
            const AVComponentDescriptor *d2 = &pd->comp[i];
221
4.04M
            if (d1->plane == d2->plane) {
222
1.73M
                if (d1->offset + (d1->depth + 7) / 8u > d2->offset &&
223
1.59M
                    d2->offset + (d2->depth + 7) / 8u > d1->offset)
224
92.7k
                    any_shared_bytes = true;
225
1.73M
            }
226
4.04M
        }
227
3.90M
    }
228
229
1.29M
    int el_bits = (pd->flags & AV_PIX_FMT_FLAG_BITSTREAM) ? 1 : 8;
230
5.17M
    for (int c = 0; c < pd->nb_components; c++) {
231
3.89M
        const AVComponentDescriptor *d = &pd->comp[c];
232
3.89M
        if (d->plane >= MP_MAX_PLANES)
233
0
            goto fail;
234
235
3.89M
        desc->num_planes = MPMAX(desc->num_planes, d->plane + 1);
236
237
3.89M
        int plane_bits = desc->bpp[d->plane];
238
3.89M
        int c_bits = d->step * el_bits;
239
240
        // The first component wins, because either all components result in
241
        // the same value, or luma wins (luma always comes before chroma).
242
3.89M
        if (plane_bits) {
243
1.13M
            if (c_bits > plane_bits)
244
5.15k
                goto fail; // inconsistent
245
2.75M
        } else {
246
2.75M
            desc->bpp[d->plane] = plane_bits = c_bits;
247
2.75M
        }
248
249
3.89M
        int shift = d->shift;
250
        // What the fuck: for some inexplicable reason, MONOB uses shift=7
251
        // in pixdesc, which is basically out of bounds. Pixdesc bug?
252
        // Make it behave like MONOW. (No, the bit-order is not different.)
253
3.89M
        if (fmt == AV_PIX_FMT_MONOBLACK)
254
1.28k
            shift = 0;
255
256
3.89M
        int offset = d->offset * el_bits;
257
        // The pixdesc logic for reading and endian swapping is as follows
258
        // (reverse engineered from av_read_image_line2()):
259
        // - determine a word size that will include the component fully;
260
        //   this includes the "active" bits and the amount "shifted" away
261
        //   (for example shift=7/depth=18 => 32 bit word reading [31:0])
262
        // - the same format can use different word sizes (e.g. bgr565: the R
263
        //   component at offset 0 is read as 8 bit; BG is read as 16 bits)
264
        // - if BE flag is set, swap the word before proceeding
265
        // - extract via shift and mask derived by depth
266
3.89M
        int word = mp_round_next_power_of_2(MPMAX(d->depth + shift, 8));
267
        // The purpose of this is unknown. It's an absurdity fished out of
268
        // av_read_image_line2()'s implementation. It seems technically
269
        // unnecessary, and provides no information. On the other hand, it
270
        // compensates for seemingly bogus packed integer pixdescs; this
271
        // is "why" some formats use d->offset = -1.
272
3.89M
        if (is_be && el_bits == 8 && word == 8)
273
33.4k
            offset += 8;
274
        // Pixdesc's model sometimes requires accesses with varying word-sizes,
275
        // as seen in bgr565 and other formats. Also, it makes you read some
276
        // formats with multiple endian-dependent accesses, where accessing a
277
        // larger unit would make more sense. (Consider X2RGB10BE, for which
278
        // pixdesc wants you to perform 3 * 2 byte accesses, and swap each of
279
        // the read 16 bit words. What you really want is to swap the entire 4
280
        // byte thing, and then extract the components with bit shifts).
281
        // This is complete bullshit, so we transform it into word swaps before
282
        // further processing. Care needs to be taken to not change formats like
283
        // P010 or YA16 (prefer component accesses for them; P010 isn't even
284
        // representable, because endian_shift is for all planes).
285
        // As a heuristic, assume that if any components share a byte, the whole
286
        // pixel is read as a single memory access and endian swapped at once.
287
3.89M
        int access_size = 8;
288
3.89M
        if (plane_bits > 8) {
289
2.10M
            if (any_shared_bytes) {
290
108k
                access_size = plane_bits;
291
108k
                if (is_be && word != access_size) {
292
                    // Before: offset = 8*byte_offset (with word bits of data)
293
                    // After: offset = bit_offset into swapped endian_size word
294
48.9k
                    offset = access_size - word - offset;
295
48.9k
                }
296
1.99M
            } else {
297
1.99M
                access_size = word;
298
1.99M
            }
299
2.10M
        }
300
3.89M
        int endian_size = (access_size && !is_ne) ? access_size : 8;
301
3.89M
        int endian_shift = mp_log2(endian_size) - 3;
302
3.89M
        if (!MP_IS_POWER_OF_2(endian_size) || endian_shift < 0 || endian_shift > 3)
303
0
            goto fail;
304
3.89M
        if (desc->endian_shift && desc->endian_shift != endian_shift)
305
0
            goto fail;
306
3.89M
        desc->endian_shift = endian_shift;
307
308
        // We always use bit offsets; this doesn't lose any information,
309
        // and pixdesc is merely more redundant.
310
3.89M
        offset += shift;
311
3.89M
        if (offset < 0 || offset >= (1 << 6))
312
7.72k
            goto fail;
313
3.88M
        if (offset + d->depth > plane_bits)
314
0
            goto fail;
315
3.88M
        if (d->depth < 0 || d->depth >= (1 << 6))
316
0
            goto fail;
317
3.88M
        desc->comps[c] = (struct mp_imgfmt_comp_desc){
318
3.88M
            .plane = d->plane,
319
3.88M
            .offset = offset,
320
3.88M
            .size = d->depth,
321
3.88M
        };
322
3.88M
    }
323
324
4.03M
    for (int p = 0; p < desc->num_planes; p++) {
325
2.75M
        if (!desc->bpp[p])
326
0
            goto fail; // plane doesn't exist
327
2.75M
    }
328
329
    // What the fuck: this is probably a pixdesc bug, so fix it.
330
1.27M
    if (fmt == AV_PIX_FMT_RGB8) {
331
1.28k
        desc->comps[2] = (struct mp_imgfmt_comp_desc){0, 0, 2};
332
1.28k
        desc->comps[1] = (struct mp_imgfmt_comp_desc){0, 2, 3};
333
1.28k
        desc->comps[0] = (struct mp_imgfmt_comp_desc){0, 5, 3};
334
1.28k
    }
335
336
    // Overlap test. If any shared bits are happening, this is not a format we
337
    // can represent (or it's something like Bayer: components in the same bits,
338
    // but different alternating lines).
339
1.27M
    bool any_shared_bits = false;
340
5.14M
    for (int c = 0; c < pd->nb_components; c++) {
341
7.85M
        for (int i = 0; i < c; i++) {
342
3.98M
            struct mp_imgfmt_comp_desc *c1 = &desc->comps[c];
343
3.98M
            struct mp_imgfmt_comp_desc *c2 = &desc->comps[i];
344
3.98M
            if (c1->plane == c2->plane) {
345
1.67M
                if (c1->offset + c1->size > c2->offset &&
346
1.53M
                    c2->offset + c2->size > c1->offset)
347
46.3k
                    any_shared_bits = true;
348
1.67M
            }
349
3.98M
        }
350
3.86M
    }
351
352
1.27M
    if (any_shared_bits) {
353
61.8k
        for (int c = 0; c < pd->nb_components; c++)
354
46.3k
            desc->comps[c] = (struct mp_imgfmt_comp_desc){0};
355
15.4k
    }
356
357
    // Many important formats have padding within an access word. For example
358
    // yuv420p10 has the upper 6 bit cleared to 0; P010 has the lower 6 bits
359
    // cleared to 0. Pixdesc cannot represent that these bits are 0. There are
360
    // other formats where padding is not guaranteed to be 0, but they are
361
    // described in the same way.
362
    // Apply a heuristic that is supposed to identify formats which use
363
    // guaranteed 0 padding. This could fail, but nobody said this pixdesc crap
364
    // is robust.
365
5.14M
    for (int c = 0; c < pd->nb_components; c++) {
366
3.86M
        struct mp_imgfmt_comp_desc *cd = &desc->comps[c];
367
        // Note: rgb444 would defeat our heuristic if we checked only per comp.
368
        //       also, exclude "bitstream" formats due to monow/monob
369
3.86M
        int fsize = MP_ALIGN_UP(cd->size, 8);
370
3.86M
        if (!any_shared_bytes && el_bits == 8 && fsize != cd->size &&
371
386k
            fsize - cd->size <= (1 << 3))
372
386k
        {
373
386k
            if (!(cd->offset % 8u)) {
374
270k
                cd->pad = -(fsize - cd->size);
375
270k
                cd->size = fsize;
376
270k
            } else if (!((cd->offset + cd->size) % 8u)) {
377
115k
                cd->pad = fsize - cd->size;
378
115k
                cd->size = fsize;
379
115k
                cd->offset = MP_ALIGN_DOWN(cd->offset, 8);
380
115k
            }
381
386k
        }
382
3.86M
    }
383
384
    // The alpha component always has ID 4 (index 3) in our representation, so
385
    // move the alpha component to there.
386
1.27M
    if (has_alpha && pd->nb_components < 4) {
387
9.01k
        desc->comps[3] = desc->comps[pd->nb_components - 1];
388
9.01k
        desc->comps[pd->nb_components - 1] = (struct mp_imgfmt_comp_desc){0};
389
9.01k
    }
390
391
1.27M
    if (is_packed_ss_yuv) {
392
12.8k
        desc->flags |= MP_IMGFLAG_PACKED_SS_YUV;
393
12.8k
        desc->bpp[0] /= 1 << pd->log2_chroma_w;
394
1.26M
    } else if (!any_shared_bits) {
395
1.25M
        desc->flags |= MP_IMGFLAG_HAS_COMPS;
396
1.25M
    }
397
398
1.27M
    return;
399
400
36.0k
fail:
401
180k
    for (int n = 0; n < 4; n++)
402
144k
        desc->comps[n] = (struct mp_imgfmt_comp_desc){0};
403
    // Average bit size fallback.
404
36.0k
    int num_planes = av_pix_fmt_count_planes(fmt);
405
36.0k
    desc->num_planes = MPCLAMP(num_planes, 0, MP_MAX_PLANES);
406
50.2k
    for (int p = 0; p < desc->num_planes; p++) {
407
14.1k
        int ls = av_image_get_linesize(fmt, 256, p);
408
14.1k
        desc->bpp[p] = ls > 0 ? ls * 8 / 256 : 0;
409
14.1k
    }
410
36.0k
}
411
412
static bool mp_imgfmt_get_desc_from_pixdesc(int mpfmt, struct mp_imgfmt_desc *out)
413
1.66M
{
414
1.66M
    enum AVPixelFormat fmt = imgfmt2pixfmt(mpfmt);
415
1.66M
    const AVPixFmtDescriptor *pd = av_pix_fmt_desc_get(fmt);
416
1.66M
    if (!pd || pd->nb_components > 4)
417
345k
        return false;
418
419
1.31M
    struct mp_imgfmt_desc desc = {
420
1.31M
        .id = mpfmt,
421
1.31M
        .chroma_xs = pd->log2_chroma_w,
422
1.31M
        .chroma_ys = pd->log2_chroma_h,
423
1.31M
    };
424
425
1.31M
    if (pd->flags & AV_PIX_FMT_FLAG_ALPHA)
426
95.6k
        desc.flags |= MP_IMGFLAG_ALPHA;
427
428
1.31M
    if (pd->flags & AV_PIX_FMT_FLAG_HWACCEL)
429
21.8k
        desc.flags |= MP_IMGFLAG_TYPE_HW;
430
431
    // Pixdesc does not provide a flag for XYZ, so this is the best we can do.
432
1.31M
    if (strncmp(pd->name, "xyz", 3) == 0) {
433
5.15k
        desc.flags |= MP_IMGFLAG_COLOR_XYZ;
434
1.31M
    } else if (pd->flags & AV_PIX_FMT_FLAG_RGB) {
435
538k
        desc.flags |= MP_IMGFLAG_COLOR_RGB;
436
772k
    } else if (fmt == AV_PIX_FMT_MONOBLACK || fmt == AV_PIX_FMT_MONOWHITE) {
437
2.57k
        desc.flags |= MP_IMGFLAG_COLOR_RGB;
438
770k
    } else if (fmt == AV_PIX_FMT_PAL8) {
439
1.28k
        desc.flags |= MP_IMGFLAG_COLOR_RGB | MP_IMGFLAG_TYPE_PAL8;
440
1.28k
    }
441
442
1.31M
    if (pd->flags & AV_PIX_FMT_FLAG_FLOAT)
443
30.9k
        desc.flags |= MP_IMGFLAG_TYPE_FLOAT;
444
445
    // Educated guess.
446
1.31M
    if (!(desc.flags & MP_IMGFLAG_COLOR_MASK) &&
447
768k
        !(desc.flags & MP_IMGFLAG_TYPE_HW))
448
746k
        desc.flags |= MP_IMGFLAG_COLOR_YUV;
449
450
1.31M
    desc.align_x = 1 << desc.chroma_xs;
451
1.31M
    desc.align_y = 1 << desc.chroma_ys;
452
453
1.31M
    fill_pixdesc_layout(&desc, fmt, pd);
454
455
1.31M
    if (desc.flags & (MP_IMGFLAG_HAS_COMPS | MP_IMGFLAG_PACKED_SS_YUV)) {
456
1.26M
        if (!(desc.flags & MP_IMGFLAG_TYPE_MASK))
457
1.23M
            desc.flags |= MP_IMGFLAG_TYPE_UINT;
458
1.26M
    }
459
460
1.31M
    if (desc.bpp[0] % 8u && (pd->flags & AV_PIX_FMT_FLAG_BITSTREAM))
461
5.15k
        desc.align_x = 8 / desc.bpp[0]; // expect power of 2
462
463
    // Very heuristical.
464
1.31M
    bool is_ne = !desc.endian_shift;
465
1.31M
    bool need_endian = (desc.comps[0].size % 8u && desc.bpp[0] > 8) ||
466
1.29M
                       desc.comps[0].size > 8;
467
468
1.31M
    if (need_endian) {
469
231k
        bool is_le = MP_SELECT_LE_BE(is_ne, !is_ne);
470
231k
        desc.flags |= is_le ? MP_IMGFLAG_LE : MP_IMGFLAG_BE;
471
1.08M
    } else {
472
1.08M
        desc.flags |= MP_IMGFLAG_LE | MP_IMGFLAG_BE;
473
1.08M
    }
474
475
1.31M
    *out = desc;
476
1.31M
    return true;
477
1.66M
}
478
479
bool mp_imgfmt_get_packed_yuv_locations(int imgfmt, uint8_t *luma_offsets)
480
0
{
481
0
    struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(imgfmt);
482
0
    if (!(desc.flags & MP_IMGFLAG_PACKED_SS_YUV))
483
0
        return false;
484
485
0
    mp_assert(desc.num_planes == 1);
486
487
    // Guess at which positions the additional luma samples are. We iterate
488
    // starting with the first byte, and then put a luma sample at places
489
    // not covered by other luma/chroma.
490
    // Pixdesc does not and can not provide this information. This heuristic
491
    // may fail in certain cases. What a load of bullshit, right?
492
0
    int lsize = desc.comps[0].size;
493
0
    int cur_offset = 0;
494
0
    for (int lsample = 1; lsample < (1 << desc.chroma_xs); lsample++) {
495
0
        while (1) {
496
0
            if (cur_offset + lsize > desc.bpp[0] * desc.align_x)
497
0
                return false;
498
0
            bool free = true;
499
0
            for (int c = 0; c < 3; c++) {
500
0
                struct mp_imgfmt_comp_desc *cd = &desc.comps[c];
501
0
                if (!cd->size)
502
0
                    continue;
503
0
                if (cd->offset + cd->size > cur_offset &&
504
0
                    cur_offset + lsize > cd->offset)
505
0
                {
506
0
                    free = false;
507
0
                    break;
508
0
                }
509
0
            }
510
0
            if (free)
511
0
                break;
512
0
            cur_offset += lsize;
513
0
        }
514
0
        luma_offsets[lsample] = cur_offset;
515
0
        cur_offset += lsize;
516
0
    }
517
518
0
    luma_offsets[0] = desc.comps[0].offset;
519
0
    return true;
520
0
}
521
522
static bool get_native_desc(int mpfmt, struct mp_imgfmt_desc *desc)
523
1.69M
{
524
1.69M
    const struct mp_imgfmt_entry *p = get_mp_desc(mpfmt);
525
1.69M
    if (!p || !p->desc.flags)
526
1.66M
        return false;
527
528
29.6k
    *desc = p->desc;
529
530
    // Fill in some fields mp_imgfmt_entry.desc is not required to set.
531
532
29.6k
    desc->id = mpfmt;
533
534
148k
    for (int n = 0; n < MP_NUM_COMPONENTS; n++) {
535
118k
        struct mp_imgfmt_comp_desc *cd = &desc->comps[n];
536
118k
        if (cd->size)
537
86.2k
            desc->num_planes = MPMAX(desc->num_planes, cd->plane + 1);
538
118k
        desc->bpp[cd->plane] =
539
118k
            MPMAX(desc->bpp[cd->plane], MP_ALIGN_UP(cd->offset + cd->size, 8));
540
118k
    }
541
542
29.6k
    if (!desc->align_x && !desc->align_y) {
543
29.6k
        desc->align_x = 1 << desc->chroma_xs;
544
29.6k
        desc->align_y = 1 << desc->chroma_ys;
545
29.6k
    }
546
547
29.6k
    if (desc->num_planes)
548
28.3k
        desc->flags |= MP_IMGFLAG_HAS_COMPS | MP_IMGFLAG_NE;
549
550
29.6k
    if (!(desc->flags & MP_IMGFLAG_TYPE_MASK))
551
11.5k
        desc->flags |= MP_IMGFLAG_TYPE_UINT;
552
553
29.6k
    return true;
554
1.69M
}
555
556
int mp_imgfmt_desc_get_num_comps(const struct mp_imgfmt_desc *desc)
557
1.31M
{
558
1.31M
    int flags = desc->flags;
559
1.31M
    if (!(flags & MP_IMGFLAG_COLOR_MASK))
560
0
        return 0;
561
1.31M
    return 3 + (flags & MP_IMGFLAG_GRAY ? -2 : 0) + !!(flags & MP_IMGFLAG_ALPHA);
562
1.31M
}
563
564
struct mp_imgfmt_desc mp_imgfmt_get_desc(int mpfmt)
565
1.69M
{
566
1.69M
    struct mp_imgfmt_desc desc;
567
568
1.69M
    if (!get_native_desc(mpfmt, &desc) &&
569
1.66M
        !mp_imgfmt_get_desc_from_pixdesc(mpfmt, &desc))
570
345k
        return (struct mp_imgfmt_desc){0};
571
572
4.20M
    for (int p = 0; p < desc.num_planes; p++) {
573
2.85M
        desc.xs[p] = (p == 1 || p == 2) ? desc.chroma_xs : 0;
574
2.85M
        desc.ys[p] = (p == 1 || p == 2) ? desc.chroma_ys : 0;
575
2.85M
    }
576
577
1.34M
    bool is_ba = desc.num_planes > 0;
578
4.20M
    for (int p = 0; p < desc.num_planes; p++)
579
2.85M
        is_ba = !(desc.bpp[p] % 8u);
580
581
1.34M
    if (is_ba)
582
1.31M
        desc.flags |= MP_IMGFLAG_BYTE_ALIGNED;
583
584
1.34M
    if (desc.flags & MP_IMGFLAG_HAS_COMPS) {
585
1.27M
        if (desc.comps[3].size)
586
100k
            desc.flags |= MP_IMGFLAG_ALPHA;
587
588
        // Assuming all colors are (CCC+[A]) or (C+[A]), the latter being gray.
589
1.27M
        if (!desc.comps[1].size)
590
38.6k
            desc.flags |= MP_IMGFLAG_GRAY;
591
592
1.27M
        bool bb = true;
593
6.39M
        for (int n = 0; n < MP_NUM_COMPONENTS; n++) {
594
5.11M
            if (desc.comps[n].offset % 8u || desc.comps[n].size % 8u)
595
95.3k
                bb = false;
596
5.11M
        }
597
1.27M
        if (bb)
598
1.24M
            desc.flags |= MP_IMGFLAG_BYTES;
599
1.27M
    }
600
601
1.34M
    if ((desc.flags & (MP_IMGFLAG_YUV | MP_IMGFLAG_RGB))
602
1.31M
        && (desc.flags & MP_IMGFLAG_HAS_COMPS)
603
1.27M
        && (desc.flags & MP_IMGFLAG_BYTES)
604
1.24M
        && ((desc.flags & MP_IMGFLAG_TYPE_MASK) == MP_IMGFLAG_TYPE_UINT))
605
1.19M
    {
606
1.19M
        int cnt = mp_imgfmt_desc_get_num_comps(&desc);
607
1.19M
        bool same_depth = true;
608
3.86M
        for (int p = 0; p < desc.num_planes; p++)
609
2.66M
            same_depth &= desc.bpp[p] == desc.bpp[0];
610
1.19M
        if (same_depth && cnt == desc.num_planes) {
611
713k
            if (desc.flags & MP_IMGFLAG_YUV) {
612
670k
                desc.flags |= MP_IMGFLAG_YUV_P;
613
670k
            } else {
614
42.5k
                desc.flags |= MP_IMGFLAG_RGB_P;
615
42.5k
            }
616
713k
        }
617
1.19M
        if (cnt == 3 && desc.num_planes == 2 &&
618
32.2k
            desc.bpp[1] == desc.bpp[0] * 2 &&
619
32.2k
            (desc.flags & MP_IMGFLAG_YUV))
620
32.2k
        {
621
622
32.2k
            desc.flags |= MP_IMGFLAG_YUV_NV;
623
32.2k
        }
624
1.19M
    }
625
626
1.34M
    return desc;
627
1.69M
}
628
629
static bool validate_regular_imgfmt(const struct mp_regular_imgfmt *fmt)
630
0
{
631
0
    bool present[MP_NUM_COMPONENTS] = {0};
632
0
    int n_comp = 0;
633
634
0
    for (int n = 0; n < fmt->num_planes; n++) {
635
0
        const struct mp_regular_imgfmt_plane *plane = &fmt->planes[n];
636
0
        n_comp += plane->num_components;
637
0
        if (n_comp > MP_NUM_COMPONENTS)
638
0
            return false;
639
0
        if (!plane->num_components)
640
0
            return false; // no empty planes in between allowed
641
642
0
        bool pad_only = true;
643
0
        int chroma_luma = 0; // luma: 1, chroma: 2, both: 3
644
0
        for (int i = 0; i < plane->num_components; i++) {
645
0
            int comp = plane->components[i];
646
0
            if (comp > MP_NUM_COMPONENTS)
647
0
                return false;
648
0
            if (comp == 0)
649
0
                continue;
650
0
            pad_only = false;
651
0
            if (present[comp - 1])
652
0
                return false; // no duplicates
653
0
            present[comp - 1] = true;
654
0
            chroma_luma |= (comp == 2 || comp == 3) ? 2 : 1;
655
0
        }
656
0
        if (pad_only)
657
0
            return false; // no planes with only padding allowed
658
0
        if ((fmt->chroma_xs > 0 || fmt->chroma_ys > 0) && chroma_luma == 3)
659
0
            return false; // separate chroma/luma planes required
660
0
    }
661
662
0
    if (!(present[0] || present[3]) ||  // at least component 1 or alpha needed
663
0
        (present[1] && !present[0]) ||  // component 2 requires component 1
664
0
        (present[2] && !present[1]))    // component 3 requires component 2
665
0
        return false;
666
667
0
    return true;
668
0
}
669
670
static enum pl_color_system get_forced_csp_from_flags(int flags)
671
336k
{
672
336k
    if (flags & MP_IMGFLAG_COLOR_XYZ)
673
0
        return PL_COLOR_SYSTEM_XYZ;
674
675
336k
    if (flags & MP_IMGFLAG_COLOR_RGB)
676
136k
        return PL_COLOR_SYSTEM_RGB;
677
678
200k
    return PL_COLOR_SYSTEM_UNKNOWN;
679
336k
}
680
681
enum pl_color_system mp_imgfmt_get_forced_csp(int imgfmt)
682
336k
{
683
336k
    return get_forced_csp_from_flags(mp_imgfmt_get_desc(imgfmt).flags);
684
336k
}
685
686
static enum mp_component_type get_component_type_from_flags(int flags)
687
0
{
688
0
    if (flags & MP_IMGFLAG_TYPE_UINT)
689
0
        return MP_COMPONENT_TYPE_UINT;
690
691
0
    if (flags & MP_IMGFLAG_TYPE_FLOAT)
692
0
        return MP_COMPONENT_TYPE_FLOAT;
693
694
0
    return MP_COMPONENT_TYPE_UNKNOWN;
695
0
}
696
697
enum mp_component_type mp_imgfmt_get_component_type(int imgfmt)
698
0
{
699
0
    return get_component_type_from_flags(mp_imgfmt_get_desc(imgfmt).flags);
700
0
}
701
702
int mp_find_other_endian(int imgfmt)
703
0
{
704
0
    return pixfmt2imgfmt(av_pix_fmt_swap_endianness(imgfmt2pixfmt(imgfmt)));
705
0
}
706
707
bool mp_get_regular_imgfmt(struct mp_regular_imgfmt *dst, int imgfmt)
708
0
{
709
0
    struct mp_regular_imgfmt res = {0};
710
711
0
    struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(imgfmt);
712
0
    if (!desc.num_planes)
713
0
        return false;
714
0
    res.num_planes = desc.num_planes;
715
716
0
    if (desc.endian_shift || !(desc.flags & MP_IMGFLAG_HAS_COMPS))
717
0
        return false;
718
719
0
    res.component_type = get_component_type_from_flags(desc.flags);
720
0
    if (!res.component_type)
721
0
        return false;
722
723
0
    struct mp_imgfmt_comp_desc *comp0 = &desc.comps[0];
724
0
    if (comp0->size < 1 || comp0->size > 64 || (comp0->size % 8u))
725
0
        return false;
726
727
0
    res.component_size = comp0->size / 8u;
728
0
    res.component_pad = comp0->pad;
729
730
0
    for (int n = 0; n < res.num_planes; n++) {
731
0
        if (desc.bpp[n] % comp0->size)
732
0
            return false;
733
0
        res.planes[n].num_components = desc.bpp[n] / comp0->size;
734
0
    }
735
736
0
    for (int n = 0; n < MP_NUM_COMPONENTS; n++) {
737
0
        struct mp_imgfmt_comp_desc *comp = &desc.comps[n];
738
0
        if (!comp->size)
739
0
            continue;
740
741
0
        struct mp_regular_imgfmt_plane *plane = &res.planes[comp->plane];
742
743
0
        res.num_planes = MPMAX(res.num_planes, comp->plane + 1);
744
745
        // We support uniform depth only.
746
0
        if (comp->size != comp0->size || comp->pad != comp0->pad)
747
0
            return false;
748
749
        // Size-aligned only.
750
0
        int pos = comp->offset / comp->size;
751
0
        if (comp->offset != pos * comp->size || pos >= MP_NUM_COMPONENTS)
752
0
            return false;
753
754
0
        if (plane->components[pos])
755
0
            return false;
756
0
        plane->components[pos] = n + 1;
757
0
    }
758
759
0
    res.chroma_xs = desc.chroma_xs;
760
0
    res.chroma_ys = desc.chroma_ys;
761
762
0
    res.forced_csp = get_forced_csp_from_flags(desc.flags);
763
764
0
    if (!validate_regular_imgfmt(&res))
765
0
        return false;
766
767
0
    *dst = res;
768
0
    return true;
769
0
}
770
771
static bool regular_imgfmt_equals(struct mp_regular_imgfmt *a,
772
                                  struct mp_regular_imgfmt *b)
773
0
{
774
0
    if (a->component_type != b->component_type ||
775
0
        a->component_size != b->component_size ||
776
0
        a->num_planes     != b->num_planes ||
777
0
        a->component_pad  != b->component_pad ||
778
0
        a->forced_csp     != b->forced_csp ||
779
0
        a->chroma_xs      != b->chroma_xs ||
780
0
        a->chroma_ys      != b->chroma_ys)
781
0
        return false;
782
783
0
    for (int n = 0; n < a->num_planes; n++) {
784
0
        int num_comps = a->planes[n].num_components;
785
0
        if (num_comps != b->planes[n].num_components)
786
0
            return false;
787
0
        for (int i = 0; i < num_comps; i++) {
788
0
            if (a->planes[n].components[i] != b->planes[n].components[i])
789
0
                return false;
790
0
        }
791
0
    }
792
793
0
    return true;
794
0
}
795
796
// Find a format that matches this one exactly.
797
int mp_find_regular_imgfmt(struct mp_regular_imgfmt *src)
798
0
{
799
0
    for (int n = IMGFMT_START + 1; n < IMGFMT_END; n++) {
800
0
        struct mp_regular_imgfmt f;
801
0
        if (mp_get_regular_imgfmt(&f, n) && regular_imgfmt_equals(src, &f))
802
0
            return n;
803
0
    }
804
0
    return 0;
805
0
}
806
807
// Compare the dst image formats, and return the one which can carry more data
808
// (e.g. higher depth, more color components, lower chroma subsampling, etc.),
809
// with respect to what is required to keep most of the src format.
810
// Returns the imgfmt, or 0 on error.
811
int mp_imgfmt_select_best(int dst1, int dst2, int src)
812
0
{
813
0
    enum AVPixelFormat dst1pxf = imgfmt2pixfmt(dst1);
814
0
    enum AVPixelFormat dst2pxf = imgfmt2pixfmt(dst2);
815
0
    enum AVPixelFormat srcpxf = imgfmt2pixfmt(src);
816
0
    enum AVPixelFormat dstlist[] = {dst1pxf, dst2pxf, AV_PIX_FMT_NONE};
817
0
    return pixfmt2imgfmt(avcodec_find_best_pix_fmt_of_list(dstlist, srcpxf, 1, 0));
818
0
}
819
820
// Same as mp_imgfmt_select_best(), but with a list of dst formats.
821
int mp_imgfmt_select_best_list(int *dst, int num_dst, int src)
822
0
{
823
0
    int best = 0;
824
0
    for (int n = 0; n < num_dst; n++)
825
0
        best = best ? mp_imgfmt_select_best(best, dst[n], src) : dst[n];
826
0
    return best;
827
0
}
828
829
bool mp_imgfmt_is_subsampled(enum mp_imgfmt fmt)
830
196k
{
831
196k
    struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(fmt);
832
196k
    return desc.chroma_xs || desc.chroma_ys;
833
196k
}
834
835
bool mp_imgfmt_is_420_subsampled(enum mp_imgfmt fmt)
836
0
{
837
0
    struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(fmt);
838
0
    return desc.chroma_xs == 1 && desc.chroma_ys == 1;
839
0
}