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.

1651 lines
52KB

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