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.

2329 lines
78KB

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