encode.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*************************************************************************
  2. * Copyright (C) [2019] 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 MODULES_ENCODER_INCLUDE_ENCODER_HPP_
  21. #define MODULES_ENCODER_INCLUDE_ENCODER_HPP_
  22. #include <memory>
  23. #include <mutex>
  24. #include <set>
  25. #include <string>
  26. #include <unordered_map>
  27. #include "cnstream_frame.hpp"
  28. #include "cnstream_module.hpp"
  29. #include "cnstream_frame_va.hpp"
  30. #include "private/cnstream_param.hpp"
  31. #include "video/video_stream/video_stream.hpp"
  32. namespace cnstream {
  33. using CNFrameInfoPtr = std::shared_ptr<CNFrameInfo>;
  34. struct EncoderContext;
  35. struct EncodeParam {
  36. int device_id = 0; // mlu device id, -1 :disable mlu
  37. bool mlu_input_frame = false; // The input frame. true: source data , false: ImageBGR()
  38. bool mlu_encoder = true; // whether use mlu encoding, default is true
  39. int dst_width = 0; // Target width, preferred size same with input
  40. int dst_height = 0; // Target height, preferred size same with input
  41. double frame_rate = 0; // Target fps
  42. int bit_rate = 4000000; // Target bit rate, default is 1Mbps
  43. int gop_size = 10; // Target gop, default is 10
  44. int tile_cols = 0; // Grids in horizontally of video tiling, only support cpu input
  45. int tile_rows = 0; // Grids in vertically of video tiling, only support cpu input
  46. bool resample = false; // Resample frame with canvas, only support cpu input
  47. std::string file_name = ""; // File name to encode to
  48. };
  49. /**
  50. * @brief Encode is a module to encode video stream to file with/without container.
  51. */
  52. class Encode : public Module, public ModuleCreator<Encode> {
  53. public:
  54. /**
  55. * @brief Encode constructor
  56. *
  57. * @param name : module name
  58. */
  59. explicit Encode(const std::string& name);
  60. /**
  61. * @brief Encode destructor
  62. */
  63. ~Encode();
  64. /**
  65. * @brief Called by pipeline when pipeline start.
  66. *
  67. * @param paramSet : parameter set
  68. *
  69. * @return true if module open succeed, otherwise false.
  70. */
  71. bool Open(ModuleParamSet paramSet) override;
  72. /**
  73. * @brief Called by pipeline when pipeline stop
  74. */
  75. void Close() override;
  76. /**
  77. * @brief Encode each frame
  78. *
  79. * @param data : data to be processed
  80. *
  81. * @return whether process succeed
  82. * @retval 0: succeed and do no intercept data
  83. * @retval <0: failed
  84. */
  85. int Process(CNFrameInfoPtr data) override;
  86. void OnEos(const std::string &stream_id) override;
  87. private:
  88. EncoderContext * GetContext(CNFrameInfoPtr data);
  89. EncoderContext * CreateContext(CNFrameInfoPtr data, const std::string &stream_id);
  90. std::unique_ptr<ModuleParamsHelper<EncodeParam>> param_helper_ = nullptr;
  91. std::mutex ctx_lock_;
  92. std::unordered_map<std::string, EncoderContext *> contexts_;
  93. std::set<std::string> tile_streams_;
  94. }; // class Encode
  95. } // namespace cnstream
  96. #endif // MODULES_ENCODER_INCLUDE_ENCODER_HPP_