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.

1931 lines
61KB

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