行业知识
FFmpeg对H246进行编解码的实现
2026-11-17 13:40:57
来源:互联网转载
avformat_open_input() ���数是用于打开媒体文件并获取媒体文件信息的函数,该函数定义在libavformat/avformat.h中。
()函数原型为:
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);函数的参数含义如下:ps: AVFormatContext结构体的指针。该参数用于存储打开的媒体文件的信息。当该函数成功返回时,AVFormatContext结构体中将存储媒体文件的相关信息。url:要打开的媒体文件的URL。可以是本地文件路径,也可以是HTTP URL或其他协议的URL。fmt:AVInputFormat结构体的指针,用于指定媒体文件的格式。如果该参数为NULL,则根据文件扩展名自动选择输入格式。options:AVDictionary结构体的指针,用于传递打开媒体文件时的选项。
-------------------------------------------------------
()比较重要结构体介绍
AVFormatContext 结构体。该参数用于存储打开的媒体文件的信息。当该函数成功返回时,AVFormatContext结构体中将存储媒体文件的相关信息。是FFmpeg 解封装(flv,mp4,rmvb,avi)功能的结构体。
typedef struct AVFormatContext{ struct AVInputFormat *iformat; //输入数据的封装格式 AVIOContext *pb; //输入数据的缓存 unsigned int nb_streams; //视音频流的个数 AVStream **streams; //视音频流 char filename[1024]; //文件名 int64_t duration; //时长(单位:微秒ms,转换为秒需要除以1_000_000) int bit_rate; //比特率(单位bps,转换为kbps需要除以1_000) AVDictionary *metadata; //元数据}AVFormatContext -----------------------------------------------------------
AVCodecContext是包含变量较多的结构体,AVCodecContext中很多的参数是编码的时候使用的,而不是解码的时候使用的。
typedef struct AVCodecContext{ enum AVMediaType codec_type:编解码器的类型(视频,音频...) struct AVCodec *codec:采用的解码器AVCodec(H.264,MPEG2...) int bit_rate:平均比特率 uint8_t *extradata; int extradata_size:针对特定编码器包含的附加信息(例如对于H.264解码器来 说,存储SPS,PPS等) AVRational time_base:根据该参数,可以把PTS转化为实际的时间(单位为秒s) int width, height:如果是视频的话,代表宽和高 int refs:运动估计参考帧的个数(H.264的话会有多帧,MPEG2这类的一般就没有了) int sample_rate:采样率(音频) int channels:声道数(音频) enum AVSampleFormat sample_fmt:采样格式 int profile:型(H.264里面就有,其他编码标准应该也有) int level:级(和profile差不太多)}AVCodecContext ----------------------------------------------------
AVCodec结构体是FFmpeg比较重要的结构体之一,主要用于存储编解码器的信息。
typedef struct AVCodec{ const char *name:编解码器的名字,比较短 const char *long_name:编解码器的名字,全称,比较长 enum AVMediaType type:指明了类型,是视频,音频,还是字幕 enum AVCodecID id:ID,不重复 const AVRational *supported_framerates:支持的帧率(仅视频) const enum AVPixelFormat *pix_fmts:支持的像素格式(仅视频) const int *supported_samplerates:支持的采样率(仅音频) const enum AVSampleFormat *sample_fmts:支持的采样格式(仅音频) const uint64_t *channel_layouts:支持的声道数(仅音频) int priv_data_size:私有数据的大小} --------------------------------------------------
AVMediaType编解码器类型:
enum AVMediaType { AVMEDIA_TYPE_UNKNOWN = -1, /// ====================================
FFmpeg对H246进行编解码的实现
#include "libavutil/adler32.h"#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"#include "libavutil/imgutils.h"#include "libavutil/timestamp.h"static int video_decode_example(const char *input_filename){ const AVCodec *codec = NULL; AVCodecContext *ctx= NULL; AVCodecParameters *origin_par = NULL; AVFrame *fr = NULL; uint8_t *byte_buffer = NULL; AVPacket *pkt; AVFormatContext *fmt_ctx = NULL; int number_of_written_bytes; int video_stream; int byte_buffer_size; int i = 0; int result; result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL); if (result streams[video_stream]->codecpar; codec = avcodec_find_decoder(origin_par->codec_id); if (!codec) { av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n"); return -1; } ctx = avcodec_alloc_context3(codec); if (!ctx) { av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n"); return AVERROR(ENOMEM); } result = avcodec_parameters_to_context(ctx, origin_par); if (result) { av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n"); return result; } result = avcodec_open2(ctx, codec, NULL); if (result pix_fmt, ctx->width, ctx->height, 16); byte_buffer = av_malloc(byte_buffer_size); if (!byte_buffer) { av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n"); return AVERROR(ENOMEM); } printf("#tb %d: %d/%d\n", video_stream, fmt_ctx->streams[video_stream]->time_base.num, fmt_ctx->streams[video_stream]->time_base.den); i = 0; result = 0; while (result >= 0) { result = av_read_frame(fmt_ctx, pkt); if (result >= 0 && pkt->stream_index != video_stream) { av_packet_unref(pkt); continue; } if (result pts == AV_NOPTS_VALUE) pkt->pts = pkt->dts = i; result = avcodec_send_packet(ctx, pkt); } av_packet_unref(pkt); if (result = 0) { result = avcodec_receive_frame(ctx, fr); if (result == AVERROR_EOF) goto finish; else if (result == AVERROR(EAGAIN)) { result = 0; break; } else if (result data, (const int*) fr->linesize, ctx->pix_fmt, ctx->width, ctx->height, 1); if (number_of_written_bytes pts), av_ts2str(fr->pkt_dts), fr->duration, number_of_written_bytes, av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes)); av_frame_unref(fr); } i++; }finish: av_packet_free(&pkt); av_frame_free(&fr); avformat_close_input(&fmt_ctx); avcodec_free_context(&ctx); av_freep(&byte_buffer); return 0;}int main(int argc, char **argv){ if (argc
上一篇:温州服务器怎么样?