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.

4485 lines
161KB

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