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.

3364 lines
115KB

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