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.

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