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.

2170 lines
70KB

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