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.

896 lines
34KB

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