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.

1436 lines
44KB

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