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.

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