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.

3307 lines
113KB

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