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.

2339 lines
79KB

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