test_frame.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <ctime>
  21. #include <memory>
  22. #include <string>
  23. #include <vector>
  24. #include "gtest/gtest.h"
  25. #include "opencv2/highgui/highgui.hpp"
  26. #include "opencv2/imgproc/imgproc.hpp"
  27. #if (CV_MAJOR_VERSION >= 3)
  28. #include "opencv2/imgcodecs/imgcodecs.hpp"
  29. #endif
  30. #include "cnrt.h"
  31. #include "cnstream_frame.hpp"
  32. #include "cnstream_frame_va.hpp"
  33. namespace cnstream {
  34. static const int width = 1280;
  35. static const int height = 720;
  36. static const int g_dev_id = 0;
  37. void InitFrame(CNDataFrame* frame, int image_type, void** ptr_cpu) {
  38. frame->ctx.dev_type = DevContext::DevType::CPU;
  39. frame->height = 1080;
  40. frame->width = 1920;
  41. frame->stride[0] = 1920;
  42. if (image_type == 0) { // RGB or BGR
  43. ptr_cpu[0] = malloc(sizeof(uint8_t) * frame->height * frame->stride[0] * 3);
  44. } else if (image_type == 1) { // YUV and height % 2 == 0
  45. frame->stride[1] = 1920;
  46. ptr_cpu[0] = malloc(sizeof(uint8_t) * frame->height * frame->stride[0]);
  47. ptr_cpu[1] = malloc(sizeof(uint8_t) * frame->height * frame->stride[1] * 0.5);
  48. } else if (image_type == 2) { // YUV and height % 2 != 0
  49. frame->stride[1] = 1920;
  50. frame->height -= 1;
  51. ptr_cpu[0] = malloc(sizeof(uint8_t) * (frame->height) * frame->stride[0]);
  52. ptr_cpu[1] = malloc(sizeof(uint8_t) * (frame->height) * frame->stride[1] * 0.5);
  53. }
  54. }
  55. void RunConvertImageTest(CNDataFrame* frame, int image_type, void** ptr_cpu) {
  56. frame->dst_device_id = g_dev_id;
  57. frame->CopyToSyncMem(ptr_cpu, true);
  58. EXPECT_FALSE(frame->ImageBGR().empty());
  59. free(ptr_cpu[0]);
  60. if (image_type == 1 || image_type == 2) { // YUV
  61. free(ptr_cpu[1]);
  62. }
  63. }
  64. TEST(CoreFrame, ConvertBGRImageToBGR) {
  65. CNDataFrame frame;
  66. void* ptr_cpu[1];
  67. InitFrame(&frame, 0, ptr_cpu);
  68. frame.fmt = CNDataFormat::CN_PIXEL_FORMAT_BGR24;
  69. RunConvertImageTest(&frame, 0, ptr_cpu);
  70. }
  71. TEST(CoreFrame, ConvertRGBImageToBGR) {
  72. CNDataFrame frame;
  73. void* ptr_cpu[1];
  74. InitFrame(&frame, 0, ptr_cpu);
  75. frame.fmt = CNDataFormat::CN_PIXEL_FORMAT_RGB24;
  76. RunConvertImageTest(&frame, 0, ptr_cpu);
  77. }
  78. TEST(CoreFrame, ConvertYUV12ImageToBGR) {
  79. CNDataFrame frame;
  80. void* ptr_cpu[2];
  81. InitFrame(&frame, 1, ptr_cpu);
  82. frame.fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV12;
  83. RunConvertImageTest(&frame, 1, ptr_cpu);
  84. }
  85. TEST(CoreFrame, ConvertYUV12ImageToBGR2) { // height % 2 != 0
  86. CNDataFrame frame;
  87. void* ptr_cpu[2];
  88. InitFrame(&frame, 2, ptr_cpu);
  89. frame.fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV12;
  90. RunConvertImageTest(&frame, 2, ptr_cpu);
  91. }
  92. TEST(CoreFrame, ConvertYUV21ImageToBGR) {
  93. CNDataFrame frame;
  94. void* ptr_cpu[2];
  95. InitFrame(&frame, 1, ptr_cpu);
  96. frame.fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV21;
  97. RunConvertImageTest(&frame, 1, ptr_cpu);
  98. }
  99. TEST(CoreFrame, ConvertYUV21ImageToBGR2) {
  100. CNDataFrame frame;
  101. void* ptr_cpu[2];
  102. InitFrame(&frame, 2, ptr_cpu);
  103. frame.fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV21;
  104. RunConvertImageTest(&frame, 2, ptr_cpu);
  105. }
  106. TEST(CoreFrame, ConvertImageToBGRFailed) {
  107. CNDataFrame frame;
  108. void* ptr_cpu[2];
  109. InitFrame(&frame, 1, ptr_cpu);
  110. frame.fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV21;
  111. frame.dst_device_id = g_dev_id;
  112. frame.CopyToSyncMem(ptr_cpu, true);
  113. frame.fmt = CNDataFormat::CN_INVALID;
  114. EXPECT_DEATH(frame.ImageBGR(), ".*Unsupported pixel format.*");
  115. free(ptr_cpu[0]);
  116. free(ptr_cpu[1]);
  117. }
  118. TEST(CoreFrameDeathTest, CopyToSyncMemFailed) {
  119. CNDataFrame frame;
  120. void* ptr_cpu[2];
  121. InitFrame(&frame, 0, ptr_cpu);
  122. frame.fmt = CNDataFormat::CN_PIXEL_FORMAT_BGR24;
  123. EXPECT_DEATH(frame.CopyToSyncMem(ptr_cpu, true), "");
  124. frame.ctx.dev_type = DevContext::DevType::INVALID;
  125. EXPECT_DEATH(frame.CopyToSyncMem(ptr_cpu, false), "");
  126. free(ptr_cpu[0]);
  127. }
  128. TEST(CoreFrameDeathTest, CopyToSyncMemOnDevice) {
  129. CNS_CNRT_CHECK(cnrtInit(0));
  130. unsigned int dev_num = 0;
  131. CNS_CNRT_CHECK(cnrtGetDeviceCount(&dev_num));
  132. size_t nbytes = width * height * 3;
  133. size_t boundary = 1 << 16;
  134. nbytes = (nbytes + boundary - 1) & ~(boundary - 1);
  135. std::shared_ptr<void> frame_data = cnMluMemAlloc(nbytes, g_dev_id);
  136. // fake frame data
  137. std::shared_ptr<CNDataFrame> frame = std::make_shared<CNDataFrame>();
  138. if (frame == nullptr) {
  139. std::cout << "frame create error\n";
  140. return;
  141. }
  142. frame->frame_id = 0;
  143. frame->width = width;
  144. frame->height = height;
  145. frame->mlu_data = frame_data;
  146. frame->stride[0] = width;
  147. frame->stride[1] = width;
  148. frame->ctx.ddr_channel = 0;
  149. frame->ctx.dev_id = g_dev_id;
  150. frame->ctx.dev_type = DevContext::DevType::MLU;
  151. frame->fmt = CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV12;
  152. EXPECT_DEATH(frame->CopyToSyncMemOnDevice(g_dev_id), "");
  153. // check device num, if num > 1, do sync
  154. // TODO(gaoyujia) : update driver and then uncomment the line below
  155. // if (dev_num > 1) frame->CopyToSyncMemOnDevice(1);
  156. EXPECT_DEATH(frame->CopyToSyncMemOnDevice(dev_num + 1), "");
  157. frame->ctx.dev_type = DevContext::DevType::INVALID;
  158. EXPECT_DEATH(frame->CopyToSyncMemOnDevice(1), "");
  159. }
  160. TEST(CoreFrame, InferObjAddAttribute) {
  161. CNInferObject infer_obj;
  162. std::string key = "test_key";
  163. CNInferAttr value;
  164. value.id = 0;
  165. value.value = 0;
  166. value.score = 0.9;
  167. // add attribute successfully
  168. EXPECT_TRUE(infer_obj.AddAttribute(key, value));
  169. // attribute exist
  170. EXPECT_FALSE(infer_obj.AddAttribute(key, value));
  171. }
  172. TEST(CoreFrame, InferObjGetAttribute) {
  173. CNInferObject infer_obj;
  174. CNInferAttr infer_attr;
  175. // get attribute failed
  176. infer_attr = infer_obj.GetAttribute("wrong_key");
  177. EXPECT_EQ(infer_attr.id, -1);
  178. EXPECT_EQ(infer_attr.value, -1);
  179. EXPECT_EQ(infer_attr.score, 0.0);
  180. std::string key = "test_key";
  181. CNInferAttr value;
  182. value.id = 0;
  183. value.value = 0;
  184. value.score = 0.9;
  185. // add attribute successfully
  186. EXPECT_TRUE(infer_obj.AddAttribute(key, value));
  187. // get attribute successfully
  188. infer_attr = infer_obj.GetAttribute(key);
  189. EXPECT_EQ(infer_attr.id, value.id);
  190. EXPECT_EQ(infer_attr.value, value.value);
  191. EXPECT_EQ(infer_attr.score, value.score);
  192. }
  193. TEST(CoreFrame, InferObjAddExtraAttribute) {
  194. CNInferObject infer_obj;
  195. std::string key = "test_key";
  196. std::string value = "test_value";
  197. // add extra attribute successfully
  198. EXPECT_TRUE(infer_obj.AddExtraAttribute(key, value));
  199. // attribute exist
  200. EXPECT_FALSE(infer_obj.AddExtraAttribute(key, value));
  201. }
  202. TEST(CoreFrame, InferObjGetExtraAttribute) {
  203. CNInferObject infer_obj;
  204. // get extra attribute failed
  205. EXPECT_EQ(infer_obj.GetExtraAttribute("wrong_key"), "");
  206. std::string key = "test_key";
  207. std::string value = "test_value";
  208. // add extra attribute successfully
  209. EXPECT_TRUE(infer_obj.AddExtraAttribute(key, value));
  210. // get extra attribute successfully
  211. EXPECT_EQ(infer_obj.GetExtraAttribute(key), value);
  212. }
  213. TEST(CoreFrame, InferObjAddAndGetfeature) {
  214. CNInferObject infer_obj;
  215. CNInferFeature infer_feature1{1, 2, 3, 4, 5};
  216. CNInferFeature infer_feature2{1, 2, 3, 4, 5, 6, 7};
  217. // add feature successfully
  218. EXPECT_NO_THROW(infer_obj.AddFeature("feature1", infer_feature1));
  219. EXPECT_NO_THROW(infer_obj.AddFeature("feature2", infer_feature2));
  220. // get features
  221. CNInferFeatures features = infer_obj.GetFeatures();
  222. EXPECT_EQ(features.size(), (uint32_t)2);
  223. EXPECT_EQ(infer_obj.GetFeature("feature1"), infer_feature1);
  224. EXPECT_EQ(infer_obj.GetFeature("feature2"), infer_feature2);
  225. }
  226. TEST(CoreFrame, CreateFrameInfo) {
  227. // create frame success
  228. EXPECT_NE(CNFrameInfo::Create("0"), nullptr);
  229. // create eos frame success
  230. EXPECT_NE(CNFrameInfo::Create("0", true), nullptr);
  231. }
  232. } // namespace cnstream