tiny-nv12-scale.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <string.h>
  2. #include "tiny-nv12-scale.h"
  3. /* TODO: optimize this stuff later, or replace with something better. it's
  4. * kind of garbage. although normally it shouldn't be called that often. plus
  5. * it's nearest neighbor so not really a huge deal. at the very least it
  6. * should be sse2 at some point. */
  7. void nv12_scale_init(nv12_scale_t *s, enum target_format format, int dst_cx,
  8. int dst_cy, int src_cx, int src_cy)
  9. {
  10. s->format = format;
  11. s->src_cx = src_cx;
  12. s->src_cy = src_cy;
  13. s->dst_cx = dst_cx;
  14. s->dst_cy = dst_cy;
  15. }
  16. static void nv12_scale_nearest(nv12_scale_t *s, uint8_t *dst_start,
  17. const uint8_t *src)
  18. {
  19. register uint8_t *dst = dst_start;
  20. const int src_cx = s->src_cx;
  21. const int src_cy = s->src_cy;
  22. const int dst_cx = s->dst_cx;
  23. const int dst_cy = s->dst_cy;
  24. /* lum */
  25. for (int y = 0; y < dst_cy; y++) {
  26. const int src_line = y * src_cy / dst_cy * s->src_cx;
  27. for (int x = 0; x < dst_cx; x++) {
  28. const int src_x = x * src_cx / dst_cx;
  29. *(dst++) = src[src_line + src_x];
  30. }
  31. }
  32. src += src_cx * src_cy;
  33. /* uv */
  34. const int dst_cx_d2 = dst_cx / 2;
  35. const int dst_cy_d2 = dst_cy / 2;
  36. for (int y = 0; y < dst_cy_d2; y++) {
  37. const int src_line = y * src_cy / dst_cy * src_cx;
  38. for (int x = 0; x < dst_cx_d2; x++) {
  39. const int src_x = x * src_cx / dst_cx * 2;
  40. const int pos = src_line + src_x;
  41. *(dst++) = src[pos];
  42. *(dst++) = src[pos + 1];
  43. }
  44. }
  45. }
  46. static void nv12_scale_nearest_to_i420(nv12_scale_t *s, uint8_t *dst_start,
  47. const uint8_t *src)
  48. {
  49. register uint8_t *dst = dst_start;
  50. const int src_cx = s->src_cx;
  51. const int src_cy = s->src_cy;
  52. const int dst_cx = s->dst_cx;
  53. const int dst_cy = s->dst_cy;
  54. const int size = src_cx * src_cy;
  55. /* lum */
  56. for (int y = 0; y < dst_cy; y++) {
  57. const int src_line = y * src_cy / dst_cy * s->src_cx;
  58. for (int x = 0; x < dst_cx; x++) {
  59. const int src_x = x * src_cx / dst_cx;
  60. *(dst++) = src[src_line + src_x];
  61. }
  62. }
  63. src += size;
  64. /* uv */
  65. const int dst_cx_d2 = dst_cx / 2;
  66. const int dst_cy_d2 = dst_cy / 2;
  67. register uint8_t *dst2 = dst + dst_cx * dst_cy / 4;
  68. for (int y = 0; y < dst_cy_d2; y++) {
  69. const int src_line = y * src_cy / dst_cy * src_cx;
  70. for (int x = 0; x < dst_cx_d2; x++) {
  71. const int src_x = x * src_cx / dst_cx * 2;
  72. const int pos = src_line + src_x;
  73. *(dst++) = src[pos];
  74. *(dst2++) = src[pos + 1];
  75. }
  76. }
  77. }
  78. static void nv12_convert_to_i420(nv12_scale_t *s, uint8_t *dst_start,
  79. const uint8_t *src_start)
  80. {
  81. const int size = s->src_cx * s->src_cy;
  82. const int size_d4 = size / 4;
  83. memcpy(dst_start, src_start, size);
  84. register uint8_t *dst1 = dst_start + size;
  85. register uint8_t *dst2 = dst1 + size_d4;
  86. register uint8_t *dst_end = dst2 + size_d4;
  87. register const uint8_t *src = src_start + size;
  88. while (dst2 < dst_end) {
  89. *(dst1++) = *(src++);
  90. *(dst2++) = *(src++);
  91. }
  92. }
  93. static void nv12_scale_nearest_to_yuy2(nv12_scale_t *s, uint8_t *dst_start,
  94. const uint8_t *src)
  95. {
  96. register uint8_t *dst = dst_start;
  97. const int src_cx = s->src_cx;
  98. const int src_cy = s->src_cy;
  99. const int dst_cx = s->dst_cx;
  100. const int dst_cy = s->dst_cy;
  101. const int src_cx_d2 = src_cx / 2;
  102. const int src_cy_d2 = src_cy / 2;
  103. const int dst_cx_d2 = dst_cx / 2;
  104. const int dst_cy_d2 = dst_cy / 2;
  105. const int size = src_cx * src_cy;
  106. const uint8_t *src_uv = src + size;
  107. register int uv_flip = 0;
  108. for (int y = 0; y < dst_cy; y++) {
  109. const int src_line = y * src_cy / dst_cy * s->src_cx;
  110. const int src_line_d2 =
  111. y / 2 * src_cy_d2 / dst_cy_d2 * s->src_cx;
  112. for (int x = 0; x < dst_cx; x++) {
  113. const int src_x = x * src_cx / dst_cx;
  114. const int src_x_d2 = x / 2 * src_cx_d2 / dst_cx_d2;
  115. const int pos = src_line + src_x;
  116. const int pos_uv = src_line_d2 + src_x_d2 * 2 + uv_flip;
  117. *(dst++) = src[pos];
  118. *(dst++) = src_uv[pos_uv];
  119. uv_flip ^= 1;
  120. }
  121. }
  122. }
  123. static void nv12_convert_to_yuy2(nv12_scale_t *s, uint8_t *dst_start,
  124. const uint8_t *src_start)
  125. {
  126. const int size = s->src_cx * s->src_cy;
  127. const int size_dst_line = s->src_cx * 2;
  128. register const uint8_t *src_y = src_start;
  129. register const uint8_t *src_uv = src_y + size;
  130. register uint8_t *dst = dst_start;
  131. register uint8_t *dst_end = dst + size * 2;
  132. while (dst < dst_end) {
  133. register uint8_t *dst_line_end = dst + size_dst_line;
  134. const uint8_t *src_uv_start = src_uv;
  135. while (dst < dst_line_end) {
  136. *(dst++) = *(src_y++);
  137. *(dst++) = *(src_uv++);
  138. *(dst++) = *(src_y++);
  139. *(dst++) = *(src_uv++);
  140. }
  141. dst_line_end = dst + size_dst_line;
  142. src_uv = src_uv_start;
  143. while (dst < dst_line_end) {
  144. *(dst++) = *(src_y++);
  145. *(dst++) = *(src_uv++);
  146. *(dst++) = *(src_y++);
  147. *(dst++) = *(src_uv++);
  148. }
  149. }
  150. }
  151. void nv12_do_scale(nv12_scale_t *s, uint8_t *dst, const uint8_t *src)
  152. {
  153. if (s->src_cx == s->dst_cx && s->src_cy == s->dst_cy) {
  154. if (s->format == TARGET_FORMAT_I420)
  155. nv12_convert_to_i420(s, dst, src);
  156. else if (s->format == TARGET_FORMAT_YUY2)
  157. nv12_convert_to_yuy2(s, dst, src);
  158. else
  159. memcpy(dst, src, s->src_cx * s->src_cy * 3 / 2);
  160. } else {
  161. if (s->format == TARGET_FORMAT_I420)
  162. nv12_scale_nearest_to_i420(s, dst, src);
  163. else if (s->format == TARGET_FORMAT_YUY2)
  164. nv12_scale_nearest_to_yuy2(s, dst, src);
  165. else
  166. nv12_scale_nearest(s, dst, src);
  167. }
  168. }