簡體   English   中英

將QImage轉換為YUV420P像素格式

[英]Converting QImage to YUV420P pixel format

有人先前解決了這個問題嗎? 我需要簡單快速的方法將QImage :: bits()緩沖區從RGB32轉換為YUV420P像素格式。 你能幫助我嗎?

libswscaleffmpeg項目的一部分,它優化了例程來執行顏色空間轉換,縮放和過濾。 如果你真的想要速度,我會建議使用它,除非你不能添加額外的依賴。 我實際上沒有測試過這段代碼,但這里是一般的想法:

QImage img = ...     //your image in RGB32

//allocate output buffer.  use av_malloc to align memory.  YUV420P 
//needs 1.5 times the number of pixels (Cb and Cr only use 0.25 
//bytes per pixel on average)
char* out_buffer = (char*)av_malloc((int)ceil(img.height() * img.width() * 1.5));

//allocate ffmpeg frame structures
AVFrame* inpic = avcodec_alloc_frame();
AVFrame* outpic = avcodec_alloc_frame();

//avpicture_fill sets all of the data pointers in the AVFrame structures
//to the right places in the data buffers.  It does not copy the data so
//the QImage and out_buffer still need to live after calling these.
avpicture_fill((AVPicture*)inpic, 
               img.bits(), 
               AV_PIX_FMT_ARGB, 
               img.width(), 
               img.height());
avpicture_fill((AVPicture*)outpic, 
               out_buffer, 
               AV_PIX_FMT_YUV420P, 
               img.width(),
               img.height());

//create the conversion context.  you only need to do this once if
//you are going to do the same conversion multiple times.
SwsContext* ctx = sws_getContext(img.width(), 
                                 img.height(), 
                                 AV_PIX_FMT_ARGB, 
                                 img.width(), 
                                 img.height(), 
                                 AV_PIX_FMT_YUV420P, 
                                 SWS_BICUBIC, 
                                 NULL, NULL, NULL);

//perform the conversion
sws_scale(ctx, 
          inpic->data, 
          inpic->linesize, 
          0, 
          img.height(), 
          outpic->data, 
          outpic->linesize);

//free memory
av_free(inpic);
av_free(outpic);

//...

//free output buffer when done with it 
av_free(out_buffer);

就像我說的,我沒有測試過這段代碼,因此可能需要進行一些調整才能使其正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM