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.

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