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.

1674 lines
54KB

  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. s->last_non_b_pict_type= s->pict_type;
  378. for(i=0;i<3;i++) {
  379. /* swap next and last */
  380. tmp = s->last_picture[i];
  381. s->last_picture[i] = s->next_picture[i];
  382. s->next_picture[i] = tmp;
  383. s->current_picture[i] = tmp;
  384. }
  385. }
  386. }
  387. /* generic function for encode/decode called after a frame has been coded/decoded */
  388. void MPV_frame_end(MpegEncContext *s)
  389. {
  390. /* draw edge for correct motion prediction if outside */
  391. if (s->pict_type != B_TYPE && !s->intra_only) {
  392. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
  393. draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
  394. draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  395. draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  396. }else{
  397. /* mpeg4? / opendivx / xvid */
  398. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  399. draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  400. draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  401. }
  402. }
  403. emms_c();
  404. }
  405. int MPV_encode_picture(AVCodecContext *avctx,
  406. unsigned char *buf, int buf_size, void *data)
  407. {
  408. MpegEncContext *s = avctx->priv_data;
  409. AVPicture *pict = data;
  410. int i, j;
  411. if (s->fixed_qscale)
  412. s->qscale = avctx->quality;
  413. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  414. if (!s->intra_only) {
  415. /* first picture of GOP is intra */
  416. if ((s->picture_number % s->gop_size) == 0)
  417. s->pict_type = I_TYPE;
  418. else
  419. s->pict_type = P_TYPE;
  420. } else {
  421. s->pict_type = I_TYPE;
  422. }
  423. avctx->key_frame = (s->pict_type == I_TYPE);
  424. MPV_frame_start(s);
  425. for(i=0;i<3;i++) {
  426. UINT8 *src = pict->data[i];
  427. UINT8 *dest = s->current_picture[i];
  428. int src_wrap = pict->linesize[i];
  429. int dest_wrap = s->linesize;
  430. int w = s->width;
  431. int h = s->height;
  432. if (i >= 1) {
  433. dest_wrap >>= 1;
  434. w >>= 1;
  435. h >>= 1;
  436. }
  437. if(dest_wrap==src_wrap){
  438. s->new_picture[i] = pict->data[i];
  439. } else {
  440. for(j=0;j<h;j++) {
  441. memcpy(dest, src, w);
  442. dest += dest_wrap;
  443. src += src_wrap;
  444. }
  445. s->new_picture[i] = s->current_picture[i];
  446. }
  447. }
  448. encode_picture(s, s->picture_number);
  449. MPV_frame_end(s);
  450. s->picture_number++;
  451. if (s->out_format == FMT_MJPEG)
  452. mjpeg_picture_trailer(s);
  453. flush_put_bits(&s->pb);
  454. s->total_bits += (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  455. avctx->quality = s->qscale;
  456. if (avctx->get_psnr) {
  457. /* At this point pict->data should have the original frame */
  458. /* an s->current_picture should have the coded/decoded frame */
  459. get_psnr(pict->data, s->current_picture,
  460. pict->linesize, s->linesize, avctx);
  461. }
  462. return pbBufPtr(&s->pb) - s->pb.buf;
  463. }
  464. static inline int clip(int a, int amin, int amax)
  465. {
  466. if (a < amin)
  467. return amin;
  468. else if (a > amax)
  469. return amax;
  470. else
  471. return a;
  472. }
  473. static inline void gmc1_motion(MpegEncContext *s,
  474. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  475. int dest_offset,
  476. UINT8 **ref_picture, int src_offset,
  477. int h)
  478. {
  479. UINT8 *ptr;
  480. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  481. int motion_x, motion_y;
  482. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  483. motion_x= s->sprite_offset[0][0];
  484. motion_y= s->sprite_offset[0][1];
  485. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  486. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  487. motion_x<<=(3-s->sprite_warping_accuracy);
  488. motion_y<<=(3-s->sprite_warping_accuracy);
  489. src_x = clip(src_x, -16, s->width);
  490. if (src_x == s->width)
  491. motion_x =0;
  492. src_y = clip(src_y, -16, s->height);
  493. if (src_y == s->height)
  494. motion_y =0;
  495. linesize = s->linesize;
  496. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  497. dest_y+=dest_offset;
  498. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  499. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  500. motion_x= s->sprite_offset[1][0];
  501. motion_y= s->sprite_offset[1][1];
  502. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  503. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  504. motion_x<<=(3-s->sprite_warping_accuracy);
  505. motion_y<<=(3-s->sprite_warping_accuracy);
  506. src_x = clip(src_x, -8, s->width>>1);
  507. if (src_x == s->width>>1)
  508. motion_x =0;
  509. src_y = clip(src_y, -8, s->height>>1);
  510. if (src_y == s->height>>1)
  511. motion_y =0;
  512. offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
  513. ptr = ref_picture[1] + offset;
  514. gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  515. ptr = ref_picture[2] + offset;
  516. gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  517. return;
  518. }
  519. /* apply one mpeg motion vector to the three components */
  520. static inline void mpeg_motion(MpegEncContext *s,
  521. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  522. int dest_offset,
  523. UINT8 **ref_picture, int src_offset,
  524. int field_based, op_pixels_func *pix_op,
  525. int motion_x, int motion_y, int h)
  526. {
  527. UINT8 *ptr;
  528. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  529. if(s->quarter_sample)
  530. {
  531. motion_x>>=1;
  532. motion_y>>=1;
  533. }
  534. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  535. src_x = s->mb_x * 16 + (motion_x >> 1);
  536. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  537. /* WARNING: do no forget half pels */
  538. height = s->height >> field_based;
  539. src_x = clip(src_x, -16, s->width);
  540. if (src_x == s->width)
  541. dxy &= ~1;
  542. src_y = clip(src_y, -16, height);
  543. if (src_y == height)
  544. dxy &= ~2;
  545. linesize = s->linesize << field_based;
  546. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  547. dest_y += dest_offset;
  548. pix_op[dxy](dest_y, ptr, linesize, h);
  549. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  550. if (s->out_format == FMT_H263) {
  551. dxy = 0;
  552. if ((motion_x & 3) != 0)
  553. dxy |= 1;
  554. if ((motion_y & 3) != 0)
  555. dxy |= 2;
  556. mx = motion_x >> 2;
  557. my = motion_y >> 2;
  558. } else {
  559. mx = motion_x / 2;
  560. my = motion_y / 2;
  561. dxy = ((my & 1) << 1) | (mx & 1);
  562. mx >>= 1;
  563. my >>= 1;
  564. }
  565. src_x = s->mb_x * 8 + mx;
  566. src_y = s->mb_y * (8 >> field_based) + my;
  567. src_x = clip(src_x, -8, s->width >> 1);
  568. if (src_x == (s->width >> 1))
  569. dxy &= ~1;
  570. src_y = clip(src_y, -8, height >> 1);
  571. if (src_y == (height >> 1))
  572. dxy &= ~2;
  573. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  574. ptr = ref_picture[1] + offset;
  575. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  576. ptr = ref_picture[2] + offset;
  577. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  578. }
  579. static inline void qpel_motion(MpegEncContext *s,
  580. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  581. int dest_offset,
  582. UINT8 **ref_picture, int src_offset,
  583. int field_based, op_pixels_func *pix_op,
  584. qpel_mc_func *qpix_op,
  585. int motion_x, int motion_y, int h)
  586. {
  587. UINT8 *ptr;
  588. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  589. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  590. src_x = s->mb_x * 16 + (motion_x >> 2);
  591. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  592. height = s->height >> field_based;
  593. src_x = clip(src_x, -16, s->width);
  594. if (src_x == s->width)
  595. dxy &= ~3;
  596. src_y = clip(src_y, -16, height);
  597. if (src_y == height)
  598. dxy &= ~12;
  599. linesize = s->linesize << field_based;
  600. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  601. dest_y += dest_offset;
  602. //printf("%d %d %d\n", src_x, src_y, dxy);
  603. qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
  604. qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
  605. qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
  606. qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
  607. mx= (motion_x>>1) | (motion_x&1);
  608. my= (motion_y>>1) | (motion_y&1);
  609. dxy = 0;
  610. if ((mx & 3) != 0)
  611. dxy |= 1;
  612. if ((my & 3) != 0)
  613. dxy |= 2;
  614. mx = mx >> 2;
  615. my = my >> 2;
  616. src_x = s->mb_x * 8 + mx;
  617. src_y = s->mb_y * (8 >> field_based) + my;
  618. src_x = clip(src_x, -8, s->width >> 1);
  619. if (src_x == (s->width >> 1))
  620. dxy &= ~1;
  621. src_y = clip(src_y, -8, height >> 1);
  622. if (src_y == (height >> 1))
  623. dxy &= ~2;
  624. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  625. ptr = ref_picture[1] + offset;
  626. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  627. ptr = ref_picture[2] + offset;
  628. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  629. }
  630. static inline void MPV_motion(MpegEncContext *s,
  631. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  632. int dir, UINT8 **ref_picture,
  633. op_pixels_func *pix_op, qpel_mc_func *qpix_op)
  634. {
  635. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  636. int mb_x, mb_y, i;
  637. UINT8 *ptr, *dest;
  638. mb_x = s->mb_x;
  639. mb_y = s->mb_y;
  640. switch(s->mv_type) {
  641. case MV_TYPE_16X16:
  642. if(s->mcsel){
  643. #if 0
  644. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  645. ref_picture, 0,
  646. 0, pix_op,
  647. s->sprite_offset[0][0]>>3,
  648. s->sprite_offset[0][1]>>3,
  649. 16);
  650. #else
  651. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  652. ref_picture, 0,
  653. 16);
  654. #endif
  655. }else if(s->quarter_sample && dir==0){ //FIXME
  656. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  657. ref_picture, 0,
  658. 0, pix_op, qpix_op,
  659. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  660. }else{
  661. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  662. ref_picture, 0,
  663. 0, pix_op,
  664. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  665. }
  666. break;
  667. case MV_TYPE_8X8:
  668. for(i=0;i<4;i++) {
  669. motion_x = s->mv[dir][i][0];
  670. motion_y = s->mv[dir][i][1];
  671. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  672. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  673. src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
  674. /* WARNING: do no forget half pels */
  675. src_x = clip(src_x, -16, s->width);
  676. if (src_x == s->width)
  677. dxy &= ~1;
  678. src_y = clip(src_y, -16, s->height);
  679. if (src_y == s->height)
  680. dxy &= ~2;
  681. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  682. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  683. pix_op[dxy](dest, ptr, s->linesize, 8);
  684. }
  685. /* In case of 8X8, we construct a single chroma motion vector
  686. with a special rounding */
  687. mx = 0;
  688. my = 0;
  689. for(i=0;i<4;i++) {
  690. mx += s->mv[dir][i][0];
  691. my += s->mv[dir][i][1];
  692. }
  693. if (mx >= 0)
  694. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  695. else {
  696. mx = -mx;
  697. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  698. }
  699. if (my >= 0)
  700. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  701. else {
  702. my = -my;
  703. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  704. }
  705. dxy = ((my & 1) << 1) | (mx & 1);
  706. mx >>= 1;
  707. my >>= 1;
  708. src_x = mb_x * 8 + mx;
  709. src_y = mb_y * 8 + my;
  710. src_x = clip(src_x, -8, s->width/2);
  711. if (src_x == s->width/2)
  712. dxy &= ~1;
  713. src_y = clip(src_y, -8, s->height/2);
  714. if (src_y == s->height/2)
  715. dxy &= ~2;
  716. offset = (src_y * (s->linesize >> 1)) + src_x;
  717. ptr = ref_picture[1] + offset;
  718. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  719. ptr = ref_picture[2] + offset;
  720. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  721. break;
  722. case MV_TYPE_FIELD:
  723. if (s->picture_structure == PICT_FRAME) {
  724. /* top field */
  725. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  726. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  727. 1, pix_op,
  728. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  729. /* bottom field */
  730. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  731. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  732. 1, pix_op,
  733. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  734. } else {
  735. }
  736. break;
  737. }
  738. }
  739. /* put block[] to dest[] */
  740. static inline void put_dct(MpegEncContext *s,
  741. DCTELEM *block, int i, UINT8 *dest, int line_size)
  742. {
  743. if (!s->mpeg2)
  744. s->dct_unquantize(s, block, i, s->qscale);
  745. ff_idct (block);
  746. put_pixels_clamped(block, dest, line_size);
  747. }
  748. /* add block[] to dest[] */
  749. static inline void add_dct(MpegEncContext *s,
  750. DCTELEM *block, int i, UINT8 *dest, int line_size)
  751. {
  752. if (s->block_last_index[i] >= 0) {
  753. if (!s->mpeg2)
  754. if(s->encoding || (!s->h263_msmpeg4))
  755. s->dct_unquantize(s, block, i, s->qscale);
  756. ff_idct (block);
  757. add_pixels_clamped(block, dest, line_size);
  758. }
  759. }
  760. /* generic function called after a macroblock has been parsed by the
  761. decoder or after it has been encoded by the encoder.
  762. Important variables used:
  763. s->mb_intra : true if intra macroblock
  764. s->mv_dir : motion vector direction
  765. s->mv_type : motion vector type
  766. s->mv : motion vector
  767. s->interlaced_dct : true if interlaced dct used (mpeg2)
  768. */
  769. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  770. {
  771. int mb_x, mb_y;
  772. int dct_linesize, dct_offset;
  773. op_pixels_func *op_pix;
  774. qpel_mc_func *op_qpix;
  775. mb_x = s->mb_x;
  776. mb_y = s->mb_y;
  777. #ifdef FF_POSTPROCESS
  778. quant_store[mb_y][mb_x]=s->qscale;
  779. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  780. #endif
  781. /* update DC predictors for P macroblocks */
  782. if (!s->mb_intra) {
  783. if (s->h263_pred || s->h263_aic) {
  784. if(s->mbintra_table[mb_x + mb_y*s->mb_width])
  785. {
  786. int wrap, xy, v;
  787. s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
  788. wrap = 2 * s->mb_width + 2;
  789. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  790. v = 1024;
  791. s->dc_val[0][xy] = v;
  792. s->dc_val[0][xy + 1] = v;
  793. s->dc_val[0][xy + wrap] = v;
  794. s->dc_val[0][xy + 1 + wrap] = v;
  795. /* ac pred */
  796. memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
  797. memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
  798. memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
  799. memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
  800. if (s->h263_msmpeg4) {
  801. s->coded_block[xy] = 0;
  802. s->coded_block[xy + 1] = 0;
  803. s->coded_block[xy + wrap] = 0;
  804. s->coded_block[xy + 1 + wrap] = 0;
  805. }
  806. /* chroma */
  807. wrap = s->mb_width + 2;
  808. xy = mb_x + 1 + (mb_y + 1) * wrap;
  809. s->dc_val[1][xy] = v;
  810. s->dc_val[2][xy] = v;
  811. /* ac pred */
  812. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  813. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  814. }
  815. } else {
  816. s->last_dc[0] = 128 << s->intra_dc_precision;
  817. s->last_dc[1] = 128 << s->intra_dc_precision;
  818. s->last_dc[2] = 128 << s->intra_dc_precision;
  819. }
  820. }
  821. else if (s->h263_pred || s->h263_aic)
  822. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  823. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  824. if (s->out_format == FMT_H263) {
  825. if(s->pict_type!=B_TYPE){
  826. int xy, wrap, motion_x, motion_y;
  827. wrap = 2 * s->mb_width + 2;
  828. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  829. if (s->mb_intra) {
  830. motion_x = 0;
  831. motion_y = 0;
  832. goto motion_init;
  833. } else if (s->mv_type == MV_TYPE_16X16) {
  834. motion_x = s->mv[0][0][0];
  835. motion_y = s->mv[0][0][1];
  836. motion_init:
  837. /* no update if 8X8 because it has been done during parsing */
  838. s->motion_val[xy][0] = motion_x;
  839. s->motion_val[xy][1] = motion_y;
  840. s->motion_val[xy + 1][0] = motion_x;
  841. s->motion_val[xy + 1][1] = motion_y;
  842. s->motion_val[xy + wrap][0] = motion_x;
  843. s->motion_val[xy + wrap][1] = motion_y;
  844. s->motion_val[xy + 1 + wrap][0] = motion_x;
  845. s->motion_val[xy + 1 + wrap][1] = motion_y;
  846. }
  847. }
  848. }
  849. if (!s->intra_only) {
  850. UINT8 *dest_y, *dest_cb, *dest_cr;
  851. UINT8 *mbskip_ptr;
  852. /* avoid copy if macroblock skipped in last frame too */
  853. if (!s->encoding && s->pict_type != B_TYPE) {
  854. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  855. if (s->mb_skiped) {
  856. s->mb_skiped = 0;
  857. /* if previous was skipped too, then nothing to do ! */
  858. if (*mbskip_ptr != 0)
  859. goto the_end;
  860. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  861. } else {
  862. *mbskip_ptr = 0; /* not skipped */
  863. }
  864. }
  865. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  866. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  867. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  868. if (s->interlaced_dct) {
  869. dct_linesize = s->linesize * 2;
  870. dct_offset = s->linesize;
  871. } else {
  872. dct_linesize = s->linesize;
  873. dct_offset = s->linesize * 8;
  874. }
  875. if (!s->mb_intra) {
  876. /* motion handling */
  877. if (!s->no_rounding){
  878. op_pix = put_pixels_tab;
  879. op_qpix= qpel_mc_rnd_tab;
  880. }else{
  881. op_pix = put_no_rnd_pixels_tab;
  882. op_qpix= qpel_mc_no_rnd_tab;
  883. }
  884. if (s->mv_dir & MV_DIR_FORWARD) {
  885. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  886. if (!s->no_rounding)
  887. op_pix = avg_pixels_tab;
  888. else
  889. op_pix = avg_no_rnd_pixels_tab;
  890. }
  891. if (s->mv_dir & MV_DIR_BACKWARD) {
  892. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  893. }
  894. /* add dct residue */
  895. add_dct(s, block[0], 0, dest_y, dct_linesize);
  896. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  897. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  898. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  899. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  900. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  901. } else {
  902. /* dct only in intra block */
  903. put_dct(s, block[0], 0, dest_y, dct_linesize);
  904. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  905. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  906. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  907. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  908. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  909. }
  910. }
  911. the_end:
  912. emms_c();
  913. }
  914. static void encode_picture(MpegEncContext *s, int picture_number)
  915. {
  916. int mb_x, mb_y, wrap, last_gob, pdif = 0;
  917. UINT8 *ptr;
  918. int i, motion_x, motion_y;
  919. s->picture_number = picture_number;
  920. if (!s->fixed_qscale)
  921. s->qscale = rate_estimate_qscale(s);
  922. /* precompute matrix */
  923. if (s->out_format == FMT_MJPEG) {
  924. /* for mjpeg, we do include qscale in the matrix */
  925. s->intra_matrix[0] = default_intra_matrix[0];
  926. for(i=1;i<64;i++)
  927. s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
  928. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
  929. } else {
  930. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
  931. convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
  932. }
  933. switch(s->out_format) {
  934. case FMT_MJPEG:
  935. mjpeg_picture_header(s);
  936. break;
  937. case FMT_H263:
  938. if (s->h263_msmpeg4)
  939. msmpeg4_encode_picture_header(s, picture_number);
  940. else if (s->h263_pred)
  941. mpeg4_encode_picture_header(s, picture_number);
  942. else if (s->h263_rv10)
  943. rv10_encode_picture_header(s, picture_number);
  944. else
  945. h263_encode_picture_header(s, picture_number);
  946. break;
  947. case FMT_MPEG1:
  948. mpeg1_encode_picture_header(s, picture_number);
  949. break;
  950. }
  951. /* init last dc values */
  952. /* note: quant matrix value (8) is implied here */
  953. s->last_dc[0] = 128;
  954. s->last_dc[1] = 128;
  955. s->last_dc[2] = 128;
  956. s->mb_incr = 1;
  957. s->last_mv[0][0][0] = 0;
  958. s->last_mv[0][0][1] = 0;
  959. s->mv_type = MV_TYPE_16X16;
  960. s->mv_dir = MV_DIR_FORWARD;
  961. /* Get the GOB height based on picture height */
  962. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  963. if (s->height <= 400)
  964. s->gob_index = 1;
  965. else if (s->height <= 800)
  966. s->gob_index = 2;
  967. else
  968. s->gob_index = 4;
  969. }
  970. /* Reset the average MB variance */
  971. s->avg_mb_var = 0;
  972. /* Estimate motion for every MB */
  973. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  974. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  975. s->mb_x = mb_x;
  976. s->mb_y = mb_y;
  977. /* compute motion vector and macro block type (intra or non intra) */
  978. motion_x = 0;
  979. motion_y = 0;
  980. if (s->pict_type == P_TYPE) {
  981. s->mb_intra = estimate_motion(s, mb_x, mb_y,
  982. &motion_x,
  983. &motion_y);
  984. } else {
  985. s->mb_intra = 1;
  986. }
  987. /* Store MB type and MV */
  988. s->mb_type[mb_y * s->mb_width + mb_x] = s->mb_intra;
  989. s->mv_table[0][mb_y * s->mb_width + mb_x] = motion_x;
  990. s->mv_table[1][mb_y * s->mb_width + mb_x] = motion_y;
  991. }
  992. }
  993. s->avg_mb_var = s->avg_mb_var / s->mb_num;
  994. s->block_wrap[0]=
  995. s->block_wrap[1]=
  996. s->block_wrap[2]=
  997. s->block_wrap[3]= s->mb_width*2 + 2;
  998. s->block_wrap[4]=
  999. s->block_wrap[5]= s->mb_width + 2;
  1000. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1001. /* Put GOB header based on RTP MTU */
  1002. /* TODO: Put all this stuff in a separate generic function */
  1003. if (s->rtp_mode) {
  1004. if (!mb_y) {
  1005. s->ptr_lastgob = s->pb.buf;
  1006. s->ptr_last_mb_line = s->pb.buf;
  1007. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1008. last_gob = h263_encode_gob_header(s, mb_y);
  1009. if (last_gob) {
  1010. s->first_gob_line = 1;
  1011. }
  1012. }
  1013. }
  1014. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1015. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1016. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1017. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1018. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1019. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1020. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1021. s->mb_x = mb_x;
  1022. s->mb_y = mb_y;
  1023. s->block_index[0]+=2;
  1024. s->block_index[1]+=2;
  1025. s->block_index[2]+=2;
  1026. s->block_index[3]+=2;
  1027. s->block_index[4]++;
  1028. s->block_index[5]++;
  1029. #if 0
  1030. /* compute motion vector and macro block type (intra or non intra) */
  1031. motion_x = 0;
  1032. motion_y = 0;
  1033. if (s->pict_type == P_TYPE) {
  1034. s->mb_intra = estimate_motion(s, mb_x, mb_y,
  1035. &motion_x,
  1036. &motion_y);
  1037. } else {
  1038. s->mb_intra = 1;
  1039. }
  1040. #endif
  1041. s->mb_intra = s->mb_type[mb_y * s->mb_width + mb_x];
  1042. motion_x = s->mv_table[0][mb_y * s->mb_width + mb_x];
  1043. motion_y = s->mv_table[1][mb_y * s->mb_width + mb_x];
  1044. /* get the pixels */
  1045. wrap = s->linesize;
  1046. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1047. get_pixels(s->block[0], ptr, wrap);
  1048. get_pixels(s->block[1], ptr + 8, wrap);
  1049. get_pixels(s->block[2], ptr + 8 * wrap, wrap);
  1050. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1051. wrap = s->linesize >> 1;
  1052. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1053. get_pixels(s->block[4], ptr, wrap);
  1054. wrap = s->linesize >> 1;
  1055. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1056. get_pixels(s->block[5], ptr, wrap);
  1057. /* subtract previous frame if non intra */
  1058. if (!s->mb_intra) {
  1059. int dxy, offset, mx, my;
  1060. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1061. ptr = s->last_picture[0] +
  1062. ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
  1063. (mb_x * 16 + (motion_x >> 1));
  1064. sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
  1065. sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
  1066. sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
  1067. sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
  1068. if (s->out_format == FMT_H263) {
  1069. /* special rounding for h263 */
  1070. dxy = 0;
  1071. if ((motion_x & 3) != 0)
  1072. dxy |= 1;
  1073. if ((motion_y & 3) != 0)
  1074. dxy |= 2;
  1075. mx = motion_x >> 2;
  1076. my = motion_y >> 2;
  1077. } else {
  1078. mx = motion_x / 2;
  1079. my = motion_y / 2;
  1080. dxy = ((my & 1) << 1) | (mx & 1);
  1081. mx >>= 1;
  1082. my >>= 1;
  1083. }
  1084. offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
  1085. ptr = s->last_picture[1] + offset;
  1086. sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
  1087. ptr = s->last_picture[2] + offset;
  1088. sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
  1089. }
  1090. emms_c();
  1091. #if 0
  1092. {
  1093. float adap_parm;
  1094. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1095. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1096. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1097. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1098. s->qscale, adap_parm, s->qscale*adap_parm,
  1099. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1100. }
  1101. #endif
  1102. /* DCT & quantize */
  1103. if (s->h263_msmpeg4) {
  1104. msmpeg4_dc_scale(s);
  1105. } else if (s->h263_pred) {
  1106. h263_dc_scale(s);
  1107. } else {
  1108. /* default quantization values */
  1109. s->y_dc_scale = 8;
  1110. s->c_dc_scale = 8;
  1111. }
  1112. for(i=0;i<6;i++) {
  1113. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
  1114. }
  1115. /* huffman encode */
  1116. switch(s->out_format) {
  1117. case FMT_MPEG1:
  1118. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1119. break;
  1120. case FMT_H263:
  1121. if (s->h263_msmpeg4)
  1122. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1123. else if(s->h263_pred)
  1124. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1125. else
  1126. h263_encode_mb(s, s->block, motion_x, motion_y);
  1127. break;
  1128. case FMT_MJPEG:
  1129. mjpeg_encode_mb(s, s->block);
  1130. break;
  1131. }
  1132. /* decompress blocks so that we keep the state of the decoder */
  1133. s->mv[0][0][0] = motion_x;
  1134. s->mv[0][0][1] = motion_y;
  1135. MPV_decode_mb(s, s->block);
  1136. }
  1137. /* Obtain average GOB size for RTP */
  1138. if (s->rtp_mode) {
  1139. if (!mb_y)
  1140. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  1141. else if (!(mb_y % s->gob_index)) {
  1142. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  1143. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  1144. }
  1145. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  1146. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  1147. s->first_gob_line = 0;
  1148. }
  1149. }
  1150. if (s->h263_msmpeg4 && s->pict_type == I_TYPE)
  1151. msmpeg4_encode_ext_header(s);
  1152. //if (s->gob_number)
  1153. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  1154. /* Send the last GOB if RTP */
  1155. if (s->rtp_mode) {
  1156. flush_put_bits(&s->pb);
  1157. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1158. /* Call the RTP callback to send the last GOB */
  1159. if (s->rtp_callback)
  1160. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  1161. s->ptr_lastgob = pbBufPtr(&s->pb);
  1162. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  1163. }
  1164. }
  1165. static int dct_quantize_c(MpegEncContext *s,
  1166. DCTELEM *block, int n,
  1167. int qscale)
  1168. {
  1169. int i, j, level, last_non_zero, q;
  1170. const int *qmat;
  1171. int minLevel, maxLevel;
  1172. if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
  1173. /* mpeg4 */
  1174. minLevel= -2048;
  1175. maxLevel= 2047;
  1176. }else if(s->out_format==FMT_MPEG1){
  1177. /* mpeg1 */
  1178. minLevel= -255;
  1179. maxLevel= 255;
  1180. }else if(s->out_format==FMT_MJPEG){
  1181. /* (m)jpeg */
  1182. minLevel= -1023;
  1183. maxLevel= 1023;
  1184. }else{
  1185. /* h263 / msmpeg4 */
  1186. minLevel= -128;
  1187. maxLevel= 127;
  1188. }
  1189. av_fdct (block);
  1190. /* we need this permutation so that we correct the IDCT
  1191. permutation. will be moved into DCT code */
  1192. block_permute(block);
  1193. if (s->mb_intra) {
  1194. if (n < 4)
  1195. q = s->y_dc_scale;
  1196. else
  1197. q = s->c_dc_scale;
  1198. q = q << 3;
  1199. /* note: block[0] is assumed to be positive */
  1200. block[0] = (block[0] + (q >> 1)) / q;
  1201. i = 1;
  1202. last_non_zero = 0;
  1203. if (s->out_format == FMT_H263) {
  1204. qmat = s->q_non_intra_matrix;
  1205. } else {
  1206. qmat = s->q_intra_matrix;
  1207. }
  1208. } else {
  1209. i = 0;
  1210. last_non_zero = -1;
  1211. qmat = s->q_non_intra_matrix;
  1212. }
  1213. for(;i<64;i++) {
  1214. j = zigzag_direct[i];
  1215. level = block[j];
  1216. level = level * qmat[j];
  1217. #ifdef PARANOID
  1218. {
  1219. static int count = 0;
  1220. int level1, level2, qmat1;
  1221. double val;
  1222. if (qmat == s->q_non_intra_matrix) {
  1223. qmat1 = default_non_intra_matrix[j] * s->qscale;
  1224. } else {
  1225. qmat1 = default_intra_matrix[j] * s->qscale;
  1226. }
  1227. if (av_fdct != jpeg_fdct_ifast)
  1228. val = ((double)block[j] * 8.0) / (double)qmat1;
  1229. else
  1230. val = ((double)block[j] * 8.0 * 2048.0) /
  1231. ((double)qmat1 * aanscales[j]);
  1232. level1 = (int)val;
  1233. level2 = level / (1 << (QMAT_SHIFT - 3));
  1234. if (level1 != level2) {
  1235. fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
  1236. count, level2, level1, block[j], qmat1, qmat[j],
  1237. val);
  1238. count++;
  1239. }
  1240. }
  1241. #endif
  1242. /* XXX: slight error for the low range. Test should be equivalent to
  1243. (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
  1244. (QMAT_SHIFT - 3)))
  1245. */
  1246. if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
  1247. level) {
  1248. level = level / (1 << (QMAT_SHIFT - 3));
  1249. /* XXX: currently, this code is not optimal. the range should be:
  1250. mpeg1: -255..255
  1251. mpeg2: -2048..2047
  1252. h263: -128..127
  1253. mpeg4: -2048..2047
  1254. */
  1255. if (level > maxLevel)
  1256. level = maxLevel;
  1257. else if (level < minLevel)
  1258. level = minLevel;
  1259. block[j] = level;
  1260. last_non_zero = i;
  1261. } else {
  1262. block[j] = 0;
  1263. }
  1264. }
  1265. return last_non_zero;
  1266. }
  1267. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1268. DCTELEM *block, int n, int qscale)
  1269. {
  1270. int i, level, nCoeffs;
  1271. const UINT16 *quant_matrix;
  1272. if(s->alternate_scan) nCoeffs= 64;
  1273. else nCoeffs= s->block_last_index[n]+1;
  1274. if (s->mb_intra) {
  1275. if (n < 4)
  1276. block[0] = block[0] * s->y_dc_scale;
  1277. else
  1278. block[0] = block[0] * s->c_dc_scale;
  1279. /* XXX: only mpeg1 */
  1280. quant_matrix = s->intra_matrix;
  1281. for(i=1;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 = (int)(level * qscale * quant_matrix[j]) >> 3;
  1288. level = (level - 1) | 1;
  1289. level = -level;
  1290. } else {
  1291. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1292. level = (level - 1) | 1;
  1293. }
  1294. #ifdef PARANOID
  1295. if (level < -2048 || level > 2047)
  1296. fprintf(stderr, "unquant error %d %d\n", i, level);
  1297. #endif
  1298. block[j] = level;
  1299. }
  1300. }
  1301. } else {
  1302. i = 0;
  1303. quant_matrix = s->non_intra_matrix;
  1304. for(;i<nCoeffs;i++) {
  1305. int j= zigzag_direct[i];
  1306. level = block[j];
  1307. if (level) {
  1308. if (level < 0) {
  1309. level = -level;
  1310. level = (((level << 1) + 1) * qscale *
  1311. ((int) (quant_matrix[j]))) >> 4;
  1312. level = (level - 1) | 1;
  1313. level = -level;
  1314. } else {
  1315. level = (((level << 1) + 1) * qscale *
  1316. ((int) (quant_matrix[j]))) >> 4;
  1317. level = (level - 1) | 1;
  1318. }
  1319. #ifdef PARANOID
  1320. if (level < -2048 || level > 2047)
  1321. fprintf(stderr, "unquant error %d %d\n", i, level);
  1322. #endif
  1323. block[j] = level;
  1324. }
  1325. }
  1326. }
  1327. }
  1328. static void dct_unquantize_h263_c(MpegEncContext *s,
  1329. DCTELEM *block, int n, int qscale)
  1330. {
  1331. int i, level, qmul, qadd;
  1332. int nCoeffs;
  1333. if (s->mb_intra) {
  1334. if (!s->h263_aic) {
  1335. if (n < 4)
  1336. block[0] = block[0] * s->y_dc_scale;
  1337. else
  1338. block[0] = block[0] * s->c_dc_scale;
  1339. }
  1340. i = 1;
  1341. nCoeffs= 64; //does not allways use zigzag table
  1342. } else {
  1343. i = 0;
  1344. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  1345. }
  1346. qmul = s->qscale << 1;
  1347. if (s->h263_aic && s->mb_intra)
  1348. qadd = 0;
  1349. else
  1350. qadd = (s->qscale - 1) | 1;
  1351. for(;i<nCoeffs;i++) {
  1352. level = block[i];
  1353. if (level) {
  1354. if (level < 0) {
  1355. level = level * qmul - qadd;
  1356. } else {
  1357. level = level * qmul + qadd;
  1358. }
  1359. #ifdef PARANOID
  1360. if (level < -2048 || level > 2047)
  1361. fprintf(stderr, "unquant error %d %d\n", i, level);
  1362. #endif
  1363. block[i] = level;
  1364. }
  1365. }
  1366. }
  1367. /* rate control */
  1368. /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
  1369. #define I_FRAME_SIZE_RATIO 3.0
  1370. #define QSCALE_K 20
  1371. static void rate_control_init(MpegEncContext *s)
  1372. {
  1373. s->wanted_bits = 0;
  1374. if (s->intra_only) {
  1375. s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
  1376. s->P_frame_bits = s->I_frame_bits;
  1377. } else {
  1378. s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
  1379. (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
  1380. s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
  1381. }
  1382. #if defined(DEBUG)
  1383. printf("I_frame_size=%d P_frame_size=%d\n",
  1384. s->I_frame_bits, s->P_frame_bits);
  1385. #endif
  1386. }
  1387. /*
  1388. * This heuristic is rather poor, but at least we do not have to
  1389. * change the qscale at every macroblock.
  1390. */
  1391. static int rate_estimate_qscale(MpegEncContext *s)
  1392. {
  1393. INT64 diff, total_bits = s->total_bits;
  1394. float q;
  1395. int qscale, qmin;
  1396. if (s->pict_type == I_TYPE) {
  1397. s->wanted_bits += s->I_frame_bits;
  1398. } else {
  1399. s->wanted_bits += s->P_frame_bits;
  1400. }
  1401. diff = s->wanted_bits - total_bits;
  1402. q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
  1403. /* adjust for I frame */
  1404. if (s->pict_type == I_TYPE && !s->intra_only) {
  1405. q /= I_FRAME_SIZE_RATIO;
  1406. }
  1407. /* using a too small Q scale leeds to problems in mpeg1 and h263
  1408. because AC coefficients are clamped to 255 or 127 */
  1409. qmin = 3;
  1410. if (q < qmin)
  1411. q = qmin;
  1412. else if (q > 31)
  1413. q = 31;
  1414. qscale = (int)(q + 0.5);
  1415. #if defined(DEBUG)
  1416. printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n",
  1417. s->picture_number,
  1418. (double)total_bits,
  1419. (double)s->wanted_bits,
  1420. (float)s->frame_rate / FRAME_RATE_BASE *
  1421. total_bits / s->picture_number,
  1422. (int)diff, q);
  1423. #endif
  1424. return qscale;
  1425. }
  1426. AVCodec mpeg1video_encoder = {
  1427. "mpeg1video",
  1428. CODEC_TYPE_VIDEO,
  1429. CODEC_ID_MPEG1VIDEO,
  1430. sizeof(MpegEncContext),
  1431. MPV_encode_init,
  1432. MPV_encode_picture,
  1433. MPV_encode_end,
  1434. };
  1435. AVCodec h263_encoder = {
  1436. "h263",
  1437. CODEC_TYPE_VIDEO,
  1438. CODEC_ID_H263,
  1439. sizeof(MpegEncContext),
  1440. MPV_encode_init,
  1441. MPV_encode_picture,
  1442. MPV_encode_end,
  1443. };
  1444. AVCodec h263p_encoder = {
  1445. "h263p",
  1446. CODEC_TYPE_VIDEO,
  1447. CODEC_ID_H263P,
  1448. sizeof(MpegEncContext),
  1449. MPV_encode_init,
  1450. MPV_encode_picture,
  1451. MPV_encode_end,
  1452. };
  1453. AVCodec rv10_encoder = {
  1454. "rv10",
  1455. CODEC_TYPE_VIDEO,
  1456. CODEC_ID_RV10,
  1457. sizeof(MpegEncContext),
  1458. MPV_encode_init,
  1459. MPV_encode_picture,
  1460. MPV_encode_end,
  1461. };
  1462. AVCodec mjpeg_encoder = {
  1463. "mjpeg",
  1464. CODEC_TYPE_VIDEO,
  1465. CODEC_ID_MJPEG,
  1466. sizeof(MpegEncContext),
  1467. MPV_encode_init,
  1468. MPV_encode_picture,
  1469. MPV_encode_end,
  1470. };
  1471. AVCodec mpeg4_encoder = {
  1472. "mpeg4",
  1473. CODEC_TYPE_VIDEO,
  1474. CODEC_ID_MPEG4,
  1475. sizeof(MpegEncContext),
  1476. MPV_encode_init,
  1477. MPV_encode_picture,
  1478. MPV_encode_end,
  1479. };
  1480. AVCodec msmpeg4_encoder = {
  1481. "msmpeg4",
  1482. CODEC_TYPE_VIDEO,
  1483. CODEC_ID_MSMPEG4,
  1484. sizeof(MpegEncContext),
  1485. MPV_encode_init,
  1486. MPV_encode_picture,
  1487. MPV_encode_end,
  1488. };