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.

1417 lines
43KB

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