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.

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