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.

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