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.

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