读书人

ffmpeg中av_seek_frame() 的有关问题

发布时间: 2013-01-06 15:44:47 作者: rapoo

ffmpeg中av_seek_frame() 的问题
ffmpeg中解码具体的某一帧,用到av_seek_frame,但是不清楚具体步骤是什么样的。
希望能给个具体的例子,看看到底要用到哪些函数
以下是ffmpeg中的seek的例子,在哪些地方加解码函数才能获得想要的具体某一帧的图像?

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#include "libavformat/avformat.h"

#undef exit

int main(int argc, char **argv)
{
const char *filename;
AVFormatContext *ic;
int i, ret, stream_id;
int64_t timestamp;
AVFormatParameters params, *ap= &params;
memset(ap, 0, sizeof(params));
ap->channels=1;
ap->sample_rate= 22050;

/* initialize libavcodec, and register all codecs and formats */
av_register_all();

if (argc != 2) {
printf("usage: %s input_file\n"
"\n", argv[0]);
exit(1);
}

filename = argv[1];

/* allocate the media context */
ic = avformat_alloc_context();
if (!ic) {
fprintf(stderr, "Memory error\n");
exit(1);
}

ret = av_open_input_file(&ic, filename, NULL, 0, ap);
if (ret < 0) {
fprintf(stderr, "cannot open %s\n", filename);
exit(1);
}

ret = av_find_stream_info(ic);
if (ret < 0) {
fprintf(stderr, "%s: could not find codec parameters\n", filename);
exit(1);
}

for(i=0; ; i++){
AVPacket pkt;
AVStream *st;

memset(&pkt, 0, sizeof(pkt));
if(ret>=0){
ret= av_read_frame(ic, &pkt);
printf("ret:%2d", ret);
if(ret>=0){
st= ic->streams[pkt.stream_index];
printf(" st:%2d dts:%f pts:%f pos:%" PRId64 " size:%d flags:%d", pkt.stream_index, pkt.dts*av_q2d(st->time_base), pkt.pts*av_q2d(st->time_base), pkt.pos, pkt.size, pkt.flags);
}
printf("\n");


}

if(i>25) break;

stream_id= (i>>1)%(ic->nb_streams+1) - 1;
timestamp= (i*19362894167LL) % (4*AV_TIME_BASE) - AV_TIME_BASE;
if(stream_id>=0){
st= ic->streams[stream_id];
timestamp= av_rescale_q(timestamp, AV_TIME_BASE_Q, st->time_base);
}
ret = av_seek_frame(ic, stream_id, timestamp, (i&1)*AVSEEK_FLAG_BACKWARD);
printf("ret:%2d st:%2d ts:%f flags:%d\n", ret, stream_id, timestamp*(stream_id<0 ? 1.0/AV_TIME_BASE : av_q2d(st->time_base)), i&1);
}

return 0;
}


谢谢
[解决办法]
清参考ffmpeg源代码,调试跟踪里面的流程,如果文件有index或者码流信息,回去找最近的indeX SEEK到I祯,如果没有会按照byte Seek,具体参考下面的函数:

int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
{
int ret;
AVStream *st;

ff_read_frame_flush(s);

if(flags & AVSEEK_FLAG_BYTE)
return av_seek_frame_byte(s, stream_index, timestamp, flags);

if(stream_index < 0){
stream_index= av_find_default_stream_index(s);
if(stream_index < 0)
return -1;

st= s->streams[stream_index];
/* timestamp for default must be expressed in AV_TIME_BASE units */
timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
}

/* first, we try the format specific seek */
if (s->iformat->read_seek)
ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
else
ret = -1;
if (ret >= 0) {
return 0;
}

if(s->iformat->read_timestamp)
return av_seek_frame_binary(s, stream_index, timestamp, flags);
else
return av_seek_frame_generic(s, stream_index, timestamp, flags);
}

[解决办法]
找到I frame以后,继续readpkt,avcode_decode_video(),和以前的流程一样
------解决方案--------------------


1)执行av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags),定位到timestamp前的最近关键帧;
2)使用av_read_frame读取数据包;
3)根据包类型分别执行音视频解码。
// 定位
av_seek_frame(pFormatCtx, -1, lStart*1000, AVSEEK_FLAG_BACKWARD
[解决办法]
AVSEEK_FLAG_ANY);
avcodec_flush_buffers(pFormatCtx->streams[video_stream]->codec);
avcodec_flush_buffers(pFormatCtx->streams[audio_stream]->codec);

while(av_read_frame(pFormatCtx, &packet)>=0)
{
// 视频解码
if(packet.stream_index==video_stream)
{
// 从关键帧处开始解码
len_video= avcodec_decode_video(decodec_video, picture, &get_picture,packet.data, packet.size);
if (get_picture)
{
// 根据packet.dts判断是否是想要的视频帧
……
}

}
// 音频解码
else if (packet.stream_index==audio_stream)
{
len_audio= avcodec_decode_audio3 (decodec_audio,p_decompressed_audio_buf,
&decompressed_audio_buf_size, &packet );
if ( len_audio != -1 )
{
……
}
}
av_free_packet(&packet);
}




[解决办法]
在while(av_read_frame(pFormatCtx, &packet)>=0)之前添加代码:
av_seek_frame(pFormatCtx, -1, lStart*1000, AVSEEK_FLAG_BACKWARD
[解决办法]
AVSEEK_FLAG_ANY);
avcodec_flush_buffers(pFormatCtx->streams[video_stream]->codec);
avcodec_flush_buffers(pFormatCtx->streams[audio_stream]->codec);
其中,lStart自己设定,单位毫秒。
[解决办法]
可能输入 av_seek_frame 的参数没有给对吧。

读书人网 >多媒体

热点推荐