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.

2784 lines
95KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. #include "avcodec.h"
  22. #include "dsputil.h"
  23. #include "mpegvideo.h"
  24. #ifdef USE_FASTMEMCPY
  25. #include "fastmemcpy.h"
  26. #endif
  27. static void encode_picture(MpegEncContext *s, int picture_number);
  28. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  29. DCTELEM *block, int n, int qscale);
  30. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  31. DCTELEM *block, int n, int qscale);
  32. static void dct_unquantize_h263_c(MpegEncContext *s,
  33. DCTELEM *block, int n, int qscale);
  34. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
  35. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  36. int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow)= dct_quantize_c;
  37. void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
  38. #define EDGE_WIDTH 16
  39. /* enable all paranoid tests for rounding, overflows, etc... */
  40. //#define PARANOID
  41. //#define DEBUG
  42. /* for jpeg fast DCT */
  43. #define CONST_BITS 14
  44. static const unsigned short aanscales[64] = {
  45. /* precomputed values scaled up by 14 bits */
  46. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  47. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  48. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  49. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  50. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  51. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  52. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  53. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  54. };
  55. static UINT8 h263_chroma_roundtab[16] = {
  56. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  57. };
  58. static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
  59. static UINT8 default_fcode_tab[MAX_MV*2+1];
  60. extern UINT8 zigzag_end[64];
  61. /* default motion estimation */
  62. int motion_estimation_method = ME_EPZS;
  63. static void convert_matrix(int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
  64. const UINT16 *quant_matrix, int bias)
  65. {
  66. int qscale;
  67. for(qscale=1; qscale<32; qscale++){
  68. int i;
  69. if (av_fdct == fdct_ifast) {
  70. for(i=0;i<64;i++) {
  71. const int j= block_permute_op(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[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
  77. (aanscales[i] * qscale * quant_matrix[j]));
  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 [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  87. qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
  88. if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
  89. qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
  90. }
  91. }
  92. }
  93. }
  94. // move into common.c perhaps
  95. #define CHECKED_ALLOCZ(p, size)\
  96. {\
  97. p= av_mallocz(size);\
  98. if(p==NULL){\
  99. perror("malloc");\
  100. goto fail;\
  101. }\
  102. }
  103. /* init common structure for both encoder and decoder */
  104. int MPV_common_init(MpegEncContext *s)
  105. {
  106. int c_size, i;
  107. UINT8 *pict;
  108. s->dct_unquantize_h263 = dct_unquantize_h263_c;
  109. s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
  110. s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
  111. #ifdef HAVE_MMX
  112. MPV_common_init_mmx(s);
  113. #endif
  114. #ifdef ARCH_ALPHA
  115. MPV_common_init_axp(s);
  116. #endif
  117. //setup default unquantizers (mpeg4 might change it later)
  118. if(s->out_format == FMT_H263)
  119. s->dct_unquantize = s->dct_unquantize_h263;
  120. else
  121. s->dct_unquantize = s->dct_unquantize_mpeg1;
  122. s->mb_width = (s->width + 15) / 16;
  123. s->mb_height = (s->height + 15) / 16;
  124. s->mb_num = s->mb_width * s->mb_height;
  125. if(!(s->flags&CODEC_FLAG_DR1)){
  126. s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
  127. s->uvlinesize = s->mb_width * 8 + EDGE_WIDTH;
  128. for(i=0;i<3;i++) {
  129. int w, h, shift, pict_start;
  130. w = s->linesize;
  131. h = s->mb_height * 16 + 2 * EDGE_WIDTH;
  132. shift = (i == 0) ? 0 : 1;
  133. c_size = (s->linesize>>shift) * (h >> shift);
  134. pict_start = (s->linesize>>shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
  135. CHECKED_ALLOCZ(pict, c_size)
  136. s->last_picture_base[i] = pict;
  137. s->last_picture[i] = pict + pict_start;
  138. if(i>0) memset(s->last_picture_base[i], 128, c_size);
  139. CHECKED_ALLOCZ(pict, c_size)
  140. s->next_picture_base[i] = pict;
  141. s->next_picture[i] = pict + pict_start;
  142. if(i>0) memset(s->next_picture_base[i], 128, c_size);
  143. if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
  144. /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but
  145. do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
  146. CHECKED_ALLOCZ(pict, c_size)
  147. s->aux_picture_base[i] = pict;
  148. s->aux_picture[i] = pict + pict_start;
  149. if(i>0) memset(s->aux_picture_base[i], 128, c_size);
  150. }
  151. }
  152. s->ip_buffer_count= 2;
  153. }
  154. CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+32)*2*17);
  155. if (s->encoding) {
  156. int j;
  157. int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
  158. CHECKED_ALLOCZ(s->mb_var , s->mb_num * sizeof(INT16))
  159. CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
  160. /* Allocate MV tables */
  161. CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16))
  162. CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
  163. CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16))
  164. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
  165. CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
  166. CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
  167. CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
  168. CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16))
  169. CHECKED_ALLOCZ(s->me_scratchpad, s->linesize*16*3*sizeof(uint8_t))
  170. CHECKED_ALLOCZ(s->me_map , ME_MAP_SIZE*sizeof(uint32_t))
  171. CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
  172. if(s->max_b_frames){
  173. for(j=0; j<REORDER_BUFFER_SIZE; j++){
  174. int i;
  175. for(i=0;i<3;i++) {
  176. int w, h, shift;
  177. w = s->linesize;
  178. h = s->mb_height * 16;
  179. shift = (i == 0) ? 0 : 1;
  180. c_size = (w >> shift) * (h >> shift);
  181. CHECKED_ALLOCZ(pict, c_size);
  182. s->picture_buffer[j][i] = pict;
  183. }
  184. }
  185. }
  186. if(s->codec_id==CODEC_ID_MPEG4){
  187. CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
  188. CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
  189. }
  190. }
  191. if (s->out_format == FMT_H263 || s->encoding) {
  192. int size;
  193. /* Allocate MB type table */
  194. CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8))
  195. /* MV prediction */
  196. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  197. CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
  198. }
  199. if (s->h263_pred || s->h263_plus) {
  200. int y_size, c_size, i, size;
  201. /* dc values */
  202. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  203. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  204. size = y_size + 2 * c_size;
  205. CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16));
  206. s->dc_val[1] = s->dc_val[0] + y_size;
  207. s->dc_val[2] = s->dc_val[1] + c_size;
  208. for(i=0;i<size;i++)
  209. s->dc_val[0][i] = 1024;
  210. /* ac values */
  211. CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16);
  212. s->ac_val[1] = s->ac_val[0] + y_size;
  213. s->ac_val[2] = s->ac_val[1] + c_size;
  214. /* cbp values */
  215. CHECKED_ALLOCZ(s->coded_block, y_size);
  216. /* which mb is a intra block */
  217. CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
  218. memset(s->mbintra_table, 1, s->mb_num);
  219. /* divx501 bitstream reorder buffer */
  220. CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
  221. /* cbp, ac_pred, pred_dir */
  222. CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8))
  223. CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
  224. CHECKED_ALLOCZ(s->qscale_table , s->mb_num * sizeof(UINT8))
  225. }
  226. /* default structure is frame */
  227. s->picture_structure = PICT_FRAME;
  228. /* init macroblock skip table */
  229. CHECKED_ALLOCZ(s->mbskip_table, s->mb_num);
  230. s->block= s->blocks[0];
  231. s->context_initialized = 1;
  232. return 0;
  233. fail:
  234. MPV_common_end(s);
  235. return -1;
  236. }
  237. //extern int sads;
  238. /* init common structure for both encoder and decoder */
  239. void MPV_common_end(MpegEncContext *s)
  240. {
  241. int i;
  242. av_freep(&s->mb_type);
  243. av_freep(&s->mb_var);
  244. av_freep(&s->mc_mb_var);
  245. av_freep(&s->p_mv_table);
  246. av_freep(&s->b_forw_mv_table);
  247. av_freep(&s->b_back_mv_table);
  248. av_freep(&s->b_bidir_forw_mv_table);
  249. av_freep(&s->b_bidir_back_mv_table);
  250. av_freep(&s->b_direct_forw_mv_table);
  251. av_freep(&s->b_direct_back_mv_table);
  252. av_freep(&s->b_direct_mv_table);
  253. av_freep(&s->motion_val);
  254. av_freep(&s->dc_val[0]);
  255. av_freep(&s->ac_val[0]);
  256. av_freep(&s->coded_block);
  257. av_freep(&s->mbintra_table);
  258. av_freep(&s->cbp_table);
  259. av_freep(&s->pred_dir_table);
  260. av_freep(&s->qscale_table);
  261. av_freep(&s->me_scratchpad);
  262. av_freep(&s->me_map);
  263. av_freep(&s->me_score_map);
  264. av_freep(&s->mbskip_table);
  265. av_freep(&s->bitstream_buffer);
  266. av_freep(&s->tex_pb_buffer);
  267. av_freep(&s->pb2_buffer);
  268. av_freep(&s->edge_emu_buffer);
  269. for(i=0;i<3;i++) {
  270. int j;
  271. if(!(s->flags&CODEC_FLAG_DR1)){
  272. av_freep(&s->last_picture_base[i]);
  273. av_freep(&s->next_picture_base[i]);
  274. av_freep(&s->aux_picture_base[i]);
  275. }
  276. s->last_picture_base[i]=
  277. s->next_picture_base[i]=
  278. s->aux_picture_base [i] = NULL;
  279. s->last_picture[i]=
  280. s->next_picture[i]=
  281. s->aux_picture [i] = NULL;
  282. for(j=0; j<REORDER_BUFFER_SIZE; j++){
  283. av_freep(&s->picture_buffer[j][i]);
  284. }
  285. }
  286. s->context_initialized = 0;
  287. }
  288. /* init video encoder */
  289. int MPV_encode_init(AVCodecContext *avctx)
  290. {
  291. MpegEncContext *s = avctx->priv_data;
  292. int i;
  293. avctx->pix_fmt = PIX_FMT_YUV420P;
  294. s->bit_rate = avctx->bit_rate;
  295. s->bit_rate_tolerance = avctx->bit_rate_tolerance;
  296. s->frame_rate = avctx->frame_rate;
  297. s->width = avctx->width;
  298. s->height = avctx->height;
  299. if(avctx->gop_size > 600){
  300. fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
  301. avctx->gop_size=600;
  302. }
  303. s->gop_size = avctx->gop_size;
  304. s->rtp_mode = avctx->rtp_mode;
  305. s->rtp_payload_size = avctx->rtp_payload_size;
  306. if (avctx->rtp_callback)
  307. s->rtp_callback = avctx->rtp_callback;
  308. s->qmin= avctx->qmin;
  309. s->qmax= avctx->qmax;
  310. s->max_qdiff= avctx->max_qdiff;
  311. s->qcompress= avctx->qcompress;
  312. s->qblur= avctx->qblur;
  313. s->b_quant_factor= avctx->b_quant_factor;
  314. s->b_quant_offset= avctx->b_quant_offset;
  315. s->avctx = avctx;
  316. s->aspect_ratio_info= avctx->aspect_ratio_info;
  317. s->flags= avctx->flags;
  318. s->max_b_frames= avctx->max_b_frames;
  319. s->rc_strategy= avctx->rc_strategy;
  320. s->b_frame_strategy= avctx->b_frame_strategy;
  321. s->codec_id= avctx->codec->id;
  322. s->luma_elim_threshold = avctx->luma_elim_threshold;
  323. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  324. s->strict_std_compliance= avctx->strict_std_compliance;
  325. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  326. if (s->gop_size <= 1) {
  327. s->intra_only = 1;
  328. s->gop_size = 12;
  329. } else {
  330. s->intra_only = 0;
  331. }
  332. /* ME algorithm */
  333. if (avctx->me_method == 0)
  334. /* For compatibility */
  335. s->me_method = motion_estimation_method;
  336. else
  337. s->me_method = avctx->me_method;
  338. /* Fixed QSCALE */
  339. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  340. switch(avctx->codec->id) {
  341. case CODEC_ID_MPEG1VIDEO:
  342. s->out_format = FMT_MPEG1;
  343. avctx->delay=0; //FIXME not sure, should check the spec
  344. break;
  345. case CODEC_ID_MJPEG:
  346. s->out_format = FMT_MJPEG;
  347. s->intra_only = 1; /* force intra only for jpeg */
  348. s->mjpeg_write_tables = 1; /* write all tables */
  349. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  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. avctx->delay=0;
  359. break;
  360. case CODEC_ID_H263:
  361. if (h263_get_picture_format(s->width, s->height) == 7) {
  362. printf("Input picture size isn't suitable for h263 codec! try h263+\n");
  363. return -1;
  364. }
  365. s->out_format = FMT_H263;
  366. avctx->delay=0;
  367. break;
  368. case CODEC_ID_H263P:
  369. s->out_format = FMT_H263;
  370. s->rtp_mode = 1;
  371. s->rtp_payload_size = 1200;
  372. s->h263_plus = 1;
  373. s->unrestricted_mv = 1;
  374. s->h263_aic = 1;
  375. /* These are just to be sure */
  376. s->umvplus = 0;
  377. s->umvplus_dec = 0;
  378. avctx->delay=0;
  379. break;
  380. case CODEC_ID_RV10:
  381. s->out_format = FMT_H263;
  382. s->h263_rv10 = 1;
  383. avctx->delay=0;
  384. break;
  385. case CODEC_ID_MPEG4:
  386. s->out_format = FMT_H263;
  387. s->h263_pred = 1;
  388. s->unrestricted_mv = 1;
  389. s->has_b_frames= s->max_b_frames ? 1 : 0;
  390. s->low_delay=0;
  391. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  392. break;
  393. case CODEC_ID_MSMPEG4V1:
  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= 1;
  399. avctx->delay=0;
  400. break;
  401. case CODEC_ID_MSMPEG4V2:
  402. s->out_format = FMT_H263;
  403. s->h263_msmpeg4 = 1;
  404. s->h263_pred = 1;
  405. s->unrestricted_mv = 1;
  406. s->msmpeg4_version= 2;
  407. avctx->delay=0;
  408. break;
  409. case CODEC_ID_MSMPEG4V3:
  410. s->out_format = FMT_H263;
  411. s->h263_msmpeg4 = 1;
  412. s->h263_pred = 1;
  413. s->unrestricted_mv = 1;
  414. s->msmpeg4_version= 3;
  415. avctx->delay=0;
  416. break;
  417. case CODEC_ID_WMV1:
  418. s->out_format = FMT_H263;
  419. s->h263_msmpeg4 = 1;
  420. s->h263_pred = 1;
  421. s->unrestricted_mv = 1;
  422. s->msmpeg4_version= 4;
  423. avctx->delay=0;
  424. break;
  425. case CODEC_ID_WMV2:
  426. s->out_format = FMT_H263;
  427. s->h263_msmpeg4 = 1;
  428. s->h263_pred = 1;
  429. s->unrestricted_mv = 1;
  430. s->msmpeg4_version= 5;
  431. avctx->delay=0;
  432. break;
  433. default:
  434. return -1;
  435. }
  436. { /* set up some save defaults, some codecs might override them later */
  437. static int done=0;
  438. if(!done){
  439. int i;
  440. done=1;
  441. memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
  442. memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
  443. for(i=-16; i<16; i++){
  444. default_fcode_tab[i + MAX_MV]= 1;
  445. }
  446. }
  447. }
  448. s->mv_penalty= default_mv_penalty;
  449. s->fcode_tab= default_fcode_tab;
  450. s->y_dc_scale_table=
  451. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  452. if (s->out_format == FMT_H263)
  453. h263_encode_init(s);
  454. else if (s->out_format == FMT_MPEG1)
  455. ff_mpeg1_encode_init(s);
  456. if(s->msmpeg4_version)
  457. ff_msmpeg4_encode_init(s);
  458. /* dont use mv_penalty table for crap MV as it would be confused */
  459. if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
  460. s->encoding = 1;
  461. /* init */
  462. if (MPV_common_init(s) < 0)
  463. return -1;
  464. /* init default q matrix */
  465. for(i=0;i<64;i++) {
  466. if(s->out_format == FMT_H263)
  467. s->intra_matrix[i] = ff_mpeg1_default_non_intra_matrix[i];
  468. else
  469. s->intra_matrix[i] = ff_mpeg1_default_intra_matrix[i];
  470. s->inter_matrix[i] = ff_mpeg1_default_non_intra_matrix[i];
  471. }
  472. /* precompute matrix */
  473. /* for mjpeg, we do include qscale in the matrix */
  474. if (s->out_format != FMT_MJPEG) {
  475. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
  476. s->intra_matrix, s->intra_quant_bias);
  477. convert_matrix(s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
  478. s->inter_matrix, s->inter_quant_bias);
  479. }
  480. if(ff_rate_control_init(s) < 0)
  481. return -1;
  482. s->picture_number = 0;
  483. s->picture_in_gop_number = 0;
  484. s->fake_picture_number = 0;
  485. /* motion detector init */
  486. s->f_code = 1;
  487. s->b_code = 1;
  488. return 0;
  489. }
  490. int MPV_encode_end(AVCodecContext *avctx)
  491. {
  492. MpegEncContext *s = avctx->priv_data;
  493. #ifdef STATS
  494. print_stats();
  495. #endif
  496. ff_rate_control_uninit(s);
  497. MPV_common_end(s);
  498. if (s->out_format == FMT_MJPEG)
  499. mjpeg_close(s);
  500. return 0;
  501. }
  502. /* draw the edges of width 'w' of an image of size width, height */
  503. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
  504. {
  505. UINT8 *ptr, *last_line;
  506. int i;
  507. last_line = buf + (height - 1) * wrap;
  508. for(i=0;i<w;i++) {
  509. /* top and bottom */
  510. memcpy(buf - (i + 1) * wrap, buf, width);
  511. memcpy(last_line + (i + 1) * wrap, last_line, width);
  512. }
  513. /* left and right */
  514. ptr = buf;
  515. for(i=0;i<height;i++) {
  516. memset(ptr - w, ptr[0], w);
  517. memset(ptr + width, ptr[width-1], w);
  518. ptr += wrap;
  519. }
  520. /* corners */
  521. for(i=0;i<w;i++) {
  522. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  523. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  524. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  525. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  526. }
  527. }
  528. /* generic function for encode/decode called before a frame is coded/decoded */
  529. void MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  530. {
  531. int i;
  532. UINT8 *tmp;
  533. s->mb_skiped = 0;
  534. s->decoding_error=0;
  535. avctx->mbskip_table= s->mbskip_table;
  536. if(avctx->flags&CODEC_FLAG_DR1){
  537. int i;
  538. avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type);
  539. s->linesize = avctx->dr_stride;
  540. s->uvlinesize= avctx->dr_uvstride;
  541. s->ip_buffer_count= avctx->dr_ip_buffer_count;
  542. }
  543. avctx->dr_ip_buffer_count= s->ip_buffer_count;
  544. if (s->pict_type == B_TYPE) {
  545. for(i=0;i<3;i++) {
  546. if(avctx->flags&CODEC_FLAG_DR1)
  547. s->aux_picture[i]= avctx->dr_buffer[i];
  548. s->current_picture[i] = s->aux_picture[i];
  549. }
  550. } else {
  551. for(i=0;i<3;i++) {
  552. /* swap next and last */
  553. if(avctx->flags&CODEC_FLAG_DR1)
  554. tmp= avctx->dr_buffer[i];
  555. else
  556. tmp = s->last_picture[i];
  557. s->last_picture[i] = s->next_picture[i];
  558. s->next_picture[i] = tmp;
  559. s->current_picture[i] = tmp;
  560. s->last_dr_opaque= s->next_dr_opaque;
  561. s->next_dr_opaque= avctx->dr_opaque_frame;
  562. if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)
  563. avctx->dr_opaque_frame= s->last_dr_opaque;
  564. else
  565. avctx->dr_opaque_frame= s->next_dr_opaque;
  566. }
  567. }
  568. }
  569. /* generic function for encode/decode called after a frame has been coded/decoded */
  570. void MPV_frame_end(MpegEncContext *s)
  571. {
  572. // if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads);
  573. /* draw edge for correct motion prediction if outside */
  574. if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  575. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version>=500){
  576. draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
  577. draw_edges(s->current_picture[1], s->uvlinesize, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  578. draw_edges(s->current_picture[2], s->uvlinesize, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  579. }else{
  580. /* mpeg4? / opendivx / xvid */
  581. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  582. draw_edges(s->current_picture[1], s->uvlinesize, s->width/2, s->height/2, EDGE_WIDTH/2);
  583. draw_edges(s->current_picture[2], s->uvlinesize, s->width/2, s->height/2, EDGE_WIDTH/2);
  584. }
  585. }
  586. emms_c();
  587. if(s->pict_type!=B_TYPE){
  588. s->last_non_b_pict_type= s->pict_type;
  589. s->last_non_b_qscale= s->qscale;
  590. s->last_non_b_mc_mb_var= s->mc_mb_var_sum;
  591. s->num_available_buffers++;
  592. if(s->num_available_buffers>2) s->num_available_buffers= 2;
  593. }
  594. }
  595. /* reorder input for encoding */
  596. void reorder_input(MpegEncContext *s, AVPicture *pict)
  597. {
  598. int i, j, index;
  599. if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
  600. // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
  601. for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
  602. s->coded_order[j]= s->coded_order[j+1];
  603. }
  604. s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
  605. s->coded_order[j].pict_type=0;
  606. switch(s->input_pict_type){
  607. default:
  608. case I_TYPE:
  609. case S_TYPE:
  610. case P_TYPE:
  611. index= s->max_b_frames - s->b_frames_since_non_b;
  612. s->b_frames_since_non_b=0;
  613. break;
  614. case B_TYPE:
  615. index= s->max_b_frames + 1;
  616. s->b_frames_since_non_b++;
  617. break;
  618. }
  619. //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
  620. if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
  621. && pict->linesize[0] == s->linesize
  622. && pict->linesize[1] == s->uvlinesize
  623. && pict->linesize[2] == s->uvlinesize){
  624. //printf("ptr\n");
  625. for(i=0; i<3; i++){
  626. s->coded_order[index].picture[i]= pict->data[i];
  627. }
  628. }else{
  629. //printf("copy\n");
  630. for(i=0; i<3; i++){
  631. uint8_t *src = pict->data[i];
  632. uint8_t *dest;
  633. int src_wrap = pict->linesize[i];
  634. int dest_wrap = s->linesize;
  635. int w = s->width;
  636. int h = s->height;
  637. if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
  638. else dest= s->picture_buffer[s->picture_buffer_index][i];
  639. if (i >= 1) {
  640. dest_wrap >>= 1;
  641. w >>= 1;
  642. h >>= 1;
  643. }
  644. s->coded_order[index].picture[i]= dest;
  645. for(j=0;j<h;j++) {
  646. memcpy(dest, src, w);
  647. dest += dest_wrap;
  648. src += src_wrap;
  649. }
  650. }
  651. if(index!=0){
  652. s->picture_buffer_index++;
  653. if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
  654. }
  655. }
  656. s->coded_order[index].pict_type = s->input_pict_type;
  657. s->coded_order[index].qscale = s->input_qscale;
  658. s->coded_order[index].force_type= s->force_input_type;
  659. s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
  660. s->coded_order[index].picture_number= s->input_picture_number;
  661. for(i=0; i<3; i++){
  662. s->new_picture[i]= s->coded_order[0].picture[i];
  663. }
  664. }
  665. int MPV_encode_picture(AVCodecContext *avctx,
  666. unsigned char *buf, int buf_size, void *data)
  667. {
  668. MpegEncContext *s = avctx->priv_data;
  669. AVPicture *pict = data;
  670. s->input_qscale = avctx->quality;
  671. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  672. if(avctx->flags&CODEC_FLAG_TYPE){
  673. s->input_pict_type=
  674. s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
  675. }else if(s->flags&CODEC_FLAG_PASS2){
  676. s->input_pict_type=
  677. s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
  678. }else{
  679. s->force_input_type=0;
  680. if (!s->intra_only) {
  681. /* first picture of GOP is intra */
  682. if (s->input_picture_in_gop_number % s->gop_size==0){
  683. s->input_pict_type = I_TYPE;
  684. }else if(s->max_b_frames==0){
  685. s->input_pict_type = P_TYPE;
  686. }else{
  687. if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
  688. s->input_pict_type = B_TYPE;
  689. else
  690. s->input_pict_type = P_TYPE;
  691. }
  692. } else {
  693. s->input_pict_type = I_TYPE;
  694. }
  695. }
  696. if(s->input_pict_type==I_TYPE)
  697. s->input_picture_in_gop_number=0;
  698. reorder_input(s, pict);
  699. /* output? */
  700. if(s->coded_order[0].picture[0]){
  701. s->pict_type= s->coded_order[0].pict_type;
  702. if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  703. s->qscale= s->coded_order[0].qscale;
  704. s->force_type= s->coded_order[0].force_type;
  705. s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
  706. s->picture_number= s->coded_order[0].picture_number;
  707. MPV_frame_start(s, avctx);
  708. encode_picture(s, s->picture_number);
  709. avctx->key_frame = (s->pict_type == I_TYPE);
  710. avctx->pict_type = s->pict_type;
  711. avctx->real_pict_num = s->picture_number;
  712. avctx->header_bits = s->header_bits;
  713. avctx->mv_bits = s->mv_bits;
  714. avctx->misc_bits = s->misc_bits;
  715. avctx->i_tex_bits = s->i_tex_bits;
  716. avctx->p_tex_bits = s->p_tex_bits;
  717. avctx->i_count = s->i_count;
  718. avctx->p_count = s->p_count;
  719. avctx->skip_count = s->skip_count;
  720. MPV_frame_end(s);
  721. if (s->out_format == FMT_MJPEG)
  722. mjpeg_picture_trailer(s);
  723. avctx->quality = s->qscale;
  724. if(s->flags&CODEC_FLAG_PASS1)
  725. ff_write_pass1_stats(s);
  726. }
  727. s->input_picture_number++;
  728. s->input_picture_in_gop_number++;
  729. flush_put_bits(&s->pb);
  730. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  731. if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
  732. else s->pb_frame_bits= s->frame_bits;
  733. s->total_bits += s->frame_bits;
  734. avctx->frame_bits = s->frame_bits;
  735. //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
  736. //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);
  737. if (avctx->get_psnr) {
  738. /* At this point pict->data should have the original frame */
  739. /* an s->current_picture should have the coded/decoded frame */
  740. get_psnr(pict->data, s->current_picture,
  741. pict->linesize, s->linesize, avctx);
  742. // printf("%f\n", avctx->psnr_y);
  743. }
  744. return pbBufPtr(&s->pb) - s->pb.buf;
  745. }
  746. static inline void gmc1_motion(MpegEncContext *s,
  747. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  748. int dest_offset,
  749. UINT8 **ref_picture, int src_offset,
  750. int h)
  751. {
  752. UINT8 *ptr;
  753. int offset, src_x, src_y, linesize, uvlinesize;
  754. int motion_x, motion_y;
  755. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  756. motion_x= s->sprite_offset[0][0];
  757. motion_y= s->sprite_offset[0][1];
  758. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  759. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  760. motion_x<<=(3-s->sprite_warping_accuracy);
  761. motion_y<<=(3-s->sprite_warping_accuracy);
  762. src_x = clip(src_x, -16, s->width);
  763. if (src_x == s->width)
  764. motion_x =0;
  765. src_y = clip(src_y, -16, s->height);
  766. if (src_y == s->height)
  767. motion_y =0;
  768. linesize = s->linesize;
  769. uvlinesize = s->uvlinesize;
  770. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  771. dest_y+=dest_offset;
  772. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  773. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  774. motion_x= s->sprite_offset[1][0];
  775. motion_y= s->sprite_offset[1][1];
  776. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  777. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  778. motion_x<<=(3-s->sprite_warping_accuracy);
  779. motion_y<<=(3-s->sprite_warping_accuracy);
  780. src_x = clip(src_x, -8, s->width>>1);
  781. if (src_x == s->width>>1)
  782. motion_x =0;
  783. src_y = clip(src_y, -8, s->height>>1);
  784. if (src_y == s->height>>1)
  785. motion_y =0;
  786. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  787. ptr = ref_picture[1] + offset;
  788. gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  789. ptr = ref_picture[2] + offset;
  790. gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  791. return;
  792. }
  793. static void emulated_edge_mc(UINT8 *buf, UINT8 *src, int linesize, int block_w, int block_h,
  794. int src_x, int src_y, int w, int h){
  795. int x, y;
  796. int start_y, start_x, end_y, end_x;
  797. if(src_y>= h){
  798. src+= (h-1-src_y)*linesize;
  799. src_y=h-1;
  800. }else if(src_y<=-block_h){
  801. src+= (1-block_h-src_y)*linesize;
  802. src_y=1-block_h;
  803. }
  804. if(src_x>= w){
  805. src+= (w-1-src_x);
  806. src_x=w-1;
  807. }else if(src_x<=-block_w){
  808. src+= (1-block_w-src_x);
  809. src_x=1-block_w;
  810. }
  811. start_y= MAX(0, -src_y);
  812. start_x= MAX(0, -src_x);
  813. end_y= MIN(block_h, h-src_y);
  814. end_x= MIN(block_w, w-src_x);
  815. // copy existing part
  816. for(y=start_y; y<end_y; y++){
  817. for(x=start_x; x<end_x; x++){
  818. buf[x + y*linesize]= src[x + y*linesize];
  819. }
  820. }
  821. //top
  822. for(y=0; y<start_y; y++){
  823. for(x=start_x; x<end_x; x++){
  824. buf[x + y*linesize]= buf[x + start_y*linesize];
  825. }
  826. }
  827. //bottom
  828. for(y=end_y; y<block_h; y++){
  829. for(x=start_x; x<end_x; x++){
  830. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  831. }
  832. }
  833. for(y=0; y<block_h; y++){
  834. //left
  835. for(x=0; x<start_x; x++){
  836. buf[x + y*linesize]= buf[start_x + y*linesize];
  837. }
  838. //right
  839. for(x=end_x; x<block_w; x++){
  840. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  841. }
  842. }
  843. }
  844. /* apply one mpeg motion vector to the three components */
  845. static inline void mpeg_motion(MpegEncContext *s,
  846. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  847. int dest_offset,
  848. UINT8 **ref_picture, int src_offset,
  849. int field_based, op_pixels_func *pix_op,
  850. int motion_x, int motion_y, int h)
  851. {
  852. UINT8 *ptr;
  853. int dxy, offset, mx, my, src_x, src_y, height, linesize, uvlinesize;
  854. int emu=0;
  855. if(s->quarter_sample)
  856. {
  857. motion_x>>=1;
  858. motion_y>>=1;
  859. }
  860. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  861. src_x = s->mb_x * 16 + (motion_x >> 1);
  862. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  863. /* WARNING: do no forget half pels */
  864. height = s->height >> field_based;
  865. src_x = clip(src_x, -16, s->width);
  866. if (src_x == s->width)
  867. dxy &= ~1;
  868. src_y = clip(src_y, -16, height);
  869. if (src_y == height)
  870. dxy &= ~2;
  871. linesize = s->linesize << field_based;
  872. uvlinesize = s->uvlinesize << field_based;
  873. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  874. dest_y += dest_offset;
  875. if(s->flags&CODEC_FLAG_EMU_EDGE){
  876. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->width
  877. || src_y + (motion_y&1) + h > height){
  878. emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, h+1, src_x, src_y, s->width, height);
  879. ptr= s->edge_emu_buffer;
  880. emu=1;
  881. }
  882. }
  883. pix_op[dxy](dest_y, ptr, linesize, h);
  884. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  885. if(s->flags&CODEC_FLAG_GRAY) return;
  886. if (s->out_format == FMT_H263) {
  887. dxy = 0;
  888. if ((motion_x & 3) != 0)
  889. dxy |= 1;
  890. if ((motion_y & 3) != 0)
  891. dxy |= 2;
  892. mx = motion_x >> 2;
  893. my = motion_y >> 2;
  894. } else {
  895. mx = motion_x / 2;
  896. my = motion_y / 2;
  897. dxy = ((my & 1) << 1) | (mx & 1);
  898. mx >>= 1;
  899. my >>= 1;
  900. }
  901. src_x = s->mb_x * 8 + mx;
  902. src_y = s->mb_y * (8 >> field_based) + my;
  903. src_x = clip(src_x, -8, s->width >> 1);
  904. if (src_x == (s->width >> 1))
  905. dxy &= ~1;
  906. src_y = clip(src_y, -8, height >> 1);
  907. if (src_y == (height >> 1))
  908. dxy &= ~2;
  909. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  910. ptr = ref_picture[1] + offset;
  911. if(emu){
  912. emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
  913. ptr= s->edge_emu_buffer;
  914. }
  915. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  916. ptr = ref_picture[2] + offset;
  917. if(emu){
  918. emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
  919. ptr= s->edge_emu_buffer;
  920. }
  921. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  922. }
  923. static inline void qpel_motion(MpegEncContext *s,
  924. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  925. int dest_offset,
  926. UINT8 **ref_picture, int src_offset,
  927. int field_based, op_pixels_func *pix_op,
  928. qpel_mc_func *qpix_op,
  929. int motion_x, int motion_y, int h)
  930. {
  931. UINT8 *ptr;
  932. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  933. int emu=0;
  934. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  935. src_x = s->mb_x * 16 + (motion_x >> 2);
  936. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  937. height = s->height >> field_based;
  938. src_x = clip(src_x, -16, s->width);
  939. if (src_x == s->width)
  940. dxy &= ~3;
  941. src_y = clip(src_y, -16, height);
  942. if (src_y == height)
  943. dxy &= ~12;
  944. linesize = s->linesize << field_based;
  945. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  946. dest_y += dest_offset;
  947. //printf("%d %d %d\n", src_x, src_y, dxy);
  948. if(s->flags&CODEC_FLAG_EMU_EDGE){
  949. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->width
  950. || src_y + (motion_y&3) + h > height){
  951. emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, h+1, src_x, src_y, s->width, height);
  952. ptr= s->edge_emu_buffer;
  953. emu=1;
  954. }
  955. }
  956. qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
  957. qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
  958. qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
  959. qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
  960. if(s->flags&CODEC_FLAG_GRAY) return;
  961. mx= (motion_x>>1) | (motion_x&1);
  962. my= (motion_y>>1) | (motion_y&1);
  963. dxy = 0;
  964. if ((mx & 3) != 0)
  965. dxy |= 1;
  966. if ((my & 3) != 0)
  967. dxy |= 2;
  968. mx = mx >> 2;
  969. my = my >> 2;
  970. src_x = s->mb_x * 8 + mx;
  971. src_y = s->mb_y * (8 >> field_based) + my;
  972. src_x = clip(src_x, -8, s->width >> 1);
  973. if (src_x == (s->width >> 1))
  974. dxy &= ~1;
  975. src_y = clip(src_y, -8, height >> 1);
  976. if (src_y == (height >> 1))
  977. dxy &= ~2;
  978. offset = (src_y * s->uvlinesize) + src_x + (src_offset >> 1);
  979. ptr = ref_picture[1] + offset;
  980. if(emu){
  981. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
  982. ptr= s->edge_emu_buffer;
  983. }
  984. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, s->uvlinesize, h >> 1);
  985. ptr = ref_picture[2] + offset;
  986. if(emu){
  987. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
  988. ptr= s->edge_emu_buffer;
  989. }
  990. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, s->uvlinesize, h >> 1);
  991. }
  992. static inline void MPV_motion(MpegEncContext *s,
  993. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  994. int dir, UINT8 **ref_picture,
  995. op_pixels_func *pix_op, qpel_mc_func *qpix_op)
  996. {
  997. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  998. int mb_x, mb_y, i;
  999. UINT8 *ptr, *dest;
  1000. int emu=0;
  1001. mb_x = s->mb_x;
  1002. mb_y = s->mb_y;
  1003. switch(s->mv_type) {
  1004. case MV_TYPE_16X16:
  1005. if(s->mcsel){
  1006. #if 0
  1007. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1008. ref_picture, 0,
  1009. 0, pix_op,
  1010. s->sprite_offset[0][0]>>3,
  1011. s->sprite_offset[0][1]>>3,
  1012. 16);
  1013. #else
  1014. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  1015. ref_picture, 0,
  1016. 16);
  1017. #endif
  1018. }else if(s->quarter_sample && dir==0){ //FIXME
  1019. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1020. ref_picture, 0,
  1021. 0, pix_op, qpix_op,
  1022. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1023. }else{
  1024. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1025. ref_picture, 0,
  1026. 0, pix_op,
  1027. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1028. }
  1029. break;
  1030. case MV_TYPE_8X8:
  1031. for(i=0;i<4;i++) {
  1032. motion_x = s->mv[dir][i][0];
  1033. motion_y = s->mv[dir][i][1];
  1034. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1035. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1036. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1037. /* WARNING: do no forget half pels */
  1038. src_x = clip(src_x, -16, s->width);
  1039. if (src_x == s->width)
  1040. dxy &= ~1;
  1041. src_y = clip(src_y, -16, s->height);
  1042. if (src_y == s->height)
  1043. dxy &= ~2;
  1044. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1045. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1046. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->width
  1047. || src_y + (motion_y&1) + 8 > s->height){
  1048. emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->width, s->height);
  1049. ptr= s->edge_emu_buffer;
  1050. }
  1051. }
  1052. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1053. pix_op[dxy](dest, ptr, s->linesize, 8);
  1054. }
  1055. if(s->flags&CODEC_FLAG_GRAY) break;
  1056. /* In case of 8X8, we construct a single chroma motion vector
  1057. with a special rounding */
  1058. mx = 0;
  1059. my = 0;
  1060. for(i=0;i<4;i++) {
  1061. mx += s->mv[dir][i][0];
  1062. my += s->mv[dir][i][1];
  1063. }
  1064. if (mx >= 0)
  1065. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1066. else {
  1067. mx = -mx;
  1068. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1069. }
  1070. if (my >= 0)
  1071. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1072. else {
  1073. my = -my;
  1074. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1075. }
  1076. dxy = ((my & 1) << 1) | (mx & 1);
  1077. mx >>= 1;
  1078. my >>= 1;
  1079. src_x = mb_x * 8 + mx;
  1080. src_y = mb_y * 8 + my;
  1081. src_x = clip(src_x, -8, s->width/2);
  1082. if (src_x == s->width/2)
  1083. dxy &= ~1;
  1084. src_y = clip(src_y, -8, s->height/2);
  1085. if (src_y == s->height/2)
  1086. dxy &= ~2;
  1087. offset = (src_y * (s->uvlinesize)) + src_x;
  1088. ptr = ref_picture[1] + offset;
  1089. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1090. if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->width >>1
  1091. || src_y + (dxy>>1) + 8 > s->height>>1){
  1092. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->width>>1, s->height>>1);
  1093. ptr= s->edge_emu_buffer;
  1094. emu=1;
  1095. }
  1096. }
  1097. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  1098. ptr = ref_picture[2] + offset;
  1099. if(emu){
  1100. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->width>>1, s->height>>1);
  1101. ptr= s->edge_emu_buffer;
  1102. }
  1103. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  1104. break;
  1105. case MV_TYPE_FIELD:
  1106. if (s->picture_structure == PICT_FRAME) {
  1107. /* top field */
  1108. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1109. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1110. 1, pix_op,
  1111. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1112. /* bottom field */
  1113. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1114. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1115. 1, pix_op,
  1116. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1117. } else {
  1118. }
  1119. break;
  1120. }
  1121. }
  1122. /* put block[] to dest[] */
  1123. static inline void put_dct(MpegEncContext *s,
  1124. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1125. {
  1126. if (!s->mpeg2)
  1127. s->dct_unquantize(s, block, i, s->qscale);
  1128. ff_idct_put (dest, line_size, block);
  1129. }
  1130. /* add block[] to dest[] */
  1131. static inline void add_dct(MpegEncContext *s,
  1132. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1133. {
  1134. if (s->block_last_index[i] >= 0) {
  1135. ff_idct_add (dest, line_size, block);
  1136. }
  1137. }
  1138. static inline void add_dequant_dct(MpegEncContext *s,
  1139. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1140. {
  1141. if (s->block_last_index[i] >= 0) {
  1142. s->dct_unquantize(s, block, i, s->qscale);
  1143. ff_idct_add (dest, line_size, block);
  1144. }
  1145. }
  1146. /**
  1147. * cleans dc, ac, coded_block for the current non intra MB
  1148. */
  1149. void ff_clean_intra_table_entries(MpegEncContext *s)
  1150. {
  1151. int wrap = s->block_wrap[0];
  1152. int xy = s->block_index[0];
  1153. s->dc_val[0][xy ] =
  1154. s->dc_val[0][xy + 1 ] =
  1155. s->dc_val[0][xy + wrap] =
  1156. s->dc_val[0][xy + 1 + wrap] = 1024;
  1157. /* ac pred */
  1158. memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16));
  1159. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
  1160. if (s->msmpeg4_version>=3) {
  1161. s->coded_block[xy ] =
  1162. s->coded_block[xy + 1 ] =
  1163. s->coded_block[xy + wrap] =
  1164. s->coded_block[xy + 1 + wrap] = 0;
  1165. }
  1166. /* chroma */
  1167. wrap = s->block_wrap[4];
  1168. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  1169. s->dc_val[1][xy] =
  1170. s->dc_val[2][xy] = 1024;
  1171. /* ac pred */
  1172. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  1173. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  1174. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1175. }
  1176. /* generic function called after a macroblock has been parsed by the
  1177. decoder or after it has been encoded by the encoder.
  1178. Important variables used:
  1179. s->mb_intra : true if intra macroblock
  1180. s->mv_dir : motion vector direction
  1181. s->mv_type : motion vector type
  1182. s->mv : motion vector
  1183. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1184. */
  1185. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1186. {
  1187. int mb_x, mb_y;
  1188. const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
  1189. mb_x = s->mb_x;
  1190. mb_y = s->mb_y;
  1191. #ifdef FF_POSTPROCESS
  1192. /* Obsolete. Exists for compatibility with mplayer only. */
  1193. quant_store[mb_y][mb_x]=s->qscale;
  1194. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  1195. #else
  1196. if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
  1197. #endif
  1198. /* update DC predictors for P macroblocks */
  1199. if (!s->mb_intra) {
  1200. if (s->h263_pred || s->h263_aic) {
  1201. if(s->mbintra_table[mb_xy])
  1202. ff_clean_intra_table_entries(s);
  1203. } else {
  1204. s->last_dc[0] =
  1205. s->last_dc[1] =
  1206. s->last_dc[2] = 128 << s->intra_dc_precision;
  1207. }
  1208. }
  1209. else if (s->h263_pred || s->h263_aic)
  1210. s->mbintra_table[mb_xy]=1;
  1211. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1212. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1213. int motion_x, motion_y;
  1214. const int wrap = s->block_wrap[0];
  1215. const int xy = s->block_index[0];
  1216. if (s->mb_intra) {
  1217. motion_x = 0;
  1218. motion_y = 0;
  1219. goto motion_init;
  1220. } else if (s->mv_type == MV_TYPE_16X16) {
  1221. motion_x = s->mv[0][0][0];
  1222. motion_y = s->mv[0][0][1];
  1223. motion_init:
  1224. /* no update if 8X8 because it has been done during parsing */
  1225. s->motion_val[xy][0] = motion_x;
  1226. s->motion_val[xy][1] = motion_y;
  1227. s->motion_val[xy + 1][0] = motion_x;
  1228. s->motion_val[xy + 1][1] = motion_y;
  1229. s->motion_val[xy + wrap][0] = motion_x;
  1230. s->motion_val[xy + wrap][1] = motion_y;
  1231. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1232. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1233. }
  1234. }
  1235. if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
  1236. UINT8 *dest_y, *dest_cb, *dest_cr;
  1237. int dct_linesize, dct_offset;
  1238. op_pixels_func *op_pix;
  1239. qpel_mc_func *op_qpix;
  1240. /* avoid copy if macroblock skipped in last frame too
  1241. dont touch it for B-frames as they need the skip info from the next p-frame */
  1242. if (s->pict_type != B_TYPE) {
  1243. UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
  1244. if (s->mb_skiped) {
  1245. s->mb_skiped = 0;
  1246. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  1247. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1248. /* if previous was skipped too, then nothing to do !
  1249. skip only during decoding as we might trash the buffers during encoding a bit */
  1250. if (*mbskip_ptr >= s->ip_buffer_count && !s->encoding)
  1251. goto the_end;
  1252. } else {
  1253. *mbskip_ptr = 0; /* not skipped */
  1254. }
  1255. }
  1256. dest_y = s->current_picture [0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1257. dest_cb = s->current_picture[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1258. dest_cr = s->current_picture[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1259. if (s->interlaced_dct) {
  1260. dct_linesize = s->linesize * 2;
  1261. dct_offset = s->linesize;
  1262. } else {
  1263. dct_linesize = s->linesize;
  1264. dct_offset = s->linesize * 8;
  1265. }
  1266. if (!s->mb_intra) {
  1267. /* motion handling */
  1268. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1269. if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
  1270. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1271. op_pix = put_pixels_tab;
  1272. op_qpix= qpel_mc_rnd_tab;
  1273. }else{
  1274. op_pix = put_no_rnd_pixels_tab;
  1275. op_qpix= qpel_mc_no_rnd_tab;
  1276. }
  1277. if (s->mv_dir & MV_DIR_FORWARD) {
  1278. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1279. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1280. op_pix = avg_pixels_tab;
  1281. else
  1282. op_pix = avg_no_rnd_pixels_tab;
  1283. }
  1284. if (s->mv_dir & MV_DIR_BACKWARD) {
  1285. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1286. }
  1287. }
  1288. /* skip dequant / idct if we are really late ;) */
  1289. if(s->hurry_up>1) goto the_end;
  1290. /* add dct residue */
  1291. if(s->encoding || !(s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG4)){
  1292. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  1293. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1294. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1295. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1296. if(!(s->flags&CODEC_FLAG_GRAY)){
  1297. add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1298. add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1299. }
  1300. } else {
  1301. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1302. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1303. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1304. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1305. if(!(s->flags&CODEC_FLAG_GRAY)){
  1306. add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1307. add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1308. }
  1309. }
  1310. } else {
  1311. /* dct only in intra block */
  1312. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1313. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1314. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1315. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1316. if(!(s->flags&CODEC_FLAG_GRAY)){
  1317. put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1318. put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1319. }
  1320. }
  1321. }
  1322. the_end:
  1323. emms_c(); //FIXME remove
  1324. }
  1325. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc)
  1326. {
  1327. static const char tab[64]=
  1328. {3,2,2,1,1,1,1,1,
  1329. 1,1,1,1,1,1,1,1,
  1330. 1,1,1,1,1,1,1,1,
  1331. 0,0,0,0,0,0,0,0,
  1332. 0,0,0,0,0,0,0,0,
  1333. 0,0,0,0,0,0,0,0,
  1334. 0,0,0,0,0,0,0,0,
  1335. 0,0,0,0,0,0,0,0};
  1336. int score=0;
  1337. int run=0;
  1338. int i;
  1339. DCTELEM *block= s->block[n];
  1340. const int last_index= s->block_last_index[n];
  1341. if(skip_dc) skip_dc=1;
  1342. /* are all which we could set to zero are allready zero? */
  1343. if(last_index<=skip_dc - 1) return;
  1344. for(i=0; i<=last_index; i++){
  1345. const int j = zigzag_direct[i];
  1346. const int level = ABS(block[j]);
  1347. if(level==1){
  1348. if(skip_dc && i==0) continue;
  1349. score+= tab[run];
  1350. run=0;
  1351. }else if(level>1){
  1352. return;
  1353. }else{
  1354. run++;
  1355. }
  1356. }
  1357. if(score >= threshold) return;
  1358. for(i=skip_dc; i<=last_index; i++){
  1359. const int j = zigzag_direct[i];
  1360. block[j]=0;
  1361. }
  1362. if(block[0]) s->block_last_index[n]= 0;
  1363. else s->block_last_index[n]= -1;
  1364. }
  1365. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1366. {
  1367. int i;
  1368. const int maxlevel= s->max_qcoeff;
  1369. const int minlevel= s->min_qcoeff;
  1370. for(i=0;i<=last_index; i++){
  1371. const int j = zigzag_direct[i];
  1372. int level = block[j];
  1373. if (level>maxlevel) level=maxlevel;
  1374. else if(level<minlevel) level=minlevel;
  1375. block[j]= level;
  1376. }
  1377. }
  1378. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1379. {
  1380. const int mb_x= s->mb_x;
  1381. const int mb_y= s->mb_y;
  1382. int i;
  1383. int skip_dct[6];
  1384. #if 0
  1385. if (s->interlaced_dct) {
  1386. dct_linesize = s->linesize * 2;
  1387. dct_offset = s->linesize;
  1388. } else {
  1389. dct_linesize = s->linesize;
  1390. dct_offset = s->linesize * 8;
  1391. }
  1392. #endif
  1393. for(i=0; i<6; i++) skip_dct[i]=0;
  1394. if (s->mb_intra) {
  1395. UINT8 *ptr;
  1396. int wrap;
  1397. wrap = s->linesize;
  1398. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1399. get_pixels(s->block[0], ptr , wrap);
  1400. get_pixels(s->block[1], ptr + 8, wrap);
  1401. get_pixels(s->block[2], ptr + 8 * wrap , wrap);
  1402. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1403. if(s->flags&CODEC_FLAG_GRAY){
  1404. skip_dct[4]= 1;
  1405. skip_dct[5]= 1;
  1406. }else{
  1407. wrap >>=1;
  1408. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1409. get_pixels(s->block[4], ptr, wrap);
  1410. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1411. get_pixels(s->block[5], ptr, wrap);
  1412. }
  1413. }else{
  1414. op_pixels_func *op_pix;
  1415. qpel_mc_func *op_qpix;
  1416. UINT8 *dest_y, *dest_cb, *dest_cr;
  1417. UINT8 *ptr_y, *ptr_cb, *ptr_cr;
  1418. int wrap_y, wrap_c;
  1419. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  1420. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1421. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1422. wrap_y = s->linesize;
  1423. wrap_c = wrap_y>>1;
  1424. ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1425. ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1426. ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1427. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1428. op_pix = put_pixels_tab;
  1429. op_qpix= qpel_mc_rnd_tab;
  1430. }else{
  1431. op_pix = put_no_rnd_pixels_tab;
  1432. op_qpix= qpel_mc_no_rnd_tab;
  1433. }
  1434. if (s->mv_dir & MV_DIR_FORWARD) {
  1435. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1436. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1437. op_pix = avg_pixels_tab;
  1438. else
  1439. op_pix = avg_no_rnd_pixels_tab;
  1440. }
  1441. if (s->mv_dir & MV_DIR_BACKWARD) {
  1442. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1443. }
  1444. diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  1445. diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1446. diff_pixels(s->block[2], ptr_y + 8 * wrap_y , dest_y + 8 * wrap_y , wrap_y);
  1447. diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y);
  1448. if(s->flags&CODEC_FLAG_GRAY){
  1449. skip_dct[4]= 1;
  1450. skip_dct[5]= 1;
  1451. }else{
  1452. diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1453. diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1454. }
  1455. /* pre quantization */
  1456. if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  1457. if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  1458. if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  1459. if(pix_abs8x8(ptr_y + 8*wrap_y , dest_y + 8*wrap_y , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  1460. if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  1461. if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
  1462. if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
  1463. #if 0
  1464. {
  1465. static int stat[7];
  1466. int num=0;
  1467. for(i=0; i<6; i++)
  1468. if(skip_dct[i]) num++;
  1469. stat[num]++;
  1470. if(s->mb_x==0 && s->mb_y==0){
  1471. for(i=0; i<7; i++){
  1472. printf("%6d %1d\n", stat[i], i);
  1473. }
  1474. }
  1475. }
  1476. #endif
  1477. }
  1478. }
  1479. #if 0
  1480. {
  1481. float adap_parm;
  1482. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1483. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1484. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1485. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1486. s->qscale, adap_parm, s->qscale*adap_parm,
  1487. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1488. }
  1489. #endif
  1490. /* DCT & quantize */
  1491. if(s->out_format==FMT_MJPEG){
  1492. for(i=0;i<6;i++) {
  1493. int overflow;
  1494. s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
  1495. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1496. }
  1497. }else{
  1498. for(i=0;i<6;i++) {
  1499. if(!skip_dct[i]){
  1500. int overflow;
  1501. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1502. // FIXME we could decide to change to quantizer instead of clipping
  1503. // JS: I don't think that would be a good idea it could lower quality instead
  1504. // of improve it. Just INTRADC clipping deserves changes in quantizer
  1505. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1506. }else
  1507. s->block_last_index[i]= -1;
  1508. }
  1509. if(s->luma_elim_threshold && !s->mb_intra)
  1510. for(i=0; i<4; i++)
  1511. dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0);
  1512. if(s->chroma_elim_threshold && !s->mb_intra)
  1513. for(i=4; i<6; i++)
  1514. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1);
  1515. }
  1516. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  1517. s->block_last_index[4]=
  1518. s->block_last_index[5]= 0;
  1519. s->block[4][0]=
  1520. s->block[5][0]= 128;
  1521. }
  1522. /* huffman encode */
  1523. switch(s->out_format) {
  1524. case FMT_MPEG1:
  1525. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1526. break;
  1527. case FMT_H263:
  1528. if (s->h263_msmpeg4)
  1529. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1530. else if(s->h263_pred)
  1531. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1532. else
  1533. h263_encode_mb(s, s->block, motion_x, motion_y);
  1534. break;
  1535. case FMT_MJPEG:
  1536. mjpeg_encode_mb(s, s->block);
  1537. break;
  1538. }
  1539. }
  1540. void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
  1541. {
  1542. int bytes= length>>4;
  1543. int bits= length&15;
  1544. int i;
  1545. if(length==0) return;
  1546. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  1547. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  1548. }
  1549. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1550. int i;
  1551. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1552. /* mpeg1 */
  1553. d->mb_incr= s->mb_incr;
  1554. for(i=0; i<3; i++)
  1555. d->last_dc[i]= s->last_dc[i];
  1556. /* statistics */
  1557. d->mv_bits= s->mv_bits;
  1558. d->i_tex_bits= s->i_tex_bits;
  1559. d->p_tex_bits= s->p_tex_bits;
  1560. d->i_count= s->i_count;
  1561. d->p_count= s->p_count;
  1562. d->skip_count= s->skip_count;
  1563. d->misc_bits= s->misc_bits;
  1564. d->last_bits= 0;
  1565. d->mb_skiped= s->mb_skiped;
  1566. }
  1567. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1568. int i;
  1569. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  1570. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1571. /* mpeg1 */
  1572. d->mb_incr= s->mb_incr;
  1573. for(i=0; i<3; i++)
  1574. d->last_dc[i]= s->last_dc[i];
  1575. /* statistics */
  1576. d->mv_bits= s->mv_bits;
  1577. d->i_tex_bits= s->i_tex_bits;
  1578. d->p_tex_bits= s->p_tex_bits;
  1579. d->i_count= s->i_count;
  1580. d->p_count= s->p_count;
  1581. d->skip_count= s->skip_count;
  1582. d->misc_bits= s->misc_bits;
  1583. d->mb_intra= s->mb_intra;
  1584. d->mb_skiped= s->mb_skiped;
  1585. d->mv_type= s->mv_type;
  1586. d->mv_dir= s->mv_dir;
  1587. d->pb= s->pb;
  1588. if(s->data_partitioning){
  1589. d->pb2= s->pb2;
  1590. d->tex_pb= s->tex_pb;
  1591. }
  1592. d->block= s->block;
  1593. for(i=0; i<6; i++)
  1594. d->block_last_index[i]= s->block_last_index[i];
  1595. }
  1596. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  1597. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  1598. int *dmin, int *next_block, int motion_x, int motion_y)
  1599. {
  1600. int bits_count;
  1601. copy_context_before_encode(s, backup, type);
  1602. s->block= s->blocks[*next_block];
  1603. s->pb= pb[*next_block];
  1604. if(s->data_partitioning){
  1605. s->pb2 = pb2 [*next_block];
  1606. s->tex_pb= tex_pb[*next_block];
  1607. }
  1608. encode_mb(s, motion_x, motion_y);
  1609. bits_count= get_bit_count(&s->pb);
  1610. if(s->data_partitioning){
  1611. bits_count+= get_bit_count(&s->pb2);
  1612. bits_count+= get_bit_count(&s->tex_pb);
  1613. }
  1614. if(bits_count<*dmin){
  1615. *dmin= bits_count;
  1616. *next_block^=1;
  1617. copy_context_after_encode(best, s, type);
  1618. }
  1619. }
  1620. static void encode_picture(MpegEncContext *s, int picture_number)
  1621. {
  1622. int mb_x, mb_y, last_gob, pdif = 0;
  1623. int i;
  1624. int bits;
  1625. MpegEncContext best_s, backup_s;
  1626. UINT8 bit_buf[2][3000];
  1627. UINT8 bit_buf2[2][3000];
  1628. UINT8 bit_buf_tex[2][3000];
  1629. PutBitContext pb[2], pb2[2], tex_pb[2];
  1630. for(i=0; i<2; i++){
  1631. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  1632. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  1633. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  1634. }
  1635. s->picture_number = picture_number;
  1636. s->block_wrap[0]=
  1637. s->block_wrap[1]=
  1638. s->block_wrap[2]=
  1639. s->block_wrap[3]= s->mb_width*2 + 2;
  1640. s->block_wrap[4]=
  1641. s->block_wrap[5]= s->mb_width + 2;
  1642. /* Reset the average MB variance */
  1643. s->mb_var_sum = 0;
  1644. s->mc_mb_var_sum = 0;
  1645. /* we need to initialize some time vars before we can encode b-frames */
  1646. if (s->h263_pred && !s->h263_msmpeg4)
  1647. ff_set_mpeg4_time(s, s->picture_number);
  1648. /* Estimate motion for every MB */
  1649. if(s->pict_type != I_TYPE){
  1650. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1651. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1652. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1653. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1654. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1655. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1656. s->mb_x = mb_x;
  1657. s->mb_y = mb_y;
  1658. s->block_index[0]+=2;
  1659. s->block_index[1]+=2;
  1660. s->block_index[2]+=2;
  1661. s->block_index[3]+=2;
  1662. /* compute motion vector & mb_type and store in context */
  1663. if(s->pict_type==B_TYPE)
  1664. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  1665. else
  1666. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  1667. // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
  1668. }
  1669. }
  1670. emms_c();
  1671. }else /* if(s->pict_type == I_TYPE) */{
  1672. /* I-Frame */
  1673. //FIXME do we need to zero them?
  1674. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  1675. memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
  1676. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1677. }
  1678. if(s->mb_var_sum < s->mc_mb_var_sum && s->pict_type == P_TYPE){ //FIXME subtract MV bits
  1679. s->pict_type= I_TYPE;
  1680. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1681. if(s->max_b_frames==0){
  1682. s->input_pict_type= I_TYPE;
  1683. s->input_picture_in_gop_number=0;
  1684. }
  1685. //printf("Scene change detected, encoding as I Frame\n");
  1686. }
  1687. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
  1688. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  1689. ff_fix_long_p_mvs(s);
  1690. if(s->pict_type==B_TYPE){
  1691. s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  1692. s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  1693. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  1694. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  1695. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  1696. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  1697. }
  1698. //printf("f_code %d ///\n", s->f_code);
  1699. // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
  1700. if(s->flags&CODEC_FLAG_PASS2)
  1701. s->qscale = ff_rate_estimate_qscale_pass2(s);
  1702. else if (!s->fixed_qscale)
  1703. s->qscale = ff_rate_estimate_qscale(s);
  1704. if (s->out_format == FMT_MJPEG) {
  1705. /* for mjpeg, we do include qscale in the matrix */
  1706. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  1707. for(i=1;i<64;i++)
  1708. s->intra_matrix[i] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  1709. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16,
  1710. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
  1711. }
  1712. s->last_bits= get_bit_count(&s->pb);
  1713. switch(s->out_format) {
  1714. case FMT_MJPEG:
  1715. mjpeg_picture_header(s);
  1716. break;
  1717. case FMT_H263:
  1718. if (s->h263_msmpeg4)
  1719. msmpeg4_encode_picture_header(s, picture_number);
  1720. else if (s->h263_pred)
  1721. mpeg4_encode_picture_header(s, picture_number);
  1722. else if (s->h263_rv10)
  1723. rv10_encode_picture_header(s, picture_number);
  1724. else
  1725. h263_encode_picture_header(s, picture_number);
  1726. break;
  1727. case FMT_MPEG1:
  1728. mpeg1_encode_picture_header(s, picture_number);
  1729. break;
  1730. }
  1731. bits= get_bit_count(&s->pb);
  1732. s->header_bits= bits - s->last_bits;
  1733. s->last_bits= bits;
  1734. s->mv_bits=0;
  1735. s->misc_bits=0;
  1736. s->i_tex_bits=0;
  1737. s->p_tex_bits=0;
  1738. s->i_count=0;
  1739. s->p_count=0;
  1740. s->skip_count=0;
  1741. /* init last dc values */
  1742. /* note: quant matrix value (8) is implied here */
  1743. s->last_dc[0] = 128;
  1744. s->last_dc[1] = 128;
  1745. s->last_dc[2] = 128;
  1746. s->mb_incr = 1;
  1747. s->last_mv[0][0][0] = 0;
  1748. s->last_mv[0][0][1] = 0;
  1749. /* Get the GOB height based on picture height */
  1750. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  1751. if (s->height <= 400)
  1752. s->gob_index = 1;
  1753. else if (s->height <= 800)
  1754. s->gob_index = 2;
  1755. else
  1756. s->gob_index = 4;
  1757. }else if(s->codec_id==CODEC_ID_MPEG4){
  1758. s->gob_index = 1;
  1759. }
  1760. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  1761. ff_mpeg4_init_partitions(s);
  1762. s->resync_mb_x=0;
  1763. s->resync_mb_y=0;
  1764. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1765. /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
  1766. /* TODO: Put all this stuff in a separate generic function */
  1767. if (s->rtp_mode) {
  1768. if (!mb_y) {
  1769. s->ptr_lastgob = s->pb.buf;
  1770. s->ptr_last_mb_line = s->pb.buf;
  1771. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1772. // MN: we could move the space check from h263 -> here, as its not h263 specific
  1773. last_gob = h263_encode_gob_header(s, mb_y);
  1774. if (last_gob) {
  1775. s->first_slice_line = 1;
  1776. }else{
  1777. /*MN: we reset it here instead at the end of each line cuz mpeg4 can have
  1778. slice lines starting & ending in the middle*/
  1779. s->first_slice_line = 0;
  1780. }
  1781. }
  1782. }
  1783. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  1784. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  1785. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1786. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1787. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1788. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1789. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1790. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1791. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1792. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  1793. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  1794. // int d;
  1795. int dmin=10000000;
  1796. s->mb_x = mb_x;
  1797. s->mb_y = mb_y;
  1798. s->block_index[0]+=2;
  1799. s->block_index[1]+=2;
  1800. s->block_index[2]+=2;
  1801. s->block_index[3]+=2;
  1802. s->block_index[4]++;
  1803. s->block_index[5]++;
  1804. /* write gob / video packet header for formats which support it at any MB (MPEG4) */
  1805. if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
  1806. int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
  1807. //the *2 is there so we stay below the requested size
  1808. if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){
  1809. if(s->codec_id==CODEC_ID_MPEG4){
  1810. if(s->data_partitioning && s->pict_type!=B_TYPE){
  1811. ff_mpeg4_merge_partitions(s);
  1812. ff_mpeg4_init_partitions(s);
  1813. }
  1814. ff_mpeg4_encode_video_packet_header(s);
  1815. if(s->flags&CODEC_FLAG_PASS1){
  1816. int bits= get_bit_count(&s->pb);
  1817. s->misc_bits+= bits - s->last_bits;
  1818. s->last_bits= bits;
  1819. }
  1820. ff_mpeg4_clean_buffers(s);
  1821. }
  1822. s->ptr_lastgob = pbBufPtr(&s->pb);
  1823. s->first_slice_line=1;
  1824. s->resync_mb_x=mb_x;
  1825. s->resync_mb_y=mb_y;
  1826. }
  1827. if( (s->resync_mb_x == s->mb_x)
  1828. && s->resync_mb_y+1 == s->mb_y){
  1829. s->first_slice_line=0;
  1830. }
  1831. }
  1832. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  1833. int next_block=0;
  1834. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  1835. copy_context_before_encode(&backup_s, s, -1);
  1836. backup_s.pb= s->pb;
  1837. best_s.data_partitioning= s->data_partitioning;
  1838. if(s->data_partitioning){
  1839. backup_s.pb2= s->pb2;
  1840. backup_s.tex_pb= s->tex_pb;
  1841. }
  1842. if(mb_type&MB_TYPE_INTER){
  1843. s->mv_dir = MV_DIR_FORWARD;
  1844. s->mv_type = MV_TYPE_16X16;
  1845. s->mb_intra= 0;
  1846. s->mv[0][0][0] = s->p_mv_table[xy][0];
  1847. s->mv[0][0][1] = s->p_mv_table[xy][1];
  1848. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  1849. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1850. }
  1851. if(mb_type&MB_TYPE_INTER4V){
  1852. s->mv_dir = MV_DIR_FORWARD;
  1853. s->mv_type = MV_TYPE_8X8;
  1854. s->mb_intra= 0;
  1855. for(i=0; i<4; i++){
  1856. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1857. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1858. }
  1859. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  1860. &dmin, &next_block, 0, 0);
  1861. }
  1862. if(mb_type&MB_TYPE_FORWARD){
  1863. s->mv_dir = MV_DIR_FORWARD;
  1864. s->mv_type = MV_TYPE_16X16;
  1865. s->mb_intra= 0;
  1866. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1867. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1868. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  1869. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1870. }
  1871. if(mb_type&MB_TYPE_BACKWARD){
  1872. s->mv_dir = MV_DIR_BACKWARD;
  1873. s->mv_type = MV_TYPE_16X16;
  1874. s->mb_intra= 0;
  1875. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1876. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1877. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  1878. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  1879. }
  1880. if(mb_type&MB_TYPE_BIDIR){
  1881. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1882. s->mv_type = MV_TYPE_16X16;
  1883. s->mb_intra= 0;
  1884. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1885. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1886. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1887. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1888. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  1889. &dmin, &next_block, 0, 0);
  1890. }
  1891. if(mb_type&MB_TYPE_DIRECT){
  1892. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1893. s->mv_type = MV_TYPE_16X16; //FIXME
  1894. s->mb_intra= 0;
  1895. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1896. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1897. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1898. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1899. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  1900. &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
  1901. }
  1902. if(mb_type&MB_TYPE_INTRA){
  1903. s->mv_dir = MV_DIR_FORWARD;
  1904. s->mv_type = MV_TYPE_16X16;
  1905. s->mb_intra= 1;
  1906. s->mv[0][0][0] = 0;
  1907. s->mv[0][0][1] = 0;
  1908. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  1909. &dmin, &next_block, 0, 0);
  1910. /* force cleaning of ac/dc pred stuff if needed ... */
  1911. if(s->h263_pred || s->h263_aic)
  1912. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1913. }
  1914. copy_context_after_encode(s, &best_s, -1);
  1915. pb_bits_count= get_bit_count(&s->pb);
  1916. flush_put_bits(&s->pb);
  1917. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  1918. s->pb= backup_s.pb;
  1919. if(s->data_partitioning){
  1920. pb2_bits_count= get_bit_count(&s->pb2);
  1921. flush_put_bits(&s->pb2);
  1922. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  1923. s->pb2= backup_s.pb2;
  1924. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  1925. flush_put_bits(&s->tex_pb);
  1926. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  1927. s->tex_pb= backup_s.tex_pb;
  1928. }
  1929. s->last_bits= get_bit_count(&s->pb);
  1930. } else {
  1931. int motion_x, motion_y;
  1932. s->mv_type=MV_TYPE_16X16;
  1933. // only one MB-Type possible
  1934. switch(mb_type){
  1935. case MB_TYPE_INTRA:
  1936. s->mv_dir = MV_DIR_FORWARD;
  1937. s->mb_intra= 1;
  1938. motion_x= s->mv[0][0][0] = 0;
  1939. motion_y= s->mv[0][0][1] = 0;
  1940. break;
  1941. case MB_TYPE_INTER:
  1942. s->mv_dir = MV_DIR_FORWARD;
  1943. s->mb_intra= 0;
  1944. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  1945. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  1946. break;
  1947. case MB_TYPE_INTER4V:
  1948. s->mv_dir = MV_DIR_FORWARD;
  1949. s->mv_type = MV_TYPE_8X8;
  1950. s->mb_intra= 0;
  1951. for(i=0; i<4; i++){
  1952. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1953. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1954. }
  1955. motion_x= motion_y= 0;
  1956. break;
  1957. case MB_TYPE_DIRECT:
  1958. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1959. s->mb_intra= 0;
  1960. motion_x=s->b_direct_mv_table[xy][0];
  1961. motion_y=s->b_direct_mv_table[xy][1];
  1962. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1963. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1964. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1965. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1966. break;
  1967. case MB_TYPE_BIDIR:
  1968. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1969. s->mb_intra= 0;
  1970. motion_x=0;
  1971. motion_y=0;
  1972. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1973. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1974. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1975. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1976. break;
  1977. case MB_TYPE_BACKWARD:
  1978. s->mv_dir = MV_DIR_BACKWARD;
  1979. s->mb_intra= 0;
  1980. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1981. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1982. break;
  1983. case MB_TYPE_FORWARD:
  1984. s->mv_dir = MV_DIR_FORWARD;
  1985. s->mb_intra= 0;
  1986. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1987. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1988. // printf(" %d %d ", motion_x, motion_y);
  1989. break;
  1990. default:
  1991. motion_x=motion_y=0; //gcc warning fix
  1992. printf("illegal MB type\n");
  1993. }
  1994. encode_mb(s, motion_x, motion_y);
  1995. }
  1996. /* clean the MV table in IPS frames for direct mode in B frames */
  1997. if(s->mb_intra /* && I,P,S_TYPE */){
  1998. s->p_mv_table[xy][0]=0;
  1999. s->p_mv_table[xy][1]=0;
  2000. }
  2001. MPV_decode_mb(s, s->block);
  2002. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  2003. }
  2004. /* Obtain average GOB size for RTP */
  2005. if (s->rtp_mode) {
  2006. if (!mb_y)
  2007. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  2008. else if (!(mb_y % s->gob_index)) {
  2009. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  2010. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  2011. }
  2012. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  2013. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  2014. if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
  2015. }
  2016. }
  2017. emms_c();
  2018. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  2019. ff_mpeg4_merge_partitions(s);
  2020. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  2021. msmpeg4_encode_ext_header(s);
  2022. if(s->codec_id==CODEC_ID_MPEG4)
  2023. ff_mpeg4_stuffing(&s->pb);
  2024. //if (s->gob_number)
  2025. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  2026. /* Send the last GOB if RTP */
  2027. if (s->rtp_mode) {
  2028. flush_put_bits(&s->pb);
  2029. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  2030. /* Call the RTP callback to send the last GOB */
  2031. if (s->rtp_callback)
  2032. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  2033. s->ptr_lastgob = pbBufPtr(&s->pb);
  2034. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  2035. }
  2036. }
  2037. static int dct_quantize_c(MpegEncContext *s,
  2038. DCTELEM *block, int n,
  2039. int qscale, int *overflow)
  2040. {
  2041. int i, j, level, last_non_zero, q;
  2042. const int *qmat;
  2043. int bias;
  2044. int max=0;
  2045. unsigned int threshold1, threshold2;
  2046. av_fdct (block);
  2047. /* we need this permutation so that we correct the IDCT
  2048. permutation. will be moved into DCT code */
  2049. block_permute(block);
  2050. if (s->mb_intra) {
  2051. if (!s->h263_aic) {
  2052. if (n < 4)
  2053. q = s->y_dc_scale;
  2054. else
  2055. q = s->c_dc_scale;
  2056. q = q << 3;
  2057. } else
  2058. /* For AIC we skip quant/dequant of INTRADC */
  2059. q = 1 << 3;
  2060. /* note: block[0] is assumed to be positive */
  2061. block[0] = (block[0] + (q >> 1)) / q;
  2062. i = 1;
  2063. last_non_zero = 0;
  2064. qmat = s->q_intra_matrix[qscale];
  2065. bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  2066. } else {
  2067. i = 0;
  2068. last_non_zero = -1;
  2069. qmat = s->q_inter_matrix[qscale];
  2070. bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  2071. }
  2072. threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
  2073. threshold2= threshold1<<1;
  2074. for(;i<64;i++) {
  2075. j = zigzag_direct[i];
  2076. level = block[j];
  2077. level = level * qmat[j];
  2078. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  2079. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  2080. if(((unsigned)(level+threshold1))>threshold2){
  2081. if(level>0){
  2082. level= (bias + level)>>(QMAT_SHIFT - 3);
  2083. block[j]= level;
  2084. }else{
  2085. level= (bias - level)>>(QMAT_SHIFT - 3);
  2086. block[j]= -level;
  2087. }
  2088. max |=level;
  2089. last_non_zero = i;
  2090. }else{
  2091. block[j]=0;
  2092. }
  2093. }
  2094. *overflow= s->max_qcoeff < max; //overflow might have happend
  2095. return last_non_zero;
  2096. }
  2097. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  2098. DCTELEM *block, int n, int qscale)
  2099. {
  2100. int i, level, nCoeffs;
  2101. const UINT16 *quant_matrix;
  2102. if(s->alternate_scan) nCoeffs= 64;
  2103. else nCoeffs= s->block_last_index[n]+1;
  2104. if (s->mb_intra) {
  2105. if (n < 4)
  2106. block[0] = block[0] * s->y_dc_scale;
  2107. else
  2108. block[0] = block[0] * s->c_dc_scale;
  2109. /* XXX: only mpeg1 */
  2110. quant_matrix = s->intra_matrix;
  2111. for(i=1;i<nCoeffs;i++) {
  2112. int j= zigzag_direct[i];
  2113. level = block[j];
  2114. if (level) {
  2115. if (level < 0) {
  2116. level = -level;
  2117. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2118. level = (level - 1) | 1;
  2119. level = -level;
  2120. } else {
  2121. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2122. level = (level - 1) | 1;
  2123. }
  2124. #ifdef PARANOID
  2125. if (level < -2048 || level > 2047)
  2126. fprintf(stderr, "unquant error %d %d\n", i, level);
  2127. #endif
  2128. block[j] = level;
  2129. }
  2130. }
  2131. } else {
  2132. i = 0;
  2133. quant_matrix = s->inter_matrix;
  2134. for(;i<nCoeffs;i++) {
  2135. int j= zigzag_direct[i];
  2136. level = block[j];
  2137. if (level) {
  2138. if (level < 0) {
  2139. level = -level;
  2140. level = (((level << 1) + 1) * qscale *
  2141. ((int) (quant_matrix[j]))) >> 4;
  2142. level = (level - 1) | 1;
  2143. level = -level;
  2144. } else {
  2145. level = (((level << 1) + 1) * qscale *
  2146. ((int) (quant_matrix[j]))) >> 4;
  2147. level = (level - 1) | 1;
  2148. }
  2149. #ifdef PARANOID
  2150. if (level < -2048 || level > 2047)
  2151. fprintf(stderr, "unquant error %d %d\n", i, level);
  2152. #endif
  2153. block[j] = level;
  2154. }
  2155. }
  2156. }
  2157. }
  2158. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  2159. DCTELEM *block, int n, int qscale)
  2160. {
  2161. int i, level, nCoeffs;
  2162. const UINT16 *quant_matrix;
  2163. if(s->alternate_scan) nCoeffs= 64;
  2164. else nCoeffs= s->block_last_index[n]+1;
  2165. if (s->mb_intra) {
  2166. if (n < 4)
  2167. block[0] = block[0] * s->y_dc_scale;
  2168. else
  2169. block[0] = block[0] * s->c_dc_scale;
  2170. quant_matrix = s->intra_matrix;
  2171. for(i=1;i<nCoeffs;i++) {
  2172. int j= zigzag_direct[i];
  2173. level = block[j];
  2174. if (level) {
  2175. if (level < 0) {
  2176. level = -level;
  2177. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2178. level = -level;
  2179. } else {
  2180. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2181. }
  2182. #ifdef PARANOID
  2183. if (level < -2048 || level > 2047)
  2184. fprintf(stderr, "unquant error %d %d\n", i, level);
  2185. #endif
  2186. block[j] = level;
  2187. }
  2188. }
  2189. } else {
  2190. int sum=-1;
  2191. i = 0;
  2192. quant_matrix = s->inter_matrix;
  2193. for(;i<nCoeffs;i++) {
  2194. int j= zigzag_direct[i];
  2195. level = block[j];
  2196. if (level) {
  2197. if (level < 0) {
  2198. level = -level;
  2199. level = (((level << 1) + 1) * qscale *
  2200. ((int) (quant_matrix[j]))) >> 4;
  2201. level = -level;
  2202. } else {
  2203. level = (((level << 1) + 1) * qscale *
  2204. ((int) (quant_matrix[j]))) >> 4;
  2205. }
  2206. #ifdef PARANOID
  2207. if (level < -2048 || level > 2047)
  2208. fprintf(stderr, "unquant error %d %d\n", i, level);
  2209. #endif
  2210. block[j] = level;
  2211. sum+=level;
  2212. }
  2213. }
  2214. block[63]^=sum&1;
  2215. }
  2216. }
  2217. static void dct_unquantize_h263_c(MpegEncContext *s,
  2218. DCTELEM *block, int n, int qscale)
  2219. {
  2220. int i, level, qmul, qadd;
  2221. int nCoeffs;
  2222. if (s->mb_intra) {
  2223. if (!s->h263_aic) {
  2224. if (n < 4)
  2225. block[0] = block[0] * s->y_dc_scale;
  2226. else
  2227. block[0] = block[0] * s->c_dc_scale;
  2228. }
  2229. i = 1;
  2230. nCoeffs= 64; //does not allways use zigzag table
  2231. } else {
  2232. i = 0;
  2233. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  2234. }
  2235. qmul = s->qscale << 1;
  2236. if (s->h263_aic && s->mb_intra)
  2237. qadd = 0;
  2238. else
  2239. qadd = (s->qscale - 1) | 1;
  2240. for(;i<nCoeffs;i++) {
  2241. level = block[i];
  2242. if (level) {
  2243. if (level < 0) {
  2244. level = level * qmul - qadd;
  2245. } else {
  2246. level = level * qmul + qadd;
  2247. }
  2248. #ifdef PARANOID
  2249. if (level < -2048 || level > 2047)
  2250. fprintf(stderr, "unquant error %d %d\n", i, level);
  2251. #endif
  2252. block[i] = level;
  2253. }
  2254. }
  2255. }
  2256. static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
  2257. {
  2258. int dc, dcb, dcr, y, i;
  2259. for(i=0; i<4; i++){
  2260. dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
  2261. for(y=0; y<8; y++){
  2262. int x;
  2263. for(x=0; x<8; x++){
  2264. dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
  2265. }
  2266. }
  2267. }
  2268. dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2269. dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2270. for(y=0; y<8; y++){
  2271. int x;
  2272. for(x=0; x<8; x++){
  2273. dest_cb[x + y*(s->uvlinesize)]= dcb/8;
  2274. dest_cr[x + y*(s->uvlinesize)]= dcr/8;
  2275. }
  2276. }
  2277. }
  2278. /**
  2279. * will conceal past errors, and allso drop b frames if needed
  2280. *
  2281. */
  2282. void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
  2283. {
  2284. int mb_x= s->mb_x;
  2285. int mb_y= s->mb_y;
  2286. int mb_dist=0;
  2287. int i, intra_count=0, inter_count=0;
  2288. int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
  2289. int inter_conceal= s->msmpeg4_version ? 50 : 50;
  2290. // for last block
  2291. if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
  2292. if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
  2293. if(s->decoding_error==0 && unknown_pos){
  2294. if(s->data_partitioning && s->pict_type!=B_TYPE)
  2295. s->decoding_error= DECODING_AC_LOST;
  2296. else
  2297. s->decoding_error= DECODING_DESYNC;
  2298. }
  2299. if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
  2300. for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
  2301. if(s->mbintra_table[i]) intra_count++;
  2302. else inter_count++;
  2303. }
  2304. if(s->decoding_error==DECODING_AC_LOST){
  2305. intra_conceal*=2;
  2306. inter_conceal*=2;
  2307. }else if(s->decoding_error==DECODING_ACDC_LOST){
  2308. intra_conceal*=2;
  2309. inter_conceal*=2;
  2310. }
  2311. if(unknown_pos && (intra_count<inter_count)){
  2312. intra_conceal= inter_conceal= s->mb_num;
  2313. // printf("%d %d\n",intra_count, inter_count);
  2314. }
  2315. fprintf(stderr, "concealing errors\n");
  2316. /* for all MBs from the current one back until the last resync marker */
  2317. for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
  2318. for(; mb_x>=0; mb_x--){
  2319. uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2320. uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2321. uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2322. int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
  2323. int mb_y_backup= s->mb_y;
  2324. s->mb_x=mb_x;
  2325. s->mb_y=mb_y;
  2326. if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
  2327. if(s->decoding_error==DECODING_AC_LOST){
  2328. remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  2329. // printf("remove ac to %d %d\n", mb_x, mb_y);
  2330. }else{
  2331. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2332. s->last_picture, 0, 0, put_pixels_tab,
  2333. 0/*mx*/, 0/*my*/, 16);
  2334. }
  2335. }
  2336. else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
  2337. int mx=0;
  2338. int my=0;
  2339. if(s->decoding_error!=DECODING_DESYNC){
  2340. int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
  2341. mx= s->motion_val[ xy ][0];
  2342. my= s->motion_val[ xy ][1];
  2343. }
  2344. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2345. s->last_picture, 0, 0, put_pixels_tab,
  2346. mx, my, 16);
  2347. }
  2348. s->mb_x= mb_x_backup;
  2349. s->mb_y= mb_y_backup;
  2350. if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
  2351. if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
  2352. }
  2353. mb_x=s->mb_width-1;
  2354. }
  2355. }
  2356. AVCodec mpeg1video_encoder = {
  2357. "mpeg1video",
  2358. CODEC_TYPE_VIDEO,
  2359. CODEC_ID_MPEG1VIDEO,
  2360. sizeof(MpegEncContext),
  2361. MPV_encode_init,
  2362. MPV_encode_picture,
  2363. MPV_encode_end,
  2364. };
  2365. AVCodec h263_encoder = {
  2366. "h263",
  2367. CODEC_TYPE_VIDEO,
  2368. CODEC_ID_H263,
  2369. sizeof(MpegEncContext),
  2370. MPV_encode_init,
  2371. MPV_encode_picture,
  2372. MPV_encode_end,
  2373. };
  2374. AVCodec h263p_encoder = {
  2375. "h263p",
  2376. CODEC_TYPE_VIDEO,
  2377. CODEC_ID_H263P,
  2378. sizeof(MpegEncContext),
  2379. MPV_encode_init,
  2380. MPV_encode_picture,
  2381. MPV_encode_end,
  2382. };
  2383. AVCodec rv10_encoder = {
  2384. "rv10",
  2385. CODEC_TYPE_VIDEO,
  2386. CODEC_ID_RV10,
  2387. sizeof(MpegEncContext),
  2388. MPV_encode_init,
  2389. MPV_encode_picture,
  2390. MPV_encode_end,
  2391. };
  2392. AVCodec mjpeg_encoder = {
  2393. "mjpeg",
  2394. CODEC_TYPE_VIDEO,
  2395. CODEC_ID_MJPEG,
  2396. sizeof(MpegEncContext),
  2397. MPV_encode_init,
  2398. MPV_encode_picture,
  2399. MPV_encode_end,
  2400. };
  2401. AVCodec mpeg4_encoder = {
  2402. "mpeg4",
  2403. CODEC_TYPE_VIDEO,
  2404. CODEC_ID_MPEG4,
  2405. sizeof(MpegEncContext),
  2406. MPV_encode_init,
  2407. MPV_encode_picture,
  2408. MPV_encode_end,
  2409. };
  2410. AVCodec msmpeg4v1_encoder = {
  2411. "msmpeg4v1",
  2412. CODEC_TYPE_VIDEO,
  2413. CODEC_ID_MSMPEG4V1,
  2414. sizeof(MpegEncContext),
  2415. MPV_encode_init,
  2416. MPV_encode_picture,
  2417. MPV_encode_end,
  2418. };
  2419. AVCodec msmpeg4v2_encoder = {
  2420. "msmpeg4v2",
  2421. CODEC_TYPE_VIDEO,
  2422. CODEC_ID_MSMPEG4V2,
  2423. sizeof(MpegEncContext),
  2424. MPV_encode_init,
  2425. MPV_encode_picture,
  2426. MPV_encode_end,
  2427. };
  2428. AVCodec msmpeg4v3_encoder = {
  2429. "msmpeg4",
  2430. CODEC_TYPE_VIDEO,
  2431. CODEC_ID_MSMPEG4V3,
  2432. sizeof(MpegEncContext),
  2433. MPV_encode_init,
  2434. MPV_encode_picture,
  2435. MPV_encode_end,
  2436. };
  2437. AVCodec wmv1_encoder = {
  2438. "wmv1",
  2439. CODEC_TYPE_VIDEO,
  2440. CODEC_ID_WMV1,
  2441. sizeof(MpegEncContext),
  2442. MPV_encode_init,
  2443. MPV_encode_picture,
  2444. MPV_encode_end,
  2445. };
  2446. AVCodec wmv2_encoder = {
  2447. "wmv2",
  2448. CODEC_TYPE_VIDEO,
  2449. CODEC_ID_WMV2,
  2450. sizeof(MpegEncContext),
  2451. MPV_encode_init,
  2452. MPV_encode_picture,
  2453. MPV_encode_end,
  2454. };