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.

995 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 "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, linesize, 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. 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, uvlinesize,
  98. ptr, 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, uvlinesize,
  110. ptr, 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 (s->unrestricted_mv && (s->flags & CODEC_FLAG_EMU_EDGE)) {
  187. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 1) - 8, 0) ||
  188. (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y & 1) - 8, 0)) {
  189. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, s->linesize, src,
  190. s->linesize,
  191. 9, 9,
  192. src_x, src_y,
  193. s->h_edge_pos, s->v_edge_pos);
  194. src = s->edge_emu_buffer;
  195. emu = 1;
  196. }
  197. }
  198. pix_op[dxy](dest, src, s->linesize, 8);
  199. return emu;
  200. }
  201. static av_always_inline
  202. void mpeg_motion_internal(MpegEncContext *s,
  203. uint8_t *dest_y,
  204. uint8_t *dest_cb,
  205. uint8_t *dest_cr,
  206. int field_based,
  207. int bottom_field,
  208. int field_select,
  209. uint8_t **ref_picture,
  210. op_pixels_func (*pix_op)[4],
  211. int motion_x,
  212. int motion_y,
  213. int h,
  214. int is_mpeg12,
  215. int mb_y)
  216. {
  217. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  218. int dxy, uvdxy, mx, my, src_x, src_y,
  219. uvsrc_x, uvsrc_y, v_edge_pos;
  220. ptrdiff_t uvlinesize, linesize;
  221. #if 0
  222. if (s->quarter_sample) {
  223. motion_x >>= 1;
  224. motion_y >>= 1;
  225. }
  226. #endif
  227. v_edge_pos = s->v_edge_pos >> field_based;
  228. linesize = s->current_picture.f.linesize[0] << field_based;
  229. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  230. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  231. src_x = s->mb_x * 16 + (motion_x >> 1);
  232. src_y = (mb_y << (4 - field_based)) + (motion_y >> 1);
  233. if (!is_mpeg12 && s->out_format == FMT_H263) {
  234. if ((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based) {
  235. mx = (motion_x >> 1) | (motion_x & 1);
  236. my = motion_y >> 1;
  237. uvdxy = ((my & 1) << 1) | (mx & 1);
  238. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  239. uvsrc_y = (mb_y << (3 - field_based)) + (my >> 1);
  240. } else {
  241. uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
  242. uvsrc_x = src_x >> 1;
  243. uvsrc_y = src_y >> 1;
  244. }
  245. // Even chroma mv's are full pel in H261
  246. } else if (!is_mpeg12 && s->out_format == FMT_H261) {
  247. mx = motion_x / 4;
  248. my = motion_y / 4;
  249. uvdxy = 0;
  250. uvsrc_x = s->mb_x * 8 + mx;
  251. uvsrc_y = mb_y * 8 + my;
  252. } else {
  253. if (s->chroma_y_shift) {
  254. mx = motion_x / 2;
  255. my = motion_y / 2;
  256. uvdxy = ((my & 1) << 1) | (mx & 1);
  257. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  258. uvsrc_y = (mb_y << (3 - field_based)) + (my >> 1);
  259. } else {
  260. if (s->chroma_x_shift) {
  261. // Chroma422
  262. mx = motion_x / 2;
  263. uvdxy = ((motion_y & 1) << 1) | (mx & 1);
  264. uvsrc_x = s->mb_x * 8 + (mx >> 1);
  265. uvsrc_y = src_y;
  266. } else {
  267. // Chroma444
  268. uvdxy = dxy;
  269. uvsrc_x = src_x;
  270. uvsrc_y = src_y;
  271. }
  272. }
  273. }
  274. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  275. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  276. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  277. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 1) - 16, 0) ||
  278. (unsigned)src_y > FFMAX( v_edge_pos - (motion_y & 1) - h , 0)) {
  279. if (is_mpeg12 ||
  280. s->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
  281. s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  282. av_log(s->avctx, AV_LOG_DEBUG,
  283. "MPEG motion vector out of boundary (%d %d)\n", src_x,
  284. src_y);
  285. return;
  286. }
  287. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, s->linesize, ptr_y,
  288. s->linesize,
  289. 17, 17 + field_based,
  290. src_x, src_y << field_based,
  291. s->h_edge_pos, s->v_edge_pos);
  292. ptr_y = s->edge_emu_buffer;
  293. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  294. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  295. s->vdsp.emulated_edge_mc(uvbuf, s->uvlinesize, ptr_cb,
  296. s->uvlinesize,
  297. 9, 9 + field_based,
  298. uvsrc_x, uvsrc_y << field_based,
  299. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  300. s->vdsp.emulated_edge_mc(uvbuf + 16, s->uvlinesize, ptr_cr,
  301. s->uvlinesize,
  302. 9, 9 + field_based,
  303. uvsrc_x, uvsrc_y << field_based,
  304. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  305. ptr_cb = uvbuf;
  306. ptr_cr = uvbuf + 16;
  307. }
  308. }
  309. /* FIXME use this for field pix too instead of the obnoxious hack which
  310. * changes picture.data */
  311. if (bottom_field) {
  312. dest_y += s->linesize;
  313. dest_cb += s->uvlinesize;
  314. dest_cr += s->uvlinesize;
  315. }
  316. if (field_select) {
  317. ptr_y += s->linesize;
  318. ptr_cb += s->uvlinesize;
  319. ptr_cr += s->uvlinesize;
  320. }
  321. pix_op[0][dxy](dest_y, ptr_y, linesize, h);
  322. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  323. pix_op[s->chroma_x_shift][uvdxy]
  324. (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
  325. pix_op[s->chroma_x_shift][uvdxy]
  326. (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
  327. }
  328. if (!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
  329. s->out_format == FMT_H261) {
  330. ff_h261_loop_filter(s);
  331. }
  332. }
  333. /* apply one mpeg motion vector to the three components */
  334. static void mpeg_motion(MpegEncContext *s,
  335. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  336. int field_select, uint8_t **ref_picture,
  337. op_pixels_func (*pix_op)[4],
  338. int motion_x, int motion_y, int h, int mb_y)
  339. {
  340. #if !CONFIG_SMALL
  341. if (s->out_format == FMT_MPEG1)
  342. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
  343. field_select, ref_picture, pix_op,
  344. motion_x, motion_y, h, 1, mb_y);
  345. else
  346. #endif
  347. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 0, 0,
  348. field_select, ref_picture, pix_op,
  349. motion_x, motion_y, h, 0, mb_y);
  350. }
  351. static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
  352. uint8_t *dest_cb, uint8_t *dest_cr,
  353. int bottom_field, int field_select,
  354. uint8_t **ref_picture,
  355. op_pixels_func (*pix_op)[4],
  356. int motion_x, int motion_y, int h, int mb_y)
  357. {
  358. #if !CONFIG_SMALL
  359. if (s->out_format == FMT_MPEG1)
  360. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
  361. bottom_field, field_select, ref_picture, pix_op,
  362. motion_x, motion_y, h, 1, mb_y);
  363. else
  364. #endif
  365. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, 1,
  366. bottom_field, field_select, ref_picture, pix_op,
  367. motion_x, motion_y, h, 0, mb_y);
  368. }
  369. // FIXME move to dsputil, avg variant, 16x16 version
  370. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride)
  371. {
  372. int x;
  373. uint8_t *const top = src[1];
  374. uint8_t *const left = src[2];
  375. uint8_t *const mid = src[0];
  376. uint8_t *const right = src[3];
  377. uint8_t *const bottom = src[4];
  378. #define OBMC_FILTER(x, t, l, m, r, b)\
  379. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  380. #define OBMC_FILTER4(x, t, l, m, r, b)\
  381. OBMC_FILTER(x , t, l, m, r, b);\
  382. OBMC_FILTER(x+1 , t, l, m, r, b);\
  383. OBMC_FILTER(x +stride, t, l, m, r, b);\
  384. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  385. x = 0;
  386. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  387. OBMC_FILTER (x + 1, 2, 1, 5, 0, 0);
  388. OBMC_FILTER4(x + 2, 2, 1, 5, 0, 0);
  389. OBMC_FILTER4(x + 4, 2, 0, 5, 1, 0);
  390. OBMC_FILTER (x + 6, 2, 0, 5, 1, 0);
  391. OBMC_FILTER (x + 7, 2, 0, 4, 2, 0);
  392. x += stride;
  393. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  394. OBMC_FILTER (x + 1, 1, 2, 5, 0, 0);
  395. OBMC_FILTER (x + 6, 1, 0, 5, 2, 0);
  396. OBMC_FILTER (x + 7, 1, 0, 5, 2, 0);
  397. x += stride;
  398. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  399. OBMC_FILTER4(x + 2, 1, 1, 6, 0, 0);
  400. OBMC_FILTER4(x + 4, 1, 0, 6, 1, 0);
  401. OBMC_FILTER4(x + 6, 1, 0, 5, 2, 0);
  402. x += 2 * stride;
  403. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  404. OBMC_FILTER4(x + 2, 0, 1, 6, 0, 1);
  405. OBMC_FILTER4(x + 4, 0, 0, 6, 1, 1);
  406. OBMC_FILTER4(x + 6, 0, 0, 5, 2, 1);
  407. x += 2*stride;
  408. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  409. OBMC_FILTER (x + 1, 0, 2, 5, 0, 1);
  410. OBMC_FILTER4(x + 2, 0, 1, 5, 0, 2);
  411. OBMC_FILTER4(x + 4, 0, 0, 5, 1, 2);
  412. OBMC_FILTER (x + 6, 0, 0, 5, 2, 1);
  413. OBMC_FILTER (x + 7, 0, 0, 5, 2, 1);
  414. x += stride;
  415. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  416. OBMC_FILTER (x + 1, 0, 1, 5, 0, 2);
  417. OBMC_FILTER (x + 6, 0, 0, 5, 1, 2);
  418. OBMC_FILTER (x + 7, 0, 0, 4, 2, 2);
  419. }
  420. /* obmc for 1 8x8 luma block */
  421. static inline void obmc_motion(MpegEncContext *s,
  422. uint8_t *dest, uint8_t *src,
  423. int src_x, int src_y,
  424. op_pixels_func *pix_op,
  425. int16_t mv[5][2] /* mid top left right bottom */)
  426. #define MID 0
  427. {
  428. int i;
  429. uint8_t *ptr[5];
  430. av_assert2(s->quarter_sample == 0);
  431. for (i = 0; i < 5; i++) {
  432. if (i && mv[i][0] == mv[MID][0] && mv[i][1] == mv[MID][1]) {
  433. ptr[i] = ptr[MID];
  434. } else {
  435. ptr[i] = s->obmc_scratchpad + 8 * (i & 1) +
  436. s->linesize * 8 * (i >> 1);
  437. hpel_motion(s, ptr[i], src, src_x, src_y, pix_op,
  438. mv[i][0], mv[i][1]);
  439. }
  440. }
  441. put_obmc(dest, ptr, s->linesize);
  442. }
  443. static inline void qpel_motion(MpegEncContext *s,
  444. uint8_t *dest_y,
  445. uint8_t *dest_cb,
  446. uint8_t *dest_cr,
  447. int field_based, int bottom_field,
  448. int field_select, uint8_t **ref_picture,
  449. op_pixels_func (*pix_op)[4],
  450. qpel_mc_func (*qpix_op)[16],
  451. int motion_x, int motion_y, int h)
  452. {
  453. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  454. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos;
  455. ptrdiff_t linesize, uvlinesize;
  456. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  457. src_x = s->mb_x * 16 + (motion_x >> 2);
  458. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  459. v_edge_pos = s->v_edge_pos >> field_based;
  460. linesize = s->linesize << field_based;
  461. uvlinesize = s->uvlinesize << field_based;
  462. if (field_based) {
  463. mx = motion_x / 2;
  464. my = motion_y >> 1;
  465. } else if (s->workaround_bugs & FF_BUG_QPEL_CHROMA2) {
  466. static const int rtab[8] = { 0, 0, 1, 1, 0, 0, 0, 1 };
  467. mx = (motion_x >> 1) + rtab[motion_x & 7];
  468. my = (motion_y >> 1) + rtab[motion_y & 7];
  469. } else if (s->workaround_bugs & FF_BUG_QPEL_CHROMA) {
  470. mx = (motion_x >> 1) | (motion_x & 1);
  471. my = (motion_y >> 1) | (motion_y & 1);
  472. } else {
  473. mx = motion_x / 2;
  474. my = motion_y / 2;
  475. }
  476. mx = (mx >> 1) | (mx & 1);
  477. my = (my >> 1) | (my & 1);
  478. uvdxy = (mx & 1) | ((my & 1) << 1);
  479. mx >>= 1;
  480. my >>= 1;
  481. uvsrc_x = s->mb_x * 8 + mx;
  482. uvsrc_y = s->mb_y * (8 >> field_based) + my;
  483. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  484. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  485. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  486. if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 3) - 16, 0) ||
  487. (unsigned)src_y > FFMAX( v_edge_pos - (motion_y & 3) - h, 0)) {
  488. s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
  489. s->linesize,
  490. ptr_y, s->linesize,
  491. 17, 17 + field_based,
  492. src_x, src_y << field_based,
  493. s->h_edge_pos, s->v_edge_pos);
  494. ptr_y = s->edge_emu_buffer;
  495. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  496. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  497. s->vdsp.emulated_edge_mc(uvbuf, s->uvlinesize,
  498. ptr_cb,
  499. s->uvlinesize,
  500. 9, 9 + field_based,
  501. uvsrc_x, uvsrc_y << field_based,
  502. s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  503. s->vdsp.emulated_edge_mc(uvbuf + 16, s->uvlinesize,
  504. 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, s->uvlinesize,
  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, s->uvlinesize,
  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. 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->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,
  711. s->linesize, ptr,
  712. s->linesize,
  713. 9, 9,
  714. src_x, src_y,
  715. s->h_edge_pos,
  716. s->v_edge_pos);
  717. ptr = s->edge_emu_buffer;
  718. }
  719. }
  720. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  721. qpix_op[1][dxy](dest, ptr, s->linesize);
  722. mx += s->mv[dir][i][0] / 2;
  723. my += s->mv[dir][i][1] / 2;
  724. }
  725. } else {
  726. for (i = 0; i < 4; i++) {
  727. hpel_motion(s,
  728. dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  729. ref_picture[0],
  730. mb_x * 16 + (i & 1) * 8,
  731. mb_y * 16 + (i >> 1) * 8,
  732. pix_op[1],
  733. s->mv[dir][i][0],
  734. s->mv[dir][i][1]);
  735. mx += s->mv[dir][i][0];
  736. my += s->mv[dir][i][1];
  737. }
  738. }
  739. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  740. chroma_4mv_motion(s, dest_cb, dest_cr,
  741. ref_picture, pix_op[1], mx, my);
  742. }
  743. /**
  744. * motion compensation of a single macroblock
  745. * @param s context
  746. * @param dest_y luma destination pointer
  747. * @param dest_cb chroma cb/u destination pointer
  748. * @param dest_cr chroma cr/v destination pointer
  749. * @param dir direction (0->forward, 1->backward)
  750. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  751. * @param pix_op halfpel motion compensation function (average or put normally)
  752. * @param qpix_op qpel motion compensation function (average or put normally)
  753. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  754. */
  755. static av_always_inline void MPV_motion_internal(MpegEncContext *s,
  756. uint8_t *dest_y,
  757. uint8_t *dest_cb,
  758. uint8_t *dest_cr,
  759. int dir,
  760. uint8_t **ref_picture,
  761. op_pixels_func (*pix_op)[4],
  762. qpel_mc_func (*qpix_op)[16],
  763. int is_mpeg12)
  764. {
  765. int i;
  766. int mb_y = s->mb_y;
  767. prefetch_motion(s, ref_picture, dir);
  768. if (!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B) {
  769. apply_obmc(s, dest_y, dest_cb, dest_cr, ref_picture, pix_op);
  770. return;
  771. }
  772. switch (s->mv_type) {
  773. case MV_TYPE_16X16:
  774. if (s->mcsel) {
  775. if (s->real_sprite_warping_points == 1) {
  776. gmc1_motion(s, dest_y, dest_cb, dest_cr,
  777. ref_picture);
  778. } else {
  779. gmc_motion(s, dest_y, dest_cb, dest_cr,
  780. ref_picture);
  781. }
  782. } else if (!is_mpeg12 && s->quarter_sample) {
  783. qpel_motion(s, dest_y, dest_cb, dest_cr,
  784. 0, 0, 0,
  785. ref_picture, pix_op, qpix_op,
  786. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  787. } else if (!is_mpeg12 && (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) &&
  788. s->mspel && s->codec_id == AV_CODEC_ID_WMV2) {
  789. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  790. ref_picture, pix_op,
  791. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  792. } else {
  793. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  794. ref_picture, pix_op,
  795. s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y);
  796. }
  797. break;
  798. case MV_TYPE_8X8:
  799. if (!is_mpeg12)
  800. apply_8x8(s, dest_y, dest_cb, dest_cr,
  801. dir, ref_picture, qpix_op, pix_op);
  802. break;
  803. case MV_TYPE_FIELD:
  804. if (s->picture_structure == PICT_FRAME) {
  805. if (!is_mpeg12 && s->quarter_sample) {
  806. for (i = 0; i < 2; i++)
  807. qpel_motion(s, dest_y, dest_cb, dest_cr,
  808. 1, i, s->field_select[dir][i],
  809. ref_picture, pix_op, qpix_op,
  810. s->mv[dir][i][0], s->mv[dir][i][1], 8);
  811. } else {
  812. /* top field */
  813. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  814. 0, s->field_select[dir][0],
  815. ref_picture, pix_op,
  816. s->mv[dir][0][0], s->mv[dir][0][1], 8, mb_y);
  817. /* bottom field */
  818. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  819. 1, s->field_select[dir][1],
  820. ref_picture, pix_op,
  821. s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
  822. }
  823. } else {
  824. if ( s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field
  825. || !ref_picture[0]) {
  826. ref_picture = s->current_picture_ptr->f.data;
  827. }
  828. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  829. s->field_select[dir][0],
  830. ref_picture, pix_op,
  831. s->mv[dir][0][0], s->mv[dir][0][1], 16, mb_y >> 1);
  832. }
  833. break;
  834. case MV_TYPE_16X8:
  835. for (i = 0; i < 2; i++) {
  836. uint8_t **ref2picture;
  837. if ((s->picture_structure == s->field_select[dir][i] + 1
  838. || s->pict_type == AV_PICTURE_TYPE_B || s->first_field) && ref_picture[0]) {
  839. ref2picture = ref_picture;
  840. } else {
  841. ref2picture = s->current_picture_ptr->f.data;
  842. }
  843. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  844. s->field_select[dir][i],
  845. ref2picture, pix_op,
  846. s->mv[dir][i][0], s->mv[dir][i][1] + 16 * i,
  847. 8, mb_y >> 1);
  848. dest_y += 16 * s->linesize;
  849. dest_cb += (16 >> s->chroma_y_shift) * s->uvlinesize;
  850. dest_cr += (16 >> s->chroma_y_shift) * s->uvlinesize;
  851. }
  852. break;
  853. case MV_TYPE_DMV:
  854. if (s->picture_structure == PICT_FRAME) {
  855. for (i = 0; i < 2; i++) {
  856. int j;
  857. for (j = 0; j < 2; j++)
  858. mpeg_motion_field(s, dest_y, dest_cb, dest_cr,
  859. j, j ^ i, ref_picture, pix_op,
  860. s->mv[dir][2 * i + j][0],
  861. s->mv[dir][2 * i + j][1], 8, mb_y);
  862. pix_op = s->hdsp.avg_pixels_tab;
  863. }
  864. } else {
  865. if (!ref_picture[0]) {
  866. ref_picture = s->current_picture_ptr->f.data;
  867. }
  868. for (i = 0; i < 2; i++) {
  869. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  870. s->picture_structure != i + 1,
  871. ref_picture, pix_op,
  872. s->mv[dir][2 * i][0], s->mv[dir][2 * i][1],
  873. 16, mb_y >> 1);
  874. // after put we make avg of the same block
  875. pix_op = s->hdsp.avg_pixels_tab;
  876. /* opposite parity is always in the same frame if this is
  877. * second field */
  878. if (!s->first_field) {
  879. ref_picture = s->current_picture_ptr->f.data;
  880. }
  881. }
  882. }
  883. break;
  884. default: av_assert2(0);
  885. }
  886. }
  887. void ff_MPV_motion(MpegEncContext *s,
  888. uint8_t *dest_y, uint8_t *dest_cb,
  889. uint8_t *dest_cr, int dir,
  890. uint8_t **ref_picture,
  891. op_pixels_func (*pix_op)[4],
  892. qpel_mc_func (*qpix_op)[16])
  893. {
  894. #if !CONFIG_SMALL
  895. if (s->out_format == FMT_MPEG1)
  896. MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
  897. ref_picture, pix_op, qpix_op, 1);
  898. else
  899. #endif
  900. MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
  901. ref_picture, pix_op, qpix_op, 0);
  902. }