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.

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