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.

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