summaryrefslogtreecommitdiffstats
path: root/test/img_utils.c
blob: 71764f304f1c3d88e20a48c98c95248d4cafc227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * This file is part of mpv.
 *
 * 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.
 *
 * 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
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <libavutil/frame.h>
#include <libavutil/pixdesc.h>

#include "common/common.h"
#include "img_utils.h"
#include "video/img_format.h"
#include "video/fmt-conversion.h"

int imgfmts[IMGFMT_AVPIXFMT_END - IMGFMT_AVPIXFMT_START + 100];
int num_imgfmts;

static enum AVPixelFormat pixfmt_unsup[100];
static int num_pixfmt_unsup;

static int cmp_imgfmt_name(const void *a, const void *b)
{
    char *name_a = mp_imgfmt_to_name(*(int *)a);
    char *name_b = mp_imgfmt_to_name(*(int *)b);

    return strcmp(name_a, name_b);
}

void init_imgfmts_list(void)
{
    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);
        int mpfmt = pixfmt2imgfmt(fmt);
        if (!mpfmt) {
            assert(num_pixfmt_unsup < MP_ARRAY_SIZE(pixfmt_unsup));
            pixfmt_unsup[num_pixfmt_unsup++] = fmt;
        }
    }

    for (int fmt = IMGFMT_START; fmt <= IMGFMT_END; fmt++) {
        struct mp_imgfmt_desc d = mp_imgfmt_get_desc(fmt);
        enum AVPixelFormat pixfmt = imgfmt2pixfmt(fmt);
        if (d.id || pixfmt != AV_PIX_FMT_NONE) {
            assert(num_imgfmts < MP_ARRAY_SIZE(imgfmts)); // enlarge that array
            imgfmts[num_imgfmts++] = fmt;
        }
    }

    qsort(imgfmts, num_imgfmts, sizeof(imgfmts[0]), cmp_imgfmt_name);
}