test_encode.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #include <gtest/gtest.h>
  21. #include <sys/stat.h>
  22. #include <chrono>
  23. #include <cstdlib>
  24. #include <ctime>
  25. #include <memory>
  26. #include <string>
  27. #include <utility>
  28. #include <vector>
  29. #include "cnstream_frame_va.hpp"
  30. #include "easyinfer/mlu_memory_op.h"
  31. #include "encode.hpp"
  32. namespace cnstream {
  33. static constexpr const char *gname = "encode";
  34. static constexpr int g_channel_id = 0;
  35. static constexpr int g_device_id = 0;
  36. TEST(EncodeModule, OpenCloseWithDefaultParameters) {
  37. Encode DefaultModule(gname);
  38. ModuleParamSet params;
  39. EXPECT_TRUE(DefaultModule.Open(params));
  40. DefaultModule.Close();
  41. }
  42. TEST(EncodeModule, OpenCloseWithDefinedParameters) {
  43. Encode module(gname);
  44. ModuleParamSet params;
  45. params["frame_rate"] = "25";
  46. params["bit_rate"] = "100000";
  47. params["gop_size"] = "30";
  48. params["dst_width"] = "1280";
  49. params["dst_height"] = "720";
  50. params["input_frame"] = "cpu";
  51. params["encoder_type"] = "mlu";
  52. params["device_id"] = "1";
  53. params["view_rows"] = "2";
  54. params["view_cols"] = "2";
  55. params["resample"] = "true";
  56. EXPECT_TRUE(module.Open(params));
  57. module.Close();
  58. params["invalid_param"] = "abc";
  59. EXPECT_FALSE(module.Open(params));
  60. module.Close();
  61. }
  62. TEST(EncodeModule, OpenFailed) {
  63. Encode module(gname);
  64. ModuleParamSet params;
  65. params["input_frame"] = "wrong_type";
  66. EXPECT_FALSE(module.Open(params));
  67. params["input_frame"] = "cpu";
  68. params["encoder_type"] = "wrong_type";
  69. EXPECT_FALSE(module.Open(params));
  70. params["encoder_type"] = "cpu";
  71. std::vector<std::string> digit_params_vec = {
  72. "device_id", "dst_width", "dst_height", "frame_rate", "bit_rate", "view_cols", "view_rows"};
  73. for (auto& param_name : digit_params_vec) {
  74. params[param_name] = "not_digit";
  75. EXPECT_FALSE(module.Open(params));
  76. params.erase(param_name);
  77. }
  78. params["resample"] = "not_bool";
  79. EXPECT_FALSE(module.Open(params));
  80. params["resample"] = "2";
  81. EXPECT_FALSE(module.Open(params));
  82. params.erase("resample");
  83. params["encoder_type"] = "mlu";
  84. params["device_id"] = "-1";
  85. EXPECT_FALSE(module.Open(params));
  86. params["device_id"] = "0";
  87. params["input_frame"] = "mlu";
  88. params["view_rows"] = "2";
  89. params["view_cols"] = "2";
  90. EXPECT_FALSE(module.Open(params));
  91. module.Close();
  92. }
  93. TEST(EncodeModule, ProcessFailedCase) {
  94. Encode module(gname);
  95. ModuleParamSet params;
  96. EXPECT_TRUE(module.Open(params));
  97. // data should not be nullptr
  98. EXPECT_EQ(-1, module.Process(nullptr));
  99. // eos is the first data processed by module will cause error
  100. auto data_eos = CNFrameInfo::Create(std::to_string(0), true);
  101. EXPECT_EQ(-1, module.Process(data_eos));
  102. // invalid width or height of data
  103. auto data = CNFrameInfo::Create(std::to_string(0));
  104. std::shared_ptr<CNDataFrame> frame(new (std::nothrow) CNDataFrame());
  105. data->collection.Add(kCNDataFrameTag, frame);
  106. frame->dst_device_id = g_device_id;
  107. frame->fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV21;
  108. frame->width = 0;
  109. frame->height = 0;
  110. EXPECT_EQ(-1, module.Process(data));
  111. frame->width = 1;
  112. frame->height = 1;
  113. EXPECT_EQ(-1, module.Process(data));
  114. module.Close();
  115. params = {{"file_name", "name_without_extension"}};
  116. EXPECT_TRUE(module.Open(params));
  117. frame->width = 1920;
  118. frame->height = 1080;
  119. EXPECT_EQ(-1, module.Process(data));
  120. module.Close();
  121. std::string folder_str = "./encode_output/";
  122. int status = mkdir(folder_str.c_str(), 0777);
  123. ASSERT_FALSE((status < 0) && (errno != EEXIST));
  124. frame->dst_device_id = 1;
  125. params = {{"input_frame", "mlu"}, {"encoder_type", "mlu"}, {"device_id", "0"},
  126. {"file_name", folder_str + "encode.mp4"}};
  127. EXPECT_TRUE(module.Open(params));
  128. EXPECT_EQ(-1, module.Process(data));
  129. module.Close();
  130. }
  131. std::shared_ptr<CNFrameInfo> CreateFrame(int frame_id, int w, int h, std::string stream_id, void** src_ptr) {
  132. size_t nbytes = ROUND_UP(w, 16) * h * 3 / 2;
  133. edk::MluMemoryOp mem_op;
  134. *src_ptr = mem_op.AllocMlu(nbytes);
  135. auto data = CNFrameInfo::Create(stream_id);
  136. std::shared_ptr<CNDataFrame> frame(new (std::nothrow) CNDataFrame());
  137. frame->frame_id = frame_id;
  138. data->timestamp = INVALID_TIMESTAMP;
  139. frame->width = w;
  140. frame->height = h;
  141. frame->stride[0] = ROUND_UP(w, 16);
  142. frame->stride[1] = ROUND_UP(w, 16);
  143. void *ptr_mlu[2] =
  144. {*src_ptr, reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(*src_ptr) + ROUND_UP(w, 16) * h)};
  145. frame->ctx.dev_type = DevContext::DevType::MLU;
  146. frame->ctx.ddr_channel = g_channel_id;
  147. frame->ctx.dev_id = g_device_id;
  148. frame->fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV21;
  149. frame->dst_device_id = g_device_id;
  150. frame->CopyToSyncMem(ptr_mlu, true);
  151. data->collection.Add(kCNDataFrameTag, frame);
  152. return data;
  153. }
  154. void TestFunc(const ModuleParamSet &params, std::vector<std::pair<uint32_t, uint32_t>> src_wh_vec,
  155. int frame_num = 1, std::string stream_id = "0", bool resample = false) {
  156. int frame_id = 0;
  157. edk::MluMemoryOp mem_op;
  158. std::shared_ptr<Module> ptr = std::make_shared<Encode>(gname);
  159. EXPECT_TRUE(ptr->Open(params)) << " encoder_type: " << params.at("encoder_type")
  160. << ", file_name: " << params.at("file_name")
  161. << ", dst_w/h: " << params.at("dst_width") << "/" << params.at("dst_height");
  162. for (auto &src_wh : src_wh_vec) {
  163. for (int i = 0; i < frame_num; i++) {
  164. auto start = std::chrono::steady_clock::now();
  165. void* src = nullptr;
  166. auto data = CreateFrame(frame_id, src_wh.first, src_wh.second, stream_id, &src);
  167. auto frame = data->collection.Get<std::shared_ptr<CNDataFrame>>(kCNDataFrameTag);
  168. EXPECT_EQ(ptr->Process(data), 0) << " encoder type: " << params.at("encoder_type")
  169. << ", file_name: " << params.at("file_name")
  170. << ", src_w/h: " << src_wh.first << "/" << src_wh.second
  171. << ", dst_w/h: " << params.at("dst_width") << "/" << params.at("dst_height")
  172. << ", process_idx: " << i;
  173. mem_op.FreeMlu(src);
  174. frame_id++;
  175. if (resample) {
  176. auto end = std::chrono::steady_clock::now();
  177. std::chrono::duration<double, std::milli> diff = end - start;
  178. int fr = 30;
  179. if (params.find("frame_rate") != params.end()) {
  180. fr = std::stoi(params.at("frame_rate"));
  181. }
  182. double delay = 1000.0 / fr;
  183. int remain = delay - diff.count();
  184. if (remain > 0) {
  185. std::this_thread::sleep_for(std::chrono::milliseconds(remain));
  186. }
  187. }
  188. }
  189. }
  190. ptr->OnEos(stream_id);
  191. ptr->Close();
  192. }
  193. TEST(EncodeModule, ProcessEncode) {
  194. std::vector<std::pair<uint32_t, uint32_t>> src_wh_vec = {{720, 480}, {1200, 720}, {360, 240}};
  195. std::vector<std::pair<uint32_t, uint32_t>> dst_wh_vec = {{720, 480}, {1920, 1080}, {352, 288}, {501, 299}};
  196. std::vector<std::string> file_name_ext_vec = {"h264", "hevc", "h265", "mp4", "mkv", "jpeg"};
  197. ModuleParamSet params;
  198. int frame_num = 10;
  199. std::string folder_str = "./encode_output/";
  200. int status = mkdir(folder_str.c_str(), 0777);
  201. ASSERT_FALSE((status < 0) && (errno != EEXIST));
  202. // file extensions
  203. for (auto &file_name_ext : file_name_ext_vec) {
  204. // dst resolutions
  205. for (auto &dst_wh : dst_wh_vec) {
  206. params["dst_width"] = std::to_string(dst_wh.first);
  207. params["dst_height"] = std::to_string(dst_wh.second);
  208. // encoder type
  209. for (std::string encoder_type : {"cpu", "mlu"}) {
  210. params["encoder_type"] = encoder_type;
  211. // use image bgr data or origin mlu data
  212. for (std::string input_frame : {"cpu", "mlu"}) {
  213. #ifndef HAVE_CNCV
  214. if (input_frame == "mlu" && encoder_type == "mlu") continue;
  215. #endif
  216. params["input_frame"] = input_frame;
  217. params["file_name"] = folder_str + file_name_ext + "_" + encoder_type + "_encoder_" + input_frame +
  218. "_input_" + params.at("dst_width") + "x" + params.at("dst_height") + "." + file_name_ext;
  219. std::cout << "---- file name : " << params.at("file_name") << std::endl;
  220. TestFunc(params, src_wh_vec, frame_num);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. TEST(EncodeModule, ProcessEncodeResample) {
  227. std::vector<std::pair<uint32_t, uint32_t>> src_wh_vec = {{720, 480}, {1200, 720}, {360, 240}};
  228. std::vector<std::pair<uint32_t, uint32_t>> dst_wh_vec = {{720, 480}, {1920, 1080}, {352, 288}, {501, 299}};
  229. std::vector<std::string> file_name_ext_vec = {"h264", "hevc", "h265", "mp4", "mkv", "jpeg"};
  230. ModuleParamSet params;
  231. int frame_num = 10;
  232. std::string folder_str = "./encode_output/";
  233. int status = mkdir(folder_str.c_str(), 0777);
  234. ASSERT_FALSE((status < 0) && (errno != EEXIST));
  235. // change bit rate, gop size, test resample
  236. params["dst_width"] = std::to_string(dst_wh_vec[1].first);
  237. params["dst_height"] = std::to_string(dst_wh_vec[1].second);
  238. params["bit_rate"] = "5000000";
  239. params["gop_size"] = "40";
  240. params["resample"] = "true";
  241. params["frame_rate"] = "30";
  242. for (auto &file_name_ext : file_name_ext_vec) {
  243. for (std::string encoder_type : {"cpu", "mlu"}) {
  244. params["encoder_type"] = encoder_type;
  245. params["input_frame"] = "cpu";
  246. params["file_name"] = folder_str + "resample_bit_rate_5M_gop50_" + encoder_type + "_encoder_cpu_input" +
  247. params.at("dst_width") + "x" + params.at("dst_height") + "." + file_name_ext;
  248. std::cout << "---- file name : " << params.at("file_name") << std::endl;
  249. TestFunc(params, src_wh_vec, frame_num, "0", true);
  250. }
  251. }
  252. }
  253. void TestFuncMultiView(const ModuleParamSet &params,
  254. std::vector<std::vector<std::pair<uint32_t, uint32_t>>> src_wh_vec,
  255. int frame_num = 1) {
  256. std::shared_ptr<Module> ptr = std::make_shared<Encode>(gname);
  257. EXPECT_TRUE(ptr->Open(params)) << " encoder_type: " << params.at("encoder_type")
  258. << ", file_name: " << params.at("file_name")
  259. << ", dst_w/h: " << params.at("dst_width") << "/" << params.at("dst_height");
  260. uint32_t stream_num = src_wh_vec.size();
  261. ASSERT_NE(stream_num, 0);
  262. uint32_t data_num = src_wh_vec[0].size();
  263. for (auto &src_wh : src_wh_vec) {
  264. ASSERT_TRUE(data_num == src_wh.size());
  265. }
  266. int frame_id = 0;
  267. edk::MluMemoryOp mem_op;
  268. for (uint32_t data_idx = 0; data_idx < data_num; data_idx++) {
  269. for (int i = 0; i < frame_num; i++) {
  270. for (uint32_t stream_id = 0 ; stream_id < stream_num; stream_id++) {
  271. void* src = nullptr;
  272. int width = src_wh_vec[stream_id][data_idx].first;
  273. int height = src_wh_vec[stream_id][data_idx].second;
  274. auto data = CreateFrame(frame_id, width, height, std::to_string(stream_id), &src);
  275. auto frame = data->collection.Get<std::shared_ptr<CNDataFrame>>(kCNDataFrameTag);
  276. EXPECT_EQ(ptr->Process(data), 0) << " encoder type: " << params.at("encoder_type")
  277. << ", file_name: " << params.at("file_name")
  278. << ", src_w/h: " << width << "/" << height
  279. << ", dst_w/h: " << params.at("dst_width") << "/" << params.at("dst_height")
  280. << ", process_idx: " << i;
  281. mem_op.FreeMlu(src);
  282. frame_id++;
  283. }
  284. }
  285. }
  286. for (uint32_t stream_id = 0 ; stream_id < stream_num; stream_id++) {
  287. ptr->OnEos(std::to_string(stream_id));
  288. }
  289. ptr->Close();
  290. }
  291. TEST(EncodeModule, ProcessCpuEncodeMultiViews) {
  292. std::vector<std::pair<uint32_t, uint32_t>> src_wh_vec_1 = {{720, 480}, {1200, 720}, {360, 240}};
  293. std::vector<std::pair<uint32_t, uint32_t>> src_wh_vec_2 = {{352, 288}, {960, 540}, {704, 576}};
  294. std::vector<std::pair<uint32_t, uint32_t>> src_wh_vec_3 = {{3840, 2160}, {1920, 1080}, {1280, 720}};
  295. std::vector<std::pair<uint32_t, uint32_t>> src_wh_vec_4 = {{1024, 768}, {2560, 1440}, {1920, 1080}};
  296. std::vector<std::vector<std::pair<uint32_t, uint32_t>>> src_wh_vec =
  297. {src_wh_vec_1, src_wh_vec_2, src_wh_vec_3, src_wh_vec_4};
  298. std::vector<std::pair<uint32_t, uint32_t>> dst_wh_vec = {{960, 540}, {1920, 1080}, {1280, 720}, {501, 299}};
  299. std::vector<std::string> file_name_ext_vec = {"h264", "hevc", "mp4", "jpeg"};
  300. ModuleParamSet params;
  301. int frame_num = 10;
  302. std::string folder_str = "./encode_output/";
  303. int status = mkdir(folder_str.c_str(), 0777);
  304. ASSERT_FALSE((status < 0) && (errno != EEXIST));
  305. params["view_cols"] = "2";
  306. params["view_rows"] = "3";
  307. for (auto &file_name_ext : file_name_ext_vec) {
  308. for (auto &dst_wh : dst_wh_vec) {
  309. params["dst_width"] = std::to_string(dst_wh.first);
  310. params["dst_height"] = std::to_string(dst_wh.second);
  311. for (auto &encoder_type : {"cpu", "mlu"}) {
  312. params["encoder_type"] = encoder_type;
  313. params["file_name"] = folder_str + "multi_" + encoder_type +
  314. "_encoder_" + params.at("dst_width") + "x" + params.at("dst_height") + "." + file_name_ext;
  315. std::cout << "---- file name : " << params.at("file_name") << std::endl;
  316. TestFuncMultiView(params, src_wh_vec, frame_num);
  317. }
  318. }
  319. }
  320. }
  321. } // namespace cnstream