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.

2342 lines
79KB

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