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.

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