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.

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