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.

1911 lines
60KB

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