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.

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