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.

2315 lines
78KB

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