/*

    gcc -g -ggdb -Wall synth1.c -o synth1

    ./synth1 > synth1.uyvy422

    # quick look into the picture
    ffmpeg -f rawvideo -pixel_format uyvy422 -video_size 720x576 -i synth1.uyvy422 -y test.png

    # another quick look
    ffmpeg -stream_loop 1500 -f rawvideo -pixel_format uyvy422 -video_size 720x576 -r 25 -i synth1.uyvy422 -t 60 -y test.mp4

    # uncompress
    ffmpeg -stream_loop 1500 -f rawvideo -pixel_format uyvy422 -video_size 720x576 -r 25 -i synth1.uyvy422 -t 60 -c:v v210 -movflags write_colr -color_primaries bt470bg -color_trc gamma28 -colorspace bt470bg -aspect 4:3 -y synth1.v210.mov

    # dvcpro50
    ffmpeg -stream_loop 1500 -f rawvideo -pixel_format uyvy422 -video_size 720x576 -r 25 -i synth1.uyvy422 -t 60 -c:v dvvideo -movflags write_colr -color_primaries bt470bg -color_trc gamma28 -colorspace bt470bg -aspect 4:3 -y synth1.dv50.mov


    ./synth1 > synth1.X.uyvy422
    ./synth1 0 > synth1.0.uyvy422
...
    ./synth1 7 > synth1.7.uyvy422

*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

// /usr/local/src/decklink/sdk/Linux/Samples/SignalGenerator/SignalGenerator.cpp

#define BARS_COUNT 8

/*

    Cb / U (Chrominance Blue)
    Cr / V (Chrominance Red)
                                                        https://videoq.com/blog2/2025/10/12/color-bars-reference-levels/
                        u  y  v  y
                        Cb Y  Cr Y                      Y       U       V
    0xeb80eb80  =>      80 eb 80 eb     // white        235     128     128
    0xa28ea22c  =>      2c a2 8e a2     // yellow       162      44     142
    0x832c839c  =>      9c 83 2c 83     // ?            131     156      44
    0x703a7048  =>      48 70 3a 70     // green        112      72      58
    0x54c654b8  =>      b8 54 c6 54     // magenta ?
    0x41d44164  =>      64 41 d4 41     // red
    0x237223d4  =>      d4 23 72 23     // blue
    0x10801080  =>      80 10 80 10     // black

*/

// SD 75% Colour Bars
static uint32_t gSD75pcColourBars[BARS_COUNT] =
{
    0xeb80eb80, 0xa28ea22c, 0x832c839c, 0x703a7048,
    0x54c654b8, 0x41d44164, 0x237223d4, 0x10801080
};

// HD 75% Colour Bars
static uint32_t gHD75pcColourBars[BARS_COUNT] =
{
    0xeb80eb80, 0xa888a82c, 0x912c9193, 0x8534853f,
    0x3fcc3fc1, 0x33d4336d, 0x1c781cd4, 0x10801080
};

static void print_coefs(uint32_t *bars, const char* name)
{
    int b;

    fprintf(stderr, "%s\n        Y   U(Cb)  V(Cr)\n", name);
    for(b = 0; b < BARS_COUNT; b++)
    {
        fprintf(stderr, "%d: %6d %6d %6d\n", b,
            (bars[b] >>  8) & 0x000000FF,
            (bars[b] >>  0) & 0x000000FF,
            (bars[b] >> 16) & 0x000000FF);
    };
    fprintf(stderr, "\n");

};

int main(int argc, char** argv)
{
    int B, b, i, j;

    B = (argc < 2) ? -1 : atoi(argv[1]);

    print_coefs(gSD75pcColourBars, "gSD75pcColourBars");
    print_coefs(gHD75pcColourBars, "gHD75pcColourBars");

    for(j = 0; j < 576; j++)
    {
        for(b = 0; b < BARS_COUNT; b++)
        {
            for(i = 0; i < (720 / BARS_COUNT / 2); i++)
                fwrite(&gSD75pcColourBars[B < 0 ? b : B], sizeof(uint32_t), 1, stdout);
        };
    }

    return 0;
};
