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.

4089 lines
139KB

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