生のフレームを nv12 形式でエンコードしようとしています。フレーム レートは 15 です。エンコードには avcodec を使用しています。私のキャプチャ デバイスには、未加工のビューファインダー フレームが利用可能になったときにアクティブになるコールバック関数があります。生のビューファインダー フレームをコピーし、データから AVFrame を作成しています。次に、API サンプルで説明されているように avcodec_encode_video にフレームを提供しますが、どういうわけか期待どおりの結果が得られません。私はposixスレッドを使用しています。生のフレームをバッファに保持します。次に、エンコーダー スレッドがバッファーからデータを収集し、エンコードします。エンコードの速度が遅すぎます (h264 および mpeg1 でテスト済み)。それは私のスレッドか何かに問題がありますか? 私は途方に暮れています。出来上がりが謎。エンコーディング プロセス全体は単一の関数であり、シングル スレッドですが、一度に多数のフレームがエンコードされていることがわかります。
while(cameraRunning)
{
pthread_mutex_lock(&lock_encoder);
if(vr->buffer->getTotalData()>0)
{
i++;
fprintf(stderr,"Encoding %d\n",i);
AVFrame *picture;
int y = 0,x;
picture = avcodec_alloc_frame();
av_image_alloc(picture->data, picture->linesize,c->width, c->height,c->pix_fmt, 1);
uint8_t* buf_source = new uint8_t[vr->width*vr->height*3/2];
uint8_t* data = vr->buffer->Read(vr->width*vr->height*3/2);
memcpy(buf_source,data,vr->width*vr->height*3/2);
//free(&vr->buffer->Buffer[vr->buffer->getRead()-1][0]);
/*for (y = 0; y < vr->height*vr->width; y++)
{
picture->data[0][(y/vr->width) * picture->linesize[0] + (y%vr->width)] = buf_source[(y/vr->width)+(y%vr->width)]x + y + i * 7;
if(y<vr->height*vr->width/4)
{
picture->data[1][(y/vr->width) * picture->linesize[1] + (y%vr->width)] = buf_source[vr->width*vr->height + 2 * ((y/vr->width)+(y%vr->width))]128 + y + i * 2;
picture->data[2][(y/vr->width) * picture->linesize[2] + (y%vr->width)] = buf_source[vr->width*vr->height + 2 * ((y/vr->width)+(y%vr->width)) + 1]64 + x + i * 5;
}
}
*/
for(y=0;y<c->height;y++) {
for(x=0;x<c->width;x++) {
picture->data[0][y * picture->linesize[0] + x] = x + y + i * 7;
}
}
/* Cb and Cr */
for(y=0;y<c->height/2;y++) {
for(x=0;x<c->width/2;x++) {
picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
}
}
free(buf_source);
fprintf(stderr,"Data ready\n");
outbuf_size = 100000 + c->width*c->height*3/2;
outbuf = (uint8_t*)malloc(outbuf_size);
fprintf(stderr,"Preparation done!!!\n");
out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
had_output |= out_size;
printf("encoding frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
av_free(picture->data[0]);
av_free(picture);
}
pthread_mutex_unlock(&lock_encoder);
}