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.

2340 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->header_bits = s->header_bits;
  697. avctx->mv_bits = s->mv_bits;
  698. avctx->misc_bits = s->misc_bits;
  699. avctx->i_tex_bits = s->i_tex_bits;
  700. avctx->p_tex_bits = s->p_tex_bits;
  701. avctx->i_count = s->i_count;
  702. avctx->p_count = s->p_count;
  703. avctx->skip_count = s->skip_count;
  704. MPV_frame_end(s);
  705. if (s->out_format == FMT_MJPEG)
  706. mjpeg_picture_trailer(s);
  707. avctx->quality = s->qscale;
  708. if(s->flags&CODEC_FLAG_PASS1)
  709. ff_write_pass1_stats(s);
  710. }
  711. s->input_picture_number++;
  712. s->input_picture_in_gop_number++;
  713. flush_put_bits(&s->pb);
  714. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  715. if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
  716. else s->pb_frame_bits= s->frame_bits;
  717. s->total_bits += s->frame_bits;
  718. avctx->frame_bits = s->frame_bits;
  719. //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
  720. //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);
  721. if (avctx->get_psnr) {
  722. /* At this point pict->data should have the original frame */
  723. /* an s->current_picture should have the coded/decoded frame */
  724. get_psnr(pict->data, s->current_picture,
  725. pict->linesize, s->linesize, avctx);
  726. // printf("%f\n", avctx->psnr_y);
  727. }
  728. return pbBufPtr(&s->pb) - s->pb.buf;
  729. }
  730. static inline void gmc1_motion(MpegEncContext *s,
  731. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  732. int dest_offset,
  733. UINT8 **ref_picture, int src_offset,
  734. int h)
  735. {
  736. UINT8 *ptr;
  737. int offset, src_x, src_y, linesize;
  738. int motion_x, motion_y;
  739. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  740. motion_x= s->sprite_offset[0][0];
  741. motion_y= s->sprite_offset[0][1];
  742. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  743. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  744. motion_x<<=(3-s->sprite_warping_accuracy);
  745. motion_y<<=(3-s->sprite_warping_accuracy);
  746. src_x = clip(src_x, -16, s->width);
  747. if (src_x == s->width)
  748. motion_x =0;
  749. src_y = clip(src_y, -16, s->height);
  750. if (src_y == s->height)
  751. motion_y =0;
  752. linesize = s->linesize;
  753. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  754. dest_y+=dest_offset;
  755. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  756. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  757. motion_x= s->sprite_offset[1][0];
  758. motion_y= s->sprite_offset[1][1];
  759. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  760. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  761. motion_x<<=(3-s->sprite_warping_accuracy);
  762. motion_y<<=(3-s->sprite_warping_accuracy);
  763. src_x = clip(src_x, -8, s->width>>1);
  764. if (src_x == s->width>>1)
  765. motion_x =0;
  766. src_y = clip(src_y, -8, s->height>>1);
  767. if (src_y == s->height>>1)
  768. motion_y =0;
  769. offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
  770. ptr = ref_picture[1] + offset;
  771. gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  772. ptr = ref_picture[2] + offset;
  773. gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  774. return;
  775. }
  776. /* apply one mpeg motion vector to the three components */
  777. static inline void mpeg_motion(MpegEncContext *s,
  778. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  779. int dest_offset,
  780. UINT8 **ref_picture, int src_offset,
  781. int field_based, op_pixels_func *pix_op,
  782. int motion_x, int motion_y, int h)
  783. {
  784. UINT8 *ptr;
  785. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  786. if(s->quarter_sample)
  787. {
  788. motion_x>>=1;
  789. motion_y>>=1;
  790. }
  791. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  792. src_x = s->mb_x * 16 + (motion_x >> 1);
  793. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  794. /* WARNING: do no forget half pels */
  795. height = s->height >> field_based;
  796. src_x = clip(src_x, -16, s->width);
  797. if (src_x == s->width)
  798. dxy &= ~1;
  799. src_y = clip(src_y, -16, height);
  800. if (src_y == height)
  801. dxy &= ~2;
  802. linesize = s->linesize << field_based;
  803. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  804. dest_y += dest_offset;
  805. pix_op[dxy](dest_y, ptr, linesize, h);
  806. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  807. if (s->out_format == FMT_H263) {
  808. dxy = 0;
  809. if ((motion_x & 3) != 0)
  810. dxy |= 1;
  811. if ((motion_y & 3) != 0)
  812. dxy |= 2;
  813. mx = motion_x >> 2;
  814. my = motion_y >> 2;
  815. } else {
  816. mx = motion_x / 2;
  817. my = motion_y / 2;
  818. dxy = ((my & 1) << 1) | (mx & 1);
  819. mx >>= 1;
  820. my >>= 1;
  821. }
  822. src_x = s->mb_x * 8 + mx;
  823. src_y = s->mb_y * (8 >> field_based) + my;
  824. src_x = clip(src_x, -8, s->width >> 1);
  825. if (src_x == (s->width >> 1))
  826. dxy &= ~1;
  827. src_y = clip(src_y, -8, height >> 1);
  828. if (src_y == (height >> 1))
  829. dxy &= ~2;
  830. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  831. ptr = ref_picture[1] + offset;
  832. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  833. ptr = ref_picture[2] + offset;
  834. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  835. }
  836. static inline void qpel_motion(MpegEncContext *s,
  837. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  838. int dest_offset,
  839. UINT8 **ref_picture, int src_offset,
  840. int field_based, op_pixels_func *pix_op,
  841. qpel_mc_func *qpix_op,
  842. int motion_x, int motion_y, int h)
  843. {
  844. UINT8 *ptr;
  845. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  846. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  847. src_x = s->mb_x * 16 + (motion_x >> 2);
  848. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  849. height = s->height >> field_based;
  850. src_x = clip(src_x, -16, s->width);
  851. if (src_x == s->width)
  852. dxy &= ~3;
  853. src_y = clip(src_y, -16, height);
  854. if (src_y == height)
  855. dxy &= ~12;
  856. linesize = s->linesize << field_based;
  857. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  858. dest_y += dest_offset;
  859. //printf("%d %d %d\n", src_x, src_y, dxy);
  860. qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
  861. qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
  862. qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
  863. qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
  864. mx= (motion_x>>1) | (motion_x&1);
  865. my= (motion_y>>1) | (motion_y&1);
  866. dxy = 0;
  867. if ((mx & 3) != 0)
  868. dxy |= 1;
  869. if ((my & 3) != 0)
  870. dxy |= 2;
  871. mx = mx >> 2;
  872. my = my >> 2;
  873. src_x = s->mb_x * 8 + mx;
  874. src_y = s->mb_y * (8 >> field_based) + my;
  875. src_x = clip(src_x, -8, s->width >> 1);
  876. if (src_x == (s->width >> 1))
  877. dxy &= ~1;
  878. src_y = clip(src_y, -8, height >> 1);
  879. if (src_y == (height >> 1))
  880. dxy &= ~2;
  881. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  882. ptr = ref_picture[1] + offset;
  883. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  884. ptr = ref_picture[2] + offset;
  885. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  886. }
  887. static inline void MPV_motion(MpegEncContext *s,
  888. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  889. int dir, UINT8 **ref_picture,
  890. op_pixels_func *pix_op, qpel_mc_func *qpix_op)
  891. {
  892. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  893. int mb_x, mb_y, i;
  894. UINT8 *ptr, *dest;
  895. mb_x = s->mb_x;
  896. mb_y = s->mb_y;
  897. switch(s->mv_type) {
  898. case MV_TYPE_16X16:
  899. if(s->mcsel){
  900. #if 0
  901. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  902. ref_picture, 0,
  903. 0, pix_op,
  904. s->sprite_offset[0][0]>>3,
  905. s->sprite_offset[0][1]>>3,
  906. 16);
  907. #else
  908. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  909. ref_picture, 0,
  910. 16);
  911. #endif
  912. }else if(s->quarter_sample && dir==0){ //FIXME
  913. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  914. ref_picture, 0,
  915. 0, pix_op, qpix_op,
  916. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  917. }else{
  918. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  919. ref_picture, 0,
  920. 0, pix_op,
  921. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  922. }
  923. break;
  924. case MV_TYPE_8X8:
  925. for(i=0;i<4;i++) {
  926. motion_x = s->mv[dir][i][0];
  927. motion_y = s->mv[dir][i][1];
  928. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  929. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  930. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  931. /* WARNING: do no forget half pels */
  932. src_x = clip(src_x, -16, s->width);
  933. if (src_x == s->width)
  934. dxy &= ~1;
  935. src_y = clip(src_y, -16, s->height);
  936. if (src_y == s->height)
  937. dxy &= ~2;
  938. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  939. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  940. pix_op[dxy](dest, ptr, s->linesize, 8);
  941. }
  942. /* In case of 8X8, we construct a single chroma motion vector
  943. with a special rounding */
  944. mx = 0;
  945. my = 0;
  946. for(i=0;i<4;i++) {
  947. mx += s->mv[dir][i][0];
  948. my += s->mv[dir][i][1];
  949. }
  950. if (mx >= 0)
  951. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  952. else {
  953. mx = -mx;
  954. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  955. }
  956. if (my >= 0)
  957. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  958. else {
  959. my = -my;
  960. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  961. }
  962. dxy = ((my & 1) << 1) | (mx & 1);
  963. mx >>= 1;
  964. my >>= 1;
  965. src_x = mb_x * 8 + mx;
  966. src_y = mb_y * 8 + my;
  967. src_x = clip(src_x, -8, s->width/2);
  968. if (src_x == s->width/2)
  969. dxy &= ~1;
  970. src_y = clip(src_y, -8, s->height/2);
  971. if (src_y == s->height/2)
  972. dxy &= ~2;
  973. offset = (src_y * (s->linesize >> 1)) + src_x;
  974. ptr = ref_picture[1] + offset;
  975. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  976. ptr = ref_picture[2] + offset;
  977. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  978. break;
  979. case MV_TYPE_FIELD:
  980. if (s->picture_structure == PICT_FRAME) {
  981. /* top field */
  982. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  983. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  984. 1, pix_op,
  985. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  986. /* bottom field */
  987. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  988. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  989. 1, pix_op,
  990. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  991. } else {
  992. }
  993. break;
  994. }
  995. }
  996. /* put block[] to dest[] */
  997. static inline void put_dct(MpegEncContext *s,
  998. DCTELEM *block, int i, UINT8 *dest, int line_size)
  999. {
  1000. if (!s->mpeg2)
  1001. s->dct_unquantize(s, block, i, s->qscale);
  1002. ff_idct (block);
  1003. put_pixels_clamped(block, dest, line_size);
  1004. }
  1005. /* add block[] to dest[] */
  1006. static inline void add_dct(MpegEncContext *s,
  1007. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1008. {
  1009. /* skip dequant / idct if we are really late ;) */
  1010. if(s->hurry_up>1) return;
  1011. if (s->block_last_index[i] >= 0) {
  1012. if (!s->mpeg2)
  1013. if(s->encoding || (!s->h263_msmpeg4))
  1014. s->dct_unquantize(s, block, i, s->qscale);
  1015. ff_idct (block);
  1016. add_pixels_clamped(block, dest, line_size);
  1017. }
  1018. }
  1019. /* generic function called after a macroblock has been parsed by the
  1020. decoder or after it has been encoded by the encoder.
  1021. Important variables used:
  1022. s->mb_intra : true if intra macroblock
  1023. s->mv_dir : motion vector direction
  1024. s->mv_type : motion vector type
  1025. s->mv : motion vector
  1026. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1027. */
  1028. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1029. {
  1030. int mb_x, mb_y;
  1031. int dct_linesize, dct_offset;
  1032. op_pixels_func *op_pix;
  1033. qpel_mc_func *op_qpix;
  1034. mb_x = s->mb_x;
  1035. mb_y = s->mb_y;
  1036. #ifdef FF_POSTPROCESS
  1037. quant_store[mb_y][mb_x]=s->qscale;
  1038. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  1039. #endif
  1040. /* update DC predictors for P macroblocks */
  1041. if (!s->mb_intra) {
  1042. if (s->h263_pred || s->h263_aic) {
  1043. if(s->mbintra_table[mb_x + mb_y*s->mb_width])
  1044. {
  1045. int wrap, xy, v;
  1046. s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
  1047. wrap = 2 * s->mb_width + 2;
  1048. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  1049. v = 1024;
  1050. s->dc_val[0][xy] = v;
  1051. s->dc_val[0][xy + 1] = v;
  1052. s->dc_val[0][xy + wrap] = v;
  1053. s->dc_val[0][xy + 1 + wrap] = v;
  1054. /* ac pred */
  1055. memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
  1056. memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
  1057. memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
  1058. memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
  1059. if (s->h263_msmpeg4) {
  1060. s->coded_block[xy] = 0;
  1061. s->coded_block[xy + 1] = 0;
  1062. s->coded_block[xy + wrap] = 0;
  1063. s->coded_block[xy + 1 + wrap] = 0;
  1064. }
  1065. /* chroma */
  1066. wrap = s->mb_width + 2;
  1067. xy = mb_x + 1 + (mb_y + 1) * wrap;
  1068. s->dc_val[1][xy] = v;
  1069. s->dc_val[2][xy] = v;
  1070. /* ac pred */
  1071. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  1072. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  1073. }
  1074. } else {
  1075. s->last_dc[0] = 128 << s->intra_dc_precision;
  1076. s->last_dc[1] = 128 << s->intra_dc_precision;
  1077. s->last_dc[2] = 128 << s->intra_dc_precision;
  1078. }
  1079. }
  1080. else if (s->h263_pred || s->h263_aic)
  1081. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1082. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1083. if (s->out_format == FMT_H263) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1084. if(s->pict_type!=B_TYPE){
  1085. int xy, wrap, motion_x, motion_y;
  1086. wrap = 2 * s->mb_width + 2;
  1087. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  1088. if (s->mb_intra) {
  1089. motion_x = 0;
  1090. motion_y = 0;
  1091. goto motion_init;
  1092. } else if (s->mv_type == MV_TYPE_16X16) {
  1093. motion_x = s->mv[0][0][0];
  1094. motion_y = s->mv[0][0][1];
  1095. motion_init:
  1096. /* no update if 8X8 because it has been done during parsing */
  1097. s->motion_val[xy][0] = motion_x;
  1098. s->motion_val[xy][1] = motion_y;
  1099. s->motion_val[xy + 1][0] = motion_x;
  1100. s->motion_val[xy + 1][1] = motion_y;
  1101. s->motion_val[xy + wrap][0] = motion_x;
  1102. s->motion_val[xy + wrap][1] = motion_y;
  1103. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1104. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1105. }
  1106. }
  1107. }
  1108. if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
  1109. UINT8 *dest_y, *dest_cb, *dest_cr;
  1110. UINT8 *mbskip_ptr;
  1111. /* avoid copy if macroblock skipped in last frame too
  1112. dont touch it for B-frames as they need the skip info from the next p-frame */
  1113. if (s->pict_type != B_TYPE) {
  1114. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  1115. if (s->mb_skiped) {
  1116. s->mb_skiped = 0;
  1117. /* if previous was skipped too, then nothing to do !
  1118. skip only during decoding as we might trash the buffers during encoding a bit */
  1119. if (*mbskip_ptr != 0 && !s->encoding)
  1120. goto the_end;
  1121. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  1122. } else {
  1123. *mbskip_ptr = 0; /* not skipped */
  1124. }
  1125. }
  1126. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  1127. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1128. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1129. if (s->interlaced_dct) {
  1130. dct_linesize = s->linesize * 2;
  1131. dct_offset = s->linesize;
  1132. } else {
  1133. dct_linesize = s->linesize;
  1134. dct_offset = s->linesize * 8;
  1135. }
  1136. if (!s->mb_intra) {
  1137. /* motion handling */
  1138. if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){
  1139. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1140. op_pix = put_pixels_tab;
  1141. op_qpix= qpel_mc_rnd_tab;
  1142. }else{
  1143. op_pix = put_no_rnd_pixels_tab;
  1144. op_qpix= qpel_mc_no_rnd_tab;
  1145. }
  1146. if (s->mv_dir & MV_DIR_FORWARD) {
  1147. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1148. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1149. op_pix = avg_pixels_tab;
  1150. else
  1151. op_pix = avg_no_rnd_pixels_tab;
  1152. }
  1153. if (s->mv_dir & MV_DIR_BACKWARD) {
  1154. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1155. }
  1156. }
  1157. /* add dct residue */
  1158. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1159. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1160. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1161. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1162. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1163. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1164. } else {
  1165. /* dct only in intra block */
  1166. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1167. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1168. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1169. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1170. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  1171. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  1172. }
  1173. }
  1174. the_end:
  1175. emms_c(); //FIXME remove
  1176. }
  1177. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1178. {
  1179. int i;
  1180. const int maxlevel= s->max_qcoeff;
  1181. const int minlevel= s->min_qcoeff;
  1182. for(i=0;i<=last_index; i++){
  1183. const int j = zigzag_direct[i];
  1184. int level = block[j];
  1185. if (level>maxlevel) level=maxlevel;
  1186. else if(level<minlevel) level=minlevel;
  1187. block[j]= level;
  1188. }
  1189. }
  1190. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1191. {
  1192. const int mb_x= s->mb_x;
  1193. const int mb_y= s->mb_y;
  1194. int i;
  1195. #if 0
  1196. if (s->interlaced_dct) {
  1197. dct_linesize = s->linesize * 2;
  1198. dct_offset = s->linesize;
  1199. } else {
  1200. dct_linesize = s->linesize;
  1201. dct_offset = s->linesize * 8;
  1202. }
  1203. #endif
  1204. if (s->mb_intra) {
  1205. UINT8 *ptr;
  1206. int wrap;
  1207. wrap = s->linesize;
  1208. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1209. get_pixels(s->block[0], ptr , wrap);
  1210. get_pixels(s->block[1], ptr + 8, wrap);
  1211. get_pixels(s->block[2], ptr + 8 * wrap , wrap);
  1212. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1213. wrap >>=1;
  1214. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1215. get_pixels(s->block[4], ptr, wrap);
  1216. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1217. get_pixels(s->block[5], ptr, wrap);
  1218. }else{
  1219. op_pixels_func *op_pix;
  1220. qpel_mc_func *op_qpix;
  1221. UINT8 *dest_y, *dest_cb, *dest_cr;
  1222. UINT8 *ptr;
  1223. int wrap;
  1224. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  1225. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1226. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  1227. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1228. op_pix = put_pixels_tab;
  1229. op_qpix= qpel_mc_rnd_tab;
  1230. }else{
  1231. op_pix = put_no_rnd_pixels_tab;
  1232. op_qpix= qpel_mc_no_rnd_tab;
  1233. }
  1234. if (s->mv_dir & MV_DIR_FORWARD) {
  1235. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1236. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1237. op_pix = avg_pixels_tab;
  1238. else
  1239. op_pix = avg_no_rnd_pixels_tab;
  1240. }
  1241. if (s->mv_dir & MV_DIR_BACKWARD) {
  1242. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1243. }
  1244. wrap = s->linesize;
  1245. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1246. diff_pixels(s->block[0], ptr , dest_y , wrap);
  1247. diff_pixels(s->block[1], ptr + 8, dest_y + 8, wrap);
  1248. diff_pixels(s->block[2], ptr + 8 * wrap , dest_y + 8 * wrap , wrap);
  1249. diff_pixels(s->block[3], ptr + 8 * wrap + 8, dest_y + 8 * wrap + 8, wrap);
  1250. wrap >>=1;
  1251. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1252. diff_pixels(s->block[4], ptr, dest_cb, wrap);
  1253. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1254. diff_pixels(s->block[5], ptr, dest_cr, wrap);
  1255. }
  1256. #if 0
  1257. {
  1258. float adap_parm;
  1259. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1260. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1261. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1262. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1263. s->qscale, adap_parm, s->qscale*adap_parm,
  1264. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1265. }
  1266. #endif
  1267. /* DCT & quantize */
  1268. if (s->h263_pred && s->msmpeg4_version!=2) {
  1269. h263_dc_scale(s);
  1270. } else if (s->h263_aic) {
  1271. s->y_dc_scale = 2*s->qscale;
  1272. s->c_dc_scale = 2*s->qscale;
  1273. } else {
  1274. /* default quantization values */
  1275. s->y_dc_scale = 8;
  1276. s->c_dc_scale = 8;
  1277. }
  1278. if(s->out_format==FMT_MJPEG){
  1279. for(i=0;i<6;i++) {
  1280. int overflow;
  1281. s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
  1282. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1283. }
  1284. }else{
  1285. for(i=0;i<6;i++) {
  1286. int overflow;
  1287. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1288. // FIXME we could decide to change to quantizer instead of clipping
  1289. // JS: I don't think that would be a good idea it could lower quality instead
  1290. // of improve it. Just INTRADC clipping deserves changes in quantizer
  1291. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1292. }
  1293. }
  1294. /* huffman encode */
  1295. switch(s->out_format) {
  1296. case FMT_MPEG1:
  1297. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1298. break;
  1299. case FMT_H263:
  1300. if (s->h263_msmpeg4)
  1301. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1302. else if(s->h263_pred)
  1303. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1304. else
  1305. h263_encode_mb(s, s->block, motion_x, motion_y);
  1306. break;
  1307. case FMT_MJPEG:
  1308. mjpeg_encode_mb(s, s->block);
  1309. break;
  1310. }
  1311. }
  1312. static void copy_bits(PutBitContext *pb, UINT8 *src, int length)
  1313. {
  1314. #if 1
  1315. int bytes= length>>4;
  1316. int bits= length&15;
  1317. int i;
  1318. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  1319. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  1320. #else
  1321. int bytes= length>>3;
  1322. int bits= length&7;
  1323. int i;
  1324. for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
  1325. put_bits(pb, bits, src[i]>>(8-bits));
  1326. #endif
  1327. }
  1328. static void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1329. int i;
  1330. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1331. /* mpeg1 */
  1332. d->mb_incr= s->mb_incr;
  1333. for(i=0; i<3; i++)
  1334. d->last_dc[i]= s->last_dc[i];
  1335. /* statistics */
  1336. d->mv_bits= s->mv_bits;
  1337. d->i_tex_bits= s->i_tex_bits;
  1338. d->p_tex_bits= s->p_tex_bits;
  1339. d->i_count= s->i_count;
  1340. d->p_count= s->p_count;
  1341. d->skip_count= s->skip_count;
  1342. d->misc_bits= s->misc_bits;
  1343. d->last_bits= 0;
  1344. d->mb_skiped= s->mb_skiped;
  1345. }
  1346. static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1347. int i;
  1348. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  1349. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1350. /* mpeg1 */
  1351. d->mb_incr= s->mb_incr;
  1352. for(i=0; i<3; i++)
  1353. d->last_dc[i]= s->last_dc[i];
  1354. /* statistics */
  1355. d->mv_bits= s->mv_bits;
  1356. d->i_tex_bits= s->i_tex_bits;
  1357. d->p_tex_bits= s->p_tex_bits;
  1358. d->i_count= s->i_count;
  1359. d->p_count= s->p_count;
  1360. d->skip_count= s->skip_count;
  1361. d->misc_bits= s->misc_bits;
  1362. d->mb_intra= s->mb_intra;
  1363. d->mb_skiped= s->mb_skiped;
  1364. d->mv_type= s->mv_type;
  1365. d->mv_dir= s->mv_dir;
  1366. d->pb= s->pb;
  1367. d->block= s->block;
  1368. for(i=0; i<6; i++)
  1369. d->block_last_index[i]= s->block_last_index[i];
  1370. }
  1371. static void encode_picture(MpegEncContext *s, int picture_number)
  1372. {
  1373. int mb_x, mb_y, last_gob, pdif = 0;
  1374. int i;
  1375. int bits;
  1376. MpegEncContext best_s, backup_s;
  1377. UINT8 bit_buf[7][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
  1378. s->picture_number = picture_number;
  1379. s->block_wrap[0]=
  1380. s->block_wrap[1]=
  1381. s->block_wrap[2]=
  1382. s->block_wrap[3]= s->mb_width*2 + 2;
  1383. s->block_wrap[4]=
  1384. s->block_wrap[5]= s->mb_width + 2;
  1385. /* Reset the average MB variance */
  1386. s->avg_mb_var = 0;
  1387. s->mc_mb_var = 0;
  1388. /* we need to initialize some time vars before we can encode b-frames */
  1389. if (s->h263_pred && !s->h263_msmpeg4)
  1390. ff_set_mpeg4_time(s, s->picture_number);
  1391. /* Estimate motion for every MB */
  1392. if(s->pict_type != I_TYPE){
  1393. // int16_t (*tmp)[2]= s->p_mv_table;
  1394. // s->p_mv_table= s->last_mv_table;
  1395. // s->last_mv_table= s->mv_table;
  1396. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1397. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1398. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1399. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1400. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1401. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1402. s->mb_x = mb_x;
  1403. s->mb_y = mb_y;
  1404. s->block_index[0]+=2;
  1405. s->block_index[1]+=2;
  1406. s->block_index[2]+=2;
  1407. s->block_index[3]+=2;
  1408. /* compute motion vector & mb_type and store in context */
  1409. if(s->pict_type==B_TYPE)
  1410. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  1411. else
  1412. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  1413. // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
  1414. }
  1415. }
  1416. emms_c();
  1417. }else if(s->pict_type == I_TYPE){
  1418. /* I-Frame */
  1419. //FIXME do we need to zero them?
  1420. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  1421. memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
  1422. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1423. }
  1424. if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ //FIXME subtract MV bits
  1425. s->pict_type= I_TYPE;
  1426. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1427. if(s->max_b_frames==0){
  1428. s->input_pict_type= I_TYPE;
  1429. s->input_picture_in_gop_number=0;
  1430. }
  1431. //printf("Scene change detected, encoding as I Frame\n");
  1432. }
  1433. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
  1434. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  1435. ff_fix_long_p_mvs(s);
  1436. if(s->pict_type==B_TYPE){
  1437. s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  1438. s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  1439. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  1440. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  1441. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  1442. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  1443. }
  1444. //printf("f_code %d ///\n", s->f_code);
  1445. // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
  1446. if(s->flags&CODEC_FLAG_PASS2)
  1447. s->qscale = ff_rate_estimate_qscale_pass2(s);
  1448. else if (!s->fixed_qscale)
  1449. s->qscale = ff_rate_estimate_qscale(s);
  1450. if (s->out_format == FMT_MJPEG) {
  1451. /* for mjpeg, we do include qscale in the matrix */
  1452. s->intra_matrix[0] = default_intra_matrix[0];
  1453. for(i=1;i<64;i++)
  1454. s->intra_matrix[i] = CLAMP_TO_8BIT((default_intra_matrix[i] * s->qscale) >> 3);
  1455. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16,
  1456. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
  1457. }
  1458. s->last_bits= get_bit_count(&s->pb);
  1459. switch(s->out_format) {
  1460. case FMT_MJPEG:
  1461. mjpeg_picture_header(s);
  1462. break;
  1463. case FMT_H263:
  1464. if (s->h263_msmpeg4)
  1465. msmpeg4_encode_picture_header(s, picture_number);
  1466. else if (s->h263_pred)
  1467. mpeg4_encode_picture_header(s, picture_number);
  1468. else if (s->h263_rv10)
  1469. rv10_encode_picture_header(s, picture_number);
  1470. else
  1471. h263_encode_picture_header(s, picture_number);
  1472. break;
  1473. case FMT_MPEG1:
  1474. mpeg1_encode_picture_header(s, picture_number);
  1475. break;
  1476. }
  1477. bits= get_bit_count(&s->pb);
  1478. s->header_bits= bits - s->last_bits;
  1479. s->last_bits= bits;
  1480. s->mv_bits=0;
  1481. s->misc_bits=0;
  1482. s->i_tex_bits=0;
  1483. s->p_tex_bits=0;
  1484. s->i_count=0;
  1485. s->p_count=0;
  1486. s->skip_count=0;
  1487. /* init last dc values */
  1488. /* note: quant matrix value (8) is implied here */
  1489. s->last_dc[0] = 128;
  1490. s->last_dc[1] = 128;
  1491. s->last_dc[2] = 128;
  1492. s->mb_incr = 1;
  1493. s->last_mv[0][0][0] = 0;
  1494. s->last_mv[0][0][1] = 0;
  1495. /* Get the GOB height based on picture height */
  1496. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  1497. if (s->height <= 400)
  1498. s->gob_index = 1;
  1499. else if (s->height <= 800)
  1500. s->gob_index = 2;
  1501. else
  1502. s->gob_index = 4;
  1503. }
  1504. s->avg_mb_var = s->avg_mb_var / s->mb_num;
  1505. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1506. /* Put GOB header based on RTP MTU */
  1507. /* TODO: Put all this stuff in a separate generic function */
  1508. if (s->rtp_mode) {
  1509. if (!mb_y) {
  1510. s->ptr_lastgob = s->pb.buf;
  1511. s->ptr_last_mb_line = s->pb.buf;
  1512. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1513. last_gob = h263_encode_gob_header(s, mb_y);
  1514. if (last_gob) {
  1515. s->first_gob_line = 1;
  1516. }
  1517. }
  1518. }
  1519. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1520. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1521. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1522. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1523. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1524. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1525. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1526. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  1527. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  1528. PutBitContext pb;
  1529. int d;
  1530. int dmin=10000000;
  1531. int best=0;
  1532. s->mb_x = mb_x;
  1533. s->mb_y = mb_y;
  1534. s->block_index[0]+=2;
  1535. s->block_index[1]+=2;
  1536. s->block_index[2]+=2;
  1537. s->block_index[3]+=2;
  1538. s->block_index[4]++;
  1539. s->block_index[5]++;
  1540. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  1541. int next_block=0;
  1542. pb= s->pb;
  1543. copy_context_before_encode(&backup_s, s, -1);
  1544. if(mb_type&MB_TYPE_INTER){
  1545. s->mv_dir = MV_DIR_FORWARD;
  1546. s->mv_type = MV_TYPE_16X16;
  1547. s->mb_intra= 0;
  1548. s->mv[0][0][0] = s->p_mv_table[xy][0];
  1549. s->mv[0][0][1] = s->p_mv_table[xy][1];
  1550. init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
  1551. s->block= s->blocks[next_block];
  1552. s->last_bits= 0; //done in copy_context_before_encode but we skip that here
  1553. encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
  1554. d= get_bit_count(&s->pb);
  1555. if(d<dmin){
  1556. flush_put_bits(&s->pb);
  1557. dmin=d;
  1558. copy_context_after_encode(&best_s, s, MB_TYPE_INTER);
  1559. best=1;
  1560. next_block^=1;
  1561. }
  1562. }
  1563. if(mb_type&MB_TYPE_INTER4V){
  1564. copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);
  1565. s->mv_dir = MV_DIR_FORWARD;
  1566. s->mv_type = MV_TYPE_8X8;
  1567. s->mb_intra= 0;
  1568. for(i=0; i<4; i++){
  1569. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1570. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1571. }
  1572. init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
  1573. s->block= s->blocks[next_block];
  1574. encode_mb(s, 0, 0);
  1575. d= get_bit_count(&s->pb);
  1576. if(d<dmin){
  1577. flush_put_bits(&s->pb);
  1578. dmin=d;
  1579. copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);
  1580. best=2;
  1581. next_block^=1;
  1582. }
  1583. }
  1584. if(mb_type&MB_TYPE_FORWARD){
  1585. copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD);
  1586. s->mv_dir = MV_DIR_FORWARD;
  1587. s->mv_type = MV_TYPE_16X16;
  1588. s->mb_intra= 0;
  1589. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1590. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1591. init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL);
  1592. s->block= s->blocks[next_block];
  1593. encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
  1594. d= get_bit_count(&s->pb);
  1595. if(d<dmin){
  1596. flush_put_bits(&s->pb);
  1597. dmin=d;
  1598. copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD);
  1599. best=3;
  1600. next_block^=1;
  1601. }
  1602. }
  1603. if(mb_type&MB_TYPE_BACKWARD){
  1604. copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD);
  1605. s->mv_dir = MV_DIR_BACKWARD;
  1606. s->mv_type = MV_TYPE_16X16;
  1607. s->mb_intra= 0;
  1608. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1609. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1610. init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL);
  1611. s->block= s->blocks[next_block];
  1612. encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]);
  1613. d= get_bit_count(&s->pb);
  1614. if(d<dmin){
  1615. flush_put_bits(&s->pb);
  1616. dmin=d;
  1617. copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD);
  1618. best=4;
  1619. next_block^=1;
  1620. }
  1621. }
  1622. if(mb_type&MB_TYPE_BIDIR){
  1623. copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR);
  1624. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1625. s->mv_type = MV_TYPE_16X16;
  1626. s->mb_intra= 0;
  1627. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1628. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1629. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1630. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1631. init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL);
  1632. s->block= s->blocks[next_block];
  1633. encode_mb(s, 0, 0);
  1634. d= get_bit_count(&s->pb);
  1635. if(d<dmin){
  1636. flush_put_bits(&s->pb);
  1637. dmin=d;
  1638. copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR);
  1639. best=5;
  1640. next_block^=1;
  1641. }
  1642. }
  1643. if(mb_type&MB_TYPE_DIRECT){
  1644. copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT);
  1645. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1646. s->mv_type = MV_TYPE_16X16; //FIXME
  1647. s->mb_intra= 0;
  1648. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1649. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1650. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1651. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1652. init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL);
  1653. s->block= s->blocks[next_block];
  1654. encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
  1655. d= get_bit_count(&s->pb);
  1656. if(d<dmin){
  1657. flush_put_bits(&s->pb);
  1658. dmin=d;
  1659. copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT);
  1660. best=6;
  1661. next_block^=1;
  1662. }
  1663. }
  1664. if(mb_type&MB_TYPE_INTRA){
  1665. copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);
  1666. s->mv_dir = MV_DIR_FORWARD;
  1667. s->mv_type = MV_TYPE_16X16;
  1668. s->mb_intra= 1;
  1669. s->mv[0][0][0] = 0;
  1670. s->mv[0][0][1] = 0;
  1671. init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
  1672. s->block= s->blocks[next_block];
  1673. encode_mb(s, 0, 0);
  1674. d= get_bit_count(&s->pb);
  1675. if(d<dmin){
  1676. flush_put_bits(&s->pb);
  1677. dmin=d;
  1678. copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);
  1679. best=0;
  1680. next_block^=1;
  1681. }
  1682. /* force cleaning of ac/dc pred stuff if needed ... */
  1683. if(s->h263_pred || s->h263_aic)
  1684. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1685. }
  1686. copy_context_after_encode(s, &best_s, -1);
  1687. copy_bits(&pb, bit_buf[best], dmin);
  1688. s->pb= pb;
  1689. s->last_bits= get_bit_count(&s->pb);
  1690. } else {
  1691. int motion_x, motion_y;
  1692. s->mv_type=MV_TYPE_16X16;
  1693. // only one MB-Type possible
  1694. switch(mb_type){
  1695. case MB_TYPE_INTRA:
  1696. s->mv_dir = MV_DIR_FORWARD;
  1697. s->mb_intra= 1;
  1698. motion_x= s->mv[0][0][0] = 0;
  1699. motion_y= s->mv[0][0][1] = 0;
  1700. break;
  1701. case MB_TYPE_INTER:
  1702. s->mv_dir = MV_DIR_FORWARD;
  1703. s->mb_intra= 0;
  1704. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  1705. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  1706. break;
  1707. case MB_TYPE_DIRECT:
  1708. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1709. s->mb_intra= 0;
  1710. motion_x=s->b_direct_mv_table[xy][0];
  1711. motion_y=s->b_direct_mv_table[xy][1];
  1712. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1713. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1714. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1715. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1716. break;
  1717. case MB_TYPE_BIDIR:
  1718. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1719. s->mb_intra= 0;
  1720. motion_x=0;
  1721. motion_y=0;
  1722. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1723. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1724. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1725. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1726. break;
  1727. case MB_TYPE_BACKWARD:
  1728. s->mv_dir = MV_DIR_BACKWARD;
  1729. s->mb_intra= 0;
  1730. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1731. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1732. break;
  1733. case MB_TYPE_FORWARD:
  1734. s->mv_dir = MV_DIR_FORWARD;
  1735. s->mb_intra= 0;
  1736. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1737. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1738. // printf(" %d %d ", motion_x, motion_y);
  1739. break;
  1740. default:
  1741. motion_x=motion_y=0; //gcc warning fix
  1742. printf("illegal MB type\n");
  1743. }
  1744. encode_mb(s, motion_x, motion_y);
  1745. }
  1746. /* clean the MV table in IPS frames for direct mode in B frames */
  1747. if(s->mb_intra /* && I,P,S_TYPE */){
  1748. s->p_mv_table[xy][0]=0;
  1749. s->p_mv_table[xy][1]=0;
  1750. }
  1751. MPV_decode_mb(s, s->block);
  1752. }
  1753. /* Obtain average GOB size for RTP */
  1754. if (s->rtp_mode) {
  1755. if (!mb_y)
  1756. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  1757. else if (!(mb_y % s->gob_index)) {
  1758. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  1759. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  1760. }
  1761. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  1762. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  1763. s->first_gob_line = 0;
  1764. }
  1765. }
  1766. emms_c();
  1767. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  1768. msmpeg4_encode_ext_header(s);
  1769. //if (s->gob_number)
  1770. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  1771. /* Send the last GOB if RTP */
  1772. if (s->rtp_mode) {
  1773. flush_put_bits(&s->pb);
  1774. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1775. /* Call the RTP callback to send the last GOB */
  1776. if (s->rtp_callback)
  1777. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  1778. s->ptr_lastgob = pbBufPtr(&s->pb);
  1779. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  1780. }
  1781. }
  1782. static int dct_quantize_c(MpegEncContext *s,
  1783. DCTELEM *block, int n,
  1784. int qscale, int *overflow)
  1785. {
  1786. int i, j, level, last_non_zero, q;
  1787. const int *qmat;
  1788. int bias;
  1789. int max=0;
  1790. unsigned int threshold1, threshold2;
  1791. av_fdct (block);
  1792. /* we need this permutation so that we correct the IDCT
  1793. permutation. will be moved into DCT code */
  1794. block_permute(block);
  1795. if (s->mb_intra) {
  1796. if (!s->h263_aic) {
  1797. if (n < 4)
  1798. q = s->y_dc_scale;
  1799. else
  1800. q = s->c_dc_scale;
  1801. q = q << 3;
  1802. } else
  1803. /* For AIC we skip quant/dequant of INTRADC */
  1804. q = 1 << 3;
  1805. /* note: block[0] is assumed to be positive */
  1806. block[0] = (block[0] + (q >> 1)) / q;
  1807. i = 1;
  1808. last_non_zero = 0;
  1809. qmat = s->q_intra_matrix[qscale];
  1810. bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  1811. } else {
  1812. i = 0;
  1813. last_non_zero = -1;
  1814. qmat = s->q_inter_matrix[qscale];
  1815. bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  1816. }
  1817. threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
  1818. threshold2= threshold1<<1;
  1819. for(;i<64;i++) {
  1820. j = zigzag_direct[i];
  1821. level = block[j];
  1822. level = level * qmat[j];
  1823. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1824. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1825. if(((unsigned)(level+threshold1))>threshold2){
  1826. if(level>0){
  1827. level= (bias + level)>>(QMAT_SHIFT - 3);
  1828. block[j]= level;
  1829. }else{
  1830. level= (bias - level)>>(QMAT_SHIFT - 3);
  1831. block[j]= -level;
  1832. }
  1833. max |=level;
  1834. last_non_zero = i;
  1835. }else{
  1836. block[j]=0;
  1837. }
  1838. }
  1839. *overflow= s->max_qcoeff < max; //overflow might have happend
  1840. return last_non_zero;
  1841. }
  1842. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1843. DCTELEM *block, int n, int qscale)
  1844. {
  1845. int i, level, nCoeffs;
  1846. const UINT16 *quant_matrix;
  1847. if(s->alternate_scan) nCoeffs= 64;
  1848. else nCoeffs= s->block_last_index[n]+1;
  1849. if (s->mb_intra) {
  1850. if (n < 4)
  1851. block[0] = block[0] * s->y_dc_scale;
  1852. else
  1853. block[0] = block[0] * s->c_dc_scale;
  1854. /* XXX: only mpeg1 */
  1855. quant_matrix = s->intra_matrix;
  1856. for(i=1;i<nCoeffs;i++) {
  1857. int j= zigzag_direct[i];
  1858. level = block[j];
  1859. if (level) {
  1860. if (level < 0) {
  1861. level = -level;
  1862. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1863. level = (level - 1) | 1;
  1864. level = -level;
  1865. } else {
  1866. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1867. level = (level - 1) | 1;
  1868. }
  1869. #ifdef PARANOID
  1870. if (level < -2048 || level > 2047)
  1871. fprintf(stderr, "unquant error %d %d\n", i, level);
  1872. #endif
  1873. block[j] = level;
  1874. }
  1875. }
  1876. } else {
  1877. i = 0;
  1878. quant_matrix = s->inter_matrix;
  1879. for(;i<nCoeffs;i++) {
  1880. int j= zigzag_direct[i];
  1881. level = block[j];
  1882. if (level) {
  1883. if (level < 0) {
  1884. level = -level;
  1885. level = (((level << 1) + 1) * qscale *
  1886. ((int) (quant_matrix[j]))) >> 4;
  1887. level = (level - 1) | 1;
  1888. level = -level;
  1889. } else {
  1890. level = (((level << 1) + 1) * qscale *
  1891. ((int) (quant_matrix[j]))) >> 4;
  1892. level = (level - 1) | 1;
  1893. }
  1894. #ifdef PARANOID
  1895. if (level < -2048 || level > 2047)
  1896. fprintf(stderr, "unquant error %d %d\n", i, level);
  1897. #endif
  1898. block[j] = level;
  1899. }
  1900. }
  1901. }
  1902. }
  1903. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  1904. DCTELEM *block, int n, int qscale)
  1905. {
  1906. int i, level, nCoeffs;
  1907. const UINT16 *quant_matrix;
  1908. if(s->alternate_scan) nCoeffs= 64;
  1909. else nCoeffs= s->block_last_index[n]+1;
  1910. if (s->mb_intra) {
  1911. if (n < 4)
  1912. block[0] = block[0] * s->y_dc_scale;
  1913. else
  1914. block[0] = block[0] * s->c_dc_scale;
  1915. quant_matrix = s->intra_matrix;
  1916. for(i=1;i<nCoeffs;i++) {
  1917. int j= zigzag_direct[i];
  1918. level = block[j];
  1919. if (level) {
  1920. if (level < 0) {
  1921. level = -level;
  1922. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1923. level = -level;
  1924. } else {
  1925. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1926. }
  1927. #ifdef PARANOID
  1928. if (level < -2048 || level > 2047)
  1929. fprintf(stderr, "unquant error %d %d\n", i, level);
  1930. #endif
  1931. block[j] = level;
  1932. }
  1933. }
  1934. } else {
  1935. int sum=-1;
  1936. i = 0;
  1937. quant_matrix = s->inter_matrix;
  1938. for(;i<nCoeffs;i++) {
  1939. int j= zigzag_direct[i];
  1940. level = block[j];
  1941. if (level) {
  1942. if (level < 0) {
  1943. level = -level;
  1944. level = (((level << 1) + 1) * qscale *
  1945. ((int) (quant_matrix[j]))) >> 4;
  1946. level = -level;
  1947. } else {
  1948. level = (((level << 1) + 1) * qscale *
  1949. ((int) (quant_matrix[j]))) >> 4;
  1950. }
  1951. #ifdef PARANOID
  1952. if (level < -2048 || level > 2047)
  1953. fprintf(stderr, "unquant error %d %d\n", i, level);
  1954. #endif
  1955. block[j] = level;
  1956. sum+=level;
  1957. }
  1958. }
  1959. block[63]^=sum&1;
  1960. }
  1961. }
  1962. static void dct_unquantize_h263_c(MpegEncContext *s,
  1963. DCTELEM *block, int n, int qscale)
  1964. {
  1965. int i, level, qmul, qadd;
  1966. int nCoeffs;
  1967. if (s->mb_intra) {
  1968. if (!s->h263_aic) {
  1969. if (n < 4)
  1970. block[0] = block[0] * s->y_dc_scale;
  1971. else
  1972. block[0] = block[0] * s->c_dc_scale;
  1973. }
  1974. i = 1;
  1975. nCoeffs= 64; //does not allways use zigzag table
  1976. } else {
  1977. i = 0;
  1978. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  1979. }
  1980. qmul = s->qscale << 1;
  1981. if (s->h263_aic && s->mb_intra)
  1982. qadd = 0;
  1983. else
  1984. qadd = (s->qscale - 1) | 1;
  1985. for(;i<nCoeffs;i++) {
  1986. level = block[i];
  1987. if (level) {
  1988. if (level < 0) {
  1989. level = level * qmul - qadd;
  1990. } else {
  1991. level = level * qmul + qadd;
  1992. }
  1993. #ifdef PARANOID
  1994. if (level < -2048 || level > 2047)
  1995. fprintf(stderr, "unquant error %d %d\n", i, level);
  1996. #endif
  1997. block[i] = level;
  1998. }
  1999. }
  2000. }
  2001. AVCodec mpeg1video_encoder = {
  2002. "mpeg1video",
  2003. CODEC_TYPE_VIDEO,
  2004. CODEC_ID_MPEG1VIDEO,
  2005. sizeof(MpegEncContext),
  2006. MPV_encode_init,
  2007. MPV_encode_picture,
  2008. MPV_encode_end,
  2009. };
  2010. AVCodec h263_encoder = {
  2011. "h263",
  2012. CODEC_TYPE_VIDEO,
  2013. CODEC_ID_H263,
  2014. sizeof(MpegEncContext),
  2015. MPV_encode_init,
  2016. MPV_encode_picture,
  2017. MPV_encode_end,
  2018. };
  2019. AVCodec h263p_encoder = {
  2020. "h263p",
  2021. CODEC_TYPE_VIDEO,
  2022. CODEC_ID_H263P,
  2023. sizeof(MpegEncContext),
  2024. MPV_encode_init,
  2025. MPV_encode_picture,
  2026. MPV_encode_end,
  2027. };
  2028. AVCodec rv10_encoder = {
  2029. "rv10",
  2030. CODEC_TYPE_VIDEO,
  2031. CODEC_ID_RV10,
  2032. sizeof(MpegEncContext),
  2033. MPV_encode_init,
  2034. MPV_encode_picture,
  2035. MPV_encode_end,
  2036. };
  2037. AVCodec mjpeg_encoder = {
  2038. "mjpeg",
  2039. CODEC_TYPE_VIDEO,
  2040. CODEC_ID_MJPEG,
  2041. sizeof(MpegEncContext),
  2042. MPV_encode_init,
  2043. MPV_encode_picture,
  2044. MPV_encode_end,
  2045. };
  2046. AVCodec mpeg4_encoder = {
  2047. "mpeg4",
  2048. CODEC_TYPE_VIDEO,
  2049. CODEC_ID_MPEG4,
  2050. sizeof(MpegEncContext),
  2051. MPV_encode_init,
  2052. MPV_encode_picture,
  2053. MPV_encode_end,
  2054. };
  2055. AVCodec msmpeg4v1_encoder = {
  2056. "msmpeg4v1",
  2057. CODEC_TYPE_VIDEO,
  2058. CODEC_ID_MSMPEG4V1,
  2059. sizeof(MpegEncContext),
  2060. MPV_encode_init,
  2061. MPV_encode_picture,
  2062. MPV_encode_end,
  2063. };
  2064. AVCodec msmpeg4v2_encoder = {
  2065. "msmpeg4v2",
  2066. CODEC_TYPE_VIDEO,
  2067. CODEC_ID_MSMPEG4V2,
  2068. sizeof(MpegEncContext),
  2069. MPV_encode_init,
  2070. MPV_encode_picture,
  2071. MPV_encode_end,
  2072. };
  2073. AVCodec msmpeg4v3_encoder = {
  2074. "msmpeg4",
  2075. CODEC_TYPE_VIDEO,
  2076. CODEC_ID_MSMPEG4V3,
  2077. sizeof(MpegEncContext),
  2078. MPV_encode_init,
  2079. MPV_encode_picture,
  2080. MPV_encode_end,
  2081. };