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.

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