chroma.c 7.39 KB
Newer Older
1
/*****************************************************************************
2
 * chroma.c: libavutil <-> libvlc conversion routines
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2008 VLC authors and VideoLAN
5 6 7 8 9
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Gildas Bazin <gbazin@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * This program 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
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 021100301, USA.
23 24
 *****************************************************************************/

25 26 27 28 29 30 31
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <vlc_common.h>
#include <vlc_codec.h>

32
#include <libavutil/avutil.h>
33
#include <libavutil/pixfmt.h>
Nicolas Bertrand's avatar
Nicolas Bertrand committed
34
#include "avcommon.h"
35
#include "chroma.h"
36

37
/*****************************************************************************
38
 * Chroma fourcc -> libavutil pixfmt mapping
39
 *****************************************************************************/
40 41 42 43 44
#if defined(WORDS_BIGENDIAN)
#   define VLC_RGB_ES( fcc, leid, beid ) \
    { fcc, beid, 0, 0, 0 },
#else
#   define VLC_RGB_ES( fcc, leid, beid ) \
45
    { fcc, leid, 0, 0, 0 },
46 47 48 49 50 51 52
#endif

#define VLC_RGB( fcc, leid, beid, rmask, gmask, bmask ) \
    { fcc, leid, rmask, gmask, bmask }, \
    { fcc, beid, bmask, gmask, rmask }, \
    VLC_RGB_ES( fcc, leid, beid )

53

54
static const struct
55 56
{
    vlc_fourcc_t  i_chroma;
57 58 59 60
    int           i_chroma_id;
    uint32_t      i_rmask;
    uint32_t      i_gmask;
    uint32_t      i_bmask;
61 62 63 64

} chroma_table[] =
{
    /* Planar YUV formats */
65 66
    {VLC_CODEC_I444, PIX_FMT_YUV444P, 0, 0, 0 },
    {VLC_CODEC_J444, PIX_FMT_YUVJ444P, 0, 0, 0 },
hartman's avatar
hartman committed
67

68 69
    {VLC_CODEC_I440, PIX_FMT_YUV440P, 0, 0, 0 },
    {VLC_CODEC_J440, PIX_FMT_YUVJ440P, 0, 0, 0 },
70

71 72
    {VLC_CODEC_I422, PIX_FMT_YUV422P, 0, 0, 0 },
    {VLC_CODEC_J422, PIX_FMT_YUVJ422P, 0, 0, 0 },
73

74 75
    {VLC_CODEC_I420, PIX_FMT_YUV420P, 0, 0, 0 },
    {VLC_CODEC_YV12, PIX_FMT_YUV420P, 0, 0, 0 },
76
    {VLC_FOURCC('I','Y','U','V'), PIX_FMT_YUV420P, 0, 0, 0 },
77 78 79
    {VLC_CODEC_J420, PIX_FMT_YUVJ420P, 0, 0, 0 },
    {VLC_CODEC_I411, PIX_FMT_YUV411P, 0, 0, 0 },
    {VLC_CODEC_I410, PIX_FMT_YUV410P, 0, 0, 0 },
80
    {VLC_FOURCC('Y','V','U','9'), PIX_FMT_YUV410P, 0, 0, 0 },
hartman's avatar
hartman committed
81

Ilkka Ollakka's avatar
Ilkka Ollakka committed
82 83
    {VLC_CODEC_NV12, PIX_FMT_NV12, 0, 0, 0 },
    {VLC_CODEC_NV21, PIX_FMT_NV21, 0, 0, 0 },
84

85 86 87 88
    {VLC_CODEC_I420_9L, PIX_FMT_YUV420P9LE, 0, 0, 0 },
    {VLC_CODEC_I420_9B, PIX_FMT_YUV420P9BE, 0, 0, 0 },
    {VLC_CODEC_I420_10L, PIX_FMT_YUV420P10LE, 0, 0, 0 },
    {VLC_CODEC_I420_10B, PIX_FMT_YUV420P10BE, 0, 0, 0 },
89 90
    {VLC_CODEC_I422_9L, PIX_FMT_YUV422P9LE, 0, 0, 0 },
    {VLC_CODEC_I422_9B, PIX_FMT_YUV422P9BE, 0, 0, 0 },
91 92 93
    {VLC_CODEC_I422_10L, PIX_FMT_YUV422P10LE, 0, 0, 0 },
    {VLC_CODEC_I422_10B, PIX_FMT_YUV422P10BE, 0, 0, 0 },

94 95
    {VLC_CODEC_YUV420A, PIX_FMT_YUVA420P, 0, 0, 0 },
    {VLC_CODEC_YUV422A, AV_PIX_FMT_YUVA422P, 0, 0, 0 },
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
96
    {VLC_CODEC_YUVA,    AV_PIX_FMT_YUVA444P, 0, 0, 0 },
97

98 99 100
    {VLC_CODEC_YUVA_444_10L, AV_PIX_FMT_YUVA444P10LE, 0, 0, 0 },
    {VLC_CODEC_YUVA_444_10B, AV_PIX_FMT_YUVA444P10BE, 0, 0, 0 },

101 102 103 104
    {VLC_CODEC_I444_9L, PIX_FMT_YUV444P9LE, 0, 0, 0 },
    {VLC_CODEC_I444_9B, PIX_FMT_YUV444P9BE, 0, 0, 0 },
    {VLC_CODEC_I444_10L, PIX_FMT_YUV444P10LE, 0, 0, 0 },
    {VLC_CODEC_I444_10B, PIX_FMT_YUV444P10BE, 0, 0, 0 },
105 106
    {VLC_CODEC_I444_16L, PIX_FMT_YUV444P16LE, 0, 0, 0 },
    {VLC_CODEC_I444_16B, PIX_FMT_YUV444P16BE, 0, 0, 0 },
107

108
    /* Packed YUV formats */
109
    {VLC_CODEC_YUYV, PIX_FMT_YUYV422, 0, 0, 0 },
110
    {VLC_FOURCC('Y','U','Y','V'), PIX_FMT_YUYV422, 0, 0, 0 },
111
    {VLC_CODEC_UYVY, PIX_FMT_UYVY422, 0, 0, 0 },
112
    {VLC_FOURCC('Y','4','1','1'), PIX_FMT_UYYVYY411, 0, 0, 0 },
113 114

    /* Packed RGB formats */
115
    VLC_RGB( VLC_FOURCC('R','G','B','4'), PIX_FMT_RGB4, PIX_FMT_BGR4, 0x10, 0x06, 0x01 )
Ilkka Ollakka's avatar
Ilkka Ollakka committed
116
    VLC_RGB( VLC_CODEC_RGB8, PIX_FMT_RGB8, PIX_FMT_BGR8, 0xC0, 0x38, 0x07 )
117

118 119
    VLC_RGB( VLC_CODEC_RGB15, PIX_FMT_RGB555, PIX_FMT_BGR555, 0x7c00, 0x03e0, 0x001f )
    VLC_RGB( VLC_CODEC_RGB16, PIX_FMT_RGB565, PIX_FMT_BGR565, 0xf800, 0x07e0, 0x001f )
120
    VLC_RGB( VLC_CODEC_RGB24, PIX_FMT_BGR24, PIX_FMT_RGB24, 0xff0000, 0x00ff00, 0x0000ff )
121

122 123
    VLC_RGB( VLC_CODEC_RGB32, PIX_FMT_RGB32, PIX_FMT_BGR32, 0x00ff0000, 0x0000ff00, 0x000000ff )
    VLC_RGB( VLC_CODEC_RGB32, PIX_FMT_RGB32_1, PIX_FMT_BGR32_1, 0xff000000, 0x00ff0000, 0x0000ff00 )
124

125
#ifdef AV_PIX_FMT_0BGR32
126 127 128
    VLC_RGB( VLC_CODEC_RGB32, AV_PIX_FMT_0BGR32, AV_PIX_FMT_0RGB32, 0x000000ff, 0x0000ff00, 0x00ff0000 )
#endif

129 130
    {VLC_CODEC_RGBA, PIX_FMT_RGBA, 0, 0, 0 },
    {VLC_CODEC_ARGB, PIX_FMT_ARGB, 0, 0, 0 },
131
    {VLC_CODEC_BGRA, PIX_FMT_BGRA, 0, 0, 0 },
132
    {VLC_CODEC_GREY, PIX_FMT_GRAY8, 0, 0, 0},
133

134
     /* Paletized RGB */
135
    {VLC_CODEC_RGBP, PIX_FMT_PAL8, 0, 0, 0},
136

137
    {VLC_CODEC_GBR_PLANAR, AV_PIX_FMT_GBRP, 0, 0, 0 },
138 139 140 141
    {VLC_CODEC_GBR_PLANAR_9L, AV_PIX_FMT_GBRP9LE, 0, 0, 0 },
    {VLC_CODEC_GBR_PLANAR_9B, AV_PIX_FMT_GBRP9BE, 0, 0, 0 },
    {VLC_CODEC_GBR_PLANAR_10L, AV_PIX_FMT_GBRP10LE, 0, 0, 0 },
    {VLC_CODEC_GBR_PLANAR_10B, AV_PIX_FMT_GBRP10BE, 0, 0, 0 },
142

Nicolas Bertrand's avatar
Nicolas Bertrand committed
143 144 145 146
    /* XYZ */
#if LIBAVUTIL_VERSION_CHECK(52, 10, 0, 25, 100)
    {VLC_CODEC_XYZ12, AV_PIX_FMT_XYZ12, 0xfff0, 0xfff0, 0xfff0},
#endif
147
    { 0, 0, 0, 0, 0 }
148 149
};

150
/* FIXME special case the RGB formats */
151
int GetFfmpegChroma( int *restrict i_ffmpeg_chroma, const video_format_t *fmt )
152
{
153 154
    for( int i = 0; chroma_table[i].i_chroma != 0; i++ )
    {
155
        if( chroma_table[i].i_chroma == fmt->i_chroma )
156
        {
157 158 159
            if( ( chroma_table[i].i_rmask == 0 &&
                  chroma_table[i].i_gmask == 0 &&
                  chroma_table[i].i_bmask == 0 ) ||
160 161 162
                ( chroma_table[i].i_rmask == fmt->i_rmask &&
                  chroma_table[i].i_gmask == fmt->i_gmask &&
                  chroma_table[i].i_bmask == fmt->i_bmask ) )
163 164 165 166
            {
                *i_ffmpeg_chroma = chroma_table[i].i_chroma_id;
                return VLC_SUCCESS;
            }
167 168 169 170
        }
    }
    return VLC_EGENERIC;
}
171

172 173 174 175 176 177 178 179
vlc_fourcc_t FindVlcChroma( int i_ffmpeg_id )
{
    for( int i = 0; chroma_table[i].i_chroma != 0; i++ )
        if( chroma_table[i].i_chroma_id == i_ffmpeg_id )
            return chroma_table[i].i_chroma;
    return 0;
}

180
int GetVlcChroma( video_format_t *fmt, int i_ffmpeg_chroma )
181
{
182
    /* TODO FIXME for rgb format we HAVE to set rgb mask/shift */
183
    for( int i = 0; chroma_table[i].i_chroma != 0; i++ )
184 185
    {
        if( chroma_table[i].i_chroma_id == i_ffmpeg_chroma )
186
        {
187 188 189
            fmt->i_rmask = chroma_table[i].i_rmask;
            fmt->i_gmask = chroma_table[i].i_gmask;
            fmt->i_bmask = chroma_table[i].i_bmask;
190 191 192
            fmt->i_chroma = chroma_table[i].i_chroma;
            return VLC_SUCCESS;
        }
193
    }
194
    return VLC_EGENERIC;
195
}
196 197 198 199 200 201 202 203

int FindFfmpegChroma( vlc_fourcc_t fourcc )
{
    for( int i = 0; chroma_table[i].i_chroma != 0; i++ )
        if( chroma_table[i].i_chroma == fourcc )
            return chroma_table[i].i_chroma_id;
    return PIX_FMT_NONE;
}