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.

2612 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. case CODEC_ID_WMV1:
  401. s->out_format = FMT_H263;
  402. s->h263_msmpeg4 = 1;
  403. s->h263_pred = 1;
  404. s->unrestricted_mv = 1;
  405. s->msmpeg4_version= 4;
  406. avctx->delay=0;
  407. break;
  408. case CODEC_ID_WMV2:
  409. s->out_format = FMT_H263;
  410. s->h263_msmpeg4 = 1;
  411. s->h263_pred = 1;
  412. s->unrestricted_mv = 1;
  413. s->msmpeg4_version= 5;
  414. avctx->delay=0;
  415. break;
  416. default:
  417. return -1;
  418. }
  419. { /* set up some save defaults, some codecs might override them later */
  420. static int done=0;
  421. if(!done){
  422. int i;
  423. done=1;
  424. memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
  425. memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
  426. for(i=-16; i<16; i++){
  427. default_fcode_tab[i + MAX_MV]= 1;
  428. }
  429. }
  430. }
  431. s->mv_penalty= default_mv_penalty;
  432. s->fcode_tab= default_fcode_tab;
  433. if (s->out_format == FMT_H263)
  434. h263_encode_init(s);
  435. else if (s->out_format == FMT_MPEG1)
  436. ff_mpeg1_encode_init(s);
  437. if(s->msmpeg4_version)
  438. ff_msmpeg4_encode_init(s);
  439. /* dont use mv_penalty table for crap MV as it would be confused */
  440. if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
  441. s->encoding = 1;
  442. /* init */
  443. if (MPV_common_init(s) < 0)
  444. return -1;
  445. /* init default q matrix */
  446. for(i=0;i<64;i++) {
  447. if(s->out_format == FMT_H263)
  448. s->intra_matrix[i] = default_non_intra_matrix[i];
  449. else
  450. s->intra_matrix[i] = default_intra_matrix[i];
  451. s->inter_matrix[i] = default_non_intra_matrix[i];
  452. }
  453. /* precompute matrix */
  454. /* for mjpeg, we do include qscale in the matrix */
  455. if (s->out_format != FMT_MJPEG) {
  456. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
  457. s->intra_matrix, s->intra_quant_bias);
  458. convert_matrix(s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
  459. s->inter_matrix, s->inter_quant_bias);
  460. }
  461. if(ff_rate_control_init(s) < 0)
  462. return -1;
  463. s->picture_number = 0;
  464. s->picture_in_gop_number = 0;
  465. s->fake_picture_number = 0;
  466. /* motion detector init */
  467. s->f_code = 1;
  468. s->b_code = 1;
  469. return 0;
  470. }
  471. int MPV_encode_end(AVCodecContext *avctx)
  472. {
  473. MpegEncContext *s = avctx->priv_data;
  474. #ifdef STATS
  475. print_stats();
  476. #endif
  477. ff_rate_control_uninit(s);
  478. MPV_common_end(s);
  479. if (s->out_format == FMT_MJPEG)
  480. mjpeg_close(s);
  481. return 0;
  482. }
  483. /* draw the edges of width 'w' of an image of size width, height */
  484. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
  485. {
  486. UINT8 *ptr, *last_line;
  487. int i;
  488. last_line = buf + (height - 1) * wrap;
  489. for(i=0;i<w;i++) {
  490. /* top and bottom */
  491. memcpy(buf - (i + 1) * wrap, buf, width);
  492. memcpy(last_line + (i + 1) * wrap, last_line, width);
  493. }
  494. /* left and right */
  495. ptr = buf;
  496. for(i=0;i<height;i++) {
  497. memset(ptr - w, ptr[0], w);
  498. memset(ptr + width, ptr[width-1], w);
  499. ptr += wrap;
  500. }
  501. /* corners */
  502. for(i=0;i<w;i++) {
  503. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  504. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  505. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  506. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  507. }
  508. }
  509. /* generic function for encode/decode called before a frame is coded/decoded */
  510. void MPV_frame_start(MpegEncContext *s)
  511. {
  512. int i;
  513. UINT8 *tmp;
  514. s->mb_skiped = 0;
  515. s->decoding_error=0;
  516. if (s->pict_type == B_TYPE) {
  517. for(i=0;i<3;i++) {
  518. s->current_picture[i] = s->aux_picture[i];
  519. }
  520. } else {
  521. for(i=0;i<3;i++) {
  522. /* swap next and last */
  523. tmp = s->last_picture[i];
  524. s->last_picture[i] = s->next_picture[i];
  525. s->next_picture[i] = tmp;
  526. s->current_picture[i] = tmp;
  527. }
  528. }
  529. }
  530. /* generic function for encode/decode called after a frame has been coded/decoded */
  531. void MPV_frame_end(MpegEncContext *s)
  532. {
  533. // if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads);
  534. /* draw edge for correct motion prediction if outside */
  535. if (s->pict_type != B_TYPE && !s->intra_only) {
  536. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version>=500){
  537. draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
  538. draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  539. draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  540. }else{
  541. /* mpeg4? / opendivx / xvid */
  542. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  543. draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  544. draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  545. }
  546. }
  547. emms_c();
  548. if(s->pict_type!=B_TYPE){
  549. s->last_non_b_pict_type= s->pict_type;
  550. s->last_non_b_qscale= s->qscale;
  551. s->last_non_b_mc_mb_var= s->mc_mb_var_sum;
  552. s->num_available_buffers++;
  553. if(s->num_available_buffers>2) s->num_available_buffers= 2;
  554. }
  555. }
  556. /* reorder input for encoding */
  557. void reorder_input(MpegEncContext *s, AVPicture *pict)
  558. {
  559. int i, j, index;
  560. if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
  561. // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
  562. for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
  563. s->coded_order[j]= s->coded_order[j+1];
  564. }
  565. s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
  566. s->coded_order[j].pict_type=0;
  567. switch(s->input_pict_type){
  568. default:
  569. case I_TYPE:
  570. case S_TYPE:
  571. case P_TYPE:
  572. index= s->max_b_frames - s->b_frames_since_non_b;
  573. s->b_frames_since_non_b=0;
  574. break;
  575. case B_TYPE:
  576. index= s->max_b_frames + 1;
  577. s->b_frames_since_non_b++;
  578. break;
  579. }
  580. //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
  581. if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
  582. && pict->linesize[0] == s->linesize
  583. && pict->linesize[1] == s->linesize>>1
  584. && pict->linesize[2] == s->linesize>>1){
  585. //printf("ptr\n");
  586. for(i=0; i<3; i++){
  587. s->coded_order[index].picture[i]= pict->data[i];
  588. }
  589. }else{
  590. //printf("copy\n");
  591. for(i=0; i<3; i++){
  592. uint8_t *src = pict->data[i];
  593. uint8_t *dest;
  594. int src_wrap = pict->linesize[i];
  595. int dest_wrap = s->linesize;
  596. int w = s->width;
  597. int h = s->height;
  598. if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
  599. else dest= s->picture_buffer[s->picture_buffer_index][i];
  600. if (i >= 1) {
  601. dest_wrap >>= 1;
  602. w >>= 1;
  603. h >>= 1;
  604. }
  605. s->coded_order[index].picture[i]= dest;
  606. for(j=0;j<h;j++) {
  607. memcpy(dest, src, w);
  608. dest += dest_wrap;
  609. src += src_wrap;
  610. }
  611. }
  612. if(index!=0){
  613. s->picture_buffer_index++;
  614. if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
  615. }
  616. }
  617. s->coded_order[index].pict_type = s->input_pict_type;
  618. s->coded_order[index].qscale = s->input_qscale;
  619. s->coded_order[index].force_type= s->force_input_type;
  620. s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
  621. s->coded_order[index].picture_number= s->input_picture_number;
  622. for(i=0; i<3; i++){
  623. s->new_picture[i]= s->coded_order[0].picture[i];
  624. }
  625. }
  626. int MPV_encode_picture(AVCodecContext *avctx,
  627. unsigned char *buf, int buf_size, void *data)
  628. {
  629. MpegEncContext *s = avctx->priv_data;
  630. AVPicture *pict = data;
  631. s->input_qscale = avctx->quality;
  632. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  633. if(avctx->flags&CODEC_FLAG_TYPE){
  634. s->input_pict_type=
  635. s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
  636. }else if(s->flags&CODEC_FLAG_PASS2){
  637. s->input_pict_type=
  638. s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
  639. }else{
  640. s->force_input_type=0;
  641. if (!s->intra_only) {
  642. /* first picture of GOP is intra */
  643. if (s->input_picture_in_gop_number % s->gop_size==0){
  644. s->input_pict_type = I_TYPE;
  645. }else if(s->max_b_frames==0){
  646. s->input_pict_type = P_TYPE;
  647. }else{
  648. if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
  649. s->input_pict_type = B_TYPE;
  650. else
  651. s->input_pict_type = P_TYPE;
  652. }
  653. } else {
  654. s->input_pict_type = I_TYPE;
  655. }
  656. }
  657. if(s->input_pict_type==I_TYPE)
  658. s->input_picture_in_gop_number=0;
  659. reorder_input(s, pict);
  660. /* output? */
  661. if(s->coded_order[0].picture[0]){
  662. s->pict_type= s->coded_order[0].pict_type;
  663. if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  664. s->qscale= s->coded_order[0].qscale;
  665. s->force_type= s->coded_order[0].force_type;
  666. s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
  667. s->picture_number= s->coded_order[0].picture_number;
  668. MPV_frame_start(s);
  669. encode_picture(s, s->picture_number);
  670. avctx->key_frame = (s->pict_type == I_TYPE);
  671. avctx->pict_type = s->pict_type;
  672. avctx->real_pict_num = s->picture_number;
  673. avctx->header_bits = s->header_bits;
  674. avctx->mv_bits = s->mv_bits;
  675. avctx->misc_bits = s->misc_bits;
  676. avctx->i_tex_bits = s->i_tex_bits;
  677. avctx->p_tex_bits = s->p_tex_bits;
  678. avctx->i_count = s->i_count;
  679. avctx->p_count = s->p_count;
  680. avctx->skip_count = s->skip_count;
  681. MPV_frame_end(s);
  682. if (s->out_format == FMT_MJPEG)
  683. mjpeg_picture_trailer(s);
  684. avctx->quality = s->qscale;
  685. if(s->flags&CODEC_FLAG_PASS1)
  686. ff_write_pass1_stats(s);
  687. }
  688. s->input_picture_number++;
  689. s->input_picture_in_gop_number++;
  690. flush_put_bits(&s->pb);
  691. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  692. if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
  693. else s->pb_frame_bits= s->frame_bits;
  694. s->total_bits += s->frame_bits;
  695. avctx->frame_bits = s->frame_bits;
  696. //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
  697. //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);
  698. if (avctx->get_psnr) {
  699. /* At this point pict->data should have the original frame */
  700. /* an s->current_picture should have the coded/decoded frame */
  701. get_psnr(pict->data, s->current_picture,
  702. pict->linesize, s->linesize, avctx);
  703. // printf("%f\n", avctx->psnr_y);
  704. }
  705. return pbBufPtr(&s->pb) - s->pb.buf;
  706. }
  707. static inline void gmc1_motion(MpegEncContext *s,
  708. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  709. int dest_offset,
  710. UINT8 **ref_picture, int src_offset,
  711. int h)
  712. {
  713. UINT8 *ptr;
  714. int offset, src_x, src_y, linesize;
  715. int motion_x, motion_y;
  716. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  717. motion_x= s->sprite_offset[0][0];
  718. motion_y= s->sprite_offset[0][1];
  719. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  720. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  721. motion_x<<=(3-s->sprite_warping_accuracy);
  722. motion_y<<=(3-s->sprite_warping_accuracy);
  723. src_x = clip(src_x, -16, s->width);
  724. if (src_x == s->width)
  725. motion_x =0;
  726. src_y = clip(src_y, -16, s->height);
  727. if (src_y == s->height)
  728. motion_y =0;
  729. linesize = s->linesize;
  730. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  731. dest_y+=dest_offset;
  732. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  733. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  734. motion_x= s->sprite_offset[1][0];
  735. motion_y= s->sprite_offset[1][1];
  736. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  737. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  738. motion_x<<=(3-s->sprite_warping_accuracy);
  739. motion_y<<=(3-s->sprite_warping_accuracy);
  740. src_x = clip(src_x, -8, s->width>>1);
  741. if (src_x == s->width>>1)
  742. motion_x =0;
  743. src_y = clip(src_y, -8, s->height>>1);
  744. if (src_y == s->height>>1)
  745. motion_y =0;
  746. offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
  747. ptr = ref_picture[1] + offset;
  748. gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  749. ptr = ref_picture[2] + offset;
  750. gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  751. return;
  752. }
  753. /* apply one mpeg motion vector to the three components */
  754. static inline void mpeg_motion(MpegEncContext *s,
  755. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  756. int dest_offset,
  757. UINT8 **ref_picture, int src_offset,
  758. int field_based, op_pixels_func *pix_op,
  759. int motion_x, int motion_y, int h)
  760. {
  761. UINT8 *ptr;
  762. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  763. if(s->quarter_sample)
  764. {
  765. motion_x>>=1;
  766. motion_y>>=1;
  767. }
  768. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  769. src_x = s->mb_x * 16 + (motion_x >> 1);
  770. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  771. /* WARNING: do no forget half pels */
  772. height = s->height >> field_based;
  773. src_x = clip(src_x, -16, s->width);
  774. if (src_x == s->width)
  775. dxy &= ~1;
  776. src_y = clip(src_y, -16, height);
  777. if (src_y == height)
  778. dxy &= ~2;
  779. linesize = s->linesize << field_based;
  780. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  781. dest_y += dest_offset;
  782. pix_op[dxy](dest_y, ptr, linesize, h);
  783. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  784. if(s->flags&CODEC_FLAG_GRAY) return;
  785. if (s->out_format == FMT_H263) {
  786. dxy = 0;
  787. if ((motion_x & 3) != 0)
  788. dxy |= 1;
  789. if ((motion_y & 3) != 0)
  790. dxy |= 2;
  791. mx = motion_x >> 2;
  792. my = motion_y >> 2;
  793. } else {
  794. mx = motion_x / 2;
  795. my = motion_y / 2;
  796. dxy = ((my & 1) << 1) | (mx & 1);
  797. mx >>= 1;
  798. my >>= 1;
  799. }
  800. src_x = s->mb_x * 8 + mx;
  801. src_y = s->mb_y * (8 >> field_based) + my;
  802. src_x = clip(src_x, -8, s->width >> 1);
  803. if (src_x == (s->width >> 1))
  804. dxy &= ~1;
  805. src_y = clip(src_y, -8, height >> 1);
  806. if (src_y == (height >> 1))
  807. dxy &= ~2;
  808. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  809. ptr = ref_picture[1] + offset;
  810. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  811. ptr = ref_picture[2] + offset;
  812. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  813. }
  814. static inline void qpel_motion(MpegEncContext *s,
  815. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  816. int dest_offset,
  817. UINT8 **ref_picture, int src_offset,
  818. int field_based, op_pixels_func *pix_op,
  819. qpel_mc_func *qpix_op,
  820. int motion_x, int motion_y, int h)
  821. {
  822. UINT8 *ptr;
  823. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  824. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  825. src_x = s->mb_x * 16 + (motion_x >> 2);
  826. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  827. height = s->height >> field_based;
  828. src_x = clip(src_x, -16, s->width);
  829. if (src_x == s->width)
  830. dxy &= ~3;
  831. src_y = clip(src_y, -16, height);
  832. if (src_y == height)
  833. dxy &= ~12;
  834. linesize = s->linesize << field_based;
  835. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  836. dest_y += dest_offset;
  837. //printf("%d %d %d\n", src_x, src_y, dxy);
  838. qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
  839. qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
  840. qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
  841. qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
  842. if(s->flags&CODEC_FLAG_GRAY) return;
  843. mx= (motion_x>>1) | (motion_x&1);
  844. my= (motion_y>>1) | (motion_y&1);
  845. dxy = 0;
  846. if ((mx & 3) != 0)
  847. dxy |= 1;
  848. if ((my & 3) != 0)
  849. dxy |= 2;
  850. mx = mx >> 2;
  851. my = my >> 2;
  852. src_x = s->mb_x * 8 + mx;
  853. src_y = s->mb_y * (8 >> field_based) + my;
  854. src_x = clip(src_x, -8, s->width >> 1);
  855. if (src_x == (s->width >> 1))
  856. dxy &= ~1;
  857. src_y = clip(src_y, -8, height >> 1);
  858. if (src_y == (height >> 1))
  859. dxy &= ~2;
  860. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  861. ptr = ref_picture[1] + offset;
  862. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  863. ptr = ref_picture[2] + offset;
  864. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  865. }
  866. static inline void MPV_motion(MpegEncContext *s,
  867. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  868. int dir, UINT8 **ref_picture,
  869. op_pixels_func *pix_op, qpel_mc_func *qpix_op)
  870. {
  871. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  872. int mb_x, mb_y, i;
  873. UINT8 *ptr, *dest;
  874. mb_x = s->mb_x;
  875. mb_y = s->mb_y;
  876. switch(s->mv_type) {
  877. case MV_TYPE_16X16:
  878. if(s->mcsel){
  879. #if 0
  880. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  881. ref_picture, 0,
  882. 0, pix_op,
  883. s->sprite_offset[0][0]>>3,
  884. s->sprite_offset[0][1]>>3,
  885. 16);
  886. #else
  887. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  888. ref_picture, 0,
  889. 16);
  890. #endif
  891. }else if(s->quarter_sample && dir==0){ //FIXME
  892. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  893. ref_picture, 0,
  894. 0, pix_op, qpix_op,
  895. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  896. }else{
  897. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  898. ref_picture, 0,
  899. 0, pix_op,
  900. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  901. }
  902. break;
  903. case MV_TYPE_8X8:
  904. for(i=0;i<4;i++) {
  905. motion_x = s->mv[dir][i][0];
  906. motion_y = s->mv[dir][i][1];
  907. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  908. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  909. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  910. /* WARNING: do no forget half pels */
  911. src_x = clip(src_x, -16, s->width);
  912. if (src_x == s->width)
  913. dxy &= ~1;
  914. src_y = clip(src_y, -16, s->height);
  915. if (src_y == s->height)
  916. dxy &= ~2;
  917. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  918. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  919. pix_op[dxy](dest, ptr, s->linesize, 8);
  920. }
  921. if(s->flags&CODEC_FLAG_GRAY) break;
  922. /* In case of 8X8, we construct a single chroma motion vector
  923. with a special rounding */
  924. mx = 0;
  925. my = 0;
  926. for(i=0;i<4;i++) {
  927. mx += s->mv[dir][i][0];
  928. my += s->mv[dir][i][1];
  929. }
  930. if (mx >= 0)
  931. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  932. else {
  933. mx = -mx;
  934. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  935. }
  936. if (my >= 0)
  937. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  938. else {
  939. my = -my;
  940. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  941. }
  942. dxy = ((my & 1) << 1) | (mx & 1);
  943. mx >>= 1;
  944. my >>= 1;
  945. src_x = mb_x * 8 + mx;
  946. src_y = mb_y * 8 + my;
  947. src_x = clip(src_x, -8, s->width/2);
  948. if (src_x == s->width/2)
  949. dxy &= ~1;
  950. src_y = clip(src_y, -8, s->height/2);
  951. if (src_y == s->height/2)
  952. dxy &= ~2;
  953. offset = (src_y * (s->linesize >> 1)) + src_x;
  954. ptr = ref_picture[1] + offset;
  955. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  956. ptr = ref_picture[2] + offset;
  957. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  958. break;
  959. case MV_TYPE_FIELD:
  960. if (s->picture_structure == PICT_FRAME) {
  961. /* top field */
  962. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  963. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  964. 1, pix_op,
  965. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  966. /* bottom field */
  967. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  968. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  969. 1, pix_op,
  970. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  971. } else {
  972. }
  973. break;
  974. }
  975. }
  976. /* put block[] to dest[] */
  977. static inline void put_dct(MpegEncContext *s,
  978. DCTELEM *block, int i, UINT8 *dest, int line_size)
  979. {
  980. if (!s->mpeg2)
  981. s->dct_unquantize(s, block, i, s->qscale);
  982. ff_idct_put (dest, line_size, block);
  983. }
  984. /* add block[] to dest[] */
  985. static inline void add_dct(MpegEncContext *s,
  986. DCTELEM *block, int i, UINT8 *dest, int line_size)
  987. {
  988. if (s->block_last_index[i] >= 0) {
  989. ff_idct_add (dest, line_size, block);
  990. }
  991. }
  992. static inline void add_dequant_dct(MpegEncContext *s,
  993. DCTELEM *block, int i, UINT8 *dest, int line_size)
  994. {
  995. if (s->block_last_index[i] >= 0) {
  996. s->dct_unquantize(s, block, i, s->qscale);
  997. ff_idct_add (dest, line_size, block);
  998. }
  999. }
  1000. /**
  1001. * cleans dc, ac, coded_block for the current non intra MB
  1002. */
  1003. void ff_clean_intra_table_entries(MpegEncContext *s)
  1004. {
  1005. int wrap = s->block_wrap[0];
  1006. int xy = s->block_index[0];
  1007. s->dc_val[0][xy ] =
  1008. s->dc_val[0][xy + 1 ] =
  1009. s->dc_val[0][xy + wrap] =
  1010. s->dc_val[0][xy + 1 + wrap] = 1024;
  1011. /* ac pred */
  1012. memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16));
  1013. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
  1014. if (s->msmpeg4_version>=3) {
  1015. s->coded_block[xy ] =
  1016. s->coded_block[xy + 1 ] =
  1017. s->coded_block[xy + wrap] =
  1018. s->coded_block[xy + 1 + wrap] = 0;
  1019. }
  1020. /* chroma */
  1021. wrap = s->block_wrap[4];
  1022. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  1023. s->dc_val[1][xy] =
  1024. s->dc_val[2][xy] = 1024;
  1025. /* ac pred */
  1026. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  1027. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  1028. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1029. }
  1030. /* generic function called after a macroblock has been parsed by the
  1031. decoder or after it has been encoded by the encoder.
  1032. Important variables used:
  1033. s->mb_intra : true if intra macroblock
  1034. s->mv_dir : motion vector direction
  1035. s->mv_type : motion vector type
  1036. s->mv : motion vector
  1037. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1038. */
  1039. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1040. {
  1041. int mb_x, mb_y;
  1042. const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
  1043. mb_x = s->mb_x;
  1044. mb_y = s->mb_y;
  1045. #ifdef FF_POSTPROCESS
  1046. /* Obsolete. Exists for compatibility with mplayer only. */
  1047. quant_store[mb_y][mb_x]=s->qscale;
  1048. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  1049. #else
  1050. if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
  1051. #endif
  1052. /* update DC predictors for P macroblocks */
  1053. if (!s->mb_intra) {
  1054. if (s->h263_pred || s->h263_aic) {
  1055. if(s->mbintra_table[mb_xy])
  1056. ff_clean_intra_table_entries(s);
  1057. } else {
  1058. s->last_dc[0] =
  1059. s->last_dc[1] =
  1060. s->last_dc[2] = 128 << s->intra_dc_precision;
  1061. }
  1062. }
  1063. else if (s->h263_pred || s->h263_aic)
  1064. s->mbintra_table[mb_xy]=1;
  1065. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1066. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1067. int motion_x, motion_y;
  1068. const int wrap = s->block_wrap[0];
  1069. const int xy = s->block_index[0];
  1070. if (s->mb_intra) {
  1071. motion_x = 0;
  1072. motion_y = 0;
  1073. goto motion_init;
  1074. } else if (s->mv_type == MV_TYPE_16X16) {
  1075. motion_x = s->mv[0][0][0];
  1076. motion_y = s->mv[0][0][1];
  1077. motion_init:
  1078. /* no update if 8X8 because it has been done during parsing */
  1079. s->motion_val[xy][0] = motion_x;
  1080. s->motion_val[xy][1] = motion_y;
  1081. s->motion_val[xy + 1][0] = motion_x;
  1082. s->motion_val[xy + 1][1] = motion_y;
  1083. s->motion_val[xy + wrap][0] = motion_x;
  1084. s->motion_val[xy + wrap][1] = motion_y;
  1085. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1086. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1087. }
  1088. }
  1089. if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
  1090. UINT8 *dest_y, *dest_cb, *dest_cr;
  1091. int dct_linesize, dct_offset;
  1092. op_pixels_func *op_pix;
  1093. qpel_mc_func *op_qpix;
  1094. /* avoid copy if macroblock skipped in last frame too
  1095. dont touch it for B-frames as they need the skip info from the next p-frame */
  1096. if (s->pict_type != B_TYPE) {
  1097. UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
  1098. if (s->mb_skiped) {
  1099. s->mb_skiped = 0;
  1100. /* if previous was skipped too, then nothing to do !
  1101. skip only during decoding as we might trash the buffers during encoding a bit */
  1102. if (*mbskip_ptr != 0 && !s->encoding)
  1103. goto the_end;
  1104. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  1105. } else {
  1106. *mbskip_ptr = 0; /* not skipped */
  1107. }
  1108. }
  1109. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  1110. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1111. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1112. if (s->interlaced_dct) {
  1113. dct_linesize = s->linesize * 2;
  1114. dct_offset = s->linesize;
  1115. } else {
  1116. dct_linesize = s->linesize;
  1117. dct_offset = s->linesize * 8;
  1118. }
  1119. if (!s->mb_intra) {
  1120. /* motion handling */
  1121. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1122. if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
  1123. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1124. op_pix = put_pixels_tab;
  1125. op_qpix= qpel_mc_rnd_tab;
  1126. }else{
  1127. op_pix = put_no_rnd_pixels_tab;
  1128. op_qpix= qpel_mc_no_rnd_tab;
  1129. }
  1130. if (s->mv_dir & MV_DIR_FORWARD) {
  1131. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1132. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1133. op_pix = avg_pixels_tab;
  1134. else
  1135. op_pix = avg_no_rnd_pixels_tab;
  1136. }
  1137. if (s->mv_dir & MV_DIR_BACKWARD) {
  1138. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1139. }
  1140. }
  1141. /* skip dequant / idct if we are really late ;) */
  1142. if(s->hurry_up>1) goto the_end;
  1143. /* add dct residue */
  1144. if(!s->mpeg2 && (s->encoding || (!s->h263_msmpeg4))){
  1145. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  1146. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1147. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1148. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1149. if(!(s->flags&CODEC_FLAG_GRAY)){
  1150. add_dequant_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1151. add_dequant_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1152. }
  1153. } else {
  1154. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1155. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1156. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1157. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1158. if(!(s->flags&CODEC_FLAG_GRAY)){
  1159. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1160. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1161. }
  1162. }
  1163. } else {
  1164. /* dct only in intra block */
  1165. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1166. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1167. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1168. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1169. if(!(s->flags&CODEC_FLAG_GRAY)){
  1170. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1171. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1172. }
  1173. }
  1174. }
  1175. the_end:
  1176. emms_c(); //FIXME remove
  1177. }
  1178. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc)
  1179. {
  1180. static const char tab[64]=
  1181. {3,2,2,1,1,1,1,1,
  1182. 1,1,1,1,1,1,1,1,
  1183. 1,1,1,1,1,1,1,1,
  1184. 0,0,0,0,0,0,0,0,
  1185. 0,0,0,0,0,0,0,0,
  1186. 0,0,0,0,0,0,0,0,
  1187. 0,0,0,0,0,0,0,0,
  1188. 0,0,0,0,0,0,0,0};
  1189. int score=0;
  1190. int run=0;
  1191. int i;
  1192. DCTELEM *block= s->block[n];
  1193. const int last_index= s->block_last_index[n];
  1194. if(skip_dc) skip_dc=1;
  1195. /* are all which we could set to zero are allready zero? */
  1196. if(last_index<=skip_dc - 1) return;
  1197. for(i=0; i<=last_index; i++){
  1198. const int j = zigzag_direct[i];
  1199. const int level = ABS(block[j]);
  1200. if(level==1){
  1201. if(skip_dc && i==0) continue;
  1202. score+= tab[run];
  1203. run=0;
  1204. }else if(level>1){
  1205. return;
  1206. }else{
  1207. run++;
  1208. }
  1209. }
  1210. if(score >= threshold) return;
  1211. for(i=skip_dc; i<=last_index; i++){
  1212. const int j = zigzag_direct[i];
  1213. block[j]=0;
  1214. }
  1215. if(block[0]) s->block_last_index[n]= 0;
  1216. else s->block_last_index[n]= -1;
  1217. }
  1218. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1219. {
  1220. int i;
  1221. const int maxlevel= s->max_qcoeff;
  1222. const int minlevel= s->min_qcoeff;
  1223. for(i=0;i<=last_index; i++){
  1224. const int j = zigzag_direct[i];
  1225. int level = block[j];
  1226. if (level>maxlevel) level=maxlevel;
  1227. else if(level<minlevel) level=minlevel;
  1228. block[j]= level;
  1229. }
  1230. }
  1231. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1232. {
  1233. const int mb_x= s->mb_x;
  1234. const int mb_y= s->mb_y;
  1235. int i;
  1236. int skip_dct[6];
  1237. #if 0
  1238. if (s->interlaced_dct) {
  1239. dct_linesize = s->linesize * 2;
  1240. dct_offset = s->linesize;
  1241. } else {
  1242. dct_linesize = s->linesize;
  1243. dct_offset = s->linesize * 8;
  1244. }
  1245. #endif
  1246. for(i=0; i<6; i++) skip_dct[i]=0;
  1247. if (s->mb_intra) {
  1248. UINT8 *ptr;
  1249. int wrap;
  1250. wrap = s->linesize;
  1251. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1252. get_pixels(s->block[0], ptr , wrap);
  1253. get_pixels(s->block[1], ptr + 8, wrap);
  1254. get_pixels(s->block[2], ptr + 8 * wrap , wrap);
  1255. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1256. if(s->flags&CODEC_FLAG_GRAY){
  1257. skip_dct[4]= 1;
  1258. skip_dct[5]= 1;
  1259. }else{
  1260. wrap >>=1;
  1261. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1262. get_pixels(s->block[4], ptr, wrap);
  1263. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1264. get_pixels(s->block[5], ptr, wrap);
  1265. }
  1266. }else{
  1267. op_pixels_func *op_pix;
  1268. qpel_mc_func *op_qpix;
  1269. UINT8 *dest_y, *dest_cb, *dest_cr;
  1270. UINT8 *ptr_y, *ptr_cb, *ptr_cr;
  1271. int wrap_y, wrap_c;
  1272. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  1273. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1274. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1275. wrap_y = s->linesize;
  1276. wrap_c = wrap_y>>1;
  1277. ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1278. ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1279. ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1280. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1281. op_pix = put_pixels_tab;
  1282. op_qpix= qpel_mc_rnd_tab;
  1283. }else{
  1284. op_pix = put_no_rnd_pixels_tab;
  1285. op_qpix= qpel_mc_no_rnd_tab;
  1286. }
  1287. if (s->mv_dir & MV_DIR_FORWARD) {
  1288. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1289. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1290. op_pix = avg_pixels_tab;
  1291. else
  1292. op_pix = avg_no_rnd_pixels_tab;
  1293. }
  1294. if (s->mv_dir & MV_DIR_BACKWARD) {
  1295. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1296. }
  1297. diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  1298. diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1299. diff_pixels(s->block[2], ptr_y + 8 * wrap_y , dest_y + 8 * wrap_y , wrap_y);
  1300. diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y);
  1301. if(s->flags&CODEC_FLAG_GRAY){
  1302. skip_dct[4]= 1;
  1303. skip_dct[5]= 1;
  1304. }else{
  1305. diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1306. diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1307. }
  1308. /* pre quantization */
  1309. if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  1310. if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  1311. if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  1312. if(pix_abs8x8(ptr_y + 8*wrap_y , dest_y + 8*wrap_y , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  1313. if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  1314. if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
  1315. if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
  1316. #if 0
  1317. {
  1318. static int stat[7];
  1319. int num=0;
  1320. for(i=0; i<6; i++)
  1321. if(skip_dct[i]) num++;
  1322. stat[num]++;
  1323. if(s->mb_x==0 && s->mb_y==0){
  1324. for(i=0; i<7; i++){
  1325. printf("%6d %1d\n", stat[i], i);
  1326. }
  1327. }
  1328. }
  1329. #endif
  1330. }
  1331. }
  1332. #if 0
  1333. {
  1334. float adap_parm;
  1335. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1336. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1337. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1338. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1339. s->qscale, adap_parm, s->qscale*adap_parm,
  1340. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1341. }
  1342. #endif
  1343. /* DCT & quantize */
  1344. if(s->out_format==FMT_MJPEG){
  1345. for(i=0;i<6;i++) {
  1346. int overflow;
  1347. s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
  1348. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1349. }
  1350. }else{
  1351. for(i=0;i<6;i++) {
  1352. if(!skip_dct[i]){
  1353. int overflow;
  1354. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1355. // FIXME we could decide to change to quantizer instead of clipping
  1356. // JS: I don't think that would be a good idea it could lower quality instead
  1357. // of improve it. Just INTRADC clipping deserves changes in quantizer
  1358. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1359. }else
  1360. s->block_last_index[i]= -1;
  1361. }
  1362. if(s->luma_elim_threshold && !s->mb_intra)
  1363. for(i=0; i<4; i++)
  1364. dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0);
  1365. if(s->chroma_elim_threshold && !s->mb_intra)
  1366. for(i=4; i<6; i++)
  1367. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1);
  1368. }
  1369. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  1370. s->block_last_index[4]=
  1371. s->block_last_index[5]= 0;
  1372. s->block[4][0]=
  1373. s->block[5][0]= 128;
  1374. }
  1375. /* huffman encode */
  1376. switch(s->out_format) {
  1377. case FMT_MPEG1:
  1378. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1379. break;
  1380. case FMT_H263:
  1381. if (s->h263_msmpeg4)
  1382. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1383. else if(s->h263_pred)
  1384. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1385. else
  1386. h263_encode_mb(s, s->block, motion_x, motion_y);
  1387. break;
  1388. case FMT_MJPEG:
  1389. mjpeg_encode_mb(s, s->block);
  1390. break;
  1391. }
  1392. }
  1393. void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
  1394. {
  1395. int bytes= length>>4;
  1396. int bits= length&15;
  1397. int i;
  1398. if(length==0) return;
  1399. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  1400. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  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->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  1637. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  1638. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1639. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1640. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1641. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1642. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1643. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1644. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1645. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  1646. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  1647. // int d;
  1648. int dmin=10000000;
  1649. s->mb_x = mb_x;
  1650. s->mb_y = mb_y;
  1651. s->block_index[0]+=2;
  1652. s->block_index[1]+=2;
  1653. s->block_index[2]+=2;
  1654. s->block_index[3]+=2;
  1655. s->block_index[4]++;
  1656. s->block_index[5]++;
  1657. /* write gob / video packet header for formats which support it at any MB (MPEG4) */
  1658. if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
  1659. int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
  1660. //the *2 is there so we stay below the requested size
  1661. if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){
  1662. if(s->codec_id==CODEC_ID_MPEG4){
  1663. if(s->data_partitioning && s->pict_type!=B_TYPE){
  1664. ff_mpeg4_merge_partitions(s);
  1665. ff_mpeg4_init_partitions(s);
  1666. }
  1667. ff_mpeg4_encode_video_packet_header(s);
  1668. if(s->flags&CODEC_FLAG_PASS1){
  1669. int bits= get_bit_count(&s->pb);
  1670. s->misc_bits+= bits - s->last_bits;
  1671. s->last_bits= bits;
  1672. }
  1673. ff_mpeg4_clean_buffers(s);
  1674. }
  1675. s->ptr_lastgob = pbBufPtr(&s->pb);
  1676. s->first_slice_line=1;
  1677. s->resync_mb_x=mb_x;
  1678. s->resync_mb_y=mb_y;
  1679. }
  1680. if( (s->resync_mb_x == s->mb_x)
  1681. && s->resync_mb_y+1 == s->mb_y){
  1682. s->first_slice_line=0;
  1683. }
  1684. }
  1685. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  1686. int next_block=0;
  1687. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  1688. copy_context_before_encode(&backup_s, s, -1);
  1689. backup_s.pb= s->pb;
  1690. best_s.data_partitioning= s->data_partitioning;
  1691. if(s->data_partitioning){
  1692. backup_s.pb2= s->pb2;
  1693. backup_s.tex_pb= s->tex_pb;
  1694. }
  1695. if(mb_type&MB_TYPE_INTER){
  1696. s->mv_dir = MV_DIR_FORWARD;
  1697. s->mv_type = MV_TYPE_16X16;
  1698. s->mb_intra= 0;
  1699. s->mv[0][0][0] = s->p_mv_table[xy][0];
  1700. s->mv[0][0][1] = s->p_mv_table[xy][1];
  1701. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  1702. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1703. }
  1704. if(mb_type&MB_TYPE_INTER4V){
  1705. s->mv_dir = MV_DIR_FORWARD;
  1706. s->mv_type = MV_TYPE_8X8;
  1707. s->mb_intra= 0;
  1708. for(i=0; i<4; i++){
  1709. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1710. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1711. }
  1712. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  1713. &dmin, &next_block, 0, 0);
  1714. }
  1715. if(mb_type&MB_TYPE_FORWARD){
  1716. s->mv_dir = MV_DIR_FORWARD;
  1717. s->mv_type = MV_TYPE_16X16;
  1718. s->mb_intra= 0;
  1719. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1720. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1721. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  1722. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1723. }
  1724. if(mb_type&MB_TYPE_BACKWARD){
  1725. s->mv_dir = MV_DIR_BACKWARD;
  1726. s->mv_type = MV_TYPE_16X16;
  1727. s->mb_intra= 0;
  1728. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1729. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1730. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  1731. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  1732. }
  1733. if(mb_type&MB_TYPE_BIDIR){
  1734. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1735. s->mv_type = MV_TYPE_16X16;
  1736. s->mb_intra= 0;
  1737. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1738. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1739. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1740. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1741. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  1742. &dmin, &next_block, 0, 0);
  1743. }
  1744. if(mb_type&MB_TYPE_DIRECT){
  1745. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1746. s->mv_type = MV_TYPE_16X16; //FIXME
  1747. s->mb_intra= 0;
  1748. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1749. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1750. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1751. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1752. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  1753. &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
  1754. }
  1755. if(mb_type&MB_TYPE_INTRA){
  1756. s->mv_dir = MV_DIR_FORWARD;
  1757. s->mv_type = MV_TYPE_16X16;
  1758. s->mb_intra= 1;
  1759. s->mv[0][0][0] = 0;
  1760. s->mv[0][0][1] = 0;
  1761. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  1762. &dmin, &next_block, 0, 0);
  1763. /* force cleaning of ac/dc pred stuff if needed ... */
  1764. if(s->h263_pred || s->h263_aic)
  1765. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1766. }
  1767. copy_context_after_encode(s, &best_s, -1);
  1768. pb_bits_count= get_bit_count(&s->pb);
  1769. flush_put_bits(&s->pb);
  1770. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  1771. s->pb= backup_s.pb;
  1772. if(s->data_partitioning){
  1773. pb2_bits_count= get_bit_count(&s->pb2);
  1774. flush_put_bits(&s->pb2);
  1775. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  1776. s->pb2= backup_s.pb2;
  1777. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  1778. flush_put_bits(&s->tex_pb);
  1779. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  1780. s->tex_pb= backup_s.tex_pb;
  1781. }
  1782. s->last_bits= get_bit_count(&s->pb);
  1783. } else {
  1784. int motion_x, motion_y;
  1785. s->mv_type=MV_TYPE_16X16;
  1786. // only one MB-Type possible
  1787. switch(mb_type){
  1788. case MB_TYPE_INTRA:
  1789. s->mv_dir = MV_DIR_FORWARD;
  1790. s->mb_intra= 1;
  1791. motion_x= s->mv[0][0][0] = 0;
  1792. motion_y= s->mv[0][0][1] = 0;
  1793. break;
  1794. case MB_TYPE_INTER:
  1795. s->mv_dir = MV_DIR_FORWARD;
  1796. s->mb_intra= 0;
  1797. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  1798. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  1799. break;
  1800. case MB_TYPE_INTER4V:
  1801. s->mv_dir = MV_DIR_FORWARD;
  1802. s->mv_type = MV_TYPE_8X8;
  1803. s->mb_intra= 0;
  1804. for(i=0; i<4; i++){
  1805. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1806. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1807. }
  1808. motion_x= motion_y= 0;
  1809. break;
  1810. case MB_TYPE_DIRECT:
  1811. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1812. s->mb_intra= 0;
  1813. motion_x=s->b_direct_mv_table[xy][0];
  1814. motion_y=s->b_direct_mv_table[xy][1];
  1815. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1816. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1817. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1818. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1819. break;
  1820. case MB_TYPE_BIDIR:
  1821. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1822. s->mb_intra= 0;
  1823. motion_x=0;
  1824. motion_y=0;
  1825. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1826. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1827. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1828. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1829. break;
  1830. case MB_TYPE_BACKWARD:
  1831. s->mv_dir = MV_DIR_BACKWARD;
  1832. s->mb_intra= 0;
  1833. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1834. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1835. break;
  1836. case MB_TYPE_FORWARD:
  1837. s->mv_dir = MV_DIR_FORWARD;
  1838. s->mb_intra= 0;
  1839. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1840. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1841. // printf(" %d %d ", motion_x, motion_y);
  1842. break;
  1843. default:
  1844. motion_x=motion_y=0; //gcc warning fix
  1845. printf("illegal MB type\n");
  1846. }
  1847. encode_mb(s, motion_x, motion_y);
  1848. }
  1849. /* clean the MV table in IPS frames for direct mode in B frames */
  1850. if(s->mb_intra /* && I,P,S_TYPE */){
  1851. s->p_mv_table[xy][0]=0;
  1852. s->p_mv_table[xy][1]=0;
  1853. }
  1854. MPV_decode_mb(s, s->block);
  1855. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  1856. }
  1857. /* Obtain average GOB size for RTP */
  1858. if (s->rtp_mode) {
  1859. if (!mb_y)
  1860. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  1861. else if (!(mb_y % s->gob_index)) {
  1862. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  1863. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  1864. }
  1865. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  1866. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  1867. if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
  1868. }
  1869. }
  1870. emms_c();
  1871. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  1872. ff_mpeg4_merge_partitions(s);
  1873. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  1874. msmpeg4_encode_ext_header(s);
  1875. if(s->codec_id==CODEC_ID_MPEG4)
  1876. ff_mpeg4_stuffing(&s->pb);
  1877. //if (s->gob_number)
  1878. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  1879. /* Send the last GOB if RTP */
  1880. if (s->rtp_mode) {
  1881. flush_put_bits(&s->pb);
  1882. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1883. /* Call the RTP callback to send the last GOB */
  1884. if (s->rtp_callback)
  1885. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  1886. s->ptr_lastgob = pbBufPtr(&s->pb);
  1887. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  1888. }
  1889. }
  1890. static int dct_quantize_c(MpegEncContext *s,
  1891. DCTELEM *block, int n,
  1892. int qscale, int *overflow)
  1893. {
  1894. int i, j, level, last_non_zero, q;
  1895. const int *qmat;
  1896. int bias;
  1897. int max=0;
  1898. unsigned int threshold1, threshold2;
  1899. av_fdct (block);
  1900. /* we need this permutation so that we correct the IDCT
  1901. permutation. will be moved into DCT code */
  1902. block_permute(block);
  1903. if (s->mb_intra) {
  1904. if (!s->h263_aic) {
  1905. if (n < 4)
  1906. q = s->y_dc_scale;
  1907. else
  1908. q = s->c_dc_scale;
  1909. q = q << 3;
  1910. } else
  1911. /* For AIC we skip quant/dequant of INTRADC */
  1912. q = 1 << 3;
  1913. /* note: block[0] is assumed to be positive */
  1914. block[0] = (block[0] + (q >> 1)) / q;
  1915. i = 1;
  1916. last_non_zero = 0;
  1917. qmat = s->q_intra_matrix[qscale];
  1918. bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  1919. } else {
  1920. i = 0;
  1921. last_non_zero = -1;
  1922. qmat = s->q_inter_matrix[qscale];
  1923. bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  1924. }
  1925. threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
  1926. threshold2= threshold1<<1;
  1927. for(;i<64;i++) {
  1928. j = zigzag_direct[i];
  1929. level = block[j];
  1930. level = level * qmat[j];
  1931. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1932. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1933. if(((unsigned)(level+threshold1))>threshold2){
  1934. if(level>0){
  1935. level= (bias + level)>>(QMAT_SHIFT - 3);
  1936. block[j]= level;
  1937. }else{
  1938. level= (bias - level)>>(QMAT_SHIFT - 3);
  1939. block[j]= -level;
  1940. }
  1941. max |=level;
  1942. last_non_zero = i;
  1943. }else{
  1944. block[j]=0;
  1945. }
  1946. }
  1947. *overflow= s->max_qcoeff < max; //overflow might have happend
  1948. return last_non_zero;
  1949. }
  1950. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1951. DCTELEM *block, int n, int qscale)
  1952. {
  1953. int i, level, nCoeffs;
  1954. const UINT16 *quant_matrix;
  1955. if(s->alternate_scan) nCoeffs= 64;
  1956. else nCoeffs= s->block_last_index[n]+1;
  1957. if (s->mb_intra) {
  1958. if (n < 4)
  1959. block[0] = block[0] * s->y_dc_scale;
  1960. else
  1961. block[0] = block[0] * s->c_dc_scale;
  1962. /* XXX: only mpeg1 */
  1963. quant_matrix = s->intra_matrix;
  1964. for(i=1;i<nCoeffs;i++) {
  1965. int j= zigzag_direct[i];
  1966. level = block[j];
  1967. if (level) {
  1968. if (level < 0) {
  1969. level = -level;
  1970. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1971. level = (level - 1) | 1;
  1972. level = -level;
  1973. } else {
  1974. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1975. level = (level - 1) | 1;
  1976. }
  1977. #ifdef PARANOID
  1978. if (level < -2048 || level > 2047)
  1979. fprintf(stderr, "unquant error %d %d\n", i, level);
  1980. #endif
  1981. block[j] = level;
  1982. }
  1983. }
  1984. } else {
  1985. i = 0;
  1986. quant_matrix = s->inter_matrix;
  1987. for(;i<nCoeffs;i++) {
  1988. int j= zigzag_direct[i];
  1989. level = block[j];
  1990. if (level) {
  1991. if (level < 0) {
  1992. level = -level;
  1993. level = (((level << 1) + 1) * qscale *
  1994. ((int) (quant_matrix[j]))) >> 4;
  1995. level = (level - 1) | 1;
  1996. level = -level;
  1997. } else {
  1998. level = (((level << 1) + 1) * qscale *
  1999. ((int) (quant_matrix[j]))) >> 4;
  2000. level = (level - 1) | 1;
  2001. }
  2002. #ifdef PARANOID
  2003. if (level < -2048 || level > 2047)
  2004. fprintf(stderr, "unquant error %d %d\n", i, level);
  2005. #endif
  2006. block[j] = level;
  2007. }
  2008. }
  2009. }
  2010. }
  2011. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  2012. DCTELEM *block, int n, int qscale)
  2013. {
  2014. int i, level, nCoeffs;
  2015. const UINT16 *quant_matrix;
  2016. if(s->alternate_scan) nCoeffs= 64;
  2017. else nCoeffs= s->block_last_index[n]+1;
  2018. if (s->mb_intra) {
  2019. if (n < 4)
  2020. block[0] = block[0] * s->y_dc_scale;
  2021. else
  2022. block[0] = block[0] * s->c_dc_scale;
  2023. quant_matrix = s->intra_matrix;
  2024. for(i=1;i<nCoeffs;i++) {
  2025. int j= zigzag_direct[i];
  2026. level = block[j];
  2027. if (level) {
  2028. if (level < 0) {
  2029. level = -level;
  2030. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2031. level = -level;
  2032. } else {
  2033. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2034. }
  2035. #ifdef PARANOID
  2036. if (level < -2048 || level > 2047)
  2037. fprintf(stderr, "unquant error %d %d\n", i, level);
  2038. #endif
  2039. block[j] = level;
  2040. }
  2041. }
  2042. } else {
  2043. int sum=-1;
  2044. i = 0;
  2045. quant_matrix = s->inter_matrix;
  2046. for(;i<nCoeffs;i++) {
  2047. int j= zigzag_direct[i];
  2048. level = block[j];
  2049. if (level) {
  2050. if (level < 0) {
  2051. level = -level;
  2052. level = (((level << 1) + 1) * qscale *
  2053. ((int) (quant_matrix[j]))) >> 4;
  2054. level = -level;
  2055. } else {
  2056. level = (((level << 1) + 1) * qscale *
  2057. ((int) (quant_matrix[j]))) >> 4;
  2058. }
  2059. #ifdef PARANOID
  2060. if (level < -2048 || level > 2047)
  2061. fprintf(stderr, "unquant error %d %d\n", i, level);
  2062. #endif
  2063. block[j] = level;
  2064. sum+=level;
  2065. }
  2066. }
  2067. block[63]^=sum&1;
  2068. }
  2069. }
  2070. static void dct_unquantize_h263_c(MpegEncContext *s,
  2071. DCTELEM *block, int n, int qscale)
  2072. {
  2073. int i, level, qmul, qadd;
  2074. int nCoeffs;
  2075. if (s->mb_intra) {
  2076. if (!s->h263_aic) {
  2077. if (n < 4)
  2078. block[0] = block[0] * s->y_dc_scale;
  2079. else
  2080. block[0] = block[0] * s->c_dc_scale;
  2081. }
  2082. i = 1;
  2083. nCoeffs= 64; //does not allways use zigzag table
  2084. } else {
  2085. i = 0;
  2086. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  2087. }
  2088. qmul = s->qscale << 1;
  2089. if (s->h263_aic && s->mb_intra)
  2090. qadd = 0;
  2091. else
  2092. qadd = (s->qscale - 1) | 1;
  2093. for(;i<nCoeffs;i++) {
  2094. level = block[i];
  2095. if (level) {
  2096. if (level < 0) {
  2097. level = level * qmul - qadd;
  2098. } else {
  2099. level = level * qmul + qadd;
  2100. }
  2101. #ifdef PARANOID
  2102. if (level < -2048 || level > 2047)
  2103. fprintf(stderr, "unquant error %d %d\n", i, level);
  2104. #endif
  2105. block[i] = level;
  2106. }
  2107. }
  2108. }
  2109. static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
  2110. {
  2111. int dc, dcb, dcr, y, i;
  2112. for(i=0; i<4; i++){
  2113. dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
  2114. for(y=0; y<8; y++){
  2115. int x;
  2116. for(x=0; x<8; x++){
  2117. dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
  2118. }
  2119. }
  2120. }
  2121. dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2122. dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2123. for(y=0; y<8; y++){
  2124. int x;
  2125. for(x=0; x<8; x++){
  2126. dest_cb[x + y*(s->linesize>>1)]= dcb/8;
  2127. dest_cr[x + y*(s->linesize>>1)]= dcr/8;
  2128. }
  2129. }
  2130. }
  2131. /**
  2132. * will conceal past errors, and allso drop b frames if needed
  2133. *
  2134. */
  2135. void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
  2136. {
  2137. int mb_x= s->mb_x;
  2138. int mb_y= s->mb_y;
  2139. int mb_dist=0;
  2140. int i, intra_count=0, inter_count=0;
  2141. int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
  2142. int inter_conceal= s->msmpeg4_version ? 50 : 50;
  2143. // for last block
  2144. if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
  2145. if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
  2146. if(s->decoding_error==0 && unknown_pos){
  2147. if(s->data_partitioning && s->pict_type!=B_TYPE)
  2148. s->decoding_error= DECODING_AC_LOST;
  2149. else
  2150. s->decoding_error= DECODING_DESYNC;
  2151. }
  2152. if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
  2153. for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
  2154. if(s->mbintra_table[i]) intra_count++;
  2155. else inter_count++;
  2156. }
  2157. if(s->decoding_error==DECODING_AC_LOST){
  2158. intra_conceal*=2;
  2159. inter_conceal*=2;
  2160. }else if(s->decoding_error==DECODING_ACDC_LOST){
  2161. intra_conceal*=2;
  2162. inter_conceal*=2;
  2163. }
  2164. if(unknown_pos && (intra_count<inter_count)){
  2165. intra_conceal= inter_conceal= s->mb_num;
  2166. // printf("%d %d\n",intra_count, inter_count);
  2167. }
  2168. fprintf(stderr, "concealing errors\n");
  2169. /* for all MBs from the current one back until the last resync marker */
  2170. for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
  2171. for(; mb_x>=0; mb_x--){
  2172. uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2173. uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  2174. uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  2175. int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
  2176. int mb_y_backup= s->mb_y;
  2177. s->mb_x=mb_x;
  2178. s->mb_y=mb_y;
  2179. if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
  2180. if(s->decoding_error==DECODING_AC_LOST){
  2181. remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  2182. // printf("remove ac to %d %d\n", mb_x, mb_y);
  2183. }else{
  2184. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2185. s->last_picture, 0, 0, put_pixels_tab,
  2186. 0/*mx*/, 0/*my*/, 16);
  2187. }
  2188. }
  2189. else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
  2190. int mx=0;
  2191. int my=0;
  2192. if(s->decoding_error!=DECODING_DESYNC){
  2193. int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
  2194. mx= s->motion_val[ xy ][0];
  2195. my= s->motion_val[ xy ][1];
  2196. }
  2197. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2198. s->last_picture, 0, 0, put_pixels_tab,
  2199. mx, my, 16);
  2200. }
  2201. s->mb_x= mb_x_backup;
  2202. s->mb_y= mb_y_backup;
  2203. if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
  2204. if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
  2205. }
  2206. mb_x=s->mb_width-1;
  2207. }
  2208. }
  2209. AVCodec mpeg1video_encoder = {
  2210. "mpeg1video",
  2211. CODEC_TYPE_VIDEO,
  2212. CODEC_ID_MPEG1VIDEO,
  2213. sizeof(MpegEncContext),
  2214. MPV_encode_init,
  2215. MPV_encode_picture,
  2216. MPV_encode_end,
  2217. };
  2218. AVCodec h263_encoder = {
  2219. "h263",
  2220. CODEC_TYPE_VIDEO,
  2221. CODEC_ID_H263,
  2222. sizeof(MpegEncContext),
  2223. MPV_encode_init,
  2224. MPV_encode_picture,
  2225. MPV_encode_end,
  2226. };
  2227. AVCodec h263p_encoder = {
  2228. "h263p",
  2229. CODEC_TYPE_VIDEO,
  2230. CODEC_ID_H263P,
  2231. sizeof(MpegEncContext),
  2232. MPV_encode_init,
  2233. MPV_encode_picture,
  2234. MPV_encode_end,
  2235. };
  2236. AVCodec rv10_encoder = {
  2237. "rv10",
  2238. CODEC_TYPE_VIDEO,
  2239. CODEC_ID_RV10,
  2240. sizeof(MpegEncContext),
  2241. MPV_encode_init,
  2242. MPV_encode_picture,
  2243. MPV_encode_end,
  2244. };
  2245. AVCodec mjpeg_encoder = {
  2246. "mjpeg",
  2247. CODEC_TYPE_VIDEO,
  2248. CODEC_ID_MJPEG,
  2249. sizeof(MpegEncContext),
  2250. MPV_encode_init,
  2251. MPV_encode_picture,
  2252. MPV_encode_end,
  2253. };
  2254. AVCodec mpeg4_encoder = {
  2255. "mpeg4",
  2256. CODEC_TYPE_VIDEO,
  2257. CODEC_ID_MPEG4,
  2258. sizeof(MpegEncContext),
  2259. MPV_encode_init,
  2260. MPV_encode_picture,
  2261. MPV_encode_end,
  2262. };
  2263. AVCodec msmpeg4v1_encoder = {
  2264. "msmpeg4v1",
  2265. CODEC_TYPE_VIDEO,
  2266. CODEC_ID_MSMPEG4V1,
  2267. sizeof(MpegEncContext),
  2268. MPV_encode_init,
  2269. MPV_encode_picture,
  2270. MPV_encode_end,
  2271. };
  2272. AVCodec msmpeg4v2_encoder = {
  2273. "msmpeg4v2",
  2274. CODEC_TYPE_VIDEO,
  2275. CODEC_ID_MSMPEG4V2,
  2276. sizeof(MpegEncContext),
  2277. MPV_encode_init,
  2278. MPV_encode_picture,
  2279. MPV_encode_end,
  2280. };
  2281. AVCodec msmpeg4v3_encoder = {
  2282. "msmpeg4",
  2283. CODEC_TYPE_VIDEO,
  2284. CODEC_ID_MSMPEG4V3,
  2285. sizeof(MpegEncContext),
  2286. MPV_encode_init,
  2287. MPV_encode_picture,
  2288. MPV_encode_end,
  2289. };
  2290. AVCodec wmv1_encoder = {
  2291. "wmv1",
  2292. CODEC_TYPE_VIDEO,
  2293. CODEC_ID_WMV1,
  2294. sizeof(MpegEncContext),
  2295. MPV_encode_init,
  2296. MPV_encode_picture,
  2297. MPV_encode_end,
  2298. };
  2299. AVCodec wmv2_encoder = {
  2300. "wmv2",
  2301. CODEC_TYPE_VIDEO,
  2302. CODEC_ID_WMV2,
  2303. sizeof(MpegEncContext),
  2304. MPV_encode_init,
  2305. MPV_encode_picture,
  2306. MPV_encode_end,
  2307. };