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.

1814 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. MPV_frame_start(s);
  430. for(i=0;i<3;i++) {
  431. UINT8 *src = pict->data[i];
  432. UINT8 *dest = s->current_picture[i];
  433. int src_wrap = pict->linesize[i];
  434. int dest_wrap = s->linesize;
  435. int w = s->width;
  436. int h = s->height;
  437. if (i >= 1) {
  438. dest_wrap >>= 1;
  439. w >>= 1;
  440. h >>= 1;
  441. }
  442. if(dest_wrap==src_wrap){
  443. s->new_picture[i] = pict->data[i];
  444. } else {
  445. for(j=0;j<h;j++) {
  446. memcpy(dest, src, w);
  447. dest += dest_wrap;
  448. src += src_wrap;
  449. }
  450. s->new_picture[i] = s->current_picture[i];
  451. }
  452. }
  453. encode_picture(s, s->picture_number);
  454. avctx->key_frame = (s->pict_type == I_TYPE);
  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->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE){ //FIXME subtract MV bits
  955. int i;
  956. s->pict_type= I_TYPE;
  957. for(i=0; i<s->mb_height*s->mb_width; i++){
  958. s->mb_type[i] = I_TYPE;
  959. s->mv_table[0][i] = 0;
  960. s->mv_table[1][i] = 0;
  961. }
  962. }
  963. // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
  964. if (!s->fixed_qscale)
  965. s->qscale = rate_estimate_qscale(s);
  966. /* precompute matrix */
  967. if (s->out_format == FMT_MJPEG) {
  968. /* for mjpeg, we do include qscale in the matrix */
  969. s->intra_matrix[0] = default_intra_matrix[0];
  970. for(i=1;i<64;i++)
  971. s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
  972. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
  973. } else {
  974. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
  975. convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
  976. }
  977. switch(s->out_format) {
  978. case FMT_MJPEG:
  979. mjpeg_picture_header(s);
  980. break;
  981. case FMT_H263:
  982. if (s->h263_msmpeg4)
  983. msmpeg4_encode_picture_header(s, picture_number);
  984. else if (s->h263_pred)
  985. mpeg4_encode_picture_header(s, picture_number);
  986. else if (s->h263_rv10)
  987. rv10_encode_picture_header(s, picture_number);
  988. else
  989. h263_encode_picture_header(s, picture_number);
  990. break;
  991. case FMT_MPEG1:
  992. mpeg1_encode_picture_header(s, picture_number);
  993. break;
  994. }
  995. /* init last dc values */
  996. /* note: quant matrix value (8) is implied here */
  997. s->last_dc[0] = 128;
  998. s->last_dc[1] = 128;
  999. s->last_dc[2] = 128;
  1000. s->mb_incr = 1;
  1001. s->last_mv[0][0][0] = 0;
  1002. s->last_mv[0][0][1] = 0;
  1003. s->mv_type = MV_TYPE_16X16;
  1004. s->mv_dir = MV_DIR_FORWARD;
  1005. /* Get the GOB height based on picture height */
  1006. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  1007. if (s->height <= 400)
  1008. s->gob_index = 1;
  1009. else if (s->height <= 800)
  1010. s->gob_index = 2;
  1011. else
  1012. s->gob_index = 4;
  1013. }
  1014. s->avg_mb_var = s->avg_mb_var / s->mb_num;
  1015. s->block_wrap[0]=
  1016. s->block_wrap[1]=
  1017. s->block_wrap[2]=
  1018. s->block_wrap[3]= s->mb_width*2 + 2;
  1019. s->block_wrap[4]=
  1020. s->block_wrap[5]= s->mb_width + 2;
  1021. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1022. /* Put GOB header based on RTP MTU */
  1023. /* TODO: Put all this stuff in a separate generic function */
  1024. if (s->rtp_mode) {
  1025. if (!mb_y) {
  1026. s->ptr_lastgob = s->pb.buf;
  1027. s->ptr_last_mb_line = s->pb.buf;
  1028. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1029. last_gob = h263_encode_gob_header(s, mb_y);
  1030. if (last_gob) {
  1031. s->first_gob_line = 1;
  1032. }
  1033. }
  1034. }
  1035. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1036. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1037. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1038. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1039. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1040. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1041. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1042. s->mb_x = mb_x;
  1043. s->mb_y = mb_y;
  1044. s->block_index[0]+=2;
  1045. s->block_index[1]+=2;
  1046. s->block_index[2]+=2;
  1047. s->block_index[3]+=2;
  1048. s->block_index[4]++;
  1049. s->block_index[5]++;
  1050. #if 0
  1051. /* compute motion vector and macro block type (intra or non intra) */
  1052. motion_x = 0;
  1053. motion_y = 0;
  1054. if (s->pict_type == P_TYPE) {
  1055. s->mb_intra = estimate_motion(s, mb_x, mb_y,
  1056. &motion_x,
  1057. &motion_y);
  1058. } else {
  1059. s->mb_intra = 1;
  1060. }
  1061. #endif
  1062. s->mb_intra = s->mb_type[mb_y * s->mb_width + mb_x];
  1063. motion_x = s->mv_table[0][mb_y * s->mb_width + mb_x];
  1064. motion_y = s->mv_table[1][mb_y * s->mb_width + mb_x];
  1065. /* get the pixels */
  1066. wrap = s->linesize;
  1067. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1068. get_pixels(s->block[0], ptr, wrap);
  1069. get_pixels(s->block[1], ptr + 8, wrap);
  1070. get_pixels(s->block[2], ptr + 8 * wrap, wrap);
  1071. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1072. wrap = s->linesize >> 1;
  1073. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1074. get_pixels(s->block[4], ptr, wrap);
  1075. wrap = s->linesize >> 1;
  1076. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1077. get_pixels(s->block[5], ptr, wrap);
  1078. /* subtract previous frame if non intra */
  1079. if (!s->mb_intra) {
  1080. int dxy, offset, mx, my;
  1081. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1082. ptr = s->last_picture[0] +
  1083. ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
  1084. (mb_x * 16 + (motion_x >> 1));
  1085. sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
  1086. sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
  1087. sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
  1088. sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
  1089. if (s->out_format == FMT_H263) {
  1090. /* special rounding for h263 */
  1091. dxy = 0;
  1092. if ((motion_x & 3) != 0)
  1093. dxy |= 1;
  1094. if ((motion_y & 3) != 0)
  1095. dxy |= 2;
  1096. mx = motion_x >> 2;
  1097. my = motion_y >> 2;
  1098. } else {
  1099. mx = motion_x / 2;
  1100. my = motion_y / 2;
  1101. dxy = ((my & 1) << 1) | (mx & 1);
  1102. mx >>= 1;
  1103. my >>= 1;
  1104. }
  1105. offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
  1106. ptr = s->last_picture[1] + offset;
  1107. sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
  1108. ptr = s->last_picture[2] + offset;
  1109. sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
  1110. }
  1111. emms_c();
  1112. #if 0
  1113. {
  1114. float adap_parm;
  1115. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1116. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1117. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1118. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1119. s->qscale, adap_parm, s->qscale*adap_parm,
  1120. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1121. }
  1122. #endif
  1123. /* DCT & quantize */
  1124. if (s->h263_msmpeg4) {
  1125. msmpeg4_dc_scale(s);
  1126. } else if (s->h263_pred) {
  1127. h263_dc_scale(s);
  1128. } else {
  1129. /* default quantization values */
  1130. s->y_dc_scale = 8;
  1131. s->c_dc_scale = 8;
  1132. }
  1133. for(i=0;i<6;i++) {
  1134. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
  1135. }
  1136. /* huffman encode */
  1137. switch(s->out_format) {
  1138. case FMT_MPEG1:
  1139. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1140. break;
  1141. case FMT_H263:
  1142. if (s->h263_msmpeg4)
  1143. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1144. else if(s->h263_pred)
  1145. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1146. else
  1147. h263_encode_mb(s, s->block, motion_x, motion_y);
  1148. break;
  1149. case FMT_MJPEG:
  1150. mjpeg_encode_mb(s, s->block);
  1151. break;
  1152. }
  1153. /* decompress blocks so that we keep the state of the decoder */
  1154. s->mv[0][0][0] = motion_x;
  1155. s->mv[0][0][1] = motion_y;
  1156. MPV_decode_mb(s, s->block);
  1157. }
  1158. /* Obtain average GOB size for RTP */
  1159. if (s->rtp_mode) {
  1160. if (!mb_y)
  1161. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  1162. else if (!(mb_y % s->gob_index)) {
  1163. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  1164. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  1165. }
  1166. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  1167. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  1168. s->first_gob_line = 0;
  1169. }
  1170. }
  1171. if (s->h263_msmpeg4 && s->pict_type == I_TYPE)
  1172. msmpeg4_encode_ext_header(s);
  1173. //if (s->gob_number)
  1174. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  1175. /* Send the last GOB if RTP */
  1176. if (s->rtp_mode) {
  1177. flush_put_bits(&s->pb);
  1178. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1179. /* Call the RTP callback to send the last GOB */
  1180. if (s->rtp_callback)
  1181. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  1182. s->ptr_lastgob = pbBufPtr(&s->pb);
  1183. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  1184. }
  1185. }
  1186. static int dct_quantize_c(MpegEncContext *s,
  1187. DCTELEM *block, int n,
  1188. int qscale)
  1189. {
  1190. int i, j, level, last_non_zero, q;
  1191. const int *qmat;
  1192. int minLevel, maxLevel;
  1193. if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
  1194. /* mpeg4 */
  1195. minLevel= -2048;
  1196. maxLevel= 2047;
  1197. }else if(s->out_format==FMT_MPEG1){
  1198. /* mpeg1 */
  1199. minLevel= -255;
  1200. maxLevel= 255;
  1201. }else if(s->out_format==FMT_MJPEG){
  1202. /* (m)jpeg */
  1203. minLevel= -1023;
  1204. maxLevel= 1023;
  1205. }else{
  1206. /* h263 / msmpeg4 */
  1207. minLevel= -128;
  1208. maxLevel= 127;
  1209. }
  1210. av_fdct (block);
  1211. /* we need this permutation so that we correct the IDCT
  1212. permutation. will be moved into DCT code */
  1213. block_permute(block);
  1214. if (s->mb_intra) {
  1215. if (n < 4)
  1216. q = s->y_dc_scale;
  1217. else
  1218. q = s->c_dc_scale;
  1219. q = q << 3;
  1220. /* note: block[0] is assumed to be positive */
  1221. block[0] = (block[0] + (q >> 1)) / q;
  1222. i = 1;
  1223. last_non_zero = 0;
  1224. if (s->out_format == FMT_H263) {
  1225. qmat = s->q_non_intra_matrix;
  1226. } else {
  1227. qmat = s->q_intra_matrix;
  1228. }
  1229. } else {
  1230. i = 0;
  1231. last_non_zero = -1;
  1232. qmat = s->q_non_intra_matrix;
  1233. }
  1234. for(;i<64;i++) {
  1235. j = zigzag_direct[i];
  1236. level = block[j];
  1237. level = level * qmat[j];
  1238. #ifdef PARANOID
  1239. {
  1240. static int count = 0;
  1241. int level1, level2, qmat1;
  1242. double val;
  1243. if (qmat == s->q_non_intra_matrix) {
  1244. qmat1 = default_non_intra_matrix[j] * s->qscale;
  1245. } else {
  1246. qmat1 = default_intra_matrix[j] * s->qscale;
  1247. }
  1248. if (av_fdct != jpeg_fdct_ifast)
  1249. val = ((double)block[j] * 8.0) / (double)qmat1;
  1250. else
  1251. val = ((double)block[j] * 8.0 * 2048.0) /
  1252. ((double)qmat1 * aanscales[j]);
  1253. level1 = (int)val;
  1254. level2 = level / (1 << (QMAT_SHIFT - 3));
  1255. if (level1 != level2) {
  1256. fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
  1257. count, level2, level1, block[j], qmat1, qmat[j],
  1258. val);
  1259. count++;
  1260. }
  1261. }
  1262. #endif
  1263. /* XXX: slight error for the low range. Test should be equivalent to
  1264. (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
  1265. (QMAT_SHIFT - 3)))
  1266. */
  1267. if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
  1268. level) {
  1269. level = level / (1 << (QMAT_SHIFT - 3));
  1270. /* XXX: currently, this code is not optimal. the range should be:
  1271. mpeg1: -255..255
  1272. mpeg2: -2048..2047
  1273. h263: -128..127
  1274. mpeg4: -2048..2047
  1275. */
  1276. if (level > maxLevel)
  1277. level = maxLevel;
  1278. else if (level < minLevel)
  1279. level = minLevel;
  1280. block[j] = level;
  1281. last_non_zero = i;
  1282. } else {
  1283. block[j] = 0;
  1284. }
  1285. }
  1286. return last_non_zero;
  1287. }
  1288. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1289. DCTELEM *block, int n, int qscale)
  1290. {
  1291. int i, level, nCoeffs;
  1292. const UINT16 *quant_matrix;
  1293. if(s->alternate_scan) nCoeffs= 64;
  1294. else nCoeffs= s->block_last_index[n]+1;
  1295. if (s->mb_intra) {
  1296. if (n < 4)
  1297. block[0] = block[0] * s->y_dc_scale;
  1298. else
  1299. block[0] = block[0] * s->c_dc_scale;
  1300. /* XXX: only mpeg1 */
  1301. quant_matrix = s->intra_matrix;
  1302. for(i=1;i<nCoeffs;i++) {
  1303. int j= zigzag_direct[i];
  1304. level = block[j];
  1305. if (level) {
  1306. if (level < 0) {
  1307. level = -level;
  1308. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1309. level = (level - 1) | 1;
  1310. level = -level;
  1311. } else {
  1312. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1313. level = (level - 1) | 1;
  1314. }
  1315. #ifdef PARANOID
  1316. if (level < -2048 || level > 2047)
  1317. fprintf(stderr, "unquant error %d %d\n", i, level);
  1318. #endif
  1319. block[j] = level;
  1320. }
  1321. }
  1322. } else {
  1323. i = 0;
  1324. quant_matrix = s->non_intra_matrix;
  1325. for(;i<nCoeffs;i++) {
  1326. int j= zigzag_direct[i];
  1327. level = block[j];
  1328. if (level) {
  1329. if (level < 0) {
  1330. level = -level;
  1331. level = (((level << 1) + 1) * qscale *
  1332. ((int) (quant_matrix[j]))) >> 4;
  1333. level = (level - 1) | 1;
  1334. level = -level;
  1335. } else {
  1336. level = (((level << 1) + 1) * qscale *
  1337. ((int) (quant_matrix[j]))) >> 4;
  1338. level = (level - 1) | 1;
  1339. }
  1340. #ifdef PARANOID
  1341. if (level < -2048 || level > 2047)
  1342. fprintf(stderr, "unquant error %d %d\n", i, level);
  1343. #endif
  1344. block[j] = level;
  1345. }
  1346. }
  1347. }
  1348. }
  1349. static void dct_unquantize_h263_c(MpegEncContext *s,
  1350. DCTELEM *block, int n, int qscale)
  1351. {
  1352. int i, level, qmul, qadd;
  1353. int nCoeffs;
  1354. if (s->mb_intra) {
  1355. if (!s->h263_aic) {
  1356. if (n < 4)
  1357. block[0] = block[0] * s->y_dc_scale;
  1358. else
  1359. block[0] = block[0] * s->c_dc_scale;
  1360. }
  1361. i = 1;
  1362. nCoeffs= 64; //does not allways use zigzag table
  1363. } else {
  1364. i = 0;
  1365. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  1366. }
  1367. qmul = s->qscale << 1;
  1368. if (s->h263_aic && s->mb_intra)
  1369. qadd = 0;
  1370. else
  1371. qadd = (s->qscale - 1) | 1;
  1372. for(;i<nCoeffs;i++) {
  1373. level = block[i];
  1374. if (level) {
  1375. if (level < 0) {
  1376. level = level * qmul - qadd;
  1377. } else {
  1378. level = level * qmul + qadd;
  1379. }
  1380. #ifdef PARANOID
  1381. if (level < -2048 || level > 2047)
  1382. fprintf(stderr, "unquant error %d %d\n", i, level);
  1383. #endif
  1384. block[i] = level;
  1385. }
  1386. }
  1387. }
  1388. /* rate control */
  1389. /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
  1390. #define I_FRAME_SIZE_RATIO 3.0
  1391. #define QSCALE_K 20
  1392. static void rate_control_init(MpegEncContext *s)
  1393. {
  1394. #if 1
  1395. emms_c();
  1396. //initial values, they dont really matter as they will be totally different within a few frames
  1397. s->i_pred.coeff= s->p_pred.coeff= 7.0;
  1398. s->i_pred.count= s->p_pred.count= 1.0;
  1399. s->i_pred.decay= s->p_pred.decay= 0.4;
  1400. // use more bits at the beginning, otherwise high motion at the begin will look like shit
  1401. s->qsum=100;
  1402. s->qcount=100;
  1403. s->short_term_qsum=0.001;
  1404. s->short_term_qcount=0.001;
  1405. #else
  1406. s->wanted_bits = 0;
  1407. if (s->intra_only) {
  1408. s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
  1409. s->P_frame_bits = s->I_frame_bits;
  1410. } else {
  1411. s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
  1412. (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
  1413. s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
  1414. }
  1415. #if defined(DEBUG)
  1416. printf("I_frame_size=%d P_frame_size=%d\n",
  1417. s->I_frame_bits, s->P_frame_bits);
  1418. #endif
  1419. #endif
  1420. }
  1421. static double predict(Predictor *p, double q, double var)
  1422. {
  1423. return p->coeff*var / (q*p->count);
  1424. }
  1425. static void update_predictor(Predictor *p, double q, double var, double size)
  1426. {
  1427. double new_coeff= size*q / (var + 1);
  1428. if(var<1000) return;
  1429. /*{
  1430. int pred= predict(p, q, var);
  1431. int error= abs(pred-size);
  1432. static double sum=0;
  1433. static int count=0;
  1434. if(count>5) sum+=error;
  1435. count++;
  1436. if(256*256*256*64%count==0){
  1437. printf("%d %f %f\n", count, sum/count, p->coeff);
  1438. }
  1439. }*/
  1440. p->count*= p->decay;
  1441. p->coeff*= p->decay;
  1442. p->count++;
  1443. p->coeff+= new_coeff;
  1444. }
  1445. static int rate_estimate_qscale(MpegEncContext *s)
  1446. {
  1447. #if 1
  1448. int qmin= s->qmin;
  1449. int qmax= s->qmax;
  1450. int rate_q=5;
  1451. float q;
  1452. int qscale;
  1453. float br_compensation;
  1454. double diff;
  1455. double short_term_q;
  1456. double long_term_q;
  1457. int last_qscale= s->qscale;
  1458. double fps;
  1459. INT64 wanted_bits;
  1460. emms_c();
  1461. fps= (double)s->frame_rate / FRAME_RATE_BASE;
  1462. wanted_bits= s->bit_rate*(double)s->picture_number/fps;
  1463. if(s->picture_number>2){
  1464. /* update predictors */
  1465. if(s->last_pict_type == I_TYPE){
  1466. //FIXME
  1467. }else{ //P Frame
  1468. //printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);
  1469. update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits);
  1470. }
  1471. }
  1472. if(s->pict_type == I_TYPE){
  1473. //FIXME
  1474. rate_q= s->qsum/s->qcount;
  1475. }else{ //P Frame
  1476. int i;
  1477. int diff, best_diff=1000000000;
  1478. for(i=1; i<=31; i++){
  1479. diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps;
  1480. if(diff<0) diff= -diff;
  1481. if(diff<best_diff){
  1482. best_diff= diff;
  1483. rate_q= i;
  1484. }
  1485. }
  1486. }
  1487. s->short_term_qsum*=s->qblur;
  1488. s->short_term_qcount*=s->qblur;
  1489. s->short_term_qsum+= rate_q;
  1490. s->short_term_qcount++;
  1491. short_term_q= s->short_term_qsum/s->short_term_qcount;
  1492. long_term_q= s->qsum/s->qcount*s->total_bits/wanted_bits;
  1493. // q= (long_term_q - short_term_q)*s->qcompress + short_term_q;
  1494. q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
  1495. diff= s->total_bits - wanted_bits;
  1496. br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
  1497. if(br_compensation<=0.0) br_compensation=0.001;
  1498. q/=br_compensation;
  1499. qscale= (int)(q + 0.5);
  1500. if (qscale<qmin) qscale=qmin;
  1501. else if(qscale>qmax) qscale=qmax;
  1502. if (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff;
  1503. else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff;
  1504. s->qsum+= qscale;
  1505. s->qcount++;
  1506. s->last_pict_type= s->pict_type;
  1507. //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%d fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation,
  1508. // rate_q, (int)short_term_q, s->mc_mb_var, s->frame_bits);
  1509. //printf("%d %d\n", s->bit_rate, (int)fps);
  1510. return qscale;
  1511. #else
  1512. INT64 diff, total_bits = s->total_bits;
  1513. float q;
  1514. int qscale;
  1515. if (s->pict_type == I_TYPE) {
  1516. s->wanted_bits += s->I_frame_bits;
  1517. } else {
  1518. s->wanted_bits += s->P_frame_bits;
  1519. }
  1520. diff = s->wanted_bits - total_bits;
  1521. q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
  1522. /* adjust for I frame */
  1523. if (s->pict_type == I_TYPE && !s->intra_only) {
  1524. q /= I_FRAME_SIZE_RATIO;
  1525. }
  1526. /* using a too small Q scale leeds to problems in mpeg1 and h263
  1527. because AC coefficients are clamped to 255 or 127 */
  1528. qmin = 3;
  1529. if (q < qmin)
  1530. q = qmin;
  1531. else if (q > 31)
  1532. q = 31;
  1533. qscale = (int)(q + 0.5);
  1534. #if defined(DEBUG)
  1535. printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n",
  1536. s->picture_number,
  1537. (double)total_bits,
  1538. (double)s->wanted_bits,
  1539. (float)s->frame_rate / FRAME_RATE_BASE *
  1540. total_bits / s->picture_number,
  1541. (int)diff, q);
  1542. #endif
  1543. return qscale;
  1544. #endif
  1545. }
  1546. AVCodec mpeg1video_encoder = {
  1547. "mpeg1video",
  1548. CODEC_TYPE_VIDEO,
  1549. CODEC_ID_MPEG1VIDEO,
  1550. sizeof(MpegEncContext),
  1551. MPV_encode_init,
  1552. MPV_encode_picture,
  1553. MPV_encode_end,
  1554. };
  1555. AVCodec h263_encoder = {
  1556. "h263",
  1557. CODEC_TYPE_VIDEO,
  1558. CODEC_ID_H263,
  1559. sizeof(MpegEncContext),
  1560. MPV_encode_init,
  1561. MPV_encode_picture,
  1562. MPV_encode_end,
  1563. };
  1564. AVCodec h263p_encoder = {
  1565. "h263p",
  1566. CODEC_TYPE_VIDEO,
  1567. CODEC_ID_H263P,
  1568. sizeof(MpegEncContext),
  1569. MPV_encode_init,
  1570. MPV_encode_picture,
  1571. MPV_encode_end,
  1572. };
  1573. AVCodec rv10_encoder = {
  1574. "rv10",
  1575. CODEC_TYPE_VIDEO,
  1576. CODEC_ID_RV10,
  1577. sizeof(MpegEncContext),
  1578. MPV_encode_init,
  1579. MPV_encode_picture,
  1580. MPV_encode_end,
  1581. };
  1582. AVCodec mjpeg_encoder = {
  1583. "mjpeg",
  1584. CODEC_TYPE_VIDEO,
  1585. CODEC_ID_MJPEG,
  1586. sizeof(MpegEncContext),
  1587. MPV_encode_init,
  1588. MPV_encode_picture,
  1589. MPV_encode_end,
  1590. };
  1591. AVCodec mpeg4_encoder = {
  1592. "mpeg4",
  1593. CODEC_TYPE_VIDEO,
  1594. CODEC_ID_MPEG4,
  1595. sizeof(MpegEncContext),
  1596. MPV_encode_init,
  1597. MPV_encode_picture,
  1598. MPV_encode_end,
  1599. };
  1600. AVCodec msmpeg4_encoder = {
  1601. "msmpeg4",
  1602. CODEC_TYPE_VIDEO,
  1603. CODEC_ID_MSMPEG4,
  1604. sizeof(MpegEncContext),
  1605. MPV_encode_init,
  1606. MPV_encode_picture,
  1607. MPV_encode_end,
  1608. };