2013-01-29 10 views

ответ

4

libswscale, часть проекта ffmpeg имеет оптимизированные подпрограммы для преобразования цветов, масштабирования и фильтрации. Если вы действительно хотите скорость, я бы предложил использовать ее, если вы не можете добавить дополнительную зависимость. На самом деле я не проверял этот код, но вот общая идея:

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); 

Как я уже сказал, я не проверял этот код, он может потребовать некоторые хитрости, чтобы заставить его работать.

Смежные вопросы