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.

984 lines
37KB

  1. /*
  2. * Copyright (c) 2000,2001 Fabrice Bellard
  3. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <string.h>
  24. #include "libavutil/internal.h"
  25. #include "avcodec.h"
  26. #include "h261.h"
  27. #include "mpegutils.h"
  28. #include "mpegvideo.h"
  29. #include "mjpegenc.h"
  30. #include "msmpeg4.h"
  31. #include "qpeldsp.h"
  32. #include <limits.h>
  33. static void gmc1_motion(MpegEncContext *s,
  34. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  35. uint8_t **ref_picture)
  36. {
  37. uint8_t *ptr;
  38. int src_x, src_y, motion_x, motion_y;
  39. ptrdiff_t offset, linesize, uvlinesize;
  40. int emu = 0;
  41. motion_x = s->sprite_offset[0][0];
  42. motion_y = s->sprite_offset[0][1];
  43. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy + 1));
  44. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy + 1));
  45. motion_x <<= (3 - s->sprite_warping_accuracy);
  46. motion_y <<= (3 - s->sprite_warping_accuracy);
  47. src_x = av_clip(src_x, -16, s->width);
  48. if (src_x == s->width)
  49. motion_x = 0;
  50. src_y = av_clip(src_y, -16, s->height);
  51. if (src_y == s->height)
  52. motion_y = 0;
  53. linesize = s->linesize;
  54. uvlinesize = s->uvlinesize;
  55. ptr = ref_picture[0] + src_y * linesize + src_x;
  56. if ((unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0) ||
  57. (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)) {
  58. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  59. linesize, linesize,
  60. 17, 17,
  61. src_x, src_y,
  62. s->h_edge_pos, s->v_edge_pos);
  63. ptr = s->edge_emu_buffer;
  64. }
  65. if ((motion_x | motion_y) & 7) {
  66. s->mdsp.gmc1(dest_y, ptr, linesize, 16,
  67. motion_x & 15, motion_y & 15, 128 - s->no_rounding);
  68. s->mdsp.gmc1(dest_y + 8, ptr + 8, linesize, 16,
  69. motion_x & 15, motion_y & 15, 128 - s->no_rounding);
  70. } else {
  71. int dxy;
  72. dxy = ((motion_x >> 3) & 1) | ((motion_y >> 2) & 2);
  73. if (s->no_rounding) {
  74. s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  75. } else {
  76. s->hdsp.put_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  77. }
  78. }
  79. if (CONFIG_GRAY && s->avctx->flags & CODEC_FLAG_GRAY)
  80. return;
  81. motion_x = s->sprite_offset[1][0];
  82. motion_y = s->sprite_offset[1][1];
  83. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy + 1));
  84. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy + 1));
  85. motion_x <<= (3 - s->sprite_warping_accuracy);
  86. motion_y <<= (3 - s->sprite_warping_accuracy);
  87. src_x = av_clip(src_x, -8, s->width >> 1);
  88. if (src_x == s->width >> 1)
  89. motion_x = 0;
  90. src_y = av_clip(src_y, -8, s->height >> 1);
  91. if (src_y == s->height >> 1)
  92. motion_y = 0;
  93. offset = (src_y * uvlinesize) + src_x;
  94. ptr = ref_picture[1] + offset;
  95. if ((unsigned)src_x >= FFMAX((s->h_edge_pos >> 1) - 9, 0) ||
  96. (unsigned)src_y >= FFMAX((s->v_edge_pos >> 1) - 9, 0)) {
  97. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  98. uvlinesize, uvlinesize,
  99. 9, 9,
  100. src_x, src_y,
  101. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  102. ptr = s->edge_emu_buffer;
  103. emu = 1;
  104. }
  105. s->mdsp.gmc1(dest_cb, ptr, uvlinesize, 8,
  106. motion_x & 15, motion_y & 15, 128 - s->no_rounding);
  107. ptr = ref_picture[2] + offset;
  108. if (emu) {
  109. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  110. uvlinesize, uvlinesize,
  111. 9, 9,
  112. src_x, src_y,
  113. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  114. ptr = s->edge_emu_buffer;
  115. }
  116. s->mdsp.gmc1(dest_cr, ptr, uvlinesize, 8,
  117. motion_x & 15, motion_y & 15, 128 - s->no_rounding);
  118. }
  119. static void gmc_motion(MpegEncContext *s,
  120. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  121. uint8_t **ref_picture)
  122. {
  123. uint8_t *ptr;
  124. int linesize, uvlinesize;
  125. const int a = s->sprite_warping_accuracy;
  126. int ox, oy;
  127. linesize = s->linesize;
  128. uvlinesize = s->uvlinesize;
  129. ptr = ref_picture[0];
  130. ox = s->sprite_offset[0][0] + s->sprite_delta[0][0] * s->mb_x * 16 +
  131. s->sprite_delta[0][1] * s->mb_y * 16;
  132. oy = s->sprite_offset[0][1] + s->sprite_delta[1][0] * s->mb_x * 16 +
  133. s->sprite_delta[1][1] * s->mb_y * 16;
  134. s->mdsp.gmc(dest_y, ptr, linesize, 16,
  135. ox, oy,
  136. s->sprite_delta[0][0], s->sprite_delta[0][1],
  137. s->sprite_delta[1][0], s->sprite_delta[1][1],
  138. a + 1, (1 << (2 * a + 1)) - s->no_rounding,
  139. s->h_edge_pos, s->v_edge_pos);
  140. s->mdsp.gmc(dest_y + 8, ptr, linesize, 16,
  141. ox + s->sprite_delta[0][0] * 8,
  142. oy + s->sprite_delta[1][0] * 8,
  143. s->sprite_delta[0][0], s->sprite_delta[0][1],
  144. s->sprite_delta[1][0], s->sprite_delta[1][1],
  145. a + 1, (1 << (2 * a + 1)) - s->no_rounding,
  146. s->h_edge_pos, s->v_edge_pos);
  147. if (CONFIG_GRAY && s->avctx->flags & CODEC_FLAG_GRAY)
  148. return;
  149. ox = s->sprite_offset[1][0] + s->sprite_delta[0][0] * s->mb_x * 8 +
  150. s->sprite_delta[0][1] * s->mb_y * 8;
  151. oy = s->sprite_offset[1][1] + s->sprite_delta[1][0] * s->mb_x * 8 +
  152. s->sprite_delta[1][1] * s->mb_y * 8;
  153. ptr = ref_picture[1];
  154. s->mdsp.gmc(dest_cb, ptr, uvlinesize, 8,
  155. ox, oy,
  156. s->sprite_delta[0][0], s->sprite_delta[0][1],
  157. s->sprite_delta[1][0], s->sprite_delta[1][1],
  158. a + 1, (1 << (2 * a + 1)) - s->no_rounding,
  159. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  160. ptr = ref_picture[2];
  161. s->mdsp.gmc(dest_cr, ptr, uvlinesize, 8,
  162. ox, oy,
  163. s->sprite_delta[0][0], s->sprite_delta[0][1],
  164. s->sprite_delta[1][0], s->sprite_delta[1][1],
  165. a + 1, (1 << (2 * a + 1)) - s->no_rounding,
  166. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  167. }
  168. static inline int hpel_motion(MpegEncContext *s,
  169. uint8_t *dest, uint8_t *src,
  170. int src_x, int src_y,
  171. op_pixels_func *pix_op,
  172. int motion_x, int motion_y)
  173. {
  174. int dxy = 0;
  175. int emu = 0;
  176. src_x += motion_x >> 1;
  177. src_y += motion_y >> 1;
  178. /* WARNING: do no forget half pels */
  179. src_x = av_clip(src_x, -16, s->width); // FIXME unneeded for emu?
  180. if (src_x != s->width)
  181. dxy |= motion_x & 1;
  182. src_y = av_clip(src_y, -16, s->height);
  183. if (src_y != s->height)
  184. dxy |= (motion_y & 1) << 1;
  185. src += src_y * s->linesize + src_x;
  186. if (s->unrestricted_mv) {
  187. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 1) - 8, 0) ||
  188. (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y & 1) - 8, 0)) {
  189. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  190. s->linesize, s->linesize,
  191. 9, 9,
  192. src_x, src_y, s->h_edge_pos,
  193. s->v_edge_pos);
  194. src = s->edge_emu_buffer;
  195. emu = 1;
  196. }
  197. }
  198. pix_op[dxy](dest, src, s->linesize, 8);
  199. return emu;
  200. }
  201. static av_always_inline
  202. void mpeg_motion_internal(MpegEncContext *s,
  203. uint8_t *dest_y,
  204. uint8_t *dest_cb,
  205. uint8_t *dest_cr,
  206. int field_based,
  207. int bottom_field,
  208. int field_select,
  209. uint8_t **ref_picture,
  210. op_pixels_func (*pix_op)[4],
  211. int motion_x,
  212. int motion_y,
  213. int h,
  214. int is_mpeg12,
  215. int mb_y)
  216. {
  217. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  218. int dxy, uvdxy, mx, my, src_x, src_y,
  219. uvsrc_x, uvsrc_y, v_edge_pos;
  220. ptrdiff_t uvlinesize, linesize;
  221. #if 0
  222. if (s->quarter_sample) {
  223. motion_x >>= 1;
  224. motion_y >>= 1;
  225. }
  226. #endif
  227. v_edge_pos = s->v_edge_pos >> field_based;
  228. linesize = s->current_picture.f->linesize[0] << field_based;
  229. uvlinesize = s->current_picture.f->linesize[1] << field_based;
  230. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  231. src_x = s->mb_x * 16 + (motion_x >> 1);
  232. src_y = (mb_y << (4 - field_based)) + (motion_y >> 1);
  233. if (!is_mpeg12 && s->out_format == FMT_H263) {
  234. if ((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based) {
  235. mx = (motion_x >> 1) | (motion_x & 1);
  236. my = motion_y >> 1;
  237. uvdxy = ((my & 1) << 1) | (mx & 1);
  238. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  239. uvsrc_y = (mb_y << (3 - field_based)) + (my >> 1);
  240. } else {
  241. uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
  242. uvsrc_x = src_x >> 1;
  243. uvsrc_y = src_y >> 1;
  244. }
  245. // Even chroma mv's are full pel in H261
  246. } else if (!is_mpeg12 && s->out_format == FMT_H261) {
  247. mx = motion_x / 4;
  248. my = motion_y / 4;
  249. uvdxy = 0;
  250. uvsrc_x = s->mb_x * 8 + mx;
  251. uvsrc_y = mb_y * 8 + my;
  252. } else {
  253. if (s->chroma_y_shift) {
  254. mx = motion_x / 2;
  255. my = motion_y / 2;
  256. uvdxy = ((my & 1) << 1) | (mx & 1);
  257. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  258. uvsrc_y = (mb_y << (3 - field_based)) + (my >> 1);
  259. } else {
  260. if (s->chroma_x_shift) {
  261. // Chroma422
  262. mx = motion_x / 2;
  263. uvdxy = ((motion_y & 1) << 1) | (mx & 1);
  264. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  265. uvsrc_y = src_y;
  266. } else {
  267. // Chroma444
  268. uvdxy = dxy;
  269. uvsrc_x = src_x;
  270. uvsrc_y = src_y;
  271. }
  272. }
  273. }
  274. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  275. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  276. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  277. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 1) - 16, 0) ||
  278. (unsigned)src_y > FFMAX(v_edge_pos - (motion_y & 1) - h, 0)) {
  279. if (is_mpeg12 ||
  280. s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
  281. s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  282. av_log(s->avctx, AV_LOG_DEBUG,
  283. "MPEG motion vector out of boundary (%d %d)\n", src_x,
  284. src_y);
  285. return;
  286. }
  287. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  288. s->linesize, s->linesize,
  289. 17, 17 + field_based,
  290. src_x, src_y << field_based,
  291. s->h_edge_pos, s->v_edge_pos);
  292. ptr_y = s->edge_emu_buffer;
  293. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  294. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  295. s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
  296. s->uvlinesize, s->uvlinesize,
  297. 9, 9 + field_based,
  298. uvsrc_x, uvsrc_y << field_based,
  299. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  300. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
  301. s->uvlinesize, s->uvlinesize,
  302. 9, 9 + field_based,
  303. uvsrc_x, uvsrc_y << field_based,
  304. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  305. ptr_cb = uvbuf;
  306. ptr_cr = uvbuf + 16;
  307. }
  308. }
  309. /* FIXME use this for field pix too instead of the obnoxious hack which
  310. * changes picture.data */
  311. if (bottom_field) {
  312. dest_y += s->linesize;
  313. dest_cb += s->uvlinesize;
  314. dest_cr += s->uvlinesize;
  315. }
  316. if (field_select) {
  317. ptr_y += s->linesize;
  318. ptr_cb += s->uvlinesize;
  319. ptr_cr += s->uvlinesize;
  320. }
  321. pix_op[0][dxy](dest_y, ptr_y, linesize, h);
  322. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  323. pix_op[s->chroma_x_shift][uvdxy]
  324. (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
  325. pix_op[s->chroma_x_shift][uvdxy]
  326. (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
  327. }
  328. if (!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
  329. s->out_format == FMT_H261) {
  330. ff_h261_loop_filter(s);
  331. }
  332. }
  333. /* apply one mpeg motion vector to the three components */
  334. static void mpeg_motion(MpegEncContext *s,
  335. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  336. int field_select, uint8_t **ref_picture,
  337. op_pixels_func (*pix_op)[4],
  338. int motion_x, int motion_y, int h, int mb_y)
  339. {
  340. #if !CONFIG_SMALL
  341. if (s->out_format == FMT_MPEG1)
  342. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
  343. field_select, ref_picture, pix_op,
  344. motion_x, motion_y, h, 1, mb_y);
  345. else
  346. #endif
  347. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
  348. field_select, ref_picture, pix_op,
  349. motion_x, motion_y, h, 0, mb_y);
  350. }
  351. static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
  352. uint8_t *dest_cb, uint8_t *dest_cr,
  353. int bottom_field, int field_select,
  354. uint8_t **ref_picture,
  355. op_pixels_func (*pix_op)[4],
  356. int motion_x, int motion_y, int h, int mb_y)
  357. {
  358. #if !CONFIG_SMALL
  359. if(s->out_format == FMT_MPEG1)
  360. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
  361. bottom_field, field_select, ref_picture, pix_op,
  362. motion_x, motion_y, h, 1, mb_y);
  363. else
  364. #endif
  365. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
  366. bottom_field, field_select, ref_picture, pix_op,
  367. motion_x, motion_y, h, 0, mb_y);
  368. }
  369. // FIXME: SIMDify, avg variant, 16x16 version
  370. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride)
  371. {
  372. int x;
  373. uint8_t *const top = src[1];
  374. uint8_t *const left = src[2];
  375. uint8_t *const mid = src[0];
  376. uint8_t *const right = src[3];
  377. uint8_t *const bottom = src[4];
  378. #define OBMC_FILTER(x, t, l, m, r, b)\
  379. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  380. #define OBMC_FILTER4(x, t, l, m, r, b)\
  381. OBMC_FILTER(x , t, l, m, r, b);\
  382. OBMC_FILTER(x+1 , t, l, m, r, b);\
  383. OBMC_FILTER(x +stride, t, l, m, r, b);\
  384. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  385. x = 0;
  386. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  387. OBMC_FILTER (x + 1, 2, 1, 5, 0, 0);
  388. OBMC_FILTER4(x + 2, 2, 1, 5, 0, 0);
  389. OBMC_FILTER4(x + 4, 2, 0, 5, 1, 0);
  390. OBMC_FILTER (x + 6, 2, 0, 5, 1, 0);
  391. OBMC_FILTER (x + 7, 2, 0, 4, 2, 0);
  392. x += stride;
  393. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  394. OBMC_FILTER (x + 1, 1, 2, 5, 0, 0);
  395. OBMC_FILTER (x + 6, 1, 0, 5, 2, 0);
  396. OBMC_FILTER (x + 7, 1, 0, 5, 2, 0);
  397. x += stride;
  398. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  399. OBMC_FILTER4(x + 2, 1, 1, 6, 0, 0);
  400. OBMC_FILTER4(x + 4, 1, 0, 6, 1, 0);
  401. OBMC_FILTER4(x + 6, 1, 0, 5, 2, 0);
  402. x += 2 * stride;
  403. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  404. OBMC_FILTER4(x + 2, 0, 1, 6, 0, 1);
  405. OBMC_FILTER4(x + 4, 0, 0, 6, 1, 1);
  406. OBMC_FILTER4(x + 6, 0, 0, 5, 2, 1);
  407. x += 2*stride;
  408. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  409. OBMC_FILTER (x + 1, 0, 2, 5, 0, 1);
  410. OBMC_FILTER4(x + 2, 0, 1, 5, 0, 2);
  411. OBMC_FILTER4(x + 4, 0, 0, 5, 1, 2);
  412. OBMC_FILTER (x + 6, 0, 0, 5, 2, 1);
  413. OBMC_FILTER (x + 7, 0, 0, 5, 2, 1);
  414. x += stride;
  415. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  416. OBMC_FILTER (x + 1, 0, 1, 5, 0, 2);
  417. OBMC_FILTER (x + 6, 0, 0, 5, 1, 2);
  418. OBMC_FILTER (x + 7, 0, 0, 4, 2, 2);
  419. }
  420. /* obmc for 1 8x8 luma block */
  421. static inline void obmc_motion(MpegEncContext *s,
  422. uint8_t *dest, uint8_t *src,
  423. int src_x, int src_y,
  424. op_pixels_func *pix_op,
  425. int16_t mv[5][2] /* mid top left right bottom */)
  426. #define MID 0
  427. {
  428. int i;
  429. uint8_t *ptr[5];
  430. assert(s->quarter_sample == 0);
  431. for (i = 0; i < 5; i++) {
  432. if (i && mv[i][0] == mv[MID][0] && mv[i][1] == mv[MID][1]) {
  433. ptr[i] = ptr[MID];
  434. } else {
  435. ptr[i] = s->obmc_scratchpad + 8 * (i & 1) +
  436. s->linesize * 8 * (i >> 1);
  437. hpel_motion(s, ptr[i], src, src_x, src_y, pix_op,
  438. mv[i][0], mv[i][1]);
  439. }
  440. }
  441. put_obmc(dest, ptr, s->linesize);
  442. }
  443. static inline void qpel_motion(MpegEncContext *s,
  444. uint8_t *dest_y,
  445. uint8_t *dest_cb,
  446. uint8_t *dest_cr,
  447. int field_based, int bottom_field,
  448. int field_select, uint8_t **ref_picture,
  449. op_pixels_func (*pix_op)[4],
  450. qpel_mc_func (*qpix_op)[16],
  451. int motion_x, int motion_y, int h)
  452. {
  453. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  454. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos;
  455. ptrdiff_t linesize, uvlinesize;
  456. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  457. src_x = s->mb_x * 16 + (motion_x >> 2);
  458. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  459. v_edge_pos = s->v_edge_pos >> field_based;
  460. linesize = s->linesize << field_based;
  461. uvlinesize = s->uvlinesize << field_based;
  462. if (field_based) {
  463. mx = motion_x / 2;
  464. my = motion_y >> 1;
  465. } else if (s->workaround_bugs & FF_BUG_QPEL_CHROMA2) {
  466. static const int rtab[8] = { 0, 0, 1, 1, 0, 0, 0, 1 };
  467. mx = (motion_x >> 1) + rtab[motion_x & 7];
  468. my = (motion_y >> 1) + rtab[motion_y & 7];
  469. } else if (s->workaround_bugs & FF_BUG_QPEL_CHROMA) {
  470. mx = (motion_x >> 1) | (motion_x & 1);
  471. my = (motion_y >> 1) | (motion_y & 1);
  472. } else {
  473. mx = motion_x / 2;
  474. my = motion_y / 2;
  475. }
  476. mx = (mx >> 1) | (mx & 1);
  477. my = (my >> 1) | (my & 1);
  478. uvdxy = (mx & 1) | ((my & 1) << 1);
  479. mx >>= 1;
  480. my >>= 1;
  481. uvsrc_x = s->mb_x * 8 + mx;
  482. uvsrc_y = s->mb_y * (8 >> field_based) + my;
  483. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  484. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  485. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  486. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 3) - 16, 0) ||
  487. (unsigned)src_y > FFMAX(v_edge_pos - (motion_y & 3) - h, 0)) {
  488. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  489. s->linesize, s->linesize,
  490. 17, 17 + field_based,
  491. src_x, src_y << field_based,
  492. s->h_edge_pos, s->v_edge_pos);
  493. ptr_y = s->edge_emu_buffer;
  494. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  495. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  496. s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
  497. s->uvlinesize, s->uvlinesize,
  498. 9, 9 + field_based,
  499. uvsrc_x, uvsrc_y << field_based,
  500. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  501. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
  502. s->uvlinesize, s->uvlinesize,
  503. 9, 9 + field_based,
  504. uvsrc_x, uvsrc_y << field_based,
  505. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  506. ptr_cb = uvbuf;
  507. ptr_cr = uvbuf + 16;
  508. }
  509. }
  510. if (!field_based)
  511. qpix_op[0][dxy](dest_y, ptr_y, linesize);
  512. else {
  513. if (bottom_field) {
  514. dest_y += s->linesize;
  515. dest_cb += s->uvlinesize;
  516. dest_cr += s->uvlinesize;
  517. }
  518. if (field_select) {
  519. ptr_y += s->linesize;
  520. ptr_cb += s->uvlinesize;
  521. ptr_cr += s->uvlinesize;
  522. }
  523. // damn interlaced mode
  524. // FIXME boundary mirroring is not exactly correct here
  525. qpix_op[1][dxy](dest_y, ptr_y, linesize);
  526. qpix_op[1][dxy](dest_y + 8, ptr_y + 8, linesize);
  527. }
  528. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  529. pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
  530. pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
  531. }
  532. }
  533. /**
  534. * h263 chroma 4mv motion compensation.
  535. */
  536. static void chroma_4mv_motion(MpegEncContext *s,
  537. uint8_t *dest_cb, uint8_t *dest_cr,
  538. uint8_t **ref_picture,
  539. op_pixels_func *pix_op,
  540. int mx, int my)
  541. {
  542. uint8_t *ptr;
  543. int src_x, src_y, dxy, emu = 0;
  544. ptrdiff_t offset;
  545. /* In case of 8X8, we construct a single chroma motion vector
  546. * with a special rounding */
  547. mx = ff_h263_round_chroma(mx);
  548. my = ff_h263_round_chroma(my);
  549. dxy = ((my & 1) << 1) | (mx & 1);
  550. mx >>= 1;
  551. my >>= 1;
  552. src_x = s->mb_x * 8 + mx;
  553. src_y = s->mb_y * 8 + my;
  554. src_x = av_clip(src_x, -8, (s->width >> 1));
  555. if (src_x == (s->width >> 1))
  556. dxy &= ~1;
  557. src_y = av_clip(src_y, -8, (s->height >> 1));
  558. if (src_y == (s->height >> 1))
  559. dxy &= ~2;
  560. offset = src_y * s->uvlinesize + src_x;
  561. ptr = ref_picture[1] + offset;
  562. if ((unsigned)src_x > FFMAX((s->h_edge_pos >> 1) - (dxy & 1) - 8, 0) ||
  563. (unsigned)src_y > FFMAX((s->v_edge_pos >> 1) - (dxy >> 1) - 8, 0)) {
  564. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  565. s->uvlinesize, s->uvlinesize,
  566. 9, 9, src_x, src_y,
  567. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  568. ptr = s->edge_emu_buffer;
  569. emu = 1;
  570. }
  571. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  572. ptr = ref_picture[2] + offset;
  573. if (emu) {
  574. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  575. s->uvlinesize, s->uvlinesize,
  576. 9, 9, src_x, src_y,
  577. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  578. ptr = s->edge_emu_buffer;
  579. }
  580. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  581. }
  582. static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir)
  583. {
  584. /* fetch pixels for estimated mv 4 macroblocks ahead
  585. * optimized for 64byte cache lines */
  586. const int shift = s->quarter_sample ? 2 : 1;
  587. const int mx = (s->mv[dir][0][0] >> shift) + 16 * s->mb_x + 8;
  588. const int my = (s->mv[dir][0][1] >> shift) + 16 * s->mb_y;
  589. int off = mx + (my + (s->mb_x & 3) * 4) * s->linesize + 64;
  590. s->vdsp.prefetch(pix[0] + off, s->linesize, 4);
  591. off = (mx >> 1) + ((my >> 1) + (s->mb_x & 7)) * s->uvlinesize + 64;
  592. s->vdsp.prefetch(pix[1] + off, pix[2] - pix[1], 2);
  593. }
  594. static inline void apply_obmc(MpegEncContext *s,
  595. uint8_t *dest_y,
  596. uint8_t *dest_cb,
  597. uint8_t *dest_cr,
  598. uint8_t **ref_picture,
  599. op_pixels_func (*pix_op)[4])
  600. {
  601. LOCAL_ALIGNED_8(int16_t, mv_cache, [4], [4][2]);
  602. Picture *cur_frame = &s->current_picture;
  603. int mb_x = s->mb_x;
  604. int mb_y = s->mb_y;
  605. const int xy = mb_x + mb_y * s->mb_stride;
  606. const int mot_stride = s->b8_stride;
  607. const int mot_xy = mb_x * 2 + mb_y * 2 * mot_stride;
  608. int mx, my, i;
  609. assert(!s->mb_skipped);
  610. AV_COPY32(mv_cache[1][1], cur_frame->motion_val[0][mot_xy]);
  611. AV_COPY32(mv_cache[1][2], cur_frame->motion_val[0][mot_xy + 1]);
  612. AV_COPY32(mv_cache[2][1],
  613. cur_frame->motion_val[0][mot_xy + mot_stride]);
  614. AV_COPY32(mv_cache[2][2],
  615. cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
  616. AV_COPY32(mv_cache[3][1],
  617. cur_frame->motion_val[0][mot_xy + mot_stride]);
  618. AV_COPY32(mv_cache[3][2],
  619. cur_frame->motion_val[0][mot_xy + mot_stride + 1]);
  620. if (mb_y == 0 || IS_INTRA(cur_frame->mb_type[xy - s->mb_stride])) {
  621. AV_COPY32(mv_cache[0][1], mv_cache[1][1]);
  622. AV_COPY32(mv_cache[0][2], mv_cache[1][2]);
  623. } else {
  624. AV_COPY32(mv_cache[0][1],
  625. cur_frame->motion_val[0][mot_xy - mot_stride]);
  626. AV_COPY32(mv_cache[0][2],
  627. cur_frame->motion_val[0][mot_xy - mot_stride + 1]);
  628. }
  629. if (mb_x == 0 || IS_INTRA(cur_frame->mb_type[xy - 1])) {
  630. AV_COPY32(mv_cache[1][0], mv_cache[1][1]);
  631. AV_COPY32(mv_cache[2][0], mv_cache[2][1]);
  632. } else {
  633. AV_COPY32(mv_cache[1][0], cur_frame->motion_val[0][mot_xy - 1]);
  634. AV_COPY32(mv_cache[2][0],
  635. cur_frame->motion_val[0][mot_xy - 1 + mot_stride]);
  636. }
  637. if (mb_x + 1 >= s->mb_width || IS_INTRA(cur_frame->mb_type[xy + 1])) {
  638. AV_COPY32(mv_cache[1][3], mv_cache[1][2]);
  639. AV_COPY32(mv_cache[2][3], mv_cache[2][2]);
  640. } else {
  641. AV_COPY32(mv_cache[1][3], cur_frame->motion_val[0][mot_xy + 2]);
  642. AV_COPY32(mv_cache[2][3],
  643. cur_frame->motion_val[0][mot_xy + 2 + mot_stride]);
  644. }
  645. mx = 0;
  646. my = 0;
  647. for (i = 0; i < 4; i++) {
  648. const int x = (i & 1) + 1;
  649. const int y = (i >> 1) + 1;
  650. int16_t mv[5][2] = {
  651. { mv_cache[y][x][0], mv_cache[y][x][1] },
  652. { mv_cache[y - 1][x][0], mv_cache[y - 1][x][1] },
  653. { mv_cache[y][x - 1][0], mv_cache[y][x - 1][1] },
  654. { mv_cache[y][x + 1][0], mv_cache[y][x + 1][1] },
  655. { mv_cache[y + 1][x][0], mv_cache[y + 1][x][1] }
  656. };
  657. // FIXME cleanup
  658. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  659. ref_picture[0],
  660. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >> 1) * 8,
  661. pix_op[1],
  662. mv);
  663. mx += mv[0][0];
  664. my += mv[0][1];
  665. }
  666. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY))
  667. chroma_4mv_motion(s, dest_cb, dest_cr,
  668. ref_picture, pix_op[1],
  669. mx, my);
  670. }
  671. static inline void apply_8x8(MpegEncContext *s,
  672. uint8_t *dest_y,
  673. uint8_t *dest_cb,
  674. uint8_t *dest_cr,
  675. int dir,
  676. uint8_t **ref_picture,
  677. qpel_mc_func (*qpix_op)[16],
  678. op_pixels_func (*pix_op)[4])
  679. {
  680. int dxy, mx, my, src_x, src_y;
  681. int i;
  682. int mb_x = s->mb_x;
  683. int mb_y = s->mb_y;
  684. uint8_t *ptr, *dest;
  685. mx = 0;
  686. my = 0;
  687. if (s->quarter_sample) {
  688. for (i = 0; i < 4; i++) {
  689. int motion_x = s->mv[dir][i][0];
  690. int motion_y = s->mv[dir][i][1];
  691. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  692. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  693. src_y = mb_y * 16 + (motion_y >> 2) + (i >> 1) * 8;
  694. /* WARNING: do no forget half pels */
  695. src_x = av_clip(src_x, -16, s->width);
  696. if (src_x == s->width)
  697. dxy &= ~3;
  698. src_y = av_clip(src_y, -16, s->height);
  699. if (src_y == s->height)
  700. dxy &= ~12;
  701. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  702. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 3) - 8, 0) ||
  703. (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y & 3) - 8, 0)) {
  704. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  705. s->linesize, s->linesize,
  706. 9, 9,
  707. src_x, src_y,
  708. s->h_edge_pos,
  709. s->v_edge_pos);
  710. ptr = s->edge_emu_buffer;
  711. }
  712. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  713. qpix_op[1][dxy](dest, ptr, s->linesize);
  714. mx += s->mv[dir][i][0] / 2;
  715. my += s->mv[dir][i][1] / 2;
  716. }
  717. } else {
  718. for (i = 0; i < 4; i++) {
  719. hpel_motion(s,
  720. dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  721. ref_picture[0],
  722. mb_x * 16 + (i & 1) * 8,
  723. mb_y * 16 + (i >> 1) * 8,
  724. pix_op[1],
  725. s->mv[dir][i][0],
  726. s->mv[dir][i][1]);
  727. mx += s->mv[dir][i][0];
  728. my += s->mv[dir][i][1];
  729. }
  730. }
  731. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY))
  732. chroma_4mv_motion(s, dest_cb, dest_cr,
  733. ref_picture, pix_op[1], mx, my);
  734. }
  735. /**
  736. * motion compensation of a single macroblock
  737. * @param s context
  738. * @param dest_y luma destination pointer
  739. * @param dest_cb chroma cb/u destination pointer
  740. * @param dest_cr chroma cr/v destination pointer
  741. * @param dir direction (0->forward, 1->backward)
  742. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  743. * @param pix_op halfpel motion compensation function (average or put normally)
  744. * @param qpix_op qpel motion compensation function (average or put normally)
  745. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  746. */
  747. static av_always_inline void mpv_motion_internal(MpegEncContext *s,
  748. uint8_t *dest_y,
  749. uint8_t *dest_cb,
  750. uint8_t *dest_cr,
  751. int dir,
  752. uint8_t **ref_picture,
  753. op_pixels_func (*pix_op)[4],
  754. qpel_mc_func (*qpix_op)[16],
  755. int is_mpeg12)
  756. {
  757. int i;
  758. int mb_y = s->mb_y;
  759. prefetch_motion(s, ref_picture, dir);
  760. if (!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B) {
  761. apply_obmc(s, dest_y, dest_cb, dest_cr, ref_picture, pix_op);
  762. return;
  763. }
  764. switch (s->mv_type) {
  765. case MV_TYPE_16X16:
  766. if (s->mcsel) {
  767. if (s->real_sprite_warping_points == 1) {
  768. gmc1_motion(s, dest_y, dest_cb, dest_cr,
  769. ref_picture);
  770. } else {
  771. gmc_motion(s, dest_y, dest_cb, dest_cr,
  772. ref_picture);
  773. }
  774. } else if (!is_mpeg12 && s->quarter_sample) {
  775. qpel_motion(s, dest_y, dest_cb, dest_cr,
  776. 0, 0, 0,
  777. ref_picture, pix_op, qpix_op,
  778. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  779. } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
  780. s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
  781. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  782. ref_picture, pix_op,
  783. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  784. } else {
  785. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  786. ref_picture, pix_op,
  787. s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
  788. }
  789. break;
  790. case MV_TYPE_8X8:
  791. if (!is_mpeg12)
  792. apply_8x8(s, dest_y, dest_cb, dest_cr,
  793. dir, ref_picture, qpix_op, pix_op);
  794. break;
  795. case MV_TYPE_FIELD:
  796. if (s->picture_structure == PICT_FRAME) {
  797. if (!is_mpeg12 && s->quarter_sample) {
  798. for (i = 0; i < 2; i++)
  799. qpel_motion(s, dest_y, dest_cb, dest_cr,
  800. 1, i, s->field_select[dir][i],
  801. ref_picture, pix_op, qpix_op,
  802. s->mv[dir][i][0], s->mv[dir][i][1], 8);
  803. } else {
  804. /* top field */
  805. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  806. 0, s->field_select[dir][0],
  807. ref_picture, pix_op,
  808. s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
  809. /* bottom field */
  810. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  811. 1, s->field_select[dir][1],
  812. ref_picture, pix_op,
  813. s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
  814. }
  815. } else {
  816. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  817. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  818. ref_picture = s->current_picture_ptr->f->data;
  819. }
  820. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  821. s->field_select[dir][0],
  822. ref_picture, pix_op,
  823. s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y >> 1);
  824. }
  825. break;
  826. case MV_TYPE_16X8:
  827. for (i = 0; i < 2; i++) {
  828. uint8_t **ref2picture;
  829. if (s->picture_structure == s->field_select[dir][i] + 1
  830. || s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  831. ref2picture = ref_picture;
  832. } else {
  833. ref2picture = s->current_picture_ptr->f->data;
  834. }
  835. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  836. s->field_select[dir][i],
  837. ref2picture, pix_op,
  838. s->mv[dir][i][0], s->mv[dir][i][1] + 16 * i,
  839. 8, mb_y >> 1);
  840. dest_y += 16 * s->linesize;
  841. dest_cb += (16 >> s->chroma_y_shift) * s->uvlinesize;
  842. dest_cr += (16 >> s->chroma_y_shift) * s->uvlinesize;
  843. }
  844. break;
  845. case MV_TYPE_DMV:
  846. if (s->picture_structure == PICT_FRAME) {
  847. for (i = 0; i < 2; i++) {
  848. int j;
  849. for (j = 0; j < 2; j++)
  850. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  851. j, j ^ i, ref_picture, pix_op,
  852. s->mv[dir][2 * i + j][0],
  853. s->mv[dir][2 * i + j][1], 8, mb_y);
  854. pix_op = s->hdsp.avg_pixels_tab;
  855. }
  856. } else {
  857. for (i = 0; i < 2; i++) {
  858. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  859. s->picture_structure != i + 1,
  860. ref_picture, pix_op,
  861. s->mv[dir][2 * i][0], s->mv[dir][2 * i][1],
  862. 16, mb_y >> 1);
  863. // after put we make avg of the same block
  864. pix_op = s->hdsp.avg_pixels_tab;
  865. /* opposite parity is always in the same frame if this is
  866. * second field */
  867. if (!s->first_field) {
  868. ref_picture = s->current_picture_ptr->f->data;
  869. }
  870. }
  871. }
  872. break;
  873. default: assert(0);
  874. }
  875. }
  876. void ff_mpv_motion(MpegEncContext *s,
  877. uint8_t *dest_y, uint8_t *dest_cb,
  878. uint8_t *dest_cr, int dir,
  879. uint8_t **ref_picture,
  880. op_pixels_func (*pix_op)[4],
  881. qpel_mc_func (*qpix_op)[16])
  882. {
  883. #if !CONFIG_SMALL
  884. if (s->out_format == FMT_MPEG1)
  885. mpv_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
  886. ref_picture, pix_op, qpix_op, 1);
  887. else
  888. #endif
  889. mpv_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
  890. ref_picture, pix_op, qpix_op, 0);
  891. }