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.

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