ffmpeg 线程 decode 问题

maikforever 2011-09-07 02:52:14
有人遇到过吗,我在另一个线程 avcodec_decode_video 结果第一帧检测出来是 B 帧,事实上应该是 I 帧,造成后面的都花屏了。

如果直接在 avcodec_read_frame 后面调用的话是正常的,但是我单独单出来一个线程就不行了。只有 ts 的特别的文件是这样子的。

是我用错了吗,还是别的什么问题?



void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
FILE *pFile;
char szFilename[32];
int y;

// Open file
sprintf(szFilename, "c:\\frame%d.ppm", iFrame);
pFile=fopen(szFilename, "wb");
if(pFile==NULL)
return;

// Write header
fprintf(pFile, "P6\n%d %d\n255\n", width, height);

// Write pixel data
for(y=0; y<height; y++)
fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);

// Close file
fclose(pFile);
}

int testcase(const char * file)
{
int i;
AVCodecContext *pCodecCtx;
AVFrame *pFrame;
AVCodec *pCodec;
AVFormatContext *pFormatCtx;

av_register_all();

// Open video file
if(av_open_input_file(&pFormatCtx, file, NULL, 0, NULL) != 0)
return -1; // Couldn't open file
// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information

// Find the first video stream
int videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
if(videoStream==-1)
return -1; // Didn't find a video stream

// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;

// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
return -1; // Could not open codec

// Allocate video frame
pFrame=avcodec_alloc_frame();

// Allocate an AVFrame structure
AVFrame *pFrameRGB=avcodec_alloc_frame();
if(pFrameRGB==NULL)
return -1;

uint8_t *buffer;
int numBytes;
// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height);

int frameFinished;
AVPacket packet;

i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
&packet);

// Did we get a video frame?
if(frameFinished) {
// Convert the image from its native format to RGB
// img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24,
// (AVPicture*)pFrame, pCodecCtx->pix_fmt,
// pCodecCtx->width, pCodecCtx->height);

static struct SwsContext* m_convertCtx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
PIX_FMT_RGB24,
SWS_POINT,
NULL,
NULL,
NULL);

// Convert the image from its native format to RGB
sws_scale(m_convertCtx,
pFrame->data,
pFrame->linesize,
0,
pCodecCtx->height,
pFrameRGB->data,
pFrameRGB->linesize);

// Save the frame to disk
if(++i<=5)
SaveFrame(pFrameRGB, pCodecCtx->width,
pCodecCtx->height, i);
}
}
else
{
ASSERT(false);
}

// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}

// Free the RGB image
av_free(buffer);
av_free(pFrameRGB);

// Free the YUV frame
av_free(pFrame);

// Close the codec
avcodec_close(pCodecCtx);

// Close the video file
av_close_input_file(pFormatCtx);

return 0;
}


上面的代码,拿到的第一帧是错误的。第二帧是 I 帧。后来是可以保存图像的。
但是当我把 AVPacket 压到一个队列中,然后另开了一个线程去解的时候,发现
第二帧,就是 5006 的那帧,解出来报的是 B 帧。。。
为什么同样的代码,难道放线程里还不一样吗。

FFMPEG 是不是这样用的,一个对,然后放在另一个线程里解。
有没有支招啊。



另外,怎么在 FFMPEG 里启动 NEON 的编译。相对安卓。

望各位达人指教。谢谢了。

文件已上传 http://good.gd/1578793.htm
大家帮忙看看是不是第一帧真的坏了?
...全文
307 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
maikforever 2011-09-23
  • 打赏
  • 举报
回复
谢谢,我找到问题了,是没有调用 av_dump_packet 导致上一个帧被冲掉了。
现在还有那个NEON编译的问题,都搞了2个多月了,还没有搞定。
哪位可以帮我看看为什么加上链接就不行了呢。
http://groups.google.com/group/android-ndk/browse_thread/thread/98617ab77b966785?hl=en
sxcong 2011-09-21
  • 打赏
  • 举报
回复
应该是帧之间有联系,分在两个线程里这个联系被断了。avcodec_read_frame,好象还有个read_next_frame之类的东西。
最好还是一个线程里处理,因为读文件快,解码慢,你分在两个线程里,就算解码正常,读文件这个线程隔一会还要sleep一下等解码。分成两个线程没什么意思。
或者,你读文件读到内存文件里,另一个线程从这个内存里再avcodec_read_frame。
以上是实现项目的方法,没有找出为什么出错。估计你要读一下代码才行。
小蚊子 2011-09-15
  • 打赏
  • 举报
回复
没有玩过这个,不过感觉是不是ffmpeg的问题呢???
sxcong 2011-09-14
  • 打赏
  • 举报
回复
第一帧是B帧就丢掉好了,从I开始
maikforever 2011-09-14
  • 打赏
  • 举报
回复
第一帧丢掉了,当把 Packet 压到了一个线程里的时候,这个文件返回第二帧是 P 帧,而实际情况第二帧是 I 帧,从上面的倒子上就看到了。

而且是只有这个文件有问题。可以播着试验一下吗
[Quote=引用 1 楼 sxcong 的回复:]

第一帧是B帧就丢掉好了,从I开始
[/Quote]

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧