ffmpeg_sample解读_extract_mvs
2020-10-29 本文已影响0人
刘佳阔
title: ffmpeg_sample解读_extract_mvs
date: 2020-10-28 10:15:02
tags: [读书笔记]
typora-copy-images-to: ./imgs
typora-root-url: ./imgs
总结
FFmpeg中的libavfilter提供了一个通用的音视频filter框架。使用avfilter可以对音视频数据做一些效果处理如去色调、模糊、水平翻转、裁剪、加方框、叠加文字等功能。
本示例为对压缩的视频数据先进行解码,然后对解码出来的帧进行特效处理。然后把图像映射为字符图效果的形式打印在终端。示例来源于[3]。与[5]的处理流程类似,增加了解封装解码的步骤。
总体流程也是很清楚.和上一篇有点区别是 他从命令行字符串中读取参数.生成一系列过滤器.然后和
graph TB
if[init_filters]
-->afgba[avfilter_get_by_name]
-->afia[avfilter_inout_alloc]
-->afga[avfilter_graph_alloc]
-->agcf[avfilter_graph_create_filter]
-->aosil[av_opt_set_int_list]
-->afgpp[avfilter_graph_parse_ptr]
-->afgc[avfilter_graph_config]
-->arf{av_read_frame>0?}
arf-->|yes|ascp[avcodec_send_packet]
arf-->|no|release[release]
ascp-->acrf{avcodec_receive_frame>0?}
acrf-->|no|release[release]
acrf-->|yes|abaff[av_buffersrc_add_frame_flags]
-->absgf[av_buffersink_get_frame]
-->pf[print_frame]
-->release
流程比较简单.这里就是初始化了三个过滤器.其中一个是从参数中读取过来的.需要解析这个字符串过滤器后把三个过滤器连起来.然后就是把数据送入abuffersrc过滤器.在从abuffersink中取出数据
image-20201028191917145源码
/**
* @file
* API example for audio decoding and filtering
* @example filtering_audio.c
*/
#include <unistd.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/opt.h>
static const char *filter_descr = "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono";
static const char *player = "ffplay -f s16le -ar 8000 -ac 1 -";
static AVFormatContext *fmt_ctx;
static AVCodecContext *dec_ctx;
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
static int audio_stream_index = -1;
static int open_input_file(const char *filename) {
int ret;
AVCodec *dec;
//数据读入到fmt_ctx中
if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}
//获取流信息.读入到fmt_ctx中
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return ret;
}
/* select the audio stream *///找到合适的音频流和解码器
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find an audio stream in the input file\n");
return ret;
}
audio_stream_index = ret;
/* create decoding context *///初始解码器上下文
dec_ctx = avcodec_alloc_context3(dec);
if (!dec_ctx)
return AVERROR(ENOMEM);
//拷贝音频流中的解码参数到 解码器上下文中
avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[audio_stream_index]->codecpar);
/* init the audio decoder */
//在用解码器的参数填入解码器上下文
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open audio decoder\n");
return ret;
}
return 0;
}
/**
* 使用字符串初始化过滤器,然后和两个输入输出过滤器相连接
* @param filters_descr
* @return
*/
static int init_filters(const char *filters_descr) {
char args[512];
int ret = 0;
//这两个分别对应输入的过滤器和输出的过滤器
const AVFilter *abuffersrc = avfilter_get_by_name("abuffer");
const AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
//过滤器上下文输入输出的包装结构,链表结构的过滤器链
AVFilterInOut *outputs = avfilter_inout_alloc();
AVFilterInOut *inputs = avfilter_inout_alloc();
//音频参数. 格式,声道layout.采样率
static const enum AVSampleFormat out_sample_fmts[] = {AV_SAMPLE_FMT_S16, -1};
static const int64_t out_channel_layouts[] = {AV_CH_LAYOUT_MONO, -1};
static const int out_sample_rates[] = {8000, -1};
//两个过滤器的连接
const AVFilterLink *outlink;
//时间基,是采样率的倒数
AVRational time_base = fmt_ctx->streams[audio_stream_index]->time_base;
//分配过滤器图形 反正每次都会分配这个东西
filter_graph = avfilter_graph_alloc();
if (!outputs || !inputs || !filter_graph) {
ret = AVERROR(ENOMEM);
goto end;
}
/* buffer audio source: the decoded frames from the decoder will be inserted here. */
if (!dec_ctx->channel_layout)
dec_ctx->channel_layout = av_get_default_channel_layout(dec_ctx->channels);
//把各种参数写入args的字符串中,就是初始化了音频参数.时间基.采样率,通道布局,采样位数
snprintf(args, sizeof(args),
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
time_base.num, time_base.den, dec_ctx->sample_rate,
av_get_sample_fmt_name(dec_ctx->sample_fmt), dec_ctx->channel_layout);
//初始化一个过滤器上下文,传给 过滤器图形,使用上边的参数, 最后生成的参数就是buffer_src_ctx
ret = avfilter_graph_create_filter(&buffersrc_ctx, abuffersrc, "in",
args, NULL, filter_graph);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
goto end;
}
//在初始化一个过滤器上下文,使用之前的输出过滤器
/* buffer audio sink: to terminate the filter chain. */
ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out",
NULL, NULL, filter_graph);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
goto end;
}
//设置参数 sample_fmts 把二进制数据设置到整数集合中
ret = av_opt_set_int_list(buffersink_ctx, "sample_fmts", out_sample_fmts, -1,
AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
goto end;
}
//设置参数channel_layouts
ret = av_opt_set_int_list(buffersink_ctx, "channel_layouts", out_channel_layouts, -1,
AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
goto end;
}
//设置参数 sample_rates
ret = av_opt_set_int_list(buffersink_ctx, "sample_rates", out_sample_rates, -1,
AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
goto end;
}
/*
* Set the endpoints for the filter graph. The filter_graph will
* be linked to the graph described by filters_descr.
*/
//接下来用上边初始化的两个过滤器上下文, 初始化这两个输入输出过滤包装结构
/*
* The buffer source output must be connected to the input pad of
* the first filter described by filters_descr; since the first
* filter input label is not specified, it is set to "in" by
* default.
*/
outputs->name = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx;
outputs->pad_idx = 0;
outputs->next = NULL;
/*
* The buffer sink input must be connected to the output pad of
* the last filter described by filters_descr; since the last
* filter output label is not specified, it is set to "out" by
* default.
*/
inputs->name = av_strdup("out");
inputs->filter_ctx = buffersink_ctx;
inputs->pad_idx = 0;
inputs->next = NULL;
//filter_descr = "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono";
//把filter_graph 连接到新的的过滤器图形上, input 和output是现有过滤器图形的输入和输出
//这是解析字符串得到新的过滤器.然后进行连接,同时因为指定了input 和output 作为输入输出过滤器而连在一起
if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
&inputs, &outputs, NULL)) < 0)
goto end;
//检测并配置fliter_graph里所有的过滤器
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
goto end;
/* Print summary of the sink buffer
* Note: args buffer is reused to store channel layout string */
//打印输出filter 的信息
outlink = buffersink_ctx->inputs[0];
av_get_channel_layout_string(args, sizeof(args), -1, outlink->channel_layout);
av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n",
(int) outlink->sample_rate,
(char *) av_x_if_null(av_get_sample_fmt_name(outlink->format), "?"),
args);
end:
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs);
return ret;
}
static void print_frame(const AVFrame *frame) {
const int n = frame->nb_samples * av_get_channel_layout_nb_channels(frame->channel_layout);
const uint16_t *p = (uint16_t *) frame->data[0];
const uint16_t *p_end = p + n;
while (p < p_end) {
fputc(*p & 0xff, stdout);
fputc(*p >> 8 & 0xff, stdout);
p++;
}
fflush(stdout);
}
/***
* 也是处理音频,理解其实不难.还是读入文件.取出packet送入解码器,解成frame,送入过滤器处理
* 过滤器 也需要初始化 过滤器上下问AVFilterContext.然后通过AVFilterGraph连接起来,同时这里设定了两个输入输出过滤器数据的buf
* @param argc
* @param argv
* @return
*/
int filtering_audio_main(int argc, char **argv) {
int ret;
AVPacket packet;
AVFrame *frame = av_frame_alloc();
AVFrame *filt_frame = av_frame_alloc();
if (!frame || !filt_frame) {
perror("Could not allocate frame");
exit(1);
}
if (argc != 2) {
fprintf(stderr, "Usage: %s file | %s\n", argv[0], player);
exit(1);
}
//打开输入文件
if ((ret = open_input_file(argv[1])) < 0)
goto end;
//初始化过滤器
if ((ret = init_filters(filter_descr)) < 0)
goto end;
/* read all packets */
while (1) {
//从格式上下中读取数据到packet .然后送入解码器.获取frame
if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
break;
if (packet.stream_index == audio_stream_index) {
//数据送入解码器
ret = avcodec_send_packet(dec_ctx, &packet);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while sending a packet to the decoder\n");
break;
}
while (ret >= 0) {
//获取解码后的帧,此时frame有数据
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while receiving a frame from the decoder\n");
goto end;
}
if (ret >= 0) {
/* push the audio data from decoded frame into the filtergraph */
//把数据送入过滤器的输入buf中
if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame,
AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n");
break;
}
/* pull filtered audio from the filtergraph */
while (1) {
//取出经过过滤器处理完成的frame帧数据,放入filt_frame中
ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
if (ret < 0)
goto end;
print_frame(filt_frame);
av_frame_unref(filt_frame);
}
av_frame_unref(frame);
}
}
}
av_packet_unref(&packet);
}
end:
avfilter_graph_free(&filter_graph);
avcodec_free_context(&dec_ctx);
avformat_close_input(&fmt_ctx);
av_frame_free(&frame);
av_frame_free(&filt_frame);
if (ret < 0 && ret != AVERROR_EOF) {
fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
exit(1);
}
exit(0);
}