forked from sambios/UsbCameraRtspServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFfmpegEncoder.cpp
96 lines (73 loc) · 2.48 KB
/
FfmpegEncoder.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
#include "FfmpegEncoder.h"
#include <assert.h>
FfmpegEncoder::FfmpegEncoder()
{
}
int FfmpegEncoder::CreateEncoder(int width, int height)
{
int ret = 0;
m_h264Codec = avcodec_find_encoder(AV_CODEC_ID_H264);
assert(m_h264Codec != nullptr);
m_h264Enc = avcodec_alloc_context3(m_h264Codec);
assert(m_h264Enc != nullptr);
/* put sample parameters */
m_h264Enc->bit_rate = 512 << 10;
/* resolution must be a multiple of two */
m_h264Enc->width = width;
m_h264Enc->height = height;
/* frames per second */
m_h264Enc->time_base.den = 25;
m_h264Enc->time_base.num = 1;
m_h264Enc->framerate.num = 25;
m_h264Enc->framerate.den = 1;
m_h264Enc->gop_size = 25; /* emit one intra frame every ten frames */
m_h264Enc->max_b_frames = 0;
m_h264Enc->pix_fmt = (enum AVPixelFormat) AV_PIX_FMT_YUV420P;
//m_h264Enc->pix_fmt = (enum AVPixelFormat) AV_PIX_FMT_YUV422P;
m_h264Enc->qmax = 2;
m_h264Enc->qmin = 32;
m_h264Enc->delay = 0;
AVDictionary *options = NULL;
av_dict_set(&options, "preset", "medium", 0);
av_dict_set(&options, "tune", "zerolatency", 0);
av_dict_set(&options, "profile", "baseline", 0);
ret = avcodec_open2(m_h264Enc, m_h264Codec, NULL);
assert(ret == 0);
return 0;
}
int FfmpegEncoder::Encode(AVFrame *src, AVPacket *pkt)
{
int ret;
int got_packet = 0;
if (src->format != AV_PIX_FMT_YUV420P) {
if (m_swsContex == nullptr) {
m_swsContex = sws_getContext(src->width, src->height, (enum AVPixelFormat)src->format, src->width, src->height, AV_PIX_FMT_YUV420P, 0, NULL, NULL, NULL);
}
AVFrame dst;
memset(&dst, 0, sizeof(dst));
dst.width = src->width;
dst.height = src->height;
dst.format = AV_PIX_FMT_YUV422P;
dst.pts = av_gettime_relative();
av_image_alloc(dst.data, dst.linesize, src->width, src->height, AV_PIX_FMT_YUV420P, 16);
sws_scale(m_swsContex, src->data, src->linesize, 0, src->height, dst.data, dst.linesize);
ret = avcodec_encode_video2(m_h264Enc, pkt, &dst, &got_packet);
if (ret < 0) {
printf("avcodec_encode_video2 err\n");
}
av_freep(&dst.data[0]);
}
else {
ret = avcodec_encode_video2(m_h264Enc, pkt, src, &got_packet);
if (ret < 0) {
printf("avcodec_encode_video2 err\n");
}
}
if (!got_packet) {
return -1;
}
return 0;
}
FfmpegEncoder::~FfmpegEncoder()
{
}