~martijnbraam/pyatem

79f3a8eb032b6e8a6fbbb06212cfdfb9f9b2abb2 — Martijn Braam a month ago 99b8cd3
Improve YCbCr decoding
3 files changed, 22 insertions(+), 14 deletions(-)

M pyatem/mediaconvertmodule.c
M pyatem/protocol.py
M utils/atem-to-rgb.c
M pyatem/mediaconvertmodule.c => pyatem/mediaconvertmodule.c +6 -6
@@ 54,12 54,12 @@ static PyObject *method_atem_to_rgb(PyObject *self, PyObject *args) {
        float y1f = ((double)(y1 << 6)-y_offset) / y_range;
        float y2f = ((double)(y2 << 6)-y_offset) / y_range;

        float r1f = y1f + crf;
        float g1f = y1f - cbf * bt709_coeff_bg - crf * bt709_coeff_rg;
        float b1f = y1f + cbf;
        float r2f = y2f + crf;
        float g2f = y2f - cbf * bt709_coeff_bg - crf * bt709_coeff_rg;
        float b2f = y2f + cbf;
        float r1f = fmin(255, y1f + crf);
        float g1f = fmin(255, y1f - cbf * bt709_coeff_bg - crf * bt709_coeff_rg);
        float b1f = fmin(255, y1f + cbf);
        float r2f = fmin(255, y2f + crf);
        float g2f = fmin(255, y2f - cbf * bt709_coeff_bg - crf * bt709_coeff_rg);
        float b2f = fmin(255, y2f + cbf);

        outbuffer[0] = (unsigned char)r1f;
        outbuffer[1] = (unsigned char)g1f;

M pyatem/protocol.py => pyatem/protocol.py +3 -1
@@ 402,12 402,14 @@ if __name__ == '__main__':

    def downloaded(store, slot, data):
        logging.info('Downloaded {}:{}'.format(store, slot))
        with open(f'/workspace/usb-{store}-{slot}.bin', 'wb') as handle:
            handle.write(data)
        data = pyatem.mediaconvert.atem_to_rgb(data, 1920, 1080)
        with open(f'/workspace/usb-{store}-{slot}.data', 'wb') as handle:
            handle.write(data)


    def progress(factor):
    def progress(store, slot, factor):
        print(factor * 100)



M utils/atem-to-rgb.c => utils/atem-to-rgb.c +13 -7
@@ 1,4 1,5 @@
#include <stdio.h>
#include <math.h>

const double bt709_coeff_r = 0.2126;
const double bt709_coeff_g = 0.7152;


@@ 30,7 31,7 @@ const double bt709_bi_range = bt709_coeff_bi / cr_middle;

int main() {
    freopen(NULL, "rb", stdin);
    freopen(NULL, "wb", stdout);
    //freopen(NULL, "wb", stdout);
    char buffer[8] = { 0 };
    char outbuffer[8] = { 0 };
    int pixel_size = 8;


@@ 50,12 51,17 @@ int main() {
        float y1f = ((double)(y1 << 6)-y_offset) / y_range;
        float y2f = ((double)(y2 << 6)-y_offset) / y_range;

        float r1f = y1f + crf;
        float g1f = y1f - cbf * bt709_coeff_bg - crf * bt709_coeff_rg;
        float b1f = y1f + cbf;
        float r2f = y2f + crf;
        float g2f = y2f - cbf * bt709_coeff_bg - crf * bt709_coeff_rg;
        float b2f = y2f + cbf;
        // printf("Y: %f Cr: %f Cb: %f\n", y1f, cbf, crf);

        float r1f = fmin(255, y1f + crf);
        float g1f = fmin(255, y1f - cbf * bt709_coeff_bg - crf * bt709_coeff_rg);
        float b1f = fmin(255, y1f + cbf);
        float r2f = fmin(255, y2f + crf);
        float g2f = fmin(255, y2f - cbf * bt709_coeff_bg - crf * bt709_coeff_rg);
        float b2f = fmin(255, y2f + cbf);

        //printf("R: %f G: %f B: %f\n", r1f, g1f, b1f);
        //return 1;

        outbuffer[0] = (unsigned char)r1f;
        outbuffer[1] = (unsigned char)g1f;