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.

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