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.

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