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.

2205 lines
73KB

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