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.

3350 lines
114KB

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