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.

979 lines
36KB

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