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.

1434 lines
45KB

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