Coverage Report

Created: 2026-06-25 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/video/fmt-conversion.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 <libavutil/pixdesc.h>
19
#include <libavutil/avutil.h>
20
21
#include "video/img_format.h"
22
#include "fmt-conversion.h"
23
24
static const struct {
25
    int fmt;
26
    enum AVPixelFormat pix_fmt;
27
} conversion_map[] = {
28
    {IMGFMT_ARGB, AV_PIX_FMT_ARGB},
29
    {IMGFMT_BGRA, AV_PIX_FMT_BGRA},
30
    {IMGFMT_BGR24, AV_PIX_FMT_BGR24},
31
    {IMGFMT_RGB565, AV_PIX_FMT_RGB565},
32
    {IMGFMT_ABGR, AV_PIX_FMT_ABGR},
33
    {IMGFMT_RGBA, AV_PIX_FMT_RGBA},
34
    {IMGFMT_RGB24, AV_PIX_FMT_RGB24},
35
    {IMGFMT_PAL8,  AV_PIX_FMT_PAL8},
36
    {IMGFMT_UYVY,  AV_PIX_FMT_UYVY422},
37
    {IMGFMT_NV12,  AV_PIX_FMT_NV12},
38
    {IMGFMT_Y8,    AV_PIX_FMT_GRAY8},
39
    {IMGFMT_Y16, AV_PIX_FMT_GRAY16},
40
    {IMGFMT_420P,  AV_PIX_FMT_YUV420P},
41
    {IMGFMT_444P,  AV_PIX_FMT_YUV444P},
42
43
    // YUVJ are YUV formats that use the full Y range. Decoder color range
44
    // information is used instead. Deprecated in ffmpeg.
45
    {IMGFMT_420P,  AV_PIX_FMT_YUVJ420P},
46
    {IMGFMT_444P,  AV_PIX_FMT_YUVJ444P},
47
48
    {IMGFMT_BGR0,  AV_PIX_FMT_BGR0},
49
    {IMGFMT_0RGB,  AV_PIX_FMT_0RGB},
50
    {IMGFMT_RGB0,  AV_PIX_FMT_RGB0},
51
    {IMGFMT_0BGR,  AV_PIX_FMT_0BGR},
52
53
    {IMGFMT_RGBA64, AV_PIX_FMT_RGBA64},
54
55
    {IMGFMT_X2RGB10, AV_PIX_FMT_X2RGB10},
56
    {IMGFMT_X2BGR10, AV_PIX_FMT_X2BGR10},
57
58
    {IMGFMT_RGBAF16, AV_PIX_FMT_RGBAF16},
59
60
    {IMGFMT_VDPAU, AV_PIX_FMT_VDPAU},
61
    {IMGFMT_VIDEOTOOLBOX,   AV_PIX_FMT_VIDEOTOOLBOX},
62
    {IMGFMT_MEDIACODEC, AV_PIX_FMT_MEDIACODEC},
63
    {IMGFMT_VAAPI, AV_PIX_FMT_VAAPI},
64
    {IMGFMT_DXVA2, AV_PIX_FMT_DXVA2_VLD},
65
    {IMGFMT_D3D11, AV_PIX_FMT_D3D11},
66
    {IMGFMT_MMAL, AV_PIX_FMT_MMAL},
67
    {IMGFMT_CUDA, AV_PIX_FMT_CUDA},
68
    {IMGFMT_P010, AV_PIX_FMT_P010},
69
    {IMGFMT_DRMPRIME, AV_PIX_FMT_DRM_PRIME},
70
    {IMGFMT_VULKAN, AV_PIX_FMT_VULKAN},
71
72
    {0, AV_PIX_FMT_NONE}
73
};
74
75
enum AVPixelFormat imgfmt2pixfmt(int fmt)
76
1.62M
{
77
1.62M
    if (fmt == IMGFMT_NONE)
78
5.38k
        return AV_PIX_FMT_NONE;
79
80
1.61M
    if (fmt >= IMGFMT_AVPIXFMT_START && fmt < IMGFMT_AVPIXFMT_END) {
81
505k
        enum AVPixelFormat pixfmt = fmt - IMGFMT_AVPIXFMT_START;
82
        // Avoid duplicate format - each format must be unique.
83
505k
        int mpfmt = pixfmt2imgfmt(pixfmt);
84
505k
        if (mpfmt == fmt && av_pix_fmt_desc_get(pixfmt))
85
495k
            return pixfmt;
86
10.1k
        return AV_PIX_FMT_NONE;
87
505k
    }
88
89
12.5M
    for (int i = 0; conversion_map[i].fmt; i++) {
90
12.5M
        if (conversion_map[i].fmt == fmt)
91
1.10M
            return conversion_map[i].pix_fmt;
92
12.5M
    }
93
965
    return AV_PIX_FMT_NONE;
94
1.11M
}
95
96
int pixfmt2imgfmt(enum AVPixelFormat pix_fmt)
97
1.39M
{
98
1.39M
    if (pix_fmt == AV_PIX_FMT_NONE)
99
1.58k
        return IMGFMT_NONE;
100
101
31.1M
    for (int i = 0; conversion_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
102
30.5M
        if (conversion_map[i].pix_fmt == pix_fmt)
103
745k
            return conversion_map[i].fmt;
104
30.5M
    }
105
106
647k
    int generic = IMGFMT_AVPIXFMT_START + pix_fmt;
107
647k
    if (generic < IMGFMT_AVPIXFMT_END && av_pix_fmt_desc_get(pix_fmt))
108
639k
        return generic;
109
110
7.92k
    return 0;
111
647k
}