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.

5188 lines
182KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. /**
  22. * @file mpegvideo.c
  23. * The simplest mpeg encoder (well, it was the simplest!).
  24. */
  25. #include <limits.h>
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "mpegvideo.h"
  29. #include "faandct.h"
  30. #ifdef USE_FASTMEMCPY
  31. #include "fastmemcpy.h"
  32. #endif
  33. //#undef NDEBUG
  34. //#include <assert.h>
  35. #ifdef CONFIG_ENCODERS
  36. static void encode_picture(MpegEncContext *s, int picture_number);
  37. #endif //CONFIG_ENCODERS
  38. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  39. DCTELEM *block, int n, int qscale);
  40. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  41. DCTELEM *block, int n, int qscale);
  42. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  43. DCTELEM *block, int n, int qscale);
  44. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  45. DCTELEM *block, int n, int qscale);
  46. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  47. DCTELEM *block, int n, int qscale);
  48. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  49. DCTELEM *block, int n, int qscale);
  50. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
  51. #ifdef CONFIG_ENCODERS
  52. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  53. static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  54. static int sse_mb(MpegEncContext *s);
  55. #endif //CONFIG_ENCODERS
  56. #ifdef HAVE_XVMC
  57. extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
  58. extern void XVMC_field_end(MpegEncContext *s);
  59. extern void XVMC_decode_mb(MpegEncContext *s);
  60. #endif
  61. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
  62. /* enable all paranoid tests for rounding, overflows, etc... */
  63. //#define PARANOID
  64. //#define DEBUG
  65. /* for jpeg fast DCT */
  66. #define CONST_BITS 14
  67. static const uint16_t aanscales[64] = {
  68. /* precomputed values scaled up by 14 bits */
  69. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  70. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  71. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  72. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  73. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  74. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  75. 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  76. 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
  77. };
  78. static const uint8_t h263_chroma_roundtab[16] = {
  79. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  80. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  81. };
  82. static const uint8_t ff_default_chroma_qscale_table[32]={
  83. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  84. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
  85. };
  86. #ifdef CONFIG_ENCODERS
  87. static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
  88. static uint8_t default_fcode_tab[MAX_MV*2+1];
  89. enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
  90. static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
  91. const uint16_t *quant_matrix, int bias, int qmin, int qmax)
  92. {
  93. int qscale;
  94. for(qscale=qmin; qscale<=qmax; qscale++){
  95. int i;
  96. if (dsp->fdct == ff_jpeg_fdct_islow
  97. #ifdef FAAN_POSTSCALE
  98. || dsp->fdct == ff_faandct
  99. #endif
  100. ) {
  101. for(i=0;i<64;i++) {
  102. const int j= dsp->idct_permutation[i];
  103. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  104. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  105. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  106. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  107. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
  108. (qscale * quant_matrix[j]));
  109. }
  110. } else if (dsp->fdct == fdct_ifast
  111. #ifndef FAAN_POSTSCALE
  112. || dsp->fdct == ff_faandct
  113. #endif
  114. ) {
  115. for(i=0;i<64;i++) {
  116. const int j= dsp->idct_permutation[i];
  117. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  118. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  119. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  120. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  121. qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
  122. (aanscales[i] * qscale * quant_matrix[j]));
  123. }
  124. } else {
  125. for(i=0;i<64;i++) {
  126. const int j= dsp->idct_permutation[i];
  127. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  128. So 16 <= qscale * quant_matrix[i] <= 7905
  129. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  130. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  131. */
  132. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
  133. // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  134. qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
  135. if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
  136. qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
  137. }
  138. }
  139. }
  140. }
  141. static inline void update_qscale(MpegEncContext *s){
  142. s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  143. s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
  144. s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  145. }
  146. #endif //CONFIG_ENCODERS
  147. void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
  148. int i;
  149. int end;
  150. st->scantable= src_scantable;
  151. for(i=0; i<64; i++){
  152. int j;
  153. j = src_scantable[i];
  154. st->permutated[i] = permutation[j];
  155. #ifdef ARCH_POWERPC
  156. st->inverse[j] = i;
  157. #endif
  158. }
  159. end=-1;
  160. for(i=0; i<64; i++){
  161. int j;
  162. j = st->permutated[i];
  163. if(j>end) end=j;
  164. st->raster_end[i]= end;
  165. }
  166. }
  167. #ifdef CONFIG_ENCODERS
  168. void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
  169. int i;
  170. if(matrix){
  171. put_bits(pb, 1, 1);
  172. for(i=0;i<64;i++) {
  173. put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
  174. }
  175. }else
  176. put_bits(pb, 1, 0);
  177. }
  178. #endif //CONFIG_ENCODERS
  179. /* init common dct for both encoder and decoder */
  180. int DCT_common_init(MpegEncContext *s)
  181. {
  182. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  183. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  184. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  185. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  186. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  187. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  188. #ifdef CONFIG_ENCODERS
  189. s->dct_quantize= dct_quantize_c;
  190. #endif
  191. #ifdef HAVE_MMX
  192. MPV_common_init_mmx(s);
  193. #endif
  194. #ifdef ARCH_ALPHA
  195. MPV_common_init_axp(s);
  196. #endif
  197. #ifdef HAVE_MLIB
  198. MPV_common_init_mlib(s);
  199. #endif
  200. #ifdef HAVE_MMI
  201. MPV_common_init_mmi(s);
  202. #endif
  203. #ifdef ARCH_ARMV4L
  204. MPV_common_init_armv4l(s);
  205. #endif
  206. #ifdef ARCH_POWERPC
  207. MPV_common_init_ppc(s);
  208. #endif
  209. #ifdef CONFIG_ENCODERS
  210. s->fast_dct_quantize= s->dct_quantize;
  211. if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
  212. s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
  213. }
  214. #endif //CONFIG_ENCODERS
  215. /* load & permutate scantables
  216. note: only wmv uses differnt ones
  217. */
  218. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  219. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  220. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  221. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  222. s->picture_structure= PICT_FRAME;
  223. return 0;
  224. }
  225. static void copy_picture(Picture *dst, Picture *src){
  226. *dst = *src;
  227. dst->type= FF_BUFFER_TYPE_COPY;
  228. }
  229. /**
  230. * allocates a Picture
  231. * The pixels are allocated/set by calling get_buffer() if shared=0
  232. */
  233. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  234. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
  235. const int mb_array_size= s->mb_stride*s->mb_height;
  236. const int b8_array_size= s->b8_stride*s->mb_height*2;
  237. const int b4_array_size= s->b4_stride*s->mb_height*4;
  238. int i;
  239. if(shared){
  240. assert(pic->data[0]);
  241. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  242. pic->type= FF_BUFFER_TYPE_SHARED;
  243. }else{
  244. int r;
  245. assert(!pic->data[0]);
  246. r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  247. if(r<0 || !pic->age || !pic->type || !pic->data[0]){
  248. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  249. return -1;
  250. }
  251. if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
  252. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  253. return -1;
  254. }
  255. if(pic->linesize[1] != pic->linesize[2]){
  256. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
  257. return -1;
  258. }
  259. s->linesize = pic->linesize[0];
  260. s->uvlinesize= pic->linesize[1];
  261. }
  262. if(pic->qscale_table==NULL){
  263. if (s->encoding) {
  264. CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
  265. CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
  266. CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
  267. }
  268. CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
  269. CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
  270. CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(int))
  271. pic->mb_type= pic->mb_type_base + s->mb_stride+1;
  272. if(s->out_format == FMT_H264){
  273. for(i=0; i<2; i++){
  274. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+1) * sizeof(uint16_t))
  275. pic->motion_val[i]= pic->motion_val_base[i]+1;
  276. CHECKED_ALLOCZ(pic->ref_index[i] , b8_array_size * sizeof(uint8_t))
  277. }
  278. pic->motion_subsample_log2= 2;
  279. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&(FF_DEBUG_VIS_MV|FF_DEBUG_MV))){
  280. for(i=0; i<2; i++){
  281. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+1) * sizeof(uint16_t)*2) //FIXME
  282. pic->motion_val[i]= pic->motion_val_base[i]+1;
  283. }
  284. pic->motion_subsample_log2= 3;
  285. }
  286. pic->qstride= s->mb_stride;
  287. CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
  288. }
  289. //it might be nicer if the application would keep track of these but it would require a API change
  290. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  291. s->prev_pict_types[0]= s->pict_type;
  292. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
  293. pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
  294. return 0;
  295. fail: //for the CHECKED_ALLOCZ macro
  296. return -1;
  297. }
  298. /**
  299. * deallocates a picture
  300. */
  301. static void free_picture(MpegEncContext *s, Picture *pic){
  302. int i;
  303. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  304. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  305. }
  306. av_freep(&pic->mb_var);
  307. av_freep(&pic->mc_mb_var);
  308. av_freep(&pic->mb_mean);
  309. av_freep(&pic->mbskip_table);
  310. av_freep(&pic->qscale_table);
  311. av_freep(&pic->mb_type_base);
  312. av_freep(&pic->pan_scan);
  313. pic->mb_type= NULL;
  314. for(i=0; i<2; i++){
  315. av_freep(&pic->motion_val_base[i]);
  316. av_freep(&pic->ref_index[i]);
  317. }
  318. if(pic->type == FF_BUFFER_TYPE_SHARED){
  319. for(i=0; i<4; i++){
  320. pic->base[i]=
  321. pic->data[i]= NULL;
  322. }
  323. pic->type= 0;
  324. }
  325. }
  326. /* init common structure for both encoder and decoder */
  327. int MPV_common_init(MpegEncContext *s)
  328. {
  329. int y_size, c_size, yc_size, i, mb_array_size, x, y;
  330. dsputil_init(&s->dsp, s->avctx);
  331. DCT_common_init(s);
  332. s->flags= s->avctx->flags;
  333. s->mb_width = (s->width + 15) / 16;
  334. s->mb_height = (s->height + 15) / 16;
  335. s->mb_stride = s->mb_width + 1;
  336. s->b8_stride = s->mb_width*2 + 1;
  337. s->b4_stride = s->mb_width*4 + 1;
  338. mb_array_size= s->mb_height * s->mb_stride;
  339. /* set default edge pos, will be overriden in decode_header if needed */
  340. s->h_edge_pos= s->mb_width*16;
  341. s->v_edge_pos= s->mb_height*16;
  342. s->mb_num = s->mb_width * s->mb_height;
  343. s->block_wrap[0]=
  344. s->block_wrap[1]=
  345. s->block_wrap[2]=
  346. s->block_wrap[3]= s->mb_width*2 + 2;
  347. s->block_wrap[4]=
  348. s->block_wrap[5]= s->mb_width + 2;
  349. s->y_dc_scale_table=
  350. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  351. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  352. if (!s->encoding)
  353. s->progressive_sequence= 1;
  354. s->progressive_frame= 1;
  355. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  356. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  357. yc_size = y_size + 2 * c_size;
  358. /* convert fourcc to upper case */
  359. s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
  360. + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
  361. + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
  362. + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
  363. s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
  364. + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
  365. + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
  366. + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
  367. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
  368. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
  369. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  370. CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
  371. for(y=0; y<s->mb_height; y++){
  372. for(x=0; x<s->mb_width; x++){
  373. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  374. }
  375. }
  376. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  377. if (s->encoding) {
  378. int mv_table_size= s->mb_stride * (s->mb_height+2) + 1;
  379. /* Allocate MV tables */
  380. CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  381. CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  382. CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  383. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  384. CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  385. CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  386. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  387. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  388. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  389. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  390. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  391. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  392. //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
  393. CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
  394. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  395. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  396. if(s->codec_id==CODEC_ID_MPEG4){
  397. CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
  398. CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
  399. }
  400. if(s->msmpeg4_version){
  401. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  402. }
  403. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  404. /* Allocate MB type table */
  405. CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint8_t)) //needed for encoding
  406. CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
  407. CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
  408. CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
  409. CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
  410. CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
  411. CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  412. CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  413. if(s->avctx->noise_reduction){
  414. CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
  415. CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
  416. }
  417. }
  418. CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM))
  419. CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
  420. CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
  421. if(s->codec_id==CODEC_ID_MPEG4){
  422. /* interlaced direct mode decoding tables */
  423. CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t))
  424. CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t))
  425. }
  426. if (s->out_format == FMT_H263) {
  427. /* ac values */
  428. CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
  429. s->ac_val[1] = s->ac_val[0] + y_size;
  430. s->ac_val[2] = s->ac_val[1] + c_size;
  431. /* cbp values */
  432. CHECKED_ALLOCZ(s->coded_block, y_size);
  433. /* divx501 bitstream reorder buffer */
  434. CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
  435. /* cbp, ac_pred, pred_dir */
  436. CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
  437. CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
  438. }
  439. if (s->h263_pred || s->h263_plus || !s->encoding) {
  440. /* dc values */
  441. //MN: we need these for error resilience of intra-frames
  442. CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
  443. s->dc_val[1] = s->dc_val[0] + y_size;
  444. s->dc_val[2] = s->dc_val[1] + c_size;
  445. for(i=0;i<yc_size;i++)
  446. s->dc_val[0][i] = 1024;
  447. }
  448. /* which mb is a intra block */
  449. CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
  450. memset(s->mbintra_table, 1, mb_array_size);
  451. /* default structure is frame */
  452. s->picture_structure = PICT_FRAME;
  453. /* init macroblock skip table */
  454. CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
  455. //Note the +1 is for a quicker mpeg4 slice_end detection
  456. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  457. s->block= s->blocks[0];
  458. for(i=0;i<12;i++){
  459. s->pblocks[i] = (short *)(&s->block[i]);
  460. }
  461. s->parse_context.state= -1;
  462. s->context_initialized = 1;
  463. return 0;
  464. fail:
  465. MPV_common_end(s);
  466. return -1;
  467. }
  468. //extern int sads;
  469. /* init common structure for both encoder and decoder */
  470. void MPV_common_end(MpegEncContext *s)
  471. {
  472. int i;
  473. av_freep(&s->parse_context.buffer);
  474. s->parse_context.buffer_size=0;
  475. av_freep(&s->mb_type);
  476. av_freep(&s->p_mv_table_base);
  477. av_freep(&s->b_forw_mv_table_base);
  478. av_freep(&s->b_back_mv_table_base);
  479. av_freep(&s->b_bidir_forw_mv_table_base);
  480. av_freep(&s->b_bidir_back_mv_table_base);
  481. av_freep(&s->b_direct_mv_table_base);
  482. s->p_mv_table= NULL;
  483. s->b_forw_mv_table= NULL;
  484. s->b_back_mv_table= NULL;
  485. s->b_bidir_forw_mv_table= NULL;
  486. s->b_bidir_back_mv_table= NULL;
  487. s->b_direct_mv_table= NULL;
  488. av_freep(&s->dc_val[0]);
  489. av_freep(&s->ac_val[0]);
  490. av_freep(&s->coded_block);
  491. av_freep(&s->mbintra_table);
  492. av_freep(&s->cbp_table);
  493. av_freep(&s->pred_dir_table);
  494. av_freep(&s->me.scratchpad);
  495. av_freep(&s->me.map);
  496. av_freep(&s->me.score_map);
  497. av_freep(&s->mbskip_table);
  498. av_freep(&s->prev_pict_types);
  499. av_freep(&s->bitstream_buffer);
  500. av_freep(&s->tex_pb_buffer);
  501. av_freep(&s->pb2_buffer);
  502. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  503. av_freep(&s->field_mv_table);
  504. av_freep(&s->field_select_table);
  505. av_freep(&s->avctx->stats_out);
  506. av_freep(&s->ac_stats);
  507. av_freep(&s->error_status_table);
  508. av_freep(&s->mb_index2xy);
  509. av_freep(&s->lambda_table);
  510. av_freep(&s->q_intra_matrix);
  511. av_freep(&s->q_inter_matrix);
  512. av_freep(&s->q_intra_matrix16);
  513. av_freep(&s->q_inter_matrix16);
  514. av_freep(&s->blocks);
  515. av_freep(&s->input_picture);
  516. av_freep(&s->reordered_input_picture);
  517. av_freep(&s->dct_error_sum);
  518. av_freep(&s->dct_offset);
  519. if(s->picture){
  520. for(i=0; i<MAX_PICTURE_COUNT; i++){
  521. free_picture(s, &s->picture[i]);
  522. }
  523. }
  524. av_freep(&s->picture);
  525. avcodec_default_free_buffers(s->avctx);
  526. s->context_initialized = 0;
  527. s->last_picture_ptr=
  528. s->next_picture_ptr=
  529. s->current_picture_ptr= NULL;
  530. }
  531. #ifdef CONFIG_ENCODERS
  532. /* init video encoder */
  533. int MPV_encode_init(AVCodecContext *avctx)
  534. {
  535. MpegEncContext *s = avctx->priv_data;
  536. int i, dummy;
  537. int chroma_h_shift, chroma_v_shift;
  538. avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
  539. s->bit_rate = avctx->bit_rate;
  540. s->width = avctx->width;
  541. s->height = avctx->height;
  542. if(avctx->gop_size > 600){
  543. av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
  544. avctx->gop_size=600;
  545. }
  546. s->gop_size = avctx->gop_size;
  547. s->avctx = avctx;
  548. s->flags= avctx->flags;
  549. s->max_b_frames= avctx->max_b_frames;
  550. s->codec_id= avctx->codec->id;
  551. s->luma_elim_threshold = avctx->luma_elim_threshold;
  552. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  553. s->strict_std_compliance= avctx->strict_std_compliance;
  554. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  555. s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
  556. s->mpeg_quant= avctx->mpeg_quant;
  557. s->rtp_mode= !!avctx->rtp_payload_size;
  558. if (s->gop_size <= 1) {
  559. s->intra_only = 1;
  560. s->gop_size = 12;
  561. } else {
  562. s->intra_only = 0;
  563. }
  564. s->me_method = avctx->me_method;
  565. /* Fixed QSCALE */
  566. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  567. s->adaptive_quant= ( s->avctx->lumi_masking
  568. || s->avctx->dark_masking
  569. || s->avctx->temporal_cplx_masking
  570. || s->avctx->spatial_cplx_masking
  571. || s->avctx->p_masking
  572. || (s->flags&CODEC_FLAG_QP_RD))
  573. && !s->fixed_qscale;
  574. s->obmc= (s->flags & CODEC_FLAG_OBMC);
  575. s->loop_filter= (s->flags & CODEC_FLAG_LOOP_FILTER);
  576. if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
  577. && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
  578. av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
  579. return -1;
  580. }
  581. if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
  582. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n");
  583. return -1;
  584. }
  585. if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
  586. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
  587. return -1;
  588. }
  589. if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
  590. av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
  591. return -1;
  592. }
  593. if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
  594. av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
  595. return -1;
  596. }
  597. if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
  598. av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
  599. return -1;
  600. }
  601. if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
  602. av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
  603. return -1;
  604. }
  605. if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
  606. av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
  607. return -1;
  608. }
  609. if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
  610. av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
  611. return -1;
  612. }
  613. if(s->codec_id==CODEC_ID_MJPEG){
  614. s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
  615. s->inter_quant_bias= 0;
  616. }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
  617. s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
  618. s->inter_quant_bias= 0;
  619. }else{
  620. s->intra_quant_bias=0;
  621. s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
  622. }
  623. if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
  624. s->intra_quant_bias= avctx->intra_quant_bias;
  625. if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
  626. s->inter_quant_bias= avctx->inter_quant_bias;
  627. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
  628. av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
  629. s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
  630. switch(avctx->codec->id) {
  631. case CODEC_ID_MPEG1VIDEO:
  632. s->out_format = FMT_MPEG1;
  633. s->low_delay= 0; //s->max_b_frames ? 0 : 1;
  634. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  635. break;
  636. case CODEC_ID_MPEG2VIDEO:
  637. s->out_format = FMT_MPEG1;
  638. s->low_delay= 0; //s->max_b_frames ? 0 : 1;
  639. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  640. s->rtp_mode= 1;
  641. break;
  642. case CODEC_ID_LJPEG:
  643. case CODEC_ID_MJPEG:
  644. s->out_format = FMT_MJPEG;
  645. s->intra_only = 1; /* force intra only for jpeg */
  646. s->mjpeg_write_tables = 1; /* write all tables */
  647. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  648. s->mjpeg_vsample[0] = 1<<chroma_v_shift;
  649. s->mjpeg_vsample[1] = 1;
  650. s->mjpeg_vsample[2] = 1;
  651. s->mjpeg_hsample[0] = 1<<chroma_h_shift;
  652. s->mjpeg_hsample[1] = 1;
  653. s->mjpeg_hsample[2] = 1;
  654. if (mjpeg_init(s) < 0)
  655. return -1;
  656. avctx->delay=0;
  657. s->low_delay=1;
  658. break;
  659. #ifdef CONFIG_RISKY
  660. case CODEC_ID_H263:
  661. if (h263_get_picture_format(s->width, s->height) == 7) {
  662. av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
  663. return -1;
  664. }
  665. s->out_format = FMT_H263;
  666. s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
  667. avctx->delay=0;
  668. s->low_delay=1;
  669. break;
  670. case CODEC_ID_H263P:
  671. s->out_format = FMT_H263;
  672. s->h263_plus = 1;
  673. /* Fx */
  674. s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
  675. s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
  676. s->modified_quant= s->h263_aic;
  677. s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
  678. s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
  679. s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
  680. s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
  681. s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
  682. /* /Fx */
  683. /* These are just to be sure */
  684. avctx->delay=0;
  685. s->low_delay=1;
  686. break;
  687. case CODEC_ID_FLV1:
  688. s->out_format = FMT_H263;
  689. s->h263_flv = 2; /* format = 1; 11-bit codes */
  690. s->unrestricted_mv = 1;
  691. s->rtp_mode=0; /* don't allow GOB */
  692. avctx->delay=0;
  693. s->low_delay=1;
  694. break;
  695. case CODEC_ID_RV10:
  696. s->out_format = FMT_H263;
  697. avctx->delay=0;
  698. s->low_delay=1;
  699. break;
  700. case CODEC_ID_MPEG4:
  701. s->out_format = FMT_H263;
  702. s->h263_pred = 1;
  703. s->unrestricted_mv = 1;
  704. s->low_delay= s->max_b_frames ? 0 : 1;
  705. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  706. break;
  707. case CODEC_ID_MSMPEG4V1:
  708. s->out_format = FMT_H263;
  709. s->h263_msmpeg4 = 1;
  710. s->h263_pred = 1;
  711. s->unrestricted_mv = 1;
  712. s->msmpeg4_version= 1;
  713. avctx->delay=0;
  714. s->low_delay=1;
  715. break;
  716. case CODEC_ID_MSMPEG4V2:
  717. s->out_format = FMT_H263;
  718. s->h263_msmpeg4 = 1;
  719. s->h263_pred = 1;
  720. s->unrestricted_mv = 1;
  721. s->msmpeg4_version= 2;
  722. avctx->delay=0;
  723. s->low_delay=1;
  724. break;
  725. case CODEC_ID_MSMPEG4V3:
  726. s->out_format = FMT_H263;
  727. s->h263_msmpeg4 = 1;
  728. s->h263_pred = 1;
  729. s->unrestricted_mv = 1;
  730. s->msmpeg4_version= 3;
  731. s->flipflop_rounding=1;
  732. avctx->delay=0;
  733. s->low_delay=1;
  734. break;
  735. case CODEC_ID_WMV1:
  736. s->out_format = FMT_H263;
  737. s->h263_msmpeg4 = 1;
  738. s->h263_pred = 1;
  739. s->unrestricted_mv = 1;
  740. s->msmpeg4_version= 4;
  741. s->flipflop_rounding=1;
  742. avctx->delay=0;
  743. s->low_delay=1;
  744. break;
  745. case CODEC_ID_WMV2:
  746. s->out_format = FMT_H263;
  747. s->h263_msmpeg4 = 1;
  748. s->h263_pred = 1;
  749. s->unrestricted_mv = 1;
  750. s->msmpeg4_version= 5;
  751. s->flipflop_rounding=1;
  752. avctx->delay=0;
  753. s->low_delay=1;
  754. break;
  755. #endif
  756. default:
  757. return -1;
  758. }
  759. { /* set up some save defaults, some codecs might override them later */
  760. static int done=0;
  761. if(!done){
  762. int i;
  763. done=1;
  764. default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
  765. memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
  766. memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
  767. for(i=-16; i<16; i++){
  768. default_fcode_tab[i + MAX_MV]= 1;
  769. }
  770. }
  771. }
  772. s->me.mv_penalty= default_mv_penalty;
  773. s->fcode_tab= default_fcode_tab;
  774. /* dont use mv_penalty table for crap MV as it would be confused */
  775. //FIXME remove after fixing / removing old ME
  776. if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
  777. s->encoding = 1;
  778. /* init */
  779. if (MPV_common_init(s) < 0)
  780. return -1;
  781. if(s->modified_quant)
  782. s->chroma_qscale_table= ff_h263_chroma_qscale_table;
  783. s->progressive_frame=
  784. s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  785. ff_init_me(s);
  786. #ifdef CONFIG_ENCODERS
  787. #ifdef CONFIG_RISKY
  788. if (s->out_format == FMT_H263)
  789. h263_encode_init(s);
  790. if(s->msmpeg4_version)
  791. ff_msmpeg4_encode_init(s);
  792. #endif
  793. if (s->out_format == FMT_MPEG1)
  794. ff_mpeg1_encode_init(s);
  795. #endif
  796. /* init default q matrix */
  797. for(i=0;i<64;i++) {
  798. int j= s->dsp.idct_permutation[i];
  799. #ifdef CONFIG_RISKY
  800. if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
  801. s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
  802. s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
  803. }else if(s->out_format == FMT_H263){
  804. s->intra_matrix[j] =
  805. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  806. }else
  807. #endif
  808. { /* mpeg1/2 */
  809. s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
  810. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  811. }
  812. if(s->avctx->intra_matrix)
  813. s->intra_matrix[j] = s->avctx->intra_matrix[i];
  814. if(s->avctx->inter_matrix)
  815. s->inter_matrix[j] = s->avctx->inter_matrix[i];
  816. }
  817. /* precompute matrix */
  818. /* for mjpeg, we do include qscale in the matrix */
  819. if (s->out_format != FMT_MJPEG) {
  820. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  821. s->intra_matrix, s->intra_quant_bias, 1, 31);
  822. convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
  823. s->inter_matrix, s->inter_quant_bias, 1, 31);
  824. }
  825. if(ff_rate_control_init(s) < 0)
  826. return -1;
  827. s->picture_number = 0;
  828. s->picture_in_gop_number = 0;
  829. s->fake_picture_number = 0;
  830. /* motion detector init */
  831. s->f_code = 1;
  832. s->b_code = 1;
  833. return 0;
  834. }
  835. int MPV_encode_end(AVCodecContext *avctx)
  836. {
  837. MpegEncContext *s = avctx->priv_data;
  838. #ifdef STATS
  839. print_stats();
  840. #endif
  841. ff_rate_control_uninit(s);
  842. MPV_common_end(s);
  843. if (s->out_format == FMT_MJPEG)
  844. mjpeg_close(s);
  845. av_freep(&avctx->extradata);
  846. return 0;
  847. }
  848. #endif //CONFIG_ENCODERS
  849. void init_rl(RLTable *rl)
  850. {
  851. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  852. uint8_t index_run[MAX_RUN+1];
  853. int last, run, level, start, end, i;
  854. /* compute max_level[], max_run[] and index_run[] */
  855. for(last=0;last<2;last++) {
  856. if (last == 0) {
  857. start = 0;
  858. end = rl->last;
  859. } else {
  860. start = rl->last;
  861. end = rl->n;
  862. }
  863. memset(max_level, 0, MAX_RUN + 1);
  864. memset(max_run, 0, MAX_LEVEL + 1);
  865. memset(index_run, rl->n, MAX_RUN + 1);
  866. for(i=start;i<end;i++) {
  867. run = rl->table_run[i];
  868. level = rl->table_level[i];
  869. if (index_run[run] == rl->n)
  870. index_run[run] = i;
  871. if (level > max_level[run])
  872. max_level[run] = level;
  873. if (run > max_run[level])
  874. max_run[level] = run;
  875. }
  876. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  877. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  878. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  879. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  880. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  881. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  882. }
  883. }
  884. /* draw the edges of width 'w' of an image of size width, height */
  885. //FIXME check that this is ok for mpeg4 interlaced
  886. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  887. {
  888. uint8_t *ptr, *last_line;
  889. int i;
  890. last_line = buf + (height - 1) * wrap;
  891. for(i=0;i<w;i++) {
  892. /* top and bottom */
  893. memcpy(buf - (i + 1) * wrap, buf, width);
  894. memcpy(last_line + (i + 1) * wrap, last_line, width);
  895. }
  896. /* left and right */
  897. ptr = buf;
  898. for(i=0;i<height;i++) {
  899. memset(ptr - w, ptr[0], w);
  900. memset(ptr + width, ptr[width-1], w);
  901. ptr += wrap;
  902. }
  903. /* corners */
  904. for(i=0;i<w;i++) {
  905. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  906. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  907. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  908. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  909. }
  910. }
  911. int ff_find_unused_picture(MpegEncContext *s, int shared){
  912. int i;
  913. if(shared){
  914. for(i=0; i<MAX_PICTURE_COUNT; i++){
  915. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  916. }
  917. }else{
  918. for(i=0; i<MAX_PICTURE_COUNT; i++){
  919. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  920. }
  921. for(i=0; i<MAX_PICTURE_COUNT; i++){
  922. if(s->picture[i].data[0]==NULL) return i;
  923. }
  924. }
  925. assert(0);
  926. return -1;
  927. }
  928. static void update_noise_reduction(MpegEncContext *s){
  929. int intra, i;
  930. for(intra=0; intra<2; intra++){
  931. if(s->dct_count[intra] > (1<<16)){
  932. for(i=0; i<64; i++){
  933. s->dct_error_sum[intra][i] >>=1;
  934. }
  935. s->dct_count[intra] >>= 1;
  936. }
  937. for(i=0; i<64; i++){
  938. s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
  939. }
  940. }
  941. }
  942. /**
  943. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  944. */
  945. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  946. {
  947. int i;
  948. AVFrame *pic;
  949. s->mb_skiped = 0;
  950. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  951. /* mark&release old frames */
  952. if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
  953. avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
  954. /* release forgotten pictures */
  955. /* if(mpeg124/h263) */
  956. if(!s->encoding){
  957. for(i=0; i<MAX_PICTURE_COUNT; i++){
  958. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  959. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  960. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  961. }
  962. }
  963. }
  964. }
  965. alloc:
  966. if(!s->encoding){
  967. /* release non refernce frames */
  968. for(i=0; i<MAX_PICTURE_COUNT; i++){
  969. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  970. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  971. }
  972. }
  973. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  974. pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
  975. else{
  976. i= ff_find_unused_picture(s, 0);
  977. pic= (AVFrame*)&s->picture[i];
  978. }
  979. pic->reference= s->pict_type != B_TYPE ? 3 : 0;
  980. if(s->current_picture_ptr) //FIXME broken, we need a coded_picture_number in MpegEncContext
  981. pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
  982. if( alloc_picture(s, (Picture*)pic, 0) < 0)
  983. return -1;
  984. s->current_picture_ptr= (Picture*)pic;
  985. s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
  986. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  987. }
  988. s->current_picture_ptr->pict_type= s->pict_type;
  989. // if(s->flags && CODEC_FLAG_QSCALE)
  990. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  991. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
  992. copy_picture(&s->current_picture, s->current_picture_ptr);
  993. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  994. if (s->pict_type != B_TYPE) {
  995. s->last_picture_ptr= s->next_picture_ptr;
  996. s->next_picture_ptr= s->current_picture_ptr;
  997. }
  998. if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
  999. if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
  1000. if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
  1001. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  1002. assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
  1003. goto alloc;
  1004. }
  1005. assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  1006. if(s->picture_structure!=PICT_FRAME){
  1007. int i;
  1008. for(i=0; i<4; i++){
  1009. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1010. s->current_picture.data[i] += s->current_picture.linesize[i];
  1011. }
  1012. s->current_picture.linesize[i] *= 2;
  1013. s->last_picture.linesize[i] *=2;
  1014. s->next_picture.linesize[i] *=2;
  1015. }
  1016. }
  1017. }
  1018. s->hurry_up= s->avctx->hurry_up;
  1019. s->error_resilience= avctx->error_resilience;
  1020. /* set dequantizer, we cant do it during init as it might change for mpeg4
  1021. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  1022. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  1023. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1024. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1025. }else if(s->out_format == FMT_H263){
  1026. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1027. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1028. }else{
  1029. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1030. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1031. }
  1032. if(s->dct_error_sum){
  1033. assert(s->avctx->noise_reduction && s->encoding);
  1034. update_noise_reduction(s);
  1035. }
  1036. #ifdef HAVE_XVMC
  1037. if(s->avctx->xvmc_acceleration)
  1038. return XVMC_field_start(s, avctx);
  1039. #endif
  1040. return 0;
  1041. }
  1042. /* generic function for encode/decode called after a frame has been coded/decoded */
  1043. void MPV_frame_end(MpegEncContext *s)
  1044. {
  1045. int i;
  1046. /* draw edge for correct motion prediction if outside */
  1047. #ifdef HAVE_XVMC
  1048. //just to make sure that all data is rendered.
  1049. if(s->avctx->xvmc_acceleration){
  1050. XVMC_field_end(s);
  1051. }else
  1052. #endif
  1053. if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1054. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  1055. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  1056. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  1057. }
  1058. emms_c();
  1059. s->last_pict_type = s->pict_type;
  1060. if(s->pict_type!=B_TYPE){
  1061. s->last_non_b_pict_type= s->pict_type;
  1062. }
  1063. #if 0
  1064. /* copy back current_picture variables */
  1065. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1066. if(s->picture[i].data[0] == s->current_picture.data[0]){
  1067. s->picture[i]= s->current_picture;
  1068. break;
  1069. }
  1070. }
  1071. assert(i<MAX_PICTURE_COUNT);
  1072. #endif
  1073. if(s->encoding){
  1074. /* release non refernce frames */
  1075. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1076. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  1077. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  1078. }
  1079. }
  1080. }
  1081. // clear copies, to avoid confusion
  1082. #if 0
  1083. memset(&s->last_picture, 0, sizeof(Picture));
  1084. memset(&s->next_picture, 0, sizeof(Picture));
  1085. memset(&s->current_picture, 0, sizeof(Picture));
  1086. #endif
  1087. }
  1088. /**
  1089. * draws an line from (ex, ey) -> (sx, sy).
  1090. * @param w width of the image
  1091. * @param h height of the image
  1092. * @param stride stride/linesize of the image
  1093. * @param color color of the arrow
  1094. */
  1095. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1096. int t, x, y, f;
  1097. sx= clip(sx, 0, w-1);
  1098. sy= clip(sy, 0, h-1);
  1099. ex= clip(ex, 0, w-1);
  1100. ey= clip(ey, 0, h-1);
  1101. buf[sy*stride + sx]+= color;
  1102. if(ABS(ex - sx) > ABS(ey - sy)){
  1103. if(sx > ex){
  1104. t=sx; sx=ex; ex=t;
  1105. t=sy; sy=ey; ey=t;
  1106. }
  1107. buf+= sx + sy*stride;
  1108. ex-= sx;
  1109. f= ((ey-sy)<<16)/ex;
  1110. for(x= 0; x <= ex; x++){
  1111. y= ((x*f) + (1<<15))>>16;
  1112. buf[y*stride + x]+= color;
  1113. }
  1114. }else{
  1115. if(sy > ey){
  1116. t=sx; sx=ex; ex=t;
  1117. t=sy; sy=ey; ey=t;
  1118. }
  1119. buf+= sx + sy*stride;
  1120. ey-= sy;
  1121. if(ey) f= ((ex-sx)<<16)/ey;
  1122. else f= 0;
  1123. for(y= 0; y <= ey; y++){
  1124. x= ((y*f) + (1<<15))>>16;
  1125. buf[y*stride + x]+= color;
  1126. }
  1127. }
  1128. }
  1129. /**
  1130. * draws an arrow from (ex, ey) -> (sx, sy).
  1131. * @param w width of the image
  1132. * @param h height of the image
  1133. * @param stride stride/linesize of the image
  1134. * @param color color of the arrow
  1135. */
  1136. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1137. int dx,dy;
  1138. sx= clip(sx, -100, w+100);
  1139. sy= clip(sy, -100, h+100);
  1140. ex= clip(ex, -100, w+100);
  1141. ey= clip(ey, -100, h+100);
  1142. dx= ex - sx;
  1143. dy= ey - sy;
  1144. if(dx*dx + dy*dy > 3*3){
  1145. int rx= dx + dy;
  1146. int ry= -dx + dy;
  1147. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1148. //FIXME subpixel accuracy
  1149. rx= ROUNDED_DIV(rx*3<<4, length);
  1150. ry= ROUNDED_DIV(ry*3<<4, length);
  1151. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1152. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1153. }
  1154. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1155. }
  1156. /**
  1157. * prints debuging info for the given picture.
  1158. */
  1159. void ff_print_debug_info(MpegEncContext *s, Picture *pict){
  1160. if(!pict || !pict->mb_type) return;
  1161. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1162. int x,y;
  1163. for(y=0; y<s->mb_height; y++){
  1164. for(x=0; x<s->mb_width; x++){
  1165. if(s->avctx->debug&FF_DEBUG_SKIP){
  1166. int count= s->mbskip_table[x + y*s->mb_stride];
  1167. if(count>9) count=9;
  1168. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1169. }
  1170. if(s->avctx->debug&FF_DEBUG_QP){
  1171. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1172. }
  1173. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1174. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1175. //Type & MV direction
  1176. if(IS_PCM(mb_type))
  1177. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1178. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1179. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1180. else if(IS_INTRA4x4(mb_type))
  1181. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1182. else if(IS_INTRA16x16(mb_type))
  1183. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1184. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1185. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1186. else if(IS_DIRECT(mb_type))
  1187. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1188. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1189. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1190. else if(IS_GMC(mb_type))
  1191. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1192. else if(IS_SKIP(mb_type))
  1193. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1194. else if(!USES_LIST(mb_type, 1))
  1195. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1196. else if(!USES_LIST(mb_type, 0))
  1197. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1198. else{
  1199. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1200. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1201. }
  1202. //segmentation
  1203. if(IS_8X8(mb_type))
  1204. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1205. else if(IS_16X8(mb_type))
  1206. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1207. else if(IS_8X16(mb_type))
  1208. av_log(s->avctx, AV_LOG_DEBUG, "¦");
  1209. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1210. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1211. else
  1212. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1213. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1214. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1215. else
  1216. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1217. }
  1218. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1219. }
  1220. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1221. }
  1222. }
  1223. if(s->avctx->debug&(FF_DEBUG_VIS_MV|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)){
  1224. const int shift= 1 + s->quarter_sample;
  1225. int mb_y;
  1226. uint8_t *ptr= pict->data[0];
  1227. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1228. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1229. int mb_x;
  1230. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1231. const int mb_index= mb_x + mb_y*s->mb_stride;
  1232. if((s->avctx->debug&FF_DEBUG_VIS_MV) && pict->motion_val){
  1233. if(IS_8X8(pict->mb_type[mb_index])){
  1234. int i;
  1235. for(i=0; i<4; i++){
  1236. int sx= mb_x*16 + 4 + 8*(i&1);
  1237. int sy= mb_y*16 + 4 + 8*(i>>1);
  1238. int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
  1239. int mx= (pict->motion_val[0][xy][0]>>shift) + sx;
  1240. int my= (pict->motion_val[0][xy][1]>>shift) + sy;
  1241. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  1242. }
  1243. }else if(IS_16X8(pict->mb_type[mb_index])){
  1244. int i;
  1245. for(i=0; i<2; i++){
  1246. int sx=mb_x*16 + 8;
  1247. int sy=mb_y*16 + 4 + 8*i;
  1248. int xy=1 + mb_x*2 + (mb_y*2 + 1 + i)*(s->mb_width*2 + 2);
  1249. int mx=(pict->motion_val[0][xy][0]>>shift) + sx;
  1250. int my=(pict->motion_val[0][xy][1]>>shift) + sy;
  1251. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  1252. }
  1253. }else{
  1254. int sx= mb_x*16 + 8;
  1255. int sy= mb_y*16 + 8;
  1256. int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
  1257. int mx= (pict->motion_val[0][xy][0]>>shift) + sx;
  1258. int my= (pict->motion_val[0][xy][1]>>shift) + sy;
  1259. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  1260. }
  1261. }
  1262. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1263. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1264. int y;
  1265. for(y=0; y<8; y++){
  1266. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
  1267. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
  1268. }
  1269. }
  1270. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1271. int mb_type= pict->mb_type[mb_index];
  1272. uint64_t u,v;
  1273. int y;
  1274. #define COLOR(theta, r)\
  1275. u= (int)(128 + r*cos(theta*3.141592/180));\
  1276. v= (int)(128 + r*sin(theta*3.141592/180));
  1277. u=v=128;
  1278. if(IS_PCM(mb_type)){
  1279. COLOR(120,48)
  1280. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1281. COLOR(30,48)
  1282. }else if(IS_INTRA4x4(mb_type)){
  1283. COLOR(90,48)
  1284. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1285. // COLOR(120,48)
  1286. }else if(IS_DIRECT(mb_type)){
  1287. COLOR(150,48)
  1288. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1289. COLOR(170,48)
  1290. }else if(IS_GMC(mb_type)){
  1291. COLOR(190,48)
  1292. }else if(IS_SKIP(mb_type)){
  1293. // COLOR(180,48)
  1294. }else if(!USES_LIST(mb_type, 1)){
  1295. COLOR(240,48)
  1296. }else if(!USES_LIST(mb_type, 0)){
  1297. COLOR(0,48)
  1298. }else{
  1299. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1300. COLOR(300,48)
  1301. }
  1302. u*= 0x0101010101010101ULL;
  1303. v*= 0x0101010101010101ULL;
  1304. for(y=0; y<8; y++){
  1305. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
  1306. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
  1307. }
  1308. //segmentation
  1309. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1310. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1311. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1312. }
  1313. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1314. for(y=0; y<16; y++)
  1315. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1316. }
  1317. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1318. // hmm
  1319. }
  1320. }
  1321. s->mbskip_table[mb_index]=0;
  1322. }
  1323. }
  1324. }
  1325. }
  1326. #ifdef CONFIG_ENCODERS
  1327. static int get_sae(uint8_t *src, int ref, int stride){
  1328. int x,y;
  1329. int acc=0;
  1330. for(y=0; y<16; y++){
  1331. for(x=0; x<16; x++){
  1332. acc+= ABS(src[x+y*stride] - ref);
  1333. }
  1334. }
  1335. return acc;
  1336. }
  1337. static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
  1338. int x, y, w, h;
  1339. int acc=0;
  1340. w= s->width &~15;
  1341. h= s->height&~15;
  1342. for(y=0; y<h; y+=16){
  1343. for(x=0; x<w; x+=16){
  1344. int offset= x + y*stride;
  1345. int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
  1346. int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
  1347. int sae = get_sae(src + offset, mean, stride);
  1348. acc+= sae + 500 < sad;
  1349. }
  1350. }
  1351. return acc;
  1352. }
  1353. static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
  1354. AVFrame *pic=NULL;
  1355. int i;
  1356. const int encoding_delay= s->max_b_frames;
  1357. int direct=1;
  1358. if(pic_arg){
  1359. if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
  1360. if(pic_arg->linesize[0] != s->linesize) direct=0;
  1361. if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
  1362. if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  1363. // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
  1364. if(direct){
  1365. i= ff_find_unused_picture(s, 1);
  1366. pic= (AVFrame*)&s->picture[i];
  1367. pic->reference= 3;
  1368. for(i=0; i<4; i++){
  1369. pic->data[i]= pic_arg->data[i];
  1370. pic->linesize[i]= pic_arg->linesize[i];
  1371. }
  1372. alloc_picture(s, (Picture*)pic, 1);
  1373. }else{
  1374. int offset= 16;
  1375. i= ff_find_unused_picture(s, 0);
  1376. pic= (AVFrame*)&s->picture[i];
  1377. pic->reference= 3;
  1378. alloc_picture(s, (Picture*)pic, 0);
  1379. if( pic->data[0] + offset == pic_arg->data[0]
  1380. && pic->data[1] + offset == pic_arg->data[1]
  1381. && pic->data[2] + offset == pic_arg->data[2]){
  1382. // empty
  1383. }else{
  1384. int h_chroma_shift, v_chroma_shift;
  1385. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1386. for(i=0; i<3; i++){
  1387. int src_stride= pic_arg->linesize[i];
  1388. int dst_stride= i ? s->uvlinesize : s->linesize;
  1389. int h_shift= i ? h_chroma_shift : 0;
  1390. int v_shift= i ? v_chroma_shift : 0;
  1391. int w= s->width >>h_shift;
  1392. int h= s->height>>v_shift;
  1393. uint8_t *src= pic_arg->data[i];
  1394. uint8_t *dst= pic->data[i] + offset;
  1395. if(src_stride==dst_stride)
  1396. memcpy(dst, src, src_stride*h);
  1397. else{
  1398. while(h--){
  1399. memcpy(dst, src, w);
  1400. dst += dst_stride;
  1401. src += src_stride;
  1402. }
  1403. }
  1404. }
  1405. }
  1406. }
  1407. pic->quality= pic_arg->quality;
  1408. pic->pict_type= pic_arg->pict_type;
  1409. pic->pts = pic_arg->pts;
  1410. pic->interlaced_frame = pic_arg->interlaced_frame;
  1411. pic->top_field_first = pic_arg->top_field_first;
  1412. if(s->input_picture[encoding_delay])
  1413. pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
  1414. }
  1415. /* shift buffer entries */
  1416. for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
  1417. s->input_picture[i-1]= s->input_picture[i];
  1418. s->input_picture[encoding_delay]= (Picture*)pic;
  1419. return 0;
  1420. }
  1421. static void select_input_picture(MpegEncContext *s){
  1422. int i;
  1423. int coded_pic_num=0;
  1424. if(s->reordered_input_picture[0])
  1425. coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
  1426. for(i=1; i<MAX_PICTURE_COUNT; i++)
  1427. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  1428. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  1429. /* set next picture types & ordering */
  1430. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  1431. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
  1432. s->reordered_input_picture[0]= s->input_picture[0];
  1433. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1434. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1435. }else{
  1436. int b_frames;
  1437. if(s->flags&CODEC_FLAG_PASS2){
  1438. for(i=0; i<s->max_b_frames+1; i++){
  1439. int pict_num= s->input_picture[0]->display_picture_number + i;
  1440. int pict_type= s->rc_context.entry[pict_num].new_pict_type;
  1441. s->input_picture[i]->pict_type= pict_type;
  1442. if(i + 1 >= s->rc_context.num_entries) break;
  1443. }
  1444. }
  1445. if(s->input_picture[0]->pict_type){
  1446. /* user selected pict_type */
  1447. for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
  1448. if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
  1449. }
  1450. if(b_frames > s->max_b_frames){
  1451. av_log(s->avctx, AV_LOG_ERROR, "warning, too many bframes in a row\n");
  1452. b_frames = s->max_b_frames;
  1453. }
  1454. }else if(s->avctx->b_frame_strategy==0){
  1455. b_frames= s->max_b_frames;
  1456. while(b_frames && !s->input_picture[b_frames]) b_frames--;
  1457. }else if(s->avctx->b_frame_strategy==1){
  1458. for(i=1; i<s->max_b_frames+1; i++){
  1459. if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
  1460. s->input_picture[i]->b_frame_score=
  1461. get_intra_count(s, s->input_picture[i ]->data[0],
  1462. s->input_picture[i-1]->data[0], s->linesize) + 1;
  1463. }
  1464. }
  1465. for(i=0; i<s->max_b_frames; i++){
  1466. if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
  1467. }
  1468. b_frames= FFMAX(0, i-1);
  1469. /* reset scores */
  1470. for(i=0; i<b_frames+1; i++){
  1471. s->input_picture[i]->b_frame_score=0;
  1472. }
  1473. }else{
  1474. av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
  1475. b_frames=0;
  1476. }
  1477. emms_c();
  1478. //static int b_count=0;
  1479. //b_count+= b_frames;
  1480. //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
  1481. s->reordered_input_picture[0]= s->input_picture[b_frames];
  1482. if( s->picture_in_gop_number + b_frames >= s->gop_size
  1483. || s->reordered_input_picture[0]->pict_type== I_TYPE)
  1484. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1485. else
  1486. s->reordered_input_picture[0]->pict_type= P_TYPE;
  1487. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1488. for(i=0; i<b_frames; i++){
  1489. coded_pic_num++;
  1490. s->reordered_input_picture[i+1]= s->input_picture[i];
  1491. s->reordered_input_picture[i+1]->pict_type= B_TYPE;
  1492. s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
  1493. }
  1494. }
  1495. }
  1496. if(s->reordered_input_picture[0]){
  1497. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
  1498. copy_picture(&s->new_picture, s->reordered_input_picture[0]);
  1499. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1500. // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
  1501. int i= ff_find_unused_picture(s, 0);
  1502. Picture *pic= &s->picture[i];
  1503. /* mark us unused / free shared pic */
  1504. for(i=0; i<4; i++)
  1505. s->reordered_input_picture[0]->data[i]= NULL;
  1506. s->reordered_input_picture[0]->type= 0;
  1507. //FIXME bad, copy * except
  1508. pic->pict_type = s->reordered_input_picture[0]->pict_type;
  1509. pic->quality = s->reordered_input_picture[0]->quality;
  1510. pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
  1511. pic->reference = s->reordered_input_picture[0]->reference;
  1512. pic->pts = s->reordered_input_picture[0]->pts;
  1513. alloc_picture(s, pic, 0);
  1514. s->current_picture_ptr= pic;
  1515. }else{
  1516. // input is not a shared pix -> reuse buffer for current_pix
  1517. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1518. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1519. s->current_picture_ptr= s->reordered_input_picture[0];
  1520. for(i=0; i<4; i++){
  1521. s->new_picture.data[i]+=16;
  1522. }
  1523. }
  1524. copy_picture(&s->current_picture, s->current_picture_ptr);
  1525. s->picture_number= s->new_picture.display_picture_number;
  1526. //printf("dpn:%d\n", s->picture_number);
  1527. }else{
  1528. memset(&s->new_picture, 0, sizeof(Picture));
  1529. }
  1530. }
  1531. int MPV_encode_picture(AVCodecContext *avctx,
  1532. unsigned char *buf, int buf_size, void *data)
  1533. {
  1534. MpegEncContext *s = avctx->priv_data;
  1535. AVFrame *pic_arg = data;
  1536. int i, stuffing_count;
  1537. if(avctx->pix_fmt != PIX_FMT_YUV420P){
  1538. av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
  1539. return -1;
  1540. }
  1541. init_put_bits(&s->pb, buf, buf_size);
  1542. s->picture_in_gop_number++;
  1543. load_input_picture(s, pic_arg);
  1544. select_input_picture(s);
  1545. /* output? */
  1546. if(s->new_picture.data[0]){
  1547. s->pict_type= s->new_picture.pict_type;
  1548. //emms_c();
  1549. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1550. MPV_frame_start(s, avctx);
  1551. encode_picture(s, s->picture_number);
  1552. avctx->real_pict_num = s->picture_number;
  1553. avctx->header_bits = s->header_bits;
  1554. avctx->mv_bits = s->mv_bits;
  1555. avctx->misc_bits = s->misc_bits;
  1556. avctx->i_tex_bits = s->i_tex_bits;
  1557. avctx->p_tex_bits = s->p_tex_bits;
  1558. avctx->i_count = s->i_count;
  1559. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  1560. avctx->skip_count = s->skip_count;
  1561. MPV_frame_end(s);
  1562. if (s->out_format == FMT_MJPEG)
  1563. mjpeg_picture_trailer(s);
  1564. if(s->flags&CODEC_FLAG_PASS1)
  1565. ff_write_pass1_stats(s);
  1566. for(i=0; i<4; i++){
  1567. avctx->error[i] += s->current_picture_ptr->error[i];
  1568. }
  1569. }
  1570. s->input_picture_number++;
  1571. flush_put_bits(&s->pb);
  1572. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1573. stuffing_count= ff_vbv_update(s, s->frame_bits);
  1574. if(stuffing_count){
  1575. switch(s->codec_id){
  1576. case CODEC_ID_MPEG1VIDEO:
  1577. case CODEC_ID_MPEG2VIDEO:
  1578. while(stuffing_count--){
  1579. put_bits(&s->pb, 8, 0);
  1580. }
  1581. break;
  1582. case CODEC_ID_MPEG4:
  1583. put_bits(&s->pb, 16, 0);
  1584. put_bits(&s->pb, 16, 0x1C3);
  1585. stuffing_count -= 4;
  1586. while(stuffing_count--){
  1587. put_bits(&s->pb, 8, 0xFF);
  1588. }
  1589. break;
  1590. default:
  1591. av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  1592. }
  1593. flush_put_bits(&s->pb);
  1594. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1595. }
  1596. s->total_bits += s->frame_bits;
  1597. avctx->frame_bits = s->frame_bits;
  1598. return s->frame_bits/8;
  1599. }
  1600. #endif //CONFIG_ENCODERS
  1601. static inline void gmc1_motion(MpegEncContext *s,
  1602. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1603. int dest_offset,
  1604. uint8_t **ref_picture, int src_offset)
  1605. {
  1606. uint8_t *ptr;
  1607. int offset, src_x, src_y, linesize, uvlinesize;
  1608. int motion_x, motion_y;
  1609. int emu=0;
  1610. motion_x= s->sprite_offset[0][0];
  1611. motion_y= s->sprite_offset[0][1];
  1612. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  1613. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  1614. motion_x<<=(3-s->sprite_warping_accuracy);
  1615. motion_y<<=(3-s->sprite_warping_accuracy);
  1616. src_x = clip(src_x, -16, s->width);
  1617. if (src_x == s->width)
  1618. motion_x =0;
  1619. src_y = clip(src_y, -16, s->height);
  1620. if (src_y == s->height)
  1621. motion_y =0;
  1622. linesize = s->linesize;
  1623. uvlinesize = s->uvlinesize;
  1624. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1625. dest_y+=dest_offset;
  1626. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1627. if( (unsigned)src_x >= s->h_edge_pos - 17
  1628. || (unsigned)src_y >= s->v_edge_pos - 17){
  1629. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1630. ptr= s->edge_emu_buffer;
  1631. }
  1632. }
  1633. if((motion_x|motion_y)&7){
  1634. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1635. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1636. }else{
  1637. int dxy;
  1638. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  1639. if (s->no_rounding){
  1640. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  1641. }else{
  1642. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  1643. }
  1644. }
  1645. if(s->flags&CODEC_FLAG_GRAY) return;
  1646. motion_x= s->sprite_offset[1][0];
  1647. motion_y= s->sprite_offset[1][1];
  1648. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  1649. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  1650. motion_x<<=(3-s->sprite_warping_accuracy);
  1651. motion_y<<=(3-s->sprite_warping_accuracy);
  1652. src_x = clip(src_x, -8, s->width>>1);
  1653. if (src_x == s->width>>1)
  1654. motion_x =0;
  1655. src_y = clip(src_y, -8, s->height>>1);
  1656. if (src_y == s->height>>1)
  1657. motion_y =0;
  1658. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  1659. ptr = ref_picture[1] + offset;
  1660. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1661. if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9
  1662. || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
  1663. 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);
  1664. ptr= s->edge_emu_buffer;
  1665. emu=1;
  1666. }
  1667. }
  1668. s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1669. ptr = ref_picture[2] + offset;
  1670. if(emu){
  1671. 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);
  1672. ptr= s->edge_emu_buffer;
  1673. }
  1674. s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1675. return;
  1676. }
  1677. static inline void gmc_motion(MpegEncContext *s,
  1678. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1679. int dest_offset,
  1680. uint8_t **ref_picture, int src_offset)
  1681. {
  1682. uint8_t *ptr;
  1683. int linesize, uvlinesize;
  1684. const int a= s->sprite_warping_accuracy;
  1685. int ox, oy;
  1686. linesize = s->linesize;
  1687. uvlinesize = s->uvlinesize;
  1688. ptr = ref_picture[0] + src_offset;
  1689. dest_y+=dest_offset;
  1690. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  1691. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  1692. s->dsp.gmc(dest_y, ptr, linesize, 16,
  1693. ox,
  1694. oy,
  1695. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1696. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1697. a+1, (1<<(2*a+1)) - s->no_rounding,
  1698. s->h_edge_pos, s->v_edge_pos);
  1699. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  1700. ox + s->sprite_delta[0][0]*8,
  1701. oy + s->sprite_delta[1][0]*8,
  1702. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1703. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1704. a+1, (1<<(2*a+1)) - s->no_rounding,
  1705. s->h_edge_pos, s->v_edge_pos);
  1706. if(s->flags&CODEC_FLAG_GRAY) return;
  1707. dest_cb+=dest_offset>>1;
  1708. dest_cr+=dest_offset>>1;
  1709. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  1710. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  1711. ptr = ref_picture[1] + (src_offset>>1);
  1712. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  1713. ox,
  1714. oy,
  1715. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1716. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1717. a+1, (1<<(2*a+1)) - s->no_rounding,
  1718. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1719. ptr = ref_picture[2] + (src_offset>>1);
  1720. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  1721. ox,
  1722. oy,
  1723. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1724. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1725. a+1, (1<<(2*a+1)) - s->no_rounding,
  1726. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1727. }
  1728. /**
  1729. * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
  1730. * @param buf destination buffer
  1731. * @param src source buffer
  1732. * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
  1733. * @param block_w width of block
  1734. * @param block_h height of block
  1735. * @param src_x x coordinate of the top left sample of the block in the source buffer
  1736. * @param src_y y coordinate of the top left sample of the block in the source buffer
  1737. * @param w width of the source buffer
  1738. * @param h height of the source buffer
  1739. */
  1740. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  1741. int src_x, int src_y, int w, int h){
  1742. int x, y;
  1743. int start_y, start_x, end_y, end_x;
  1744. if(src_y>= h){
  1745. src+= (h-1-src_y)*linesize;
  1746. src_y=h-1;
  1747. }else if(src_y<=-block_h){
  1748. src+= (1-block_h-src_y)*linesize;
  1749. src_y=1-block_h;
  1750. }
  1751. if(src_x>= w){
  1752. src+= (w-1-src_x);
  1753. src_x=w-1;
  1754. }else if(src_x<=-block_w){
  1755. src+= (1-block_w-src_x);
  1756. src_x=1-block_w;
  1757. }
  1758. start_y= FFMAX(0, -src_y);
  1759. start_x= FFMAX(0, -src_x);
  1760. end_y= FFMIN(block_h, h-src_y);
  1761. end_x= FFMIN(block_w, w-src_x);
  1762. // copy existing part
  1763. for(y=start_y; y<end_y; y++){
  1764. for(x=start_x; x<end_x; x++){
  1765. buf[x + y*linesize]= src[x + y*linesize];
  1766. }
  1767. }
  1768. //top
  1769. for(y=0; y<start_y; y++){
  1770. for(x=start_x; x<end_x; x++){
  1771. buf[x + y*linesize]= buf[x + start_y*linesize];
  1772. }
  1773. }
  1774. //bottom
  1775. for(y=end_y; y<block_h; y++){
  1776. for(x=start_x; x<end_x; x++){
  1777. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1778. }
  1779. }
  1780. for(y=0; y<block_h; y++){
  1781. //left
  1782. for(x=0; x<start_x; x++){
  1783. buf[x + y*linesize]= buf[start_x + y*linesize];
  1784. }
  1785. //right
  1786. for(x=end_x; x<block_w; x++){
  1787. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1788. }
  1789. }
  1790. }
  1791. static inline int hpel_motion(MpegEncContext *s,
  1792. uint8_t *dest, uint8_t *src,
  1793. int src_x, int src_y,
  1794. int width, int height, int stride,
  1795. int h_edge_pos, int v_edge_pos,
  1796. int w, int h, op_pixels_func *pix_op,
  1797. int motion_x, int motion_y)
  1798. {
  1799. int dxy;
  1800. int emu=0;
  1801. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1802. src_x += motion_x >> 1;
  1803. src_y += motion_y >> 1;
  1804. /* WARNING: do no forget half pels */
  1805. src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
  1806. if (src_x == width)
  1807. dxy &= ~1;
  1808. src_y = clip(src_y, -16, height);
  1809. if (src_y == height)
  1810. dxy &= ~2;
  1811. src += src_y * stride + src_x;
  1812. if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
  1813. if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w
  1814. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  1815. ff_emulated_edge_mc(s->edge_emu_buffer, src, stride, w+1, h+1,
  1816. src_x, src_y, h_edge_pos, v_edge_pos);
  1817. src= s->edge_emu_buffer;
  1818. emu=1;
  1819. }
  1820. }
  1821. pix_op[dxy](dest, src, stride, h);
  1822. return emu;
  1823. }
  1824. /* apply one mpeg motion vector to the three components */
  1825. static inline void mpeg_motion(MpegEncContext *s,
  1826. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1827. int dest_offset,
  1828. uint8_t **ref_picture, int src_offset,
  1829. int field_based, op_pixels_func (*pix_op)[4],
  1830. int motion_x, int motion_y, int h)
  1831. {
  1832. uint8_t *ptr;
  1833. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, uvlinesize;
  1834. int emu=0;
  1835. #if 0
  1836. if(s->quarter_sample)
  1837. {
  1838. motion_x>>=1;
  1839. motion_y>>=1;
  1840. }
  1841. #endif
  1842. height = s->height >> field_based;
  1843. v_edge_pos = s->v_edge_pos >> field_based;
  1844. uvlinesize = s->current_picture.linesize[1] << field_based;
  1845. emu= hpel_motion(s,
  1846. dest_y + dest_offset, ref_picture[0] + src_offset,
  1847. s->mb_x * 16, s->mb_y * (16 >> field_based),
  1848. s->width, height, s->current_picture.linesize[0] << field_based,
  1849. s->h_edge_pos, v_edge_pos,
  1850. 16, h, pix_op[0],
  1851. motion_x, motion_y);
  1852. if(s->flags&CODEC_FLAG_GRAY) return;
  1853. if (s->out_format == FMT_H263) {
  1854. dxy = 0;
  1855. if ((motion_x & 3) != 0)
  1856. dxy |= 1;
  1857. if ((motion_y & 3) != 0)
  1858. dxy |= 2;
  1859. mx = motion_x >> 2;
  1860. my = motion_y >> 2;
  1861. } else {
  1862. mx = motion_x / 2;
  1863. my = motion_y / 2;
  1864. dxy = ((my & 1) << 1) | (mx & 1);
  1865. mx >>= 1;
  1866. my >>= 1;
  1867. }
  1868. src_x = s->mb_x * 8 + mx;
  1869. src_y = s->mb_y * (8 >> field_based) + my;
  1870. src_x = clip(src_x, -8, s->width >> 1);
  1871. if (src_x == (s->width >> 1))
  1872. dxy &= ~1;
  1873. src_y = clip(src_y, -8, height >> 1);
  1874. if (src_y == (height >> 1))
  1875. dxy &= ~2;
  1876. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1877. ptr = ref_picture[1] + offset;
  1878. if(emu){
  1879. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1880. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1881. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1882. }
  1883. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1884. ptr = ref_picture[2] + offset;
  1885. if(emu){
  1886. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1887. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1888. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1889. }
  1890. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1891. }
  1892. //FIXME move to dsputil, avg variant, 16x16 version
  1893. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
  1894. int x;
  1895. uint8_t * const top = src[1];
  1896. uint8_t * const left = src[2];
  1897. uint8_t * const mid = src[0];
  1898. uint8_t * const right = src[3];
  1899. uint8_t * const bottom= src[4];
  1900. #define OBMC_FILTER(x, t, l, m, r, b)\
  1901. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  1902. #define OBMC_FILTER4(x, t, l, m, r, b)\
  1903. OBMC_FILTER(x , t, l, m, r, b);\
  1904. OBMC_FILTER(x+1 , t, l, m, r, b);\
  1905. OBMC_FILTER(x +stride, t, l, m, r, b);\
  1906. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  1907. x=0;
  1908. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  1909. OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
  1910. OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
  1911. OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
  1912. OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
  1913. OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
  1914. x+= stride;
  1915. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  1916. OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
  1917. OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
  1918. OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
  1919. x+= stride;
  1920. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  1921. OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
  1922. OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
  1923. OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
  1924. x+= 2*stride;
  1925. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  1926. OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
  1927. OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
  1928. OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
  1929. x+= 2*stride;
  1930. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  1931. OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
  1932. OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
  1933. OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
  1934. OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
  1935. OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
  1936. x+= stride;
  1937. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  1938. OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
  1939. OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
  1940. OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
  1941. }
  1942. /* obmc for 1 8x8 luma block */
  1943. static inline void obmc_motion(MpegEncContext *s,
  1944. uint8_t *dest, uint8_t *src,
  1945. int src_x, int src_y,
  1946. op_pixels_func *pix_op,
  1947. int16_t mv[5][2]/* mid top left right bottom*/)
  1948. #define MID 0
  1949. {
  1950. int i;
  1951. uint8_t *ptr[5];
  1952. assert(s->quarter_sample==0);
  1953. for(i=0; i<5; i++){
  1954. if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
  1955. ptr[i]= ptr[MID];
  1956. }else{
  1957. ptr[i]= s->edge_emu_buffer + 16 + 8*(i&1) + s->linesize*8*(i>>1);
  1958. hpel_motion(s, ptr[i], src,
  1959. src_x, src_y,
  1960. s->width, s->height, s->linesize,
  1961. s->h_edge_pos, s->v_edge_pos,
  1962. 8, 8, pix_op,
  1963. mv[i][0], mv[i][1]);
  1964. }
  1965. }
  1966. put_obmc(dest, ptr, s->linesize);
  1967. }
  1968. static inline void qpel_motion(MpegEncContext *s,
  1969. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1970. int dest_offset,
  1971. uint8_t **ref_picture, int src_offset,
  1972. int field_based, op_pixels_func (*pix_op)[4],
  1973. qpel_mc_func (*qpix_op)[16],
  1974. int motion_x, int motion_y, int h)
  1975. {
  1976. uint8_t *ptr;
  1977. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1978. int emu=0;
  1979. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1980. src_x = s->mb_x * 16 + (motion_x >> 2);
  1981. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  1982. height = s->height >> field_based;
  1983. v_edge_pos = s->v_edge_pos >> field_based;
  1984. src_x = clip(src_x, -16, s->width);
  1985. if (src_x == s->width)
  1986. dxy &= ~3;
  1987. src_y = clip(src_y, -16, height);
  1988. if (src_y == height)
  1989. dxy &= ~12;
  1990. linesize = s->linesize << field_based;
  1991. uvlinesize = s->uvlinesize << field_based;
  1992. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1993. dest_y += dest_offset;
  1994. //printf("%d %d %d\n", src_x, src_y, dxy);
  1995. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1996. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
  1997. || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
  1998. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based,
  1999. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  2000. ptr= s->edge_emu_buffer + src_offset;
  2001. emu=1;
  2002. }
  2003. }
  2004. if(!field_based)
  2005. qpix_op[0][dxy](dest_y, ptr, linesize);
  2006. else{
  2007. //damn interlaced mode
  2008. //FIXME boundary mirroring is not exactly correct here
  2009. qpix_op[1][dxy](dest_y , ptr , linesize);
  2010. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  2011. }
  2012. if(s->flags&CODEC_FLAG_GRAY) return;
  2013. if(field_based){
  2014. mx= motion_x/2;
  2015. my= motion_y>>1;
  2016. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  2017. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  2018. mx= (motion_x>>1) + rtab[motion_x&7];
  2019. my= (motion_y>>1) + rtab[motion_y&7];
  2020. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  2021. mx= (motion_x>>1)|(motion_x&1);
  2022. my= (motion_y>>1)|(motion_y&1);
  2023. }else{
  2024. mx= motion_x/2;
  2025. my= motion_y/2;
  2026. }
  2027. mx= (mx>>1)|(mx&1);
  2028. my= (my>>1)|(my&1);
  2029. dxy= (mx&1) | ((my&1)<<1);
  2030. mx>>=1;
  2031. my>>=1;
  2032. src_x = s->mb_x * 8 + mx;
  2033. src_y = s->mb_y * (8 >> field_based) + my;
  2034. src_x = clip(src_x, -8, s->width >> 1);
  2035. if (src_x == (s->width >> 1))
  2036. dxy &= ~1;
  2037. src_y = clip(src_y, -8, height >> 1);
  2038. if (src_y == (height >> 1))
  2039. dxy &= ~2;
  2040. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  2041. ptr = ref_picture[1] + offset;
  2042. if(emu){
  2043. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  2044. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2045. ptr= s->edge_emu_buffer + (src_offset >> 1);
  2046. }
  2047. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  2048. ptr = ref_picture[2] + offset;
  2049. if(emu){
  2050. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  2051. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2052. ptr= s->edge_emu_buffer + (src_offset >> 1);
  2053. }
  2054. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  2055. }
  2056. inline int ff_h263_round_chroma(int x){
  2057. if (x >= 0)
  2058. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  2059. else {
  2060. x = -x;
  2061. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  2062. }
  2063. }
  2064. /**
  2065. * h263 chorma 4mv motion compensation.
  2066. */
  2067. static inline void chroma_4mv_motion(MpegEncContext *s,
  2068. uint8_t *dest_cb, uint8_t *dest_cr,
  2069. uint8_t **ref_picture,
  2070. op_pixels_func *pix_op,
  2071. int mx, int my){
  2072. int dxy, emu=0, src_x, src_y, offset;
  2073. uint8_t *ptr;
  2074. /* In case of 8X8, we construct a single chroma motion vector
  2075. with a special rounding */
  2076. mx= ff_h263_round_chroma(mx);
  2077. my= ff_h263_round_chroma(my);
  2078. dxy = ((my & 1) << 1) | (mx & 1);
  2079. mx >>= 1;
  2080. my >>= 1;
  2081. src_x = s->mb_x * 8 + mx;
  2082. src_y = s->mb_y * 8 + my;
  2083. src_x = clip(src_x, -8, s->width/2);
  2084. if (src_x == s->width/2)
  2085. dxy &= ~1;
  2086. src_y = clip(src_y, -8, s->height/2);
  2087. if (src_y == s->height/2)
  2088. dxy &= ~2;
  2089. offset = (src_y * (s->uvlinesize)) + src_x;
  2090. ptr = ref_picture[1] + offset;
  2091. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2092. if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
  2093. || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
  2094. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2095. ptr= s->edge_emu_buffer;
  2096. emu=1;
  2097. }
  2098. }
  2099. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  2100. ptr = ref_picture[2] + offset;
  2101. if(emu){
  2102. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2103. ptr= s->edge_emu_buffer;
  2104. }
  2105. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  2106. }
  2107. /**
  2108. * motion compesation of a single macroblock
  2109. * @param s context
  2110. * @param dest_y luma destination pointer
  2111. * @param dest_cb chroma cb/u destination pointer
  2112. * @param dest_cr chroma cr/v destination pointer
  2113. * @param dir direction (0->forward, 1->backward)
  2114. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2115. * @param pic_op halfpel motion compensation function (average or put normally)
  2116. * @param pic_op qpel motion compensation function (average or put normally)
  2117. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2118. */
  2119. static inline void MPV_motion(MpegEncContext *s,
  2120. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2121. int dir, uint8_t **ref_picture,
  2122. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  2123. {
  2124. int dxy, mx, my, src_x, src_y, motion_x, motion_y;
  2125. int mb_x, mb_y, i;
  2126. uint8_t *ptr, *dest;
  2127. mb_x = s->mb_x;
  2128. mb_y = s->mb_y;
  2129. if(s->obmc && s->pict_type != B_TYPE){
  2130. int16_t mv_cache[4][4][2];
  2131. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  2132. const int mot_stride= s->mb_width*2 + 2;
  2133. const int mot_xy= 1 + mb_x*2 + (mb_y*2 + 1)*mot_stride;
  2134. assert(!s->mb_skiped);
  2135. memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
  2136. memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  2137. memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  2138. if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
  2139. memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
  2140. }else{
  2141. memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
  2142. }
  2143. if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
  2144. *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
  2145. *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
  2146. }else{
  2147. *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
  2148. *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
  2149. }
  2150. if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
  2151. *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
  2152. *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
  2153. }else{
  2154. *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
  2155. *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
  2156. }
  2157. mx = 0;
  2158. my = 0;
  2159. for(i=0;i<4;i++) {
  2160. const int x= (i&1)+1;
  2161. const int y= (i>>1)+1;
  2162. int16_t mv[5][2]= {
  2163. {mv_cache[y][x ][0], mv_cache[y][x ][1]},
  2164. {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
  2165. {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
  2166. {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
  2167. {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
  2168. //FIXME cleanup
  2169. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2170. ref_picture[0],
  2171. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2172. pix_op[1],
  2173. mv);
  2174. mx += mv[0][0];
  2175. my += mv[0][1];
  2176. }
  2177. if(!(s->flags&CODEC_FLAG_GRAY))
  2178. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2179. return;
  2180. }
  2181. switch(s->mv_type) {
  2182. case MV_TYPE_16X16:
  2183. #ifdef CONFIG_RISKY
  2184. if(s->mcsel){
  2185. if(s->real_sprite_warping_points==1){
  2186. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  2187. ref_picture, 0);
  2188. }else{
  2189. gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
  2190. ref_picture, 0);
  2191. }
  2192. }else if(s->quarter_sample){
  2193. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  2194. ref_picture, 0,
  2195. 0, pix_op, qpix_op,
  2196. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2197. }else if(s->mspel){
  2198. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  2199. ref_picture, pix_op,
  2200. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2201. }else
  2202. #endif
  2203. {
  2204. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2205. ref_picture, 0,
  2206. 0, pix_op,
  2207. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2208. }
  2209. break;
  2210. case MV_TYPE_8X8:
  2211. mx = 0;
  2212. my = 0;
  2213. if(s->quarter_sample){
  2214. for(i=0;i<4;i++) {
  2215. motion_x = s->mv[dir][i][0];
  2216. motion_y = s->mv[dir][i][1];
  2217. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  2218. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  2219. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  2220. /* WARNING: do no forget half pels */
  2221. src_x = clip(src_x, -16, s->width);
  2222. if (src_x == s->width)
  2223. dxy &= ~3;
  2224. src_y = clip(src_y, -16, s->height);
  2225. if (src_y == s->height)
  2226. dxy &= ~12;
  2227. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  2228. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2229. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
  2230. || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
  2231. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  2232. ptr= s->edge_emu_buffer;
  2233. }
  2234. }
  2235. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  2236. qpix_op[1][dxy](dest, ptr, s->linesize);
  2237. mx += s->mv[dir][i][0]/2;
  2238. my += s->mv[dir][i][1]/2;
  2239. }
  2240. }else{
  2241. for(i=0;i<4;i++) {
  2242. hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2243. ref_picture[0],
  2244. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2245. s->width, s->height, s->linesize,
  2246. s->h_edge_pos, s->v_edge_pos,
  2247. 8, 8, pix_op[1],
  2248. s->mv[dir][i][0], s->mv[dir][i][1]);
  2249. mx += s->mv[dir][i][0];
  2250. my += s->mv[dir][i][1];
  2251. }
  2252. }
  2253. if(!(s->flags&CODEC_FLAG_GRAY))
  2254. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2255. break;
  2256. case MV_TYPE_FIELD:
  2257. if (s->picture_structure == PICT_FRAME) {
  2258. if(s->quarter_sample){
  2259. /* top field */
  2260. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  2261. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  2262. 1, pix_op, qpix_op,
  2263. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2264. /* bottom field */
  2265. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2266. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  2267. 1, pix_op, qpix_op,
  2268. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  2269. }else{
  2270. /* top field */
  2271. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2272. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  2273. 1, pix_op,
  2274. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2275. /* bottom field */
  2276. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2277. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  2278. 1, pix_op,
  2279. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  2280. }
  2281. } else {
  2282. int offset;
  2283. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  2284. offset= s->field_select[dir][0] ? s->linesize : 0;
  2285. }else{
  2286. ref_picture= s->current_picture.data;
  2287. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  2288. }
  2289. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2290. ref_picture, offset,
  2291. 0, pix_op,
  2292. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2293. }
  2294. break;
  2295. case MV_TYPE_16X8:{
  2296. int offset;
  2297. uint8_t ** ref2picture;
  2298. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  2299. ref2picture= ref_picture;
  2300. offset= s->field_select[dir][0] ? s->linesize : 0;
  2301. }else{
  2302. ref2picture= s->current_picture.data;
  2303. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  2304. }
  2305. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2306. ref2picture, offset,
  2307. 0, pix_op,
  2308. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2309. if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){
  2310. ref2picture= ref_picture;
  2311. offset= s->field_select[dir][1] ? s->linesize : 0;
  2312. }else{
  2313. ref2picture= s->current_picture.data;
  2314. offset= s->field_select[dir][1] ? s->linesize : -s->linesize;
  2315. }
  2316. // I know it is ugly but this is the only way to fool emu_edge without rewrite mpeg_motion
  2317. mpeg_motion(s, dest_y+16*s->linesize, dest_cb+8*s->uvlinesize, dest_cr+8*s->uvlinesize,
  2318. 0,
  2319. ref2picture, offset,
  2320. 0, pix_op,
  2321. s->mv[dir][1][0], s->mv[dir][1][1]+16, 8);
  2322. }
  2323. break;
  2324. case MV_TYPE_DMV:
  2325. {
  2326. op_pixels_func (*dmv_pix_op)[4];
  2327. int offset;
  2328. dmv_pix_op = s->dsp.put_pixels_tab;
  2329. if(s->picture_structure == PICT_FRAME){
  2330. //put top field from top field
  2331. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2332. ref_picture, 0,
  2333. 1, dmv_pix_op,
  2334. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2335. //put bottom field from bottom field
  2336. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2337. ref_picture, s->linesize,
  2338. 1, dmv_pix_op,
  2339. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2340. dmv_pix_op = s->dsp.avg_pixels_tab;
  2341. //avg top field from bottom field
  2342. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2343. ref_picture, s->linesize,
  2344. 1, dmv_pix_op,
  2345. s->mv[dir][2][0], s->mv[dir][2][1], 8);
  2346. //avg bottom field from top field
  2347. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2348. ref_picture, 0,
  2349. 1, dmv_pix_op,
  2350. s->mv[dir][3][0], s->mv[dir][3][1], 8);
  2351. }else{
  2352. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2353. s->linesize : 0;
  2354. //put field from the same parity
  2355. //same parity is never in the same frame
  2356. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2357. ref_picture,offset,
  2358. 0,dmv_pix_op,
  2359. s->mv[dir][0][0],s->mv[dir][0][1],16);
  2360. // after put we make avg of the same block
  2361. dmv_pix_op=s->dsp.avg_pixels_tab;
  2362. //opposite parity is always in the same frame if this is second field
  2363. if(!s->first_field){
  2364. ref_picture = s->current_picture.data;
  2365. //top field is one linesize from frame beginig
  2366. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2367. -s->linesize : s->linesize;
  2368. }else
  2369. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2370. 0 : s->linesize;
  2371. //avg field from the opposite parity
  2372. mpeg_motion(s, dest_y, dest_cb, dest_cr,0,
  2373. ref_picture, offset,
  2374. 0,dmv_pix_op,
  2375. s->mv[dir][2][0],s->mv[dir][2][1],16);
  2376. }
  2377. }
  2378. break;
  2379. default: assert(0);
  2380. }
  2381. }
  2382. /* put block[] to dest[] */
  2383. static inline void put_dct(MpegEncContext *s,
  2384. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  2385. {
  2386. s->dct_unquantize_intra(s, block, i, qscale);
  2387. s->dsp.idct_put (dest, line_size, block);
  2388. }
  2389. /* add block[] to dest[] */
  2390. static inline void add_dct(MpegEncContext *s,
  2391. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2392. {
  2393. if (s->block_last_index[i] >= 0) {
  2394. s->dsp.idct_add (dest, line_size, block);
  2395. }
  2396. }
  2397. static inline void add_dequant_dct(MpegEncContext *s,
  2398. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  2399. {
  2400. if (s->block_last_index[i] >= 0) {
  2401. s->dct_unquantize_inter(s, block, i, qscale);
  2402. s->dsp.idct_add (dest, line_size, block);
  2403. }
  2404. }
  2405. /**
  2406. * cleans dc, ac, coded_block for the current non intra MB
  2407. */
  2408. void ff_clean_intra_table_entries(MpegEncContext *s)
  2409. {
  2410. int wrap = s->block_wrap[0];
  2411. int xy = s->block_index[0];
  2412. s->dc_val[0][xy ] =
  2413. s->dc_val[0][xy + 1 ] =
  2414. s->dc_val[0][xy + wrap] =
  2415. s->dc_val[0][xy + 1 + wrap] = 1024;
  2416. /* ac pred */
  2417. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2418. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2419. if (s->msmpeg4_version>=3) {
  2420. s->coded_block[xy ] =
  2421. s->coded_block[xy + 1 ] =
  2422. s->coded_block[xy + wrap] =
  2423. s->coded_block[xy + 1 + wrap] = 0;
  2424. }
  2425. /* chroma */
  2426. wrap = s->block_wrap[4];
  2427. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  2428. s->dc_val[1][xy] =
  2429. s->dc_val[2][xy] = 1024;
  2430. /* ac pred */
  2431. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2432. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2433. s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
  2434. }
  2435. /* generic function called after a macroblock has been parsed by the
  2436. decoder or after it has been encoded by the encoder.
  2437. Important variables used:
  2438. s->mb_intra : true if intra macroblock
  2439. s->mv_dir : motion vector direction
  2440. s->mv_type : motion vector type
  2441. s->mv : motion vector
  2442. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2443. */
  2444. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  2445. {
  2446. int mb_x, mb_y;
  2447. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2448. #ifdef HAVE_XVMC
  2449. if(s->avctx->xvmc_acceleration){
  2450. XVMC_decode_mb(s);//xvmc uses pblocks
  2451. return;
  2452. }
  2453. #endif
  2454. mb_x = s->mb_x;
  2455. mb_y = s->mb_y;
  2456. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2457. /* update DC predictors for P macroblocks */
  2458. if (!s->mb_intra) {
  2459. if (s->h263_pred || s->h263_aic) {
  2460. if(s->mbintra_table[mb_xy])
  2461. ff_clean_intra_table_entries(s);
  2462. } else {
  2463. s->last_dc[0] =
  2464. s->last_dc[1] =
  2465. s->last_dc[2] = 128 << s->intra_dc_precision;
  2466. }
  2467. }
  2468. else if (s->h263_pred || s->h263_aic)
  2469. s->mbintra_table[mb_xy]=1;
  2470. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  2471. uint8_t *dest_y, *dest_cb, *dest_cr;
  2472. int dct_linesize, dct_offset;
  2473. op_pixels_func (*op_pix)[4];
  2474. qpel_mc_func (*op_qpix)[16];
  2475. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2476. const int uvlinesize= s->current_picture.linesize[1];
  2477. const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band;
  2478. /* avoid copy if macroblock skipped in last frame too */
  2479. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2480. if(!s->encoding){
  2481. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2482. const int age= s->current_picture.age;
  2483. assert(age);
  2484. if (s->mb_skiped) {
  2485. s->mb_skiped= 0;
  2486. assert(s->pict_type!=I_TYPE);
  2487. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  2488. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2489. /* if previous was skipped too, then nothing to do ! */
  2490. if (*mbskip_ptr >= age && s->current_picture.reference){
  2491. return;
  2492. }
  2493. } else if(!s->current_picture.reference){
  2494. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  2495. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2496. } else{
  2497. *mbskip_ptr = 0; /* not skipped */
  2498. }
  2499. }
  2500. if (s->interlaced_dct) {
  2501. dct_linesize = linesize * 2;
  2502. dct_offset = linesize;
  2503. } else {
  2504. dct_linesize = linesize;
  2505. dct_offset = linesize * 8;
  2506. }
  2507. if(readable){
  2508. dest_y= s->dest[0];
  2509. dest_cb= s->dest[1];
  2510. dest_cr= s->dest[2];
  2511. }else{
  2512. dest_y = s->edge_emu_buffer+32; //FIXME cleanup scratchpad pointers
  2513. dest_cb= s->edge_emu_buffer+48;
  2514. dest_cr= s->edge_emu_buffer+56;
  2515. }
  2516. if (!s->mb_intra) {
  2517. /* motion handling */
  2518. /* decoding or more than one mb_type (MC was allready done otherwise) */
  2519. if(!s->encoding){
  2520. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2521. op_pix = s->dsp.put_pixels_tab;
  2522. op_qpix= s->dsp.put_qpel_pixels_tab;
  2523. }else{
  2524. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2525. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2526. }
  2527. if (s->mv_dir & MV_DIR_FORWARD) {
  2528. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2529. op_pix = s->dsp.avg_pixels_tab;
  2530. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2531. }
  2532. if (s->mv_dir & MV_DIR_BACKWARD) {
  2533. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2534. }
  2535. }
  2536. /* skip dequant / idct if we are really late ;) */
  2537. if(s->hurry_up>1) return;
  2538. /* add dct residue */
  2539. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  2540. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2541. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
  2542. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
  2543. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
  2544. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
  2545. if(!(s->flags&CODEC_FLAG_GRAY)){
  2546. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2547. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2548. }
  2549. } else if(s->codec_id != CODEC_ID_WMV2){
  2550. add_dct(s, block[0], 0, dest_y, dct_linesize);
  2551. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2552. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2553. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2554. if(!(s->flags&CODEC_FLAG_GRAY)){
  2555. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2556. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2557. }
  2558. }
  2559. #ifdef CONFIG_RISKY
  2560. else{
  2561. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2562. }
  2563. #endif
  2564. } else {
  2565. /* dct only in intra block */
  2566. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  2567. put_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
  2568. put_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
  2569. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
  2570. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
  2571. if(!(s->flags&CODEC_FLAG_GRAY)){
  2572. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2573. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2574. }
  2575. }else{
  2576. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2577. s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
  2578. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2579. s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  2580. if(!(s->flags&CODEC_FLAG_GRAY)){
  2581. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2582. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2583. }
  2584. }
  2585. }
  2586. if(!readable){
  2587. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2588. s->dsp.put_pixels_tab[1][0](s->dest[1], dest_cb, uvlinesize, 8);
  2589. s->dsp.put_pixels_tab[1][0](s->dest[2], dest_cr, uvlinesize, 8);
  2590. }
  2591. }
  2592. }
  2593. #ifdef CONFIG_ENCODERS
  2594. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  2595. {
  2596. static const char tab[64]=
  2597. {3,2,2,1,1,1,1,1,
  2598. 1,1,1,1,1,1,1,1,
  2599. 1,1,1,1,1,1,1,1,
  2600. 0,0,0,0,0,0,0,0,
  2601. 0,0,0,0,0,0,0,0,
  2602. 0,0,0,0,0,0,0,0,
  2603. 0,0,0,0,0,0,0,0,
  2604. 0,0,0,0,0,0,0,0};
  2605. int score=0;
  2606. int run=0;
  2607. int i;
  2608. DCTELEM *block= s->block[n];
  2609. const int last_index= s->block_last_index[n];
  2610. int skip_dc;
  2611. if(threshold<0){
  2612. skip_dc=0;
  2613. threshold= -threshold;
  2614. }else
  2615. skip_dc=1;
  2616. /* are all which we could set to zero are allready zero? */
  2617. if(last_index<=skip_dc - 1) return;
  2618. for(i=0; i<=last_index; i++){
  2619. const int j = s->intra_scantable.permutated[i];
  2620. const int level = ABS(block[j]);
  2621. if(level==1){
  2622. if(skip_dc && i==0) continue;
  2623. score+= tab[run];
  2624. run=0;
  2625. }else if(level>1){
  2626. return;
  2627. }else{
  2628. run++;
  2629. }
  2630. }
  2631. if(score >= threshold) return;
  2632. for(i=skip_dc; i<=last_index; i++){
  2633. const int j = s->intra_scantable.permutated[i];
  2634. block[j]=0;
  2635. }
  2636. if(block[0]) s->block_last_index[n]= 0;
  2637. else s->block_last_index[n]= -1;
  2638. }
  2639. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  2640. {
  2641. int i;
  2642. const int maxlevel= s->max_qcoeff;
  2643. const int minlevel= s->min_qcoeff;
  2644. if(s->mb_intra){
  2645. i=1; //skip clipping of intra dc
  2646. }else
  2647. i=0;
  2648. for(;i<=last_index; i++){
  2649. const int j= s->intra_scantable.permutated[i];
  2650. int level = block[j];
  2651. if (level>maxlevel) level=maxlevel;
  2652. else if(level<minlevel) level=minlevel;
  2653. block[j]= level;
  2654. }
  2655. }
  2656. #if 0
  2657. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2658. int score=0;
  2659. int x,y;
  2660. for(y=0; y<7; y++){
  2661. for(x=0; x<16; x+=4){
  2662. score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
  2663. +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
  2664. }
  2665. s+= stride;
  2666. }
  2667. return score;
  2668. }
  2669. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2670. int score=0;
  2671. int x,y;
  2672. for(y=0; y<7; y++){
  2673. for(x=0; x<16; x++){
  2674. score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2675. }
  2676. s1+= stride;
  2677. s2+= stride;
  2678. }
  2679. return score;
  2680. }
  2681. #else
  2682. #define SQ(a) ((a)*(a))
  2683. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2684. int score=0;
  2685. int x,y;
  2686. for(y=0; y<7; y++){
  2687. for(x=0; x<16; x+=4){
  2688. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  2689. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  2690. }
  2691. s+= stride;
  2692. }
  2693. return score;
  2694. }
  2695. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2696. int score=0;
  2697. int x,y;
  2698. for(y=0; y<7; y++){
  2699. for(x=0; x<16; x++){
  2700. score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2701. }
  2702. s1+= stride;
  2703. s2+= stride;
  2704. }
  2705. return score;
  2706. }
  2707. #endif
  2708. #endif //CONFIG_ENCODERS
  2709. /**
  2710. *
  2711. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2712. */
  2713. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2714. if (s->avctx->draw_horiz_band) {
  2715. AVFrame *src;
  2716. int offset[4];
  2717. if(s->picture_structure != PICT_FRAME){
  2718. h <<= 1;
  2719. y <<= 1;
  2720. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2721. }
  2722. h= FFMIN(h, s->height - y);
  2723. if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2724. src= (AVFrame*)s->current_picture_ptr;
  2725. else if(s->last_picture_ptr)
  2726. src= (AVFrame*)s->last_picture_ptr;
  2727. else
  2728. return;
  2729. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2730. offset[0]=
  2731. offset[1]=
  2732. offset[2]=
  2733. offset[3]= 0;
  2734. }else{
  2735. offset[0]= y * s->linesize;;
  2736. offset[1]=
  2737. offset[2]= (y>>1) * s->uvlinesize;;
  2738. offset[3]= 0;
  2739. }
  2740. emms_c();
  2741. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2742. y, s->picture_structure, h);
  2743. }
  2744. }
  2745. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2746. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2747. const int uvlinesize= s->current_picture.linesize[1];
  2748. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2749. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
  2750. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
  2751. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
  2752. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
  2753. s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
  2754. if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){
  2755. s->dest[0] = s->current_picture.data[0] + s->mb_x * 16 - 16;
  2756. s->dest[1] = s->current_picture.data[1] + s->mb_x * 8 - 8;
  2757. s->dest[2] = s->current_picture.data[2] + s->mb_x * 8 - 8;
  2758. }else{
  2759. s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* linesize ) + s->mb_x * 16 - 16;
  2760. s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
  2761. s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
  2762. }
  2763. }
  2764. #ifdef CONFIG_ENCODERS
  2765. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2766. {
  2767. const int mb_x= s->mb_x;
  2768. const int mb_y= s->mb_y;
  2769. int i;
  2770. int skip_dct[6];
  2771. int dct_offset = s->linesize*8; //default for progressive frames
  2772. for(i=0; i<6; i++) skip_dct[i]=0;
  2773. if(s->adaptive_quant){
  2774. const int last_qp= s->qscale;
  2775. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2776. s->lambda= s->lambda_table[mb_xy];
  2777. update_qscale(s);
  2778. if(!(s->flags&CODEC_FLAG_QP_RD)){
  2779. s->dquant= s->qscale - last_qp;
  2780. if(s->out_format==FMT_H263)
  2781. s->dquant= clip(s->dquant, -2, 2); //FIXME RD
  2782. if(s->codec_id==CODEC_ID_MPEG4){
  2783. if(!s->mb_intra){
  2784. if((s->mv_dir&MV_DIRECT) || s->mv_type==MV_TYPE_8X8)
  2785. s->dquant=0;
  2786. }
  2787. }
  2788. }
  2789. ff_set_qscale(s, last_qp + s->dquant);
  2790. }
  2791. if (s->mb_intra) {
  2792. uint8_t *ptr;
  2793. int wrap_y;
  2794. int emu=0;
  2795. wrap_y = s->linesize;
  2796. ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2797. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2798. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2799. ptr= s->edge_emu_buffer;
  2800. emu=1;
  2801. }
  2802. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2803. int progressive_score, interlaced_score;
  2804. progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
  2805. interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
  2806. if(progressive_score > interlaced_score + 100){
  2807. s->interlaced_dct=1;
  2808. dct_offset= wrap_y;
  2809. wrap_y<<=1;
  2810. }else
  2811. s->interlaced_dct=0;
  2812. }
  2813. s->dsp.get_pixels(s->block[0], ptr , wrap_y);
  2814. s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
  2815. s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  2816. s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  2817. if(s->flags&CODEC_FLAG_GRAY){
  2818. skip_dct[4]= 1;
  2819. skip_dct[5]= 1;
  2820. }else{
  2821. int wrap_c = s->uvlinesize;
  2822. ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2823. if(emu){
  2824. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2825. ptr= s->edge_emu_buffer;
  2826. }
  2827. s->dsp.get_pixels(s->block[4], ptr, wrap_c);
  2828. ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2829. if(emu){
  2830. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2831. ptr= s->edge_emu_buffer;
  2832. }
  2833. s->dsp.get_pixels(s->block[5], ptr, wrap_c);
  2834. }
  2835. }else{
  2836. op_pixels_func (*op_pix)[4];
  2837. qpel_mc_func (*op_qpix)[16];
  2838. uint8_t *dest_y, *dest_cb, *dest_cr;
  2839. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2840. int wrap_y, wrap_c;
  2841. int emu=0;
  2842. dest_y = s->dest[0];
  2843. dest_cb = s->dest[1];
  2844. dest_cr = s->dest[2];
  2845. wrap_y = s->linesize;
  2846. wrap_c = s->uvlinesize;
  2847. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2848. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2849. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2850. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2851. op_pix = s->dsp.put_pixels_tab;
  2852. op_qpix= s->dsp.put_qpel_pixels_tab;
  2853. }else{
  2854. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2855. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2856. }
  2857. if (s->mv_dir & MV_DIR_FORWARD) {
  2858. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2859. op_pix = s->dsp.avg_pixels_tab;
  2860. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2861. }
  2862. if (s->mv_dir & MV_DIR_BACKWARD) {
  2863. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2864. }
  2865. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2866. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2867. ptr_y= s->edge_emu_buffer;
  2868. emu=1;
  2869. }
  2870. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2871. int progressive_score, interlaced_score;
  2872. progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
  2873. + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
  2874. interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
  2875. + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
  2876. if(progressive_score > interlaced_score + 600){
  2877. s->interlaced_dct=1;
  2878. dct_offset= wrap_y;
  2879. wrap_y<<=1;
  2880. }else
  2881. s->interlaced_dct=0;
  2882. }
  2883. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  2884. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2885. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  2886. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  2887. if(s->flags&CODEC_FLAG_GRAY){
  2888. skip_dct[4]= 1;
  2889. skip_dct[5]= 1;
  2890. }else{
  2891. if(emu){
  2892. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2893. ptr_cb= s->edge_emu_buffer;
  2894. }
  2895. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2896. if(emu){
  2897. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2898. ptr_cr= s->edge_emu_buffer;
  2899. }
  2900. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2901. }
  2902. /* pre quantization */
  2903. if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
  2904. //FIXME optimize
  2905. if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  2906. if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  2907. if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  2908. if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  2909. if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
  2910. if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
  2911. #if 0
  2912. {
  2913. static int stat[7];
  2914. int num=0;
  2915. for(i=0; i<6; i++)
  2916. if(skip_dct[i]) num++;
  2917. stat[num]++;
  2918. if(s->mb_x==0 && s->mb_y==0){
  2919. for(i=0; i<7; i++){
  2920. printf("%6d %1d\n", stat[i], i);
  2921. }
  2922. }
  2923. }
  2924. #endif
  2925. }
  2926. }
  2927. /* DCT & quantize */
  2928. if(s->out_format==FMT_MJPEG){
  2929. for(i=0;i<6;i++) {
  2930. int overflow;
  2931. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  2932. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2933. }
  2934. }else{
  2935. for(i=0;i<6;i++) {
  2936. if(!skip_dct[i]){
  2937. int overflow;
  2938. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  2939. // FIXME we could decide to change to quantizer instead of clipping
  2940. // JS: I don't think that would be a good idea it could lower quality instead
  2941. // of improve it. Just INTRADC clipping deserves changes in quantizer
  2942. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2943. }else
  2944. s->block_last_index[i]= -1;
  2945. }
  2946. if(s->luma_elim_threshold && !s->mb_intra)
  2947. for(i=0; i<4; i++)
  2948. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2949. if(s->chroma_elim_threshold && !s->mb_intra)
  2950. for(i=4; i<6; i++)
  2951. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2952. if(s->flags & CODEC_FLAG_CBP_RD){
  2953. for(i=0;i<6;i++) {
  2954. if(s->block_last_index[i] == -1)
  2955. s->coded_score[i]= INT_MAX/256;
  2956. }
  2957. }
  2958. }
  2959. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  2960. s->block_last_index[4]=
  2961. s->block_last_index[5]= 0;
  2962. s->block[4][0]=
  2963. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  2964. }
  2965. /* huffman encode */
  2966. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  2967. case CODEC_ID_MPEG1VIDEO:
  2968. case CODEC_ID_MPEG2VIDEO:
  2969. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  2970. #ifdef CONFIG_RISKY
  2971. case CODEC_ID_MPEG4:
  2972. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2973. case CODEC_ID_MSMPEG4V2:
  2974. case CODEC_ID_MSMPEG4V3:
  2975. case CODEC_ID_WMV1:
  2976. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2977. case CODEC_ID_WMV2:
  2978. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  2979. case CODEC_ID_H263:
  2980. case CODEC_ID_H263P:
  2981. case CODEC_ID_FLV1:
  2982. case CODEC_ID_RV10:
  2983. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  2984. #endif
  2985. case CODEC_ID_MJPEG:
  2986. mjpeg_encode_mb(s, s->block); break;
  2987. default:
  2988. assert(0);
  2989. }
  2990. }
  2991. #endif //CONFIG_ENCODERS
  2992. /**
  2993. * combines the (truncated) bitstream to a complete frame
  2994. * @returns -1 if no complete frame could be created
  2995. */
  2996. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
  2997. ParseContext *pc= &s->parse_context;
  2998. #if 0
  2999. if(pc->overread){
  3000. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  3001. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  3002. }
  3003. #endif
  3004. /* copy overreaded byes from last frame into buffer */
  3005. for(; pc->overread>0; pc->overread--){
  3006. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  3007. }
  3008. pc->last_index= pc->index;
  3009. /* copy into buffer end return */
  3010. if(next == END_NOT_FOUND){
  3011. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  3012. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  3013. pc->index += *buf_size;
  3014. return -1;
  3015. }
  3016. *buf_size=
  3017. pc->overread_index= pc->index + next;
  3018. /* append to buffer */
  3019. if(pc->index){
  3020. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  3021. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  3022. pc->index = 0;
  3023. *buf= pc->buffer;
  3024. }
  3025. /* store overread bytes */
  3026. for(;next < 0; next++){
  3027. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  3028. pc->overread++;
  3029. }
  3030. #if 0
  3031. if(pc->overread){
  3032. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  3033. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  3034. }
  3035. #endif
  3036. return 0;
  3037. }
  3038. void ff_mpeg_flush(AVCodecContext *avctx){
  3039. int i;
  3040. MpegEncContext *s = avctx->priv_data;
  3041. for(i=0; i<MAX_PICTURE_COUNT; i++){
  3042. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  3043. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  3044. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  3045. }
  3046. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  3047. s->parse_context.state= -1;
  3048. s->parse_context.frame_start_found= 0;
  3049. s->parse_context.overread= 0;
  3050. s->parse_context.overread_index= 0;
  3051. s->parse_context.index= 0;
  3052. s->parse_context.last_index= 0;
  3053. }
  3054. #ifdef CONFIG_ENCODERS
  3055. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  3056. {
  3057. int bytes= length>>4;
  3058. int bits= length&15;
  3059. int i;
  3060. if(length==0) return;
  3061. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  3062. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  3063. }
  3064. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  3065. int i;
  3066. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  3067. /* mpeg1 */
  3068. d->mb_skip_run= s->mb_skip_run;
  3069. for(i=0; i<3; i++)
  3070. d->last_dc[i]= s->last_dc[i];
  3071. /* statistics */
  3072. d->mv_bits= s->mv_bits;
  3073. d->i_tex_bits= s->i_tex_bits;
  3074. d->p_tex_bits= s->p_tex_bits;
  3075. d->i_count= s->i_count;
  3076. d->f_count= s->f_count;
  3077. d->b_count= s->b_count;
  3078. d->skip_count= s->skip_count;
  3079. d->misc_bits= s->misc_bits;
  3080. d->last_bits= 0;
  3081. d->mb_skiped= 0;
  3082. d->qscale= s->qscale;
  3083. d->dquant= s->dquant;
  3084. }
  3085. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  3086. int i;
  3087. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  3088. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  3089. /* mpeg1 */
  3090. d->mb_skip_run= s->mb_skip_run;
  3091. for(i=0; i<3; i++)
  3092. d->last_dc[i]= s->last_dc[i];
  3093. /* statistics */
  3094. d->mv_bits= s->mv_bits;
  3095. d->i_tex_bits= s->i_tex_bits;
  3096. d->p_tex_bits= s->p_tex_bits;
  3097. d->i_count= s->i_count;
  3098. d->f_count= s->f_count;
  3099. d->b_count= s->b_count;
  3100. d->skip_count= s->skip_count;
  3101. d->misc_bits= s->misc_bits;
  3102. d->mb_intra= s->mb_intra;
  3103. d->mb_skiped= s->mb_skiped;
  3104. d->mv_type= s->mv_type;
  3105. d->mv_dir= s->mv_dir;
  3106. d->pb= s->pb;
  3107. if(s->data_partitioning){
  3108. d->pb2= s->pb2;
  3109. d->tex_pb= s->tex_pb;
  3110. }
  3111. d->block= s->block;
  3112. for(i=0; i<6; i++)
  3113. d->block_last_index[i]= s->block_last_index[i];
  3114. d->interlaced_dct= s->interlaced_dct;
  3115. d->qscale= s->qscale;
  3116. }
  3117. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  3118. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  3119. int *dmin, int *next_block, int motion_x, int motion_y)
  3120. {
  3121. int score;
  3122. uint8_t *dest_backup[3];
  3123. copy_context_before_encode(s, backup, type);
  3124. s->block= s->blocks[*next_block];
  3125. s->pb= pb[*next_block];
  3126. if(s->data_partitioning){
  3127. s->pb2 = pb2 [*next_block];
  3128. s->tex_pb= tex_pb[*next_block];
  3129. }
  3130. if(*next_block){
  3131. memcpy(dest_backup, s->dest, sizeof(s->dest));
  3132. s->dest[0] = s->me.scratchpad;
  3133. s->dest[1] = s->me.scratchpad + 16;
  3134. s->dest[2] = s->me.scratchpad + 16 + 8;
  3135. assert(2*s->uvlinesize == s->linesize); //should be no prob for encoding
  3136. assert(s->linesize >= 64); //FIXME
  3137. }
  3138. encode_mb(s, motion_x, motion_y);
  3139. score= get_bit_count(&s->pb);
  3140. if(s->data_partitioning){
  3141. score+= get_bit_count(&s->pb2);
  3142. score+= get_bit_count(&s->tex_pb);
  3143. }
  3144. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  3145. MPV_decode_mb(s, s->block);
  3146. score *= s->lambda2;
  3147. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  3148. }
  3149. if(*next_block){
  3150. memcpy(s->dest, dest_backup, sizeof(s->dest));
  3151. }
  3152. if(score<*dmin){
  3153. *dmin= score;
  3154. *next_block^=1;
  3155. copy_context_after_encode(best, s, type);
  3156. }
  3157. }
  3158. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  3159. uint32_t *sq = squareTbl + 256;
  3160. int acc=0;
  3161. int x,y;
  3162. if(w==16 && h==16)
  3163. return s->dsp.sse[0](NULL, src1, src2, stride);
  3164. else if(w==8 && h==8)
  3165. return s->dsp.sse[1](NULL, src1, src2, stride);
  3166. for(y=0; y<h; y++){
  3167. for(x=0; x<w; x++){
  3168. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  3169. }
  3170. }
  3171. assert(acc>=0);
  3172. return acc;
  3173. }
  3174. static int sse_mb(MpegEncContext *s){
  3175. int w= 16;
  3176. int h= 16;
  3177. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3178. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3179. if(w==16 && h==16)
  3180. return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize)
  3181. +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize)
  3182. +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize);
  3183. else
  3184. return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
  3185. +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
  3186. +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
  3187. }
  3188. static void encode_picture(MpegEncContext *s, int picture_number)
  3189. {
  3190. int mb_x, mb_y, pdif = 0;
  3191. int i;
  3192. int bits;
  3193. MpegEncContext best_s, backup_s;
  3194. uint8_t bit_buf[2][3000];
  3195. uint8_t bit_buf2[2][3000];
  3196. uint8_t bit_buf_tex[2][3000];
  3197. PutBitContext pb[2], pb2[2], tex_pb[2];
  3198. for(i=0; i<2; i++){
  3199. init_put_bits(&pb [i], bit_buf [i], 3000);
  3200. init_put_bits(&pb2 [i], bit_buf2 [i], 3000);
  3201. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000);
  3202. }
  3203. s->picture_number = picture_number;
  3204. /* Reset the average MB variance */
  3205. s->current_picture.mb_var_sum = 0;
  3206. s->current_picture.mc_mb_var_sum = 0;
  3207. #ifdef CONFIG_RISKY
  3208. /* we need to initialize some time vars before we can encode b-frames */
  3209. // RAL: Condition added for MPEG1VIDEO
  3210. if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  3211. ff_set_mpeg4_time(s, s->picture_number);
  3212. #endif
  3213. s->scene_change_score=0;
  3214. s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
  3215. if(s->pict_type==I_TYPE){
  3216. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  3217. else s->no_rounding=0;
  3218. }else if(s->pict_type!=B_TYPE){
  3219. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  3220. s->no_rounding ^= 1;
  3221. }
  3222. /* Estimate motion for every MB */
  3223. s->mb_intra=0; //for the rate distoration & bit compare functions
  3224. if(s->pict_type != I_TYPE){
  3225. if(s->pict_type != B_TYPE){
  3226. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  3227. s->me.pre_pass=1;
  3228. s->me.dia_size= s->avctx->pre_dia_size;
  3229. for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
  3230. for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
  3231. s->mb_x = mb_x;
  3232. s->mb_y = mb_y;
  3233. ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
  3234. }
  3235. }
  3236. s->me.pre_pass=0;
  3237. }
  3238. }
  3239. s->me.dia_size= s->avctx->dia_size;
  3240. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3241. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  3242. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  3243. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  3244. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  3245. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3246. s->mb_x = mb_x;
  3247. s->mb_y = mb_y;
  3248. s->block_index[0]+=2;
  3249. s->block_index[1]+=2;
  3250. s->block_index[2]+=2;
  3251. s->block_index[3]+=2;
  3252. /* compute motion vector & mb_type and store in context */
  3253. if(s->pict_type==B_TYPE)
  3254. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  3255. else
  3256. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  3257. }
  3258. }
  3259. }else /* if(s->pict_type == I_TYPE) */{
  3260. /* I-Frame */
  3261. //FIXME do we need to zero them?
  3262. memset(s->current_picture.motion_val[0][0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  3263. memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_stride)*s->mb_height*2);
  3264. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
  3265. if(!s->fixed_qscale){
  3266. /* finding spatial complexity for I-frame rate control */
  3267. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3268. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3269. int xx = mb_x * 16;
  3270. int yy = mb_y * 16;
  3271. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  3272. int varc;
  3273. int sum = s->dsp.pix_sum(pix, s->linesize);
  3274. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  3275. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  3276. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  3277. s->current_picture.mb_var_sum += varc;
  3278. }
  3279. }
  3280. }
  3281. }
  3282. emms_c();
  3283. if(s->scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
  3284. s->pict_type= I_TYPE;
  3285. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
  3286. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  3287. }
  3288. if(!s->umvplus){
  3289. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  3290. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  3291. ff_fix_long_p_mvs(s);
  3292. }
  3293. if(s->pict_type==B_TYPE){
  3294. int a, b;
  3295. a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  3296. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
  3297. s->f_code = FFMAX(a, b);
  3298. a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  3299. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
  3300. s->b_code = FFMAX(a, b);
  3301. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  3302. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  3303. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  3304. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  3305. }
  3306. }
  3307. if (!s->fixed_qscale)
  3308. s->current_picture.quality = ff_rate_estimate_qscale(s);
  3309. if(s->adaptive_quant){
  3310. #ifdef CONFIG_RISKY
  3311. switch(s->codec_id){
  3312. case CODEC_ID_MPEG4:
  3313. ff_clean_mpeg4_qscales(s);
  3314. break;
  3315. case CODEC_ID_H263:
  3316. case CODEC_ID_H263P:
  3317. case CODEC_ID_FLV1:
  3318. ff_clean_h263_qscales(s);
  3319. break;
  3320. }
  3321. #endif
  3322. s->lambda= s->lambda_table[0];
  3323. //FIXME broken
  3324. }else
  3325. s->lambda= s->current_picture.quality;
  3326. //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
  3327. update_qscale(s);
  3328. if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
  3329. s->qscale= 3; //reduce cliping problems
  3330. if (s->out_format == FMT_MJPEG) {
  3331. /* for mjpeg, we do include qscale in the matrix */
  3332. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  3333. for(i=1;i<64;i++){
  3334. int j= s->dsp.idct_permutation[i];
  3335. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  3336. }
  3337. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  3338. s->intra_matrix, s->intra_quant_bias, 8, 8);
  3339. }
  3340. //FIXME var duplication
  3341. s->current_picture.key_frame= s->pict_type == I_TYPE;
  3342. s->current_picture.pict_type= s->pict_type;
  3343. if(s->current_picture.key_frame)
  3344. s->picture_in_gop_number=0;
  3345. s->last_bits= get_bit_count(&s->pb);
  3346. switch(s->out_format) {
  3347. case FMT_MJPEG:
  3348. mjpeg_picture_header(s);
  3349. break;
  3350. #ifdef CONFIG_RISKY
  3351. case FMT_H263:
  3352. if (s->codec_id == CODEC_ID_WMV2)
  3353. ff_wmv2_encode_picture_header(s, picture_number);
  3354. else if (s->h263_msmpeg4)
  3355. msmpeg4_encode_picture_header(s, picture_number);
  3356. else if (s->h263_pred)
  3357. mpeg4_encode_picture_header(s, picture_number);
  3358. else if (s->codec_id == CODEC_ID_RV10)
  3359. rv10_encode_picture_header(s, picture_number);
  3360. else if (s->codec_id == CODEC_ID_FLV1)
  3361. ff_flv_encode_picture_header(s, picture_number);
  3362. else
  3363. h263_encode_picture_header(s, picture_number);
  3364. break;
  3365. #endif
  3366. case FMT_MPEG1:
  3367. mpeg1_encode_picture_header(s, picture_number);
  3368. break;
  3369. case FMT_H264:
  3370. break;
  3371. }
  3372. bits= get_bit_count(&s->pb);
  3373. s->header_bits= bits - s->last_bits;
  3374. s->last_bits= bits;
  3375. s->mv_bits=0;
  3376. s->misc_bits=0;
  3377. s->i_tex_bits=0;
  3378. s->p_tex_bits=0;
  3379. s->i_count=0;
  3380. s->f_count=0;
  3381. s->b_count=0;
  3382. s->skip_count=0;
  3383. for(i=0; i<3; i++){
  3384. /* init last dc values */
  3385. /* note: quant matrix value (8) is implied here */
  3386. s->last_dc[i] = 128;
  3387. s->current_picture_ptr->error[i] = 0;
  3388. }
  3389. s->mb_skip_run = 0;
  3390. s->last_mv[0][0][0] = 0;
  3391. s->last_mv[0][0][1] = 0;
  3392. s->last_mv[1][0][0] = 0;
  3393. s->last_mv[1][0][1] = 0;
  3394. s->last_mv_dir = 0;
  3395. #ifdef CONFIG_RISKY
  3396. switch(s->codec_id){
  3397. case CODEC_ID_H263:
  3398. case CODEC_ID_H263P:
  3399. case CODEC_ID_FLV1:
  3400. s->gob_index = ff_h263_get_gob_height(s);
  3401. break;
  3402. case CODEC_ID_MPEG4:
  3403. if(s->partitioned_frame)
  3404. ff_mpeg4_init_partitions(s);
  3405. break;
  3406. }
  3407. #endif
  3408. s->resync_mb_x=0;
  3409. s->resync_mb_y=0;
  3410. s->first_slice_line = 1;
  3411. s->ptr_lastgob = s->pb.buf;
  3412. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3413. s->mb_x=0;
  3414. s->mb_y= mb_y;
  3415. ff_set_qscale(s, s->qscale);
  3416. ff_init_block_index(s);
  3417. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3418. const int xy= mb_y*s->mb_stride + mb_x;
  3419. int mb_type= s->mb_type[xy];
  3420. // int d;
  3421. int dmin= INT_MAX;
  3422. s->mb_x = mb_x;
  3423. ff_update_block_index(s);
  3424. /* write gob / video packet header */
  3425. #ifdef CONFIG_RISKY
  3426. if(s->rtp_mode){
  3427. int current_packet_size, is_gob_start;
  3428. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  3429. is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
  3430. switch(s->codec_id){
  3431. case CODEC_ID_H263:
  3432. case CODEC_ID_H263P:
  3433. if(!s->h263_slice_structured)
  3434. if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  3435. break;
  3436. case CODEC_ID_MPEG2VIDEO:
  3437. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  3438. case CODEC_ID_MPEG1VIDEO:
  3439. if(s->mb_skip_run) is_gob_start=0;
  3440. break;
  3441. }
  3442. if(is_gob_start){
  3443. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
  3444. ff_mpeg4_merge_partitions(s);
  3445. ff_mpeg4_init_partitions(s);
  3446. }
  3447. if(s->codec_id==CODEC_ID_MPEG4)
  3448. ff_mpeg4_stuffing(&s->pb);
  3449. align_put_bits(&s->pb);
  3450. flush_put_bits(&s->pb);
  3451. assert((get_bit_count(&s->pb)&7) == 0);
  3452. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  3453. if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
  3454. int r= get_bit_count(&s->pb)/8 + s->picture_number + s->codec_id + s->mb_x + s->mb_y;
  3455. int d= 100 / s->avctx->error_rate;
  3456. if(r % d == 0){
  3457. current_packet_size=0;
  3458. #ifndef ALT_BITSTREAM_WRITER
  3459. s->pb.buf_ptr= s->ptr_lastgob;
  3460. #endif
  3461. assert(pbBufPtr(&s->pb) == s->ptr_lastgob);
  3462. }
  3463. }
  3464. if (s->avctx->rtp_callback)
  3465. s->avctx->rtp_callback(s->ptr_lastgob, current_packet_size, 0);
  3466. switch(s->codec_id){
  3467. case CODEC_ID_MPEG4:
  3468. ff_mpeg4_encode_video_packet_header(s);
  3469. ff_mpeg4_clean_buffers(s);
  3470. break;
  3471. case CODEC_ID_MPEG1VIDEO:
  3472. case CODEC_ID_MPEG2VIDEO:
  3473. ff_mpeg1_encode_slice_header(s);
  3474. ff_mpeg1_clean_buffers(s);
  3475. break;
  3476. case CODEC_ID_H263:
  3477. case CODEC_ID_H263P:
  3478. h263_encode_gob_header(s, mb_y);
  3479. break;
  3480. }
  3481. if(s->flags&CODEC_FLAG_PASS1){
  3482. int bits= get_bit_count(&s->pb);
  3483. s->misc_bits+= bits - s->last_bits;
  3484. s->last_bits= bits;
  3485. }
  3486. s->ptr_lastgob += current_packet_size;
  3487. s->first_slice_line=1;
  3488. s->resync_mb_x=mb_x;
  3489. s->resync_mb_y=mb_y;
  3490. }
  3491. }
  3492. #endif
  3493. if( (s->resync_mb_x == s->mb_x)
  3494. && s->resync_mb_y+1 == s->mb_y){
  3495. s->first_slice_line=0;
  3496. }
  3497. s->mb_skiped=0;
  3498. s->dquant=0; //only for QP_RD
  3499. if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible
  3500. int next_block=0;
  3501. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  3502. copy_context_before_encode(&backup_s, s, -1);
  3503. backup_s.pb= s->pb;
  3504. best_s.data_partitioning= s->data_partitioning;
  3505. best_s.partitioned_frame= s->partitioned_frame;
  3506. if(s->data_partitioning){
  3507. backup_s.pb2= s->pb2;
  3508. backup_s.tex_pb= s->tex_pb;
  3509. }
  3510. if(mb_type&MB_TYPE_INTER){
  3511. s->mv_dir = MV_DIR_FORWARD;
  3512. s->mv_type = MV_TYPE_16X16;
  3513. s->mb_intra= 0;
  3514. s->mv[0][0][0] = s->p_mv_table[xy][0];
  3515. s->mv[0][0][1] = s->p_mv_table[xy][1];
  3516. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  3517. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3518. }
  3519. if(mb_type&MB_TYPE_SKIPED){
  3520. s->mv_dir = MV_DIR_FORWARD;
  3521. s->mv_type = MV_TYPE_16X16;
  3522. s->mb_intra= 0;
  3523. s->mv[0][0][0] = 0;
  3524. s->mv[0][0][1] = 0;
  3525. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_SKIPED, pb, pb2, tex_pb,
  3526. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3527. }
  3528. if(mb_type&MB_TYPE_INTER4V){
  3529. s->mv_dir = MV_DIR_FORWARD;
  3530. s->mv_type = MV_TYPE_8X8;
  3531. s->mb_intra= 0;
  3532. for(i=0; i<4; i++){
  3533. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  3534. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  3535. }
  3536. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  3537. &dmin, &next_block, 0, 0);
  3538. }
  3539. if(mb_type&MB_TYPE_FORWARD){
  3540. s->mv_dir = MV_DIR_FORWARD;
  3541. s->mv_type = MV_TYPE_16X16;
  3542. s->mb_intra= 0;
  3543. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3544. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3545. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  3546. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3547. }
  3548. if(mb_type&MB_TYPE_BACKWARD){
  3549. s->mv_dir = MV_DIR_BACKWARD;
  3550. s->mv_type = MV_TYPE_16X16;
  3551. s->mb_intra= 0;
  3552. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3553. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3554. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  3555. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  3556. }
  3557. if(mb_type&MB_TYPE_BIDIR){
  3558. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3559. s->mv_type = MV_TYPE_16X16;
  3560. s->mb_intra= 0;
  3561. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3562. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3563. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3564. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3565. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  3566. &dmin, &next_block, 0, 0);
  3567. }
  3568. if(mb_type&MB_TYPE_DIRECT){
  3569. int mx= s->b_direct_mv_table[xy][0];
  3570. int my= s->b_direct_mv_table[xy][1];
  3571. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3572. s->mb_intra= 0;
  3573. #ifdef CONFIG_RISKY
  3574. ff_mpeg4_set_direct_mv(s, mx, my);
  3575. #endif
  3576. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  3577. &dmin, &next_block, mx, my);
  3578. }
  3579. if(mb_type&MB_TYPE_INTRA){
  3580. s->mv_dir = 0;
  3581. s->mv_type = MV_TYPE_16X16;
  3582. s->mb_intra= 1;
  3583. s->mv[0][0][0] = 0;
  3584. s->mv[0][0][1] = 0;
  3585. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  3586. &dmin, &next_block, 0, 0);
  3587. if(s->h263_pred || s->h263_aic){
  3588. if(best_s.mb_intra)
  3589. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  3590. else
  3591. ff_clean_intra_table_entries(s); //old mode?
  3592. }
  3593. }
  3594. if(s->flags & CODEC_FLAG_QP_RD){
  3595. if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
  3596. const int last_qp= backup_s.qscale;
  3597. int dquant, dir, qp, dc[6];
  3598. DCTELEM ac[6][16];
  3599. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  3600. assert(backup_s.dquant == 0);
  3601. //FIXME intra
  3602. s->mv_dir= best_s.mv_dir;
  3603. s->mv_type = MV_TYPE_16X16;
  3604. s->mb_intra= best_s.mb_intra;
  3605. s->mv[0][0][0] = best_s.mv[0][0][0];
  3606. s->mv[0][0][1] = best_s.mv[0][0][1];
  3607. s->mv[1][0][0] = best_s.mv[1][0][0];
  3608. s->mv[1][0][1] = best_s.mv[1][0][1];
  3609. dir= s->pict_type == B_TYPE ? 2 : 1;
  3610. if(last_qp + dir > s->avctx->qmax) dir= -dir;
  3611. for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
  3612. qp= last_qp + dquant;
  3613. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  3614. break;
  3615. backup_s.dquant= dquant;
  3616. if(s->mb_intra){
  3617. for(i=0; i<6; i++){
  3618. dc[i]= s->dc_val[0][ s->block_index[i] ];
  3619. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
  3620. }
  3621. }
  3622. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  3623. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  3624. if(best_s.qscale != qp){
  3625. if(s->mb_intra){
  3626. for(i=0; i<6; i++){
  3627. s->dc_val[0][ s->block_index[i] ]= dc[i];
  3628. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
  3629. }
  3630. }
  3631. if(dir > 0 && dquant==dir){
  3632. dquant= 0;
  3633. dir= -dir;
  3634. }else
  3635. break;
  3636. }
  3637. }
  3638. qp= best_s.qscale;
  3639. s->current_picture.qscale_table[xy]= qp;
  3640. }
  3641. }
  3642. copy_context_after_encode(s, &best_s, -1);
  3643. pb_bits_count= get_bit_count(&s->pb);
  3644. flush_put_bits(&s->pb);
  3645. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  3646. s->pb= backup_s.pb;
  3647. if(s->data_partitioning){
  3648. pb2_bits_count= get_bit_count(&s->pb2);
  3649. flush_put_bits(&s->pb2);
  3650. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  3651. s->pb2= backup_s.pb2;
  3652. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  3653. flush_put_bits(&s->tex_pb);
  3654. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  3655. s->tex_pb= backup_s.tex_pb;
  3656. }
  3657. s->last_bits= get_bit_count(&s->pb);
  3658. #ifdef CONFIG_RISKY
  3659. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3660. ff_h263_update_motion_val(s);
  3661. #endif
  3662. if(next_block==0){
  3663. s->dsp.put_pixels_tab[0][0](s->dest[0], s->me.scratchpad , s->linesize ,16);
  3664. s->dsp.put_pixels_tab[1][0](s->dest[1], s->me.scratchpad + 16, s->uvlinesize, 8);
  3665. s->dsp.put_pixels_tab[1][0](s->dest[2], s->me.scratchpad + 24, s->uvlinesize, 8);
  3666. }
  3667. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  3668. MPV_decode_mb(s, s->block);
  3669. } else {
  3670. int motion_x, motion_y;
  3671. s->mv_type=MV_TYPE_16X16;
  3672. // only one MB-Type possible
  3673. switch(mb_type){
  3674. case MB_TYPE_INTRA:
  3675. s->mv_dir = 0;
  3676. s->mb_intra= 1;
  3677. motion_x= s->mv[0][0][0] = 0;
  3678. motion_y= s->mv[0][0][1] = 0;
  3679. break;
  3680. case MB_TYPE_INTER:
  3681. s->mv_dir = MV_DIR_FORWARD;
  3682. s->mb_intra= 0;
  3683. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  3684. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  3685. break;
  3686. case MB_TYPE_INTER4V:
  3687. s->mv_dir = MV_DIR_FORWARD;
  3688. s->mv_type = MV_TYPE_8X8;
  3689. s->mb_intra= 0;
  3690. for(i=0; i<4; i++){
  3691. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  3692. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  3693. }
  3694. motion_x= motion_y= 0;
  3695. break;
  3696. case MB_TYPE_DIRECT:
  3697. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3698. s->mb_intra= 0;
  3699. motion_x=s->b_direct_mv_table[xy][0];
  3700. motion_y=s->b_direct_mv_table[xy][1];
  3701. #ifdef CONFIG_RISKY
  3702. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  3703. #endif
  3704. break;
  3705. case MB_TYPE_BIDIR:
  3706. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3707. s->mb_intra= 0;
  3708. motion_x=0;
  3709. motion_y=0;
  3710. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3711. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3712. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3713. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3714. break;
  3715. case MB_TYPE_BACKWARD:
  3716. s->mv_dir = MV_DIR_BACKWARD;
  3717. s->mb_intra= 0;
  3718. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3719. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3720. break;
  3721. case MB_TYPE_FORWARD:
  3722. s->mv_dir = MV_DIR_FORWARD;
  3723. s->mb_intra= 0;
  3724. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3725. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3726. // printf(" %d %d ", motion_x, motion_y);
  3727. break;
  3728. default:
  3729. motion_x=motion_y=0; //gcc warning fix
  3730. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  3731. }
  3732. encode_mb(s, motion_x, motion_y);
  3733. // RAL: Update last macrobloc type
  3734. s->last_mv_dir = s->mv_dir;
  3735. #ifdef CONFIG_RISKY
  3736. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3737. ff_h263_update_motion_val(s);
  3738. #endif
  3739. MPV_decode_mb(s, s->block);
  3740. }
  3741. /* clean the MV table in IPS frames for direct mode in B frames */
  3742. if(s->mb_intra /* && I,P,S_TYPE */){
  3743. s->p_mv_table[xy][0]=0;
  3744. s->p_mv_table[xy][1]=0;
  3745. }
  3746. if(s->flags&CODEC_FLAG_PSNR){
  3747. int w= 16;
  3748. int h= 16;
  3749. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3750. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3751. s->current_picture_ptr->error[0] += sse(
  3752. s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  3753. s->dest[0], w, h, s->linesize);
  3754. s->current_picture_ptr->error[1] += sse(
  3755. s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  3756. s->dest[1], w>>1, h>>1, s->uvlinesize);
  3757. s->current_picture_ptr->error[2] += sse(
  3758. s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  3759. s->dest[2], w>>1, h>>1, s->uvlinesize);
  3760. }
  3761. if(s->loop_filter)
  3762. ff_h263_loop_filter(s);
  3763. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb));
  3764. }
  3765. }
  3766. emms_c();
  3767. #ifdef CONFIG_RISKY
  3768. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  3769. ff_mpeg4_merge_partitions(s);
  3770. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  3771. msmpeg4_encode_ext_header(s);
  3772. if(s->codec_id==CODEC_ID_MPEG4)
  3773. ff_mpeg4_stuffing(&s->pb);
  3774. #endif
  3775. /* Send the last GOB if RTP */
  3776. if (s->avctx->rtp_callback) {
  3777. flush_put_bits(&s->pb);
  3778. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  3779. /* Call the RTP callback to send the last GOB */
  3780. s->avctx->rtp_callback(s->ptr_lastgob, pdif, 0);
  3781. }
  3782. }
  3783. #endif //CONFIG_ENCODERS
  3784. void ff_denoise_dct(MpegEncContext *s, DCTELEM *block){
  3785. const int intra= s->mb_intra;
  3786. int i;
  3787. s->dct_count[intra]++;
  3788. for(i=0; i<64; i++){
  3789. int level= block[i];
  3790. if(level){
  3791. if(level>0){
  3792. s->dct_error_sum[intra][i] += level;
  3793. level -= s->dct_offset[intra][i];
  3794. if(level<0) level=0;
  3795. }else{
  3796. s->dct_error_sum[intra][i] -= level;
  3797. level += s->dct_offset[intra][i];
  3798. if(level>0) level=0;
  3799. }
  3800. block[i]= level;
  3801. }
  3802. }
  3803. }
  3804. #ifdef CONFIG_ENCODERS
  3805. static int dct_quantize_trellis_c(MpegEncContext *s,
  3806. DCTELEM *block, int n,
  3807. int qscale, int *overflow){
  3808. const int *qmat;
  3809. const uint8_t *scantable= s->intra_scantable.scantable;
  3810. int max=0;
  3811. unsigned int threshold1, threshold2;
  3812. int bias=0;
  3813. int run_tab[65];
  3814. int level_tab[65];
  3815. int score_tab[65];
  3816. int last_run=0;
  3817. int last_level=0;
  3818. int last_score= 0;
  3819. int last_i= 0;
  3820. int not_coded_score= 0;
  3821. int coeff[3][64];
  3822. int coeff_count[64];
  3823. int qmul, qadd, start_i, last_non_zero, i, dc;
  3824. const int esc_length= s->ac_esc_length;
  3825. uint8_t * length;
  3826. uint8_t * last_length;
  3827. int score_limit=0;
  3828. int left_limit= 0;
  3829. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  3830. const int patch_table= s->out_format == FMT_MPEG1 && !s->mb_intra;
  3831. s->dsp.fdct (block);
  3832. if(s->dct_error_sum)
  3833. ff_denoise_dct(s, block);
  3834. qmul= qscale*16;
  3835. qadd= ((qscale-1)|1)*8;
  3836. if (s->mb_intra) {
  3837. int q;
  3838. if (!s->h263_aic) {
  3839. if (n < 4)
  3840. q = s->y_dc_scale;
  3841. else
  3842. q = s->c_dc_scale;
  3843. q = q << 3;
  3844. } else{
  3845. /* For AIC we skip quant/dequant of INTRADC */
  3846. q = 1 << 3;
  3847. qadd=0;
  3848. }
  3849. /* note: block[0] is assumed to be positive */
  3850. block[0] = (block[0] + (q >> 1)) / q;
  3851. start_i = 1;
  3852. last_non_zero = 0;
  3853. qmat = s->q_intra_matrix[qscale];
  3854. if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  3855. bias= 1<<(QMAT_SHIFT-1);
  3856. length = s->intra_ac_vlc_length;
  3857. last_length= s->intra_ac_vlc_last_length;
  3858. } else {
  3859. start_i = 0;
  3860. last_non_zero = -1;
  3861. qmat = s->q_inter_matrix[qscale];
  3862. length = s->inter_ac_vlc_length;
  3863. last_length= s->inter_ac_vlc_last_length;
  3864. }
  3865. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3866. threshold2= (threshold1<<1);
  3867. for(i=start_i; i<64; i++) {
  3868. const int j = scantable[i];
  3869. const int k= i-start_i;
  3870. int level = block[j];
  3871. level = level * qmat[j];
  3872. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3873. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3874. if(((unsigned)(level+threshold1))>threshold2){
  3875. if(level>0){
  3876. level= (bias + level)>>QMAT_SHIFT;
  3877. coeff[0][k]= level;
  3878. coeff[1][k]= level-1;
  3879. // coeff[2][k]= level-2;
  3880. }else{
  3881. level= (bias - level)>>QMAT_SHIFT;
  3882. coeff[0][k]= -level;
  3883. coeff[1][k]= -level+1;
  3884. // coeff[2][k]= -level+2;
  3885. }
  3886. coeff_count[k]= FFMIN(level, 2);
  3887. assert(coeff_count[k]);
  3888. max |=level;
  3889. last_non_zero = i;
  3890. }else{
  3891. coeff[0][k]= (level>>31)|1;
  3892. coeff_count[k]= 1;
  3893. }
  3894. }
  3895. *overflow= s->max_qcoeff < max; //overflow might have happend
  3896. if(last_non_zero < start_i){
  3897. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3898. return last_non_zero;
  3899. }
  3900. score_tab[0]= 0;
  3901. if(patch_table){
  3902. // length[UNI_AC_ENC_INDEX(0, 63)]=
  3903. // length[UNI_AC_ENC_INDEX(0, 65)]= 2;
  3904. }
  3905. for(i=0; i<=last_non_zero - start_i; i++){
  3906. int level_index, run, j;
  3907. const int dct_coeff= block[ scantable[i + start_i] ];
  3908. const int zero_distoration= dct_coeff*dct_coeff;
  3909. int best_score=256*256*256*120;
  3910. last_score += zero_distoration;
  3911. not_coded_score += zero_distoration;
  3912. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3913. int distoration;
  3914. int level= coeff[level_index][i];
  3915. int unquant_coeff;
  3916. assert(level);
  3917. if(s->out_format == FMT_H263){
  3918. if(level>0){
  3919. unquant_coeff= level*qmul + qadd;
  3920. }else{
  3921. unquant_coeff= level*qmul - qadd;
  3922. }
  3923. }else{ //MPEG1
  3924. j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
  3925. if(s->mb_intra){
  3926. if (level < 0) {
  3927. unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
  3928. unquant_coeff = -((unquant_coeff - 1) | 1);
  3929. } else {
  3930. unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
  3931. unquant_coeff = (unquant_coeff - 1) | 1;
  3932. }
  3933. }else{
  3934. if (level < 0) {
  3935. unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3936. unquant_coeff = -((unquant_coeff - 1) | 1);
  3937. } else {
  3938. unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3939. unquant_coeff = (unquant_coeff - 1) | 1;
  3940. }
  3941. }
  3942. unquant_coeff<<= 3;
  3943. }
  3944. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
  3945. level+=64;
  3946. if((level&(~127)) == 0){
  3947. for(run=0; run<=i - left_limit; run++){
  3948. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3949. score += score_tab[i-run];
  3950. if(score < best_score){
  3951. best_score=
  3952. score_tab[i+1]= score;
  3953. run_tab[i+1]= run;
  3954. level_tab[i+1]= level-64;
  3955. }
  3956. }
  3957. if(s->out_format == FMT_H263){
  3958. for(run=0; run<=i - left_limit; run++){
  3959. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3960. score += score_tab[i-run];
  3961. if(score < last_score){
  3962. last_score= score;
  3963. last_run= run;
  3964. last_level= level-64;
  3965. last_i= i+1;
  3966. }
  3967. }
  3968. }
  3969. }else{
  3970. distoration += esc_length*lambda;
  3971. for(run=0; run<=i - left_limit; run++){
  3972. int score= distoration + score_tab[i-run];
  3973. if(score < best_score){
  3974. best_score=
  3975. score_tab[i+1]= score;
  3976. run_tab[i+1]= run;
  3977. level_tab[i+1]= level-64;
  3978. }
  3979. }
  3980. if(s->out_format == FMT_H263){
  3981. for(run=0; run<=i - left_limit; run++){
  3982. int score= distoration + score_tab[i-run];
  3983. if(score < last_score){
  3984. last_score= score;
  3985. last_run= run;
  3986. last_level= level-64;
  3987. last_i= i+1;
  3988. }
  3989. }
  3990. }
  3991. }
  3992. }
  3993. for(j=left_limit; j<=i; j++){
  3994. score_tab[j] += zero_distoration;
  3995. }
  3996. score_limit+= zero_distoration;
  3997. if(score_tab[i+1] < score_limit)
  3998. score_limit= score_tab[i+1];
  3999. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  4000. while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
  4001. if(patch_table){
  4002. // length[UNI_AC_ENC_INDEX(0, 63)]=
  4003. // length[UNI_AC_ENC_INDEX(0, 65)]= 3;
  4004. }
  4005. }
  4006. if(s->out_format != FMT_H263){
  4007. last_score= 256*256*256*120;
  4008. for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
  4009. int score= score_tab[i];
  4010. if(i) score += lambda*2; //FIXME exacter?
  4011. if(score < last_score){
  4012. last_score= score;
  4013. last_i= i;
  4014. last_level= level_tab[i];
  4015. last_run= run_tab[i];
  4016. }
  4017. }
  4018. }
  4019. s->coded_score[n] = last_score - not_coded_score;
  4020. dc= block[0];
  4021. last_non_zero= last_i - 1 + start_i;
  4022. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  4023. if(last_non_zero < start_i)
  4024. return last_non_zero;
  4025. if(last_non_zero == 0 && start_i == 0){
  4026. int best_level= 0;
  4027. int best_score= dc * dc;
  4028. for(i=0; i<coeff_count[0]; i++){
  4029. int level= coeff[i][0];
  4030. int unquant_coeff, score, distoration;
  4031. if(s->out_format == FMT_H263){
  4032. if(level>0){
  4033. unquant_coeff= (level*qmul + qadd)>>3;
  4034. }else{
  4035. unquant_coeff= (level*qmul - qadd)>>3;
  4036. }
  4037. }else{ //MPEG1
  4038. if (level < 0) {
  4039. unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  4040. unquant_coeff = -((unquant_coeff - 1) | 1);
  4041. } else {
  4042. unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  4043. unquant_coeff = (unquant_coeff - 1) | 1;
  4044. }
  4045. }
  4046. unquant_coeff = (unquant_coeff + 4) >> 3;
  4047. unquant_coeff<<= 3 + 3;
  4048. distoration= (unquant_coeff - dc) * (unquant_coeff - dc);
  4049. level+=64;
  4050. if((level&(~127)) == 0)
  4051. score= distoration + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  4052. else
  4053. score= distoration + esc_length*lambda;
  4054. if(score < best_score){
  4055. best_score= score;
  4056. best_level= level - 64;
  4057. }
  4058. }
  4059. block[0]= best_level;
  4060. s->coded_score[n] = best_score - dc*dc;
  4061. if(best_level == 0) return -1;
  4062. else return last_non_zero;
  4063. }
  4064. i= last_i;
  4065. assert(last_level);
  4066. //FIXME use permutated scantable
  4067. block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
  4068. i -= last_run + 1;
  4069. for(;i>0 ; i -= run_tab[i] + 1){
  4070. const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
  4071. block[j]= level_tab[i];
  4072. assert(block[j]);
  4073. }
  4074. return last_non_zero;
  4075. }
  4076. static int dct_quantize_c(MpegEncContext *s,
  4077. DCTELEM *block, int n,
  4078. int qscale, int *overflow)
  4079. {
  4080. int i, j, level, last_non_zero, q;
  4081. const int *qmat;
  4082. const uint8_t *scantable= s->intra_scantable.scantable;
  4083. int bias;
  4084. int max=0;
  4085. unsigned int threshold1, threshold2;
  4086. s->dsp.fdct (block);
  4087. if(s->dct_error_sum)
  4088. ff_denoise_dct(s, block);
  4089. if (s->mb_intra) {
  4090. if (!s->h263_aic) {
  4091. if (n < 4)
  4092. q = s->y_dc_scale;
  4093. else
  4094. q = s->c_dc_scale;
  4095. q = q << 3;
  4096. } else
  4097. /* For AIC we skip quant/dequant of INTRADC */
  4098. q = 1 << 3;
  4099. /* note: block[0] is assumed to be positive */
  4100. block[0] = (block[0] + (q >> 1)) / q;
  4101. i = 1;
  4102. last_non_zero = 0;
  4103. qmat = s->q_intra_matrix[qscale];
  4104. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  4105. } else {
  4106. i = 0;
  4107. last_non_zero = -1;
  4108. qmat = s->q_inter_matrix[qscale];
  4109. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  4110. }
  4111. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  4112. threshold2= (threshold1<<1);
  4113. for(;i<64;i++) {
  4114. j = scantable[i];
  4115. level = block[j];
  4116. level = level * qmat[j];
  4117. // if( bias+level >= (1<<QMAT_SHIFT)
  4118. // || bias-level >= (1<<QMAT_SHIFT)){
  4119. if(((unsigned)(level+threshold1))>threshold2){
  4120. if(level>0){
  4121. level= (bias + level)>>QMAT_SHIFT;
  4122. block[j]= level;
  4123. }else{
  4124. level= (bias - level)>>QMAT_SHIFT;
  4125. block[j]= -level;
  4126. }
  4127. max |=level;
  4128. last_non_zero = i;
  4129. }else{
  4130. block[j]=0;
  4131. }
  4132. }
  4133. *overflow= s->max_qcoeff < max; //overflow might have happend
  4134. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  4135. if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  4136. ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  4137. return last_non_zero;
  4138. }
  4139. #endif //CONFIG_ENCODERS
  4140. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  4141. DCTELEM *block, int n, int qscale)
  4142. {
  4143. int i, level, nCoeffs;
  4144. const uint16_t *quant_matrix;
  4145. nCoeffs= s->block_last_index[n];
  4146. if (n < 4)
  4147. block[0] = block[0] * s->y_dc_scale;
  4148. else
  4149. block[0] = block[0] * s->c_dc_scale;
  4150. /* XXX: only mpeg1 */
  4151. quant_matrix = s->intra_matrix;
  4152. for(i=1;i<=nCoeffs;i++) {
  4153. int j= s->intra_scantable.permutated[i];
  4154. level = block[j];
  4155. if (level) {
  4156. if (level < 0) {
  4157. level = -level;
  4158. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4159. level = (level - 1) | 1;
  4160. level = -level;
  4161. } else {
  4162. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4163. level = (level - 1) | 1;
  4164. }
  4165. block[j] = level;
  4166. }
  4167. }
  4168. }
  4169. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  4170. DCTELEM *block, int n, int qscale)
  4171. {
  4172. int i, level, nCoeffs;
  4173. const uint16_t *quant_matrix;
  4174. nCoeffs= s->block_last_index[n];
  4175. quant_matrix = s->inter_matrix;
  4176. for(i=0; i<=nCoeffs; i++) {
  4177. int j= s->intra_scantable.permutated[i];
  4178. level = block[j];
  4179. if (level) {
  4180. if (level < 0) {
  4181. level = -level;
  4182. level = (((level << 1) + 1) * qscale *
  4183. ((int) (quant_matrix[j]))) >> 4;
  4184. level = (level - 1) | 1;
  4185. level = -level;
  4186. } else {
  4187. level = (((level << 1) + 1) * qscale *
  4188. ((int) (quant_matrix[j]))) >> 4;
  4189. level = (level - 1) | 1;
  4190. }
  4191. block[j] = level;
  4192. }
  4193. }
  4194. }
  4195. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  4196. DCTELEM *block, int n, int qscale)
  4197. {
  4198. int i, level, nCoeffs;
  4199. const uint16_t *quant_matrix;
  4200. if(s->alternate_scan) nCoeffs= 63;
  4201. else nCoeffs= s->block_last_index[n];
  4202. if (n < 4)
  4203. block[0] = block[0] * s->y_dc_scale;
  4204. else
  4205. block[0] = block[0] * s->c_dc_scale;
  4206. quant_matrix = s->intra_matrix;
  4207. for(i=1;i<=nCoeffs;i++) {
  4208. int j= s->intra_scantable.permutated[i];
  4209. level = block[j];
  4210. if (level) {
  4211. if (level < 0) {
  4212. level = -level;
  4213. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4214. level = -level;
  4215. } else {
  4216. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4217. }
  4218. block[j] = level;
  4219. }
  4220. }
  4221. }
  4222. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  4223. DCTELEM *block, int n, int qscale)
  4224. {
  4225. int i, level, nCoeffs;
  4226. const uint16_t *quant_matrix;
  4227. int sum=-1;
  4228. if(s->alternate_scan) nCoeffs= 63;
  4229. else nCoeffs= s->block_last_index[n];
  4230. quant_matrix = s->inter_matrix;
  4231. for(i=0; i<=nCoeffs; i++) {
  4232. int j= s->intra_scantable.permutated[i];
  4233. level = block[j];
  4234. if (level) {
  4235. if (level < 0) {
  4236. level = -level;
  4237. level = (((level << 1) + 1) * qscale *
  4238. ((int) (quant_matrix[j]))) >> 4;
  4239. level = -level;
  4240. } else {
  4241. level = (((level << 1) + 1) * qscale *
  4242. ((int) (quant_matrix[j]))) >> 4;
  4243. }
  4244. block[j] = level;
  4245. sum+=level;
  4246. }
  4247. }
  4248. block[63]^=sum&1;
  4249. }
  4250. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  4251. DCTELEM *block, int n, int qscale)
  4252. {
  4253. int i, level, qmul, qadd;
  4254. int nCoeffs;
  4255. assert(s->block_last_index[n]>=0);
  4256. qmul = qscale << 1;
  4257. if (!s->h263_aic) {
  4258. if (n < 4)
  4259. block[0] = block[0] * s->y_dc_scale;
  4260. else
  4261. block[0] = block[0] * s->c_dc_scale;
  4262. qadd = (qscale - 1) | 1;
  4263. }else{
  4264. qadd = 0;
  4265. }
  4266. if(s->ac_pred)
  4267. nCoeffs=63;
  4268. else
  4269. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  4270. for(i=1; i<=nCoeffs; i++) {
  4271. level = block[i];
  4272. if (level) {
  4273. if (level < 0) {
  4274. level = level * qmul - qadd;
  4275. } else {
  4276. level = level * qmul + qadd;
  4277. }
  4278. block[i] = level;
  4279. }
  4280. }
  4281. }
  4282. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  4283. DCTELEM *block, int n, int qscale)
  4284. {
  4285. int i, level, qmul, qadd;
  4286. int nCoeffs;
  4287. assert(s->block_last_index[n]>=0);
  4288. qadd = (qscale - 1) | 1;
  4289. qmul = qscale << 1;
  4290. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  4291. for(i=0; i<=nCoeffs; i++) {
  4292. level = block[i];
  4293. if (level) {
  4294. if (level < 0) {
  4295. level = level * qmul - qadd;
  4296. } else {
  4297. level = level * qmul + qadd;
  4298. }
  4299. block[i] = level;
  4300. }
  4301. }
  4302. }
  4303. static const AVOption mpeg4_options[] =
  4304. {
  4305. AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  4306. AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  4307. "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  4308. bit_rate_tolerance, 4, 240000000, 8000),
  4309. AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  4310. AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  4311. AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  4312. rc_eq, "tex^qComp,option1,options2", 0),
  4313. AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  4314. rc_min_rate, 4, 24000000, 0),
  4315. AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  4316. rc_max_rate, 4, 24000000, 0),
  4317. AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
  4318. rc_buffer_aggressivity, 4, 24000000, 0),
  4319. AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
  4320. rc_initial_cplx, 0., 9999999., 0),
  4321. AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
  4322. i_quant_factor, 0., 0., 0),
  4323. AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
  4324. i_quant_factor, -999999., 999999., 0),
  4325. AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
  4326. dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
  4327. AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
  4328. lumi_masking, 0., 999999., 0),
  4329. AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
  4330. temporal_cplx_masking, 0., 999999., 0),
  4331. AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
  4332. spatial_cplx_masking, 0., 999999., 0),
  4333. AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
  4334. p_masking, 0., 999999., 0),
  4335. AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
  4336. dark_masking, 0., 999999., 0),
  4337. AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
  4338. idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
  4339. AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
  4340. mb_qmin, 0, 8, 0),
  4341. AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
  4342. mb_qmin, 0, 8, 0),
  4343. AVOPTION_CODEC_INT("me_cmp", "ME compare function",
  4344. me_cmp, 0, 24000000, 0),
  4345. AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
  4346. me_sub_cmp, 0, 24000000, 0),
  4347. AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
  4348. dia_size, 0, 24000000, 0),
  4349. AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
  4350. last_predictor_count, 0, 24000000, 0),
  4351. AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
  4352. pre_me, 0, 24000000, 0),
  4353. AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
  4354. me_pre_cmp, 0, 24000000, 0),
  4355. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  4356. me_range, 0, 24000000, 0),
  4357. AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
  4358. pre_dia_size, 0, 24000000, 0),
  4359. AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
  4360. me_subpel_quality, 0, 24000000, 0),
  4361. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  4362. me_range, 0, 24000000, 0),
  4363. AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  4364. flags, CODEC_FLAG_PSNR, 0),
  4365. AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  4366. rc_override),
  4367. AVOPTION_SUB(avoptions_common),
  4368. AVOPTION_END()
  4369. };
  4370. #ifdef CONFIG_ENCODERS
  4371. AVCodec mpeg1video_encoder = {
  4372. "mpeg1video",
  4373. CODEC_TYPE_VIDEO,
  4374. CODEC_ID_MPEG1VIDEO,
  4375. sizeof(MpegEncContext),
  4376. MPV_encode_init,
  4377. MPV_encode_picture,
  4378. MPV_encode_end,
  4379. };
  4380. #ifdef CONFIG_RISKY
  4381. AVCodec mpeg2video_encoder = {
  4382. "mpeg2video",
  4383. CODEC_TYPE_VIDEO,
  4384. CODEC_ID_MPEG2VIDEO,
  4385. sizeof(MpegEncContext),
  4386. MPV_encode_init,
  4387. MPV_encode_picture,
  4388. MPV_encode_end,
  4389. };
  4390. AVCodec h263_encoder = {
  4391. "h263",
  4392. CODEC_TYPE_VIDEO,
  4393. CODEC_ID_H263,
  4394. sizeof(MpegEncContext),
  4395. MPV_encode_init,
  4396. MPV_encode_picture,
  4397. MPV_encode_end,
  4398. };
  4399. AVCodec h263p_encoder = {
  4400. "h263p",
  4401. CODEC_TYPE_VIDEO,
  4402. CODEC_ID_H263P,
  4403. sizeof(MpegEncContext),
  4404. MPV_encode_init,
  4405. MPV_encode_picture,
  4406. MPV_encode_end,
  4407. };
  4408. AVCodec flv_encoder = {
  4409. "flv",
  4410. CODEC_TYPE_VIDEO,
  4411. CODEC_ID_FLV1,
  4412. sizeof(MpegEncContext),
  4413. MPV_encode_init,
  4414. MPV_encode_picture,
  4415. MPV_encode_end,
  4416. };
  4417. AVCodec rv10_encoder = {
  4418. "rv10",
  4419. CODEC_TYPE_VIDEO,
  4420. CODEC_ID_RV10,
  4421. sizeof(MpegEncContext),
  4422. MPV_encode_init,
  4423. MPV_encode_picture,
  4424. MPV_encode_end,
  4425. };
  4426. AVCodec mpeg4_encoder = {
  4427. "mpeg4",
  4428. CODEC_TYPE_VIDEO,
  4429. CODEC_ID_MPEG4,
  4430. sizeof(MpegEncContext),
  4431. MPV_encode_init,
  4432. MPV_encode_picture,
  4433. MPV_encode_end,
  4434. .options = mpeg4_options,
  4435. };
  4436. AVCodec msmpeg4v1_encoder = {
  4437. "msmpeg4v1",
  4438. CODEC_TYPE_VIDEO,
  4439. CODEC_ID_MSMPEG4V1,
  4440. sizeof(MpegEncContext),
  4441. MPV_encode_init,
  4442. MPV_encode_picture,
  4443. MPV_encode_end,
  4444. .options = mpeg4_options,
  4445. };
  4446. AVCodec msmpeg4v2_encoder = {
  4447. "msmpeg4v2",
  4448. CODEC_TYPE_VIDEO,
  4449. CODEC_ID_MSMPEG4V2,
  4450. sizeof(MpegEncContext),
  4451. MPV_encode_init,
  4452. MPV_encode_picture,
  4453. MPV_encode_end,
  4454. .options = mpeg4_options,
  4455. };
  4456. AVCodec msmpeg4v3_encoder = {
  4457. "msmpeg4",
  4458. CODEC_TYPE_VIDEO,
  4459. CODEC_ID_MSMPEG4V3,
  4460. sizeof(MpegEncContext),
  4461. MPV_encode_init,
  4462. MPV_encode_picture,
  4463. MPV_encode_end,
  4464. .options = mpeg4_options,
  4465. };
  4466. AVCodec wmv1_encoder = {
  4467. "wmv1",
  4468. CODEC_TYPE_VIDEO,
  4469. CODEC_ID_WMV1,
  4470. sizeof(MpegEncContext),
  4471. MPV_encode_init,
  4472. MPV_encode_picture,
  4473. MPV_encode_end,
  4474. .options = mpeg4_options,
  4475. };
  4476. #endif
  4477. AVCodec mjpeg_encoder = {
  4478. "mjpeg",
  4479. CODEC_TYPE_VIDEO,
  4480. CODEC_ID_MJPEG,
  4481. sizeof(MpegEncContext),
  4482. MPV_encode_init,
  4483. MPV_encode_picture,
  4484. MPV_encode_end,
  4485. };
  4486. #endif //CONFIG_ENCODERS