annotate DOCS/tech/colorspaces.txt @ 6059:c0ae8c0eda72

fix Arpi's bugs
author pontscho
date Sun, 12 May 2002 16:01:08 +0000
parents cebe55b15cd4
children 0aebf17f33ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5586
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
1 In general
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
2 ==========
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
3
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
4 There are planar and packed modes.
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
5 - Planar mode means: you have 3 separated image, one for each component,
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
6 each image 8 bits/pixel. To get the real colored pixel, you have to
5586
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
7 mix the components from all planes. The resolution of planes may differ!
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
8 - Packed mode means: you have all components mixed/interleaved together,
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
9 so you have small "packs" of components in a single, big image.
5312
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
10
5586
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
11 There are RGB and YUV colorspaces.
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
12 - RGB: Read, Green and Blue components. Used by analog VGA monitors.
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
13 - YUV: Luminance (Y) and Chrominance (U,V) components. Used by some
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
14 video systems, like PAL. Also most m(j)peg/dct based codecs use this.
5312
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
15
5586
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
16 With YUV, they used to reduce the resolution of U,V planes:
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
17 The most common YUV formats:
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
18 fourcc: bpp: IEEE: plane sizes: (w=width h=height of original image)
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
19 ? 24 YUV 4:4:4 Y: w * h U,V: w * h
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
20 YUY2,UYVY 16 YUV 4:2:2 Y: w * h U,V: (w/2) * h
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
21 YV12,I420 12 YUV 4:2:0 Y: w * h U,V: (w/2) * (h/2)
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
22 YVU9 9 YUV 4:1:1 Y: w * h U,V: (w/4) * (h/4)
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
23
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
24 conversion: (some cut'n'paste from www and maillist)
5312
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
25
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
26 RGB to YUV Conversion:
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
27 Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
28 Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
29 Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
30 YUV to RGB Conversion:
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
31 B = 1.164(Y - 16) + 2.018(U - 128)
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
32 G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
33 R = 1.164(Y - 16) + 1.596(V - 128)
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
34
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
35 In both these cases, you have to clamp the output values to keep them in
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
36 the [0-255] range. Rumour has it that the valid range is actually a subset
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
37 of [0-255] (I've seen an RGB range of [16-235] mentioned) but clamping the
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
38 values into [0-255] seems to produce acceptable results to me.
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
39
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
40 Julien (sorry, I can't call back his surname) suggests that there are
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
41 problems with the above formula and suggests the following instead:
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
42 Y = 0.299R + 0.587G + 0.114B
5312
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
43 Cb = U'= (B-Y)*0.565
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
44 Cr = V'= (R-Y)*0.713
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
45 with reciprocal versions:
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
46 R = Y + 1.403V'
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
47 G = Y - 0.344U' - 0.714V'
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
48 B = Y + 1.770U'
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
49 note: this formula doesn't contain the +128 offsets of U,V values!
5312
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
50
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
51 Conclusion:
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
52 Y = luminance, the weighted average of R G B components. (0=black 255=white)
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
53 U = Cb = blue component (0=green 128=grey 255=blue)
5314
ba5c92e64c5d fixes, extended by mplayer's planes[]
arpi
parents: 5312
diff changeset
54 V = Cr = red component (0=green 128=grey 255=red)
5312
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
55
5586
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
56
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
57 Huh. The planar YUV modes.
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
58 ==========================
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
59
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
60 The most misunderstood thingie...
5586
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
61
5312
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
62 In MPlayer, we usually have 3 pointers to the Y, U and V planes, so it
5586
6ce9c6231bdd updated
arpi
parents: 5322
diff changeset
63 doesn't matter what is the order of the planes in the memory:
5314
ba5c92e64c5d fixes, extended by mplayer's planes[]
arpi
parents: 5312
diff changeset
64 for mp_image_t and libvo's draw_slice():
ba5c92e64c5d fixes, extended by mplayer's planes[]
arpi
parents: 5312
diff changeset
65 planes[0] = Y = luminance
ba5c92e64c5d fixes, extended by mplayer's planes[]
arpi
parents: 5312
diff changeset
66 planes[1] = U = Cb = blue
ba5c92e64c5d fixes, extended by mplayer's planes[]
arpi
parents: 5312
diff changeset
67 planes[2] = V = Cr = red
5322
329896ab1989 typo...
arpi
parents: 5314
diff changeset
68 Note: planes[1] is ALWAYS U, and planes[2] is V, the fourcc
329896ab1989 typo...
arpi
parents: 5314
diff changeset
69 (YV12 vs. I420) doesn't matter here! So, every codecs using 3 pointers
329896ab1989 typo...
arpi
parents: 5314
diff changeset
70 (not only the first one) normally supports YV12 and I420 (=IYUV) too!
5314
ba5c92e64c5d fixes, extended by mplayer's planes[]
arpi
parents: 5312
diff changeset
71
5312
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
72 But there are some codecs (vfw, dshow) and vo drivers (xv) ignoring the 2nd
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
73 and 3rd pointer, and use only a single pointer to the planar yuv image. In
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
74 this case we must know the right order and alignment of planes in the memory!
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
75
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
76 from the webartz fourcc list:
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
77 YV12: 12 bpp, full sized Y plane followed by 2x2 subsampled V and U planes
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
78 I420: 12 bpp, full sized Y plane followed by 2x2 subsampled U and V planes
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
79 IYUV: the same as I420
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
80 YVU9: 9 bpp, full sized Y plane followed by 4x4 subsampled V and U planes
211c0f1ec065 soem explanation
arpi
parents:
diff changeset
81
5587
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
82 Huh 2. RGB vs. BGR ?
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
83 ====================
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
84
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
85 The 2nd most misunderstood thingie...
5587
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
86
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
87 You know, there are Intel and Motorola, and they use different byteorder.
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
88 There are also others, like MIPS or Alpha, they all follow either Intel
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
89 or Motorola byteorder.
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
90 Unfortunately, the packed colorspaces depend on CPU byteorder. So, RGB
5587
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
91 on Intel and Motorola means different order of bytes.
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
92
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
93 In MPlayer, we have constants IMGFMT_RGBxx and IMGFMT_BGRxx.
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
94 Unfortunately, some codecs and vo drivers follow Intel, some follow Motorola
5587
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
95 byteorder, so they are incompatible. We had to find a stable base, so long
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
96 time ago I've chosen OpenGL, as it's a wide-spreaded standard, and it well
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
97 defines what RGB is and what BGR is. So, MPlayer's RGB is compatible with
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
98 OpenGL's GL_RGB on all platforms, and the same goes for BGR - GL_BGR.
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
99 Unfortunately, most of the x86 codecs call our BGR to RGB, so it sometimes
5587
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
100 confuse developers.
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
101
441c98167877 RGBvsBGR added
arpi
parents: 5586
diff changeset
102 If you are unsure, try the OpenGL driver (-vo gl). There is at least software
5735
cebe55b15cd4 some typos
alex
parents: 5587
diff changeset
103 OpenGL implementation for all major platforms and OS's.