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.

2326 lines
78KB

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