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.

4294 lines
156KB

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