【Android FFMPEG 开发】FFMPEG 获取编解码器 ( 获取编解码参数 | 查找编解码器 | 获取编解码器上下文 | 设置上下文参数 | 打开编解码器 )

简介: 【Android FFMPEG 开发】FFMPEG 获取编解码器 ( 获取编解码参数 | 查找编解码器 | 获取编解码器上下文 | 设置上下文参数 | 打开编解码器 )

文章目录

博客简介 . FFMPEG 编解码器获取流程

I . FFMPEG 获取音视频流的编解码参数 AVCodecParameters *codecpar

II . FFMPEG 查找解码器 avcodec_find_decoder ( )

III . FFMPEG 获取编解码器上下文 avcodec_alloc_context3 ( )

IV . FFMPEG 设置编解码器上下文参数 avcodec_parameters_to_context ( )

V . FFMPEG 打开编解码器 avcodec_open2 ( )

VI . FFMPEG 获取编解码器 代码示例



博客简介 . FFMPEG 编解码器获取流程


FFMPEG 编解码器获取流程 : 在获取音视频流 AVStream *stream 之后 , 执行以下流程 ;



〇 获取 AVStream * 音视频流 ( 获取编解码器前提 ) : 参考博客 【Android FFMPEG 开发】FFMPEG 获取 AVStream 音视频流 ( AVFormatContext 结构体 | 获取音视频流信息 | 获取音视频流个数 | 获取音视频流 )



① 获取音视频流的编码参数 : AVStream *stream 结构体的 AVCodecParameters *codecpar 元素是音视频流的编解码参数 ; 包含 码率 , 宽度 , 高度 , 采样率 等参数信息 ;


//解码这个媒体流的参数信息 , 包含 码率 , 宽度 , 高度 , 采样率 等参数信息
AVCodecParameters *codecParameters = stream->codecpar;



② 查找编解码器 : 调用 avcodec_find_decoder ( ) 获取当前音视频流使用的编解码器 ;


//① 查找 当前流 使用的编码方式 , 进而查找编解码器 ( 可能失败 , 不支持的解码方式 )
AVCodec *avCodec = avcodec_find_decoder(codecParameters->codec_id);



③ 获取编解码器上下文 : 调用 avcodec_alloc_context3 ( ) 方法 , 获取编解码器上下文 ;


//② 获取编解码器上下文
AVCodecContext *avCodecContext = avcodec_alloc_context3(avCodec);



④ 设置编解码器上下文参数 : 调用 avcodec_parameters_to_context ( ) 方法 , 设置编解码器的上下文参数 ;


//③ 设置 编解码器上下文 参数
//      int avcodec_parameters_to_context(AVCodecContext *codec,
//              const AVCodecParameters *par);
//      返回值 > 0 成功 , < 0 失败
int parameters_to_context_result =
        avcodec_parameters_to_context(avCodecContext, codecParameters);



⑤ 打开编解码器 : 调用 avcodec_open2 ( ) 方法 , 打开编解码器 ;


//④ 打开编解码器
//   int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, 
//   返回 0 成功 , 其它失败
int open_codec_result = avcodec_open2(avCodecContext, avCodec, 0);



I . FFMPEG 获取音视频流的编解码参数 AVCodecParameters *codecpar


1 . 编解码参数封装在 AVStream 结构体中 : FFMPEG 音视频流的编码参数 AVCodecParameters *codecpar 是 AVStream 结构体的元素 ;


/**
 * Stream structure.
 * New fields can be added to the end with minor version bumps.
 * Removal, reordering and changes to existing fields require a major
 * version bump.
 * sizeof(AVStream) must not be used outside libav*.
 */
typedef struct AVStream {
  ...
  /**
     * Codec parameters associated with this stream. Allocated and freed by
     * libavformat in avformat_new_stream() and avformat_free_context()
     * respectively.
     *
     * - demuxing: filled by libavformat on stream creation or in
     *             avformat_find_stream_info()
     * - muxing: filled by the caller before avformat_write_header()
     */
    AVCodecParameters *codecpar;
  ...
}AVStream;



2 . FFMPEG 获取音视频流的编码参数 示例 : 其中 AVStream *stream 是之前获取的音视频流结构体指针 ;


//解码这个媒体流的参数信息 , 包含 码率 , 宽度 , 高度 , 采样率 等参数信息
AVCodecParameters *codecParameters = stream->codecpar;


3 . 编解码参数 AVCodecParameters *codecpar 中封装的数据 : 这里只列举我们需要使用的 ;


① enum AVCodecID codec_id : 编解码器使用的编码数据的特定类型 , 需要使用该值获取解码器 ;


/**
 * This struct describes the properties of an encoded stream.
 *
 * sizeof(AVCodecParameters) is not a part of the public ABI, this struct must
 * be allocated with avcodec_parameters_alloc() and freed with
 * avcodec_parameters_free().
 */
typedef struct AVCodecParameters{
  ...
    /**
     * Specific type of the encoded data (the codec used).
     */
    enum AVCodecID   codec_id;
    ...
}AVCodecParameters;



II . FFMPEG 查找解码器 avcodec_find_decoder ( )


1 . avcodec_find_decoder ( ) 函数原型 : 根据编解码器 ID , 查找被注册的解码器 ;



① enum AVCodecID id 参数 : 代表了一个编码数据的特定类型 ; ( 详情查看 I . 3 . ① 小节内容 )


② AVCodec *avCodec 返回值 : 返回值是 AVCodec * 结构体指针类型变量 , 代表了获取的解码器 ;


/**  avcodec.h
 * Find a registered decoder with a matching codec ID.
 *
 * @param id AVCodecID of the requested decoder
 * @return A decoder if one was found, NULL otherwise.
 */
AVCodec *avcodec_find_decoder(enum AVCodecID id



2 . FFMPEG 查找解码器 avcodec_find_decoder ( ) 使用示例 :


//① 查找 当前流 使用的编码方式 , 进而查找编解码器 ( 可能失败 , 不支持的解码方式 )
AVCodec *avCodec = avcodec_find_decoder(codecParameters->codec_id);
//查找失败处理
if(avCodec == NULL){
    //如果没有找到编解码器 , 回调失败 , 方法直接返回 , 后续代码不执行
    callHelper->onError(pid, 2);
    __android_log_print(ANDROID_LOG_ERROR , "FFMPEG" , "查找 编解码器 失败");
    return;
}



III . FFMPEG 获取编解码器上下文 avcodec_alloc_context3 ( )


avcodec_alloc_context3 ( ) 函数原型 : 获取编解码器上下文 ;



① const AVCodec *codec 参数 : 要获取上下文的编解码器 ;


② AVCodecContext *avCodecContext 返回值 : 编解码器上下文 , 封装了很多编解码器相关参数 ; 如果为 NULL , 说明获取失败 ;


/**  avcodec.h
 * Allocate an AVCodecContext and set its fields to default values. The
 * resulting struct should be freed with avcodec_free_context().
 *
 * @param codec if non-NULL, allocate private data and initialize defaults
 *              for the given codec. It is illegal to then call avcodec_open2()
 *              with a different codec.
 *              If NULL, then the codec-specific defaults won't be initialized,
 *              which may result in suboptimal default settings (this is
 *              important mainly for encoders, e.g. libx264).
 *
 * @return An AVCodecContext filled with default values or NULL on failure.
 */
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);



2 . FFMPEG 获取编解码器上下文 avcodec_alloc_context3 ( ) 使用示例 :


//② 获取编解码器上下文
AVCodecContext *avCodecContext = avcodec_alloc_context3(avCodec);
//获取编解码器失败处理
if(avCodecContext == NULL){
    callHelper->onError(pid, 3);
    __android_log_print(ANDROID_LOG_ERROR , "FFMPEG" , "创建编解码器上下文 失败");
    return;
}




IV . FFMPEG 设置编解码器上下文参数 avcodec_parameters_to_context ( )


1 . avcodec_parameters_to_context ( ) 函数原型 : 基于编解码器提供的编解码参数设置编解码器上下文参数 ;



① AVCodecContext *codec 参数 : 要设置参数的编解码器上下文 , 这里当做返回值使用 , 这个值之后还要使用 ;


② const AVCodecParameters *par 参数 : 媒体流的参数信息 , 包含 码率 , 宽度 , 高度 , 采样率 等参数信息 , 是 AVStream 结构体封装的元素 ;


③ 返回值 : 如果返回值 >= 0 , 说明设置编解码器上下文参数操作成功 , 如果 < 0 , 设置失败 ;


/**
 * Fill the codec context based on the values from the supplied codec
 * parameters. Any allocated fields in codec that have a corresponding field in
 * par are freed and replaced with duplicates of the corresponding field in par.
 * Fields in codec that do not have a counterpart in par are not touched.
 *
 * @return >= 0 on success, a negative AVERROR code on failure.
 */
int avcodec_parameters_to_context(AVCodecContext *codec,
                                  const AVCodecParameters *par);


2 . FFMPEG 设置编解码器上下文参数 avcodec_parameters_to_context ( ) 使用示例 :


//③ 设置 编解码器上下文 参数
//      int avcodec_parameters_to_context(AVCodecContext *codec,
//              const AVCodecParameters *par);
//      返回值 > 0 成功 , < 0 失败
int parameters_to_context_result =
        avcodec_parameters_to_context(avCodecContext, codecParameters);
//设置 编解码器上下文 参数 失败处理
if(parameters_to_context_result < 0){
    callHelper->onError(pid, 4);
    __android_log_print(ANDROID_LOG_ERROR , "FFMPEG" , "设置编解码器上下文参数 失败");
    return;
}



V . FFMPEG 打开编解码器 avcodec_open2 ( )


1 . avcodec_open2 ( ) 函数原型 : 打开编解码器 , 之前必须先初始化编解码器上下文 AVCodecContext ;



① AVCodecContext *avctx 参数 : 之前根据编解码器获取上下文参数 avcodec_alloc_context3(avCodec) , 并为其设置了音视频流编解码参数 avcodec_parameters_to_context(avCodecContext, codecParameters) ;


② const AVCodec *codec 参数 : 需要打开的编解码上下文对应的编解码器 ;


③ 返回值 : 返回 0 成功 , 其它值 失败 ;


/**
 * Initialize the AVCodecContext to use the given AVCodec. Prior to using this
 * function the context has to be allocated with avcodec_alloc_context3().
 *
 * The functions avcodec_find_decoder_by_name(), avcodec_find_encoder_by_name(),
 * avcodec_find_decoder() and avcodec_find_encoder() provide an easy way for
 * retrieving a codec.
 *
 * @warning This function is not thread safe!
 *
 * @note Always call this function before using decoding routines (such as
 * @ref avcodec_receive_frame()).
 *
 * @code
 * avcodec_register_all();
 * av_dict_set(&opts, "b", "2.5M", 0);
 * codec = avcodec_find_decoder(AV_CODEC_ID_H264);
 * if (!codec)
 *     exit(1);
 *
 * context = avcodec_alloc_context3(codec);
 *
 * if (avcodec_open2(context, codec, opts) < 0)
 *     exit(1);
 * @endcode
 *
 * @param avctx The context to initialize.
 * @param codec The codec to open this context for. If a non-NULL codec has been
 *              previously passed to avcodec_alloc_context3() or
 *              for this context, then this parameter MUST be either NULL or
 *              equal to the previously passed codec.
 * @param options A dictionary filled with AVCodecContext and codec-private options.
 *                On return this object will be filled with options that were not found.
 *
 * @return zero on success, a negative value on error
 * @see avcodec_alloc_context3(), avcodec_find_decoder(), avcodec_find_encoder(),
 *      av_dict_set(), av_opt_find().
 */
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);



2 . FFMPEG 打开编解码器 avcodec_open2 ( ) 使用示例 :


//④ 打开编解码器
//   int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
//   返回 0 成功 , 其它失败
int open_codec_result = avcodec_open2(avCodecContext, avCodec, 0);
//打开编解码器 失败处理
if(open_codec_result != 0){
    callHelper->onError(pid, 5);
    __android_log_print(ANDROID_LOG_ERROR , "FFMPEG" , "打开 编解码器 失败");
    return;
}




VI . FFMPEG 获取编解码器 代码示例


   

//视频 / 音频 处理需要的操作 ( 获取编解码器 )
        //① 查找 当前流 使用的编码方式 , 进而查找编解码器 ( 可能失败 , 不支持的解码方式 )
        AVCodec *avCodec = avcodec_find_decoder(codecParameters->codec_id);
        //查找失败处理
        if(avCodec == NULL){
            //如果没有找到编解码器 , 回调失败 , 方法直接返回 , 后续代码不执行
            callHelper->onError(pid, 2);
            __android_log_print(ANDROID_LOG_ERROR , "FFMPEG" , "查找 编解码器 失败");
            return;
        }
        //② 获取编解码器上下文
        AVCodecContext *avCodecContext = avcodec_alloc_context3(avCodec);
        //获取编解码器失败处理
        if(avCodecContext == NULL){
            callHelper->onError(pid, 3);
            __android_log_print(ANDROID_LOG_ERROR , "FFMPEG" , "创建编解码器上下文 失败");
            return;
        }
        //③ 设置 编解码器上下文 参数
        //      int avcodec_parameters_to_context(AVCodecContext *codec,
        //              const AVCodecParameters *par);
        //      返回值 > 0 成功 , < 0 失败
        int parameters_to_context_result =
                avcodec_parameters_to_context(avCodecContext, codecParameters);
        //设置 编解码器上下文 参数 失败处理
        if(parameters_to_context_result < 0){
            callHelper->onError(pid, 4);
            __android_log_print(ANDROID_LOG_ERROR , "FFMPEG" , "设置编解码器上下文参数 失败");
            return;
        }
        //④ 打开编解码器
        //   int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
        //   返回 0 成功 , 其它失败
        int open_codec_result = avcodec_open2(avCodecContext, avCodec, 0);
        //打开编解码器 失败处理
        if(open_codec_result != 0){
            callHelper->onError(pid, 5);
            __android_log_print(ANDROID_LOG_ERROR , "FFMPEG" , "打开 编解码器 失败");
            return;
        }


目录
相关文章
|
21小时前
FFmpeg开发笔记(十八)FFmpeg兼容各种音频格式的播放
《FFmpeg开发实战》一书中,第10章示例程序playaudio.c原本仅支持mp3和aac音频播放。为支持ogg、amr、wma等非固定帧率音频,需进行三处修改:1)当frame_size为0时,将输出采样数量设为512;2)遍历音频帧时,计算实际采样位数以确定播放数据大小;3)在SDL音频回调函数中,确保每次发送len字节数据。改进后的代码在chapter10/playaudio2.c,可编译运行播放ring.ogg测试,成功则显示日志并播放铃声。
FFmpeg开发笔记(十八)FFmpeg兼容各种音频格式的播放
|
2天前
|
算法 Linux Windows
FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass
在Windows环境下为FFmpeg集成字幕渲染库libass涉及多个步骤,包括安装freetype、libxml2、gperf、fontconfig、fribidi、harfbuzz和libass。每个库的安装都需要下载源码、配置、编译和安装,并更新PKG_CONFIG_PATH环境变量。最后,重新配置并编译FFmpeg以启用libass及相关依赖。完成上述步骤后,通过`ffmpeg -version`确认libass已成功集成。
FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass
|
5天前
|
编解码 缓存 安全
Android SELinux 参数语法介绍及基础分析
Android SELinux 参数语法介绍及基础分析
10 0
|
5天前
|
Java Shell Android开发
Android11 有线网和wifi优先级设置
Android11 有线网和wifi优先级设置
11 0
|
6天前
|
Java Android开发
Android 设置系统时区的源码追踪
Android 设置系统时区的源码追踪
11 1
|
7天前
|
安全 Linux Android开发
FFmpeg开发笔记(十六)Linux交叉编译Android的OpenSSL库
该文介绍了如何在Linux服务器上交叉编译Android的FFmpeg库以支持HTTPS视频播放。首先,从GitHub下载openssl源码,解压后通过编译脚本`build_openssl.sh`生成64位静态库。接着,更新环境变量加载openssl,并编辑FFmpeg配置脚本`config_ffmpeg_openssl.sh`启用openssl支持。然后,编译安装FFmpeg。最后,将编译好的库文件导入App工程的相应目录,修改视频链接为HTTPS,App即可播放HTTPS在线视频。
FFmpeg开发笔记(十六)Linux交叉编译Android的OpenSSL库
|
9天前
|
Web App开发 Windows
FFmpeg开发笔记(十五)详解MediaMTX的推拉流
MediaMTX是开源轻量级流媒体服务器,提供RTSP, RTMP, HLS, WebRTC和SRT服务。启动后,它在不同端口监听。通过FFmpeg的推拉流测试,证明了MediaMTX成功实现HLS流媒体转发,但HLS播放兼容性问题可能因缺少音频流导致。推流地址为rtsp://127.0.0.1:8554/stream,RTMP地址为rtmp://127.0.0.1:1935/stream,HLS播放地址为http://127.0.0.1:8888/stream(Chrome)和http://127.0.0.1:8888/stream/index.m3u8(其他播放器可能不支持)。
FFmpeg开发笔记(十五)详解MediaMTX的推拉流
|
12天前
|
缓存 编解码
FFmpeg开发笔记(十四)FFmpeg音频重采样的缓存
FFmpeg在视频流重编码和音频重采样中使用缓存机制。在音频文件格式转换时,特别是对于帧长度不固定的格式如ogg、amr、wma,需处理重采样缓存。通过调用`swr_convert`,传入空输入和0大小来清空缓存。在`swrmp3.c`中,修改帧样本数处理,并在循环结束后添加代码以冲刷缓存。编译并运行程序,将ogg文件重采样为MP3,日志显示操作成功,播放转换后的文件确认功能正常。
FFmpeg开发笔记(十四)FFmpeg音频重采样的缓存
|
14天前
|
编解码 Linux Windows
FFmpeg开发笔记(十三)Windows环境给FFmpeg集成libopus和libvpx
本文档介绍了在Windows环境下如何为FFmpeg集成libopus和libvpx库。首先,详细阐述了安装libopus的步骤,包括下载源码、配置、编译和安装,并更新环境变量。接着,同样详细说明了libvpx的安装过程,注意需启用--enable-pic选项以避免编译错误。最后,介绍了重新配置并编译FFmpeg以启用这两个库,通过`ffmpeg -version`检查是否成功集成。整个过程参照了《FFmpeg开发实战:从零基础到短视频上线》一书的相关章节。
FFmpeg开发笔记(十三)Windows环境给FFmpeg集成libopus和libvpx
|
16天前
|
编解码 Linux
FFmpeg开发笔记(十二)Linux环境给FFmpeg集成libopus和libvpx
在《FFmpeg开发实战》一书中,介绍了如何在Linux环境下为FFmpeg集成libopus和libvpx,以支持WebM格式的Opus和VP8/VP9编码。首先,下载并安装libopus。接着,下载并安装libvpx。最后,在FFmpeg源码目录下,重新配置FFmpeg,启用libopus和libvpx,编译并安装。通过`ffmpeg -version`检查版本信息,确认libopus和libvpx已启用。
FFmpeg开发笔记(十二)Linux环境给FFmpeg集成libopus和libvpx