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.

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