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.

991 lines
37KB

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