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.

3848 lines
139KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file libavcodec/mpegvideo_enc.c
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "avcodec.h"
  29. #include "dsputil.h"
  30. #include "mpegvideo.h"
  31. #include "mpegvideo_common.h"
  32. #include "mjpegenc.h"
  33. #include "msmpeg4.h"
  34. #include "h263.h"
  35. #include "faandct.h"
  36. #include "aandcttab.h"
  37. #include <limits.h>
  38. //#undef NDEBUG
  39. //#include <assert.h>
  40. static int encode_picture(MpegEncContext *s, int picture_number);
  41. static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
  42. static int sse_mb(MpegEncContext *s);
  43. /* enable all paranoid tests for rounding, overflows, etc... */
  44. //#define PARANOID
  45. //#define DEBUG
  46. static uint8_t default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
  47. static uint8_t default_fcode_tab[MAX_MV*2+1];
  48. void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
  49. const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
  50. {
  51. int qscale;
  52. int shift=0;
  53. for(qscale=qmin; qscale<=qmax; qscale++){
  54. int i;
  55. if (dsp->fdct == ff_jpeg_fdct_islow
  56. #ifdef FAAN_POSTSCALE
  57. || dsp->fdct == ff_faandct
  58. #endif
  59. ) {
  60. for(i=0;i<64;i++) {
  61. const int j= dsp->idct_permutation[i];
  62. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  63. /* 19952 <= ff_aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  64. /* (1 << 36) / 19952 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= (1 << 36) / 249205026 */
  65. /* 3444240 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  66. qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
  67. (qscale * quant_matrix[j]));
  68. }
  69. } else if (dsp->fdct == fdct_ifast
  70. #ifndef FAAN_POSTSCALE
  71. || dsp->fdct == ff_faandct
  72. #endif
  73. ) {
  74. for(i=0;i<64;i++) {
  75. const int j= dsp->idct_permutation[i];
  76. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  77. /* 19952 <= ff_aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  78. /* (1 << 36) / 19952 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  79. /* 3444240 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  80. qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
  81. (ff_aanscales[i] * qscale * quant_matrix[j]));
  82. }
  83. } else {
  84. for(i=0;i<64;i++) {
  85. const int j= dsp->idct_permutation[i];
  86. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  87. So 16 <= qscale * quant_matrix[i] <= 7905
  88. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  89. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  90. */
  91. qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
  92. // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  93. qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
  94. if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
  95. qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
  96. }
  97. }
  98. for(i=intra; i<64; i++){
  99. int64_t max= 8191;
  100. if (dsp->fdct == fdct_ifast
  101. #ifndef FAAN_POSTSCALE
  102. || dsp->fdct == ff_faandct
  103. #endif
  104. ) {
  105. max = (8191LL*ff_aanscales[i]) >> 14;
  106. }
  107. while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
  108. shift++;
  109. }
  110. }
  111. }
  112. if(shift){
  113. av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger than %d, overflows possible\n", QMAT_SHIFT - shift);
  114. }
  115. }
  116. static inline void update_qscale(MpegEncContext *s){
  117. s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  118. s->qscale= av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
  119. s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  120. }
  121. void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix){
  122. int i;
  123. if(matrix){
  124. put_bits(pb, 1, 1);
  125. for(i=0;i<64;i++) {
  126. put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
  127. }
  128. }else
  129. put_bits(pb, 1, 0);
  130. }
  131. /**
  132. * init s->current_picture.qscale_table from s->lambda_table
  133. */
  134. void ff_init_qscale_tab(MpegEncContext *s){
  135. int8_t * const qscale_table= s->current_picture.qscale_table;
  136. int i;
  137. for(i=0; i<s->mb_num; i++){
  138. unsigned int lam= s->lambda_table[ s->mb_index2xy[i] ];
  139. int qp= (lam*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  140. qscale_table[ s->mb_index2xy[i] ]= av_clip(qp, s->avctx->qmin, s->avctx->qmax);
  141. }
  142. }
  143. static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
  144. int i;
  145. dst->pict_type = src->pict_type;
  146. dst->quality = src->quality;
  147. dst->coded_picture_number = src->coded_picture_number;
  148. dst->display_picture_number = src->display_picture_number;
  149. // dst->reference = src->reference;
  150. dst->pts = src->pts;
  151. dst->interlaced_frame = src->interlaced_frame;
  152. dst->top_field_first = src->top_field_first;
  153. if(s->avctx->me_threshold){
  154. if(!src->motion_val[0])
  155. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
  156. if(!src->mb_type)
  157. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
  158. if(!src->ref_index[0])
  159. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
  160. if(src->motion_subsample_log2 != dst->motion_subsample_log2)
  161. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
  162. src->motion_subsample_log2, dst->motion_subsample_log2);
  163. memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
  164. for(i=0; i<2; i++){
  165. int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
  166. int height= ((16*s->mb_height)>>src->motion_subsample_log2);
  167. if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
  168. memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
  169. }
  170. if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
  171. memcpy(dst->ref_index[i], src->ref_index[i], s->b8_stride*2*s->mb_height*sizeof(int8_t));
  172. }
  173. }
  174. }
  175. }
  176. static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  177. #define COPY(a) dst->a= src->a
  178. COPY(pict_type);
  179. COPY(current_picture);
  180. COPY(f_code);
  181. COPY(b_code);
  182. COPY(qscale);
  183. COPY(lambda);
  184. COPY(lambda2);
  185. COPY(picture_in_gop_number);
  186. COPY(gop_picture_number);
  187. COPY(frame_pred_frame_dct); //FIXME don't set in encode_header
  188. COPY(progressive_frame); //FIXME don't set in encode_header
  189. COPY(partitioned_frame); //FIXME don't set in encode_header
  190. #undef COPY
  191. }
  192. /**
  193. * sets the given MpegEncContext to defaults for encoding.
  194. * the changed fields will not depend upon the prior state of the MpegEncContext.
  195. */
  196. static void MPV_encode_defaults(MpegEncContext *s){
  197. int i;
  198. MPV_common_defaults(s);
  199. for(i=-16; i<16; i++){
  200. default_fcode_tab[i + MAX_MV]= 1;
  201. }
  202. s->me.mv_penalty= default_mv_penalty;
  203. s->fcode_tab= default_fcode_tab;
  204. }
  205. /* init video encoder */
  206. av_cold int MPV_encode_init(AVCodecContext *avctx)
  207. {
  208. MpegEncContext *s = avctx->priv_data;
  209. int i;
  210. int chroma_h_shift, chroma_v_shift;
  211. MPV_encode_defaults(s);
  212. switch (avctx->codec_id) {
  213. case CODEC_ID_MPEG2VIDEO:
  214. if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P){
  215. av_log(avctx, AV_LOG_ERROR, "only YUV420 and YUV422 are supported\n");
  216. return -1;
  217. }
  218. break;
  219. case CODEC_ID_LJPEG:
  220. case CODEC_ID_MJPEG:
  221. if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUVJ422P && avctx->pix_fmt != PIX_FMT_RGB32 &&
  222. ((avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P) || avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL)){
  223. av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
  224. return -1;
  225. }
  226. break;
  227. default:
  228. if(avctx->pix_fmt != PIX_FMT_YUV420P){
  229. av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
  230. return -1;
  231. }
  232. }
  233. switch (avctx->pix_fmt) {
  234. case PIX_FMT_YUVJ422P:
  235. case PIX_FMT_YUV422P:
  236. s->chroma_format = CHROMA_422;
  237. break;
  238. case PIX_FMT_YUVJ420P:
  239. case PIX_FMT_YUV420P:
  240. default:
  241. s->chroma_format = CHROMA_420;
  242. break;
  243. }
  244. s->bit_rate = avctx->bit_rate;
  245. s->width = avctx->width;
  246. s->height = avctx->height;
  247. if(avctx->gop_size > 600 && avctx->strict_std_compliance>FF_COMPLIANCE_EXPERIMENTAL){
  248. av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
  249. avctx->gop_size=600;
  250. }
  251. s->gop_size = avctx->gop_size;
  252. s->avctx = avctx;
  253. s->flags= avctx->flags;
  254. s->flags2= avctx->flags2;
  255. s->max_b_frames= avctx->max_b_frames;
  256. s->codec_id= avctx->codec->id;
  257. s->luma_elim_threshold = avctx->luma_elim_threshold;
  258. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  259. s->strict_std_compliance= avctx->strict_std_compliance;
  260. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  261. s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
  262. s->mpeg_quant= avctx->mpeg_quant;
  263. s->rtp_mode= !!avctx->rtp_payload_size;
  264. s->intra_dc_precision= avctx->intra_dc_precision;
  265. s->user_specified_pts = AV_NOPTS_VALUE;
  266. if (s->gop_size <= 1) {
  267. s->intra_only = 1;
  268. s->gop_size = 12;
  269. } else {
  270. s->intra_only = 0;
  271. }
  272. s->me_method = avctx->me_method;
  273. /* Fixed QSCALE */
  274. s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
  275. s->adaptive_quant= ( s->avctx->lumi_masking
  276. || s->avctx->dark_masking
  277. || s->avctx->temporal_cplx_masking
  278. || s->avctx->spatial_cplx_masking
  279. || s->avctx->p_masking
  280. || s->avctx->border_masking
  281. || (s->flags&CODEC_FLAG_QP_RD))
  282. && !s->fixed_qscale;
  283. s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
  284. s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
  285. s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
  286. s->intra_vlc_format= !!(s->flags2 & CODEC_FLAG2_INTRA_VLC);
  287. s->q_scale_type= !!(s->flags2 & CODEC_FLAG2_NON_LINEAR_QUANT);
  288. if(avctx->rc_max_rate && !avctx->rc_buffer_size){
  289. av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
  290. return -1;
  291. }
  292. if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
  293. av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
  294. }
  295. if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
  296. av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
  297. return -1;
  298. }
  299. if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
  300. av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
  301. return -1;
  302. }
  303. if(avctx->rc_max_rate && avctx->rc_max_rate == avctx->bit_rate && avctx->rc_max_rate != avctx->rc_min_rate){
  304. av_log(avctx, AV_LOG_INFO, "impossible bitrate constraints, this will fail\n");
  305. }
  306. if(avctx->rc_buffer_size && avctx->bit_rate*av_q2d(avctx->time_base) > avctx->rc_buffer_size){
  307. av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
  308. return -1;
  309. }
  310. if(avctx->bit_rate*av_q2d(avctx->time_base) > avctx->bit_rate_tolerance){
  311. av_log(avctx, AV_LOG_ERROR, "bitrate tolerance too small for bitrate\n");
  312. return -1;
  313. }
  314. if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
  315. && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
  316. && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
  317. av_log(avctx, AV_LOG_INFO, "Warning vbv_delay will be set to 0xFFFF (=VBR) as the specified vbv buffer is too large for the given bitrate!\n");
  318. }
  319. if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
  320. && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
  321. av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
  322. return -1;
  323. }
  324. if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
  325. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n");
  326. return -1;
  327. }
  328. if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
  329. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
  330. return -1;
  331. }
  332. if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
  333. av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
  334. return -1;
  335. }
  336. if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
  337. av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
  338. return -1;
  339. }
  340. if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
  341. av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
  342. return -1;
  343. }
  344. if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
  345. && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
  346. av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
  347. return -1;
  348. }
  349. if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
  350. av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n");
  351. return -1;
  352. }
  353. if((s->flags & CODEC_FLAG_CBP_RD) && !avctx->trellis){
  354. av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
  355. return -1;
  356. }
  357. if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
  358. av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
  359. return -1;
  360. }
  361. if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
  362. av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection are not supported yet, set threshold to 1000000000\n");
  363. return -1;
  364. }
  365. if((s->flags2 & CODEC_FLAG2_INTRA_VLC) && s->codec_id != CODEC_ID_MPEG2VIDEO){
  366. av_log(avctx, AV_LOG_ERROR, "intra vlc table not supported by codec\n");
  367. return -1;
  368. }
  369. if(s->flags & CODEC_FLAG_LOW_DELAY){
  370. if (s->codec_id != CODEC_ID_MPEG2VIDEO){
  371. av_log(avctx, AV_LOG_ERROR, "low delay forcing is only available for mpeg2\n");
  372. return -1;
  373. }
  374. if (s->max_b_frames != 0){
  375. av_log(avctx, AV_LOG_ERROR, "b frames cannot be used with low delay\n");
  376. return -1;
  377. }
  378. }
  379. if(s->q_scale_type == 1){
  380. if(s->codec_id != CODEC_ID_MPEG2VIDEO){
  381. av_log(avctx, AV_LOG_ERROR, "non linear quant is only available for mpeg2\n");
  382. return -1;
  383. }
  384. if(avctx->qmax > 12){
  385. av_log(avctx, AV_LOG_ERROR, "non linear quant only supports qmax <= 12 currently\n");
  386. return -1;
  387. }
  388. }
  389. if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
  390. && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
  391. && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
  392. av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
  393. return -1;
  394. }
  395. if(s->avctx->thread_count < 1){
  396. av_log(avctx, AV_LOG_ERROR, "automatic thread number detection not supported by codec, patch welcome\n");
  397. return -1;
  398. }
  399. if(s->avctx->thread_count > 1)
  400. s->rtp_mode= 1;
  401. if(!avctx->time_base.den || !avctx->time_base.num){
  402. av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
  403. return -1;
  404. }
  405. i= (INT_MAX/2+128)>>8;
  406. if(avctx->me_threshold >= i){
  407. av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1);
  408. return -1;
  409. }
  410. if(avctx->mb_threshold >= i){
  411. av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1);
  412. return -1;
  413. }
  414. if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){
  415. av_log(avctx, AV_LOG_INFO, "notice: b_frame_strategy only affects the first pass\n");
  416. avctx->b_frame_strategy = 0;
  417. }
  418. i= av_gcd(avctx->time_base.den, avctx->time_base.num);
  419. if(i > 1){
  420. av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
  421. avctx->time_base.den /= i;
  422. avctx->time_base.num /= i;
  423. // return -1;
  424. }
  425. if(s->codec_id==CODEC_ID_MJPEG){
  426. s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
  427. s->inter_quant_bias= 0;
  428. }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
  429. s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
  430. s->inter_quant_bias= 0;
  431. }else{
  432. s->intra_quant_bias=0;
  433. s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
  434. }
  435. if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
  436. s->intra_quant_bias= avctx->intra_quant_bias;
  437. if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
  438. s->inter_quant_bias= avctx->inter_quant_bias;
  439. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
  440. if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){
  441. av_log(avctx, AV_LOG_ERROR, "timebase not supported by mpeg 4 standard\n");
  442. return -1;
  443. }
  444. s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
  445. switch(avctx->codec->id) {
  446. case CODEC_ID_MPEG1VIDEO:
  447. s->out_format = FMT_MPEG1;
  448. s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
  449. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  450. break;
  451. case CODEC_ID_MPEG2VIDEO:
  452. s->out_format = FMT_MPEG1;
  453. s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
  454. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  455. s->rtp_mode= 1;
  456. break;
  457. case CODEC_ID_LJPEG:
  458. case CODEC_ID_MJPEG:
  459. s->out_format = FMT_MJPEG;
  460. s->intra_only = 1; /* force intra only for jpeg */
  461. if(avctx->codec->id == CODEC_ID_LJPEG && avctx->pix_fmt == PIX_FMT_BGRA){
  462. s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
  463. s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
  464. s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
  465. }else{
  466. s->mjpeg_vsample[0] = 2;
  467. s->mjpeg_vsample[1] = 2>>chroma_v_shift;
  468. s->mjpeg_vsample[2] = 2>>chroma_v_shift;
  469. s->mjpeg_hsample[0] = 2;
  470. s->mjpeg_hsample[1] = 2>>chroma_h_shift;
  471. s->mjpeg_hsample[2] = 2>>chroma_h_shift;
  472. }
  473. if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER)
  474. || ff_mjpeg_encode_init(s) < 0)
  475. return -1;
  476. avctx->delay=0;
  477. s->low_delay=1;
  478. break;
  479. case CODEC_ID_H261:
  480. if (!CONFIG_H261_ENCODER) return -1;
  481. if (ff_h261_get_picture_format(s->width, s->height) < 0) {
  482. av_log(avctx, AV_LOG_ERROR, "The specified picture size of %dx%d is not valid for the H.261 codec.\nValid sizes are 176x144, 352x288\n", s->width, s->height);
  483. return -1;
  484. }
  485. s->out_format = FMT_H261;
  486. avctx->delay=0;
  487. s->low_delay=1;
  488. break;
  489. case CODEC_ID_H263:
  490. if (!CONFIG_H263_ENCODER) return -1;
  491. if (h263_get_picture_format(s->width, s->height) == 7) {
  492. av_log(avctx, AV_LOG_INFO, "The specified picture size of %dx%d is not valid for the H.263 codec.\nValid sizes are 128x96, 176x144, 352x288, 704x576, and 1408x1152. Try H.263+.\n", s->width, s->height);
  493. return -1;
  494. }
  495. s->out_format = FMT_H263;
  496. s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
  497. avctx->delay=0;
  498. s->low_delay=1;
  499. break;
  500. case CODEC_ID_H263P:
  501. s->out_format = FMT_H263;
  502. s->h263_plus = 1;
  503. /* Fx */
  504. s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
  505. s->h263_aic= (avctx->flags & CODEC_FLAG_AC_PRED) ? 1:0;
  506. s->modified_quant= s->h263_aic;
  507. s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
  508. s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
  509. s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
  510. s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
  511. s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
  512. /* /Fx */
  513. /* These are just to be sure */
  514. avctx->delay=0;
  515. s->low_delay=1;
  516. break;
  517. case CODEC_ID_FLV1:
  518. s->out_format = FMT_H263;
  519. s->h263_flv = 2; /* format = 1; 11-bit codes */
  520. s->unrestricted_mv = 1;
  521. s->rtp_mode=0; /* don't allow GOB */
  522. avctx->delay=0;
  523. s->low_delay=1;
  524. break;
  525. case CODEC_ID_RV10:
  526. s->out_format = FMT_H263;
  527. avctx->delay=0;
  528. s->low_delay=1;
  529. break;
  530. case CODEC_ID_RV20:
  531. s->out_format = FMT_H263;
  532. avctx->delay=0;
  533. s->low_delay=1;
  534. s->modified_quant=1;
  535. s->h263_aic=1;
  536. s->h263_plus=1;
  537. s->loop_filter=1;
  538. s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
  539. break;
  540. case CODEC_ID_MPEG4:
  541. s->out_format = FMT_H263;
  542. s->h263_pred = 1;
  543. s->unrestricted_mv = 1;
  544. s->low_delay= s->max_b_frames ? 0 : 1;
  545. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  546. break;
  547. case CODEC_ID_MSMPEG4V1:
  548. s->out_format = FMT_H263;
  549. s->h263_msmpeg4 = 1;
  550. s->h263_pred = 1;
  551. s->unrestricted_mv = 1;
  552. s->msmpeg4_version= 1;
  553. avctx->delay=0;
  554. s->low_delay=1;
  555. break;
  556. case CODEC_ID_MSMPEG4V2:
  557. s->out_format = FMT_H263;
  558. s->h263_msmpeg4 = 1;
  559. s->h263_pred = 1;
  560. s->unrestricted_mv = 1;
  561. s->msmpeg4_version= 2;
  562. avctx->delay=0;
  563. s->low_delay=1;
  564. break;
  565. case CODEC_ID_MSMPEG4V3:
  566. s->out_format = FMT_H263;
  567. s->h263_msmpeg4 = 1;
  568. s->h263_pred = 1;
  569. s->unrestricted_mv = 1;
  570. s->msmpeg4_version= 3;
  571. s->flipflop_rounding=1;
  572. avctx->delay=0;
  573. s->low_delay=1;
  574. break;
  575. case CODEC_ID_WMV1:
  576. s->out_format = FMT_H263;
  577. s->h263_msmpeg4 = 1;
  578. s->h263_pred = 1;
  579. s->unrestricted_mv = 1;
  580. s->msmpeg4_version= 4;
  581. s->flipflop_rounding=1;
  582. avctx->delay=0;
  583. s->low_delay=1;
  584. break;
  585. case CODEC_ID_WMV2:
  586. s->out_format = FMT_H263;
  587. s->h263_msmpeg4 = 1;
  588. s->h263_pred = 1;
  589. s->unrestricted_mv = 1;
  590. s->msmpeg4_version= 5;
  591. s->flipflop_rounding=1;
  592. avctx->delay=0;
  593. s->low_delay=1;
  594. break;
  595. default:
  596. return -1;
  597. }
  598. avctx->has_b_frames= !s->low_delay;
  599. s->encoding = 1;
  600. s->progressive_frame=
  601. s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN));
  602. /* init */
  603. if (MPV_common_init(s) < 0)
  604. return -1;
  605. if(!s->dct_quantize)
  606. s->dct_quantize = dct_quantize_c;
  607. if(!s->denoise_dct)
  608. s->denoise_dct = denoise_dct_c;
  609. s->fast_dct_quantize = s->dct_quantize;
  610. if(avctx->trellis)
  611. s->dct_quantize = dct_quantize_trellis_c;
  612. if((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
  613. s->chroma_qscale_table= ff_h263_chroma_qscale_table;
  614. s->quant_precision=5;
  615. ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
  616. ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
  617. if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
  618. ff_h261_encode_init(s);
  619. if (CONFIG_ANY_H263_ENCODER && s->out_format == FMT_H263)
  620. h263_encode_init(s);
  621. if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
  622. ff_msmpeg4_encode_init(s);
  623. if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  624. && s->out_format == FMT_MPEG1)
  625. ff_mpeg1_encode_init(s);
  626. /* init q matrix */
  627. for(i=0;i<64;i++) {
  628. int j= s->dsp.idct_permutation[i];
  629. if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
  630. s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
  631. s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
  632. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  633. s->intra_matrix[j] =
  634. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  635. }else
  636. { /* mpeg1/2 */
  637. s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
  638. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  639. }
  640. if(s->avctx->intra_matrix)
  641. s->intra_matrix[j] = s->avctx->intra_matrix[i];
  642. if(s->avctx->inter_matrix)
  643. s->inter_matrix[j] = s->avctx->inter_matrix[i];
  644. }
  645. /* precompute matrix */
  646. /* for mjpeg, we do include qscale in the matrix */
  647. if (s->out_format != FMT_MJPEG) {
  648. ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  649. s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1);
  650. ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
  651. s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0);
  652. }
  653. if(ff_rate_control_init(s) < 0)
  654. return -1;
  655. return 0;
  656. }
  657. av_cold int MPV_encode_end(AVCodecContext *avctx)
  658. {
  659. MpegEncContext *s = avctx->priv_data;
  660. ff_rate_control_uninit(s);
  661. MPV_common_end(s);
  662. if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) && s->out_format == FMT_MJPEG)
  663. ff_mjpeg_encode_close(s);
  664. av_freep(&avctx->extradata);
  665. return 0;
  666. }
  667. static int get_sae(uint8_t *src, int ref, int stride){
  668. int x,y;
  669. int acc=0;
  670. for(y=0; y<16; y++){
  671. for(x=0; x<16; x++){
  672. acc+= FFABS(src[x+y*stride] - ref);
  673. }
  674. }
  675. return acc;
  676. }
  677. static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
  678. int x, y, w, h;
  679. int acc=0;
  680. w= s->width &~15;
  681. h= s->height&~15;
  682. for(y=0; y<h; y+=16){
  683. for(x=0; x<w; x+=16){
  684. int offset= x + y*stride;
  685. int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
  686. int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
  687. int sae = get_sae(src + offset, mean, stride);
  688. acc+= sae + 500 < sad;
  689. }
  690. }
  691. return acc;
  692. }
  693. static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
  694. AVFrame *pic=NULL;
  695. int64_t pts;
  696. int i;
  697. const int encoding_delay= s->max_b_frames;
  698. int direct=1;
  699. if(pic_arg){
  700. pts= pic_arg->pts;
  701. pic_arg->display_picture_number= s->input_picture_number++;
  702. if(pts != AV_NOPTS_VALUE){
  703. if(s->user_specified_pts != AV_NOPTS_VALUE){
  704. int64_t time= pts;
  705. int64_t last= s->user_specified_pts;
  706. if(time <= last){
  707. av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%"PRId64", last=%"PRId64"\n", pts, s->user_specified_pts);
  708. return -1;
  709. }
  710. }
  711. s->user_specified_pts= pts;
  712. }else{
  713. if(s->user_specified_pts != AV_NOPTS_VALUE){
  714. s->user_specified_pts=
  715. pts= s->user_specified_pts + 1;
  716. av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n", pts);
  717. }else{
  718. pts= pic_arg->display_picture_number;
  719. }
  720. }
  721. }
  722. if(pic_arg){
  723. if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
  724. if(pic_arg->linesize[0] != s->linesize) direct=0;
  725. if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
  726. if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  727. // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
  728. if(direct){
  729. i= ff_find_unused_picture(s, 1);
  730. pic= (AVFrame*)&s->picture[i];
  731. pic->reference= 3;
  732. for(i=0; i<4; i++){
  733. pic->data[i]= pic_arg->data[i];
  734. pic->linesize[i]= pic_arg->linesize[i];
  735. }
  736. ff_alloc_picture(s, (Picture*)pic, 1);
  737. }else{
  738. i= ff_find_unused_picture(s, 0);
  739. pic= (AVFrame*)&s->picture[i];
  740. pic->reference= 3;
  741. ff_alloc_picture(s, (Picture*)pic, 0);
  742. if( pic->data[0] + INPLACE_OFFSET == pic_arg->data[0]
  743. && pic->data[1] + INPLACE_OFFSET == pic_arg->data[1]
  744. && pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]){
  745. // empty
  746. }else{
  747. int h_chroma_shift, v_chroma_shift;
  748. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  749. for(i=0; i<3; i++){
  750. int src_stride= pic_arg->linesize[i];
  751. int dst_stride= i ? s->uvlinesize : s->linesize;
  752. int h_shift= i ? h_chroma_shift : 0;
  753. int v_shift= i ? v_chroma_shift : 0;
  754. int w= s->width >>h_shift;
  755. int h= s->height>>v_shift;
  756. uint8_t *src= pic_arg->data[i];
  757. uint8_t *dst= pic->data[i];
  758. if(!s->avctx->rc_buffer_size)
  759. dst +=INPLACE_OFFSET;
  760. if(src_stride==dst_stride)
  761. memcpy(dst, src, src_stride*h);
  762. else{
  763. while(h--){
  764. memcpy(dst, src, w);
  765. dst += dst_stride;
  766. src += src_stride;
  767. }
  768. }
  769. }
  770. }
  771. }
  772. copy_picture_attributes(s, pic, pic_arg);
  773. pic->pts= pts; //we set this here to avoid modifiying pic_arg
  774. }
  775. /* shift buffer entries */
  776. for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
  777. s->input_picture[i-1]= s->input_picture[i];
  778. s->input_picture[encoding_delay]= (Picture*)pic;
  779. return 0;
  780. }
  781. static int skip_check(MpegEncContext *s, Picture *p, Picture *ref){
  782. int x, y, plane;
  783. int score=0;
  784. int64_t score64=0;
  785. for(plane=0; plane<3; plane++){
  786. const int stride= p->linesize[plane];
  787. const int bw= plane ? 1 : 2;
  788. for(y=0; y<s->mb_height*bw; y++){
  789. for(x=0; x<s->mb_width*bw; x++){
  790. int off= p->type == FF_BUFFER_TYPE_SHARED ? 0: 16;
  791. int v= s->dsp.frame_skip_cmp[1](s, p->data[plane] + 8*(x + y*stride)+off, ref->data[plane] + 8*(x + y*stride), stride, 8);
  792. switch(s->avctx->frame_skip_exp){
  793. case 0: score= FFMAX(score, v); break;
  794. case 1: score+= FFABS(v);break;
  795. case 2: score+= v*v;break;
  796. case 3: score64+= FFABS(v*v*(int64_t)v);break;
  797. case 4: score64+= v*v*(int64_t)(v*v);break;
  798. }
  799. }
  800. }
  801. }
  802. if(score) score64= score;
  803. if(score64 < s->avctx->frame_skip_threshold)
  804. return 1;
  805. if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8))
  806. return 1;
  807. return 0;
  808. }
  809. static int estimate_best_b_count(MpegEncContext *s){
  810. AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id);
  811. AVCodecContext *c= avcodec_alloc_context();
  812. AVFrame input[FF_MAX_B_FRAMES+2];
  813. const int scale= s->avctx->brd_scale;
  814. int i, j, out_size, p_lambda, b_lambda, lambda2;
  815. int outbuf_size= s->width * s->height; //FIXME
  816. uint8_t *outbuf= av_malloc(outbuf_size);
  817. int64_t best_rd= INT64_MAX;
  818. int best_b_count= -1;
  819. assert(scale>=0 && scale <=3);
  820. // emms_c();
  821. p_lambda= s->last_lambda_for[FF_P_TYPE]; //s->next_picture_ptr->quality;
  822. b_lambda= s->last_lambda_for[FF_B_TYPE]; //p_lambda *FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
  823. if(!b_lambda) b_lambda= p_lambda; //FIXME we should do this somewhere else
  824. lambda2= (b_lambda*b_lambda + (1<<FF_LAMBDA_SHIFT)/2 ) >> FF_LAMBDA_SHIFT;
  825. c->width = s->width >> scale;
  826. c->height= s->height>> scale;
  827. c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
  828. c->flags|= s->avctx->flags & CODEC_FLAG_QPEL;
  829. c->mb_decision= s->avctx->mb_decision;
  830. c->me_cmp= s->avctx->me_cmp;
  831. c->mb_cmp= s->avctx->mb_cmp;
  832. c->me_sub_cmp= s->avctx->me_sub_cmp;
  833. c->pix_fmt = PIX_FMT_YUV420P;
  834. c->time_base= s->avctx->time_base;
  835. c->max_b_frames= s->max_b_frames;
  836. if (avcodec_open(c, codec) < 0)
  837. return -1;
  838. for(i=0; i<s->max_b_frames+2; i++){
  839. int ysize= c->width*c->height;
  840. int csize= (c->width/2)*(c->height/2);
  841. Picture pre_input, *pre_input_ptr= i ? s->input_picture[i-1] : s->next_picture_ptr;
  842. avcodec_get_frame_defaults(&input[i]);
  843. input[i].data[0]= av_malloc(ysize + 2*csize);
  844. input[i].data[1]= input[i].data[0] + ysize;
  845. input[i].data[2]= input[i].data[1] + csize;
  846. input[i].linesize[0]= c->width;
  847. input[i].linesize[1]=
  848. input[i].linesize[2]= c->width/2;
  849. if(pre_input_ptr && (!i || s->input_picture[i-1])) {
  850. pre_input= *pre_input_ptr;
  851. if(pre_input.type != FF_BUFFER_TYPE_SHARED && i) {
  852. pre_input.data[0]+=INPLACE_OFFSET;
  853. pre_input.data[1]+=INPLACE_OFFSET;
  854. pre_input.data[2]+=INPLACE_OFFSET;
  855. }
  856. s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0], pre_input.data[0], pre_input.linesize[0], c->width, c->height);
  857. s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1], pre_input.data[1], pre_input.linesize[1], c->width>>1, c->height>>1);
  858. s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2], pre_input.data[2], pre_input.linesize[2], c->width>>1, c->height>>1);
  859. }
  860. }
  861. for(j=0; j<s->max_b_frames+1; j++){
  862. int64_t rd=0;
  863. if(!s->input_picture[j])
  864. break;
  865. c->error[0]= c->error[1]= c->error[2]= 0;
  866. input[0].pict_type= FF_I_TYPE;
  867. input[0].quality= 1 * FF_QP2LAMBDA;
  868. out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]);
  869. // rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
  870. for(i=0; i<s->max_b_frames+1; i++){
  871. int is_p= i % (j+1) == j || i==s->max_b_frames;
  872. input[i+1].pict_type= is_p ? FF_P_TYPE : FF_B_TYPE;
  873. input[i+1].quality= is_p ? p_lambda : b_lambda;
  874. out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]);
  875. rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
  876. }
  877. /* get the delayed frames */
  878. while(out_size){
  879. out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
  880. rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
  881. }
  882. rd += c->error[0] + c->error[1] + c->error[2];
  883. if(rd < best_rd){
  884. best_rd= rd;
  885. best_b_count= j;
  886. }
  887. }
  888. av_freep(&outbuf);
  889. avcodec_close(c);
  890. av_freep(&c);
  891. for(i=0; i<s->max_b_frames+2; i++){
  892. av_freep(&input[i].data[0]);
  893. }
  894. return best_b_count;
  895. }
  896. static void select_input_picture(MpegEncContext *s){
  897. int i;
  898. for(i=1; i<MAX_PICTURE_COUNT; i++)
  899. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  900. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  901. /* set next picture type & ordering */
  902. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  903. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
  904. s->reordered_input_picture[0]= s->input_picture[0];
  905. s->reordered_input_picture[0]->pict_type= FF_I_TYPE;
  906. s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
  907. }else{
  908. int b_frames;
  909. if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){
  910. if(s->picture_in_gop_number < s->gop_size && skip_check(s, s->input_picture[0], s->next_picture_ptr)){
  911. //FIXME check that te gop check above is +-1 correct
  912. //av_log(NULL, AV_LOG_DEBUG, "skip %p %"PRId64"\n", s->input_picture[0]->data[0], s->input_picture[0]->pts);
  913. if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  914. for(i=0; i<4; i++)
  915. s->input_picture[0]->data[i]= NULL;
  916. s->input_picture[0]->type= 0;
  917. }else{
  918. assert( s->input_picture[0]->type==FF_BUFFER_TYPE_USER
  919. || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  920. s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]);
  921. }
  922. emms_c();
  923. ff_vbv_update(s, 0);
  924. goto no_output_pic;
  925. }
  926. }
  927. if(s->flags&CODEC_FLAG_PASS2){
  928. for(i=0; i<s->max_b_frames+1; i++){
  929. int pict_num= s->input_picture[0]->display_picture_number + i;
  930. if(pict_num >= s->rc_context.num_entries)
  931. break;
  932. if(!s->input_picture[i]){
  933. s->rc_context.entry[pict_num-1].new_pict_type = FF_P_TYPE;
  934. break;
  935. }
  936. s->input_picture[i]->pict_type=
  937. s->rc_context.entry[pict_num].new_pict_type;
  938. }
  939. }
  940. if(s->avctx->b_frame_strategy==0){
  941. b_frames= s->max_b_frames;
  942. while(b_frames && !s->input_picture[b_frames]) b_frames--;
  943. }else if(s->avctx->b_frame_strategy==1){
  944. for(i=1; i<s->max_b_frames+1; i++){
  945. if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
  946. s->input_picture[i]->b_frame_score=
  947. get_intra_count(s, s->input_picture[i ]->data[0],
  948. s->input_picture[i-1]->data[0], s->linesize) + 1;
  949. }
  950. }
  951. for(i=0; i<s->max_b_frames+1; i++){
  952. if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/s->avctx->b_sensitivity) break;
  953. }
  954. b_frames= FFMAX(0, i-1);
  955. /* reset scores */
  956. for(i=0; i<b_frames+1; i++){
  957. s->input_picture[i]->b_frame_score=0;
  958. }
  959. }else if(s->avctx->b_frame_strategy==2){
  960. b_frames= estimate_best_b_count(s);
  961. }else{
  962. av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
  963. b_frames=0;
  964. }
  965. emms_c();
  966. //static int b_count=0;
  967. //b_count+= b_frames;
  968. //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
  969. for(i= b_frames - 1; i>=0; i--){
  970. int type= s->input_picture[i]->pict_type;
  971. if(type && type != FF_B_TYPE)
  972. b_frames= i;
  973. }
  974. if(s->input_picture[b_frames]->pict_type == FF_B_TYPE && b_frames == s->max_b_frames){
  975. av_log(s->avctx, AV_LOG_ERROR, "warning, too many b frames in a row\n");
  976. }
  977. if(s->picture_in_gop_number + b_frames >= s->gop_size){
  978. if((s->flags2 & CODEC_FLAG2_STRICT_GOP) && s->gop_size > s->picture_in_gop_number){
  979. b_frames= s->gop_size - s->picture_in_gop_number - 1;
  980. }else{
  981. if(s->flags & CODEC_FLAG_CLOSED_GOP)
  982. b_frames=0;
  983. s->input_picture[b_frames]->pict_type= FF_I_TYPE;
  984. }
  985. }
  986. if( (s->flags & CODEC_FLAG_CLOSED_GOP)
  987. && b_frames
  988. && s->input_picture[b_frames]->pict_type== FF_I_TYPE)
  989. b_frames--;
  990. s->reordered_input_picture[0]= s->input_picture[b_frames];
  991. if(s->reordered_input_picture[0]->pict_type != FF_I_TYPE)
  992. s->reordered_input_picture[0]->pict_type= FF_P_TYPE;
  993. s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
  994. for(i=0; i<b_frames; i++){
  995. s->reordered_input_picture[i+1]= s->input_picture[i];
  996. s->reordered_input_picture[i+1]->pict_type= FF_B_TYPE;
  997. s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
  998. }
  999. }
  1000. }
  1001. no_output_pic:
  1002. if(s->reordered_input_picture[0]){
  1003. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=FF_B_TYPE ? 3 : 0;
  1004. ff_copy_picture(&s->new_picture, s->reordered_input_picture[0]);
  1005. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED || s->avctx->rc_buffer_size){
  1006. // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable
  1007. int i= ff_find_unused_picture(s, 0);
  1008. Picture *pic= &s->picture[i];
  1009. pic->reference = s->reordered_input_picture[0]->reference;
  1010. ff_alloc_picture(s, pic, 0);
  1011. /* mark us unused / free shared pic */
  1012. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_INTERNAL)
  1013. s->avctx->release_buffer(s->avctx, (AVFrame*)s->reordered_input_picture[0]);
  1014. for(i=0; i<4; i++)
  1015. s->reordered_input_picture[0]->data[i]= NULL;
  1016. s->reordered_input_picture[0]->type= 0;
  1017. copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
  1018. s->current_picture_ptr= pic;
  1019. }else{
  1020. // input is not a shared pix -> reuse buffer for current_pix
  1021. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1022. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1023. s->current_picture_ptr= s->reordered_input_picture[0];
  1024. for(i=0; i<4; i++){
  1025. s->new_picture.data[i]+= INPLACE_OFFSET;
  1026. }
  1027. }
  1028. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  1029. s->picture_number= s->new_picture.display_picture_number;
  1030. //printf("dpn:%d\n", s->picture_number);
  1031. }else{
  1032. memset(&s->new_picture, 0, sizeof(Picture));
  1033. }
  1034. }
  1035. int MPV_encode_picture(AVCodecContext *avctx,
  1036. unsigned char *buf, int buf_size, void *data)
  1037. {
  1038. MpegEncContext *s = avctx->priv_data;
  1039. AVFrame *pic_arg = data;
  1040. int i, stuffing_count;
  1041. for(i=0; i<avctx->thread_count; i++){
  1042. int start_y= s->thread_context[i]->start_mb_y;
  1043. int end_y= s->thread_context[i]-> end_mb_y;
  1044. int h= s->mb_height;
  1045. uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h);
  1046. uint8_t *end = buf + (size_t)(((int64_t) buf_size)* end_y/h);
  1047. init_put_bits(&s->thread_context[i]->pb, start, end - start);
  1048. }
  1049. s->picture_in_gop_number++;
  1050. if(load_input_picture(s, pic_arg) < 0)
  1051. return -1;
  1052. select_input_picture(s);
  1053. /* output? */
  1054. if(s->new_picture.data[0]){
  1055. s->pict_type= s->new_picture.pict_type;
  1056. //emms_c();
  1057. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1058. MPV_frame_start(s, avctx);
  1059. vbv_retry:
  1060. if (encode_picture(s, s->picture_number) < 0)
  1061. return -1;
  1062. avctx->header_bits = s->header_bits;
  1063. avctx->mv_bits = s->mv_bits;
  1064. avctx->misc_bits = s->misc_bits;
  1065. avctx->i_tex_bits = s->i_tex_bits;
  1066. avctx->p_tex_bits = s->p_tex_bits;
  1067. avctx->i_count = s->i_count;
  1068. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  1069. avctx->skip_count = s->skip_count;
  1070. MPV_frame_end(s);
  1071. if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
  1072. ff_mjpeg_encode_picture_trailer(s);
  1073. if(avctx->rc_buffer_size){
  1074. RateControlContext *rcc= &s->rc_context;
  1075. int max_size= rcc->buffer_index * avctx->rc_max_available_vbv_use;
  1076. if(put_bits_count(&s->pb) > max_size && s->lambda < s->avctx->lmax){
  1077. s->next_lambda= FFMAX(s->lambda+1, s->lambda*(s->qscale+1) / s->qscale);
  1078. if(s->adaptive_quant){
  1079. int i;
  1080. for(i=0; i<s->mb_height*s->mb_stride; i++)
  1081. s->lambda_table[i]= FFMAX(s->lambda_table[i]+1, s->lambda_table[i]*(s->qscale+1) / s->qscale);
  1082. }
  1083. s->mb_skipped = 0; //done in MPV_frame_start()
  1084. if(s->pict_type==FF_P_TYPE){ //done in encode_picture() so we must undo it
  1085. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  1086. s->no_rounding ^= 1;
  1087. }
  1088. if(s->pict_type!=FF_B_TYPE){
  1089. s->time_base= s->last_time_base;
  1090. s->last_non_b_time= s->time - s->pp_time;
  1091. }
  1092. // av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda);
  1093. for(i=0; i<avctx->thread_count; i++){
  1094. PutBitContext *pb= &s->thread_context[i]->pb;
  1095. init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
  1096. }
  1097. goto vbv_retry;
  1098. }
  1099. assert(s->avctx->rc_max_rate);
  1100. }
  1101. if(s->flags&CODEC_FLAG_PASS1)
  1102. ff_write_pass1_stats(s);
  1103. for(i=0; i<4; i++){
  1104. s->current_picture_ptr->error[i]= s->current_picture.error[i];
  1105. avctx->error[i] += s->current_picture_ptr->error[i];
  1106. }
  1107. if(s->flags&CODEC_FLAG_PASS1)
  1108. assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits + avctx->i_tex_bits + avctx->p_tex_bits == put_bits_count(&s->pb));
  1109. flush_put_bits(&s->pb);
  1110. s->frame_bits = put_bits_count(&s->pb);
  1111. stuffing_count= ff_vbv_update(s, s->frame_bits);
  1112. if(stuffing_count){
  1113. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < stuffing_count + 50){
  1114. av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
  1115. return -1;
  1116. }
  1117. switch(s->codec_id){
  1118. case CODEC_ID_MPEG1VIDEO:
  1119. case CODEC_ID_MPEG2VIDEO:
  1120. while(stuffing_count--){
  1121. put_bits(&s->pb, 8, 0);
  1122. }
  1123. break;
  1124. case CODEC_ID_MPEG4:
  1125. put_bits(&s->pb, 16, 0);
  1126. put_bits(&s->pb, 16, 0x1C3);
  1127. stuffing_count -= 4;
  1128. while(stuffing_count--){
  1129. put_bits(&s->pb, 8, 0xFF);
  1130. }
  1131. break;
  1132. default:
  1133. av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  1134. }
  1135. flush_put_bits(&s->pb);
  1136. s->frame_bits = put_bits_count(&s->pb);
  1137. }
  1138. /* update mpeg1/2 vbv_delay for CBR */
  1139. if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
  1140. && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
  1141. int vbv_delay, min_delay;
  1142. double inbits = s->avctx->rc_max_rate*av_q2d(s->avctx->time_base);
  1143. int minbits= s->frame_bits - 8*(s->vbv_delay_ptr - s->pb.buf - 1);
  1144. double bits = s->rc_context.buffer_index + minbits - inbits;
  1145. if(bits<0)
  1146. av_log(s->avctx, AV_LOG_ERROR, "Internal error, negative bits\n");
  1147. assert(s->repeat_first_field==0);
  1148. vbv_delay= bits * 90000 / s->avctx->rc_max_rate;
  1149. min_delay= (minbits * 90000LL + s->avctx->rc_max_rate - 1)/ s->avctx->rc_max_rate;
  1150. vbv_delay= FFMAX(vbv_delay, min_delay);
  1151. assert(vbv_delay < 0xFFFF);
  1152. s->vbv_delay_ptr[0] &= 0xF8;
  1153. s->vbv_delay_ptr[0] |= vbv_delay>>13;
  1154. s->vbv_delay_ptr[1] = vbv_delay>>5;
  1155. s->vbv_delay_ptr[2] &= 0x07;
  1156. s->vbv_delay_ptr[2] |= vbv_delay<<3;
  1157. }
  1158. s->total_bits += s->frame_bits;
  1159. avctx->frame_bits = s->frame_bits;
  1160. }else{
  1161. assert((put_bits_ptr(&s->pb) == s->pb.buf));
  1162. s->frame_bits=0;
  1163. }
  1164. assert((s->frame_bits&7)==0);
  1165. return s->frame_bits/8;
  1166. }
  1167. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  1168. {
  1169. static const char tab[64]=
  1170. {3,2,2,1,1,1,1,1,
  1171. 1,1,1,1,1,1,1,1,
  1172. 1,1,1,1,1,1,1,1,
  1173. 0,0,0,0,0,0,0,0,
  1174. 0,0,0,0,0,0,0,0,
  1175. 0,0,0,0,0,0,0,0,
  1176. 0,0,0,0,0,0,0,0,
  1177. 0,0,0,0,0,0,0,0};
  1178. int score=0;
  1179. int run=0;
  1180. int i;
  1181. DCTELEM *block= s->block[n];
  1182. const int last_index= s->block_last_index[n];
  1183. int skip_dc;
  1184. if(threshold<0){
  1185. skip_dc=0;
  1186. threshold= -threshold;
  1187. }else
  1188. skip_dc=1;
  1189. /* Are all we could set to zero already zero? */
  1190. if(last_index<=skip_dc - 1) return;
  1191. for(i=0; i<=last_index; i++){
  1192. const int j = s->intra_scantable.permutated[i];
  1193. const int level = FFABS(block[j]);
  1194. if(level==1){
  1195. if(skip_dc && i==0) continue;
  1196. score+= tab[run];
  1197. run=0;
  1198. }else if(level>1){
  1199. return;
  1200. }else{
  1201. run++;
  1202. }
  1203. }
  1204. if(score >= threshold) return;
  1205. for(i=skip_dc; i<=last_index; i++){
  1206. const int j = s->intra_scantable.permutated[i];
  1207. block[j]=0;
  1208. }
  1209. if(block[0]) s->block_last_index[n]= 0;
  1210. else s->block_last_index[n]= -1;
  1211. }
  1212. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1213. {
  1214. int i;
  1215. const int maxlevel= s->max_qcoeff;
  1216. const int minlevel= s->min_qcoeff;
  1217. int overflow=0;
  1218. if(s->mb_intra){
  1219. i=1; //skip clipping of intra dc
  1220. }else
  1221. i=0;
  1222. for(;i<=last_index; i++){
  1223. const int j= s->intra_scantable.permutated[i];
  1224. int level = block[j];
  1225. if (level>maxlevel){
  1226. level=maxlevel;
  1227. overflow++;
  1228. }else if(level<minlevel){
  1229. level=minlevel;
  1230. overflow++;
  1231. }
  1232. block[j]= level;
  1233. }
  1234. if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  1235. av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel);
  1236. }
  1237. static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride){
  1238. int x, y;
  1239. //FIXME optimize
  1240. for(y=0; y<8; y++){
  1241. for(x=0; x<8; x++){
  1242. int x2, y2;
  1243. int sum=0;
  1244. int sqr=0;
  1245. int count=0;
  1246. for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
  1247. for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
  1248. int v= ptr[x2 + y2*stride];
  1249. sum += v;
  1250. sqr += v*v;
  1251. count++;
  1252. }
  1253. }
  1254. weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
  1255. }
  1256. }
  1257. }
  1258. static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count)
  1259. {
  1260. int16_t weight[8][64];
  1261. DCTELEM orig[8][64];
  1262. const int mb_x= s->mb_x;
  1263. const int mb_y= s->mb_y;
  1264. int i;
  1265. int skip_dct[8];
  1266. int dct_offset = s->linesize*8; //default for progressive frames
  1267. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1268. int wrap_y, wrap_c;
  1269. for(i=0; i<mb_block_count; i++) skip_dct[i]=s->skipdct;
  1270. if(s->adaptive_quant){
  1271. const int last_qp= s->qscale;
  1272. const int mb_xy= mb_x + mb_y*s->mb_stride;
  1273. s->lambda= s->lambda_table[mb_xy];
  1274. update_qscale(s);
  1275. if(!(s->flags&CODEC_FLAG_QP_RD)){
  1276. s->qscale= s->current_picture_ptr->qscale_table[mb_xy];
  1277. s->dquant= s->qscale - last_qp;
  1278. if(s->out_format==FMT_H263){
  1279. s->dquant= av_clip(s->dquant, -2, 2);
  1280. if(s->codec_id==CODEC_ID_MPEG4){
  1281. if(!s->mb_intra){
  1282. if(s->pict_type == FF_B_TYPE){
  1283. if(s->dquant&1 || s->mv_dir&MV_DIRECT)
  1284. s->dquant= 0;
  1285. }
  1286. if(s->mv_type==MV_TYPE_8X8)
  1287. s->dquant=0;
  1288. }
  1289. }
  1290. }
  1291. }
  1292. ff_set_qscale(s, last_qp + s->dquant);
  1293. }else if(s->flags&CODEC_FLAG_QP_RD)
  1294. ff_set_qscale(s, s->qscale + s->dquant);
  1295. wrap_y = s->linesize;
  1296. wrap_c = s->uvlinesize;
  1297. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  1298. ptr_cb = s->new_picture.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
  1299. ptr_cr = s->new_picture.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
  1300. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  1301. uint8_t *ebuf= s->edge_emu_buffer + 32;
  1302. ff_emulated_edge_mc(ebuf , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width , s->height);
  1303. ptr_y= ebuf;
  1304. ff_emulated_edge_mc(ebuf+18*wrap_y , ptr_cb, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  1305. ptr_cb= ebuf+18*wrap_y;
  1306. ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  1307. ptr_cr= ebuf+18*wrap_y+8;
  1308. }
  1309. if (s->mb_intra) {
  1310. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  1311. int progressive_score, interlaced_score;
  1312. s->interlaced_dct=0;
  1313. progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8)
  1314. +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
  1315. if(progressive_score > 0){
  1316. interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8)
  1317. +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8);
  1318. if(progressive_score > interlaced_score){
  1319. s->interlaced_dct=1;
  1320. dct_offset= wrap_y;
  1321. wrap_y<<=1;
  1322. if (s->chroma_format == CHROMA_422)
  1323. wrap_c<<=1;
  1324. }
  1325. }
  1326. }
  1327. s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
  1328. s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
  1329. s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
  1330. s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
  1331. if(s->flags&CODEC_FLAG_GRAY){
  1332. skip_dct[4]= 1;
  1333. skip_dct[5]= 1;
  1334. }else{
  1335. s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
  1336. s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
  1337. if(!s->chroma_y_shift){ /* 422 */
  1338. s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset>>1), wrap_c);
  1339. s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset>>1), wrap_c);
  1340. }
  1341. }
  1342. }else{
  1343. op_pixels_func (*op_pix)[4];
  1344. qpel_mc_func (*op_qpix)[16];
  1345. uint8_t *dest_y, *dest_cb, *dest_cr;
  1346. dest_y = s->dest[0];
  1347. dest_cb = s->dest[1];
  1348. dest_cr = s->dest[2];
  1349. if ((!s->no_rounding) || s->pict_type==FF_B_TYPE){
  1350. op_pix = s->dsp.put_pixels_tab;
  1351. op_qpix= s->dsp.put_qpel_pixels_tab;
  1352. }else{
  1353. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1354. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  1355. }
  1356. if (s->mv_dir & MV_DIR_FORWARD) {
  1357. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1358. op_pix = s->dsp.avg_pixels_tab;
  1359. op_qpix= s->dsp.avg_qpel_pixels_tab;
  1360. }
  1361. if (s->mv_dir & MV_DIR_BACKWARD) {
  1362. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1363. }
  1364. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  1365. int progressive_score, interlaced_score;
  1366. s->interlaced_dct=0;
  1367. progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
  1368. +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
  1369. if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
  1370. if(progressive_score>0){
  1371. interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
  1372. +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
  1373. if(progressive_score > interlaced_score){
  1374. s->interlaced_dct=1;
  1375. dct_offset= wrap_y;
  1376. wrap_y<<=1;
  1377. if (s->chroma_format == CHROMA_422)
  1378. wrap_c<<=1;
  1379. }
  1380. }
  1381. }
  1382. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  1383. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1384. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  1385. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  1386. if(s->flags&CODEC_FLAG_GRAY){
  1387. skip_dct[4]= 1;
  1388. skip_dct[5]= 1;
  1389. }else{
  1390. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1391. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1392. if(!s->chroma_y_shift){ /* 422 */
  1393. s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset>>1), dest_cb + (dct_offset>>1), wrap_c);
  1394. s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset>>1), dest_cr + (dct_offset>>1), wrap_c);
  1395. }
  1396. }
  1397. /* pre quantization */
  1398. if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
  1399. //FIXME optimize
  1400. if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
  1401. if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
  1402. if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
  1403. if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1;
  1404. if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
  1405. if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
  1406. if(!s->chroma_y_shift){ /* 422 */
  1407. if(s->dsp.sad[1](NULL, ptr_cb +(dct_offset>>1), dest_cb +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[6]= 1;
  1408. if(s->dsp.sad[1](NULL, ptr_cr +(dct_offset>>1), dest_cr +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[7]= 1;
  1409. }
  1410. }
  1411. }
  1412. if(s->avctx->quantizer_noise_shaping){
  1413. if(!skip_dct[0]) get_visual_weight(weight[0], ptr_y , wrap_y);
  1414. if(!skip_dct[1]) get_visual_weight(weight[1], ptr_y + 8, wrap_y);
  1415. if(!skip_dct[2]) get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
  1416. if(!skip_dct[3]) get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
  1417. if(!skip_dct[4]) get_visual_weight(weight[4], ptr_cb , wrap_c);
  1418. if(!skip_dct[5]) get_visual_weight(weight[5], ptr_cr , wrap_c);
  1419. if(!s->chroma_y_shift){ /* 422 */
  1420. if(!skip_dct[6]) get_visual_weight(weight[6], ptr_cb + (dct_offset>>1), wrap_c);
  1421. if(!skip_dct[7]) get_visual_weight(weight[7], ptr_cr + (dct_offset>>1), wrap_c);
  1422. }
  1423. memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*mb_block_count);
  1424. }
  1425. /* DCT & quantize */
  1426. assert(s->out_format!=FMT_MJPEG || s->qscale==8);
  1427. {
  1428. for(i=0;i<mb_block_count;i++) {
  1429. if(!skip_dct[i]){
  1430. int overflow;
  1431. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1432. // FIXME we could decide to change to quantizer instead of clipping
  1433. // JS: I don't think that would be a good idea it could lower quality instead
  1434. // of improve it. Just INTRADC clipping deserves changes in quantizer
  1435. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1436. }else
  1437. s->block_last_index[i]= -1;
  1438. }
  1439. if(s->avctx->quantizer_noise_shaping){
  1440. for(i=0;i<mb_block_count;i++) {
  1441. if(!skip_dct[i]){
  1442. s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
  1443. }
  1444. }
  1445. }
  1446. if(s->luma_elim_threshold && !s->mb_intra)
  1447. for(i=0; i<4; i++)
  1448. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  1449. if(s->chroma_elim_threshold && !s->mb_intra)
  1450. for(i=4; i<mb_block_count; i++)
  1451. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  1452. if(s->flags & CODEC_FLAG_CBP_RD){
  1453. for(i=0;i<mb_block_count;i++) {
  1454. if(s->block_last_index[i] == -1)
  1455. s->coded_score[i]= INT_MAX/256;
  1456. }
  1457. }
  1458. }
  1459. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  1460. s->block_last_index[4]=
  1461. s->block_last_index[5]= 0;
  1462. s->block[4][0]=
  1463. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  1464. }
  1465. //non c quantize code returns incorrect block_last_index FIXME
  1466. if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
  1467. for(i=0; i<mb_block_count; i++){
  1468. int j;
  1469. if(s->block_last_index[i]>0){
  1470. for(j=63; j>0; j--){
  1471. if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
  1472. }
  1473. s->block_last_index[i]= j;
  1474. }
  1475. }
  1476. }
  1477. /* huffman encode */
  1478. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  1479. case CODEC_ID_MPEG1VIDEO:
  1480. case CODEC_ID_MPEG2VIDEO:
  1481. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  1482. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1483. break;
  1484. case CODEC_ID_MPEG4:
  1485. if (CONFIG_MPEG4_ENCODER)
  1486. mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1487. break;
  1488. case CODEC_ID_MSMPEG4V2:
  1489. case CODEC_ID_MSMPEG4V3:
  1490. case CODEC_ID_WMV1:
  1491. if (CONFIG_MSMPEG4_ENCODER)
  1492. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1493. break;
  1494. case CODEC_ID_WMV2:
  1495. if (CONFIG_WMV2_ENCODER)
  1496. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
  1497. break;
  1498. case CODEC_ID_H261:
  1499. if (CONFIG_H261_ENCODER)
  1500. ff_h261_encode_mb(s, s->block, motion_x, motion_y);
  1501. break;
  1502. case CODEC_ID_H263:
  1503. case CODEC_ID_H263P:
  1504. case CODEC_ID_FLV1:
  1505. case CODEC_ID_RV10:
  1506. case CODEC_ID_RV20:
  1507. if (CONFIG_H263_ENCODER)
  1508. h263_encode_mb(s, s->block, motion_x, motion_y);
  1509. break;
  1510. case CODEC_ID_MJPEG:
  1511. if (CONFIG_MJPEG_ENCODER)
  1512. ff_mjpeg_encode_mb(s, s->block);
  1513. break;
  1514. default:
  1515. assert(0);
  1516. }
  1517. }
  1518. static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1519. {
  1520. if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
  1521. else encode_mb_internal(s, motion_x, motion_y, 16, 8);
  1522. }
  1523. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1524. int i;
  1525. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1526. /* mpeg1 */
  1527. d->mb_skip_run= s->mb_skip_run;
  1528. for(i=0; i<3; i++)
  1529. d->last_dc[i]= s->last_dc[i];
  1530. /* statistics */
  1531. d->mv_bits= s->mv_bits;
  1532. d->i_tex_bits= s->i_tex_bits;
  1533. d->p_tex_bits= s->p_tex_bits;
  1534. d->i_count= s->i_count;
  1535. d->f_count= s->f_count;
  1536. d->b_count= s->b_count;
  1537. d->skip_count= s->skip_count;
  1538. d->misc_bits= s->misc_bits;
  1539. d->last_bits= 0;
  1540. d->mb_skipped= 0;
  1541. d->qscale= s->qscale;
  1542. d->dquant= s->dquant;
  1543. d->esc3_level_length= s->esc3_level_length;
  1544. }
  1545. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1546. int i;
  1547. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  1548. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  1549. /* mpeg1 */
  1550. d->mb_skip_run= s->mb_skip_run;
  1551. for(i=0; i<3; i++)
  1552. d->last_dc[i]= s->last_dc[i];
  1553. /* statistics */
  1554. d->mv_bits= s->mv_bits;
  1555. d->i_tex_bits= s->i_tex_bits;
  1556. d->p_tex_bits= s->p_tex_bits;
  1557. d->i_count= s->i_count;
  1558. d->f_count= s->f_count;
  1559. d->b_count= s->b_count;
  1560. d->skip_count= s->skip_count;
  1561. d->misc_bits= s->misc_bits;
  1562. d->mb_intra= s->mb_intra;
  1563. d->mb_skipped= s->mb_skipped;
  1564. d->mv_type= s->mv_type;
  1565. d->mv_dir= s->mv_dir;
  1566. d->pb= s->pb;
  1567. if(s->data_partitioning){
  1568. d->pb2= s->pb2;
  1569. d->tex_pb= s->tex_pb;
  1570. }
  1571. d->block= s->block;
  1572. for(i=0; i<8; i++)
  1573. d->block_last_index[i]= s->block_last_index[i];
  1574. d->interlaced_dct= s->interlaced_dct;
  1575. d->qscale= s->qscale;
  1576. d->esc3_level_length= s->esc3_level_length;
  1577. }
  1578. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  1579. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  1580. int *dmin, int *next_block, int motion_x, int motion_y)
  1581. {
  1582. int score;
  1583. uint8_t *dest_backup[3];
  1584. copy_context_before_encode(s, backup, type);
  1585. s->block= s->blocks[*next_block];
  1586. s->pb= pb[*next_block];
  1587. if(s->data_partitioning){
  1588. s->pb2 = pb2 [*next_block];
  1589. s->tex_pb= tex_pb[*next_block];
  1590. }
  1591. if(*next_block){
  1592. memcpy(dest_backup, s->dest, sizeof(s->dest));
  1593. s->dest[0] = s->rd_scratchpad;
  1594. s->dest[1] = s->rd_scratchpad + 16*s->linesize;
  1595. s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
  1596. assert(s->linesize >= 32); //FIXME
  1597. }
  1598. encode_mb(s, motion_x, motion_y);
  1599. score= put_bits_count(&s->pb);
  1600. if(s->data_partitioning){
  1601. score+= put_bits_count(&s->pb2);
  1602. score+= put_bits_count(&s->tex_pb);
  1603. }
  1604. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  1605. MPV_decode_mb(s, s->block);
  1606. score *= s->lambda2;
  1607. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  1608. }
  1609. if(*next_block){
  1610. memcpy(s->dest, dest_backup, sizeof(s->dest));
  1611. }
  1612. if(score<*dmin){
  1613. *dmin= score;
  1614. *next_block^=1;
  1615. copy_context_after_encode(best, s, type);
  1616. }
  1617. }
  1618. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  1619. uint32_t *sq = ff_squareTbl + 256;
  1620. int acc=0;
  1621. int x,y;
  1622. if(w==16 && h==16)
  1623. return s->dsp.sse[0](NULL, src1, src2, stride, 16);
  1624. else if(w==8 && h==8)
  1625. return s->dsp.sse[1](NULL, src1, src2, stride, 8);
  1626. for(y=0; y<h; y++){
  1627. for(x=0; x<w; x++){
  1628. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  1629. }
  1630. }
  1631. assert(acc>=0);
  1632. return acc;
  1633. }
  1634. static int sse_mb(MpegEncContext *s){
  1635. int w= 16;
  1636. int h= 16;
  1637. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  1638. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  1639. if(w==16 && h==16)
  1640. if(s->avctx->mb_cmp == FF_CMP_NSSE){
  1641. return s->dsp.nsse[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
  1642. +s->dsp.nsse[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
  1643. +s->dsp.nsse[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
  1644. }else{
  1645. return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
  1646. +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
  1647. +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
  1648. }
  1649. else
  1650. return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
  1651. +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
  1652. +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
  1653. }
  1654. static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
  1655. MpegEncContext *s= *(void**)arg;
  1656. s->me.pre_pass=1;
  1657. s->me.dia_size= s->avctx->pre_dia_size;
  1658. s->first_slice_line=1;
  1659. for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
  1660. for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
  1661. ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  1662. }
  1663. s->first_slice_line=0;
  1664. }
  1665. s->me.pre_pass=0;
  1666. return 0;
  1667. }
  1668. static int estimate_motion_thread(AVCodecContext *c, void *arg){
  1669. MpegEncContext *s= *(void**)arg;
  1670. ff_check_alignment();
  1671. s->me.dia_size= s->avctx->dia_size;
  1672. s->first_slice_line=1;
  1673. for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  1674. s->mb_x=0; //for block init below
  1675. ff_init_block_index(s);
  1676. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  1677. s->block_index[0]+=2;
  1678. s->block_index[1]+=2;
  1679. s->block_index[2]+=2;
  1680. s->block_index[3]+=2;
  1681. /* compute motion vector & mb_type and store in context */
  1682. if(s->pict_type==FF_B_TYPE)
  1683. ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
  1684. else
  1685. ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  1686. }
  1687. s->first_slice_line=0;
  1688. }
  1689. return 0;
  1690. }
  1691. static int mb_var_thread(AVCodecContext *c, void *arg){
  1692. MpegEncContext *s= *(void**)arg;
  1693. int mb_x, mb_y;
  1694. ff_check_alignment();
  1695. for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  1696. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1697. int xx = mb_x * 16;
  1698. int yy = mb_y * 16;
  1699. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  1700. int varc;
  1701. int sum = s->dsp.pix_sum(pix, s->linesize);
  1702. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  1703. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  1704. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1705. s->me.mb_var_sum_temp += varc;
  1706. }
  1707. }
  1708. return 0;
  1709. }
  1710. static void write_slice_end(MpegEncContext *s){
  1711. if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4){
  1712. if(s->partitioned_frame){
  1713. ff_mpeg4_merge_partitions(s);
  1714. }
  1715. ff_mpeg4_stuffing(&s->pb);
  1716. }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
  1717. ff_mjpeg_encode_stuffing(&s->pb);
  1718. }
  1719. align_put_bits(&s->pb);
  1720. flush_put_bits(&s->pb);
  1721. if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
  1722. s->misc_bits+= get_bits_diff(s);
  1723. }
  1724. static int encode_thread(AVCodecContext *c, void *arg){
  1725. MpegEncContext *s= *(void**)arg;
  1726. int mb_x, mb_y, pdif = 0;
  1727. int chr_h= 16>>s->chroma_y_shift;
  1728. int i, j;
  1729. MpegEncContext best_s, backup_s;
  1730. uint8_t bit_buf[2][MAX_MB_BYTES];
  1731. uint8_t bit_buf2[2][MAX_MB_BYTES];
  1732. uint8_t bit_buf_tex[2][MAX_MB_BYTES];
  1733. PutBitContext pb[2], pb2[2], tex_pb[2];
  1734. //printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
  1735. ff_check_alignment();
  1736. for(i=0; i<2; i++){
  1737. init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
  1738. init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
  1739. init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
  1740. }
  1741. s->last_bits= put_bits_count(&s->pb);
  1742. s->mv_bits=0;
  1743. s->misc_bits=0;
  1744. s->i_tex_bits=0;
  1745. s->p_tex_bits=0;
  1746. s->i_count=0;
  1747. s->f_count=0;
  1748. s->b_count=0;
  1749. s->skip_count=0;
  1750. for(i=0; i<3; i++){
  1751. /* init last dc values */
  1752. /* note: quant matrix value (8) is implied here */
  1753. s->last_dc[i] = 128 << s->intra_dc_precision;
  1754. s->current_picture.error[i] = 0;
  1755. }
  1756. s->mb_skip_run = 0;
  1757. memset(s->last_mv, 0, sizeof(s->last_mv));
  1758. s->last_mv_dir = 0;
  1759. switch(s->codec_id){
  1760. case CODEC_ID_H263:
  1761. case CODEC_ID_H263P:
  1762. case CODEC_ID_FLV1:
  1763. if (CONFIG_H263_ENCODER)
  1764. s->gob_index = ff_h263_get_gob_height(s);
  1765. break;
  1766. case CODEC_ID_MPEG4:
  1767. if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
  1768. ff_mpeg4_init_partitions(s);
  1769. break;
  1770. }
  1771. s->resync_mb_x=0;
  1772. s->resync_mb_y=0;
  1773. s->first_slice_line = 1;
  1774. s->ptr_lastgob = s->pb.buf;
  1775. for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  1776. // printf("row %d at %X\n", s->mb_y, (int)s);
  1777. s->mb_x=0;
  1778. s->mb_y= mb_y;
  1779. ff_set_qscale(s, s->qscale);
  1780. ff_init_block_index(s);
  1781. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  1782. int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
  1783. int mb_type= s->mb_type[xy];
  1784. // int d;
  1785. int dmin= INT_MAX;
  1786. int dir;
  1787. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
  1788. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1789. return -1;
  1790. }
  1791. if(s->data_partitioning){
  1792. if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
  1793. || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
  1794. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1795. return -1;
  1796. }
  1797. }
  1798. s->mb_x = mb_x;
  1799. s->mb_y = mb_y; // moved into loop, can get changed by H.261
  1800. ff_update_block_index(s);
  1801. if(CONFIG_H261_ENCODER && s->codec_id == CODEC_ID_H261){
  1802. ff_h261_reorder_mb_index(s);
  1803. xy= s->mb_y*s->mb_stride + s->mb_x;
  1804. mb_type= s->mb_type[xy];
  1805. }
  1806. /* write gob / video packet header */
  1807. if(s->rtp_mode){
  1808. int current_packet_size, is_gob_start;
  1809. current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
  1810. is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
  1811. if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
  1812. switch(s->codec_id){
  1813. case CODEC_ID_H263:
  1814. case CODEC_ID_H263P:
  1815. if(!s->h263_slice_structured)
  1816. if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  1817. break;
  1818. case CODEC_ID_MPEG2VIDEO:
  1819. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  1820. case CODEC_ID_MPEG1VIDEO:
  1821. if(s->mb_skip_run) is_gob_start=0;
  1822. break;
  1823. }
  1824. if(is_gob_start){
  1825. if(s->start_mb_y != mb_y || mb_x!=0){
  1826. write_slice_end(s);
  1827. if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
  1828. ff_mpeg4_init_partitions(s);
  1829. }
  1830. }
  1831. assert((put_bits_count(&s->pb)&7) == 0);
  1832. current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
  1833. if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
  1834. int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
  1835. int d= 100 / s->avctx->error_rate;
  1836. if(r % d == 0){
  1837. current_packet_size=0;
  1838. #ifndef ALT_BITSTREAM_WRITER
  1839. s->pb.buf_ptr= s->ptr_lastgob;
  1840. #endif
  1841. assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
  1842. }
  1843. }
  1844. if (s->avctx->rtp_callback){
  1845. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
  1846. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
  1847. }
  1848. switch(s->codec_id){
  1849. case CODEC_ID_MPEG4:
  1850. if (CONFIG_MPEG4_ENCODER) {
  1851. ff_mpeg4_encode_video_packet_header(s);
  1852. ff_mpeg4_clean_buffers(s);
  1853. }
  1854. break;
  1855. case CODEC_ID_MPEG1VIDEO:
  1856. case CODEC_ID_MPEG2VIDEO:
  1857. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
  1858. ff_mpeg1_encode_slice_header(s);
  1859. ff_mpeg1_clean_buffers(s);
  1860. }
  1861. break;
  1862. case CODEC_ID_H263:
  1863. case CODEC_ID_H263P:
  1864. if (CONFIG_H263_ENCODER)
  1865. h263_encode_gob_header(s, mb_y);
  1866. break;
  1867. }
  1868. if(s->flags&CODEC_FLAG_PASS1){
  1869. int bits= put_bits_count(&s->pb);
  1870. s->misc_bits+= bits - s->last_bits;
  1871. s->last_bits= bits;
  1872. }
  1873. s->ptr_lastgob += current_packet_size;
  1874. s->first_slice_line=1;
  1875. s->resync_mb_x=mb_x;
  1876. s->resync_mb_y=mb_y;
  1877. }
  1878. }
  1879. if( (s->resync_mb_x == s->mb_x)
  1880. && s->resync_mb_y+1 == s->mb_y){
  1881. s->first_slice_line=0;
  1882. }
  1883. s->mb_skipped=0;
  1884. s->dquant=0; //only for QP_RD
  1885. if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible or CODEC_FLAG_QP_RD
  1886. int next_block=0;
  1887. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  1888. copy_context_before_encode(&backup_s, s, -1);
  1889. backup_s.pb= s->pb;
  1890. best_s.data_partitioning= s->data_partitioning;
  1891. best_s.partitioned_frame= s->partitioned_frame;
  1892. if(s->data_partitioning){
  1893. backup_s.pb2= s->pb2;
  1894. backup_s.tex_pb= s->tex_pb;
  1895. }
  1896. if(mb_type&CANDIDATE_MB_TYPE_INTER){
  1897. s->mv_dir = MV_DIR_FORWARD;
  1898. s->mv_type = MV_TYPE_16X16;
  1899. s->mb_intra= 0;
  1900. s->mv[0][0][0] = s->p_mv_table[xy][0];
  1901. s->mv[0][0][1] = s->p_mv_table[xy][1];
  1902. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
  1903. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1904. }
  1905. if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
  1906. s->mv_dir = MV_DIR_FORWARD;
  1907. s->mv_type = MV_TYPE_FIELD;
  1908. s->mb_intra= 0;
  1909. for(i=0; i<2; i++){
  1910. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  1911. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  1912. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  1913. }
  1914. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
  1915. &dmin, &next_block, 0, 0);
  1916. }
  1917. if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
  1918. s->mv_dir = MV_DIR_FORWARD;
  1919. s->mv_type = MV_TYPE_16X16;
  1920. s->mb_intra= 0;
  1921. s->mv[0][0][0] = 0;
  1922. s->mv[0][0][1] = 0;
  1923. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
  1924. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1925. }
  1926. if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
  1927. s->mv_dir = MV_DIR_FORWARD;
  1928. s->mv_type = MV_TYPE_8X8;
  1929. s->mb_intra= 0;
  1930. for(i=0; i<4; i++){
  1931. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  1932. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  1933. }
  1934. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
  1935. &dmin, &next_block, 0, 0);
  1936. }
  1937. if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  1938. s->mv_dir = MV_DIR_FORWARD;
  1939. s->mv_type = MV_TYPE_16X16;
  1940. s->mb_intra= 0;
  1941. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  1942. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  1943. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
  1944. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  1945. }
  1946. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  1947. s->mv_dir = MV_DIR_BACKWARD;
  1948. s->mv_type = MV_TYPE_16X16;
  1949. s->mb_intra= 0;
  1950. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  1951. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  1952. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  1953. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  1954. }
  1955. if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  1956. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1957. s->mv_type = MV_TYPE_16X16;
  1958. s->mb_intra= 0;
  1959. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  1960. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  1961. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  1962. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  1963. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
  1964. &dmin, &next_block, 0, 0);
  1965. }
  1966. if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
  1967. s->mv_dir = MV_DIR_FORWARD;
  1968. s->mv_type = MV_TYPE_FIELD;
  1969. s->mb_intra= 0;
  1970. for(i=0; i<2; i++){
  1971. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  1972. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  1973. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  1974. }
  1975. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
  1976. &dmin, &next_block, 0, 0);
  1977. }
  1978. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
  1979. s->mv_dir = MV_DIR_BACKWARD;
  1980. s->mv_type = MV_TYPE_FIELD;
  1981. s->mb_intra= 0;
  1982. for(i=0; i<2; i++){
  1983. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  1984. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  1985. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  1986. }
  1987. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
  1988. &dmin, &next_block, 0, 0);
  1989. }
  1990. if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
  1991. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1992. s->mv_type = MV_TYPE_FIELD;
  1993. s->mb_intra= 0;
  1994. for(dir=0; dir<2; dir++){
  1995. for(i=0; i<2; i++){
  1996. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  1997. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  1998. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  1999. }
  2000. }
  2001. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
  2002. &dmin, &next_block, 0, 0);
  2003. }
  2004. if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  2005. s->mv_dir = 0;
  2006. s->mv_type = MV_TYPE_16X16;
  2007. s->mb_intra= 1;
  2008. s->mv[0][0][0] = 0;
  2009. s->mv[0][0][1] = 0;
  2010. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
  2011. &dmin, &next_block, 0, 0);
  2012. if(s->h263_pred || s->h263_aic){
  2013. if(best_s.mb_intra)
  2014. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  2015. else
  2016. ff_clean_intra_table_entries(s); //old mode?
  2017. }
  2018. }
  2019. if((s->flags & CODEC_FLAG_QP_RD) && dmin < INT_MAX){
  2020. if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
  2021. const int last_qp= backup_s.qscale;
  2022. int qpi, qp, dc[6];
  2023. DCTELEM ac[6][16];
  2024. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  2025. static const int dquant_tab[4]={-1,1,-2,2};
  2026. assert(backup_s.dquant == 0);
  2027. //FIXME intra
  2028. s->mv_dir= best_s.mv_dir;
  2029. s->mv_type = MV_TYPE_16X16;
  2030. s->mb_intra= best_s.mb_intra;
  2031. s->mv[0][0][0] = best_s.mv[0][0][0];
  2032. s->mv[0][0][1] = best_s.mv[0][0][1];
  2033. s->mv[1][0][0] = best_s.mv[1][0][0];
  2034. s->mv[1][0][1] = best_s.mv[1][0][1];
  2035. qpi = s->pict_type == FF_B_TYPE ? 2 : 0;
  2036. for(; qpi<4; qpi++){
  2037. int dquant= dquant_tab[qpi];
  2038. qp= last_qp + dquant;
  2039. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  2040. continue;
  2041. backup_s.dquant= dquant;
  2042. if(s->mb_intra && s->dc_val[0]){
  2043. for(i=0; i<6; i++){
  2044. dc[i]= s->dc_val[0][ s->block_index[i] ];
  2045. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
  2046. }
  2047. }
  2048. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  2049. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  2050. if(best_s.qscale != qp){
  2051. if(s->mb_intra && s->dc_val[0]){
  2052. for(i=0; i<6; i++){
  2053. s->dc_val[0][ s->block_index[i] ]= dc[i];
  2054. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
  2055. }
  2056. }
  2057. }
  2058. }
  2059. }
  2060. }
  2061. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
  2062. int mx= s->b_direct_mv_table[xy][0];
  2063. int my= s->b_direct_mv_table[xy][1];
  2064. backup_s.dquant = 0;
  2065. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2066. s->mb_intra= 0;
  2067. ff_mpeg4_set_direct_mv(s, mx, my);
  2068. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2069. &dmin, &next_block, mx, my);
  2070. }
  2071. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
  2072. backup_s.dquant = 0;
  2073. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2074. s->mb_intra= 0;
  2075. ff_mpeg4_set_direct_mv(s, 0, 0);
  2076. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2077. &dmin, &next_block, 0, 0);
  2078. }
  2079. if(!best_s.mb_intra && s->flags2&CODEC_FLAG2_SKIP_RD){
  2080. int coded=0;
  2081. for(i=0; i<6; i++)
  2082. coded |= s->block_last_index[i];
  2083. if(coded){
  2084. int mx,my;
  2085. memcpy(s->mv, best_s.mv, sizeof(s->mv));
  2086. if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
  2087. mx=my=0; //FIXME find the one we actually used
  2088. ff_mpeg4_set_direct_mv(s, mx, my);
  2089. }else if(best_s.mv_dir&MV_DIR_BACKWARD){
  2090. mx= s->mv[1][0][0];
  2091. my= s->mv[1][0][1];
  2092. }else{
  2093. mx= s->mv[0][0][0];
  2094. my= s->mv[0][0][1];
  2095. }
  2096. s->mv_dir= best_s.mv_dir;
  2097. s->mv_type = best_s.mv_type;
  2098. s->mb_intra= 0;
  2099. /* s->mv[0][0][0] = best_s.mv[0][0][0];
  2100. s->mv[0][0][1] = best_s.mv[0][0][1];
  2101. s->mv[1][0][0] = best_s.mv[1][0][0];
  2102. s->mv[1][0][1] = best_s.mv[1][0][1];*/
  2103. backup_s.dquant= 0;
  2104. s->skipdct=1;
  2105. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  2106. &dmin, &next_block, mx, my);
  2107. s->skipdct=0;
  2108. }
  2109. }
  2110. s->current_picture.qscale_table[xy]= best_s.qscale;
  2111. copy_context_after_encode(s, &best_s, -1);
  2112. pb_bits_count= put_bits_count(&s->pb);
  2113. flush_put_bits(&s->pb);
  2114. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2115. s->pb= backup_s.pb;
  2116. if(s->data_partitioning){
  2117. pb2_bits_count= put_bits_count(&s->pb2);
  2118. flush_put_bits(&s->pb2);
  2119. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2120. s->pb2= backup_s.pb2;
  2121. tex_pb_bits_count= put_bits_count(&s->tex_pb);
  2122. flush_put_bits(&s->tex_pb);
  2123. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2124. s->tex_pb= backup_s.tex_pb;
  2125. }
  2126. s->last_bits= put_bits_count(&s->pb);
  2127. if (CONFIG_ANY_H263_ENCODER &&
  2128. s->out_format == FMT_H263 && s->pict_type!=FF_B_TYPE)
  2129. ff_h263_update_motion_val(s);
  2130. if(next_block==0){ //FIXME 16 vs linesize16
  2131. s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
  2132. s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
  2133. s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
  2134. }
  2135. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  2136. MPV_decode_mb(s, s->block);
  2137. } else {
  2138. int motion_x = 0, motion_y = 0;
  2139. s->mv_type=MV_TYPE_16X16;
  2140. // only one MB-Type possible
  2141. switch(mb_type){
  2142. case CANDIDATE_MB_TYPE_INTRA:
  2143. s->mv_dir = 0;
  2144. s->mb_intra= 1;
  2145. motion_x= s->mv[0][0][0] = 0;
  2146. motion_y= s->mv[0][0][1] = 0;
  2147. break;
  2148. case CANDIDATE_MB_TYPE_INTER:
  2149. s->mv_dir = MV_DIR_FORWARD;
  2150. s->mb_intra= 0;
  2151. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2152. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2153. break;
  2154. case CANDIDATE_MB_TYPE_INTER_I:
  2155. s->mv_dir = MV_DIR_FORWARD;
  2156. s->mv_type = MV_TYPE_FIELD;
  2157. s->mb_intra= 0;
  2158. for(i=0; i<2; i++){
  2159. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  2160. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  2161. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  2162. }
  2163. break;
  2164. case CANDIDATE_MB_TYPE_INTER4V:
  2165. s->mv_dir = MV_DIR_FORWARD;
  2166. s->mv_type = MV_TYPE_8X8;
  2167. s->mb_intra= 0;
  2168. for(i=0; i<4; i++){
  2169. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  2170. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  2171. }
  2172. break;
  2173. case CANDIDATE_MB_TYPE_DIRECT:
  2174. if (CONFIG_MPEG4_ENCODER) {
  2175. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  2176. s->mb_intra= 0;
  2177. motion_x=s->b_direct_mv_table[xy][0];
  2178. motion_y=s->b_direct_mv_table[xy][1];
  2179. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  2180. }
  2181. break;
  2182. case CANDIDATE_MB_TYPE_DIRECT0:
  2183. if (CONFIG_MPEG4_ENCODER) {
  2184. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  2185. s->mb_intra= 0;
  2186. ff_mpeg4_set_direct_mv(s, 0, 0);
  2187. }
  2188. break;
  2189. case CANDIDATE_MB_TYPE_BIDIR:
  2190. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2191. s->mb_intra= 0;
  2192. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2193. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2194. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2195. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2196. break;
  2197. case CANDIDATE_MB_TYPE_BACKWARD:
  2198. s->mv_dir = MV_DIR_BACKWARD;
  2199. s->mb_intra= 0;
  2200. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2201. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2202. break;
  2203. case CANDIDATE_MB_TYPE_FORWARD:
  2204. s->mv_dir = MV_DIR_FORWARD;
  2205. s->mb_intra= 0;
  2206. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2207. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2208. // printf(" %d %d ", motion_x, motion_y);
  2209. break;
  2210. case CANDIDATE_MB_TYPE_FORWARD_I:
  2211. s->mv_dir = MV_DIR_FORWARD;
  2212. s->mv_type = MV_TYPE_FIELD;
  2213. s->mb_intra= 0;
  2214. for(i=0; i<2; i++){
  2215. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  2216. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  2217. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  2218. }
  2219. break;
  2220. case CANDIDATE_MB_TYPE_BACKWARD_I:
  2221. s->mv_dir = MV_DIR_BACKWARD;
  2222. s->mv_type = MV_TYPE_FIELD;
  2223. s->mb_intra= 0;
  2224. for(i=0; i<2; i++){
  2225. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  2226. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  2227. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  2228. }
  2229. break;
  2230. case CANDIDATE_MB_TYPE_BIDIR_I:
  2231. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2232. s->mv_type = MV_TYPE_FIELD;
  2233. s->mb_intra= 0;
  2234. for(dir=0; dir<2; dir++){
  2235. for(i=0; i<2; i++){
  2236. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  2237. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  2238. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  2239. }
  2240. }
  2241. break;
  2242. default:
  2243. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  2244. }
  2245. encode_mb(s, motion_x, motion_y);
  2246. // RAL: Update last macroblock type
  2247. s->last_mv_dir = s->mv_dir;
  2248. if (CONFIG_ANY_H263_ENCODER &&
  2249. s->out_format == FMT_H263 && s->pict_type!=FF_B_TYPE)
  2250. ff_h263_update_motion_val(s);
  2251. MPV_decode_mb(s, s->block);
  2252. }
  2253. /* clean the MV table in IPS frames for direct mode in B frames */
  2254. if(s->mb_intra /* && I,P,S_TYPE */){
  2255. s->p_mv_table[xy][0]=0;
  2256. s->p_mv_table[xy][1]=0;
  2257. }
  2258. if(s->flags&CODEC_FLAG_PSNR){
  2259. int w= 16;
  2260. int h= 16;
  2261. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2262. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2263. s->current_picture.error[0] += sse(
  2264. s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2265. s->dest[0], w, h, s->linesize);
  2266. s->current_picture.error[1] += sse(
  2267. s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
  2268. s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
  2269. s->current_picture.error[2] += sse(
  2270. s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
  2271. s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
  2272. }
  2273. if(s->loop_filter){
  2274. if(CONFIG_ANY_H263_ENCODER && s->out_format == FMT_H263)
  2275. ff_h263_loop_filter(s);
  2276. }
  2277. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
  2278. }
  2279. }
  2280. //not beautiful here but we must write it before flushing so it has to be here
  2281. if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == FF_I_TYPE)
  2282. msmpeg4_encode_ext_header(s);
  2283. write_slice_end(s);
  2284. /* Send the last GOB if RTP */
  2285. if (s->avctx->rtp_callback) {
  2286. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
  2287. pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
  2288. /* Call the RTP callback to send the last GOB */
  2289. emms_c();
  2290. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
  2291. }
  2292. return 0;
  2293. }
  2294. #define MERGE(field) dst->field += src->field; src->field=0
  2295. static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  2296. MERGE(me.scene_change_score);
  2297. MERGE(me.mc_mb_var_sum_temp);
  2298. MERGE(me.mb_var_sum_temp);
  2299. }
  2300. static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
  2301. int i;
  2302. MERGE(dct_count[0]); //note, the other dct vars are not part of the context
  2303. MERGE(dct_count[1]);
  2304. MERGE(mv_bits);
  2305. MERGE(i_tex_bits);
  2306. MERGE(p_tex_bits);
  2307. MERGE(i_count);
  2308. MERGE(f_count);
  2309. MERGE(b_count);
  2310. MERGE(skip_count);
  2311. MERGE(misc_bits);
  2312. MERGE(error_count);
  2313. MERGE(padding_bug_score);
  2314. MERGE(current_picture.error[0]);
  2315. MERGE(current_picture.error[1]);
  2316. MERGE(current_picture.error[2]);
  2317. if(dst->avctx->noise_reduction){
  2318. for(i=0; i<64; i++){
  2319. MERGE(dct_error_sum[0][i]);
  2320. MERGE(dct_error_sum[1][i]);
  2321. }
  2322. }
  2323. assert(put_bits_count(&src->pb) % 8 ==0);
  2324. assert(put_bits_count(&dst->pb) % 8 ==0);
  2325. ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
  2326. flush_put_bits(&dst->pb);
  2327. }
  2328. static int estimate_qp(MpegEncContext *s, int dry_run){
  2329. if (s->next_lambda){
  2330. s->current_picture_ptr->quality=
  2331. s->current_picture.quality = s->next_lambda;
  2332. if(!dry_run) s->next_lambda= 0;
  2333. } else if (!s->fixed_qscale) {
  2334. s->current_picture_ptr->quality=
  2335. s->current_picture.quality = ff_rate_estimate_qscale(s, dry_run);
  2336. if (s->current_picture.quality < 0)
  2337. return -1;
  2338. }
  2339. if(s->adaptive_quant){
  2340. switch(s->codec_id){
  2341. case CODEC_ID_MPEG4:
  2342. if (CONFIG_MPEG4_ENCODER)
  2343. ff_clean_mpeg4_qscales(s);
  2344. break;
  2345. case CODEC_ID_H263:
  2346. case CODEC_ID_H263P:
  2347. case CODEC_ID_FLV1:
  2348. if (CONFIG_H263_ENCODER)
  2349. ff_clean_h263_qscales(s);
  2350. break;
  2351. default:
  2352. ff_init_qscale_tab(s);
  2353. }
  2354. s->lambda= s->lambda_table[0];
  2355. //FIXME broken
  2356. }else
  2357. s->lambda= s->current_picture.quality;
  2358. //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
  2359. update_qscale(s);
  2360. return 0;
  2361. }
  2362. /* must be called before writing the header */
  2363. static void set_frame_distances(MpegEncContext * s){
  2364. assert(s->current_picture_ptr->pts != AV_NOPTS_VALUE);
  2365. s->time= s->current_picture_ptr->pts*s->avctx->time_base.num;
  2366. if(s->pict_type==FF_B_TYPE){
  2367. s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
  2368. assert(s->pb_time > 0 && s->pb_time < s->pp_time);
  2369. }else{
  2370. s->pp_time= s->time - s->last_non_b_time;
  2371. s->last_non_b_time= s->time;
  2372. assert(s->picture_number==0 || s->pp_time > 0);
  2373. }
  2374. }
  2375. static int encode_picture(MpegEncContext *s, int picture_number)
  2376. {
  2377. int i;
  2378. int bits;
  2379. s->picture_number = picture_number;
  2380. /* Reset the average MB variance */
  2381. s->me.mb_var_sum_temp =
  2382. s->me.mc_mb_var_sum_temp = 0;
  2383. /* we need to initialize some time vars before we can encode b-frames */
  2384. // RAL: Condition added for MPEG1VIDEO
  2385. if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  2386. set_frame_distances(s);
  2387. if(CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4)
  2388. ff_set_mpeg4_time(s);
  2389. s->me.scene_change_score=0;
  2390. // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
  2391. if(s->pict_type==FF_I_TYPE){
  2392. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  2393. else s->no_rounding=0;
  2394. }else if(s->pict_type!=FF_B_TYPE){
  2395. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  2396. s->no_rounding ^= 1;
  2397. }
  2398. if(s->flags & CODEC_FLAG_PASS2){
  2399. if (estimate_qp(s,1) < 0)
  2400. return -1;
  2401. ff_get_2pass_fcode(s);
  2402. }else if(!(s->flags & CODEC_FLAG_QSCALE)){
  2403. if(s->pict_type==FF_B_TYPE)
  2404. s->lambda= s->last_lambda_for[s->pict_type];
  2405. else
  2406. s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
  2407. update_qscale(s);
  2408. }
  2409. s->mb_intra=0; //for the rate distortion & bit compare functions
  2410. for(i=1; i<s->avctx->thread_count; i++){
  2411. ff_update_duplicate_context(s->thread_context[i], s);
  2412. }
  2413. if(ff_init_me(s)<0)
  2414. return -1;
  2415. /* Estimate motion for every MB */
  2416. if(s->pict_type != FF_I_TYPE){
  2417. s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
  2418. s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
  2419. if(s->pict_type != FF_B_TYPE && s->avctx->me_threshold==0){
  2420. if((s->avctx->pre_me && s->last_non_b_pict_type==FF_I_TYPE) || s->avctx->pre_me==2){
  2421. s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
  2422. }
  2423. }
  2424. s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
  2425. }else /* if(s->pict_type == FF_I_TYPE) */{
  2426. /* I-Frame */
  2427. for(i=0; i<s->mb_stride*s->mb_height; i++)
  2428. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  2429. if(!s->fixed_qscale){
  2430. /* finding spatial complexity for I-frame rate control */
  2431. s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
  2432. }
  2433. }
  2434. for(i=1; i<s->avctx->thread_count; i++){
  2435. merge_context_after_me(s, s->thread_context[i]);
  2436. }
  2437. s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
  2438. s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
  2439. emms_c();
  2440. if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == FF_P_TYPE){
  2441. s->pict_type= FF_I_TYPE;
  2442. for(i=0; i<s->mb_stride*s->mb_height; i++)
  2443. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  2444. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  2445. }
  2446. if(!s->umvplus){
  2447. if(s->pict_type==FF_P_TYPE || s->pict_type==FF_S_TYPE) {
  2448. s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  2449. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  2450. int a,b;
  2451. a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  2452. b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  2453. s->f_code= FFMAX3(s->f_code, a, b);
  2454. }
  2455. ff_fix_long_p_mvs(s);
  2456. ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
  2457. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  2458. int j;
  2459. for(i=0; i<2; i++){
  2460. for(j=0; j<2; j++)
  2461. ff_fix_long_mvs(s, s->p_field_select_table[i], j,
  2462. s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
  2463. }
  2464. }
  2465. }
  2466. if(s->pict_type==FF_B_TYPE){
  2467. int a, b;
  2468. a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  2469. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  2470. s->f_code = FFMAX(a, b);
  2471. a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  2472. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  2473. s->b_code = FFMAX(a, b);
  2474. ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  2475. ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  2476. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  2477. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  2478. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  2479. int dir, j;
  2480. for(dir=0; dir<2; dir++){
  2481. for(i=0; i<2; i++){
  2482. for(j=0; j<2; j++){
  2483. int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
  2484. : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  2485. ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
  2486. s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  2487. }
  2488. }
  2489. }
  2490. }
  2491. }
  2492. }
  2493. if (estimate_qp(s, 0) < 0)
  2494. return -1;
  2495. if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==FF_I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
  2496. s->qscale= 3; //reduce clipping problems
  2497. if (s->out_format == FMT_MJPEG) {
  2498. /* for mjpeg, we do include qscale in the matrix */
  2499. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  2500. for(i=1;i<64;i++){
  2501. int j= s->dsp.idct_permutation[i];
  2502. s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  2503. }
  2504. ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  2505. s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
  2506. s->qscale= 8;
  2507. }
  2508. //FIXME var duplication
  2509. s->current_picture_ptr->key_frame=
  2510. s->current_picture.key_frame= s->pict_type == FF_I_TYPE; //FIXME pic_ptr
  2511. s->current_picture_ptr->pict_type=
  2512. s->current_picture.pict_type= s->pict_type;
  2513. if(s->current_picture.key_frame)
  2514. s->picture_in_gop_number=0;
  2515. s->last_bits= put_bits_count(&s->pb);
  2516. switch(s->out_format) {
  2517. case FMT_MJPEG:
  2518. if (CONFIG_MJPEG_ENCODER)
  2519. ff_mjpeg_encode_picture_header(s);
  2520. break;
  2521. case FMT_H261:
  2522. if (CONFIG_H261_ENCODER)
  2523. ff_h261_encode_picture_header(s, picture_number);
  2524. break;
  2525. case FMT_H263:
  2526. if (CONFIG_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
  2527. ff_wmv2_encode_picture_header(s, picture_number);
  2528. else if (CONFIG_MSMPEG4_ENCODER && s->h263_msmpeg4)
  2529. msmpeg4_encode_picture_header(s, picture_number);
  2530. else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
  2531. mpeg4_encode_picture_header(s, picture_number);
  2532. else if (CONFIG_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
  2533. rv10_encode_picture_header(s, picture_number);
  2534. else if (CONFIG_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
  2535. rv20_encode_picture_header(s, picture_number);
  2536. else if (CONFIG_FLV_ENCODER && s->codec_id == CODEC_ID_FLV1)
  2537. ff_flv_encode_picture_header(s, picture_number);
  2538. else if (CONFIG_ANY_H263_ENCODER)
  2539. h263_encode_picture_header(s, picture_number);
  2540. break;
  2541. case FMT_MPEG1:
  2542. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  2543. mpeg1_encode_picture_header(s, picture_number);
  2544. break;
  2545. case FMT_H264:
  2546. break;
  2547. default:
  2548. assert(0);
  2549. }
  2550. bits= put_bits_count(&s->pb);
  2551. s->header_bits= bits - s->last_bits;
  2552. for(i=1; i<s->avctx->thread_count; i++){
  2553. update_duplicate_context_after_me(s->thread_context[i], s);
  2554. }
  2555. s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
  2556. for(i=1; i<s->avctx->thread_count; i++){
  2557. merge_context_after_encode(s, s->thread_context[i]);
  2558. }
  2559. emms_c();
  2560. return 0;
  2561. }
  2562. void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
  2563. const int intra= s->mb_intra;
  2564. int i;
  2565. s->dct_count[intra]++;
  2566. for(i=0; i<64; i++){
  2567. int level= block[i];
  2568. if(level){
  2569. if(level>0){
  2570. s->dct_error_sum[intra][i] += level;
  2571. level -= s->dct_offset[intra][i];
  2572. if(level<0) level=0;
  2573. }else{
  2574. s->dct_error_sum[intra][i] -= level;
  2575. level += s->dct_offset[intra][i];
  2576. if(level>0) level=0;
  2577. }
  2578. block[i]= level;
  2579. }
  2580. }
  2581. }
  2582. int dct_quantize_trellis_c(MpegEncContext *s,
  2583. DCTELEM *block, int n,
  2584. int qscale, int *overflow){
  2585. const int *qmat;
  2586. const uint8_t *scantable= s->intra_scantable.scantable;
  2587. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  2588. int max=0;
  2589. unsigned int threshold1, threshold2;
  2590. int bias=0;
  2591. int run_tab[65];
  2592. int level_tab[65];
  2593. int score_tab[65];
  2594. int survivor[65];
  2595. int survivor_count;
  2596. int last_run=0;
  2597. int last_level=0;
  2598. int last_score= 0;
  2599. int last_i;
  2600. int coeff[2][64];
  2601. int coeff_count[64];
  2602. int qmul, qadd, start_i, last_non_zero, i, dc;
  2603. const int esc_length= s->ac_esc_length;
  2604. uint8_t * length;
  2605. uint8_t * last_length;
  2606. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  2607. s->dsp.fdct (block);
  2608. if(s->dct_error_sum)
  2609. s->denoise_dct(s, block);
  2610. qmul= qscale*16;
  2611. qadd= ((qscale-1)|1)*8;
  2612. if (s->mb_intra) {
  2613. int q;
  2614. if (!s->h263_aic) {
  2615. if (n < 4)
  2616. q = s->y_dc_scale;
  2617. else
  2618. q = s->c_dc_scale;
  2619. q = q << 3;
  2620. } else{
  2621. /* For AIC we skip quant/dequant of INTRADC */
  2622. q = 1 << 3;
  2623. qadd=0;
  2624. }
  2625. /* note: block[0] is assumed to be positive */
  2626. block[0] = (block[0] + (q >> 1)) / q;
  2627. start_i = 1;
  2628. last_non_zero = 0;
  2629. qmat = s->q_intra_matrix[qscale];
  2630. if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  2631. bias= 1<<(QMAT_SHIFT-1);
  2632. length = s->intra_ac_vlc_length;
  2633. last_length= s->intra_ac_vlc_last_length;
  2634. } else {
  2635. start_i = 0;
  2636. last_non_zero = -1;
  2637. qmat = s->q_inter_matrix[qscale];
  2638. length = s->inter_ac_vlc_length;
  2639. last_length= s->inter_ac_vlc_last_length;
  2640. }
  2641. last_i= start_i;
  2642. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  2643. threshold2= (threshold1<<1);
  2644. for(i=63; i>=start_i; i--) {
  2645. const int j = scantable[i];
  2646. int level = block[j] * qmat[j];
  2647. if(((unsigned)(level+threshold1))>threshold2){
  2648. last_non_zero = i;
  2649. break;
  2650. }
  2651. }
  2652. for(i=start_i; i<=last_non_zero; i++) {
  2653. const int j = scantable[i];
  2654. int level = block[j] * qmat[j];
  2655. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  2656. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  2657. if(((unsigned)(level+threshold1))>threshold2){
  2658. if(level>0){
  2659. level= (bias + level)>>QMAT_SHIFT;
  2660. coeff[0][i]= level;
  2661. coeff[1][i]= level-1;
  2662. // coeff[2][k]= level-2;
  2663. }else{
  2664. level= (bias - level)>>QMAT_SHIFT;
  2665. coeff[0][i]= -level;
  2666. coeff[1][i]= -level+1;
  2667. // coeff[2][k]= -level+2;
  2668. }
  2669. coeff_count[i]= FFMIN(level, 2);
  2670. assert(coeff_count[i]);
  2671. max |=level;
  2672. }else{
  2673. coeff[0][i]= (level>>31)|1;
  2674. coeff_count[i]= 1;
  2675. }
  2676. }
  2677. *overflow= s->max_qcoeff < max; //overflow might have happened
  2678. if(last_non_zero < start_i){
  2679. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  2680. return last_non_zero;
  2681. }
  2682. score_tab[start_i]= 0;
  2683. survivor[0]= start_i;
  2684. survivor_count= 1;
  2685. for(i=start_i; i<=last_non_zero; i++){
  2686. int level_index, j, zero_distortion;
  2687. int dct_coeff= FFABS(block[ scantable[i] ]);
  2688. int best_score=256*256*256*120;
  2689. if ( s->dsp.fdct == fdct_ifast
  2690. #ifndef FAAN_POSTSCALE
  2691. || s->dsp.fdct == ff_faandct
  2692. #endif
  2693. )
  2694. dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
  2695. zero_distortion= dct_coeff*dct_coeff;
  2696. for(level_index=0; level_index < coeff_count[i]; level_index++){
  2697. int distortion;
  2698. int level= coeff[level_index][i];
  2699. const int alevel= FFABS(level);
  2700. int unquant_coeff;
  2701. assert(level);
  2702. if(s->out_format == FMT_H263){
  2703. unquant_coeff= alevel*qmul + qadd;
  2704. }else{ //MPEG1
  2705. j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
  2706. if(s->mb_intra){
  2707. unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
  2708. unquant_coeff = (unquant_coeff - 1) | 1;
  2709. }else{
  2710. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  2711. unquant_coeff = (unquant_coeff - 1) | 1;
  2712. }
  2713. unquant_coeff<<= 3;
  2714. }
  2715. distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
  2716. level+=64;
  2717. if((level&(~127)) == 0){
  2718. for(j=survivor_count-1; j>=0; j--){
  2719. int run= i - survivor[j];
  2720. int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  2721. score += score_tab[i-run];
  2722. if(score < best_score){
  2723. best_score= score;
  2724. run_tab[i+1]= run;
  2725. level_tab[i+1]= level-64;
  2726. }
  2727. }
  2728. if(s->out_format == FMT_H263){
  2729. for(j=survivor_count-1; j>=0; j--){
  2730. int run= i - survivor[j];
  2731. int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  2732. score += score_tab[i-run];
  2733. if(score < last_score){
  2734. last_score= score;
  2735. last_run= run;
  2736. last_level= level-64;
  2737. last_i= i+1;
  2738. }
  2739. }
  2740. }
  2741. }else{
  2742. distortion += esc_length*lambda;
  2743. for(j=survivor_count-1; j>=0; j--){
  2744. int run= i - survivor[j];
  2745. int score= distortion + score_tab[i-run];
  2746. if(score < best_score){
  2747. best_score= score;
  2748. run_tab[i+1]= run;
  2749. level_tab[i+1]= level-64;
  2750. }
  2751. }
  2752. if(s->out_format == FMT_H263){
  2753. for(j=survivor_count-1; j>=0; j--){
  2754. int run= i - survivor[j];
  2755. int score= distortion + score_tab[i-run];
  2756. if(score < last_score){
  2757. last_score= score;
  2758. last_run= run;
  2759. last_level= level-64;
  2760. last_i= i+1;
  2761. }
  2762. }
  2763. }
  2764. }
  2765. }
  2766. score_tab[i+1]= best_score;
  2767. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  2768. if(last_non_zero <= 27){
  2769. for(; survivor_count; survivor_count--){
  2770. if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  2771. break;
  2772. }
  2773. }else{
  2774. for(; survivor_count; survivor_count--){
  2775. if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  2776. break;
  2777. }
  2778. }
  2779. survivor[ survivor_count++ ]= i+1;
  2780. }
  2781. if(s->out_format != FMT_H263){
  2782. last_score= 256*256*256*120;
  2783. for(i= survivor[0]; i<=last_non_zero + 1; i++){
  2784. int score= score_tab[i];
  2785. if(i) score += lambda*2; //FIXME exacter?
  2786. if(score < last_score){
  2787. last_score= score;
  2788. last_i= i;
  2789. last_level= level_tab[i];
  2790. last_run= run_tab[i];
  2791. }
  2792. }
  2793. }
  2794. s->coded_score[n] = last_score;
  2795. dc= FFABS(block[0]);
  2796. last_non_zero= last_i - 1;
  2797. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  2798. if(last_non_zero < start_i)
  2799. return last_non_zero;
  2800. if(last_non_zero == 0 && start_i == 0){
  2801. int best_level= 0;
  2802. int best_score= dc * dc;
  2803. for(i=0; i<coeff_count[0]; i++){
  2804. int level= coeff[i][0];
  2805. int alevel= FFABS(level);
  2806. int unquant_coeff, score, distortion;
  2807. if(s->out_format == FMT_H263){
  2808. unquant_coeff= (alevel*qmul + qadd)>>3;
  2809. }else{ //MPEG1
  2810. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  2811. unquant_coeff = (unquant_coeff - 1) | 1;
  2812. }
  2813. unquant_coeff = (unquant_coeff + 4) >> 3;
  2814. unquant_coeff<<= 3 + 3;
  2815. distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
  2816. level+=64;
  2817. if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  2818. else score= distortion + esc_length*lambda;
  2819. if(score < best_score){
  2820. best_score= score;
  2821. best_level= level - 64;
  2822. }
  2823. }
  2824. block[0]= best_level;
  2825. s->coded_score[n] = best_score - dc*dc;
  2826. if(best_level == 0) return -1;
  2827. else return last_non_zero;
  2828. }
  2829. i= last_i;
  2830. assert(last_level);
  2831. block[ perm_scantable[last_non_zero] ]= last_level;
  2832. i -= last_run + 1;
  2833. for(; i>start_i; i -= run_tab[i] + 1){
  2834. block[ perm_scantable[i-1] ]= level_tab[i];
  2835. }
  2836. return last_non_zero;
  2837. }
  2838. //#define REFINE_STATS 1
  2839. static int16_t basis[64][64];
  2840. static void build_basis(uint8_t *perm){
  2841. int i, j, x, y;
  2842. emms_c();
  2843. for(i=0; i<8; i++){
  2844. for(j=0; j<8; j++){
  2845. for(y=0; y<8; y++){
  2846. for(x=0; x<8; x++){
  2847. double s= 0.25*(1<<BASIS_SHIFT);
  2848. int index= 8*i + j;
  2849. int perm_index= perm[index];
  2850. if(i==0) s*= sqrt(0.5);
  2851. if(j==0) s*= sqrt(0.5);
  2852. basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
  2853. }
  2854. }
  2855. }
  2856. }
  2857. }
  2858. static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
  2859. DCTELEM *block, int16_t *weight, DCTELEM *orig,
  2860. int n, int qscale){
  2861. int16_t rem[64];
  2862. DECLARE_ALIGNED_16(DCTELEM, d1[64]);
  2863. const uint8_t *scantable= s->intra_scantable.scantable;
  2864. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  2865. // unsigned int threshold1, threshold2;
  2866. // int bias=0;
  2867. int run_tab[65];
  2868. int prev_run=0;
  2869. int prev_level=0;
  2870. int qmul, qadd, start_i, last_non_zero, i, dc;
  2871. uint8_t * length;
  2872. uint8_t * last_length;
  2873. int lambda;
  2874. int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
  2875. #ifdef REFINE_STATS
  2876. static int count=0;
  2877. static int after_last=0;
  2878. static int to_zero=0;
  2879. static int from_zero=0;
  2880. static int raise=0;
  2881. static int lower=0;
  2882. static int messed_sign=0;
  2883. #endif
  2884. if(basis[0][0] == 0)
  2885. build_basis(s->dsp.idct_permutation);
  2886. qmul= qscale*2;
  2887. qadd= (qscale-1)|1;
  2888. if (s->mb_intra) {
  2889. if (!s->h263_aic) {
  2890. if (n < 4)
  2891. q = s->y_dc_scale;
  2892. else
  2893. q = s->c_dc_scale;
  2894. } else{
  2895. /* For AIC we skip quant/dequant of INTRADC */
  2896. q = 1;
  2897. qadd=0;
  2898. }
  2899. q <<= RECON_SHIFT-3;
  2900. /* note: block[0] is assumed to be positive */
  2901. dc= block[0]*q;
  2902. // block[0] = (block[0] + (q >> 1)) / q;
  2903. start_i = 1;
  2904. // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  2905. // bias= 1<<(QMAT_SHIFT-1);
  2906. length = s->intra_ac_vlc_length;
  2907. last_length= s->intra_ac_vlc_last_length;
  2908. } else {
  2909. dc= 0;
  2910. start_i = 0;
  2911. length = s->inter_ac_vlc_length;
  2912. last_length= s->inter_ac_vlc_last_length;
  2913. }
  2914. last_non_zero = s->block_last_index[n];
  2915. #ifdef REFINE_STATS
  2916. {START_TIMER
  2917. #endif
  2918. dc += (1<<(RECON_SHIFT-1));
  2919. for(i=0; i<64; i++){
  2920. rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
  2921. }
  2922. #ifdef REFINE_STATS
  2923. STOP_TIMER("memset rem[]")}
  2924. #endif
  2925. sum=0;
  2926. for(i=0; i<64; i++){
  2927. int one= 36;
  2928. int qns=4;
  2929. int w;
  2930. w= FFABS(weight[i]) + qns*one;
  2931. w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
  2932. weight[i] = w;
  2933. // w=weight[i] = (63*qns + (w/2)) / w;
  2934. assert(w>0);
  2935. assert(w<(1<<6));
  2936. sum += w*w;
  2937. }
  2938. lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
  2939. #ifdef REFINE_STATS
  2940. {START_TIMER
  2941. #endif
  2942. run=0;
  2943. rle_index=0;
  2944. for(i=start_i; i<=last_non_zero; i++){
  2945. int j= perm_scantable[i];
  2946. const int level= block[j];
  2947. int coeff;
  2948. if(level){
  2949. if(level<0) coeff= qmul*level - qadd;
  2950. else coeff= qmul*level + qadd;
  2951. run_tab[rle_index++]=run;
  2952. run=0;
  2953. s->dsp.add_8x8basis(rem, basis[j], coeff);
  2954. }else{
  2955. run++;
  2956. }
  2957. }
  2958. #ifdef REFINE_STATS
  2959. if(last_non_zero>0){
  2960. STOP_TIMER("init rem[]")
  2961. }
  2962. }
  2963. {START_TIMER
  2964. #endif
  2965. for(;;){
  2966. int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
  2967. int best_coeff=0;
  2968. int best_change=0;
  2969. int run2, best_unquant_change=0, analyze_gradient;
  2970. #ifdef REFINE_STATS
  2971. {START_TIMER
  2972. #endif
  2973. analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
  2974. if(analyze_gradient){
  2975. #ifdef REFINE_STATS
  2976. {START_TIMER
  2977. #endif
  2978. for(i=0; i<64; i++){
  2979. int w= weight[i];
  2980. d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
  2981. }
  2982. #ifdef REFINE_STATS
  2983. STOP_TIMER("rem*w*w")}
  2984. {START_TIMER
  2985. #endif
  2986. s->dsp.fdct(d1);
  2987. #ifdef REFINE_STATS
  2988. STOP_TIMER("dct")}
  2989. #endif
  2990. }
  2991. if(start_i){
  2992. const int level= block[0];
  2993. int change, old_coeff;
  2994. assert(s->mb_intra);
  2995. old_coeff= q*level;
  2996. for(change=-1; change<=1; change+=2){
  2997. int new_level= level + change;
  2998. int score, new_coeff;
  2999. new_coeff= q*new_level;
  3000. if(new_coeff >= 2048 || new_coeff < 0)
  3001. continue;
  3002. score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
  3003. if(score<best_score){
  3004. best_score= score;
  3005. best_coeff= 0;
  3006. best_change= change;
  3007. best_unquant_change= new_coeff - old_coeff;
  3008. }
  3009. }
  3010. }
  3011. run=0;
  3012. rle_index=0;
  3013. run2= run_tab[rle_index++];
  3014. prev_level=0;
  3015. prev_run=0;
  3016. for(i=start_i; i<64; i++){
  3017. int j= perm_scantable[i];
  3018. const int level= block[j];
  3019. int change, old_coeff;
  3020. if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
  3021. break;
  3022. if(level){
  3023. if(level<0) old_coeff= qmul*level - qadd;
  3024. else old_coeff= qmul*level + qadd;
  3025. run2= run_tab[rle_index++]; //FIXME ! maybe after last
  3026. }else{
  3027. old_coeff=0;
  3028. run2--;
  3029. assert(run2>=0 || i >= last_non_zero );
  3030. }
  3031. for(change=-1; change<=1; change+=2){
  3032. int new_level= level + change;
  3033. int score, new_coeff, unquant_change;
  3034. score=0;
  3035. if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
  3036. continue;
  3037. if(new_level){
  3038. if(new_level<0) new_coeff= qmul*new_level - qadd;
  3039. else new_coeff= qmul*new_level + qadd;
  3040. if(new_coeff >= 2048 || new_coeff <= -2048)
  3041. continue;
  3042. //FIXME check for overflow
  3043. if(level){
  3044. if(level < 63 && level > -63){
  3045. if(i < last_non_zero)
  3046. score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
  3047. - length[UNI_AC_ENC_INDEX(run, level+64)];
  3048. else
  3049. score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
  3050. - last_length[UNI_AC_ENC_INDEX(run, level+64)];
  3051. }
  3052. }else{
  3053. assert(FFABS(new_level)==1);
  3054. if(analyze_gradient){
  3055. int g= d1[ scantable[i] ];
  3056. if(g && (g^new_level) >= 0)
  3057. continue;
  3058. }
  3059. if(i < last_non_zero){
  3060. int next_i= i + run2 + 1;
  3061. int next_level= block[ perm_scantable[next_i] ] + 64;
  3062. if(next_level&(~127))
  3063. next_level= 0;
  3064. if(next_i < last_non_zero)
  3065. score += length[UNI_AC_ENC_INDEX(run, 65)]
  3066. + length[UNI_AC_ENC_INDEX(run2, next_level)]
  3067. - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  3068. else
  3069. score += length[UNI_AC_ENC_INDEX(run, 65)]
  3070. + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  3071. - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  3072. }else{
  3073. score += last_length[UNI_AC_ENC_INDEX(run, 65)];
  3074. if(prev_level){
  3075. score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  3076. - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  3077. }
  3078. }
  3079. }
  3080. }else{
  3081. new_coeff=0;
  3082. assert(FFABS(level)==1);
  3083. if(i < last_non_zero){
  3084. int next_i= i + run2 + 1;
  3085. int next_level= block[ perm_scantable[next_i] ] + 64;
  3086. if(next_level&(~127))
  3087. next_level= 0;
  3088. if(next_i < last_non_zero)
  3089. score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  3090. - length[UNI_AC_ENC_INDEX(run2, next_level)]
  3091. - length[UNI_AC_ENC_INDEX(run, 65)];
  3092. else
  3093. score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  3094. - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  3095. - length[UNI_AC_ENC_INDEX(run, 65)];
  3096. }else{
  3097. score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
  3098. if(prev_level){
  3099. score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  3100. - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  3101. }
  3102. }
  3103. }
  3104. score *= lambda;
  3105. unquant_change= new_coeff - old_coeff;
  3106. assert((score < 100*lambda && score > -100*lambda) || lambda==0);
  3107. score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
  3108. if(score<best_score){
  3109. best_score= score;
  3110. best_coeff= i;
  3111. best_change= change;
  3112. best_unquant_change= unquant_change;
  3113. }
  3114. }
  3115. if(level){
  3116. prev_level= level + 64;
  3117. if(prev_level&(~127))
  3118. prev_level= 0;
  3119. prev_run= run;
  3120. run=0;
  3121. }else{
  3122. run++;
  3123. }
  3124. }
  3125. #ifdef REFINE_STATS
  3126. STOP_TIMER("iterative step")}
  3127. #endif
  3128. if(best_change){
  3129. int j= perm_scantable[ best_coeff ];
  3130. block[j] += best_change;
  3131. if(best_coeff > last_non_zero){
  3132. last_non_zero= best_coeff;
  3133. assert(block[j]);
  3134. #ifdef REFINE_STATS
  3135. after_last++;
  3136. #endif
  3137. }else{
  3138. #ifdef REFINE_STATS
  3139. if(block[j]){
  3140. if(block[j] - best_change){
  3141. if(FFABS(block[j]) > FFABS(block[j] - best_change)){
  3142. raise++;
  3143. }else{
  3144. lower++;
  3145. }
  3146. }else{
  3147. from_zero++;
  3148. }
  3149. }else{
  3150. to_zero++;
  3151. }
  3152. #endif
  3153. for(; last_non_zero>=start_i; last_non_zero--){
  3154. if(block[perm_scantable[last_non_zero]])
  3155. break;
  3156. }
  3157. }
  3158. #ifdef REFINE_STATS
  3159. count++;
  3160. if(256*256*256*64 % count == 0){
  3161. printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
  3162. }
  3163. #endif
  3164. run=0;
  3165. rle_index=0;
  3166. for(i=start_i; i<=last_non_zero; i++){
  3167. int j= perm_scantable[i];
  3168. const int level= block[j];
  3169. if(level){
  3170. run_tab[rle_index++]=run;
  3171. run=0;
  3172. }else{
  3173. run++;
  3174. }
  3175. }
  3176. s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
  3177. }else{
  3178. break;
  3179. }
  3180. }
  3181. #ifdef REFINE_STATS
  3182. if(last_non_zero>0){
  3183. STOP_TIMER("iterative search")
  3184. }
  3185. }
  3186. #endif
  3187. return last_non_zero;
  3188. }
  3189. int dct_quantize_c(MpegEncContext *s,
  3190. DCTELEM *block, int n,
  3191. int qscale, int *overflow)
  3192. {
  3193. int i, j, level, last_non_zero, q, start_i;
  3194. const int *qmat;
  3195. const uint8_t *scantable= s->intra_scantable.scantable;
  3196. int bias;
  3197. int max=0;
  3198. unsigned int threshold1, threshold2;
  3199. s->dsp.fdct (block);
  3200. if(s->dct_error_sum)
  3201. s->denoise_dct(s, block);
  3202. if (s->mb_intra) {
  3203. if (!s->h263_aic) {
  3204. if (n < 4)
  3205. q = s->y_dc_scale;
  3206. else
  3207. q = s->c_dc_scale;
  3208. q = q << 3;
  3209. } else
  3210. /* For AIC we skip quant/dequant of INTRADC */
  3211. q = 1 << 3;
  3212. /* note: block[0] is assumed to be positive */
  3213. block[0] = (block[0] + (q >> 1)) / q;
  3214. start_i = 1;
  3215. last_non_zero = 0;
  3216. qmat = s->q_intra_matrix[qscale];
  3217. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3218. } else {
  3219. start_i = 0;
  3220. last_non_zero = -1;
  3221. qmat = s->q_inter_matrix[qscale];
  3222. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3223. }
  3224. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3225. threshold2= (threshold1<<1);
  3226. for(i=63;i>=start_i;i--) {
  3227. j = scantable[i];
  3228. level = block[j] * qmat[j];
  3229. if(((unsigned)(level+threshold1))>threshold2){
  3230. last_non_zero = i;
  3231. break;
  3232. }else{
  3233. block[j]=0;
  3234. }
  3235. }
  3236. for(i=start_i; i<=last_non_zero; i++) {
  3237. j = scantable[i];
  3238. level = block[j] * qmat[j];
  3239. // if( bias+level >= (1<<QMAT_SHIFT)
  3240. // || bias-level >= (1<<QMAT_SHIFT)){
  3241. if(((unsigned)(level+threshold1))>threshold2){
  3242. if(level>0){
  3243. level= (bias + level)>>QMAT_SHIFT;
  3244. block[j]= level;
  3245. }else{
  3246. level= (bias - level)>>QMAT_SHIFT;
  3247. block[j]= -level;
  3248. }
  3249. max |=level;
  3250. }else{
  3251. block[j]=0;
  3252. }
  3253. }
  3254. *overflow= s->max_qcoeff < max; //overflow might have happened
  3255. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  3256. if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  3257. ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  3258. return last_non_zero;
  3259. }
  3260. AVCodec h263_encoder = {
  3261. "h263",
  3262. CODEC_TYPE_VIDEO,
  3263. CODEC_ID_H263,
  3264. sizeof(MpegEncContext),
  3265. MPV_encode_init,
  3266. MPV_encode_picture,
  3267. MPV_encode_end,
  3268. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  3269. .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
  3270. };
  3271. AVCodec h263p_encoder = {
  3272. "h263p",
  3273. CODEC_TYPE_VIDEO,
  3274. CODEC_ID_H263P,
  3275. sizeof(MpegEncContext),
  3276. MPV_encode_init,
  3277. MPV_encode_picture,
  3278. MPV_encode_end,
  3279. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  3280. .long_name= NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
  3281. };
  3282. AVCodec flv_encoder = {
  3283. "flv",
  3284. CODEC_TYPE_VIDEO,
  3285. CODEC_ID_FLV1,
  3286. sizeof(MpegEncContext),
  3287. MPV_encode_init,
  3288. MPV_encode_picture,
  3289. MPV_encode_end,
  3290. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  3291. .long_name= NULL_IF_CONFIG_SMALL("Flash Video (FLV) / Sorenson Spark / Sorenson H.263"),
  3292. };
  3293. AVCodec mpeg4_encoder = {
  3294. "mpeg4",
  3295. CODEC_TYPE_VIDEO,
  3296. CODEC_ID_MPEG4,
  3297. sizeof(MpegEncContext),
  3298. MPV_encode_init,
  3299. MPV_encode_picture,
  3300. MPV_encode_end,
  3301. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  3302. .capabilities= CODEC_CAP_DELAY,
  3303. .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
  3304. };
  3305. AVCodec msmpeg4v1_encoder = {
  3306. "msmpeg4v1",
  3307. CODEC_TYPE_VIDEO,
  3308. CODEC_ID_MSMPEG4V1,
  3309. sizeof(MpegEncContext),
  3310. MPV_encode_init,
  3311. MPV_encode_picture,
  3312. MPV_encode_end,
  3313. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  3314. .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
  3315. };
  3316. AVCodec msmpeg4v2_encoder = {
  3317. "msmpeg4v2",
  3318. CODEC_TYPE_VIDEO,
  3319. CODEC_ID_MSMPEG4V2,
  3320. sizeof(MpegEncContext),
  3321. MPV_encode_init,
  3322. MPV_encode_picture,
  3323. MPV_encode_end,
  3324. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  3325. .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
  3326. };
  3327. AVCodec msmpeg4v3_encoder = {
  3328. "msmpeg4",
  3329. CODEC_TYPE_VIDEO,
  3330. CODEC_ID_MSMPEG4V3,
  3331. sizeof(MpegEncContext),
  3332. MPV_encode_init,
  3333. MPV_encode_picture,
  3334. MPV_encode_end,
  3335. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  3336. .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
  3337. };
  3338. AVCodec wmv1_encoder = {
  3339. "wmv1",
  3340. CODEC_TYPE_VIDEO,
  3341. CODEC_ID_WMV1,
  3342. sizeof(MpegEncContext),
  3343. MPV_encode_init,
  3344. MPV_encode_picture,
  3345. MPV_encode_end,
  3346. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  3347. .long_name= NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
  3348. };