video_preprocess_common.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*************************************************************************
  2. * Copyright (C) [2021] 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 "video_preprocess_common.hpp"
  21. bool ConvertColorSpace(size_t width, size_t height, VideoPixelFmt src_fmt, VideoPixelFmt dst_fmt,
  22. uint8_t* src_img_data, cv::Mat* dst_img) {
  23. cv::Mat src_img;
  24. bool cvt_ret = true;
  25. switch (src_fmt) {
  26. case VideoPixelFmt::NV12: { /*src nv12*/
  27. src_img = cv::Mat(height * 3 / 2, width, CV_8UC1, src_img_data);
  28. switch (dst_fmt) {
  29. case VideoPixelFmt::RGB24: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2RGB_NV12); break;
  30. case VideoPixelFmt::BGR24: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2BGR_NV12); break;
  31. case VideoPixelFmt::RGBA:
  32. case VideoPixelFmt::BGRA:
  33. case VideoPixelFmt::ARGB:
  34. case VideoPixelFmt::ABGR: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2BGRA_NV12); break;
  35. default: cvt_ret = false; break;
  36. }
  37. break;
  38. }
  39. case VideoPixelFmt::NV21: { /*src nv21*/
  40. src_img = cv::Mat(height * 3 / 2, width, CV_8UC1, src_img_data);
  41. switch (dst_fmt) {
  42. case VideoPixelFmt::RGB24: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2RGB_NV21); break;
  43. case VideoPixelFmt::BGR24: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2BGR_NV21); break;
  44. case VideoPixelFmt::RGBA:
  45. case VideoPixelFmt::BGRA:
  46. case VideoPixelFmt::ARGB:
  47. case VideoPixelFmt::ABGR: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2BGRA_NV21); break;
  48. default: cvt_ret = false; break;
  49. }
  50. break;
  51. }
  52. case VideoPixelFmt::RGB24: { /*src rgb*/
  53. src_img = cv::Mat(height, width, CV_8UC3, src_img_data);
  54. switch (dst_fmt) {
  55. case VideoPixelFmt::RGB24: *dst_img = src_img; break;
  56. case VideoPixelFmt::BGR24: cv::cvtColor(src_img, *dst_img, cv::COLOR_RGB2BGR); break;
  57. case VideoPixelFmt::RGBA:
  58. case VideoPixelFmt::BGRA:
  59. case VideoPixelFmt::ARGB:
  60. case VideoPixelFmt::ABGR: cv::cvtColor(src_img, *dst_img, cv::COLOR_RGB2BGRA); break;
  61. default: cvt_ret = false; break;
  62. }
  63. break;
  64. }
  65. case VideoPixelFmt::BGR24: { /*src bgr*/
  66. src_img = cv::Mat(height, width, CV_8UC3, src_img_data);
  67. switch (dst_fmt) {
  68. case VideoPixelFmt::RGB24: cv::cvtColor(src_img, *dst_img, cv::COLOR_BGR2RGB); break;
  69. case VideoPixelFmt::BGR24: *dst_img = src_img; break;
  70. case VideoPixelFmt::RGBA:
  71. case VideoPixelFmt::BGRA:
  72. case VideoPixelFmt::ARGB:
  73. case VideoPixelFmt::ABGR: cv::cvtColor(src_img, *dst_img, cv::COLOR_BGR2BGRA); break;
  74. default: cvt_ret = false; break;
  75. }
  76. break;
  77. }
  78. default: cvt_ret = false; break;
  79. }
  80. if (!cvt_ret) {
  81. return false;
  82. }
  83. switch (dst_fmt) {
  84. case VideoPixelFmt::RGBA: {
  85. cv::Mat rgba(dst_img->size(), dst_img->type());
  86. // bgra->rgba b:0->2 g:1->1 r:2->0 a:3->3
  87. int from_to[] = {0, 2, 1, 1, 2, 0, 3, 3};
  88. cv::mixChannels(dst_img, 1, &rgba, 1, from_to, 4);
  89. *dst_img = rgba;
  90. break;
  91. }
  92. case VideoPixelFmt::ARGB: {
  93. cv::Mat argb(dst_img->size(), dst_img->type());
  94. // bgra->argb b:0->3 g:1->2 r:2->1 a:3->0
  95. int from_to[] = {0, 3, 1, 2, 2, 1, 3, 0};
  96. cv::mixChannels(dst_img, 1, &argb, 1, from_to, 4);
  97. *dst_img = argb;
  98. break;
  99. }
  100. case VideoPixelFmt::ABGR: {
  101. cv::Mat abgr(dst_img->size(), dst_img->type());
  102. // bgra->abgr b:0->1 g:1->2 r:2->3 a:3->0
  103. int from_to[] = {0, 1, 1, 2, 2, 3, 3, 0};
  104. cv::mixChannels(dst_img, 1, &abgr, 1, from_to, 4);
  105. *dst_img = abgr;
  106. break;
  107. }
  108. default: break;
  109. }
  110. return true;
  111. }