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.

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