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.

3361 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. if(avctx->flags&CODEC_FLAG_DR1){
  709. if(avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type) < 0){
  710. fprintf(stderr, "get_buffer() failed\n");
  711. return -1;
  712. }
  713. s->linesize = avctx->dr_stride;
  714. s->uvlinesize= avctx->dr_uvstride;
  715. s->ip_buffer_count= avctx->dr_ip_buffer_count;
  716. }
  717. avctx->dr_ip_buffer_count= s->ip_buffer_count;
  718. if (s->pict_type == B_TYPE) {
  719. for(i=0;i<3;i++) {
  720. if(avctx->flags&CODEC_FLAG_DR1)
  721. s->aux_picture[i]= avctx->dr_buffer[i];
  722. //FIXME the following should never be needed, the decoder should drop b frames if no reference is available
  723. if(s->next_picture[i]==NULL)
  724. s->next_picture[i]= s->aux_picture[i];
  725. if(s->last_picture[i]==NULL)
  726. s->last_picture[i]= s->next_picture[i];
  727. s->current_picture[i] = s->aux_picture[i];
  728. }
  729. s->avctx->display_qscale_table=
  730. s->avctx->current_qscale_table=
  731. s->qscale_table= s->aux_qscale_table;
  732. } else {
  733. for(i=0;i<3;i++) {
  734. /* swap next and last */
  735. if(avctx->flags&CODEC_FLAG_DR1)
  736. tmp= avctx->dr_buffer[i];
  737. else
  738. tmp = s->last_picture[i];
  739. s->last_picture[i] = s->next_picture[i];
  740. s->next_picture[i] = tmp;
  741. s->current_picture[i] = tmp;
  742. if(s->last_picture[i]==NULL)
  743. s->last_picture[i]= s->next_picture[i];
  744. s->last_dr_opaque= s->next_dr_opaque;
  745. s->next_dr_opaque= avctx->dr_opaque_frame;
  746. if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)
  747. avctx->dr_opaque_frame= s->last_dr_opaque;
  748. else
  749. avctx->dr_opaque_frame= s->next_dr_opaque;
  750. }
  751. s->avctx->current_qscale_table= s->qscale_table = s->last_qscale_table;
  752. s->avctx->display_qscale_table= s->last_qscale_table = s->next_qscale_table;
  753. s->next_qscale_table= s->qscale_table;
  754. }
  755. /* set dequantizer, we cant do it during init as it might change for mpeg4
  756. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  757. if(s->out_format == FMT_H263){
  758. if(s->mpeg_quant)
  759. s->dct_unquantize = s->dct_unquantize_mpeg2;
  760. else
  761. s->dct_unquantize = s->dct_unquantize_h263;
  762. }else
  763. s->dct_unquantize = s->dct_unquantize_mpeg1;
  764. return 0;
  765. }
  766. /* generic function for encode/decode called after a frame has been coded/decoded */
  767. void MPV_frame_end(MpegEncContext *s)
  768. {
  769. s->avctx->key_frame = (s->pict_type == I_TYPE);
  770. s->avctx->pict_type = s->pict_type;
  771. /* draw edge for correct motion prediction if outside */
  772. if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  773. draw_edges(s->current_picture[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  774. draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  775. draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  776. }
  777. emms_c();
  778. s->last_pict_type = s->pict_type;
  779. if(s->pict_type!=B_TYPE){
  780. s->last_non_b_pict_type= s->pict_type;
  781. s->num_available_buffers++;
  782. if(s->num_available_buffers>2) s->num_available_buffers= 2;
  783. }
  784. }
  785. /* reorder input for encoding */
  786. void reorder_input(MpegEncContext *s, AVPicture *pict)
  787. {
  788. int i, j, index;
  789. if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
  790. // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
  791. for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
  792. s->coded_order[j]= s->coded_order[j+1];
  793. }
  794. s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
  795. s->coded_order[j].pict_type=0;
  796. switch(s->input_pict_type){
  797. default:
  798. case I_TYPE:
  799. case S_TYPE:
  800. case P_TYPE:
  801. index= s->max_b_frames - s->b_frames_since_non_b;
  802. s->b_frames_since_non_b=0;
  803. break;
  804. case B_TYPE:
  805. index= s->max_b_frames + 1;
  806. s->b_frames_since_non_b++;
  807. break;
  808. }
  809. //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
  810. if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
  811. && pict->linesize[0] == s->linesize
  812. && pict->linesize[1] == s->uvlinesize
  813. && pict->linesize[2] == s->uvlinesize){
  814. //printf("ptr\n");
  815. for(i=0; i<3; i++){
  816. s->coded_order[index].picture[i]= pict->data[i];
  817. }
  818. }else{
  819. //printf("copy\n");
  820. for(i=0; i<3; i++){
  821. uint8_t *src = pict->data[i];
  822. uint8_t *dest;
  823. int src_wrap = pict->linesize[i];
  824. int dest_wrap = s->linesize;
  825. int w = s->width;
  826. int h = s->height;
  827. if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
  828. else dest= s->picture_buffer[s->picture_buffer_index][i];
  829. if (i >= 1) {
  830. dest_wrap >>= 1;
  831. w >>= 1;
  832. h >>= 1;
  833. }
  834. s->coded_order[index].picture[i]= dest;
  835. for(j=0;j<h;j++) {
  836. memcpy(dest, src, w);
  837. dest += dest_wrap;
  838. src += src_wrap;
  839. }
  840. }
  841. if(index!=0){
  842. s->picture_buffer_index++;
  843. if(s->picture_buffer_index >= REORDER_BUFFER_SIZE) s->picture_buffer_index=0;
  844. }
  845. }
  846. s->coded_order[index].pict_type = s->input_pict_type;
  847. s->coded_order[index].qscale = s->input_qscale;
  848. s->coded_order[index].force_type= s->force_input_type;
  849. s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
  850. s->coded_order[index].picture_number= s->input_picture_number;
  851. for(i=0; i<3; i++){
  852. s->new_picture[i]= s->coded_order[0].picture[i];
  853. }
  854. }
  855. int MPV_encode_picture(AVCodecContext *avctx,
  856. unsigned char *buf, int buf_size, void *data)
  857. {
  858. MpegEncContext *s = avctx->priv_data;
  859. AVPicture *pict = data;
  860. s->input_qscale = avctx->quality;
  861. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  862. if(avctx->force_type){
  863. s->input_pict_type=
  864. s->force_input_type= avctx->force_type;
  865. }else if(s->flags&CODEC_FLAG_PASS2){
  866. s->input_pict_type=
  867. s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
  868. }else{
  869. s->force_input_type=0;
  870. if (!s->intra_only) {
  871. /* first picture of GOP is intra */
  872. if (s->input_picture_in_gop_number % s->gop_size==0){
  873. s->input_pict_type = I_TYPE;
  874. }else if(s->max_b_frames==0){
  875. s->input_pict_type = P_TYPE;
  876. }else{
  877. if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
  878. s->input_pict_type = B_TYPE;
  879. else
  880. s->input_pict_type = P_TYPE;
  881. }
  882. } else {
  883. s->input_pict_type = I_TYPE;
  884. }
  885. }
  886. if(s->input_pict_type==I_TYPE)
  887. s->input_picture_in_gop_number=0;
  888. reorder_input(s, pict);
  889. /* output? */
  890. if(s->coded_order[0].picture[0]){
  891. s->pict_type= s->coded_order[0].pict_type;
  892. if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  893. s->qscale= s->coded_order[0].qscale;
  894. s->force_type= s->coded_order[0].force_type;
  895. s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
  896. s->picture_number= s->coded_order[0].picture_number;
  897. MPV_frame_start(s, avctx);
  898. encode_picture(s, s->picture_number);
  899. avctx->real_pict_num = s->picture_number;
  900. avctx->header_bits = s->header_bits;
  901. avctx->mv_bits = s->mv_bits;
  902. avctx->misc_bits = s->misc_bits;
  903. avctx->i_tex_bits = s->i_tex_bits;
  904. avctx->p_tex_bits = s->p_tex_bits;
  905. avctx->i_count = s->i_count;
  906. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  907. avctx->skip_count = s->skip_count;
  908. MPV_frame_end(s);
  909. if (s->out_format == FMT_MJPEG)
  910. mjpeg_picture_trailer(s);
  911. if(!s->fixed_qscale)
  912. avctx->quality = s->qscale;
  913. if(s->flags&CODEC_FLAG_PASS1)
  914. ff_write_pass1_stats(s);
  915. }
  916. s->input_picture_number++;
  917. s->input_picture_in_gop_number++;
  918. flush_put_bits(&s->pb);
  919. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  920. s->total_bits += s->frame_bits;
  921. avctx->frame_bits = s->frame_bits;
  922. //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
  923. //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);
  924. #if 0 //dump some stats to stats.txt for testing/debuging
  925. if(s->max_b_frames==0)
  926. {
  927. static FILE *f=NULL;
  928. if(!f) f= fopen("stats.txt", "wb");
  929. get_psnr(pict->data, s->current_picture,
  930. pict->linesize, s->linesize, avctx);
  931. fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y);
  932. }
  933. #endif
  934. if (avctx->get_psnr) {
  935. /* At this point pict->data should have the original frame */
  936. /* an s->current_picture should have the coded/decoded frame */
  937. get_psnr(pict->data, s->current_picture,
  938. pict->linesize, s->linesize, avctx);
  939. // printf("%f\n", avctx->psnr_y);
  940. }
  941. return pbBufPtr(&s->pb) - s->pb.buf;
  942. }
  943. static inline void gmc1_motion(MpegEncContext *s,
  944. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  945. int dest_offset,
  946. UINT8 **ref_picture, int src_offset)
  947. {
  948. UINT8 *ptr;
  949. int offset, src_x, src_y, linesize, uvlinesize;
  950. int motion_x, motion_y;
  951. int emu=0;
  952. motion_x= s->sprite_offset[0][0];
  953. motion_y= s->sprite_offset[0][1];
  954. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  955. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  956. motion_x<<=(3-s->sprite_warping_accuracy);
  957. motion_y<<=(3-s->sprite_warping_accuracy);
  958. src_x = clip(src_x, -16, s->width);
  959. if (src_x == s->width)
  960. motion_x =0;
  961. src_y = clip(src_y, -16, s->height);
  962. if (src_y == s->height)
  963. motion_y =0;
  964. linesize = s->linesize;
  965. uvlinesize = s->uvlinesize;
  966. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  967. dest_y+=dest_offset;
  968. if(s->flags&CODEC_FLAG_EMU_EDGE){
  969. if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->h_edge_pos
  970. || src_y + (motion_y&15) + 16 > s->v_edge_pos){
  971. emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  972. ptr= s->edge_emu_buffer;
  973. emu=1;
  974. }
  975. }
  976. if((motion_x|motion_y)&7){
  977. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  978. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  979. }else{
  980. int dxy;
  981. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  982. if (s->no_rounding){
  983. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  984. }else{
  985. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  986. }
  987. }
  988. if(s->flags&CODEC_FLAG_GRAY) return;
  989. motion_x= s->sprite_offset[1][0];
  990. motion_y= s->sprite_offset[1][1];
  991. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  992. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  993. motion_x<<=(3-s->sprite_warping_accuracy);
  994. motion_y<<=(3-s->sprite_warping_accuracy);
  995. src_x = clip(src_x, -8, s->width>>1);
  996. if (src_x == s->width>>1)
  997. motion_x =0;
  998. src_y = clip(src_y, -8, s->height>>1);
  999. if (src_y == s->height>>1)
  1000. motion_y =0;
  1001. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  1002. ptr = ref_picture[1] + offset;
  1003. if(emu){
  1004. emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1005. ptr= s->edge_emu_buffer;
  1006. }
  1007. s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1008. ptr = ref_picture[2] + offset;
  1009. if(emu){
  1010. emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1011. ptr= s->edge_emu_buffer;
  1012. }
  1013. s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1014. return;
  1015. }
  1016. static inline void gmc_motion(MpegEncContext *s,
  1017. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1018. int dest_offset,
  1019. UINT8 **ref_picture, int src_offset)
  1020. {
  1021. UINT8 *ptr;
  1022. int linesize, uvlinesize;
  1023. const int a= s->sprite_warping_accuracy;
  1024. int ox, oy;
  1025. linesize = s->linesize;
  1026. uvlinesize = s->uvlinesize;
  1027. ptr = ref_picture[0] + src_offset;
  1028. dest_y+=dest_offset;
  1029. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  1030. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  1031. s->dsp.gmc(dest_y, ptr, linesize, 16,
  1032. ox,
  1033. oy,
  1034. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1035. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1036. a+1, (1<<(2*a+1)) - s->no_rounding,
  1037. s->h_edge_pos, s->v_edge_pos);
  1038. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  1039. ox + s->sprite_delta[0][0]*8,
  1040. oy + s->sprite_delta[1][0]*8,
  1041. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1042. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1043. a+1, (1<<(2*a+1)) - s->no_rounding,
  1044. s->h_edge_pos, s->v_edge_pos);
  1045. if(s->flags&CODEC_FLAG_GRAY) return;
  1046. dest_cb+=dest_offset>>1;
  1047. dest_cr+=dest_offset>>1;
  1048. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  1049. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  1050. ptr = ref_picture[1] + (src_offset>>1);
  1051. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  1052. ox,
  1053. oy,
  1054. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1055. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1056. a+1, (1<<(2*a+1)) - s->no_rounding,
  1057. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1058. ptr = ref_picture[2] + (src_offset>>1);
  1059. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  1060. ox,
  1061. oy,
  1062. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1063. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1064. a+1, (1<<(2*a+1)) - s->no_rounding,
  1065. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1066. }
  1067. static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h,
  1068. int src_x, int src_y, int w, int h){
  1069. int x, y;
  1070. int start_y, start_x, end_y, end_x;
  1071. UINT8 *buf= s->edge_emu_buffer;
  1072. if(src_y>= h){
  1073. src+= (h-1-src_y)*linesize;
  1074. src_y=h-1;
  1075. }else if(src_y<=-block_h){
  1076. src+= (1-block_h-src_y)*linesize;
  1077. src_y=1-block_h;
  1078. }
  1079. if(src_x>= w){
  1080. src+= (w-1-src_x);
  1081. src_x=w-1;
  1082. }else if(src_x<=-block_w){
  1083. src+= (1-block_w-src_x);
  1084. src_x=1-block_w;
  1085. }
  1086. start_y= FFMAX(0, -src_y);
  1087. start_x= FFMAX(0, -src_x);
  1088. end_y= FFMIN(block_h, h-src_y);
  1089. end_x= FFMIN(block_w, w-src_x);
  1090. // copy existing part
  1091. for(y=start_y; y<end_y; y++){
  1092. for(x=start_x; x<end_x; x++){
  1093. buf[x + y*linesize]= src[x + y*linesize];
  1094. }
  1095. }
  1096. //top
  1097. for(y=0; y<start_y; y++){
  1098. for(x=start_x; x<end_x; x++){
  1099. buf[x + y*linesize]= buf[x + start_y*linesize];
  1100. }
  1101. }
  1102. //bottom
  1103. for(y=end_y; y<block_h; y++){
  1104. for(x=start_x; x<end_x; x++){
  1105. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1106. }
  1107. }
  1108. for(y=0; y<block_h; y++){
  1109. //left
  1110. for(x=0; x<start_x; x++){
  1111. buf[x + y*linesize]= buf[start_x + y*linesize];
  1112. }
  1113. //right
  1114. for(x=end_x; x<block_w; x++){
  1115. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1116. }
  1117. }
  1118. }
  1119. /* apply one mpeg motion vector to the three components */
  1120. static inline void mpeg_motion(MpegEncContext *s,
  1121. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1122. int dest_offset,
  1123. UINT8 **ref_picture, int src_offset,
  1124. int field_based, op_pixels_func (*pix_op)[4],
  1125. int motion_x, int motion_y, int h)
  1126. {
  1127. UINT8 *ptr;
  1128. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1129. int emu=0;
  1130. #if 0
  1131. if(s->quarter_sample)
  1132. {
  1133. motion_x>>=1;
  1134. motion_y>>=1;
  1135. }
  1136. #endif
  1137. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1138. src_x = s->mb_x * 16 + (motion_x >> 1);
  1139. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  1140. /* WARNING: do no forget half pels */
  1141. height = s->height >> field_based;
  1142. v_edge_pos = s->v_edge_pos >> field_based;
  1143. src_x = clip(src_x, -16, s->width);
  1144. if (src_x == s->width)
  1145. dxy &= ~1;
  1146. src_y = clip(src_y, -16, height);
  1147. if (src_y == height)
  1148. dxy &= ~2;
  1149. linesize = s->linesize << field_based;
  1150. uvlinesize = s->uvlinesize << field_based;
  1151. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  1152. dest_y += dest_offset;
  1153. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1154. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
  1155. || src_y + (motion_y&1) + h > v_edge_pos){
  1156. emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
  1157. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1158. ptr= s->edge_emu_buffer + src_offset;
  1159. emu=1;
  1160. }
  1161. }
  1162. pix_op[0][dxy](dest_y, ptr, linesize, h);
  1163. if(s->flags&CODEC_FLAG_GRAY) return;
  1164. if (s->out_format == FMT_H263) {
  1165. dxy = 0;
  1166. if ((motion_x & 3) != 0)
  1167. dxy |= 1;
  1168. if ((motion_y & 3) != 0)
  1169. dxy |= 2;
  1170. mx = motion_x >> 2;
  1171. my = motion_y >> 2;
  1172. } else {
  1173. mx = motion_x / 2;
  1174. my = motion_y / 2;
  1175. dxy = ((my & 1) << 1) | (mx & 1);
  1176. mx >>= 1;
  1177. my >>= 1;
  1178. }
  1179. src_x = s->mb_x * 8 + mx;
  1180. src_y = s->mb_y * (8 >> field_based) + my;
  1181. src_x = clip(src_x, -8, s->width >> 1);
  1182. if (src_x == (s->width >> 1))
  1183. dxy &= ~1;
  1184. src_y = clip(src_y, -8, height >> 1);
  1185. if (src_y == (height >> 1))
  1186. dxy &= ~2;
  1187. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1188. ptr = ref_picture[1] + offset;
  1189. if(emu){
  1190. emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1191. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1192. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1193. }
  1194. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1195. ptr = ref_picture[2] + offset;
  1196. if(emu){
  1197. emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1198. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1199. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1200. }
  1201. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1202. }
  1203. static inline void qpel_motion(MpegEncContext *s,
  1204. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1205. int dest_offset,
  1206. UINT8 **ref_picture, int src_offset,
  1207. int field_based, op_pixels_func (*pix_op)[4],
  1208. qpel_mc_func (*qpix_op)[16],
  1209. int motion_x, int motion_y, int h)
  1210. {
  1211. UINT8 *ptr;
  1212. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1213. int emu=0;
  1214. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1215. src_x = s->mb_x * 16 + (motion_x >> 2);
  1216. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  1217. height = s->height >> field_based;
  1218. v_edge_pos = s->v_edge_pos >> field_based;
  1219. src_x = clip(src_x, -16, s->width);
  1220. if (src_x == s->width)
  1221. dxy &= ~3;
  1222. src_y = clip(src_y, -16, height);
  1223. if (src_y == height)
  1224. dxy &= ~12;
  1225. linesize = s->linesize << field_based;
  1226. uvlinesize = s->uvlinesize << field_based;
  1227. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1228. dest_y += dest_offset;
  1229. //printf("%d %d %d\n", src_x, src_y, dxy);
  1230. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1231. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
  1232. || src_y + (motion_y&3) + h > v_edge_pos){
  1233. emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
  1234. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1235. ptr= s->edge_emu_buffer + src_offset;
  1236. emu=1;
  1237. }
  1238. }
  1239. if(!field_based)
  1240. qpix_op[0][dxy](dest_y, ptr, linesize);
  1241. else{
  1242. //damn interlaced mode
  1243. //FIXME boundary mirroring is not exactly correct here
  1244. qpix_op[1][dxy](dest_y , ptr , linesize);
  1245. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  1246. }
  1247. if(s->flags&CODEC_FLAG_GRAY) return;
  1248. if(field_based){
  1249. mx= motion_x/2;
  1250. my= motion_y>>1;
  1251. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  1252. mx= (motion_x>>1)|(motion_x&1);
  1253. my= (motion_y>>1)|(motion_y&1);
  1254. }else{
  1255. mx= motion_x/2;
  1256. my= motion_y/2;
  1257. }
  1258. mx= (mx>>1)|(mx&1);
  1259. my= (my>>1)|(my&1);
  1260. dxy= (mx&1) | ((my&1)<<1);
  1261. mx>>=1;
  1262. my>>=1;
  1263. src_x = s->mb_x * 8 + mx;
  1264. src_y = s->mb_y * (8 >> field_based) + my;
  1265. src_x = clip(src_x, -8, s->width >> 1);
  1266. if (src_x == (s->width >> 1))
  1267. dxy &= ~1;
  1268. src_y = clip(src_y, -8, height >> 1);
  1269. if (src_y == (height >> 1))
  1270. dxy &= ~2;
  1271. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1272. ptr = ref_picture[1] + offset;
  1273. if(emu){
  1274. emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1275. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1276. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1277. }
  1278. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1279. ptr = ref_picture[2] + offset;
  1280. if(emu){
  1281. emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1282. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1283. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1284. }
  1285. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1286. }
  1287. static inline void MPV_motion(MpegEncContext *s,
  1288. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1289. int dir, UINT8 **ref_picture,
  1290. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  1291. {
  1292. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  1293. int mb_x, mb_y, i;
  1294. UINT8 *ptr, *dest;
  1295. int emu=0;
  1296. mb_x = s->mb_x;
  1297. mb_y = s->mb_y;
  1298. switch(s->mv_type) {
  1299. case MV_TYPE_16X16:
  1300. if(s->mcsel){
  1301. if(s->real_sprite_warping_points==1){
  1302. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  1303. ref_picture, 0);
  1304. }else{
  1305. gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
  1306. ref_picture, 0);
  1307. }
  1308. }else if(s->quarter_sample){
  1309. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1310. ref_picture, 0,
  1311. 0, pix_op, qpix_op,
  1312. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1313. }else{
  1314. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1315. ref_picture, 0,
  1316. 0, pix_op,
  1317. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1318. }
  1319. break;
  1320. case MV_TYPE_8X8:
  1321. mx = 0;
  1322. my = 0;
  1323. if(s->quarter_sample){
  1324. for(i=0;i<4;i++) {
  1325. motion_x = s->mv[dir][i][0];
  1326. motion_y = s->mv[dir][i][1];
  1327. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1328. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  1329. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  1330. /* WARNING: do no forget half pels */
  1331. src_x = clip(src_x, -16, s->width);
  1332. if (src_x == s->width)
  1333. dxy &= ~3;
  1334. src_y = clip(src_y, -16, s->height);
  1335. if (src_y == s->height)
  1336. dxy &= ~12;
  1337. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1338. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1339. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
  1340. || src_y + (motion_y&3) + 8 > s->v_edge_pos){
  1341. emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1342. ptr= s->edge_emu_buffer;
  1343. }
  1344. }
  1345. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1346. qpix_op[1][dxy](dest, ptr, s->linesize);
  1347. mx += s->mv[dir][i][0]/2;
  1348. my += s->mv[dir][i][1]/2;
  1349. }
  1350. }else{
  1351. for(i=0;i<4;i++) {
  1352. motion_x = s->mv[dir][i][0];
  1353. motion_y = s->mv[dir][i][1];
  1354. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1355. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1356. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1357. /* WARNING: do no forget half pels */
  1358. src_x = clip(src_x, -16, s->width);
  1359. if (src_x == s->width)
  1360. dxy &= ~1;
  1361. src_y = clip(src_y, -16, s->height);
  1362. if (src_y == s->height)
  1363. dxy &= ~2;
  1364. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1365. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1366. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
  1367. || src_y + (motion_y&1) + 8 > s->v_edge_pos){
  1368. emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1369. ptr= s->edge_emu_buffer;
  1370. }
  1371. }
  1372. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1373. pix_op[1][dxy](dest, ptr, s->linesize, 8);
  1374. mx += s->mv[dir][i][0];
  1375. my += s->mv[dir][i][1];
  1376. }
  1377. }
  1378. if(s->flags&CODEC_FLAG_GRAY) break;
  1379. /* In case of 8X8, we construct a single chroma motion vector
  1380. with a special rounding */
  1381. for(i=0;i<4;i++) {
  1382. }
  1383. if (mx >= 0)
  1384. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1385. else {
  1386. mx = -mx;
  1387. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  1388. }
  1389. if (my >= 0)
  1390. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1391. else {
  1392. my = -my;
  1393. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  1394. }
  1395. dxy = ((my & 1) << 1) | (mx & 1);
  1396. mx >>= 1;
  1397. my >>= 1;
  1398. src_x = mb_x * 8 + mx;
  1399. src_y = mb_y * 8 + my;
  1400. src_x = clip(src_x, -8, s->width/2);
  1401. if (src_x == s->width/2)
  1402. dxy &= ~1;
  1403. src_y = clip(src_y, -8, s->height/2);
  1404. if (src_y == s->height/2)
  1405. dxy &= ~2;
  1406. offset = (src_y * (s->uvlinesize)) + src_x;
  1407. ptr = ref_picture[1] + offset;
  1408. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1409. if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
  1410. || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
  1411. emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1412. ptr= s->edge_emu_buffer;
  1413. emu=1;
  1414. }
  1415. }
  1416. pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
  1417. ptr = ref_picture[2] + offset;
  1418. if(emu){
  1419. emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1420. ptr= s->edge_emu_buffer;
  1421. }
  1422. pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
  1423. break;
  1424. case MV_TYPE_FIELD:
  1425. if (s->picture_structure == PICT_FRAME) {
  1426. if(s->quarter_sample){
  1427. /* top field */
  1428. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1429. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1430. 1, pix_op, qpix_op,
  1431. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1432. /* bottom field */
  1433. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1434. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1435. 1, pix_op, qpix_op,
  1436. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1437. }else{
  1438. /* top field */
  1439. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1440. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1441. 1, pix_op,
  1442. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1443. /* bottom field */
  1444. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1445. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1446. 1, pix_op,
  1447. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1448. }
  1449. } else {
  1450. }
  1451. break;
  1452. }
  1453. }
  1454. /* put block[] to dest[] */
  1455. static inline void put_dct(MpegEncContext *s,
  1456. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1457. {
  1458. s->dct_unquantize(s, block, i, s->qscale);
  1459. s->idct_put (dest, line_size, block);
  1460. }
  1461. /* add block[] to dest[] */
  1462. static inline void add_dct(MpegEncContext *s,
  1463. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1464. {
  1465. if (s->block_last_index[i] >= 0) {
  1466. s->idct_add (dest, line_size, block);
  1467. }
  1468. }
  1469. static inline void add_dequant_dct(MpegEncContext *s,
  1470. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1471. {
  1472. if (s->block_last_index[i] >= 0) {
  1473. s->dct_unquantize(s, block, i, s->qscale);
  1474. s->idct_add (dest, line_size, block);
  1475. }
  1476. }
  1477. /**
  1478. * cleans dc, ac, coded_block for the current non intra MB
  1479. */
  1480. void ff_clean_intra_table_entries(MpegEncContext *s)
  1481. {
  1482. int wrap = s->block_wrap[0];
  1483. int xy = s->block_index[0];
  1484. s->dc_val[0][xy ] =
  1485. s->dc_val[0][xy + 1 ] =
  1486. s->dc_val[0][xy + wrap] =
  1487. s->dc_val[0][xy + 1 + wrap] = 1024;
  1488. /* ac pred */
  1489. memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16));
  1490. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
  1491. if (s->msmpeg4_version>=3) {
  1492. s->coded_block[xy ] =
  1493. s->coded_block[xy + 1 ] =
  1494. s->coded_block[xy + wrap] =
  1495. s->coded_block[xy + 1 + wrap] = 0;
  1496. }
  1497. /* chroma */
  1498. wrap = s->block_wrap[4];
  1499. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  1500. s->dc_val[1][xy] =
  1501. s->dc_val[2][xy] = 1024;
  1502. /* ac pred */
  1503. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  1504. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  1505. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1506. }
  1507. /* generic function called after a macroblock has been parsed by the
  1508. decoder or after it has been encoded by the encoder.
  1509. Important variables used:
  1510. s->mb_intra : true if intra macroblock
  1511. s->mv_dir : motion vector direction
  1512. s->mv_type : motion vector type
  1513. s->mv : motion vector
  1514. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1515. */
  1516. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1517. {
  1518. int mb_x, mb_y;
  1519. const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
  1520. mb_x = s->mb_x;
  1521. mb_y = s->mb_y;
  1522. s->qscale_table[mb_xy]= s->qscale;
  1523. /* update DC predictors for P macroblocks */
  1524. if (!s->mb_intra) {
  1525. if (s->h263_pred || s->h263_aic) {
  1526. if(s->mbintra_table[mb_xy])
  1527. ff_clean_intra_table_entries(s);
  1528. } else {
  1529. s->last_dc[0] =
  1530. s->last_dc[1] =
  1531. s->last_dc[2] = 128 << s->intra_dc_precision;
  1532. }
  1533. }
  1534. else if (s->h263_pred || s->h263_aic)
  1535. s->mbintra_table[mb_xy]=1;
  1536. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1537. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1538. const int wrap = s->block_wrap[0];
  1539. const int xy = s->block_index[0];
  1540. const int mb_index= s->mb_x + s->mb_y*s->mb_width;
  1541. if(s->mv_type == MV_TYPE_8X8){
  1542. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
  1543. } else {
  1544. int motion_x, motion_y;
  1545. if (s->mb_intra) {
  1546. motion_x = 0;
  1547. motion_y = 0;
  1548. if(s->co_located_type_table)
  1549. s->co_located_type_table[mb_index]= 0;
  1550. } else if (s->mv_type == MV_TYPE_16X16) {
  1551. motion_x = s->mv[0][0][0];
  1552. motion_y = s->mv[0][0][1];
  1553. if(s->co_located_type_table)
  1554. s->co_located_type_table[mb_index]= 0;
  1555. } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
  1556. int i;
  1557. motion_x = s->mv[0][0][0] + s->mv[0][1][0];
  1558. motion_y = s->mv[0][0][1] + s->mv[0][1][1];
  1559. motion_x = (motion_x>>1) | (motion_x&1);
  1560. for(i=0; i<2; i++){
  1561. s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
  1562. s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
  1563. s->field_select_table[mb_index][i]= s->field_select[0][i];
  1564. }
  1565. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
  1566. }
  1567. /* no update if 8X8 because it has been done during parsing */
  1568. s->motion_val[xy][0] = motion_x;
  1569. s->motion_val[xy][1] = motion_y;
  1570. s->motion_val[xy + 1][0] = motion_x;
  1571. s->motion_val[xy + 1][1] = motion_y;
  1572. s->motion_val[xy + wrap][0] = motion_x;
  1573. s->motion_val[xy + wrap][1] = motion_y;
  1574. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1575. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1576. }
  1577. }
  1578. if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
  1579. UINT8 *dest_y, *dest_cb, *dest_cr;
  1580. int dct_linesize, dct_offset;
  1581. op_pixels_func (*op_pix)[4];
  1582. qpel_mc_func (*op_qpix)[16];
  1583. /* avoid copy if macroblock skipped in last frame too
  1584. dont touch it for B-frames as they need the skip info from the next p-frame */
  1585. if (s->pict_type != B_TYPE) {
  1586. UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
  1587. if (s->mb_skiped) {
  1588. s->mb_skiped = 0;
  1589. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  1590. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1591. /* if previous was skipped too, then nothing to do !
  1592. skip only during decoding as we might trash the buffers during encoding a bit */
  1593. if (*mbskip_ptr >= s->ip_buffer_count && !s->encoding)
  1594. return;
  1595. } else {
  1596. *mbskip_ptr = 0; /* not skipped */
  1597. }
  1598. }
  1599. if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band){
  1600. dest_y = s->current_picture [0] + mb_x * 16;
  1601. dest_cb = s->current_picture[1] + mb_x * 8;
  1602. dest_cr = s->current_picture[2] + mb_x * 8;
  1603. }else{
  1604. dest_y = s->current_picture [0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1605. dest_cb = s->current_picture[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1606. dest_cr = s->current_picture[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1607. }
  1608. if (s->interlaced_dct) {
  1609. dct_linesize = s->linesize * 2;
  1610. dct_offset = s->linesize;
  1611. } else {
  1612. dct_linesize = s->linesize;
  1613. dct_offset = s->linesize * 8;
  1614. }
  1615. if (!s->mb_intra) {
  1616. /* motion handling */
  1617. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1618. if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
  1619. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1620. op_pix = s->dsp.put_pixels_tab;
  1621. op_qpix= s->dsp.put_qpel_pixels_tab;
  1622. }else{
  1623. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1624. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  1625. }
  1626. if (s->mv_dir & MV_DIR_FORWARD) {
  1627. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1628. op_pix = s->dsp.avg_pixels_tab;
  1629. op_qpix= s->dsp.avg_qpel_pixels_tab;
  1630. }
  1631. if (s->mv_dir & MV_DIR_BACKWARD) {
  1632. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1633. }
  1634. }
  1635. /* skip dequant / idct if we are really late ;) */
  1636. if(s->hurry_up>1) return;
  1637. /* add dct residue */
  1638. if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
  1639. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1640. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  1641. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1642. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1643. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1644. if(!(s->flags&CODEC_FLAG_GRAY)){
  1645. add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1646. add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1647. }
  1648. } else {
  1649. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1650. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1651. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1652. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1653. if(!(s->flags&CODEC_FLAG_GRAY)){
  1654. add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1655. add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1656. }
  1657. }
  1658. } else {
  1659. /* dct only in intra block */
  1660. if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
  1661. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1662. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1663. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1664. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1665. if(!(s->flags&CODEC_FLAG_GRAY)){
  1666. put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1667. put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1668. }
  1669. }else{
  1670. s->idct_put(dest_y , dct_linesize, block[0]);
  1671. s->idct_put(dest_y + 8, dct_linesize, block[1]);
  1672. s->idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1673. s->idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  1674. if(!(s->flags&CODEC_FLAG_GRAY)){
  1675. s->idct_put(dest_cb, s->uvlinesize, block[4]);
  1676. s->idct_put(dest_cr, s->uvlinesize, block[5]);
  1677. }
  1678. }
  1679. }
  1680. }
  1681. }
  1682. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  1683. {
  1684. static const char tab[64]=
  1685. {3,2,2,1,1,1,1,1,
  1686. 1,1,1,1,1,1,1,1,
  1687. 1,1,1,1,1,1,1,1,
  1688. 0,0,0,0,0,0,0,0,
  1689. 0,0,0,0,0,0,0,0,
  1690. 0,0,0,0,0,0,0,0,
  1691. 0,0,0,0,0,0,0,0,
  1692. 0,0,0,0,0,0,0,0};
  1693. int score=0;
  1694. int run=0;
  1695. int i;
  1696. DCTELEM *block= s->block[n];
  1697. const int last_index= s->block_last_index[n];
  1698. int skip_dc;
  1699. if(threshold<0){
  1700. skip_dc=0;
  1701. threshold= -threshold;
  1702. }else
  1703. skip_dc=1;
  1704. /* are all which we could set to zero are allready zero? */
  1705. if(last_index<=skip_dc - 1) return;
  1706. for(i=0; i<=last_index; i++){
  1707. const int j = s->intra_scantable.permutated[i];
  1708. const int level = ABS(block[j]);
  1709. if(level==1){
  1710. if(skip_dc && i==0) continue;
  1711. score+= tab[run];
  1712. run=0;
  1713. }else if(level>1){
  1714. return;
  1715. }else{
  1716. run++;
  1717. }
  1718. }
  1719. if(score >= threshold) return;
  1720. for(i=skip_dc; i<=last_index; i++){
  1721. const int j = s->intra_scantable.permutated[i];
  1722. block[j]=0;
  1723. }
  1724. if(block[0]) s->block_last_index[n]= 0;
  1725. else s->block_last_index[n]= -1;
  1726. }
  1727. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1728. {
  1729. int i;
  1730. const int maxlevel= s->max_qcoeff;
  1731. const int minlevel= s->min_qcoeff;
  1732. if(s->mb_intra){
  1733. i=1; //skip clipping of intra dc
  1734. }else
  1735. i=0;
  1736. for(;i<=last_index; i++){
  1737. const int j= s->intra_scantable.permutated[i];
  1738. int level = block[j];
  1739. if (level>maxlevel) level=maxlevel;
  1740. else if(level<minlevel) level=minlevel;
  1741. block[j]= level;
  1742. }
  1743. }
  1744. static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n)
  1745. {
  1746. int i;
  1747. if(s->mb_intra){
  1748. i=1; //skip clipping of intra dc
  1749. //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
  1750. }else
  1751. i=0;
  1752. for(;i<=s->block_last_index[n]; i++){
  1753. const int j = s->intra_scantable.permutated[i];
  1754. int level = block[j];
  1755. block[j]= ROUNDED_DIV(level*oldq, newq);
  1756. }
  1757. for(i=s->block_last_index[n]; i>=0; i--){
  1758. const int j = s->intra_scantable.permutated[i];
  1759. if(block[j]) break;
  1760. }
  1761. s->block_last_index[n]= i;
  1762. }
  1763. static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64])
  1764. {
  1765. int i,n, newq;
  1766. const int maxlevel= s->max_qcoeff;
  1767. const int minlevel= s->min_qcoeff;
  1768. int largest=0, smallest=0;
  1769. assert(s->adaptive_quant);
  1770. for(n=0; n<6; n++){
  1771. if(s->mb_intra){
  1772. i=1; //skip clipping of intra dc
  1773. //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
  1774. }else
  1775. i=0;
  1776. for(;i<=s->block_last_index[n]; i++){
  1777. const int j = s->intra_scantable.permutated[i];
  1778. int level = block[n][j];
  1779. if(largest < level) largest = level;
  1780. if(smallest > level) smallest= level;
  1781. }
  1782. }
  1783. for(newq=s->qscale+1; newq<32; newq++){
  1784. if( ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel
  1785. && ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel)
  1786. break;
  1787. }
  1788. if(s->out_format==FMT_H263){
  1789. /* h263 like formats cannot change qscale by more than 2 easiely */
  1790. if(s->avctx->qmin + 2 < newq)
  1791. newq= s->avctx->qmin + 2;
  1792. }
  1793. for(n=0; n<6; n++){
  1794. requantize_coeffs(s, block[n], s->qscale, newq, n);
  1795. clip_coeffs(s, block[n], s->block_last_index[n]);
  1796. }
  1797. s->dquant+= newq - s->qscale;
  1798. s->qscale= newq;
  1799. }
  1800. #if 0
  1801. static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
  1802. int score=0;
  1803. int x,y;
  1804. for(y=0; y<7; y++){
  1805. for(x=0; x<16; x+=4){
  1806. score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
  1807. +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
  1808. }
  1809. s+= stride;
  1810. }
  1811. return score;
  1812. }
  1813. static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
  1814. int score=0;
  1815. int x,y;
  1816. for(y=0; y<7; y++){
  1817. for(x=0; x<16; x++){
  1818. score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  1819. }
  1820. s1+= stride;
  1821. s2+= stride;
  1822. }
  1823. return score;
  1824. }
  1825. #else
  1826. #define SQ(a) ((a)*(a))
  1827. static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
  1828. int score=0;
  1829. int x,y;
  1830. for(y=0; y<7; y++){
  1831. for(x=0; x<16; x+=4){
  1832. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  1833. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  1834. }
  1835. s+= stride;
  1836. }
  1837. return score;
  1838. }
  1839. static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
  1840. int score=0;
  1841. int x,y;
  1842. for(y=0; y<7; y++){
  1843. for(x=0; x<16; x++){
  1844. score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  1845. }
  1846. s1+= stride;
  1847. s2+= stride;
  1848. }
  1849. return score;
  1850. }
  1851. #endif
  1852. void ff_draw_horiz_band(MpegEncContext *s){
  1853. if ( s->avctx->draw_horiz_band
  1854. && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
  1855. UINT8 *src_ptr[3];
  1856. int y, h, offset;
  1857. y = s->mb_y * 16;
  1858. h = s->height - y;
  1859. if (h > 16)
  1860. h = 16;
  1861. if(s->pict_type==B_TYPE)
  1862. offset = 0;
  1863. else
  1864. offset = y * s->linesize;
  1865. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  1866. src_ptr[0] = s->current_picture[0] + offset;
  1867. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  1868. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  1869. } else {
  1870. src_ptr[0] = s->last_picture[0] + offset;
  1871. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  1872. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  1873. }
  1874. emms_c();
  1875. s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
  1876. y, s->width, h);
  1877. }
  1878. }
  1879. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1880. {
  1881. const int mb_x= s->mb_x;
  1882. const int mb_y= s->mb_y;
  1883. int i;
  1884. int skip_dct[6];
  1885. int dct_offset = s->linesize*8; //default for progressive frames
  1886. for(i=0; i<6; i++) skip_dct[i]=0;
  1887. if(s->adaptive_quant){
  1888. s->dquant= s->qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
  1889. if(s->out_format==FMT_H263){
  1890. if (s->dquant> 2) s->dquant= 2;
  1891. else if(s->dquant<-2) s->dquant=-2;
  1892. }
  1893. if(s->codec_id==CODEC_ID_MPEG4){
  1894. if(!s->mb_intra){
  1895. assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
  1896. if(s->mv_dir&MV_DIRECT)
  1897. s->dquant=0;
  1898. }
  1899. }
  1900. s->qscale+= s->dquant;
  1901. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  1902. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  1903. }
  1904. if (s->mb_intra) {
  1905. UINT8 *ptr;
  1906. int wrap_y;
  1907. int emu=0;
  1908. wrap_y = s->linesize;
  1909. ptr = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1910. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  1911. emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  1912. ptr= s->edge_emu_buffer;
  1913. emu=1;
  1914. }
  1915. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  1916. int progressive_score, interlaced_score;
  1917. progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
  1918. interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
  1919. if(progressive_score > interlaced_score + 100){
  1920. s->interlaced_dct=1;
  1921. dct_offset= wrap_y;
  1922. wrap_y<<=1;
  1923. }else
  1924. s->interlaced_dct=0;
  1925. }
  1926. s->dsp.get_pixels(s->block[0], ptr , wrap_y);
  1927. s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
  1928. s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  1929. s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  1930. if(s->flags&CODEC_FLAG_GRAY){
  1931. skip_dct[4]= 1;
  1932. skip_dct[5]= 1;
  1933. }else{
  1934. int wrap_c = s->uvlinesize;
  1935. ptr = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1936. if(emu){
  1937. emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  1938. ptr= s->edge_emu_buffer;
  1939. }
  1940. s->dsp.get_pixels(s->block[4], ptr, wrap_c);
  1941. ptr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1942. if(emu){
  1943. emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  1944. ptr= s->edge_emu_buffer;
  1945. }
  1946. s->dsp.get_pixels(s->block[5], ptr, wrap_c);
  1947. }
  1948. }else{
  1949. op_pixels_func (*op_pix)[4];
  1950. qpel_mc_func (*op_qpix)[16];
  1951. UINT8 *dest_y, *dest_cb, *dest_cr;
  1952. UINT8 *ptr_y, *ptr_cb, *ptr_cr;
  1953. int wrap_y, wrap_c;
  1954. int emu=0;
  1955. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  1956. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1957. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1958. wrap_y = s->linesize;
  1959. wrap_c = s->uvlinesize;
  1960. ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1961. ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1962. ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  1963. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1964. op_pix = s->dsp.put_pixels_tab;
  1965. op_qpix= s->dsp.put_qpel_pixels_tab;
  1966. }else{
  1967. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1968. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  1969. }
  1970. if (s->mv_dir & MV_DIR_FORWARD) {
  1971. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
  1972. op_pix = s->dsp.avg_pixels_tab;
  1973. op_qpix= s->dsp.avg_qpel_pixels_tab;
  1974. }
  1975. if (s->mv_dir & MV_DIR_BACKWARD) {
  1976. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
  1977. }
  1978. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  1979. emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  1980. ptr_y= s->edge_emu_buffer;
  1981. emu=1;
  1982. }
  1983. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  1984. int progressive_score, interlaced_score;
  1985. progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
  1986. + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
  1987. interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
  1988. + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
  1989. if(progressive_score > interlaced_score + 600){
  1990. s->interlaced_dct=1;
  1991. dct_offset= wrap_y;
  1992. wrap_y<<=1;
  1993. }else
  1994. s->interlaced_dct=0;
  1995. }
  1996. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  1997. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1998. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  1999. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  2000. if(s->flags&CODEC_FLAG_GRAY){
  2001. skip_dct[4]= 1;
  2002. skip_dct[5]= 1;
  2003. }else{
  2004. if(emu){
  2005. emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2006. ptr_cb= s->edge_emu_buffer;
  2007. }
  2008. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2009. if(emu){
  2010. emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2011. ptr_cr= s->edge_emu_buffer;
  2012. }
  2013. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2014. }
  2015. /* pre quantization */
  2016. if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  2017. //FIXME optimize
  2018. if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  2019. if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  2020. if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  2021. if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  2022. if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
  2023. if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
  2024. #if 0
  2025. {
  2026. static int stat[7];
  2027. int num=0;
  2028. for(i=0; i<6; i++)
  2029. if(skip_dct[i]) num++;
  2030. stat[num]++;
  2031. if(s->mb_x==0 && s->mb_y==0){
  2032. for(i=0; i<7; i++){
  2033. printf("%6d %1d\n", stat[i], i);
  2034. }
  2035. }
  2036. }
  2037. #endif
  2038. }
  2039. }
  2040. #if 0
  2041. {
  2042. float adap_parm;
  2043. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  2044. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  2045. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  2046. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  2047. s->qscale, adap_parm, s->qscale*adap_parm,
  2048. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  2049. }
  2050. #endif
  2051. /* DCT & quantize */
  2052. if(s->out_format==FMT_MJPEG){
  2053. for(i=0;i<6;i++) {
  2054. int overflow;
  2055. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  2056. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2057. }
  2058. }else{
  2059. for(i=0;i<6;i++) {
  2060. if(!skip_dct[i]){
  2061. int overflow;
  2062. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  2063. // FIXME we could decide to change to quantizer instead of clipping
  2064. // JS: I don't think that would be a good idea it could lower quality instead
  2065. // of improve it. Just INTRADC clipping deserves changes in quantizer
  2066. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2067. }else
  2068. s->block_last_index[i]= -1;
  2069. }
  2070. if(s->luma_elim_threshold && !s->mb_intra)
  2071. for(i=0; i<4; i++)
  2072. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2073. if(s->chroma_elim_threshold && !s->mb_intra)
  2074. for(i=4; i<6; i++)
  2075. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2076. }
  2077. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  2078. s->block_last_index[4]=
  2079. s->block_last_index[5]= 0;
  2080. s->block[4][0]=
  2081. s->block[5][0]= 128;
  2082. }
  2083. #ifdef CONFIG_ENCODERS
  2084. /* huffman encode */
  2085. switch(s->out_format) {
  2086. case FMT_MPEG1:
  2087. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  2088. break;
  2089. case FMT_H263:
  2090. if (s->h263_msmpeg4)
  2091. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  2092. else if(s->h263_pred)
  2093. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  2094. else
  2095. h263_encode_mb(s, s->block, motion_x, motion_y);
  2096. break;
  2097. case FMT_MJPEG:
  2098. mjpeg_encode_mb(s, s->block);
  2099. break;
  2100. }
  2101. #endif
  2102. }
  2103. void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
  2104. {
  2105. int bytes= length>>4;
  2106. int bits= length&15;
  2107. int i;
  2108. if(length==0) return;
  2109. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  2110. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  2111. }
  2112. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2113. int i;
  2114. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2115. /* mpeg1 */
  2116. d->mb_incr= s->mb_incr;
  2117. for(i=0; i<3; i++)
  2118. d->last_dc[i]= s->last_dc[i];
  2119. /* statistics */
  2120. d->mv_bits= s->mv_bits;
  2121. d->i_tex_bits= s->i_tex_bits;
  2122. d->p_tex_bits= s->p_tex_bits;
  2123. d->i_count= s->i_count;
  2124. d->f_count= s->f_count;
  2125. d->b_count= s->b_count;
  2126. d->skip_count= s->skip_count;
  2127. d->misc_bits= s->misc_bits;
  2128. d->last_bits= 0;
  2129. d->mb_skiped= s->mb_skiped;
  2130. }
  2131. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2132. int i;
  2133. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2134. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2135. /* mpeg1 */
  2136. d->mb_incr= s->mb_incr;
  2137. for(i=0; i<3; i++)
  2138. d->last_dc[i]= s->last_dc[i];
  2139. /* statistics */
  2140. d->mv_bits= s->mv_bits;
  2141. d->i_tex_bits= s->i_tex_bits;
  2142. d->p_tex_bits= s->p_tex_bits;
  2143. d->i_count= s->i_count;
  2144. d->f_count= s->f_count;
  2145. d->b_count= s->b_count;
  2146. d->skip_count= s->skip_count;
  2147. d->misc_bits= s->misc_bits;
  2148. d->mb_intra= s->mb_intra;
  2149. d->mb_skiped= s->mb_skiped;
  2150. d->mv_type= s->mv_type;
  2151. d->mv_dir= s->mv_dir;
  2152. d->pb= s->pb;
  2153. if(s->data_partitioning){
  2154. d->pb2= s->pb2;
  2155. d->tex_pb= s->tex_pb;
  2156. }
  2157. d->block= s->block;
  2158. for(i=0; i<6; i++)
  2159. d->block_last_index[i]= s->block_last_index[i];
  2160. d->interlaced_dct= s->interlaced_dct;
  2161. }
  2162. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  2163. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2164. int *dmin, int *next_block, int motion_x, int motion_y)
  2165. {
  2166. int bits_count;
  2167. copy_context_before_encode(s, backup, type);
  2168. s->block= s->blocks[*next_block];
  2169. s->pb= pb[*next_block];
  2170. if(s->data_partitioning){
  2171. s->pb2 = pb2 [*next_block];
  2172. s->tex_pb= tex_pb[*next_block];
  2173. }
  2174. encode_mb(s, motion_x, motion_y);
  2175. bits_count= get_bit_count(&s->pb);
  2176. if(s->data_partitioning){
  2177. bits_count+= get_bit_count(&s->pb2);
  2178. bits_count+= get_bit_count(&s->tex_pb);
  2179. }
  2180. if(bits_count<*dmin){
  2181. *dmin= bits_count;
  2182. *next_block^=1;
  2183. copy_context_after_encode(best, s, type);
  2184. }
  2185. }
  2186. static void encode_picture(MpegEncContext *s, int picture_number)
  2187. {
  2188. int mb_x, mb_y, pdif = 0;
  2189. int i;
  2190. int bits;
  2191. MpegEncContext best_s, backup_s;
  2192. UINT8 bit_buf[2][3000];
  2193. UINT8 bit_buf2[2][3000];
  2194. UINT8 bit_buf_tex[2][3000];
  2195. PutBitContext pb[2], pb2[2], tex_pb[2];
  2196. for(i=0; i<2; i++){
  2197. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  2198. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  2199. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  2200. }
  2201. s->picture_number = picture_number;
  2202. s->block_wrap[0]=
  2203. s->block_wrap[1]=
  2204. s->block_wrap[2]=
  2205. s->block_wrap[3]= s->mb_width*2 + 2;
  2206. s->block_wrap[4]=
  2207. s->block_wrap[5]= s->mb_width + 2;
  2208. /* Reset the average MB variance */
  2209. s->mb_var_sum = 0;
  2210. s->mc_mb_var_sum = 0;
  2211. /* we need to initialize some time vars before we can encode b-frames */
  2212. if (s->h263_pred && !s->h263_msmpeg4)
  2213. ff_set_mpeg4_time(s, s->picture_number);
  2214. s->scene_change_score=0;
  2215. s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
  2216. /* Estimate motion for every MB */
  2217. if(s->pict_type != I_TYPE){
  2218. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2219. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2220. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2221. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2222. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2223. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2224. s->mb_x = mb_x;
  2225. s->mb_y = mb_y;
  2226. s->block_index[0]+=2;
  2227. s->block_index[1]+=2;
  2228. s->block_index[2]+=2;
  2229. s->block_index[3]+=2;
  2230. /* compute motion vector & mb_type and store in context */
  2231. if(s->pict_type==B_TYPE)
  2232. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  2233. else
  2234. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  2235. // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
  2236. }
  2237. }
  2238. }else /* if(s->pict_type == I_TYPE) */{
  2239. /* I-Frame */
  2240. //FIXME do we need to zero them?
  2241. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  2242. memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
  2243. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  2244. if(!s->fixed_qscale){
  2245. /* finding spatial complexity for I-frame rate control */
  2246. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2247. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2248. int xx = mb_x * 16;
  2249. int yy = mb_y * 16;
  2250. uint8_t *pix = s->new_picture[0] + (yy * s->linesize) + xx;
  2251. int varc;
  2252. int sum = s->dsp.pix_sum(pix, s->linesize);
  2253. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  2254. s->mb_var [s->mb_width * mb_y + mb_x] = varc;
  2255. s->mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
  2256. s->mb_var_sum += varc;
  2257. }
  2258. }
  2259. }
  2260. }
  2261. emms_c();
  2262. if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
  2263. s->pict_type= I_TYPE;
  2264. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  2265. if(s->max_b_frames==0){
  2266. s->input_pict_type= I_TYPE;
  2267. s->input_picture_in_gop_number=0;
  2268. }
  2269. //printf("Scene change detected, encoding as I Frame %d %d\n", s->mb_var_sum, s->mc_mb_var_sum);
  2270. }
  2271. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
  2272. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  2273. ff_fix_long_p_mvs(s);
  2274. if(s->pict_type==B_TYPE){
  2275. s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  2276. s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  2277. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  2278. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  2279. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  2280. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  2281. }
  2282. if (s->fixed_qscale)
  2283. s->frame_qscale = s->avctx->quality;
  2284. else
  2285. s->frame_qscale = ff_rate_estimate_qscale(s);
  2286. if(s->adaptive_quant){
  2287. switch(s->codec_id){
  2288. case CODEC_ID_MPEG4:
  2289. ff_clean_mpeg4_qscales(s);
  2290. break;
  2291. case CODEC_ID_H263:
  2292. case CODEC_ID_H263P:
  2293. ff_clean_h263_qscales(s);
  2294. break;
  2295. }
  2296. s->qscale= s->qscale_table[0];
  2297. }else
  2298. s->qscale= (int)(s->frame_qscale + 0.5);
  2299. if (s->out_format == FMT_MJPEG) {
  2300. /* for mjpeg, we do include qscale in the matrix */
  2301. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  2302. for(i=1;i<64;i++){
  2303. int j= s->idct_permutation[i];
  2304. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  2305. }
  2306. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  2307. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
  2308. }
  2309. s->last_bits= get_bit_count(&s->pb);
  2310. switch(s->out_format) {
  2311. case FMT_MJPEG:
  2312. mjpeg_picture_header(s);
  2313. break;
  2314. case FMT_H263:
  2315. if (s->h263_msmpeg4)
  2316. msmpeg4_encode_picture_header(s, picture_number);
  2317. else if (s->h263_pred)
  2318. mpeg4_encode_picture_header(s, picture_number);
  2319. else if (s->h263_rv10)
  2320. rv10_encode_picture_header(s, picture_number);
  2321. else
  2322. h263_encode_picture_header(s, picture_number);
  2323. break;
  2324. case FMT_MPEG1:
  2325. mpeg1_encode_picture_header(s, picture_number);
  2326. break;
  2327. }
  2328. bits= get_bit_count(&s->pb);
  2329. s->header_bits= bits - s->last_bits;
  2330. s->last_bits= bits;
  2331. s->mv_bits=0;
  2332. s->misc_bits=0;
  2333. s->i_tex_bits=0;
  2334. s->p_tex_bits=0;
  2335. s->i_count=0;
  2336. s->f_count=0;
  2337. s->b_count=0;
  2338. s->skip_count=0;
  2339. /* init last dc values */
  2340. /* note: quant matrix value (8) is implied here */
  2341. s->last_dc[0] = 128;
  2342. s->last_dc[1] = 128;
  2343. s->last_dc[2] = 128;
  2344. s->mb_incr = 1;
  2345. s->last_mv[0][0][0] = 0;
  2346. s->last_mv[0][0][1] = 0;
  2347. if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
  2348. s->gob_index = ff_h263_get_gob_height(s);
  2349. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  2350. ff_mpeg4_init_partitions(s);
  2351. s->resync_mb_x=0;
  2352. s->resync_mb_y=0;
  2353. s->first_slice_line = 1;
  2354. s->ptr_lastgob = s->pb.buf;
  2355. s->ptr_last_mb_line = s->pb.buf;
  2356. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2357. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2358. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2359. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2360. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2361. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2362. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2363. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2364. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2365. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2366. const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  2367. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  2368. // int d;
  2369. int dmin=10000000;
  2370. s->mb_x = mb_x;
  2371. s->mb_y = mb_y;
  2372. s->block_index[0]+=2;
  2373. s->block_index[1]+=2;
  2374. s->block_index[2]+=2;
  2375. s->block_index[3]+=2;
  2376. s->block_index[4]++;
  2377. s->block_index[5]++;
  2378. /* write gob / video packet header */
  2379. if(s->rtp_mode){
  2380. int current_packet_size, is_gob_start;
  2381. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  2382. is_gob_start=0;
  2383. if(s->codec_id==CODEC_ID_MPEG4){
  2384. if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
  2385. && s->mb_y + s->mb_x>0){
  2386. if(s->partitioned_frame){
  2387. ff_mpeg4_merge_partitions(s);
  2388. ff_mpeg4_init_partitions(s);
  2389. }
  2390. ff_mpeg4_encode_video_packet_header(s);
  2391. if(s->flags&CODEC_FLAG_PASS1){
  2392. int bits= get_bit_count(&s->pb);
  2393. s->misc_bits+= bits - s->last_bits;
  2394. s->last_bits= bits;
  2395. }
  2396. ff_mpeg4_clean_buffers(s);
  2397. is_gob_start=1;
  2398. }
  2399. }else{
  2400. if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
  2401. && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
  2402. h263_encode_gob_header(s, mb_y);
  2403. is_gob_start=1;
  2404. }
  2405. }
  2406. if(is_gob_start){
  2407. s->ptr_lastgob = pbBufPtr(&s->pb);
  2408. s->first_slice_line=1;
  2409. s->resync_mb_x=mb_x;
  2410. s->resync_mb_y=mb_y;
  2411. }
  2412. }
  2413. if( (s->resync_mb_x == s->mb_x)
  2414. && s->resync_mb_y+1 == s->mb_y){
  2415. s->first_slice_line=0;
  2416. }
  2417. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  2418. int next_block=0;
  2419. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2420. copy_context_before_encode(&backup_s, s, -1);
  2421. backup_s.pb= s->pb;
  2422. best_s.data_partitioning= s->data_partitioning;
  2423. best_s.partitioned_frame= s->partitioned_frame;
  2424. if(s->data_partitioning){
  2425. backup_s.pb2= s->pb2;
  2426. backup_s.tex_pb= s->tex_pb;
  2427. }
  2428. if(mb_type&MB_TYPE_INTER){
  2429. s->mv_dir = MV_DIR_FORWARD;
  2430. s->mv_type = MV_TYPE_16X16;
  2431. s->mb_intra= 0;
  2432. s->mv[0][0][0] = s->p_mv_table[xy][0];
  2433. s->mv[0][0][1] = s->p_mv_table[xy][1];
  2434. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  2435. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2436. }
  2437. if(mb_type&MB_TYPE_INTER4V){
  2438. s->mv_dir = MV_DIR_FORWARD;
  2439. s->mv_type = MV_TYPE_8X8;
  2440. s->mb_intra= 0;
  2441. for(i=0; i<4; i++){
  2442. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2443. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2444. }
  2445. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  2446. &dmin, &next_block, 0, 0);
  2447. }
  2448. if(mb_type&MB_TYPE_FORWARD){
  2449. s->mv_dir = MV_DIR_FORWARD;
  2450. s->mv_type = MV_TYPE_16X16;
  2451. s->mb_intra= 0;
  2452. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2453. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2454. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  2455. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2456. }
  2457. if(mb_type&MB_TYPE_BACKWARD){
  2458. s->mv_dir = MV_DIR_BACKWARD;
  2459. s->mv_type = MV_TYPE_16X16;
  2460. s->mb_intra= 0;
  2461. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2462. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2463. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  2464. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  2465. }
  2466. if(mb_type&MB_TYPE_BIDIR){
  2467. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2468. s->mv_type = MV_TYPE_16X16;
  2469. s->mb_intra= 0;
  2470. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2471. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2472. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2473. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2474. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  2475. &dmin, &next_block, 0, 0);
  2476. }
  2477. if(mb_type&MB_TYPE_DIRECT){
  2478. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2479. s->mv_type = MV_TYPE_16X16; //FIXME
  2480. s->mb_intra= 0;
  2481. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  2482. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  2483. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  2484. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  2485. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2486. &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
  2487. }
  2488. if(mb_type&MB_TYPE_INTRA){
  2489. s->mv_dir = MV_DIR_FORWARD;
  2490. s->mv_type = MV_TYPE_16X16;
  2491. s->mb_intra= 1;
  2492. s->mv[0][0][0] = 0;
  2493. s->mv[0][0][1] = 0;
  2494. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  2495. &dmin, &next_block, 0, 0);
  2496. /* force cleaning of ac/dc pred stuff if needed ... */
  2497. if(s->h263_pred || s->h263_aic)
  2498. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  2499. }
  2500. copy_context_after_encode(s, &best_s, -1);
  2501. pb_bits_count= get_bit_count(&s->pb);
  2502. flush_put_bits(&s->pb);
  2503. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2504. s->pb= backup_s.pb;
  2505. if(s->data_partitioning){
  2506. pb2_bits_count= get_bit_count(&s->pb2);
  2507. flush_put_bits(&s->pb2);
  2508. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2509. s->pb2= backup_s.pb2;
  2510. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  2511. flush_put_bits(&s->tex_pb);
  2512. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2513. s->tex_pb= backup_s.tex_pb;
  2514. }
  2515. s->last_bits= get_bit_count(&s->pb);
  2516. } else {
  2517. int motion_x, motion_y;
  2518. s->mv_type=MV_TYPE_16X16;
  2519. // only one MB-Type possible
  2520. switch(mb_type){
  2521. case MB_TYPE_INTRA:
  2522. s->mv_dir = MV_DIR_FORWARD;
  2523. s->mb_intra= 1;
  2524. motion_x= s->mv[0][0][0] = 0;
  2525. motion_y= s->mv[0][0][1] = 0;
  2526. break;
  2527. case MB_TYPE_INTER:
  2528. s->mv_dir = MV_DIR_FORWARD;
  2529. s->mb_intra= 0;
  2530. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2531. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2532. break;
  2533. case MB_TYPE_INTER4V:
  2534. s->mv_dir = MV_DIR_FORWARD;
  2535. s->mv_type = MV_TYPE_8X8;
  2536. s->mb_intra= 0;
  2537. for(i=0; i<4; i++){
  2538. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2539. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2540. }
  2541. motion_x= motion_y= 0;
  2542. break;
  2543. case MB_TYPE_DIRECT:
  2544. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2545. s->mb_intra= 0;
  2546. motion_x=s->b_direct_mv_table[xy][0];
  2547. motion_y=s->b_direct_mv_table[xy][1];
  2548. s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
  2549. s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
  2550. s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
  2551. s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
  2552. break;
  2553. case MB_TYPE_BIDIR:
  2554. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2555. s->mb_intra= 0;
  2556. motion_x=0;
  2557. motion_y=0;
  2558. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2559. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2560. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2561. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2562. break;
  2563. case MB_TYPE_BACKWARD:
  2564. s->mv_dir = MV_DIR_BACKWARD;
  2565. s->mb_intra= 0;
  2566. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2567. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2568. break;
  2569. case MB_TYPE_FORWARD:
  2570. s->mv_dir = MV_DIR_FORWARD;
  2571. s->mb_intra= 0;
  2572. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2573. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2574. // printf(" %d %d ", motion_x, motion_y);
  2575. break;
  2576. default:
  2577. motion_x=motion_y=0; //gcc warning fix
  2578. printf("illegal MB type\n");
  2579. }
  2580. encode_mb(s, motion_x, motion_y);
  2581. }
  2582. /* clean the MV table in IPS frames for direct mode in B frames */
  2583. if(s->mb_intra /* && I,P,S_TYPE */){
  2584. s->p_mv_table[xy][0]=0;
  2585. s->p_mv_table[xy][1]=0;
  2586. }
  2587. MPV_decode_mb(s, s->block);
  2588. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  2589. }
  2590. /* Obtain average mb_row size for RTP */
  2591. if (s->rtp_mode) {
  2592. if (mb_y==0)
  2593. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  2594. else {
  2595. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  2596. }
  2597. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  2598. }
  2599. }
  2600. emms_c();
  2601. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  2602. ff_mpeg4_merge_partitions(s);
  2603. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  2604. msmpeg4_encode_ext_header(s);
  2605. if(s->codec_id==CODEC_ID_MPEG4)
  2606. ff_mpeg4_stuffing(&s->pb);
  2607. //if (s->gob_number)
  2608. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  2609. /* Send the last GOB if RTP */
  2610. if (s->rtp_mode) {
  2611. flush_put_bits(&s->pb);
  2612. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  2613. /* Call the RTP callback to send the last GOB */
  2614. if (s->rtp_callback)
  2615. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  2616. s->ptr_lastgob = pbBufPtr(&s->pb);
  2617. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  2618. }
  2619. }
  2620. static int dct_quantize_c(MpegEncContext *s,
  2621. DCTELEM *block, int n,
  2622. int qscale, int *overflow)
  2623. {
  2624. int i, j, level, last_non_zero, q;
  2625. const int *qmat;
  2626. const UINT8 *scantable= s->intra_scantable.scantable;
  2627. int bias;
  2628. int max=0;
  2629. unsigned int threshold1, threshold2;
  2630. s->fdct (block);
  2631. if (s->mb_intra) {
  2632. if (!s->h263_aic) {
  2633. if (n < 4)
  2634. q = s->y_dc_scale;
  2635. else
  2636. q = s->c_dc_scale;
  2637. q = q << 3;
  2638. } else
  2639. /* For AIC we skip quant/dequant of INTRADC */
  2640. q = 1 << 3;
  2641. /* note: block[0] is assumed to be positive */
  2642. block[0] = (block[0] + (q >> 1)) / q;
  2643. i = 1;
  2644. last_non_zero = 0;
  2645. qmat = s->q_intra_matrix[qscale];
  2646. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  2647. } else {
  2648. i = 0;
  2649. last_non_zero = -1;
  2650. qmat = s->q_inter_matrix[qscale];
  2651. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  2652. }
  2653. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  2654. threshold2= (threshold1<<1);
  2655. for(;i<64;i++) {
  2656. j = scantable[i];
  2657. level = block[j];
  2658. level = level * qmat[j];
  2659. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  2660. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  2661. if(((unsigned)(level+threshold1))>threshold2){
  2662. if(level>0){
  2663. level= (bias + level)>>QMAT_SHIFT;
  2664. block[j]= level;
  2665. }else{
  2666. level= (bias - level)>>QMAT_SHIFT;
  2667. block[j]= -level;
  2668. }
  2669. max |=level;
  2670. last_non_zero = i;
  2671. }else{
  2672. block[j]=0;
  2673. }
  2674. }
  2675. *overflow= s->max_qcoeff < max; //overflow might have happend
  2676. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  2677. if (s->idct_permutation_type != FF_NO_IDCT_PERM)
  2678. ff_block_permute(block, s->idct_permutation, scantable, last_non_zero);
  2679. return last_non_zero;
  2680. }
  2681. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  2682. DCTELEM *block, int n, int qscale)
  2683. {
  2684. int i, level, nCoeffs;
  2685. const UINT16 *quant_matrix;
  2686. nCoeffs= s->block_last_index[n];
  2687. if (s->mb_intra) {
  2688. if (n < 4)
  2689. block[0] = block[0] * s->y_dc_scale;
  2690. else
  2691. block[0] = block[0] * s->c_dc_scale;
  2692. /* XXX: only mpeg1 */
  2693. quant_matrix = s->intra_matrix;
  2694. for(i=1;i<=nCoeffs;i++) {
  2695. int j= s->intra_scantable.permutated[i];
  2696. level = block[j];
  2697. if (level) {
  2698. if (level < 0) {
  2699. level = -level;
  2700. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2701. level = (level - 1) | 1;
  2702. level = -level;
  2703. } else {
  2704. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2705. level = (level - 1) | 1;
  2706. }
  2707. #ifdef PARANOID
  2708. if (level < -2048 || level > 2047)
  2709. fprintf(stderr, "unquant error %d %d\n", i, level);
  2710. #endif
  2711. block[j] = level;
  2712. }
  2713. }
  2714. } else {
  2715. i = 0;
  2716. quant_matrix = s->inter_matrix;
  2717. for(;i<=nCoeffs;i++) {
  2718. int j= s->intra_scantable.permutated[i];
  2719. level = block[j];
  2720. if (level) {
  2721. if (level < 0) {
  2722. level = -level;
  2723. level = (((level << 1) + 1) * qscale *
  2724. ((int) (quant_matrix[j]))) >> 4;
  2725. level = (level - 1) | 1;
  2726. level = -level;
  2727. } else {
  2728. level = (((level << 1) + 1) * qscale *
  2729. ((int) (quant_matrix[j]))) >> 4;
  2730. level = (level - 1) | 1;
  2731. }
  2732. #ifdef PARANOID
  2733. if (level < -2048 || level > 2047)
  2734. fprintf(stderr, "unquant error %d %d\n", i, level);
  2735. #endif
  2736. block[j] = level;
  2737. }
  2738. }
  2739. }
  2740. }
  2741. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  2742. DCTELEM *block, int n, int qscale)
  2743. {
  2744. int i, level, nCoeffs;
  2745. const UINT16 *quant_matrix;
  2746. if(s->alternate_scan) nCoeffs= 63;
  2747. else nCoeffs= s->block_last_index[n];
  2748. if (s->mb_intra) {
  2749. if (n < 4)
  2750. block[0] = block[0] * s->y_dc_scale;
  2751. else
  2752. block[0] = block[0] * s->c_dc_scale;
  2753. quant_matrix = s->intra_matrix;
  2754. for(i=1;i<=nCoeffs;i++) {
  2755. int j= s->intra_scantable.permutated[i];
  2756. level = block[j];
  2757. if (level) {
  2758. if (level < 0) {
  2759. level = -level;
  2760. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2761. level = -level;
  2762. } else {
  2763. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2764. }
  2765. #ifdef PARANOID
  2766. if (level < -2048 || level > 2047)
  2767. fprintf(stderr, "unquant error %d %d\n", i, level);
  2768. #endif
  2769. block[j] = level;
  2770. }
  2771. }
  2772. } else {
  2773. int sum=-1;
  2774. i = 0;
  2775. quant_matrix = s->inter_matrix;
  2776. for(;i<=nCoeffs;i++) {
  2777. int j= s->intra_scantable.permutated[i];
  2778. level = block[j];
  2779. if (level) {
  2780. if (level < 0) {
  2781. level = -level;
  2782. level = (((level << 1) + 1) * qscale *
  2783. ((int) (quant_matrix[j]))) >> 4;
  2784. level = -level;
  2785. } else {
  2786. level = (((level << 1) + 1) * qscale *
  2787. ((int) (quant_matrix[j]))) >> 4;
  2788. }
  2789. #ifdef PARANOID
  2790. if (level < -2048 || level > 2047)
  2791. fprintf(stderr, "unquant error %d %d\n", i, level);
  2792. #endif
  2793. block[j] = level;
  2794. sum+=level;
  2795. }
  2796. }
  2797. block[63]^=sum&1;
  2798. }
  2799. }
  2800. static void dct_unquantize_h263_c(MpegEncContext *s,
  2801. DCTELEM *block, int n, int qscale)
  2802. {
  2803. int i, level, qmul, qadd;
  2804. int nCoeffs;
  2805. assert(s->block_last_index[n]>=0);
  2806. qadd = (qscale - 1) | 1;
  2807. qmul = qscale << 1;
  2808. if (s->mb_intra) {
  2809. if (!s->h263_aic) {
  2810. if (n < 4)
  2811. block[0] = block[0] * s->y_dc_scale;
  2812. else
  2813. block[0] = block[0] * s->c_dc_scale;
  2814. }else
  2815. qadd = 0;
  2816. i = 1;
  2817. nCoeffs= 63; //does not allways use zigzag table
  2818. } else {
  2819. i = 0;
  2820. nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
  2821. }
  2822. for(;i<=nCoeffs;i++) {
  2823. level = block[i];
  2824. if (level) {
  2825. if (level < 0) {
  2826. level = level * qmul - qadd;
  2827. } else {
  2828. level = level * qmul + qadd;
  2829. }
  2830. #ifdef PARANOID
  2831. if (level < -2048 || level > 2047)
  2832. fprintf(stderr, "unquant error %d %d\n", i, level);
  2833. #endif
  2834. block[i] = level;
  2835. }
  2836. }
  2837. }
  2838. AVCodec mpeg1video_encoder = {
  2839. "mpeg1video",
  2840. CODEC_TYPE_VIDEO,
  2841. CODEC_ID_MPEG1VIDEO,
  2842. sizeof(MpegEncContext),
  2843. MPV_encode_init,
  2844. MPV_encode_picture,
  2845. MPV_encode_end,
  2846. };
  2847. AVCodec h263_encoder = {
  2848. "h263",
  2849. CODEC_TYPE_VIDEO,
  2850. CODEC_ID_H263,
  2851. sizeof(MpegEncContext),
  2852. MPV_encode_init,
  2853. MPV_encode_picture,
  2854. MPV_encode_end,
  2855. };
  2856. AVCodec h263p_encoder = {
  2857. "h263p",
  2858. CODEC_TYPE_VIDEO,
  2859. CODEC_ID_H263P,
  2860. sizeof(MpegEncContext),
  2861. MPV_encode_init,
  2862. MPV_encode_picture,
  2863. MPV_encode_end,
  2864. };
  2865. AVCodec rv10_encoder = {
  2866. "rv10",
  2867. CODEC_TYPE_VIDEO,
  2868. CODEC_ID_RV10,
  2869. sizeof(MpegEncContext),
  2870. MPV_encode_init,
  2871. MPV_encode_picture,
  2872. MPV_encode_end,
  2873. };
  2874. AVCodec mjpeg_encoder = {
  2875. "mjpeg",
  2876. CODEC_TYPE_VIDEO,
  2877. CODEC_ID_MJPEG,
  2878. sizeof(MpegEncContext),
  2879. MPV_encode_init,
  2880. MPV_encode_picture,
  2881. MPV_encode_end,
  2882. };
  2883. AVCodec mpeg4_encoder = {
  2884. "mpeg4",
  2885. CODEC_TYPE_VIDEO,
  2886. CODEC_ID_MPEG4,
  2887. sizeof(MpegEncContext),
  2888. MPV_encode_init,
  2889. MPV_encode_picture,
  2890. MPV_encode_end,
  2891. };
  2892. AVCodec msmpeg4v1_encoder = {
  2893. "msmpeg4v1",
  2894. CODEC_TYPE_VIDEO,
  2895. CODEC_ID_MSMPEG4V1,
  2896. sizeof(MpegEncContext),
  2897. MPV_encode_init,
  2898. MPV_encode_picture,
  2899. MPV_encode_end,
  2900. };
  2901. AVCodec msmpeg4v2_encoder = {
  2902. "msmpeg4v2",
  2903. CODEC_TYPE_VIDEO,
  2904. CODEC_ID_MSMPEG4V2,
  2905. sizeof(MpegEncContext),
  2906. MPV_encode_init,
  2907. MPV_encode_picture,
  2908. MPV_encode_end,
  2909. };
  2910. AVCodec msmpeg4v3_encoder = {
  2911. "msmpeg4",
  2912. CODEC_TYPE_VIDEO,
  2913. CODEC_ID_MSMPEG4V3,
  2914. sizeof(MpegEncContext),
  2915. MPV_encode_init,
  2916. MPV_encode_picture,
  2917. MPV_encode_end,
  2918. };
  2919. AVCodec wmv1_encoder = {
  2920. "wmv1",
  2921. CODEC_TYPE_VIDEO,
  2922. CODEC_ID_WMV1,
  2923. sizeof(MpegEncContext),
  2924. MPV_encode_init,
  2925. MPV_encode_picture,
  2926. MPV_encode_end,
  2927. };
  2928. AVCodec wmv2_encoder = {
  2929. "wmv2",
  2930. CODEC_TYPE_VIDEO,
  2931. CODEC_ID_WMV2,
  2932. sizeof(MpegEncContext),
  2933. MPV_encode_init,
  2934. MPV_encode_picture,
  2935. MPV_encode_end,
  2936. };