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. /* MPEG-1/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 modifying 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_free_context(&c);
  1175. return best_b_count;
  1176. }
  1177. static int select_input_picture(MpegEncContext *s)
  1178. {
  1179. int i, ret;
  1180. for (i = 1; i < MAX_PICTURE_COUNT; i++)
  1181. s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
  1182. s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
  1183. /* set next picture type & ordering */
  1184. if (!s->reordered_input_picture[0] && s->input_picture[0]) {
  1185. if (/*s->picture_in_gop_number >= s->gop_size ||*/
  1186. !s->next_picture_ptr || s->intra_only) {
  1187. s->reordered_input_picture[0] = s->input_picture[0];
  1188. s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
  1189. s->reordered_input_picture[0]->f->coded_picture_number =
  1190. s->coded_picture_number++;
  1191. } else {
  1192. int b_frames = 0;
  1193. if (s->frame_skip_threshold || s->frame_skip_factor) {
  1194. if (s->picture_in_gop_number < s->gop_size &&
  1195. skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
  1196. // FIXME check that the gop check above is +-1 correct
  1197. av_frame_unref(s->input_picture[0]->f);
  1198. emms_c();
  1199. ff_vbv_update(s, 0);
  1200. goto no_output_pic;
  1201. }
  1202. }
  1203. if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
  1204. for (i = 0; i < s->max_b_frames + 1; i++) {
  1205. int pict_num = s->input_picture[0]->f->display_picture_number + i;
  1206. if (pict_num >= s->rc_context.num_entries)
  1207. break;
  1208. if (!s->input_picture[i]) {
  1209. s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
  1210. break;
  1211. }
  1212. s->input_picture[i]->f->pict_type =
  1213. s->rc_context.entry[pict_num].new_pict_type;
  1214. }
  1215. }
  1216. if (s->b_frame_strategy == 0) {
  1217. b_frames = s->max_b_frames;
  1218. while (b_frames && !s->input_picture[b_frames])
  1219. b_frames--;
  1220. } else if (s->b_frame_strategy == 1) {
  1221. for (i = 1; i < s->max_b_frames + 1; i++) {
  1222. if (s->input_picture[i] &&
  1223. s->input_picture[i]->b_frame_score == 0) {
  1224. s->input_picture[i]->b_frame_score =
  1225. get_intra_count(s,
  1226. s->input_picture[i ]->f->data[0],
  1227. s->input_picture[i - 1]->f->data[0],
  1228. s->linesize) + 1;
  1229. }
  1230. }
  1231. for (i = 0; i < s->max_b_frames + 1; i++) {
  1232. if (!s->input_picture[i] ||
  1233. s->input_picture[i]->b_frame_score - 1 >
  1234. s->mb_num / s->b_sensitivity)
  1235. break;
  1236. }
  1237. b_frames = FFMAX(0, i - 1);
  1238. /* reset scores */
  1239. for (i = 0; i < b_frames + 1; i++) {
  1240. s->input_picture[i]->b_frame_score = 0;
  1241. }
  1242. } else if (s->b_frame_strategy == 2) {
  1243. b_frames = estimate_best_b_count(s);
  1244. }
  1245. emms_c();
  1246. for (i = b_frames - 1; i >= 0; i--) {
  1247. int type = s->input_picture[i]->f->pict_type;
  1248. if (type && type != AV_PICTURE_TYPE_B)
  1249. b_frames = i;
  1250. }
  1251. if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
  1252. b_frames == s->max_b_frames) {
  1253. av_log(s->avctx, AV_LOG_ERROR,
  1254. "warning, too many B-frames in a row\n");
  1255. }
  1256. if (s->picture_in_gop_number + b_frames >= s->gop_size) {
  1257. if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
  1258. s->gop_size > s->picture_in_gop_number) {
  1259. b_frames = s->gop_size - s->picture_in_gop_number - 1;
  1260. } else {
  1261. if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
  1262. b_frames = 0;
  1263. s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
  1264. }
  1265. }
  1266. if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
  1267. s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
  1268. b_frames--;
  1269. s->reordered_input_picture[0] = s->input_picture[b_frames];
  1270. if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
  1271. s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
  1272. s->reordered_input_picture[0]->f->coded_picture_number =
  1273. s->coded_picture_number++;
  1274. for (i = 0; i < b_frames; i++) {
  1275. s->reordered_input_picture[i + 1] = s->input_picture[i];
  1276. s->reordered_input_picture[i + 1]->f->pict_type =
  1277. AV_PICTURE_TYPE_B;
  1278. s->reordered_input_picture[i + 1]->f->coded_picture_number =
  1279. s->coded_picture_number++;
  1280. }
  1281. }
  1282. }
  1283. no_output_pic:
  1284. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  1285. if (s->reordered_input_picture[0]) {
  1286. s->reordered_input_picture[0]->reference =
  1287. s->reordered_input_picture[0]->f->pict_type !=
  1288. AV_PICTURE_TYPE_B ? 3 : 0;
  1289. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))
  1290. return ret;
  1291. if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
  1292. // input is a shared pix, so we can't modify it -> allocate a new
  1293. // one & ensure that the shared one is reuseable
  1294. Picture *pic;
  1295. int i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1296. if (i < 0)
  1297. return i;
  1298. pic = &s->picture[i];
  1299. pic->reference = s->reordered_input_picture[0]->reference;
  1300. if (alloc_picture(s, pic, 0) < 0) {
  1301. return -1;
  1302. }
  1303. ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
  1304. if (ret < 0)
  1305. return ret;
  1306. /* mark us unused / free shared pic */
  1307. av_frame_unref(s->reordered_input_picture[0]->f);
  1308. s->reordered_input_picture[0]->shared = 0;
  1309. s->current_picture_ptr = pic;
  1310. } else {
  1311. // input is not a shared pix -> reuse buffer for current_pix
  1312. s->current_picture_ptr = s->reordered_input_picture[0];
  1313. for (i = 0; i < 4; i++) {
  1314. s->new_picture.f->data[i] += INPLACE_OFFSET;
  1315. }
  1316. }
  1317. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1318. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1319. s->current_picture_ptr)) < 0)
  1320. return ret;
  1321. s->picture_number = s->new_picture.f->display_picture_number;
  1322. }
  1323. return 0;
  1324. }
  1325. static void frame_end(MpegEncContext *s)
  1326. {
  1327. int i;
  1328. if (s->unrestricted_mv &&
  1329. s->current_picture.reference &&
  1330. !s->intra_only) {
  1331. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1332. int hshift = desc->log2_chroma_w;
  1333. int vshift = desc->log2_chroma_h;
  1334. s->mpvencdsp.draw_edges(s->current_picture.f->data[0], s->linesize,
  1335. s->h_edge_pos, s->v_edge_pos,
  1336. EDGE_WIDTH, EDGE_WIDTH,
  1337. EDGE_TOP | EDGE_BOTTOM);
  1338. s->mpvencdsp.draw_edges(s->current_picture.f->data[1], s->uvlinesize,
  1339. s->h_edge_pos >> hshift,
  1340. s->v_edge_pos >> vshift,
  1341. EDGE_WIDTH >> hshift,
  1342. EDGE_WIDTH >> vshift,
  1343. EDGE_TOP | EDGE_BOTTOM);
  1344. s->mpvencdsp.draw_edges(s->current_picture.f->data[2], s->uvlinesize,
  1345. s->h_edge_pos >> hshift,
  1346. s->v_edge_pos >> vshift,
  1347. EDGE_WIDTH >> hshift,
  1348. EDGE_WIDTH >> vshift,
  1349. EDGE_TOP | EDGE_BOTTOM);
  1350. }
  1351. emms_c();
  1352. s->last_pict_type = s->pict_type;
  1353. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
  1354. if (s->pict_type!= AV_PICTURE_TYPE_B)
  1355. s->last_non_b_pict_type = s->pict_type;
  1356. if (s->encoding) {
  1357. /* release non-reference frames */
  1358. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1359. if (!s->picture[i].reference)
  1360. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1361. }
  1362. }
  1363. #if FF_API_CODED_FRAME
  1364. FF_DISABLE_DEPRECATION_WARNINGS
  1365. av_frame_copy_props(s->avctx->coded_frame, s->current_picture.f);
  1366. FF_ENABLE_DEPRECATION_WARNINGS
  1367. #endif
  1368. #if FF_API_ERROR_FRAME
  1369. FF_DISABLE_DEPRECATION_WARNINGS
  1370. memcpy(s->current_picture.f->error, s->current_picture.encoding_error,
  1371. sizeof(s->current_picture.encoding_error));
  1372. FF_ENABLE_DEPRECATION_WARNINGS
  1373. #endif
  1374. }
  1375. static void update_noise_reduction(MpegEncContext *s)
  1376. {
  1377. int intra, i;
  1378. for (intra = 0; intra < 2; intra++) {
  1379. if (s->dct_count[intra] > (1 << 16)) {
  1380. for (i = 0; i < 64; i++) {
  1381. s->dct_error_sum[intra][i] >>= 1;
  1382. }
  1383. s->dct_count[intra] >>= 1;
  1384. }
  1385. for (i = 0; i < 64; i++) {
  1386. s->dct_offset[intra][i] = (s->noise_reduction *
  1387. s->dct_count[intra] +
  1388. s->dct_error_sum[intra][i] / 2) /
  1389. (s->dct_error_sum[intra][i] + 1);
  1390. }
  1391. }
  1392. }
  1393. static int frame_start(MpegEncContext *s)
  1394. {
  1395. int ret;
  1396. /* mark & release old frames */
  1397. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1398. s->last_picture_ptr != s->next_picture_ptr &&
  1399. s->last_picture_ptr->f->buf[0]) {
  1400. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  1401. }
  1402. s->current_picture_ptr->f->pict_type = s->pict_type;
  1403. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1404. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1405. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1406. s->current_picture_ptr)) < 0)
  1407. return ret;
  1408. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1409. s->last_picture_ptr = s->next_picture_ptr;
  1410. if (!s->droppable)
  1411. s->next_picture_ptr = s->current_picture_ptr;
  1412. }
  1413. if (s->last_picture_ptr) {
  1414. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1415. if (s->last_picture_ptr->f->buf[0] &&
  1416. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1417. s->last_picture_ptr)) < 0)
  1418. return ret;
  1419. }
  1420. if (s->next_picture_ptr) {
  1421. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1422. if (s->next_picture_ptr->f->buf[0] &&
  1423. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1424. s->next_picture_ptr)) < 0)
  1425. return ret;
  1426. }
  1427. if (s->picture_structure!= PICT_FRAME) {
  1428. int i;
  1429. for (i = 0; i < 4; i++) {
  1430. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1431. s->current_picture.f->data[i] +=
  1432. s->current_picture.f->linesize[i];
  1433. }
  1434. s->current_picture.f->linesize[i] *= 2;
  1435. s->last_picture.f->linesize[i] *= 2;
  1436. s->next_picture.f->linesize[i] *= 2;
  1437. }
  1438. }
  1439. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1440. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1441. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1442. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1443. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1444. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1445. } else {
  1446. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1447. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1448. }
  1449. if (s->dct_error_sum) {
  1450. assert(s->noise_reduction && s->encoding);
  1451. update_noise_reduction(s);
  1452. }
  1453. return 0;
  1454. }
  1455. int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  1456. const AVFrame *pic_arg, int *got_packet)
  1457. {
  1458. MpegEncContext *s = avctx->priv_data;
  1459. int i, stuffing_count, ret;
  1460. int context_count = s->slice_context_count;
  1461. s->picture_in_gop_number++;
  1462. if (load_input_picture(s, pic_arg) < 0)
  1463. return -1;
  1464. if (select_input_picture(s) < 0) {
  1465. return -1;
  1466. }
  1467. /* output? */
  1468. if (s->new_picture.f->data[0]) {
  1469. uint8_t *sd;
  1470. if (!pkt->data &&
  1471. (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
  1472. return ret;
  1473. if (s->mb_info) {
  1474. s->mb_info_ptr = av_packet_new_side_data(pkt,
  1475. AV_PKT_DATA_H263_MB_INFO,
  1476. s->mb_width*s->mb_height*12);
  1477. s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
  1478. }
  1479. for (i = 0; i < context_count; i++) {
  1480. int start_y = s->thread_context[i]->start_mb_y;
  1481. int end_y = s->thread_context[i]-> end_mb_y;
  1482. int h = s->mb_height;
  1483. uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
  1484. uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
  1485. init_put_bits(&s->thread_context[i]->pb, start, end - start);
  1486. }
  1487. s->pict_type = s->new_picture.f->pict_type;
  1488. //emms_c();
  1489. ret = frame_start(s);
  1490. if (ret < 0)
  1491. return ret;
  1492. vbv_retry:
  1493. if (encode_picture(s, s->picture_number) < 0)
  1494. return -1;
  1495. #if FF_API_STAT_BITS
  1496. FF_DISABLE_DEPRECATION_WARNINGS
  1497. avctx->header_bits = s->header_bits;
  1498. avctx->mv_bits = s->mv_bits;
  1499. avctx->misc_bits = s->misc_bits;
  1500. avctx->i_tex_bits = s->i_tex_bits;
  1501. avctx->p_tex_bits = s->p_tex_bits;
  1502. avctx->i_count = s->i_count;
  1503. // FIXME f/b_count in avctx
  1504. avctx->p_count = s->mb_num - s->i_count - s->skip_count;
  1505. avctx->skip_count = s->skip_count;
  1506. FF_ENABLE_DEPRECATION_WARNINGS
  1507. #endif
  1508. frame_end(s);
  1509. sd = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
  1510. sizeof(int));
  1511. if (!sd)
  1512. return AVERROR(ENOMEM);
  1513. *(int *)sd = s->current_picture.f->quality;
  1514. if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
  1515. ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
  1516. if (avctx->rc_buffer_size) {
  1517. RateControlContext *rcc = &s->rc_context;
  1518. int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
  1519. if (put_bits_count(&s->pb) > max_size &&
  1520. s->lambda < s->lmax) {
  1521. s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
  1522. (s->qscale + 1) / s->qscale);
  1523. if (s->adaptive_quant) {
  1524. int i;
  1525. for (i = 0; i < s->mb_height * s->mb_stride; i++)
  1526. s->lambda_table[i] =
  1527. FFMAX(s->lambda_table[i] + 1,
  1528. s->lambda_table[i] * (s->qscale + 1) /
  1529. s->qscale);
  1530. }
  1531. s->mb_skipped = 0; // done in frame_start()
  1532. // done in encode_picture() so we must undo it
  1533. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1534. if (s->flipflop_rounding ||
  1535. s->codec_id == AV_CODEC_ID_H263P ||
  1536. s->codec_id == AV_CODEC_ID_MPEG4)
  1537. s->no_rounding ^= 1;
  1538. }
  1539. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1540. s->time_base = s->last_time_base;
  1541. s->last_non_b_time = s->time - s->pp_time;
  1542. }
  1543. for (i = 0; i < context_count; i++) {
  1544. PutBitContext *pb = &s->thread_context[i]->pb;
  1545. init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
  1546. }
  1547. goto vbv_retry;
  1548. }
  1549. assert(s->avctx->rc_max_rate);
  1550. }
  1551. if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
  1552. ff_write_pass1_stats(s);
  1553. for (i = 0; i < 4; i++) {
  1554. s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
  1555. avctx->error[i] += s->current_picture_ptr->encoding_error[i];
  1556. }
  1557. if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
  1558. assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
  1559. s->misc_bits + s->i_tex_bits +
  1560. s->p_tex_bits);
  1561. flush_put_bits(&s->pb);
  1562. s->frame_bits = put_bits_count(&s->pb);
  1563. stuffing_count = ff_vbv_update(s, s->frame_bits);
  1564. if (stuffing_count) {
  1565. if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
  1566. stuffing_count + 50) {
  1567. av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
  1568. return -1;
  1569. }
  1570. switch (s->codec_id) {
  1571. case AV_CODEC_ID_MPEG1VIDEO:
  1572. case AV_CODEC_ID_MPEG2VIDEO:
  1573. while (stuffing_count--) {
  1574. put_bits(&s->pb, 8, 0);
  1575. }
  1576. break;
  1577. case AV_CODEC_ID_MPEG4:
  1578. put_bits(&s->pb, 16, 0);
  1579. put_bits(&s->pb, 16, 0x1C3);
  1580. stuffing_count -= 4;
  1581. while (stuffing_count--) {
  1582. put_bits(&s->pb, 8, 0xFF);
  1583. }
  1584. break;
  1585. default:
  1586. av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  1587. }
  1588. flush_put_bits(&s->pb);
  1589. s->frame_bits = put_bits_count(&s->pb);
  1590. }
  1591. /* update MPEG-1/2 vbv_delay for CBR */
  1592. if (s->avctx->rc_max_rate &&
  1593. s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
  1594. s->out_format == FMT_MPEG1 &&
  1595. 90000LL * (avctx->rc_buffer_size - 1) <=
  1596. s->avctx->rc_max_rate * 0xFFFFLL) {
  1597. AVCPBProperties *props;
  1598. size_t props_size;
  1599. int vbv_delay, min_delay;
  1600. double inbits = s->avctx->rc_max_rate *
  1601. av_q2d(s->avctx->time_base);
  1602. int minbits = s->frame_bits - 8 *
  1603. (s->vbv_delay_ptr - s->pb.buf - 1);
  1604. double bits = s->rc_context.buffer_index + minbits - inbits;
  1605. if (bits < 0)
  1606. av_log(s->avctx, AV_LOG_ERROR,
  1607. "Internal error, negative bits\n");
  1608. assert(s->repeat_first_field == 0);
  1609. vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
  1610. min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
  1611. s->avctx->rc_max_rate;
  1612. vbv_delay = FFMAX(vbv_delay, min_delay);
  1613. assert(vbv_delay < 0xFFFF);
  1614. s->vbv_delay_ptr[0] &= 0xF8;
  1615. s->vbv_delay_ptr[0] |= vbv_delay >> 13;
  1616. s->vbv_delay_ptr[1] = vbv_delay >> 5;
  1617. s->vbv_delay_ptr[2] &= 0x07;
  1618. s->vbv_delay_ptr[2] |= vbv_delay << 3;
  1619. props = av_cpb_properties_alloc(&props_size);
  1620. if (!props)
  1621. return AVERROR(ENOMEM);
  1622. props->vbv_delay = vbv_delay * 300;
  1623. ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
  1624. (uint8_t*)props, props_size);
  1625. if (ret < 0) {
  1626. av_freep(&props);
  1627. return ret;
  1628. }
  1629. #if FF_API_VBV_DELAY
  1630. FF_DISABLE_DEPRECATION_WARNINGS
  1631. avctx->vbv_delay = vbv_delay * 300;
  1632. FF_ENABLE_DEPRECATION_WARNINGS
  1633. #endif
  1634. }
  1635. s->total_bits += s->frame_bits;
  1636. #if FF_API_STAT_BITS
  1637. FF_DISABLE_DEPRECATION_WARNINGS
  1638. avctx->frame_bits = s->frame_bits;
  1639. FF_ENABLE_DEPRECATION_WARNINGS
  1640. #endif
  1641. pkt->pts = s->current_picture.f->pts;
  1642. if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
  1643. if (!s->current_picture.f->coded_picture_number)
  1644. pkt->dts = pkt->pts - s->dts_delta;
  1645. else
  1646. pkt->dts = s->reordered_pts;
  1647. s->reordered_pts = pkt->pts;
  1648. } else
  1649. pkt->dts = pkt->pts;
  1650. if (s->current_picture.f->key_frame)
  1651. pkt->flags |= AV_PKT_FLAG_KEY;
  1652. if (s->mb_info)
  1653. av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
  1654. } else {
  1655. s->frame_bits = 0;
  1656. }
  1657. assert((s->frame_bits & 7) == 0);
  1658. pkt->size = s->frame_bits / 8;
  1659. *got_packet = !!pkt->size;
  1660. return 0;
  1661. }
  1662. static inline void dct_single_coeff_elimination(MpegEncContext *s,
  1663. int n, int threshold)
  1664. {
  1665. static const char tab[64] = {
  1666. 3, 2, 2, 1, 1, 1, 1, 1,
  1667. 1, 1, 1, 1, 1, 1, 1, 1,
  1668. 1, 1, 1, 1, 1, 1, 1, 1,
  1669. 0, 0, 0, 0, 0, 0, 0, 0,
  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. };
  1675. int score = 0;
  1676. int run = 0;
  1677. int i;
  1678. int16_t *block = s->block[n];
  1679. const int last_index = s->block_last_index[n];
  1680. int skip_dc;
  1681. if (threshold < 0) {
  1682. skip_dc = 0;
  1683. threshold = -threshold;
  1684. } else
  1685. skip_dc = 1;
  1686. /* Are all we could set to zero already zero? */
  1687. if (last_index <= skip_dc - 1)
  1688. return;
  1689. for (i = 0; i <= last_index; i++) {
  1690. const int j = s->intra_scantable.permutated[i];
  1691. const int level = FFABS(block[j]);
  1692. if (level == 1) {
  1693. if (skip_dc && i == 0)
  1694. continue;
  1695. score += tab[run];
  1696. run = 0;
  1697. } else if (level > 1) {
  1698. return;
  1699. } else {
  1700. run++;
  1701. }
  1702. }
  1703. if (score >= threshold)
  1704. return;
  1705. for (i = skip_dc; i <= last_index; i++) {
  1706. const int j = s->intra_scantable.permutated[i];
  1707. block[j] = 0;
  1708. }
  1709. if (block[0])
  1710. s->block_last_index[n] = 0;
  1711. else
  1712. s->block_last_index[n] = -1;
  1713. }
  1714. static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
  1715. int last_index)
  1716. {
  1717. int i;
  1718. const int maxlevel = s->max_qcoeff;
  1719. const int minlevel = s->min_qcoeff;
  1720. int overflow = 0;
  1721. if (s->mb_intra) {
  1722. i = 1; // skip clipping of intra dc
  1723. } else
  1724. i = 0;
  1725. for (; i <= last_index; i++) {
  1726. const int j = s->intra_scantable.permutated[i];
  1727. int level = block[j];
  1728. if (level > maxlevel) {
  1729. level = maxlevel;
  1730. overflow++;
  1731. } else if (level < minlevel) {
  1732. level = minlevel;
  1733. overflow++;
  1734. }
  1735. block[j] = level;
  1736. }
  1737. if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  1738. av_log(s->avctx, AV_LOG_INFO,
  1739. "warning, clipping %d dct coefficients to %d..%d\n",
  1740. overflow, minlevel, maxlevel);
  1741. }
  1742. static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
  1743. {
  1744. int x, y;
  1745. // FIXME optimize
  1746. for (y = 0; y < 8; y++) {
  1747. for (x = 0; x < 8; x++) {
  1748. int x2, y2;
  1749. int sum = 0;
  1750. int sqr = 0;
  1751. int count = 0;
  1752. for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
  1753. for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
  1754. int v = ptr[x2 + y2 * stride];
  1755. sum += v;
  1756. sqr += v * v;
  1757. count++;
  1758. }
  1759. }
  1760. weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
  1761. }
  1762. }
  1763. }
  1764. static av_always_inline void encode_mb_internal(MpegEncContext *s,
  1765. int motion_x, int motion_y,
  1766. int mb_block_height,
  1767. int mb_block_count)
  1768. {
  1769. int16_t weight[8][64];
  1770. int16_t orig[8][64];
  1771. const int mb_x = s->mb_x;
  1772. const int mb_y = s->mb_y;
  1773. int i;
  1774. int skip_dct[8];
  1775. int dct_offset = s->linesize * 8; // default for progressive frames
  1776. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1777. ptrdiff_t wrap_y, wrap_c;
  1778. for (i = 0; i < mb_block_count; i++)
  1779. skip_dct[i] = s->skipdct;
  1780. if (s->adaptive_quant) {
  1781. const int last_qp = s->qscale;
  1782. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1783. s->lambda = s->lambda_table[mb_xy];
  1784. update_qscale(s);
  1785. if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
  1786. s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
  1787. s->dquant = s->qscale - last_qp;
  1788. if (s->out_format == FMT_H263) {
  1789. s->dquant = av_clip(s->dquant, -2, 2);
  1790. if (s->codec_id == AV_CODEC_ID_MPEG4) {
  1791. if (!s->mb_intra) {
  1792. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1793. if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
  1794. s->dquant = 0;
  1795. }
  1796. if (s->mv_type == MV_TYPE_8X8)
  1797. s->dquant = 0;
  1798. }
  1799. }
  1800. }
  1801. }
  1802. ff_set_qscale(s, last_qp + s->dquant);
  1803. } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
  1804. ff_set_qscale(s, s->qscale + s->dquant);
  1805. wrap_y = s->linesize;
  1806. wrap_c = s->uvlinesize;
  1807. ptr_y = s->new_picture.f->data[0] +
  1808. (mb_y * 16 * wrap_y) + mb_x * 16;
  1809. ptr_cb = s->new_picture.f->data[1] +
  1810. (mb_y * mb_block_height * wrap_c) + mb_x * 8;
  1811. ptr_cr = s->new_picture.f->data[2] +
  1812. (mb_y * mb_block_height * wrap_c) + mb_x * 8;
  1813. if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
  1814. uint8_t *ebuf = s->sc.edge_emu_buffer + 32;
  1815. s->vdsp.emulated_edge_mc(ebuf, ptr_y,
  1816. wrap_y, wrap_y,
  1817. 16, 16, mb_x * 16, mb_y * 16,
  1818. s->width, s->height);
  1819. ptr_y = ebuf;
  1820. s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
  1821. wrap_c, wrap_c,
  1822. 8, mb_block_height, mb_x * 8, mb_y * 8,
  1823. s->width >> 1, s->height >> 1);
  1824. ptr_cb = ebuf + 18 * wrap_y;
  1825. s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr,
  1826. wrap_c, wrap_c,
  1827. 8, mb_block_height, mb_x * 8, mb_y * 8,
  1828. s->width >> 1, s->height >> 1);
  1829. ptr_cr = ebuf + 18 * wrap_y + 8;
  1830. }
  1831. if (s->mb_intra) {
  1832. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  1833. int progressive_score, interlaced_score;
  1834. s->interlaced_dct = 0;
  1835. progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
  1836. s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
  1837. NULL, wrap_y, 8) - 400;
  1838. if (progressive_score > 0) {
  1839. interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
  1840. NULL, wrap_y * 2, 8) +
  1841. s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
  1842. NULL, wrap_y * 2, 8);
  1843. if (progressive_score > interlaced_score) {
  1844. s->interlaced_dct = 1;
  1845. dct_offset = wrap_y;
  1846. wrap_y <<= 1;
  1847. if (s->chroma_format == CHROMA_422)
  1848. wrap_c <<= 1;
  1849. }
  1850. }
  1851. }
  1852. s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
  1853. s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
  1854. s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
  1855. s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
  1856. if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  1857. skip_dct[4] = 1;
  1858. skip_dct[5] = 1;
  1859. } else {
  1860. s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
  1861. s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
  1862. if (!s->chroma_y_shift) { /* 422 */
  1863. s->pdsp.get_pixels(s->block[6],
  1864. ptr_cb + (dct_offset >> 1), wrap_c);
  1865. s->pdsp.get_pixels(s->block[7],
  1866. ptr_cr + (dct_offset >> 1), wrap_c);
  1867. }
  1868. }
  1869. } else {
  1870. op_pixels_func (*op_pix)[4];
  1871. qpel_mc_func (*op_qpix)[16];
  1872. uint8_t *dest_y, *dest_cb, *dest_cr;
  1873. dest_y = s->dest[0];
  1874. dest_cb = s->dest[1];
  1875. dest_cr = s->dest[2];
  1876. if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
  1877. op_pix = s->hdsp.put_pixels_tab;
  1878. op_qpix = s->qdsp.put_qpel_pixels_tab;
  1879. } else {
  1880. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1881. op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
  1882. }
  1883. if (s->mv_dir & MV_DIR_FORWARD) {
  1884. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
  1885. s->last_picture.f->data,
  1886. op_pix, op_qpix);
  1887. op_pix = s->hdsp.avg_pixels_tab;
  1888. op_qpix = s->qdsp.avg_qpel_pixels_tab;
  1889. }
  1890. if (s->mv_dir & MV_DIR_BACKWARD) {
  1891. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
  1892. s->next_picture.f->data,
  1893. op_pix, op_qpix);
  1894. }
  1895. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  1896. int progressive_score, interlaced_score;
  1897. s->interlaced_dct = 0;
  1898. progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
  1899. s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
  1900. ptr_y + wrap_y * 8,
  1901. wrap_y, 8) - 400;
  1902. if (s->avctx->ildct_cmp == FF_CMP_VSSE)
  1903. progressive_score -= 400;
  1904. if (progressive_score > 0) {
  1905. interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
  1906. wrap_y * 2, 8) +
  1907. s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
  1908. ptr_y + wrap_y,
  1909. wrap_y * 2, 8);
  1910. if (progressive_score > interlaced_score) {
  1911. s->interlaced_dct = 1;
  1912. dct_offset = wrap_y;
  1913. wrap_y <<= 1;
  1914. if (s->chroma_format == CHROMA_422)
  1915. wrap_c <<= 1;
  1916. }
  1917. }
  1918. }
  1919. s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
  1920. s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1921. s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
  1922. dest_y + dct_offset, wrap_y);
  1923. s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
  1924. dest_y + dct_offset + 8, wrap_y);
  1925. if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  1926. skip_dct[4] = 1;
  1927. skip_dct[5] = 1;
  1928. } else {
  1929. s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1930. s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1931. if (!s->chroma_y_shift) { /* 422 */
  1932. s->pdsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
  1933. dest_cb + (dct_offset >> 1), wrap_c);
  1934. s->pdsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
  1935. dest_cr + (dct_offset >> 1), wrap_c);
  1936. }
  1937. }
  1938. /* pre quantization */
  1939. if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
  1940. 2 * s->qscale * s->qscale) {
  1941. // FIXME optimize
  1942. if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
  1943. skip_dct[0] = 1;
  1944. if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
  1945. skip_dct[1] = 1;
  1946. if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
  1947. wrap_y, 8) < 20 * s->qscale)
  1948. skip_dct[2] = 1;
  1949. if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
  1950. wrap_y, 8) < 20 * s->qscale)
  1951. skip_dct[3] = 1;
  1952. if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
  1953. skip_dct[4] = 1;
  1954. if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
  1955. skip_dct[5] = 1;
  1956. if (!s->chroma_y_shift) { /* 422 */
  1957. if (s->mecc.sad[1](NULL, ptr_cb + (dct_offset >> 1),
  1958. dest_cb + (dct_offset >> 1),
  1959. wrap_c, 8) < 20 * s->qscale)
  1960. skip_dct[6] = 1;
  1961. if (s->mecc.sad[1](NULL, ptr_cr + (dct_offset >> 1),
  1962. dest_cr + (dct_offset >> 1),
  1963. wrap_c, 8) < 20 * s->qscale)
  1964. skip_dct[7] = 1;
  1965. }
  1966. }
  1967. }
  1968. if (s->quantizer_noise_shaping) {
  1969. if (!skip_dct[0])
  1970. get_visual_weight(weight[0], ptr_y , wrap_y);
  1971. if (!skip_dct[1])
  1972. get_visual_weight(weight[1], ptr_y + 8, wrap_y);
  1973. if (!skip_dct[2])
  1974. get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
  1975. if (!skip_dct[3])
  1976. get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
  1977. if (!skip_dct[4])
  1978. get_visual_weight(weight[4], ptr_cb , wrap_c);
  1979. if (!skip_dct[5])
  1980. get_visual_weight(weight[5], ptr_cr , wrap_c);
  1981. if (!s->chroma_y_shift) { /* 422 */
  1982. if (!skip_dct[6])
  1983. get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
  1984. wrap_c);
  1985. if (!skip_dct[7])
  1986. get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
  1987. wrap_c);
  1988. }
  1989. memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
  1990. }
  1991. /* DCT & quantize */
  1992. assert(s->out_format != FMT_MJPEG || s->qscale == 8);
  1993. {
  1994. for (i = 0; i < mb_block_count; i++) {
  1995. if (!skip_dct[i]) {
  1996. int overflow;
  1997. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1998. // FIXME we could decide to change to quantizer instead of
  1999. // clipping
  2000. // JS: I don't think that would be a good idea it could lower
  2001. // quality instead of improve it. Just INTRADC clipping
  2002. // deserves changes in quantizer
  2003. if (overflow)
  2004. clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2005. } else
  2006. s->block_last_index[i] = -1;
  2007. }
  2008. if (s->quantizer_noise_shaping) {
  2009. for (i = 0; i < mb_block_count; i++) {
  2010. if (!skip_dct[i]) {
  2011. s->block_last_index[i] =
  2012. dct_quantize_refine(s, s->block[i], weight[i],
  2013. orig[i], i, s->qscale);
  2014. }
  2015. }
  2016. }
  2017. if (s->luma_elim_threshold && !s->mb_intra)
  2018. for (i = 0; i < 4; i++)
  2019. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2020. if (s->chroma_elim_threshold && !s->mb_intra)
  2021. for (i = 4; i < mb_block_count; i++)
  2022. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2023. if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
  2024. for (i = 0; i < mb_block_count; i++) {
  2025. if (s->block_last_index[i] == -1)
  2026. s->coded_score[i] = INT_MAX / 256;
  2027. }
  2028. }
  2029. }
  2030. if ((s->avctx->flags & AV_CODEC_FLAG_GRAY) && s->mb_intra) {
  2031. s->block_last_index[4] =
  2032. s->block_last_index[5] = 0;
  2033. s->block[4][0] =
  2034. s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
  2035. }
  2036. // non c quantize code returns incorrect block_last_index FIXME
  2037. if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
  2038. for (i = 0; i < mb_block_count; i++) {
  2039. int j;
  2040. if (s->block_last_index[i] > 0) {
  2041. for (j = 63; j > 0; j--) {
  2042. if (s->block[i][s->intra_scantable.permutated[j]])
  2043. break;
  2044. }
  2045. s->block_last_index[i] = j;
  2046. }
  2047. }
  2048. }
  2049. /* huffman encode */
  2050. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  2051. case AV_CODEC_ID_MPEG1VIDEO:
  2052. case AV_CODEC_ID_MPEG2VIDEO:
  2053. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  2054. ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  2055. break;
  2056. case AV_CODEC_ID_MPEG4:
  2057. if (CONFIG_MPEG4_ENCODER)
  2058. ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  2059. break;
  2060. case AV_CODEC_ID_MSMPEG4V2:
  2061. case AV_CODEC_ID_MSMPEG4V3:
  2062. case AV_CODEC_ID_WMV1:
  2063. if (CONFIG_MSMPEG4_ENCODER)
  2064. ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  2065. break;
  2066. case AV_CODEC_ID_WMV2:
  2067. if (CONFIG_WMV2_ENCODER)
  2068. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
  2069. break;
  2070. case AV_CODEC_ID_H261:
  2071. if (CONFIG_H261_ENCODER)
  2072. ff_h261_encode_mb(s, s->block, motion_x, motion_y);
  2073. break;
  2074. case AV_CODEC_ID_H263:
  2075. case AV_CODEC_ID_H263P:
  2076. case AV_CODEC_ID_FLV1:
  2077. case AV_CODEC_ID_RV10:
  2078. case AV_CODEC_ID_RV20:
  2079. if (CONFIG_H263_ENCODER)
  2080. ff_h263_encode_mb(s, s->block, motion_x, motion_y);
  2081. break;
  2082. case AV_CODEC_ID_MJPEG:
  2083. if (CONFIG_MJPEG_ENCODER)
  2084. ff_mjpeg_encode_mb(s, s->block);
  2085. break;
  2086. default:
  2087. assert(0);
  2088. }
  2089. }
  2090. static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2091. {
  2092. if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
  2093. else encode_mb_internal(s, motion_x, motion_y, 16, 8);
  2094. }
  2095. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2096. int i;
  2097. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
  2098. /* MPEG-1 */
  2099. d->mb_skip_run= s->mb_skip_run;
  2100. for(i=0; i<3; i++)
  2101. d->last_dc[i] = s->last_dc[i];
  2102. /* statistics */
  2103. d->mv_bits= s->mv_bits;
  2104. d->i_tex_bits= s->i_tex_bits;
  2105. d->p_tex_bits= s->p_tex_bits;
  2106. d->i_count= s->i_count;
  2107. d->f_count= s->f_count;
  2108. d->b_count= s->b_count;
  2109. d->skip_count= s->skip_count;
  2110. d->misc_bits= s->misc_bits;
  2111. d->last_bits= 0;
  2112. d->mb_skipped= 0;
  2113. d->qscale= s->qscale;
  2114. d->dquant= s->dquant;
  2115. d->esc3_level_length= s->esc3_level_length;
  2116. }
  2117. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2118. int i;
  2119. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2120. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
  2121. /* MPEG-1 */
  2122. d->mb_skip_run= s->mb_skip_run;
  2123. for(i=0; i<3; i++)
  2124. d->last_dc[i] = s->last_dc[i];
  2125. /* statistics */
  2126. d->mv_bits= s->mv_bits;
  2127. d->i_tex_bits= s->i_tex_bits;
  2128. d->p_tex_bits= s->p_tex_bits;
  2129. d->i_count= s->i_count;
  2130. d->f_count= s->f_count;
  2131. d->b_count= s->b_count;
  2132. d->skip_count= s->skip_count;
  2133. d->misc_bits= s->misc_bits;
  2134. d->mb_intra= s->mb_intra;
  2135. d->mb_skipped= s->mb_skipped;
  2136. d->mv_type= s->mv_type;
  2137. d->mv_dir= s->mv_dir;
  2138. d->pb= s->pb;
  2139. if(s->data_partitioning){
  2140. d->pb2= s->pb2;
  2141. d->tex_pb= s->tex_pb;
  2142. }
  2143. d->block= s->block;
  2144. for(i=0; i<8; i++)
  2145. d->block_last_index[i]= s->block_last_index[i];
  2146. d->interlaced_dct= s->interlaced_dct;
  2147. d->qscale= s->qscale;
  2148. d->esc3_level_length= s->esc3_level_length;
  2149. }
  2150. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  2151. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2152. int *dmin, int *next_block, int motion_x, int motion_y)
  2153. {
  2154. int score;
  2155. uint8_t *dest_backup[3];
  2156. copy_context_before_encode(s, backup, type);
  2157. s->block= s->blocks[*next_block];
  2158. s->pb= pb[*next_block];
  2159. if(s->data_partitioning){
  2160. s->pb2 = pb2 [*next_block];
  2161. s->tex_pb= tex_pb[*next_block];
  2162. }
  2163. if(*next_block){
  2164. memcpy(dest_backup, s->dest, sizeof(s->dest));
  2165. s->dest[0] = s->sc.rd_scratchpad;
  2166. s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
  2167. s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
  2168. assert(s->linesize >= 32); //FIXME
  2169. }
  2170. encode_mb(s, motion_x, motion_y);
  2171. score= put_bits_count(&s->pb);
  2172. if(s->data_partitioning){
  2173. score+= put_bits_count(&s->pb2);
  2174. score+= put_bits_count(&s->tex_pb);
  2175. }
  2176. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  2177. ff_mpv_decode_mb(s, s->block);
  2178. score *= s->lambda2;
  2179. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  2180. }
  2181. if(*next_block){
  2182. memcpy(s->dest, dest_backup, sizeof(s->dest));
  2183. }
  2184. if(score<*dmin){
  2185. *dmin= score;
  2186. *next_block^=1;
  2187. copy_context_after_encode(best, s, type);
  2188. }
  2189. }
  2190. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  2191. uint32_t *sq = ff_square_tab + 256;
  2192. int acc=0;
  2193. int x,y;
  2194. if(w==16 && h==16)
  2195. return s->mecc.sse[0](NULL, src1, src2, stride, 16);
  2196. else if(w==8 && h==8)
  2197. return s->mecc.sse[1](NULL, src1, src2, stride, 8);
  2198. for(y=0; y<h; y++){
  2199. for(x=0; x<w; x++){
  2200. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  2201. }
  2202. }
  2203. assert(acc>=0);
  2204. return acc;
  2205. }
  2206. static int sse_mb(MpegEncContext *s){
  2207. int w= 16;
  2208. int h= 16;
  2209. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2210. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2211. if(w==16 && h==16)
  2212. if(s->avctx->mb_cmp == FF_CMP_NSSE){
  2213. 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) +
  2214. 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) +
  2215. 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);
  2216. }else{
  2217. 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) +
  2218. 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) +
  2219. 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);
  2220. }
  2221. else
  2222. 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)
  2223. +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)
  2224. +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);
  2225. }
  2226. static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
  2227. MpegEncContext *s= *(void**)arg;
  2228. s->me.pre_pass=1;
  2229. s->me.dia_size= s->avctx->pre_dia_size;
  2230. s->first_slice_line=1;
  2231. for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
  2232. for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
  2233. ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  2234. }
  2235. s->first_slice_line=0;
  2236. }
  2237. s->me.pre_pass=0;
  2238. return 0;
  2239. }
  2240. static int estimate_motion_thread(AVCodecContext *c, void *arg){
  2241. MpegEncContext *s= *(void**)arg;
  2242. s->me.dia_size= s->avctx->dia_size;
  2243. s->first_slice_line=1;
  2244. for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  2245. s->mb_x=0; //for block init below
  2246. ff_init_block_index(s);
  2247. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  2248. s->block_index[0]+=2;
  2249. s->block_index[1]+=2;
  2250. s->block_index[2]+=2;
  2251. s->block_index[3]+=2;
  2252. /* compute motion vector & mb_type and store in context */
  2253. if(s->pict_type==AV_PICTURE_TYPE_B)
  2254. ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
  2255. else
  2256. ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  2257. }
  2258. s->first_slice_line=0;
  2259. }
  2260. return 0;
  2261. }
  2262. static int mb_var_thread(AVCodecContext *c, void *arg){
  2263. MpegEncContext *s= *(void**)arg;
  2264. int mb_x, mb_y;
  2265. for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  2266. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2267. int xx = mb_x * 16;
  2268. int yy = mb_y * 16;
  2269. uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
  2270. int varc;
  2271. int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
  2272. varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
  2273. (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
  2274. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  2275. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  2276. s->me.mb_var_sum_temp += varc;
  2277. }
  2278. }
  2279. return 0;
  2280. }
  2281. static void write_slice_end(MpegEncContext *s){
  2282. if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
  2283. if(s->partitioned_frame){
  2284. ff_mpeg4_merge_partitions(s);
  2285. }
  2286. ff_mpeg4_stuffing(&s->pb);
  2287. }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
  2288. ff_mjpeg_encode_stuffing(&s->pb);
  2289. }
  2290. avpriv_align_put_bits(&s->pb);
  2291. flush_put_bits(&s->pb);
  2292. if ((s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
  2293. s->misc_bits+= get_bits_diff(s);
  2294. }
  2295. static void write_mb_info(MpegEncContext *s)
  2296. {
  2297. uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
  2298. int offset = put_bits_count(&s->pb);
  2299. int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
  2300. int gobn = s->mb_y / s->gob_index;
  2301. int pred_x, pred_y;
  2302. if (CONFIG_H263_ENCODER)
  2303. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  2304. bytestream_put_le32(&ptr, offset);
  2305. bytestream_put_byte(&ptr, s->qscale);
  2306. bytestream_put_byte(&ptr, gobn);
  2307. bytestream_put_le16(&ptr, mba);
  2308. bytestream_put_byte(&ptr, pred_x); /* hmv1 */
  2309. bytestream_put_byte(&ptr, pred_y); /* vmv1 */
  2310. /* 4MV not implemented */
  2311. bytestream_put_byte(&ptr, 0); /* hmv2 */
  2312. bytestream_put_byte(&ptr, 0); /* vmv2 */
  2313. }
  2314. static void update_mb_info(MpegEncContext *s, int startcode)
  2315. {
  2316. if (!s->mb_info)
  2317. return;
  2318. if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
  2319. s->mb_info_size += 12;
  2320. s->prev_mb_info = s->last_mb_info;
  2321. }
  2322. if (startcode) {
  2323. s->prev_mb_info = put_bits_count(&s->pb)/8;
  2324. /* This might have incremented mb_info_size above, and we return without
  2325. * actually writing any info into that slot yet. But in that case,
  2326. * this will be called again at the start of the after writing the
  2327. * start code, actually writing the mb info. */
  2328. return;
  2329. }
  2330. s->last_mb_info = put_bits_count(&s->pb)/8;
  2331. if (!s->mb_info_size)
  2332. s->mb_info_size += 12;
  2333. write_mb_info(s);
  2334. }
  2335. static int encode_thread(AVCodecContext *c, void *arg){
  2336. MpegEncContext *s= *(void**)arg;
  2337. int mb_x, mb_y;
  2338. int chr_h= 16>>s->chroma_y_shift;
  2339. int i, j;
  2340. MpegEncContext best_s = { 0 }, backup_s;
  2341. uint8_t bit_buf[2][MAX_MB_BYTES];
  2342. uint8_t bit_buf2[2][MAX_MB_BYTES];
  2343. uint8_t bit_buf_tex[2][MAX_MB_BYTES];
  2344. PutBitContext pb[2], pb2[2], tex_pb[2];
  2345. for(i=0; i<2; i++){
  2346. init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
  2347. init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
  2348. init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
  2349. }
  2350. s->last_bits= put_bits_count(&s->pb);
  2351. s->mv_bits=0;
  2352. s->misc_bits=0;
  2353. s->i_tex_bits=0;
  2354. s->p_tex_bits=0;
  2355. s->i_count=0;
  2356. s->f_count=0;
  2357. s->b_count=0;
  2358. s->skip_count=0;
  2359. for(i=0; i<3; i++){
  2360. /* init last dc values */
  2361. /* note: quant matrix value (8) is implied here */
  2362. s->last_dc[i] = 128 << s->intra_dc_precision;
  2363. s->current_picture.encoding_error[i] = 0;
  2364. }
  2365. s->mb_skip_run = 0;
  2366. memset(s->last_mv, 0, sizeof(s->last_mv));
  2367. s->last_mv_dir = 0;
  2368. switch(s->codec_id){
  2369. case AV_CODEC_ID_H263:
  2370. case AV_CODEC_ID_H263P:
  2371. case AV_CODEC_ID_FLV1:
  2372. if (CONFIG_H263_ENCODER)
  2373. s->gob_index = H263_GOB_HEIGHT(s->height);
  2374. break;
  2375. case AV_CODEC_ID_MPEG4:
  2376. if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
  2377. ff_mpeg4_init_partitions(s);
  2378. break;
  2379. }
  2380. s->resync_mb_x=0;
  2381. s->resync_mb_y=0;
  2382. s->first_slice_line = 1;
  2383. s->ptr_lastgob = s->pb.buf;
  2384. for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  2385. s->mb_x=0;
  2386. s->mb_y= mb_y;
  2387. ff_set_qscale(s, s->qscale);
  2388. ff_init_block_index(s);
  2389. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2390. int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
  2391. int mb_type= s->mb_type[xy];
  2392. // int d;
  2393. int dmin= INT_MAX;
  2394. int dir;
  2395. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
  2396. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2397. return -1;
  2398. }
  2399. if(s->data_partitioning){
  2400. if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
  2401. || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
  2402. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2403. return -1;
  2404. }
  2405. }
  2406. s->mb_x = mb_x;
  2407. s->mb_y = mb_y; // moved into loop, can get changed by H.261
  2408. ff_update_block_index(s);
  2409. if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
  2410. ff_h261_reorder_mb_index(s);
  2411. xy= s->mb_y*s->mb_stride + s->mb_x;
  2412. mb_type= s->mb_type[xy];
  2413. }
  2414. /* write gob / video packet header */
  2415. if(s->rtp_mode){
  2416. int current_packet_size, is_gob_start;
  2417. current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
  2418. is_gob_start = s->rtp_payload_size &&
  2419. current_packet_size >= s->rtp_payload_size &&
  2420. mb_y + mb_x > 0;
  2421. if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
  2422. switch(s->codec_id){
  2423. case AV_CODEC_ID_H263:
  2424. case AV_CODEC_ID_H263P:
  2425. if(!s->h263_slice_structured)
  2426. if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  2427. break;
  2428. case AV_CODEC_ID_MPEG2VIDEO:
  2429. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  2430. case AV_CODEC_ID_MPEG1VIDEO:
  2431. if(s->mb_skip_run) is_gob_start=0;
  2432. break;
  2433. }
  2434. if(is_gob_start){
  2435. if(s->start_mb_y != mb_y || mb_x!=0){
  2436. write_slice_end(s);
  2437. if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
  2438. ff_mpeg4_init_partitions(s);
  2439. }
  2440. }
  2441. assert((put_bits_count(&s->pb)&7) == 0);
  2442. current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
  2443. if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
  2444. int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
  2445. int d = 100 / s->error_rate;
  2446. if(r % d == 0){
  2447. current_packet_size=0;
  2448. s->pb.buf_ptr= s->ptr_lastgob;
  2449. assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
  2450. }
  2451. }
  2452. #if FF_API_RTP_CALLBACK
  2453. FF_DISABLE_DEPRECATION_WARNINGS
  2454. if (s->avctx->rtp_callback){
  2455. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
  2456. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
  2457. }
  2458. FF_ENABLE_DEPRECATION_WARNINGS
  2459. #endif
  2460. update_mb_info(s, 1);
  2461. switch(s->codec_id){
  2462. case AV_CODEC_ID_MPEG4:
  2463. if (CONFIG_MPEG4_ENCODER) {
  2464. ff_mpeg4_encode_video_packet_header(s);
  2465. ff_mpeg4_clean_buffers(s);
  2466. }
  2467. break;
  2468. case AV_CODEC_ID_MPEG1VIDEO:
  2469. case AV_CODEC_ID_MPEG2VIDEO:
  2470. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
  2471. ff_mpeg1_encode_slice_header(s);
  2472. ff_mpeg1_clean_buffers(s);
  2473. }
  2474. break;
  2475. case AV_CODEC_ID_H263:
  2476. case AV_CODEC_ID_H263P:
  2477. if (CONFIG_H263_ENCODER)
  2478. ff_h263_encode_gob_header(s, mb_y);
  2479. break;
  2480. }
  2481. if (s->avctx->flags & AV_CODEC_FLAG_PASS1) {
  2482. int bits= put_bits_count(&s->pb);
  2483. s->misc_bits+= bits - s->last_bits;
  2484. s->last_bits= bits;
  2485. }
  2486. s->ptr_lastgob += current_packet_size;
  2487. s->first_slice_line=1;
  2488. s->resync_mb_x=mb_x;
  2489. s->resync_mb_y=mb_y;
  2490. }
  2491. }
  2492. if( (s->resync_mb_x == s->mb_x)
  2493. && s->resync_mb_y+1 == s->mb_y){
  2494. s->first_slice_line=0;
  2495. }
  2496. s->mb_skipped=0;
  2497. s->dquant=0; //only for QP_RD
  2498. update_mb_info(s, 0);
  2499. 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
  2500. int next_block=0;
  2501. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2502. copy_context_before_encode(&backup_s, s, -1);
  2503. backup_s.pb= s->pb;
  2504. best_s.data_partitioning= s->data_partitioning;
  2505. best_s.partitioned_frame= s->partitioned_frame;
  2506. if(s->data_partitioning){
  2507. backup_s.pb2= s->pb2;
  2508. backup_s.tex_pb= s->tex_pb;
  2509. }
  2510. if(mb_type&CANDIDATE_MB_TYPE_INTER){
  2511. s->mv_dir = MV_DIR_FORWARD;
  2512. s->mv_type = MV_TYPE_16X16;
  2513. s->mb_intra= 0;
  2514. s->mv[0][0][0] = s->p_mv_table[xy][0];
  2515. s->mv[0][0][1] = s->p_mv_table[xy][1];
  2516. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
  2517. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2518. }
  2519. if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
  2520. s->mv_dir = MV_DIR_FORWARD;
  2521. s->mv_type = MV_TYPE_FIELD;
  2522. s->mb_intra= 0;
  2523. for(i=0; i<2; i++){
  2524. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  2525. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  2526. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  2527. }
  2528. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
  2529. &dmin, &next_block, 0, 0);
  2530. }
  2531. if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
  2532. s->mv_dir = MV_DIR_FORWARD;
  2533. s->mv_type = MV_TYPE_16X16;
  2534. s->mb_intra= 0;
  2535. s->mv[0][0][0] = 0;
  2536. s->mv[0][0][1] = 0;
  2537. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
  2538. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2539. }
  2540. if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
  2541. s->mv_dir = MV_DIR_FORWARD;
  2542. s->mv_type = MV_TYPE_8X8;
  2543. s->mb_intra= 0;
  2544. for(i=0; i<4; i++){
  2545. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  2546. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  2547. }
  2548. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
  2549. &dmin, &next_block, 0, 0);
  2550. }
  2551. if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  2552. s->mv_dir = MV_DIR_FORWARD;
  2553. s->mv_type = MV_TYPE_16X16;
  2554. s->mb_intra= 0;
  2555. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2556. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2557. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
  2558. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2559. }
  2560. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  2561. s->mv_dir = MV_DIR_BACKWARD;
  2562. s->mv_type = MV_TYPE_16X16;
  2563. s->mb_intra= 0;
  2564. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2565. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2566. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  2567. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  2568. }
  2569. if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  2570. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2571. s->mv_type = MV_TYPE_16X16;
  2572. s->mb_intra= 0;
  2573. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2574. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2575. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2576. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2577. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
  2578. &dmin, &next_block, 0, 0);
  2579. }
  2580. if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
  2581. s->mv_dir = MV_DIR_FORWARD;
  2582. s->mv_type = MV_TYPE_FIELD;
  2583. s->mb_intra= 0;
  2584. for(i=0; i<2; i++){
  2585. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  2586. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  2587. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  2588. }
  2589. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
  2590. &dmin, &next_block, 0, 0);
  2591. }
  2592. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
  2593. s->mv_dir = MV_DIR_BACKWARD;
  2594. s->mv_type = MV_TYPE_FIELD;
  2595. s->mb_intra= 0;
  2596. for(i=0; i<2; i++){
  2597. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  2598. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  2599. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  2600. }
  2601. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
  2602. &dmin, &next_block, 0, 0);
  2603. }
  2604. if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
  2605. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2606. s->mv_type = MV_TYPE_FIELD;
  2607. s->mb_intra= 0;
  2608. for(dir=0; dir<2; dir++){
  2609. for(i=0; i<2; i++){
  2610. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  2611. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  2612. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  2613. }
  2614. }
  2615. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
  2616. &dmin, &next_block, 0, 0);
  2617. }
  2618. if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  2619. s->mv_dir = 0;
  2620. s->mv_type = MV_TYPE_16X16;
  2621. s->mb_intra= 1;
  2622. s->mv[0][0][0] = 0;
  2623. s->mv[0][0][1] = 0;
  2624. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
  2625. &dmin, &next_block, 0, 0);
  2626. if(s->h263_pred || s->h263_aic){
  2627. if(best_s.mb_intra)
  2628. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  2629. else
  2630. ff_clean_intra_table_entries(s); //old mode?
  2631. }
  2632. }
  2633. if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
  2634. if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
  2635. const int last_qp= backup_s.qscale;
  2636. int qpi, qp, dc[6];
  2637. int16_t ac[6][16];
  2638. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  2639. static const int dquant_tab[4]={-1,1,-2,2};
  2640. assert(backup_s.dquant == 0);
  2641. //FIXME intra
  2642. s->mv_dir= best_s.mv_dir;
  2643. s->mv_type = MV_TYPE_16X16;
  2644. s->mb_intra= best_s.mb_intra;
  2645. s->mv[0][0][0] = best_s.mv[0][0][0];
  2646. s->mv[0][0][1] = best_s.mv[0][0][1];
  2647. s->mv[1][0][0] = best_s.mv[1][0][0];
  2648. s->mv[1][0][1] = best_s.mv[1][0][1];
  2649. qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
  2650. for(; qpi<4; qpi++){
  2651. int dquant= dquant_tab[qpi];
  2652. qp= last_qp + dquant;
  2653. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  2654. continue;
  2655. backup_s.dquant= dquant;
  2656. if(s->mb_intra && s->dc_val[0]){
  2657. for(i=0; i<6; i++){
  2658. dc[i]= s->dc_val[0][ s->block_index[i] ];
  2659. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
  2660. }
  2661. }
  2662. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  2663. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  2664. if(best_s.qscale != qp){
  2665. if(s->mb_intra && s->dc_val[0]){
  2666. for(i=0; i<6; i++){
  2667. s->dc_val[0][ s->block_index[i] ]= dc[i];
  2668. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
  2669. }
  2670. }
  2671. }
  2672. }
  2673. }
  2674. }
  2675. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
  2676. int mx= s->b_direct_mv_table[xy][0];
  2677. int my= s->b_direct_mv_table[xy][1];
  2678. backup_s.dquant = 0;
  2679. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2680. s->mb_intra= 0;
  2681. ff_mpeg4_set_direct_mv(s, mx, my);
  2682. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2683. &dmin, &next_block, mx, my);
  2684. }
  2685. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
  2686. backup_s.dquant = 0;
  2687. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2688. s->mb_intra= 0;
  2689. ff_mpeg4_set_direct_mv(s, 0, 0);
  2690. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2691. &dmin, &next_block, 0, 0);
  2692. }
  2693. if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
  2694. int coded=0;
  2695. for(i=0; i<6; i++)
  2696. coded |= s->block_last_index[i];
  2697. if(coded){
  2698. int mx,my;
  2699. memcpy(s->mv, best_s.mv, sizeof(s->mv));
  2700. if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
  2701. mx=my=0; //FIXME find the one we actually used
  2702. ff_mpeg4_set_direct_mv(s, mx, my);
  2703. }else if(best_s.mv_dir&MV_DIR_BACKWARD){
  2704. mx= s->mv[1][0][0];
  2705. my= s->mv[1][0][1];
  2706. }else{
  2707. mx= s->mv[0][0][0];
  2708. my= s->mv[0][0][1];
  2709. }
  2710. s->mv_dir= best_s.mv_dir;
  2711. s->mv_type = best_s.mv_type;
  2712. s->mb_intra= 0;
  2713. /* s->mv[0][0][0] = best_s.mv[0][0][0];
  2714. s->mv[0][0][1] = best_s.mv[0][0][1];
  2715. s->mv[1][0][0] = best_s.mv[1][0][0];
  2716. s->mv[1][0][1] = best_s.mv[1][0][1];*/
  2717. backup_s.dquant= 0;
  2718. s->skipdct=1;
  2719. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  2720. &dmin, &next_block, mx, my);
  2721. s->skipdct=0;
  2722. }
  2723. }
  2724. s->current_picture.qscale_table[xy] = best_s.qscale;
  2725. copy_context_after_encode(s, &best_s, -1);
  2726. pb_bits_count= put_bits_count(&s->pb);
  2727. flush_put_bits(&s->pb);
  2728. avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2729. s->pb= backup_s.pb;
  2730. if(s->data_partitioning){
  2731. pb2_bits_count= put_bits_count(&s->pb2);
  2732. flush_put_bits(&s->pb2);
  2733. avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2734. s->pb2= backup_s.pb2;
  2735. tex_pb_bits_count= put_bits_count(&s->tex_pb);
  2736. flush_put_bits(&s->tex_pb);
  2737. avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2738. s->tex_pb= backup_s.tex_pb;
  2739. }
  2740. s->last_bits= put_bits_count(&s->pb);
  2741. if (CONFIG_H263_ENCODER &&
  2742. s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
  2743. ff_h263_update_motion_val(s);
  2744. if(next_block==0){ //FIXME 16 vs linesize16
  2745. s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
  2746. s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
  2747. s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
  2748. }
  2749. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  2750. ff_mpv_decode_mb(s, s->block);
  2751. } else {
  2752. int motion_x = 0, motion_y = 0;
  2753. s->mv_type=MV_TYPE_16X16;
  2754. // only one MB-Type possible
  2755. switch(mb_type){
  2756. case CANDIDATE_MB_TYPE_INTRA:
  2757. s->mv_dir = 0;
  2758. s->mb_intra= 1;
  2759. motion_x= s->mv[0][0][0] = 0;
  2760. motion_y= s->mv[0][0][1] = 0;
  2761. break;
  2762. case CANDIDATE_MB_TYPE_INTER:
  2763. s->mv_dir = MV_DIR_FORWARD;
  2764. s->mb_intra= 0;
  2765. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2766. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2767. break;
  2768. case CANDIDATE_MB_TYPE_INTER_I:
  2769. s->mv_dir = MV_DIR_FORWARD;
  2770. s->mv_type = MV_TYPE_FIELD;
  2771. s->mb_intra= 0;
  2772. for(i=0; i<2; i++){
  2773. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  2774. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  2775. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  2776. }
  2777. break;
  2778. case CANDIDATE_MB_TYPE_INTER4V:
  2779. s->mv_dir = MV_DIR_FORWARD;
  2780. s->mv_type = MV_TYPE_8X8;
  2781. s->mb_intra= 0;
  2782. for(i=0; i<4; i++){
  2783. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  2784. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  2785. }
  2786. break;
  2787. case CANDIDATE_MB_TYPE_DIRECT:
  2788. if (CONFIG_MPEG4_ENCODER) {
  2789. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  2790. s->mb_intra= 0;
  2791. motion_x=s->b_direct_mv_table[xy][0];
  2792. motion_y=s->b_direct_mv_table[xy][1];
  2793. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  2794. }
  2795. break;
  2796. case CANDIDATE_MB_TYPE_DIRECT0:
  2797. if (CONFIG_MPEG4_ENCODER) {
  2798. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  2799. s->mb_intra= 0;
  2800. ff_mpeg4_set_direct_mv(s, 0, 0);
  2801. }
  2802. break;
  2803. case CANDIDATE_MB_TYPE_BIDIR:
  2804. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2805. s->mb_intra= 0;
  2806. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2807. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2808. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2809. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2810. break;
  2811. case CANDIDATE_MB_TYPE_BACKWARD:
  2812. s->mv_dir = MV_DIR_BACKWARD;
  2813. s->mb_intra= 0;
  2814. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2815. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2816. break;
  2817. case CANDIDATE_MB_TYPE_FORWARD:
  2818. s->mv_dir = MV_DIR_FORWARD;
  2819. s->mb_intra= 0;
  2820. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2821. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2822. break;
  2823. case CANDIDATE_MB_TYPE_FORWARD_I:
  2824. s->mv_dir = MV_DIR_FORWARD;
  2825. s->mv_type = MV_TYPE_FIELD;
  2826. s->mb_intra= 0;
  2827. for(i=0; i<2; i++){
  2828. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  2829. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  2830. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  2831. }
  2832. break;
  2833. case CANDIDATE_MB_TYPE_BACKWARD_I:
  2834. s->mv_dir = MV_DIR_BACKWARD;
  2835. s->mv_type = MV_TYPE_FIELD;
  2836. s->mb_intra= 0;
  2837. for(i=0; i<2; i++){
  2838. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  2839. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  2840. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  2841. }
  2842. break;
  2843. case CANDIDATE_MB_TYPE_BIDIR_I:
  2844. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2845. s->mv_type = MV_TYPE_FIELD;
  2846. s->mb_intra= 0;
  2847. for(dir=0; dir<2; dir++){
  2848. for(i=0; i<2; i++){
  2849. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  2850. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  2851. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  2852. }
  2853. }
  2854. break;
  2855. default:
  2856. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  2857. }
  2858. encode_mb(s, motion_x, motion_y);
  2859. // RAL: Update last macroblock type
  2860. s->last_mv_dir = s->mv_dir;
  2861. if (CONFIG_H263_ENCODER &&
  2862. s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
  2863. ff_h263_update_motion_val(s);
  2864. ff_mpv_decode_mb(s, s->block);
  2865. }
  2866. /* clean the MV table in IPS frames for direct mode in B-frames */
  2867. if(s->mb_intra /* && I,P,S_TYPE */){
  2868. s->p_mv_table[xy][0]=0;
  2869. s->p_mv_table[xy][1]=0;
  2870. }
  2871. if (s->avctx->flags & AV_CODEC_FLAG_PSNR) {
  2872. int w= 16;
  2873. int h= 16;
  2874. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2875. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2876. s->current_picture.encoding_error[0] += sse(
  2877. s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2878. s->dest[0], w, h, s->linesize);
  2879. s->current_picture.encoding_error[1] += sse(
  2880. s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
  2881. s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
  2882. s->current_picture.encoding_error[2] += sse(
  2883. s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
  2884. s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
  2885. }
  2886. if(s->loop_filter){
  2887. if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
  2888. ff_h263_loop_filter(s);
  2889. }
  2890. ff_dlog(s->avctx, "MB %d %d bits\n",
  2891. s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
  2892. }
  2893. }
  2894. //not beautiful here but we must write it before flushing so it has to be here
  2895. if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
  2896. ff_msmpeg4_encode_ext_header(s);
  2897. write_slice_end(s);
  2898. #if FF_API_RTP_CALLBACK
  2899. FF_DISABLE_DEPRECATION_WARNINGS
  2900. /* Send the last GOB if RTP */
  2901. if (s->avctx->rtp_callback) {
  2902. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
  2903. int pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
  2904. /* Call the RTP callback to send the last GOB */
  2905. emms_c();
  2906. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
  2907. }
  2908. FF_ENABLE_DEPRECATION_WARNINGS
  2909. #endif
  2910. return 0;
  2911. }
  2912. #define MERGE(field) dst->field += src->field; src->field=0
  2913. static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  2914. MERGE(me.scene_change_score);
  2915. MERGE(me.mc_mb_var_sum_temp);
  2916. MERGE(me.mb_var_sum_temp);
  2917. }
  2918. static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
  2919. int i;
  2920. MERGE(dct_count[0]); //note, the other dct vars are not part of the context
  2921. MERGE(dct_count[1]);
  2922. MERGE(mv_bits);
  2923. MERGE(i_tex_bits);
  2924. MERGE(p_tex_bits);
  2925. MERGE(i_count);
  2926. MERGE(f_count);
  2927. MERGE(b_count);
  2928. MERGE(skip_count);
  2929. MERGE(misc_bits);
  2930. MERGE(er.error_count);
  2931. MERGE(padding_bug_score);
  2932. MERGE(current_picture.encoding_error[0]);
  2933. MERGE(current_picture.encoding_error[1]);
  2934. MERGE(current_picture.encoding_error[2]);
  2935. if (dst->noise_reduction){
  2936. for(i=0; i<64; i++){
  2937. MERGE(dct_error_sum[0][i]);
  2938. MERGE(dct_error_sum[1][i]);
  2939. }
  2940. }
  2941. assert(put_bits_count(&src->pb) % 8 ==0);
  2942. assert(put_bits_count(&dst->pb) % 8 ==0);
  2943. avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
  2944. flush_put_bits(&dst->pb);
  2945. }
  2946. static int estimate_qp(MpegEncContext *s, int dry_run){
  2947. if (s->next_lambda){
  2948. s->current_picture_ptr->f->quality =
  2949. s->current_picture.f->quality = s->next_lambda;
  2950. if(!dry_run) s->next_lambda= 0;
  2951. } else if (!s->fixed_qscale) {
  2952. s->current_picture_ptr->f->quality =
  2953. s->current_picture.f->quality = ff_rate_estimate_qscale(s, dry_run);
  2954. if (s->current_picture.f->quality < 0)
  2955. return -1;
  2956. }
  2957. if(s->adaptive_quant){
  2958. switch(s->codec_id){
  2959. case AV_CODEC_ID_MPEG4:
  2960. if (CONFIG_MPEG4_ENCODER)
  2961. ff_clean_mpeg4_qscales(s);
  2962. break;
  2963. case AV_CODEC_ID_H263:
  2964. case AV_CODEC_ID_H263P:
  2965. case AV_CODEC_ID_FLV1:
  2966. if (CONFIG_H263_ENCODER)
  2967. ff_clean_h263_qscales(s);
  2968. break;
  2969. default:
  2970. ff_init_qscale_tab(s);
  2971. }
  2972. s->lambda= s->lambda_table[0];
  2973. //FIXME broken
  2974. }else
  2975. s->lambda = s->current_picture.f->quality;
  2976. update_qscale(s);
  2977. return 0;
  2978. }
  2979. /* must be called before writing the header */
  2980. static void set_frame_distances(MpegEncContext * s){
  2981. assert(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
  2982. s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
  2983. if(s->pict_type==AV_PICTURE_TYPE_B){
  2984. s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
  2985. assert(s->pb_time > 0 && s->pb_time < s->pp_time);
  2986. }else{
  2987. s->pp_time= s->time - s->last_non_b_time;
  2988. s->last_non_b_time= s->time;
  2989. assert(s->picture_number==0 || s->pp_time > 0);
  2990. }
  2991. }
  2992. static int encode_picture(MpegEncContext *s, int picture_number)
  2993. {
  2994. int i, ret;
  2995. int bits;
  2996. int context_count = s->slice_context_count;
  2997. s->picture_number = picture_number;
  2998. /* Reset the average MB variance */
  2999. s->me.mb_var_sum_temp =
  3000. s->me.mc_mb_var_sum_temp = 0;
  3001. /* we need to initialize some time vars before we can encode B-frames */
  3002. // RAL: Condition added for MPEG1VIDEO
  3003. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
  3004. set_frame_distances(s);
  3005. if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
  3006. ff_set_mpeg4_time(s);
  3007. s->me.scene_change_score=0;
  3008. // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
  3009. if(s->pict_type==AV_PICTURE_TYPE_I){
  3010. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  3011. else s->no_rounding=0;
  3012. }else if(s->pict_type!=AV_PICTURE_TYPE_B){
  3013. if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
  3014. s->no_rounding ^= 1;
  3015. }
  3016. if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
  3017. if (estimate_qp(s,1) < 0)
  3018. return -1;
  3019. ff_get_2pass_fcode(s);
  3020. } else if (!(s->avctx->flags & AV_CODEC_FLAG_QSCALE)) {
  3021. if(s->pict_type==AV_PICTURE_TYPE_B)
  3022. s->lambda= s->last_lambda_for[s->pict_type];
  3023. else
  3024. s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
  3025. update_qscale(s);
  3026. }
  3027. s->mb_intra=0; //for the rate distortion & bit compare functions
  3028. for(i=1; i<context_count; i++){
  3029. ret = ff_update_duplicate_context(s->thread_context[i], s);
  3030. if (ret < 0)
  3031. return ret;
  3032. }
  3033. if(ff_init_me(s)<0)
  3034. return -1;
  3035. /* Estimate motion for every MB */
  3036. if(s->pict_type != AV_PICTURE_TYPE_I){
  3037. s->lambda = (s->lambda * s->me_penalty_compensation + 128) >> 8;
  3038. s->lambda2 = (s->lambda2 * (int64_t) s->me_penalty_compensation + 128) >> 8;
  3039. if (s->pict_type != AV_PICTURE_TYPE_B) {
  3040. if ((s->me_pre && s->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
  3041. s->me_pre == 2) {
  3042. s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3043. }
  3044. }
  3045. s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3046. }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
  3047. /* I-Frame */
  3048. for(i=0; i<s->mb_stride*s->mb_height; i++)
  3049. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  3050. if(!s->fixed_qscale){
  3051. /* finding spatial complexity for I-frame rate control */
  3052. s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3053. }
  3054. }
  3055. for(i=1; i<context_count; i++){
  3056. merge_context_after_me(s, s->thread_context[i]);
  3057. }
  3058. s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
  3059. s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
  3060. emms_c();
  3061. if (s->me.scene_change_score > s->scenechange_threshold &&
  3062. s->pict_type == AV_PICTURE_TYPE_P) {
  3063. s->pict_type= AV_PICTURE_TYPE_I;
  3064. for(i=0; i<s->mb_stride*s->mb_height; i++)
  3065. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  3066. ff_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
  3067. s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  3068. }
  3069. if(!s->umvplus){
  3070. if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
  3071. s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  3072. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3073. int a,b;
  3074. a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  3075. b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  3076. s->f_code= FFMAX3(s->f_code, a, b);
  3077. }
  3078. ff_fix_long_p_mvs(s);
  3079. ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
  3080. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3081. int j;
  3082. for(i=0; i<2; i++){
  3083. for(j=0; j<2; j++)
  3084. ff_fix_long_mvs(s, s->p_field_select_table[i], j,
  3085. s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
  3086. }
  3087. }
  3088. }
  3089. if(s->pict_type==AV_PICTURE_TYPE_B){
  3090. int a, b;
  3091. a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  3092. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  3093. s->f_code = FFMAX(a, b);
  3094. a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  3095. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  3096. s->b_code = FFMAX(a, b);
  3097. ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  3098. ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  3099. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  3100. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  3101. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3102. int dir, j;
  3103. for(dir=0; dir<2; dir++){
  3104. for(i=0; i<2; i++){
  3105. for(j=0; j<2; j++){
  3106. int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
  3107. : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  3108. ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
  3109. s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  3110. }
  3111. }
  3112. }
  3113. }
  3114. }
  3115. }
  3116. if (estimate_qp(s, 0) < 0)
  3117. return -1;
  3118. if (s->qscale < 3 && s->max_qcoeff <= 128 &&
  3119. s->pict_type == AV_PICTURE_TYPE_I &&
  3120. !(s->avctx->flags & AV_CODEC_FLAG_QSCALE))
  3121. s->qscale= 3; //reduce clipping problems
  3122. if (s->out_format == FMT_MJPEG) {
  3123. /* for mjpeg, we do include qscale in the matrix */
  3124. for(i=1;i<64;i++){
  3125. int j = s->idsp.idct_permutation[i];
  3126. s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  3127. }
  3128. s->y_dc_scale_table=
  3129. s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
  3130. s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
  3131. ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  3132. s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
  3133. s->qscale= 8;
  3134. }
  3135. //FIXME var duplication
  3136. s->current_picture_ptr->f->key_frame =
  3137. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
  3138. s->current_picture_ptr->f->pict_type =
  3139. s->current_picture.f->pict_type = s->pict_type;
  3140. if (s->current_picture.f->key_frame)
  3141. s->picture_in_gop_number=0;
  3142. s->last_bits= put_bits_count(&s->pb);
  3143. switch(s->out_format) {
  3144. case FMT_MJPEG:
  3145. if (CONFIG_MJPEG_ENCODER)
  3146. ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
  3147. s->pred, s->intra_matrix);
  3148. break;
  3149. case FMT_H261:
  3150. if (CONFIG_H261_ENCODER)
  3151. ff_h261_encode_picture_header(s, picture_number);
  3152. break;
  3153. case FMT_H263:
  3154. if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
  3155. ff_wmv2_encode_picture_header(s, picture_number);
  3156. else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
  3157. ff_msmpeg4_encode_picture_header(s, picture_number);
  3158. else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
  3159. ff_mpeg4_encode_picture_header(s, picture_number);
  3160. else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
  3161. ret = ff_rv10_encode_picture_header(s, picture_number);
  3162. if (ret < 0)
  3163. return ret;
  3164. }
  3165. else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
  3166. ff_rv20_encode_picture_header(s, picture_number);
  3167. else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
  3168. ff_flv_encode_picture_header(s, picture_number);
  3169. else if (CONFIG_H263_ENCODER)
  3170. ff_h263_encode_picture_header(s, picture_number);
  3171. break;
  3172. case FMT_MPEG1:
  3173. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  3174. ff_mpeg1_encode_picture_header(s, picture_number);
  3175. break;
  3176. default:
  3177. assert(0);
  3178. }
  3179. bits= put_bits_count(&s->pb);
  3180. s->header_bits= bits - s->last_bits;
  3181. for(i=1; i<context_count; i++){
  3182. update_duplicate_context_after_me(s->thread_context[i], s);
  3183. }
  3184. s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3185. for(i=1; i<context_count; i++){
  3186. merge_context_after_encode(s, s->thread_context[i]);
  3187. }
  3188. emms_c();
  3189. return 0;
  3190. }
  3191. static void denoise_dct_c(MpegEncContext *s, int16_t *block){
  3192. const int intra= s->mb_intra;
  3193. int i;
  3194. s->dct_count[intra]++;
  3195. for(i=0; i<64; i++){
  3196. int level= block[i];
  3197. if(level){
  3198. if(level>0){
  3199. s->dct_error_sum[intra][i] += level;
  3200. level -= s->dct_offset[intra][i];
  3201. if(level<0) level=0;
  3202. }else{
  3203. s->dct_error_sum[intra][i] -= level;
  3204. level += s->dct_offset[intra][i];
  3205. if(level>0) level=0;
  3206. }
  3207. block[i]= level;
  3208. }
  3209. }
  3210. }
  3211. static int dct_quantize_trellis_c(MpegEncContext *s,
  3212. int16_t *block, int n,
  3213. int qscale, int *overflow){
  3214. const int *qmat;
  3215. const uint8_t *scantable= s->intra_scantable.scantable;
  3216. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  3217. int max=0;
  3218. unsigned int threshold1, threshold2;
  3219. int bias=0;
  3220. int run_tab[65];
  3221. int level_tab[65];
  3222. int score_tab[65];
  3223. int survivor[65];
  3224. int survivor_count;
  3225. int last_run=0;
  3226. int last_level=0;
  3227. int last_score= 0;
  3228. int last_i;
  3229. int coeff[2][64];
  3230. int coeff_count[64];
  3231. int qmul, qadd, start_i, last_non_zero, i, dc;
  3232. const int esc_length= s->ac_esc_length;
  3233. uint8_t * length;
  3234. uint8_t * last_length;
  3235. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  3236. s->fdsp.fdct(block);
  3237. if(s->dct_error_sum)
  3238. s->denoise_dct(s, block);
  3239. qmul= qscale*16;
  3240. qadd= ((qscale-1)|1)*8;
  3241. if (s->mb_intra) {
  3242. int q;
  3243. if (!s->h263_aic) {
  3244. if (n < 4)
  3245. q = s->y_dc_scale;
  3246. else
  3247. q = s->c_dc_scale;
  3248. q = q << 3;
  3249. } else{
  3250. /* For AIC we skip quant/dequant of INTRADC */
  3251. q = 1 << 3;
  3252. qadd=0;
  3253. }
  3254. /* note: block[0] is assumed to be positive */
  3255. block[0] = (block[0] + (q >> 1)) / q;
  3256. start_i = 1;
  3257. last_non_zero = 0;
  3258. qmat = s->q_intra_matrix[qscale];
  3259. if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  3260. bias= 1<<(QMAT_SHIFT-1);
  3261. length = s->intra_ac_vlc_length;
  3262. last_length= s->intra_ac_vlc_last_length;
  3263. } else {
  3264. start_i = 0;
  3265. last_non_zero = -1;
  3266. qmat = s->q_inter_matrix[qscale];
  3267. length = s->inter_ac_vlc_length;
  3268. last_length= s->inter_ac_vlc_last_length;
  3269. }
  3270. last_i= start_i;
  3271. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3272. threshold2= (threshold1<<1);
  3273. for(i=63; i>=start_i; i--) {
  3274. const int j = scantable[i];
  3275. int level = block[j] * qmat[j];
  3276. if(((unsigned)(level+threshold1))>threshold2){
  3277. last_non_zero = i;
  3278. break;
  3279. }
  3280. }
  3281. for(i=start_i; i<=last_non_zero; i++) {
  3282. const int j = scantable[i];
  3283. int level = block[j] * qmat[j];
  3284. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3285. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3286. if(((unsigned)(level+threshold1))>threshold2){
  3287. if(level>0){
  3288. level= (bias + level)>>QMAT_SHIFT;
  3289. coeff[0][i]= level;
  3290. coeff[1][i]= level-1;
  3291. // coeff[2][k]= level-2;
  3292. }else{
  3293. level= (bias - level)>>QMAT_SHIFT;
  3294. coeff[0][i]= -level;
  3295. coeff[1][i]= -level+1;
  3296. // coeff[2][k]= -level+2;
  3297. }
  3298. coeff_count[i]= FFMIN(level, 2);
  3299. assert(coeff_count[i]);
  3300. max |=level;
  3301. }else{
  3302. coeff[0][i]= (level>>31)|1;
  3303. coeff_count[i]= 1;
  3304. }
  3305. }
  3306. *overflow= s->max_qcoeff < max; //overflow might have happened
  3307. if(last_non_zero < start_i){
  3308. memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
  3309. return last_non_zero;
  3310. }
  3311. score_tab[start_i]= 0;
  3312. survivor[0]= start_i;
  3313. survivor_count= 1;
  3314. for(i=start_i; i<=last_non_zero; i++){
  3315. int level_index, j, zero_distortion;
  3316. int dct_coeff= FFABS(block[ scantable[i] ]);
  3317. int best_score=256*256*256*120;
  3318. if (s->fdsp.fdct == ff_fdct_ifast)
  3319. dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
  3320. zero_distortion= dct_coeff*dct_coeff;
  3321. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3322. int distortion;
  3323. int level= coeff[level_index][i];
  3324. const int alevel= FFABS(level);
  3325. int unquant_coeff;
  3326. assert(level);
  3327. if(s->out_format == FMT_H263){
  3328. unquant_coeff= alevel*qmul + qadd;
  3329. } else { // MPEG-1
  3330. j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
  3331. if(s->mb_intra){
  3332. unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
  3333. unquant_coeff = (unquant_coeff - 1) | 1;
  3334. }else{
  3335. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3336. unquant_coeff = (unquant_coeff - 1) | 1;
  3337. }
  3338. unquant_coeff<<= 3;
  3339. }
  3340. distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
  3341. level+=64;
  3342. if((level&(~127)) == 0){
  3343. for(j=survivor_count-1; j>=0; j--){
  3344. int run= i - survivor[j];
  3345. int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3346. score += score_tab[i-run];
  3347. if(score < best_score){
  3348. best_score= score;
  3349. run_tab[i+1]= run;
  3350. level_tab[i+1]= level-64;
  3351. }
  3352. }
  3353. if(s->out_format == FMT_H263){
  3354. for(j=survivor_count-1; j>=0; j--){
  3355. int run= i - survivor[j];
  3356. int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3357. score += score_tab[i-run];
  3358. if(score < last_score){
  3359. last_score= score;
  3360. last_run= run;
  3361. last_level= level-64;
  3362. last_i= i+1;
  3363. }
  3364. }
  3365. }
  3366. }else{
  3367. distortion += esc_length*lambda;
  3368. for(j=survivor_count-1; j>=0; j--){
  3369. int run= i - survivor[j];
  3370. int score= distortion + score_tab[i-run];
  3371. if(score < best_score){
  3372. best_score= score;
  3373. run_tab[i+1]= run;
  3374. level_tab[i+1]= level-64;
  3375. }
  3376. }
  3377. if(s->out_format == FMT_H263){
  3378. for(j=survivor_count-1; j>=0; j--){
  3379. int run= i - survivor[j];
  3380. int score= distortion + score_tab[i-run];
  3381. if(score < last_score){
  3382. last_score= score;
  3383. last_run= run;
  3384. last_level= level-64;
  3385. last_i= i+1;
  3386. }
  3387. }
  3388. }
  3389. }
  3390. }
  3391. score_tab[i+1]= best_score;
  3392. // Note: there is a vlc code in MPEG-4 which is 1 bit shorter then another one with a shorter run and the same level
  3393. if(last_non_zero <= 27){
  3394. for(; survivor_count; survivor_count--){
  3395. if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  3396. break;
  3397. }
  3398. }else{
  3399. for(; survivor_count; survivor_count--){
  3400. if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  3401. break;
  3402. }
  3403. }
  3404. survivor[ survivor_count++ ]= i+1;
  3405. }
  3406. if(s->out_format != FMT_H263){
  3407. last_score= 256*256*256*120;
  3408. for(i= survivor[0]; i<=last_non_zero + 1; i++){
  3409. int score= score_tab[i];
  3410. if (i)
  3411. score += lambda * 2; // FIXME more exact?
  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 { // MPEG-1
  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 directly 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 permutation.
  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. };