FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
colorspace.h
Go to the documentation of this file.
1 /*
2  * Colorspace conversion defines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Various defines for YUV<->RGB conversion
25  */
26 
27 #ifndef AVUTIL_COLORSPACE_H
28 #define AVUTIL_COLORSPACE_H
29 
30 #define SCALEBITS 10
31 #define ONE_HALF (1 << (SCALEBITS - 1))
32 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
33 
34 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
35 {\
36  cb = (cb1) - 128;\
37  cr = (cr1) - 128;\
38  r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
39  g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
40  ONE_HALF;\
41  b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
42 }
43 
44 #define YUV_TO_RGB1_CCIR_BT709(cb1, cr1)\
45 {\
46  cb = (cb1) - 128;\
47  cr = (cr1) - 128;\
48  r_add = FIX(1.5747*255.0/224.0) * cr + ONE_HALF;\
49  g_add = - FIX(0.1873*255.0/224.0) * cb - FIX(0.4682*255.0/224.0) * cr + \
50  ONE_HALF;\
51  b_add = FIX(1.8556*255.0/224.0) * cb + ONE_HALF;\
52 }
53 
54 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
55 {\
56  y = ((y1) - 16) * FIX(255.0/219.0);\
57  r = cm[(y + r_add) >> SCALEBITS];\
58  g = cm[(y + g_add) >> SCALEBITS];\
59  b = cm[(y + b_add) >> SCALEBITS];\
60 }
61 
62 #define YUV_TO_RGB1(cb1, cr1)\
63 {\
64  cb = (cb1) - 128;\
65  cr = (cr1) - 128;\
66  r_add = FIX(1.40200) * cr + ONE_HALF;\
67  g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
68  b_add = FIX(1.77200) * cb + ONE_HALF;\
69 }
70 
71 #define YUV_TO_RGB2(r, g, b, y1)\
72 {\
73  y = (y1) << SCALEBITS;\
74  r = cm[(y + r_add) >> SCALEBITS];\
75  g = cm[(y + g_add) >> SCALEBITS];\
76  b = cm[(y + b_add) >> SCALEBITS];\
77 }
78 
79 #define Y_CCIR_TO_JPEG(y)\
80  cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
81 
82 #define Y_JPEG_TO_CCIR(y)\
83  (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
84 
85 #define C_CCIR_TO_JPEG(y)\
86  cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
87 
88 /* NOTE: the clamp is really necessary! */
89 static inline int C_JPEG_TO_CCIR(int y) {
90  y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
91  if (y < 16)
92  y = 16;
93  return y;
94 }
95 
96 
97 #define RGB_TO_Y_CCIR(r, g, b) \
98 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
99  FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
100 
101 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
102 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
103  FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
104 
105 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
106 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
107  FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
108 
109 #endif /* AVUTIL_COLORSPACE_H */
#define ONE_HALF
Definition: colorspace.h:31
#define FIX(x)
Definition: colorspace.h:32
static int C_JPEG_TO_CCIR(int y)
Definition: colorspace.h:89
#define SCALEBITS
Definition: colorspace.h:30