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.

2306 lines
75KB

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