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.

4125 lines
142KB

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