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.

4040 lines
138KB

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