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.

2557 lines
88KB

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