video.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <jni.h>
  2. #include <libyuv.h>
  3. enum COLOR_FORMATTYPE {
  4. COLOR_FormatMonochrome = 1,
  5. COLOR_Format8bitRGB332 = 2,
  6. COLOR_Format12bitRGB444 = 3,
  7. COLOR_Format16bitARGB4444 = 4,
  8. COLOR_Format16bitARGB1555 = 5,
  9. COLOR_Format16bitRGB565 = 6,
  10. COLOR_Format16bitBGR565 = 7,
  11. COLOR_Format18bitRGB666 = 8,
  12. COLOR_Format18bitARGB1665 = 9,
  13. COLOR_Format19bitARGB1666 = 10,
  14. COLOR_Format24bitRGB888 = 11,
  15. COLOR_Format24bitBGR888 = 12,
  16. COLOR_Format24bitARGB1887 = 13,
  17. COLOR_Format25bitARGB1888 = 14,
  18. COLOR_Format32bitBGRA8888 = 15,
  19. COLOR_Format32bitARGB8888 = 16,
  20. COLOR_FormatYUV411Planar = 17,
  21. COLOR_FormatYUV411PackedPlanar = 18,
  22. COLOR_FormatYUV420Planar = 19,
  23. COLOR_FormatYUV420PackedPlanar = 20,
  24. COLOR_FormatYUV420SemiPlanar = 21,
  25. COLOR_FormatYUV422Planar = 22,
  26. COLOR_FormatYUV422PackedPlanar = 23,
  27. COLOR_FormatYUV422SemiPlanar = 24,
  28. COLOR_FormatYCbYCr = 25,
  29. COLOR_FormatYCrYCb = 26,
  30. COLOR_FormatCbYCrY = 27,
  31. COLOR_FormatCrYCbY = 28,
  32. COLOR_FormatYUV444Interleaved = 29,
  33. COLOR_FormatRawBayer8bit = 30,
  34. COLOR_FormatRawBayer10bit = 31,
  35. COLOR_FormatRawBayer8bitcompressed = 32,
  36. COLOR_FormatL2 = 33,
  37. COLOR_FormatL4 = 34,
  38. COLOR_FormatL8 = 35,
  39. COLOR_FormatL16 = 36,
  40. COLOR_FormatL24 = 37,
  41. COLOR_FormatL32 = 38,
  42. COLOR_FormatYUV420PackedSemiPlanar = 39,
  43. COLOR_FormatYUV422PackedSemiPlanar = 40,
  44. COLOR_Format18BitBGR666 = 41,
  45. COLOR_Format24BitARGB6666 = 42,
  46. COLOR_Format24BitABGR6666 = 43,
  47. COLOR_TI_FormatYUV420PackedSemiPlanar = 0x7f000100,
  48. COLOR_FormatSurface = 0x7F000789,
  49. COLOR_QCOM_FormatYUV420SemiPlanar = 0x7fa30c00
  50. };
  51. jint isSemiPlanarYUV(jint colorFormat) {
  52. switch (colorFormat) {
  53. case COLOR_FormatYUV420Planar:
  54. case COLOR_FormatYUV420PackedPlanar:
  55. return 0;
  56. case COLOR_FormatYUV420SemiPlanar:
  57. case COLOR_FormatYUV420PackedSemiPlanar:
  58. case COLOR_TI_FormatYUV420PackedSemiPlanar:
  59. return 1;
  60. default:
  61. return 0;
  62. }
  63. }
  64. JNIEXPORT jint Java_org_telegram_messenger_Utilities_convertVideoFrame(JNIEnv *env, jclass class, jobject src, jobject dest, jint destFormat, jint width, jint height, jint padding, jint swap) {
  65. if (!src || !dest || !destFormat) {
  66. return 0;
  67. }
  68. jbyte *srcBuff = (*env)->GetDirectBufferAddress(env, src);
  69. jbyte *destBuff = (*env)->GetDirectBufferAddress(env, dest);
  70. int half_width = (width + 1) / 2;
  71. int half_height = (height + 1) / 2;
  72. if (!isSemiPlanarYUV(destFormat)) {
  73. if (!swap) {
  74. ARGBToI420((uint8_t *) srcBuff, width * 4,
  75. (uint8_t *) destBuff, width,
  76. (uint8_t *) (destBuff + width * height + half_width * half_height + padding * 5 / 4), half_width,
  77. (uint8_t *) (destBuff + width * height + padding), half_width,
  78. width, height);
  79. } else {
  80. ARGBToI420((uint8_t *) srcBuff, width * 4,
  81. (uint8_t *) destBuff, width,
  82. (uint8_t *) (destBuff + width * height + padding), half_width,
  83. (uint8_t *) (destBuff + width * height + half_width * half_height + padding * 5 / 4), half_width,
  84. width, height);
  85. }
  86. } else {
  87. if (!swap) {
  88. ARGBToNV21((uint8_t *) srcBuff, width * 4,
  89. (uint8_t *) destBuff, width,
  90. (uint8_t *) (destBuff + width * height + padding), half_width * 2,
  91. width, height);
  92. } else {
  93. ARGBToNV12((uint8_t *) srcBuff, width * 4,
  94. (uint8_t *) destBuff, width,
  95. (uint8_t *) (destBuff + width * height + padding), half_width * 2,
  96. width, height);
  97. }
  98. }
  99. return 1;
  100. }