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.

1801 lines
57KB

  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. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <string.h>
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. #ifdef USE_FASTMEMCPY
  27. #include "fastmemcpy.h"
  28. #endif
  29. static void encode_picture(MpegEncContext *s, int picture_number);
  30. static void rate_control_init(MpegEncContext *s);
  31. static int rate_estimate_qscale(MpegEncContext *s);
  32. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  33. DCTELEM *block, int n, int qscale);
  34. static void dct_unquantize_h263_c(MpegEncContext *s,
  35. DCTELEM *block, int n, int qscale);
  36. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
  37. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale);
  38. int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c;
  39. void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
  40. #define EDGE_WIDTH 16
  41. /* enable all paranoid tests for rounding, overflows, etc... */
  42. //#define PARANOID
  43. //#define DEBUG
  44. /* for jpeg fast DCT */
  45. #define CONST_BITS 14
  46. static const unsigned short aanscales[64] = {
  47. /* precomputed values scaled up by 14 bits */
  48. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  49. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  50. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  51. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  52. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  53. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  54. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  55. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  56. };
  57. static UINT8 h263_chroma_roundtab[16] = {
  58. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  59. };
  60. /* default motion estimation */
  61. int motion_estimation_method = ME_LOG;
  62. extern UINT8 zigzag_end[64];
  63. static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale)
  64. {
  65. int i;
  66. if (av_fdct == jpeg_fdct_ifast) {
  67. for(i=0;i<64;i++) {
  68. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  69. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  70. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  71. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  72. qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
  73. (aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
  74. }
  75. } else {
  76. for(i=0;i<64;i++) {
  77. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  78. So 16 <= qscale * quant_matrix[i] <= 7905
  79. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  80. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  81. */
  82. qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  83. qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
  84. }
  85. }
  86. }
  87. /* init common structure for both encoder and decoder */
  88. int MPV_common_init(MpegEncContext *s)
  89. {
  90. int c_size, i;
  91. UINT8 *pict;
  92. if (s->out_format == FMT_H263)
  93. s->dct_unquantize = dct_unquantize_h263_c;
  94. else
  95. s->dct_unquantize = dct_unquantize_mpeg1_c;
  96. #ifdef HAVE_MMX
  97. MPV_common_init_mmx(s);
  98. #endif
  99. s->mb_width = (s->width + 15) / 16;
  100. s->mb_height = (s->height + 15) / 16;
  101. s->mb_num = s->mb_width * s->mb_height;
  102. s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
  103. for(i=0;i<3;i++) {
  104. int w, h, shift, pict_start;
  105. w = s->linesize;
  106. h = s->mb_height * 16 + 2 * EDGE_WIDTH;
  107. shift = (i == 0) ? 0 : 1;
  108. c_size = (w >> shift) * (h >> shift);
  109. pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
  110. pict = av_mallocz(c_size);
  111. if (pict == NULL)
  112. goto fail;
  113. s->last_picture_base[i] = pict;
  114. s->last_picture[i] = pict + pict_start;
  115. pict = av_mallocz(c_size);
  116. if (pict == NULL)
  117. goto fail;
  118. s->next_picture_base[i] = pict;
  119. s->next_picture[i] = pict + pict_start;
  120. if (s->has_b_frames) {
  121. pict = av_mallocz(c_size);
  122. if (pict == NULL)
  123. goto fail;
  124. s->aux_picture_base[i] = pict;
  125. s->aux_picture[i] = pict + pict_start;
  126. }
  127. }
  128. if (s->encoding) {
  129. /* Allocate MB type table */
  130. s->mb_type = av_mallocz(s->mb_num * sizeof(char));
  131. if (s->mb_type == NULL) {
  132. perror("malloc");
  133. goto fail;
  134. }
  135. s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
  136. if (s->mb_var == NULL) {
  137. perror("malloc");
  138. goto fail;
  139. }
  140. /* Allocate MV table */
  141. /* By now we just have one MV per MB */
  142. s->mv_table[0] = av_mallocz(s->mb_num * sizeof(INT16));
  143. s->mv_table[1] = av_mallocz(s->mb_num * sizeof(INT16));
  144. if (s->mv_table[1] == NULL || s->mv_table[0] == NULL) {
  145. perror("malloc");
  146. goto fail;
  147. }
  148. }
  149. if (s->out_format == FMT_H263) {
  150. int size;
  151. /* MV prediction */
  152. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  153. s->motion_val = malloc(size * 2 * sizeof(INT16));
  154. if (s->motion_val == NULL)
  155. goto fail;
  156. memset(s->motion_val, 0, size * 2 * sizeof(INT16));
  157. }
  158. if (s->h263_pred || s->h263_plus) {
  159. int y_size, c_size, i, size;
  160. /* dc values */
  161. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  162. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  163. size = y_size + 2 * c_size;
  164. s->dc_val[0] = malloc(size * sizeof(INT16));
  165. if (s->dc_val[0] == NULL)
  166. goto fail;
  167. s->dc_val[1] = s->dc_val[0] + y_size;
  168. s->dc_val[2] = s->dc_val[1] + c_size;
  169. for(i=0;i<size;i++)
  170. s->dc_val[0][i] = 1024;
  171. /* ac values */
  172. s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
  173. if (s->ac_val[0] == NULL)
  174. goto fail;
  175. s->ac_val[1] = s->ac_val[0] + y_size;
  176. s->ac_val[2] = s->ac_val[1] + c_size;
  177. /* cbp values */
  178. s->coded_block = av_mallocz(y_size);
  179. if (!s->coded_block)
  180. goto fail;
  181. /* which mb is a intra block */
  182. s->mbintra_table = av_mallocz(s->mb_num);
  183. if (!s->mbintra_table)
  184. goto fail;
  185. memset(s->mbintra_table, 1, s->mb_num);
  186. }
  187. /* default structure is frame */
  188. s->picture_structure = PICT_FRAME;
  189. /* init macroblock skip table */
  190. if (!s->encoding) {
  191. s->mbskip_table = av_mallocz(s->mb_num);
  192. if (!s->mbskip_table)
  193. goto fail;
  194. }
  195. s->context_initialized = 1;
  196. return 0;
  197. fail:
  198. MPV_common_end(s);
  199. return -1;
  200. }
  201. /* init common structure for both encoder and decoder */
  202. void MPV_common_end(MpegEncContext *s)
  203. {
  204. int i;
  205. if (s->mb_type)
  206. free(s->mb_type);
  207. if (s->mb_var)
  208. free(s->mb_var);
  209. if (s->mv_table[0])
  210. free(s->mv_table[0]);
  211. if (s->mv_table[1])
  212. free(s->mv_table[1]);
  213. if (s->motion_val)
  214. free(s->motion_val);
  215. if (s->dc_val[0])
  216. free(s->dc_val[0]);
  217. if (s->ac_val[0])
  218. free(s->ac_val[0]);
  219. if (s->coded_block)
  220. free(s->coded_block);
  221. if (s->mbintra_table)
  222. free(s->mbintra_table);
  223. if (s->mbskip_table)
  224. free(s->mbskip_table);
  225. for(i=0;i<3;i++) {
  226. if (s->last_picture_base[i])
  227. free(s->last_picture_base[i]);
  228. if (s->next_picture_base[i])
  229. free(s->next_picture_base[i]);
  230. if (s->has_b_frames)
  231. free(s->aux_picture_base[i]);
  232. }
  233. s->context_initialized = 0;
  234. }
  235. /* init video encoder */
  236. int MPV_encode_init(AVCodecContext *avctx)
  237. {
  238. MpegEncContext *s = avctx->priv_data;
  239. int i;
  240. s->bit_rate = avctx->bit_rate;
  241. s->bit_rate_tolerance = avctx->bit_rate_tolerance;
  242. s->frame_rate = avctx->frame_rate;
  243. s->width = avctx->width;
  244. s->height = avctx->height;
  245. s->gop_size = avctx->gop_size;
  246. s->rtp_mode = avctx->rtp_mode;
  247. s->rtp_payload_size = avctx->rtp_payload_size;
  248. if (avctx->rtp_callback)
  249. s->rtp_callback = avctx->rtp_callback;
  250. s->qmin= avctx->qmin;
  251. s->qmax= avctx->qmax;
  252. s->max_qdiff= avctx->max_qdiff;
  253. s->qcompress= avctx->qcompress;
  254. s->qblur= avctx->qblur;
  255. s->avctx = avctx;
  256. if (s->gop_size <= 1) {
  257. s->intra_only = 1;
  258. s->gop_size = 12;
  259. } else {
  260. s->intra_only = 0;
  261. }
  262. s->full_search = motion_estimation_method;
  263. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  264. switch(avctx->codec->id) {
  265. case CODEC_ID_MPEG1VIDEO:
  266. s->out_format = FMT_MPEG1;
  267. break;
  268. case CODEC_ID_MJPEG:
  269. s->out_format = FMT_MJPEG;
  270. s->intra_only = 1; /* force intra only for jpeg */
  271. s->mjpeg_write_tables = 1; /* write all tables */
  272. s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
  273. s->mjpeg_vsample[1] = 1; /* the only currently supported values */
  274. s->mjpeg_vsample[2] = 1;
  275. s->mjpeg_hsample[0] = 2;
  276. s->mjpeg_hsample[1] = 1;
  277. s->mjpeg_hsample[2] = 1;
  278. if (mjpeg_init(s) < 0)
  279. return -1;
  280. break;
  281. case CODEC_ID_H263:
  282. if (h263_get_picture_format(s->width, s->height) == 7) {
  283. printf("Input picture size isn't suitable for h263 codec! try h263+\n");
  284. return -1;
  285. }
  286. s->out_format = FMT_H263;
  287. break;
  288. case CODEC_ID_H263P:
  289. s->out_format = FMT_H263;
  290. s->rtp_mode = 1;
  291. s->rtp_payload_size = 1200;
  292. s->h263_plus = 1;
  293. s->unrestricted_mv = 1;
  294. /* These are just to be sure */
  295. s->umvplus = 0;
  296. s->umvplus_dec = 0;
  297. break;
  298. case CODEC_ID_RV10:
  299. s->out_format = FMT_H263;
  300. s->h263_rv10 = 1;
  301. break;
  302. case CODEC_ID_MPEG4:
  303. s->out_format = FMT_H263;
  304. s->h263_pred = 1;
  305. s->unrestricted_mv = 1;
  306. break;
  307. case CODEC_ID_MSMPEG4:
  308. s->out_format = FMT_H263;
  309. s->h263_msmpeg4 = 1;
  310. s->h263_pred = 1;
  311. s->unrestricted_mv = 1;
  312. break;
  313. default:
  314. return -1;
  315. }
  316. if (s->out_format == FMT_H263)
  317. h263_encode_init_vlc(s);
  318. s->encoding = 1;
  319. /* init */
  320. if (MPV_common_init(s) < 0)
  321. return -1;
  322. /* init default q matrix */
  323. for(i=0;i<64;i++) {
  324. s->intra_matrix[i] = default_intra_matrix[i];
  325. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  326. }
  327. /* rate control init */
  328. rate_control_init(s);
  329. s->picture_number = 0;
  330. s->fake_picture_number = 0;
  331. /* motion detector init */
  332. s->f_code = 1;
  333. return 0;
  334. }
  335. int MPV_encode_end(AVCodecContext *avctx)
  336. {
  337. MpegEncContext *s = avctx->priv_data;
  338. #ifdef STATS
  339. print_stats();
  340. #endif
  341. MPV_common_end(s);
  342. if (s->out_format == FMT_MJPEG)
  343. mjpeg_close(s);
  344. return 0;
  345. }
  346. /* draw the edges of width 'w' of an image of size width, height */
  347. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
  348. {
  349. UINT8 *ptr, *last_line;
  350. int i;
  351. last_line = buf + (height - 1) * wrap;
  352. for(i=0;i<w;i++) {
  353. /* top and bottom */
  354. memcpy(buf - (i + 1) * wrap, buf, width);
  355. memcpy(last_line + (i + 1) * wrap, last_line, width);
  356. }
  357. /* left and right */
  358. ptr = buf;
  359. for(i=0;i<height;i++) {
  360. memset(ptr - w, ptr[0], w);
  361. memset(ptr + width, ptr[width-1], w);
  362. ptr += wrap;
  363. }
  364. /* corners */
  365. for(i=0;i<w;i++) {
  366. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  367. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  368. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  369. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  370. }
  371. }
  372. /* generic function for encode/decode called before a frame is coded/decoded */
  373. void MPV_frame_start(MpegEncContext *s)
  374. {
  375. int i;
  376. UINT8 *tmp;
  377. s->mb_skiped = 0;
  378. if (s->pict_type == B_TYPE) {
  379. for(i=0;i<3;i++) {
  380. s->current_picture[i] = s->aux_picture[i];
  381. }
  382. } else {
  383. s->last_non_b_pict_type= s->pict_type;
  384. for(i=0;i<3;i++) {
  385. /* swap next and last */
  386. tmp = s->last_picture[i];
  387. s->last_picture[i] = s->next_picture[i];
  388. s->next_picture[i] = tmp;
  389. s->current_picture[i] = tmp;
  390. }
  391. }
  392. }
  393. /* generic function for encode/decode called after a frame has been coded/decoded */
  394. void MPV_frame_end(MpegEncContext *s)
  395. {
  396. /* draw edge for correct motion prediction if outside */
  397. if (s->pict_type != B_TYPE && !s->intra_only) {
  398. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
  399. draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
  400. draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  401. draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  402. }else{
  403. /* mpeg4? / opendivx / xvid */
  404. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  405. draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  406. draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  407. }
  408. }
  409. emms_c();
  410. }
  411. int MPV_encode_picture(AVCodecContext *avctx,
  412. unsigned char *buf, int buf_size, void *data)
  413. {
  414. MpegEncContext *s = avctx->priv_data;
  415. AVPicture *pict = data;
  416. int i, j;
  417. if (s->fixed_qscale)
  418. s->qscale = avctx->quality;
  419. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  420. if (!s->intra_only) {
  421. /* first picture of GOP is intra */
  422. if ((s->picture_number % s->gop_size) == 0)
  423. s->pict_type = I_TYPE;
  424. else
  425. s->pict_type = P_TYPE;
  426. } else {
  427. s->pict_type = I_TYPE;
  428. }
  429. avctx->key_frame = (s->pict_type == I_TYPE);
  430. MPV_frame_start(s);
  431. for(i=0;i<3;i++) {
  432. UINT8 *src = pict->data[i];
  433. UINT8 *dest = s->current_picture[i];
  434. int src_wrap = pict->linesize[i];
  435. int dest_wrap = s->linesize;
  436. int w = s->width;
  437. int h = s->height;
  438. if (i >= 1) {
  439. dest_wrap >>= 1;
  440. w >>= 1;
  441. h >>= 1;
  442. }
  443. if(dest_wrap==src_wrap){
  444. s->new_picture[i] = pict->data[i];
  445. } else {
  446. for(j=0;j<h;j++) {
  447. memcpy(dest, src, w);
  448. dest += dest_wrap;
  449. src += src_wrap;
  450. }
  451. s->new_picture[i] = s->current_picture[i];
  452. }
  453. }
  454. encode_picture(s, s->picture_number);
  455. MPV_frame_end(s);
  456. s->picture_number++;
  457. if (s->out_format == FMT_MJPEG)
  458. mjpeg_picture_trailer(s);
  459. flush_put_bits(&s->pb);
  460. s->last_frame_bits= s->frame_bits;
  461. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  462. s->total_bits += s->frame_bits;
  463. avctx->quality = s->qscale;
  464. if (avctx->get_psnr) {
  465. /* At this point pict->data should have the original frame */
  466. /* an s->current_picture should have the coded/decoded frame */
  467. get_psnr(pict->data, s->current_picture,
  468. pict->linesize, s->linesize, avctx);
  469. }
  470. return pbBufPtr(&s->pb) - s->pb.buf;
  471. }
  472. static inline int clip(int a, int amin, int amax)
  473. {
  474. if (a < amin)
  475. return amin;
  476. else if (a > amax)
  477. return amax;
  478. else
  479. return a;
  480. }
  481. static inline void gmc1_motion(MpegEncContext *s,
  482. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  483. int dest_offset,
  484. UINT8 **ref_picture, int src_offset,
  485. int h)
  486. {
  487. UINT8 *ptr;
  488. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  489. int motion_x, motion_y;
  490. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  491. motion_x= s->sprite_offset[0][0];
  492. motion_y= s->sprite_offset[0][1];
  493. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  494. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  495. motion_x<<=(3-s->sprite_warping_accuracy);
  496. motion_y<<=(3-s->sprite_warping_accuracy);
  497. src_x = clip(src_x, -16, s->width);
  498. if (src_x == s->width)
  499. motion_x =0;
  500. src_y = clip(src_y, -16, s->height);
  501. if (src_y == s->height)
  502. motion_y =0;
  503. linesize = s->linesize;
  504. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  505. dest_y+=dest_offset;
  506. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  507. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  508. motion_x= s->sprite_offset[1][0];
  509. motion_y= s->sprite_offset[1][1];
  510. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  511. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  512. motion_x<<=(3-s->sprite_warping_accuracy);
  513. motion_y<<=(3-s->sprite_warping_accuracy);
  514. src_x = clip(src_x, -8, s->width>>1);
  515. if (src_x == s->width>>1)
  516. motion_x =0;
  517. src_y = clip(src_y, -8, s->height>>1);
  518. if (src_y == s->height>>1)
  519. motion_y =0;
  520. offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
  521. ptr = ref_picture[1] + offset;
  522. gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  523. ptr = ref_picture[2] + offset;
  524. gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  525. return;
  526. }
  527. /* apply one mpeg motion vector to the three components */
  528. static inline void mpeg_motion(MpegEncContext *s,
  529. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  530. int dest_offset,
  531. UINT8 **ref_picture, int src_offset,
  532. int field_based, op_pixels_func *pix_op,
  533. int motion_x, int motion_y, int h)
  534. {
  535. UINT8 *ptr;
  536. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  537. if(s->quarter_sample)
  538. {
  539. motion_x>>=1;
  540. motion_y>>=1;
  541. }
  542. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  543. src_x = s->mb_x * 16 + (motion_x >> 1);
  544. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  545. /* WARNING: do no forget half pels */
  546. height = s->height >> field_based;
  547. src_x = clip(src_x, -16, s->width);
  548. if (src_x == s->width)
  549. dxy &= ~1;
  550. src_y = clip(src_y, -16, height);
  551. if (src_y == height)
  552. dxy &= ~2;
  553. linesize = s->linesize << field_based;
  554. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  555. dest_y += dest_offset;
  556. pix_op[dxy](dest_y, ptr, linesize, h);
  557. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  558. if (s->out_format == FMT_H263) {
  559. dxy = 0;
  560. if ((motion_x & 3) != 0)
  561. dxy |= 1;
  562. if ((motion_y & 3) != 0)
  563. dxy |= 2;
  564. mx = motion_x >> 2;
  565. my = motion_y >> 2;
  566. } else {
  567. mx = motion_x / 2;
  568. my = motion_y / 2;
  569. dxy = ((my & 1) << 1) | (mx & 1);
  570. mx >>= 1;
  571. my >>= 1;
  572. }
  573. src_x = s->mb_x * 8 + mx;
  574. src_y = s->mb_y * (8 >> field_based) + my;
  575. src_x = clip(src_x, -8, s->width >> 1);
  576. if (src_x == (s->width >> 1))
  577. dxy &= ~1;
  578. src_y = clip(src_y, -8, height >> 1);
  579. if (src_y == (height >> 1))
  580. dxy &= ~2;
  581. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  582. ptr = ref_picture[1] + offset;
  583. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  584. ptr = ref_picture[2] + offset;
  585. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  586. }
  587. static inline void qpel_motion(MpegEncContext *s,
  588. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  589. int dest_offset,
  590. UINT8 **ref_picture, int src_offset,
  591. int field_based, op_pixels_func *pix_op,
  592. qpel_mc_func *qpix_op,
  593. int motion_x, int motion_y, int h)
  594. {
  595. UINT8 *ptr;
  596. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  597. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  598. src_x = s->mb_x * 16 + (motion_x >> 2);
  599. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  600. height = s->height >> field_based;
  601. src_x = clip(src_x, -16, s->width);
  602. if (src_x == s->width)
  603. dxy &= ~3;
  604. src_y = clip(src_y, -16, height);
  605. if (src_y == height)
  606. dxy &= ~12;
  607. linesize = s->linesize << field_based;
  608. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  609. dest_y += dest_offset;
  610. //printf("%d %d %d\n", src_x, src_y, dxy);
  611. qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
  612. qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
  613. qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
  614. qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
  615. mx= (motion_x>>1) | (motion_x&1);
  616. my= (motion_y>>1) | (motion_y&1);
  617. dxy = 0;
  618. if ((mx & 3) != 0)
  619. dxy |= 1;
  620. if ((my & 3) != 0)
  621. dxy |= 2;
  622. mx = mx >> 2;
  623. my = my >> 2;
  624. src_x = s->mb_x * 8 + mx;
  625. src_y = s->mb_y * (8 >> field_based) + my;
  626. src_x = clip(src_x, -8, s->width >> 1);
  627. if (src_x == (s->width >> 1))
  628. dxy &= ~1;
  629. src_y = clip(src_y, -8, height >> 1);
  630. if (src_y == (height >> 1))
  631. dxy &= ~2;
  632. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  633. ptr = ref_picture[1] + offset;
  634. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  635. ptr = ref_picture[2] + offset;
  636. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  637. }
  638. static inline void MPV_motion(MpegEncContext *s,
  639. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  640. int dir, UINT8 **ref_picture,
  641. op_pixels_func *pix_op, qpel_mc_func *qpix_op)
  642. {
  643. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  644. int mb_x, mb_y, i;
  645. UINT8 *ptr, *dest;
  646. mb_x = s->mb_x;
  647. mb_y = s->mb_y;
  648. switch(s->mv_type) {
  649. case MV_TYPE_16X16:
  650. if(s->mcsel){
  651. #if 0
  652. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  653. ref_picture, 0,
  654. 0, pix_op,
  655. s->sprite_offset[0][0]>>3,
  656. s->sprite_offset[0][1]>>3,
  657. 16);
  658. #else
  659. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  660. ref_picture, 0,
  661. 16);
  662. #endif
  663. }else if(s->quarter_sample && dir==0){ //FIXME
  664. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  665. ref_picture, 0,
  666. 0, pix_op, qpix_op,
  667. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  668. }else{
  669. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  670. ref_picture, 0,
  671. 0, pix_op,
  672. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  673. }
  674. break;
  675. case MV_TYPE_8X8:
  676. for(i=0;i<4;i++) {
  677. motion_x = s->mv[dir][i][0];
  678. motion_y = s->mv[dir][i][1];
  679. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  680. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  681. src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
  682. /* WARNING: do no forget half pels */
  683. src_x = clip(src_x, -16, s->width);
  684. if (src_x == s->width)
  685. dxy &= ~1;
  686. src_y = clip(src_y, -16, s->height);
  687. if (src_y == s->height)
  688. dxy &= ~2;
  689. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  690. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  691. pix_op[dxy](dest, ptr, s->linesize, 8);
  692. }
  693. /* In case of 8X8, we construct a single chroma motion vector
  694. with a special rounding */
  695. mx = 0;
  696. my = 0;
  697. for(i=0;i<4;i++) {
  698. mx += s->mv[dir][i][0];
  699. my += s->mv[dir][i][1];
  700. }
  701. if (mx >= 0)
  702. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  703. else {
  704. mx = -mx;
  705. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  706. }
  707. if (my >= 0)
  708. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  709. else {
  710. my = -my;
  711. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  712. }
  713. dxy = ((my & 1) << 1) | (mx & 1);
  714. mx >>= 1;
  715. my >>= 1;
  716. src_x = mb_x * 8 + mx;
  717. src_y = mb_y * 8 + my;
  718. src_x = clip(src_x, -8, s->width/2);
  719. if (src_x == s->width/2)
  720. dxy &= ~1;
  721. src_y = clip(src_y, -8, s->height/2);
  722. if (src_y == s->height/2)
  723. dxy &= ~2;
  724. offset = (src_y * (s->linesize >> 1)) + src_x;
  725. ptr = ref_picture[1] + offset;
  726. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  727. ptr = ref_picture[2] + offset;
  728. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  729. break;
  730. case MV_TYPE_FIELD:
  731. if (s->picture_structure == PICT_FRAME) {
  732. /* top field */
  733. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  734. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  735. 1, pix_op,
  736. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  737. /* bottom field */
  738. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  739. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  740. 1, pix_op,
  741. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  742. } else {
  743. }
  744. break;
  745. }
  746. }
  747. /* put block[] to dest[] */
  748. static inline void put_dct(MpegEncContext *s,
  749. DCTELEM *block, int i, UINT8 *dest, int line_size)
  750. {
  751. if (!s->mpeg2)
  752. s->dct_unquantize(s, block, i, s->qscale);
  753. ff_idct (block);
  754. put_pixels_clamped(block, dest, line_size);
  755. }
  756. /* add block[] to dest[] */
  757. static inline void add_dct(MpegEncContext *s,
  758. DCTELEM *block, int i, UINT8 *dest, int line_size)
  759. {
  760. if (s->block_last_index[i] >= 0) {
  761. if (!s->mpeg2)
  762. if(s->encoding || (!s->h263_msmpeg4))
  763. s->dct_unquantize(s, block, i, s->qscale);
  764. ff_idct (block);
  765. add_pixels_clamped(block, dest, line_size);
  766. }
  767. }
  768. /* generic function called after a macroblock has been parsed by the
  769. decoder or after it has been encoded by the encoder.
  770. Important variables used:
  771. s->mb_intra : true if intra macroblock
  772. s->mv_dir : motion vector direction
  773. s->mv_type : motion vector type
  774. s->mv : motion vector
  775. s->interlaced_dct : true if interlaced dct used (mpeg2)
  776. */
  777. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  778. {
  779. int mb_x, mb_y;
  780. int dct_linesize, dct_offset;
  781. op_pixels_func *op_pix;
  782. qpel_mc_func *op_qpix;
  783. mb_x = s->mb_x;
  784. mb_y = s->mb_y;
  785. #ifdef FF_POSTPROCESS
  786. quant_store[mb_y][mb_x]=s->qscale;
  787. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  788. #endif
  789. /* update DC predictors for P macroblocks */
  790. if (!s->mb_intra) {
  791. if (s->h263_pred || s->h263_aic) {
  792. if(s->mbintra_table[mb_x + mb_y*s->mb_width])
  793. {
  794. int wrap, xy, v;
  795. s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
  796. wrap = 2 * s->mb_width + 2;
  797. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  798. v = 1024;
  799. s->dc_val[0][xy] = v;
  800. s->dc_val[0][xy + 1] = v;
  801. s->dc_val[0][xy + wrap] = v;
  802. s->dc_val[0][xy + 1 + wrap] = v;
  803. /* ac pred */
  804. memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
  805. memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
  806. memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
  807. memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
  808. if (s->h263_msmpeg4) {
  809. s->coded_block[xy] = 0;
  810. s->coded_block[xy + 1] = 0;
  811. s->coded_block[xy + wrap] = 0;
  812. s->coded_block[xy + 1 + wrap] = 0;
  813. }
  814. /* chroma */
  815. wrap = s->mb_width + 2;
  816. xy = mb_x + 1 + (mb_y + 1) * wrap;
  817. s->dc_val[1][xy] = v;
  818. s->dc_val[2][xy] = v;
  819. /* ac pred */
  820. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  821. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  822. }
  823. } else {
  824. s->last_dc[0] = 128 << s->intra_dc_precision;
  825. s->last_dc[1] = 128 << s->intra_dc_precision;
  826. s->last_dc[2] = 128 << s->intra_dc_precision;
  827. }
  828. }
  829. else if (s->h263_pred || s->h263_aic)
  830. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  831. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  832. if (s->out_format == FMT_H263) {
  833. if(s->pict_type!=B_TYPE){
  834. int xy, wrap, motion_x, motion_y;
  835. wrap = 2 * s->mb_width + 2;
  836. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  837. if (s->mb_intra) {
  838. motion_x = 0;
  839. motion_y = 0;
  840. goto motion_init;
  841. } else if (s->mv_type == MV_TYPE_16X16) {
  842. motion_x = s->mv[0][0][0];
  843. motion_y = s->mv[0][0][1];
  844. motion_init:
  845. /* no update if 8X8 because it has been done during parsing */
  846. s->motion_val[xy][0] = motion_x;
  847. s->motion_val[xy][1] = motion_y;
  848. s->motion_val[xy + 1][0] = motion_x;
  849. s->motion_val[xy + 1][1] = motion_y;
  850. s->motion_val[xy + wrap][0] = motion_x;
  851. s->motion_val[xy + wrap][1] = motion_y;
  852. s->motion_val[xy + 1 + wrap][0] = motion_x;
  853. s->motion_val[xy + 1 + wrap][1] = motion_y;
  854. }
  855. }
  856. }
  857. if (!s->intra_only) {
  858. UINT8 *dest_y, *dest_cb, *dest_cr;
  859. UINT8 *mbskip_ptr;
  860. /* avoid copy if macroblock skipped in last frame too */
  861. if (!s->encoding && s->pict_type != B_TYPE) {
  862. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  863. if (s->mb_skiped) {
  864. s->mb_skiped = 0;
  865. /* if previous was skipped too, then nothing to do ! */
  866. if (*mbskip_ptr != 0)
  867. goto the_end;
  868. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  869. } else {
  870. *mbskip_ptr = 0; /* not skipped */
  871. }
  872. }
  873. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  874. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  875. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  876. if (s->interlaced_dct) {
  877. dct_linesize = s->linesize * 2;
  878. dct_offset = s->linesize;
  879. } else {
  880. dct_linesize = s->linesize;
  881. dct_offset = s->linesize * 8;
  882. }
  883. if (!s->mb_intra) {
  884. /* motion handling */
  885. if (!s->no_rounding){
  886. op_pix = put_pixels_tab;
  887. op_qpix= qpel_mc_rnd_tab;
  888. }else{
  889. op_pix = put_no_rnd_pixels_tab;
  890. op_qpix= qpel_mc_no_rnd_tab;
  891. }
  892. if (s->mv_dir & MV_DIR_FORWARD) {
  893. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  894. if (!s->no_rounding)
  895. op_pix = avg_pixels_tab;
  896. else
  897. op_pix = avg_no_rnd_pixels_tab;
  898. }
  899. if (s->mv_dir & MV_DIR_BACKWARD) {
  900. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  901. }
  902. /* add dct residue */
  903. add_dct(s, block[0], 0, dest_y, dct_linesize);
  904. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  905. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  906. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  907. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  908. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  909. } else {
  910. /* dct only in intra block */
  911. put_dct(s, block[0], 0, dest_y, dct_linesize);
  912. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  913. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  914. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  915. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  916. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  917. }
  918. }
  919. the_end:
  920. emms_c();
  921. }
  922. static void encode_picture(MpegEncContext *s, int picture_number)
  923. {
  924. int mb_x, mb_y, wrap, last_gob, pdif = 0;
  925. UINT8 *ptr;
  926. int i, motion_x, motion_y;
  927. s->picture_number = picture_number;
  928. s->last_mc_mb_var = s->mc_mb_var;
  929. /* Reset the average MB variance */
  930. s->avg_mb_var = 0;
  931. s->mc_mb_var = 0;
  932. /* Estimate motion for every MB */
  933. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  934. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  935. int xy= mb_y * s->mb_width + mb_x;
  936. s->mb_x = mb_x;
  937. s->mb_y = mb_y;
  938. /* compute motion vector and macro block type (intra or non intra) */
  939. motion_x = 0;
  940. motion_y = 0;
  941. if (s->pict_type == P_TYPE) {
  942. s->mb_intra = estimate_motion(s, mb_x, mb_y,
  943. &motion_x,
  944. &motion_y);
  945. } else {
  946. s->mb_intra = 1;
  947. }
  948. /* Store MB type and MV */
  949. s->mb_type[xy] = s->mb_intra;
  950. s->mv_table[0][xy] = motion_x;
  951. s->mv_table[1][xy] = motion_y;
  952. }
  953. }
  954. if (!s->fixed_qscale)
  955. s->qscale = rate_estimate_qscale(s);
  956. /* precompute matrix */
  957. if (s->out_format == FMT_MJPEG) {
  958. /* for mjpeg, we do include qscale in the matrix */
  959. s->intra_matrix[0] = default_intra_matrix[0];
  960. for(i=1;i<64;i++)
  961. s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
  962. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
  963. } else {
  964. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
  965. convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
  966. }
  967. switch(s->out_format) {
  968. case FMT_MJPEG:
  969. mjpeg_picture_header(s);
  970. break;
  971. case FMT_H263:
  972. if (s->h263_msmpeg4)
  973. msmpeg4_encode_picture_header(s, picture_number);
  974. else if (s->h263_pred)
  975. mpeg4_encode_picture_header(s, picture_number);
  976. else if (s->h263_rv10)
  977. rv10_encode_picture_header(s, picture_number);
  978. else
  979. h263_encode_picture_header(s, picture_number);
  980. break;
  981. case FMT_MPEG1:
  982. mpeg1_encode_picture_header(s, picture_number);
  983. break;
  984. }
  985. /* init last dc values */
  986. /* note: quant matrix value (8) is implied here */
  987. s->last_dc[0] = 128;
  988. s->last_dc[1] = 128;
  989. s->last_dc[2] = 128;
  990. s->mb_incr = 1;
  991. s->last_mv[0][0][0] = 0;
  992. s->last_mv[0][0][1] = 0;
  993. s->mv_type = MV_TYPE_16X16;
  994. s->mv_dir = MV_DIR_FORWARD;
  995. /* Get the GOB height based on picture height */
  996. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  997. if (s->height <= 400)
  998. s->gob_index = 1;
  999. else if (s->height <= 800)
  1000. s->gob_index = 2;
  1001. else
  1002. s->gob_index = 4;
  1003. }
  1004. s->avg_mb_var = s->avg_mb_var / s->mb_num;
  1005. s->block_wrap[0]=
  1006. s->block_wrap[1]=
  1007. s->block_wrap[2]=
  1008. s->block_wrap[3]= s->mb_width*2 + 2;
  1009. s->block_wrap[4]=
  1010. s->block_wrap[5]= s->mb_width + 2;
  1011. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1012. /* Put GOB header based on RTP MTU */
  1013. /* TODO: Put all this stuff in a separate generic function */
  1014. if (s->rtp_mode) {
  1015. if (!mb_y) {
  1016. s->ptr_lastgob = s->pb.buf;
  1017. s->ptr_last_mb_line = s->pb.buf;
  1018. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1019. last_gob = h263_encode_gob_header(s, mb_y);
  1020. if (last_gob) {
  1021. s->first_gob_line = 1;
  1022. }
  1023. }
  1024. }
  1025. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1026. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1027. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1028. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1029. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1030. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1031. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1032. s->mb_x = mb_x;
  1033. s->mb_y = mb_y;
  1034. s->block_index[0]+=2;
  1035. s->block_index[1]+=2;
  1036. s->block_index[2]+=2;
  1037. s->block_index[3]+=2;
  1038. s->block_index[4]++;
  1039. s->block_index[5]++;
  1040. #if 0
  1041. /* compute motion vector and macro block type (intra or non intra) */
  1042. motion_x = 0;
  1043. motion_y = 0;
  1044. if (s->pict_type == P_TYPE) {
  1045. s->mb_intra = estimate_motion(s, mb_x, mb_y,
  1046. &motion_x,
  1047. &motion_y);
  1048. } else {
  1049. s->mb_intra = 1;
  1050. }
  1051. #endif
  1052. s->mb_intra = s->mb_type[mb_y * s->mb_width + mb_x];
  1053. motion_x = s->mv_table[0][mb_y * s->mb_width + mb_x];
  1054. motion_y = s->mv_table[1][mb_y * s->mb_width + mb_x];
  1055. /* get the pixels */
  1056. wrap = s->linesize;
  1057. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1058. get_pixels(s->block[0], ptr, wrap);
  1059. get_pixels(s->block[1], ptr + 8, wrap);
  1060. get_pixels(s->block[2], ptr + 8 * wrap, wrap);
  1061. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1062. wrap = s->linesize >> 1;
  1063. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1064. get_pixels(s->block[4], ptr, wrap);
  1065. wrap = s->linesize >> 1;
  1066. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1067. get_pixels(s->block[5], ptr, wrap);
  1068. /* subtract previous frame if non intra */
  1069. if (!s->mb_intra) {
  1070. int dxy, offset, mx, my;
  1071. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1072. ptr = s->last_picture[0] +
  1073. ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
  1074. (mb_x * 16 + (motion_x >> 1));
  1075. sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
  1076. sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
  1077. sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
  1078. sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
  1079. if (s->out_format == FMT_H263) {
  1080. /* special rounding for h263 */
  1081. dxy = 0;
  1082. if ((motion_x & 3) != 0)
  1083. dxy |= 1;
  1084. if ((motion_y & 3) != 0)
  1085. dxy |= 2;
  1086. mx = motion_x >> 2;
  1087. my = motion_y >> 2;
  1088. } else {
  1089. mx = motion_x / 2;
  1090. my = motion_y / 2;
  1091. dxy = ((my & 1) << 1) | (mx & 1);
  1092. mx >>= 1;
  1093. my >>= 1;
  1094. }
  1095. offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
  1096. ptr = s->last_picture[1] + offset;
  1097. sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
  1098. ptr = s->last_picture[2] + offset;
  1099. sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
  1100. }
  1101. emms_c();
  1102. #if 0
  1103. {
  1104. float adap_parm;
  1105. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1106. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1107. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1108. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1109. s->qscale, adap_parm, s->qscale*adap_parm,
  1110. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1111. }
  1112. #endif
  1113. /* DCT & quantize */
  1114. if (s->h263_msmpeg4) {
  1115. msmpeg4_dc_scale(s);
  1116. } else if (s->h263_pred) {
  1117. h263_dc_scale(s);
  1118. } else {
  1119. /* default quantization values */
  1120. s->y_dc_scale = 8;
  1121. s->c_dc_scale = 8;
  1122. }
  1123. for(i=0;i<6;i++) {
  1124. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
  1125. }
  1126. /* huffman encode */
  1127. switch(s->out_format) {
  1128. case FMT_MPEG1:
  1129. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1130. break;
  1131. case FMT_H263:
  1132. if (s->h263_msmpeg4)
  1133. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1134. else if(s->h263_pred)
  1135. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1136. else
  1137. h263_encode_mb(s, s->block, motion_x, motion_y);
  1138. break;
  1139. case FMT_MJPEG:
  1140. mjpeg_encode_mb(s, s->block);
  1141. break;
  1142. }
  1143. /* decompress blocks so that we keep the state of the decoder */
  1144. s->mv[0][0][0] = motion_x;
  1145. s->mv[0][0][1] = motion_y;
  1146. MPV_decode_mb(s, s->block);
  1147. }
  1148. /* Obtain average GOB size for RTP */
  1149. if (s->rtp_mode) {
  1150. if (!mb_y)
  1151. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  1152. else if (!(mb_y % s->gob_index)) {
  1153. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  1154. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  1155. }
  1156. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  1157. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  1158. s->first_gob_line = 0;
  1159. }
  1160. }
  1161. if (s->h263_msmpeg4 && s->pict_type == I_TYPE)
  1162. msmpeg4_encode_ext_header(s);
  1163. //if (s->gob_number)
  1164. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  1165. /* Send the last GOB if RTP */
  1166. if (s->rtp_mode) {
  1167. flush_put_bits(&s->pb);
  1168. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1169. /* Call the RTP callback to send the last GOB */
  1170. if (s->rtp_callback)
  1171. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  1172. s->ptr_lastgob = pbBufPtr(&s->pb);
  1173. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  1174. }
  1175. }
  1176. static int dct_quantize_c(MpegEncContext *s,
  1177. DCTELEM *block, int n,
  1178. int qscale)
  1179. {
  1180. int i, j, level, last_non_zero, q;
  1181. const int *qmat;
  1182. int minLevel, maxLevel;
  1183. if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
  1184. /* mpeg4 */
  1185. minLevel= -2048;
  1186. maxLevel= 2047;
  1187. }else if(s->out_format==FMT_MPEG1){
  1188. /* mpeg1 */
  1189. minLevel= -255;
  1190. maxLevel= 255;
  1191. }else if(s->out_format==FMT_MJPEG){
  1192. /* (m)jpeg */
  1193. minLevel= -1023;
  1194. maxLevel= 1023;
  1195. }else{
  1196. /* h263 / msmpeg4 */
  1197. minLevel= -128;
  1198. maxLevel= 127;
  1199. }
  1200. av_fdct (block);
  1201. /* we need this permutation so that we correct the IDCT
  1202. permutation. will be moved into DCT code */
  1203. block_permute(block);
  1204. if (s->mb_intra) {
  1205. if (n < 4)
  1206. q = s->y_dc_scale;
  1207. else
  1208. q = s->c_dc_scale;
  1209. q = q << 3;
  1210. /* note: block[0] is assumed to be positive */
  1211. block[0] = (block[0] + (q >> 1)) / q;
  1212. i = 1;
  1213. last_non_zero = 0;
  1214. if (s->out_format == FMT_H263) {
  1215. qmat = s->q_non_intra_matrix;
  1216. } else {
  1217. qmat = s->q_intra_matrix;
  1218. }
  1219. } else {
  1220. i = 0;
  1221. last_non_zero = -1;
  1222. qmat = s->q_non_intra_matrix;
  1223. }
  1224. for(;i<64;i++) {
  1225. j = zigzag_direct[i];
  1226. level = block[j];
  1227. level = level * qmat[j];
  1228. #ifdef PARANOID
  1229. {
  1230. static int count = 0;
  1231. int level1, level2, qmat1;
  1232. double val;
  1233. if (qmat == s->q_non_intra_matrix) {
  1234. qmat1 = default_non_intra_matrix[j] * s->qscale;
  1235. } else {
  1236. qmat1 = default_intra_matrix[j] * s->qscale;
  1237. }
  1238. if (av_fdct != jpeg_fdct_ifast)
  1239. val = ((double)block[j] * 8.0) / (double)qmat1;
  1240. else
  1241. val = ((double)block[j] * 8.0 * 2048.0) /
  1242. ((double)qmat1 * aanscales[j]);
  1243. level1 = (int)val;
  1244. level2 = level / (1 << (QMAT_SHIFT - 3));
  1245. if (level1 != level2) {
  1246. fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
  1247. count, level2, level1, block[j], qmat1, qmat[j],
  1248. val);
  1249. count++;
  1250. }
  1251. }
  1252. #endif
  1253. /* XXX: slight error for the low range. Test should be equivalent to
  1254. (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
  1255. (QMAT_SHIFT - 3)))
  1256. */
  1257. if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
  1258. level) {
  1259. level = level / (1 << (QMAT_SHIFT - 3));
  1260. /* XXX: currently, this code is not optimal. the range should be:
  1261. mpeg1: -255..255
  1262. mpeg2: -2048..2047
  1263. h263: -128..127
  1264. mpeg4: -2048..2047
  1265. */
  1266. if (level > maxLevel)
  1267. level = maxLevel;
  1268. else if (level < minLevel)
  1269. level = minLevel;
  1270. block[j] = level;
  1271. last_non_zero = i;
  1272. } else {
  1273. block[j] = 0;
  1274. }
  1275. }
  1276. return last_non_zero;
  1277. }
  1278. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1279. DCTELEM *block, int n, int qscale)
  1280. {
  1281. int i, level, nCoeffs;
  1282. const UINT16 *quant_matrix;
  1283. if(s->alternate_scan) nCoeffs= 64;
  1284. else nCoeffs= s->block_last_index[n]+1;
  1285. if (s->mb_intra) {
  1286. if (n < 4)
  1287. block[0] = block[0] * s->y_dc_scale;
  1288. else
  1289. block[0] = block[0] * s->c_dc_scale;
  1290. /* XXX: only mpeg1 */
  1291. quant_matrix = s->intra_matrix;
  1292. for(i=1;i<nCoeffs;i++) {
  1293. int j= zigzag_direct[i];
  1294. level = block[j];
  1295. if (level) {
  1296. if (level < 0) {
  1297. level = -level;
  1298. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1299. level = (level - 1) | 1;
  1300. level = -level;
  1301. } else {
  1302. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1303. level = (level - 1) | 1;
  1304. }
  1305. #ifdef PARANOID
  1306. if (level < -2048 || level > 2047)
  1307. fprintf(stderr, "unquant error %d %d\n", i, level);
  1308. #endif
  1309. block[j] = level;
  1310. }
  1311. }
  1312. } else {
  1313. i = 0;
  1314. quant_matrix = s->non_intra_matrix;
  1315. for(;i<nCoeffs;i++) {
  1316. int j= zigzag_direct[i];
  1317. level = block[j];
  1318. if (level) {
  1319. if (level < 0) {
  1320. level = -level;
  1321. level = (((level << 1) + 1) * qscale *
  1322. ((int) (quant_matrix[j]))) >> 4;
  1323. level = (level - 1) | 1;
  1324. level = -level;
  1325. } else {
  1326. level = (((level << 1) + 1) * qscale *
  1327. ((int) (quant_matrix[j]))) >> 4;
  1328. level = (level - 1) | 1;
  1329. }
  1330. #ifdef PARANOID
  1331. if (level < -2048 || level > 2047)
  1332. fprintf(stderr, "unquant error %d %d\n", i, level);
  1333. #endif
  1334. block[j] = level;
  1335. }
  1336. }
  1337. }
  1338. }
  1339. static void dct_unquantize_h263_c(MpegEncContext *s,
  1340. DCTELEM *block, int n, int qscale)
  1341. {
  1342. int i, level, qmul, qadd;
  1343. int nCoeffs;
  1344. if (s->mb_intra) {
  1345. if (!s->h263_aic) {
  1346. if (n < 4)
  1347. block[0] = block[0] * s->y_dc_scale;
  1348. else
  1349. block[0] = block[0] * s->c_dc_scale;
  1350. }
  1351. i = 1;
  1352. nCoeffs= 64; //does not allways use zigzag table
  1353. } else {
  1354. i = 0;
  1355. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  1356. }
  1357. qmul = s->qscale << 1;
  1358. if (s->h263_aic && s->mb_intra)
  1359. qadd = 0;
  1360. else
  1361. qadd = (s->qscale - 1) | 1;
  1362. for(;i<nCoeffs;i++) {
  1363. level = block[i];
  1364. if (level) {
  1365. if (level < 0) {
  1366. level = level * qmul - qadd;
  1367. } else {
  1368. level = level * qmul + qadd;
  1369. }
  1370. #ifdef PARANOID
  1371. if (level < -2048 || level > 2047)
  1372. fprintf(stderr, "unquant error %d %d\n", i, level);
  1373. #endif
  1374. block[i] = level;
  1375. }
  1376. }
  1377. }
  1378. /* rate control */
  1379. /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
  1380. #define I_FRAME_SIZE_RATIO 3.0
  1381. #define QSCALE_K 20
  1382. static void rate_control_init(MpegEncContext *s)
  1383. {
  1384. #if 1
  1385. emms_c();
  1386. //initial values, they dont really matter as they will be totally different within a few frames
  1387. s->i_pred.coeff= s->p_pred.coeff= 7.0;
  1388. s->i_pred.count= s->p_pred.count= 1.0;
  1389. s->i_pred.decay= s->p_pred.decay= 0.4;
  1390. // use more bits at the beginning, otherwise high motion at the begin will look like shit
  1391. s->qsum=100;
  1392. s->qcount=100;
  1393. s->short_term_qsum=0.001;
  1394. s->short_term_qcount=0.001;
  1395. #else
  1396. s->wanted_bits = 0;
  1397. if (s->intra_only) {
  1398. s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
  1399. s->P_frame_bits = s->I_frame_bits;
  1400. } else {
  1401. s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
  1402. (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
  1403. s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
  1404. }
  1405. #if defined(DEBUG)
  1406. printf("I_frame_size=%d P_frame_size=%d\n",
  1407. s->I_frame_bits, s->P_frame_bits);
  1408. #endif
  1409. #endif
  1410. }
  1411. static double predict(Predictor *p, double q, double var)
  1412. {
  1413. return p->coeff*var / (q*p->count);
  1414. }
  1415. static void update_predictor(Predictor *p, double q, double var, double size)
  1416. {
  1417. double new_coeff= size*q / (var + 1);
  1418. if(var<1000) return;
  1419. /*{
  1420. int pred= predict(p, q, var);
  1421. int error= abs(pred-size);
  1422. static double sum=0;
  1423. static int count=0;
  1424. if(count>5) sum+=error;
  1425. count++;
  1426. if(256*256*256*64%count==0){
  1427. printf("%d %f %f\n", count, sum/count, p->coeff);
  1428. }
  1429. }*/
  1430. p->count*= p->decay;
  1431. p->coeff*= p->decay;
  1432. p->count++;
  1433. p->coeff+= new_coeff;
  1434. }
  1435. static int rate_estimate_qscale(MpegEncContext *s)
  1436. {
  1437. #if 1
  1438. int qmin= s->qmin;
  1439. int qmax= s->qmax;
  1440. int rate_q=5;
  1441. float q;
  1442. int qscale;
  1443. float br_compensation;
  1444. double diff;
  1445. double short_term_q;
  1446. double long_term_q;
  1447. int last_qscale= s->qscale;
  1448. double fps;
  1449. INT64 wanted_bits;
  1450. emms_c();
  1451. fps= (double)s->frame_rate / FRAME_RATE_BASE;
  1452. wanted_bits= s->bit_rate*(double)s->picture_number/fps;
  1453. if(s->picture_number>2){
  1454. /* update predictors */
  1455. if(s->last_pict_type == I_TYPE){
  1456. //FIXME
  1457. }else{ //P Frame
  1458. //printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);
  1459. update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits);
  1460. }
  1461. }
  1462. if(s->pict_type == I_TYPE){
  1463. //FIXME
  1464. rate_q= s->qsum/s->qcount;
  1465. }else{ //P Frame
  1466. int i;
  1467. int diff, best_diff=1000000000;
  1468. for(i=1; i<=31; i++){
  1469. diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps;
  1470. if(diff<0) diff= -diff;
  1471. if(diff<best_diff){
  1472. best_diff= diff;
  1473. rate_q= i;
  1474. }
  1475. }
  1476. }
  1477. s->short_term_qsum*=s->qblur;
  1478. s->short_term_qcount*=s->qblur;
  1479. s->short_term_qsum+= rate_q;
  1480. s->short_term_qcount++;
  1481. short_term_q= s->short_term_qsum/s->short_term_qcount;
  1482. long_term_q= s->qsum/s->qcount*s->total_bits/wanted_bits;
  1483. // q= (long_term_q - short_term_q)*s->qcompress + short_term_q;
  1484. q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
  1485. diff= s->total_bits - wanted_bits;
  1486. br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
  1487. q/=br_compensation;
  1488. qscale= (int)(q + 0.5);
  1489. if (qscale<qmin) qscale=qmin;
  1490. else if(qscale>qmax) qscale=qmax;
  1491. if (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff;
  1492. else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff;
  1493. s->qsum+= qscale;
  1494. s->qcount++;
  1495. s->last_pict_type= s->pict_type;
  1496. //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%d fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation,
  1497. // rate_q, (int)short_term_q, s->mc_mb_var, s->frame_bits);
  1498. //printf("%d %d\n", s->bit_rate, (int)fps);
  1499. return qscale;
  1500. #else
  1501. INT64 diff, total_bits = s->total_bits;
  1502. float q;
  1503. int qscale;
  1504. if (s->pict_type == I_TYPE) {
  1505. s->wanted_bits += s->I_frame_bits;
  1506. } else {
  1507. s->wanted_bits += s->P_frame_bits;
  1508. }
  1509. diff = s->wanted_bits - total_bits;
  1510. q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
  1511. /* adjust for I frame */
  1512. if (s->pict_type == I_TYPE && !s->intra_only) {
  1513. q /= I_FRAME_SIZE_RATIO;
  1514. }
  1515. /* using a too small Q scale leeds to problems in mpeg1 and h263
  1516. because AC coefficients are clamped to 255 or 127 */
  1517. qmin = 3;
  1518. if (q < qmin)
  1519. q = qmin;
  1520. else if (q > 31)
  1521. q = 31;
  1522. qscale = (int)(q + 0.5);
  1523. #if defined(DEBUG)
  1524. printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n",
  1525. s->picture_number,
  1526. (double)total_bits,
  1527. (double)s->wanted_bits,
  1528. (float)s->frame_rate / FRAME_RATE_BASE *
  1529. total_bits / s->picture_number,
  1530. (int)diff, q);
  1531. #endif
  1532. return qscale;
  1533. #endif
  1534. }
  1535. AVCodec mpeg1video_encoder = {
  1536. "mpeg1video",
  1537. CODEC_TYPE_VIDEO,
  1538. CODEC_ID_MPEG1VIDEO,
  1539. sizeof(MpegEncContext),
  1540. MPV_encode_init,
  1541. MPV_encode_picture,
  1542. MPV_encode_end,
  1543. };
  1544. AVCodec h263_encoder = {
  1545. "h263",
  1546. CODEC_TYPE_VIDEO,
  1547. CODEC_ID_H263,
  1548. sizeof(MpegEncContext),
  1549. MPV_encode_init,
  1550. MPV_encode_picture,
  1551. MPV_encode_end,
  1552. };
  1553. AVCodec h263p_encoder = {
  1554. "h263p",
  1555. CODEC_TYPE_VIDEO,
  1556. CODEC_ID_H263P,
  1557. sizeof(MpegEncContext),
  1558. MPV_encode_init,
  1559. MPV_encode_picture,
  1560. MPV_encode_end,
  1561. };
  1562. AVCodec rv10_encoder = {
  1563. "rv10",
  1564. CODEC_TYPE_VIDEO,
  1565. CODEC_ID_RV10,
  1566. sizeof(MpegEncContext),
  1567. MPV_encode_init,
  1568. MPV_encode_picture,
  1569. MPV_encode_end,
  1570. };
  1571. AVCodec mjpeg_encoder = {
  1572. "mjpeg",
  1573. CODEC_TYPE_VIDEO,
  1574. CODEC_ID_MJPEG,
  1575. sizeof(MpegEncContext),
  1576. MPV_encode_init,
  1577. MPV_encode_picture,
  1578. MPV_encode_end,
  1579. };
  1580. AVCodec mpeg4_encoder = {
  1581. "mpeg4",
  1582. CODEC_TYPE_VIDEO,
  1583. CODEC_ID_MPEG4,
  1584. sizeof(MpegEncContext),
  1585. MPV_encode_init,
  1586. MPV_encode_picture,
  1587. MPV_encode_end,
  1588. };
  1589. AVCodec msmpeg4_encoder = {
  1590. "msmpeg4",
  1591. CODEC_TYPE_VIDEO,
  1592. CODEC_ID_MSMPEG4,
  1593. sizeof(MpegEncContext),
  1594. MPV_encode_init,
  1595. MPV_encode_picture,
  1596. MPV_encode_end,
  1597. };