Skip to content

Commit c86aeb8

Browse files
committed
as per @r-lyeh , Addendum to joMPEG colormath
1 parent 5c30af8 commit c86aeb8

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

posts/jo-mpeg-in-c/compareV104.mp4

553 KB
Binary file not shown.

posts/jo-mpeg-in-c/jo-mpeg-in-c.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Jo_MPEG converted to C
33
permalink: "/{{ page.fileSlug }}/"
44
date: 2022-02-18
5-
last_modified: 2024-08-17
5+
last_modified: 2025-01-21
66
description: Single-header MPEG-1 Video library ported to C
77
publicTags:
88
- C++
@@ -48,4 +48,28 @@ Unfortunately, the output has increased saturation and contrast. This is due to
4848
<figcaption>Input frame vs Output frame. Reverted to old color math.</figcaption>
4949
</figure>
5050
51-
I'm not sure why the code change credited to `r- lyeh` happened, but I guess the used video player handled color space incorrectly. Both [VLC](https://www.videolan.org/) and [MPV](https://mpv.io/) playback the colors correctly with `v1.03`.
51+
I'm not sure why the code change credited to `r- lyeh` happened, but I guess the used video player handled color space incorrectly. Both [VLC](https://www.videolan.org/) and [MPV](https://mpv.io/) playback the colors correctly with `v1.03`.
52+
53+
## Addendum
54+
As [clarified](https://github.com/FrostKiwi/treasurechest/issues/5#issuecomment-2602237649) by [@r-lyeh](https://github.com/r-lyeh) in the [comments](#comments), the color space fix was regarding the YUV math. I reinserted the fix, as it was always meant to be and bumped the version to `v1.04`. Here is a sample with the correct color math, shown scene from [NeoTokyo°](https://store.steampowered.com/app/244630/NEOTOKYO/).
55+
56+
<video poster="v104Thumb.png" width="684" height="512" controls mute autoplay loop><source src="compareV104.mp4" type="video/mp4"></video>
57+
58+
The conversion to [YCbCr](https://en.wikipedia.org/wiki/YCbCr) was this in `v1.02`, scaling the color vectors too much.
59+
```c
60+
Y[i] = ( 0.299f*r + 0.587f*g + 0.114f*b) * (219.f/255) + 16;
61+
CBx[i] = (-0.299f*r - 0.587f*g + 0.886f*b) * (224.f/255) + 128;
62+
CRx[i] = ( 0.701f*r - 0.587f*g - 0.114f*b) * (224.f/255) + 128;
63+
```
64+
Here is the same but in `v1.03`, correct scale but mixed up color components.
65+
```c
66+
Y[i] = ( 0.59f*r + 0.30f*g + 0.11f*b) * (219.f/255) + 16;
67+
CBx[i] = (-0.17f*r - 0.33f*g + 0.50f*b) * (224.f/255) + 128;
68+
CRx[i] = ( 0.50f*r - 0.42f*g - 0.08f*b) * (224.f/255) + 128;
69+
```
70+
And finally the new fix, as pointed out by [@r-lyeh](https://github.com/r-lyeh) in `v1.04`.
71+
```c
72+
Y[i] = ( 0.59f*g + 0.30f*r + 0.11f*b) * (219.f/255) + 16;
73+
CBx[i] = (-0.17f*r - 0.33f*g + 0.50f*b) * (224.f/255) + 128;
74+
CRx[i] = ( 0.50f*r - 0.42f*g - 0.08f*b) * (224.f/255) + 128;
75+
```

posts/jo-mpeg-in-c/jo_mpeg.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Converted to C by Wladislav Artsimovich https://blog.frost.kiwi/jo-mpeg-in-c
44
*
55
* Latest revisions:
6+
* 1.04 (21-01-2025) YUV math fix, this time for real ( thx @r-lyeh, again )
7+
* as per https://github.com/FrostKiwi/treasurechest/issues/5
68
* 1.03 (15-08-2024) Reverted color space change from 1.02, as it resulted in
79
* overscaled color vectors and thus oversaturated colors
810
* 1.02 (22-03-2017) Fixed AC encoding bug.
@@ -228,7 +230,7 @@ void jo_write_mpeg(FILE *fp, const unsigned char *rgbx, int width, int height, i
228230
y = y >= height ? height-1 : y;
229231
const unsigned char *c = rgbx + y*width*4+x*4;
230232
float r = c[0], g = c[1], b = c[2];
231-
Y[i] = (0.59f*r + 0.30f*g + 0.11f*b) * (219.f/255) + 16;
233+
Y[i] = (0.59f*g + 0.30f*r + 0.11f*b) * (219.f/255) + 16;
232234
CBx[i] = (-0.17f*r - 0.33f*g + 0.50f*b) * (224.f/255) + 128;
233235
CRx[i] = (0.50f*r - 0.42f*g - 0.08f*b) * (224.f/255) + 128;
234236
}

posts/jo-mpeg-in-c/v104Thumb.png

324 KB
Loading

0 commit comments

Comments
 (0)