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.

1409 lines
43KB

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