Question

I need to decode video from pipe or socket, then convert it set of images and draw with Qt(4.8.5!!). I'm using default example of libAV and adding to it what i need.

Here is my code:

    AVCodec *codec;
    AVCodecContext *codecContext= NULL;
    int frameNumber, got_picture, len;
    FILE *f;
    AVFrame *avFrame, *avFrameYUV, *avFrameRGB;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    AVPacket avpkt;

    av_init_packet(&avpkt);

    f = fopen("/tmp/test.mpg", "rb");
     if (!f) {
         fprintf(stderr, "could not open /tmp/test.mpg\n");
         exit(1);
     }

     /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
     memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);

     /* decode until eof */
     avpkt.data = inbuf;
     avpkt.size = fread(inbuf, 1, INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE, f);

    /* find the mpeg1 video decoder */
    codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    codecContext = avcodec_alloc_context3(codec);
    codecContext->get_format = &my_get_format;
    avFrameYUV = avcodec_alloc_frame();
    avFrameRGB = avcodec_alloc_frame();

    if(codec->capabilities&CODEC_CAP_TRUNCATED)
        codecContext->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */

    /* For some codecs, such as msmpeg4 and mpeg4, width and height
       MUST be initialized there because this information is not
       available in the bitstream. */

    /* open it */
    if (avcodec_open2(codecContext, codec, NULL) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    /* the codec gives us the frame size, in samples */

    int srcX = 352;  //Size of output picture in example
    int srcY = 288;
    struct SwsContext *swsContext = sws_getContext(srcX, srcY, AV_PIX_FMT_YUV420P,
                                                srcX, srcY, PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);
    frameNumber = 0;
    for(;;) {
        if (avpkt.size == 0)
            break;
        avpkt.data = inbuf;
        while (avpkt.size > 0) {
            len = avcodec_decode_video2(codecContext, avFrameYUV, &got_picture, &avpkt);
            if (len < 0) {
                fprintf(stderr, "Error while decoding frame %d\n", frameNumber);
                exit(1);
            }
            if (got_picture) {
                printf("saving frame %3d\n", frameNumber);
                fflush(stdout);

                sws_scale(swsContext, avFrameYUV->data, avFrameYUV->linesize, 0, dstY, avFrameRGB->data, avFrameRGB->linesize);

                myImage = new QImage(avFrameRGB->data[0], srcX, srcY, QImage::Format_RGB32);

                emit update(myImage); // Here i draw it to the screen
                usleep(50000);
                frameNumber++;
            }
            avpkt.size -= len;
            avpkt.data += len;
        }
    }

    avpkt.data = NULL;
    avpkt.size = 0;
    len = avcodec_decode_video2(codecContext, avFrameYUV, &got_picture, &avpkt);
    if (got_picture) {
        printf("saving last frame %3d\n", frameNumber);
        fflush(stdout);
        frameNumber++;
    }

    avcodec_close(codecContext);
    av_free(codecContext);
    avcodec_free_frame(&avFrameRGB);
    avcodec_free_frame(&avFrameYUV);

Now it doesn't work: "[swscaler @ 0xb0005460] bad dst image pointers" misstake, but the questions are: -What i'm doing wrong globaly?

-Do i need a AVPicture? Like here ?

-Is QImage - best stuff do draw frame?

-I give sws_scale() two different initialized frames and it crashes. Why?

Was it helpful?

Solution

I would so something like this:

AVPicture m_Rgb;
int srcX = codecContext->width;
int srcY = codecContext->height;
struct SwsContext *swsContext = sws_getContext(srcX, srcY, codecContext->pix_fmt, srcX, srcY, PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);
m_Rgb.linesize[0] = srcX * 4;
m_Rgb.data[0]     = (uint8_t*)malloc( m_Rgb.linesize[0] * srcY );
sws_scale(swsContext, avFrameYUV->data, avFrameYUV->linesize, 0, srcY, m_Rgb.data, m_Rgb.linesize );

QImage image( m_Rgb.data[0], srcX, srcY, QImage::Format_RGB32 );
image = image.copy(); // This makes a deep copy of the pixels. Currently image just has a copy of the m_Rgb.data[0] pointer

You can reuse m_Rgb over and over, and just delete it at the end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top