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.

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