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.

3878 lines
132KB

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