You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/media/base/videoframe.h"
  28. #include <cstring>
  29. #ifdef HAVE_YUV
  30. #include "libyuv/compare.h"
  31. #include "libyuv/planar_functions.h"
  32. #include "libyuv/scale.h"
  33. #endif
  34. #include "talk/base/logging.h"
  35. #include "talk/media/base/videocommon.h"
  36. namespace cricket {
  37. // Round to 2 pixels because Chroma channels are half size.
  38. #define ROUNDTO2(v) (v & ~1)
  39. talk_base::StreamResult VideoFrame::Write(talk_base::StreamInterface* stream,
  40. int* error) {
  41. talk_base::StreamResult result = talk_base::SR_SUCCESS;
  42. const uint8* in_y = GetYPlane();
  43. const uint8* in_u = GetUPlane();
  44. const uint8* in_v = GetVPlane();
  45. if (!in_y || !in_u || !in_v) {
  46. return result; // Nothing to write.
  47. }
  48. const int32 y_pitch = GetYPitch();
  49. const int32 u_pitch = GetUPitch();
  50. const int32 v_pitch = GetVPitch();
  51. const size_t width = GetWidth();
  52. const size_t height = GetHeight();
  53. const size_t half_width = (width + 1) >> 1;
  54. const size_t half_height = (height + 1) >> 1;
  55. // Write Y.
  56. for (size_t row = 0; row < height; ++row) {
  57. result = stream->Write(in_y + row * y_pitch, width, NULL, error);
  58. if (result != talk_base::SR_SUCCESS) {
  59. return result;
  60. }
  61. }
  62. // Write U.
  63. for (size_t row = 0; row < half_height; ++row) {
  64. result = stream->Write(in_u + row * u_pitch, half_width, NULL, error);
  65. if (result != talk_base::SR_SUCCESS) {
  66. return result;
  67. }
  68. }
  69. // Write V.
  70. for (size_t row = 0; row < half_height; ++row) {
  71. result = stream->Write(in_v + row * v_pitch, half_width, NULL, error);
  72. if (result != talk_base::SR_SUCCESS) {
  73. return result;
  74. }
  75. }
  76. return result;
  77. }
  78. // TODO(fbarchard): Handle odd width/height with rounding.
  79. void VideoFrame::StretchToPlanes(
  80. uint8* y, uint8* u, uint8* v,
  81. int32 dst_pitch_y, int32 dst_pitch_u, int32 dst_pitch_v,
  82. size_t width, size_t height, bool interpolate, bool vert_crop) const {
  83. #ifdef HAVE_YUV
  84. if (!GetYPlane() || !GetUPlane() || !GetVPlane())
  85. return;
  86. const uint8* in_y = GetYPlane();
  87. const uint8* in_u = GetUPlane();
  88. const uint8* in_v = GetVPlane();
  89. int32 iwidth = GetWidth();
  90. int32 iheight = GetHeight();
  91. if (vert_crop) {
  92. // Adjust the input width:height ratio to be the same as the output ratio.
  93. if (iwidth * height > iheight * width) {
  94. // Reduce the input width, but keep size/position aligned for YuvScaler
  95. iwidth = ROUNDTO2(iheight * width / height);
  96. int32 iwidth_offset = ROUNDTO2((GetWidth() - iwidth) / 2);
  97. in_y += iwidth_offset;
  98. in_u += iwidth_offset / 2;
  99. in_v += iwidth_offset / 2;
  100. } else if (iwidth * height < iheight * width) {
  101. // Reduce the input height.
  102. iheight = iwidth * height / width;
  103. int32 iheight_offset = (GetHeight() - iheight) >> 2;
  104. iheight_offset <<= 1; // Ensure that iheight_offset is even.
  105. in_y += iheight_offset * GetYPitch();
  106. in_u += iheight_offset / 2 * GetUPitch();
  107. in_v += iheight_offset / 2 * GetVPitch();
  108. }
  109. }
  110. // Scale to the output I420 frame.
  111. libyuv::Scale(in_y, in_u, in_v,
  112. GetYPitch(),
  113. GetUPitch(),
  114. GetVPitch(),
  115. iwidth, iheight,
  116. y, u, v, dst_pitch_y, dst_pitch_u, dst_pitch_v,
  117. width, height, interpolate);
  118. #endif
  119. }
  120. size_t VideoFrame::StretchToBuffer(size_t w, size_t h,
  121. uint8* buffer, size_t size,
  122. bool interpolate, bool vert_crop) const {
  123. if (!buffer) return 0;
  124. size_t needed = SizeOf(w, h);
  125. if (needed <= size) {
  126. uint8* bufy = buffer;
  127. uint8* bufu = bufy + w * h;
  128. uint8* bufv = bufu + ((w + 1) >> 1) * ((h + 1) >> 1);
  129. StretchToPlanes(bufy, bufu, bufv, w, (w + 1) >> 1, (w + 1) >> 1, w, h,
  130. interpolate, vert_crop);
  131. }
  132. return needed;
  133. }
  134. void VideoFrame::StretchToFrame(VideoFrame *target,
  135. bool interpolate, bool vert_crop) const {
  136. if (!target) return;
  137. StretchToPlanes(target->GetYPlane(),
  138. target->GetUPlane(),
  139. target->GetVPlane(),
  140. target->GetYPitch(),
  141. target->GetUPitch(),
  142. target->GetVPitch(),
  143. target->GetWidth(),
  144. target->GetHeight(),
  145. interpolate, vert_crop);
  146. target->SetElapsedTime(GetElapsedTime());
  147. target->SetTimeStamp(GetTimeStamp());
  148. }
  149. VideoFrame* VideoFrame::Stretch(size_t w, size_t h,
  150. bool interpolate, bool vert_crop) const {
  151. VideoFrame* dest = CreateEmptyFrame(w, h, GetPixelWidth(), GetPixelHeight(),
  152. GetElapsedTime(), GetTimeStamp());
  153. if (dest) {
  154. StretchToFrame(dest, interpolate, vert_crop);
  155. }
  156. return dest;
  157. }
  158. bool VideoFrame::SetToBlack() {
  159. #ifdef HAVE_YUV
  160. return libyuv::I420Rect(GetYPlane(), GetYPitch(),
  161. GetUPlane(), GetUPitch(),
  162. GetVPlane(), GetVPitch(),
  163. 0, 0, GetWidth(), GetHeight(),
  164. 16, 128, 128) == 0;
  165. #else
  166. int uv_size = GetUPitch() * GetChromaHeight();
  167. memset(GetYPlane(), 16, GetWidth() * GetHeight());
  168. memset(GetUPlane(), 128, uv_size);
  169. memset(GetVPlane(), 128, uv_size);
  170. return true;
  171. #endif
  172. }
  173. static const size_t kMaxSampleSize = 1000000000u;
  174. // Returns whether a sample is valid
  175. bool VideoFrame::Validate(uint32 fourcc, int w, int h,
  176. const uint8 *sample, size_t sample_size) {
  177. if (h < 0) {
  178. h = -h;
  179. }
  180. // 16384 is maximum resolution for VP8 codec.
  181. if (w < 1 || w > 16384 || h < 1 || h > 16384) {
  182. LOG(LS_ERROR) << "Invalid dimensions: " << w << "x" << h;
  183. return false;
  184. }
  185. uint32 format = CanonicalFourCC(fourcc);
  186. int expected_bpp = 8;
  187. switch (format) {
  188. case FOURCC_I400:
  189. case FOURCC_RGGB:
  190. case FOURCC_BGGR:
  191. case FOURCC_GRBG:
  192. case FOURCC_GBRG:
  193. expected_bpp = 8;
  194. break;
  195. case FOURCC_I420:
  196. case FOURCC_I411:
  197. case FOURCC_YU12:
  198. case FOURCC_YV12:
  199. case FOURCC_M420:
  200. case FOURCC_Q420:
  201. case FOURCC_NV21:
  202. case FOURCC_NV12:
  203. expected_bpp = 12;
  204. break;
  205. case FOURCC_I422:
  206. case FOURCC_YV16:
  207. case FOURCC_YUY2:
  208. case FOURCC_UYVY:
  209. case FOURCC_RGBP:
  210. case FOURCC_RGBO:
  211. case FOURCC_R444:
  212. expected_bpp = 16;
  213. break;
  214. case FOURCC_V210:
  215. expected_bpp = 22; // 22.5 actually.
  216. break;
  217. case FOURCC_I444:
  218. case FOURCC_YV24:
  219. case FOURCC_24BG:
  220. case FOURCC_RAW:
  221. expected_bpp = 24;
  222. break;
  223. case FOURCC_ABGR:
  224. case FOURCC_BGRA:
  225. case FOURCC_ARGB:
  226. expected_bpp = 32;
  227. break;
  228. case FOURCC_MJPG:
  229. case FOURCC_H264:
  230. expected_bpp = 0;
  231. break;
  232. default:
  233. expected_bpp = 8; // Expect format is at least 8 bits per pixel.
  234. break;
  235. }
  236. size_t expected_size = (w * expected_bpp + 7) / 8 * h;
  237. // For compressed formats, expect 4 bits per 16 x 16 macro. I420 would be
  238. // 6 bits, but grey can be 4 bits.
  239. if (expected_bpp == 0) {
  240. expected_size = ((w + 15) / 16) * ((h + 15) / 16) * 4 / 8;
  241. }
  242. if (sample == NULL) {
  243. LOG(LS_ERROR) << "NULL sample pointer."
  244. << " format: " << GetFourccName(format)
  245. << " bpp: " << expected_bpp
  246. << " size: " << w << "x" << h
  247. << " expected: " << expected_size
  248. << " " << sample_size;
  249. return false;
  250. }
  251. if (sample_size < expected_size) {
  252. LOG(LS_ERROR) << "Size field is too small."
  253. << " format: " << GetFourccName(format)
  254. << " bpp: " << expected_bpp
  255. << " size: " << w << "x" << h
  256. << " " << sample_size
  257. << " expected: " << expected_size
  258. << " sample[0..3]: " << static_cast<int>(sample[0])
  259. << ", " << static_cast<int>(sample[1])
  260. << ", " << static_cast<int>(sample[2])
  261. << ", " << static_cast<int>(sample[3]);
  262. return false;
  263. }
  264. if (sample_size > kMaxSampleSize) {
  265. LOG(LS_WARNING) << "Size field is invalid."
  266. << " format: " << GetFourccName(format)
  267. << " bpp: " << expected_bpp
  268. << " size: " << w << "x" << h
  269. << " " << sample_size
  270. << " expected: " << 2 * expected_size
  271. << " sample[0..3]: " << static_cast<int>(sample[0])
  272. << ", " << static_cast<int>(sample[1])
  273. << ", " << static_cast<int>(sample[2])
  274. << ", " << static_cast<int>(sample[3]);
  275. return false;
  276. }
  277. // Show large size warning once every 100 frames.
  278. static int large_warn100 = 0;
  279. size_t large_expected_size = expected_size * 2;
  280. if (expected_bpp >= 8 &&
  281. (sample_size > large_expected_size || sample_size > kMaxSampleSize) &&
  282. large_warn100 % 100 == 0) {
  283. ++large_warn100;
  284. LOG(LS_WARNING) << "Size field is too large."
  285. << " format: " << GetFourccName(format)
  286. << " bpp: " << expected_bpp
  287. << " size: " << w << "x" << h
  288. << " bytes: " << sample_size
  289. << " expected: " << large_expected_size
  290. << " sample[0..3]: " << static_cast<int>(sample[0])
  291. << ", " << static_cast<int>(sample[1])
  292. << ", " << static_cast<int>(sample[2])
  293. << ", " << static_cast<int>(sample[3]);
  294. }
  295. // Scan pages to ensure they are there and don't contain a single value and
  296. // to generate an error.
  297. if (!memcmp(sample + sample_size - 8, sample + sample_size - 4, 4) &&
  298. !memcmp(sample, sample + 4, sample_size - 4)) {
  299. LOG(LS_WARNING) << "Duplicate value for all pixels."
  300. << " format: " << GetFourccName(format)
  301. << " bpp: " << expected_bpp
  302. << " size: " << w << "x" << h
  303. << " bytes: " << sample_size
  304. << " expected: " << expected_size
  305. << " sample[0..3]: " << static_cast<int>(sample[0])
  306. << ", " << static_cast<int>(sample[1])
  307. << ", " << static_cast<int>(sample[2])
  308. << ", " << static_cast<int>(sample[3]);
  309. }
  310. static bool valid_once = true;
  311. if (valid_once) {
  312. valid_once = false;
  313. LOG(LS_INFO) << "Validate frame passed."
  314. << " format: " << GetFourccName(format)
  315. << " bpp: " << expected_bpp
  316. << " size: " << w << "x" << h
  317. << " bytes: " << sample_size
  318. << " expected: " << expected_size
  319. << " sample[0..3]: " << static_cast<int>(sample[0])
  320. << ", " << static_cast<int>(sample[1])
  321. << ", " << static_cast<int>(sample[2])
  322. << ", " << static_cast<int>(sample[3]);
  323. }
  324. return true;
  325. }
  326. } // namespace cricket