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.

230 lines
7.0KB

  1. /*
  2. * Copyright (C) 2003 the ffmpeg project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. /**
  22. * @file roqvideo.c
  23. * Id RoQ Video common functions based on work by Dr. Tim Ferguson
  24. */
  25. #include "avcodec.h"
  26. #include "roqvideo.h"
  27. #define avg2(a,b) av_clip_uint8(((int)(a)+(int)(b)+1)>>1)
  28. #define avg4(a,b,c,d) av_clip_uint8(((int)(a)+(int)(b)+(int)(c)+(int)(d)+2)>>2)
  29. void ff_apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
  30. {
  31. unsigned char *yptr;
  32. yptr = ri->current_frame->data[0] + (y * ri->y_stride) + x;
  33. *yptr++ = cell->y[0];
  34. *yptr++ = cell->y[1];
  35. yptr += (ri->y_stride - 2);
  36. *yptr++ = cell->y[2];
  37. *yptr++ = cell->y[3];
  38. ri->current_frame->data[1][(y/2) * (ri->c_stride) + x/2] = cell->u;
  39. ri->current_frame->data[2][(y/2) * (ri->c_stride) + x/2] = cell->v;
  40. }
  41. void ff_apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
  42. {
  43. unsigned long row_inc, c_row_inc;
  44. register unsigned char y0, y1, u, v;
  45. unsigned char *yptr, *uptr, *vptr;
  46. yptr = ri->current_frame->data[0] + (y * ri->y_stride) + x;
  47. uptr = ri->current_frame->data[1] + (y/2) * (ri->c_stride) + x/2;
  48. vptr = ri->current_frame->data[2] + (y/2) * (ri->c_stride) + x/2;
  49. row_inc = ri->y_stride - 4;
  50. c_row_inc = (ri->c_stride) - 2;
  51. *yptr++ = y0 = cell->y[0]; *uptr++ = u = cell->u; *vptr++ = v = cell->v;
  52. *yptr++ = y0;
  53. *yptr++ = y1 = cell->y[1]; *uptr++ = u; *vptr++ = v;
  54. *yptr++ = y1;
  55. yptr += row_inc;
  56. *yptr++ = y0;
  57. *yptr++ = y0;
  58. *yptr++ = y1;
  59. *yptr++ = y1;
  60. yptr += row_inc; uptr += c_row_inc; vptr += c_row_inc;
  61. *yptr++ = y0 = cell->y[2]; *uptr++ = u; *vptr++ = v;
  62. *yptr++ = y0;
  63. *yptr++ = y1 = cell->y[3]; *uptr++ = u; *vptr++ = v;
  64. *yptr++ = y1;
  65. yptr += row_inc;
  66. *yptr++ = y0;
  67. *yptr++ = y0;
  68. *yptr++ = y1;
  69. *yptr++ = y1;
  70. }
  71. void ff_apply_motion_4x4(RoqContext *ri, int x, int y,
  72. int deltax, int deltay)
  73. {
  74. int i, hw, mx, my;
  75. unsigned char *pa, *pb;
  76. mx = x + deltax;
  77. my = y + deltay;
  78. /* check MV against frame boundaries */
  79. if ((mx < 0) || (mx > ri->avctx->width - 4) ||
  80. (my < 0) || (my > ri->avctx->height - 4)) {
  81. av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
  82. mx, my, ri->avctx->width, ri->avctx->height);
  83. return;
  84. }
  85. pa = ri->current_frame->data[0] + (y * ri->y_stride) + x;
  86. pb = ri->last_frame->data[0] + (my * ri->y_stride) + mx;
  87. for(i = 0; i < 4; i++) {
  88. pa[0] = pb[0];
  89. pa[1] = pb[1];
  90. pa[2] = pb[2];
  91. pa[3] = pb[3];
  92. pa += ri->y_stride;
  93. pb += ri->y_stride;
  94. }
  95. hw = ri->y_stride/2;
  96. pa = ri->current_frame->data[1] + (y * ri->y_stride)/4 + x/2;
  97. pb = ri->last_frame->data[1] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
  98. for(i = 0; i < 2; i++) {
  99. switch(((my & 0x01) << 1) | (mx & 0x01)) {
  100. case 0:
  101. pa[0] = pb[0];
  102. pa[1] = pb[1];
  103. pa[hw] = pb[hw];
  104. pa[hw+1] = pb[hw+1];
  105. break;
  106. case 1:
  107. pa[0] = avg2(pb[0], pb[1]);
  108. pa[1] = avg2(pb[1], pb[2]);
  109. pa[hw] = avg2(pb[hw], pb[hw+1]);
  110. pa[hw+1] = avg2(pb[hw+1], pb[hw+2]);
  111. break;
  112. case 2:
  113. pa[0] = avg2(pb[0], pb[hw]);
  114. pa[1] = avg2(pb[1], pb[hw+1]);
  115. pa[hw] = avg2(pb[hw], pb[hw*2]);
  116. pa[hw+1] = avg2(pb[hw+1], pb[(hw*2)+1]);
  117. break;
  118. case 3:
  119. pa[0] = avg4(pb[0], pb[1], pb[hw], pb[hw+1]);
  120. pa[1] = avg4(pb[1], pb[2], pb[hw+1], pb[hw+2]);
  121. pa[hw] = avg4(pb[hw], pb[hw+1], pb[hw*2], pb[(hw*2)+1]);
  122. pa[hw+1] = avg4(pb[hw+1], pb[hw+2], pb[(hw*2)+1], pb[(hw*2)+1]);
  123. break;
  124. }
  125. pa = ri->current_frame->data[2] + (y * ri->y_stride)/4 + x/2;
  126. pb = ri->last_frame->data[2] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
  127. }
  128. }
  129. void ff_apply_motion_8x8(RoqContext *ri, int x, int y,
  130. int deltax, int deltay)
  131. {
  132. int mx, my, i, j, hw;
  133. unsigned char *pa, *pb;
  134. mx = x + deltax;
  135. my = y + deltay;
  136. /* check MV against frame boundaries */
  137. if ((mx < 0) || (mx > ri->avctx->width - 8) ||
  138. (my < 0) || (my > ri->avctx->height - 8)) {
  139. av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
  140. mx, my, ri->avctx->width, ri->avctx->height);
  141. return;
  142. }
  143. pa = ri->current_frame->data[0] + (y * ri->y_stride) + x;
  144. pb = ri->last_frame->data[0] + (my * ri->y_stride) + mx;
  145. for(i = 0; i < 8; i++) {
  146. pa[0] = pb[0];
  147. pa[1] = pb[1];
  148. pa[2] = pb[2];
  149. pa[3] = pb[3];
  150. pa[4] = pb[4];
  151. pa[5] = pb[5];
  152. pa[6] = pb[6];
  153. pa[7] = pb[7];
  154. pa += ri->y_stride;
  155. pb += ri->y_stride;
  156. }
  157. hw = ri->c_stride;
  158. pa = ri->current_frame->data[1] + (y * ri->y_stride)/4 + x/2;
  159. pb = ri->last_frame->data[1] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
  160. for(j = 0; j < 2; j++) {
  161. for(i = 0; i < 4; i++) {
  162. switch(((my & 0x01) << 1) | (mx & 0x01)) {
  163. case 0:
  164. pa[0] = pb[0];
  165. pa[1] = pb[1];
  166. pa[2] = pb[2];
  167. pa[3] = pb[3];
  168. break;
  169. case 1:
  170. pa[0] = avg2(pb[0], pb[1]);
  171. pa[1] = avg2(pb[1], pb[2]);
  172. pa[2] = avg2(pb[2], pb[3]);
  173. pa[3] = avg2(pb[3], pb[4]);
  174. break;
  175. case 2:
  176. pa[0] = avg2(pb[0], pb[hw]);
  177. pa[1] = avg2(pb[1], pb[hw+1]);
  178. pa[2] = avg2(pb[2], pb[hw+2]);
  179. pa[3] = avg2(pb[3], pb[hw+3]);
  180. break;
  181. case 3:
  182. pa[0] = avg4(pb[0], pb[1], pb[hw], pb[hw+1]);
  183. pa[1] = avg4(pb[1], pb[2], pb[hw+1], pb[hw+2]);
  184. pa[2] = avg4(pb[2], pb[3], pb[hw+2], pb[hw+3]);
  185. pa[3] = avg4(pb[3], pb[4], pb[hw+3], pb[hw+4]);
  186. break;
  187. }
  188. pa += ri->c_stride;
  189. pb += ri->c_stride;
  190. }
  191. pa = ri->current_frame->data[2] + (y * ri->y_stride)/4 + x/2;
  192. pb = ri->last_frame->data[2] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
  193. }
  194. }