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.

2271 lines
75KB

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