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.

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