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.

4005 lines
137KB

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