MPlayer源代码分析


一.Mplayer支持的格式

MPlayer是一个LINUX下的视频播放器,它支持相当多的媒体格式,无论在音频播放还是在视频播放方面,可以说它支持的格式是相当全面的。
视频格式支持:MPEG、AVI、ASF 与WMV、QuickTime 与 OGG/OGM、SDP、PVA、GIF。
音频格式支持:MP3、WAV、OGG/OGM 文件(Vorbis)、WMA 与 ASF、MP4、CD音频、XMMS。

二. Mplayer 中头文件的功能分析

config.h // 各种本地配置宏定义头
version.h // 版本定义头 #define VERSION "1.0pre7try2-3.4.2"
mp_msg.h // 消息处理头
help_mp.h // 根据配置自动生成的帮助头 #include "help/help_mpen.h"
cfg-mplayer-def.h // Mplayer 运行时的选项缺省值头文件 char*
default_config =
sub_reader.h // 拥有格式自动发现功能的字幕(subtitle)阅读器
libvo/video_out.h // 该文件包含 libvo 视频输出的公共函数、变量
libvo/font_load.h // 有关字体装载的例程
libao2/audio_out.h // 音频输出驱动程序相关结构定义和全局数据
libmpcodecs/dec_audio.h // 音频解码
libmpcodecs/dec_video.h // 视频解码
libmpdemux/matroska.h // 多路解复用,媒体容器格式 matroska 处理头
libmpdemux/stream.h // 流处理
libmpdemux/demuxer.h // 多路解复用头文件
libmpdemux/stheader.h // 媒体流头处理
get_path.c // 路径获取头文件
spudec.h // SPU 子画面单元头,DVD 字幕流
edl.h // 剪辑控制清单
m_option.h // 选项类型处理头
m_config.h // 配置处理头文件


三. MPlayer.main 主流程简要说明

int main() {
1) 变量声明,电影信息 movie info:
2) 初始化,消息系统……
play_next_file:
3)播放文件 filename 的循环 goto play_next_file 开始
main:
4) 主处理 main
5) 播放真正主循环 2010 ~3541 while (!eof)
while (!eof) {
5.1) 播放音频 PLAY AUDIO 2017 ~ 2064 decode_audio(sh_audio, ...);
5.2) 播放视频 PLAY VIDEO, 2068 ~ 2300 decode_video(sh_video, ...);
5.3) 处理暂停 PAUSE
5.4) 处理 EDL
5.5) 键盘事件处理, 搜索2400~3216 while (!brk_cmd &&
(cmd=mp_input_get_cmd(0,0,0))!=NULL)
5.6) 时间寻道(秒) if (seek_to_sec)
5.7) 寻道 3243 ~ 3306, if (rel_seek_secs || abs_seek_pos)
5.8) 处理 GUI
5.9) 变更 Update OSD
5.10) 找到字幕 find sub
5.11) 处理 X11 窗口
5.12) DVD 字幕 sub:
}
goto_next_file:
6) 播放结束,转到下个文件 goto_next_file:
}


四.Mplayer源码分析

从Mplayer.c的main开始处理参数
mconfig = m_config_new();
m_config_register_options(mconfig,mplayer_opts);
// TODO : add something to let modules register their options
mp_input_register_options(mconfig);
parse_cfgfiles(mconfig);
初始化mpctx结构体,mpctx应该是mplayer context的意思,顾名思义是一个统筹全局的变量。
static MPContext *mpctx = &mpctx_s;
// Not all functions in mplayer.c take the context as an argument yet
static MPContext mpctx_s = {
.osd_function = OSD_PLAY,
.begin_skip = MP_NOPTS_VALUE,
.play_tree_step = 1,
.global_sub_pos = -1,
.set_of_sub_pos = -1,
.file_format = DEMUXER_TYPE_UNKNOWN,
.loop_times = -1,
#ifdef HAS_DVBIN_SUPPORT
.last_dvb_step = 1,
#endif
};

原型

//真正统筹全局的结构
typedef struct MPContext {
    int osd_show_percentage;
    int osd_function;
    const ao_functions_t *audio_out;
    play_tree_t *playtree;
    play_tree_iter_t *playtree_iter;
    int eof;
    int play_tree_step;
    int loop_times;

    stream_t *stream;
    demuxer_t *demuxer;
    sh_audio_t *sh_audio;
    sh_video_t *sh_video;
    demux_stream_t *d_audio;
    demux_stream_t *d_video;
    demux_stream_t *d_sub;
    mixer_t mixer;
    const vo_functions_t *video_out;
    // Frames buffered in the vo ready to flip. Currently always 0 or 1.
    // This is really a vo variable but currently there's no suitable vo
    // struct.
    int num_buffered_frames;

    // used to retry decoding after startup/seeking to compensate for codec delay
    int startup_decode_retry;
    // how long until we need to display the "current" frame
    float time_frame;

    // AV sync: the next frame should be shown when the audio out has this
    // much (in seconds) buffered data left. Increased when more data is
    // written to the ao, decreased when moving to the next frame.
    // In the audio-only case used as a timer since the last seek
    // by the audio CPU usage meter.
    double delay;

    float begin_skip; ///< start time of the current skip while on edlout mode
    // audio is muted if either EDL or user activates mute
    short edl_muted; ///< Stores whether EDL is currently in muted mode.
    short user_muted; ///< Stores whether user wanted muted mode.

    int global_sub_size; // this encompasses all subtitle sources
    int global_sub_pos; // this encompasses all subtitle sources
    int set_of_sub_pos;
    int set_of_sub_size;
    int sub_counts[SUB_SOURCES];
#ifdef CONFIG_ASS
    // set_of_ass_tracks[i] contains subtitles from set_of_subtitles[i]
    // parsed by libass or NULL if format unsupported
    ASS_Track* set_of_ass_tracks[MAX_SUBTITLE_FILES];
#endif
    sub_data* set_of_subtitles[MAX_SUBTITLE_FILES];

    int file_format;

#ifdef CONFIG_DVBIN
    int last_dvb_step;
    int dvbin_reopen;
#endif

    int was_paused;

#ifdef CONFIG_DVDNAV
    struct mp_image *nav_smpi;   ///< last decoded dvdnav video image
    unsigned char *nav_buffer;   ///< last read dvdnav video frame
    unsigned char *nav_start;    ///< pointer to last read video buffer
    int            nav_in_size;  ///< last read size
#endif
} MPContext;

一些GUI相关的操作
打开字幕流
打开音视频流
mpctx->stream=open_stream(filename,0,&mpctx->file_format);
fileformat 文件还是TV 流DEMUXER_TYPE_PLAYLIST 或DEMUXER_TYPE_UNKNOWN
DEMUXER_TYPE_TV
current_module记录状态vobsub open_stream handle_playlist dumpstream
stream_reset(mpctx->stream);
stream_seek(mpctx->stream,mpctx->stream->start_pos);
f=fopen(stream_dump_name,”wb”); dump文件流
stream->type==STREAMTYPE_DVD
//============ Open DEMUXERS — DETECT file type ======================
Demux。分离视频流和音频流
mpctx->demuxer=demux_open(mpctx->stream,mpctx-
>file_format,audio_id,video_id,dvdsub_id,filename);
Demux过程
demux_open
get_demuxer_type_from_name
……
mpctx->d_audio=mpctx->demuxer->audio;
mpctx->d_video=mpctx->demuxer->video;
mpctx->d_sub=mpctx->demuxer->sub;
mpctx->sh_audio=mpctx->d_audio->sh;
mpctx->sh_video=mpctx->d_video->sh;
分离了之后就开始分别Play audio和video
这里只关心play video
/*======================== PLAY VIDEO ============================*/
vo_pts=mpctx->sh_video->timer*90000.0;
vo_fps=mpctx->sh_video->fps;
if (!mpctx->num_buffered_frames) {
double frame_time = update_video(&blit_frame);
mp_dbg(MSGT_AVSYNC,MSGL_DBG2,”*** ftime=%5.3f ***\n”,frame_time);
if (mpctx->sh_video->vf_inited < 0) {
mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
mpctx->eof = 1; goto goto_next_file;
}
if (frame_time < 0)
mpctx->eof = 1;
else {
// might return with !eof && !blit_frame if !correct_pts
mpctx->num_buffered_frames += blit_frame;
time_frame += frame_time / playback_speed; // for nosound
}
}


关键的函数是update_video根据pts是否正确调整一下同步并在必要的时候丢帧处理。最终调用decode_video开始解码(包括generate_video_frame里)。mpi = mpvdec->decode(sh_video, start, in_size, drop_frame);mpvdec是在main里通过reinit_video_chain的一系列调用动态选定的解码程序。其实就一结构体。它的原型是
typedef struct vd_functions_s
{
vd_info_t *info;
int (*init)(sh_video_t *sh);
void (*uninit)(sh_video_t *sh);
int (*control)(sh_video_t *sh,int cmd,void* arg, …);
mp_image_t* (*decode)(sh_video_t *sh,void* data,int len,int flags);
} vd_functions_t;
这是所有解码器必须实现的接口。
int (*init)(sh_video_t *sh);是一个名为init的指针,指向一个接受sh_video_t *类型参数,并返回int类型值的函数地址。那些vd_开头的文件都是解码相关的。随便打开一个vd文件以上几个函数和info变量肯定都包含了。mpi被mplayer用来存储解码后的图像。在mp_image.h里定义。

typedef struct mp_image_s {
unsigned short flags;
unsigned char type;
unsigned char bpp; // bits/pixel. NOT depth! for RGB it will be n*8
unsigned int imgfmt;
int width,height; // stored dimensions
int x,y,w,h; // visible dimensions
unsigned char* planes[MP_MAX_PLANES];
int stride[MP_MAX_PLANES];
char * qscale;
int qstride;
int pict_type; // 0->unknown, 1->I, 2->P, 3->B
int fields;
int qscale_type; // 0->mpeg1/4/h263, 1->mpeg2
int num_planes;
/* these are only used by planar formats Y,U(Cb),V(Cr) */
int chroma_width;
int chroma_height;
int chroma_x_shift; // horizontal
int chroma_y_shift; // vertical
/* for private use by filter or vo driver (to store buffer id or dmpi) */
void* priv;
} mp_image_t;


图像在解码以后会输出到显示器,mplayer本来就是一个视频播放器么。但也有可能作为输入提供给编码器进行二次编码,MP附带的mencoder.exe就是专门用来编码的。在这之前可以定义filter对图像进行处理,以实现各种效果。所有以vf_开头的文件,都是这样的filter。图像的显示是通过vo,即video out来实现的。解码器只负责把解码完成的帧传给vo,怎样显示就不用管了。这也是平台相关性最大的部分,单独分出来的好处是不言而喻的,像在Windows下有通过direcx实现的vo,Linux下有输出到X的vo。vo_*文件是各种不同的vo实现,只是他们不都是以显示为目的,像vo_md5sum.c只是计算一下图像的md5值。在解码完成以后,即得到mpi以后,filter_video被调用,其结果是整个filter链上的所有filter都被调用了一遍,包括最后的VO,在vo的put_image里把图像输出到显示器。这个时候需要考虑的是图像存储的方法即用哪种色彩空间。

附上两张MPlayer结构图:



MPLayer源代码下载地址:http://download.csdn.net/detail/leixiaohua1020/6374337

  • 19
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
目录 第一章 多媒体概念介绍 6 1.1视频格式 6 1.1.1常见格式 6 1.2音频格式 9 1.2.1常见格式 9 1.2.2比较 15 1.3字幕格式 15 1.3.1外挂字幕与内嵌字幕的阐述 15 1.3.2外挂字幕视频与内嵌字幕视频的画面比较 15 1.3.3外挂字幕的三种格式 15 1.4采集录制和播放渲染 16 1.4.1视频采集 16 1. 4.2视频录制 17 1.4.3视频渲染 17 1.5编解码器 18 1.6容器和协议 19 1.6.1容器格式和编码格式 19 1.6.2协议 26 1.6.2.1 视频协议 26 1.6.2.2 音频协议. 26 1.6.2.3 上层通讯协议 27 1.7常用概念介绍 27 1.7.1硬解 27 1.7.2 IBP帧 28 1.7.3 DTS和PTS 31 1.7.4 分辨率 31 1.7.5 码率 32 1.7.6 帧率 32 1.7.7 RGB和YUV 32 1.7.8 实时和非实时 32 1.7.9 复合视频和s-video 32 1.7.10 硬件加速 32 1.7.11 FFmpeg Device 32 第二章 FFmpeg框架 34 2.1 FFmpeg概述 34 2.1.1简介 34 2.1.2功能 34 2.1.3模块组成 35 2.1.4命令集 35 2.2 媒体播放器三大底层框架 37 第三章 编译及简单应用 41 3.1 FFmpeg库编译和入门介绍 41 41 3.2 流媒体数据流程讲解 49 3.3 简单应用 51 3.4 SDL( Simple Direct Layer) 55 3.4.1 SDL显示视频 55 3.4.2 SDL显示音频 55 3.5 ffmpeg程序的使用(ffmpeg.exe,ffplay.exe,ffprobe.exe) 56 3.5.1 ffmpeg.exe 56 3.5.2 ffplay.exe 56 3.5.3 ffprobe.exe 56 第四章 数据结构 57 4.1 AVCodec结构体 59 4.2 AVCodecContext结构体 59 4.3 AVInputFormat结构体 60 4.4 AVFormatContext结构体 61 4.5 MovContext结构体 62 4.6 URLProtocol结构体 62 4.7 URLContext结构体 63 4.8 AVIOContext结构体(老版本为:ByteIOContext) 63 4.9 AVStream结构体 64 4.10 MOVStreamContext 结构体 65 4.11 AVPacket 结构体 66 4.12 AVPacketList 结构体 67 4.13 AVFrame结构体 67 第五章 重要模块 76 5.1 libavutil公共模块 76 1 文件列表 76 2 common.h 文件 76 3 bswap.h 文件 78 4 rational.h 文件 79 5 mathematics.h 文件 80 6 avutil.h 文件 80 5.2 libavcodec编解码模块 82 1 文件列表 82 2 avcodec.h 文件 82 3 allcodec.c 文件 87 4 dsputil.h 文件 87 5 dsputil.c 文件 88 6 utils_codec.c 文件 88 7 imgconvert_template.h 文件 99 8 imgconvert.c 文件 121 9 msrle.c 文件 164 10 turespeech_data.h 文件 171 11 turespeech.c 文件 174 5.3 libavformat容器模块 184 1 文件列表 184 2 avformat.h 文件 184 3 allformat.c 文件 190 4 cutils.c 文件 190 5 file.c 文件 192 6 avio.h 文件 194 7 avio.c 文件 196 8 aviobuf.c 文件 200 9 utils_format.c 文件 209 10 avidec.c 文件 220 5.4 libswscale视频色彩空间转换 243 5.5 libswresample音频重采样 243 5.6 libavfilter音视频滤器 243 5.7 libavdevice设备输入和输出容器 243 5.8 libpostproc视频后期处理 243 第六章 播放器 243 6.1 视频播放器 243 6.1.1 ffmpeg库的配置 243 6.1.2 一个简单的视频播放器 244 6.2 音频播放器 247 6.3 一个完整的播放器--ffplay 253 6.3.1 ffplay流程图 253 6.3.2 ffplay源码剖析 254 第七章 应用开发 275 7.1 ffmpeg库的使用:编码 275 第八章 关键函数介绍 280 8.1 avformat_open_input 280 8.2 avcodec_register_all() 281 8.3 av_read_frame() 283 8.4 avcodec_decode_video2() 283 8.5 transcode_init() 283 8.6 transcode() 294 第九章 ffmpeg相关工程 301 9.1 ffdshow 301 ffdshow 源代码分析1 : 整体结构 302 ffdshow 源代码分析 2: 位图覆盖滤镜(对话框部分Dialog) 304 ffdshow 源代码分析 3: 位图覆盖滤镜(设置部分Settings) 312 ffdshow 源代码分析 4: 位图覆盖滤镜(滤镜部分Filter) 317 ffdshow 源代码分析 5: 位图覆盖滤镜(总结) 322 ffdshow 源代码分析 6: 对解码器的dll的封装(libavcodec) 322 ffdshow 源代码分析 8: 视频解码器类(TvideoCodecDec) 344 ffdshow 源代码分析 9: 编解码器有关类的总结 352 9.2 LAV filters 357 LAV Filter 源代码分析 1: 总体结构 357 LAV Filter 源代码分析 2: LAV Splitter 358 LAV Filter 源代码分析 3: LAV Video (1) 382 LAV Filter 源代码分析 4: LAV Video (2) 400 9.3 MPlayer 427 9.3.1 Mplayer支持的格式 427 9.3.2 Mplayer 中头文件的功能分析 427 9.3.3 MPlayer.main 主流程简要说明 428 9.3.4 Mplayer源码分析 429 第十章 开发实例 436 第十一章 mp4文件封装协议分析 436 11.1 概述 436 11.2 mp4的物理结构 436 11.3 数据的组织结构 437 11.4 mp4的时间结构 437 11.5 文件结构分析 438 11.5.1 File Type Box(ftyp) 438 11.5.2 Movie Box(moov) 438 第十二章 flv 文件格式分析 457 12.1 概述 457 12.2 文件总体结构 457 12.3 文件结构分析 458 12.3.1 flv文件头的结构 458 12.3.2 body主体结构 459 附录A:常见问题 465 1 ffmpeg 从内存中读取数据 465 2 MFC中使用SDL播放音频没有声音的解决方法 465 附录B:经典代码示例 466 附录C:ffmpeg参数中文详细解释 477 附录D:ffplay的快捷键以及选项 479 附录E: ffmpeg处理rtmp流媒体 481

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值