1 /*****************************************************************************
2  * chroma.c: libavutil <-> libvlc conversion routines
3  *****************************************************************************
4  * Copyright (C) 1999-2008 VLC authors and VideoLAN
5  * $Id: 10338450b2c2342edb857e513cef5dc83b3745c6 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * 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 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <vlc_common.h>
30 #include <vlc_codec.h>
31 
32 #include <libavutil/avutil.h>
33 #include <libavutil/pixfmt.h>
34 #include "avcommon.h"
35 #include "chroma.h"
36 
37 /*****************************************************************************
38  * Chroma fourcc -> libavutil pixfmt mapping
39  *****************************************************************************/
40 #if defined(WORDS_BIGENDIAN)
41 #   define VLC_RGB_ES( fcc, leid, beid ) \
42     { fcc, beid, 0, 0, 0 },
43 #else
44 #   define VLC_RGB_ES( fcc, leid, beid ) \
45     { fcc, leid, 0, 0, 0 },
46 #endif
47 
48 #define VLC_RGB( fcc, leid, beid, rmask, gmask, bmask ) \
49     { fcc, leid, rmask, gmask, bmask }, \
50     { fcc, beid, bmask, gmask, rmask }, \
51     VLC_RGB_ES( fcc, leid, beid )
52 
53 
54 static const struct
55 {
56     vlc_fourcc_t  i_chroma;
57     int           i_chroma_id;
58     uint32_t      i_rmask;
59     uint32_t      i_gmask;
60     uint32_t      i_bmask;
61 
62 } chroma_table[] =
63 {
64     /* Planar YUV formats */
65     {VLC_CODEC_I444, AV_PIX_FMT_YUV444P, 0, 0, 0 },
66     {VLC_CODEC_J444, AV_PIX_FMT_YUVJ444P, 0, 0, 0 },
67 
68     {VLC_CODEC_I440, AV_PIX_FMT_YUV440P, 0, 0, 0 },
69     {VLC_CODEC_J440, AV_PIX_FMT_YUVJ440P, 0, 0, 0 },
70 
71     {VLC_CODEC_I422, AV_PIX_FMT_YUV422P, 0, 0, 0 },
72     {VLC_CODEC_J422, AV_PIX_FMT_YUVJ422P, 0, 0, 0 },
73 
74     {VLC_CODEC_I420, AV_PIX_FMT_YUV420P, 0, 0, 0 },
75     {VLC_CODEC_YV12, AV_PIX_FMT_YUV420P, 0, 0, 0 },
76     {VLC_FOURCC('I','Y','U','V'), AV_PIX_FMT_YUV420P, 0, 0, 0 },
77     {VLC_CODEC_J420, AV_PIX_FMT_YUVJ420P, 0, 0, 0 },
78     {VLC_CODEC_I411, AV_PIX_FMT_YUV411P, 0, 0, 0 },
79     {VLC_CODEC_I410, AV_PIX_FMT_YUV410P, 0, 0, 0 },
80     {VLC_FOURCC('Y','V','U','9'), AV_PIX_FMT_YUV410P, 0, 0, 0 },
81 
82     {VLC_CODEC_NV12, AV_PIX_FMT_NV12, 0, 0, 0 },
83     {VLC_CODEC_NV21, AV_PIX_FMT_NV21, 0, 0, 0 },
84 
85     {VLC_CODEC_I420_9L, AV_PIX_FMT_YUV420P9LE, 0, 0, 0 },
86     {VLC_CODEC_I420_9B, AV_PIX_FMT_YUV420P9BE, 0, 0, 0 },
87     {VLC_CODEC_I420_10L, AV_PIX_FMT_YUV420P10LE, 0, 0, 0 },
88     {VLC_CODEC_I420_10B, AV_PIX_FMT_YUV420P10BE, 0, 0, 0 },
89 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 54, 17, 100 ) )
90     {VLC_CODEC_I420_12L, AV_PIX_FMT_YUV420P12LE, 0, 0, 0 },
91     {VLC_CODEC_I420_12B, AV_PIX_FMT_YUV420P12BE, 0, 0, 0 },
92 #endif
93     {VLC_CODEC_I420_16L, AV_PIX_FMT_YUV420P16LE, 0, 0, 0 },
94     {VLC_CODEC_I420_16B, AV_PIX_FMT_YUV420P16BE, 0, 0, 0 },
95 #ifdef AV_PIX_FMT_P010
96     {VLC_CODEC_P010, AV_PIX_FMT_P010, 0, 0, 0 },
97 #endif
98 
99     {VLC_CODEC_I422_9L, AV_PIX_FMT_YUV422P9LE, 0, 0, 0 },
100     {VLC_CODEC_I422_9B, AV_PIX_FMT_YUV422P9BE, 0, 0, 0 },
101     {VLC_CODEC_I422_10L, AV_PIX_FMT_YUV422P10LE, 0, 0, 0 },
102     {VLC_CODEC_I422_10B, AV_PIX_FMT_YUV422P10BE, 0, 0, 0 },
103 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 54, 17, 100 ) )
104     {VLC_CODEC_I422_12L, AV_PIX_FMT_YUV422P12LE, 0, 0, 0 },
105     {VLC_CODEC_I422_12B, AV_PIX_FMT_YUV422P12BE, 0, 0, 0 },
106 #endif
107 
108     {VLC_CODEC_YUV420A, AV_PIX_FMT_YUVA420P, 0, 0, 0 },
109     {VLC_CODEC_YUV422A, AV_PIX_FMT_YUVA422P, 0, 0, 0 },
110     {VLC_CODEC_YUVA,    AV_PIX_FMT_YUVA444P, 0, 0, 0 },
111 
112     {VLC_CODEC_YUVA_444_10L, AV_PIX_FMT_YUVA444P10LE, 0, 0, 0 },
113     {VLC_CODEC_YUVA_444_10B, AV_PIX_FMT_YUVA444P10BE, 0, 0, 0 },
114 
115     {VLC_CODEC_I444_9L, AV_PIX_FMT_YUV444P9LE, 0, 0, 0 },
116     {VLC_CODEC_I444_9B, AV_PIX_FMT_YUV444P9BE, 0, 0, 0 },
117     {VLC_CODEC_I444_10L, AV_PIX_FMT_YUV444P10LE, 0, 0, 0 },
118     {VLC_CODEC_I444_10B, AV_PIX_FMT_YUV444P10BE, 0, 0, 0 },
119 #if (LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 54, 17, 100 ) )
120     {VLC_CODEC_I444_12L, AV_PIX_FMT_YUV444P12LE, 0, 0, 0 },
121     {VLC_CODEC_I444_12B, AV_PIX_FMT_YUV444P12BE, 0, 0, 0 },
122 #endif
123     {VLC_CODEC_I444_16L, AV_PIX_FMT_YUV444P16LE, 0, 0, 0 },
124     {VLC_CODEC_I444_16B, AV_PIX_FMT_YUV444P16BE, 0, 0, 0 },
125 
126     /* Packed YUV formats */
127     {VLC_CODEC_YUYV, AV_PIX_FMT_YUYV422, 0, 0, 0 },
128     {VLC_FOURCC('Y','U','Y','V'), AV_PIX_FMT_YUYV422, 0, 0, 0 },
129     {VLC_CODEC_UYVY, AV_PIX_FMT_UYVY422, 0, 0, 0 },
130     {VLC_CODEC_YVYU, AV_PIX_FMT_YVYU422, 0, 0, 0 },
131     {VLC_FOURCC('Y','4','1','1'), AV_PIX_FMT_UYYVYY411, 0, 0, 0 },
132 
133     /* Packed RGB formats */
134     VLC_RGB( VLC_FOURCC('R','G','B','4'), AV_PIX_FMT_RGB4, AV_PIX_FMT_BGR4, 0x10, 0x06, 0x01 )
135     VLC_RGB( VLC_CODEC_RGB8, AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, 0xC0, 0x38, 0x07 )
136 
137     VLC_RGB( VLC_CODEC_RGB15, AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555, 0x7c00, 0x03e0, 0x001f )
138     VLC_RGB( VLC_CODEC_RGB16, AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565, 0xf800, 0x07e0, 0x001f )
139     VLC_RGB( VLC_CODEC_RGB24, AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, 0xff0000, 0x00ff00, 0x0000ff )
140 
141     VLC_RGB( VLC_CODEC_RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_BGR32, 0x00ff0000, 0x0000ff00, 0x000000ff )
142     VLC_RGB( VLC_CODEC_RGB32, AV_PIX_FMT_RGB32_1, AV_PIX_FMT_BGR32_1, 0xff000000, 0x00ff0000, 0x0000ff00 )
143 
144 #ifdef AV_PIX_FMT_0BGR32
145     VLC_RGB( VLC_CODEC_RGB32, AV_PIX_FMT_0BGR32, AV_PIX_FMT_0RGB32, 0x000000ff, 0x0000ff00, 0x00ff0000 )
146 #endif
147 
148 #if (LIBAVUTIL_VERSION_MICRO == 0 || LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 55, 0, 100 ) )
149 #ifdef WORDS_BIGENDIAN
150     {VLC_CODEC_RGBA64, AV_PIX_FMT_RGBA64BE, 0, 0, 0 },
151 #else /* !WORDS_BIGENDIAN */
152     {VLC_CODEC_RGBA64, AV_PIX_FMT_RGBA64LE, 0, 0, 0 },
153 #endif /* !WORDS_BIGENDIAN */
154 #endif
155 
156     {VLC_CODEC_RGBA, AV_PIX_FMT_RGBA, 0, 0, 0 },
157     {VLC_CODEC_ARGB, AV_PIX_FMT_ARGB, 0, 0, 0 },
158     {VLC_CODEC_BGRA, AV_PIX_FMT_BGRA, 0, 0, 0 },
159     {VLC_CODEC_GREY, AV_PIX_FMT_GRAY8, 0, 0, 0},
160 
161      /* Paletized RGB */
162     {VLC_CODEC_RGBP, AV_PIX_FMT_PAL8, 0, 0, 0},
163 
164     {VLC_CODEC_GBR_PLANAR, AV_PIX_FMT_GBRP, 0, 0, 0 },
165     {VLC_CODEC_GBR_PLANAR_9L, AV_PIX_FMT_GBRP9LE, 0, 0, 0 },
166     {VLC_CODEC_GBR_PLANAR_9B, AV_PIX_FMT_GBRP9BE, 0, 0, 0 },
167     {VLC_CODEC_GBR_PLANAR_10L, AV_PIX_FMT_GBRP10LE, 0, 0, 0 },
168     {VLC_CODEC_GBR_PLANAR_10B, AV_PIX_FMT_GBRP10BE, 0, 0, 0 },
169     {VLC_CODEC_GBR_PLANAR_16L, AV_PIX_FMT_GBRP16LE, 0, 0, 0 },
170     {VLC_CODEC_GBR_PLANAR_16B, AV_PIX_FMT_GBRP16BE, 0, 0, 0 },
171 
172     /* XYZ */
173 #if LIBAVUTIL_VERSION_CHECK(52, 10, 0, 25, 100)
174     {VLC_CODEC_XYZ12, AV_PIX_FMT_XYZ12, 0xfff0, 0xfff0, 0xfff0},
175 #endif
176     { 0, 0, 0, 0, 0 }
177 };
178 
179 /* FIXME special case the RGB formats */
GetFfmpegChroma(int * restrict i_ffmpeg_chroma,const video_format_t * fmt)180 int GetFfmpegChroma( int *restrict i_ffmpeg_chroma, const video_format_t *fmt )
181 {
182     for( int i = 0; chroma_table[i].i_chroma != 0; i++ )
183     {
184         if( chroma_table[i].i_chroma == fmt->i_chroma )
185         {
186             if( ( chroma_table[i].i_rmask == 0 &&
187                   chroma_table[i].i_gmask == 0 &&
188                   chroma_table[i].i_bmask == 0 ) ||
189                 ( chroma_table[i].i_rmask == fmt->i_rmask &&
190                   chroma_table[i].i_gmask == fmt->i_gmask &&
191                   chroma_table[i].i_bmask == fmt->i_bmask ) )
192             {
193                 *i_ffmpeg_chroma = chroma_table[i].i_chroma_id;
194                 return VLC_SUCCESS;
195             }
196         }
197     }
198     return VLC_EGENERIC;
199 }
200 
FindVlcChroma(int i_ffmpeg_id)201 vlc_fourcc_t FindVlcChroma( int i_ffmpeg_id )
202 {
203     for( int i = 0; chroma_table[i].i_chroma != 0; i++ )
204         if( chroma_table[i].i_chroma_id == i_ffmpeg_id )
205             return chroma_table[i].i_chroma;
206     return 0;
207 }
208 
GetVlcChroma(video_format_t * fmt,int i_ffmpeg_chroma)209 int GetVlcChroma( video_format_t *fmt, int i_ffmpeg_chroma )
210 {
211     /* TODO FIXME for rgb format we HAVE to set rgb mask/shift */
212     for( int i = 0; chroma_table[i].i_chroma != 0; i++ )
213     {
214         if( chroma_table[i].i_chroma_id == i_ffmpeg_chroma )
215         {
216             fmt->i_rmask = chroma_table[i].i_rmask;
217             fmt->i_gmask = chroma_table[i].i_gmask;
218             fmt->i_bmask = chroma_table[i].i_bmask;
219             fmt->i_chroma = chroma_table[i].i_chroma;
220             return VLC_SUCCESS;
221         }
222     }
223     return VLC_EGENERIC;
224 }
225 
FindFfmpegChroma(vlc_fourcc_t fourcc)226 int FindFfmpegChroma( vlc_fourcc_t fourcc )
227 {
228     for( int i = 0; chroma_table[i].i_chroma != 0; i++ )
229         if( chroma_table[i].i_chroma == fourcc )
230             return chroma_table[i].i_chroma_id;
231     return AV_PIX_FMT_NONE;
232 }
233