I'm wrote the following code to convert a decoded video frame to a PNG image. The code doesn't crash but the image data stored in 'avpkt' results in an all green image. What am I doing wrong? Any help would be appreciated.
// pFrame - the decoded frame
// avpkt - the packet to fill with the converted image
void convert_image(AVCodecContext *pCodecCtx, AVFrame *pFrame, AVPacket *avpkt, int *got_packet_ptr) {
AVCodecContext *codecCtx;
AVCodec *codec;
*got_packet_ptr = 0;
codec = avcodec_find_encoder(TARGET_IMAGE_CODEC);
if (!codec) {
printf("avcodec_find_decoder() failed to find decoder\n");
goto fail;
}
codecCtx = avcodec_alloc_context3(codec);
if (!codecCtx) {
printf("avcodec_alloc_context3 failed\n");
goto fail;
}
codecCtx->bit_rate = pCodecCtx->bit_rate;
codecCtx->width = pCodecCtx->width;
codecCtx->height = pCodecCtx->height;
codecCtx->pix_fmt = TARGET_IMAGE_FORMAT;
codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
codecCtx->time_base.num = pCodecCtx->time_base.num;
codecCtx->time_base.den = pCodecCtx->time_base.den;
if (!codec || avcodec_open2(codecCtx, codec, NULL) < 0) {
printf("avcodec_open2() failed\n");
goto fail;
}
int src_width = pCodecCtx->width;
int src_height = pCodecCtx->height;
enum PixelFormat src_pixfmt = pCodecCtx->pix_fmt;
int dst_width = pCodecCtx->width;
int dst_height = pCodecCtx->height;
struct SwsContext *scalerCtx;
scalerCtx = sws_getContext(src_width,
src_height,
src_pixfmt,
dst_width,
dst_height,
TARGET_IMAGE_FORMAT,
SWS_BILINEAR, //SWS_BICUBIC
NULL, NULL, NULL);
if (!scalerCtx) {
printf("sws_getContext() failed\n");
goto fail;
}
AVFrame *pSrcFrame = avcodec_alloc_frame();
if (!pSrcFrame) {
goto fail;
}
AVFrame *pFrameRGB = avcodec_alloc_frame();
if (!pFrameRGB) {
goto fail;
}
if (avpicture_fill((AVPicture *) pSrcFrame,
pFrame->data,
src_pixfmt,
src_width,
src_height) < 0) {
printf("avpicture_fill() failed\n");
goto fail;
}
int numBytes = avpicture_get_size(TARGET_IMAGE_FORMAT, src_width, src_height);
uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
if (avpicture_fill((AVPicture *) pFrameRGB,
buffer,
TARGET_IMAGE_FORMAT,
src_width,
src_height) < 0) {
printf("avpicture_fill() failed\n");
goto fail;
}
sws_scale(scalerCtx,
(const uint8_t * const *) pSrcFrame->data,
pSrcFrame->linesize,
0,
src_height,
pFrameRGB->data,
pFrameRGB->linesize);
int ret = avcodec_encode_video2(codecCtx, avpkt, pFrameRGB, got_packet_ptr);
if (ret < 0) {
*got_packet_ptr = 0;
}
fail:
if (codecCtx) {
avcodec_close(codecCtx);
}
if (scalerCtx) {
sws_freeContext(scalerCtx);
}
if (ret < 0 || !*got_packet_ptr) {
av_free_packet(avpkt);
}
}