mpegvideo_common.h 34 KB

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