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.

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