一.背景說明
用戶反映,某課程MP3文件在安卓機(jī)上可以播放,蘋果機(jī)上不能播放。
二.測(cè)試分析
拿到該MP3文件的鏈接,測(cè)試如下:
不能播放:safari瀏覽器,微信瀏覽器,iOS AVPlayer
可以播放:谷歌瀏覽器,ffplay,VLC
下載并查看該文件的格式,發(fā)現(xiàn)實(shí)際上不是MP3格式而是MP4格式,修改文件后綴名為MP4后,使用AVPlayer可正常播放。
那么問題來了,當(dāng)文件后綴名錯(cuò)誤的時(shí)候,為什么有些播放器可以正確播放呢?
初步猜測(cè)是:
這些播放器不僅僅使用音視頻文件的后綴名去判斷格式,而是探測(cè)到了音視頻文件的真正格式,使用了正確的格式去解析。
ffplay是基于ffmpeg的播放器。通過閱讀ffmpeg的源碼可以了解它的音視頻格式探測(cè)邏輯。
三.avformat_open_input函數(shù)
avformat_open_input:此函數(shù)是打開媒體文件,初始化AVFormatContext結(jié)構(gòu)體的關(guān)鍵函數(shù)。filename此處傳入的是mp3文件的本地路徑。
/*
ps:AVFormatContext結(jié)構(gòu)體的二級(jí)指針
filename:文件名,/var/containers/Bundle/Application/A5CD4766-08A9-4A3D-AC75-0FC4B3B7EF13/IJKMediaDemo.app/input.mp3
fmt:AVInputFormat,如果外部傳了,就不會(huì)去探測(cè)實(shí)際格式。此處為NULL。
options:字典,此處可忽略。
*/
int avformat_open_input(AVFormatContext **ps, const char *filename,
AVInputFormat *fmt, AVDictionary **options)
四.init_input函數(shù)
init_input是avformat_open_input內(nèi)部函數(shù),是探測(cè)音視頻格式的關(guān)鍵函數(shù)。
/*
ps:AVFormatContext結(jié)構(gòu)體的二級(jí)指針
filename:文件名,/var/containers/Bundle/Application/A5CD4766-08A9-4A3D-AC75-0FC4B3B7EF13/IJKMediaDemo.app/input.mp3
options:字典,此處可忽略。
*/
static int init_input(AVFormatContext *s, const char *filename,
AVDictionary **options)
當(dāng)執(zhí)行完此函數(shù)后,可以看到
AVFormatContext結(jié)構(gòu)體中iformat(AVInputFormat結(jié)構(gòu)體)已經(jīng)準(zhǔn)確識(shí)別到了文件的實(shí)際格式:

五.FFmpeg的音視頻探測(cè)邏輯
分析init_input的內(nèi)部邏輯,使用輸入文件的結(jié)構(gòu)體AVProbeData依次匹配ffmpeg中鏈表結(jié)構(gòu)的AVInputFormat,從以下三個(gè)維度進(jìn)行匹配打分,并返回得分最高的AVInputFormat(低于25分則探測(cè)失敗)。

1.擴(kuò)展名匹配:不需要讀取文件。使用av_match_ext函數(shù)比較輸入媒體的擴(kuò)展名和AVInputFormat的擴(kuò)展名,匹配成功則為50分。
AVPROBE_SCORE_EXTENSION 50 ///< score for file extension
2.資源的媒體類型匹配:不需要讀取文件。使用av_match_name函數(shù)比較輸入媒體的mime_type和AVInputFormat的mime_type,匹配成功則為75分。
AVPROBE_SCORE_MIME 75 ///< score for file mime type
3.讀取文件頭部數(shù)據(jù)進(jìn)行格式匹配:使用read_probe函數(shù)解析文件頭部數(shù)據(jù),一般解析成功則為100分(最高得分)。
AVPROBE_SCORE_MAX 100 ///< maximum score
此mp3文件在read_probe流程中,在mov.c文件的mov_probe函數(shù)中匹配格式成功,返回100分。并將此AVInputFormat賦值給AVFormatContext結(jié)構(gòu)體的iformat。完成探測(cè)工作。
mp4格式的探測(cè)邏輯如下:可以看到利用了MP4結(jié)構(gòu)的box進(jìn)行解析。
static int mov_probe(AVProbeData *p)
{
int64_t offset;
uint32_t tag;
int score = 0;
int moov_offset = -1;
/* check file header */
offset = 0;
for (;;) {
/* ignore invalid offset */
if ((offset + 8) > (unsigned int)p->buf_size)
break;
tag = AV_RL32(p->buf + offset + 4);
switch(tag) {
/* check for obvious tags */
case MKTAG('m','o','o','v'):
moov_offset = offset + 4;
case MKTAG('m','d','a','t'):
case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
case MKTAG('f','t','y','p'):
if (AV_RB32(p->buf+offset) < 8 &&
(AV_RB32(p->buf+offset) != 1 ||
offset + 12 > (unsigned int)p->buf_size ||
AV_RB64(p->buf+offset + 8) == 0)) {
score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
} else if (tag == MKTAG('f','t','y','p') &&
( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
|| AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
)) {
score = FFMAX(score, 5);
} else {
score = AVPROBE_SCORE_MAX;
}
offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
break;
/* those are more common words, so rate then a bit less */
case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
case MKTAG('w','i','d','e'):
case MKTAG('f','r','e','e'):
case MKTAG('j','u','n','k'):
case MKTAG('p','i','c','t'):
score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
break;
case MKTAG(0x82,0x82,0x7f,0x7d):
case MKTAG('s','k','i','p'):
case MKTAG('u','u','i','d'):
case MKTAG('p','r','f','l'):
/* if we only find those cause probedata is too small at least rate them */
score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
break;
default:
offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
}
}
if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
/* moov atom in the header - we should make sure that this is not a
* MOV-packed MPEG-PS */
offset = moov_offset;
while(offset < (p->buf_size - 16)){ /* Sufficient space */
/* We found an actual hdlr atom */
if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
/* We found a media handler reference atom describing an
* MPEG-PS-in-MOV, return a
* low score to force expanding the probe window until
* mpegps_probe finds what it needs */
return 5;
}else
/* Keep looking */
offset+=2;
}
}
return score;
}
六.思考
一般來說,后臺(tái)會(huì)將不同格式的音視頻轉(zhuǎn)碼成固定格式,比如mp4,aac等。在這種情況下,前端播放器直接指定AVInputFormat效率會(huì)更高:
is->iformat = av_find_input_format("mp4");
err = avformat_open_input(&ic, is->filename, is->iformat, &ffp->format_opts);
av_find_input_format的內(nèi)部實(shí)現(xiàn),用fmt->name來匹配:
AVInputFormat *av_find_input_format(const char *short_name)
{
AVInputFormat *fmt = NULL;
while ((fmt = av_iformat_next(fmt))) //遍歷fmt鏈表
if (av_match_name(short_name, fmt->name))
return fmt;
return NULL;
}