2009-08-31 4 views
2

libavcodecを使用して、HBitmapをビデオストリームに変換したいと思います。 は、私が使用して私のHBITMAPを取得:ビットマップをビデオに保存する(libavcodec ffmpeg)

HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight); 
SelectObject(hCaptureDC,hCaptureBitmap); 
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY); 

をそして、私は(私が使用しているコーデックによって必要とされる)、YUVに変換したいと思います。そのために私は次のものを使用します:

SwsContext *fooContext = sws_getContext(c->width,c->height,PIX_FMT_BGR32, c->width,c->height,PIX_FMT_YUV420P,SWS_FAST_BILINEAR,NULL,NULL,NULL); 

uint8_t *movie_dib_bits = reinterpret_cast<uint8_t *>(bm.bmBits) + bm.bmWidthBytes * (bm.bmHeight - 1); 

int dibrowbytes = -bm.bmWidthBytes; 

uint8_t* data_out[1]; 
int stride_out[1]; 
data_out[0] = movie_dib_bits; 
stride_out[0] = dibrowbytes; 

sws_scale(fooContext,data_out,stride_out,0,c->height,picture->data,picture->linesize); 

しかし、これはまったく動作しません。なぜでしょうか?あるいは私はそれをどうやって変えることができますか?

ありがとうございました!

答えて

5

私はあなたがビットマップを取得するために使用されているものに慣れていないですが、それが正しいと仮定すると、あなたはこのような何かしようと、BGR 32ビット/ピクセルデータへのポインタを持っている:

uint8_t* inbuffer; 
int in_width, in_height, out_width, out_height; 

//here, make sure inbuffer points to the input BGR32 data, 
//and the input and output dimensions are set correctly. 

//calculate the bytes needed for the output image 
int nbytes = avpicture_get_size(PIX_FMT_YUV420P, out_width, out_height); 

//create buffer for the output image 
uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes); 

//create ffmpeg frame structures. These do not allocate space for image data, 
//just the pointers and other information about the image. 
AVFrame* inpic = avcodec_alloc_frame(); 
AVFrame* outpic = avcodec_alloc_frame(); 

//this will set the pointers in the frame structures to the right points in 
//the input and output buffers. 
avpicture_fill((AVPicture*)inpic, inbuffer, PIX_FMT_BGR32, in_width, in_height); 
avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, out_width, out_height); 

//create the conversion context 
SwsContext* fooContext = sws_getContext(in_width, in_height, PIX_FMT_BGR32, out_width, out_height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL); 

//perform the conversion 
sws_scale(fooContext, inpic->data, inpic->linesize, 0, in_height, outpic->data, outpic->linesize); 

//encode the frame here... 

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

もちろん、一連のフレームを変換しようとするならば、最初に1回、最後に1回割り振りを行うだけです。

関連する問題