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.

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