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.

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