Hello,

Aim of the code I am writting is to stream through the internet a stream
of frames.
My frames are IplImage (from the openCV library), so I can access to the
data buffer of my images.

What is the easiest way to proceed ?

I started by instanciate a codec and a codec context :

==== CODE ====

    av_register_all();

    AVCodec* codec = avcodec_find_encoder(CODEC_ID_MPEG4);
    if(!codec)
    {
        cout << "can't find encoder" << endl;
        return -1;
    }
    AVCodecContext* context = avcodec_alloc_context();

    context->bit_rate = 250000;
    context->time_base = (AVRational){1,25};
    context->pix_fmt = PIX_FMT_YUV420P;
    context->width = 640;
    context->height = 480;
    context->gop_size = 10;
    context->max_b_frames = 1;

    if(avcodec_open(context, codec)<0)
    {
        cout << "Can't open codec" << endl;
        return -1;
    }

==== /CODE ====

Then, I create an UrlContext :

==== /CODE =====

    URLContext* urlContext = NULL;
    if(url_open(&urlContext,"rtp://127.0.0.1:1234", URL_WRONLY) < 0)
    {
        cout << "can't open url" << endl;
        return -1;
    }

==== /CODE =====

Then, I create a Frame, and I fill this frame with my Image :
==== CODE =====
    AVFrame* frame = avcodec_alloc_frame();
    int imgSize = 640*480;
    int encodedSize = 0;
    uint8_t *outbuf;
    outbuf = (uint8_t *)malloc(imgSize*3);

    char c = 0;
    IplImage *yuv_img=NULL;
    yuv_img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);

    frame->data[0] = (uint8_t *)yuv_img->imageDataOrigin;
    frame->data[1] = frame->data[0]+imgSize;
    frame->data[2] = frame->data[1]+imgSize/4;
    frame->linesize[0] = yuv_img->widthStep;
    frame->linesize[1] = frame->linesize[2] = yuv_img->widthStep/2;

==== /CODE =====
Then, in a loop I fill my OpenCV Image, and I try to send my frame through
internet.

==== CODE ====
    while(c!='q')
    {
        img = cvQueryFrame(cam);
        cvConvertImage(yuv_img, yuv_img, CV_CVTIMG_SWAP_RB);
        cvConvertImage(img, yuv_img, CV_RGB2YCrCb);

        encodedSize = avcodec_encode_video(context, outbuf, (imgSize*3)/2,
frame);
        cout << "encoded size : " << encodedSize << " rough size" <<
(imgSize*3)/2 << endl;
        int err = url_write(urlContext, (unsigned char *)outbuf+4,
encodedSize);
        if(err < 0)
        {
            cout << " ERR: can't send data :" << err << endl;
        }
        c = cvWaitKey(20);
    }
==== /CODE ====

When I run this code, I have got a lot of "err == -5".

Could you please tell me what am I doing wrong (a lot of things ...).

Any advise will be welcome...

Kings regards, Adrien BARRAL

_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to