Skip to content

Commit 74dde7b

Browse files
committed
[dev] test and fix some bugs
1 parent 4e7caee commit 74dde7b

11 files changed

+794
-659
lines changed

src/ffmepg_transcode/ffmpeg_decode.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ FFmpegDecode::~FFmpegDecode()
3131
bool FFmpegDecode::setup() {
3232
m_codec_context = new FFmpegCodecContext("", m_decoder_name);
3333
if (m_codec_context->is_null()) {
34+
teardown();
3435
return false;
3536
}
3637

3738
if (!m_codec_context->open({ {"threads", "1"} })) {
39+
teardown();
3840
return false;
3941
}
4042

src/ffmepg_transcode/ffmpeg_demux.cpp

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ FFmpegDemux::FFmpegDemux(std::string input_url)
2121
, m_video_stream_index(-1)
2222
, m_video_stream(nullptr)
2323
, m_format_context(nullptr)
24+
, m_codec_id(-1)
25+
, m_width(-1)
26+
, m_height(-1)
27+
, m_fps(-1.0)
2428
{
2529
}
2630

@@ -33,32 +37,53 @@ FFmpegDemux::~FFmpegDemux()
3337

3438
bool FFmpegDemux::setup() {
3539
int code = 0;
36-
do {
37-
AVDictionary *options = 0;
38-
av_dict_set(&options, "rtsp_transport", "tcp", 0);
39-
40-
code = avformat_open_input(&m_format_context, m_input_url.c_str(), NULL, &options);
41-
if (code < 0) {
42-
SPDLOG_ERROR("avformat_open_input error, code: {}, msg: {}, m_input_url: {}", code, ffmpeg_error_str(code), m_input_url);
43-
return false;
44-
}
4540

46-
code = avformat_find_stream_info(m_format_context, NULL);
47-
if (code < 0) {
48-
SPDLOG_ERROR("avformat_find_stream_info error, code: {}, msg: {}, m_input_url: {}", code, ffmpeg_error_str(code), m_input_url);
49-
return false;
50-
}
41+
AVDictionary *options = 0;
42+
av_dict_set(&options, "rtsp_transport", "tcp", 0);
5143

52-
code = av_find_best_stream(m_format_context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
53-
if (code < 0) {
54-
SPDLOG_ERROR("av_find_best_stream error, code: {}, msg: {}, m_input_url: {}", code, ffmpeg_error_str(code), m_input_url);
55-
return false;
56-
}
44+
code = avformat_open_input(&m_format_context, m_input_url.c_str(), NULL, &options);
45+
if (code < 0) {
46+
SPDLOG_ERROR("avformat_open_input error, code: {}, msg: {}, m_input_url: {}", code, ffmpeg_error_str(code), m_input_url);
47+
return false;
48+
}
49+
50+
code = avformat_find_stream_info(m_format_context, NULL);
51+
if (code < 0) {
52+
SPDLOG_ERROR("avformat_find_stream_info error, code: {}, msg: {}, m_input_url: {}", code, ffmpeg_error_str(code), m_input_url);
53+
return false;
54+
}
55+
56+
code = av_find_best_stream(m_format_context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
57+
if (code < 0) {
58+
SPDLOG_ERROR("av_find_best_stream error, code: {}, msg: {}, m_input_url: {}", code, ffmpeg_error_str(code), m_input_url);
59+
return false;
60+
}
5761

58-
m_video_stream_index = code;
59-
m_video_stream = m_format_context->streams[m_video_stream_index];
62+
m_video_stream_index = code;
63+
m_video_stream = m_format_context->streams[m_video_stream_index];
6064

61-
} while (false);
65+
AVCodecParameters *codec_params = m_video_stream->codecpar;
66+
m_codec_id = (int)codec_params->codec_id;
67+
m_width = codec_params->width;
68+
m_height = codec_params->height;
69+
70+
if(m_video_stream->avg_frame_rate.num > 0 && m_video_stream->avg_frame_rate.den > 0) {
71+
// fps
72+
m_fps = av_q2d(m_video_stream->avg_frame_rate);
73+
}
74+
else if(m_video_stream->r_frame_rate.num > 0 && m_video_stream->r_frame_rate.den > 0) {
75+
// tbr
76+
m_fps = av_q2d(m_video_stream->r_frame_rate);
77+
}
78+
else if(m_video_stream->time_base.num > 0 && m_video_stream->time_base.den > 0) {
79+
// tbn
80+
m_fps = 1.0 / av_q2d(m_video_stream->time_base);
81+
}
82+
else {
83+
// default
84+
m_fps = 25.0;
85+
SPDLOG_WARN("can't find fps from frame_rate and timebase, set default to 25.0");
86+
}
6287

6388
return true;
6489
}
@@ -95,3 +120,45 @@ FFmpegPacket FFmpegDemux::read_frame() {
95120

96121
return packet;
97122
}
123+
124+
125+
std::vector<FFmpegPacket> FFmpegDemux::read_some_frames(int limit_packets)
126+
{
127+
std::vector<FFmpegPacket> vec;
128+
129+
for (int i = 0; i < limit_packets; i++) {
130+
FFmpegPacket packet = read_frame();
131+
if (packet.is_null()) {
132+
break;
133+
}
134+
135+
vec.push_back(packet);
136+
}
137+
138+
return vec;
139+
}
140+
141+
142+
int FFmpegDemux::codec_id()
143+
{
144+
return m_codec_id;
145+
}
146+
147+
148+
int FFmpegDemux::width()
149+
{
150+
return m_width;
151+
}
152+
153+
154+
int FFmpegDemux::height()
155+
{
156+
return m_height;
157+
}
158+
159+
160+
double FFmpegDemux::fps()
161+
{
162+
return m_fps;
163+
}
164+

src/ffmepg_transcode/ffmpeg_demux.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// c++
44
#include <string>
5+
#include <vector>
56

67
// project
78
#include "ffmpeg_types.hpp"
@@ -21,6 +22,12 @@ class FFmpegDemux {
2122
void teardown();
2223

2324
FFmpegPacket read_frame();
25+
std::vector<FFmpegPacket> read_some_frames(int limit_packets = INT_MAX);
26+
27+
int codec_id();
28+
int width();
29+
int height();
30+
double fps();
2431

2532

2633
private:
@@ -30,5 +37,10 @@ class FFmpegDemux {
3037
AVStream *m_video_stream;
3138

3239
AVFormatContext *m_format_context;
40+
41+
int m_codec_id;
42+
int m_width;
43+
int m_height;
44+
double m_fps;
3345
};
3446

src/ffmepg_transcode/ffmpeg_encode.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ FFmpegEncode::~FFmpegEncode()
3636
bool FFmpegEncode::setup() {
3737
m_codec_context = new FFmpegCodecContext(m_encoder_name, "");
3838
if (m_codec_context->is_null()) {
39+
teardown();
3940
return false;
4041
}
4142

@@ -64,6 +65,7 @@ bool FFmpegEncode::setup() {
6465
}
6566

6667
if (!m_codec_context->open({ {"threads", "1"} })) {
68+
teardown();
6769
return false;
6870
}
6971

src/ffmepg_transcode/ffmpeg_scale.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool FFmpegScale::setup() {
3939
m_dst_width, m_dst_height, (enum AVPixelFormat)m_dst_pixel_format,
4040
SWS_FAST_BILINEAR, NULL, NULL, NULL
4141
);
42-
if (!m_SwsContext) {
42+
if (nullptr == m_SwsContext) {
4343
SPDLOG_ERROR("sws_getContext error");
4444
return false;
4545
}
@@ -58,32 +58,27 @@ void FFmpegScale::teardown()
5858

5959

6060
FFmpegFrame FFmpegScale::scale(FFmpegFrame &frame) {
61-
do {
62-
FFmpegFrame scaled_frame;
63-
if (scaled_frame.is_null()) {
64-
break;
65-
}
66-
67-
scaled_frame.raw_ptr()->format = (enum AVPixelFormat)m_dst_pixel_format;
68-
scaled_frame.raw_ptr()->width = m_dst_width;
69-
scaled_frame.raw_ptr()->height = m_dst_height;
70-
71-
int code = av_frame_get_buffer(scaled_frame.raw_ptr(), 1);
72-
if (code < 0) {
73-
SPDLOG_ERROR("av_frame_get_buffer error, code: {}, msg: {}", code, ffmpeg_error_str(code));
74-
break;
75-
}
76-
77-
code = sws_scale(m_SwsContext, frame.raw_ptr()->data, frame.raw_ptr()->linesize, 0, m_src_height, scaled_frame.raw_ptr()->data, scaled_frame.raw_ptr()->linesize);
78-
if (code < 0) {
79-
SPDLOG_ERROR("sws_scale error, code: {}, msg: {}", code, ffmpeg_error_str(code));
80-
break;
81-
}
61+
FFmpegFrame scaled_frame;
62+
if (scaled_frame.is_null()) {
63+
return scaled_frame;
64+
}
65+
66+
scaled_frame.raw_ptr()->format = (enum AVPixelFormat)m_dst_pixel_format;
67+
scaled_frame.raw_ptr()->width = m_dst_width;
68+
scaled_frame.raw_ptr()->height = m_dst_height;
8269

70+
int code = av_frame_get_buffer(scaled_frame.raw_ptr(), 1);
71+
if (code < 0) {
72+
SPDLOG_ERROR("av_frame_get_buffer error, code: {}, msg: {}", code, ffmpeg_error_str(code));
8373
return scaled_frame;
74+
}
8475

85-
} while (false);
76+
code = sws_scale(m_SwsContext, frame.raw_ptr()->data, frame.raw_ptr()->linesize, 0, m_src_height, scaled_frame.raw_ptr()->data, scaled_frame.raw_ptr()->linesize);
77+
if (code < 0) {
78+
SPDLOG_ERROR("sws_scale error, code: {}, msg: {}", code, ffmpeg_error_str(code));
79+
return scaled_frame;
80+
}
8681

87-
return FFmpegFrame(nullptr);
82+
return scaled_frame;
8883
}
8984

0 commit comments

Comments
 (0)