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.

2309 lines
77KB

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