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 Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <string.h>
  24. #include "libavutil/internal.h"
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #include "h261.h"
  28. #include "mpegvideo.h"
  29. #include "mjpegenc.h"
  30. #include "msmpeg4.h"
  31. #include <limits.h>
  32. static void gmc1_motion(MpegEncContext *s,
  33. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  34. uint8_t **ref_picture)
  35. {
  36. uint8_t *ptr;
  37. int src_x, src_y, motion_x, motion_y;
  38. ptrdiff_t offset, linesize, uvlinesize;
  39. int emu = 0;
  40. motion_x = s->sprite_offset[0][0];
  41. motion_y = s->sprite_offset[0][1];
  42. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy + 1));
  43. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy + 1));
  44. motion_x <<= (3 - s->sprite_warping_accuracy);
  45. motion_y <<= (3 - s->sprite_warping_accuracy);
  46. src_x = av_clip(src_x, -16, s->width);
  47. if (src_x == s->width)
  48. motion_x = 0;
  49. src_y = av_clip(src_y, -16, s->height);
  50. if (src_y == s->height)
  51. motion_y = 0;
  52. linesize = s->linesize;
  53. uvlinesize = s->uvlinesize;
  54. ptr = ref_picture[0] + src_y * linesize + src_x;
  55. if (s->flags & CODEC_FLAG_EMU_EDGE) {
  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,
  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. }
  66. if ((motion_x | motion_y) & 7) {
  67. s->dsp.gmc1(dest_y, ptr, linesize, 16,
  68. motion_x & 15, motion_y & 15, 128 - s->no_rounding);
  69. s->dsp.gmc1(dest_y + 8, ptr + 8, linesize, 16,
  70. motion_x & 15, motion_y & 15, 128 - s->no_rounding);
  71. } else {
  72. int dxy;
  73. dxy = ((motion_x >> 3) & 1) | ((motion_y >> 2) & 2);
  74. if (s->no_rounding) {
  75. s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  76. } else {
  77. s->hdsp.put_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  78. }
  79. }
  80. if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
  81. return;
  82. motion_x = s->sprite_offset[1][0];
  83. motion_y = s->sprite_offset[1][1];
  84. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy + 1));
  85. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy + 1));
  86. motion_x <<= (3 - s->sprite_warping_accuracy);
  87. motion_y <<= (3 - s->sprite_warping_accuracy);
  88. src_x = av_clip(src_x, -8, s->width >> 1);
  89. if (src_x == s->width >> 1)
  90. motion_x = 0;
  91. src_y = av_clip(src_y, -8, s->height >> 1);
  92. if (src_y == s->height >> 1)
  93. motion_y = 0;
  94. offset = (src_y * uvlinesize) + src_x;
  95. ptr = ref_picture[1] + offset;
  96. if (s->flags & CODEC_FLAG_EMU_EDGE) {
  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->edge_emu_buffer, ptr,
  100. uvlinesize,
  101. 9, 9,
  102. src_x, src_y,
  103. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  104. ptr = s->edge_emu_buffer;
  105. emu = 1;
  106. }
  107. }
  108. s->dsp.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->edge_emu_buffer, ptr,
  113. uvlinesize,
  114. 9, 9,
  115. src_x, src_y,
  116. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  117. ptr = s->edge_emu_buffer;
  118. }
  119. s->dsp.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->dsp.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->dsp.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->flags & 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->dsp.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, s->v_edge_pos >> 1);
  163. ptr = ref_picture[2];
  164. s->dsp.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, s->v_edge_pos >> 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 (s->unrestricted_mv && (s->flags & CODEC_FLAG_EMU_EDGE)) {
  190. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 1) - 8, 0) ||
  191. (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y & 1) - 8, 0)) {
  192. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  193. s->linesize,
  194. 9, 9,
  195. src_x, src_y, s->h_edge_pos,
  196. s->v_edge_pos);
  197. src = s->edge_emu_buffer;
  198. emu = 1;
  199. }
  200. }
  201. pix_op[dxy](dest, src, s->linesize, 8);
  202. return emu;
  203. }
  204. static av_always_inline
  205. void mpeg_motion_internal(MpegEncContext *s,
  206. uint8_t *dest_y,
  207. uint8_t *dest_cb,
  208. uint8_t *dest_cr,
  209. int field_based,
  210. int bottom_field,
  211. int field_select,
  212. uint8_t **ref_picture,
  213. op_pixels_func (*pix_op)[4],
  214. int motion_x,
  215. int motion_y,
  216. int h,
  217. int is_mpeg12,
  218. int mb_y)
  219. {
  220. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  221. int dxy, uvdxy, mx, my, src_x, src_y,
  222. uvsrc_x, uvsrc_y, v_edge_pos;
  223. ptrdiff_t uvlinesize, linesize;
  224. #if 0
  225. if (s->quarter_sample) {
  226. motion_x >>= 1;
  227. motion_y >>= 1;
  228. }
  229. #endif
  230. v_edge_pos = s->v_edge_pos >> field_based;
  231. linesize = s->current_picture.f.linesize[0] << field_based;
  232. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  233. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  234. src_x = s->mb_x * 16 + (motion_x >> 1);
  235. src_y = (mb_y << (4 - field_based)) + (motion_y >> 1);
  236. if (!is_mpeg12 && s->out_format == FMT_H263) {
  237. if ((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based) {
  238. mx = (motion_x >> 1) | (motion_x & 1);
  239. my = motion_y >> 1;
  240. uvdxy = ((my & 1) << 1) | (mx & 1);
  241. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  242. uvsrc_y = (mb_y << (3 - field_based)) + (my >> 1);
  243. } else {
  244. uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
  245. uvsrc_x = src_x >> 1;
  246. uvsrc_y = src_y >> 1;
  247. }
  248. // Even chroma mv's are full pel in H261
  249. } else if (!is_mpeg12 && s->out_format == FMT_H261) {
  250. mx = motion_x / 4;
  251. my = motion_y / 4;
  252. uvdxy = 0;
  253. uvsrc_x = s->mb_x * 8 + mx;
  254. uvsrc_y = mb_y * 8 + my;
  255. } else {
  256. if (s->chroma_y_shift) {
  257. mx = motion_x / 2;
  258. my = motion_y / 2;
  259. uvdxy = ((my & 1) << 1) | (mx & 1);
  260. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  261. uvsrc_y = (mb_y << (3 - field_based)) + (my >> 1);
  262. } else {
  263. if (s->chroma_x_shift) {
  264. // Chroma422
  265. mx = motion_x / 2;
  266. uvdxy = ((motion_y & 1) << 1) | (mx & 1);
  267. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  268. uvsrc_y = src_y;
  269. } else {
  270. // Chroma444
  271. uvdxy = dxy;
  272. uvsrc_x = src_x;
  273. uvsrc_y = src_y;
  274. }
  275. }
  276. }
  277. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  278. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  279. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  280. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 1) - 16, 0) ||
  281. (unsigned)src_y > FFMAX(v_edge_pos - (motion_y & 1) - h, 0)) {
  282. if (is_mpeg12 ||
  283. s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
  284. s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  285. av_log(s->avctx, AV_LOG_DEBUG,
  286. "MPEG motion vector out of boundary (%d %d)\n", src_x,
  287. src_y);
  288. return;
  289. }
  290. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  291. s->linesize,
  292. 17, 17 + field_based,
  293. src_x, src_y << field_based,
  294. s->h_edge_pos, s->v_edge_pos);
  295. ptr_y = s->edge_emu_buffer;
  296. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  297. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  298. s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
  299. 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. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
  304. s->uvlinesize,
  305. 9, 9 + field_based,
  306. uvsrc_x, uvsrc_y << field_based,
  307. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  308. ptr_cb = uvbuf;
  309. ptr_cr = uvbuf + 16;
  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->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 move to dsputil, 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. assert(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->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) - 16, 0) ||
  490. (unsigned)src_y > FFMAX(v_edge_pos - (motion_y & 3) - h, 0)) {
  491. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  492. 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->edge_emu_buffer;
  497. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  498. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  499. s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
  500. 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. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
  505. s->uvlinesize,
  506. 9, 9 + field_based,
  507. uvsrc_x, uvsrc_y << field_based,
  508. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  509. ptr_cb = uvbuf;
  510. ptr_cr = uvbuf + 16;
  511. }
  512. }
  513. if (!field_based)
  514. qpix_op[0][dxy](dest_y, ptr_y, linesize);
  515. else {
  516. if (bottom_field) {
  517. dest_y += s->linesize;
  518. dest_cb += s->uvlinesize;
  519. dest_cr += s->uvlinesize;
  520. }
  521. if (field_select) {
  522. ptr_y += s->linesize;
  523. ptr_cb += s->uvlinesize;
  524. ptr_cr += s->uvlinesize;
  525. }
  526. // damn interlaced mode
  527. // FIXME boundary mirroring is not exactly correct here
  528. qpix_op[1][dxy](dest_y, ptr_y, linesize);
  529. qpix_op[1][dxy](dest_y + 8, ptr_y + 8, linesize);
  530. }
  531. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  532. pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
  533. pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
  534. }
  535. }
  536. /**
  537. * h263 chroma 4mv motion compensation.
  538. */
  539. static void chroma_4mv_motion(MpegEncContext *s,
  540. uint8_t *dest_cb, uint8_t *dest_cr,
  541. uint8_t **ref_picture,
  542. op_pixels_func *pix_op,
  543. int mx, int my)
  544. {
  545. uint8_t *ptr;
  546. int src_x, src_y, dxy, emu = 0;
  547. ptrdiff_t offset;
  548. /* In case of 8X8, we construct a single chroma motion vector
  549. * with a special rounding */
  550. mx = ff_h263_round_chroma(mx);
  551. my = ff_h263_round_chroma(my);
  552. dxy = ((my & 1) << 1) | (mx & 1);
  553. mx >>= 1;
  554. my >>= 1;
  555. src_x = s->mb_x * 8 + mx;
  556. src_y = s->mb_y * 8 + my;
  557. src_x = av_clip(src_x, -8, (s->width >> 1));
  558. if (src_x == (s->width >> 1))
  559. dxy &= ~1;
  560. src_y = av_clip(src_y, -8, (s->height >> 1));
  561. if (src_y == (s->height >> 1))
  562. dxy &= ~2;
  563. offset = src_y * s->uvlinesize + src_x;
  564. ptr = ref_picture[1] + offset;
  565. if (s->flags & CODEC_FLAG_EMU_EDGE) {
  566. if ((unsigned)src_x > FFMAX((s->h_edge_pos >> 1) - (dxy & 1) - 8, 0) ||
  567. (unsigned)src_y > FFMAX((s->v_edge_pos >> 1) - (dxy >> 1) - 8, 0)) {
  568. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  569. ptr, s->uvlinesize,
  570. 9, 9, src_x, src_y,
  571. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  572. ptr = s->edge_emu_buffer;
  573. emu = 1;
  574. }
  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->edge_emu_buffer,
  580. ptr, s->uvlinesize,
  581. 9, 9, src_x, src_y,
  582. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  583. ptr = s->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. assert(!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->flags & 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 (s->flags & CODEC_FLAG_EMU_EDGE) {
  708. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 3) - 8, 0) ||
  709. (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y & 3) - 8, 0)) {
  710. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  711. s->linesize,
  712. 9, 9,
  713. src_x, src_y,
  714. s->h_edge_pos,
  715. s->v_edge_pos);
  716. ptr = s->edge_emu_buffer;
  717. }
  718. }
  719. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  720. qpix_op[1][dxy](dest, ptr, s->linesize);
  721. mx += s->mv[dir][i][0] / 2;
  722. my += s->mv[dir][i][1] / 2;
  723. }
  724. } else {
  725. for (i = 0; i < 4; i++) {
  726. hpel_motion(s,
  727. dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  728. ref_picture[0],
  729. mb_x * 16 + (i & 1) * 8,
  730. mb_y * 16 + (i >> 1) * 8,
  731. pix_op[1],
  732. s->mv[dir][i][0],
  733. s->mv[dir][i][1]);
  734. mx += s->mv[dir][i][0];
  735. my += s->mv[dir][i][1];
  736. }
  737. }
  738. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  739. chroma_4mv_motion(s, dest_cb, dest_cr,
  740. ref_picture, pix_op[1], mx, my);
  741. }
  742. /**
  743. * motion compensation of a single macroblock
  744. * @param s context
  745. * @param dest_y luma destination pointer
  746. * @param dest_cb chroma cb/u destination pointer
  747. * @param dest_cr chroma cr/v destination pointer
  748. * @param dir direction (0->forward, 1->backward)
  749. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  750. * @param pix_op halfpel motion compensation function (average or put normally)
  751. * @param qpix_op qpel motion compensation function (average or put normally)
  752. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  753. */
  754. static av_always_inline void MPV_motion_internal(MpegEncContext *s,
  755. uint8_t *dest_y,
  756. uint8_t *dest_cb,
  757. uint8_t *dest_cr,
  758. int dir,
  759. uint8_t **ref_picture,
  760. op_pixels_func (*pix_op)[4],
  761. qpel_mc_func (*qpix_op)[16],
  762. int is_mpeg12)
  763. {
  764. int i;
  765. int mb_y = s->mb_y;
  766. prefetch_motion(s, ref_picture, dir);
  767. if (!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B) {
  768. apply_obmc(s, dest_y, dest_cb, dest_cr, ref_picture, pix_op);
  769. return;
  770. }
  771. switch (s->mv_type) {
  772. case MV_TYPE_16X16:
  773. if (s->mcsel) {
  774. if (s->real_sprite_warping_points == 1) {
  775. gmc1_motion(s, dest_y, dest_cb, dest_cr,
  776. ref_picture);
  777. } else {
  778. gmc_motion(s, dest_y, dest_cb, dest_cr,
  779. ref_picture);
  780. }
  781. } else if (!is_mpeg12 && s->quarter_sample) {
  782. qpel_motion(s, dest_y, dest_cb, dest_cr,
  783. 0, 0, 0,
  784. ref_picture, pix_op, qpix_op,
  785. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  786. } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
  787. s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
  788. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  789. ref_picture, pix_op,
  790. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  791. } else {
  792. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  793. ref_picture, pix_op,
  794. s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
  795. }
  796. break;
  797. case MV_TYPE_8X8:
  798. if (!is_mpeg12)
  799. apply_8x8(s, dest_y, dest_cb, dest_cr,
  800. dir, ref_picture, qpix_op, pix_op);
  801. break;
  802. case MV_TYPE_FIELD:
  803. if (s->picture_structure == PICT_FRAME) {
  804. if (!is_mpeg12 && s->quarter_sample) {
  805. for (i = 0; i < 2; i++)
  806. qpel_motion(s, dest_y, dest_cb, dest_cr,
  807. 1, i, s->field_select[dir][i],
  808. ref_picture, pix_op, qpix_op,
  809. s->mv[dir][i][0], s->mv[dir][i][1], 8);
  810. } else {
  811. /* top field */
  812. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  813. 0, s->field_select[dir][0],
  814. ref_picture, pix_op,
  815. s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
  816. /* bottom field */
  817. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  818. 1, s->field_select[dir][1],
  819. ref_picture, pix_op,
  820. s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
  821. }
  822. } else {
  823. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  824. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  825. ref_picture = s->current_picture_ptr->f.data;
  826. }
  827. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  828. s->field_select[dir][0],
  829. ref_picture, pix_op,
  830. s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y >> 1);
  831. }
  832. break;
  833. case MV_TYPE_16X8:
  834. for (i = 0; i < 2; i++) {
  835. uint8_t **ref2picture;
  836. if (s->picture_structure == s->field_select[dir][i] + 1
  837. || s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  838. ref2picture = ref_picture;
  839. } else {
  840. ref2picture = s->current_picture_ptr->f.data;
  841. }
  842. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  843. s->field_select[dir][i],
  844. ref2picture, pix_op,
  845. s->mv[dir][i][0], s->mv[dir][i][1] + 16 * i,
  846. 8, mb_y >> 1);
  847. dest_y += 16 * s->linesize;
  848. dest_cb += (16 >> s->chroma_y_shift) * s->uvlinesize;
  849. dest_cr += (16 >> s->chroma_y_shift) * s->uvlinesize;
  850. }
  851. break;
  852. case MV_TYPE_DMV:
  853. if (s->picture_structure == PICT_FRAME) {
  854. for (i = 0; i < 2; i++) {
  855. int j;
  856. for (j = 0; j < 2; j++)
  857. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  858. j, j ^ i, ref_picture, pix_op,
  859. s->mv[dir][2 * i + j][0],
  860. s->mv[dir][2 * i + j][1], 8, mb_y);
  861. pix_op = s->hdsp.avg_pixels_tab;
  862. }
  863. } else {
  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: assert(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. }