Apollo 10.0
自动驾驶开放平台
apollo::drivers::video::FrameProcessor类 参考

FrameProcessor is a class to process video streams. 更多...

#include <frame_processor.h>

apollo::drivers::video::FrameProcessor 的协作图:

Public 成员函数

 FrameProcessor (const std::string &input_video_file, const std::string &output_jpg_dir)
 
bool ProcessStream () const
 
 ~FrameProcessor ()=default
 

详细描述

FrameProcessor is a class to process video streams.

在文件 frame_processor.h30 行定义.

构造及析构函数说明

◆ FrameProcessor()

apollo::drivers::video::FrameProcessor::FrameProcessor ( const std::string &  input_video_file,
const std::string &  output_jpg_dir 
)

在文件 frame_processor.cc31 行定义.

33 : output_jpg_dir_(output_jpg_dir) {
34 std::ifstream video_file(input_video_file, std::ios::binary);
35 std::istreambuf_iterator<char> buf_begin(video_file), buf_end;
36 while (buf_begin != buf_end) {
37 input_video_buffer_.emplace_back(*buf_begin++);
38 }
39}

◆ ~FrameProcessor()

apollo::drivers::video::FrameProcessor::~FrameProcessor ( )
default

成员函数说明

◆ ProcessStream()

bool apollo::drivers::video::FrameProcessor::ProcessStream ( ) const

在文件 frame_processor.cc41 行定义.

41 {
42 if (input_video_buffer_.empty()) {
43 AERROR << "error: failed to read from input video file";
44 return false;
45 }
46 AVCodecParserContext* codec_parser = av_parser_init(AV_CODEC_ID_H265);
47 if (codec_parser == nullptr) {
48 AERROR << "error: failed to init AVCodecParserContext";
49 return false;
50 }
51 AVPacket apt;
52 av_init_packet(&apt);
53 const std::unique_ptr<H265Decoder> decoder(new H265Decoder());
54 if (!decoder->Init()) {
55 AERROR << "error: failed to init decoder";
56 return false;
57 }
58 uint32_t local_size = static_cast<uint32_t>(input_video_buffer_.size());
59 uint8_t* local_data = const_cast<uint8_t*>(input_video_buffer_.data());
60 AINFO << "decoding: video size = " << local_size;
61 int frame_num = 0;
62 int warn_frame_num = 0;
63 std::vector<uint8_t> jpeg_buffer;
64 while (local_size > 0) {
65 int frame_len = av_parser_parse2(
66 codec_parser, decoder->GetCodecCtxH265(), &(apt.data), &(apt.size),
67 local_data, local_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
68 if (apt.data == nullptr) {
69 apt.data = local_data;
70 apt.size = local_size;
71 }
72 AINFO << "frame " << frame_num << ": frame_len=" << frame_len
73 << ". left_size=" << local_size;
74 const auto decoding_result =
75 decoder->Process(apt.data, apt.size, &jpeg_buffer);
76 if (decoding_result != H265Decoder::DecodingResult::WARN) {
77 // Write to output even if failed to convert, in order to
78 // maintain the order of frames
79 WriteOutputJpgFile(jpeg_buffer, GetOutputFile(frame_num));
80 ++frame_num;
81 } else {
82 // Retry later if returns warn, which indicates reading more from buffer
83 ++warn_frame_num;
84 }
85 local_data += frame_len;
86 local_size -= frame_len;
87 }
88 // Decode the left over frames from buffer exactly warning times
89 for (int i = warn_frame_num; i > 0; --i) {
90 // When retry to decode from buffer, we do not care if it succeeds or not
91 decoder->Process(nullptr, 0, &jpeg_buffer);
92 WriteOutputJpgFile(jpeg_buffer, GetOutputFile(frame_num));
93 AINFO << "frame " << frame_num << ": read from buffer";
94 ++frame_num;
95 }
96 AINFO << "total frames: " << frame_num;
97 av_parser_close(codec_parser);
98 return true;
99}
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42

该类的文档由以下文件生成: