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.

2590 lines
89KB

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