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.

992 lines
38KB

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