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.

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