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.

4454 lines
160KB

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