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.

1451 lines
45KB

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