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
/**
* @file
* libavformat/libavcodec demuxing and muxing API example.
*
* Remux streams from one container format to another.
* @example remuxing.c
*/
avformat_open_input();//打开输入流和读取文件头 ifmtCtx
avformat_find_stream_info();//获取文件流信息
av_dump_format();//输出format信息
avformat_alloc_output_context2();//分配一个输出封装上下文ofmtCtx
av_mallocz_array();//分配一个内存数组
for (i = 0; i < ifmt_ctx->nb_streams; i++) {//创建ofmtCtx里面的流
avformat_new_stream();//分配个新的流到封装上下文ofmtCtx 并创建一个oStream
avcodec_parameters_copy();//编码器参数拷贝
oStream->codecpar->codec_tag=0;
}
av_dump_format();
if (!(ofmtCtx->oformat->flags & AVFMT_NOFILE)) {//输出格式 不应该有文件被打开
ret = avio_open(&ofmtCtx->pb, out_filename, AVIO_FLAG_WRITE);//创建初始化AVIOContext
}
avformat_write_header();//给ofmtCtx定入头信息
while(1){
av_read_frame();//从ifmtCtx读取一个Pkt

pkt.stream_index = stream_mapping[pkt.stream_index];
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
log_packet();//输出包信息
av_interleaved_write_frame();//写Pkt到ofmtCtx 内部会缓存 以保证按照dtx排序
av_packet_unref();//释放pkt
}
av_write_trailer();//将流末尾写入ofmtCtx并释放文件私有数据

avformat_close_input(&ifmtCtx);//avformat_open_input
if (ofmtCtx && !(ofmtCtx->oformat->flags & AVFMT_NOFILE))
avio_closep(&ofmtCtx->pb);
avformat_free_context(ofmtCtx);//avformat_new_stream
av_freep();
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
/**
* @file
* libavformat API example.
*
* Output a media file in any supported libavformat format. The default
* codecs are used.
* @example muxing.c
*/
avformat_alloc_output_context2(&oc, NULL, NULL, filename);//分配输出上下文
if (!oc) {
avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);//尝试打开MPEG
}
if(oc->oformat->video_codec!= AV_CODEC_ID_NONE){
add_stream(&video_st, oc, &video_codec, fmt->video_codec);
open_video(oc, video_codec, &video_st, opt);
}
if (fmt->audio_codec != AV_CODEC_ID_NONE) {
add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec);
open_audio(oc, audio_codec, &audio_st, opt);
}
av_dump_format();//输出信息
if (!(fmt->flags & AVFMT_NOFILE)) {//需要打开文件
avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
}
avformat_write_header();//写入流头信息
while (encode_video || encode_audio) {
if (encode_video && (!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base,audio_st.next_pts, audio_st.enc->time_base) <= 0)) {//比较两个时间戳
encode_video = !write_video_frame(oc, &video_st);
}
else {
encode_audio = !write_audio_frame(oc, &audio_st);
}
}
av_write_trailer();
if (!(fmt->flags & AVFMT_NOFILE))
avio_closep(&oc->pb);
avformat_free_context(oc);
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
static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag)
{
AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;

printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
tag,
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
pkt->stream_index);
}
static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
{
av_packet_rescale_ts(pkt, *time_base, st->time_base);//修改时间戳
pkt->stream_index = st->index;
log_packet(fmt_ctx, pkt);
return av_interleaved_write_frame(fmt_ctx, pkt);
}
static void close_stream(AVFormatContext *oc, OutputStream *ost)
{
avcodec_free_context(&ost->enc);
av_frame_free(&ost->frame);
av_frame_free(&ost->tmp_frame);
sws_freeContext(ost->sws_ctx);
swr_free(&ost->swr_ctx);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef struct OutputStream {
AVStream *st;
AVCodecContext *enc;

int64_t next_pts;//生成时下一帧的pts
int samples_count;

AVFrame *frame;
AVFrame *tmp_frame;

float t, tincr, tincr2;

struct SwsContext *sws_ctx;
struct SwrContext *swr_ctx;
} OutputStream;
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
static void add_stream(OutputStream *ost, AVFormatContext *oc,AVCodec **codec,enum AVCodecID codec_id)//根据AVCodecID添加一个输出流
{
AVCodecContext *c;
int i;
*codec = avcodec_find_encoder(codec_id);//根据id查找一个编码器
if (!(*codec)) {
fprintf(stderr, "Could not find encoder for '%s'\n",avcodec_get_name(codec_id));
exit(1);
}
ost->st = avformat_new_stream(oc, NULL);//为oc分配一个流
if (!ost->st) {
exit(1);
}
ost->st->id = oc->nb_streams - 1;
c = avcodec_alloc_context3(*codec);//分配和初始化c
if (!c) {
exit(1);
}
ost->enc = c;

switch ((*codec)->type) {
case AVMEDIA_TYPE_AUDIO:
c->sample_fmt = (*codec)->sample_fmts ?(*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
c->bit_rate = 64000;
c->sample_rate = 44100;//采样率
if ((*codec)->supported_samplerates) {
c->sample_rate = (*codec)->supported_samplerates[0];
for (i = 0; (*codec)->supported_samplerates[i]; i++) {
if ((*codec)->supported_samplerates[i] == 44100)
c->sample_rate = 44100;
}
}
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);//获取通道数
c->channel_layout = AV_CH_LAYOUT_STEREO;//左右声道
if ((*codec)->channel_layouts) {
c->channel_layout = (*codec)->channel_layouts[0];
for (i = 0; (*codec)->channel_layouts[i]; i++) {
if ((*codec)->channel_layouts[i] == AV_CH_LAYOUT_STEREO)
c->channel_layout = AV_CH_LAYOUT_STEREO;
}
}
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
ost->st->time_base = (AVRational) { 1, c->sample_rate };//时基
break;
case AVMEDIA_TYPE_VIDEO:
c->codec_id = codec_id;

c->bit_rate = 400000;
c->width = 352;
c->height = 288;
ost->st->time_base = (AVRational) { 1, 25 };
c->time_base = ost->st->time_base;

c->gop_size = 12;//每12帧一个帧
c->pix_fmt = AV_PIX_FMT_YUV420P;
if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
c->max_b_frames = 2;//b帧数量
}
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
c->mb_decision = 2;//FF_MB_DECISION_RD 2///失帧率
}
break;
default:
break;
}
if (oc->oformat->flags & AVFMT_GLOBALHEADER)//上下文需要全局头
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
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
static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
int ret;
AVCodecContext *c = ost->enc;
AVDictionary *opt = NULL;

av_dict_copy(&opt, opt_arg, 0);//复制
ret = avcodec_open2(c, codec, &opt);//根据codec初始化c
av_dict_free(&opt);//释放
if (ret < 0) {
exit(1);
}
ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);//分配大小
if (!ost->frame) {
exit(1);
}
ost->tmp_frame = NULL;
if (c->pix_fmt != AV_PIX_FMT_YUV420P) {//多存一个yuv420p用于转换
ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
if (!ost->tmp_frame) {
exit(1);
}
}
ret = avcodec_parameters_from_context(ost->st->codecpar, c);//复制流参数到复用器
if (ret < 0) {
exit(1);
}
}
static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
{
AVFrame *picture;
int ret;

picture = av_frame_alloc();
if (!picture)
return NULL;
picture->format = pix_fmt;
picture->width = width;
picture->height = height;
ret = av_frame_get_buffer(picture, 32);
if (ret < 0) {
exit(1);
}
return picture;
}
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
static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
AVCodecContext *c;
int nb_samples;
int ret;
AVDictionary *opt = NULL;

c = ost->enc;
av_dict_copy(&opt, opt_arg, 0);
ret = avcodec_open2(c, codec, &opt);
av_dict_free(&opt);
if (ret < 0) {
exit(1);
}
ost->t = 0;
ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;//每秒增长110HZ
ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;

if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)//编码器是否支持每次不同数量的样本
nb_samples = 10000;
else
nb_samples = c->frame_size;

ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout,
c->sample_rate, nb_samples);
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
c->sample_rate, nb_samples);

ret = avcodec_parameters_from_context(ost->st->codecpar, c);//根据codec填充par
if (ret < 0) {
exit(1);
}ost->swr_ctx = swr_alloc();
if (!ost->swr_ctx) {
exit(1);
}
av_opt_set_int(ost->swr_ctx, "in_channel_count", c->channels, 0);
av_opt_set_int(ost->swr_ctx, "in_sample_rate", c->sample_rate, 0);
av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
av_opt_set_int(ost->swr_ctx, "out_channel_count", c->channels, 0);
av_opt_set_int(ost->swr_ctx, "out_sample_rate", c->sample_rate, 0);
av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt", c->sample_fmt, 0);

if ((ret = swr_init(ost->swr_ctx)) < 0) {//根据设置初始化上下文
exit(1);
}
}
static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,uint64_t channel_layout, int sample_rate, int nb_samples)
{
AVFrame *frame = av_frame_alloc();
int ret;
if (!frame) {
exit(1);
}
frame->format = sample_fmt;
frame->channel_layout = channel_layout;
frame->sample_rate = sample_rate;
frame->nb_samples = nb_samples;
if (nb_samples) {
ret = av_frame_get_buffer(frame, 0);
if (ret < 0) {
exit(1);
}
}
return frame;
}
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
static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
{
int ret;
AVCodecContext *c;
AVFrame *frame;
int got_packet = 0;
AVPacket pkt = { 0 };

c = ost->enc;
frame = get_video_frame(ost);
av_init_packet(&pkt);//初始化pkt
ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
if (ret < 0) {
exit(1);
}
if (got_packet) {
ret = write_frame(oc, &c->time_base, ost->st, &pkt);//写帧到oc
}
else {
ret = 0;
}
if (ret < 0) {
exit(1);
}
return (frame || got_packet) ? 0 : 1;//1表示结束
}

static AVFrame *get_video_frame(OutputStream *ost)
{
AVCodecContext *c = ost->enc;

if (av_compare_ts(ost->next_pts, c->time_base,STREAM_DURATION, (AVRational) { 1, 1 }) >= 0)//需要生成更多的帧
return NULL;
if (av_frame_make_writable(ost->frame) < 0)//确保帧可写
exit(1);
if (c->pix_fmt != AV_PIX_FMT_YUV420P) {//如果不是YUV420P进行转换
if (!ost->sws_ctx) {//重采样上下文
ost->sws_ctx = sws_getContext(c->width, c->height,
AV_PIX_FMT_YUV420P,
c->width, c->height,
c->pix_fmt,
SCALE_FLAGS, NULL, NULL, NULL);
if (!ost->sws_ctx) {
exit(1);
}
}
fill_yuv_image(ost->tmp_frame, ost->next_pts, c->width, c->height);
sws_scale(ost->sws_ctx, (const uint8_t * const *)ost->tmp_frame->data,
ost->tmp_frame->linesize, 0, c->height, ost->frame->data,
ost->frame->linesize);
}
else {
fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
}
ost->frame->pts = ost->next_pts++;
return ost->frame;
}
static void fill_yuv_image(AVFrame *pict, int frame_index,
int width, int height)//生成一个frame
{
int x, y, i;
i = frame_index;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
for (y = 0; y < height / 2; y++) {
for (x = 0; x < width / 2; x++) {
pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
}
}
}
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
static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
{
AVCodecContext *c;
AVPacket pkt = { 0 };
AVFrame *frame;
int ret;
int got_packet;
int dst_nb_samples;

av_init_packet(&pkt);
c = ost->enc;
frame = get_audio_frame(ost);

if (frame) {
dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,c->sample_rate, c->sample_rate, AV_ROUND_UP);
av_assert0(dst_nb_samples == frame->nb_samples);//检查样本是否丢失

ret = av_frame_make_writable(ost->frame);//确保帧是可写的
if (ret < 0)
exit(1);

ret = swr_convert(ost->swr_ctx,
ost->frame->data, dst_nb_samples,
(const uint8_t **)frame->data, frame->nb_samples);
if (ret < 0) {
exit(1);
}
frame = ost->frame;
frame->pts = av_rescale_q(ost->samples_count, (AVRational) { 1, c->sample_rate }, c->time_base);
ost->samples_count += dst_nb_samples;
}
ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
if (ret < 0) {
exit(1);
}
if (got_packet) {
ret = write_frame(oc, &c->time_base, ost->st, &pkt);
if (ret < 0) {
exit(1);
}
}
return (frame || got_packet) ? 0 : 1;
}
static AVFrame *get_audio_frame(OutputStream *ost)
{
AVFrame *frame = ost->tmp_frame;
int j, i, v;
int16_t *q = (int16_t*)frame->data[0];

if (av_compare_ts(ost->next_pts, ost->enc->time_base,
STREAM_DURATION, (AVRational) { 1, 1 }) >= 0)
return NULL;

for (j = 0; j <frame->nb_samples; j++) {
v = (int)(sin(ost->t) * 10000);
for (i = 0; i < ost->enc->channels; i++)
*q++ = v;
ost->t += ost->tincr;
ost->tincr += ost->tincr2;
}
frame->pts = ost->next_pts;
ost->next_pts += frame->nb_samples;
return frame;
}