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.

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