user1573610
user1573610

Reputation: 43

FFMPEG - Read video file and convert to Bitmap

I can successfully read a video file using ffmpeg

Now i want to show these video frames on my MFC mdi.

For that i need a bitmap to feed my CBitmap::FromHandle() function

memDC.CreateCompatibleDC(dc);

CBitmap * bmp = CBitmap::FromHandle();

CBitmap * oldBmp = memDC.SelectObject(bmp);

dc->BitBlt(0,0,320,240,&memDC,0,0,SRCCOPY);

For ffmpeg i am using dranger tutorial 01

http://dranger.com/ffmpeg/tutorial01.html

Please advise how to convert frames to Bitmap

Thanks

Upvotes: 0

Views: 6408

Answers (1)

user1573610
user1573610

Reputation: 43

I have been working on it for the last 3 days and gotten this far

Now Frames are BitBlt to dc successfully but they are upside down

Please let me know if there is anything wrong with the code

avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
               pCodecCtx->width, pCodecCtx->height);


int w = pCodecCtx->width;
int h = pCodecCtx->height;
img_convert_ctx = sws_getContext(w, h, pCodecCtx->pix_fmt,
                                 w, h, PIX_FMT_RGB24,
                                 SWS_BICUBIC, NULL, NULL, NULL);

/*CClientDC dc;*/
BITMAPINFO bmi = {0};
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biHeight = -pCodecCtx->height;
bmi.bmiHeader.biWidth = pCodecCtx->width;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biSizeImage = pCodecCtx->height * pCodecCtx->width * 3;
hbmp = CreateDIBSection(hdcmem, &bmi, DIB_RGB_COLORS, &pbmpdata , NULL, 0); //&pbmpdata

hdcscr = GetDC(0);
hdcmem = CreateCompatibleDC(hdcscr);


i=0;
while((av_read_frame(pFormatCtx, &packet)>=0)) { 
    // Is this a packet from the video stream?
    if(packet.stream_index==videoStreamIdx) {

        /// Decode video frame
        //avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);
        avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

        // Did we get a video frame?
        if(frameFinished) {
            i++;
            sws_scale(img_convert_ctx, pFrame->data,
                      pFrame->linesize, 0, pCodecCtx->height,
                      pFrameRGB->data, pFrameRGB->linesize);


            pFrameRGB->data[0] = (uint8_t*)pbmpdata;
            pFrameRGB->linesize[0] = pCodecCtx->width * 3;

            SelectObject(hdcmem, hbmp);//hbmp


            BitBlt(hdcscr, 0, 0, pCodecCtx->width, pCodecCtx->height, hdcmem , 0, 0, SRCCOPY);
            Sleep(10);
        }
    }

    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);
}

Upvotes: 1

Related Questions