test_decoder.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include <gtest/gtest.h>
  21. #include <chrono>
  22. #include <memory>
  23. #include <string>
  24. #include <thread>
  25. #include "cnrt.h"
  26. #include "cnstream_source.hpp"
  27. #include "data_handler_file.hpp"
  28. #include "data_handler_mem.hpp"
  29. #include "data_source.hpp"
  30. #include "util/video_decoder.hpp"
  31. #include "test_base.hpp"
  32. namespace cnstream {
  33. static constexpr const char *gname = "source";
  34. static constexpr const char *gmp4_path = "../../modules/unitest/source/data/img.mp4";
  35. static constexpr const char *gh264_path = "../../modules/unitest/source/data/raw.h264";
  36. static constexpr const char *gimage_path = "../../data/images/%d.jpg";
  37. class PrepareEnvFile {
  38. public:
  39. // device = 0 mlu, device = 1 cpu
  40. explicit PrepareEnvFile(int device, bool img = false) {
  41. std::string mp4_path = GetExePath() + gmp4_path;
  42. std::string image_path = GetExePath() + gimage_path;
  43. src = new DataSource(gname);
  44. if (img == false) {
  45. auto handler = FileHandler::Create(src, "0", mp4_path, 30, false);
  46. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  47. } else {
  48. auto handler = FileHandler::Create(src, "0", image_path, 30, false);
  49. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  50. }
  51. if (device == 0) {
  52. ModuleParamSet param;
  53. param["output_type"] = "mlu";
  54. param["interval"] = "1";
  55. param["decoder_type"] = "mlu";
  56. param["device_id"] = "0";
  57. param["reuse_cndec_buf"] = "false";
  58. src->Open(param);
  59. file_handler->impl_->SetDecodeParam(src->GetSourceParam());
  60. mlu_decoder = std::make_shared<MluDecoder>("fake_id", file_handler->impl_);
  61. } else {
  62. ModuleParamSet param;
  63. param["output_type"] = "cpu";
  64. param["interval"] = "1";
  65. param["decoder_type"] = "cpu";
  66. src->Open(param);
  67. file_handler->impl_->SetDecodeParam(src->GetSourceParam());
  68. ffmpeg_cpu_decoder = std::make_shared<FFmpegCpuDecoder>("fake_id", file_handler->impl_);
  69. }
  70. info.codec_id = AV_CODEC_ID_H264;
  71. }
  72. ~PrepareEnvFile() {
  73. if (src) delete src;
  74. }
  75. DataSource *src = nullptr;
  76. std::shared_ptr<FileHandler> file_handler;
  77. std::shared_ptr<MluDecoder> mlu_decoder;
  78. std::shared_ptr<FFmpegCpuDecoder> ffmpeg_cpu_decoder;
  79. VideoInfo info;
  80. ExtraDecoderInfo extra;
  81. VideoEsPacket pkt;
  82. }; // PrepareEnvFile
  83. class PrepareEnvMem {
  84. public:
  85. PrepareEnvMem() {
  86. std::string h264_path = GetExePath() + gh264_path;
  87. src = new DataSource(gname);
  88. auto handler = ESMemHandler::Create(src, "0");
  89. mem_handler = std::dynamic_pointer_cast<ESMemHandler>(handler);
  90. ModuleParamSet param;
  91. param["output_type"] = "mlu";
  92. param["interval"] = "1";
  93. param["decoder_type"] = "mlu";
  94. param["device_id"] = "0";
  95. param["reuse_cndec_buf"] = "false";
  96. src->Open(param);
  97. mem_handler->impl_->SetDecodeParam(src->GetSourceParam());
  98. mlu_decoder = std::make_shared<MluDecoder>("fake_id", mem_handler->impl_);
  99. info.codec_id = AV_CODEC_ID_H264;
  100. }
  101. ~PrepareEnvMem() {
  102. if (src) delete src;
  103. }
  104. DataSource *src = nullptr;
  105. std::shared_ptr<ESMemHandler> mem_handler;
  106. std::shared_ptr<MluDecoder> mlu_decoder;
  107. VideoInfo info;
  108. ExtraDecoderInfo extra;
  109. VideoEsPacket pkt;
  110. }; // PrepareEnvMem
  111. TEST(SourceMluDecoder, CreateDestroyJpeg) {
  112. PrepareEnvFile env(0, true);
  113. // mjpeg
  114. env.info.codec_id = AV_CODEC_ID_MJPEG;
  115. EXPECT_TRUE(env.mlu_decoder->Create(&env.info, &env.extra));
  116. env.mlu_decoder->Destroy();
  117. }
  118. // Cpu FFmpeg Decoder
  119. TEST(SourceCpuFFmpegDecoder, CreateDestroy) {
  120. int device_type = 1;
  121. PrepareEnvFile env(device_type);
  122. // h264
  123. EXPECT_TRUE(env.ffmpeg_cpu_decoder->Create(&env.info, &env.extra));
  124. env.ffmpeg_cpu_decoder->Destroy();
  125. // h265
  126. env.info.codec_id = AV_CODEC_ID_HEVC;
  127. EXPECT_TRUE(env.ffmpeg_cpu_decoder->Create(&env.info, &env.extra));
  128. env.ffmpeg_cpu_decoder->Destroy();
  129. // mjpeg
  130. PrepareEnvFile env_jpeg(device_type, true);
  131. env_jpeg.info.codec_id = AV_CODEC_ID_MJPEG;
  132. EXPECT_TRUE(env_jpeg.ffmpeg_cpu_decoder->Create(&env_jpeg.info, &env_jpeg.extra));
  133. env_jpeg.ffmpeg_cpu_decoder->Destroy();
  134. // invalid
  135. env_jpeg.info.codec_id = AV_CODEC_ID_NONE;
  136. EXPECT_FALSE(env_jpeg.ffmpeg_cpu_decoder->Create(&env_jpeg.info, &env_jpeg.extra));
  137. env_jpeg.ffmpeg_cpu_decoder->Destroy();
  138. }
  139. TEST(SourceCpuFFmpegDecoder, Process) {
  140. PrepareEnvFile env(1);
  141. EXPECT_TRUE(env.ffmpeg_cpu_decoder->Create(&env.info, &env.extra));
  142. EXPECT_FALSE(env.ffmpeg_cpu_decoder->Process(&env.pkt)); // EOS
  143. env.ffmpeg_cpu_decoder->Destroy();
  144. }
  145. // Mlu Mem Decoder
  146. TEST(SourceMluDecoder, CreateDestroy) {
  147. PrepareEnvMem env;
  148. // h264
  149. env.info.codec_id = AV_CODEC_ID_H264;
  150. EXPECT_TRUE(env.mlu_decoder->Create(&env.info, &env.extra));
  151. env.mlu_decoder->Destroy();
  152. // destroy twice, the function will return directly.
  153. env.mlu_decoder->Destroy();
  154. env.info.codec_id = AV_CODEC_ID_HEVC;
  155. EXPECT_TRUE(env.mlu_decoder->Create(&env.info, &env.extra));
  156. env.mlu_decoder->Destroy();
  157. }
  158. } // namespace cnstream