RGB to YUV ofPixels ffmpeg rtmp send


I am trying to convert an image which I have loaded into an array of ofPixels to send it with ffmpeg.
I receive - on the other end - only green pixels missaligned.
My hint is that i have to convert my pixels from RGB to YUV as the pixel format is YUV420P, to send with the codec id AV_CODEC_ID_H264.
I have seen this, but it doesn’t work for me, as the app crashes.

I have based my code on this repo, which works fine with an cv::Mat from the camera:

In Streamer.cpp :

void Streamer::stream_frame(const uint8_t *data, int64_t frame_duration)
    if(can_stream()) {

        const int stride[] = {static_cast< unsigned char >(config.src_width * 3)};
        int h = sws_scale(scaler.ctx, &data, stride, 0, config.src_height, picture.frame->data, picture.frame->linesize);
        if(h != config.dst_height){
            printf("wrong height");
        picture.frame->pts += frame_duration; //time of frame in milliseconds
        //picture.frame->pkt_dts = picture.frame->pts; 
        encode_and_write_frame(out_codec_ctx, format_ctx, picture.frame);

static int encode_and_write_frame(AVCodecContext *codec_ctx, AVFormatContext *fmt_ctx, AVFrame *frame)
    AVPacket pkt = {0};

    int ret = avcodec_send_frame(codec_ctx, frame);
    if (ret < 0)
        fprintf(stderr, "Error sending frame to codec context!\n");
        return ret;

    while (ret >= 0) {
        ret = avcodec_receive_packet(codec_ctx, &pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
        else if (ret < 0) {
            fprintf(stderr, "Error during encoding\n");

        // avio_write(server, pkt.data, pkt.size);
        av_interleaved_write_frame(fmt_ctx, &pkt);

    return 0;

ofApp.cpp::setup() :
bikers.jpg, coming from the example load image in OF examples ( 3 channel rgb image ).

// p is  ofPixels p;
ofLoadImage(p, "bikers.jpg");

ofApp.cpp::update() :

if(ofGetElapsedTimeMillis() - timeStreaming > streamingInterval){
//p is my array of pixels
        float delta = ofGetElapsedTimeMillis() - timeStreaming;
        streamer.stream_frame(p.getData(), (int64_t)(delta * .001 * streamer.inv_stream_timebase) );

I’m not sure how to proceed, as i haven’t done a lot of ffmpeg codecs / context / format / etc… work before, and would appreciate any pointers.