video_parser.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef EDK_SAMPLES_VIDEO_PARSER_H_
  21. #define EDK_SAMPLES_VIDEO_PARSER_H_
  22. #include <atomic>
  23. #include <chrono>
  24. #include <fstream>
  25. #include <memory>
  26. #include <string>
  27. #include <vector>
  28. #include "easycodec/easy_decode.h"
  29. #include "easycodec/vformat.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include <libavcodec/avcodec.h>
  34. #include <libavformat/avformat.h>
  35. #include <libavutil/avutil.h>
  36. #include <libavutil/imgutils.h>
  37. #ifdef __cplusplus
  38. }
  39. #endif
  40. namespace detail {
  41. struct BeginWith {
  42. explicit BeginWith(const std::string& str) noexcept : s(str) {}
  43. inline bool operator()(const std::string& prefix) noexcept {
  44. if (s.size() < prefix.size()) return false;
  45. return prefix == s.substr(0, prefix.size());
  46. }
  47. std::string s;
  48. }; // struct BeginWith
  49. class FileSaver {
  50. public:
  51. explicit FileSaver(const char* file_name) {
  52. of_.open(file_name);
  53. if (!of_.is_open()) {
  54. throw std::runtime_error("open file failed");
  55. }
  56. }
  57. ~FileSaver() { of_.close(); }
  58. void Write(char* buf, size_t len) { of_.write(buf, len); }
  59. private:
  60. std::ofstream of_;
  61. };
  62. } // namespace detail
  63. inline bool IsRtsp(const std::string& url) { return detail::BeginWith(url)("rtsp://"); }
  64. struct VideoInfo {
  65. edk::CodecType codec_type;
  66. std::vector<uint8_t> extra_data;
  67. int width, height;
  68. int progressive;
  69. };
  70. class IDemuxEventHandle {
  71. public:
  72. virtual bool OnPacket(const edk::CnPacket& frame) = 0;
  73. virtual void OnEos() = 0;
  74. virtual bool Running() = 0;
  75. };
  76. class VideoParser {
  77. public:
  78. explicit VideoParser(IDemuxEventHandle* handle) : handler_(handle) {}
  79. ~VideoParser() { Close(); }
  80. bool Open(const char* url, bool save_file = false);
  81. // -1 for error, 1 for eos
  82. int ParseLoop(uint32_t frame_interval);
  83. void Close();
  84. bool CheckTimeout();
  85. bool IsRtsp() { return is_rtsp_; }
  86. const VideoInfo& GetVideoInfo() const { return info_; }
  87. private:
  88. static constexpr uint32_t max_receive_timeout_{3000};
  89. AVFormatContext* p_format_ctx_;
  90. AVBitStreamFilterContext* p_bsfc_;
  91. AVPacket packet_;
  92. AVDictionary* options_{nullptr};
  93. VideoInfo info_;
  94. IDemuxEventHandle* handler_;
  95. std::unique_ptr<detail::FileSaver> saver_{nullptr};
  96. std::chrono::time_point<std::chrono::steady_clock> last_receive_frame_time_{};
  97. uint64_t frame_index_{0};
  98. int32_t video_index_;
  99. std::atomic<bool> have_video_source_{false};
  100. bool first_frame_{true};
  101. bool is_rtsp_{false};
  102. };
  103. #endif // EDK_SAMPLES_VIDEO_PARSER_H_