32 const std::string& output_jpg_dir)
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++);
42 if (input_video_buffer_.empty()) {
43 AERROR <<
"error: failed to read from input video file";
46 AVCodecParserContext* codec_parser = av_parser_init(AV_CODEC_ID_H265);
47 if (codec_parser ==
nullptr) {
48 AERROR <<
"error: failed to init AVCodecParserContext";
53 const std::unique_ptr<H265Decoder> decoder(
new H265Decoder());
54 if (!decoder->Init()) {
55 AERROR <<
"error: failed to init decoder";
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;
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;
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);
79 WriteOutputJpgFile(jpeg_buffer, GetOutputFile(frame_num));
85 local_data += frame_len;
86 local_size -= frame_len;
89 for (
int i = warn_frame_num; i > 0; --i) {
91 decoder->Process(
nullptr, 0, &jpeg_buffer);
92 WriteOutputJpgFile(jpeg_buffer, GetOutputFile(frame_num));
93 AINFO <<
"frame " << frame_num <<
": read from buffer";
96 AINFO <<
"total frames: " << frame_num;
97 av_parser_close(codec_parser);