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.

2543 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 == jpeg_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. quant_store[mb_y][mb_x]=s->qscale;
  1024. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  1025. #endif
  1026. /* update DC predictors for P macroblocks */
  1027. if (!s->mb_intra) {
  1028. if (s->h263_pred || s->h263_aic) {
  1029. if(s->mbintra_table[mb_x + mb_y*s->mb_width])
  1030. ff_clean_intra_table_entries(s);
  1031. } else {
  1032. s->last_dc[0] =
  1033. s->last_dc[1] =
  1034. s->last_dc[2] = 128 << s->intra_dc_precision;
  1035. }
  1036. }
  1037. else if (s->h263_pred || s->h263_aic)
  1038. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1039. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1040. if (s->out_format == FMT_H263) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1041. if(s->pict_type!=B_TYPE){
  1042. int xy, wrap, motion_x, motion_y;
  1043. wrap = 2 * s->mb_width + 2;
  1044. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  1045. if (s->mb_intra) {
  1046. motion_x = 0;
  1047. motion_y = 0;
  1048. goto motion_init;
  1049. } else if (s->mv_type == MV_TYPE_16X16) {
  1050. motion_x = s->mv[0][0][0];
  1051. motion_y = s->mv[0][0][1];
  1052. motion_init:
  1053. /* no update if 8X8 because it has been done during parsing */
  1054. s->motion_val[xy][0] = motion_x;
  1055. s->motion_val[xy][1] = motion_y;
  1056. s->motion_val[xy + 1][0] = motion_x;
  1057. s->motion_val[xy + 1][1] = motion_y;
  1058. s->motion_val[xy + wrap][0] = motion_x;
  1059. s->motion_val[xy + wrap][1] = motion_y;
  1060. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1061. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1062. }
  1063. }
  1064. }
  1065. if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
  1066. UINT8 *dest_y, *dest_cb, *dest_cr;
  1067. UINT8 *mbskip_ptr;
  1068. /* avoid copy if macroblock skipped in last frame too
  1069. dont touch it for B-frames as they need the skip info from the next p-frame */
  1070. if (s->pict_type != B_TYPE) {
  1071. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  1072. if (s->mb_skiped) {
  1073. s->mb_skiped = 0;
  1074. /* if previous was skipped too, then nothing to do !
  1075. skip only during decoding as we might trash the buffers during encoding a bit */
  1076. if (*mbskip_ptr != 0 && !s->encoding)
  1077. goto the_end;
  1078. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  1079. } else {
  1080. *mbskip_ptr = 0; /* not skipped */
  1081. }
  1082. }
  1083. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  1084. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1085. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1086. if (s->interlaced_dct) {
  1087. dct_linesize = s->linesize * 2;
  1088. dct_offset = s->linesize;
  1089. } else {
  1090. dct_linesize = s->linesize;
  1091. dct_offset = s->linesize * 8;
  1092. }
  1093. if (!s->mb_intra) {
  1094. const int xy= s->mb_y * s->mb_width + s->mb_x;
  1095. /* motion handling */
  1096. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1097. if((!s->encoding) || (s->mb_type[xy]&(s->mb_type[xy]-1))){
  1098. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1099. op_pix = put_pixels_tab;
  1100. op_qpix= qpel_mc_rnd_tab;
  1101. }else{
  1102. op_pix = put_no_rnd_pixels_tab;
  1103. op_qpix= qpel_mc_no_rnd_tab;
  1104. }
  1105. if (s->mv_dir & MV_DIR_FORWARD) {
  1106. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1107. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1108. op_pix = avg_pixels_tab;
  1109. else
  1110. op_pix = avg_no_rnd_pixels_tab;
  1111. }
  1112. if (s->mv_dir & MV_DIR_BACKWARD) {
  1113. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1114. }
  1115. }
  1116. /* add dct residue */
  1117. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1118. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1119. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1120. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1121. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1122. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1123. } else {
  1124. /* dct only in intra block */
  1125. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1126. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1127. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1128. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1129. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1130. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1131. }
  1132. }
  1133. the_end:
  1134. emms_c(); //FIXME remove
  1135. }
  1136. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc)
  1137. {
  1138. static const char tab[64]=
  1139. {3,2,2,1,1,1,1,1,
  1140. 1,1,1,1,1,1,1,1,
  1141. 1,1,1,1,1,1,1,1,
  1142. 0,0,0,0,0,0,0,0,
  1143. 0,0,0,0,0,0,0,0,
  1144. 0,0,0,0,0,0,0,0,
  1145. 0,0,0,0,0,0,0,0,
  1146. 0,0,0,0,0,0,0,0};
  1147. int score=0;
  1148. int run=0;
  1149. int i;
  1150. DCTELEM *block= s->block[n];
  1151. const int last_index= s->block_last_index[n];
  1152. if(skip_dc) skip_dc=1;
  1153. /* are all which we could set to zero are allready zero? */
  1154. if(last_index<=skip_dc - 1) return;
  1155. for(i=0; i<=last_index; i++){
  1156. const int j = zigzag_direct[i];
  1157. const int level = ABS(block[j]);
  1158. if(level==1){
  1159. if(skip_dc && i==0) continue;
  1160. score+= tab[run];
  1161. run=0;
  1162. }else if(level>1){
  1163. return;
  1164. }else{
  1165. run++;
  1166. }
  1167. }
  1168. if(score >= threshold) return;
  1169. for(i=skip_dc; i<=last_index; i++){
  1170. const int j = zigzag_direct[i];
  1171. block[j]=0;
  1172. }
  1173. if(block[0]) s->block_last_index[n]= 0;
  1174. else s->block_last_index[n]= -1;
  1175. }
  1176. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1177. {
  1178. int i;
  1179. const int maxlevel= s->max_qcoeff;
  1180. const int minlevel= s->min_qcoeff;
  1181. for(i=0;i<=last_index; i++){
  1182. const int j = zigzag_direct[i];
  1183. int level = block[j];
  1184. if (level>maxlevel) level=maxlevel;
  1185. else if(level<minlevel) level=minlevel;
  1186. block[j]= level;
  1187. }
  1188. }
  1189. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1190. {
  1191. const int mb_x= s->mb_x;
  1192. const int mb_y= s->mb_y;
  1193. int i;
  1194. int skip_dct[6];
  1195. #if 0
  1196. if (s->interlaced_dct) {
  1197. dct_linesize = s->linesize * 2;
  1198. dct_offset = s->linesize;
  1199. } else {
  1200. dct_linesize = s->linesize;
  1201. dct_offset = s->linesize * 8;
  1202. }
  1203. #endif
  1204. for(i=0; i<6; i++) skip_dct[i]=0;
  1205. if (s->mb_intra) {
  1206. UINT8 *ptr;
  1207. int wrap;
  1208. wrap = s->linesize;
  1209. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1210. get_pixels(s->block[0], ptr , wrap);
  1211. get_pixels(s->block[1], ptr + 8, wrap);
  1212. get_pixels(s->block[2], ptr + 8 * wrap , wrap);
  1213. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1214. wrap >>=1;
  1215. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1216. get_pixels(s->block[4], ptr, wrap);
  1217. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1218. get_pixels(s->block[5], ptr, wrap);
  1219. }else{
  1220. op_pixels_func *op_pix;
  1221. qpel_mc_func *op_qpix;
  1222. UINT8 *dest_y, *dest_cb, *dest_cr;
  1223. UINT8 *ptr_y, *ptr_cb, *ptr_cr;
  1224. int wrap_y, wrap_c;
  1225. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  1226. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1227. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1228. wrap_y = s->linesize;
  1229. wrap_c = wrap_y>>1;
  1230. ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1231. ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1232. ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1233. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1234. op_pix = put_pixels_tab;
  1235. op_qpix= qpel_mc_rnd_tab;
  1236. }else{
  1237. op_pix = put_no_rnd_pixels_tab;
  1238. op_qpix= qpel_mc_no_rnd_tab;
  1239. }
  1240. if (s->mv_dir & MV_DIR_FORWARD) {
  1241. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1242. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1243. op_pix = avg_pixels_tab;
  1244. else
  1245. op_pix = avg_no_rnd_pixels_tab;
  1246. }
  1247. if (s->mv_dir & MV_DIR_BACKWARD) {
  1248. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1249. }
  1250. diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  1251. diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1252. diff_pixels(s->block[2], ptr_y + 8 * wrap_y , dest_y + 8 * wrap_y , wrap_y);
  1253. diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y);
  1254. diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1255. diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1256. /* pre quantization */
  1257. if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  1258. if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  1259. if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  1260. if(pix_abs8x8(ptr_y + 8*wrap_y , dest_y + 8*wrap_y , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  1261. if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  1262. if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
  1263. if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
  1264. #if 0
  1265. {
  1266. static int stat[7];
  1267. int num=0;
  1268. for(i=0; i<6; i++)
  1269. if(skip_dct[i]) num++;
  1270. stat[num]++;
  1271. if(s->mb_x==0 && s->mb_y==0){
  1272. for(i=0; i<7; i++){
  1273. printf("%6d %1d\n", stat[i], i);
  1274. }
  1275. }
  1276. }
  1277. #endif
  1278. }
  1279. }
  1280. #if 0
  1281. {
  1282. float adap_parm;
  1283. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1284. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1285. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1286. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1287. s->qscale, adap_parm, s->qscale*adap_parm,
  1288. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1289. }
  1290. #endif
  1291. /* DCT & quantize */
  1292. if (s->h263_pred && !(s->msmpeg4_version==1 || s->msmpeg4_version==2)) {
  1293. h263_dc_scale(s);
  1294. } else if (s->h263_aic) {
  1295. s->y_dc_scale = 2*s->qscale;
  1296. s->c_dc_scale = 2*s->qscale;
  1297. } else {
  1298. /* default quantization values */
  1299. s->y_dc_scale = 8;
  1300. s->c_dc_scale = 8;
  1301. }
  1302. if(s->out_format==FMT_MJPEG){
  1303. for(i=0;i<6;i++) {
  1304. int overflow;
  1305. s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
  1306. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1307. }
  1308. }else{
  1309. for(i=0;i<6;i++) {
  1310. if(!skip_dct[i]){
  1311. int overflow;
  1312. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1313. // FIXME we could decide to change to quantizer instead of clipping
  1314. // JS: I don't think that would be a good idea it could lower quality instead
  1315. // of improve it. Just INTRADC clipping deserves changes in quantizer
  1316. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1317. }else
  1318. s->block_last_index[i]= -1;
  1319. }
  1320. if(s->luma_elim_threshold && !s->mb_intra)
  1321. for(i=0; i<4; i++)
  1322. dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0);
  1323. if(s->chroma_elim_threshold && !s->mb_intra)
  1324. for(i=4; i<6; i++)
  1325. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1);
  1326. }
  1327. /* huffman encode */
  1328. switch(s->out_format) {
  1329. case FMT_MPEG1:
  1330. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1331. break;
  1332. case FMT_H263:
  1333. if (s->h263_msmpeg4)
  1334. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1335. else if(s->h263_pred)
  1336. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1337. else
  1338. h263_encode_mb(s, s->block, motion_x, motion_y);
  1339. break;
  1340. case FMT_MJPEG:
  1341. mjpeg_encode_mb(s, s->block);
  1342. break;
  1343. }
  1344. }
  1345. void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
  1346. {
  1347. #if 1
  1348. int bytes= length>>4;
  1349. int bits= length&15;
  1350. int i;
  1351. if(length==0) return;
  1352. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  1353. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  1354. #else
  1355. int bytes= length>>3;
  1356. int bits= length&7;
  1357. int i;
  1358. for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
  1359. put_bits(pb, bits, src[i]>>(8-bits));
  1360. #endif
  1361. }
  1362. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1363. int i;
  1364. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1365. /* mpeg1 */
  1366. d->mb_incr= s->mb_incr;
  1367. for(i=0; i<3; i++)
  1368. d->last_dc[i]= s->last_dc[i];
  1369. /* statistics */
  1370. d->mv_bits= s->mv_bits;
  1371. d->i_tex_bits= s->i_tex_bits;
  1372. d->p_tex_bits= s->p_tex_bits;
  1373. d->i_count= s->i_count;
  1374. d->p_count= s->p_count;
  1375. d->skip_count= s->skip_count;
  1376. d->misc_bits= s->misc_bits;
  1377. d->last_bits= 0;
  1378. d->mb_skiped= s->mb_skiped;
  1379. }
  1380. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1381. int i;
  1382. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  1383. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1384. /* mpeg1 */
  1385. d->mb_incr= s->mb_incr;
  1386. for(i=0; i<3; i++)
  1387. d->last_dc[i]= s->last_dc[i];
  1388. /* statistics */
  1389. d->mv_bits= s->mv_bits;
  1390. d->i_tex_bits= s->i_tex_bits;
  1391. d->p_tex_bits= s->p_tex_bits;
  1392. d->i_count= s->i_count;
  1393. d->p_count= s->p_count;
  1394. d->skip_count= s->skip_count;
  1395. d->misc_bits= s->misc_bits;
  1396. d->mb_intra= s->mb_intra;
  1397. d->mb_skiped= s->mb_skiped;
  1398. d->mv_type= s->mv_type;
  1399. d->mv_dir= s->mv_dir;
  1400. d->pb= s->pb;
  1401. if(s->data_partitioning){
  1402. d->pb2= s->pb2;
  1403. d->tex_pb= s->tex_pb;
  1404. }
  1405. d->block= s->block;
  1406. for(i=0; i<6; i++)
  1407. d->block_last_index[i]= s->block_last_index[i];
  1408. }
  1409. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  1410. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  1411. int *dmin, int *next_block, int motion_x, int motion_y)
  1412. {
  1413. int bits_count;
  1414. copy_context_before_encode(s, backup, type);
  1415. s->block= s->blocks[*next_block];
  1416. s->pb= pb[*next_block];
  1417. if(s->data_partitioning){
  1418. s->pb2 = pb2 [*next_block];
  1419. s->tex_pb= tex_pb[*next_block];
  1420. }
  1421. encode_mb(s, motion_x, motion_y);
  1422. bits_count= get_bit_count(&s->pb);
  1423. if(s->data_partitioning){
  1424. bits_count+= get_bit_count(&s->pb2);
  1425. bits_count+= get_bit_count(&s->tex_pb);
  1426. }
  1427. if(bits_count<*dmin){
  1428. *dmin= bits_count;
  1429. *next_block^=1;
  1430. copy_context_after_encode(best, s, type);
  1431. }
  1432. }
  1433. static void encode_picture(MpegEncContext *s, int picture_number)
  1434. {
  1435. int mb_x, mb_y, last_gob, pdif = 0;
  1436. int i;
  1437. int bits;
  1438. MpegEncContext best_s, backup_s;
  1439. UINT8 bit_buf[2][3000];
  1440. UINT8 bit_buf2[2][3000];
  1441. UINT8 bit_buf_tex[2][3000];
  1442. PutBitContext pb[2], pb2[2], tex_pb[2];
  1443. for(i=0; i<2; i++){
  1444. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  1445. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  1446. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  1447. }
  1448. s->picture_number = picture_number;
  1449. s->block_wrap[0]=
  1450. s->block_wrap[1]=
  1451. s->block_wrap[2]=
  1452. s->block_wrap[3]= s->mb_width*2 + 2;
  1453. s->block_wrap[4]=
  1454. s->block_wrap[5]= s->mb_width + 2;
  1455. /* Reset the average MB variance */
  1456. s->mb_var_sum = 0;
  1457. s->mc_mb_var_sum = 0;
  1458. /* we need to initialize some time vars before we can encode b-frames */
  1459. if (s->h263_pred && !s->h263_msmpeg4)
  1460. ff_set_mpeg4_time(s, s->picture_number);
  1461. /* Estimate motion for every MB */
  1462. if(s->pict_type != I_TYPE){
  1463. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1464. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1465. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1466. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1467. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1468. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1469. s->mb_x = mb_x;
  1470. s->mb_y = mb_y;
  1471. s->block_index[0]+=2;
  1472. s->block_index[1]+=2;
  1473. s->block_index[2]+=2;
  1474. s->block_index[3]+=2;
  1475. /* compute motion vector & mb_type and store in context */
  1476. if(s->pict_type==B_TYPE)
  1477. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  1478. else
  1479. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  1480. // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
  1481. }
  1482. }
  1483. emms_c();
  1484. }else /* if(s->pict_type == I_TYPE) */{
  1485. /* I-Frame */
  1486. //FIXME do we need to zero them?
  1487. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  1488. memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
  1489. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1490. }
  1491. if(s->mb_var_sum < s->mc_mb_var_sum && s->pict_type == P_TYPE){ //FIXME subtract MV bits
  1492. s->pict_type= I_TYPE;
  1493. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1494. if(s->max_b_frames==0){
  1495. s->input_pict_type= I_TYPE;
  1496. s->input_picture_in_gop_number=0;
  1497. }
  1498. //printf("Scene change detected, encoding as I Frame\n");
  1499. }
  1500. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
  1501. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  1502. ff_fix_long_p_mvs(s);
  1503. if(s->pict_type==B_TYPE){
  1504. s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  1505. s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  1506. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  1507. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  1508. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  1509. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  1510. }
  1511. //printf("f_code %d ///\n", s->f_code);
  1512. // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
  1513. if(s->flags&CODEC_FLAG_PASS2)
  1514. s->qscale = ff_rate_estimate_qscale_pass2(s);
  1515. else if (!s->fixed_qscale)
  1516. s->qscale = ff_rate_estimate_qscale(s);
  1517. if (s->out_format == FMT_MJPEG) {
  1518. /* for mjpeg, we do include qscale in the matrix */
  1519. s->intra_matrix[0] = default_intra_matrix[0];
  1520. for(i=1;i<64;i++)
  1521. s->intra_matrix[i] = CLAMP_TO_8BIT((default_intra_matrix[i] * s->qscale) >> 3);
  1522. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16,
  1523. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
  1524. }
  1525. s->last_bits= get_bit_count(&s->pb);
  1526. switch(s->out_format) {
  1527. case FMT_MJPEG:
  1528. mjpeg_picture_header(s);
  1529. break;
  1530. case FMT_H263:
  1531. if (s->h263_msmpeg4)
  1532. msmpeg4_encode_picture_header(s, picture_number);
  1533. else if (s->h263_pred)
  1534. mpeg4_encode_picture_header(s, picture_number);
  1535. else if (s->h263_rv10)
  1536. rv10_encode_picture_header(s, picture_number);
  1537. else
  1538. h263_encode_picture_header(s, picture_number);
  1539. break;
  1540. case FMT_MPEG1:
  1541. mpeg1_encode_picture_header(s, picture_number);
  1542. break;
  1543. }
  1544. bits= get_bit_count(&s->pb);
  1545. s->header_bits= bits - s->last_bits;
  1546. s->last_bits= bits;
  1547. s->mv_bits=0;
  1548. s->misc_bits=0;
  1549. s->i_tex_bits=0;
  1550. s->p_tex_bits=0;
  1551. s->i_count=0;
  1552. s->p_count=0;
  1553. s->skip_count=0;
  1554. /* init last dc values */
  1555. /* note: quant matrix value (8) is implied here */
  1556. s->last_dc[0] = 128;
  1557. s->last_dc[1] = 128;
  1558. s->last_dc[2] = 128;
  1559. s->mb_incr = 1;
  1560. s->last_mv[0][0][0] = 0;
  1561. s->last_mv[0][0][1] = 0;
  1562. /* Get the GOB height based on picture height */
  1563. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  1564. if (s->height <= 400)
  1565. s->gob_index = 1;
  1566. else if (s->height <= 800)
  1567. s->gob_index = 2;
  1568. else
  1569. s->gob_index = 4;
  1570. }else if(s->codec_id==CODEC_ID_MPEG4){
  1571. s->gob_index = 1;
  1572. }
  1573. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  1574. ff_mpeg4_init_partitions(s);
  1575. s->resync_mb_x=0;
  1576. s->resync_mb_y=0;
  1577. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1578. /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
  1579. /* TODO: Put all this stuff in a separate generic function */
  1580. if (s->rtp_mode) {
  1581. if (!mb_y) {
  1582. s->ptr_lastgob = s->pb.buf;
  1583. s->ptr_last_mb_line = s->pb.buf;
  1584. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1585. // MN: we could move the space check from h263 -> here, as its not h263 specific
  1586. last_gob = h263_encode_gob_header(s, mb_y);
  1587. if (last_gob) {
  1588. s->first_slice_line = 1;
  1589. }else{
  1590. /*MN: we reset it here instead at the end of each line cuz mpeg4 can have
  1591. slice lines starting & ending in the middle*/
  1592. s->first_slice_line = 0;
  1593. }
  1594. }
  1595. }
  1596. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1597. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1598. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1599. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1600. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1601. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1602. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1603. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  1604. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  1605. // int d;
  1606. int dmin=10000000;
  1607. s->mb_x = mb_x;
  1608. s->mb_y = mb_y;
  1609. s->block_index[0]+=2;
  1610. s->block_index[1]+=2;
  1611. s->block_index[2]+=2;
  1612. s->block_index[3]+=2;
  1613. s->block_index[4]++;
  1614. s->block_index[5]++;
  1615. /* write gob / video packet header for formats which support it at any MB (MPEG4) */
  1616. if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
  1617. int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
  1618. //the *2 is there so we stay below the requested size
  1619. if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){
  1620. if(s->codec_id==CODEC_ID_MPEG4){
  1621. if(s->data_partitioning && s->pict_type!=B_TYPE){
  1622. ff_mpeg4_merge_partitions(s);
  1623. ff_mpeg4_init_partitions(s);
  1624. }
  1625. ff_mpeg4_encode_video_packet_header(s);
  1626. if(s->flags&CODEC_FLAG_PASS1){
  1627. int bits= get_bit_count(&s->pb);
  1628. s->misc_bits+= bits - s->last_bits;
  1629. s->last_bits= bits;
  1630. }
  1631. ff_mpeg4_clean_buffers(s);
  1632. }
  1633. s->ptr_lastgob = pbBufPtr(&s->pb);
  1634. s->first_slice_line=1;
  1635. s->resync_mb_x=mb_x;
  1636. s->resync_mb_y=mb_y;
  1637. }
  1638. if( (s->resync_mb_x == s->mb_x)
  1639. && s->resync_mb_y+1 == s->mb_y){
  1640. s->first_slice_line=0;
  1641. }
  1642. }
  1643. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  1644. int next_block=0;
  1645. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  1646. copy_context_before_encode(&backup_s, s, -1);
  1647. backup_s.pb= s->pb;
  1648. best_s.data_partitioning= s->data_partitioning;
  1649. if(s->data_partitioning){
  1650. backup_s.pb2= s->pb2;
  1651. backup_s.tex_pb= s->tex_pb;
  1652. }
  1653. if(mb_type&MB_TYPE_INTER){
  1654. s->mv_dir = MV_DIR_FORWARD;
  1655. s->mv_type = MV_TYPE_16X16;
  1656. s->mb_intra= 0;
  1657. s->mv[0][0][0] = s->p_mv_table[xy][0];
  1658. s->mv[0][0][1] = s->p_mv_table[xy][1];
  1659. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  1660. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1661. }
  1662. if(mb_type&MB_TYPE_INTER4V){
  1663. s->mv_dir = MV_DIR_FORWARD;
  1664. s->mv_type = MV_TYPE_8X8;
  1665. s->mb_intra= 0;
  1666. for(i=0; i<4; i++){
  1667. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1668. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1669. }
  1670. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  1671. &dmin, &next_block, 0, 0);
  1672. }
  1673. if(mb_type&MB_TYPE_FORWARD){
  1674. s->mv_dir = MV_DIR_FORWARD;
  1675. s->mv_type = MV_TYPE_16X16;
  1676. s->mb_intra= 0;
  1677. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1678. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1679. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  1680. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1681. }
  1682. if(mb_type&MB_TYPE_BACKWARD){
  1683. s->mv_dir = MV_DIR_BACKWARD;
  1684. s->mv_type = MV_TYPE_16X16;
  1685. s->mb_intra= 0;
  1686. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1687. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1688. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  1689. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  1690. }
  1691. if(mb_type&MB_TYPE_BIDIR){
  1692. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1693. s->mv_type = MV_TYPE_16X16;
  1694. s->mb_intra= 0;
  1695. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1696. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1697. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1698. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1699. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  1700. &dmin, &next_block, 0, 0);
  1701. }
  1702. if(mb_type&MB_TYPE_DIRECT){
  1703. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1704. s->mv_type = MV_TYPE_16X16; //FIXME
  1705. s->mb_intra= 0;
  1706. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1707. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1708. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1709. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1710. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  1711. &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
  1712. }
  1713. if(mb_type&MB_TYPE_INTRA){
  1714. s->mv_dir = MV_DIR_FORWARD;
  1715. s->mv_type = MV_TYPE_16X16;
  1716. s->mb_intra= 1;
  1717. s->mv[0][0][0] = 0;
  1718. s->mv[0][0][1] = 0;
  1719. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  1720. &dmin, &next_block, 0, 0);
  1721. /* force cleaning of ac/dc pred stuff if needed ... */
  1722. if(s->h263_pred || s->h263_aic)
  1723. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1724. }
  1725. copy_context_after_encode(s, &best_s, -1);
  1726. pb_bits_count= get_bit_count(&s->pb);
  1727. flush_put_bits(&s->pb);
  1728. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  1729. s->pb= backup_s.pb;
  1730. if(s->data_partitioning){
  1731. pb2_bits_count= get_bit_count(&s->pb2);
  1732. flush_put_bits(&s->pb2);
  1733. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  1734. s->pb2= backup_s.pb2;
  1735. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  1736. flush_put_bits(&s->tex_pb);
  1737. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  1738. s->tex_pb= backup_s.tex_pb;
  1739. }
  1740. s->last_bits= get_bit_count(&s->pb);
  1741. } else {
  1742. int motion_x, motion_y;
  1743. s->mv_type=MV_TYPE_16X16;
  1744. // only one MB-Type possible
  1745. switch(mb_type){
  1746. case MB_TYPE_INTRA:
  1747. s->mv_dir = MV_DIR_FORWARD;
  1748. s->mb_intra= 1;
  1749. motion_x= s->mv[0][0][0] = 0;
  1750. motion_y= s->mv[0][0][1] = 0;
  1751. break;
  1752. case MB_TYPE_INTER:
  1753. s->mv_dir = MV_DIR_FORWARD;
  1754. s->mb_intra= 0;
  1755. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  1756. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  1757. break;
  1758. case MB_TYPE_INTER4V:
  1759. s->mv_dir = MV_DIR_FORWARD;
  1760. s->mv_type = MV_TYPE_8X8;
  1761. s->mb_intra= 0;
  1762. for(i=0; i<4; i++){
  1763. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1764. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1765. }
  1766. motion_x= motion_y= 0;
  1767. break;
  1768. case MB_TYPE_DIRECT:
  1769. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1770. s->mb_intra= 0;
  1771. motion_x=s->b_direct_mv_table[xy][0];
  1772. motion_y=s->b_direct_mv_table[xy][1];
  1773. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1774. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1775. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1776. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1777. break;
  1778. case MB_TYPE_BIDIR:
  1779. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1780. s->mb_intra= 0;
  1781. motion_x=0;
  1782. motion_y=0;
  1783. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1784. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1785. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1786. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1787. break;
  1788. case MB_TYPE_BACKWARD:
  1789. s->mv_dir = MV_DIR_BACKWARD;
  1790. s->mb_intra= 0;
  1791. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1792. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1793. break;
  1794. case MB_TYPE_FORWARD:
  1795. s->mv_dir = MV_DIR_FORWARD;
  1796. s->mb_intra= 0;
  1797. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1798. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1799. // printf(" %d %d ", motion_x, motion_y);
  1800. break;
  1801. default:
  1802. motion_x=motion_y=0; //gcc warning fix
  1803. printf("illegal MB type\n");
  1804. }
  1805. encode_mb(s, motion_x, motion_y);
  1806. }
  1807. /* clean the MV table in IPS frames for direct mode in B frames */
  1808. if(s->mb_intra /* && I,P,S_TYPE */){
  1809. s->p_mv_table[xy][0]=0;
  1810. s->p_mv_table[xy][1]=0;
  1811. }
  1812. MPV_decode_mb(s, s->block);
  1813. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  1814. }
  1815. /* Obtain average GOB size for RTP */
  1816. if (s->rtp_mode) {
  1817. if (!mb_y)
  1818. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  1819. else if (!(mb_y % s->gob_index)) {
  1820. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  1821. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  1822. }
  1823. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  1824. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  1825. if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
  1826. }
  1827. }
  1828. emms_c();
  1829. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  1830. ff_mpeg4_merge_partitions(s);
  1831. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  1832. msmpeg4_encode_ext_header(s);
  1833. if(s->codec_id==CODEC_ID_MPEG4)
  1834. ff_mpeg4_stuffing(&s->pb);
  1835. //if (s->gob_number)
  1836. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  1837. /* Send the last GOB if RTP */
  1838. if (s->rtp_mode) {
  1839. flush_put_bits(&s->pb);
  1840. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1841. /* Call the RTP callback to send the last GOB */
  1842. if (s->rtp_callback)
  1843. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  1844. s->ptr_lastgob = pbBufPtr(&s->pb);
  1845. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  1846. }
  1847. }
  1848. static int dct_quantize_c(MpegEncContext *s,
  1849. DCTELEM *block, int n,
  1850. int qscale, int *overflow)
  1851. {
  1852. int i, j, level, last_non_zero, q;
  1853. const int *qmat;
  1854. int bias;
  1855. int max=0;
  1856. unsigned int threshold1, threshold2;
  1857. av_fdct (block);
  1858. /* we need this permutation so that we correct the IDCT
  1859. permutation. will be moved into DCT code */
  1860. block_permute(block);
  1861. if (s->mb_intra) {
  1862. if (!s->h263_aic) {
  1863. if (n < 4)
  1864. q = s->y_dc_scale;
  1865. else
  1866. q = s->c_dc_scale;
  1867. q = q << 3;
  1868. } else
  1869. /* For AIC we skip quant/dequant of INTRADC */
  1870. q = 1 << 3;
  1871. /* note: block[0] is assumed to be positive */
  1872. block[0] = (block[0] + (q >> 1)) / q;
  1873. i = 1;
  1874. last_non_zero = 0;
  1875. qmat = s->q_intra_matrix[qscale];
  1876. bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  1877. } else {
  1878. i = 0;
  1879. last_non_zero = -1;
  1880. qmat = s->q_inter_matrix[qscale];
  1881. bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  1882. }
  1883. threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
  1884. threshold2= threshold1<<1;
  1885. for(;i<64;i++) {
  1886. j = zigzag_direct[i];
  1887. level = block[j];
  1888. level = level * qmat[j];
  1889. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1890. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1891. if(((unsigned)(level+threshold1))>threshold2){
  1892. if(level>0){
  1893. level= (bias + level)>>(QMAT_SHIFT - 3);
  1894. block[j]= level;
  1895. }else{
  1896. level= (bias - level)>>(QMAT_SHIFT - 3);
  1897. block[j]= -level;
  1898. }
  1899. max |=level;
  1900. last_non_zero = i;
  1901. }else{
  1902. block[j]=0;
  1903. }
  1904. }
  1905. *overflow= s->max_qcoeff < max; //overflow might have happend
  1906. return last_non_zero;
  1907. }
  1908. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1909. DCTELEM *block, int n, int qscale)
  1910. {
  1911. int i, level, nCoeffs;
  1912. const UINT16 *quant_matrix;
  1913. if(s->alternate_scan) nCoeffs= 64;
  1914. else nCoeffs= s->block_last_index[n]+1;
  1915. if (s->mb_intra) {
  1916. if (n < 4)
  1917. block[0] = block[0] * s->y_dc_scale;
  1918. else
  1919. block[0] = block[0] * s->c_dc_scale;
  1920. /* XXX: only mpeg1 */
  1921. quant_matrix = s->intra_matrix;
  1922. for(i=1;i<nCoeffs;i++) {
  1923. int j= zigzag_direct[i];
  1924. level = block[j];
  1925. if (level) {
  1926. if (level < 0) {
  1927. level = -level;
  1928. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1929. level = (level - 1) | 1;
  1930. level = -level;
  1931. } else {
  1932. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1933. level = (level - 1) | 1;
  1934. }
  1935. #ifdef PARANOID
  1936. if (level < -2048 || level > 2047)
  1937. fprintf(stderr, "unquant error %d %d\n", i, level);
  1938. #endif
  1939. block[j] = level;
  1940. }
  1941. }
  1942. } else {
  1943. i = 0;
  1944. quant_matrix = s->inter_matrix;
  1945. for(;i<nCoeffs;i++) {
  1946. int j= zigzag_direct[i];
  1947. level = block[j];
  1948. if (level) {
  1949. if (level < 0) {
  1950. level = -level;
  1951. level = (((level << 1) + 1) * qscale *
  1952. ((int) (quant_matrix[j]))) >> 4;
  1953. level = (level - 1) | 1;
  1954. level = -level;
  1955. } else {
  1956. level = (((level << 1) + 1) * qscale *
  1957. ((int) (quant_matrix[j]))) >> 4;
  1958. level = (level - 1) | 1;
  1959. }
  1960. #ifdef PARANOID
  1961. if (level < -2048 || level > 2047)
  1962. fprintf(stderr, "unquant error %d %d\n", i, level);
  1963. #endif
  1964. block[j] = level;
  1965. }
  1966. }
  1967. }
  1968. }
  1969. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  1970. DCTELEM *block, int n, int qscale)
  1971. {
  1972. int i, level, nCoeffs;
  1973. const UINT16 *quant_matrix;
  1974. if(s->alternate_scan) nCoeffs= 64;
  1975. else nCoeffs= s->block_last_index[n]+1;
  1976. if (s->mb_intra) {
  1977. if (n < 4)
  1978. block[0] = block[0] * s->y_dc_scale;
  1979. else
  1980. block[0] = block[0] * s->c_dc_scale;
  1981. quant_matrix = s->intra_matrix;
  1982. for(i=1;i<nCoeffs;i++) {
  1983. int j= zigzag_direct[i];
  1984. level = block[j];
  1985. if (level) {
  1986. if (level < 0) {
  1987. level = -level;
  1988. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1989. level = -level;
  1990. } else {
  1991. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1992. }
  1993. #ifdef PARANOID
  1994. if (level < -2048 || level > 2047)
  1995. fprintf(stderr, "unquant error %d %d\n", i, level);
  1996. #endif
  1997. block[j] = level;
  1998. }
  1999. }
  2000. } else {
  2001. int sum=-1;
  2002. i = 0;
  2003. quant_matrix = s->inter_matrix;
  2004. for(;i<nCoeffs;i++) {
  2005. int j= zigzag_direct[i];
  2006. level = block[j];
  2007. if (level) {
  2008. if (level < 0) {
  2009. level = -level;
  2010. level = (((level << 1) + 1) * qscale *
  2011. ((int) (quant_matrix[j]))) >> 4;
  2012. level = -level;
  2013. } else {
  2014. level = (((level << 1) + 1) * qscale *
  2015. ((int) (quant_matrix[j]))) >> 4;
  2016. }
  2017. #ifdef PARANOID
  2018. if (level < -2048 || level > 2047)
  2019. fprintf(stderr, "unquant error %d %d\n", i, level);
  2020. #endif
  2021. block[j] = level;
  2022. sum+=level;
  2023. }
  2024. }
  2025. block[63]^=sum&1;
  2026. }
  2027. }
  2028. static void dct_unquantize_h263_c(MpegEncContext *s,
  2029. DCTELEM *block, int n, int qscale)
  2030. {
  2031. int i, level, qmul, qadd;
  2032. int nCoeffs;
  2033. if (s->mb_intra) {
  2034. if (!s->h263_aic) {
  2035. if (n < 4)
  2036. block[0] = block[0] * s->y_dc_scale;
  2037. else
  2038. block[0] = block[0] * s->c_dc_scale;
  2039. }
  2040. i = 1;
  2041. nCoeffs= 64; //does not allways use zigzag table
  2042. } else {
  2043. i = 0;
  2044. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  2045. }
  2046. qmul = s->qscale << 1;
  2047. if (s->h263_aic && s->mb_intra)
  2048. qadd = 0;
  2049. else
  2050. qadd = (s->qscale - 1) | 1;
  2051. for(;i<nCoeffs;i++) {
  2052. level = block[i];
  2053. if (level) {
  2054. if (level < 0) {
  2055. level = level * qmul - qadd;
  2056. } else {
  2057. level = level * qmul + qadd;
  2058. }
  2059. #ifdef PARANOID
  2060. if (level < -2048 || level > 2047)
  2061. fprintf(stderr, "unquant error %d %d\n", i, level);
  2062. #endif
  2063. block[i] = level;
  2064. }
  2065. }
  2066. }
  2067. static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
  2068. {
  2069. int dc, dcb, dcr, y, i;
  2070. for(i=0; i<4; i++){
  2071. dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
  2072. for(y=0; y<8; y++){
  2073. int x;
  2074. for(x=0; x<8; x++){
  2075. dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
  2076. }
  2077. }
  2078. }
  2079. dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2080. dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2081. for(y=0; y<8; y++){
  2082. int x;
  2083. for(x=0; x<8; x++){
  2084. dest_cb[x + y*(s->linesize>>1)]= dcb/8;
  2085. dest_cr[x + y*(s->linesize>>1)]= dcr/8;
  2086. }
  2087. }
  2088. }
  2089. /**
  2090. * will conceal past errors, and allso drop b frames if needed
  2091. *
  2092. */
  2093. void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
  2094. {
  2095. int mb_x= s->mb_x;
  2096. int mb_y= s->mb_y;
  2097. int mb_dist=0;
  2098. int i, intra_count=0, inter_count=0;
  2099. int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
  2100. int inter_conceal= s->msmpeg4_version ? 50 : 50;
  2101. // for last block
  2102. if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
  2103. if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
  2104. if(s->decoding_error==0 && unknown_pos){
  2105. if(s->data_partitioning && s->pict_type!=B_TYPE)
  2106. s->decoding_error= DECODING_AC_LOST;
  2107. else
  2108. s->decoding_error= DECODING_DESYNC;
  2109. }
  2110. if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
  2111. for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
  2112. if(s->mbintra_table[i]) intra_count++;
  2113. else inter_count++;
  2114. }
  2115. if(s->decoding_error==DECODING_AC_LOST){
  2116. intra_conceal*=2;
  2117. inter_conceal*=2;
  2118. }else if(s->decoding_error==DECODING_ACDC_LOST){
  2119. intra_conceal*=2;
  2120. inter_conceal*=2;
  2121. }
  2122. if(unknown_pos && (intra_count<inter_count)){
  2123. intra_conceal= inter_conceal= s->mb_num;
  2124. // printf("%d %d\n",intra_count, inter_count);
  2125. }
  2126. fprintf(stderr, "concealing errors\n");
  2127. /* for all MBs from the current one back until the last resync marker */
  2128. for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
  2129. for(; mb_x>=0; mb_x--){
  2130. uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2131. uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  2132. uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  2133. int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
  2134. int mb_y_backup= s->mb_y;
  2135. s->mb_x=mb_x;
  2136. s->mb_y=mb_y;
  2137. if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
  2138. if(s->decoding_error==DECODING_AC_LOST){
  2139. remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  2140. // printf("remove ac to %d %d\n", mb_x, mb_y);
  2141. }else{
  2142. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2143. s->last_picture, 0, 0, put_pixels_tab,
  2144. 0/*mx*/, 0/*my*/, 16);
  2145. }
  2146. }
  2147. else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
  2148. int mx=0;
  2149. int my=0;
  2150. if(s->decoding_error!=DECODING_DESYNC){
  2151. int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
  2152. mx= s->motion_val[ xy ][0];
  2153. my= s->motion_val[ xy ][1];
  2154. }
  2155. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2156. s->last_picture, 0, 0, put_pixels_tab,
  2157. mx, my, 16);
  2158. }
  2159. s->mb_x= mb_x_backup;
  2160. s->mb_y= mb_y_backup;
  2161. if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
  2162. if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
  2163. }
  2164. mb_x=s->mb_width-1;
  2165. }
  2166. }
  2167. AVCodec mpeg1video_encoder = {
  2168. "mpeg1video",
  2169. CODEC_TYPE_VIDEO,
  2170. CODEC_ID_MPEG1VIDEO,
  2171. sizeof(MpegEncContext),
  2172. MPV_encode_init,
  2173. MPV_encode_picture,
  2174. MPV_encode_end,
  2175. };
  2176. AVCodec h263_encoder = {
  2177. "h263",
  2178. CODEC_TYPE_VIDEO,
  2179. CODEC_ID_H263,
  2180. sizeof(MpegEncContext),
  2181. MPV_encode_init,
  2182. MPV_encode_picture,
  2183. MPV_encode_end,
  2184. };
  2185. AVCodec h263p_encoder = {
  2186. "h263p",
  2187. CODEC_TYPE_VIDEO,
  2188. CODEC_ID_H263P,
  2189. sizeof(MpegEncContext),
  2190. MPV_encode_init,
  2191. MPV_encode_picture,
  2192. MPV_encode_end,
  2193. };
  2194. AVCodec rv10_encoder = {
  2195. "rv10",
  2196. CODEC_TYPE_VIDEO,
  2197. CODEC_ID_RV10,
  2198. sizeof(MpegEncContext),
  2199. MPV_encode_init,
  2200. MPV_encode_picture,
  2201. MPV_encode_end,
  2202. };
  2203. AVCodec mjpeg_encoder = {
  2204. "mjpeg",
  2205. CODEC_TYPE_VIDEO,
  2206. CODEC_ID_MJPEG,
  2207. sizeof(MpegEncContext),
  2208. MPV_encode_init,
  2209. MPV_encode_picture,
  2210. MPV_encode_end,
  2211. };
  2212. AVCodec mpeg4_encoder = {
  2213. "mpeg4",
  2214. CODEC_TYPE_VIDEO,
  2215. CODEC_ID_MPEG4,
  2216. sizeof(MpegEncContext),
  2217. MPV_encode_init,
  2218. MPV_encode_picture,
  2219. MPV_encode_end,
  2220. };
  2221. AVCodec msmpeg4v1_encoder = {
  2222. "msmpeg4v1",
  2223. CODEC_TYPE_VIDEO,
  2224. CODEC_ID_MSMPEG4V1,
  2225. sizeof(MpegEncContext),
  2226. MPV_encode_init,
  2227. MPV_encode_picture,
  2228. MPV_encode_end,
  2229. };
  2230. AVCodec msmpeg4v2_encoder = {
  2231. "msmpeg4v2",
  2232. CODEC_TYPE_VIDEO,
  2233. CODEC_ID_MSMPEG4V2,
  2234. sizeof(MpegEncContext),
  2235. MPV_encode_init,
  2236. MPV_encode_picture,
  2237. MPV_encode_end,
  2238. };
  2239. AVCodec msmpeg4v3_encoder = {
  2240. "msmpeg4",
  2241. CODEC_TYPE_VIDEO,
  2242. CODEC_ID_MSMPEG4V3,
  2243. sizeof(MpegEncContext),
  2244. MPV_encode_init,
  2245. MPV_encode_picture,
  2246. MPV_encode_end,
  2247. };