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.

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