FFMPEG详解(完整版)

Posted 音视频开发老马

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FFMPEG详解(完整版)相关的知识,希望对你有一定的参考价值。

一、认识FFMPEG

FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几乎实现了所有当下常见的数据封装格式、多媒体传输协议以及音视频编解码器,堪称多媒体业界的瑞士军刀。因此,对于从事多媒体技术开发的工程师来说,深入研究FFMPEG成为一门必不可少的工作,可以这样说,FFMPEG之于多媒体开发工程师的重要性正如kernel之于嵌入式系统工程师一般。

几个小知识:

  • FFMPEG项目是由法国人Fabrice Bellard发起的,此人也是著名的CPU模拟器项目QEMU的发起者,同时还是圆周率算法纪录的保持者。

  • FF是Fast Forward的意思,翻译成中文是“快进”。

  • FFMPEG的LOGO是一个”Z字扫描”示意图,Z字扫描用于将图像的二维频域数据一维化,同时保证了一维化的数据具备良好的统计特性,从而提高其后要进行的一维熵编码的效率。

关于耻辱厅(Hall of Shame):FFMPEG大部分代码遵循LGPL许可证,如果使用者对FFMpeg进行了修改,要求公布修改的源代码;有少部分代码遵循GPL许可证,要求使用者同时公开使用FFMpeg的软件的源代码。实际上,除去部分具备系统软件开发能力的大型公司(Microsoft、Apple等)以及某些著名的音视频技术提供商(Divx、Real等)提供的自有播放器之外,绝大部分第三方开发的播放器都离不开FFMpeg的支持,像Linux桌面环境中的开源播放器VLC、MPlayer,Windows下的KMPlayer、暴风影音以及android下几乎全部第三方播放器都是基于FFMPEG的。也有许多看似具备自主技术的播放器,其实也都不声不响地使用了FFMPEG,这种行为被称为“盗窃”,参与“盗窃”的公司则被请入耻辱厅,如于2009年上榜的国产播放器暴风影音、QQ影音。

关于FFMPEG的商业应用:与其他开源软件不同的是,FFMPEG所触及的多媒体编解码算法中有相当一部分处于大量的专利涵盖范围之内,因此,在商业软件中使用FFMPEG必须考虑可能造成的对专利所有者的权利侵犯,这一点在FFMPEG的官方网站也有所提及,所涉及的风险需使用者自行评估应对。

FFMPEG从功能上划分为几个模块,分别为核心工具(libutils)、媒体格式(libavformat)、编解码(libavcodec)、设备(libavdevice)和后处理(libavfilter, libswscale, libpostproc),分别负责提供公用的功能函数、实现多媒体文件的读包和写包、完成音视频的编解码、管理音视频设备的操作以及进行音视频后处理。

使用FFMPEG

这里指FFMPEG提供的命令行(CLI)工具ffmpeg,其使用方法如下(方括号表示可选项,花括号表示必选项目):

ffmpeg [global options] [infile options]['-i' 'infile'] ... [outfile options] 'outfile' ...

参数选项由三部分组成:可选的一组全局参数、一组或多组输入文件参数、一组或多组输出文件参数,其中,每组输入文件参数以‘-i’为结束标记;每组输出文件参数以输出文件名为结束标记。

基本选项

能力集列表

  • -formats:列出支持的文件格式。

  • -codecs:列出支持的编解码器。

  • -decoders:列出支持的解码器。

  • -encoders:列出支持的编码器。

  • -protocols:列出支持的协议。

  • -bsfs:列出支持的比特流过滤器。

  • -filters:列出支持的滤镜。

  • -pix_fmts:列出支持的图像采样格式。

  • -sample_fmts:列出支持的声音采样格式。

常用输入选项

  • -i filename:指定输入文件名。

  • -f fmt:强制设定文件格式,需使用能力集列表中的名称(缺省是根据扩展名选择的)。

  • -ss hh:mm:ss[.xxx]:设定输入文件的起始时间点,启动后将跳转到此时间点然后开始读取数据。

对于输入,以下选项通常是自动识别的,但也可以强制设定。

  • -c codec:指定解码器,需使用能力集列表中的名称。

  • -acodec codec:指定声音的解码器,需使用能力集列表中的名称。

  • -vcodec codec:指定视频的解码器,需使用能力集列表中的名称。

  • -b:v bitrate:设定视频流的比特率,整数,单位bps。

  • -r fps:设定视频流的帧率,整数,单位fps。

  • -s WxH : 设定视频的画面大小。也可以通过挂载画面缩放滤镜实现。

  • -pix_fmt format:设定视频流的图像格式(如RGB还是YUV)。

  • -ar sample rate:设定音频流的采样率,整数,单位Hz。

  • -ab bitrate:设定音频流的比特率,整数,单位bps。

  • -ac channels:设置音频流的声道数目。

常用输出选项

  • -f fmt:强制设定文件格式,需使用能力集列表中的名称(缺省是根据扩展名选择的)。

  • -c codec:指定编码器,需使用能力集列表中的名称(编码器设定为”copy“表示不进行编解码)。

  • -acodec codec:指定声音的编码器,需使用能力集列表中的名称(编码器设定为”copy“表示不进行编解码)。

  • -vcodec codec:指定视频的编码器,需使用能力集列表中的名称(编解码器设定为”copy“表示不进行编解码)。

  • -r fps:设定视频编码器的帧率,整数,单位fps。

  • -pix_fmt format:设置视频编码器使用的图像格式(如RGB还是YUV)。

  • -ar sample rate:设定音频编码器的采样率,整数,单位Hz。

  • -b bitrate:设定音视频编码器输出的比特率,整数,单位bps。

  • -ab bitrate:设定音频编码器输出的比特率,整数,单位bps。

  • -ac channels:设置音频编码器的声道数目。

  • -an 忽略任何音频流。

  • -vn 忽略任何视频流。

  • -t hh:mm:ss[.xxx]:设定输出文件的时间长度。

  • -to hh:mm:ss[.xxx]:如果没有设定输出文件的时间长度的画可以设定终止时间点。

  • ★文末名片可以免费领取音视频开发学习资料,内容包括(C/C++,Linux 服务器开发,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,srs)以及音视频学习路线图等等。

    见下方!↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

流标识

FFMPEG的某些选项可以对一个特定的媒体流起作用,这种情况下需要在选项后面增加一个流标识。流标识允许以下几种格式:

  • 流序号。譬如“:1”表示第二个流。

  • 流类型。譬如“:a“表示音频流,流类型可以和流序号合并使用,譬如“🅰️1”表示第二个音频流。

  • 节目。节目和流序号可以合并使用。

  • 流标识。流标识是一个内部标识号。

假如要设定第二个音频流为copy,则需要指定-codec🅰️1 copy

音频选项

  • -aframes:等价于frames:a,输出选项,用于指定输出的音频帧数目。

  • -aq:等价于q:a,老版本为qscale:a,用于设定音频质量。

  • -atag:等价于tag:a,用于设定音频流的标签。

  • -af:等价于filter:a,用于设定一个声音的后处理过滤链,其参数为一个描述声音后处理链的字符串。

视频选项

  • -vframes:等价于frames:v,输出选项,用于指定输出的视频帧数目。

  • -aspect:设置宽高比,如4:3、16:9、1.3333、1.7777等。

  • -bits_per_raw_sample:设置每个像素点的比特数。

  • -vstats:产生video统计信息。

  • -vf:等价于filter:v,用于设定一个图像的后处理过滤链,其参数为一个描述图像后处理链的字符串。

  • -vtag:等价于tag:v,用于设定视频流的标签。

  • -force_fps:强制设定视频帧率。

  • -force_key_frames:显式控制关键帧的插入,参数为字符串,可以是一个时间戳,也可以是一个“expr:”前缀的表达式。如“-force_key_frames 0:05:00”、“-force_key_frames expr:gte(t,n_forced*5)”

滤镜选项

高级选项

  • -re:要求按照既定速率处理输入数据,这个速率即是输入文件的帧率。

  • -map:指定输出文件的流映射关系。例如 “-map 1:0 -map 1:1”要求将第二个输入文件的第一个流和第二个流写入到输出文件。如果没有-map选项,ffmpeg采用缺省的映射关系。

用例

1、将一个老式的avi文件转成mp4

ffmpeg -i final.avi -acodec copy -vcodec copy final.mp4 

2、从一个视频文件中抽取一帧图像:

ffmpeg -y -i test.mp4 -ss 00:03:22.000 -vframes 1 -an test.jpg

3

ffmpeg -i final.avi -vf scale=640:640 square.avi  

4、使用alsa接口录制一段音频存放到某个wav文件中

ffmpeg -f alsa -i hw:0 -t 100 alsaout.wav

5、使用alsa接口搭建一个个人网络电台

ffmpeg -f alsa -i default -acodec aac -strict -2 -b:a 128k -r 44100 /var/www/data/main.m3u8

6、将一个mp4文件的音视频流实时转码之后发送给某个远程设备,远程设备可以通过http获取的sdp文件来接收rtp媒体数据。

ffmpeg -re -i example.mp4 -acodec copy -vcodec libx264 -s 480x270 -map 0:0 -f rtp rtp://10.131.202.62:1234 -map 0:1 -f rtp rtp://10.131.202.62:1238 > /var/www/live.sdp

编译和裁剪

FFMpeg与大部分GNU软件的编译方式类似,是通过configure脚本来实现编译前定制的。这种途径允许用户在编译前对软件进行裁剪,同时通过对宿主系统和目标系统的自动检测来筛选参与编译的模块并为其设定合适的配置。但是,FFMpeg的configure脚本并非如通常的GNU软件一样通过配置工具自动生成,而是完全由人工编写的。configure脚本生成的config.mak和config.h分别在Makefile和源代码的层次上实现编译的控制。

通过运行“./configure –help”可以了解到脚本支持的参数,这些参数大体分为下面几类:

  • 标准选项——GNU软件例行配置项目如安装路径等。例:–prefix=…,……

  • 列出当前源代码支持的能力集,如编解码器,解复用器,输入输出设备,文件系统等。例:–list-decoders,–list-encoders,……

  • 授权选项:–enable-version3,–enable-gpl,–enable-nofree。代码的缺省授权是LGPL v2,如果要使用以LGPL v3、GPL授权的模块或者某些不遵循自有软件授权协议的模块,必须在运行configure时显式使能相应的选项。

  • 编译、链接选项。例:–disable-static,–enable-shared,…… 缺省配置是生成静态库而不生成动态库,如果希望禁止静态库、生成动态库都需要显式指定。

  • 可执行程序控制选项,决定是否生成ffmpeg、ffplay、ffprobe和ffserver。

  • 模块控制选项,筛选参与编译的模块,包括整个库的筛选,例如:–disable-avdevice;一组模块的筛选,例如:–disable-decoders,单个模块的筛选,如:–disable-decoder=… 等。

  • 专家级选项,允许开发者进行深度定制,如交叉编译环境的配置、自定义编译器参数的设定、指令级优化、debug控制等。

对于–disable、–enable类的控制选项,如果以–disable为前缀,则缺省是enable的,反之亦然。

总之,无论从商业角度还是技术角度出发,使用configure脚本对FFMpeg进行裁剪是最安全的方式,只有针对于某些configure无法满足的定制要求,才需要考虑修改configure脚本——甚至修改configure生成的配置文件。

以下是一个配置实例,实现运行与Android系统中的ffmpeg库的编译:

./configure --prefix=. --cross-prefix=$NDK_TOOLCHAIN_PREFIX --enable-cross-compile --arch=arm --target-os=linux --cpu=cortex-a8 \\
                       --disable-static --enable-shared --enable-pic --disable-ffmpeg --disable-ffplay --disable-ffserver --disable-ffprobe \\
                       --extra-cflags="-I$NDK_PLATFORM/usr/include" \\
                       --extra-ldflags="-nostdlib -Wl,-T,$NDK_PREBUILT/arm-linux-androideabi/lib/ldscripts/armelf_linux_eabi.x \\
                                        -L$NDK_PLATFORM/usr/lib \\
                                        $NDK_PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/crtbegin.o $NDK_PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/crtend.o \\
                                        -lc -lm -ldl"

缺省的编译会生成4个可执行文件和9个库:可执行文件包括用于转码的ffmpeg、用于获取媒体信息的ffprobe、用于播放媒体的ffplay和用于推送媒体流的ffserver;库包括avutil、avformat、avcodec、avfilter、avdevice、swresample、swscale、postproc及avresample,其中,核心库有5个,分别为基础库avutil、文件格式及协议库avformat、编解码库avcodec、输入输出设备库avdevice和过滤器avfilter。

深入FFMPEG

示例程序

解码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#include <sys/time.h>
 
#include "libavutil/avstring.h"
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libavutil/opt.h"
#include "libswscale/swscale.h"
 
#define DECODED_AUDIO_BUFFER_SIZE            192000
 
struct options

    int streamId;
    int frames;
    int nodec;
    int bplay;
    int thread_count;
    int64_t lstart;
    char finput[256];
    char foutput1[256];
    char foutput2[256];
;
 
int parse_options(struct options *opts, int argc, char** argv)

    int optidx;
    char *optstr;
 
    if (argc < 2) return -1;
 
    opts->streamId = -1;
    opts->lstart = -1;
    opts->frames = -1;
    opts->foutput1[0] = 0;
    opts->foutput2[0] = 0;
    opts->nodec = 0;
    opts->bplay = 0;
    opts->thread_count = 0;
    strcpy(opts->finput, argv[1]);
 
    optidx = 2;
    while (optidx < argc)
    
        optstr = argv[optidx++];
        if (*optstr++ != '-') return -1;
        switch (*optstr++)
        
        case 's':  //< stream id
            opts->streamId = atoi(optstr);
            break;
        case 'f':  //< frames
            opts->frames = atoi(optstr);
            break;
        case 'k':  //< skipped
            opts->lstart = atoll(optstr);
            break;
        case 'o':  //< output
            strcpy(opts->foutput1, optstr);
            strcat(opts->foutput1, ".mpg");
            strcpy(opts->foutput2, optstr);
            strcat(opts->foutput2, ".raw");
            break;
        case 'n': //decoding and output options
            if (strcmp("dec", optstr) == 0)
                opts->nodec = 1;
            break;
        case 'p':
            opts->bplay = 1;
            break;
        case 't':
            opts->thread_count = atoi(optstr);
            break;
        default:
            return -1;
        
    
 
    return 0;

 
void show_help(char* program)

    printf("Simple FFMPEG test program\\n");
    printf("Usage: %s inputfile [-sstreamid [-fframes] [-kskipped] [-ooutput_filename(without extension)] [-ndec] [-p] [-tthread_count]]\\n",
           program);
    return;

 
static void log_callback(void* ptr, int level, const char* fmt, va_list vl)

    vfprintf(stdout, fmt, vl);

 
/*
* audio renderer code (oss)
*/
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/soundcard.h>
 
#define OSS_DEVICE "/dev/dsp0"
 
struct audio_dsp

    int audio_fd;
    int channels;
    int format;
    int speed;
;
int map_formats(enum AVSampleFormat format)

    switch(format)
    
        case AV_SAMPLE_FMT_U8:
            return AFMT_U8;
        case AV_SAMPLE_FMT_S16:
            return AFMT_S16_LE;
        default:
            return AFMT_U8; 
    

int set_audio(struct audio_dsp* dsp)

    if (dsp->audio_fd == -1)
    
        printf("Invalid audio dsp id!\\n");
        return -1;
        
 
    if (-1 == ioctl(dsp->audio_fd, SNDCTL_DSP_SETFMT, &dsp->format))
    
        printf("Failed to set dsp format!\\n");
        return -1;
    
 
    if (-1 == ioctl(dsp->audio_fd, SNDCTL_DSP_CHANNELS, &dsp->channels))
    
        printf("Failed to set dsp format!\\n");
        return -1;
    
 
    if (-1 == ioctl(dsp->audio_fd, SNDCTL_DSP_SPEED, &dsp->speed))
    
        printf("Failed to set dsp format!\\n");
        return -1;
        
    return 0;

 
int play_pcm(struct audio_dsp* dsp, unsigned char *buf, int size)

    if (dsp->audio_fd == -1)
    
        printf("Invalid audio dsp id!\\n");
        return -1;
    
 
    if (-1 == write(dsp->audio_fd, buf, size))
    
        printf("Failed to write audio dsp!\\n");
        return -1;
    
 
    return 0;

/* audio renderer code end */
 
/* video renderer code*/
#include <linux/fb.h>
#include <sys/mman.h>
 
#define FB_DEVICE "/dev/fb0"
 
enum pic_format

    eYUV_420_Planer,
;
struct video_fb

    int video_fd;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    unsigned char *fbp;
    AVFrame *frameRGB;
    struct 
    
        int x;
        int y;
     video_pos;
;
 
int open_video(struct video_fb *fb, int x, int y)

    int screensize;
    fb->video_fd = open(FB_DEVICE, O_WRONLY);
    if (fb->video_fd == -1) return -1;
 
    if (ioctl(fb->video_fd, FBIOGET_FSCREENINFO, &fb->finfo)) return -2;
    if (ioctl(fb->video_fd, FBIOGET_VSCREENINFO, &fb->vinfo)) return -2;
 
 
    printf("video device: resolution %dx%d, %dbpp\\n", fb->vinfo.xres, fb->vinfo.yres, fb->vinfo.bits_per_pixel);
    screensize = fb->vinfo.xres * fb->vinfo.yres * fb->vinfo.bits_per_pixel / 8;
    fb->fbp = (unsigned char *) mmap(0, screensize, PROT_READ|PROT_WRITE, MAP_SHARED, fb->video_fd, 0);
    if (fb->fbp == -1) return -3;
 
    if (x >= fb->vinfo.xres || y >= fb->vinfo.yres)
    
        return -4;
    
    else
    
        fb->video_pos.x = x;
        fb->video_pos.y = y;
    
 
    fb->frameRGB = avcodec_alloc_frame();
    if (!fb->frameRGB) return -5;
 
    return 0;

 
/* only 420P supported now */
int show_picture(struct video_fb *fb, AVFrame *frame, int width, int height, enum pic_format format)

    struct SwsContext *sws;
    int i;
    unsigned char *dest;
    unsigned char *src;
 
    if (fb->video_fd == -1) return -1;
    if ((fb->video_pos.x >= fb->vinfo.xres) || (fb->video_pos.y >= fb->vinfo.yres)) return -2;
 
    if (fb->video_pos.x + width > fb->vinfo.xres)
    
        width = fb->vinfo.xres - fb->video_pos.x;
    
    if (fb->video_pos.y + height > fb->vinfo.yres)
    
        height = fb->vinfo.yres - fb->video_pos.y;
    
 
    if (format == PIX_FMT_YUV420P)
    
        sws = sws_getContext(width, height, format, width, height, PIX_FMT_RGB32, SWS_FAST_BILINEAR, NULL, NULL, NULL);
        if (sws == 0)
        
            return -3;
        
        if (sws_scale(sws, frame->data, frame->linesize, 0, height, fb->frameRGB->data, fb->frameRGB->linesize))
        
            return -3;
        
 
        dest = fb->fbp + (fb->video_pos.x+fb->vinfo.xoffset) * (fb->vinfo.bits_per_pixel/8) +(fb->video_pos.y+fb->vinfo.yoffset) * fb->finfo.line_length;
        for (i = 0; i < height; i++)
        
            memcpy(dest, src, width*4);
            src += fb->frameRGB->linesize[0];
            dest += fb->finfo.line_length;
        
    
    return 0;

 
void close_video(struct video_fb *fb)

    if (fb->video_fd != -1) 
    
        munmap(fb->fbp, fb->vinfo.xres * fb->vinfo.yres * fb->vinfo.bits_per_pixel / 8);
        close(fb->video_fd);
        fb->video_fd = -1;
    

/* video renderer code end */
 
int main(int argc, char **argv)

    AVFormatContext* pCtx = 0;
    AVCodecContext *pCodecCtx = 0;
    AVCodec *pCodec = 0;
    AVPacket packet;
    AVFrame *pFrame = 0;
    FILE *fpo1 = NULL;
    FILE *fpo2 = NULL;
    int nframe;
    int err;
    int got_picture;
    int picwidth, picheight, linesize;
    unsigned char *pBuf;
    int i;
    int64_t timestamp;
    struct options opt;
    int usefo = 0;
    struct audio_dsp dsp;
    struct video_fb fb;
    int dusecs;
    float usecs1 = 0;
    float usecs2 = 0;
    struct timeval elapsed1, elapsed2;
    int decoded = 0;
 
    av_register_all();
 
    av_log_set_callback(log_callback);
    av_log_set_level(50);
 
    if (parse_options(&opt, argc, argv) < 0 || (strlen(opt.finput) == 0))
    
        show_help(argv[0]);
        return 0;
    
 
 
    err = avformat_open_input(&pCtx, opt.finput, 0, 0);
    if (err < 0)
    
        printf("\\n->(avformat_open_input)\\tERROR:\\t%d\\n", err);
        goto fail;
    
    err = avformat_find_stream_info(pCtx, 0);
    if (err < 0)
    
        printf("\\n->(avformat_find_stream_info)\\tERROR:\\t%d\\n", err);
        goto fail;
    
    if (opt.streamId < 0)
    
        av_dump_format(pCtx, 0, pCtx->filename, 0);
        goto fail;
    
    else
    
        printf("\\n extra data in Stream %d (%dB):", opt.streamId, pCtx->streams[opt.streamId]->codec->extradata_size);
        for (i = 0; i < pCtx->streams[opt.streamId]->codec->extradata_size; i++)
        
            if (i%16 == 0) printf("\\n");
            printf("%2x  ", pCtx->streams[opt.streamId]->codec->extradata[i]);
        
    
    /* try opening output files */
    if (strlen(opt.foutput1) && strlen(opt.foutput2))
    
        fpo1 = fopen(opt.foutput1, "wb");
        fpo2 = fopen(opt.foutput2, "wb");
        if (!fpo1 || !fpo2)
        
            printf("\\n->error opening output files\\n");
            goto fail;
        
        usefo = 1;
    
    else
    
        usefo = 0;
    
 
    if (opt.streamId >= pCtx->nb_streams)
    
        printf("\\n->StreamId\\tERROR\\n");
        goto fail;
    
 
    if (opt.lstart > 0)
    
        err = av_seek_frame(pCtx, opt.streamId, opt.lstart, AVSEEK_FLAG_ANY);
        if (err < 0)
        
            printf("\\n->(av_seek_frame)\\tERROR:\\t%d\\n", err);
            goto fail;
        
    
 
    /* for decoder configuration */
    if (!opt.nodec)
    
        /* prepare codec */
        pCodecCtx = pCtx->streams[opt.streamId]->codec;
 
        if (opt.thread_count <= 16 && opt.thread_count > 0 )
        
            pCodecCtx->thread_count = opt.thread_count;
            pCodecCtx->thread_type = FF_THREAD_FRAME;
        
        pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
        if (!pCodec)
        
            printf("\\n->can not find codec!\\n");
            goto fail;
        
        err = avcodec_open2(pCodecCtx, pCodec, 0);
        if (err < 0)
        
            printf("\\n->(avcodec_open)\\tERROR:\\t%d\\n", err);
            goto fail;
        
        pFrame = avcodec_alloc_frame();        
 
        /* prepare device */
        if (opt.bplay)
        
            /* audio devices */
            dsp.audio_fd = open(OSS_DEVICE, O_WRONLY);
            if (dsp.audio_fd == -1)
            
                printf("\\n-> can not open audio device\\n");
                goto fail;
            
            dsp.channels = pCodecCtx->channels;
            dsp.speed = pCodecCtx->sample_rate;
            dsp.format = map_formats(pCodecCtx->sample_fmt);
            if (set_audio(&dsp) < 0)
            
                printf("\\n-> can not set audio device\\n");
                goto fail;
            
            /* video devices */
            if (open_video(&fb, 0, 0) != 0)
            
                printf("\\n-> can not open video device\\n");
                goto fail;
            
        
    
 
    nframe = 0;
    while(nframe < opt.frames || opt.frames == -1)
    
        gettimeofday(&elapsed1, NULL);
        err = av_read_frame(pCtx, &packet);
        if (err < 0)
        
            printf("\\n->(av_read_frame)\\tERROR:\\t%d\\n", err);
            break;
        
        gettimeofday(&elapsed2, NULL);
        dusecs = (elapsed2.tv_sec - elapsed1.tv_sec)*1000000 + (elapsed2.tv_usec - elapsed1.tv_usec);
        usecs2 += dusecs;        
        timestamp = av_rescale_q(packet.dts, pCtx->streams[packet.stream_index]->time_base, (AVRational)1, AV_TIME_BASE);
        printf("\\nFrame No %5d stream#%d\\tsize %6dB, timestamp:%6lld, dts:%6lld, pts:%6lld, ", nframe++, packet.stream_index, packet.size, 
               timestamp, packet.dts, packet.pts);
 
        if (packet.stream_index == opt.streamId)
        
#if 0 
            for (i = 0; i < 16; /*packet.size;*/ i++)
            
                if (i%16 == 0) printf("\\n pktdata: ");
                printf("%2x  ", packet.data[i]);
            
            printf("\\n");
#endif            
            if (usefo)
            
                fwrite(packet.data, packet.size, 1, fpo1);
                fflush(fpo1);
            
 
            if (pCtx->streams[opt.streamId]->codec->codec_type == AVMEDIA_TYPE_VIDEO && !opt.nodec)
            
                picheight = pCtx->streams[opt.streamId]->codec->height;
                picwidth = pCtx->streams[opt.streamId]->codec->width;
 
                gettimeofday(&elapsed1, NULL); 
                avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);
                decoded++;
                gettimeofday(&elapsed2, NULL);
                dusecs = (elapsed2.tv_sec - elapsed1.tv_sec)*1000000 + (elapsed2.tv_usec - elapsed1.tv_usec);
                usecs1 += dusecs;
 
                if (got_picture)
                
                    printf("[Video: type %d, ref %d, pts %lld, pkt_pts %lld, pkt_dts %lld]", 
                            pFrame->pict_type, pFrame->reference, pFrame->pts, pFrame->pkt_pts, pFrame->pkt_dts);
 
                    if (pCtx->streams[opt.streamId]->codec->pix_fmt == PIX_FMT_YUV420P)
                    
                        if (usefo)
                        
                            linesize = pFrame->linesize[0];
                            pBuf = pFrame->data[0];
                            for (i = 0; i < picheight; i++)
                            
                                fwrite(pBuf, picwidth, 1, fpo2);
                                pBuf += linesize;
                            
 
                            linesize = pFrame->linesize[1];
                            pBuf = pFrame->data[1];
                            for (i = 0; i < picheight/2; i++)
                            
                                fwrite(pBuf, picwidth/2, 1, fpo2);
                                pBuf += linesize;
                                       
 
                            linesize = pFrame->linesize[2];
                            pBuf = pFrame->data[2];
                            for (i = 0; i < picheight/2; i++)
                            
                                fwrite(pBuf, picwidth/2, 1, fpo2);
                                pBuf += linesize;
                              
                            fflush(fpo2);
                         
 
                        if (opt.bplay)
                        
                            /* show picture */
                            show_picture(&fb, pFrame, picheight, picwidth, PIX_FMT_YUV420P);
                        
                    
                
                av_free_packet(&packet);
            
            else if (pCtx->streams[opt.streamId]->codec->codec_type == AVMEDIA_TYPE_AUDIO && !opt.nodec)
            
                int got;
 
                gettimeofday(&elapsed1, NULL);
                avcodec_decode_audio4(pCodecCtx, pFrame, &got, &packet);
                decoded++;
                gettimeofday(&elapsed2, NULL);
                dusecs = (elapsed2.tv_sec - elapsed1.tv_sec)*1000000 + (elapsed2.tv_usec - elapsed1.tv_usec);
                usecs1 += dusecs;
 
                if (got)
                
                    printf("[Audio: %5dB raw data, decoding time: %d]", pFrame->linesize[0], dusecs);
                    if (usefo)
                    
                        fwrite(pFrame->data[0],  pFrame->linesize[0], 1, fpo2);
                        fflush(fpo2);
                    
                    if (opt.bplay)
                    
                        play_pcm(&dsp, pFrame->data[0],  pFrame->linesize[0]);
                    
                
            
        
      
 
    if (!opt.nodec && pCodecCtx)
    
        avcodec_close(pCodecCtx);
    
 
    printf("\\n%d frames parsed, average %.2f us per frame\\n", nframe, usecs2/nframe);
    printf("%d frames decoded, average %.2f us per frame\\n", decoded, usecs1/decoded);
 
fail:
    if (pCtx)
    
        avformat_close_input(&pCtx);
    
    if (fpo1)
    
        fclose(fpo1);
    
    if (fpo2)
    
        fclose(fpo2);
    
    if (!pFrame)
    
        av_free(pFrame);
    
    if (!usefo && (dsp.audio_fd != -1))
    
        close(dsp.audio_fd);
    
    if (!usefo && (fb.video_fd != -1))
    
        close_video(&fb);
    
    return 0;

这一小段代码可以实现的功能包括:

  • 打开一个多媒体文件并获取基本的媒体信息。

  • 获取编码器句柄。

  • 根据给定的时间标签进行一个跳转。

  • 读取数据帧。

  • 解码音频帧或者视频帧。

  • 关闭多媒体文件。

这些功能足以支持一个功能强大的多媒体播放器,因为最复杂的解复用、解码、数据分析过程已经在FFMpeg内部实现了,需要关注的仅剩同步问题。

用户接口

数据结构

基本概念

编解码器、数据帧、媒体流和容器是数字媒体处理系统的四个基本概念。

首先需要统一术语:

  • 容器/文件(Conainer/File):即特定格式的多媒体文件。

  • 媒体流(Stream):指时间轴上的一段连续数据,如一段声音数据,一段视频数据或一段字幕数据,可以是压缩的,也可以是非压缩的,压缩的数据需要关联特定的编解码器。

  • 数据帧/数据包(Frame/Packet):通常,一个媒体流由大量的数据帧组成,对于压缩数据,帧对应着编解码器的最小处理单元。通常,分属于不同媒体流的数据帧交错复用于容器之中,参见交错

  • 编解码器:编解码器以帧为单位实现压缩数据和原始数据之间的相互转换。

在FFMPEG中,使用AVFormatContext、AVStream、AVCodecContext、AVCodec及AVPacket等结构来抽象这些基本要素,它们的关系如下图所示:

AVCodecContext

这是一个描述编解码器上下文的数据结构,包含了众多编解码器需要的参数信息,如下列出了部分比较重要的域:

typedef struct AVCodecContext 
 
    ......
 
    /**
     * some codecs need / can use extradata like Huffman tables.
     * mjpeg: Huffman tables
     * rv10: additional flags
     * mpeg4: global headers (they can be in the bitstream or here)
     * The allocated memory should be FF_INPUT_BUFFER_PADDING_SIZE bytes larger
     * than extradata_size to avoid prolems if it is read with the bitstream reader.
     * The bytewise contents of extradata must not depend on the architecture or CPU endianness.
     * - encoding: Set/allocated/freed by libavcodec.
     * - decoding: Set/allocated/freed by user.
     */
    uint8_t *extradata;
    int extradata_size;
    /**
     * This is the fundamental unit of time (in seconds) in terms
     * of which frame timestamps are represented. For fixed-fps content,
     * timebase should be 1/framerate and timestamp increments should be
     * identically 1.
     * - encoding: MUST be set by user.
     * - decoding: Set by libavcodec.
     */
    AVRational time_base;
 
    /* video only */
    /**
     * picture width / height.
     * - encoding: MUST be set by user.
     * - decoding: Set by libavcodec.
     * Note: For compatibility it is possible to set this instead of
     * coded_width/height before decoding.
     */
    int width, height;
 
    ......
 
    /* audio only */
    int sample_rate; ///< samples per second
    int channels;    ///< number of audio channels
 
    /**
     * audio sample format
     * - encoding: Set by user.
     * - decoding: Set by libavcodec.
     */
    enum SampleFormat sample_fmt;  ///< sample format
 
    /* The following data should not be initialized. */
    /**
     * Samples per packet, initialized when calling 'init'.
     */
    int frame_size;
    int frame_number;   ///< audio or video frame number
 
    ......
 
    char codec_name[32];
    enum AVMediaType codec_type; /* see AVMEDIA_TYPE_xxx */
    enum CodecID codec_id; /* see CODEC_ID_xxx */
 
    /**
     * fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
     * This is used to work around some encoder bugs.
     * A demuxer should set this to what is stored in the field used to identify the codec.
     * If there are multiple such fields in a container then the demuxer should choose the one
     * which maximizes the information about the used codec.
     * If the codec tag field in a container is larger then 32 bits then the demuxer should
     * remap the longer ID to 32 bits with a table or other structure. Alternatively a new
     * extra_codec_tag + size could be added but for this a clear advantage must be demonstrated
     * first.
     * - encoding: Set by user, if not then the default based on codec_id will be used.
     * - decoding: Set by user, will be converted to uppercase by libavcodec during init.
     */
    unsigned int codec_tag;            
 
    ......
 
    /**
     * Size of the frame reordering buffer in the decoder.
     * For MPEG-2 it is 1 IPB or 0 low delay IP.
     * - encoding: Set by libavcodec.
     * - decoding: Set by libavcodec.
     */
    int has_b_frames;
 
    /**
     * number of bytes per packet if constant and known or 0
     * Used by some WAV based audio codecs.
     */
    int block_align;
 
    ......
 
    /**
     * bits per sample/pixel from the demuxer (needed for huffyuv).
     * - encoding: Set by libavcodec.
     * - decoding: Set by user.
     */
     int bits_per_coded_sample;  
 
     ......
 
 AVCodecContext;

如果是单纯使用libavcodec,这部分信息需要调用者进行初始化;如果是使用整个FFMPEG库,这部分信息在调用avformat_open_input和avformat_find_stream_info的过程中根据文件的头信息及媒体流内的头部信息完成初始化。其中几个主要域的释义如下:

  1. extradata/extradata_size:这个buffer中存放了解码器可能会用到的额外信息,在av_read_frame中填充。一般来说,首先,某种具体格式的demuxer在读取格式头信息的时候会填充extradata,其次,如果demuxer没有做这个事情,比如可能在头部压根儿就没有相关的编解码信息,则相应的parser会继续从已经解复用出来的媒体流中继续寻找。在没有找到任何额外信息的情况下,这个buffer指针为空。

  2. time_base:编解码器的时间基准,实际上就是视频的帧率(或场率)。

  3. width/height:视频的宽和高。

  4. sample_rate/channels:音频的采样率和信道数目。

  5. sample_fmt: 音频的原始采样格式。

  6. codec_name/codec_type/codec_id/codec_tag:编解码器的信息。

AVStream

该结构体描述一个媒体流,定义如下:

typedef struct AVStream 
    int index;    /**< stream index in AVFormatContext */
    int id;       /**< format-specific stream ID */
    AVCodecContext *codec; /**< codec context */
    /**
     * Real base framerate of the stream.
     * This is the lowest framerate with which all timestamps can be
     * represented accurately (it is the least common multiple of all
     * framerates in the stream). Note, this value is just a guess!
     * For example, if the time base is 1/90000 and all frames have either
     * approximately 3600 or 1800 timer ticks, then r_frame_rate will be 50/1.
     */
    AVRational r_frame_rate;
 
    ......
 
    /**
     * This is the fundamental unit of time (in seconds) in terms
     * of which frame timestamps are represented. For fixed-fps content,
     * time base should be 1/framerate and timestamp increments should be 1.
     */
    AVRational time_base;
 
    ......
 
    /**
     * Decoding: pts of the first frame of the stream, in stream time base.
     * Only set this if you are absolutely 100% sure that the value you set
     * it to really is the pts of the first frame.
     * This may be undefined (AV_NOPTS_VALUE).
     * @note The ASF header does NOT contain a correct start_time the ASF
     * demuxer must NOT set this.
     */
    int64_t start_time;
    /**
     * Decoding: duration of the stream, in stream time base.
     * If a source file does not specify a duration, but does specify
     * a bitrate, this value will be estimated from bitrate and file size.
     */
    int64_t duration;
 
#if LIBAVFORMAT_VERSION_INT < (53<<16)
    char language[4]; /** ISO 639-2/B 3-letter language code (empty string if undefined) */
#endif
 
    /* av_read_frame() support */
    enum AVStreamParseType need_parsing;
    struct AVCodecParserContext *parser;
 
    ......
 
    /* av_seek_frame() support */
    AVIndexEntry *index_entries; /**< Only used if the format does not
                                    support seeking natively. */
    int nb_index_entries;
    unsigned int index_entries_allocated_size;
 
    int64_t nb_frames;                 ///< number of frames in this stream if known or 0
 
    ......
 
    /**
     * Average framerate
     */
    AVRational avg_frame_rate;
    ......
 AVStream;

主要域的释义如下,其中大部分域的值可以由avformat_open_input根据文件头的信息确定,缺少的信息需要通过调用avformat_find_stream_info读帧及软解码进一步获取:

  1. index/id:index对应流的索引,这个数字是自动生成的,根据index可以从AVFormatContext::streams表中索引到该流;而id则是流的标识,依赖于具体的容器格式。比如对于MPEG TS格式,id就是pid。

  2. time_base:流的时间基准,是一个实数,该流中媒体数据的pts和dts都将以这个时间基准为粒度。通常,使用av_rescale/av_rescale_q可以实现不同时间基准的转换。

  3. start_time:流的起始时间,以流的时间基准为单位,通常是该流中第一个帧的pts。

  4. duration:流的总时间,以流的时间基准为单位。

  5. need_parsing:对该流parsing过程的控制域。

  6. nb_frames:流内的帧数目。

  7. r_frame_rate/framerate/avg_frame_rate:帧率相关。

  8. codec:指向该流对应的AVCodecContext结构,调用avformat_open_input时生成。

  9. parser:指向该流对应的AVCodecParserContext结构,调用avformat_find_stream_info时生成。。

AVFormatContext

这个结构体描述了一个媒体文件或媒体流的构成和基本信息,定义如下:

typedef struct AVFormatContext 
    const AVClass *av_class; /**< Set by avformat_alloc_context. */
    /* Can only be iformat or oformat, not both at the same time. */
    struct AVInputFormat *iformat;
    struct AVOutputFormat *oformat;
    void *priv_data;
    ByteIOContext *pb;
    unsigned int nb_streams;
    AVStream *streams[MAX_STREAMS];
    char filename[1024]; /**< input or output filename */
    /* stream info */
    int64_t timestamp;
#if LIBAVFORMAT_VERSION_INT < (53<<16)
    char title[512];
    char author[512];
    char copyright[512];
    char comment[512];
    char album[512];
    int year;  /**< ID3 year, 0 if none */
    int track; /**< track number, 0 if none */
    char genre[32]; /**< ID3 genre */
#endif
 
    int ctx_flags; /**< Format-specific flags, see AVFMTCTX_xx */
    /* private data for pts handling (do not modify directly). */
    /** This buffer is only needed when packets were already buffered but
       not decoded, for example to get the codec parameters in MPEG
       streams. */
    struct AVPacketList *packet_buffer;
 
    /** Decoding: position of the first frame of the component, in
       AV_TIME_BASE fractional seconds. NEVER set this value directly:
       It is deduced from the AVStream values.  */
    int64_t start_time;
    /** Decoding: duration of the stream, in AV_TIME_BASE fractional
       seconds. Only set this value if you know none of the individual stream
       durations and also dont set any of them. This is deduced from the
       AVStream values if not set.  */
    int64_t duration;
    /** decoding: total file size, 0 if unknown */
    int64_t file_size;
    /** Decoding: total stream bitrate in bit/s, 0 if not
       available. Never set it directly if the file_size and the
       duration are known as FFmpeg can compute it automatically. */
    int bit_rate;
 
    /* av_read_frame() support */
    AVStream *cur_st;
#if LIBAVFORMAT_VERSION_INT < (53<<16)
    const uint8_t *cur_ptr_deprecated;
    int cur_len_deprecated;
    AVPacket cur_pkt_deprecated;
#endif
 
    /* av_seek_frame() support */
    int64_t data_offset; /** offset of the first packet */
    int index_built;
 
    int mux_rate;
    unsigned int packet_size;
    int preload;
    int max_delay;
 
#define AVFMT_NOOUTPUTLOOP -1
#define AVFMT_INFINITEOUTPUTLOOP 0
    /** number of times to loop output in formats that support it */
    int loop_output;
 
    int flags;
#define AVFMT_FLAG_GENPTS       0x0001 ///< Generate missing pts even if it requires parsing future frames.
#define AVFMT_FLAG_IGNIDX       0x0002 ///< Ignore index.
#define AVFMT_FLAG_NONBLOCK     0x0004 ///< Do not block when reading packets from input.
#define AVFMT_FLAG_IGNDTS       0x0008 ///< Ignore DTS on frames that contain both DTS & PTS
#define AVFMT_FLAG_NOFILLIN     0x0010 ///< Do not infer any values from other values, just return what is stored in the container
#define AVFMT_FLAG_NOPARSE      0x0020 ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled
#define AVFMT_FLAG_RTP_HINT     0x0040 ///< Add RTP hinting to the output file
 
    int loop_input;
    /** decoding: size of data to probe; encoding: unused. */
    unsigned int probesize;
 
    /**
     * Maximum time (in AV_TIME_BASE units) during which the input should
     * be analyzed in avformat_find_stream_info().
     */
    int max_analyze_duration;
 
    const uint8_t *key;
    int keylen;
 
    unsigned int nb_programs;
    AVProgram **programs;
 
    /**
     * Forced video codec_id.
     * Demuxing: Set by user.
     */
    enum CodecID video_codec_id;
    /**
     * Forced audio codec_id.
     * Demuxing: Set by user.
     */
    enum CodecID audio_codec_id;
    /**
     * Forced subtitle codec_id.
     * Demuxing: Set by user.
     */
    enum CodecID subtitle_codec_id;
 
    /**
     * Maximum amount of memory in bytes to use for the index of each stream.
     * If the index exceeds this size, entries will be discarded as
     * needed to maintain a smaller size. This can lead to slower or less
     * accurate seeking (depends on demuxer).
     * Demuxers for which a full in-memory index is mandatory will ignore
     * this.
     * muxing  : unused
     * demuxing: set by user
     */
    unsigned int max_index_size;
 
    /**
     * Maximum amount of memory in bytes to use for buffering frames
     * obtained from realtime capture devices.
     */
    unsigned int max_picture_buffer;
 
    unsigned int nb_chapters;
    AVChapter **chapters;
 
    /**
     * Flags to enable debugging.
     */
    int debug;
#define FF_FDEBUG_TS        0x0001
 
    /**
     * Raw packets from the demuxer, prior to parsing and decoding.
     * This buffer is used for buffering packets until the codec can
     * be identified, as parsing cannot be done without knowing the
     * codec.
     */
    struct AVPacketList *raw_packet_buffer;
    struct AVPacketList *raw_packet_buffer_end;
 
    struct AVPacketList *packet_buffer_end;
 
    AVMetadata *metadata;
 
    /**
     * Remaining size available for raw_packet_buffer, in bytes.
     * NOT PART OF PUBLIC API
     */
#define RAW_PACKET_BUFFER_SIZE 2500000
    int raw_packet_buffer_remaining_size;
 
    /**
     * Start time of the stream in real world time, in microseconds
     * since the unix epoch (00:00 1st January 1970). That is, pts=0
     * in the stream was captured at this real world time.
     * - encoding: Set by user.
     * - decoding: Unused.
     */
    int64_t start_time_realtime;
 AVFormatContext;

这是FFMpeg中最为基本的一个结构,是其他所有结构的根,是一个多媒体文件或流的根本抽象。其中:

  • nb_streams和streams所表示的AVStream结构指针数组包含了所有内嵌媒体流的描述;

  • iformat和oformat指向对应的demuxer和muxer指针;

  • pb则指向一个控制底层数据读写的ByteIOContext结构。

  • start_time和duration是从streams数组的各个AVStream中推断出的多媒体文件的起始时间和长度,以微妙为单位。

通常,这个结构由avformat_open_input在内部创建并以缺省值初始化部分成员。但是,如果调用者希望自己创建该结构,则需要显式为该结构的一些成员置缺省值——如果没有缺省值的话,会导致之后的动作产生异常。以下成员需要被关注:

  • probesize

  • mux_rate

  • packet_size

  • flags

  • max_analyze_duration

  • key

  • max_index_size

  • max_picture_buffer

  • max_delay

AVPacket

AVPacket定义在avcodec.h中,如下:

typedef struct AVPacket 
    /**
     * Presentation timestamp in AVStream->time_base units; the time at which
     * the decompressed packet will be presented to the user.
     * Can be AV_NOPTS_VALUE if it is not stored in the file.
     * pts MUST be larger or equal to dts as presentation cannot happen before
     * decompression, unless one wants to view hex dumps. Some formats misuse
     * the terms dts and pts/cts to mean something different. Such timestamps
     * must be converted to true pts/dts before they are stored in AVPacket.
     */
    int64_t pts;
    /**
     * Decompression timestamp in AVStream->time_base units; the time at which
     * the packet is decompressed.
     * Can be AV_NOPTS_VALUE if it is not stored in the file.
     */
    int64_t dts;
    uint8_t *data;
    int   size;
    int   stream_index;
    int   flags;
    /**
     * Duration of this packet in AVStream->time_base units, 0 if unknown.
     * Equals next_pts - this_pts in presentation order.
     */
    int   duration;
    void  (*destruct)(struct AVPacket *);
    void  *priv;
    int64_t pos;                            ///< byte position in stream, -1 if unknown
 
    /**
     * Time difference in AVStream->time_base units from the pts of this
     * packet to the point at which the output from the decoder has converged
     * independent from the availability of previous frames. That is, the
     * frames are virtually identical no matter if decoding started from
     * the very first frame or from this keyframe.
     * Is AV_NOPTS_VALUE if unknown.
     * This field is not the display duration of the current packet.
     *
     * The purpose of this field is to allow seeking in streams that have no
     * keyframes in the conventional sense. It corresponds to the
     * recovery point SEI in H.264 and match_time_delta in NUT. It is also
     * essential for some types of subtitle streams to ensure that all
     * subtitles are correctly displayed after seeking.
     */
    int64_t convergence_duration;
 AVPacket;

FFMPEG使用AVPacket来暂存媒体数据包及附加信息(解码时间戳、显示时间戳、时长等),这样的媒体数据包所承载的往往不是原始格式的音视频数据,而是以某种方式编码后的数据,编码信息由对应的媒体流结构AVStream给出。AVPacket包含以下数据域:

  • dts表示解码时间戳,pts表示显示时间戳,它们的单位是所属媒体流的时间基准。

  • stream_index给出所属媒体流的索引;

  • data为数据缓冲区指针,size为长度;

  • duration为数据的时长,也是以所属媒体流的时间基准为单位;

  • pos表示该数据在媒体流中的字节偏移量;

  • destruct为用于释放数据缓冲区的函数指针;

  • flags为标志域,其中,最低为置1表示该数据是一个关键帧。

AVPacket结构本身只是个容器,它使用data成员引用实际的数据缓冲区。这个缓冲区的管理方式有两种,其一是通过调用av_new_packet直接创建缓冲区,其二是引用已经存在的缓冲区。缓冲区的释放通过调用av_free_packet实现,其内部实现也采用了两种不同的释放方式,第一种方式是调用AVPacket的destruct函数,这个destruct函数可能是缺省的av_destruct_packet,对应av_new_packet或av_dup_packet创建的缓冲区,也可能是某个自定义的释放函数,表示缓冲区的提供者希望使用者在结束缓冲区的时候按照提供者期望的方式将其释放,第二种方式是仅仅将data和size的值清0,这种情况下往往是引用了一个已经存在的缓冲区,AVPacket的destruct指针为空。

在使用AVPacket时,对于缓冲区的提供者,必须注意通过设置destruct函数指针指定正确的释放方式,如果缓冲区提供者打算自己释放缓冲区,则切记将destruct置空;而对于缓冲区的使用者,务必在使用结束时调用av_free_packet释放缓冲区(虽然释放操作可能只是一个假动作)。如果某个使用者打算较长时间内占用一个AVPacket——比如不打算在函数返回之前释放它——最好调用av_dup_packet进行缓冲区的克隆,将其转化为自有分配的缓冲区,以免对缓冲区的不当占用造成异常错误。av_dup_packet会为destruct指针为空的AVPacket新建一个缓冲区,然后将原缓冲区的数据拷贝至新缓冲区,置data的值为新缓冲区的地址,同时设destruct指针为av_destruct_packet。

上述媒体结构可以通过FFMPEG提供的av_dump_format方法直观展示出来,以下例子以一个MPEG-TS文件为输入,其展示结果为:

Input #0, mpegts, from '/Videos/suite/ts/H.264_High_L3.1_720x480_23.976fps_AAC-LC.ts':
  Duration: 00:01:43.29, start: 599.958300, bitrate: 20934 kb/s
  Program 1 
    Stream #0.0[0x1011]: Video: h264 (High), yuv420p, 720x480 [PAR 32:27 DAR 16:9], 23.98 fps, 23.98 tbr, 90k tbn, 47.95 tbc
    Stream #0.1[0x1100]: Audio: aac, 48000 Hz, stereo, s16, 159 kb/s

从中可以找到媒体的格式、路径、时长、开始时间、全局比特率。此外,列出了媒体包含的一个节目,由两个媒体流组成,第一个媒体流是视频流,id为0x1011;第二个为音频流,id为0x1100。视频流是h264的High Profile编码,色彩空间为420p,大小为720×480,平均帧率23.98,参考帧率23.98,流时间基准是90000,编码的时间基准为47.95;音频流是aac编码,48kHz的采样,立体声,每个样点16比特,流的比特率是159kbps。

时间信息

时间信息用于实现多媒体同步。

同步的目的在于展示多媒体信息时,能够保持媒体对象之间固有的时间关系。同步有两类,一类是流内同步,其主要任务是保证单个媒体流内的时间关系,以满足感知要求,如按照规定的帧率播放一段视频;另一类是流间同步,主要任务是保证不同媒体流之间的时间关系,如音频和视频之间的关系(lipsync)。

对于固定速率的媒体,如固定帧率的视频或固定比特率的音频,可以将时间信息(帧率或比特率)置于文件首部(header),如AVI的hdrl List、MP4的moov box,还有一种相对复杂的方案是将时间信息嵌入媒体流的内部,如MPEG TS和Real video,这种方案可以处理变速率的媒体,亦可有效避免同步过程中的时间漂移。

FFMPEG会为每一个数据包打上时间标签,以更有效地支持上层应用的同步机制。时间标签有两种,一种是DTS,称为解码时间标签,另一种是PTS,称为显示时间标签。对于声音来说 ,这两个时间标签是相同的,但对于某些视频编码格式,由于采用了双向预测技术,会造成DTS和PTS的不一致。

无双向预测帧的情况:

图像类型: I   P   P   P   P   P   P ...  I   P   P
DTS:     0   1   2   3   4   5   6...  100 101 102
PTS:     0   1   2   3   4   5   6...  100 101 102

有双向预测帧的情况:

图像类型: I   P   B   B   P   B   B ...  I   P   B
DTS:     0   1   2   3   4   5   6 ...  100 101 102
PTS:     0   3   1   2   6   4   5 ...  100 104 102

对于存在双向预测帧的情况,通常要求解码器对图像重排序,以保证输出的图像顺序为显示顺序:

解码器输入:I   P   B   B   P   B   B
 (DTS)     0   1   2   3   4   5   6  
 (PTS)     0   3   1   2   6   4   5
解码器输出:X   I   B   B   P   B   B   P
 (PTS)     X   0   1   2   3   4   5   6

时间信息的获取:

通过调用avformat_find_stream_info,多媒体应用可以从AVFormatContext对象中拿到媒体文件的时间信息:主要是总时间长度和开始时间,此外还有与时间信息相关的比特率和文件大小。其中时间信息的单位是AV_TIME_BASE:微秒。

typedef struct AVFormatContext 
 
    ......
 
    /** Decoding: position of the first frame of the component, in
       AV_TIME_BASE fractional seconds. NEVER set this value directly:
       It is deduced from the AVStream values.  */
    int64_t start_time;
    /** Decoding: duration of the stream, in AV_TIME_BASE fractional
       seconds. Only set this value if you know none of the individual stream
       durations and also dont set any of them. This is deduced from the
       AVStream values if not set.  */
    int64_t duration;
    /** decoding: total file size, 0 if unknown */
    int64_t file_size;
    /** Decoding: total stream bitrate in bit/s, 0 if not
       available. Never set it directly if the file_size and the
       duration are known as FFmpeg can compute it automatically. */
    int bit_rate;
 
    ......
 
 AVFormatContext;

以上4个成员变量都是只读的,基于FFMpeg的中间件需要将其封装到某个接口中,如:

LONG GetDuratioin(IntfX*);
LONG GetStartTime(IntfX*);
LONG GetFileSize(IntfX*);
LONG GetBitRate(IntfX*);

APIs

FFMpeg的API大部分以0作为成功返回值而一个负数作为错误码。

读系列

读系列API的主要功能是根据某个指定的源获取媒体数据包,这个源可以是一个本地文件、一个RTSP或HTTP源、一个摄像头驱动或者其它。

avformat_open_input

int avformat_open_input(AVFormatContext **ic_ptr, const char *filename, AVInputFormat *fmt, AVDictionary **options);

avformat_open_input完成两个任务:

  1. 打开一个文件或URL,基于字节流的底层输入模块得到初始化。

  2. 解析多媒体文件或多媒体流的头信息,创建AV

    C语言游戏超详解扫雷游戏完整版,细节满满!!

    目录

     

    扫雷

    扫雷游戏规则介绍

    如何将扫雷游戏实现代码

    基本思路

    分步代码实现

    创建和打印游戏菜单

    初始化棋盘

    打印棋盘

    布置雷

    排查雷

    游戏主体——game()函数

    总代码实现

    game.h

    test.c

    game.c

    总结


    扫雷

    实现扫雷的算法有很多种,我在这里给大家最详细的代码介绍以及思考方法,细节满满哦!!


    扫雷游戏规则介绍

    每个格子有两种状态,有地雷或者没有地雷。玩家点到地雷游戏结束,玩家标记出所有地雷游戏胜利。

    每个没有地雷的格子点开后显示相邻8个格子里面存在地雷的数目,周边没有地雷则可以递归地打开与空相邻的方块;如果不幸触雷,则游戏结束。

    如何将扫雷游戏实现代码

    与上次三子棋游戏模块一致,分类创建:

    game.h:相关游戏函数的声明,变量的宏定义等;

    game.c:游戏相关函数的功能实现;

    test.c:游戏的测试,游戏的主题体;


    基本思路

    1.创建和打印游戏菜单

    2.创建两个棋盘数组,一个是布置雷的棋盘数组,一个是排查雷的棋盘数组

    3.初始化两个棋盘,为了防止后期统计排查雷的个数出现矛盾,所以我这里把布置雷的那个棋盘全部初始为'0',把排查雷的棋盘全部初始化为'*'

    4.打印棋盘

    5.布置雷,由电脑自主完成随机布置雷的个数,个数可以自己在头文件中定义

    6.排查雷,在布置雷的数组里排查,如果是雷则打印被炸死,并退出游戏,打印排查雷的棋盘;如果不是雷,则统计雷的个数,是0则展开空白,不是0则将雷的个数传给排查雷的那个数组

    7.判断输赢,如果空格的总的个数于行和列的乘积减去布雷的个数,则表示排雷成功


    分步代码实现

    创建和打印游戏菜单

    void menu()
    {
    	printf("**********************\\n");
    	printf("******* 1.play *******\\n");
    	printf("******* 0.exit *******\\n");
    	printf("**********************\\n");
    }
    int main()
    {
    	int input = 0;
    	srand((unsigned int)time(NULL));//用于随机函数rand的调用
    	do
    	{
    		menu();
    		scanf_s("%d", &input);
    		switch (input)
    		{
    		case 1:
    			printf("扫雷游戏开始\\n");
    			game();
    			break;
    		case 0:
    			printf("退出游戏\\n");
    			break;
    		default:
    			printf("输入错误,请重新选择!\\n");
    
    		}
    
    	} while (input);
    	return 0;
    }

    初始化棋盘

    	//两个数组初始化数组的实现,char board[ROWS][COLS]接收mine数组和show数组
    	//这里设置一个字符set接收mine和show数组传参过来的‘0’和‘*’
    void Initboard(char board[ROWS][COLS], int rows, int cols,char set)
    {
    	int i = 0;
    	int j = 0;
    	for (i = 0; i < rows; i++)
    	{
    		for (j = 0; j < cols; j++)
    		{
    			board[i][j] = set;
    		}
    	}
    }

    这里的初始化函数调用两次,分别初始化布雷数组和排雷数组,字符set分别接收'0'和'*';

    打印棋盘

    void Displayboard(char board[ROWS][COLS], int row, int col)
    {
    	int i = 0;
    	int j = 0;
    	printf("————扫雷游戏 ————\\n");
    	for (i = 0; i <= col; i++)
    	{
    		printf("%d ", i);     //为了方便用户判断行数和列数,将列数打印出来
    	}
    	printf("\\n");
    	for (i = 1; i <= row; i++)
    	{
    		printf("%d ", i);        //每行开头打印行数
    		for (j = 1; j <=col; j++)
    		{
    			printf("%c ", board[i][j]);
    		}
    		printf("\\n");
    	}
    	printf("————扫雷游戏————\\n");
    }

    打印棋盘结果如下:

    这样打印出来的棋盘好看也方便用户写入坐标

    布置雷

    void Setmine(char mine[ROWS][COLS], int row, int col)
    {
    	int count = SETCOUNT;	//为了灵活变通,可以自己设置布置雷的个数,在头文件中自定义个数
    	
         while (count)           //直到count为0才退出循环,并且每次都是随机坐标布置雷,count是几,
        {                          x和y就要随机几次
                           
    		
    		int x = rand() % row + 1;//用户输入的坐标的范围就是行数列数的范围,应该是row+1才是正确 
                                       的坐标范围
    		int y = rand() % col + 1;
    		if (mine[x][y] == '0')
    		{
    			mine[x][y] = '1';
    			count--;//每布置一个,雷的个数就减一;
    		}
    	}
    }

    排查雷

    void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col)
    {
    	int x = 0;
    	int y = 0;
    	int win = 0;//记录不是雷的个数,总数
    	while (win < row * col - SETCOUNT)
    	{
    		int count_blank = 0;//空白个数,每次进入循环都要重置,如果放在循环外面依旧是上一次的值会重复叠加空白;
    		printf("请输入排查雷的坐标:\\n");
    
    		scanf_s("%d%d", &x, &y);
    		if (x >= 1 && x <= row && y >= 1 && y <= col)		//判断坐标合法性
    		{
    			if (mine[x][y] == '1')		//遍历到了雷
    			{
    				printf("很遗憾,你被炸死了!\\n");
    				Displayboard(mine, row, col);
    				break;
    
    			}
    			else
    			{
    				int count_mine = get_mine_count(mine, x, y);
    				
    				if (count_mine == 0 && show[x][y]=='*')//周围雷的个数为0,且没有被遍历
    				{
    					count_blank=get_showblank(mine, show, x, y);//递归展开空白,返回空白个数
    					win += count_blank;//空白的个数加到不是雷个数的总数中
    				}
    				else if(show[x][y]=='*')//这里用else不太合适,这里控制的条件是当周围个数不是0,但是没有被遍历
    				{
                        show[x][y] = count_mine + '0';//不是雷,则统计周围有几个雷,放入show数组对应坐标
    					win++;//他统计了周围雷的个数,但本身不是雷,加1
    					
    				}
    				
    				Displayboard(show, row, col);
    				
    			}
    		}
    		else
    		{
    			printf("坐标不合法,请重新输入!\\n");
    		}
    	}
    	if (win== row * col - SETCOUNT)
    	{
    		printf("恭喜你,排雷成功!\\n");
    		Displayboard(show, row, col);
    	}
    
    		
    }

    当排查雷时,(x,y)处不是雷且周围雷的个数不为0则需要统计周围雷的个数:

    static int  get_mine_count(char mine[ROWS][COLS], int x,int y)  //static修饰函数,那这个函数 
                                                                      就只能在当前源文件里使用了
    
    {   //统计这些坐标周围有几个雷
    	return mine[x][y - 1] +
    		mine[x - 1][y - 1] +
    		mine[x + 1][y - 1] +
    		mine[x - 1][y] +
    		mine[x - 1][y + 1] +
    		mine[x][y + 1] +
    		mine[x + 1][y + 1] +
    		mine[x + 1][y] - 8 * '0';  //这里返回的是数字,num+'0'='num';即一个数字加上字符0等于数字代表的字符
    
    }

    当(x,y)周围雷的个数为0时展开为空格,去递归遍历周围四个坐标雷的个数,并且统计空格的个数:

    int get_showblank(char mine[ROWS][COLS], char show[ROWS][COLS], int x , int y)//当雷的个数为0时计算展开的空白个数
    {
    	
    	int count_mine = get_mine_count(mine, x,y);
        int count_blank = 0;//初始化空白的个数
    	if(count_mine == 0)
    	{
    		show[x][y] = ' ';
    		count_blank++;//只要是空白就加1;下次递归也一样
    
    	    //判断周围四个坐标合法性,并且要满足没有被遍历,依旧是*号,以防重复遍历
    		if (x - 1 >= 1 && x - 1 <= ROW && y >= 1 && y <= COL && show[x - 1][y] == '*')
    		{
    			count_blank+=get_showblank(mine, show, x - 1, y);//每递归一次,空白个数要累加,包括了x,y的空白和周围四个坐标的空白;
    			
    
    		}
    		if (x >= 1 && x <= ROW && y - 1 >= 1 && y - 1 <= COL && show[x][y - 1] == '*')
    		{
    			count_blank += get_showblank(mine, show, x, y - 1);
    			
    		}
    		if (x + 1 >= 1 && x + 1 <= ROW && y >= 1 && y <= COL && show[x + 1][y] == '*')
    		{
    			count_blank += get_showblank(mine, show, x + 1, y);
    			
    		}
    		if (x >= 1 && x <= ROW && y + 1 >= 1 && y + 1 <= COL && show[x][y + 1] == '*')
    		{
    			count_blank += get_showblank(mine, show, x, y + 1);
    		}
    			
    	}
    	return count_blank;//返回空白个数
       
    	
    
    
    
    	
    }

    游戏主体——game()函数

    void game()
    {
    	char mine[ROWS][COLS] = { 0 };	//创建存放布置雷的数组
    	char show[ROWS][COLS] = { 0 };	//创建存放排查雷的数组
    	Initboard(mine, ROWS, COLS,'0');	//初始化布置雷的棋盘,mine表示布置雷数组的首地址
    	Initboard(show, ROWS, COLS, '*');	//初始化排查雷的数组,show表示排查雷数组的首地址
    	//Displayboard(mine, ROW, COL);	//打印布置雷的棋盘,这里不用打印扩展的两行两列,只需在中间的棋盘布雷,扫雷也同样
    	Displayboard(show, ROW, COL);
    	Setmine(mine, ROW, COL);	//布置雷
    	//Displayboard(mine, ROW, COL);
    	Findmine(mine,show, ROW, COL);	//排查雷
    }

    扫雷游戏主体函数则相比三子棋简单,只需要调用游戏相关函数即可,这里需要注意的是在调用这些函数进行数组传参时,要注意参数的顺序,这里创建mine和show数组都是用的ROWS和COLS,所以在实现相关函数的功能时用的依然是ROWS和COLS。


    总代码实现

    game.h

    #pragma once
    #define ROW 9
    #define COL 9	
    #define SETCOUNT 10
    
    #define ROWS ROW+2		//为了能成功遍历边界的棋盘,所以需要创建两个扩展的数组将布雷和扫雷一一对应,初始化时也一样要扩展
    #define COLS COL+2		
    void Initboard(char board[ROWS][COLS], int rows, int cols,char set);
    void Displayboard(char board[ROWS][COLS], int row, int col);		//打印棋盘只打印中间的棋盘,不需要打印扩列的
    void Setmine(char mine[ROWS][COLS], int row, int col);
    void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);

    test.c

    #include<stdio.h>
    #include"game.h"
    #include<stdlib.h>
    #include<time.h>
    void game()
    {
    	char mine[ROWS][COLS] = { 0 };	//创建存放布置雷的数组
    	char show[ROWS][COLS] = { 0 };	//创建存放排查雷的数组
    	Initboard(mine, ROWS, COLS,'0');	//初始化布置雷的棋盘,mine表示布置雷数组的首地址
    	Initboard(show, ROWS, COLS, '*');	//初始化排查雷的数组,show表示排查雷数组的首地址
    	//Displayboard(mine, ROW, COL);	//打印布置雷的棋盘,这里不用打印扩展的两行两列,只需在中间的棋盘布雷,扫雷也同样
    	Displayboard(show, ROW, COL);
    	Setmine(mine, ROW, COL);	//布置雷
    	//Displayboard(mine, ROW, COL);
    	Findmine(mine,show, ROW, COL);	//排查雷
    }
    void menu()
    {
    	printf("**********************\\n");
    	printf("******* 1.play *******\\n");
    	printf("******* 0.exit *******\\n");
    	printf("**********************\\n");
    }
    int main()
    {
    	int input = 0;
    	srand((unsigned int)time(NULL));//用于随机函数rand的调用
    	do
    	{
    		menu();
    		scanf_s("%d", &input);
    		switch (input)
    		{
    		case 1:
    			printf("扫雷游戏开始\\n");
    			game();
    			break;
    		case 0:
    			printf("退出游戏\\n");
    			break;
    		default:
    			printf("输入错误,请重新选择!\\n");
    
    		}
    
    	} while (input);
    	return 0;
    }

    game.c

    #include<stdio.h>
    #include"game.h"
    	//两个数组初始化数组的实现,char board[ROWS][COLS]接收mine数组和show数组
    	//这里设置一个字符set接收mine和show数组传参过来的‘0’和‘*’
    void Initboard(char board[ROWS][COLS], int rows, int cols,char set)
    {
    	int i = 0;
    	int j = 0;
    	for (i = 0; i < rows; i++)
    	{
    		for (j = 0; j < cols; j++)
    		{
    			board[i][j] = set;
    		}
    	}
    }
    void Displayboard(char board[ROWS][COLS], int row, int col)
    {
    	int i = 0;
    	int j = 0;
    	printf("————扫雷游戏 ————\\n");
    	for (i = 0; i <= col; i++)
    	{
    		printf("%d ", i); //为了方便用户判断行数和列数,将列数打印出来
    	}
    	printf("\\n");
    	for (i = 1; i <= row; i++)
    	{
    		printf("%d ", i); //每行开头打印行数
    		for (j = 1; j <=col; j++)
    		{
    			printf("%c ", board[i][j]);
    		}
    		printf("\\n");
    	}
    	printf("————扫雷游戏————\\n");
    }
    void Setmine(char mine[ROWS][COLS], int row, int col)
    {
    	int count = SETCOUNT;	//为了灵活变通,可以自己设置布置雷的个数,在头文件中自定义个数
    	while (count)
    	{	//直到count为0才退出循环,并且每次都是随机坐标布置雷,count是几,x和y就要随机几次
    		int x = rand() % row + 1;//用户输入的坐标的范围就是行数列数的范围,应该是row+1才是正确的坐标范围
    		int y = rand() % col + 1;
    		if (mine[x][y] == '0')
    		{
    			mine[x][y] = '1';
    			count--;//每布置一个,雷的个数就减一;
    		}
    	}
    }
    static int  get_mine_count(char mine[ROWS][COLS], int x,int y)  //static修饰函数,那这个函数就只能在当前源文件里使用了
    
    {   //统计这些坐标周围有几个雷
    	return mine[x][y - 1] +
    		mine[x - 1][y - 1] +
    		mine[x + 1][y - 1] +
    		mine[x - 1][y] +
    		mine[x - 1][y + 1] +
    		mine[x][y + 1] +
    		mine[x + 1][y + 1] +
    		mine[x + 1][y] - 8 * '0';  //这里返回的是数字,num+'0'='num';即一个数字加上字符0等于数字代表的字符
    
    }
    int get_showblank(char mine[ROWS][COLS], char show[ROWS][COLS], int x , int y)//当雷的个数为0时计算展开的空白个数
    {
    	
    	int count_mine = get_mine_count(mine, x,y);
        int count_blank = 0;//初始化空白的个数
    	if(count_mine == 0)
    	{
    		show[x][y] = ' ';
    		count_blank++;//只要是空白就加1;下次递归也一样
    
    	    //判断周围四个坐标合法性,并且要满足没有被遍历,依旧是*号,以防重复遍历
    		if (x - 1 >= 1 && x - 1 <= ROW && y >= 1 && y <= COL && show[x - 1][y] == '*')
    		{
    			count_blank+=get_showblank(mine, show, x - 1, y);//每递归一次,空白个数要累加,包括了x,y的空白和周围四个坐标的空白;
    			
    
    		}
    		if (x >= 1 && x <= ROW && y - 1 >= 1 && y - 1 <= COL && show[x][y - 1] == '*')
    		{
    			count_blank += get_showblank(mine, show, x, y - 1);
    			
    		}
    		if (x + 1 >= 1 && x + 1 <= ROW && y >= 1 && y <= COL && show[x + 1][y] == '*')
    		{
    			count_blank += get_showblank(mine, show, x + 1, y);
    			
    		}
    		if (x >= 1 && x <= ROW && y + 1 >= 1 && y + 1 <= COL && show[x][y + 1] == '*')
    		{
    			count_blank += get_showblank(mine, show, x, y + 1);
    		}
    			
    	}
    	return count_blank;//返回空白个数
       
    	
    
    
    
    	
    }
    void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col)
    {
    	int x = 0;
    	int y = 0;
    	int win = 0;//记录不是雷的个数,总数
    	while (win < row * col - SETCOUNT)
    	{
    		int count_blank = 0;//空白个数,每次进入循环都要重置,如果放在循环外面依旧是上一次的值会重复叠加空白;
    		printf("请输入排查雷的坐标:\\n");
    
    		scanf_s("%d%d", &x, &y);
    		if (x >= 1 && x <= row && y >= 1 && y <= col)		//判断坐标合法性
    		{
    			if (mine[x][y] == '1')		//遍历到了雷
    			{
    				printf("很遗憾,你被炸死了!\\n");
    				Displayboard(mine, row, col);
    				break;
    
    			}
    			else
    			{
    				int count_mine = get_mine_count(mine, x, y);
    				
    				if (count_mine == 0 && show[x][y]=='*')//周围雷的个数为0,且没有被遍历
    				{
    					count_blank=get_showblank(mine, show, x, y);//递归展开空白,返回空白个数
    					win += count_blank;//空白的个数加到不是雷个数的总数中
    				}
    				else if(show[x][y]=='*')//这里用else不太合适,这里控制的条件是当周围个数不是0,但是没有被遍历
    				{
                        show[x][y] = count_mine + '0';//不是雷,则统计周围有几个雷,放入show数组对应坐标
    					win++;//他统计了周围雷的个数,但本身不是雷,加1
    					
    				}
    				
    				Displayboard(show, row, col);
    				
    			}
    		}
    		else
    		{
    			printf("坐标不合法,请重新输入!\\n");
    		}
    	}
    	if (win== row * col - SETCOUNT)
    	{
    		printf("恭喜你,排雷成功!\\n");
    		Displayboard(show, row, col);
    	}
    
    		
    }
    

    总结

    这是扫雷优化也叫完整版的扫雷游戏,博主花了一天时间思考和修改代码,整理博客,实属不易。这让我感觉到每一个游戏的实现离不开每一个细节的把控,如果忽略一个细节,整个游戏就无法完成。扫雷游戏细节颇多,对于我们的思考能力和代码实现能力有一定考验,彻底掌握则需要我们反复去琢磨,去动手实现,只有这样我们才能变得更强。如果这篇文章能帮助到你,请给我一键三连,有了大家的鼓励和指教我才能更进一步!!谢谢大家

     

     

     

     

     

     

     

     

    以上是关于FFMPEG详解(完整版)的主要内容,如果未能解决你的问题,请参考以下文章

    FFmpeg命令读取RTMP流如何设置超时时间

    使用FFMPEG在windows平台下推rtmp流

    在不中断 rtmp 流的情况下更新 ffmpeg 过滤器

    ffmpeg udp 直播发布到 rtmp

    FFmpeg入门 - rtmp推流

    Windows使用Nginx+ffmpeg搭建RTMP服务器