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.

4141 lines
143KB

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