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.

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