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.

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