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.

2546 lines
87KB

  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. #include "avcodec.h"
  22. #include "dsputil.h"
  23. #include "mpegvideo.h"
  24. #ifdef USE_FASTMEMCPY
  25. #include "fastmemcpy.h"
  26. #endif
  27. static void encode_picture(MpegEncContext *s, int picture_number);
  28. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  29. DCTELEM *block, int n, int qscale);
  30. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  31. DCTELEM *block, int n, int qscale);
  32. static void dct_unquantize_h263_c(MpegEncContext *s,
  33. DCTELEM *block, int n, int qscale);
  34. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
  35. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  36. int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow)= dct_quantize_c;
  37. void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
  38. #define EDGE_WIDTH 16
  39. /* enable all paranoid tests for rounding, overflows, etc... */
  40. //#define PARANOID
  41. //#define DEBUG
  42. /* for jpeg fast DCT */
  43. #define CONST_BITS 14
  44. static const unsigned short aanscales[64] = {
  45. /* precomputed values scaled up by 14 bits */
  46. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  47. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  48. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  49. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  50. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  51. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  52. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  53. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  54. };
  55. static UINT8 h263_chroma_roundtab[16] = {
  56. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  57. };
  58. static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
  59. static UINT8 default_fcode_tab[MAX_MV*2+1];
  60. extern UINT8 zigzag_end[64];
  61. /* default motion estimation */
  62. int motion_estimation_method = ME_EPZS;
  63. static void convert_matrix(int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
  64. const UINT16 *quant_matrix, int bias)
  65. {
  66. int qscale;
  67. for(qscale=1; qscale<32; qscale++){
  68. int i;
  69. if (av_fdct == fdct_ifast) {
  70. for(i=0;i<64;i++) {
  71. const int j= block_permute_op(i);
  72. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  73. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  74. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  75. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  76. qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
  77. (aanscales[i] * qscale * quant_matrix[j]));
  78. }
  79. } else {
  80. for(i=0;i<64;i++) {
  81. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  82. So 16 <= qscale * quant_matrix[i] <= 7905
  83. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  84. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  85. */
  86. qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  87. qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
  88. if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
  89. qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
  90. }
  91. }
  92. }
  93. }
  94. // move into common.c perhaps
  95. #define CHECKED_ALLOCZ(p, size)\
  96. {\
  97. p= av_mallocz(size);\
  98. if(p==NULL){\
  99. perror("malloc");\
  100. goto fail;\
  101. }\
  102. }
  103. /* init common structure for both encoder and decoder */
  104. int MPV_common_init(MpegEncContext *s)
  105. {
  106. int c_size, i;
  107. UINT8 *pict;
  108. s->dct_unquantize_h263 = dct_unquantize_h263_c;
  109. s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
  110. s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
  111. #ifdef HAVE_MMX
  112. MPV_common_init_mmx(s);
  113. #endif
  114. //setup default unquantizers (mpeg4 might change it later)
  115. if(s->out_format == FMT_H263)
  116. s->dct_unquantize = s->dct_unquantize_h263;
  117. else
  118. s->dct_unquantize = s->dct_unquantize_mpeg1;
  119. s->mb_width = (s->width + 15) / 16;
  120. s->mb_height = (s->height + 15) / 16;
  121. s->mb_num = s->mb_width * s->mb_height;
  122. s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
  123. for(i=0;i<3;i++) {
  124. int w, h, shift, pict_start;
  125. w = s->linesize;
  126. h = s->mb_height * 16 + 2 * EDGE_WIDTH;
  127. shift = (i == 0) ? 0 : 1;
  128. c_size = (w >> shift) * (h >> shift);
  129. pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
  130. CHECKED_ALLOCZ(pict, c_size)
  131. s->last_picture_base[i] = pict;
  132. s->last_picture[i] = pict + pict_start;
  133. CHECKED_ALLOCZ(pict, c_size)
  134. s->next_picture_base[i] = pict;
  135. s->next_picture[i] = pict + pict_start;
  136. if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
  137. /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but
  138. do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
  139. CHECKED_ALLOCZ(pict, c_size)
  140. s->aux_picture_base[i] = pict;
  141. s->aux_picture[i] = pict + pict_start;
  142. }
  143. }
  144. if (s->encoding) {
  145. int j;
  146. int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
  147. CHECKED_ALLOCZ(s->mb_var , s->mb_num * sizeof(INT16))
  148. CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
  149. /* Allocate MV tables */
  150. CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16))
  151. CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
  152. CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16))
  153. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
  154. CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
  155. CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
  156. CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
  157. CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16))
  158. CHECKED_ALLOCZ(s->me_scratchpad, s->linesize*16*3*sizeof(uint8_t))
  159. CHECKED_ALLOCZ(s->me_map , ME_MAP_SIZE*sizeof(uint32_t))
  160. CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
  161. if(s->max_b_frames){
  162. for(j=0; j<REORDER_BUFFER_SIZE; j++){
  163. int i;
  164. for(i=0;i<3;i++) {
  165. int w, h, shift;
  166. w = s->linesize;
  167. h = s->mb_height * 16;
  168. shift = (i == 0) ? 0 : 1;
  169. c_size = (w >> shift) * (h >> shift);
  170. CHECKED_ALLOCZ(pict, c_size);
  171. s->picture_buffer[j][i] = pict;
  172. }
  173. }
  174. }
  175. if(s->codec_id==CODEC_ID_MPEG4){
  176. CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
  177. CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
  178. }
  179. }
  180. if (s->out_format == FMT_H263 || s->encoding) {
  181. int size;
  182. /* Allocate MB type table */
  183. CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8))
  184. /* MV prediction */
  185. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  186. CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
  187. }
  188. if (s->h263_pred || s->h263_plus) {
  189. int y_size, c_size, i, size;
  190. /* dc values */
  191. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  192. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  193. size = y_size + 2 * c_size;
  194. CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16));
  195. s->dc_val[1] = s->dc_val[0] + y_size;
  196. s->dc_val[2] = s->dc_val[1] + c_size;
  197. for(i=0;i<size;i++)
  198. s->dc_val[0][i] = 1024;
  199. /* ac values */
  200. CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16);
  201. s->ac_val[1] = s->ac_val[0] + y_size;
  202. s->ac_val[2] = s->ac_val[1] + c_size;
  203. /* cbp values */
  204. CHECKED_ALLOCZ(s->coded_block, y_size);
  205. /* which mb is a intra block */
  206. CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
  207. memset(s->mbintra_table, 1, s->mb_num);
  208. /* divx501 bitstream reorder buffer */
  209. CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
  210. /* cbp, ac_pred, pred_dir */
  211. CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8))
  212. CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
  213. CHECKED_ALLOCZ(s->qscale_table , s->mb_num * sizeof(UINT8))
  214. }
  215. /* default structure is frame */
  216. s->picture_structure = PICT_FRAME;
  217. /* init macroblock skip table */
  218. CHECKED_ALLOCZ(s->mbskip_table, s->mb_num);
  219. s->block= s->blocks[0];
  220. s->context_initialized = 1;
  221. return 0;
  222. fail:
  223. MPV_common_end(s);
  224. return -1;
  225. }
  226. //extern int sads;
  227. /* init common structure for both encoder and decoder */
  228. void MPV_common_end(MpegEncContext *s)
  229. {
  230. int i;
  231. av_freep(&s->mb_type);
  232. av_freep(&s->mb_var);
  233. av_freep(&s->mc_mb_var);
  234. av_freep(&s->p_mv_table);
  235. av_freep(&s->b_forw_mv_table);
  236. av_freep(&s->b_back_mv_table);
  237. av_freep(&s->b_bidir_forw_mv_table);
  238. av_freep(&s->b_bidir_back_mv_table);
  239. av_freep(&s->b_direct_forw_mv_table);
  240. av_freep(&s->b_direct_back_mv_table);
  241. av_freep(&s->b_direct_mv_table);
  242. av_freep(&s->motion_val);
  243. av_freep(&s->dc_val[0]);
  244. av_freep(&s->ac_val[0]);
  245. av_freep(&s->coded_block);
  246. av_freep(&s->mbintra_table);
  247. av_freep(&s->cbp_table);
  248. av_freep(&s->pred_dir_table);
  249. av_freep(&s->qscale_table);
  250. av_freep(&s->me_scratchpad);
  251. av_freep(&s->me_map);
  252. av_freep(&s->me_score_map);
  253. av_freep(&s->mbskip_table);
  254. av_freep(&s->bitstream_buffer);
  255. av_freep(&s->tex_pb_buffer);
  256. av_freep(&s->pb2_buffer);
  257. for(i=0;i<3;i++) {
  258. int j;
  259. av_freep(&s->last_picture_base[i]);
  260. av_freep(&s->next_picture_base[i]);
  261. av_freep(&s->aux_picture_base[i]);
  262. for(j=0; j<REORDER_BUFFER_SIZE; j++){
  263. av_freep(&s->picture_buffer[j][i]);
  264. }
  265. }
  266. s->context_initialized = 0;
  267. }
  268. /* init video encoder */
  269. int MPV_encode_init(AVCodecContext *avctx)
  270. {
  271. MpegEncContext *s = avctx->priv_data;
  272. int i;
  273. avctx->pix_fmt = PIX_FMT_YUV420P;
  274. s->bit_rate = avctx->bit_rate;
  275. s->bit_rate_tolerance = avctx->bit_rate_tolerance;
  276. s->frame_rate = avctx->frame_rate;
  277. s->width = avctx->width;
  278. s->height = avctx->height;
  279. if(avctx->gop_size > 600){
  280. fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
  281. avctx->gop_size=600;
  282. }
  283. s->gop_size = avctx->gop_size;
  284. s->rtp_mode = avctx->rtp_mode;
  285. s->rtp_payload_size = avctx->rtp_payload_size;
  286. if (avctx->rtp_callback)
  287. s->rtp_callback = avctx->rtp_callback;
  288. s->qmin= avctx->qmin;
  289. s->qmax= avctx->qmax;
  290. s->max_qdiff= avctx->max_qdiff;
  291. s->qcompress= avctx->qcompress;
  292. s->qblur= avctx->qblur;
  293. s->b_quant_factor= avctx->b_quant_factor;
  294. s->b_quant_offset= avctx->b_quant_offset;
  295. s->avctx = avctx;
  296. s->aspect_ratio_info= avctx->aspect_ratio_info;
  297. s->flags= avctx->flags;
  298. s->max_b_frames= avctx->max_b_frames;
  299. s->rc_strategy= avctx->rc_strategy;
  300. s->b_frame_strategy= avctx->b_frame_strategy;
  301. s->codec_id= avctx->codec->id;
  302. s->luma_elim_threshold = avctx->luma_elim_threshold;
  303. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  304. s->strict_std_compliance= avctx->strict_std_compliance;
  305. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  306. if (s->gop_size <= 1) {
  307. s->intra_only = 1;
  308. s->gop_size = 12;
  309. } else {
  310. s->intra_only = 0;
  311. }
  312. /* ME algorithm */
  313. if (avctx->me_method == 0)
  314. /* For compatibility */
  315. s->me_method = motion_estimation_method;
  316. else
  317. s->me_method = avctx->me_method;
  318. /* Fixed QSCALE */
  319. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  320. switch(avctx->codec->id) {
  321. case CODEC_ID_MPEG1VIDEO:
  322. s->out_format = FMT_MPEG1;
  323. avctx->delay=0; //FIXME not sure, should check the spec
  324. break;
  325. case CODEC_ID_MJPEG:
  326. s->out_format = FMT_MJPEG;
  327. s->intra_only = 1; /* force intra only for jpeg */
  328. s->mjpeg_write_tables = 1; /* write all tables */
  329. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  330. s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
  331. s->mjpeg_vsample[1] = 1; /* the only currently supported values */
  332. s->mjpeg_vsample[2] = 1;
  333. s->mjpeg_hsample[0] = 2;
  334. s->mjpeg_hsample[1] = 1;
  335. s->mjpeg_hsample[2] = 1;
  336. if (mjpeg_init(s) < 0)
  337. return -1;
  338. avctx->delay=0;
  339. break;
  340. case CODEC_ID_H263:
  341. if (h263_get_picture_format(s->width, s->height) == 7) {
  342. printf("Input picture size isn't suitable for h263 codec! try h263+\n");
  343. return -1;
  344. }
  345. s->out_format = FMT_H263;
  346. avctx->delay=0;
  347. break;
  348. case CODEC_ID_H263P:
  349. s->out_format = FMT_H263;
  350. s->rtp_mode = 1;
  351. s->rtp_payload_size = 1200;
  352. s->h263_plus = 1;
  353. s->unrestricted_mv = 1;
  354. s->h263_aic = 1;
  355. /* These are just to be sure */
  356. s->umvplus = 0;
  357. s->umvplus_dec = 0;
  358. avctx->delay=0;
  359. break;
  360. case CODEC_ID_RV10:
  361. s->out_format = FMT_H263;
  362. s->h263_rv10 = 1;
  363. avctx->delay=0;
  364. break;
  365. case CODEC_ID_MPEG4:
  366. s->out_format = FMT_H263;
  367. s->h263_pred = 1;
  368. s->unrestricted_mv = 1;
  369. s->has_b_frames= s->max_b_frames ? 1 : 0;
  370. s->low_delay=0;
  371. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  372. break;
  373. case CODEC_ID_MSMPEG4V1:
  374. s->out_format = FMT_H263;
  375. s->h263_msmpeg4 = 1;
  376. s->h263_pred = 1;
  377. s->unrestricted_mv = 1;
  378. s->msmpeg4_version= 1;
  379. avctx->delay=0;
  380. break;
  381. case CODEC_ID_MSMPEG4V2:
  382. s->out_format = FMT_H263;
  383. s->h263_msmpeg4 = 1;
  384. s->h263_pred = 1;
  385. s->unrestricted_mv = 1;
  386. s->msmpeg4_version= 2;
  387. avctx->delay=0;
  388. break;
  389. case CODEC_ID_MSMPEG4V3:
  390. s->out_format = FMT_H263;
  391. s->h263_msmpeg4 = 1;
  392. s->h263_pred = 1;
  393. s->unrestricted_mv = 1;
  394. s->msmpeg4_version= 3;
  395. avctx->delay=0;
  396. break;
  397. default:
  398. return -1;
  399. }
  400. { /* set up some save defaults, some codecs might override them later */
  401. static int done=0;
  402. if(!done){
  403. int i;
  404. done=1;
  405. memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
  406. memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
  407. for(i=-16; i<16; i++){
  408. default_fcode_tab[i + MAX_MV]= 1;
  409. }
  410. }
  411. }
  412. s->mv_penalty= default_mv_penalty;
  413. s->fcode_tab= default_fcode_tab;
  414. if (s->out_format == FMT_H263)
  415. h263_encode_init(s);
  416. else if (s->out_format == FMT_MPEG1)
  417. mpeg1_encode_init(s);
  418. /* dont use mv_penalty table for crap MV as it would be confused */
  419. if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
  420. s->encoding = 1;
  421. /* init */
  422. if (MPV_common_init(s) < 0)
  423. return -1;
  424. /* init default q matrix */
  425. for(i=0;i<64;i++) {
  426. if(s->out_format == FMT_H263)
  427. s->intra_matrix[i] = default_non_intra_matrix[i];
  428. else
  429. s->intra_matrix[i] = default_intra_matrix[i];
  430. s->inter_matrix[i] = default_non_intra_matrix[i];
  431. }
  432. /* precompute matrix */
  433. /* for mjpeg, we do include qscale in the matrix */
  434. if (s->out_format != FMT_MJPEG) {
  435. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
  436. s->intra_matrix, s->intra_quant_bias);
  437. convert_matrix(s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
  438. s->inter_matrix, s->inter_quant_bias);
  439. }
  440. if(ff_rate_control_init(s) < 0)
  441. return -1;
  442. s->picture_number = 0;
  443. s->picture_in_gop_number = 0;
  444. s->fake_picture_number = 0;
  445. /* motion detector init */
  446. s->f_code = 1;
  447. s->b_code = 1;
  448. return 0;
  449. }
  450. int MPV_encode_end(AVCodecContext *avctx)
  451. {
  452. MpegEncContext *s = avctx->priv_data;
  453. #ifdef STATS
  454. print_stats();
  455. #endif
  456. ff_rate_control_uninit(s);
  457. MPV_common_end(s);
  458. if (s->out_format == FMT_MJPEG)
  459. mjpeg_close(s);
  460. return 0;
  461. }
  462. /* draw the edges of width 'w' of an image of size width, height */
  463. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
  464. {
  465. UINT8 *ptr, *last_line;
  466. int i;
  467. last_line = buf + (height - 1) * wrap;
  468. for(i=0;i<w;i++) {
  469. /* top and bottom */
  470. memcpy(buf - (i + 1) * wrap, buf, width);
  471. memcpy(last_line + (i + 1) * wrap, last_line, width);
  472. }
  473. /* left and right */
  474. ptr = buf;
  475. for(i=0;i<height;i++) {
  476. memset(ptr - w, ptr[0], w);
  477. memset(ptr + width, ptr[width-1], w);
  478. ptr += wrap;
  479. }
  480. /* corners */
  481. for(i=0;i<w;i++) {
  482. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  483. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  484. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  485. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  486. }
  487. }
  488. /* generic function for encode/decode called before a frame is coded/decoded */
  489. void MPV_frame_start(MpegEncContext *s)
  490. {
  491. int i;
  492. UINT8 *tmp;
  493. s->mb_skiped = 0;
  494. s->decoding_error=0;
  495. if (s->pict_type == B_TYPE) {
  496. for(i=0;i<3;i++) {
  497. s->current_picture[i] = s->aux_picture[i];
  498. }
  499. } else {
  500. for(i=0;i<3;i++) {
  501. /* swap next and last */
  502. tmp = s->last_picture[i];
  503. s->last_picture[i] = s->next_picture[i];
  504. s->next_picture[i] = tmp;
  505. s->current_picture[i] = tmp;
  506. }
  507. }
  508. }
  509. /* generic function for encode/decode called after a frame has been coded/decoded */
  510. void MPV_frame_end(MpegEncContext *s)
  511. {
  512. // if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads);
  513. /* draw edge for correct motion prediction if outside */
  514. if (s->pict_type != B_TYPE && !s->intra_only) {
  515. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version>=500){
  516. draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
  517. draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  518. draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  519. }else{
  520. /* mpeg4? / opendivx / xvid */
  521. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  522. draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  523. draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  524. }
  525. }
  526. emms_c();
  527. if(s->pict_type!=B_TYPE){
  528. s->last_non_b_pict_type= s->pict_type;
  529. s->last_non_b_qscale= s->qscale;
  530. s->last_non_b_mc_mb_var= s->mc_mb_var_sum;
  531. s->num_available_buffers++;
  532. if(s->num_available_buffers>2) s->num_available_buffers= 2;
  533. }
  534. }
  535. /* reorder input for encoding */
  536. void reorder_input(MpegEncContext *s, AVPicture *pict)
  537. {
  538. int i, j, index;
  539. if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
  540. // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
  541. for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
  542. s->coded_order[j]= s->coded_order[j+1];
  543. }
  544. s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
  545. s->coded_order[j].pict_type=0;
  546. switch(s->input_pict_type){
  547. default:
  548. case I_TYPE:
  549. case S_TYPE:
  550. case P_TYPE:
  551. index= s->max_b_frames - s->b_frames_since_non_b;
  552. s->b_frames_since_non_b=0;
  553. break;
  554. case B_TYPE:
  555. index= s->max_b_frames + 1;
  556. s->b_frames_since_non_b++;
  557. break;
  558. }
  559. //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
  560. if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
  561. && pict->linesize[0] == s->linesize
  562. && pict->linesize[1] == s->linesize>>1
  563. && pict->linesize[2] == s->linesize>>1){
  564. //printf("ptr\n");
  565. for(i=0; i<3; i++){
  566. s->coded_order[index].picture[i]= pict->data[i];
  567. }
  568. }else{
  569. //printf("copy\n");
  570. for(i=0; i<3; i++){
  571. uint8_t *src = pict->data[i];
  572. uint8_t *dest;
  573. int src_wrap = pict->linesize[i];
  574. int dest_wrap = s->linesize;
  575. int w = s->width;
  576. int h = s->height;
  577. if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
  578. else dest= s->picture_buffer[s->picture_buffer_index][i];
  579. if (i >= 1) {
  580. dest_wrap >>= 1;
  581. w >>= 1;
  582. h >>= 1;
  583. }
  584. s->coded_order[index].picture[i]= dest;
  585. for(j=0;j<h;j++) {
  586. memcpy(dest, src, w);
  587. dest += dest_wrap;
  588. src += src_wrap;
  589. }
  590. }
  591. if(index!=0){
  592. s->picture_buffer_index++;
  593. if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
  594. }
  595. }
  596. s->coded_order[index].pict_type = s->input_pict_type;
  597. s->coded_order[index].qscale = s->input_qscale;
  598. s->coded_order[index].force_type= s->force_input_type;
  599. s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
  600. s->coded_order[index].picture_number= s->input_picture_number;
  601. for(i=0; i<3; i++){
  602. s->new_picture[i]= s->coded_order[0].picture[i];
  603. }
  604. }
  605. int MPV_encode_picture(AVCodecContext *avctx,
  606. unsigned char *buf, int buf_size, void *data)
  607. {
  608. MpegEncContext *s = avctx->priv_data;
  609. AVPicture *pict = data;
  610. s->input_qscale = avctx->quality;
  611. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  612. if(avctx->flags&CODEC_FLAG_TYPE){
  613. s->input_pict_type=
  614. s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
  615. }else if(s->flags&CODEC_FLAG_PASS2){
  616. s->input_pict_type=
  617. s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
  618. }else{
  619. s->force_input_type=0;
  620. if (!s->intra_only) {
  621. /* first picture of GOP is intra */
  622. if (s->input_picture_in_gop_number % s->gop_size==0){
  623. s->input_pict_type = I_TYPE;
  624. }else if(s->max_b_frames==0){
  625. s->input_pict_type = P_TYPE;
  626. }else{
  627. if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
  628. s->input_pict_type = B_TYPE;
  629. else
  630. s->input_pict_type = P_TYPE;
  631. }
  632. } else {
  633. s->input_pict_type = I_TYPE;
  634. }
  635. }
  636. if(s->input_pict_type==I_TYPE)
  637. s->input_picture_in_gop_number=0;
  638. reorder_input(s, pict);
  639. /* output? */
  640. if(s->coded_order[0].picture[0]){
  641. s->pict_type= s->coded_order[0].pict_type;
  642. if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  643. s->qscale= s->coded_order[0].qscale;
  644. s->force_type= s->coded_order[0].force_type;
  645. s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
  646. s->picture_number= s->coded_order[0].picture_number;
  647. MPV_frame_start(s);
  648. encode_picture(s, s->picture_number);
  649. avctx->key_frame = (s->pict_type == I_TYPE);
  650. avctx->pict_type = s->pict_type;
  651. avctx->real_pict_num = s->picture_number;
  652. avctx->header_bits = s->header_bits;
  653. avctx->mv_bits = s->mv_bits;
  654. avctx->misc_bits = s->misc_bits;
  655. avctx->i_tex_bits = s->i_tex_bits;
  656. avctx->p_tex_bits = s->p_tex_bits;
  657. avctx->i_count = s->i_count;
  658. avctx->p_count = s->p_count;
  659. avctx->skip_count = s->skip_count;
  660. MPV_frame_end(s);
  661. if (s->out_format == FMT_MJPEG)
  662. mjpeg_picture_trailer(s);
  663. avctx->quality = s->qscale;
  664. if(s->flags&CODEC_FLAG_PASS1)
  665. ff_write_pass1_stats(s);
  666. }
  667. s->input_picture_number++;
  668. s->input_picture_in_gop_number++;
  669. flush_put_bits(&s->pb);
  670. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  671. if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
  672. else s->pb_frame_bits= s->frame_bits;
  673. s->total_bits += s->frame_bits;
  674. avctx->frame_bits = s->frame_bits;
  675. //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
  676. //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
  677. if (avctx->get_psnr) {
  678. /* At this point pict->data should have the original frame */
  679. /* an s->current_picture should have the coded/decoded frame */
  680. get_psnr(pict->data, s->current_picture,
  681. pict->linesize, s->linesize, avctx);
  682. // printf("%f\n", avctx->psnr_y);
  683. }
  684. return pbBufPtr(&s->pb) - s->pb.buf;
  685. }
  686. static inline void gmc1_motion(MpegEncContext *s,
  687. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  688. int dest_offset,
  689. UINT8 **ref_picture, int src_offset,
  690. int h)
  691. {
  692. UINT8 *ptr;
  693. int offset, src_x, src_y, linesize;
  694. int motion_x, motion_y;
  695. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  696. motion_x= s->sprite_offset[0][0];
  697. motion_y= s->sprite_offset[0][1];
  698. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  699. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  700. motion_x<<=(3-s->sprite_warping_accuracy);
  701. motion_y<<=(3-s->sprite_warping_accuracy);
  702. src_x = clip(src_x, -16, s->width);
  703. if (src_x == s->width)
  704. motion_x =0;
  705. src_y = clip(src_y, -16, s->height);
  706. if (src_y == s->height)
  707. motion_y =0;
  708. linesize = s->linesize;
  709. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  710. dest_y+=dest_offset;
  711. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  712. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  713. motion_x= s->sprite_offset[1][0];
  714. motion_y= s->sprite_offset[1][1];
  715. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  716. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  717. motion_x<<=(3-s->sprite_warping_accuracy);
  718. motion_y<<=(3-s->sprite_warping_accuracy);
  719. src_x = clip(src_x, -8, s->width>>1);
  720. if (src_x == s->width>>1)
  721. motion_x =0;
  722. src_y = clip(src_y, -8, s->height>>1);
  723. if (src_y == s->height>>1)
  724. motion_y =0;
  725. offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
  726. ptr = ref_picture[1] + offset;
  727. gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  728. ptr = ref_picture[2] + offset;
  729. gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  730. return;
  731. }
  732. /* apply one mpeg motion vector to the three components */
  733. static inline void mpeg_motion(MpegEncContext *s,
  734. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  735. int dest_offset,
  736. UINT8 **ref_picture, int src_offset,
  737. int field_based, op_pixels_func *pix_op,
  738. int motion_x, int motion_y, int h)
  739. {
  740. UINT8 *ptr;
  741. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  742. if(s->quarter_sample)
  743. {
  744. motion_x>>=1;
  745. motion_y>>=1;
  746. }
  747. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  748. src_x = s->mb_x * 16 + (motion_x >> 1);
  749. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  750. /* WARNING: do no forget half pels */
  751. height = s->height >> field_based;
  752. src_x = clip(src_x, -16, s->width);
  753. if (src_x == s->width)
  754. dxy &= ~1;
  755. src_y = clip(src_y, -16, height);
  756. if (src_y == height)
  757. dxy &= ~2;
  758. linesize = s->linesize << field_based;
  759. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  760. dest_y += dest_offset;
  761. pix_op[dxy](dest_y, ptr, linesize, h);
  762. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  763. if (s->out_format == FMT_H263) {
  764. dxy = 0;
  765. if ((motion_x & 3) != 0)
  766. dxy |= 1;
  767. if ((motion_y & 3) != 0)
  768. dxy |= 2;
  769. mx = motion_x >> 2;
  770. my = motion_y >> 2;
  771. } else {
  772. mx = motion_x / 2;
  773. my = motion_y / 2;
  774. dxy = ((my & 1) << 1) | (mx & 1);
  775. mx >>= 1;
  776. my >>= 1;
  777. }
  778. src_x = s->mb_x * 8 + mx;
  779. src_y = s->mb_y * (8 >> field_based) + my;
  780. src_x = clip(src_x, -8, s->width >> 1);
  781. if (src_x == (s->width >> 1))
  782. dxy &= ~1;
  783. src_y = clip(src_y, -8, height >> 1);
  784. if (src_y == (height >> 1))
  785. dxy &= ~2;
  786. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  787. ptr = ref_picture[1] + offset;
  788. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  789. ptr = ref_picture[2] + offset;
  790. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  791. }
  792. static inline void qpel_motion(MpegEncContext *s,
  793. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  794. int dest_offset,
  795. UINT8 **ref_picture, int src_offset,
  796. int field_based, op_pixels_func *pix_op,
  797. qpel_mc_func *qpix_op,
  798. int motion_x, int motion_y, int h)
  799. {
  800. UINT8 *ptr;
  801. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  802. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  803. src_x = s->mb_x * 16 + (motion_x >> 2);
  804. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  805. height = s->height >> field_based;
  806. src_x = clip(src_x, -16, s->width);
  807. if (src_x == s->width)
  808. dxy &= ~3;
  809. src_y = clip(src_y, -16, height);
  810. if (src_y == height)
  811. dxy &= ~12;
  812. linesize = s->linesize << field_based;
  813. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  814. dest_y += dest_offset;
  815. //printf("%d %d %d\n", src_x, src_y, dxy);
  816. qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
  817. qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
  818. qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
  819. qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
  820. mx= (motion_x>>1) | (motion_x&1);
  821. my= (motion_y>>1) | (motion_y&1);
  822. dxy = 0;
  823. if ((mx & 3) != 0)
  824. dxy |= 1;
  825. if ((my & 3) != 0)
  826. dxy |= 2;
  827. mx = mx >> 2;
  828. my = my >> 2;
  829. src_x = s->mb_x * 8 + mx;
  830. src_y = s->mb_y * (8 >> field_based) + my;
  831. src_x = clip(src_x, -8, s->width >> 1);
  832. if (src_x == (s->width >> 1))
  833. dxy &= ~1;
  834. src_y = clip(src_y, -8, height >> 1);
  835. if (src_y == (height >> 1))
  836. dxy &= ~2;
  837. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  838. ptr = ref_picture[1] + offset;
  839. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  840. ptr = ref_picture[2] + offset;
  841. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  842. }
  843. static inline void MPV_motion(MpegEncContext *s,
  844. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  845. int dir, UINT8 **ref_picture,
  846. op_pixels_func *pix_op, qpel_mc_func *qpix_op)
  847. {
  848. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  849. int mb_x, mb_y, i;
  850. UINT8 *ptr, *dest;
  851. mb_x = s->mb_x;
  852. mb_y = s->mb_y;
  853. switch(s->mv_type) {
  854. case MV_TYPE_16X16:
  855. if(s->mcsel){
  856. #if 0
  857. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  858. ref_picture, 0,
  859. 0, pix_op,
  860. s->sprite_offset[0][0]>>3,
  861. s->sprite_offset[0][1]>>3,
  862. 16);
  863. #else
  864. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  865. ref_picture, 0,
  866. 16);
  867. #endif
  868. }else if(s->quarter_sample && dir==0){ //FIXME
  869. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  870. ref_picture, 0,
  871. 0, pix_op, qpix_op,
  872. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  873. }else{
  874. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  875. ref_picture, 0,
  876. 0, pix_op,
  877. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  878. }
  879. break;
  880. case MV_TYPE_8X8:
  881. for(i=0;i<4;i++) {
  882. motion_x = s->mv[dir][i][0];
  883. motion_y = s->mv[dir][i][1];
  884. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  885. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  886. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  887. /* WARNING: do no forget half pels */
  888. src_x = clip(src_x, -16, s->width);
  889. if (src_x == s->width)
  890. dxy &= ~1;
  891. src_y = clip(src_y, -16, s->height);
  892. if (src_y == s->height)
  893. dxy &= ~2;
  894. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  895. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  896. pix_op[dxy](dest, ptr, s->linesize, 8);
  897. }
  898. /* In case of 8X8, we construct a single chroma motion vector
  899. with a special rounding */
  900. mx = 0;
  901. my = 0;
  902. for(i=0;i<4;i++) {
  903. mx += s->mv[dir][i][0];
  904. my += s->mv[dir][i][1];
  905. }
  906. if (mx >= 0)
  907. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  908. else {
  909. mx = -mx;
  910. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  911. }
  912. if (my >= 0)
  913. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  914. else {
  915. my = -my;
  916. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  917. }
  918. dxy = ((my & 1) << 1) | (mx & 1);
  919. mx >>= 1;
  920. my >>= 1;
  921. src_x = mb_x * 8 + mx;
  922. src_y = mb_y * 8 + my;
  923. src_x = clip(src_x, -8, s->width/2);
  924. if (src_x == s->width/2)
  925. dxy &= ~1;
  926. src_y = clip(src_y, -8, s->height/2);
  927. if (src_y == s->height/2)
  928. dxy &= ~2;
  929. offset = (src_y * (s->linesize >> 1)) + src_x;
  930. ptr = ref_picture[1] + offset;
  931. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  932. ptr = ref_picture[2] + offset;
  933. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  934. break;
  935. case MV_TYPE_FIELD:
  936. if (s->picture_structure == PICT_FRAME) {
  937. /* top field */
  938. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  939. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  940. 1, pix_op,
  941. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  942. /* bottom field */
  943. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  944. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  945. 1, pix_op,
  946. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  947. } else {
  948. }
  949. break;
  950. }
  951. }
  952. /* put block[] to dest[] */
  953. static inline void put_dct(MpegEncContext *s,
  954. DCTELEM *block, int i, UINT8 *dest, int line_size)
  955. {
  956. if (!s->mpeg2)
  957. s->dct_unquantize(s, block, i, s->qscale);
  958. ff_idct (block);
  959. put_pixels_clamped(block, dest, line_size);
  960. }
  961. /* add block[] to dest[] */
  962. static inline void add_dct(MpegEncContext *s,
  963. DCTELEM *block, int i, UINT8 *dest, int line_size)
  964. {
  965. /* skip dequant / idct if we are really late ;) */
  966. if(s->hurry_up>1) return;
  967. if (s->block_last_index[i] >= 0) {
  968. if (!s->mpeg2)
  969. if(s->encoding || (!s->h263_msmpeg4))
  970. s->dct_unquantize(s, block, i, s->qscale);
  971. ff_idct (block);
  972. add_pixels_clamped(block, dest, line_size);
  973. }
  974. }
  975. /**
  976. * cleans dc, ac, coded_block for the current non intra MB
  977. */
  978. void ff_clean_intra_table_entries(MpegEncContext *s)
  979. {
  980. int wrap = s->block_wrap[0];
  981. int xy = s->block_index[0];
  982. s->dc_val[0][xy ] =
  983. s->dc_val[0][xy + 1 ] =
  984. s->dc_val[0][xy + wrap] =
  985. s->dc_val[0][xy + 1 + wrap] = 1024;
  986. /* ac pred */
  987. memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16));
  988. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
  989. if (s->msmpeg4_version>=3) {
  990. s->coded_block[xy ] =
  991. s->coded_block[xy + 1 ] =
  992. s->coded_block[xy + wrap] =
  993. s->coded_block[xy + 1 + wrap] = 0;
  994. }
  995. /* chroma */
  996. wrap = s->block_wrap[4];
  997. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  998. s->dc_val[1][xy] =
  999. s->dc_val[2][xy] = 1024;
  1000. /* ac pred */
  1001. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  1002. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  1003. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1004. }
  1005. /* generic function called after a macroblock has been parsed by the
  1006. decoder or after it has been encoded by the encoder.
  1007. Important variables used:
  1008. s->mb_intra : true if intra macroblock
  1009. s->mv_dir : motion vector direction
  1010. s->mv_type : motion vector type
  1011. s->mv : motion vector
  1012. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1013. */
  1014. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1015. {
  1016. int mb_x, mb_y;
  1017. int dct_linesize, dct_offset;
  1018. op_pixels_func *op_pix;
  1019. qpel_mc_func *op_qpix;
  1020. mb_x = s->mb_x;
  1021. mb_y = s->mb_y;
  1022. #ifdef FF_POSTPROCESS
  1023. /* Obsolete. Exists for compatibility with mplayer only. */
  1024. quant_store[mb_y][mb_x]=s->qscale;
  1025. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  1026. #else
  1027. if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
  1028. #endif
  1029. /* update DC predictors for P macroblocks */
  1030. if (!s->mb_intra) {
  1031. if (s->h263_pred || s->h263_aic) {
  1032. if(s->mbintra_table[mb_x + mb_y*s->mb_width])
  1033. ff_clean_intra_table_entries(s);
  1034. } else {
  1035. s->last_dc[0] =
  1036. s->last_dc[1] =
  1037. s->last_dc[2] = 128 << s->intra_dc_precision;
  1038. }
  1039. }
  1040. else if (s->h263_pred || s->h263_aic)
  1041. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1042. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1043. if (s->out_format == FMT_H263) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1044. if(s->pict_type!=B_TYPE){
  1045. int xy, wrap, motion_x, motion_y;
  1046. wrap = 2 * s->mb_width + 2;
  1047. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  1048. if (s->mb_intra) {
  1049. motion_x = 0;
  1050. motion_y = 0;
  1051. goto motion_init;
  1052. } else if (s->mv_type == MV_TYPE_16X16) {
  1053. motion_x = s->mv[0][0][0];
  1054. motion_y = s->mv[0][0][1];
  1055. motion_init:
  1056. /* no update if 8X8 because it has been done during parsing */
  1057. s->motion_val[xy][0] = motion_x;
  1058. s->motion_val[xy][1] = motion_y;
  1059. s->motion_val[xy + 1][0] = motion_x;
  1060. s->motion_val[xy + 1][1] = motion_y;
  1061. s->motion_val[xy + wrap][0] = motion_x;
  1062. s->motion_val[xy + wrap][1] = motion_y;
  1063. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1064. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1065. }
  1066. }
  1067. }
  1068. if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
  1069. UINT8 *dest_y, *dest_cb, *dest_cr;
  1070. UINT8 *mbskip_ptr;
  1071. /* avoid copy if macroblock skipped in last frame too
  1072. dont touch it for B-frames as they need the skip info from the next p-frame */
  1073. if (s->pict_type != B_TYPE) {
  1074. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  1075. if (s->mb_skiped) {
  1076. s->mb_skiped = 0;
  1077. /* if previous was skipped too, then nothing to do !
  1078. skip only during decoding as we might trash the buffers during encoding a bit */
  1079. if (*mbskip_ptr != 0 && !s->encoding)
  1080. goto the_end;
  1081. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  1082. } else {
  1083. *mbskip_ptr = 0; /* not skipped */
  1084. }
  1085. }
  1086. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  1087. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1088. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1089. if (s->interlaced_dct) {
  1090. dct_linesize = s->linesize * 2;
  1091. dct_offset = s->linesize;
  1092. } else {
  1093. dct_linesize = s->linesize;
  1094. dct_offset = s->linesize * 8;
  1095. }
  1096. if (!s->mb_intra) {
  1097. const int xy= s->mb_y * s->mb_width + s->mb_x;
  1098. /* motion handling */
  1099. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1100. if((!s->encoding) || (s->mb_type[xy]&(s->mb_type[xy]-1))){
  1101. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1102. op_pix = put_pixels_tab;
  1103. op_qpix= qpel_mc_rnd_tab;
  1104. }else{
  1105. op_pix = put_no_rnd_pixels_tab;
  1106. op_qpix= qpel_mc_no_rnd_tab;
  1107. }
  1108. if (s->mv_dir & MV_DIR_FORWARD) {
  1109. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1110. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1111. op_pix = avg_pixels_tab;
  1112. else
  1113. op_pix = avg_no_rnd_pixels_tab;
  1114. }
  1115. if (s->mv_dir & MV_DIR_BACKWARD) {
  1116. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1117. }
  1118. }
  1119. /* add dct residue */
  1120. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1121. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1122. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1123. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1124. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1125. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1126. } else {
  1127. /* dct only in intra block */
  1128. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1129. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1130. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1131. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1132. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1133. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1134. }
  1135. }
  1136. the_end:
  1137. emms_c(); //FIXME remove
  1138. }
  1139. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc)
  1140. {
  1141. static const char tab[64]=
  1142. {3,2,2,1,1,1,1,1,
  1143. 1,1,1,1,1,1,1,1,
  1144. 1,1,1,1,1,1,1,1,
  1145. 0,0,0,0,0,0,0,0,
  1146. 0,0,0,0,0,0,0,0,
  1147. 0,0,0,0,0,0,0,0,
  1148. 0,0,0,0,0,0,0,0,
  1149. 0,0,0,0,0,0,0,0};
  1150. int score=0;
  1151. int run=0;
  1152. int i;
  1153. DCTELEM *block= s->block[n];
  1154. const int last_index= s->block_last_index[n];
  1155. if(skip_dc) skip_dc=1;
  1156. /* are all which we could set to zero are allready zero? */
  1157. if(last_index<=skip_dc - 1) return;
  1158. for(i=0; i<=last_index; i++){
  1159. const int j = zigzag_direct[i];
  1160. const int level = ABS(block[j]);
  1161. if(level==1){
  1162. if(skip_dc && i==0) continue;
  1163. score+= tab[run];
  1164. run=0;
  1165. }else if(level>1){
  1166. return;
  1167. }else{
  1168. run++;
  1169. }
  1170. }
  1171. if(score >= threshold) return;
  1172. for(i=skip_dc; i<=last_index; i++){
  1173. const int j = zigzag_direct[i];
  1174. block[j]=0;
  1175. }
  1176. if(block[0]) s->block_last_index[n]= 0;
  1177. else s->block_last_index[n]= -1;
  1178. }
  1179. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1180. {
  1181. int i;
  1182. const int maxlevel= s->max_qcoeff;
  1183. const int minlevel= s->min_qcoeff;
  1184. for(i=0;i<=last_index; i++){
  1185. const int j = zigzag_direct[i];
  1186. int level = block[j];
  1187. if (level>maxlevel) level=maxlevel;
  1188. else if(level<minlevel) level=minlevel;
  1189. block[j]= level;
  1190. }
  1191. }
  1192. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1193. {
  1194. const int mb_x= s->mb_x;
  1195. const int mb_y= s->mb_y;
  1196. int i;
  1197. int skip_dct[6];
  1198. #if 0
  1199. if (s->interlaced_dct) {
  1200. dct_linesize = s->linesize * 2;
  1201. dct_offset = s->linesize;
  1202. } else {
  1203. dct_linesize = s->linesize;
  1204. dct_offset = s->linesize * 8;
  1205. }
  1206. #endif
  1207. for(i=0; i<6; i++) skip_dct[i]=0;
  1208. if (s->mb_intra) {
  1209. UINT8 *ptr;
  1210. int wrap;
  1211. wrap = s->linesize;
  1212. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1213. get_pixels(s->block[0], ptr , wrap);
  1214. get_pixels(s->block[1], ptr + 8, wrap);
  1215. get_pixels(s->block[2], ptr + 8 * wrap , wrap);
  1216. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1217. wrap >>=1;
  1218. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1219. get_pixels(s->block[4], ptr, wrap);
  1220. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1221. get_pixels(s->block[5], ptr, wrap);
  1222. }else{
  1223. op_pixels_func *op_pix;
  1224. qpel_mc_func *op_qpix;
  1225. UINT8 *dest_y, *dest_cb, *dest_cr;
  1226. UINT8 *ptr_y, *ptr_cb, *ptr_cr;
  1227. int wrap_y, wrap_c;
  1228. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  1229. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1230. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1231. wrap_y = s->linesize;
  1232. wrap_c = wrap_y>>1;
  1233. ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1234. ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1235. ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1236. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1237. op_pix = put_pixels_tab;
  1238. op_qpix= qpel_mc_rnd_tab;
  1239. }else{
  1240. op_pix = put_no_rnd_pixels_tab;
  1241. op_qpix= qpel_mc_no_rnd_tab;
  1242. }
  1243. if (s->mv_dir & MV_DIR_FORWARD) {
  1244. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1245. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1246. op_pix = avg_pixels_tab;
  1247. else
  1248. op_pix = avg_no_rnd_pixels_tab;
  1249. }
  1250. if (s->mv_dir & MV_DIR_BACKWARD) {
  1251. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1252. }
  1253. diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  1254. diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1255. diff_pixels(s->block[2], ptr_y + 8 * wrap_y , dest_y + 8 * wrap_y , wrap_y);
  1256. diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y);
  1257. diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1258. diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1259. /* pre quantization */
  1260. if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  1261. if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  1262. if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  1263. if(pix_abs8x8(ptr_y + 8*wrap_y , dest_y + 8*wrap_y , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  1264. if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  1265. if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
  1266. if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
  1267. #if 0
  1268. {
  1269. static int stat[7];
  1270. int num=0;
  1271. for(i=0; i<6; i++)
  1272. if(skip_dct[i]) num++;
  1273. stat[num]++;
  1274. if(s->mb_x==0 && s->mb_y==0){
  1275. for(i=0; i<7; i++){
  1276. printf("%6d %1d\n", stat[i], i);
  1277. }
  1278. }
  1279. }
  1280. #endif
  1281. }
  1282. }
  1283. #if 0
  1284. {
  1285. float adap_parm;
  1286. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1287. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1288. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1289. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1290. s->qscale, adap_parm, s->qscale*adap_parm,
  1291. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1292. }
  1293. #endif
  1294. /* DCT & quantize */
  1295. if (s->h263_pred && !(s->msmpeg4_version==1 || s->msmpeg4_version==2)) {
  1296. h263_dc_scale(s);
  1297. } else if (s->h263_aic) {
  1298. s->y_dc_scale = 2*s->qscale;
  1299. s->c_dc_scale = 2*s->qscale;
  1300. } else {
  1301. /* default quantization values */
  1302. s->y_dc_scale = 8;
  1303. s->c_dc_scale = 8;
  1304. }
  1305. if(s->out_format==FMT_MJPEG){
  1306. for(i=0;i<6;i++) {
  1307. int overflow;
  1308. s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
  1309. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1310. }
  1311. }else{
  1312. for(i=0;i<6;i++) {
  1313. if(!skip_dct[i]){
  1314. int overflow;
  1315. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1316. // FIXME we could decide to change to quantizer instead of clipping
  1317. // JS: I don't think that would be a good idea it could lower quality instead
  1318. // of improve it. Just INTRADC clipping deserves changes in quantizer
  1319. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1320. }else
  1321. s->block_last_index[i]= -1;
  1322. }
  1323. if(s->luma_elim_threshold && !s->mb_intra)
  1324. for(i=0; i<4; i++)
  1325. dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0);
  1326. if(s->chroma_elim_threshold && !s->mb_intra)
  1327. for(i=4; i<6; i++)
  1328. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1);
  1329. }
  1330. /* huffman encode */
  1331. switch(s->out_format) {
  1332. case FMT_MPEG1:
  1333. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1334. break;
  1335. case FMT_H263:
  1336. if (s->h263_msmpeg4)
  1337. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1338. else if(s->h263_pred)
  1339. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1340. else
  1341. h263_encode_mb(s, s->block, motion_x, motion_y);
  1342. break;
  1343. case FMT_MJPEG:
  1344. mjpeg_encode_mb(s, s->block);
  1345. break;
  1346. }
  1347. }
  1348. void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
  1349. {
  1350. #if 1
  1351. int bytes= length>>4;
  1352. int bits= length&15;
  1353. int i;
  1354. if(length==0) return;
  1355. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  1356. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  1357. #else
  1358. int bytes= length>>3;
  1359. int bits= length&7;
  1360. int i;
  1361. for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
  1362. put_bits(pb, bits, src[i]>>(8-bits));
  1363. #endif
  1364. }
  1365. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1366. int i;
  1367. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1368. /* mpeg1 */
  1369. d->mb_incr= s->mb_incr;
  1370. for(i=0; i<3; i++)
  1371. d->last_dc[i]= s->last_dc[i];
  1372. /* statistics */
  1373. d->mv_bits= s->mv_bits;
  1374. d->i_tex_bits= s->i_tex_bits;
  1375. d->p_tex_bits= s->p_tex_bits;
  1376. d->i_count= s->i_count;
  1377. d->p_count= s->p_count;
  1378. d->skip_count= s->skip_count;
  1379. d->misc_bits= s->misc_bits;
  1380. d->last_bits= 0;
  1381. d->mb_skiped= s->mb_skiped;
  1382. }
  1383. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1384. int i;
  1385. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  1386. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1387. /* mpeg1 */
  1388. d->mb_incr= s->mb_incr;
  1389. for(i=0; i<3; i++)
  1390. d->last_dc[i]= s->last_dc[i];
  1391. /* statistics */
  1392. d->mv_bits= s->mv_bits;
  1393. d->i_tex_bits= s->i_tex_bits;
  1394. d->p_tex_bits= s->p_tex_bits;
  1395. d->i_count= s->i_count;
  1396. d->p_count= s->p_count;
  1397. d->skip_count= s->skip_count;
  1398. d->misc_bits= s->misc_bits;
  1399. d->mb_intra= s->mb_intra;
  1400. d->mb_skiped= s->mb_skiped;
  1401. d->mv_type= s->mv_type;
  1402. d->mv_dir= s->mv_dir;
  1403. d->pb= s->pb;
  1404. if(s->data_partitioning){
  1405. d->pb2= s->pb2;
  1406. d->tex_pb= s->tex_pb;
  1407. }
  1408. d->block= s->block;
  1409. for(i=0; i<6; i++)
  1410. d->block_last_index[i]= s->block_last_index[i];
  1411. }
  1412. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  1413. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  1414. int *dmin, int *next_block, int motion_x, int motion_y)
  1415. {
  1416. int bits_count;
  1417. copy_context_before_encode(s, backup, type);
  1418. s->block= s->blocks[*next_block];
  1419. s->pb= pb[*next_block];
  1420. if(s->data_partitioning){
  1421. s->pb2 = pb2 [*next_block];
  1422. s->tex_pb= tex_pb[*next_block];
  1423. }
  1424. encode_mb(s, motion_x, motion_y);
  1425. bits_count= get_bit_count(&s->pb);
  1426. if(s->data_partitioning){
  1427. bits_count+= get_bit_count(&s->pb2);
  1428. bits_count+= get_bit_count(&s->tex_pb);
  1429. }
  1430. if(bits_count<*dmin){
  1431. *dmin= bits_count;
  1432. *next_block^=1;
  1433. copy_context_after_encode(best, s, type);
  1434. }
  1435. }
  1436. static void encode_picture(MpegEncContext *s, int picture_number)
  1437. {
  1438. int mb_x, mb_y, last_gob, pdif = 0;
  1439. int i;
  1440. int bits;
  1441. MpegEncContext best_s, backup_s;
  1442. UINT8 bit_buf[2][3000];
  1443. UINT8 bit_buf2[2][3000];
  1444. UINT8 bit_buf_tex[2][3000];
  1445. PutBitContext pb[2], pb2[2], tex_pb[2];
  1446. for(i=0; i<2; i++){
  1447. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  1448. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  1449. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  1450. }
  1451. s->picture_number = picture_number;
  1452. s->block_wrap[0]=
  1453. s->block_wrap[1]=
  1454. s->block_wrap[2]=
  1455. s->block_wrap[3]= s->mb_width*2 + 2;
  1456. s->block_wrap[4]=
  1457. s->block_wrap[5]= s->mb_width + 2;
  1458. /* Reset the average MB variance */
  1459. s->mb_var_sum = 0;
  1460. s->mc_mb_var_sum = 0;
  1461. /* we need to initialize some time vars before we can encode b-frames */
  1462. if (s->h263_pred && !s->h263_msmpeg4)
  1463. ff_set_mpeg4_time(s, s->picture_number);
  1464. /* Estimate motion for every MB */
  1465. if(s->pict_type != I_TYPE){
  1466. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1467. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1468. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1469. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1470. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1471. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1472. s->mb_x = mb_x;
  1473. s->mb_y = mb_y;
  1474. s->block_index[0]+=2;
  1475. s->block_index[1]+=2;
  1476. s->block_index[2]+=2;
  1477. s->block_index[3]+=2;
  1478. /* compute motion vector & mb_type and store in context */
  1479. if(s->pict_type==B_TYPE)
  1480. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  1481. else
  1482. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  1483. // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
  1484. }
  1485. }
  1486. emms_c();
  1487. }else /* if(s->pict_type == I_TYPE) */{
  1488. /* I-Frame */
  1489. //FIXME do we need to zero them?
  1490. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  1491. memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
  1492. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1493. }
  1494. if(s->mb_var_sum < s->mc_mb_var_sum && s->pict_type == P_TYPE){ //FIXME subtract MV bits
  1495. s->pict_type= I_TYPE;
  1496. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1497. if(s->max_b_frames==0){
  1498. s->input_pict_type= I_TYPE;
  1499. s->input_picture_in_gop_number=0;
  1500. }
  1501. //printf("Scene change detected, encoding as I Frame\n");
  1502. }
  1503. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
  1504. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  1505. ff_fix_long_p_mvs(s);
  1506. if(s->pict_type==B_TYPE){
  1507. s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  1508. s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  1509. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  1510. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  1511. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  1512. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  1513. }
  1514. //printf("f_code %d ///\n", s->f_code);
  1515. // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
  1516. if(s->flags&CODEC_FLAG_PASS2)
  1517. s->qscale = ff_rate_estimate_qscale_pass2(s);
  1518. else if (!s->fixed_qscale)
  1519. s->qscale = ff_rate_estimate_qscale(s);
  1520. if (s->out_format == FMT_MJPEG) {
  1521. /* for mjpeg, we do include qscale in the matrix */
  1522. s->intra_matrix[0] = default_intra_matrix[0];
  1523. for(i=1;i<64;i++)
  1524. s->intra_matrix[i] = CLAMP_TO_8BIT((default_intra_matrix[i] * s->qscale) >> 3);
  1525. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16,
  1526. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
  1527. }
  1528. s->last_bits= get_bit_count(&s->pb);
  1529. switch(s->out_format) {
  1530. case FMT_MJPEG:
  1531. mjpeg_picture_header(s);
  1532. break;
  1533. case FMT_H263:
  1534. if (s->h263_msmpeg4)
  1535. msmpeg4_encode_picture_header(s, picture_number);
  1536. else if (s->h263_pred)
  1537. mpeg4_encode_picture_header(s, picture_number);
  1538. else if (s->h263_rv10)
  1539. rv10_encode_picture_header(s, picture_number);
  1540. else
  1541. h263_encode_picture_header(s, picture_number);
  1542. break;
  1543. case FMT_MPEG1:
  1544. mpeg1_encode_picture_header(s, picture_number);
  1545. break;
  1546. }
  1547. bits= get_bit_count(&s->pb);
  1548. s->header_bits= bits - s->last_bits;
  1549. s->last_bits= bits;
  1550. s->mv_bits=0;
  1551. s->misc_bits=0;
  1552. s->i_tex_bits=0;
  1553. s->p_tex_bits=0;
  1554. s->i_count=0;
  1555. s->p_count=0;
  1556. s->skip_count=0;
  1557. /* init last dc values */
  1558. /* note: quant matrix value (8) is implied here */
  1559. s->last_dc[0] = 128;
  1560. s->last_dc[1] = 128;
  1561. s->last_dc[2] = 128;
  1562. s->mb_incr = 1;
  1563. s->last_mv[0][0][0] = 0;
  1564. s->last_mv[0][0][1] = 0;
  1565. /* Get the GOB height based on picture height */
  1566. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  1567. if (s->height <= 400)
  1568. s->gob_index = 1;
  1569. else if (s->height <= 800)
  1570. s->gob_index = 2;
  1571. else
  1572. s->gob_index = 4;
  1573. }else if(s->codec_id==CODEC_ID_MPEG4){
  1574. s->gob_index = 1;
  1575. }
  1576. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  1577. ff_mpeg4_init_partitions(s);
  1578. s->resync_mb_x=0;
  1579. s->resync_mb_y=0;
  1580. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1581. /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
  1582. /* TODO: Put all this stuff in a separate generic function */
  1583. if (s->rtp_mode) {
  1584. if (!mb_y) {
  1585. s->ptr_lastgob = s->pb.buf;
  1586. s->ptr_last_mb_line = s->pb.buf;
  1587. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1588. // MN: we could move the space check from h263 -> here, as its not h263 specific
  1589. last_gob = h263_encode_gob_header(s, mb_y);
  1590. if (last_gob) {
  1591. s->first_slice_line = 1;
  1592. }else{
  1593. /*MN: we reset it here instead at the end of each line cuz mpeg4 can have
  1594. slice lines starting & ending in the middle*/
  1595. s->first_slice_line = 0;
  1596. }
  1597. }
  1598. }
  1599. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1600. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1601. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1602. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1603. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1604. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1605. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1606. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  1607. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  1608. // int d;
  1609. int dmin=10000000;
  1610. s->mb_x = mb_x;
  1611. s->mb_y = mb_y;
  1612. s->block_index[0]+=2;
  1613. s->block_index[1]+=2;
  1614. s->block_index[2]+=2;
  1615. s->block_index[3]+=2;
  1616. s->block_index[4]++;
  1617. s->block_index[5]++;
  1618. /* write gob / video packet header for formats which support it at any MB (MPEG4) */
  1619. if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
  1620. int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
  1621. //the *2 is there so we stay below the requested size
  1622. if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){
  1623. if(s->codec_id==CODEC_ID_MPEG4){
  1624. if(s->data_partitioning && s->pict_type!=B_TYPE){
  1625. ff_mpeg4_merge_partitions(s);
  1626. ff_mpeg4_init_partitions(s);
  1627. }
  1628. ff_mpeg4_encode_video_packet_header(s);
  1629. if(s->flags&CODEC_FLAG_PASS1){
  1630. int bits= get_bit_count(&s->pb);
  1631. s->misc_bits+= bits - s->last_bits;
  1632. s->last_bits= bits;
  1633. }
  1634. ff_mpeg4_clean_buffers(s);
  1635. }
  1636. s->ptr_lastgob = pbBufPtr(&s->pb);
  1637. s->first_slice_line=1;
  1638. s->resync_mb_x=mb_x;
  1639. s->resync_mb_y=mb_y;
  1640. }
  1641. if( (s->resync_mb_x == s->mb_x)
  1642. && s->resync_mb_y+1 == s->mb_y){
  1643. s->first_slice_line=0;
  1644. }
  1645. }
  1646. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  1647. int next_block=0;
  1648. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  1649. copy_context_before_encode(&backup_s, s, -1);
  1650. backup_s.pb= s->pb;
  1651. best_s.data_partitioning= s->data_partitioning;
  1652. if(s->data_partitioning){
  1653. backup_s.pb2= s->pb2;
  1654. backup_s.tex_pb= s->tex_pb;
  1655. }
  1656. if(mb_type&MB_TYPE_INTER){
  1657. s->mv_dir = MV_DIR_FORWARD;
  1658. s->mv_type = MV_TYPE_16X16;
  1659. s->mb_intra= 0;
  1660. s->mv[0][0][0] = s->p_mv_table[xy][0];
  1661. s->mv[0][0][1] = s->p_mv_table[xy][1];
  1662. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  1663. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1664. }
  1665. if(mb_type&MB_TYPE_INTER4V){
  1666. s->mv_dir = MV_DIR_FORWARD;
  1667. s->mv_type = MV_TYPE_8X8;
  1668. s->mb_intra= 0;
  1669. for(i=0; i<4; i++){
  1670. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1671. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1672. }
  1673. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  1674. &dmin, &next_block, 0, 0);
  1675. }
  1676. if(mb_type&MB_TYPE_FORWARD){
  1677. s->mv_dir = MV_DIR_FORWARD;
  1678. s->mv_type = MV_TYPE_16X16;
  1679. s->mb_intra= 0;
  1680. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1681. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1682. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  1683. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1684. }
  1685. if(mb_type&MB_TYPE_BACKWARD){
  1686. s->mv_dir = MV_DIR_BACKWARD;
  1687. s->mv_type = MV_TYPE_16X16;
  1688. s->mb_intra= 0;
  1689. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1690. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1691. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  1692. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  1693. }
  1694. if(mb_type&MB_TYPE_BIDIR){
  1695. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1696. s->mv_type = MV_TYPE_16X16;
  1697. s->mb_intra= 0;
  1698. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1699. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1700. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1701. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1702. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  1703. &dmin, &next_block, 0, 0);
  1704. }
  1705. if(mb_type&MB_TYPE_DIRECT){
  1706. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1707. s->mv_type = MV_TYPE_16X16; //FIXME
  1708. s->mb_intra= 0;
  1709. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1710. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1711. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1712. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1713. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  1714. &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
  1715. }
  1716. if(mb_type&MB_TYPE_INTRA){
  1717. s->mv_dir = MV_DIR_FORWARD;
  1718. s->mv_type = MV_TYPE_16X16;
  1719. s->mb_intra= 1;
  1720. s->mv[0][0][0] = 0;
  1721. s->mv[0][0][1] = 0;
  1722. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  1723. &dmin, &next_block, 0, 0);
  1724. /* force cleaning of ac/dc pred stuff if needed ... */
  1725. if(s->h263_pred || s->h263_aic)
  1726. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1727. }
  1728. copy_context_after_encode(s, &best_s, -1);
  1729. pb_bits_count= get_bit_count(&s->pb);
  1730. flush_put_bits(&s->pb);
  1731. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  1732. s->pb= backup_s.pb;
  1733. if(s->data_partitioning){
  1734. pb2_bits_count= get_bit_count(&s->pb2);
  1735. flush_put_bits(&s->pb2);
  1736. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  1737. s->pb2= backup_s.pb2;
  1738. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  1739. flush_put_bits(&s->tex_pb);
  1740. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  1741. s->tex_pb= backup_s.tex_pb;
  1742. }
  1743. s->last_bits= get_bit_count(&s->pb);
  1744. } else {
  1745. int motion_x, motion_y;
  1746. s->mv_type=MV_TYPE_16X16;
  1747. // only one MB-Type possible
  1748. switch(mb_type){
  1749. case MB_TYPE_INTRA:
  1750. s->mv_dir = MV_DIR_FORWARD;
  1751. s->mb_intra= 1;
  1752. motion_x= s->mv[0][0][0] = 0;
  1753. motion_y= s->mv[0][0][1] = 0;
  1754. break;
  1755. case MB_TYPE_INTER:
  1756. s->mv_dir = MV_DIR_FORWARD;
  1757. s->mb_intra= 0;
  1758. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  1759. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  1760. break;
  1761. case MB_TYPE_INTER4V:
  1762. s->mv_dir = MV_DIR_FORWARD;
  1763. s->mv_type = MV_TYPE_8X8;
  1764. s->mb_intra= 0;
  1765. for(i=0; i<4; i++){
  1766. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1767. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1768. }
  1769. motion_x= motion_y= 0;
  1770. break;
  1771. case MB_TYPE_DIRECT:
  1772. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1773. s->mb_intra= 0;
  1774. motion_x=s->b_direct_mv_table[xy][0];
  1775. motion_y=s->b_direct_mv_table[xy][1];
  1776. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1777. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1778. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1779. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1780. break;
  1781. case MB_TYPE_BIDIR:
  1782. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1783. s->mb_intra= 0;
  1784. motion_x=0;
  1785. motion_y=0;
  1786. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1787. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1788. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1789. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1790. break;
  1791. case MB_TYPE_BACKWARD:
  1792. s->mv_dir = MV_DIR_BACKWARD;
  1793. s->mb_intra= 0;
  1794. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1795. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1796. break;
  1797. case MB_TYPE_FORWARD:
  1798. s->mv_dir = MV_DIR_FORWARD;
  1799. s->mb_intra= 0;
  1800. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1801. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1802. // printf(" %d %d ", motion_x, motion_y);
  1803. break;
  1804. default:
  1805. motion_x=motion_y=0; //gcc warning fix
  1806. printf("illegal MB type\n");
  1807. }
  1808. encode_mb(s, motion_x, motion_y);
  1809. }
  1810. /* clean the MV table in IPS frames for direct mode in B frames */
  1811. if(s->mb_intra /* && I,P,S_TYPE */){
  1812. s->p_mv_table[xy][0]=0;
  1813. s->p_mv_table[xy][1]=0;
  1814. }
  1815. MPV_decode_mb(s, s->block);
  1816. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  1817. }
  1818. /* Obtain average GOB size for RTP */
  1819. if (s->rtp_mode) {
  1820. if (!mb_y)
  1821. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  1822. else if (!(mb_y % s->gob_index)) {
  1823. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  1824. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  1825. }
  1826. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  1827. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  1828. if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
  1829. }
  1830. }
  1831. emms_c();
  1832. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  1833. ff_mpeg4_merge_partitions(s);
  1834. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  1835. msmpeg4_encode_ext_header(s);
  1836. if(s->codec_id==CODEC_ID_MPEG4)
  1837. ff_mpeg4_stuffing(&s->pb);
  1838. //if (s->gob_number)
  1839. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  1840. /* Send the last GOB if RTP */
  1841. if (s->rtp_mode) {
  1842. flush_put_bits(&s->pb);
  1843. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1844. /* Call the RTP callback to send the last GOB */
  1845. if (s->rtp_callback)
  1846. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  1847. s->ptr_lastgob = pbBufPtr(&s->pb);
  1848. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  1849. }
  1850. }
  1851. static int dct_quantize_c(MpegEncContext *s,
  1852. DCTELEM *block, int n,
  1853. int qscale, int *overflow)
  1854. {
  1855. int i, j, level, last_non_zero, q;
  1856. const int *qmat;
  1857. int bias;
  1858. int max=0;
  1859. unsigned int threshold1, threshold2;
  1860. av_fdct (block);
  1861. /* we need this permutation so that we correct the IDCT
  1862. permutation. will be moved into DCT code */
  1863. block_permute(block);
  1864. if (s->mb_intra) {
  1865. if (!s->h263_aic) {
  1866. if (n < 4)
  1867. q = s->y_dc_scale;
  1868. else
  1869. q = s->c_dc_scale;
  1870. q = q << 3;
  1871. } else
  1872. /* For AIC we skip quant/dequant of INTRADC */
  1873. q = 1 << 3;
  1874. /* note: block[0] is assumed to be positive */
  1875. block[0] = (block[0] + (q >> 1)) / q;
  1876. i = 1;
  1877. last_non_zero = 0;
  1878. qmat = s->q_intra_matrix[qscale];
  1879. bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  1880. } else {
  1881. i = 0;
  1882. last_non_zero = -1;
  1883. qmat = s->q_inter_matrix[qscale];
  1884. bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  1885. }
  1886. threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
  1887. threshold2= threshold1<<1;
  1888. for(;i<64;i++) {
  1889. j = zigzag_direct[i];
  1890. level = block[j];
  1891. level = level * qmat[j];
  1892. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1893. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1894. if(((unsigned)(level+threshold1))>threshold2){
  1895. if(level>0){
  1896. level= (bias + level)>>(QMAT_SHIFT - 3);
  1897. block[j]= level;
  1898. }else{
  1899. level= (bias - level)>>(QMAT_SHIFT - 3);
  1900. block[j]= -level;
  1901. }
  1902. max |=level;
  1903. last_non_zero = i;
  1904. }else{
  1905. block[j]=0;
  1906. }
  1907. }
  1908. *overflow= s->max_qcoeff < max; //overflow might have happend
  1909. return last_non_zero;
  1910. }
  1911. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1912. DCTELEM *block, int n, int qscale)
  1913. {
  1914. int i, level, nCoeffs;
  1915. const UINT16 *quant_matrix;
  1916. if(s->alternate_scan) nCoeffs= 64;
  1917. else nCoeffs= s->block_last_index[n]+1;
  1918. if (s->mb_intra) {
  1919. if (n < 4)
  1920. block[0] = block[0] * s->y_dc_scale;
  1921. else
  1922. block[0] = block[0] * s->c_dc_scale;
  1923. /* XXX: only mpeg1 */
  1924. quant_matrix = s->intra_matrix;
  1925. for(i=1;i<nCoeffs;i++) {
  1926. int j= zigzag_direct[i];
  1927. level = block[j];
  1928. if (level) {
  1929. if (level < 0) {
  1930. level = -level;
  1931. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1932. level = (level - 1) | 1;
  1933. level = -level;
  1934. } else {
  1935. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1936. level = (level - 1) | 1;
  1937. }
  1938. #ifdef PARANOID
  1939. if (level < -2048 || level > 2047)
  1940. fprintf(stderr, "unquant error %d %d\n", i, level);
  1941. #endif
  1942. block[j] = level;
  1943. }
  1944. }
  1945. } else {
  1946. i = 0;
  1947. quant_matrix = s->inter_matrix;
  1948. for(;i<nCoeffs;i++) {
  1949. int j= zigzag_direct[i];
  1950. level = block[j];
  1951. if (level) {
  1952. if (level < 0) {
  1953. level = -level;
  1954. level = (((level << 1) + 1) * qscale *
  1955. ((int) (quant_matrix[j]))) >> 4;
  1956. level = (level - 1) | 1;
  1957. level = -level;
  1958. } else {
  1959. level = (((level << 1) + 1) * qscale *
  1960. ((int) (quant_matrix[j]))) >> 4;
  1961. level = (level - 1) | 1;
  1962. }
  1963. #ifdef PARANOID
  1964. if (level < -2048 || level > 2047)
  1965. fprintf(stderr, "unquant error %d %d\n", i, level);
  1966. #endif
  1967. block[j] = level;
  1968. }
  1969. }
  1970. }
  1971. }
  1972. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  1973. DCTELEM *block, int n, int qscale)
  1974. {
  1975. int i, level, nCoeffs;
  1976. const UINT16 *quant_matrix;
  1977. if(s->alternate_scan) nCoeffs= 64;
  1978. else nCoeffs= s->block_last_index[n]+1;
  1979. if (s->mb_intra) {
  1980. if (n < 4)
  1981. block[0] = block[0] * s->y_dc_scale;
  1982. else
  1983. block[0] = block[0] * s->c_dc_scale;
  1984. quant_matrix = s->intra_matrix;
  1985. for(i=1;i<nCoeffs;i++) {
  1986. int j= zigzag_direct[i];
  1987. level = block[j];
  1988. if (level) {
  1989. if (level < 0) {
  1990. level = -level;
  1991. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1992. level = -level;
  1993. } else {
  1994. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1995. }
  1996. #ifdef PARANOID
  1997. if (level < -2048 || level > 2047)
  1998. fprintf(stderr, "unquant error %d %d\n", i, level);
  1999. #endif
  2000. block[j] = level;
  2001. }
  2002. }
  2003. } else {
  2004. int sum=-1;
  2005. i = 0;
  2006. quant_matrix = s->inter_matrix;
  2007. for(;i<nCoeffs;i++) {
  2008. int j= zigzag_direct[i];
  2009. level = block[j];
  2010. if (level) {
  2011. if (level < 0) {
  2012. level = -level;
  2013. level = (((level << 1) + 1) * qscale *
  2014. ((int) (quant_matrix[j]))) >> 4;
  2015. level = -level;
  2016. } else {
  2017. level = (((level << 1) + 1) * qscale *
  2018. ((int) (quant_matrix[j]))) >> 4;
  2019. }
  2020. #ifdef PARANOID
  2021. if (level < -2048 || level > 2047)
  2022. fprintf(stderr, "unquant error %d %d\n", i, level);
  2023. #endif
  2024. block[j] = level;
  2025. sum+=level;
  2026. }
  2027. }
  2028. block[63]^=sum&1;
  2029. }
  2030. }
  2031. static void dct_unquantize_h263_c(MpegEncContext *s,
  2032. DCTELEM *block, int n, int qscale)
  2033. {
  2034. int i, level, qmul, qadd;
  2035. int nCoeffs;
  2036. if (s->mb_intra) {
  2037. if (!s->h263_aic) {
  2038. if (n < 4)
  2039. block[0] = block[0] * s->y_dc_scale;
  2040. else
  2041. block[0] = block[0] * s->c_dc_scale;
  2042. }
  2043. i = 1;
  2044. nCoeffs= 64; //does not allways use zigzag table
  2045. } else {
  2046. i = 0;
  2047. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  2048. }
  2049. qmul = s->qscale << 1;
  2050. if (s->h263_aic && s->mb_intra)
  2051. qadd = 0;
  2052. else
  2053. qadd = (s->qscale - 1) | 1;
  2054. for(;i<nCoeffs;i++) {
  2055. level = block[i];
  2056. if (level) {
  2057. if (level < 0) {
  2058. level = level * qmul - qadd;
  2059. } else {
  2060. level = level * qmul + qadd;
  2061. }
  2062. #ifdef PARANOID
  2063. if (level < -2048 || level > 2047)
  2064. fprintf(stderr, "unquant error %d %d\n", i, level);
  2065. #endif
  2066. block[i] = level;
  2067. }
  2068. }
  2069. }
  2070. static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
  2071. {
  2072. int dc, dcb, dcr, y, i;
  2073. for(i=0; i<4; i++){
  2074. dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
  2075. for(y=0; y<8; y++){
  2076. int x;
  2077. for(x=0; x<8; x++){
  2078. dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
  2079. }
  2080. }
  2081. }
  2082. dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2083. dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2084. for(y=0; y<8; y++){
  2085. int x;
  2086. for(x=0; x<8; x++){
  2087. dest_cb[x + y*(s->linesize>>1)]= dcb/8;
  2088. dest_cr[x + y*(s->linesize>>1)]= dcr/8;
  2089. }
  2090. }
  2091. }
  2092. /**
  2093. * will conceal past errors, and allso drop b frames if needed
  2094. *
  2095. */
  2096. void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
  2097. {
  2098. int mb_x= s->mb_x;
  2099. int mb_y= s->mb_y;
  2100. int mb_dist=0;
  2101. int i, intra_count=0, inter_count=0;
  2102. int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
  2103. int inter_conceal= s->msmpeg4_version ? 50 : 50;
  2104. // for last block
  2105. if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
  2106. if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
  2107. if(s->decoding_error==0 && unknown_pos){
  2108. if(s->data_partitioning && s->pict_type!=B_TYPE)
  2109. s->decoding_error= DECODING_AC_LOST;
  2110. else
  2111. s->decoding_error= DECODING_DESYNC;
  2112. }
  2113. if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
  2114. for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
  2115. if(s->mbintra_table[i]) intra_count++;
  2116. else inter_count++;
  2117. }
  2118. if(s->decoding_error==DECODING_AC_LOST){
  2119. intra_conceal*=2;
  2120. inter_conceal*=2;
  2121. }else if(s->decoding_error==DECODING_ACDC_LOST){
  2122. intra_conceal*=2;
  2123. inter_conceal*=2;
  2124. }
  2125. if(unknown_pos && (intra_count<inter_count)){
  2126. intra_conceal= inter_conceal= s->mb_num;
  2127. // printf("%d %d\n",intra_count, inter_count);
  2128. }
  2129. fprintf(stderr, "concealing errors\n");
  2130. /* for all MBs from the current one back until the last resync marker */
  2131. for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
  2132. for(; mb_x>=0; mb_x--){
  2133. uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2134. uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  2135. uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  2136. int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
  2137. int mb_y_backup= s->mb_y;
  2138. s->mb_x=mb_x;
  2139. s->mb_y=mb_y;
  2140. if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
  2141. if(s->decoding_error==DECODING_AC_LOST){
  2142. remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  2143. // printf("remove ac to %d %d\n", mb_x, mb_y);
  2144. }else{
  2145. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2146. s->last_picture, 0, 0, put_pixels_tab,
  2147. 0/*mx*/, 0/*my*/, 16);
  2148. }
  2149. }
  2150. else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
  2151. int mx=0;
  2152. int my=0;
  2153. if(s->decoding_error!=DECODING_DESYNC){
  2154. int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
  2155. mx= s->motion_val[ xy ][0];
  2156. my= s->motion_val[ xy ][1];
  2157. }
  2158. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2159. s->last_picture, 0, 0, put_pixels_tab,
  2160. mx, my, 16);
  2161. }
  2162. s->mb_x= mb_x_backup;
  2163. s->mb_y= mb_y_backup;
  2164. if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
  2165. if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
  2166. }
  2167. mb_x=s->mb_width-1;
  2168. }
  2169. }
  2170. AVCodec mpeg1video_encoder = {
  2171. "mpeg1video",
  2172. CODEC_TYPE_VIDEO,
  2173. CODEC_ID_MPEG1VIDEO,
  2174. sizeof(MpegEncContext),
  2175. MPV_encode_init,
  2176. MPV_encode_picture,
  2177. MPV_encode_end,
  2178. };
  2179. AVCodec h263_encoder = {
  2180. "h263",
  2181. CODEC_TYPE_VIDEO,
  2182. CODEC_ID_H263,
  2183. sizeof(MpegEncContext),
  2184. MPV_encode_init,
  2185. MPV_encode_picture,
  2186. MPV_encode_end,
  2187. };
  2188. AVCodec h263p_encoder = {
  2189. "h263p",
  2190. CODEC_TYPE_VIDEO,
  2191. CODEC_ID_H263P,
  2192. sizeof(MpegEncContext),
  2193. MPV_encode_init,
  2194. MPV_encode_picture,
  2195. MPV_encode_end,
  2196. };
  2197. AVCodec rv10_encoder = {
  2198. "rv10",
  2199. CODEC_TYPE_VIDEO,
  2200. CODEC_ID_RV10,
  2201. sizeof(MpegEncContext),
  2202. MPV_encode_init,
  2203. MPV_encode_picture,
  2204. MPV_encode_end,
  2205. };
  2206. AVCodec mjpeg_encoder = {
  2207. "mjpeg",
  2208. CODEC_TYPE_VIDEO,
  2209. CODEC_ID_MJPEG,
  2210. sizeof(MpegEncContext),
  2211. MPV_encode_init,
  2212. MPV_encode_picture,
  2213. MPV_encode_end,
  2214. };
  2215. AVCodec mpeg4_encoder = {
  2216. "mpeg4",
  2217. CODEC_TYPE_VIDEO,
  2218. CODEC_ID_MPEG4,
  2219. sizeof(MpegEncContext),
  2220. MPV_encode_init,
  2221. MPV_encode_picture,
  2222. MPV_encode_end,
  2223. };
  2224. AVCodec msmpeg4v1_encoder = {
  2225. "msmpeg4v1",
  2226. CODEC_TYPE_VIDEO,
  2227. CODEC_ID_MSMPEG4V1,
  2228. sizeof(MpegEncContext),
  2229. MPV_encode_init,
  2230. MPV_encode_picture,
  2231. MPV_encode_end,
  2232. };
  2233. AVCodec msmpeg4v2_encoder = {
  2234. "msmpeg4v2",
  2235. CODEC_TYPE_VIDEO,
  2236. CODEC_ID_MSMPEG4V2,
  2237. sizeof(MpegEncContext),
  2238. MPV_encode_init,
  2239. MPV_encode_picture,
  2240. MPV_encode_end,
  2241. };
  2242. AVCodec msmpeg4v3_encoder = {
  2243. "msmpeg4",
  2244. CODEC_TYPE_VIDEO,
  2245. CODEC_ID_MSMPEG4V3,
  2246. sizeof(MpegEncContext),
  2247. MPV_encode_init,
  2248. MPV_encode_picture,
  2249. MPV_encode_end,
  2250. };