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.

1372 lines
42KB

  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 int dct_quantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
  37. static int dct_quantize_mmx(MpegEncContext *s,
  38. DCTELEM *block, int n,
  39. int qscale);
  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. /* default motion estimation */
  61. int motion_estimation_method = ME_LOG;
  62. /* XXX: should use variable shift ? */
  63. #define QMAT_SHIFT_MMX 19
  64. #define QMAT_SHIFT 25
  65. static void convert_matrix(int *qmat, 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. qmat[i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
  73. (aanscales[i] * qscale * quant_matrix[i]));
  74. }
  75. } else {
  76. for(i=0;i<64;i++) {
  77. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  78. So 16 <= qscale * quant_matrix[i] <= 7905
  79. so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905
  80. */
  81. qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  82. }
  83. }
  84. }
  85. /* init common structure for both encoder and decoder */
  86. int MPV_common_init(MpegEncContext *s)
  87. {
  88. int c_size, i;
  89. UINT8 *pict;
  90. if (s->out_format == FMT_H263)
  91. s->dct_unquantize = dct_unquantize_h263_c;
  92. else
  93. s->dct_unquantize = dct_unquantize_mpeg1_c;
  94. #ifdef HAVE_MMX
  95. MPV_common_init_mmx(s);
  96. #endif
  97. s->mb_width = (s->width + 15) / 16;
  98. s->mb_height = (s->height + 15) / 16;
  99. s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
  100. for(i=0;i<3;i++) {
  101. int w, h, shift, pict_start;
  102. w = s->linesize;
  103. h = s->mb_height * 16 + 2 * EDGE_WIDTH;
  104. shift = (i == 0) ? 0 : 1;
  105. c_size = (w >> shift) * (h >> shift);
  106. pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
  107. pict = av_mallocz(c_size);
  108. if (pict == NULL)
  109. goto fail;
  110. s->last_picture_base[i] = pict;
  111. s->last_picture[i] = pict + pict_start;
  112. pict = av_mallocz(c_size);
  113. if (pict == NULL)
  114. goto fail;
  115. s->next_picture_base[i] = pict;
  116. s->next_picture[i] = pict + pict_start;
  117. if (s->has_b_frames) {
  118. pict = av_mallocz(c_size);
  119. if (pict == NULL)
  120. goto fail;
  121. s->aux_picture_base[i] = pict;
  122. s->aux_picture[i] = pict + pict_start;
  123. }
  124. }
  125. if (s->out_format == FMT_H263) {
  126. int size;
  127. /* MV prediction */
  128. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  129. s->motion_val = malloc(size * 2 * sizeof(INT16));
  130. if (s->motion_val == NULL)
  131. goto fail;
  132. memset(s->motion_val, 0, size * 2 * sizeof(INT16));
  133. }
  134. if (s->h263_pred) {
  135. int y_size, c_size, i, size;
  136. /* dc values */
  137. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  138. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  139. size = y_size + 2 * c_size;
  140. s->dc_val[0] = malloc(size * sizeof(INT16));
  141. if (s->dc_val[0] == NULL)
  142. goto fail;
  143. s->dc_val[1] = s->dc_val[0] + y_size;
  144. s->dc_val[2] = s->dc_val[1] + c_size;
  145. for(i=0;i<size;i++)
  146. s->dc_val[0][i] = 1024;
  147. /* ac values */
  148. s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
  149. if (s->ac_val[0] == NULL)
  150. goto fail;
  151. s->ac_val[1] = s->ac_val[0] + y_size;
  152. s->ac_val[2] = s->ac_val[1] + c_size;
  153. /* cbp values */
  154. s->coded_block = av_mallocz(y_size);
  155. if (!s->coded_block)
  156. goto fail;
  157. }
  158. /* default structure is frame */
  159. s->picture_structure = PICT_FRAME;
  160. /* init macroblock skip table */
  161. if (!s->encoding) {
  162. s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
  163. if (!s->mbskip_table)
  164. goto fail;
  165. }
  166. s->context_initialized = 1;
  167. return 0;
  168. fail:
  169. if (s->motion_val)
  170. free(s->motion_val);
  171. if (s->dc_val[0])
  172. free(s->dc_val[0]);
  173. if (s->ac_val[0])
  174. free(s->ac_val[0]);
  175. if (s->coded_block)
  176. free(s->coded_block);
  177. if (s->mbskip_table)
  178. free(s->mbskip_table);
  179. for(i=0;i<3;i++) {
  180. if (s->last_picture_base[i])
  181. free(s->last_picture_base[i]);
  182. if (s->next_picture_base[i])
  183. free(s->next_picture_base[i]);
  184. if (s->aux_picture_base[i])
  185. free(s->aux_picture_base[i]);
  186. }
  187. return -1;
  188. }
  189. /* init common structure for both encoder and decoder */
  190. void MPV_common_end(MpegEncContext *s)
  191. {
  192. int i;
  193. if (s->motion_val)
  194. free(s->motion_val);
  195. if (s->h263_pred) {
  196. free(s->dc_val[0]);
  197. free(s->ac_val[0]);
  198. free(s->coded_block);
  199. }
  200. if (s->mbskip_table)
  201. free(s->mbskip_table);
  202. for(i=0;i<3;i++) {
  203. free(s->last_picture_base[i]);
  204. free(s->next_picture_base[i]);
  205. if (s->has_b_frames)
  206. free(s->aux_picture_base[i]);
  207. }
  208. s->context_initialized = 0;
  209. }
  210. /* init video encoder */
  211. int MPV_encode_init(AVCodecContext *avctx)
  212. {
  213. MpegEncContext *s = avctx->priv_data;
  214. int i;
  215. s->bit_rate = avctx->bit_rate;
  216. s->frame_rate = avctx->frame_rate;
  217. s->width = avctx->width;
  218. s->height = avctx->height;
  219. s->gop_size = avctx->gop_size;
  220. if (s->gop_size <= 1) {
  221. s->intra_only = 1;
  222. s->gop_size = 12;
  223. } else {
  224. s->intra_only = 0;
  225. }
  226. s->full_search = motion_estimation_method;
  227. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  228. switch(avctx->codec->id) {
  229. case CODEC_ID_MPEG1VIDEO:
  230. s->out_format = FMT_MPEG1;
  231. break;
  232. case CODEC_ID_MJPEG:
  233. s->out_format = FMT_MJPEG;
  234. s->intra_only = 1; /* force intra only for jpeg */
  235. if (mjpeg_init(s) < 0)
  236. return -1;
  237. break;
  238. case CODEC_ID_H263:
  239. if (h263_get_picture_format(s->width, s->height) == 7)
  240. return -1;
  241. s->out_format = FMT_H263;
  242. break;
  243. case CODEC_ID_H263P:
  244. s->out_format = FMT_H263;
  245. s->h263_plus = 1;
  246. s->unrestricted_mv = 1;
  247. /* These are just to be sure */
  248. s->umvplus = 0;
  249. s->umvplus_dec = 0;
  250. break;
  251. case CODEC_ID_RV10:
  252. s->out_format = FMT_H263;
  253. s->h263_rv10 = 1;
  254. break;
  255. case CODEC_ID_MPEG4:
  256. s->out_format = FMT_H263;
  257. s->h263_pred = 1;
  258. s->unrestricted_mv = 1;
  259. break;
  260. case CODEC_ID_MSMPEG4:
  261. s->out_format = FMT_H263;
  262. s->h263_msmpeg4 = 1;
  263. s->h263_pred = 1;
  264. s->unrestricted_mv = 1;
  265. break;
  266. default:
  267. return -1;
  268. }
  269. if (s->out_format == FMT_H263)
  270. h263_encode_init_vlc(s);
  271. s->encoding = 1;
  272. /* init */
  273. if (MPV_common_init(s) < 0)
  274. return -1;
  275. /* init default q matrix */
  276. for(i=0;i<64;i++) {
  277. s->intra_matrix[i] = default_intra_matrix[i];
  278. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  279. }
  280. /* rate control init */
  281. rate_control_init(s);
  282. s->picture_number = 0;
  283. s->fake_picture_number = 0;
  284. /* motion detector init */
  285. s->f_code = 1;
  286. return 0;
  287. }
  288. int MPV_encode_end(AVCodecContext *avctx)
  289. {
  290. MpegEncContext *s = avctx->priv_data;
  291. #ifdef STATS
  292. print_stats();
  293. #endif
  294. MPV_common_end(s);
  295. if (s->out_format == FMT_MJPEG)
  296. mjpeg_close(s);
  297. return 0;
  298. }
  299. /* draw the edges of width 'w' of an image of size width, height */
  300. static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w)
  301. {
  302. UINT8 *ptr, *last_line;
  303. int i;
  304. last_line = buf + (height - 1) * wrap;
  305. for(i=0;i<w;i++) {
  306. /* top and bottom */
  307. memcpy(buf - (i + 1) * wrap, buf, width);
  308. memcpy(last_line + (i + 1) * wrap, last_line, width);
  309. }
  310. /* left and right */
  311. ptr = buf;
  312. for(i=0;i<height;i++) {
  313. memset(ptr - w, ptr[0], w);
  314. memset(ptr + width, ptr[width-1], w);
  315. ptr += wrap;
  316. }
  317. /* corners */
  318. for(i=0;i<w;i++) {
  319. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  320. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  321. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  322. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  323. }
  324. }
  325. /* generic function for encode/decode called before a frame is coded/decoded */
  326. void MPV_frame_start(MpegEncContext *s)
  327. {
  328. int i;
  329. UINT8 *tmp;
  330. s->mb_skiped = 0;
  331. if (s->pict_type == B_TYPE) {
  332. for(i=0;i<3;i++) {
  333. s->current_picture[i] = s->aux_picture[i];
  334. }
  335. } else {
  336. for(i=0;i<3;i++) {
  337. /* swap next and last */
  338. tmp = s->last_picture[i];
  339. s->last_picture[i] = s->next_picture[i];
  340. s->next_picture[i] = tmp;
  341. s->current_picture[i] = tmp;
  342. }
  343. }
  344. }
  345. /* generic function for encode/decode called after a frame has been coded/decoded */
  346. void MPV_frame_end(MpegEncContext *s)
  347. {
  348. /* draw edge for correct motion prediction if outside */
  349. if (s->pict_type != B_TYPE) {
  350. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  351. draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  352. draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  353. }
  354. }
  355. int MPV_encode_picture(AVCodecContext *avctx,
  356. unsigned char *buf, int buf_size, void *data)
  357. {
  358. MpegEncContext *s = avctx->priv_data;
  359. AVPicture *pict = data;
  360. int i, j;
  361. if (s->fixed_qscale)
  362. s->qscale = avctx->quality;
  363. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  364. if (!s->intra_only) {
  365. /* first picture of GOP is intra */
  366. if ((s->picture_number % s->gop_size) == 0)
  367. s->pict_type = I_TYPE;
  368. else
  369. s->pict_type = P_TYPE;
  370. } else {
  371. s->pict_type = I_TYPE;
  372. }
  373. avctx->key_frame = (s->pict_type == I_TYPE);
  374. MPV_frame_start(s);
  375. for(i=0;i<3;i++) {
  376. UINT8 *src = pict->data[i];
  377. UINT8 *dest = s->current_picture[i];
  378. int src_wrap = pict->linesize[i];
  379. int dest_wrap = s->linesize;
  380. int w = s->width;
  381. int h = s->height;
  382. if (i >= 1) {
  383. dest_wrap >>= 1;
  384. w >>= 1;
  385. h >>= 1;
  386. }
  387. for(j=0;j<h;j++) {
  388. memcpy(dest, src, w);
  389. dest += dest_wrap;
  390. src += src_wrap;
  391. }
  392. s->new_picture[i] = s->current_picture[i];
  393. }
  394. encode_picture(s, s->picture_number);
  395. MPV_frame_end(s);
  396. s->picture_number++;
  397. if (s->out_format == FMT_MJPEG)
  398. mjpeg_picture_trailer(s);
  399. flush_put_bits(&s->pb);
  400. s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
  401. avctx->quality = s->qscale;
  402. return s->pb.buf_ptr - s->pb.buf;
  403. }
  404. static inline int clip(int a, int amin, int amax)
  405. {
  406. if (a < amin)
  407. return amin;
  408. else if (a > amax)
  409. return amax;
  410. else
  411. return a;
  412. }
  413. /* apply one mpeg motion vector to the three components */
  414. static inline void mpeg_motion(MpegEncContext *s,
  415. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  416. int dest_offset,
  417. UINT8 **ref_picture, int src_offset,
  418. int field_based, op_pixels_func *pix_op,
  419. int motion_x, int motion_y, int h)
  420. {
  421. UINT8 *ptr;
  422. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  423. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  424. src_x = s->mb_x * 16 + (motion_x >> 1);
  425. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  426. /* WARNING: do no forget half pels */
  427. height = s->height >> field_based;
  428. src_x = clip(src_x, -16, s->width);
  429. if (src_x == s->width)
  430. dxy &= ~1;
  431. src_y = clip(src_y, -16, height);
  432. if (src_y == height)
  433. dxy &= ~2;
  434. linesize = s->linesize << field_based;
  435. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  436. dest_y += dest_offset;
  437. pix_op[dxy](dest_y, ptr, linesize, h);
  438. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  439. if (s->out_format == FMT_H263) {
  440. dxy = 0;
  441. if ((motion_x & 3) != 0)
  442. dxy |= 1;
  443. if ((motion_y & 3) != 0)
  444. dxy |= 2;
  445. mx = motion_x >> 2;
  446. my = motion_y >> 2;
  447. } else {
  448. mx = motion_x / 2;
  449. my = motion_y / 2;
  450. dxy = ((my & 1) << 1) | (mx & 1);
  451. mx >>= 1;
  452. my >>= 1;
  453. }
  454. src_x = s->mb_x * 8 + mx;
  455. src_y = s->mb_y * (8 >> field_based) + my;
  456. src_x = clip(src_x, -8, s->width >> 1);
  457. if (src_x == (s->width >> 1))
  458. dxy &= ~1;
  459. src_y = clip(src_y, -8, height >> 1);
  460. if (src_y == (height >> 1))
  461. dxy &= ~2;
  462. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  463. ptr = ref_picture[1] + offset;
  464. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  465. ptr = ref_picture[2] + offset;
  466. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  467. }
  468. static inline void MPV_motion(MpegEncContext *s,
  469. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  470. int dir, UINT8 **ref_picture,
  471. op_pixels_func *pix_op)
  472. {
  473. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  474. int mb_x, mb_y, i;
  475. UINT8 *ptr, *dest;
  476. mb_x = s->mb_x;
  477. mb_y = s->mb_y;
  478. switch(s->mv_type) {
  479. case MV_TYPE_16X16:
  480. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  481. ref_picture, 0,
  482. 0, pix_op,
  483. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  484. break;
  485. case MV_TYPE_8X8:
  486. for(i=0;i<4;i++) {
  487. motion_x = s->mv[dir][i][0];
  488. motion_y = s->mv[dir][i][1];
  489. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  490. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  491. src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
  492. /* WARNING: do no forget half pels */
  493. src_x = clip(src_x, -16, s->width);
  494. if (src_x == s->width)
  495. dxy &= ~1;
  496. src_y = clip(src_y, -16, s->height);
  497. if (src_y == s->height)
  498. dxy &= ~2;
  499. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  500. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  501. pix_op[dxy](dest, ptr, s->linesize, 8);
  502. }
  503. /* In case of 8X8, we construct a single chroma motion vector
  504. with a special rounding */
  505. mx = 0;
  506. my = 0;
  507. for(i=0;i<4;i++) {
  508. mx += s->mv[dir][i][0];
  509. my += s->mv[dir][i][1];
  510. }
  511. if (mx >= 0)
  512. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  513. else {
  514. mx = -mx;
  515. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  516. }
  517. if (my >= 0)
  518. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  519. else {
  520. my = -my;
  521. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  522. }
  523. dxy = ((my & 1) << 1) | (mx & 1);
  524. mx >>= 1;
  525. my >>= 1;
  526. src_x = mb_x * 8 + mx;
  527. src_y = mb_y * 8 + my;
  528. src_x = clip(src_x, -8, s->width/2);
  529. if (src_x == s->width/2)
  530. dxy &= ~1;
  531. src_y = clip(src_y, -8, s->height/2);
  532. if (src_y == s->height/2)
  533. dxy &= ~2;
  534. offset = (src_y * (s->linesize >> 1)) + src_x;
  535. ptr = ref_picture[1] + offset;
  536. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  537. ptr = ref_picture[2] + offset;
  538. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  539. break;
  540. case MV_TYPE_FIELD:
  541. if (s->picture_structure == PICT_FRAME) {
  542. /* top field */
  543. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  544. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  545. 1, pix_op,
  546. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  547. /* bottom field */
  548. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  549. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  550. 1, pix_op,
  551. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  552. } else {
  553. }
  554. break;
  555. }
  556. }
  557. /* put block[] to dest[] */
  558. static inline void put_dct(MpegEncContext *s,
  559. DCTELEM *block, int i, UINT8 *dest, int line_size)
  560. {
  561. if (!s->mpeg2)
  562. s->dct_unquantize(s, block, i, s->qscale);
  563. ff_idct (block);
  564. put_pixels_clamped(block, dest, line_size);
  565. }
  566. /* add block[] to dest[] */
  567. static inline void add_dct(MpegEncContext *s,
  568. DCTELEM *block, int i, UINT8 *dest, int line_size)
  569. {
  570. if (s->block_last_index[i] >= 0) {
  571. if (!s->mpeg2)
  572. s->dct_unquantize(s, block, i, s->qscale);
  573. ff_idct (block);
  574. add_pixels_clamped(block, dest, line_size);
  575. }
  576. }
  577. /* generic function called after a macroblock has been parsed by the
  578. decoder or after it has been encoded by the encoder.
  579. Important variables used:
  580. s->mb_intra : true if intra macroblock
  581. s->mv_dir : motion vector direction
  582. s->mv_type : motion vector type
  583. s->mv : motion vector
  584. s->interlaced_dct : true if interlaced dct used (mpeg2)
  585. */
  586. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  587. {
  588. int mb_x, mb_y, motion_x, motion_y;
  589. int dct_linesize, dct_offset;
  590. op_pixels_func *op_pix;
  591. mb_x = s->mb_x;
  592. mb_y = s->mb_y;
  593. #ifdef FF_POSTPROCESS
  594. quant_store[mb_y][mb_x]=s->qscale;
  595. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  596. #endif
  597. /* update DC predictors for P macroblocks */
  598. if (!s->mb_intra) {
  599. if (s->h263_pred) {
  600. int wrap, x, y, v;
  601. wrap = 2 * s->mb_width + 2;
  602. v = 1024;
  603. x = 2 * mb_x + 1;
  604. y = 2 * mb_y + 1;
  605. s->dc_val[0][(x) + (y) * wrap] = v;
  606. s->dc_val[0][(x + 1) + (y) * wrap] = v;
  607. s->dc_val[0][(x) + (y + 1) * wrap] = v;
  608. s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
  609. /* ac pred */
  610. memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  611. memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
  612. memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
  613. memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
  614. if (s->h263_msmpeg4) {
  615. s->coded_block[(x) + (y) * wrap] = 0;
  616. s->coded_block[(x + 1) + (y) * wrap] = 0;
  617. s->coded_block[(x) + (y + 1) * wrap] = 0;
  618. s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
  619. }
  620. /* chroma */
  621. wrap = s->mb_width + 2;
  622. x = mb_x + 1;
  623. y = mb_y + 1;
  624. s->dc_val[1][(x) + (y) * wrap] = v;
  625. s->dc_val[2][(x) + (y) * wrap] = v;
  626. /* ac pred */
  627. memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  628. memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  629. } else {
  630. s->last_dc[0] = 128 << s->intra_dc_precision;
  631. s->last_dc[1] = 128 << s->intra_dc_precision;
  632. s->last_dc[2] = 128 << s->intra_dc_precision;
  633. }
  634. }
  635. /* update motion predictor */
  636. if (s->out_format == FMT_H263) {
  637. int x, y, wrap;
  638. x = 2 * mb_x + 1;
  639. y = 2 * mb_y + 1;
  640. wrap = 2 * s->mb_width + 2;
  641. if (s->mb_intra) {
  642. motion_x = 0;
  643. motion_y = 0;
  644. goto motion_init;
  645. } else if (s->mv_type == MV_TYPE_16X16) {
  646. motion_x = s->mv[0][0][0];
  647. motion_y = s->mv[0][0][1];
  648. motion_init:
  649. /* no update if 8X8 because it has been done during parsing */
  650. s->motion_val[(x) + (y) * wrap][0] = motion_x;
  651. s->motion_val[(x) + (y) * wrap][1] = motion_y;
  652. s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
  653. s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
  654. s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
  655. s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
  656. s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
  657. s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
  658. }
  659. }
  660. if (!s->intra_only) {
  661. UINT8 *dest_y, *dest_cb, *dest_cr;
  662. UINT8 *mbskip_ptr;
  663. /* avoid copy if macroblock skipped in last frame too */
  664. if (!s->encoding && s->pict_type != B_TYPE) {
  665. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  666. if (s->mb_skiped) {
  667. s->mb_skiped = 0;
  668. /* if previous was skipped too, then nothing to do ! */
  669. if (*mbskip_ptr != 0)
  670. goto the_end;
  671. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  672. } else {
  673. *mbskip_ptr = 0; /* not skipped */
  674. }
  675. }
  676. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  677. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  678. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  679. if (s->interlaced_dct) {
  680. dct_linesize = s->linesize * 2;
  681. dct_offset = s->linesize;
  682. } else {
  683. dct_linesize = s->linesize;
  684. dct_offset = s->linesize * 8;
  685. }
  686. if (!s->mb_intra) {
  687. /* motion handling */
  688. if (!s->no_rounding)
  689. op_pix = put_pixels_tab;
  690. else
  691. op_pix = put_no_rnd_pixels_tab;
  692. if (s->mv_dir & MV_DIR_FORWARD) {
  693. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
  694. if (!s->no_rounding)
  695. op_pix = avg_pixels_tab;
  696. else
  697. op_pix = avg_no_rnd_pixels_tab;
  698. }
  699. if (s->mv_dir & MV_DIR_BACKWARD) {
  700. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
  701. }
  702. /* add dct residue */
  703. add_dct(s, block[0], 0, dest_y, dct_linesize);
  704. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  705. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  706. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  707. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  708. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  709. } else {
  710. /* dct only in intra block */
  711. put_dct(s, block[0], 0, dest_y, dct_linesize);
  712. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  713. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  714. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  715. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  716. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  717. }
  718. }
  719. the_end:
  720. emms_c();
  721. }
  722. static void encode_picture(MpegEncContext *s, int picture_number)
  723. {
  724. int mb_x, mb_y, wrap;
  725. UINT8 *ptr;
  726. int i, motion_x, motion_y;
  727. s->picture_number = picture_number;
  728. if (!s->fixed_qscale)
  729. s->qscale = rate_estimate_qscale(s);
  730. /* precompute matrix */
  731. if (s->out_format == FMT_MJPEG) {
  732. /* for mjpeg, we do include qscale in the matrix */
  733. s->intra_matrix[0] = default_intra_matrix[0];
  734. for(i=1;i<64;i++)
  735. s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
  736. convert_matrix(s->q_intra_matrix, s->intra_matrix, 8);
  737. } else {
  738. convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale);
  739. convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale);
  740. }
  741. switch(s->out_format) {
  742. case FMT_MJPEG:
  743. mjpeg_picture_header(s);
  744. break;
  745. case FMT_H263:
  746. if (s->h263_msmpeg4)
  747. msmpeg4_encode_picture_header(s, picture_number);
  748. else if (s->h263_pred)
  749. mpeg4_encode_picture_header(s, picture_number);
  750. else if (s->h263_rv10)
  751. rv10_encode_picture_header(s, picture_number);
  752. else
  753. h263_encode_picture_header(s, picture_number);
  754. break;
  755. case FMT_MPEG1:
  756. mpeg1_encode_picture_header(s, picture_number);
  757. break;
  758. }
  759. /* init last dc values */
  760. /* note: quant matrix value (8) is implied here */
  761. s->last_dc[0] = 128;
  762. s->last_dc[1] = 128;
  763. s->last_dc[2] = 128;
  764. s->mb_incr = 1;
  765. s->last_mv[0][0][0] = 0;
  766. s->last_mv[0][0][1] = 0;
  767. s->mv_type = MV_TYPE_16X16;
  768. s->mv_dir = MV_DIR_FORWARD;
  769. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  770. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  771. s->mb_x = mb_x;
  772. s->mb_y = mb_y;
  773. /* compute motion vector and macro block type (intra or non intra) */
  774. motion_x = 0;
  775. motion_y = 0;
  776. if (s->pict_type == P_TYPE) {
  777. s->mb_intra = estimate_motion(s, mb_x, mb_y,
  778. &motion_x,
  779. &motion_y);
  780. } else {
  781. s->mb_intra = 1;
  782. }
  783. /* get the pixels */
  784. wrap = s->linesize;
  785. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  786. get_pixels(s->block[0], ptr, wrap);
  787. get_pixels(s->block[1], ptr + 8, wrap);
  788. get_pixels(s->block[2], ptr + 8 * wrap, wrap);
  789. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  790. wrap = s->linesize >> 1;
  791. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  792. get_pixels(s->block[4], ptr, wrap);
  793. wrap = s->linesize >> 1;
  794. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  795. get_pixels(s->block[5], ptr, wrap);
  796. /* subtract previous frame if non intra */
  797. if (!s->mb_intra) {
  798. int dxy, offset, mx, my;
  799. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  800. ptr = s->last_picture[0] +
  801. ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
  802. (mb_x * 16 + (motion_x >> 1));
  803. sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
  804. sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
  805. sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
  806. sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
  807. if (s->out_format == FMT_H263) {
  808. /* special rounding for h263 */
  809. dxy = 0;
  810. if ((motion_x & 3) != 0)
  811. dxy |= 1;
  812. if ((motion_y & 3) != 0)
  813. dxy |= 2;
  814. mx = motion_x >> 2;
  815. my = motion_y >> 2;
  816. } else {
  817. mx = motion_x / 2;
  818. my = motion_y / 2;
  819. dxy = ((my & 1) << 1) | (mx & 1);
  820. mx >>= 1;
  821. my >>= 1;
  822. }
  823. offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
  824. ptr = s->last_picture[1] + offset;
  825. sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
  826. ptr = s->last_picture[2] + offset;
  827. sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
  828. }
  829. emms_c();
  830. /* DCT & quantize */
  831. if (s->h263_msmpeg4) {
  832. msmpeg4_dc_scale(s);
  833. } else if (s->h263_pred) {
  834. h263_dc_scale(s);
  835. } else {
  836. /* default quantization values */
  837. s->y_dc_scale = 8;
  838. s->c_dc_scale = 8;
  839. }
  840. for(i=0;i<6;i++) {
  841. int last_index;
  842. if (av_fdct == jpeg_fdct_ifast)
  843. last_index = dct_quantize(s, s->block[i], i, s->qscale);
  844. else
  845. last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale);
  846. s->block_last_index[i] = last_index;
  847. }
  848. /* huffman encode */
  849. switch(s->out_format) {
  850. case FMT_MPEG1:
  851. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  852. break;
  853. case FMT_H263:
  854. if (s->h263_msmpeg4)
  855. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  856. else
  857. h263_encode_mb(s, s->block, motion_x, motion_y);
  858. break;
  859. case FMT_MJPEG:
  860. mjpeg_encode_mb(s, s->block);
  861. break;
  862. }
  863. /* decompress blocks so that we keep the state of the decoder */
  864. s->mv[0][0][0] = motion_x;
  865. s->mv[0][0][1] = motion_y;
  866. MPV_decode_mb(s, s->block);
  867. }
  868. }
  869. }
  870. static int dct_quantize(MpegEncContext *s,
  871. DCTELEM *block, int n,
  872. int qscale)
  873. {
  874. int i, j, level, last_non_zero, q;
  875. const int *qmat;
  876. av_fdct (block);
  877. /* we need this permutation so that we correct the IDCT
  878. permutation. will be moved into DCT code */
  879. block_permute(block);
  880. if (s->mb_intra) {
  881. if (n < 4)
  882. q = s->y_dc_scale;
  883. else
  884. q = s->c_dc_scale;
  885. q = q << 3;
  886. /* note: block[0] is assumed to be positive */
  887. block[0] = (block[0] + (q >> 1)) / q;
  888. i = 1;
  889. last_non_zero = 0;
  890. if (s->out_format == FMT_H263) {
  891. qmat = s->q_non_intra_matrix;
  892. } else {
  893. qmat = s->q_intra_matrix;
  894. }
  895. } else {
  896. i = 0;
  897. last_non_zero = -1;
  898. qmat = s->q_non_intra_matrix;
  899. }
  900. for(;i<64;i++) {
  901. j = zigzag_direct[i];
  902. level = block[j];
  903. level = level * qmat[j];
  904. #ifdef PARANOID
  905. {
  906. static int count = 0;
  907. int level1, level2, qmat1;
  908. double val;
  909. if (qmat == s->q_non_intra_matrix) {
  910. qmat1 = default_non_intra_matrix[j] * s->qscale;
  911. } else {
  912. qmat1 = default_intra_matrix[j] * s->qscale;
  913. }
  914. if (av_fdct != jpeg_fdct_ifast)
  915. val = ((double)block[j] * 8.0) / (double)qmat1;
  916. else
  917. val = ((double)block[j] * 8.0 * 2048.0) /
  918. ((double)qmat1 * aanscales[j]);
  919. level1 = (int)val;
  920. level2 = level / (1 << (QMAT_SHIFT - 3));
  921. if (level1 != level2) {
  922. fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
  923. count, level2, level1, block[j], qmat1, qmat[j],
  924. val);
  925. count++;
  926. }
  927. }
  928. #endif
  929. /* XXX: slight error for the low range. Test should be equivalent to
  930. (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
  931. (QMAT_SHIFT - 3)))
  932. */
  933. if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
  934. level) {
  935. level = level / (1 << (QMAT_SHIFT - 3));
  936. /* XXX: currently, this code is not optimal. the range should be:
  937. mpeg1: -255..255
  938. mpeg2: -2048..2047
  939. h263: -128..127
  940. mpeg4: -2048..2047
  941. */
  942. if (level > 127)
  943. level = 127;
  944. else if (level < -128)
  945. level = -128;
  946. block[j] = level;
  947. last_non_zero = i;
  948. } else {
  949. block[j] = 0;
  950. }
  951. }
  952. return last_non_zero;
  953. }
  954. static int dct_quantize_mmx(MpegEncContext *s,
  955. DCTELEM *block, int n,
  956. int qscale)
  957. {
  958. int i, j, level, last_non_zero, q;
  959. const int *qmat;
  960. av_fdct (block);
  961. /* we need this permutation so that we correct the IDCT
  962. permutation. will be moved into DCT code */
  963. block_permute(block);
  964. if (s->mb_intra) {
  965. if (n < 4)
  966. q = s->y_dc_scale;
  967. else
  968. q = s->c_dc_scale;
  969. /* note: block[0] is assumed to be positive */
  970. block[0] = (block[0] + (q >> 1)) / q;
  971. i = 1;
  972. last_non_zero = 0;
  973. if (s->out_format == FMT_H263) {
  974. qmat = s->q_non_intra_matrix;
  975. } else {
  976. qmat = s->q_intra_matrix;
  977. }
  978. } else {
  979. i = 0;
  980. last_non_zero = -1;
  981. qmat = s->q_non_intra_matrix;
  982. }
  983. for(;i<64;i++) {
  984. j = zigzag_direct[i];
  985. level = block[j];
  986. level = level * qmat[j];
  987. /* XXX: slight error for the low range. Test should be equivalent to
  988. (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
  989. (QMAT_SHIFT_MMX - 3)))
  990. */
  991. if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) !=
  992. level) {
  993. level = level / (1 << (QMAT_SHIFT_MMX - 3));
  994. /* XXX: currently, this code is not optimal. the range should be:
  995. mpeg1: -255..255
  996. mpeg2: -2048..2047
  997. h263: -128..127
  998. mpeg4: -2048..2047
  999. */
  1000. if (level > 127)
  1001. level = 127;
  1002. else if (level < -128)
  1003. level = -128;
  1004. block[j] = level;
  1005. last_non_zero = i;
  1006. } else {
  1007. block[j] = 0;
  1008. }
  1009. }
  1010. return last_non_zero;
  1011. }
  1012. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1013. DCTELEM *block, int n, int qscale)
  1014. {
  1015. int i, level;
  1016. const UINT16 *quant_matrix;
  1017. if (s->mb_intra) {
  1018. if (n < 4)
  1019. block[0] = block[0] * s->y_dc_scale;
  1020. else
  1021. block[0] = block[0] * s->c_dc_scale;
  1022. /* XXX: only mpeg1 */
  1023. quant_matrix = s->intra_matrix;
  1024. for(i=1;i<64;i++) {
  1025. level = block[i];
  1026. if (level) {
  1027. if (level < 0) {
  1028. level = -level;
  1029. level = (int)(level * qscale * quant_matrix[i]) >> 3;
  1030. level = (level - 1) | 1;
  1031. level = -level;
  1032. } else {
  1033. level = (int)(level * qscale * quant_matrix[i]) >> 3;
  1034. level = (level - 1) | 1;
  1035. }
  1036. #ifdef PARANOID
  1037. if (level < -2048 || level > 2047)
  1038. fprintf(stderr, "unquant error %d %d\n", i, level);
  1039. #endif
  1040. block[i] = level;
  1041. }
  1042. }
  1043. } else {
  1044. i = 0;
  1045. quant_matrix = s->non_intra_matrix;
  1046. for(;i<64;i++) {
  1047. level = block[i];
  1048. if (level) {
  1049. if (level < 0) {
  1050. level = -level;
  1051. level = (((level << 1) + 1) * qscale *
  1052. ((int) (quant_matrix[i]))) >> 4;
  1053. level = (level - 1) | 1;
  1054. level = -level;
  1055. } else {
  1056. level = (((level << 1) + 1) * qscale *
  1057. ((int) (quant_matrix[i]))) >> 4;
  1058. level = (level - 1) | 1;
  1059. }
  1060. #ifdef PARANOID
  1061. if (level < -2048 || level > 2047)
  1062. fprintf(stderr, "unquant error %d %d\n", i, level);
  1063. #endif
  1064. block[i] = level;
  1065. }
  1066. }
  1067. }
  1068. }
  1069. static void dct_unquantize_h263_c(MpegEncContext *s,
  1070. DCTELEM *block, int n, int qscale)
  1071. {
  1072. int i, level, qmul, qadd;
  1073. if (s->mb_intra) {
  1074. if (n < 4)
  1075. block[0] = block[0] * s->y_dc_scale;
  1076. else
  1077. block[0] = block[0] * s->c_dc_scale;
  1078. i = 1;
  1079. } else {
  1080. i = 0;
  1081. }
  1082. qmul = s->qscale << 1;
  1083. qadd = (s->qscale - 1) | 1;
  1084. for(;i<64;i++) {
  1085. level = block[i];
  1086. if (level) {
  1087. if (level < 0) {
  1088. level = level * qmul - qadd;
  1089. } else {
  1090. level = level * qmul + qadd;
  1091. }
  1092. #ifdef PARANOID
  1093. if (level < -2048 || level > 2047)
  1094. fprintf(stderr, "unquant error %d %d\n", i, level);
  1095. #endif
  1096. block[i] = level;
  1097. }
  1098. }
  1099. }
  1100. /* rate control */
  1101. /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
  1102. #define I_FRAME_SIZE_RATIO 3.0
  1103. #define QSCALE_K 20
  1104. static void rate_control_init(MpegEncContext *s)
  1105. {
  1106. s->wanted_bits = 0;
  1107. if (s->intra_only) {
  1108. s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
  1109. s->P_frame_bits = s->I_frame_bits;
  1110. } else {
  1111. s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
  1112. (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
  1113. s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
  1114. }
  1115. #if defined(DEBUG)
  1116. printf("I_frame_size=%d P_frame_size=%d\n",
  1117. s->I_frame_bits, s->P_frame_bits);
  1118. #endif
  1119. }
  1120. /*
  1121. * This heuristic is rather poor, but at least we do not have to
  1122. * change the qscale at every macroblock.
  1123. */
  1124. static int rate_estimate_qscale(MpegEncContext *s)
  1125. {
  1126. INT64 total_bits = s->total_bits;
  1127. float q;
  1128. int qscale, diff, qmin;
  1129. if (s->pict_type == I_TYPE) {
  1130. s->wanted_bits += s->I_frame_bits;
  1131. } else {
  1132. s->wanted_bits += s->P_frame_bits;
  1133. }
  1134. diff = s->wanted_bits - total_bits;
  1135. q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
  1136. /* adjust for I frame */
  1137. if (s->pict_type == I_TYPE && !s->intra_only) {
  1138. q /= I_FRAME_SIZE_RATIO;
  1139. }
  1140. /* using a too small Q scale leeds to problems in mpeg1 and h263
  1141. because AC coefficients are clamped to 255 or 127 */
  1142. qmin = 3;
  1143. if (q < qmin)
  1144. q = qmin;
  1145. else if (q > 31)
  1146. q = 31;
  1147. qscale = (int)(q + 0.5);
  1148. #if defined(DEBUG)
  1149. printf("%d: total=%0.0f br=%0.1f diff=%d qest=%0.1f\n",
  1150. s->picture_number,
  1151. (double)total_bits,
  1152. (float)s->frame_rate / FRAME_RATE_BASE *
  1153. total_bits / s->picture_number,
  1154. diff, q);
  1155. #endif
  1156. return qscale;
  1157. }
  1158. AVCodec mpeg1video_encoder = {
  1159. "mpeg1video",
  1160. CODEC_TYPE_VIDEO,
  1161. CODEC_ID_MPEG1VIDEO,
  1162. sizeof(MpegEncContext),
  1163. MPV_encode_init,
  1164. MPV_encode_picture,
  1165. MPV_encode_end,
  1166. };
  1167. AVCodec h263_encoder = {
  1168. "h263",
  1169. CODEC_TYPE_VIDEO,
  1170. CODEC_ID_H263,
  1171. sizeof(MpegEncContext),
  1172. MPV_encode_init,
  1173. MPV_encode_picture,
  1174. MPV_encode_end,
  1175. };
  1176. AVCodec h263p_encoder = {
  1177. "h263p",
  1178. CODEC_TYPE_VIDEO,
  1179. CODEC_ID_H263P,
  1180. sizeof(MpegEncContext),
  1181. MPV_encode_init,
  1182. MPV_encode_picture,
  1183. MPV_encode_end,
  1184. };
  1185. AVCodec rv10_encoder = {
  1186. "rv10",
  1187. CODEC_TYPE_VIDEO,
  1188. CODEC_ID_RV10,
  1189. sizeof(MpegEncContext),
  1190. MPV_encode_init,
  1191. MPV_encode_picture,
  1192. MPV_encode_end,
  1193. };
  1194. AVCodec mjpeg_encoder = {
  1195. "mjpeg",
  1196. CODEC_TYPE_VIDEO,
  1197. CODEC_ID_MJPEG,
  1198. sizeof(MpegEncContext),
  1199. MPV_encode_init,
  1200. MPV_encode_picture,
  1201. MPV_encode_end,
  1202. };
  1203. AVCodec mpeg4_encoder = {
  1204. "mpeg4",
  1205. CODEC_TYPE_VIDEO,
  1206. CODEC_ID_MPEG4,
  1207. sizeof(MpegEncContext),
  1208. MPV_encode_init,
  1209. MPV_encode_picture,
  1210. MPV_encode_end,
  1211. };
  1212. AVCodec msmpeg4_encoder = {
  1213. "msmpeg4",
  1214. CODEC_TYPE_VIDEO,
  1215. CODEC_ID_MSMPEG4,
  1216. sizeof(MpegEncContext),
  1217. MPV_encode_init,
  1218. MPV_encode_picture,
  1219. MPV_encode_end,
  1220. };