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.

1404 lines
44KB

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