1 AVPacket内存问题
AVPacket pkt;
av_init_packet(&pkt); //一次
av_packet_unref(&pkt); //很多次 ?
2 MP4中H264 annexb模式
读mp4存h264,不用h264_mp4toannexb_filter,保存的h264不能播,因为没有起始码和SPS、PPS。
H264 annexb模式:有起始码,SPS、PPS。
3 MP4文件播不了
mp4不写尾,播不了,解决办法:moov放到文件前面。
索引在moov,数据在mdat。
4 MP4怎么seek的?
答:1 在stts即Time to Sample,根据时间,找到Sample-序列号。
2 在stsc,根据Sample,找到Chunk。
3 在stco即Chunk offset,找到Chunk偏移。
4 如果一个Chunk有多个Sample,stss的sample size列表。
记忆点:Time–Sample–Chunk–stss。
Track–>Media–>minf–>stbl–>stss/stts。
5 音频格式
AV_SAMPLE_FMT_S16, ///< signed 16 bits
AV_SAMPLE_FMT_FLT, ///< float
AV_SAMPLE_FMT_FLTP, ///< float, planar
Planar(平面的,即带P)是左右声道分开存储,左声道data[0],右声道data[1]。
Ffmpeg内部存储使用的格式。
不带P,交错排序LRLR,data[0]。音频文件用这种格式。
调用avcodec_open2(codec_ctx, codec, NULL)后,codec_ctx->frame_size被赋值。
codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
4 * 1024 * 2=8192,一次读8192字节的PCM数据。
AAC和MP3只支持AV_SAMPLE_FMT_FLTP。
codec_ctx->frame_size:AAC–1024、mp3–1152–都是FLTP。
opus支持AV_SAMPLE_FMT_S16, frame_size =960。
FLTP是存储在data[0]、data[1]。
Android采集声卡格式:AUDIO_FORMAT_PCM_16_BIT,S16。
Windows平台:WAVE_FORMAT_PCM,S16。
6 AVIO内存输入模式
AVIOContext *avio_ctx = avio_alloc_context(io_buffer,…)
format_ctx->pb = avio_ctx;
7 X264编码参数
编码速度:ultrafast >medium(默认值)。
m_ptParam.rc.i_rc_method = X264_RC_ABR; //平均码率
codec_ctx->max_b_frames = 0;
8 封装格式
flv header 9字节+PreviousTag Size 4字节+Tag。
存aac文件,每个包前需要加7字节的adts header。
9 细节
codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
设置该值,只包含I帧,SPS需要从codec_ctx->extradata读取。
avformat_write_header(oc, &opt);执行后,flv,oc->stream->time_base=1/1000。
10 函数
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b);
1—a after b,-1 a before b
avcodec_parameters_from_context(ost->st->codecpar, codec_ctx);//参1拷贝到参2