2008/4/17, Tijl Vercaemer <[EMAIL PROTECTED]>:
> Hi,
>
>  I'm trying to encode video to mjpeg with libavcodec. I've written a
>  test application in which I have a buffer with a test pattern, that I
>  encode to an mjpeg file. However, the resulting file is interlaced (I
>  see only the top half of my test pattern, and the image consists of
>  horizontal stripes, half of them containing the left half of the test
>  pattern, the others containing the right half). That's not what I
>  want, I don't want the resulting video to be interlaced.
>
>  How can I do that? The flags variable of my AVCodecContext and the the
>  interlaced_frame member of my AVFrame are both 0. What else should I
>  do to disable interlacing?

To make this a bit more clear, I created an image of my test pattern:
http://VrijZijn.be/libavProblem/expected.png
And one of a frame from the resulting mjpeg file:
http://VrijZijn.be/libavProblem/result.png

And I added some code to this mail that could be used to reproduce the problem.

In my first mail, I said the video was interlaced. But actually, I
don't think it is. It seems more like libavcodec deinterlaced the
input. Because the input wasn't interlaced in the first place, the
result isn't what it should be.

Does anyone know how I can make sure that libavcodec doesn't
deinterlace my input?

Tijl


#define CODEC_FORMAT PIX_FMT_YUVJ422P

DWORD encodeThread(void* arg) {
        AVCodec *codec;
        AVCodecContext *c= NULL;
        int i, out_size, size, x, y, outbuf_size;
        FILE *f;
        AVFrame *picture_RGB, *picture_YUV;
        uint8_t *outbuf;
        uint16_t testbuf[320*240*2];

        encodeThreadFinished = 0;

        // find the video encoder
        codec = p_avcodec_find_encoder(CODEC_ID_MJPEG);
        if (!codec) {
                // codec not found
                ReportError(0,L"Codec not found");
                encodeThreadFinished = 1;
                return 0;
        }

        img_convert_ctx = p_sws_getContext(320,240,PIX_FMT_RGB565, 320, 240,
                        CODEC_FORMAT, SWS_FAST_BILINEAR, NULL, NULL, NULL);
        if(img_convert_ctx == NULL) {
                ReportError(0,L"Codec not initialize convert ctx");
                encodeThreadFinished = 1;
                return 0;
        }

        c= p_avcodec_alloc_context();
        picture_RGB = p_avcodec_alloc_frame();
        picture_YUV = p_avcodec_alloc_frame();

        // set parameters
        c->width = 320;
        c->height = 240;
        c->time_base= (AVRational){1,10};//(AVRational){1,25};
        c->pix_fmt = CODEC_FORMAT;

        if (p_avcodec_open(c, codec) < 0) {
                // could not open codec
                ReportError(0,L"Could not open codec");
                encodeThreadFinished = 1;
                return 0;
        }

        f = fopen("\\Temp\\testvid.mjpeg", "wb");
        if (!f) {
                //could not open file
                ReportError(0,L"Codec not open file");
                encodeThreadFinished = 1;
                return 0;
        }

        /* alloc image and output buffer */
        outbuf_size = 100000;
        outbuf = malloc(outbuf_size);
        size = c->width * c->height;

        // Prepare picture_RGB
        picture_RGB->data[0] = (uint8_t*) testbuf;//(uint8_t*) cameraBuffer;
        picture_RGB->data[1] = NULL;
        picture_RGB->data[2] = NULL;
        picture_RGB->linesize[0] = 320;
        picture_RGB->linesize[1] = 0;
        picture_RGB->linesize[2] = 0;
        picture_RGB->interlaced_frame = 0;

        // Generate the test pattern
        for(y=0;y<320*240;y++) {
                if(y<320*20) testbuf[y] = 0xF81F;
                else if (y>320*220) testbuf[y] = 0x07E0;
                else if(y%320<80) testbuf[y] = 0x001F;
                else if(y%320<160) testbuf[y] = 0xFFFF;
                else if(y%320<240) testbuf[y] = 0x0000;
                else testbuf[y] = 0xF800;
        }
        
        p_avpicture_alloc((AVPicture*) picture_YUV, CODEC_FORMAT, 320, 240);
        while(proceed) {
                int result;
                Sleep(10);
                if(!proceed)
                        break;

                // Convert the data
                result = p_sws_scale(img_convert_ctx, picture_RGB->data,
picture_RGB->linesize,
                        0, 240, picture_YUV->data, picture_YUV->linesize);

                // encode the image
                if(result>=0) { 
                        out_size = p_avcodec_encode_video(c, outbuf, 
outbuf_size, picture_YUV);
                        fwrite(outbuf, 1, out_size, f);
                }
        }
        fclose(f);
        free(outbuf);

        p_av_free(picture_YUV);
        p_av_free(picture_RGB);
        p_avcodec_close(c);
        p_av_free(c);

        encodeThreadFinished = 1;
        return 0;
}
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to