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.

3586 lines
122KB

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