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.

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