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.

1511 lines
47KB

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