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.

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