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.

4588 lines
164KB

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