FFMPEG1.2 音频解码的过程

FFMPEG输出WAV的audio:ffmpeg -i ~/test_22m.audio.flv -acodec pcm_s16le -ar 8000 -ac 1 -f wav -y ~/test_22m.audio.8000.ffmpeg.wav
FFMPEG输出WAV的audio:ffmpeg -i ~/test_22m.audio.flv -acodec pcm_s16le -ar 32000 -ac 1 -f wav -y ~/test_22m.audio.32000.ffmpeg.wav
FFMPEG输出WAV的audio:ffmpeg -i ~/test_22m.audio.flv -acodec pcm_s16le -ar 44100 -ac 1 -f wav -y ~/test_22m.audio.44100.ffmpeg.wav
FFMPEG几个经常定义的变量:
// 用户的参数Context
OptionsContext *o
// 自定义输入文件
InputFile* ifile
//  自定义 输入流
InputStream  *ist
// 输入AVFormatContext
AVFormatContext *ic
// 自定义输出文件
InputFile* ofile
// 输出自定义流
OutputStream *ost
// 输出AVFormatContext
AVFormatContext *oc
// 输入输出AVStream
AVStream *st
// 音频编码AVCodecContext
AVCodecContext *audio_enc
// 视频编码AVCodecContext
AVCodecContext *video_enc
// 音频解码AVCodecContext
AVCodecContext *audio_dec
// 视频解码AVCodecContext
AVCodecContext *video_dec
// 输入的AVCodecContext
AVCodecContext *icodec
// 输出的AVCodecContext
AVCodecContext *codec
// 解码器
AVCodec *dec
// 编码器
AVCodec *enc
调试以下命令,看ffmpeg对于纯音频如何处理:

调试单声道输出:ffmpeg -i ~/test_22m.audio.flv -acodec pcm_s16le -ar 8000 -ac 1 -f wav -y ~/output/ffmpeg.wav
设置输出文件的断点:
b open_input_file
b add_input_streams
b avformat_open_input
b avformat_find_stream_info
b av_find_best_stream
b avcodec_find_decoder
b avcodec_open2
b avio_open2
b transcode_init
b transcode_step

设置输出文件的断点:

b open_output_file

b new_audio_stream

b avcodec_find_encoder_by_name

b avcodec_alloc_context3

b avformat_alloc_output_context2

avcodec_get_context_defaults3
b avcodec_find_encoder

设置filter的断点:
b configure_filtergraph
b configure_input_audio_filter
b avfilter_get_by_name 
b avfilter_init_filter 
b avfilter_graph_parse
b avfilter_graph_parse2 

下面详细分析重要的处理步骤。

主流程摘要:

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
             
             
/*
The MIT License (MIT)
Copyright (c) 2013 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// 主函数
main (){
ffmpeg_parse_options () {
open_input_file () {
// 若指定了输入格式,使用指定的格式。
av_find_input_format ( o -> format )
// 分配AVFormatContext
ic = avformat_alloc_context ();
// 打开输入的AVFormatContext
avformat_open_input ( & ic , filename , file_iformat , & o -> g -> format_opts );
// 建立AVFormatStream的基本信息结构
avformat_find_stream_info ( ic , opts );
// 打开和初始化解码器
add_input_streams () {
// 打开解码器choose_decoder
// codec_id为AV_CODEC_ID_AAC
codec = ist -> dec = avcodec_find_decoder ( st -> codec -> codec_id )
// 音频的channel_layout,若codec中没有设置,则需要
// 猜测音频channel_layout,参数为ist->dec即上面的dec
// guess_input_channel_layout(codec)
// dec->channel_layout值为3(AV_CH_LAYOUT_STEREO),不用猜测。
dec -> channel_layout = av_get_default_channel_layout ()
// 保存其他设置到自定义的InputStream *ist
// ist保存在全局变量input_streams中
ist -> resample_sample_fmt = dec -> sample_fmt ; // AV_SAMPLE_FMT_FLTP
ist -> resample_sample_rate = dec -> sample_rate ; // 44100
}
// 打印输入流的信息
av_dump_format ( ic , nb_input_files , filename , 0 );
}
open_output_file () {
// 打开AVFormatContext
// 参数o->format为"wav",filename为"/home/winlin/output/ffmpeg.wav"
avformat_alloc_output_context2 ( & oc , NULL , o -> format , filename );
// idx为选中的InputStream*的索引,若有多个流,则选择声道最多的那个
new_audio_stream ( o , oc , idx ) {
// 参数:source_index=0
new_output_stream ( o , oc , AVMEDIA_TYPE_AUDIO , source_index ) {
st = avformat_new_stream ( oc , NULL );
// 设置st的编码类型
st -> codec -> codec_type = type ; // AVMEDIA_TYPE_AUDIO
// 打开编码器,调用choose_encoder
// 指定的acodec为codec_name = ”pcm_s16le“
// 调用find_codec_or_die找到指定的编码器
ost -> enc = codec = avcodec_find_encoder_by_name ( name )
// 设置st的编码器id
ost -> st -> codec -> codec_id = ost -> enc -> id ;
// 初始化AVStream的codec
// 注意,这个把AVStream的codec_id设置为AV_CODEC_ID_NONE
// 可能AVStream中这个没有关系吧,ffmpeg后面加了一句:
// st->codec->codec_type = type; // XXX hack, avcodec_get_context_defaults2() sets type to unknown for stream copy
// 实际上现在的avcodec_get_context_defaults3已经设置了这个codec_type,所以没有关系了。
// 如果有需要,把AVStream的codec_id在这里设置一下
avcodec_get_context_defaults3 ( st -> codec , ost -> enc );
// 设置OutputStream的其他参数
av_opt_get_int ( o -> g -> sws_opts , "sws_flags" , 0 , & ost -> sws_flags ); // ost->sws_flags set to 4
av_dict_copy ( & ost -> swr_opts , o -> g -> swr_opts , 0 );
av_dict_copy ( & ost -> resample_opts , o -> g -> resample_opts , 0 );
// 经常看到这个,是在这里设置的
if ( oc -> oformat -> flags & AVFMT_GLOBALHEADER ) st -> codec -> flags |= CODEC_FLAG_GLOBAL_HEADER ;
}
// 设置编码器的参数
MATCH_PER_STREAM_OPT ( audio_channels , i , audio_enc -> channels , oc , st ); // audio_enc->channels = 1
MATCH_PER_STREAM_OPT ( sample_fmts , str , sample_fmt , oc , st ); // sample_fmt=0;若sample_fmt不为0,则设置audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)
MATCH_PER_STREAM_OPT ( audio_sample_rate , i , audio_enc -> sample_rate , oc , st ); // audio_enc->sample_rate = 8000
MATCH_PER_STREAM_OPT ( filters , str , filters , oc , st ); //filters="anull"
ost -> avfilter = av_strdup ( filters );
}
// 打开输出文件
// 参数:(s=0x1af6be0, filename=0x7fffffffe84d "/home/winlin/output/ffmpeg.wav", flags=2, int_cb=0x1af7070, options=0x1afd5a8)
// 其中:options[0] is NULL,flags is 2(AVIO_FLAG_WRITE)
avio_open2 ( & oc -> pb , filename , AVIO_FLAG_WRITE , & oc -> interrupt_callback , & output_files [ nb_output_files - 1 ] -> opts )
// 将metedata从输入ic拷贝到输出oc
av_dict_copy ( & oc -> metadata , input_files [ 0 ] -> ctx -> metadata , AV_DICT_DONT_OVERWRITE );
// 将metadata从输入st拷贝到输出st,此时为NULL,忽略拷贝
av_dict_copy ( & output_streams [ i ] -> st -> metadata , ist -> st -> metadata , AV_DICT_DONT_OVERWRITE );
}
}
transcode () {
transcode_init () {
// 初始化输出流的编码参数
// 所有的AVCodecContext都是从AVStream中取的。
codec -> bits_per_raw_sample = icodec -> bits_per_raw_sample ;
codec -> chroma_sample_location = icodec -> chroma_sample_location ;
// 以下为非copy时的初始化。
// 检测编码器是否初始化
// 可见ost->enc是作为编码器的,而不是codec->codec。
if ( ! ost -> enc ) ost -> enc = avcodec_find_encoder ( codec -> codec_id );
// 若ost->filter未初始化,可能是没有自定义的filter,则:
fg = init_simple_filtergraph ( ist , ost ); // 结构初始化
configure_filtergraph ( fg ) {
// 初始化数据
fg -> graph = avfilter_graph_alloc ()
graph -> scale_sws_opts = av_strdup ( args ); args : flags = 0x4
graph -> resample_lavr_opts = av_strdup ( args ); // args: aresample_swr_opts=""
avfilter_graph_parse2 ( fg -> graph , "anull" , & inputs , & outputs )
// 配置输入的filter,对每个输入都要配置
for ( cur = inputs , i = 0 ; cur ; cur = cur -> next , i ++ ) {
configure_input_filter ( fg , fg -> inputs [ i ], cur ) {
configure_input_audio_filter () {
AVFilterContext * first_filter = inputs -> filter_ctx ; // "anull"
AVFilter * filter = avfilter_get_by_name ( "abuffer" );
// name is "graph 0 input from stream 0:0"
// args is "time_base=1/44100:sample_rate=44100:sample_fmt=fltp:channel_layout=0x3"
avfilter_graph_create_filter ( & ifilter -> filter , filter , name , args . str , NULL , fg -> graph )
// link: src="abuffer", dst="anull"
avfilter_link ( ifilter -> filter , 0 , first_filter , pad_idx )
}
}
}
// 删除inputs, 估计是这个inputs只是用来建立图的,建立完就没有用了。
avfilter_inout_free ( & inputs );
// 配置输出的filter, 对每个outputs都要配置
for ( cur = outputs , i = 0 ; cur ; cur = cur -> next , i ++ ) {
configure_output_filter ( fg , fg -> outputs [ i ], cur ) {
configure_output_audio_filter () {
AVFilterContext * last_filter = out -> filter_ctx ; // "anull"
int pad_idx = out -> pad_idx ;
// 初始化abuffersink的参数
AVABufferSinkParams * params = av_abuffersink_params_alloc ();
params -> all_channel_counts = 1 ;
// 创建filter,这个filter还没有和"anull"连接,可能还需要插入其他的。
ffabuffersink = avfilter_get_by_name ( "ffabuffersink" )
avfilter_graph_create_filter ( & ofilter -> filter , ffabuffersink , name , NULL , params , fg -> graph );
// 若输出编码AVCodecContext指定了声道,但是没有指定声道的布局,
// 就添加aformatfilter。其中,codec为ic->streams[i]->codec
if ( codec -> channels && ! codec -> channel_layout ) {
// 初始化声道的layout
codec -> channel_layout = av_get_default_channel_layout ( codec -> channels );
// 将枚举变为字符串,这个函数是通过宏定义的。
// 这个值是从ost->enc->sample_fmts里取的,因为ost->st->codec->sample_fmt为AV_SAMPLE_FMT_NONE
// 相当于:sample_fmts = av_get_sample_fmt_name(*ost->enc->sample_fmts)
// 结果是:sample_fmts = "s16"
sample_fmts = choose_sample_fmts ( ost );
// 这个值不是从ost->enc取的,因为已经有设置。
// snprintf(name, sizeof(name), "%d", ost->st->codec->sample_rate);
// 结果是:sample_rates = "8000"
sample_rates = choose_sample_rates ( ost );
// 这个值不是从ost->enc取的,因为已经有设置。
// snprintf(name, sizeof(name), "0x%"PRIx64, ost->st->codec->channel_layout);
channel_layouts = choose_channel_layouts ( ost );
// 若上面的值有设置,则需要加一个format的filter。
// args = "sample_fmts=s16:sample_rates=8000:channel_layouts=0x4:"
// name = "audio format for output stream 0:0"
avfilter_graph_create_filter ( & format , avfilter_get_by_name ( "aformat" ), name , args , NULL , fg -> graph );
// 将aformat连接到anull
// 这个是给输出的filter,所以是经过aformat然后给anull(不做改变,直接输出)
// link的顺序和input的正好相反。但是数据流是一样的:
// 数据流:dst("aformat") ===> src("anull)
avfilter_link ( last_filter , pad_idx , format , 0 );
// 更新最后的filter为"aformat"
last_filter = format ;
pad_idx = 0 ;
}
// 将abuffersink连接到最后的filter
// 数据流:abuffersink ===> aformat ===> anull
avfilter_link ( last_filter , pad_idx , ofilter -> filter , 0 )
}
}
}
// outputs也没有用了,删除
avfilter_inout_free ( & outputs );
// 结束图配置
avfilter_graph_config ( fg -> graph , NULL )
}
// 设置音频编码器
// *ost->filter->filter->filter是:"ffabuffersink"
codec -> sample_fmt = ost -> filter -> filter -> inputs [ 0 ] -> format ;
codec -> sample_rate = ost -> filter -> filter -> inputs [ 0 ] -> sample_rate ;
codec -> channel_layout = ost -> filter -> filter -> inputs [ 0 ] -> channel_layout ;
codec -> channels = avfilter_link_get_channels ( ost -> filter -> filter -> inputs [ 0 ]);
codec -> time_base = ( AVRational ){ 1 , codec -> sample_rate };
// 打开编码器
// 编码器使用的是ost->enc,而不是ost->st->codec->codec
AVCodec * codec = ost -> enc ;
// 解码器使用的是流的解码器
AVCodecContext × dec = ist -> st -> codec ;
// 设置编码线程
if ( ! av_dict_get ( ost -> opts , "threads" , NULL , 0 ))
av_dict_set ( & ost -> opts , "threads" , "auto" , 0 );
// 打开编码器
// 打开后,ost->st->codec->codec就等于ost->enc了。
avcodec_open2 ( ost -> st -> codec , codec , & ost -> opts )
// 设置frame大小
if ( ost -> enc -> type == AVMEDIA_TYPE_AUDIO && ! ( ost -> enc -> capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ))
av_buffersink_set_frame_size ( ost -> filter -> filter , ost -> st -> codec -> frame_size );
// 检查音频码率,不能小于1k
if ( ost -> st -> codec -> bit_rate && ost -> st -> codec -> bit_rate < 1000 )
av_log ( NULL , AV_LOG_WARNING , "The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s \n " );
// 额外数据的尺寸,wav为0
extra_size += ost -> st -> codec -> extradata_size ;
// 打开输入流
init_input_stream ( i , error , sizeof ( error )) {
// 获取解码器
AVCodec * codec = ist -> dec ;
// 设置线程
if ( ! av_dict_get ( ist -> opts , "threads" , NULL , 0 ))
av_dict_set ( & ist -> opts , "threads" , "auto" , 0 );
// 打开解码器
avcodec_open2 ( ist -> st -> codec , codec , & ist -> opts )
// 设置dts和pts为AV_NOPTS_VALUE(0x8000000000000000)
ist -> next_pts = AV_NOPTS_VALUE ;
ist -> next_dts = AV_NOPTS_VALUE ;
ist -> is_start = 1 ;
}
// 写入头
avformat_write_header ( oc , & output_files [ i ] -> opts )
// 输出日志
av_dump_format ( output_files [ i ] -> ctx , i , output_files [ i ] -> ctx -> filename , 1 );
}
while ( ! received_sigterm ) {
transcode_step () {
// 若设置了filter,使用filter获取输入。
transcode_from_filter () {
// 获取输入的filter,这个filter就是"abuffer":
// *ifilter ->filter ->filter "abuffer"
ifilter = graph -> inputs [ i ];
// 最佳的输入filter
* best_ist = ist ; // "abuffer"
}
// 读取packet,解码,输出给filter
process_input () {
// process_input处理活动的输入
// 读取packet,函数为:get_input_packet
av_read_frame ( f -> ctx , pkt );
// 有一段纠正pkt时间的函数,或者是计算starttime的,略过。
pkt . dts -= 1ULL << ist -> st -> pts_wrap_bits ;
pkt . pts -= 1ULL << ist -> st -> pts_wrap_bits ;
// 输出包
output_packet ( ist , & pkt ) {
// 将AVPacket拷贝一份,主要是pkt为NULL代表EOF
// pkt就是参数的包,原始包。pkt可能为NULL。
// avpkt一般和pkt等价,永远不为NULL。
AVPacket avpkt = * pkt ;
// 开始解码视频
while ( ist -> decoding_needed && ( avpkt . size > 0 || ( ! pkt && got_output ))) {
decode_audio () {
// 分配一个解码的frame,只有当没有分配(NULL)时才分配
// 此处分配为(AVFrame *)0x1af8500。
if ( ! ist -> decoded_frame ) {
ist -> decoded_frame = avcodec_alloc_frame ()
}
decoded_frame = ist -> decoded_frame ;
avcodec_decode_audio4 ( avctx , decoded_frame , got_output , pkt );
// 没有解出来,返回
if ( !* got_output ) return ret ;
// 矫正时间戳
// ist->next_pts change from 0 to 46439
ist -> next_pts += (( int64_t ) AV_TIME_BASE * decoded_frame -> nb_samples ) / avctx -> sample_rate ;
// 略过重新采样的代码
if ( resample_changed ) {
}
// 使用decoder的时间戳
if ( decoded_frame -> pkt_pts != AV_NOPTS_VALUE ) {
decoded_frame -> pts = decoded_frame -> pkt_pts ;
pkt -> pts = AV_NOPTS_VALUE ;
decoded_frame_tb = ist -> st -> time_base ;
}
// 输出到filter: "abuffer"
for ( i = 0 ; i < ist -> nb_filters ; i ++ )
av_buffersrc_add_frame ( ist -> filters [ i ] -> filter , decoded_frame , AV_BUFFERSRC_FLAG_PUSH );
decoded_frame -> pts = AV_NOPTS_VALUE ;
}
// 重置pkt的时间
avpkt . dts = avpkt . pts = AV_NOPTS_VALUE ;
// 检查已经处理的数据,video直接全部处理完,audio不一定
if ( ist -> st -> codec -> codec_type != AVMEDIA_TYPE_AUDIO )
ret = avpkt . size ;
avpkt . data += ret ;
avpkt . size -= ret ;
}
}
// 释放包
av_free_packet ( & pkt );
}
// 从filter读取包,并编码输出
reap_filters () {
// 创建frame
if ( ! ost -> filtered_frame ) {
ost -> filtered_frame = avcodec_alloc_frame ();
}
// 读取已经filter的数据
while ( 1 ) {
ret = av_buffersink_get_buffer_ref ( ost -> filter -> filter , & picref , AV_BUFFERSINK_FLAG_NO_REQUEST );
// 矫正时间戳,略过
frame_pts = AV_NOPTS_VALUE ;
if ( picref -> pts != AV_NOPTS_VALUE ) {
}
// 拷贝到frame
avfilter_copy_buf_props ( filtered_frame , picref );
filtered_frame -> pts = frame_pts ;
// 编码输出
do_audio_out () {
// 输出音频: do_audio_out(of->ctx, ost, filtered_frame);
AVPacket pkt ;
av_init_packet ( & pkt ); pkt . data = NULL ; pkt . size = 0 ;
avcodec_encode_audio2 ( enc , & pkt , frame , & got_packet )
// 设置时间戳和输出
if ( got_packet ) {
if ( pkt . pts != AV_NOPTS_VALUE )
pkt . pts = av_rescale_q ( pkt . pts , enc -> time_base , ost -> st -> time_base );
if ( pkt . dts != AV_NOPTS_VALUE )
pkt . dts = av_rescale_q ( pkt . dts , enc -> time_base , ost -> st -> time_base );
if ( pkt . duration > 0 )
pkt . duration = av_rescale_q ( pkt . duration , enc -> time_base , ost -> st -> time_base );
write_frame ( s , & pkt , ost );
av_free_packet ( & pkt );
}
}
// 删除数据
avfilter_unref_buffer ( picref );
}
}
}
}
}
}
 来自CODE的代码片
ffmpeg-transcode-logic.cpp
详细的处理流程:
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
              
              
/*
The MIT License (MIT)
Copyright (c) 2013 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
主要流程是:
main // 主函数
ffmpeg_parse_options // 会调用open_input_file和open_output_file,下面有详细解释
transcode // 下面有详细解释
打开文件 open_input_file
堆栈如下,解析 - i 参数时调用:
#0 open_input_file (o=0x7fffffffe020, filename=0x7fffffffe7fa "/home/winlin/test_22m.audio.flv") at ffmpeg_opt.c:694
#1 0x000000000040f3d8 in open_files (l=0x1adf058, inout=0xf99cc0 "input", open_file=0x407a4a <open_input_file>) at ffmpeg_opt.c:2307
#2 0x000000000040f574 in ffmpeg_parse_options (argc=15, argv=0x7fffffffe538) at ffmpeg_opt.c:2344
#3 0x000000000041fffd in main (argc=15, argv=0x7fffffffe538) at ffmpeg.c:3322
主要的处理逻辑是:
// 若指定了输入格式,使用指定的格式。
if ( o -> format ) {
file_iformat = av_find_input_format ( o -> format )
}
// 分配AVFormatContext
ic = avformat_alloc_context ();
// 根据用户参数设置值,譬如:
// -acodec libfdk_aac -i input.flv
// 会设置解码参数为:audio_codec_name=libfdk_aac
MATCH_PER_TYPE_OPT ( codec_names , str , audio_codec_name , ic , "a" );
ic -> audio_codec_id = find_codec_or_die ( audio_codec_name , AVMEDIA_TYPE_AUDIO , 0 ) -> id
// 根据用户参数设置options,譬如:
// -ar 44100 -i input.flv
if ( o -> nb_audio_sample_rate ) {
// set buf to "44100"
av_dict_set ( & o -> g -> format_opts , "sample_rate" , buf , 0 );
}
// 打开输入的AVFormatContext
avformat_open_input ( & ic , filename , file_iformat , & o -> g -> format_opts );
// 使用函数choose_decoder强制打开
// 用户指定的流的解码器
// i = range(0, ic->nb_streams)
ic -> streams [ i ] -> codec -> codec_id = codec -> id ;
// 建立AVFormatStream的基本信息结构
avformat_find_stream_info ( ic , opts );
// 若指定了起始时间(-ss),则seek
// timestamp = o->start_time;
// timestamp += ic->start_time;
if ( o -> start_time != 0 ) {
avformat_seek_file ( ic , - 1 , INT64_MIN , timestamp , timestamp , 0 );
}
// 打开和初始化解码器
add_input_streams ( o , ic ) // 下面有详细解释
// 打印输入流的信息
av_dump_format ( ic , nb_input_files , filename , 0 );
// 保存信息到自定义文件中:InputFile* file
// InputFile* file和InputStream* ist通过ist_index对应
// 一个file可以对应多个ist,即一个文件中可以有多个流。
file -> ctx = ic ;
file -> ist_index = nb_input_streams - ic -> nb_streams ; // 0
file -> ts_offset = o -> input_ts_offset - ( copy_ts ? 0 : timestamp ); // 0
file -> nb_streams = ic -> nb_streams ; // 1
file -> rate_emu = o -> rate_emu ; // 0
初始化解码器 add_input_streams
调用堆栈是:
#0 add_input_streams (o=0x7fffffffe020, ic=0x1af50c0) at ffmpeg_opt.c:611
#1 0x00000000004082f8 in open_input_file (o=0x7fffffffe020, filename=0x7fffffffe7fa "/home/winlin/test_22m.audio.flv") at ffmpeg_opt.c:808
#2 0x000000000040f3d8 in open_files (l=0x1adf058, inout=0xf99cc0 "input", open_file=0x407a4a <open_input_file>) at ffmpeg_opt.c:2307
#3 0x000000000040f574 in ffmpeg_parse_options (argc=15, argv=0x7fffffffe538) at ffmpeg_opt.c:2344
#4 0x000000000041fffd in main (argc=15, argv=0x7fffffffe538) at ffmpeg.c:3322
主要的处理逻辑是:
// 循环处理每个stream,此处只有audio
AVStream * st = ic -> streams [ i ];
// 设置AVStream为丢弃
st -> discard = AVDISCARD_ALL ;
// 打开解码器choose_decoder
// codec_id为AV_CODEC_ID_AAC
AVCodec * codec = ist -> dec = avcodec_find_decoder ( st -> codec -> codec_id )
// 音频的channel_layout,若codec中没有设置,则需要
// 猜测音频channel_layout,参数为ist->dec即上面的dec
// guess_input_channel_layout(codec)
// dec->channel_layout值为3(AV_CH_LAYOUT_STEREO),不用猜测。
if ( ! dec -> channel_layout ) {
dec -> channel_layout = av_get_default_channel_layout ()
}
// 保存其他设置到自定义的InputStream *ist
// ist保存在全局变量input_streams中
ist -> dec = codec ; // decoder AV_CODEC_ID_AAC
ist -> st = st ;
ist -> file_index = nb_input_files ; // 0
ist -> discard = 1 ;
ist -> ts_scale = 1.0 ;
ist -> opts = ...; // NULL
ist -> reinit_filters = - 1 ;
ist -> filter_in_rescale_delta_last = AV_NOPTS_VALUE ; // 0x8000000000000000
ist -> resample_sample_fmt = dec -> sample_fmt ; // AV_SAMPLE_FMT_FLTP
ist -> resample_sample_rate = dec -> sample_rate ; // 44100
ist -> resample_channels = dec -> channels ; // 2
ist -> resample_channel_layout = dec -> channel_layout ; // 3(AV_CH_LAYOUT_STEREO)
打开输出文件 open_output_file
调用堆栈是:
#1 0x000000000040c0a1 in open_output_file (o=0x7fffffffe020, filename=0x7fffffffe84d "/home/winlin/output/ffmpeg.wav") at ffmpeg_opt.c:1546
#2 0x000000000040f3d8 in open_files (l=0x1adf040, inout=0xf99dab "output", open_file=0x40b8f4 <open_output_file>) at ffmpeg_opt.c:2307
#3 0x000000000040f5ba in ffmpeg_parse_options (argc=15, argv=0x7fffffffe538) at ffmpeg_opt.c:2351
#4 0x000000000041fffd in main (argc=15, argv=0x7fffffffe538) at ffmpeg.c:3322
主要的处理逻辑是:
// 打开AVFormatContext
// 参数o->format为"wav",filename为"/home/winlin/output/ffmpeg.wav"
avformat_alloc_output_context2 ( & oc , NULL , o -> format , filename );
// 若没有指定an(o->audio_disable为0),并且有解码器(oc->oformat->audio_codec)
// 则获取输入流信息,并添加输出的AVStream
if ( ! o -> audio_disable && oc -> oformat -> audio_codec != AV_CODEC_ID_NONE ) {
// idx为选中的InputStream*的索引,若有多个流,则选择声道最多的那个
new_audio_stream ( o , oc , idx ); // 下面有详细解释
}
// 输出保存到了全局的OutputStream* output_streams中
ost -> avfilter is "anull"
ost -> st is ( AVStream * ) 0x1af7180
ost -> enc is ( AVCodec * ) 0x1485540
// 创建全局的OutputFile* output_files列表。
output_files [ nb_output_files - 1 ] -> ctx = oc ;
output_files [ nb_output_files - 1 ] -> ost_index = nb_output_streams - oc -> nb_streams ;
output_files [ nb_output_files - 1 ] -> recording_time = o -> recording_time ;
// 打开输出文件
// 参数:(s=0x1af6be0, filename=0x7fffffffe84d "/home/winlin/output/ffmpeg.wav", flags=2, int_cb=0x1af7070, options=0x1afd5a8)
// 其中:options[0] is NULL,flags is 2(AVIO_FLAG_WRITE)
avio_open2 ( & oc -> pb , filename , AVIO_FLAG_WRITE , & oc -> interrupt_callback , & output_files [ nb_output_files - 1 ] -> opts )
// 设置oc的max_delay,这个应该可以忽略
oc -> max_delay = ( int )( o -> mux_max_delay * AV_TIME_BASE ); //o->mux_max_delay is 0.699999988
// 将metedata从输入ic拷贝到输出oc
av_dict_copy ( & oc -> metadata , input_files [ 0 ] -> ctx -> metadata , AV_DICT_DONT_OVERWRITE );
// 将metadata从输入st拷贝到输出st,此时为NULL,忽略拷贝
av_dict_copy ( & output_streams [ i ] -> st -> metadata , ist -> st -> metadata , AV_DICT_DONT_OVERWRITE );
// 输出文件打开后,ffmpeg_parse_options就完毕了。
// 接下来就是transcode了。
创建输出音频流 new_audio_stream
调用堆栈是:
#0 new_audio_stream (o=0x7fffffffe020, oc=0x1af6a00, source_index=0) at ffmpeg_opt.c:1191
#1 0x000000000040c0a1 in open_output_file (o=0x7fffffffe020, filename=0x7fffffffe84d "/home/winlin/output/ffmpeg.wav") at ffmpeg_opt.c:1546
#2 0x000000000040f3d8 in open_files (l=0x1adf040, inout=0xf99dab "output", open_file=0x40b8f4 <open_output_file>) at ffmpeg_opt.c:2307
#3 0x000000000040f5ba in ffmpeg_parse_options (argc=15, argv=0x7fffffffe538) at ffmpeg_opt.c:2351
#4 0x000000000041fffd in main (argc=15, argv=0x7fffffffe538) at ffmpeg.c:3322
主要处理逻辑是:
/***********************************************************/
/* 调用new_output_stream创建AVStream */
/***********************************************************/
// ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO, source_index);
// 参数:source_index=0
// 基本实现如下:
AVStream * st = avformat_new_stream ( oc , NULL );
// 保存st到自定义的OutputStream* ost,全局变量为output_streams
output_streams [ nb_output_streams - 1 ] = ost ;
ost -> file_index = nb_output_files ;
ost -> index = idx ;
ost -> st = st ;
// 设置st的编码类型
st -> codec -> codec_type = type ; // AVMEDIA_TYPE_AUDIO
// 打开编码器,调用choose_encoder
// 指定的acodec为codec_name = ”pcm_s16le“
// 调用find_codec_or_die找到指定的编码器
// codec = find_codec_or_die (name=0x1afd600 "pcm_s16le", type=AVMEDIA_TYPE_AUDIO, encoder=1)
// id=AV_CODEC_ID_FIRST_AUDIO,等价于 codec = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE);
ost -> enc = codec = avcodec_find_encoder_by_name ( name )
// 设置st的编码器id
ost -> st -> codec -> codec_id = ost -> enc -> id ;
// 解析编码参数,譬如指定了-ab 48k,则o->g->codec_opts中有这个设置
// 其他的-ac和-ar不属于编码参数,应该属于filter,在这个地方没有
// 此处的o->g->codec_opts和ost->opts均为NULL
ost -> opts = filter_codec_opts ( o -> g -> codec_opts , ost -> enc -> id , oc , st , ost -> enc );
// 加载和解析preset文件,此处没有指定,忽略
get_preset_file_2 ( preset , ost -> enc -> name , & s ))
// 初始化AVStream的codec
// 注意,这个把AVStream的codec_id设置为AV_CODEC_ID_NONE
// 可能AVStream中这个没有关系吧,ffmpeg后面加了一句:
// st->codec->codec_type = type; // XXX hack, avcodec_get_context_defaults2() sets type to unknown for stream copy
// 实际上现在的avcodec_get_context_defaults3已经设置了这个codec_type,所以没有关系了。
// 如果有需要,把AVStream的codec_id在这里设置一下
avcodec_get_context_defaults3 ( st -> codec , ost -> enc );
// 设置OutputStream的其他参数
ost -> max_frames = INT64_MAX ;
ost -> copy_prior_start = - 1 ;
av_opt_get_int ( o -> g -> sws_opts , "sws_flags" , 0 , & ost -> sws_flags ); // ost->sws_flags set to 4
av_dict_copy ( & ost -> swr_opts , o -> g -> swr_opts , 0 );
av_dict_copy ( & ost -> resample_opts , o -> g -> resample_opts , 0 );
ost -> source_index = source_index ; // 0
// 经常看到这个,是在这里设置的
if ( oc -> oformat -> flags & AVFMT_GLOBALHEADER )
st -> codec -> flags |= CODEC_FLAG_GLOBAL_HEADER ;
// 设置对应的InputStream的属性,初始化时是丢弃这个流(AVDISCARD_ALL)
ost -> sync_ist = input_streams [ source_index ];
input_streams [ source_index ] -> discard = 0 ;
input_streams [ source_index ] -> st -> discard = AVDISCARD_NONE ;
/***********************************************************/
/* new_output_stream结束 */
/***********************************************************/
// 获取创建的参数。
// 似乎AVCodecContext就是用的AVStream中的,没有再用avcodec_alloc_context3创建一个。
// 此时auodio_enc中的codec为NULL,编码器是放在ost->enc中的。
AVStream * st = ost -> st ;
AVCodecContext * audio_enc = st -> codec ; // audio_enc->codec is NULL, ost->enc->id is AV_CODEC_ID_FIRST_AUDIO
// 设置编码器的参数
MATCH_PER_STREAM_OPT ( audio_channels , i , audio_enc -> channels , oc , st ); // audio_enc->channels = 1
MATCH_PER_STREAM_OPT ( sample_fmts , str , sample_fmt , oc , st ); // sample_fmt=0;若sample_fmt不为0,则设置audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)
MATCH_PER_STREAM_OPT ( audio_sample_rate , i , audio_enc -> sample_rate , oc , st ); // audio_enc->sample_rate = 8000
MATCH_PER_STREAM_OPT ( filters , str , filters , oc , st ); //filters="anull"
ost -> avfilter = av_strdup ( filters );
// 设置声音通道的映射,此处忽略
if ( o -> nb_audio_channel_maps ) {
ost -> audio_channels_map [ ost -> audio_channels_mapped ++ ] = map -> channel_idx ;
}
转码函数 transcode
调用堆栈如下:
#0 transcode () at ffmpeg.c:3138
#1 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑如下:
// 初始化转码
ret = transcode_init (); //下面有详细解释
// 若一直没有退出,执行转码
while ( ! received_sigterm ) {
ret = transcode_step (); //下面有详细解释
}
转码初始化 transcode_init
调用堆栈如下:
#0 transcode_init () at ffmpeg.c:2087
#1 0x000000000041fac0 in transcode () at ffmpeg.c:3138
#2 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑是:
// 初始化rate_emunate(-re)
if ( ifile -> rate_em ) {
input_streams [ j + ifile -> ist_index ] -> start = av_gettime ();
}
// 初始化输出流的编码参数
// 所有的AVCodecContext都是从AVStream中取的。
codec = ost -> st -> codec ;
icodec = ist -> st -> codec ;
ost -> st -> disposition = ist -> st -> disposition ;
codec -> bits_per_raw_sample = icodec -> bits_per_raw_sample ;
codec -> chroma_sample_location = icodec -> chroma_sample_location ;
// 当编码为copy时的初始化和需要编码的初始化不一样。
if ( ost -> stream_copy ) {
}
// 以下为非copy时的初始化。
// 检测编码器是否初始化
// 可见ost->enc是作为编码器的,而不是codec->codec。
if ( ! ost -> enc ) ost -> enc = avcodec_find_encoder ( codec -> codec_id );
ist -> decoding_needed ++ ;
ost -> encoding_needed = 1 ;
// 若ost->filter未初始化,可能是没有自定义的filter,则:
fg = init_simple_filtergraph ( ist , ost ); // 结构初始化
configure_filtergraph ( fg ) // 下面有详细解释
// 设置音频编码器
// *ost->filter->filter->filter是:"ffabuffersink"
codec -> sample_fmt = ost -> filter -> filter -> inputs [ 0 ] -> format ;
codec -> sample_rate = ost -> filter -> filter -> inputs [ 0 ] -> sample_rate ;
codec -> channel_layout = ost -> filter -> filter -> inputs [ 0 ] -> channel_layout ;
codec -> channels = avfilter_link_get_channels ( ost -> filter -> filter -> inputs [ 0 ]);
codec -> time_base = ( AVRational ){ 1 , codec -> sample_rate };
// 打开编码器
// 编码器使用的是ost->enc,而不是ost->st->codec->codec
AVCodec * codec = ost -> enc ;
// 解码器使用的是流的解码器
AVCodecContext × dec = ist -> st -> codec ;
// 设置编码线程
if ( ! av_dict_get ( ost -> opts , "threads" , NULL , 0 ))
av_dict_set ( & ost -> opts , "threads" , "auto" , 0 );
// 打开编码器
// 打开后,ost->st->codec->codec就等于ost->enc了。
avcodec_open2 ( ost -> st -> codec , codec , & ost -> opts )
// 设置frame大小
if ( ost -> enc -> type == AVMEDIA_TYPE_AUDIO && ! ( ost -> enc -> capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ))
av_buffersink_set_frame_size ( ost -> filter -> filter , ost -> st -> codec -> frame_size );
// 检查音频码率,不能小于1k
if ( ost -> st -> codec -> bit_rate && ost -> st -> codec -> bit_rate < 1000 )
av_log ( NULL , AV_LOG_WARNING , "The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s \n " );
// 额外数据的尺寸,wav为0
extra_size += ost -> st -> codec -> extradata_size ;
// 打开输入流
init_input_stream ( i , error , sizeof ( error )) // 下面有详细解释
// 写入头
avformat_write_header ( oc , & output_files [ i ] -> opts )
// 输出日志
av_dump_format ( output_files [ i ] -> ctx , i , output_files [ i ] -> ctx -> filename , 1 );
配置 filter configure_filtergraph
调用堆栈如下:
#0 configure_filtergraph (fg=0x1ae6460) at ffmpeg_filter.c:727
#1 0x000000000041bc69 in transcode_init () at ffmpeg.c:2283
#2 0x000000000041fac0 in transcode () at ffmpeg.c:3138
#3 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑如下:
// 初始化数据
simple = ! fg -> graph_desc // true
const char * graph_desc = ... // "anull"
avfilter_graph_free ( & fg -> graph );
fg -> graph = avfilter_graph_alloc ()
// filter的参数:flags=0x4
snprintf ( args , sizeof ( args ), "flags=0x%X" , ( unsigned ) ost -> sws_flags );
graph -> scale_sws_opts = av_strdup ( args );
// filter的参数:args=""
av_opt_set ( fg -> graph , "aresample_swr_opts" , args , 0 );
graph -> resample_lavr_opts = av_strdup ( args );
// 建立filter图
avfilter_graph_parse2 ( fg -> graph , graph_desc , & inputs , & outputs )
// 配置输入的filter,对每个输入都要配置
for ( cur = inputs , i = 0 ; cur ; cur = cur -> next , i ++ )
configure_input_filter ( fg , fg -> inputs [ i ], cur ) // 下面有详细解释
// 删除inputs, 估计是这个inputs只是用来建立图的,建立完就没有用了。
avfilter_inout_free ( & inputs );
// 若是simple,则直接配置输出的filter
// 对每个outputs都要配置
for ( cur = outputs , i = 0 ; cur ; cur = cur -> next , i ++ )
configure_output_filter ( fg , fg -> outputs [ i ], cur ); // 下面有详细解释
// outputs也没有用了,删除
avfilter_inout_free ( & outputs );
// 结束图配置
avfilter_graph_config ( fg -> graph , NULL )
配置输入 filter configure_input_filter
调用堆栈如下:
#0 configure_input_filter (fg=0x1ae6460, ifilter=0x1b216e0, in=0x1b20700) at ffmpeg_filter.c:714
#1 0x0000000000412783 in configure_filtergraph (fg=0x1ae6460) at ffmpeg_filter.c:775
#2 0x000000000041bc69 in transcode_init () at ffmpeg.c:2283
#3 0x000000000041fac0 in transcode () at ffmpeg.c:3138
#4 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑如下:
// 根据不同的类型,初始化不同的filter
// 此处是调用configure_input_audio_filter
// 设置第一个filter为AVFilterInOut×inputs
// 这个inputs就是avfilter_graph_parse2创建的inputs
AVFilterContext * first_filter = inputs -> filter_ctx ;
// 创建音频[in] filter
AVFilter * filter = avfilter_get_by_name ( "abuffer" );
// 初始化abuffer的参数
av_bprintf ( & args , "time_base=%d/%d:sample_rate=%d:sample_fmt=%s" ,
1 , ist -> st -> codec -> sample_rate ,
ist -> st -> codec -> sample_rate ,
av_get_sample_fmt_name ( ist -> st -> codec -> sample_fmt ));
if ( ist -> st -> codec -> channel_layout )
av_bprintf ( & args , ":channel_layout=0x%" PRIx64 ,
ist -> st -> codec -> channel_layout );
else
av_bprintf ( & args , ":channels=%d" , ist -> st -> codec -> channels );
snprintf ( name , sizeof ( name ), "graph %d input from stream %d:%d" , fg -> index ,
ist -> file_index , ist -> st -> index );
// 添加filter到图中
// name is "graph 0 input from stream 0:0"
// args is "time_base=1/44100:sample_rate=44100:sample_fmt=fltp:channel_layout=0x3"
avfilter_graph_create_filter ( & ifilter -> filter , filter , name , args . str , NULL , fg -> graph )
// 将filter链接到第一个filter
// 创建一个AVFilterLink,将这两个filter关联起来,
// src("abuffer")->outputs = dst("anull")->inputs = link
// 就是说, src("abuffer")的输出端接的是dst("anull"),dst("anull")的输入端接的是src("abuffer")
// 数据流:src("abuffer") ==> dst("anull")
// 可见,数据就是从src流向dst。
avfilter_link ( ifilter -> filter , 0 , first_filter , pad_idx )
配置输出 filter configure_output_filter
调用堆栈如下:
#0 configure_output_filter (fg=0x1ae6460, ofilter=0x1b21660, out=0x1b203a0) at ffmpeg_filter.c:491
#1 0x0000000000412841 in configure_filtergraph (fg=0x1ae6460) at ffmpeg_filter.c:783
#2 0x000000000041bc69 in transcode_init () at ffmpeg.c:2283
#3 0x000000000041fac0 in transcode () at ffmpeg.c:3138
#4 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑说明:
// 调用的是configure_output_audio_filter
// 指定最后一个filter。和input时的first_filter一样。
// 实际上,假设使用的是"anull",那么inputs和outputs的filter_ctx是一样的,
// 也就是说,avfilter_graph_parse2创建的"anull",对应的inputs->filter_ctx和outputs->filter_ctx是一个对象。
// 在avfilter_graph_parse2返回后打印:
// (gdb) p *inputs {name = 0x0, filter_ctx = 0xf048e0, pad_idx = 0, next = 0x0}
// (gdb) p *outputs {name = 0x0, filter_ctx = 0xf048e0, pad_idx = 0, next = 0x0}
AVFilterContext * last_filter = out -> filter_ctx ;
int pad_idx = out -> pad_idx ;
// 初始化abuffersink的参数
AVABufferSinkParams * params = av_abuffersink_params_alloc ();
params -> all_channel_counts = 1 ;
// 创建filter,这个filter还没有和"anull"连接,可能还需要插入其他的。
ffabuffersink = avfilter_get_by_name ( "ffabuffersink" )
avfilter_graph_create_filter ( & ofilter -> filter , ffabuffersink , name , NULL , params , fg -> graph );
// 清理参数
av_freep ( & params );
// 若输出编码AVCodecContext指定了声道,但是没有指定声道的布局,
// 就添加aformatfilter。其中,codec为ic->streams[i]->codec
if ( codec -> channels && ! codec -> channel_layout ) {
// 初始化声道的layout
codec -> channel_layout = av_get_default_channel_layout ( codec -> channels );
/**
* 对应的这几组值是:
* ost->st->codec->sample_fmt AV_SAMPLE_FMT_NONE
* ost->enc->sample_fmts[0] AV_SAMPLE_FMT_S16
* ost->st->codec->sample_rate 8000
* ost->enc->supported_samplerates NULL
* ost->st->codec->channel_layout 4
* ost->enc->channel_layouts NULL
* 优先选择st->codec中的值,当为NONE等时才从enc里面选(估计里面是编码器的默认值)。
*/
// 将枚举变为字符串,这个函数是通过宏定义的。
// 这个值是从ost->enc->sample_fmts里取的,因为ost->st->codec->sample_fmt为AV_SAMPLE_FMT_NONE
// 相当于:sample_fmts = av_get_sample_fmt_name(*ost->enc->sample_fmts)
// 结果是:sample_fmts = "s16"
sample_fmts = choose_sample_fmts ( ost );
// 这个值不是从ost->enc取的,因为已经有设置。
// snprintf(name, sizeof(name), "%d", ost->st->codec->sample_rate);
// 结果是:sample_rates = "8000"
sample_rates = choose_sample_rates ( ost );
// 这个值不是从ost->enc取的,因为已经有设置。
// snprintf(name, sizeof(name), "0x%"PRIx64, ost->st->codec->channel_layout);
channel_layouts = choose_channel_layouts ( ost );
// 若上面的值有设置,则需要加一个format的filter。
// args = "sample_fmts=s16:sample_rates=8000:channel_layouts=0x4:"
// name = "audio format for output stream 0:0"
avfilter_graph_create_filter ( & format , avfilter_get_by_name ( "aformat" ), name , args , NULL , fg -> graph );
// 连接aformat和anull
// src("anull")->outputs = dst("aformat")->inputs = link
// 就是说, src("anull")的输出端接的是dst("aformat"),dst("aformat")的输入端接的是src("anull")
// 数据流:dst("anull") ===> src("aformat")
// 可见,数据就是从src流向dst。
avfilter_link ( last_filter , pad_idx , format , 0 );
// 更新最后的filter为"aformat"
last_filter = format ;
pad_idx = 0 ;
}
// 将abuffersink连接到最后的filter
// 数据流:anull ===> aformat ==> abuffersink
avfilter_link ( last_filter , pad_idx , ofilter -> filter , 0 )
打开输入流 init_input_stream
调用堆栈如下:
#0 init_input_stream (ist_index=0, error=0x7fffffffdf50 "`\337\377\377\377\177", error_len=1024) at ffmpeg.c:1958
#1 0x000000000041cb24 in transcode_init () at ffmpeg.c:2452
#2 0x000000000041fac0 in transcode () at ffmpeg.c:3138
#3 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑如下:
// 获取解码器
AVCodec * codec = ist -> dec ;
// 设置线程
if ( ! av_dict_get ( ist -> opts , "threads" , NULL , 0 ))
av_dict_set ( & ist -> opts , "threads" , "auto" , 0 );
// 打开解码器
avcodec_open2 ( ist -> st -> codec , codec , & ist -> opts )
// 设置dts和pts为AV_NOPTS_VALUE(0x8000000000000000)
ist -> next_pts = AV_NOPTS_VALUE ;
ist -> next_dts = AV_NOPTS_VALUE ;
ist -> is_start = 1 ;
转码执行 trasncode_step
调用堆栈如下:
#0 transcode_step () at ffmpeg.c:3094
#1 0x000000000041fb63 in transcode () at ffmpeg.c:3167
#2 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑如下:
// 若设置了filter,使用filter转码。
/*******************************************/
// transcode_from_filter start
// transcode_from_filter获取输入filter
// 处理filter graph
avfilter_graph_request_oldest ( graph -> graph );
// 获取输入的filter,这个filter就是"abuffer":
// *ifilter ->filter ->filter "abuffer"
ifilter = graph -> inputs [ i ];
// 获取filter的请求,nb_requests is 1
nb_requests = av_buffersrc_get_nb_failed_requests ( ifilter -> filter );
// 最佳的输入filter
* best_ist = ist ;
// transcode_from_filter end
/*******************************************/
/*******************************************/
// process_input start
// process_input处理活动的输入
// 读取packet,函数为:get_input_packet
av_read_frame ( f -> ctx , pkt );
// 有一段纠正pkt时间的函数,或者是计算starttime的,略过。
pkt . dts -= 1ULL << ist -> st -> pts_wrap_bits ;
pkt . pts -= 1ULL << ist -> st -> pts_wrap_bits ;
// 计算pkt的时间戳,
// ifile->ts_offset is 0
// ist->st->time_base is {num = 1, den = 1000}
if ( pkt . dts != AV_NOPTS_VALUE )
pkt . dts += av_rescale_q ( ifile -> ts_offset , AV_TIME_BASE_Q , ist -> st -> time_base );
if ( pkt . pts != AV_NOPTS_VALUE )
pkt . pts += av_rescale_q ( ifile -> ts_offset , AV_TIME_BASE_Q , ist -> st -> time_base );
// 继续,计算pkt的时间戳
if ( pkt . pts != AV_NOPTS_VALUE )
pkt . pts *= ist -> ts_scale ;
if ( pkt . dts != AV_NOPTS_VALUE )
pkt . dts *= ist -> ts_scale ;
// 这段计算时间戳,略过
if ( pkt . dts != AV_NOPTS_VALUE && ist -> next_dts != AV_NOPTS_VALUE && ! copy_ts ) {
}
// 输出包
output_packet ( ist , & pkt ) // 下面有详细解释
// 释放包
av_free_packet ( & pkt );
// process_input end
/*******************************************/
// 从filter读取包,并编码输出
reap_filters (); //下面有详细解释
输出包 output_packet
调用堆栈如下:
#0 output_packet (ist=0x1b227c0, pkt=0x7fffffffe000) at ffmpeg.c:1805
#1 0x000000000041f6a5 in process_input (file_index=0) at ffmpeg.c:3019
#2 0x000000000041fa4c in transcode_step () at ffmpeg.c:3115
#3 0x000000000041fb63 in transcode () at ffmpeg.c:3167
#4 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑如下:
// 第一次初始化ist的ts
// ist->dts is 0
if ( ! ist -> saw_first_ts ) {
ist -> dts = ist -> st -> avg_frame_rate . num ? - ist -> st -> codec -> has_b_frames * AV_TIME_BASE / av_q2d ( ist -> st -> avg_frame_rate ) : 0 ;
ist -> pts = 0 ;
// 不解码时的逻辑,copy时矫正时间戳用
if ( pkt != NULL && pkt -> pts != AV_NOPTS_VALUE && ! ist -> decoding_needed ) {
ist -> dts += av_rescale_q ( pkt -> pts , ist -> st -> time_base , AV_TIME_BASE_Q );
ist -> pts = ist -> dts ; //unused but better to set it to a value thats not totally wrong
}
ist -> saw_first_ts = 1 ;
}
// 初始化next_ts
if ( ist -> next_dts == AV_NOPTS_VALUE )
ist -> next_dts = ist -> dts ;
if ( ist -> next_pts == AV_NOPTS_VALUE )
ist -> next_pts = ist -> pts ;
// 将AVPacket拷贝一份,主要是pkt为NULL代表EOF
// pkt就是参数的包,原始包。pkt可能为NULL。
// avpkt一般和pkt等价,永远不为NULL。
AVPacket avpkt = * pkt ;
// 若包dts有效,用它来矫正ist的时间戳
if ( pkt -> dts != AV_NOPTS_VALUE ) {
ist -> next_dts = ist -> dts = av_rescale_q ( pkt -> dts , ist -> st -> time_base , AV_TIME_BASE_Q );
if ( ist -> st -> codec -> codec_type != AVMEDIA_TYPE_VIDEO || ! ist -> decoding_needed )
ist -> next_pts = ist -> pts = ist -> dts ;
}
// 开始解码视频
while ( ist -> decoding_needed && ( avpkt . size > 0 || ( ! pkt && got_output ))) {
/******************************************************************
* decode_audio start
******************************************************************/
// 调用的函数是:decode_audio
// 参数:pkt is &avpkt
// 分配一个解码的frame,只有当没有分配(NULL)时才分配
// 此处分配为(AVFrame *)0x1af8500。
if ( ! ist -> decoded_frame ) {
ist -> decoded_frame = avcodec_alloc_frame ()
}
decoded_frame = ist -> decoded_frame ;
avcodec_decode_audio4 ( avctx , decoded_frame , got_output , pkt );
// 若失败,
// 或者没有解出来,而且pkt为NULL(EOF),
// 给filter加一个NULL帧。
if ( !* got_output || ret < 0 ) {
if ( ! pkt -> size ) {
for ( i = 0 ; i < ist -> nb_filters ; i ++ )
av_buffersrc_add_ref ( ist -> filters [ i ] -> filter , NULL , 0 );
}
}
// 矫正时间戳
// ist->next_pts change from 0 to 46439
ist -> next_pts += (( int64_t ) AV_TIME_BASE * decoded_frame -> nb_samples ) / avctx -> sample_rate ;
// ist->next_dts change from 0 to 46439
ist -> next_dts += (( int64_t ) AV_TIME_BASE * decoded_frame -> nb_samples ) / avctx -> sample_rate ;
// 检测采样是否改变
resample_changed = ....;
// 略过重新采样的代码
if ( resample_changed ) {
// 加filter重新采样。
}
// 使用decoder的时间戳
if ( decoded_frame -> pts != AV_NOPTS_VALUE ) {
ist -> dts = ist -> next_dts = ist -> pts = ist -> next_pts = av_rescale_q ( decoded_frame -> pts , avctx -> time_base , AV_TIME_BASE_Q );
decoded_frame_tb = avctx -> time_base ;
} else if ( decoded_frame -> pkt_pts != AV_NOPTS_VALUE ) {
decoded_frame -> pts = decoded_frame -> pkt_pts ;
pkt -> pts = AV_NOPTS_VALUE ;
decoded_frame_tb = ist -> st -> time_base ;
} else if ( pkt -> pts != AV_NOPTS_VALUE ) {
decoded_frame -> pts = pkt -> pts ;
pkt -> pts = AV_NOPTS_VALUE ;
decoded_frame_tb = ist -> st -> time_base ;
} else {
decoded_frame -> pts = ist -> dts ;
decoded_frame_tb = AV_TIME_BASE_Q ;
}
if ( decoded_frame -> pts != AV_NOPTS_VALUE )
decoded_frame -> pts = av_rescale_delta ( decoded_frame_tb , decoded_frame -> pts ,
( AVRational ){ 1 , ist -> st -> codec -> sample_rate }, decoded_frame -> nb_samples , & ist -> filter_in_rescale_delta_last ,
( AVRational ){ 1 , ist -> st -> codec -> sample_rate });
// 输出到filter: "abuffer"
for ( i = 0 ; i < ist -> nb_filters ; i ++ )
av_buffersrc_add_frame ( ist -> filters [ i ] -> filter , decoded_frame , AV_BUFFERSRC_FLAG_PUSH );
decoded_frame -> pts = AV_NOPTS_VALUE ;
/******************************************************************
* decode_audio end
******************************************************************/
// 重置pkt的时间
avpkt . dts = avpkt . pts = AV_NOPTS_VALUE ;
// 检查已经处理的数据,video直接全部处理完,audio不一定
if ( pkt ) {
if ( ist -> st -> codec -> codec_type != AVMEDIA_TYPE_AUDIO )
ret = avpkt . size ;
avpkt . data += ret ;
avpkt . size -= ret ;
}
}
filter 结果,编码和发送 reap_filters
调用堆栈如下:
#0 reap_filters () at ffmpeg.c:1071
#1 0x000000000041fab2 in transcode_step () at ffmpeg.c:3124
#2 0x000000000041fb63 in transcode () at ffmpeg.c:3167
#3 0x000000000042009a in main (argc=13, argv=0x7fffffffe558) at ffmpeg.c:3344
主要逻辑如下:
// 创建frame
if ( ! ost -> filtered_frame ) {
ost -> filtered_frame = avcodec_alloc_frame ();
}
// 读取已经filter的数据
while ( 1 ) {
ret = av_buffersink_get_buffer_ref ( ost -> filter -> filter , & picref , AV_BUFFERSINK_FLAG_NO_REQUEST );
// 矫正时间戳
frame_pts = AV_NOPTS_VALUE ;
if ( picref -> pts != AV_NOPTS_VALUE ) {
filtered_frame -> pts = frame_pts = av_rescale_q ( picref -> pts ,
ost -> filter -> filter -> inputs [ 0 ] -> time_base ,
ost -> st -> codec -> time_base ) -
av_rescale_q ( of -> start_time ,
AV_TIME_BASE_Q ,
ost -> st -> codec -> time_base );
if ( of -> start_time && filtered_frame -> pts < 0 ) {
avfilter_unref_buffer ( picref );
continue ;
}
}
// 拷贝到frame
avfilter_copy_buf_props ( filtered_frame , picref );
filtered_frame -> pts = frame_pts ;
/******************************************************
* do_audio_out start
*******************************************************/
// 输出音频: do_audio_out(of->ctx, ost, filtered_frame);
AVPacket pkt ;
av_init_packet ( & pkt );
pkt . data = NULL ;
pkt . size = 0 ;
avcodec_encode_audio2 ( enc , & pkt , frame , & got_packet )
// 设置时间戳和输出
if ( got_packet ) {
if ( pkt . pts != AV_NOPTS_VALUE )
pkt . pts = av_rescale_q ( pkt . pts , enc -> time_base , ost -> st -> time_base );
if ( pkt . dts != AV_NOPTS_VALUE )
pkt . dts = av_rescale_q ( pkt . dts , enc -> time_base , ost -> st -> time_base );
if ( pkt . duration > 0 )
pkt . duration = av_rescale_q ( pkt . duration , enc -> time_base , ost -> st -> time_base );
write_frame ( s , & pkt , ost );
av_free_packet ( & pkt );
}
/******************************************************
* do_audio_out end
*******************************************************/
// 删除数据
avfilter_unref_buffer ( picref );
}
 来自CODE的代码片
ffmpeg-transcode-audio-detail.cpp
代码

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
             
             
/*
The MIT License (MIT)
Copyright (c) 2013 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
tool.cpp to implements the following command:
ffmpeg -i ~/test_22m.audio.flv -acodec pcm_s16le -ar 8000 -ac 1 -f wav -y ~/output/ffmpeg.wav
*/
// for int64_t print using PRId64 format.
#ifndef __STDC_FORMAT_MACROS
# define __STDC_FORMAT_MACROS
#endif
// for cpp to use c-style macro UINT64_C in libavformat
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif
#include <stdio.h>
#include <assert.h>
#include <inttypes.h>
extern "C" {
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/avcodec.h>
#include <libavfilter/avfiltergraph.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/opt.h>
#include <libavutil/samplefmt.h>
}
/**
* open input and output files
* AVFormatContext* ic, AVStream* ist, AVCodecContext* ist->codec, AVCodec* dec
* AVFormatContext* oc, AVStream* ost, AVCodecContext* ost->codec, AVCodec* enc
* @remark ist->codec->codec is NULL.
* @remark ost->codec->codec is NULL.
*/
int open_input_output_files (
/*input*/
const char * input , const char * output , const char * format_name ,
int sample_rate , int channels ,
/*output*/
AVFormatContext *& ic , int & stream_index , AVStream *& ist ,
AVCodec *& dec , AVFormatContext *& oc , AVStream *& ost , AVCodec *& enc )
{
int ret = 0 ;
// open ic
ret = avformat_open_input ( & ic , input , NULL , NULL );
assert ( ret >= 0 );
ret = avformat_find_stream_info ( ic , NULL );
assert ( ret >= 0 );
// find decoder
stream_index = av_find_best_stream ( ic , AVMEDIA_TYPE_AUDIO , - 1 , - 1 , NULL , 0 );
assert ( stream_index >= 0 );
ist = ic -> streams [ stream_index ];
dec = avcodec_find_decoder ( ist -> codec -> codec_id );
assert ( dec );
av_dump_format ( ic , 0 , input , 0 );
// open oc
ret = avformat_alloc_output_context2 ( & oc , NULL , format_name , output );
assert ( ret >= 0 );
ost = avformat_new_stream ( oc , NULL );
assert ( ost );
enc = avcodec_find_encoder ( AV_CODEC_ID_PCM_S16LE );
assert ( enc );
if ( true ) {
// copy codec info to stream.
ost -> codec -> codec_id = enc -> id ;
avcodec_get_context_defaults3 ( ost -> codec , enc );
ost -> discard = AVDISCARD_NONE ;
// Some formats want stream headers to be separate.
if ( oc -> oformat -> flags & AVFMT_GLOBALHEADER ) {
ost -> codec -> flags |= CODEC_FLAG_GLOBAL_HEADER ;
}
// set encode params
ost -> codec -> channels = channels ;
ost -> codec -> sample_rate = sample_rate ;
}
ret = avio_open2 ( & oc -> pb , output , AVIO_FLAG_WRITE , & oc -> interrupt_callback , NULL );
assert ( ret >= 0 );
av_dict_copy ( & oc -> metadata , ic -> metadata , AV_DICT_DONT_OVERWRITE );
av_dict_set ( & oc -> metadata , "creation_time" , NULL , 0 );
av_dict_copy ( & ost -> metadata , ist -> metadata , AV_DICT_DONT_OVERWRITE );
return ret ;
}
/**
* setup the filter graph, init the ifilter(buffersrc_ctx) and ofilter(buffersink_ctx).
* AVFilterContext* buffersrc_ctx, to where put decoded frame
* AVFilterContext* buffersink_ctx, from where get filtered frame
*/
int configure_filtergraph (
/*input*/
AVStream * ist , AVStream * ost , AVCodec * enc ,
/*output*/
AVFilterContext *& buffersrc_ctx , AVFilterContext *& buffersink_ctx )
{
int ret = 0 ;
AVFilterGraph * graph = avfilter_graph_alloc ();
assert ( graph );
// inputs/outputs build by avfilter_graph_parse2
AVFilterInOut * inputs = NULL ;
AVFilterInOut * outputs = NULL ;
// init filter graph
if ( true ) {
// init simple filters
const char * anull_filters_desc = "anull" ;
// ost->sws_flags
graph -> scale_sws_opts = av_strdup ( "flags=0x4" );
av_opt_set ( graph , "aresample_swr_opts" , "" , 0 );
graph -> resample_lavr_opts = av_strdup ( "" );
// build filter graph
ret = avfilter_graph_parse2 ( graph , anull_filters_desc , & inputs , & outputs );
assert ( ret >= 0 );
// simple filter must have only one input and output.
assert ( inputs && ! inputs -> next );
assert ( outputs && ! outputs -> next );
}
// config input filter
if ( true ) {
// first_filter is "anull"
AVFilterContext * first_filter = inputs -> filter_ctx ;
int pad_idx = inputs -> pad_idx ;
// get abuffer audio filter
AVFilter * abuffersrc = avfilter_get_by_name ( "abuffer" );
// init abuffer audio filter
char args [ 512 ]; memset ( args , 0 , sizeof ( args ));
// time_base=1/44100:sample_rate=44100:sample_fmt=fltp:channel_layout=0x3
snprintf ( args , sizeof ( args ),
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%" PRIx64 ,
1 , ist -> codec -> sample_rate , ist -> codec -> sample_rate ,
av_get_sample_fmt_name ( ist -> codec -> sample_fmt ), ist -> codec -> channel_layout );
ret = avfilter_graph_create_filter ( & buffersrc_ctx , abuffersrc , "abuffer-filter" , args , NULL , graph );
assert ( ret >= 0 );
// link src "abuffer" to dst "anull"
// the data flow: abuffer ===> anull
ret = avfilter_link ( buffersrc_ctx , 0 , first_filter , pad_idx );
assert ( ret >= 0 );
avfilter_inout_free ( & inputs );
}
// config output filter
if ( true ) {
// last_filter is "anull"
AVFilterContext * last_filter = outputs -> filter_ctx ;
int pad_idx = outputs -> pad_idx ;
// init ffabuffersink audio filter
// link it later.
AVABufferSinkParams * params = av_abuffersink_params_alloc ();
params -> all_channel_counts = 1 ;
AVFilter * abuffersink = avfilter_get_by_name ( "ffabuffersink" );
ret = avfilter_graph_create_filter ( & buffersink_ctx , abuffersink , "abuffersink-filter" , NULL , params , graph );
assert ( ret >= 0 );
av_free ( params );
// init the encoder context channel_layout.
if ( ost -> codec -> channels && ! ost -> codec -> channel_layout ) {
ost -> codec -> channel_layout = av_get_default_channel_layout ( ost -> codec -> channels );
const char * sample_fmts = av_get_sample_fmt_name ( * enc -> sample_fmts );
char args [ 512 ]; memset ( args , 0 , sizeof ( args ));
snprintf ( args , sizeof ( args ),
"sample_fmts=%s:sample_rates=%d:channel_layouts=0x%" PRIx64 ":" ,
sample_fmts , ost -> codec -> sample_rate , ost -> codec -> channel_layout );
AVFilterContext * aformat_ctx = NULL ;
AVFilter * aformat = avfilter_get_by_name ( "aformat" );
ret = avfilter_graph_create_filter ( & aformat_ctx , aformat , "aformat-filter" , args , NULL , graph );
assert ( ret >= 0 );
// the data flow: anull ===> aformat
ret = avfilter_link ( last_filter , pad_idx , aformat_ctx , 0 );
assert ( ret >= 0 );
// now, "aformat" is the last filter
last_filter = aformat_ctx ;
pad_idx = 0 ;
}
// link the abuffersink to the last filer
// the data flow: aformat ===> abuffersink
// full data flow: anull ===> aformat ===> abuffersink
ret = avfilter_link ( last_filter , pad_idx , buffersink_ctx , 0 );
assert ( ret >= 0 );
avfilter_inout_free ( & outputs );
}
ret = avfilter_graph_config ( graph , NULL );
assert ( ret >= 0 );
return ret ;
}
/**
* setup ost->codec, open enc and dec
* @remark ist->codec->codec equals to dec
* @remark ost->codec->codec equals to enc
*/
int setup_and_open_codec (
AVFilterContext * ofilter , AVStream * ost , AVCodec * enc ,
AVFormatContext * oc , AVStream * ist , AVCodec * dec )
{
int ret = 0 ;
// set encoder
if ( true ) {
ost -> codec -> sample_fmt = ( AVSampleFormat ) ofilter -> inputs [ 0 ] -> format ;
ost -> codec -> sample_rate = ofilter -> inputs [ 0 ] -> sample_rate ;
ost -> codec -> channel_layout = ofilter -> inputs [ 0 ] -> channel_layout ;
ost -> codec -> channels = avfilter_link_get_channels ( ofilter -> inputs [ 0 ]);
ost -> codec -> time_base = ( AVRational ){ 1 , ost -> codec -> sample_rate };
AVDictionary * opts = NULL ;
if ( ! av_dict_get ( opts , "threads" , NULL , 0 )) {
av_dict_set ( & opts , "threads" , "auto" , 0 );
}
// open encoder, set ost->codec->codec to enc
if ( avcodec_open2 ( ost -> codec , enc , & opts ) < 0 ) {
exit ( - 1 );
}
av_dict_free ( & opts );
// set frame size
if ( enc -> type == AVMEDIA_TYPE_AUDIO && ! ( enc -> capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE )) {
av_buffersink_set_frame_size ( ofilter , ost -> codec -> frame_size );
}
}
// open decoder
if ( true ) {
AVDictionary * opts = NULL ;
if ( ! av_dict_get ( opts , "threads" , NULL , 0 )) {
av_dict_set ( & opts , "threads" , "auto" , 0 );
}
// ffmpeg donot open the dec when find it.
if ( avcodec_open2 ( ist -> codec , dec , & opts ) < 0 ) {
exit ( - 1 );
}
av_dict_free ( & opts );
}
// write encoder header
if ( avformat_write_header ( oc , NULL ) != 0 ) {
exit ( - 1 );
}
return ret ;
}
/**
* output packet to filter
*/
int output_packet ( AVFilterContext * ifilter , AVStream * ist , AVPacket * pkt ,
AVFrame *& decoded_frame , int64_t & filter_in_rescale_delta_last )
{
int ret = 0 ;
// alloc frame if NULL
if ( ! decoded_frame ) {
decoded_frame = avcodec_alloc_frame ();
}
int got_frame = 0 ;
// decode pkt to frame
// maybe not got_frame, but the ret>0, we need to decode again? ffmpeg did this.
// see ffmpeg.c:1895, 1898
ret = avcodec_decode_audio4 ( ist -> codec , decoded_frame , & got_frame , pkt );
assert ( ret >= 0 );
// not ready yet.
if ( ! got_frame ) {
return ret ;
}
// set decoded frame ts
AVRational decoded_frame_tb ;
if ( decoded_frame -> pkt_pts != AV_NOPTS_VALUE ) {
decoded_frame -> pts = decoded_frame -> pkt_pts ;
pkt -> pts = AV_NOPTS_VALUE ;
decoded_frame_tb = ist -> time_base ;
}
if ( decoded_frame -> pts != AV_NOPTS_VALUE ) {
decoded_frame -> pts = av_rescale_delta ( decoded_frame_tb , decoded_frame -> pts , ( AVRational ){ 1 , ist -> codec -> sample_rate }, decoded_frame -> nb_samples , & filter_in_rescale_delta_last , ( AVRational ){ 1 , ist -> codec -> sample_rate });
}
// output to filter: "abuffer"
ret = av_buffersrc_add_frame ( ifilter , decoded_frame , AV_BUFFERSRC_FLAG_PUSH );
assert ( ret >= 0 );
// reset the pts
decoded_frame -> pts = AV_NOPTS_VALUE ;
pkt -> dts = pkt -> pts = AV_NOPTS_VALUE ;
return ret ;
}
/**
* encode and output
*/
int do_audio_out ( AVFormatContext * oc , AVStream * ost , AVFrame * filtered_frame )
{
int ret = 0 ;
AVPacket pkt ;
av_init_packet ( & pkt );
pkt . data = NULL ;
pkt . size = 0 ;
int got_packet = 0 ;
ret = avcodec_encode_audio2 ( ost -> codec , & pkt , filtered_frame , & got_packet );
if ( ! got_packet ) {
return ret ;
}
// correct the pkt
printf ( "encoded packet pts=%" PRId64 ", dts=%" PRId64 " \n " , pkt . pts , pkt . dts );
if ( pkt . pts != AV_NOPTS_VALUE ) {
pkt . pts = av_rescale_q ( pkt . pts , ost -> codec -> time_base , ost -> time_base );
}
if ( pkt . dts != AV_NOPTS_VALUE ) {
pkt . dts = av_rescale_q ( pkt . dts , ost -> codec -> time_base , ost -> time_base );
}
if ( pkt . duration > 0 ) {
pkt . duration = av_rescale_q ( pkt . duration , ost -> codec -> time_base , ost -> time_base );
}
printf ( "corrected packet to output pts=%" PRId64 ", dts=%" PRId64 " \n " , pkt . pts , pkt . dts );
ret = av_interleaved_write_frame ( oc , & pkt );
assert ( ret >= 0 );
av_free_packet ( & pkt );
return ret ;
}
/**
* read from filter, encode and output
*/
int reap_filters ( AVFormatContext * oc , AVStream * ost , AVFilterContext * ofilter , AVFrame *& filtered_frame )
{
int ret = 0 ;
if ( ! filtered_frame ) {
filtered_frame = avcodec_alloc_frame ();
}
avcodec_get_frame_defaults ( filtered_frame );
// pull filtered audio from the filtergraph
int64_t start_time = 0 ;
while ( true ) {
// get filtered frame.
AVFilterBufferRef * picref = NULL ;
ret = av_buffersink_get_buffer_ref ( ofilter , & picref , AV_BUFFERSINK_FLAG_NO_REQUEST );
if ( ret == AVERROR ( EAGAIN ) || ret == AVERROR_EOF ) {
return 0 ; // no frame filtered.
}
assert ( ret >= 0 );
// correct the pts
int64_t filtered_frame_pts = AV_NOPTS_VALUE ;
if ( picref -> pts != AV_NOPTS_VALUE ) {
filtered_frame_pts = av_rescale_q ( picref -> pts , ofilter -> inputs [ 0 ] -> time_base , ost -> codec -> time_base ) - av_rescale_q ( start_time , AV_TIME_BASE_Q , ost -> codec -> time_base );
}
// convert to frame
avfilter_copy_buf_props ( filtered_frame , picref );
filtered_frame -> pts = filtered_frame_pts ;
// do_audio_out
ret = do_audio_out ( oc , ost , filtered_frame );
assert ( ret >= 0 );
avfilter_unref_bufferp ( & picref );
}
}
int main ( int /*argc*/ , char ** /*argv*/ )
{
int ret = 0 ;
const char * input = "/home/winlin/test_22m.audio.flv" ;
const char * output = "/home/winlin/output/winlin.wav" ;
const char * format_name = "wav" ;
// transcoding params
int sample_rate = 22100 ;
int channels = 1 ;
// register all.
avcodec_register_all ();
av_register_all ();
avfilter_register_all ();
/* ffmpeg_parse_options */
// open input and output files
AVFormatContext * ic = NULL ;
int stream_index = 0 ;
AVStream * ist = NULL ;
AVCodec * dec = NULL ;
AVFormatContext * oc = NULL ;
AVStream * ost = NULL ;
AVCodec * enc = NULL ;
ret = open_input_output_files ( /*input*/ input , output , format_name , sample_rate , channels ,
/*output*/ ic , stream_index , ist , dec , oc , ost , enc );
assert ( ret >= 0 );
/* transcode_init */
// configure_filtergraph: setup the filter graph, init the ifilter(buffersrc_ctx) and ofilter(buffersink_ctx).
AVFilterContext * buffersrc_ctx = NULL ;
AVFilterContext * buffersink_ctx = NULL ;
ret = configure_filtergraph ( /*input*/ ist , ost , enc , /*output*/ buffersrc_ctx , buffersink_ctx );
assert ( ret >= 0 );
// setup encoder, open the encoder then decoder
AVFilterContext * ofilter = buffersink_ctx ; // the output filter is the buffersink
ret = setup_and_open_codec ( ofilter , ost , enc , oc , ist , dec );
assert ( ret >= 0 );
av_dump_format ( oc , 0 , output , 1 );
/* transcode_step */
// the decoded_frame and filtered_frame is shared.
AVFrame * decoded_frame = NULL ;
AVFrame * filtered_frame = NULL ;
int64_t filter_in_rescale_delta_last = AV_NOPTS_VALUE ;
while ( true ) {
AVPacket pkt ;
// get_input_packet
if ( av_read_frame ( ic , & pkt ) < 0 ) {
exit ( - 1 );
}
if ( pkt . stream_index != stream_index ) {
av_free_packet ( & pkt );
continue ;
}
printf ( "decoded packet pts=%" PRId64 ", dts=%" PRId64 " \n " , pkt . pts , pkt . dts );
// output_packet: output packet to filter
AVFilterContext * ifilter = buffersrc_ctx ;
ret = output_packet ( ifilter , ist , & pkt , decoded_frame , filter_in_rescale_delta_last );
assert ( ret >= 0 );
av_free_packet ( & pkt );
// reap_filters: read from filter, encode and output
ret = reap_filters ( oc , ost , ofilter , filtered_frame );
assert ( ret >= 0 );
}
// cleanup.
if ( ist -> codec ) {
avcodec_close ( ist -> codec );
}
if ( oc ) {
avformat_free_context ( oc );
}
avformat_close_input ( & ic );
return 0 ;
}
 来自CODE的代码片
ffmpeg-transcode-audio.cpp

猜你喜欢

转载自blog.csdn.net/xiaojun111111/article/details/52815407