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);
}