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.

2128 lines
69KB

  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. * 4MV & hq encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <string.h>
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #include "mpegvideo.h"
  28. #ifdef USE_FASTMEMCPY
  29. #include "fastmemcpy.h"
  30. #endif
  31. static void encode_picture(MpegEncContext *s, int picture_number);
  32. static void rate_control_init(MpegEncContext *s);
  33. static int rate_estimate_qscale(MpegEncContext *s);
  34. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  35. DCTELEM *block, int n, int qscale);
  36. static void dct_unquantize_h263_c(MpegEncContext *s,
  37. DCTELEM *block, int n, int qscale);
  38. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
  39. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale);
  40. int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c;
  41. void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
  42. #define EDGE_WIDTH 16
  43. /* enable all paranoid tests for rounding, overflows, etc... */
  44. //#define PARANOID
  45. //#define DEBUG
  46. /* for jpeg fast DCT */
  47. #define CONST_BITS 14
  48. static const unsigned short aanscales[64] = {
  49. /* precomputed values scaled up by 14 bits */
  50. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  51. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  52. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  53. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  54. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  55. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  56. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  57. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  58. };
  59. static UINT8 h263_chroma_roundtab[16] = {
  60. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  61. };
  62. static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
  63. static UINT8 default_fcode_tab[MAX_MV*2+1];
  64. /* default motion estimation */
  65. int motion_estimation_method = ME_LOG;
  66. extern UINT8 zigzag_end[64];
  67. static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale)
  68. {
  69. int i;
  70. if (av_fdct == jpeg_fdct_ifast) {
  71. for(i=0;i<64;i++) {
  72. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  73. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  74. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  75. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  76. qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
  77. (aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
  78. }
  79. } else {
  80. for(i=0;i<64;i++) {
  81. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  82. So 16 <= qscale * quant_matrix[i] <= 7905
  83. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  84. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  85. */
  86. qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  87. qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
  88. }
  89. }
  90. }
  91. /* init common structure for both encoder and decoder */
  92. int MPV_common_init(MpegEncContext *s)
  93. {
  94. int c_size, i;
  95. UINT8 *pict;
  96. if (s->out_format == FMT_H263)
  97. s->dct_unquantize = dct_unquantize_h263_c;
  98. else
  99. s->dct_unquantize = dct_unquantize_mpeg1_c;
  100. #ifdef HAVE_MMX
  101. MPV_common_init_mmx(s);
  102. #endif
  103. s->mb_width = (s->width + 15) / 16;
  104. s->mb_height = (s->height + 15) / 16;
  105. s->mb_num = s->mb_width * s->mb_height;
  106. s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
  107. for(i=0;i<3;i++) {
  108. int w, h, shift, pict_start;
  109. w = s->linesize;
  110. h = s->mb_height * 16 + 2 * EDGE_WIDTH;
  111. shift = (i == 0) ? 0 : 1;
  112. c_size = (w >> shift) * (h >> shift);
  113. pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
  114. pict = av_mallocz(c_size);
  115. if (pict == NULL)
  116. goto fail;
  117. s->last_picture_base[i] = pict;
  118. s->last_picture[i] = pict + pict_start;
  119. pict = av_mallocz(c_size);
  120. if (pict == NULL)
  121. goto fail;
  122. s->next_picture_base[i] = pict;
  123. s->next_picture[i] = pict + pict_start;
  124. if (s->has_b_frames) {
  125. pict = av_mallocz(c_size);
  126. if (pict == NULL)
  127. goto fail;
  128. s->aux_picture_base[i] = pict;
  129. s->aux_picture[i] = pict + pict_start;
  130. }
  131. }
  132. if (s->encoding) {
  133. /* Allocate MB type table */
  134. s->mb_type = av_mallocz(s->mb_num * sizeof(char));
  135. if (s->mb_type == NULL) {
  136. perror("malloc");
  137. goto fail;
  138. }
  139. s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
  140. if (s->mb_var == NULL) {
  141. perror("malloc");
  142. goto fail;
  143. }
  144. /* Allocate MV table */
  145. /* By now we just have one MV per MB */
  146. s->mv_table[0] = av_mallocz(s->mb_num * sizeof(INT16));
  147. s->mv_table[1] = av_mallocz(s->mb_num * sizeof(INT16));
  148. if (s->mv_table[1] == NULL || s->mv_table[0] == NULL) {
  149. perror("malloc");
  150. goto fail;
  151. }
  152. }
  153. if (s->out_format == FMT_H263 || s->encoding) {
  154. int size;
  155. /* MV prediction */
  156. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  157. s->motion_val = malloc(size * 2 * sizeof(INT16));
  158. if (s->motion_val == NULL)
  159. goto fail;
  160. memset(s->motion_val, 0, size * 2 * sizeof(INT16));
  161. }
  162. if (s->h263_pred || s->h263_plus) {
  163. int y_size, c_size, i, size;
  164. /* dc values */
  165. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  166. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  167. size = y_size + 2 * c_size;
  168. s->dc_val[0] = malloc(size * sizeof(INT16));
  169. if (s->dc_val[0] == NULL)
  170. goto fail;
  171. s->dc_val[1] = s->dc_val[0] + y_size;
  172. s->dc_val[2] = s->dc_val[1] + c_size;
  173. for(i=0;i<size;i++)
  174. s->dc_val[0][i] = 1024;
  175. /* ac values */
  176. s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
  177. if (s->ac_val[0] == NULL)
  178. goto fail;
  179. s->ac_val[1] = s->ac_val[0] + y_size;
  180. s->ac_val[2] = s->ac_val[1] + c_size;
  181. /* cbp values */
  182. s->coded_block = av_mallocz(y_size);
  183. if (!s->coded_block)
  184. goto fail;
  185. /* which mb is a intra block */
  186. s->mbintra_table = av_mallocz(s->mb_num);
  187. if (!s->mbintra_table)
  188. goto fail;
  189. memset(s->mbintra_table, 1, s->mb_num);
  190. }
  191. /* default structure is frame */
  192. s->picture_structure = PICT_FRAME;
  193. /* init macroblock skip table */
  194. if (!s->encoding) {
  195. s->mbskip_table = av_mallocz(s->mb_num);
  196. if (!s->mbskip_table)
  197. goto fail;
  198. }
  199. s->block= s->intra_block;
  200. s->context_initialized = 1;
  201. return 0;
  202. fail:
  203. MPV_common_end(s);
  204. return -1;
  205. }
  206. /* init common structure for both encoder and decoder */
  207. void MPV_common_end(MpegEncContext *s)
  208. {
  209. int i;
  210. if (s->mb_type)
  211. free(s->mb_type);
  212. if (s->mb_var)
  213. free(s->mb_var);
  214. if (s->mv_table[0])
  215. free(s->mv_table[0]);
  216. if (s->mv_table[1])
  217. free(s->mv_table[1]);
  218. if (s->motion_val)
  219. free(s->motion_val);
  220. if (s->dc_val[0])
  221. free(s->dc_val[0]);
  222. if (s->ac_val[0])
  223. free(s->ac_val[0]);
  224. if (s->coded_block)
  225. free(s->coded_block);
  226. if (s->mbintra_table)
  227. free(s->mbintra_table);
  228. if (s->mbskip_table)
  229. free(s->mbskip_table);
  230. for(i=0;i<3;i++) {
  231. if (s->last_picture_base[i])
  232. free(s->last_picture_base[i]);
  233. if (s->next_picture_base[i])
  234. free(s->next_picture_base[i]);
  235. if (s->has_b_frames)
  236. free(s->aux_picture_base[i]);
  237. }
  238. s->context_initialized = 0;
  239. }
  240. /* init video encoder */
  241. int MPV_encode_init(AVCodecContext *avctx)
  242. {
  243. MpegEncContext *s = avctx->priv_data;
  244. int i;
  245. s->bit_rate = avctx->bit_rate;
  246. s->bit_rate_tolerance = avctx->bit_rate_tolerance;
  247. s->frame_rate = avctx->frame_rate;
  248. s->width = avctx->width;
  249. s->height = avctx->height;
  250. s->gop_size = avctx->gop_size;
  251. s->rtp_mode = avctx->rtp_mode;
  252. s->rtp_payload_size = avctx->rtp_payload_size;
  253. if (avctx->rtp_callback)
  254. s->rtp_callback = avctx->rtp_callback;
  255. s->qmin= avctx->qmin;
  256. s->qmax= avctx->qmax;
  257. s->max_qdiff= avctx->max_qdiff;
  258. s->qcompress= avctx->qcompress;
  259. s->qblur= avctx->qblur;
  260. s->avctx = avctx;
  261. s->aspect_ratio_info= avctx->aspect_ratio_info;
  262. s->flags= avctx->flags;
  263. if (s->gop_size <= 1) {
  264. s->intra_only = 1;
  265. s->gop_size = 12;
  266. } else {
  267. s->intra_only = 0;
  268. }
  269. s->full_search = motion_estimation_method;
  270. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  271. switch(avctx->codec->id) {
  272. case CODEC_ID_MPEG1VIDEO:
  273. s->out_format = FMT_MPEG1;
  274. break;
  275. case CODEC_ID_MJPEG:
  276. s->out_format = FMT_MJPEG;
  277. s->intra_only = 1; /* force intra only for jpeg */
  278. s->mjpeg_write_tables = 1; /* write all tables */
  279. s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
  280. s->mjpeg_vsample[1] = 1; /* the only currently supported values */
  281. s->mjpeg_vsample[2] = 1;
  282. s->mjpeg_hsample[0] = 2;
  283. s->mjpeg_hsample[1] = 1;
  284. s->mjpeg_hsample[2] = 1;
  285. if (mjpeg_init(s) < 0)
  286. return -1;
  287. break;
  288. case CODEC_ID_H263:
  289. if (h263_get_picture_format(s->width, s->height) == 7) {
  290. printf("Input picture size isn't suitable for h263 codec! try h263+\n");
  291. return -1;
  292. }
  293. s->out_format = FMT_H263;
  294. break;
  295. case CODEC_ID_H263P:
  296. s->out_format = FMT_H263;
  297. s->rtp_mode = 1;
  298. s->rtp_payload_size = 1200;
  299. s->h263_plus = 1;
  300. s->unrestricted_mv = 1;
  301. /* These are just to be sure */
  302. s->umvplus = 0;
  303. s->umvplus_dec = 0;
  304. break;
  305. case CODEC_ID_RV10:
  306. s->out_format = FMT_H263;
  307. s->h263_rv10 = 1;
  308. break;
  309. case CODEC_ID_MPEG4:
  310. s->out_format = FMT_H263;
  311. s->h263_pred = 1;
  312. s->unrestricted_mv = 1;
  313. break;
  314. case CODEC_ID_MSMPEG4:
  315. s->out_format = FMT_H263;
  316. s->h263_msmpeg4 = 1;
  317. s->h263_pred = 1;
  318. s->unrestricted_mv = 1;
  319. break;
  320. default:
  321. return -1;
  322. }
  323. if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){
  324. printf("4MV is currently only supported in HQ mode\n");
  325. return -1;
  326. }
  327. { /* set up some save defaults, some codecs might override them later */
  328. static int done=0;
  329. if(!done){
  330. int i;
  331. done=1;
  332. memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
  333. memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
  334. for(i=-16; i<16; i++){
  335. default_fcode_tab[i + MAX_MV]= 1;
  336. }
  337. }
  338. }
  339. s->mv_penalty= default_mv_penalty;
  340. s->fcode_tab= default_fcode_tab;
  341. if (s->out_format == FMT_H263)
  342. h263_encode_init(s);
  343. else if (s->out_format == FMT_MPEG1)
  344. mpeg1_encode_init(s);
  345. /* dont use mv_penalty table for crap MV as it would be confused */
  346. if(s->full_search<4) s->mv_penalty= default_mv_penalty;
  347. s->encoding = 1;
  348. /* init */
  349. if (MPV_common_init(s) < 0)
  350. return -1;
  351. /* init default q matrix */
  352. for(i=0;i<64;i++) {
  353. s->intra_matrix[i] = default_intra_matrix[i];
  354. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  355. }
  356. /* rate control init */
  357. rate_control_init(s);
  358. s->picture_number = 0;
  359. s->picture_in_gop_number = 0;
  360. s->fake_picture_number = 0;
  361. /* motion detector init */
  362. s->f_code = 1;
  363. return 0;
  364. }
  365. int MPV_encode_end(AVCodecContext *avctx)
  366. {
  367. MpegEncContext *s = avctx->priv_data;
  368. #ifdef STATS
  369. print_stats();
  370. #endif
  371. MPV_common_end(s);
  372. if (s->out_format == FMT_MJPEG)
  373. mjpeg_close(s);
  374. return 0;
  375. }
  376. /* draw the edges of width 'w' of an image of size width, height */
  377. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
  378. {
  379. UINT8 *ptr, *last_line;
  380. int i;
  381. last_line = buf + (height - 1) * wrap;
  382. for(i=0;i<w;i++) {
  383. /* top and bottom */
  384. memcpy(buf - (i + 1) * wrap, buf, width);
  385. memcpy(last_line + (i + 1) * wrap, last_line, width);
  386. }
  387. /* left and right */
  388. ptr = buf;
  389. for(i=0;i<height;i++) {
  390. memset(ptr - w, ptr[0], w);
  391. memset(ptr + width, ptr[width-1], w);
  392. ptr += wrap;
  393. }
  394. /* corners */
  395. for(i=0;i<w;i++) {
  396. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  397. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  398. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  399. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  400. }
  401. }
  402. /* generic function for encode/decode called before a frame is coded/decoded */
  403. void MPV_frame_start(MpegEncContext *s)
  404. {
  405. int i;
  406. UINT8 *tmp;
  407. s->mb_skiped = 0;
  408. if (s->pict_type == B_TYPE) {
  409. for(i=0;i<3;i++) {
  410. s->current_picture[i] = s->aux_picture[i];
  411. }
  412. } else {
  413. s->last_non_b_pict_type= s->pict_type;
  414. for(i=0;i<3;i++) {
  415. /* swap next and last */
  416. tmp = s->last_picture[i];
  417. s->last_picture[i] = s->next_picture[i];
  418. s->next_picture[i] = tmp;
  419. s->current_picture[i] = tmp;
  420. }
  421. }
  422. }
  423. /* generic function for encode/decode called after a frame has been coded/decoded */
  424. void MPV_frame_end(MpegEncContext *s)
  425. {
  426. /* draw edge for correct motion prediction if outside */
  427. if (s->pict_type != B_TYPE && !s->intra_only) {
  428. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
  429. draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
  430. draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  431. draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  432. }else{
  433. /* mpeg4? / opendivx / xvid */
  434. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  435. draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  436. draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  437. }
  438. }
  439. emms_c();
  440. }
  441. int MPV_encode_picture(AVCodecContext *avctx,
  442. unsigned char *buf, int buf_size, void *data)
  443. {
  444. MpegEncContext *s = avctx->priv_data;
  445. AVPicture *pict = data;
  446. int i, j;
  447. if (s->fixed_qscale)
  448. s->qscale = avctx->quality;
  449. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  450. s->force_type= avctx->force_type;
  451. if (!s->intra_only) {
  452. /* first picture of GOP is intra */
  453. if (s->picture_in_gop_number % s->gop_size==0 || s->force_type==I_TYPE){
  454. s->picture_in_gop_number=0;
  455. s->pict_type = I_TYPE;
  456. }else
  457. s->pict_type = P_TYPE;
  458. } else {
  459. s->pict_type = I_TYPE;
  460. }
  461. MPV_frame_start(s);
  462. for(i=0;i<3;i++) {
  463. UINT8 *src = pict->data[i];
  464. UINT8 *dest = s->current_picture[i];
  465. int src_wrap = pict->linesize[i];
  466. int dest_wrap = s->linesize;
  467. int w = s->width;
  468. int h = s->height;
  469. if (i >= 1) {
  470. dest_wrap >>= 1;
  471. w >>= 1;
  472. h >>= 1;
  473. }
  474. if(dest_wrap==src_wrap){
  475. s->new_picture[i] = pict->data[i];
  476. } else {
  477. for(j=0;j<h;j++) {
  478. memcpy(dest, src, w);
  479. dest += dest_wrap;
  480. src += src_wrap;
  481. }
  482. s->new_picture[i] = s->current_picture[i];
  483. }
  484. }
  485. encode_picture(s, s->picture_number);
  486. avctx->key_frame = (s->pict_type == I_TYPE);
  487. avctx->header_bits = s->header_bits;
  488. avctx->mv_bits = s->mv_bits;
  489. avctx->misc_bits = s->misc_bits;
  490. avctx->i_tex_bits = s->i_tex_bits;
  491. avctx->p_tex_bits = s->p_tex_bits;
  492. avctx->i_count = s->i_count;
  493. avctx->p_count = s->p_count;
  494. avctx->skip_count = s->skip_count;
  495. MPV_frame_end(s);
  496. s->picture_number++;
  497. s->picture_in_gop_number++;
  498. if (s->out_format == FMT_MJPEG)
  499. mjpeg_picture_trailer(s);
  500. flush_put_bits(&s->pb);
  501. s->last_frame_bits= s->frame_bits;
  502. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  503. s->total_bits += s->frame_bits;
  504. avctx->frame_bits = s->frame_bits;
  505. //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
  506. //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
  507. avctx->quality = s->qscale;
  508. if (avctx->get_psnr) {
  509. /* At this point pict->data should have the original frame */
  510. /* an s->current_picture should have the coded/decoded frame */
  511. get_psnr(pict->data, s->current_picture,
  512. pict->linesize, s->linesize, avctx);
  513. }
  514. return pbBufPtr(&s->pb) - s->pb.buf;
  515. }
  516. static inline int clip(int a, int amin, int amax)
  517. {
  518. if (a < amin)
  519. return amin;
  520. else if (a > amax)
  521. return amax;
  522. else
  523. return a;
  524. }
  525. static inline void gmc1_motion(MpegEncContext *s,
  526. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  527. int dest_offset,
  528. UINT8 **ref_picture, int src_offset,
  529. int h)
  530. {
  531. UINT8 *ptr;
  532. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  533. int motion_x, motion_y;
  534. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  535. motion_x= s->sprite_offset[0][0];
  536. motion_y= s->sprite_offset[0][1];
  537. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  538. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  539. motion_x<<=(3-s->sprite_warping_accuracy);
  540. motion_y<<=(3-s->sprite_warping_accuracy);
  541. src_x = clip(src_x, -16, s->width);
  542. if (src_x == s->width)
  543. motion_x =0;
  544. src_y = clip(src_y, -16, s->height);
  545. if (src_y == s->height)
  546. motion_y =0;
  547. linesize = s->linesize;
  548. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  549. dest_y+=dest_offset;
  550. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  551. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  552. motion_x= s->sprite_offset[1][0];
  553. motion_y= s->sprite_offset[1][1];
  554. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  555. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  556. motion_x<<=(3-s->sprite_warping_accuracy);
  557. motion_y<<=(3-s->sprite_warping_accuracy);
  558. src_x = clip(src_x, -8, s->width>>1);
  559. if (src_x == s->width>>1)
  560. motion_x =0;
  561. src_y = clip(src_y, -8, s->height>>1);
  562. if (src_y == s->height>>1)
  563. motion_y =0;
  564. offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
  565. ptr = ref_picture[1] + offset;
  566. gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  567. ptr = ref_picture[2] + offset;
  568. gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  569. return;
  570. }
  571. /* apply one mpeg motion vector to the three components */
  572. static inline void mpeg_motion(MpegEncContext *s,
  573. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  574. int dest_offset,
  575. UINT8 **ref_picture, int src_offset,
  576. int field_based, op_pixels_func *pix_op,
  577. int motion_x, int motion_y, int h)
  578. {
  579. UINT8 *ptr;
  580. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  581. if(s->quarter_sample)
  582. {
  583. motion_x>>=1;
  584. motion_y>>=1;
  585. }
  586. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  587. src_x = s->mb_x * 16 + (motion_x >> 1);
  588. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  589. /* WARNING: do no forget half pels */
  590. height = s->height >> field_based;
  591. src_x = clip(src_x, -16, s->width);
  592. if (src_x == s->width)
  593. dxy &= ~1;
  594. src_y = clip(src_y, -16, height);
  595. if (src_y == height)
  596. dxy &= ~2;
  597. linesize = s->linesize << field_based;
  598. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  599. dest_y += dest_offset;
  600. pix_op[dxy](dest_y, ptr, linesize, h);
  601. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  602. if (s->out_format == FMT_H263) {
  603. dxy = 0;
  604. if ((motion_x & 3) != 0)
  605. dxy |= 1;
  606. if ((motion_y & 3) != 0)
  607. dxy |= 2;
  608. mx = motion_x >> 2;
  609. my = motion_y >> 2;
  610. } else {
  611. mx = motion_x / 2;
  612. my = motion_y / 2;
  613. dxy = ((my & 1) << 1) | (mx & 1);
  614. mx >>= 1;
  615. my >>= 1;
  616. }
  617. src_x = s->mb_x * 8 + mx;
  618. src_y = s->mb_y * (8 >> field_based) + my;
  619. src_x = clip(src_x, -8, s->width >> 1);
  620. if (src_x == (s->width >> 1))
  621. dxy &= ~1;
  622. src_y = clip(src_y, -8, height >> 1);
  623. if (src_y == (height >> 1))
  624. dxy &= ~2;
  625. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  626. ptr = ref_picture[1] + offset;
  627. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  628. ptr = ref_picture[2] + offset;
  629. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  630. }
  631. static inline void qpel_motion(MpegEncContext *s,
  632. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  633. int dest_offset,
  634. UINT8 **ref_picture, int src_offset,
  635. int field_based, op_pixels_func *pix_op,
  636. qpel_mc_func *qpix_op,
  637. int motion_x, int motion_y, int h)
  638. {
  639. UINT8 *ptr;
  640. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  641. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  642. src_x = s->mb_x * 16 + (motion_x >> 2);
  643. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  644. height = s->height >> field_based;
  645. src_x = clip(src_x, -16, s->width);
  646. if (src_x == s->width)
  647. dxy &= ~3;
  648. src_y = clip(src_y, -16, height);
  649. if (src_y == height)
  650. dxy &= ~12;
  651. linesize = s->linesize << field_based;
  652. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  653. dest_y += dest_offset;
  654. //printf("%d %d %d\n", src_x, src_y, dxy);
  655. qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
  656. qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
  657. qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
  658. qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
  659. mx= (motion_x>>1) | (motion_x&1);
  660. my= (motion_y>>1) | (motion_y&1);
  661. dxy = 0;
  662. if ((mx & 3) != 0)
  663. dxy |= 1;
  664. if ((my & 3) != 0)
  665. dxy |= 2;
  666. mx = mx >> 2;
  667. my = my >> 2;
  668. src_x = s->mb_x * 8 + mx;
  669. src_y = s->mb_y * (8 >> field_based) + my;
  670. src_x = clip(src_x, -8, s->width >> 1);
  671. if (src_x == (s->width >> 1))
  672. dxy &= ~1;
  673. src_y = clip(src_y, -8, height >> 1);
  674. if (src_y == (height >> 1))
  675. dxy &= ~2;
  676. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  677. ptr = ref_picture[1] + offset;
  678. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  679. ptr = ref_picture[2] + offset;
  680. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  681. }
  682. static inline void MPV_motion(MpegEncContext *s,
  683. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  684. int dir, UINT8 **ref_picture,
  685. op_pixels_func *pix_op, qpel_mc_func *qpix_op)
  686. {
  687. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  688. int mb_x, mb_y, i;
  689. UINT8 *ptr, *dest;
  690. mb_x = s->mb_x;
  691. mb_y = s->mb_y;
  692. switch(s->mv_type) {
  693. case MV_TYPE_16X16:
  694. if(s->mcsel){
  695. #if 0
  696. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  697. ref_picture, 0,
  698. 0, pix_op,
  699. s->sprite_offset[0][0]>>3,
  700. s->sprite_offset[0][1]>>3,
  701. 16);
  702. #else
  703. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  704. ref_picture, 0,
  705. 16);
  706. #endif
  707. }else if(s->quarter_sample && dir==0){ //FIXME
  708. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  709. ref_picture, 0,
  710. 0, pix_op, qpix_op,
  711. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  712. }else{
  713. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  714. ref_picture, 0,
  715. 0, pix_op,
  716. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  717. }
  718. break;
  719. case MV_TYPE_8X8:
  720. for(i=0;i<4;i++) {
  721. motion_x = s->mv[dir][i][0];
  722. motion_y = s->mv[dir][i][1];
  723. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  724. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  725. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  726. /* WARNING: do no forget half pels */
  727. src_x = clip(src_x, -16, s->width);
  728. if (src_x == s->width)
  729. dxy &= ~1;
  730. src_y = clip(src_y, -16, s->height);
  731. if (src_y == s->height)
  732. dxy &= ~2;
  733. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  734. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  735. pix_op[dxy](dest, ptr, s->linesize, 8);
  736. }
  737. /* In case of 8X8, we construct a single chroma motion vector
  738. with a special rounding */
  739. mx = 0;
  740. my = 0;
  741. for(i=0;i<4;i++) {
  742. mx += s->mv[dir][i][0];
  743. my += s->mv[dir][i][1];
  744. }
  745. if (mx >= 0)
  746. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  747. else {
  748. mx = -mx;
  749. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  750. }
  751. if (my >= 0)
  752. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  753. else {
  754. my = -my;
  755. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  756. }
  757. dxy = ((my & 1) << 1) | (mx & 1);
  758. mx >>= 1;
  759. my >>= 1;
  760. src_x = mb_x * 8 + mx;
  761. src_y = mb_y * 8 + my;
  762. src_x = clip(src_x, -8, s->width/2);
  763. if (src_x == s->width/2)
  764. dxy &= ~1;
  765. src_y = clip(src_y, -8, s->height/2);
  766. if (src_y == s->height/2)
  767. dxy &= ~2;
  768. offset = (src_y * (s->linesize >> 1)) + src_x;
  769. ptr = ref_picture[1] + offset;
  770. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  771. ptr = ref_picture[2] + offset;
  772. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  773. break;
  774. case MV_TYPE_FIELD:
  775. if (s->picture_structure == PICT_FRAME) {
  776. /* top field */
  777. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  778. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  779. 1, pix_op,
  780. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  781. /* bottom field */
  782. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  783. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  784. 1, pix_op,
  785. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  786. } else {
  787. }
  788. break;
  789. }
  790. }
  791. /* put block[] to dest[] */
  792. static inline void put_dct(MpegEncContext *s,
  793. DCTELEM *block, int i, UINT8 *dest, int line_size)
  794. {
  795. if (!s->mpeg2)
  796. s->dct_unquantize(s, block, i, s->qscale);
  797. ff_idct (block);
  798. put_pixels_clamped(block, dest, line_size);
  799. }
  800. /* add block[] to dest[] */
  801. static inline void add_dct(MpegEncContext *s,
  802. DCTELEM *block, int i, UINT8 *dest, int line_size)
  803. {
  804. if (s->block_last_index[i] >= 0) {
  805. if (!s->mpeg2)
  806. if(s->encoding || (!s->h263_msmpeg4))
  807. s->dct_unquantize(s, block, i, s->qscale);
  808. ff_idct (block);
  809. add_pixels_clamped(block, dest, line_size);
  810. }
  811. }
  812. /* generic function called after a macroblock has been parsed by the
  813. decoder or after it has been encoded by the encoder.
  814. Important variables used:
  815. s->mb_intra : true if intra macroblock
  816. s->mv_dir : motion vector direction
  817. s->mv_type : motion vector type
  818. s->mv : motion vector
  819. s->interlaced_dct : true if interlaced dct used (mpeg2)
  820. */
  821. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  822. {
  823. int mb_x, mb_y;
  824. int dct_linesize, dct_offset;
  825. op_pixels_func *op_pix;
  826. qpel_mc_func *op_qpix;
  827. mb_x = s->mb_x;
  828. mb_y = s->mb_y;
  829. #ifdef FF_POSTPROCESS
  830. quant_store[mb_y][mb_x]=s->qscale;
  831. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  832. #endif
  833. /* update DC predictors for P macroblocks */
  834. if (!s->mb_intra) {
  835. if (s->h263_pred || s->h263_aic) {
  836. if(s->mbintra_table[mb_x + mb_y*s->mb_width])
  837. {
  838. int wrap, xy, v;
  839. s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
  840. wrap = 2 * s->mb_width + 2;
  841. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  842. v = 1024;
  843. s->dc_val[0][xy] = v;
  844. s->dc_val[0][xy + 1] = v;
  845. s->dc_val[0][xy + wrap] = v;
  846. s->dc_val[0][xy + 1 + wrap] = v;
  847. /* ac pred */
  848. memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
  849. memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
  850. memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
  851. memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
  852. if (s->h263_msmpeg4) {
  853. s->coded_block[xy] = 0;
  854. s->coded_block[xy + 1] = 0;
  855. s->coded_block[xy + wrap] = 0;
  856. s->coded_block[xy + 1 + wrap] = 0;
  857. }
  858. /* chroma */
  859. wrap = s->mb_width + 2;
  860. xy = mb_x + 1 + (mb_y + 1) * wrap;
  861. s->dc_val[1][xy] = v;
  862. s->dc_val[2][xy] = v;
  863. /* ac pred */
  864. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  865. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  866. }
  867. } else {
  868. s->last_dc[0] = 128 << s->intra_dc_precision;
  869. s->last_dc[1] = 128 << s->intra_dc_precision;
  870. s->last_dc[2] = 128 << s->intra_dc_precision;
  871. }
  872. }
  873. else if (s->h263_pred || s->h263_aic)
  874. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  875. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  876. if (s->out_format == FMT_H263) {
  877. if(s->pict_type!=B_TYPE){
  878. int xy, wrap, motion_x, motion_y;
  879. wrap = 2 * s->mb_width + 2;
  880. xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
  881. if (s->mb_intra) {
  882. motion_x = 0;
  883. motion_y = 0;
  884. goto motion_init;
  885. } else if (s->mv_type == MV_TYPE_16X16) {
  886. motion_x = s->mv[0][0][0];
  887. motion_y = s->mv[0][0][1];
  888. motion_init:
  889. /* no update if 8X8 because it has been done during parsing */
  890. s->motion_val[xy][0] = motion_x;
  891. s->motion_val[xy][1] = motion_y;
  892. s->motion_val[xy + 1][0] = motion_x;
  893. s->motion_val[xy + 1][1] = motion_y;
  894. s->motion_val[xy + wrap][0] = motion_x;
  895. s->motion_val[xy + wrap][1] = motion_y;
  896. s->motion_val[xy + 1 + wrap][0] = motion_x;
  897. s->motion_val[xy + 1 + wrap][1] = motion_y;
  898. }
  899. }
  900. }
  901. if (!s->intra_only) {
  902. UINT8 *dest_y, *dest_cb, *dest_cr;
  903. UINT8 *mbskip_ptr;
  904. /* avoid copy if macroblock skipped in last frame too */
  905. if (!s->encoding && s->pict_type != B_TYPE) {
  906. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  907. if (s->mb_skiped) {
  908. s->mb_skiped = 0;
  909. /* if previous was skipped too, then nothing to do ! */
  910. if (*mbskip_ptr != 0)
  911. goto the_end;
  912. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  913. } else {
  914. *mbskip_ptr = 0; /* not skipped */
  915. }
  916. }
  917. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  918. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  919. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  920. if (s->interlaced_dct) {
  921. dct_linesize = s->linesize * 2;
  922. dct_offset = s->linesize;
  923. } else {
  924. dct_linesize = s->linesize;
  925. dct_offset = s->linesize * 8;
  926. }
  927. if (!s->mb_intra) {
  928. /* motion handling */
  929. if (!s->no_rounding){
  930. op_pix = put_pixels_tab;
  931. op_qpix= qpel_mc_rnd_tab;
  932. }else{
  933. op_pix = put_no_rnd_pixels_tab;
  934. op_qpix= qpel_mc_no_rnd_tab;
  935. }
  936. if (s->mv_dir & MV_DIR_FORWARD) {
  937. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  938. if (!s->no_rounding)
  939. op_pix = avg_pixels_tab;
  940. else
  941. op_pix = avg_no_rnd_pixels_tab;
  942. }
  943. if (s->mv_dir & MV_DIR_BACKWARD) {
  944. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  945. }
  946. /* add dct residue */
  947. add_dct(s, block[0], 0, dest_y, dct_linesize);
  948. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  949. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  950. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  951. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  952. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  953. } else {
  954. /* dct only in intra block */
  955. put_dct(s, block[0], 0, dest_y, dct_linesize);
  956. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  957. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  958. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  959. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  960. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  961. }
  962. }
  963. the_end:
  964. emms_c(); //FIXME remove
  965. }
  966. static void encode_mb(MpegEncContext *s)
  967. {
  968. int wrap;
  969. const int mb_x= s->mb_x;
  970. const int mb_y= s->mb_y;
  971. UINT8 *ptr;
  972. const int motion_x= s->mv[0][0][0];
  973. const int motion_y= s->mv[0][0][1];
  974. int i;
  975. /* get the pixels */
  976. wrap = s->linesize;
  977. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  978. get_pixels(s->block[0], ptr, wrap);
  979. get_pixels(s->block[1], ptr + 8, wrap);
  980. get_pixels(s->block[2], ptr + 8 * wrap, wrap);
  981. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  982. wrap = s->linesize >> 1;
  983. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  984. get_pixels(s->block[4], ptr, wrap);
  985. wrap = s->linesize >> 1;
  986. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  987. get_pixels(s->block[5], ptr, wrap);
  988. /* subtract previous frame if non intra */
  989. if (!s->mb_intra) {
  990. int dxy, offset, mx, my;
  991. if(s->mv_type==MV_TYPE_16X16){
  992. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  993. ptr = s->last_picture[0] +
  994. ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
  995. (mb_x * 16 + (motion_x >> 1));
  996. sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
  997. sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
  998. sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
  999. sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
  1000. if (s->out_format == FMT_H263) {
  1001. /* special rounding for h263 */
  1002. dxy = 0;
  1003. if ((motion_x & 3) != 0)
  1004. dxy |= 1;
  1005. if ((motion_y & 3) != 0)
  1006. dxy |= 2;
  1007. mx = motion_x >> 2;
  1008. my = motion_y >> 2;
  1009. } else {
  1010. mx = motion_x / 2;
  1011. my = motion_y / 2;
  1012. dxy = ((my & 1) << 1) | (mx & 1);
  1013. mx >>= 1;
  1014. my >>= 1;
  1015. }
  1016. offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
  1017. ptr = s->last_picture[1] + offset;
  1018. sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
  1019. ptr = s->last_picture[2] + offset;
  1020. sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
  1021. }else{
  1022. int src_x, src_y;
  1023. for(i=0;i<4;i++) {
  1024. int motion_x = s->mv[0][i][0];
  1025. int motion_y = s->mv[0][i][1];
  1026. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1027. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1028. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1029. ptr = s->last_picture[0] + (src_y * s->linesize) + (src_x);
  1030. sub_pixels_2(s->block[i], ptr, s->linesize, dxy);
  1031. }
  1032. /* In case of 8X8, we construct a single chroma motion vector
  1033. with a special rounding */
  1034. mx = 0;
  1035. my = 0;
  1036. for(i=0;i<4;i++) {
  1037. mx += s->mv[0][i][0];
  1038. my += s->mv[0][i][1];
  1039. }
  1040. if (mx >= 0)
  1041. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1042. else {
  1043. mx = -mx;
  1044. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1045. }
  1046. if (my >= 0)
  1047. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1048. else {
  1049. my = -my;
  1050. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1051. }
  1052. dxy = ((my & 1) << 1) | (mx & 1);
  1053. mx >>= 1;
  1054. my >>= 1;
  1055. src_x = mb_x * 8 + mx;
  1056. src_y = mb_y * 8 + my;
  1057. src_x = clip(src_x, -8, s->width/2);
  1058. if (src_x == s->width/2)
  1059. dxy &= ~1;
  1060. src_y = clip(src_y, -8, s->height/2);
  1061. if (src_y == s->height/2)
  1062. dxy &= ~2;
  1063. offset = (src_y * (s->linesize >> 1)) + src_x;
  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. }
  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 if(s->h263_pred)
  1103. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1104. else
  1105. h263_encode_mb(s, s->block, motion_x, motion_y);
  1106. break;
  1107. case FMT_MJPEG:
  1108. mjpeg_encode_mb(s, s->block);
  1109. break;
  1110. }
  1111. }
  1112. static void copy_bits(PutBitContext *pb, UINT8 *src, int length)
  1113. {
  1114. int bytes= length>>3;
  1115. int bits= length&7;
  1116. int i;
  1117. for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
  1118. put_bits(pb, bits, src[i]>>(8-bits));
  1119. }
  1120. static void encode_picture(MpegEncContext *s, int picture_number)
  1121. {
  1122. int mb_x, mb_y, last_gob, pdif = 0;
  1123. int i;
  1124. int bits;
  1125. MpegEncContext best_s;
  1126. UINT8 bit_buf[4][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
  1127. s->picture_number = picture_number;
  1128. s->block_wrap[0]=
  1129. s->block_wrap[1]=
  1130. s->block_wrap[2]=
  1131. s->block_wrap[3]= s->mb_width*2 + 2;
  1132. s->block_wrap[4]=
  1133. s->block_wrap[5]= s->mb_width + 2;
  1134. s->last_mc_mb_var = s->mc_mb_var;
  1135. /* Reset the average MB variance */
  1136. s->avg_mb_var = 0;
  1137. s->mc_mb_var = 0;
  1138. /* Estimate motion for every MB */
  1139. if(s->pict_type == P_TYPE){
  1140. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1141. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1142. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1143. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1144. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1145. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1146. s->mb_x = mb_x;
  1147. s->mb_y = mb_y;
  1148. s->block_index[0]+=2;
  1149. s->block_index[1]+=2;
  1150. s->block_index[2]+=2;
  1151. s->block_index[3]+=2;
  1152. /* compute motion vector & mb_type and store in context */
  1153. estimate_motion(s, mb_x, mb_y);
  1154. // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
  1155. }
  1156. }
  1157. emms_c();
  1158. }else{
  1159. /* I-Frame */
  1160. //FIXME do we need to zero them?
  1161. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  1162. memset(s->mv_table[0] , 0, sizeof(INT16)*s->mb_width*s->mb_height);
  1163. memset(s->mv_table[1] , 0, sizeof(INT16)*s->mb_width*s->mb_height);
  1164. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1165. }
  1166. if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE && (!s->force_type)){ //FIXME subtract MV bits
  1167. s->pict_type= I_TYPE;
  1168. s->picture_in_gop_number=0;
  1169. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1170. //printf("Scene change detected, encoding as I Frame\n");
  1171. }
  1172. /* find best f_code for ME which do unlimited searches */
  1173. if(s->pict_type==P_TYPE && s->full_search>3){
  1174. int mv_num[8];
  1175. int i;
  1176. int loose=0;
  1177. UINT8 * fcode_tab= s->fcode_tab;
  1178. for(i=0; i<8; i++) mv_num[i]=0;
  1179. for(i=0; i<s->mb_num; i++){
  1180. if(s->mb_type[i] & MB_TYPE_INTER){
  1181. mv_num[ fcode_tab[s->mv_table[0][i] + MAX_MV] ]++;
  1182. mv_num[ fcode_tab[s->mv_table[1][i] + MAX_MV] ]++;
  1183. //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i);
  1184. }
  1185. //else printf("I");
  1186. }
  1187. for(i=MAX_FCODE; i>1; i--){
  1188. loose+= mv_num[i];
  1189. if(loose > 10) break; //FIXME this is pretty ineffective
  1190. }
  1191. s->f_code= i;
  1192. /* for(i=0; i<=MAX_FCODE; i++){
  1193. printf("%d ", mv_num[i]);
  1194. }
  1195. printf("\n");*/
  1196. }else{
  1197. s->f_code= 1;
  1198. }
  1199. //printf("f_code %d ///\n", s->f_code);
  1200. /* convert MBs with too long MVs to I-Blocks */
  1201. if(s->pict_type==P_TYPE){
  1202. int i, x, y;
  1203. const int f_code= s->f_code;
  1204. UINT8 * fcode_tab= s->fcode_tab;
  1205. //FIXME try to clip instead of intra izing ;)
  1206. /* clip / convert to intra 16x16 type MVs */
  1207. for(i=0; i<s->mb_num; i++){
  1208. if(s->mb_type[i]&MB_TYPE_INTER){
  1209. if( fcode_tab[s->mv_table[0][i] + MAX_MV] > f_code
  1210. || fcode_tab[s->mv_table[0][i] + MAX_MV] == 0
  1211. || fcode_tab[s->mv_table[1][i] + MAX_MV] > f_code
  1212. || fcode_tab[s->mv_table[1][i] + MAX_MV] == 0 ){
  1213. s->mb_type[i] &= ~MB_TYPE_INTER;
  1214. s->mb_type[i] |= MB_TYPE_INTRA;
  1215. s->mv_table[0][i] = 0;
  1216. s->mv_table[1][i] = 0;
  1217. }
  1218. }
  1219. }
  1220. if(s->flags&CODEC_FLAG_4MV){
  1221. int wrap= 2+ s->mb_width*2;
  1222. /* clip / convert to intra 8x8 type MVs */
  1223. for(y=0; y<s->mb_height; y++){
  1224. int xy= (y*2 + 1)*wrap + 1;
  1225. i= y*s->mb_width;
  1226. for(x=0; x<s->mb_width; x++){
  1227. if(s->mb_type[i]&MB_TYPE_INTER4V){
  1228. int block;
  1229. for(block=0; block<4; block++){
  1230. int off= (block& 1) + (block>>1)*wrap;
  1231. int mx= s->motion_val[ xy + off ][0];
  1232. int my= s->motion_val[ xy + off ][1];
  1233. if( fcode_tab[mx + MAX_MV] > f_code
  1234. || fcode_tab[mx + MAX_MV] == 0
  1235. || fcode_tab[my + MAX_MV] > f_code
  1236. || fcode_tab[my + MAX_MV] == 0 ){
  1237. s->mb_type[i] &= ~MB_TYPE_INTER4V;
  1238. s->mb_type[i] |= MB_TYPE_INTRA;
  1239. }
  1240. }
  1241. xy+=2;
  1242. i++;
  1243. }
  1244. }
  1245. }
  1246. }
  1247. }
  1248. // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
  1249. if (!s->fixed_qscale)
  1250. s->qscale = rate_estimate_qscale(s);
  1251. /* precompute matrix */
  1252. if (s->out_format == FMT_MJPEG) {
  1253. /* for mjpeg, we do include qscale in the matrix */
  1254. s->intra_matrix[0] = default_intra_matrix[0];
  1255. for(i=1;i<64;i++)
  1256. s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
  1257. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
  1258. } else {
  1259. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
  1260. convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
  1261. }
  1262. s->last_bits= get_bit_count(&s->pb);
  1263. switch(s->out_format) {
  1264. case FMT_MJPEG:
  1265. mjpeg_picture_header(s);
  1266. break;
  1267. case FMT_H263:
  1268. if (s->h263_msmpeg4)
  1269. msmpeg4_encode_picture_header(s, picture_number);
  1270. else if (s->h263_pred)
  1271. mpeg4_encode_picture_header(s, picture_number);
  1272. else if (s->h263_rv10)
  1273. rv10_encode_picture_header(s, picture_number);
  1274. else
  1275. h263_encode_picture_header(s, picture_number);
  1276. break;
  1277. case FMT_MPEG1:
  1278. mpeg1_encode_picture_header(s, picture_number);
  1279. break;
  1280. }
  1281. bits= get_bit_count(&s->pb);
  1282. s->header_bits= bits - s->last_bits;
  1283. s->last_bits= bits;
  1284. s->mv_bits=0;
  1285. s->misc_bits=0;
  1286. s->i_tex_bits=0;
  1287. s->p_tex_bits=0;
  1288. s->i_count=0;
  1289. s->p_count=0;
  1290. s->skip_count=0;
  1291. /* init last dc values */
  1292. /* note: quant matrix value (8) is implied here */
  1293. s->last_dc[0] = 128;
  1294. s->last_dc[1] = 128;
  1295. s->last_dc[2] = 128;
  1296. s->mb_incr = 1;
  1297. s->last_mv[0][0][0] = 0;
  1298. s->last_mv[0][0][1] = 0;
  1299. /* Get the GOB height based on picture height */
  1300. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  1301. if (s->height <= 400)
  1302. s->gob_index = 1;
  1303. else if (s->height <= 800)
  1304. s->gob_index = 2;
  1305. else
  1306. s->gob_index = 4;
  1307. }
  1308. s->avg_mb_var = s->avg_mb_var / s->mb_num;
  1309. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1310. /* Put GOB header based on RTP MTU */
  1311. /* TODO: Put all this stuff in a separate generic function */
  1312. if (s->rtp_mode) {
  1313. if (!mb_y) {
  1314. s->ptr_lastgob = s->pb.buf;
  1315. s->ptr_last_mb_line = s->pb.buf;
  1316. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1317. last_gob = h263_encode_gob_header(s, mb_y);
  1318. if (last_gob) {
  1319. s->first_gob_line = 1;
  1320. }
  1321. }
  1322. }
  1323. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1324. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1325. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1326. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1327. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1328. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1329. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1330. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  1331. PutBitContext pb;
  1332. int d;
  1333. int dmin=10000000;
  1334. int best=0;
  1335. s->mb_x = mb_x;
  1336. s->mb_y = mb_y;
  1337. s->block_index[0]+=2;
  1338. s->block_index[1]+=2;
  1339. s->block_index[2]+=2;
  1340. s->block_index[3]+=2;
  1341. s->block_index[4]++;
  1342. s->block_index[5]++;
  1343. s->mv_dir = MV_DIR_FORWARD;
  1344. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  1345. pb= s->pb;
  1346. if(mb_type&MB_TYPE_INTER){
  1347. s->mv_type = MV_TYPE_16X16;
  1348. s->mb_intra= 0;
  1349. s->mv[0][0][0] = s->mv_table[0][mb_y * s->mb_width + mb_x];
  1350. s->mv[0][0][1] = s->mv_table[1][mb_y * s->mb_width + mb_x];
  1351. init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
  1352. s->block= s->inter_block;
  1353. encode_mb(s);
  1354. d= get_bit_count(&s->pb);
  1355. if(d<dmin){
  1356. flush_put_bits(&s->pb);
  1357. dmin=d;
  1358. best_s.mv[0][0][0]= s->mv[0][0][0];
  1359. best_s.mv[0][0][1]= s->mv[0][0][1];
  1360. best_s.mb_intra= 0;
  1361. best_s.mv_type = MV_TYPE_16X16;
  1362. best_s.pb=s->pb;
  1363. best_s.block= s->block;
  1364. best=1;
  1365. for(i=0; i<6; i++)
  1366. best_s.block_last_index[i]= s->block_last_index[i];
  1367. }
  1368. }
  1369. if(mb_type&MB_TYPE_INTER4V){
  1370. s->mv_type = MV_TYPE_8X8;
  1371. s->mb_intra= 0;
  1372. for(i=0; i<4; i++){
  1373. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1374. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1375. }
  1376. init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
  1377. s->block= s->inter4v_block;
  1378. encode_mb(s);
  1379. d= get_bit_count(&s->pb);
  1380. if(d<dmin){
  1381. flush_put_bits(&s->pb);
  1382. dmin=d;
  1383. for(i=0; i<4; i++){
  1384. best_s.mv[0][i][0] = s->mv[0][i][0];
  1385. best_s.mv[0][i][1] = s->mv[0][i][1];
  1386. }
  1387. best_s.mb_intra= 0;
  1388. best_s.mv_type = MV_TYPE_8X8;
  1389. best_s.pb=s->pb;
  1390. best_s.block= s->block;
  1391. best=2;
  1392. for(i=0; i<6; i++)
  1393. best_s.block_last_index[i]= s->block_last_index[i];
  1394. }
  1395. }
  1396. if(mb_type&MB_TYPE_INTRA){
  1397. s->mv_type = MV_TYPE_16X16;
  1398. s->mb_intra= 1;
  1399. s->mv[0][0][0] = 0;
  1400. s->mv[0][0][1] = 0;
  1401. init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
  1402. s->block= s->intra_block;
  1403. encode_mb(s);
  1404. d= get_bit_count(&s->pb);
  1405. if(d<dmin){
  1406. flush_put_bits(&s->pb);
  1407. dmin=d;
  1408. best_s.mv[0][0][0]= 0;
  1409. best_s.mv[0][0][1]= 0;
  1410. best_s.mb_intra= 1;
  1411. best_s.mv_type = MV_TYPE_16X16;
  1412. best_s.pb=s->pb;
  1413. best_s.block= s->block;
  1414. for(i=0; i<6; i++)
  1415. best_s.block_last_index[i]= s->block_last_index[i];
  1416. best=0;
  1417. }
  1418. /* force cleaning of ac/dc if needed ... */
  1419. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1420. }
  1421. for(i=0; i<4; i++){
  1422. s->mv[0][i][0] = best_s.mv[0][i][0];
  1423. s->mv[0][i][1] = best_s.mv[0][i][1];
  1424. }
  1425. s->mb_intra= best_s.mb_intra;
  1426. s->mv_type= best_s.mv_type;
  1427. for(i=0; i<6; i++)
  1428. s->block_last_index[i]= best_s.block_last_index[i];
  1429. copy_bits(&pb, bit_buf[best], dmin);
  1430. s->block= best_s.block;
  1431. s->pb= pb;
  1432. } else {
  1433. // only one MB-Type possible
  1434. if(mb_type&MB_TYPE_INTRA){
  1435. s->mb_intra= 1;
  1436. s->mv[0][0][0] = 0;
  1437. s->mv[0][0][1] = 0;
  1438. }else{
  1439. s->mb_intra= 0;
  1440. s->mv[0][0][0] = s->mv_table[0][mb_y * s->mb_width + mb_x];
  1441. s->mv[0][0][1] = s->mv_table[1][mb_y * s->mb_width + mb_x];
  1442. }
  1443. encode_mb(s);
  1444. }
  1445. MPV_decode_mb(s, s->block);
  1446. }
  1447. /* Obtain average GOB size for RTP */
  1448. if (s->rtp_mode) {
  1449. if (!mb_y)
  1450. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  1451. else if (!(mb_y % s->gob_index)) {
  1452. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  1453. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  1454. }
  1455. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  1456. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  1457. s->first_gob_line = 0;
  1458. }
  1459. }
  1460. emms_c();
  1461. if (s->h263_msmpeg4 && s->pict_type == I_TYPE)
  1462. msmpeg4_encode_ext_header(s);
  1463. //if (s->gob_number)
  1464. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  1465. /* Send the last GOB if RTP */
  1466. if (s->rtp_mode) {
  1467. flush_put_bits(&s->pb);
  1468. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  1469. /* Call the RTP callback to send the last GOB */
  1470. if (s->rtp_callback)
  1471. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  1472. s->ptr_lastgob = pbBufPtr(&s->pb);
  1473. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  1474. }
  1475. }
  1476. static int dct_quantize_c(MpegEncContext *s,
  1477. DCTELEM *block, int n,
  1478. int qscale)
  1479. {
  1480. int i, j, level, last_non_zero, q;
  1481. const int *qmat;
  1482. int minLevel, maxLevel;
  1483. if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
  1484. /* mpeg4 */
  1485. minLevel= -2048;
  1486. maxLevel= 2047;
  1487. }else if(s->out_format==FMT_MPEG1){
  1488. /* mpeg1 */
  1489. minLevel= -255;
  1490. maxLevel= 255;
  1491. }else if(s->out_format==FMT_MJPEG){
  1492. /* (m)jpeg */
  1493. minLevel= -1023;
  1494. maxLevel= 1023;
  1495. }else{
  1496. /* h263 / msmpeg4 */
  1497. minLevel= -128;
  1498. maxLevel= 127;
  1499. }
  1500. av_fdct (block);
  1501. /* we need this permutation so that we correct the IDCT
  1502. permutation. will be moved into DCT code */
  1503. block_permute(block);
  1504. if (s->mb_intra) {
  1505. if (n < 4)
  1506. q = s->y_dc_scale;
  1507. else
  1508. q = s->c_dc_scale;
  1509. q = q << 3;
  1510. /* note: block[0] is assumed to be positive */
  1511. block[0] = (block[0] + (q >> 1)) / q;
  1512. i = 1;
  1513. last_non_zero = 0;
  1514. if (s->out_format == FMT_H263) {
  1515. qmat = s->q_non_intra_matrix;
  1516. } else {
  1517. qmat = s->q_intra_matrix;
  1518. }
  1519. } else {
  1520. i = 0;
  1521. last_non_zero = -1;
  1522. qmat = s->q_non_intra_matrix;
  1523. }
  1524. for(;i<64;i++) {
  1525. j = zigzag_direct[i];
  1526. level = block[j];
  1527. level = level * qmat[j];
  1528. #ifdef PARANOID
  1529. {
  1530. static int count = 0;
  1531. int level1, level2, qmat1;
  1532. double val;
  1533. if (qmat == s->q_non_intra_matrix) {
  1534. qmat1 = default_non_intra_matrix[j] * s->qscale;
  1535. } else {
  1536. qmat1 = default_intra_matrix[j] * s->qscale;
  1537. }
  1538. if (av_fdct != jpeg_fdct_ifast)
  1539. val = ((double)block[j] * 8.0) / (double)qmat1;
  1540. else
  1541. val = ((double)block[j] * 8.0 * 2048.0) /
  1542. ((double)qmat1 * aanscales[j]);
  1543. level1 = (int)val;
  1544. level2 = level / (1 << (QMAT_SHIFT - 3));
  1545. if (level1 != level2) {
  1546. fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
  1547. count, level2, level1, block[j], qmat1, qmat[j],
  1548. val);
  1549. count++;
  1550. }
  1551. }
  1552. #endif
  1553. /* XXX: slight error for the low range. Test should be equivalent to
  1554. (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
  1555. (QMAT_SHIFT - 3)))
  1556. */
  1557. if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
  1558. level) {
  1559. level = level / (1 << (QMAT_SHIFT - 3));
  1560. /* XXX: currently, this code is not optimal. the range should be:
  1561. mpeg1: -255..255
  1562. mpeg2: -2048..2047
  1563. h263: -128..127
  1564. mpeg4: -2048..2047
  1565. */
  1566. if (level > maxLevel)
  1567. level = maxLevel;
  1568. else if (level < minLevel)
  1569. level = minLevel;
  1570. block[j] = level;
  1571. last_non_zero = i;
  1572. } else {
  1573. block[j] = 0;
  1574. }
  1575. }
  1576. return last_non_zero;
  1577. }
  1578. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1579. DCTELEM *block, int n, int qscale)
  1580. {
  1581. int i, level, nCoeffs;
  1582. const UINT16 *quant_matrix;
  1583. if(s->alternate_scan) nCoeffs= 64;
  1584. else nCoeffs= s->block_last_index[n]+1;
  1585. if (s->mb_intra) {
  1586. if (n < 4)
  1587. block[0] = block[0] * s->y_dc_scale;
  1588. else
  1589. block[0] = block[0] * s->c_dc_scale;
  1590. /* XXX: only mpeg1 */
  1591. quant_matrix = s->intra_matrix;
  1592. for(i=1;i<nCoeffs;i++) {
  1593. int j= zigzag_direct[i];
  1594. level = block[j];
  1595. if (level) {
  1596. if (level < 0) {
  1597. level = -level;
  1598. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1599. level = (level - 1) | 1;
  1600. level = -level;
  1601. } else {
  1602. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1603. level = (level - 1) | 1;
  1604. }
  1605. #ifdef PARANOID
  1606. if (level < -2048 || level > 2047)
  1607. fprintf(stderr, "unquant error %d %d\n", i, level);
  1608. #endif
  1609. block[j] = level;
  1610. }
  1611. }
  1612. } else {
  1613. i = 0;
  1614. quant_matrix = s->non_intra_matrix;
  1615. for(;i<nCoeffs;i++) {
  1616. int j= zigzag_direct[i];
  1617. level = block[j];
  1618. if (level) {
  1619. if (level < 0) {
  1620. level = -level;
  1621. level = (((level << 1) + 1) * qscale *
  1622. ((int) (quant_matrix[j]))) >> 4;
  1623. level = (level - 1) | 1;
  1624. level = -level;
  1625. } else {
  1626. level = (((level << 1) + 1) * qscale *
  1627. ((int) (quant_matrix[j]))) >> 4;
  1628. level = (level - 1) | 1;
  1629. }
  1630. #ifdef PARANOID
  1631. if (level < -2048 || level > 2047)
  1632. fprintf(stderr, "unquant error %d %d\n", i, level);
  1633. #endif
  1634. block[j] = level;
  1635. }
  1636. }
  1637. }
  1638. }
  1639. static void dct_unquantize_h263_c(MpegEncContext *s,
  1640. DCTELEM *block, int n, int qscale)
  1641. {
  1642. int i, level, qmul, qadd;
  1643. int nCoeffs;
  1644. if (s->mb_intra) {
  1645. if (!s->h263_aic) {
  1646. if (n < 4)
  1647. block[0] = block[0] * s->y_dc_scale;
  1648. else
  1649. block[0] = block[0] * s->c_dc_scale;
  1650. }
  1651. i = 1;
  1652. nCoeffs= 64; //does not allways use zigzag table
  1653. } else {
  1654. i = 0;
  1655. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  1656. }
  1657. qmul = s->qscale << 1;
  1658. if (s->h263_aic && s->mb_intra)
  1659. qadd = 0;
  1660. else
  1661. qadd = (s->qscale - 1) | 1;
  1662. for(;i<nCoeffs;i++) {
  1663. level = block[i];
  1664. if (level) {
  1665. if (level < 0) {
  1666. level = level * qmul - qadd;
  1667. } else {
  1668. level = level * qmul + qadd;
  1669. }
  1670. #ifdef PARANOID
  1671. if (level < -2048 || level > 2047)
  1672. fprintf(stderr, "unquant error %d %d\n", i, level);
  1673. #endif
  1674. block[i] = level;
  1675. }
  1676. }
  1677. }
  1678. /* rate control */
  1679. /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
  1680. #define I_FRAME_SIZE_RATIO 3.0
  1681. #define QSCALE_K 20
  1682. static void rate_control_init(MpegEncContext *s)
  1683. {
  1684. #if 1
  1685. emms_c();
  1686. //initial values, they dont really matter as they will be totally different within a few frames
  1687. s->i_pred.coeff= s->p_pred.coeff= 7.0;
  1688. s->i_pred.count= s->p_pred.count= 1.0;
  1689. s->i_pred.decay= s->p_pred.decay= 0.4;
  1690. // use more bits at the beginning, otherwise high motion at the begin will look like shit
  1691. s->qsum=100;
  1692. s->qcount=100;
  1693. s->short_term_qsum=0.001;
  1694. s->short_term_qcount=0.001;
  1695. #else
  1696. s->wanted_bits = 0;
  1697. if (s->intra_only) {
  1698. s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
  1699. s->P_frame_bits = s->I_frame_bits;
  1700. } else {
  1701. s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
  1702. (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
  1703. s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
  1704. }
  1705. #if defined(DEBUG)
  1706. printf("I_frame_size=%d P_frame_size=%d\n",
  1707. s->I_frame_bits, s->P_frame_bits);
  1708. #endif
  1709. #endif
  1710. }
  1711. static double predict(Predictor *p, double q, double var)
  1712. {
  1713. return p->coeff*var / (q*p->count);
  1714. }
  1715. static void update_predictor(Predictor *p, double q, double var, double size)
  1716. {
  1717. double new_coeff= size*q / (var + 1);
  1718. if(var<1000) return;
  1719. /*{
  1720. int pred= predict(p, q, var);
  1721. int error= abs(pred-size);
  1722. static double sum=0;
  1723. static int count=0;
  1724. if(count>5) sum+=error;
  1725. count++;
  1726. if(256*256*256*64%count==0){
  1727. printf("%d %f %f\n", count, sum/count, p->coeff);
  1728. }
  1729. }*/
  1730. p->count*= p->decay;
  1731. p->coeff*= p->decay;
  1732. p->count++;
  1733. p->coeff+= new_coeff;
  1734. }
  1735. static int rate_estimate_qscale(MpegEncContext *s)
  1736. {
  1737. #if 1
  1738. int qmin= s->qmin;
  1739. int qmax= s->qmax;
  1740. int rate_q=5;
  1741. float q;
  1742. int qscale;
  1743. float br_compensation;
  1744. double diff;
  1745. double short_term_q;
  1746. double long_term_q;
  1747. int last_qscale= s->qscale;
  1748. double fps;
  1749. INT64 wanted_bits;
  1750. emms_c();
  1751. fps= (double)s->frame_rate / FRAME_RATE_BASE;
  1752. wanted_bits= s->bit_rate*(double)s->picture_number/fps;
  1753. if(s->picture_number>2){
  1754. /* update predictors */
  1755. if(s->last_pict_type == I_TYPE){
  1756. //FIXME
  1757. }else{ //P Frame
  1758. //printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);
  1759. update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits);
  1760. }
  1761. }
  1762. if(s->pict_type == I_TYPE){
  1763. //FIXME
  1764. rate_q= s->qsum/s->qcount;
  1765. }else{ //P Frame
  1766. int i;
  1767. int diff, best_diff=1000000000;
  1768. for(i=1; i<=31; i++){
  1769. diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps;
  1770. if(diff<0) diff= -diff;
  1771. if(diff<best_diff){
  1772. best_diff= diff;
  1773. rate_q= i;
  1774. }
  1775. }
  1776. }
  1777. s->short_term_qsum*=s->qblur;
  1778. s->short_term_qcount*=s->qblur;
  1779. s->short_term_qsum+= rate_q;
  1780. s->short_term_qcount++;
  1781. short_term_q= s->short_term_qsum/s->short_term_qcount;
  1782. long_term_q= s->qsum/s->qcount*s->total_bits/wanted_bits;
  1783. // q= (long_term_q - short_term_q)*s->qcompress + short_term_q;
  1784. q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
  1785. diff= s->total_bits - wanted_bits;
  1786. br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
  1787. if(br_compensation<=0.0) br_compensation=0.001;
  1788. q/=br_compensation;
  1789. qscale= (int)(q + 0.5);
  1790. if (qscale<qmin) qscale=qmin;
  1791. else if(qscale>qmax) qscale=qmax;
  1792. if (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff;
  1793. else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff;
  1794. s->qsum+= qscale;
  1795. s->qcount++;
  1796. s->last_pict_type= s->pict_type;
  1797. //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%d fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation,
  1798. // rate_q, (int)short_term_q, s->mc_mb_var, s->frame_bits);
  1799. //printf("%d %d\n", s->bit_rate, (int)fps);
  1800. return qscale;
  1801. #else
  1802. INT64 diff, total_bits = s->total_bits;
  1803. float q;
  1804. int qscale;
  1805. if (s->pict_type == I_TYPE) {
  1806. s->wanted_bits += s->I_frame_bits;
  1807. } else {
  1808. s->wanted_bits += s->P_frame_bits;
  1809. }
  1810. diff = s->wanted_bits - total_bits;
  1811. q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
  1812. /* adjust for I frame */
  1813. if (s->pict_type == I_TYPE && !s->intra_only) {
  1814. q /= I_FRAME_SIZE_RATIO;
  1815. }
  1816. /* using a too small Q scale leeds to problems in mpeg1 and h263
  1817. because AC coefficients are clamped to 255 or 127 */
  1818. qmin = 3;
  1819. if (q < qmin)
  1820. q = qmin;
  1821. else if (q > 31)
  1822. q = 31;
  1823. qscale = (int)(q + 0.5);
  1824. #if defined(DEBUG)
  1825. printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n",
  1826. s->picture_number,
  1827. (double)total_bits,
  1828. (double)s->wanted_bits,
  1829. (float)s->frame_rate / FRAME_RATE_BASE *
  1830. total_bits / s->picture_number,
  1831. (int)diff, q);
  1832. #endif
  1833. return qscale;
  1834. #endif
  1835. }
  1836. AVCodec mpeg1video_encoder = {
  1837. "mpeg1video",
  1838. CODEC_TYPE_VIDEO,
  1839. CODEC_ID_MPEG1VIDEO,
  1840. sizeof(MpegEncContext),
  1841. MPV_encode_init,
  1842. MPV_encode_picture,
  1843. MPV_encode_end,
  1844. };
  1845. AVCodec h263_encoder = {
  1846. "h263",
  1847. CODEC_TYPE_VIDEO,
  1848. CODEC_ID_H263,
  1849. sizeof(MpegEncContext),
  1850. MPV_encode_init,
  1851. MPV_encode_picture,
  1852. MPV_encode_end,
  1853. };
  1854. AVCodec h263p_encoder = {
  1855. "h263p",
  1856. CODEC_TYPE_VIDEO,
  1857. CODEC_ID_H263P,
  1858. sizeof(MpegEncContext),
  1859. MPV_encode_init,
  1860. MPV_encode_picture,
  1861. MPV_encode_end,
  1862. };
  1863. AVCodec rv10_encoder = {
  1864. "rv10",
  1865. CODEC_TYPE_VIDEO,
  1866. CODEC_ID_RV10,
  1867. sizeof(MpegEncContext),
  1868. MPV_encode_init,
  1869. MPV_encode_picture,
  1870. MPV_encode_end,
  1871. };
  1872. AVCodec mjpeg_encoder = {
  1873. "mjpeg",
  1874. CODEC_TYPE_VIDEO,
  1875. CODEC_ID_MJPEG,
  1876. sizeof(MpegEncContext),
  1877. MPV_encode_init,
  1878. MPV_encode_picture,
  1879. MPV_encode_end,
  1880. };
  1881. AVCodec mpeg4_encoder = {
  1882. "mpeg4",
  1883. CODEC_TYPE_VIDEO,
  1884. CODEC_ID_MPEG4,
  1885. sizeof(MpegEncContext),
  1886. MPV_encode_init,
  1887. MPV_encode_picture,
  1888. MPV_encode_end,
  1889. };
  1890. AVCodec msmpeg4_encoder = {
  1891. "msmpeg4",
  1892. CODEC_TYPE_VIDEO,
  1893. CODEC_ID_MSMPEG4,
  1894. sizeof(MpegEncContext),
  1895. MPV_encode_init,
  1896. MPV_encode_picture,
  1897. MPV_encode_end,
  1898. };