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.

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