mpegvideo_common.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. return;
  281. }
  282. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
  283. 17, 17+field_based,
  284. src_x, src_y<<field_based,
  285. s->h_edge_pos, s->v_edge_pos);
  286. ptr_y = s->edge_emu_buffer;
  287. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  288. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  289. ff_emulated_edge_mc(uvbuf ,
  290. ptr_cb, s->uvlinesize,
  291. 9, 9+field_based,
  292. uvsrc_x, uvsrc_y<<field_based,
  293. s->h_edge_pos>>1, s->v_edge_pos>>1);
  294. ff_emulated_edge_mc(uvbuf+16,
  295. ptr_cr, s->uvlinesize,
  296. 9, 9+field_based,
  297. uvsrc_x, uvsrc_y<<field_based,
  298. s->h_edge_pos>>1, s->v_edge_pos>>1);
  299. ptr_cb= uvbuf;
  300. ptr_cr= uvbuf+16;
  301. }
  302. }
  303. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  304. dest_y += s->linesize;
  305. dest_cb+= s->uvlinesize;
  306. dest_cr+= s->uvlinesize;
  307. }
  308. if(field_select){
  309. ptr_y += s->linesize;
  310. ptr_cb+= s->uvlinesize;
  311. ptr_cr+= s->uvlinesize;
  312. }
  313. pix_op[0][dxy](dest_y, ptr_y, linesize, h);
  314. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  315. pix_op[s->chroma_x_shift][uvdxy]
  316. (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
  317. pix_op[s->chroma_x_shift][uvdxy]
  318. (dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
  319. }
  320. if(!is_mpeg12 && (CONFIG_H261_ENCODER || CONFIG_H261_DECODER) &&
  321. s->out_format == FMT_H261){
  322. ff_h261_loop_filter(s);
  323. }
  324. }
  325. /* apply one mpeg motion vector to the three components */
  326. static av_always_inline
  327. void mpeg_motion(MpegEncContext *s,
  328. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  329. int field_based, int bottom_field, int field_select,
  330. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  331. int motion_x, int motion_y, int h)
  332. {
  333. #if !CONFIG_SMALL
  334. if(s->out_format == FMT_MPEG1)
  335. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
  336. bottom_field, field_select, ref_picture, pix_op,
  337. motion_x, motion_y, h, 1);
  338. else
  339. #endif
  340. mpeg_motion_internal(s, dest_y, dest_cb, dest_cr, field_based,
  341. bottom_field, field_select, ref_picture, pix_op,
  342. motion_x, motion_y, h, 0);
  343. }
  344. //FIXME move to dsputil, avg variant, 16x16 version
  345. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
  346. int x;
  347. uint8_t * const top = src[1];
  348. uint8_t * const left = src[2];
  349. uint8_t * const mid = src[0];
  350. uint8_t * const right = src[3];
  351. uint8_t * const bottom= src[4];
  352. #define OBMC_FILTER(x, t, l, m, r, b)\
  353. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  354. #define OBMC_FILTER4(x, t, l, m, r, b)\
  355. OBMC_FILTER(x , t, l, m, r, b);\
  356. OBMC_FILTER(x+1 , t, l, m, r, b);\
  357. OBMC_FILTER(x +stride, t, l, m, r, b);\
  358. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  359. x=0;
  360. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  361. OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
  362. OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
  363. OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
  364. OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
  365. OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
  366. x+= stride;
  367. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  368. OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
  369. OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
  370. OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
  371. x+= stride;
  372. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  373. OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
  374. OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
  375. OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
  376. x+= 2*stride;
  377. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  378. OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
  379. OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
  380. OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
  381. x+= 2*stride;
  382. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  383. OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
  384. OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
  385. OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
  386. OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
  387. OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
  388. x+= stride;
  389. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  390. OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
  391. OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
  392. OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
  393. }
  394. /* obmc for 1 8x8 luma block */
  395. static inline void obmc_motion(MpegEncContext *s,
  396. uint8_t *dest, uint8_t *src,
  397. int src_x, int src_y,
  398. op_pixels_func *pix_op,
  399. int16_t mv[5][2]/* mid top left right bottom*/)
  400. #define MID 0
  401. {
  402. int i;
  403. uint8_t *ptr[5];
  404. assert(s->quarter_sample==0);
  405. for(i=0; i<5; i++){
  406. if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
  407. ptr[i]= ptr[MID];
  408. }else{
  409. ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
  410. hpel_motion(s, ptr[i], src, 0, 0,
  411. src_x, src_y,
  412. s->width, s->height, s->linesize,
  413. s->h_edge_pos, s->v_edge_pos,
  414. 8, 8, pix_op,
  415. mv[i][0], mv[i][1]);
  416. }
  417. }
  418. put_obmc(dest, ptr, s->linesize);
  419. }
  420. static inline void qpel_motion(MpegEncContext *s,
  421. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  422. int field_based, int bottom_field, int field_select,
  423. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  424. qpel_mc_func (*qpix_op)[16],
  425. int motion_x, int motion_y, int h)
  426. {
  427. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  428. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
  429. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  430. src_x = s->mb_x * 16 + (motion_x >> 2);
  431. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  432. v_edge_pos = s->v_edge_pos >> field_based;
  433. linesize = s->linesize << field_based;
  434. uvlinesize = s->uvlinesize << field_based;
  435. if(field_based){
  436. mx= motion_x/2;
  437. my= motion_y>>1;
  438. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  439. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  440. mx= (motion_x>>1) + rtab[motion_x&7];
  441. my= (motion_y>>1) + rtab[motion_y&7];
  442. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  443. mx= (motion_x>>1)|(motion_x&1);
  444. my= (motion_y>>1)|(motion_y&1);
  445. }else{
  446. mx= motion_x/2;
  447. my= motion_y/2;
  448. }
  449. mx= (mx>>1)|(mx&1);
  450. my= (my>>1)|(my&1);
  451. uvdxy= (mx&1) | ((my&1)<<1);
  452. mx>>=1;
  453. my>>=1;
  454. uvsrc_x = s->mb_x * 8 + mx;
  455. uvsrc_y = s->mb_y * (8 >> field_based) + my;
  456. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  457. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  458. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  459. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
  460. || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
  461. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize,
  462. 17, 17+field_based, src_x, src_y<<field_based,
  463. s->h_edge_pos, s->v_edge_pos);
  464. ptr_y= s->edge_emu_buffer;
  465. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  466. uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
  467. ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize,
  468. 9, 9 + field_based,
  469. uvsrc_x, uvsrc_y<<field_based,
  470. s->h_edge_pos>>1, s->v_edge_pos>>1);
  471. ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize,
  472. 9, 9 + field_based,
  473. uvsrc_x, uvsrc_y<<field_based,
  474. s->h_edge_pos>>1, s->v_edge_pos>>1);
  475. ptr_cb= uvbuf;
  476. ptr_cr= uvbuf + 16;
  477. }
  478. }
  479. if(!field_based)
  480. qpix_op[0][dxy](dest_y, ptr_y, linesize);
  481. else{
  482. if(bottom_field){
  483. dest_y += s->linesize;
  484. dest_cb+= s->uvlinesize;
  485. dest_cr+= s->uvlinesize;
  486. }
  487. if(field_select){
  488. ptr_y += s->linesize;
  489. ptr_cb += s->uvlinesize;
  490. ptr_cr += s->uvlinesize;
  491. }
  492. //damn interlaced mode
  493. //FIXME boundary mirroring is not exactly correct here
  494. qpix_op[1][dxy](dest_y , ptr_y , linesize);
  495. qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
  496. }
  497. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  498. pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
  499. pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
  500. }
  501. }
  502. /**
  503. * h263 chroma 4mv motion compensation.
  504. */
  505. static inline void chroma_4mv_motion(MpegEncContext *s,
  506. uint8_t *dest_cb, uint8_t *dest_cr,
  507. uint8_t **ref_picture,
  508. op_pixels_func *pix_op,
  509. int mx, int my){
  510. int dxy, emu=0, src_x, src_y, offset;
  511. uint8_t *ptr;
  512. /* In case of 8X8, we construct a single chroma motion vector
  513. with a special rounding */
  514. mx= ff_h263_round_chroma(mx);
  515. my= ff_h263_round_chroma(my);
  516. dxy = ((my & 1) << 1) | (mx & 1);
  517. mx >>= 1;
  518. my >>= 1;
  519. src_x = s->mb_x * 8 + mx;
  520. src_y = s->mb_y * 8 + my;
  521. src_x = av_clip(src_x, -8, s->width/2);
  522. if (src_x == s->width/2)
  523. dxy &= ~1;
  524. src_y = av_clip(src_y, -8, s->height/2);
  525. if (src_y == s->height/2)
  526. dxy &= ~2;
  527. offset = (src_y * (s->uvlinesize)) + src_x;
  528. ptr = ref_picture[1] + offset;
  529. if(s->flags&CODEC_FLAG_EMU_EDGE){
  530. if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
  531. || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
  532. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  533. 9, 9, src_x, src_y,
  534. s->h_edge_pos>>1, s->v_edge_pos>>1);
  535. ptr= s->edge_emu_buffer;
  536. emu=1;
  537. }
  538. }
  539. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  540. ptr = ref_picture[2] + offset;
  541. if(emu){
  542. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  543. 9, 9, src_x, src_y,
  544. s->h_edge_pos>>1, s->v_edge_pos>>1);
  545. ptr= s->edge_emu_buffer;
  546. }
  547. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  548. }
  549. static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){
  550. /* fetch pixels for estimated mv 4 macroblocks ahead
  551. * optimized for 64byte cache lines */
  552. const int shift = s->quarter_sample ? 2 : 1;
  553. const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8;
  554. const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y;
  555. int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
  556. s->dsp.prefetch(pix[0]+off, s->linesize, 4);
  557. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  558. s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2);
  559. }
  560. /**
  561. * motion compensation of a single macroblock
  562. * @param s context
  563. * @param dest_y luma destination pointer
  564. * @param dest_cb chroma cb/u destination pointer
  565. * @param dest_cr chroma cr/v destination pointer
  566. * @param dir direction (0->forward, 1->backward)
  567. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  568. * @param pic_op halfpel motion compensation function (average or put normally)
  569. * @param pic_op qpel motion compensation function (average or put normally)
  570. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  571. */
  572. static av_always_inline void MPV_motion_internal(MpegEncContext *s,
  573. uint8_t *dest_y, uint8_t *dest_cb,
  574. uint8_t *dest_cr, int dir,
  575. uint8_t **ref_picture,
  576. op_pixels_func (*pix_op)[4],
  577. qpel_mc_func (*qpix_op)[16], int is_mpeg12)
  578. {
  579. int dxy, mx, my, src_x, src_y, motion_x, motion_y;
  580. int mb_x, mb_y, i;
  581. uint8_t *ptr, *dest;
  582. mb_x = s->mb_x;
  583. mb_y = s->mb_y;
  584. prefetch_motion(s, ref_picture, dir);
  585. if(!is_mpeg12 && s->obmc && s->pict_type != FF_B_TYPE){
  586. int16_t mv_cache[4][4][2];
  587. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  588. const int mot_stride= s->b8_stride;
  589. const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
  590. assert(!s->mb_skipped);
  591. memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
  592. memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  593. memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  594. if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
  595. memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
  596. }else{
  597. memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
  598. }
  599. if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
  600. *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
  601. *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
  602. }else{
  603. *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
  604. *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
  605. }
  606. if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
  607. *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
  608. *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
  609. }else{
  610. *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
  611. *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
  612. }
  613. mx = 0;
  614. my = 0;
  615. for(i=0;i<4;i++) {
  616. const int x= (i&1)+1;
  617. const int y= (i>>1)+1;
  618. int16_t mv[5][2]= {
  619. {mv_cache[y][x ][0], mv_cache[y][x ][1]},
  620. {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
  621. {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
  622. {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
  623. {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
  624. //FIXME cleanup
  625. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  626. ref_picture[0],
  627. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  628. pix_op[1],
  629. mv);
  630. mx += mv[0][0];
  631. my += mv[0][1];
  632. }
  633. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  634. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  635. return;
  636. }
  637. switch(s->mv_type) {
  638. case MV_TYPE_16X16:
  639. if(s->mcsel){
  640. if(s->real_sprite_warping_points==1){
  641. gmc1_motion(s, dest_y, dest_cb, dest_cr,
  642. ref_picture);
  643. }else{
  644. gmc_motion(s, dest_y, dest_cb, dest_cr,
  645. ref_picture);
  646. }
  647. }else if(!is_mpeg12 && s->quarter_sample){
  648. qpel_motion(s, dest_y, dest_cb, dest_cr,
  649. 0, 0, 0,
  650. ref_picture, pix_op, qpix_op,
  651. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  652. }else if(!is_mpeg12 && CONFIG_WMV2 && s->mspel){
  653. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  654. ref_picture, pix_op,
  655. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  656. }else
  657. {
  658. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  659. 0, 0, 0,
  660. ref_picture, pix_op,
  661. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  662. }
  663. break;
  664. case MV_TYPE_8X8:
  665. if (!is_mpeg12) {
  666. mx = 0;
  667. my = 0;
  668. if(s->quarter_sample){
  669. for(i=0;i<4;i++) {
  670. motion_x = s->mv[dir][i][0];
  671. motion_y = s->mv[dir][i][1];
  672. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  673. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  674. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  675. /* WARNING: do no forget half pels */
  676. src_x = av_clip(src_x, -16, s->width);
  677. if (src_x == s->width)
  678. dxy &= ~3;
  679. src_y = av_clip(src_y, -16, s->height);
  680. if (src_y == s->height)
  681. dxy &= ~12;
  682. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  683. if(s->flags&CODEC_FLAG_EMU_EDGE){
  684. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
  685. || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
  686. ff_emulated_edge_mc(s->edge_emu_buffer, ptr,
  687. s->linesize, 9, 9,
  688. src_x, src_y,
  689. s->h_edge_pos, s->v_edge_pos);
  690. ptr= s->edge_emu_buffer;
  691. }
  692. }
  693. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  694. qpix_op[1][dxy](dest, ptr, s->linesize);
  695. mx += s->mv[dir][i][0]/2;
  696. my += s->mv[dir][i][1]/2;
  697. }
  698. }else{
  699. for(i=0;i<4;i++) {
  700. hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  701. ref_picture[0], 0, 0,
  702. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  703. s->width, s->height, s->linesize,
  704. s->h_edge_pos, s->v_edge_pos,
  705. 8, 8, pix_op[1],
  706. s->mv[dir][i][0], s->mv[dir][i][1]);
  707. mx += s->mv[dir][i][0];
  708. my += s->mv[dir][i][1];
  709. }
  710. }
  711. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  712. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  713. }
  714. break;
  715. case MV_TYPE_FIELD:
  716. if (s->picture_structure == PICT_FRAME) {
  717. if(!is_mpeg12 && s->quarter_sample){
  718. for(i=0; i<2; i++){
  719. qpel_motion(s, dest_y, dest_cb, dest_cr,
  720. 1, i, s->field_select[dir][i],
  721. ref_picture, pix_op, qpix_op,
  722. s->mv[dir][i][0], s->mv[dir][i][1], 8);
  723. }
  724. }else{
  725. /* top field */
  726. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  727. 1, 0, s->field_select[dir][0],
  728. ref_picture, pix_op,
  729. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  730. /* bottom field */
  731. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  732. 1, 1, s->field_select[dir][1],
  733. ref_picture, pix_op,
  734. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  735. }
  736. } else {
  737. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != FF_B_TYPE && !s->first_field){
  738. ref_picture= s->current_picture_ptr->data;
  739. }
  740. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  741. 0, 0, s->field_select[dir][0],
  742. ref_picture, pix_op,
  743. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  744. }
  745. break;
  746. case MV_TYPE_16X8:
  747. for(i=0; i<2; i++){
  748. uint8_t ** ref2picture;
  749. if(s->picture_structure == s->field_select[dir][i] + 1
  750. || s->pict_type == FF_B_TYPE || s->first_field){
  751. ref2picture= ref_picture;
  752. }else{
  753. ref2picture= s->current_picture_ptr->data;
  754. }
  755. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  756. 0, 0, s->field_select[dir][i],
  757. ref2picture, pix_op,
  758. s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8);
  759. dest_y += 16*s->linesize;
  760. dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
  761. dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
  762. }
  763. break;
  764. case MV_TYPE_DMV:
  765. if(s->picture_structure == PICT_FRAME){
  766. for(i=0; i<2; i++){
  767. int j;
  768. for(j=0; j<2; j++){
  769. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  770. 1, j, j^i,
  771. ref_picture, pix_op,
  772. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8);
  773. }
  774. pix_op = s->dsp.avg_pixels_tab;
  775. }
  776. }else{
  777. for(i=0; i<2; i++){
  778. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  779. 0, 0, s->picture_structure != i+1,
  780. ref_picture, pix_op,
  781. s->mv[dir][2*i][0],s->mv[dir][2*i][1],16);
  782. // after put we make avg of the same block
  783. pix_op=s->dsp.avg_pixels_tab;
  784. //opposite parity is always in the same frame if this is second field
  785. if(!s->first_field){
  786. ref_picture = s->current_picture_ptr->data;
  787. }
  788. }
  789. }
  790. break;
  791. default: assert(0);
  792. }
  793. }
  794. static inline void MPV_motion(MpegEncContext *s,
  795. uint8_t *dest_y, uint8_t *dest_cb,
  796. uint8_t *dest_cr, int dir,
  797. uint8_t **ref_picture,
  798. op_pixels_func (*pix_op)[4],
  799. qpel_mc_func (*qpix_op)[16])
  800. {
  801. #if !CONFIG_SMALL
  802. if(s->out_format == FMT_MPEG1)
  803. MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
  804. ref_picture, pix_op, qpix_op, 1);
  805. else
  806. #endif
  807. MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
  808. ref_picture, pix_op, qpix_op, 0);
  809. }
  810. #endif /* AVCODEC_MPEGVIDEO_COMMON_H */