我想用Qt结合ffmpeg写一个简单的播放avi的程序,关键代码如下:
void MainWindow::playVideo(char *videoName)
{
AVFormatContext *pFormatCtx;
int i, videoStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVPacket packet;
int frameFinished;
AVFrame *pFrame, *pFrameRGB;
int numBytes;
uint8_t *buffer;
av_register_all();
if (av_open_input_file(&pFormatCtx, videoName, NULL, 0, NULL) != 0)
handleError();
if (av_find_stream_info(pFormatCtx) < 0)
handleError();
videoStream = -1;
for(i = 0; i < pFormatCtx->nb_streams; i++)
if (pFormatCtx->streams[0]->codec->codec_type == CODEC_TYPE_VIDEO)
{
videoStream = i;
break;
}
if (videoStream == -1)
handleError();
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL)
handleError();
if (pCodec->capabilities & CODEC_CAP_TRUNCATED)
pCodecCtx->flags |= CODEC_CAP_TRUNCATED;
if (avcodec_open(pCodecCtx, pCodec) < 0)
handleError();
pFrame = avcodec_alloc_frame();
pFrameRGB = avcodec_alloc_frame();
if (pFrame == NULL || pFrameRGB == NULL)
handleError();
numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
buffer = new uint8_t[numBytes];
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
while (av_read_frame(pFormatCtx, &packet) >= 0)
{
if (packet.stream_index == videoStream)
{
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet.data, packet.size);
}
if (frameFinished)
{
img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, (AVPicture *)pFrame,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
showPicture(pFrameRGB, pCodecCtx->width, pCodecCtx->height);
}
av_free_packet(&packet);
}
delete [] buffer;
av_free(pFrameRGB);
av_free(pFrame);
avcodec_close(pCodecCtx);
av_close_input_file(pFormatCtx);
}
使用QtCreator能通过编译,但就是无法运行,老是exited with code -1073741515
如果把这部分代码注释掉,程序就能正常运行了,如果只保留av_register_all()也是运行不起来,
请问各位高手这是什么原因??是代码中什么地方写错了吗?