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.

2782 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. if(avctx->flags&CODEC_FLAG_DR1){
  536. int i;
  537. avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type);
  538. s->linesize = avctx->dr_stride;
  539. s->uvlinesize= avctx->dr_uvstride;
  540. s->ip_buffer_count= avctx->dr_ip_buffer_count;
  541. }
  542. if (s->pict_type == B_TYPE) {
  543. for(i=0;i<3;i++) {
  544. if(avctx->flags&CODEC_FLAG_DR1)
  545. s->aux_picture[i]= avctx->dr_buffer[i];
  546. s->current_picture[i] = s->aux_picture[i];
  547. }
  548. } else {
  549. for(i=0;i<3;i++) {
  550. /* swap next and last */
  551. if(avctx->flags&CODEC_FLAG_DR1)
  552. tmp= avctx->dr_buffer[i];
  553. else
  554. tmp = s->last_picture[i];
  555. s->last_picture[i] = s->next_picture[i];
  556. s->next_picture[i] = tmp;
  557. s->current_picture[i] = tmp;
  558. s->last_dr_opaque= s->next_dr_opaque;
  559. s->next_dr_opaque= avctx->dr_opaque_frame;
  560. if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)
  561. avctx->dr_opaque_frame= s->last_dr_opaque;
  562. else
  563. avctx->dr_opaque_frame= s->next_dr_opaque;
  564. }
  565. }
  566. }
  567. /* generic function for encode/decode called after a frame has been coded/decoded */
  568. void MPV_frame_end(MpegEncContext *s)
  569. {
  570. // if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads);
  571. /* draw edge for correct motion prediction if outside */
  572. if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  573. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version>=500){
  574. draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
  575. draw_edges(s->current_picture[1], s->uvlinesize, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  576. draw_edges(s->current_picture[2], s->uvlinesize, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
  577. }else{
  578. /* mpeg4? / opendivx / xvid */
  579. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  580. draw_edges(s->current_picture[1], s->uvlinesize, s->width/2, s->height/2, EDGE_WIDTH/2);
  581. draw_edges(s->current_picture[2], s->uvlinesize, s->width/2, s->height/2, EDGE_WIDTH/2);
  582. }
  583. }
  584. emms_c();
  585. if(s->pict_type!=B_TYPE){
  586. s->last_non_b_pict_type= s->pict_type;
  587. s->last_non_b_qscale= s->qscale;
  588. s->last_non_b_mc_mb_var= s->mc_mb_var_sum;
  589. s->num_available_buffers++;
  590. if(s->num_available_buffers>2) s->num_available_buffers= 2;
  591. }
  592. }
  593. /* reorder input for encoding */
  594. void reorder_input(MpegEncContext *s, AVPicture *pict)
  595. {
  596. int i, j, index;
  597. if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
  598. // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
  599. for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
  600. s->coded_order[j]= s->coded_order[j+1];
  601. }
  602. s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
  603. s->coded_order[j].pict_type=0;
  604. switch(s->input_pict_type){
  605. default:
  606. case I_TYPE:
  607. case S_TYPE:
  608. case P_TYPE:
  609. index= s->max_b_frames - s->b_frames_since_non_b;
  610. s->b_frames_since_non_b=0;
  611. break;
  612. case B_TYPE:
  613. index= s->max_b_frames + 1;
  614. s->b_frames_since_non_b++;
  615. break;
  616. }
  617. //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
  618. if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
  619. && pict->linesize[0] == s->linesize
  620. && pict->linesize[1] == s->uvlinesize
  621. && pict->linesize[2] == s->uvlinesize){
  622. //printf("ptr\n");
  623. for(i=0; i<3; i++){
  624. s->coded_order[index].picture[i]= pict->data[i];
  625. }
  626. }else{
  627. //printf("copy\n");
  628. for(i=0; i<3; i++){
  629. uint8_t *src = pict->data[i];
  630. uint8_t *dest;
  631. int src_wrap = pict->linesize[i];
  632. int dest_wrap = s->linesize;
  633. int w = s->width;
  634. int h = s->height;
  635. if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
  636. else dest= s->picture_buffer[s->picture_buffer_index][i];
  637. if (i >= 1) {
  638. dest_wrap >>= 1;
  639. w >>= 1;
  640. h >>= 1;
  641. }
  642. s->coded_order[index].picture[i]= dest;
  643. for(j=0;j<h;j++) {
  644. memcpy(dest, src, w);
  645. dest += dest_wrap;
  646. src += src_wrap;
  647. }
  648. }
  649. if(index!=0){
  650. s->picture_buffer_index++;
  651. if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
  652. }
  653. }
  654. s->coded_order[index].pict_type = s->input_pict_type;
  655. s->coded_order[index].qscale = s->input_qscale;
  656. s->coded_order[index].force_type= s->force_input_type;
  657. s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
  658. s->coded_order[index].picture_number= s->input_picture_number;
  659. for(i=0; i<3; i++){
  660. s->new_picture[i]= s->coded_order[0].picture[i];
  661. }
  662. }
  663. int MPV_encode_picture(AVCodecContext *avctx,
  664. unsigned char *buf, int buf_size, void *data)
  665. {
  666. MpegEncContext *s = avctx->priv_data;
  667. AVPicture *pict = data;
  668. s->input_qscale = avctx->quality;
  669. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  670. if(avctx->flags&CODEC_FLAG_TYPE){
  671. s->input_pict_type=
  672. s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
  673. }else if(s->flags&CODEC_FLAG_PASS2){
  674. s->input_pict_type=
  675. s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
  676. }else{
  677. s->force_input_type=0;
  678. if (!s->intra_only) {
  679. /* first picture of GOP is intra */
  680. if (s->input_picture_in_gop_number % s->gop_size==0){
  681. s->input_pict_type = I_TYPE;
  682. }else if(s->max_b_frames==0){
  683. s->input_pict_type = P_TYPE;
  684. }else{
  685. if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
  686. s->input_pict_type = B_TYPE;
  687. else
  688. s->input_pict_type = P_TYPE;
  689. }
  690. } else {
  691. s->input_pict_type = I_TYPE;
  692. }
  693. }
  694. if(s->input_pict_type==I_TYPE)
  695. s->input_picture_in_gop_number=0;
  696. reorder_input(s, pict);
  697. /* output? */
  698. if(s->coded_order[0].picture[0]){
  699. s->pict_type= s->coded_order[0].pict_type;
  700. if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  701. s->qscale= s->coded_order[0].qscale;
  702. s->force_type= s->coded_order[0].force_type;
  703. s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
  704. s->picture_number= s->coded_order[0].picture_number;
  705. MPV_frame_start(s, avctx);
  706. encode_picture(s, s->picture_number);
  707. avctx->key_frame = (s->pict_type == I_TYPE);
  708. avctx->pict_type = s->pict_type;
  709. avctx->real_pict_num = s->picture_number;
  710. avctx->header_bits = s->header_bits;
  711. avctx->mv_bits = s->mv_bits;
  712. avctx->misc_bits = s->misc_bits;
  713. avctx->i_tex_bits = s->i_tex_bits;
  714. avctx->p_tex_bits = s->p_tex_bits;
  715. avctx->i_count = s->i_count;
  716. avctx->p_count = s->p_count;
  717. avctx->skip_count = s->skip_count;
  718. MPV_frame_end(s);
  719. if (s->out_format == FMT_MJPEG)
  720. mjpeg_picture_trailer(s);
  721. avctx->quality = s->qscale;
  722. if(s->flags&CODEC_FLAG_PASS1)
  723. ff_write_pass1_stats(s);
  724. }
  725. s->input_picture_number++;
  726. s->input_picture_in_gop_number++;
  727. flush_put_bits(&s->pb);
  728. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  729. if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
  730. else s->pb_frame_bits= s->frame_bits;
  731. s->total_bits += s->frame_bits;
  732. avctx->frame_bits = s->frame_bits;
  733. //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
  734. //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);
  735. if (avctx->get_psnr) {
  736. /* At this point pict->data should have the original frame */
  737. /* an s->current_picture should have the coded/decoded frame */
  738. get_psnr(pict->data, s->current_picture,
  739. pict->linesize, s->linesize, avctx);
  740. // printf("%f\n", avctx->psnr_y);
  741. }
  742. return pbBufPtr(&s->pb) - s->pb.buf;
  743. }
  744. static inline void gmc1_motion(MpegEncContext *s,
  745. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  746. int dest_offset,
  747. UINT8 **ref_picture, int src_offset,
  748. int h)
  749. {
  750. UINT8 *ptr;
  751. int offset, src_x, src_y, linesize, uvlinesize;
  752. int motion_x, motion_y;
  753. if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
  754. motion_x= s->sprite_offset[0][0];
  755. motion_y= s->sprite_offset[0][1];
  756. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  757. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  758. motion_x<<=(3-s->sprite_warping_accuracy);
  759. motion_y<<=(3-s->sprite_warping_accuracy);
  760. src_x = clip(src_x, -16, s->width);
  761. if (src_x == s->width)
  762. motion_x =0;
  763. src_y = clip(src_y, -16, s->height);
  764. if (src_y == s->height)
  765. motion_y =0;
  766. linesize = s->linesize;
  767. uvlinesize = s->uvlinesize;
  768. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  769. dest_y+=dest_offset;
  770. gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  771. gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
  772. motion_x= s->sprite_offset[1][0];
  773. motion_y= s->sprite_offset[1][1];
  774. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  775. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  776. motion_x<<=(3-s->sprite_warping_accuracy);
  777. motion_y<<=(3-s->sprite_warping_accuracy);
  778. src_x = clip(src_x, -8, s->width>>1);
  779. if (src_x == s->width>>1)
  780. motion_x =0;
  781. src_y = clip(src_y, -8, s->height>>1);
  782. if (src_y == s->height>>1)
  783. motion_y =0;
  784. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  785. ptr = ref_picture[1] + offset;
  786. gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  787. ptr = ref_picture[2] + offset;
  788. gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
  789. return;
  790. }
  791. static void emulated_edge_mc(UINT8 *buf, UINT8 *src, int linesize, int block_w, int block_h,
  792. int src_x, int src_y, int w, int h){
  793. int x, y;
  794. int start_y, start_x, end_y, end_x;
  795. if(src_y>= h){
  796. src+= (h-1-src_y)*linesize;
  797. src_y=h-1;
  798. }else if(src_y<=-block_h){
  799. src+= (1-block_h-src_y)*linesize;
  800. src_y=1-block_h;
  801. }
  802. if(src_x>= w){
  803. src+= (w-1-src_x);
  804. src_x=w-1;
  805. }else if(src_x<=-block_w){
  806. src+= (1-block_w-src_x);
  807. src_x=1-block_w;
  808. }
  809. start_y= MAX(0, -src_y);
  810. start_x= MAX(0, -src_x);
  811. end_y= MIN(block_h, h-src_y);
  812. end_x= MIN(block_w, w-src_x);
  813. // copy existing part
  814. for(y=start_y; y<end_y; y++){
  815. for(x=start_x; x<end_x; x++){
  816. buf[x + y*linesize]= src[x + y*linesize];
  817. }
  818. }
  819. //top
  820. for(y=0; y<start_y; y++){
  821. for(x=start_x; x<end_x; x++){
  822. buf[x + y*linesize]= buf[x + start_y*linesize];
  823. }
  824. }
  825. //bottom
  826. for(y=end_y; y<block_h; y++){
  827. for(x=start_x; x<end_x; x++){
  828. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  829. }
  830. }
  831. for(y=0; y<block_h; y++){
  832. //left
  833. for(x=0; x<start_x; x++){
  834. buf[x + y*linesize]= buf[start_x + y*linesize];
  835. }
  836. //right
  837. for(x=end_x; x<block_w; x++){
  838. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  839. }
  840. }
  841. }
  842. /* apply one mpeg motion vector to the three components */
  843. static inline void mpeg_motion(MpegEncContext *s,
  844. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  845. int dest_offset,
  846. UINT8 **ref_picture, int src_offset,
  847. int field_based, op_pixels_func *pix_op,
  848. int motion_x, int motion_y, int h)
  849. {
  850. UINT8 *ptr;
  851. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  852. int emu=0;
  853. if(s->quarter_sample)
  854. {
  855. motion_x>>=1;
  856. motion_y>>=1;
  857. }
  858. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  859. src_x = s->mb_x * 16 + (motion_x >> 1);
  860. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  861. /* WARNING: do no forget half pels */
  862. height = s->height >> field_based;
  863. src_x = clip(src_x, -16, s->width);
  864. if (src_x == s->width)
  865. dxy &= ~1;
  866. src_y = clip(src_y, -16, height);
  867. if (src_y == height)
  868. dxy &= ~2;
  869. linesize = s->linesize << field_based;
  870. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  871. dest_y += dest_offset;
  872. if(s->flags&CODEC_FLAG_EMU_EDGE){
  873. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->width
  874. || src_y + (motion_y&1) + h > height){
  875. emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, h+1, src_x, src_y, s->width, height);
  876. ptr= s->edge_emu_buffer;
  877. emu=1;
  878. }
  879. }
  880. pix_op[dxy](dest_y, ptr, linesize, h);
  881. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  882. if(s->flags&CODEC_FLAG_GRAY) return;
  883. if (s->out_format == FMT_H263) {
  884. dxy = 0;
  885. if ((motion_x & 3) != 0)
  886. dxy |= 1;
  887. if ((motion_y & 3) != 0)
  888. dxy |= 2;
  889. mx = motion_x >> 2;
  890. my = motion_y >> 2;
  891. } else {
  892. mx = motion_x / 2;
  893. my = motion_y / 2;
  894. dxy = ((my & 1) << 1) | (mx & 1);
  895. mx >>= 1;
  896. my >>= 1;
  897. }
  898. src_x = s->mb_x * 8 + mx;
  899. src_y = s->mb_y * (8 >> field_based) + my;
  900. src_x = clip(src_x, -8, s->width >> 1);
  901. if (src_x == (s->width >> 1))
  902. dxy &= ~1;
  903. src_y = clip(src_y, -8, height >> 1);
  904. if (src_y == (height >> 1))
  905. dxy &= ~2;
  906. offset = (src_y * s->uvlinesize) + src_x + (src_offset >> 1);
  907. ptr = ref_picture[1] + offset;
  908. if(emu){
  909. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
  910. ptr= s->edge_emu_buffer;
  911. }
  912. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, s->uvlinesize, h >> 1);
  913. ptr = ref_picture[2] + offset;
  914. if(emu){
  915. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
  916. ptr= s->edge_emu_buffer;
  917. }
  918. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, s->uvlinesize, h >> 1);
  919. }
  920. static inline void qpel_motion(MpegEncContext *s,
  921. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  922. int dest_offset,
  923. UINT8 **ref_picture, int src_offset,
  924. int field_based, op_pixels_func *pix_op,
  925. qpel_mc_func *qpix_op,
  926. int motion_x, int motion_y, int h)
  927. {
  928. UINT8 *ptr;
  929. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  930. int emu=0;
  931. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  932. src_x = s->mb_x * 16 + (motion_x >> 2);
  933. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  934. height = s->height >> field_based;
  935. src_x = clip(src_x, -16, s->width);
  936. if (src_x == s->width)
  937. dxy &= ~3;
  938. src_y = clip(src_y, -16, height);
  939. if (src_y == height)
  940. dxy &= ~12;
  941. linesize = s->linesize << field_based;
  942. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  943. dest_y += dest_offset;
  944. //printf("%d %d %d\n", src_x, src_y, dxy);
  945. if(s->flags&CODEC_FLAG_EMU_EDGE){
  946. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->width
  947. || src_y + (motion_y&3) + h > height){
  948. emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, h+1, src_x, src_y, s->width, height);
  949. ptr= s->edge_emu_buffer;
  950. emu=1;
  951. }
  952. }
  953. qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
  954. qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
  955. qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
  956. qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
  957. if(s->flags&CODEC_FLAG_GRAY) return;
  958. mx= (motion_x>>1) | (motion_x&1);
  959. my= (motion_y>>1) | (motion_y&1);
  960. dxy = 0;
  961. if ((mx & 3) != 0)
  962. dxy |= 1;
  963. if ((my & 3) != 0)
  964. dxy |= 2;
  965. mx = mx >> 2;
  966. my = my >> 2;
  967. src_x = s->mb_x * 8 + mx;
  968. src_y = s->mb_y * (8 >> field_based) + my;
  969. src_x = clip(src_x, -8, s->width >> 1);
  970. if (src_x == (s->width >> 1))
  971. dxy &= ~1;
  972. src_y = clip(src_y, -8, height >> 1);
  973. if (src_y == (height >> 1))
  974. dxy &= ~2;
  975. offset = (src_y * s->uvlinesize) + src_x + (src_offset >> 1);
  976. ptr = ref_picture[1] + offset;
  977. if(emu){
  978. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
  979. ptr= s->edge_emu_buffer;
  980. }
  981. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, s->uvlinesize, h >> 1);
  982. ptr = ref_picture[2] + offset;
  983. if(emu){
  984. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
  985. ptr= s->edge_emu_buffer;
  986. }
  987. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, s->uvlinesize, h >> 1);
  988. }
  989. static inline void MPV_motion(MpegEncContext *s,
  990. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  991. int dir, UINT8 **ref_picture,
  992. op_pixels_func *pix_op, qpel_mc_func *qpix_op)
  993. {
  994. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  995. int mb_x, mb_y, i;
  996. UINT8 *ptr, *dest;
  997. int emu=0;
  998. mb_x = s->mb_x;
  999. mb_y = s->mb_y;
  1000. switch(s->mv_type) {
  1001. case MV_TYPE_16X16:
  1002. if(s->mcsel){
  1003. #if 0
  1004. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1005. ref_picture, 0,
  1006. 0, pix_op,
  1007. s->sprite_offset[0][0]>>3,
  1008. s->sprite_offset[0][1]>>3,
  1009. 16);
  1010. #else
  1011. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  1012. ref_picture, 0,
  1013. 16);
  1014. #endif
  1015. }else if(s->quarter_sample && dir==0){ //FIXME
  1016. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1017. ref_picture, 0,
  1018. 0, pix_op, qpix_op,
  1019. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1020. }else{
  1021. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1022. ref_picture, 0,
  1023. 0, pix_op,
  1024. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1025. }
  1026. break;
  1027. case MV_TYPE_8X8:
  1028. for(i=0;i<4;i++) {
  1029. motion_x = s->mv[dir][i][0];
  1030. motion_y = s->mv[dir][i][1];
  1031. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1032. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1033. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1034. /* WARNING: do no forget half pels */
  1035. src_x = clip(src_x, -16, s->width);
  1036. if (src_x == s->width)
  1037. dxy &= ~1;
  1038. src_y = clip(src_y, -16, s->height);
  1039. if (src_y == s->height)
  1040. dxy &= ~2;
  1041. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1042. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1043. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->width
  1044. || src_y + (motion_y&1) + 8 > s->height){
  1045. emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->width, s->height);
  1046. ptr= s->edge_emu_buffer;
  1047. }
  1048. }
  1049. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1050. pix_op[dxy](dest, ptr, s->linesize, 8);
  1051. }
  1052. if(s->flags&CODEC_FLAG_GRAY) break;
  1053. /* In case of 8X8, we construct a single chroma motion vector
  1054. with a special rounding */
  1055. mx = 0;
  1056. my = 0;
  1057. for(i=0;i<4;i++) {
  1058. mx += s->mv[dir][i][0];
  1059. my += s->mv[dir][i][1];
  1060. }
  1061. if (mx >= 0)
  1062. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1063. else {
  1064. mx = -mx;
  1065. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1066. }
  1067. if (my >= 0)
  1068. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1069. else {
  1070. my = -my;
  1071. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1072. }
  1073. dxy = ((my & 1) << 1) | (mx & 1);
  1074. mx >>= 1;
  1075. my >>= 1;
  1076. src_x = mb_x * 8 + mx;
  1077. src_y = mb_y * 8 + my;
  1078. src_x = clip(src_x, -8, s->width/2);
  1079. if (src_x == s->width/2)
  1080. dxy &= ~1;
  1081. src_y = clip(src_y, -8, s->height/2);
  1082. if (src_y == s->height/2)
  1083. dxy &= ~2;
  1084. offset = (src_y * (s->uvlinesize)) + src_x;
  1085. ptr = ref_picture[1] + offset;
  1086. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1087. if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->width >>1
  1088. || src_y + (dxy>>1) + 8 > s->height>>1){
  1089. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->width>>1, s->height>>1);
  1090. ptr= s->edge_emu_buffer;
  1091. emu=1;
  1092. }
  1093. }
  1094. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  1095. ptr = ref_picture[2] + offset;
  1096. if(emu){
  1097. emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->width>>1, s->height>>1);
  1098. ptr= s->edge_emu_buffer;
  1099. }
  1100. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  1101. break;
  1102. case MV_TYPE_FIELD:
  1103. if (s->picture_structure == PICT_FRAME) {
  1104. /* top field */
  1105. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1106. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1107. 1, pix_op,
  1108. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1109. /* bottom field */
  1110. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1111. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1112. 1, pix_op,
  1113. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1114. } else {
  1115. }
  1116. break;
  1117. }
  1118. }
  1119. /* put block[] to dest[] */
  1120. static inline void put_dct(MpegEncContext *s,
  1121. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1122. {
  1123. if (!s->mpeg2)
  1124. s->dct_unquantize(s, block, i, s->qscale);
  1125. ff_idct_put (dest, line_size, block);
  1126. }
  1127. /* add block[] to dest[] */
  1128. static inline void add_dct(MpegEncContext *s,
  1129. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1130. {
  1131. if (s->block_last_index[i] >= 0) {
  1132. ff_idct_add (dest, line_size, block);
  1133. }
  1134. }
  1135. static inline void add_dequant_dct(MpegEncContext *s,
  1136. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1137. {
  1138. if (s->block_last_index[i] >= 0) {
  1139. s->dct_unquantize(s, block, i, s->qscale);
  1140. ff_idct_add (dest, line_size, block);
  1141. }
  1142. }
  1143. /**
  1144. * cleans dc, ac, coded_block for the current non intra MB
  1145. */
  1146. void ff_clean_intra_table_entries(MpegEncContext *s)
  1147. {
  1148. int wrap = s->block_wrap[0];
  1149. int xy = s->block_index[0];
  1150. s->dc_val[0][xy ] =
  1151. s->dc_val[0][xy + 1 ] =
  1152. s->dc_val[0][xy + wrap] =
  1153. s->dc_val[0][xy + 1 + wrap] = 1024;
  1154. /* ac pred */
  1155. memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16));
  1156. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
  1157. if (s->msmpeg4_version>=3) {
  1158. s->coded_block[xy ] =
  1159. s->coded_block[xy + 1 ] =
  1160. s->coded_block[xy + wrap] =
  1161. s->coded_block[xy + 1 + wrap] = 0;
  1162. }
  1163. /* chroma */
  1164. wrap = s->block_wrap[4];
  1165. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  1166. s->dc_val[1][xy] =
  1167. s->dc_val[2][xy] = 1024;
  1168. /* ac pred */
  1169. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  1170. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  1171. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1172. }
  1173. /* generic function called after a macroblock has been parsed by the
  1174. decoder or after it has been encoded by the encoder.
  1175. Important variables used:
  1176. s->mb_intra : true if intra macroblock
  1177. s->mv_dir : motion vector direction
  1178. s->mv_type : motion vector type
  1179. s->mv : motion vector
  1180. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1181. */
  1182. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1183. {
  1184. int mb_x, mb_y;
  1185. const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
  1186. mb_x = s->mb_x;
  1187. mb_y = s->mb_y;
  1188. #ifdef FF_POSTPROCESS
  1189. /* Obsolete. Exists for compatibility with mplayer only. */
  1190. quant_store[mb_y][mb_x]=s->qscale;
  1191. //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
  1192. #else
  1193. if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
  1194. #endif
  1195. /* update DC predictors for P macroblocks */
  1196. if (!s->mb_intra) {
  1197. if (s->h263_pred || s->h263_aic) {
  1198. if(s->mbintra_table[mb_xy])
  1199. ff_clean_intra_table_entries(s);
  1200. } else {
  1201. s->last_dc[0] =
  1202. s->last_dc[1] =
  1203. s->last_dc[2] = 128 << s->intra_dc_precision;
  1204. }
  1205. }
  1206. else if (s->h263_pred || s->h263_aic)
  1207. s->mbintra_table[mb_xy]=1;
  1208. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1209. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1210. int motion_x, motion_y;
  1211. const int wrap = s->block_wrap[0];
  1212. const int xy = s->block_index[0];
  1213. if (s->mb_intra) {
  1214. motion_x = 0;
  1215. motion_y = 0;
  1216. goto motion_init;
  1217. } else if (s->mv_type == MV_TYPE_16X16) {
  1218. motion_x = s->mv[0][0][0];
  1219. motion_y = s->mv[0][0][1];
  1220. motion_init:
  1221. /* no update if 8X8 because it has been done during parsing */
  1222. s->motion_val[xy][0] = motion_x;
  1223. s->motion_val[xy][1] = motion_y;
  1224. s->motion_val[xy + 1][0] = motion_x;
  1225. s->motion_val[xy + 1][1] = motion_y;
  1226. s->motion_val[xy + wrap][0] = motion_x;
  1227. s->motion_val[xy + wrap][1] = motion_y;
  1228. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1229. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1230. }
  1231. }
  1232. if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
  1233. UINT8 *dest_y, *dest_cb, *dest_cr;
  1234. int dct_linesize, dct_offset;
  1235. op_pixels_func *op_pix;
  1236. qpel_mc_func *op_qpix;
  1237. /* avoid copy if macroblock skipped in last frame too
  1238. dont touch it for B-frames as they need the skip info from the next p-frame */
  1239. if (s->pict_type != B_TYPE) {
  1240. UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
  1241. if (s->mb_skiped) {
  1242. s->mb_skiped = 0;
  1243. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  1244. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1245. /* if previous was skipped too, then nothing to do !
  1246. skip only during decoding as we might trash the buffers during encoding a bit */
  1247. if (*mbskip_ptr >= s->ip_buffer_count && !s->encoding)
  1248. goto the_end;
  1249. } else {
  1250. *mbskip_ptr = 0; /* not skipped */
  1251. }
  1252. }
  1253. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  1254. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1255. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1256. if (s->interlaced_dct) {
  1257. dct_linesize = s->linesize * 2;
  1258. dct_offset = s->linesize;
  1259. } else {
  1260. dct_linesize = s->linesize;
  1261. dct_offset = s->linesize * 8;
  1262. }
  1263. if (!s->mb_intra) {
  1264. /* motion handling */
  1265. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1266. if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
  1267. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1268. op_pix = put_pixels_tab;
  1269. op_qpix= qpel_mc_rnd_tab;
  1270. }else{
  1271. op_pix = put_no_rnd_pixels_tab;
  1272. op_qpix= qpel_mc_no_rnd_tab;
  1273. }
  1274. if (s->mv_dir & MV_DIR_FORWARD) {
  1275. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1276. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1277. op_pix = avg_pixels_tab;
  1278. else
  1279. op_pix = avg_no_rnd_pixels_tab;
  1280. }
  1281. if (s->mv_dir & MV_DIR_BACKWARD) {
  1282. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1283. }
  1284. }
  1285. /* skip dequant / idct if we are really late ;) */
  1286. if(s->hurry_up>1) goto the_end;
  1287. /* add dct residue */
  1288. if(s->encoding || !(s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG4)){
  1289. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  1290. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1291. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1292. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1293. if(!(s->flags&CODEC_FLAG_GRAY)){
  1294. add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1295. add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1296. }
  1297. } else {
  1298. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1299. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1300. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1301. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1302. if(!(s->flags&CODEC_FLAG_GRAY)){
  1303. add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1304. add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1305. }
  1306. }
  1307. } else {
  1308. /* dct only in intra block */
  1309. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1310. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1311. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1312. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1313. if(!(s->flags&CODEC_FLAG_GRAY)){
  1314. put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1315. put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1316. }
  1317. }
  1318. }
  1319. the_end:
  1320. emms_c(); //FIXME remove
  1321. }
  1322. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc)
  1323. {
  1324. static const char tab[64]=
  1325. {3,2,2,1,1,1,1,1,
  1326. 1,1,1,1,1,1,1,1,
  1327. 1,1,1,1,1,1,1,1,
  1328. 0,0,0,0,0,0,0,0,
  1329. 0,0,0,0,0,0,0,0,
  1330. 0,0,0,0,0,0,0,0,
  1331. 0,0,0,0,0,0,0,0,
  1332. 0,0,0,0,0,0,0,0};
  1333. int score=0;
  1334. int run=0;
  1335. int i;
  1336. DCTELEM *block= s->block[n];
  1337. const int last_index= s->block_last_index[n];
  1338. if(skip_dc) skip_dc=1;
  1339. /* are all which we could set to zero are allready zero? */
  1340. if(last_index<=skip_dc - 1) return;
  1341. for(i=0; i<=last_index; i++){
  1342. const int j = zigzag_direct[i];
  1343. const int level = ABS(block[j]);
  1344. if(level==1){
  1345. if(skip_dc && i==0) continue;
  1346. score+= tab[run];
  1347. run=0;
  1348. }else if(level>1){
  1349. return;
  1350. }else{
  1351. run++;
  1352. }
  1353. }
  1354. if(score >= threshold) return;
  1355. for(i=skip_dc; i<=last_index; i++){
  1356. const int j = zigzag_direct[i];
  1357. block[j]=0;
  1358. }
  1359. if(block[0]) s->block_last_index[n]= 0;
  1360. else s->block_last_index[n]= -1;
  1361. }
  1362. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1363. {
  1364. int i;
  1365. const int maxlevel= s->max_qcoeff;
  1366. const int minlevel= s->min_qcoeff;
  1367. for(i=0;i<=last_index; i++){
  1368. const int j = zigzag_direct[i];
  1369. int level = block[j];
  1370. if (level>maxlevel) level=maxlevel;
  1371. else if(level<minlevel) level=minlevel;
  1372. block[j]= level;
  1373. }
  1374. }
  1375. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1376. {
  1377. const int mb_x= s->mb_x;
  1378. const int mb_y= s->mb_y;
  1379. int i;
  1380. int skip_dct[6];
  1381. #if 0
  1382. if (s->interlaced_dct) {
  1383. dct_linesize = s->linesize * 2;
  1384. dct_offset = s->linesize;
  1385. } else {
  1386. dct_linesize = s->linesize;
  1387. dct_offset = s->linesize * 8;
  1388. }
  1389. #endif
  1390. for(i=0; i<6; i++) skip_dct[i]=0;
  1391. if (s->mb_intra) {
  1392. UINT8 *ptr;
  1393. int wrap;
  1394. wrap = s->linesize;
  1395. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  1396. get_pixels(s->block[0], ptr , wrap);
  1397. get_pixels(s->block[1], ptr + 8, wrap);
  1398. get_pixels(s->block[2], ptr + 8 * wrap , wrap);
  1399. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  1400. if(s->flags&CODEC_FLAG_GRAY){
  1401. skip_dct[4]= 1;
  1402. skip_dct[5]= 1;
  1403. }else{
  1404. wrap >>=1;
  1405. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  1406. get_pixels(s->block[4], ptr, wrap);
  1407. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  1408. get_pixels(s->block[5], ptr, wrap);
  1409. }
  1410. }else{
  1411. op_pixels_func *op_pix;
  1412. qpel_mc_func *op_qpix;
  1413. UINT8 *dest_y, *dest_cb, *dest_cr;
  1414. UINT8 *ptr_y, *ptr_cb, *ptr_cr;
  1415. int wrap_y, wrap_c;
  1416. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  1417. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1418. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1419. wrap_y = s->linesize;
  1420. wrap_c = wrap_y>>1;
  1421. ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1422. ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1423. ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1424. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1425. op_pix = put_pixels_tab;
  1426. op_qpix= qpel_mc_rnd_tab;
  1427. }else{
  1428. op_pix = put_no_rnd_pixels_tab;
  1429. op_qpix= qpel_mc_no_rnd_tab;
  1430. }
  1431. if (s->mv_dir & MV_DIR_FORWARD) {
  1432. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1433. if ((!s->no_rounding) || s->pict_type==B_TYPE)
  1434. op_pix = avg_pixels_tab;
  1435. else
  1436. op_pix = avg_no_rnd_pixels_tab;
  1437. }
  1438. if (s->mv_dir & MV_DIR_BACKWARD) {
  1439. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1440. }
  1441. diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  1442. diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1443. diff_pixels(s->block[2], ptr_y + 8 * wrap_y , dest_y + 8 * wrap_y , wrap_y);
  1444. diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y);
  1445. if(s->flags&CODEC_FLAG_GRAY){
  1446. skip_dct[4]= 1;
  1447. skip_dct[5]= 1;
  1448. }else{
  1449. diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1450. diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1451. }
  1452. /* pre quantization */
  1453. if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  1454. if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  1455. if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  1456. if(pix_abs8x8(ptr_y + 8*wrap_y , dest_y + 8*wrap_y , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  1457. if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  1458. if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
  1459. if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
  1460. #if 0
  1461. {
  1462. static int stat[7];
  1463. int num=0;
  1464. for(i=0; i<6; i++)
  1465. if(skip_dct[i]) num++;
  1466. stat[num]++;
  1467. if(s->mb_x==0 && s->mb_y==0){
  1468. for(i=0; i<7; i++){
  1469. printf("%6d %1d\n", stat[i], i);
  1470. }
  1471. }
  1472. }
  1473. #endif
  1474. }
  1475. }
  1476. #if 0
  1477. {
  1478. float adap_parm;
  1479. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  1480. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  1481. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  1482. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  1483. s->qscale, adap_parm, s->qscale*adap_parm,
  1484. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  1485. }
  1486. #endif
  1487. /* DCT & quantize */
  1488. if(s->out_format==FMT_MJPEG){
  1489. for(i=0;i<6;i++) {
  1490. int overflow;
  1491. s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
  1492. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1493. }
  1494. }else{
  1495. for(i=0;i<6;i++) {
  1496. if(!skip_dct[i]){
  1497. int overflow;
  1498. s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1499. // FIXME we could decide to change to quantizer instead of clipping
  1500. // JS: I don't think that would be a good idea it could lower quality instead
  1501. // of improve it. Just INTRADC clipping deserves changes in quantizer
  1502. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1503. }else
  1504. s->block_last_index[i]= -1;
  1505. }
  1506. if(s->luma_elim_threshold && !s->mb_intra)
  1507. for(i=0; i<4; i++)
  1508. dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0);
  1509. if(s->chroma_elim_threshold && !s->mb_intra)
  1510. for(i=4; i<6; i++)
  1511. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1);
  1512. }
  1513. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  1514. s->block_last_index[4]=
  1515. s->block_last_index[5]= 0;
  1516. s->block[4][0]=
  1517. s->block[5][0]= 128;
  1518. }
  1519. /* huffman encode */
  1520. switch(s->out_format) {
  1521. case FMT_MPEG1:
  1522. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1523. break;
  1524. case FMT_H263:
  1525. if (s->h263_msmpeg4)
  1526. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1527. else if(s->h263_pred)
  1528. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1529. else
  1530. h263_encode_mb(s, s->block, motion_x, motion_y);
  1531. break;
  1532. case FMT_MJPEG:
  1533. mjpeg_encode_mb(s, s->block);
  1534. break;
  1535. }
  1536. }
  1537. void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
  1538. {
  1539. int bytes= length>>4;
  1540. int bits= length&15;
  1541. int i;
  1542. if(length==0) return;
  1543. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  1544. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  1545. }
  1546. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1547. int i;
  1548. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1549. /* mpeg1 */
  1550. d->mb_incr= s->mb_incr;
  1551. for(i=0; i<3; i++)
  1552. d->last_dc[i]= s->last_dc[i];
  1553. /* statistics */
  1554. d->mv_bits= s->mv_bits;
  1555. d->i_tex_bits= s->i_tex_bits;
  1556. d->p_tex_bits= s->p_tex_bits;
  1557. d->i_count= s->i_count;
  1558. d->p_count= s->p_count;
  1559. d->skip_count= s->skip_count;
  1560. d->misc_bits= s->misc_bits;
  1561. d->last_bits= 0;
  1562. d->mb_skiped= s->mb_skiped;
  1563. }
  1564. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1565. int i;
  1566. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  1567. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1568. /* mpeg1 */
  1569. d->mb_incr= s->mb_incr;
  1570. for(i=0; i<3; i++)
  1571. d->last_dc[i]= s->last_dc[i];
  1572. /* statistics */
  1573. d->mv_bits= s->mv_bits;
  1574. d->i_tex_bits= s->i_tex_bits;
  1575. d->p_tex_bits= s->p_tex_bits;
  1576. d->i_count= s->i_count;
  1577. d->p_count= s->p_count;
  1578. d->skip_count= s->skip_count;
  1579. d->misc_bits= s->misc_bits;
  1580. d->mb_intra= s->mb_intra;
  1581. d->mb_skiped= s->mb_skiped;
  1582. d->mv_type= s->mv_type;
  1583. d->mv_dir= s->mv_dir;
  1584. d->pb= s->pb;
  1585. if(s->data_partitioning){
  1586. d->pb2= s->pb2;
  1587. d->tex_pb= s->tex_pb;
  1588. }
  1589. d->block= s->block;
  1590. for(i=0; i<6; i++)
  1591. d->block_last_index[i]= s->block_last_index[i];
  1592. }
  1593. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  1594. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  1595. int *dmin, int *next_block, int motion_x, int motion_y)
  1596. {
  1597. int bits_count;
  1598. copy_context_before_encode(s, backup, type);
  1599. s->block= s->blocks[*next_block];
  1600. s->pb= pb[*next_block];
  1601. if(s->data_partitioning){
  1602. s->pb2 = pb2 [*next_block];
  1603. s->tex_pb= tex_pb[*next_block];
  1604. }
  1605. encode_mb(s, motion_x, motion_y);
  1606. bits_count= get_bit_count(&s->pb);
  1607. if(s->data_partitioning){
  1608. bits_count+= get_bit_count(&s->pb2);
  1609. bits_count+= get_bit_count(&s->tex_pb);
  1610. }
  1611. if(bits_count<*dmin){
  1612. *dmin= bits_count;
  1613. *next_block^=1;
  1614. copy_context_after_encode(best, s, type);
  1615. }
  1616. }
  1617. static void encode_picture(MpegEncContext *s, int picture_number)
  1618. {
  1619. int mb_x, mb_y, last_gob, pdif = 0;
  1620. int i;
  1621. int bits;
  1622. MpegEncContext best_s, backup_s;
  1623. UINT8 bit_buf[2][3000];
  1624. UINT8 bit_buf2[2][3000];
  1625. UINT8 bit_buf_tex[2][3000];
  1626. PutBitContext pb[2], pb2[2], tex_pb[2];
  1627. for(i=0; i<2; i++){
  1628. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  1629. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  1630. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  1631. }
  1632. s->picture_number = picture_number;
  1633. s->block_wrap[0]=
  1634. s->block_wrap[1]=
  1635. s->block_wrap[2]=
  1636. s->block_wrap[3]= s->mb_width*2 + 2;
  1637. s->block_wrap[4]=
  1638. s->block_wrap[5]= s->mb_width + 2;
  1639. /* Reset the average MB variance */
  1640. s->mb_var_sum = 0;
  1641. s->mc_mb_var_sum = 0;
  1642. /* we need to initialize some time vars before we can encode b-frames */
  1643. if (s->h263_pred && !s->h263_msmpeg4)
  1644. ff_set_mpeg4_time(s, s->picture_number);
  1645. /* Estimate motion for every MB */
  1646. if(s->pict_type != I_TYPE){
  1647. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1648. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1649. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1650. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1651. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1652. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1653. s->mb_x = mb_x;
  1654. s->mb_y = mb_y;
  1655. s->block_index[0]+=2;
  1656. s->block_index[1]+=2;
  1657. s->block_index[2]+=2;
  1658. s->block_index[3]+=2;
  1659. /* compute motion vector & mb_type and store in context */
  1660. if(s->pict_type==B_TYPE)
  1661. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  1662. else
  1663. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  1664. // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
  1665. }
  1666. }
  1667. emms_c();
  1668. }else /* if(s->pict_type == I_TYPE) */{
  1669. /* I-Frame */
  1670. //FIXME do we need to zero them?
  1671. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  1672. memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
  1673. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1674. }
  1675. if(s->mb_var_sum < s->mc_mb_var_sum && s->pict_type == P_TYPE){ //FIXME subtract MV bits
  1676. s->pict_type= I_TYPE;
  1677. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  1678. if(s->max_b_frames==0){
  1679. s->input_pict_type= I_TYPE;
  1680. s->input_picture_in_gop_number=0;
  1681. }
  1682. //printf("Scene change detected, encoding as I Frame\n");
  1683. }
  1684. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
  1685. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  1686. ff_fix_long_p_mvs(s);
  1687. if(s->pict_type==B_TYPE){
  1688. s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  1689. s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  1690. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  1691. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  1692. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  1693. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  1694. }
  1695. //printf("f_code %d ///\n", s->f_code);
  1696. // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
  1697. if(s->flags&CODEC_FLAG_PASS2)
  1698. s->qscale = ff_rate_estimate_qscale_pass2(s);
  1699. else if (!s->fixed_qscale)
  1700. s->qscale = ff_rate_estimate_qscale(s);
  1701. if (s->out_format == FMT_MJPEG) {
  1702. /* for mjpeg, we do include qscale in the matrix */
  1703. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  1704. for(i=1;i<64;i++)
  1705. s->intra_matrix[i] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  1706. convert_matrix(s->q_intra_matrix, s->q_intra_matrix16,
  1707. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
  1708. }
  1709. s->last_bits= get_bit_count(&s->pb);
  1710. switch(s->out_format) {
  1711. case FMT_MJPEG:
  1712. mjpeg_picture_header(s);
  1713. break;
  1714. case FMT_H263:
  1715. if (s->h263_msmpeg4)
  1716. msmpeg4_encode_picture_header(s, picture_number);
  1717. else if (s->h263_pred)
  1718. mpeg4_encode_picture_header(s, picture_number);
  1719. else if (s->h263_rv10)
  1720. rv10_encode_picture_header(s, picture_number);
  1721. else
  1722. h263_encode_picture_header(s, picture_number);
  1723. break;
  1724. case FMT_MPEG1:
  1725. mpeg1_encode_picture_header(s, picture_number);
  1726. break;
  1727. }
  1728. bits= get_bit_count(&s->pb);
  1729. s->header_bits= bits - s->last_bits;
  1730. s->last_bits= bits;
  1731. s->mv_bits=0;
  1732. s->misc_bits=0;
  1733. s->i_tex_bits=0;
  1734. s->p_tex_bits=0;
  1735. s->i_count=0;
  1736. s->p_count=0;
  1737. s->skip_count=0;
  1738. /* init last dc values */
  1739. /* note: quant matrix value (8) is implied here */
  1740. s->last_dc[0] = 128;
  1741. s->last_dc[1] = 128;
  1742. s->last_dc[2] = 128;
  1743. s->mb_incr = 1;
  1744. s->last_mv[0][0][0] = 0;
  1745. s->last_mv[0][0][1] = 0;
  1746. /* Get the GOB height based on picture height */
  1747. if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
  1748. if (s->height <= 400)
  1749. s->gob_index = 1;
  1750. else if (s->height <= 800)
  1751. s->gob_index = 2;
  1752. else
  1753. s->gob_index = 4;
  1754. }else if(s->codec_id==CODEC_ID_MPEG4){
  1755. s->gob_index = 1;
  1756. }
  1757. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  1758. ff_mpeg4_init_partitions(s);
  1759. s->resync_mb_x=0;
  1760. s->resync_mb_y=0;
  1761. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  1762. /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
  1763. /* TODO: Put all this stuff in a separate generic function */
  1764. if (s->rtp_mode) {
  1765. if (!mb_y) {
  1766. s->ptr_lastgob = s->pb.buf;
  1767. s->ptr_last_mb_line = s->pb.buf;
  1768. } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
  1769. // MN: we could move the space check from h263 -> here, as its not h263 specific
  1770. last_gob = h263_encode_gob_header(s, mb_y);
  1771. if (last_gob) {
  1772. s->first_slice_line = 1;
  1773. }else{
  1774. /*MN: we reset it here instead at the end of each line cuz mpeg4 can have
  1775. slice lines starting & ending in the middle*/
  1776. s->first_slice_line = 0;
  1777. }
  1778. }
  1779. }
  1780. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  1781. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  1782. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  1783. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  1784. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  1785. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  1786. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1787. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  1788. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1789. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  1790. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  1791. // int d;
  1792. int dmin=10000000;
  1793. s->mb_x = mb_x;
  1794. s->mb_y = mb_y;
  1795. s->block_index[0]+=2;
  1796. s->block_index[1]+=2;
  1797. s->block_index[2]+=2;
  1798. s->block_index[3]+=2;
  1799. s->block_index[4]++;
  1800. s->block_index[5]++;
  1801. /* write gob / video packet header for formats which support it at any MB (MPEG4) */
  1802. if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
  1803. int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
  1804. //the *2 is there so we stay below the requested size
  1805. if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){
  1806. if(s->codec_id==CODEC_ID_MPEG4){
  1807. if(s->data_partitioning && s->pict_type!=B_TYPE){
  1808. ff_mpeg4_merge_partitions(s);
  1809. ff_mpeg4_init_partitions(s);
  1810. }
  1811. ff_mpeg4_encode_video_packet_header(s);
  1812. if(s->flags&CODEC_FLAG_PASS1){
  1813. int bits= get_bit_count(&s->pb);
  1814. s->misc_bits+= bits - s->last_bits;
  1815. s->last_bits= bits;
  1816. }
  1817. ff_mpeg4_clean_buffers(s);
  1818. }
  1819. s->ptr_lastgob = pbBufPtr(&s->pb);
  1820. s->first_slice_line=1;
  1821. s->resync_mb_x=mb_x;
  1822. s->resync_mb_y=mb_y;
  1823. }
  1824. if( (s->resync_mb_x == s->mb_x)
  1825. && s->resync_mb_y+1 == s->mb_y){
  1826. s->first_slice_line=0;
  1827. }
  1828. }
  1829. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  1830. int next_block=0;
  1831. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  1832. copy_context_before_encode(&backup_s, s, -1);
  1833. backup_s.pb= s->pb;
  1834. best_s.data_partitioning= s->data_partitioning;
  1835. if(s->data_partitioning){
  1836. backup_s.pb2= s->pb2;
  1837. backup_s.tex_pb= s->tex_pb;
  1838. }
  1839. if(mb_type&MB_TYPE_INTER){
  1840. s->mv_dir = MV_DIR_FORWARD;
  1841. s->mv_type = MV_TYPE_16X16;
  1842. s->mb_intra= 0;
  1843. s->mv[0][0][0] = s->p_mv_table[xy][0];
  1844. s->mv[0][0][1] = s->p_mv_table[xy][1];
  1845. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  1846. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1847. }
  1848. if(mb_type&MB_TYPE_INTER4V){
  1849. s->mv_dir = MV_DIR_FORWARD;
  1850. s->mv_type = MV_TYPE_8X8;
  1851. s->mb_intra= 0;
  1852. for(i=0; i<4; i++){
  1853. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1854. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1855. }
  1856. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  1857. &dmin, &next_block, 0, 0);
  1858. }
  1859. if(mb_type&MB_TYPE_FORWARD){
  1860. s->mv_dir = MV_DIR_FORWARD;
  1861. s->mv_type = MV_TYPE_16X16;
  1862. s->mb_intra= 0;
  1863. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1864. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1865. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  1866. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1867. }
  1868. if(mb_type&MB_TYPE_BACKWARD){
  1869. s->mv_dir = MV_DIR_BACKWARD;
  1870. s->mv_type = MV_TYPE_16X16;
  1871. s->mb_intra= 0;
  1872. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1873. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1874. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  1875. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  1876. }
  1877. if(mb_type&MB_TYPE_BIDIR){
  1878. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1879. s->mv_type = MV_TYPE_16X16;
  1880. s->mb_intra= 0;
  1881. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1882. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1883. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1884. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1885. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  1886. &dmin, &next_block, 0, 0);
  1887. }
  1888. if(mb_type&MB_TYPE_DIRECT){
  1889. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1890. s->mv_type = MV_TYPE_16X16; //FIXME
  1891. s->mb_intra= 0;
  1892. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1893. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1894. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1895. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1896. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  1897. &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
  1898. }
  1899. if(mb_type&MB_TYPE_INTRA){
  1900. s->mv_dir = MV_DIR_FORWARD;
  1901. s->mv_type = MV_TYPE_16X16;
  1902. s->mb_intra= 1;
  1903. s->mv[0][0][0] = 0;
  1904. s->mv[0][0][1] = 0;
  1905. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  1906. &dmin, &next_block, 0, 0);
  1907. /* force cleaning of ac/dc pred stuff if needed ... */
  1908. if(s->h263_pred || s->h263_aic)
  1909. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  1910. }
  1911. copy_context_after_encode(s, &best_s, -1);
  1912. pb_bits_count= get_bit_count(&s->pb);
  1913. flush_put_bits(&s->pb);
  1914. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  1915. s->pb= backup_s.pb;
  1916. if(s->data_partitioning){
  1917. pb2_bits_count= get_bit_count(&s->pb2);
  1918. flush_put_bits(&s->pb2);
  1919. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  1920. s->pb2= backup_s.pb2;
  1921. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  1922. flush_put_bits(&s->tex_pb);
  1923. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  1924. s->tex_pb= backup_s.tex_pb;
  1925. }
  1926. s->last_bits= get_bit_count(&s->pb);
  1927. } else {
  1928. int motion_x, motion_y;
  1929. s->mv_type=MV_TYPE_16X16;
  1930. // only one MB-Type possible
  1931. switch(mb_type){
  1932. case MB_TYPE_INTRA:
  1933. s->mv_dir = MV_DIR_FORWARD;
  1934. s->mb_intra= 1;
  1935. motion_x= s->mv[0][0][0] = 0;
  1936. motion_y= s->mv[0][0][1] = 0;
  1937. break;
  1938. case MB_TYPE_INTER:
  1939. s->mv_dir = MV_DIR_FORWARD;
  1940. s->mb_intra= 0;
  1941. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  1942. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  1943. break;
  1944. case MB_TYPE_INTER4V:
  1945. s->mv_dir = MV_DIR_FORWARD;
  1946. s->mv_type = MV_TYPE_8X8;
  1947. s->mb_intra= 0;
  1948. for(i=0; i<4; i++){
  1949. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  1950. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  1951. }
  1952. motion_x= motion_y= 0;
  1953. break;
  1954. case MB_TYPE_DIRECT:
  1955. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  1956. s->mb_intra= 0;
  1957. motion_x=s->b_direct_mv_table[xy][0];
  1958. motion_y=s->b_direct_mv_table[xy][1];
  1959. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  1960. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  1961. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  1962. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  1963. break;
  1964. case MB_TYPE_BIDIR:
  1965. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1966. s->mb_intra= 0;
  1967. motion_x=0;
  1968. motion_y=0;
  1969. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1970. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1971. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1972. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1973. break;
  1974. case MB_TYPE_BACKWARD:
  1975. s->mv_dir = MV_DIR_BACKWARD;
  1976. s->mb_intra= 0;
  1977. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1978. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1979. break;
  1980. case MB_TYPE_FORWARD:
  1981. s->mv_dir = MV_DIR_FORWARD;
  1982. s->mb_intra= 0;
  1983. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1984. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1985. // printf(" %d %d ", motion_x, motion_y);
  1986. break;
  1987. default:
  1988. motion_x=motion_y=0; //gcc warning fix
  1989. printf("illegal MB type\n");
  1990. }
  1991. encode_mb(s, motion_x, motion_y);
  1992. }
  1993. /* clean the MV table in IPS frames for direct mode in B frames */
  1994. if(s->mb_intra /* && I,P,S_TYPE */){
  1995. s->p_mv_table[xy][0]=0;
  1996. s->p_mv_table[xy][1]=0;
  1997. }
  1998. MPV_decode_mb(s, s->block);
  1999. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  2000. }
  2001. /* Obtain average GOB size for RTP */
  2002. if (s->rtp_mode) {
  2003. if (!mb_y)
  2004. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  2005. else if (!(mb_y % s->gob_index)) {
  2006. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  2007. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  2008. }
  2009. //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
  2010. // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
  2011. if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
  2012. }
  2013. }
  2014. emms_c();
  2015. if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
  2016. ff_mpeg4_merge_partitions(s);
  2017. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  2018. msmpeg4_encode_ext_header(s);
  2019. if(s->codec_id==CODEC_ID_MPEG4)
  2020. ff_mpeg4_stuffing(&s->pb);
  2021. //if (s->gob_number)
  2022. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  2023. /* Send the last GOB if RTP */
  2024. if (s->rtp_mode) {
  2025. flush_put_bits(&s->pb);
  2026. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  2027. /* Call the RTP callback to send the last GOB */
  2028. if (s->rtp_callback)
  2029. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  2030. s->ptr_lastgob = pbBufPtr(&s->pb);
  2031. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  2032. }
  2033. }
  2034. static int dct_quantize_c(MpegEncContext *s,
  2035. DCTELEM *block, int n,
  2036. int qscale, int *overflow)
  2037. {
  2038. int i, j, level, last_non_zero, q;
  2039. const int *qmat;
  2040. int bias;
  2041. int max=0;
  2042. unsigned int threshold1, threshold2;
  2043. av_fdct (block);
  2044. /* we need this permutation so that we correct the IDCT
  2045. permutation. will be moved into DCT code */
  2046. block_permute(block);
  2047. if (s->mb_intra) {
  2048. if (!s->h263_aic) {
  2049. if (n < 4)
  2050. q = s->y_dc_scale;
  2051. else
  2052. q = s->c_dc_scale;
  2053. q = q << 3;
  2054. } else
  2055. /* For AIC we skip quant/dequant of INTRADC */
  2056. q = 1 << 3;
  2057. /* note: block[0] is assumed to be positive */
  2058. block[0] = (block[0] + (q >> 1)) / q;
  2059. i = 1;
  2060. last_non_zero = 0;
  2061. qmat = s->q_intra_matrix[qscale];
  2062. bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  2063. } else {
  2064. i = 0;
  2065. last_non_zero = -1;
  2066. qmat = s->q_inter_matrix[qscale];
  2067. bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
  2068. }
  2069. threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
  2070. threshold2= threshold1<<1;
  2071. for(;i<64;i++) {
  2072. j = zigzag_direct[i];
  2073. level = block[j];
  2074. level = level * qmat[j];
  2075. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  2076. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  2077. if(((unsigned)(level+threshold1))>threshold2){
  2078. if(level>0){
  2079. level= (bias + level)>>(QMAT_SHIFT - 3);
  2080. block[j]= level;
  2081. }else{
  2082. level= (bias - level)>>(QMAT_SHIFT - 3);
  2083. block[j]= -level;
  2084. }
  2085. max |=level;
  2086. last_non_zero = i;
  2087. }else{
  2088. block[j]=0;
  2089. }
  2090. }
  2091. *overflow= s->max_qcoeff < max; //overflow might have happend
  2092. return last_non_zero;
  2093. }
  2094. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  2095. DCTELEM *block, int n, int qscale)
  2096. {
  2097. int i, level, nCoeffs;
  2098. const UINT16 *quant_matrix;
  2099. if(s->alternate_scan) nCoeffs= 64;
  2100. else nCoeffs= s->block_last_index[n]+1;
  2101. if (s->mb_intra) {
  2102. if (n < 4)
  2103. block[0] = block[0] * s->y_dc_scale;
  2104. else
  2105. block[0] = block[0] * s->c_dc_scale;
  2106. /* XXX: only mpeg1 */
  2107. quant_matrix = s->intra_matrix;
  2108. for(i=1;i<nCoeffs;i++) {
  2109. int j= zigzag_direct[i];
  2110. level = block[j];
  2111. if (level) {
  2112. if (level < 0) {
  2113. level = -level;
  2114. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2115. level = (level - 1) | 1;
  2116. level = -level;
  2117. } else {
  2118. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2119. level = (level - 1) | 1;
  2120. }
  2121. #ifdef PARANOID
  2122. if (level < -2048 || level > 2047)
  2123. fprintf(stderr, "unquant error %d %d\n", i, level);
  2124. #endif
  2125. block[j] = level;
  2126. }
  2127. }
  2128. } else {
  2129. i = 0;
  2130. quant_matrix = s->inter_matrix;
  2131. for(;i<nCoeffs;i++) {
  2132. int j= zigzag_direct[i];
  2133. level = block[j];
  2134. if (level) {
  2135. if (level < 0) {
  2136. level = -level;
  2137. level = (((level << 1) + 1) * qscale *
  2138. ((int) (quant_matrix[j]))) >> 4;
  2139. level = (level - 1) | 1;
  2140. level = -level;
  2141. } else {
  2142. level = (((level << 1) + 1) * qscale *
  2143. ((int) (quant_matrix[j]))) >> 4;
  2144. level = (level - 1) | 1;
  2145. }
  2146. #ifdef PARANOID
  2147. if (level < -2048 || level > 2047)
  2148. fprintf(stderr, "unquant error %d %d\n", i, level);
  2149. #endif
  2150. block[j] = level;
  2151. }
  2152. }
  2153. }
  2154. }
  2155. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  2156. DCTELEM *block, int n, int qscale)
  2157. {
  2158. int i, level, nCoeffs;
  2159. const UINT16 *quant_matrix;
  2160. if(s->alternate_scan) nCoeffs= 64;
  2161. else nCoeffs= s->block_last_index[n]+1;
  2162. if (s->mb_intra) {
  2163. if (n < 4)
  2164. block[0] = block[0] * s->y_dc_scale;
  2165. else
  2166. block[0] = block[0] * s->c_dc_scale;
  2167. quant_matrix = s->intra_matrix;
  2168. for(i=1;i<nCoeffs;i++) {
  2169. int j= zigzag_direct[i];
  2170. level = block[j];
  2171. if (level) {
  2172. if (level < 0) {
  2173. level = -level;
  2174. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2175. level = -level;
  2176. } else {
  2177. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2178. }
  2179. #ifdef PARANOID
  2180. if (level < -2048 || level > 2047)
  2181. fprintf(stderr, "unquant error %d %d\n", i, level);
  2182. #endif
  2183. block[j] = level;
  2184. }
  2185. }
  2186. } else {
  2187. int sum=-1;
  2188. i = 0;
  2189. quant_matrix = s->inter_matrix;
  2190. for(;i<nCoeffs;i++) {
  2191. int j= zigzag_direct[i];
  2192. level = block[j];
  2193. if (level) {
  2194. if (level < 0) {
  2195. level = -level;
  2196. level = (((level << 1) + 1) * qscale *
  2197. ((int) (quant_matrix[j]))) >> 4;
  2198. level = -level;
  2199. } else {
  2200. level = (((level << 1) + 1) * qscale *
  2201. ((int) (quant_matrix[j]))) >> 4;
  2202. }
  2203. #ifdef PARANOID
  2204. if (level < -2048 || level > 2047)
  2205. fprintf(stderr, "unquant error %d %d\n", i, level);
  2206. #endif
  2207. block[j] = level;
  2208. sum+=level;
  2209. }
  2210. }
  2211. block[63]^=sum&1;
  2212. }
  2213. }
  2214. static void dct_unquantize_h263_c(MpegEncContext *s,
  2215. DCTELEM *block, int n, int qscale)
  2216. {
  2217. int i, level, qmul, qadd;
  2218. int nCoeffs;
  2219. if (s->mb_intra) {
  2220. if (!s->h263_aic) {
  2221. if (n < 4)
  2222. block[0] = block[0] * s->y_dc_scale;
  2223. else
  2224. block[0] = block[0] * s->c_dc_scale;
  2225. }
  2226. i = 1;
  2227. nCoeffs= 64; //does not allways use zigzag table
  2228. } else {
  2229. i = 0;
  2230. nCoeffs= zigzag_end[ s->block_last_index[n] ];
  2231. }
  2232. qmul = s->qscale << 1;
  2233. if (s->h263_aic && s->mb_intra)
  2234. qadd = 0;
  2235. else
  2236. qadd = (s->qscale - 1) | 1;
  2237. for(;i<nCoeffs;i++) {
  2238. level = block[i];
  2239. if (level) {
  2240. if (level < 0) {
  2241. level = level * qmul - qadd;
  2242. } else {
  2243. level = level * qmul + qadd;
  2244. }
  2245. #ifdef PARANOID
  2246. if (level < -2048 || level > 2047)
  2247. fprintf(stderr, "unquant error %d %d\n", i, level);
  2248. #endif
  2249. block[i] = level;
  2250. }
  2251. }
  2252. }
  2253. static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
  2254. {
  2255. int dc, dcb, dcr, y, i;
  2256. for(i=0; i<4; i++){
  2257. dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
  2258. for(y=0; y<8; y++){
  2259. int x;
  2260. for(x=0; x<8; x++){
  2261. dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
  2262. }
  2263. }
  2264. }
  2265. dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2266. dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  2267. for(y=0; y<8; y++){
  2268. int x;
  2269. for(x=0; x<8; x++){
  2270. dest_cb[x + y*(s->uvlinesize)]= dcb/8;
  2271. dest_cr[x + y*(s->uvlinesize)]= dcr/8;
  2272. }
  2273. }
  2274. }
  2275. /**
  2276. * will conceal past errors, and allso drop b frames if needed
  2277. *
  2278. */
  2279. void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
  2280. {
  2281. int mb_x= s->mb_x;
  2282. int mb_y= s->mb_y;
  2283. int mb_dist=0;
  2284. int i, intra_count=0, inter_count=0;
  2285. int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
  2286. int inter_conceal= s->msmpeg4_version ? 50 : 50;
  2287. // for last block
  2288. if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
  2289. if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
  2290. if(s->decoding_error==0 && unknown_pos){
  2291. if(s->data_partitioning && s->pict_type!=B_TYPE)
  2292. s->decoding_error= DECODING_AC_LOST;
  2293. else
  2294. s->decoding_error= DECODING_DESYNC;
  2295. }
  2296. if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
  2297. for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
  2298. if(s->mbintra_table[i]) intra_count++;
  2299. else inter_count++;
  2300. }
  2301. if(s->decoding_error==DECODING_AC_LOST){
  2302. intra_conceal*=2;
  2303. inter_conceal*=2;
  2304. }else if(s->decoding_error==DECODING_ACDC_LOST){
  2305. intra_conceal*=2;
  2306. inter_conceal*=2;
  2307. }
  2308. if(unknown_pos && (intra_count<inter_count)){
  2309. intra_conceal= inter_conceal= s->mb_num;
  2310. // printf("%d %d\n",intra_count, inter_count);
  2311. }
  2312. fprintf(stderr, "concealing errors\n");
  2313. /* for all MBs from the current one back until the last resync marker */
  2314. for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
  2315. for(; mb_x>=0; mb_x--){
  2316. uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2317. uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2318. uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2319. int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
  2320. int mb_y_backup= s->mb_y;
  2321. s->mb_x=mb_x;
  2322. s->mb_y=mb_y;
  2323. if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
  2324. if(s->decoding_error==DECODING_AC_LOST){
  2325. remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  2326. // printf("remove ac to %d %d\n", mb_x, mb_y);
  2327. }else{
  2328. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2329. s->last_picture, 0, 0, put_pixels_tab,
  2330. 0/*mx*/, 0/*my*/, 16);
  2331. }
  2332. }
  2333. else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
  2334. int mx=0;
  2335. int my=0;
  2336. if(s->decoding_error!=DECODING_DESYNC){
  2337. int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
  2338. mx= s->motion_val[ xy ][0];
  2339. my= s->motion_val[ xy ][1];
  2340. }
  2341. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2342. s->last_picture, 0, 0, put_pixels_tab,
  2343. mx, my, 16);
  2344. }
  2345. s->mb_x= mb_x_backup;
  2346. s->mb_y= mb_y_backup;
  2347. if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
  2348. if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
  2349. }
  2350. mb_x=s->mb_width-1;
  2351. }
  2352. }
  2353. AVCodec mpeg1video_encoder = {
  2354. "mpeg1video",
  2355. CODEC_TYPE_VIDEO,
  2356. CODEC_ID_MPEG1VIDEO,
  2357. sizeof(MpegEncContext),
  2358. MPV_encode_init,
  2359. MPV_encode_picture,
  2360. MPV_encode_end,
  2361. };
  2362. AVCodec h263_encoder = {
  2363. "h263",
  2364. CODEC_TYPE_VIDEO,
  2365. CODEC_ID_H263,
  2366. sizeof(MpegEncContext),
  2367. MPV_encode_init,
  2368. MPV_encode_picture,
  2369. MPV_encode_end,
  2370. };
  2371. AVCodec h263p_encoder = {
  2372. "h263p",
  2373. CODEC_TYPE_VIDEO,
  2374. CODEC_ID_H263P,
  2375. sizeof(MpegEncContext),
  2376. MPV_encode_init,
  2377. MPV_encode_picture,
  2378. MPV_encode_end,
  2379. };
  2380. AVCodec rv10_encoder = {
  2381. "rv10",
  2382. CODEC_TYPE_VIDEO,
  2383. CODEC_ID_RV10,
  2384. sizeof(MpegEncContext),
  2385. MPV_encode_init,
  2386. MPV_encode_picture,
  2387. MPV_encode_end,
  2388. };
  2389. AVCodec mjpeg_encoder = {
  2390. "mjpeg",
  2391. CODEC_TYPE_VIDEO,
  2392. CODEC_ID_MJPEG,
  2393. sizeof(MpegEncContext),
  2394. MPV_encode_init,
  2395. MPV_encode_picture,
  2396. MPV_encode_end,
  2397. };
  2398. AVCodec mpeg4_encoder = {
  2399. "mpeg4",
  2400. CODEC_TYPE_VIDEO,
  2401. CODEC_ID_MPEG4,
  2402. sizeof(MpegEncContext),
  2403. MPV_encode_init,
  2404. MPV_encode_picture,
  2405. MPV_encode_end,
  2406. };
  2407. AVCodec msmpeg4v1_encoder = {
  2408. "msmpeg4v1",
  2409. CODEC_TYPE_VIDEO,
  2410. CODEC_ID_MSMPEG4V1,
  2411. sizeof(MpegEncContext),
  2412. MPV_encode_init,
  2413. MPV_encode_picture,
  2414. MPV_encode_end,
  2415. };
  2416. AVCodec msmpeg4v2_encoder = {
  2417. "msmpeg4v2",
  2418. CODEC_TYPE_VIDEO,
  2419. CODEC_ID_MSMPEG4V2,
  2420. sizeof(MpegEncContext),
  2421. MPV_encode_init,
  2422. MPV_encode_picture,
  2423. MPV_encode_end,
  2424. };
  2425. AVCodec msmpeg4v3_encoder = {
  2426. "msmpeg4",
  2427. CODEC_TYPE_VIDEO,
  2428. CODEC_ID_MSMPEG4V3,
  2429. sizeof(MpegEncContext),
  2430. MPV_encode_init,
  2431. MPV_encode_picture,
  2432. MPV_encode_end,
  2433. };
  2434. AVCodec wmv1_encoder = {
  2435. "wmv1",
  2436. CODEC_TYPE_VIDEO,
  2437. CODEC_ID_WMV1,
  2438. sizeof(MpegEncContext),
  2439. MPV_encode_init,
  2440. MPV_encode_picture,
  2441. MPV_encode_end,
  2442. };
  2443. AVCodec wmv2_encoder = {
  2444. "wmv2",
  2445. CODEC_TYPE_VIDEO,
  2446. CODEC_ID_WMV2,
  2447. sizeof(MpegEncContext),
  2448. MPV_encode_init,
  2449. MPV_encode_picture,
  2450. MPV_encode_end,
  2451. };