buffer_decoder.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**************************************************************************/
  2. /* buffer_decoder.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "buffer_decoder.h"
  31. #include "servers/camera/camera_feed.h"
  32. #include <linux/videodev2.h>
  33. BufferDecoder::BufferDecoder(CameraFeed *p_camera_feed) {
  34. camera_feed = p_camera_feed;
  35. width = camera_feed->get_format().width;
  36. height = camera_feed->get_format().height;
  37. image.instantiate();
  38. }
  39. AbstractYuyvBufferDecoder::AbstractYuyvBufferDecoder(CameraFeed *p_camera_feed) :
  40. BufferDecoder(p_camera_feed) {
  41. switch (camera_feed->get_format().pixel_format) {
  42. case V4L2_PIX_FMT_YYUV:
  43. component_indexes = new int[4]{ 0, 1, 2, 3 };
  44. break;
  45. case V4L2_PIX_FMT_YVYU:
  46. component_indexes = new int[4]{ 0, 2, 3, 1 };
  47. break;
  48. case V4L2_PIX_FMT_UYVY:
  49. component_indexes = new int[4]{ 1, 3, 0, 2 };
  50. break;
  51. case V4L2_PIX_FMT_VYUY:
  52. component_indexes = new int[4]{ 1, 3, 2, 0 };
  53. break;
  54. default:
  55. component_indexes = new int[4]{ 0, 2, 1, 3 };
  56. }
  57. }
  58. AbstractYuyvBufferDecoder::~AbstractYuyvBufferDecoder() {
  59. delete[] component_indexes;
  60. }
  61. SeparateYuyvBufferDecoder::SeparateYuyvBufferDecoder(CameraFeed *p_camera_feed) :
  62. AbstractYuyvBufferDecoder(p_camera_feed) {
  63. y_image_data.resize(width * height);
  64. cbcr_image_data.resize(width * height);
  65. y_image.instantiate();
  66. cbcr_image.instantiate();
  67. }
  68. void SeparateYuyvBufferDecoder::decode(StreamingBuffer p_buffer) {
  69. uint8_t *y_dst = (uint8_t *)y_image_data.ptrw();
  70. uint8_t *uv_dst = (uint8_t *)cbcr_image_data.ptrw();
  71. uint8_t *src = (uint8_t *)p_buffer.start;
  72. uint8_t *y0_src = src + component_indexes[0];
  73. uint8_t *y1_src = src + component_indexes[1];
  74. uint8_t *u_src = src + component_indexes[2];
  75. uint8_t *v_src = src + component_indexes[3];
  76. for (int i = 0; i < width * height; i += 2) {
  77. *y_dst++ = *y0_src;
  78. *y_dst++ = *y1_src;
  79. *uv_dst++ = *u_src;
  80. *uv_dst++ = *v_src;
  81. y0_src += 4;
  82. y1_src += 4;
  83. u_src += 4;
  84. v_src += 4;
  85. }
  86. if (y_image.is_valid()) {
  87. y_image->set_data(width, height, false, Image::FORMAT_L8, y_image_data);
  88. } else {
  89. y_image.instantiate(width, height, false, Image::FORMAT_RGB8, y_image_data);
  90. }
  91. if (cbcr_image.is_valid()) {
  92. cbcr_image->set_data(width, height, false, Image::FORMAT_L8, cbcr_image_data);
  93. } else {
  94. cbcr_image.instantiate(width, height, false, Image::FORMAT_RGB8, cbcr_image_data);
  95. }
  96. camera_feed->set_ycbcr_images(y_image, cbcr_image);
  97. }
  98. YuyvToGrayscaleBufferDecoder::YuyvToGrayscaleBufferDecoder(CameraFeed *p_camera_feed) :
  99. AbstractYuyvBufferDecoder(p_camera_feed) {
  100. image_data.resize(width * height);
  101. }
  102. void YuyvToGrayscaleBufferDecoder::decode(StreamingBuffer p_buffer) {
  103. uint8_t *dst = (uint8_t *)image_data.ptrw();
  104. uint8_t *src = (uint8_t *)p_buffer.start;
  105. uint8_t *y0_src = src + component_indexes[0];
  106. uint8_t *y1_src = src + component_indexes[1];
  107. for (int i = 0; i < width * height; i += 2) {
  108. *dst++ = *y0_src;
  109. *dst++ = *y1_src;
  110. y0_src += 4;
  111. y1_src += 4;
  112. }
  113. if (image.is_valid()) {
  114. image->set_data(width, height, false, Image::FORMAT_L8, image_data);
  115. } else {
  116. image.instantiate(width, height, false, Image::FORMAT_RGB8, image_data);
  117. }
  118. camera_feed->set_rgb_image(image);
  119. }
  120. YuyvToRgbBufferDecoder::YuyvToRgbBufferDecoder(CameraFeed *p_camera_feed) :
  121. AbstractYuyvBufferDecoder(p_camera_feed) {
  122. image_data.resize(width * height * 3);
  123. }
  124. void YuyvToRgbBufferDecoder::decode(StreamingBuffer p_buffer) {
  125. uint8_t *src = (uint8_t *)p_buffer.start;
  126. uint8_t *y0_src = src + component_indexes[0];
  127. uint8_t *y1_src = src + component_indexes[1];
  128. uint8_t *u_src = src + component_indexes[2];
  129. uint8_t *v_src = src + component_indexes[3];
  130. uint8_t *dst = (uint8_t *)image_data.ptrw();
  131. for (int i = 0; i < width * height; i += 2) {
  132. int u = *u_src;
  133. int v = *v_src;
  134. int u1 = (((u - 128) << 7) + (u - 128)) >> 6;
  135. int rg = (((u - 128) << 1) + (u - 128) + ((v - 128) << 2) + ((v - 128) << 1)) >> 3;
  136. int v1 = (((v - 128) << 1) + (v - 128)) >> 1;
  137. *dst++ = CLAMP(*y0_src + v1, 0, 255);
  138. *dst++ = CLAMP(*y0_src - rg, 0, 255);
  139. *dst++ = CLAMP(*y0_src + u1, 0, 255);
  140. *dst++ = CLAMP(*y1_src + v1, 0, 255);
  141. *dst++ = CLAMP(*y1_src - rg, 0, 255);
  142. *dst++ = CLAMP(*y1_src + u1, 0, 255);
  143. y0_src += 4;
  144. y1_src += 4;
  145. u_src += 4;
  146. v_src += 4;
  147. }
  148. if (image.is_valid()) {
  149. image->set_data(width, height, false, Image::FORMAT_RGB8, image_data);
  150. } else {
  151. image.instantiate(width, height, false, Image::FORMAT_RGB8, image_data);
  152. }
  153. camera_feed->set_rgb_image(image);
  154. }
  155. CopyBufferDecoder::CopyBufferDecoder(CameraFeed *p_camera_feed, bool p_rgba) :
  156. BufferDecoder(p_camera_feed) {
  157. rgba = p_rgba;
  158. image_data.resize(width * height * (rgba ? 4 : 2));
  159. }
  160. void CopyBufferDecoder::decode(StreamingBuffer p_buffer) {
  161. uint8_t *dst = (uint8_t *)image_data.ptrw();
  162. memcpy(dst, p_buffer.start, p_buffer.length);
  163. if (image.is_valid()) {
  164. image->set_data(width, height, false, rgba ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8, image_data);
  165. } else {
  166. image.instantiate(width, height, false, rgba ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8, image_data);
  167. }
  168. camera_feed->set_rgb_image(image);
  169. }
  170. JpegBufferDecoder::JpegBufferDecoder(CameraFeed *p_camera_feed) :
  171. BufferDecoder(p_camera_feed) {
  172. }
  173. void JpegBufferDecoder::decode(StreamingBuffer p_buffer) {
  174. image_data.resize(p_buffer.length);
  175. uint8_t *dst = (uint8_t *)image_data.ptrw();
  176. memcpy(dst, p_buffer.start, p_buffer.length);
  177. if (image->load_jpg_from_buffer(image_data) == OK) {
  178. camera_feed->set_rgb_image(image);
  179. }
  180. }