---
title: "ffmpeg + sdl -02 利用ffmpeg来分析文件信息"
author: "he91_com"
date: "2013-09-23T15:15:00Z"
tags: []
slug: "ffmpeg-sdl-02-ffmpeg"
draft: false
lastmod: "2013-09-26T18:07:31Z"
id: 1947870
url: "https://he91-com.iteye.com/blog/1947870"
---

利用ffmpeg来分析wma文件信息

STEP1: 参考ffprobe编写如下测试代码。  
fileParse.c

#include &lt;libavformat/avformat.h&gt;  
#include &lt;libavcodec/avcodec.h&gt;  
#include &lt;libavutil/avstring.h&gt;  
#include &lt;libavutil/opt.h&gt;  
#include &lt;libavutil/pixdesc.h&gt;  
#include &lt;libavutil/dict.h&gt;

#define print\_error(a, b) printf(" Open %s with error (%d) \\n", a, b)

int dec\_init()  
{  
av\_register\_all();  
avformat\_network\_init();  
}

int dec\_exit()  
{  
avformat\_network\_deinit();  
}

int dec\_parse(const char \*filename)  
{  
int err, i;  
AVFormatContext \*fmt\_ctx = NULL;  
AVDictionaryEntry \*t;  
AVInputFormat \*iformat = NULL;  
AVDictionary \*format\_opts = NULL;

if ((err = avformat\_open\_input(&fmt\_ctx, filename, iformat, &format\_opts)) &lt; 0) {  
print\_error(filename, err);  
return err;  
}  
if ((t = av\_dict\_get(format\_opts, "", NULL, AV\_DICT\_IGNORE\_SUFFIX))) {  
av\_log(NULL, AV\_LOG\_ERROR, "Option %s not found.\\n", t-&gt;key);  
return AVERROR\_OPTION\_NOT\_FOUND;  
}

/* fill the streams in the format context \*/  
if ((err = avformat\_find\_stream\_info(fmt\_ctx, NULL)) &lt; 0) {  
print\_error(filename, err);  
return err;  
}

av\_dump\_format(fmt\_ctx, 0, filename, 0);

/* bind a decoder to each input stream \*/  
for (i = 0; i &lt; fmt\_ctx-&gt;nb\_streams; i++) {  
AVStream \*stream = fmt\_ctx-&gt;streams\[i];  
AVCodec \*codec;

if (!(codec = avcodec\_find\_decoder(stream-&gt;codec-&gt;codec\_id))) {  
fprintf(stderr, "Unsupported codec with id %d for input stream %d\\n",  
stream-&gt;codec-&gt;codec\_id, stream-&gt;index);  
} else if (avcodec\_open2(stream-&gt;codec, codec, NULL) &lt; 0) {  
fprintf(stderr, "Error while opening codec for input stream %d\\n",  
stream-&gt;index);  
}  
}

avformat\_close\_input(&fmt\_ctx);  
return 0;  
}

int main(int argc, char \*\*argv)  
{  
dec\_init();

dec\_parse(argv\[1]);  
dec\_exit();  
}

Makefile文件  
.PHONY: fileParse  
INCLUDE= -I../ffmpeg-0.9.2/libavcodec -I../ffmpeg-0.9.2/libavformat -I../ffmpeg-0.9.2/libavfilter -I../ffmpeg-0.9.2/libavformat -I../ffmpeg-0.9.2/libswscale

fileParse:  
\-rm fileParse  
gcc -g ${INCLUDE} -o fileParse fileParse.c -lavformat -lavcodec -lavutil -lswscale -lbz2 -lz -lm \`sdl-config --cflags --libs\`  
chmod +x fileParse

STEP2: 使用gdb来Debug FFMPEG  
1〉修改ffmpeg配置。  
make clean  
./configure --enable-debug --disable-optimizations --disable-yasm --disable-asm  
make  
make install

2&gt; 使用gdb 来调试fileParse  
为了了解ffmpeg流程，用wma来测试。  
注意到wma采用asf封装，猜测格式检测函数是asf\_probe  
文件：libavformat/Asfdec.c  
.read\_probe = asf\_probe

gdb  
file fileParse  
b asf\_probe  
r /projects/media/wma/123.wma （测试文件路径）  
bt

#0 asf\_probe (pd=0xbffff12c) at libavformat/asfdec.c:139  
#1 0x0812a2a3 in av\_probe\_input\_format3 (pd=0xbffff1c0, is\_opened=1,  
score\_ret=0xbffff178) at libavformat/utils.c:351  
#2 0x0812a384 in av\_probe\_input\_format2 (pd=0xbffff1c0, is\_opened=1,  
score\_max=0xbffff1bc) at libavformat/utils.c:373  
#3 0x0812aae5 in av\_probe\_input\_buffer (pb=0x8c375d0, fmt=0x8c2f024,  
filename=0xbffff548 "/projects/media/wma/123.wma", logctx=0x8c2f020,  
offset=0, max\_probe\_size=1048576) at libavformat/utils.c:586  
#4 0x0812add6 in init\_input (s=0x8c2f020,  
filename=0xbffff548 "/projects/media/wma/123.wma", options=0xbffff24c)  
at libavformat/utils.c:653  
#5 0x0812ae90 in avformat\_open\_input (ps=0xbffff2a4,  
filename=0xbffff548 "/projects/media/wma/123.wma", fmt=0x0,  
options=0xbffff2a0) at libavformat/utils.c:674  
#6 0x0804a48f in dec\_parse (filename=0xbffff548 "/projects/media/wma/123.wma")  
at fileParse.c:29  
#7 0x0804a66d in main (argc=2, argv=0xbffff394) at fileParse.c:69

可见ffmpeg的调用关系如下：  
avformat\_open\_input  
↓  
init\_input  
　 ↓  
av\_probe\_input\_buffer  
　　↓  
av\_probe\_input\_format2  
　　↓  
av\_probe\_input\_format3  
　　↓

while ((fmt1 = av\_iformat\_next(fmt1))) {  
if (!is\_opened == !(fmt1-&gt;flags & AVFMT\_NOFILE))  
continue;  
score = 0;  
if (fmt1-&gt;read\_probe) {  
score = fmt1-&gt;read\_probe(&lpd);  
if(fmt1-&gt;extensions && av\_match\_ext(lpd.filename, fmt1-&gt;extensions))  
score = FFMAX(score, nodat ? AVPROBE\_SCORE\_MAX/4-1 : 1);  
} else if (fmt1-&gt;extensions) {  
if (av\_match\_ext(lpd.filename, fmt1-&gt;extensions)) {  
score = 50;  
}  
}  
if (score &gt; score\_max) {  
score\_max = score;  
fmt = fmt1;  
}else if (score == score\_max)  
fmt = NULL;  
}

评分算法：  
1〉文件头检查OK, 直接打100分  
2〉文件后缀匹配，直接打50分

av\_iformat\_next遍历的链表信息主要在av\_register\_all中调用REGISTER\_MUXER宏来  
注册。

同样  
b asf\_read\_header  
bt

可以得到如下调试信息  
#0 asf\_read\_header (s=0x8c2f020, ap=0xbffff250) at libavformat/asfdec.c:581  
#1 0x0812aff8 in avformat\_open\_input (ps=0xbffff2a4,  
filename=0xbffff548 "/projects/media/wma/123.wma", fmt=0x0,  
options=0xbffff2a0) at libavformat/utils.c:707  
#2 0x0804a48f in dec\_parse (filename=0xbffff548 "/projects/media/wma/123.wma")  
at fileParse.c:29  
#3 0x0804a66d in main (argc=2, argv=0xbffff394) at fileParse.c:69

通过以上分析可以知道，对于ASF文件格式解析来说，  
我们只需要研究如下函数。

.read\_probe = asf\_probe,  
.read\_header = asf\_read\_header,  
.read\_packet = asf\_read\_packet,  
.read\_close = asf\_read\_close,  
.read\_seek = asf\_read\_seek,  
.read\_timestamp = asf\_read\_pts,

STEP3:  
参考上面的函数，在工作平台实现相关函数。（略）