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.

4458 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. #if FF_API_ERROR_FRAME
  1295. FF_DISABLE_DEPRECATION_WARNINGS
  1296. memcpy(s->current_picture.f->error, s->current_picture.encoding_error,
  1297. sizeof(s->current_picture.encoding_error));
  1298. FF_ENABLE_DEPRECATION_WARNINGS
  1299. #endif
  1300. }
  1301. static void update_noise_reduction(MpegEncContext *s)
  1302. {
  1303. int intra, i;
  1304. for (intra = 0; intra < 2; intra++) {
  1305. if (s->dct_count[intra] > (1 << 16)) {
  1306. for (i = 0; i < 64; i++) {
  1307. s->dct_error_sum[intra][i] >>= 1;
  1308. }
  1309. s->dct_count[intra] >>= 1;
  1310. }
  1311. for (i = 0; i < 64; i++) {
  1312. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1313. s->dct_count[intra] +
  1314. s->dct_error_sum[intra][i] / 2) /
  1315. (s->dct_error_sum[intra][i] + 1);
  1316. }
  1317. }
  1318. }
  1319. static int frame_start(MpegEncContext *s)
  1320. {
  1321. int ret;
  1322. /* mark & release old frames */
  1323. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1324. s->last_picture_ptr != s->next_picture_ptr &&
  1325. s->last_picture_ptr->f->buf[0]) {
  1326. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  1327. }
  1328. s->current_picture_ptr->f->pict_type = s->pict_type;
  1329. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1330. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1331. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1332. s->current_picture_ptr)) < 0)
  1333. return ret;
  1334. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1335. s->last_picture_ptr = s->next_picture_ptr;
  1336. if (!s->droppable)
  1337. s->next_picture_ptr = s->current_picture_ptr;
  1338. }
  1339. if (s->last_picture_ptr) {
  1340. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1341. if (s->last_picture_ptr->f->buf[0] &&
  1342. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1343. s->last_picture_ptr)) < 0)
  1344. return ret;
  1345. }
  1346. if (s->next_picture_ptr) {
  1347. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1348. if (s->next_picture_ptr->f->buf[0] &&
  1349. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1350. s->next_picture_ptr)) < 0)
  1351. return ret;
  1352. }
  1353. if (s->picture_structure!= PICT_FRAME) {
  1354. int i;
  1355. for (i = 0; i < 4; i++) {
  1356. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1357. s->current_picture.f->data[i] +=
  1358. s->current_picture.f->linesize[i];
  1359. }
  1360. s->current_picture.f->linesize[i] *= 2;
  1361. s->last_picture.f->linesize[i] *= 2;
  1362. s->next_picture.f->linesize[i] *= 2;
  1363. }
  1364. }
  1365. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1366. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1367. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1368. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1369. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1370. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1371. } else {
  1372. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1373. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1374. }
  1375. if (s->dct_error_sum) {
  1376. assert(s->avctx->noise_reduction && s->encoding);
  1377. update_noise_reduction(s);
  1378. }
  1379. return 0;
  1380. }
  1381. int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  1382. const AVFrame *pic_arg, int *got_packet)
  1383. {
  1384. MpegEncContext *s = avctx->priv_data;
  1385. int i, stuffing_count, ret;
  1386. int context_count = s->slice_context_count;
  1387. s->picture_in_gop_number++;
  1388. if (load_input_picture(s, pic_arg) < 0)
  1389. return -1;
  1390. if (select_input_picture(s) < 0) {
  1391. return -1;
  1392. }
  1393. /* output? */
  1394. if (s->new_picture.f->data[0]) {
  1395. uint8_t *sd;
  1396. if (!pkt->data &&
  1397. (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
  1398. return ret;
  1399. if (s->mb_info) {
  1400. s->mb_info_ptr = av_packet_new_side_data(pkt,
  1401. AV_PKT_DATA_H263_MB_INFO,
  1402. s->mb_width*s->mb_height*12);
  1403. s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
  1404. }
  1405. for (i = 0; i < context_count; i++) {
  1406. int start_y = s->thread_context[i]->start_mb_y;
  1407. int end_y = s->thread_context[i]-> end_mb_y;
  1408. int h = s->mb_height;
  1409. uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
  1410. uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
  1411. init_put_bits(&s->thread_context[i]->pb, start, end - start);
  1412. }
  1413. s->pict_type = s->new_picture.f->pict_type;
  1414. //emms_c();
  1415. ret = frame_start(s);
  1416. if (ret < 0)
  1417. return ret;
  1418. vbv_retry:
  1419. if (encode_picture(s, s->picture_number) < 0)
  1420. return -1;
  1421. avctx->header_bits = s->header_bits;
  1422. avctx->mv_bits = s->mv_bits;
  1423. avctx->misc_bits = s->misc_bits;
  1424. avctx->i_tex_bits = s->i_tex_bits;
  1425. avctx->p_tex_bits = s->p_tex_bits;
  1426. avctx->i_count = s->i_count;
  1427. // FIXME f/b_count in avctx
  1428. avctx->p_count = s->mb_num - s->i_count - s->skip_count;
  1429. avctx->skip_count = s->skip_count;
  1430. frame_end(s);
  1431. sd = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
  1432. sizeof(int));
  1433. if (!sd)
  1434. return AVERROR(ENOMEM);
  1435. *(int *)sd = s->current_picture.f->quality;
  1436. if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
  1437. ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
  1438. if (avctx->rc_buffer_size) {
  1439. RateControlContext *rcc = &s->rc_context;
  1440. int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
  1441. if (put_bits_count(&s->pb) > max_size &&
  1442. s->lambda < s->lmax) {
  1443. s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
  1444. (s->qscale + 1) / s->qscale);
  1445. if (s->adaptive_quant) {
  1446. int i;
  1447. for (i = 0; i < s->mb_height * s->mb_stride; i++)
  1448. s->lambda_table[i] =
  1449. FFMAX(s->lambda_table[i] + 1,
  1450. s->lambda_table[i] * (s->qscale + 1) /
  1451. s->qscale);
  1452. }
  1453. s->mb_skipped = 0; // done in frame_start()
  1454. // done in encode_picture() so we must undo it
  1455. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1456. if (s->flipflop_rounding ||
  1457. s->codec_id == AV_CODEC_ID_H263P ||
  1458. s->codec_id == AV_CODEC_ID_MPEG4)
  1459. s->no_rounding ^= 1;
  1460. }
  1461. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1462. s->time_base = s->last_time_base;
  1463. s->last_non_b_time = s->time - s->pp_time;
  1464. }
  1465. for (i = 0; i < context_count; i++) {
  1466. PutBitContext *pb = &s->thread_context[i]->pb;
  1467. init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
  1468. }
  1469. goto vbv_retry;
  1470. }
  1471. assert(s->avctx->rc_max_rate);
  1472. }
  1473. if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
  1474. ff_write_pass1_stats(s);
  1475. for (i = 0; i < 4; i++) {
  1476. s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
  1477. avctx->error[i] += s->current_picture_ptr->encoding_error[i];
  1478. }
  1479. if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
  1480. assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
  1481. avctx->i_tex_bits + avctx->p_tex_bits ==
  1482. put_bits_count(&s->pb));
  1483. flush_put_bits(&s->pb);
  1484. s->frame_bits = put_bits_count(&s->pb);
  1485. stuffing_count = ff_vbv_update(s, s->frame_bits);
  1486. if (stuffing_count) {
  1487. if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
  1488. stuffing_count + 50) {
  1489. av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
  1490. return -1;
  1491. }
  1492. switch (s->codec_id) {
  1493. case AV_CODEC_ID_MPEG1VIDEO:
  1494. case AV_CODEC_ID_MPEG2VIDEO:
  1495. while (stuffing_count--) {
  1496. put_bits(&s->pb, 8, 0);
  1497. }
  1498. break;
  1499. case AV_CODEC_ID_MPEG4:
  1500. put_bits(&s->pb, 16, 0);
  1501. put_bits(&s->pb, 16, 0x1C3);
  1502. stuffing_count -= 4;
  1503. while (stuffing_count--) {
  1504. put_bits(&s->pb, 8, 0xFF);
  1505. }
  1506. break;
  1507. default:
  1508. av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  1509. }
  1510. flush_put_bits(&s->pb);
  1511. s->frame_bits = put_bits_count(&s->pb);
  1512. }
  1513. /* update mpeg1/2 vbv_delay for CBR */
  1514. if (s->avctx->rc_max_rate &&
  1515. s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
  1516. s->out_format == FMT_MPEG1 &&
  1517. 90000LL * (avctx->rc_buffer_size - 1) <=
  1518. s->avctx->rc_max_rate * 0xFFFFLL) {
  1519. int vbv_delay, min_delay;
  1520. double inbits = s->avctx->rc_max_rate *
  1521. av_q2d(s->avctx->time_base);
  1522. int minbits = s->frame_bits - 8 *
  1523. (s->vbv_delay_ptr - s->pb.buf - 1);
  1524. double bits = s->rc_context.buffer_index + minbits - inbits;
  1525. if (bits < 0)
  1526. av_log(s->avctx, AV_LOG_ERROR,
  1527. "Internal error, negative bits\n");
  1528. assert(s->repeat_first_field == 0);
  1529. vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
  1530. min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
  1531. s->avctx->rc_max_rate;
  1532. vbv_delay = FFMAX(vbv_delay, min_delay);
  1533. assert(vbv_delay < 0xFFFF);
  1534. s->vbv_delay_ptr[0] &= 0xF8;
  1535. s->vbv_delay_ptr[0] |= vbv_delay >> 13;
  1536. s->vbv_delay_ptr[1] = vbv_delay >> 5;
  1537. s->vbv_delay_ptr[2] &= 0x07;
  1538. s->vbv_delay_ptr[2] |= vbv_delay << 3;
  1539. avctx->vbv_delay = vbv_delay * 300;
  1540. }
  1541. s->total_bits += s->frame_bits;
  1542. avctx->frame_bits = s->frame_bits;
  1543. pkt->pts = s->current_picture.f->pts;
  1544. if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
  1545. if (!s->current_picture.f->coded_picture_number)
  1546. pkt->dts = pkt->pts - s->dts_delta;
  1547. else
  1548. pkt->dts = s->reordered_pts;
  1549. s->reordered_pts = pkt->pts;
  1550. } else
  1551. pkt->dts = pkt->pts;
  1552. if (s->current_picture.f->key_frame)
  1553. pkt->flags |= AV_PKT_FLAG_KEY;
  1554. if (s->mb_info)
  1555. av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
  1556. } else {
  1557. s->frame_bits = 0;
  1558. }
  1559. assert((s->frame_bits & 7) == 0);
  1560. pkt->size = s->frame_bits / 8;
  1561. *got_packet = !!pkt->size;
  1562. return 0;
  1563. }
  1564. static inline void dct_single_coeff_elimination(MpegEncContext *s,
  1565. int n, int threshold)
  1566. {
  1567. static const char tab[64] = {
  1568. 3, 2, 2, 1, 1, 1, 1, 1,
  1569. 1, 1, 1, 1, 1, 1, 1, 1,
  1570. 1, 1, 1, 1, 1, 1, 1, 1,
  1571. 0, 0, 0, 0, 0, 0, 0, 0,
  1572. 0, 0, 0, 0, 0, 0, 0, 0,
  1573. 0, 0, 0, 0, 0, 0, 0, 0,
  1574. 0, 0, 0, 0, 0, 0, 0, 0,
  1575. 0, 0, 0, 0, 0, 0, 0, 0
  1576. };
  1577. int score = 0;
  1578. int run = 0;
  1579. int i;
  1580. int16_t *block = s->block[n];
  1581. const int last_index = s->block_last_index[n];
  1582. int skip_dc;
  1583. if (threshold < 0) {
  1584. skip_dc = 0;
  1585. threshold = -threshold;
  1586. } else
  1587. skip_dc = 1;
  1588. /* Are all we could set to zero already zero? */
  1589. if (last_index <= skip_dc - 1)
  1590. return;
  1591. for (i = 0; i <= last_index; i++) {
  1592. const int j = s->intra_scantable.permutated[i];
  1593. const int level = FFABS(block[j]);
  1594. if (level == 1) {
  1595. if (skip_dc && i == 0)
  1596. continue;
  1597. score += tab[run];
  1598. run = 0;
  1599. } else if (level > 1) {
  1600. return;
  1601. } else {
  1602. run++;
  1603. }
  1604. }
  1605. if (score >= threshold)
  1606. return;
  1607. for (i = skip_dc; i <= last_index; i++) {
  1608. const int j = s->intra_scantable.permutated[i];
  1609. block[j] = 0;
  1610. }
  1611. if (block[0])
  1612. s->block_last_index[n] = 0;
  1613. else
  1614. s->block_last_index[n] = -1;
  1615. }
  1616. static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
  1617. int last_index)
  1618. {
  1619. int i;
  1620. const int maxlevel = s->max_qcoeff;
  1621. const int minlevel = s->min_qcoeff;
  1622. int overflow = 0;
  1623. if (s->mb_intra) {
  1624. i = 1; // skip clipping of intra dc
  1625. } else
  1626. i = 0;
  1627. for (; i <= last_index; i++) {
  1628. const int j = s->intra_scantable.permutated[i];
  1629. int level = block[j];
  1630. if (level > maxlevel) {
  1631. level = maxlevel;
  1632. overflow++;
  1633. } else if (level < minlevel) {
  1634. level = minlevel;
  1635. overflow++;
  1636. }
  1637. block[j] = level;
  1638. }
  1639. if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  1640. av_log(s->avctx, AV_LOG_INFO,
  1641. "warning, clipping %d dct coefficients to %d..%d\n",
  1642. overflow, minlevel, maxlevel);
  1643. }
  1644. static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
  1645. {
  1646. int x, y;
  1647. // FIXME optimize
  1648. for (y = 0; y < 8; y++) {
  1649. for (x = 0; x < 8; x++) {
  1650. int x2, y2;
  1651. int sum = 0;
  1652. int sqr = 0;
  1653. int count = 0;
  1654. for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
  1655. for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
  1656. int v = ptr[x2 + y2 * stride];
  1657. sum += v;
  1658. sqr += v * v;
  1659. count++;
  1660. }
  1661. }
  1662. weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
  1663. }
  1664. }
  1665. }
  1666. static av_always_inline void encode_mb_internal(MpegEncContext *s,
  1667. int motion_x, int motion_y,
  1668. int mb_block_height,
  1669. int mb_block_count)
  1670. {
  1671. int16_t weight[8][64];
  1672. int16_t orig[8][64];
  1673. const int mb_x = s->mb_x;
  1674. const int mb_y = s->mb_y;
  1675. int i;
  1676. int skip_dct[8];
  1677. int dct_offset = s->linesize * 8; // default for progressive frames
  1678. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1679. ptrdiff_t wrap_y, wrap_c;
  1680. for (i = 0; i < mb_block_count; i++)
  1681. skip_dct[i] = s->skipdct;
  1682. if (s->adaptive_quant) {
  1683. const int last_qp = s->qscale;
  1684. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1685. s->lambda = s->lambda_table[mb_xy];
  1686. update_qscale(s);
  1687. if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
  1688. s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
  1689. s->dquant = s->qscale - last_qp;
  1690. if (s->out_format == FMT_H263) {
  1691. s->dquant = av_clip(s->dquant, -2, 2);
  1692. if (s->codec_id == AV_CODEC_ID_MPEG4) {
  1693. if (!s->mb_intra) {
  1694. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1695. if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
  1696. s->dquant = 0;
  1697. }
  1698. if (s->mv_type == MV_TYPE_8X8)
  1699. s->dquant = 0;
  1700. }
  1701. }
  1702. }
  1703. }
  1704. ff_set_qscale(s, last_qp + s->dquant);
  1705. } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
  1706. ff_set_qscale(s, s->qscale + s->dquant);
  1707. wrap_y = s->linesize;
  1708. wrap_c = s->uvlinesize;
  1709. ptr_y = s->new_picture.f->data[0] +
  1710. (mb_y * 16 * wrap_y) + mb_x * 16;
  1711. ptr_cb = s->new_picture.f->data[1] +
  1712. (mb_y * mb_block_height * wrap_c) + mb_x * 8;
  1713. ptr_cr = s->new_picture.f->data[2] +
  1714. (mb_y * mb_block_height * wrap_c) + mb_x * 8;
  1715. if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
  1716. uint8_t *ebuf = s->sc.edge_emu_buffer + 32;
  1717. s->vdsp.emulated_edge_mc(ebuf, ptr_y,
  1718. wrap_y, wrap_y,
  1719. 16, 16, mb_x * 16, mb_y * 16,
  1720. s->width, s->height);
  1721. ptr_y = ebuf;
  1722. s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
  1723. wrap_c, wrap_c,
  1724. 8, mb_block_height, mb_x * 8, mb_y * 8,
  1725. s->width >> 1, s->height >> 1);
  1726. ptr_cb = ebuf + 18 * wrap_y;
  1727. s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr,
  1728. wrap_c, wrap_c,
  1729. 8, mb_block_height, mb_x * 8, mb_y * 8,
  1730. s->width >> 1, s->height >> 1);
  1731. ptr_cr = ebuf + 18 * wrap_y + 8;
  1732. }
  1733. if (s->mb_intra) {
  1734. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  1735. int progressive_score, interlaced_score;
  1736. s->interlaced_dct = 0;
  1737. progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
  1738. s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
  1739. NULL, wrap_y, 8) - 400;
  1740. if (progressive_score > 0) {
  1741. interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
  1742. NULL, wrap_y * 2, 8) +
  1743. s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
  1744. NULL, wrap_y * 2, 8);
  1745. if (progressive_score > interlaced_score) {
  1746. s->interlaced_dct = 1;
  1747. dct_offset = wrap_y;
  1748. wrap_y <<= 1;
  1749. if (s->chroma_format == CHROMA_422)
  1750. wrap_c <<= 1;
  1751. }
  1752. }
  1753. }
  1754. s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
  1755. s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
  1756. s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
  1757. s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
  1758. if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  1759. skip_dct[4] = 1;
  1760. skip_dct[5] = 1;
  1761. } else {
  1762. s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
  1763. s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
  1764. if (!s->chroma_y_shift) { /* 422 */
  1765. s->pdsp.get_pixels(s->block[6],
  1766. ptr_cb + (dct_offset >> 1), wrap_c);
  1767. s->pdsp.get_pixels(s->block[7],
  1768. ptr_cr + (dct_offset >> 1), wrap_c);
  1769. }
  1770. }
  1771. } else {
  1772. op_pixels_func (*op_pix)[4];
  1773. qpel_mc_func (*op_qpix)[16];
  1774. uint8_t *dest_y, *dest_cb, *dest_cr;
  1775. dest_y = s->dest[0];
  1776. dest_cb = s->dest[1];
  1777. dest_cr = s->dest[2];
  1778. if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
  1779. op_pix = s->hdsp.put_pixels_tab;
  1780. op_qpix = s->qdsp.put_qpel_pixels_tab;
  1781. } else {
  1782. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1783. op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
  1784. }
  1785. if (s->mv_dir & MV_DIR_FORWARD) {
  1786. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
  1787. s->last_picture.f->data,
  1788. op_pix, op_qpix);
  1789. op_pix = s->hdsp.avg_pixels_tab;
  1790. op_qpix = s->qdsp.avg_qpel_pixels_tab;
  1791. }
  1792. if (s->mv_dir & MV_DIR_BACKWARD) {
  1793. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
  1794. s->next_picture.f->data,
  1795. op_pix, op_qpix);
  1796. }
  1797. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  1798. int progressive_score, interlaced_score;
  1799. s->interlaced_dct = 0;
  1800. progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
  1801. s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
  1802. ptr_y + wrap_y * 8,
  1803. wrap_y, 8) - 400;
  1804. if (s->avctx->ildct_cmp == FF_CMP_VSSE)
  1805. progressive_score -= 400;
  1806. if (progressive_score > 0) {
  1807. interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
  1808. wrap_y * 2, 8) +
  1809. s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
  1810. ptr_y + wrap_y,
  1811. wrap_y * 2, 8);
  1812. if (progressive_score > interlaced_score) {
  1813. s->interlaced_dct = 1;
  1814. dct_offset = wrap_y;
  1815. wrap_y <<= 1;
  1816. if (s->chroma_format == CHROMA_422)
  1817. wrap_c <<= 1;
  1818. }
  1819. }
  1820. }
  1821. s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
  1822. s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  1823. s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
  1824. dest_y + dct_offset, wrap_y);
  1825. s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
  1826. dest_y + dct_offset + 8, wrap_y);
  1827. if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
  1828. skip_dct[4] = 1;
  1829. skip_dct[5] = 1;
  1830. } else {
  1831. s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  1832. s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  1833. if (!s->chroma_y_shift) { /* 422 */
  1834. s->pdsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
  1835. dest_cb + (dct_offset >> 1), wrap_c);
  1836. s->pdsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
  1837. dest_cr + (dct_offset >> 1), wrap_c);
  1838. }
  1839. }
  1840. /* pre quantization */
  1841. if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
  1842. 2 * s->qscale * s->qscale) {
  1843. // FIXME optimize
  1844. if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
  1845. skip_dct[0] = 1;
  1846. if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
  1847. skip_dct[1] = 1;
  1848. if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
  1849. wrap_y, 8) < 20 * s->qscale)
  1850. skip_dct[2] = 1;
  1851. if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
  1852. wrap_y, 8) < 20 * s->qscale)
  1853. skip_dct[3] = 1;
  1854. if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
  1855. skip_dct[4] = 1;
  1856. if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
  1857. skip_dct[5] = 1;
  1858. if (!s->chroma_y_shift) { /* 422 */
  1859. if (s->mecc.sad[1](NULL, ptr_cb + (dct_offset >> 1),
  1860. dest_cb + (dct_offset >> 1),
  1861. wrap_c, 8) < 20 * s->qscale)
  1862. skip_dct[6] = 1;
  1863. if (s->mecc.sad[1](NULL, ptr_cr + (dct_offset >> 1),
  1864. dest_cr + (dct_offset >> 1),
  1865. wrap_c, 8) < 20 * s->qscale)
  1866. skip_dct[7] = 1;
  1867. }
  1868. }
  1869. }
  1870. if (s->quantizer_noise_shaping) {
  1871. if (!skip_dct[0])
  1872. get_visual_weight(weight[0], ptr_y , wrap_y);
  1873. if (!skip_dct[1])
  1874. get_visual_weight(weight[1], ptr_y + 8, wrap_y);
  1875. if (!skip_dct[2])
  1876. get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
  1877. if (!skip_dct[3])
  1878. get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
  1879. if (!skip_dct[4])
  1880. get_visual_weight(weight[4], ptr_cb , wrap_c);
  1881. if (!skip_dct[5])
  1882. get_visual_weight(weight[5], ptr_cr , wrap_c);
  1883. if (!s->chroma_y_shift) { /* 422 */
  1884. if (!skip_dct[6])
  1885. get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
  1886. wrap_c);
  1887. if (!skip_dct[7])
  1888. get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
  1889. wrap_c);
  1890. }
  1891. memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
  1892. }
  1893. /* DCT & quantize */
  1894. assert(s->out_format != FMT_MJPEG || s->qscale == 8);
  1895. {
  1896. for (i = 0; i < mb_block_count; i++) {
  1897. if (!skip_dct[i]) {
  1898. int overflow;
  1899. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  1900. // FIXME we could decide to change to quantizer instead of
  1901. // clipping
  1902. // JS: I don't think that would be a good idea it could lower
  1903. // quality instead of improve it. Just INTRADC clipping
  1904. // deserves changes in quantizer
  1905. if (overflow)
  1906. clip_coeffs(s, s->block[i], s->block_last_index[i]);
  1907. } else
  1908. s->block_last_index[i] = -1;
  1909. }
  1910. if (s->quantizer_noise_shaping) {
  1911. for (i = 0; i < mb_block_count; i++) {
  1912. if (!skip_dct[i]) {
  1913. s->block_last_index[i] =
  1914. dct_quantize_refine(s, s->block[i], weight[i],
  1915. orig[i], i, s->qscale);
  1916. }
  1917. }
  1918. }
  1919. if (s->luma_elim_threshold && !s->mb_intra)
  1920. for (i = 0; i < 4; i++)
  1921. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  1922. if (s->chroma_elim_threshold && !s->mb_intra)
  1923. for (i = 4; i < mb_block_count; i++)
  1924. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  1925. if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
  1926. for (i = 0; i < mb_block_count; i++) {
  1927. if (s->block_last_index[i] == -1)
  1928. s->coded_score[i] = INT_MAX / 256;
  1929. }
  1930. }
  1931. }
  1932. if ((s->avctx->flags & AV_CODEC_FLAG_GRAY) && s->mb_intra) {
  1933. s->block_last_index[4] =
  1934. s->block_last_index[5] = 0;
  1935. s->block[4][0] =
  1936. s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
  1937. }
  1938. // non c quantize code returns incorrect block_last_index FIXME
  1939. if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
  1940. for (i = 0; i < mb_block_count; i++) {
  1941. int j;
  1942. if (s->block_last_index[i] > 0) {
  1943. for (j = 63; j > 0; j--) {
  1944. if (s->block[i][s->intra_scantable.permutated[j]])
  1945. break;
  1946. }
  1947. s->block_last_index[i] = j;
  1948. }
  1949. }
  1950. }
  1951. /* huffman encode */
  1952. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  1953. case AV_CODEC_ID_MPEG1VIDEO:
  1954. case AV_CODEC_ID_MPEG2VIDEO:
  1955. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  1956. ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  1957. break;
  1958. case AV_CODEC_ID_MPEG4:
  1959. if (CONFIG_MPEG4_ENCODER)
  1960. ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1961. break;
  1962. case AV_CODEC_ID_MSMPEG4V2:
  1963. case AV_CODEC_ID_MSMPEG4V3:
  1964. case AV_CODEC_ID_WMV1:
  1965. if (CONFIG_MSMPEG4_ENCODER)
  1966. ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  1967. break;
  1968. case AV_CODEC_ID_WMV2:
  1969. if (CONFIG_WMV2_ENCODER)
  1970. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
  1971. break;
  1972. case AV_CODEC_ID_H261:
  1973. if (CONFIG_H261_ENCODER)
  1974. ff_h261_encode_mb(s, s->block, motion_x, motion_y);
  1975. break;
  1976. case AV_CODEC_ID_H263:
  1977. case AV_CODEC_ID_H263P:
  1978. case AV_CODEC_ID_FLV1:
  1979. case AV_CODEC_ID_RV10:
  1980. case AV_CODEC_ID_RV20:
  1981. if (CONFIG_H263_ENCODER)
  1982. ff_h263_encode_mb(s, s->block, motion_x, motion_y);
  1983. break;
  1984. case AV_CODEC_ID_MJPEG:
  1985. if (CONFIG_MJPEG_ENCODER)
  1986. ff_mjpeg_encode_mb(s, s->block);
  1987. break;
  1988. default:
  1989. assert(0);
  1990. }
  1991. }
  1992. static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  1993. {
  1994. if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
  1995. else encode_mb_internal(s, motion_x, motion_y, 16, 8);
  1996. }
  1997. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  1998. int i;
  1999. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
  2000. /* mpeg1 */
  2001. d->mb_skip_run= s->mb_skip_run;
  2002. for(i=0; i<3; i++)
  2003. d->last_dc[i] = s->last_dc[i];
  2004. /* statistics */
  2005. d->mv_bits= s->mv_bits;
  2006. d->i_tex_bits= s->i_tex_bits;
  2007. d->p_tex_bits= s->p_tex_bits;
  2008. d->i_count= s->i_count;
  2009. d->f_count= s->f_count;
  2010. d->b_count= s->b_count;
  2011. d->skip_count= s->skip_count;
  2012. d->misc_bits= s->misc_bits;
  2013. d->last_bits= 0;
  2014. d->mb_skipped= 0;
  2015. d->qscale= s->qscale;
  2016. d->dquant= s->dquant;
  2017. d->esc3_level_length= s->esc3_level_length;
  2018. }
  2019. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2020. int i;
  2021. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2022. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
  2023. /* mpeg1 */
  2024. d->mb_skip_run= s->mb_skip_run;
  2025. for(i=0; i<3; i++)
  2026. d->last_dc[i] = s->last_dc[i];
  2027. /* statistics */
  2028. d->mv_bits= s->mv_bits;
  2029. d->i_tex_bits= s->i_tex_bits;
  2030. d->p_tex_bits= s->p_tex_bits;
  2031. d->i_count= s->i_count;
  2032. d->f_count= s->f_count;
  2033. d->b_count= s->b_count;
  2034. d->skip_count= s->skip_count;
  2035. d->misc_bits= s->misc_bits;
  2036. d->mb_intra= s->mb_intra;
  2037. d->mb_skipped= s->mb_skipped;
  2038. d->mv_type= s->mv_type;
  2039. d->mv_dir= s->mv_dir;
  2040. d->pb= s->pb;
  2041. if(s->data_partitioning){
  2042. d->pb2= s->pb2;
  2043. d->tex_pb= s->tex_pb;
  2044. }
  2045. d->block= s->block;
  2046. for(i=0; i<8; i++)
  2047. d->block_last_index[i]= s->block_last_index[i];
  2048. d->interlaced_dct= s->interlaced_dct;
  2049. d->qscale= s->qscale;
  2050. d->esc3_level_length= s->esc3_level_length;
  2051. }
  2052. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  2053. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2054. int *dmin, int *next_block, int motion_x, int motion_y)
  2055. {
  2056. int score;
  2057. uint8_t *dest_backup[3];
  2058. copy_context_before_encode(s, backup, type);
  2059. s->block= s->blocks[*next_block];
  2060. s->pb= pb[*next_block];
  2061. if(s->data_partitioning){
  2062. s->pb2 = pb2 [*next_block];
  2063. s->tex_pb= tex_pb[*next_block];
  2064. }
  2065. if(*next_block){
  2066. memcpy(dest_backup, s->dest, sizeof(s->dest));
  2067. s->dest[0] = s->sc.rd_scratchpad;
  2068. s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
  2069. s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
  2070. assert(s->linesize >= 32); //FIXME
  2071. }
  2072. encode_mb(s, motion_x, motion_y);
  2073. score= put_bits_count(&s->pb);
  2074. if(s->data_partitioning){
  2075. score+= put_bits_count(&s->pb2);
  2076. score+= put_bits_count(&s->tex_pb);
  2077. }
  2078. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  2079. ff_mpv_decode_mb(s, s->block);
  2080. score *= s->lambda2;
  2081. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  2082. }
  2083. if(*next_block){
  2084. memcpy(s->dest, dest_backup, sizeof(s->dest));
  2085. }
  2086. if(score<*dmin){
  2087. *dmin= score;
  2088. *next_block^=1;
  2089. copy_context_after_encode(best, s, type);
  2090. }
  2091. }
  2092. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  2093. uint32_t *sq = ff_square_tab + 256;
  2094. int acc=0;
  2095. int x,y;
  2096. if(w==16 && h==16)
  2097. return s->mecc.sse[0](NULL, src1, src2, stride, 16);
  2098. else if(w==8 && h==8)
  2099. return s->mecc.sse[1](NULL, src1, src2, stride, 8);
  2100. for(y=0; y<h; y++){
  2101. for(x=0; x<w; x++){
  2102. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  2103. }
  2104. }
  2105. assert(acc>=0);
  2106. return acc;
  2107. }
  2108. static int sse_mb(MpegEncContext *s){
  2109. int w= 16;
  2110. int h= 16;
  2111. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2112. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2113. if(w==16 && h==16)
  2114. if(s->avctx->mb_cmp == FF_CMP_NSSE){
  2115. 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) +
  2116. 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) +
  2117. 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);
  2118. }else{
  2119. 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) +
  2120. 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) +
  2121. 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);
  2122. }
  2123. else
  2124. 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)
  2125. +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)
  2126. +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);
  2127. }
  2128. static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
  2129. MpegEncContext *s= *(void**)arg;
  2130. s->me.pre_pass=1;
  2131. s->me.dia_size= s->avctx->pre_dia_size;
  2132. s->first_slice_line=1;
  2133. for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
  2134. for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
  2135. ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  2136. }
  2137. s->first_slice_line=0;
  2138. }
  2139. s->me.pre_pass=0;
  2140. return 0;
  2141. }
  2142. static int estimate_motion_thread(AVCodecContext *c, void *arg){
  2143. MpegEncContext *s= *(void**)arg;
  2144. s->me.dia_size= s->avctx->dia_size;
  2145. s->first_slice_line=1;
  2146. for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  2147. s->mb_x=0; //for block init below
  2148. ff_init_block_index(s);
  2149. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  2150. s->block_index[0]+=2;
  2151. s->block_index[1]+=2;
  2152. s->block_index[2]+=2;
  2153. s->block_index[3]+=2;
  2154. /* compute motion vector & mb_type and store in context */
  2155. if(s->pict_type==AV_PICTURE_TYPE_B)
  2156. ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
  2157. else
  2158. ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  2159. }
  2160. s->first_slice_line=0;
  2161. }
  2162. return 0;
  2163. }
  2164. static int mb_var_thread(AVCodecContext *c, void *arg){
  2165. MpegEncContext *s= *(void**)arg;
  2166. int mb_x, mb_y;
  2167. for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  2168. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2169. int xx = mb_x * 16;
  2170. int yy = mb_y * 16;
  2171. uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
  2172. int varc;
  2173. int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
  2174. varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
  2175. (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
  2176. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  2177. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  2178. s->me.mb_var_sum_temp += varc;
  2179. }
  2180. }
  2181. return 0;
  2182. }
  2183. static void write_slice_end(MpegEncContext *s){
  2184. if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
  2185. if(s->partitioned_frame){
  2186. ff_mpeg4_merge_partitions(s);
  2187. }
  2188. ff_mpeg4_stuffing(&s->pb);
  2189. }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
  2190. ff_mjpeg_encode_stuffing(&s->pb);
  2191. }
  2192. avpriv_align_put_bits(&s->pb);
  2193. flush_put_bits(&s->pb);
  2194. if ((s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
  2195. s->misc_bits+= get_bits_diff(s);
  2196. }
  2197. static void write_mb_info(MpegEncContext *s)
  2198. {
  2199. uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
  2200. int offset = put_bits_count(&s->pb);
  2201. int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
  2202. int gobn = s->mb_y / s->gob_index;
  2203. int pred_x, pred_y;
  2204. if (CONFIG_H263_ENCODER)
  2205. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  2206. bytestream_put_le32(&ptr, offset);
  2207. bytestream_put_byte(&ptr, s->qscale);
  2208. bytestream_put_byte(&ptr, gobn);
  2209. bytestream_put_le16(&ptr, mba);
  2210. bytestream_put_byte(&ptr, pred_x); /* hmv1 */
  2211. bytestream_put_byte(&ptr, pred_y); /* vmv1 */
  2212. /* 4MV not implemented */
  2213. bytestream_put_byte(&ptr, 0); /* hmv2 */
  2214. bytestream_put_byte(&ptr, 0); /* vmv2 */
  2215. }
  2216. static void update_mb_info(MpegEncContext *s, int startcode)
  2217. {
  2218. if (!s->mb_info)
  2219. return;
  2220. if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
  2221. s->mb_info_size += 12;
  2222. s->prev_mb_info = s->last_mb_info;
  2223. }
  2224. if (startcode) {
  2225. s->prev_mb_info = put_bits_count(&s->pb)/8;
  2226. /* This might have incremented mb_info_size above, and we return without
  2227. * actually writing any info into that slot yet. But in that case,
  2228. * this will be called again at the start of the after writing the
  2229. * start code, actually writing the mb info. */
  2230. return;
  2231. }
  2232. s->last_mb_info = put_bits_count(&s->pb)/8;
  2233. if (!s->mb_info_size)
  2234. s->mb_info_size += 12;
  2235. write_mb_info(s);
  2236. }
  2237. static int encode_thread(AVCodecContext *c, void *arg){
  2238. MpegEncContext *s= *(void**)arg;
  2239. int mb_x, mb_y, pdif = 0;
  2240. int chr_h= 16>>s->chroma_y_shift;
  2241. int i, j;
  2242. MpegEncContext best_s = { 0 }, backup_s;
  2243. uint8_t bit_buf[2][MAX_MB_BYTES];
  2244. uint8_t bit_buf2[2][MAX_MB_BYTES];
  2245. uint8_t bit_buf_tex[2][MAX_MB_BYTES];
  2246. PutBitContext pb[2], pb2[2], tex_pb[2];
  2247. for(i=0; i<2; i++){
  2248. init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
  2249. init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
  2250. init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
  2251. }
  2252. s->last_bits= put_bits_count(&s->pb);
  2253. s->mv_bits=0;
  2254. s->misc_bits=0;
  2255. s->i_tex_bits=0;
  2256. s->p_tex_bits=0;
  2257. s->i_count=0;
  2258. s->f_count=0;
  2259. s->b_count=0;
  2260. s->skip_count=0;
  2261. for(i=0; i<3; i++){
  2262. /* init last dc values */
  2263. /* note: quant matrix value (8) is implied here */
  2264. s->last_dc[i] = 128 << s->intra_dc_precision;
  2265. s->current_picture.encoding_error[i] = 0;
  2266. }
  2267. s->mb_skip_run = 0;
  2268. memset(s->last_mv, 0, sizeof(s->last_mv));
  2269. s->last_mv_dir = 0;
  2270. switch(s->codec_id){
  2271. case AV_CODEC_ID_H263:
  2272. case AV_CODEC_ID_H263P:
  2273. case AV_CODEC_ID_FLV1:
  2274. if (CONFIG_H263_ENCODER)
  2275. s->gob_index = H263_GOB_HEIGHT(s->height);
  2276. break;
  2277. case AV_CODEC_ID_MPEG4:
  2278. if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
  2279. ff_mpeg4_init_partitions(s);
  2280. break;
  2281. }
  2282. s->resync_mb_x=0;
  2283. s->resync_mb_y=0;
  2284. s->first_slice_line = 1;
  2285. s->ptr_lastgob = s->pb.buf;
  2286. for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  2287. s->mb_x=0;
  2288. s->mb_y= mb_y;
  2289. ff_set_qscale(s, s->qscale);
  2290. ff_init_block_index(s);
  2291. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2292. int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
  2293. int mb_type= s->mb_type[xy];
  2294. // int d;
  2295. int dmin= INT_MAX;
  2296. int dir;
  2297. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
  2298. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2299. return -1;
  2300. }
  2301. if(s->data_partitioning){
  2302. if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
  2303. || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
  2304. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2305. return -1;
  2306. }
  2307. }
  2308. s->mb_x = mb_x;
  2309. s->mb_y = mb_y; // moved into loop, can get changed by H.261
  2310. ff_update_block_index(s);
  2311. if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
  2312. ff_h261_reorder_mb_index(s);
  2313. xy= s->mb_y*s->mb_stride + s->mb_x;
  2314. mb_type= s->mb_type[xy];
  2315. }
  2316. /* write gob / video packet header */
  2317. if(s->rtp_mode){
  2318. int current_packet_size, is_gob_start;
  2319. current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
  2320. is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
  2321. if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
  2322. switch(s->codec_id){
  2323. case AV_CODEC_ID_H263:
  2324. case AV_CODEC_ID_H263P:
  2325. if(!s->h263_slice_structured)
  2326. if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  2327. break;
  2328. case AV_CODEC_ID_MPEG2VIDEO:
  2329. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  2330. case AV_CODEC_ID_MPEG1VIDEO:
  2331. if(s->mb_skip_run) is_gob_start=0;
  2332. break;
  2333. }
  2334. if(is_gob_start){
  2335. if(s->start_mb_y != mb_y || mb_x!=0){
  2336. write_slice_end(s);
  2337. if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
  2338. ff_mpeg4_init_partitions(s);
  2339. }
  2340. }
  2341. assert((put_bits_count(&s->pb)&7) == 0);
  2342. current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
  2343. if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
  2344. int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
  2345. int d = 100 / s->error_rate;
  2346. if(r % d == 0){
  2347. current_packet_size=0;
  2348. s->pb.buf_ptr= s->ptr_lastgob;
  2349. assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
  2350. }
  2351. }
  2352. if (s->avctx->rtp_callback){
  2353. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
  2354. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
  2355. }
  2356. update_mb_info(s, 1);
  2357. switch(s->codec_id){
  2358. case AV_CODEC_ID_MPEG4:
  2359. if (CONFIG_MPEG4_ENCODER) {
  2360. ff_mpeg4_encode_video_packet_header(s);
  2361. ff_mpeg4_clean_buffers(s);
  2362. }
  2363. break;
  2364. case AV_CODEC_ID_MPEG1VIDEO:
  2365. case AV_CODEC_ID_MPEG2VIDEO:
  2366. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
  2367. ff_mpeg1_encode_slice_header(s);
  2368. ff_mpeg1_clean_buffers(s);
  2369. }
  2370. break;
  2371. case AV_CODEC_ID_H263:
  2372. case AV_CODEC_ID_H263P:
  2373. if (CONFIG_H263_ENCODER)
  2374. ff_h263_encode_gob_header(s, mb_y);
  2375. break;
  2376. }
  2377. if (s->avctx->flags & AV_CODEC_FLAG_PASS1) {
  2378. int bits= put_bits_count(&s->pb);
  2379. s->misc_bits+= bits - s->last_bits;
  2380. s->last_bits= bits;
  2381. }
  2382. s->ptr_lastgob += current_packet_size;
  2383. s->first_slice_line=1;
  2384. s->resync_mb_x=mb_x;
  2385. s->resync_mb_y=mb_y;
  2386. }
  2387. }
  2388. if( (s->resync_mb_x == s->mb_x)
  2389. && s->resync_mb_y+1 == s->mb_y){
  2390. s->first_slice_line=0;
  2391. }
  2392. s->mb_skipped=0;
  2393. s->dquant=0; //only for QP_RD
  2394. update_mb_info(s, 0);
  2395. 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
  2396. int next_block=0;
  2397. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2398. copy_context_before_encode(&backup_s, s, -1);
  2399. backup_s.pb= s->pb;
  2400. best_s.data_partitioning= s->data_partitioning;
  2401. best_s.partitioned_frame= s->partitioned_frame;
  2402. if(s->data_partitioning){
  2403. backup_s.pb2= s->pb2;
  2404. backup_s.tex_pb= s->tex_pb;
  2405. }
  2406. if(mb_type&CANDIDATE_MB_TYPE_INTER){
  2407. s->mv_dir = MV_DIR_FORWARD;
  2408. s->mv_type = MV_TYPE_16X16;
  2409. s->mb_intra= 0;
  2410. s->mv[0][0][0] = s->p_mv_table[xy][0];
  2411. s->mv[0][0][1] = s->p_mv_table[xy][1];
  2412. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
  2413. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2414. }
  2415. if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
  2416. s->mv_dir = MV_DIR_FORWARD;
  2417. s->mv_type = MV_TYPE_FIELD;
  2418. s->mb_intra= 0;
  2419. for(i=0; i<2; i++){
  2420. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  2421. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  2422. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  2423. }
  2424. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
  2425. &dmin, &next_block, 0, 0);
  2426. }
  2427. if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
  2428. s->mv_dir = MV_DIR_FORWARD;
  2429. s->mv_type = MV_TYPE_16X16;
  2430. s->mb_intra= 0;
  2431. s->mv[0][0][0] = 0;
  2432. s->mv[0][0][1] = 0;
  2433. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
  2434. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2435. }
  2436. if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
  2437. s->mv_dir = MV_DIR_FORWARD;
  2438. s->mv_type = MV_TYPE_8X8;
  2439. s->mb_intra= 0;
  2440. for(i=0; i<4; i++){
  2441. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  2442. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  2443. }
  2444. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
  2445. &dmin, &next_block, 0, 0);
  2446. }
  2447. if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  2448. s->mv_dir = MV_DIR_FORWARD;
  2449. s->mv_type = MV_TYPE_16X16;
  2450. s->mb_intra= 0;
  2451. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2452. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2453. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
  2454. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2455. }
  2456. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  2457. s->mv_dir = MV_DIR_BACKWARD;
  2458. s->mv_type = MV_TYPE_16X16;
  2459. s->mb_intra= 0;
  2460. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2461. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2462. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  2463. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  2464. }
  2465. if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  2466. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2467. s->mv_type = MV_TYPE_16X16;
  2468. s->mb_intra= 0;
  2469. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2470. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2471. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2472. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2473. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
  2474. &dmin, &next_block, 0, 0);
  2475. }
  2476. if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
  2477. s->mv_dir = MV_DIR_FORWARD;
  2478. s->mv_type = MV_TYPE_FIELD;
  2479. s->mb_intra= 0;
  2480. for(i=0; i<2; i++){
  2481. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  2482. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  2483. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  2484. }
  2485. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
  2486. &dmin, &next_block, 0, 0);
  2487. }
  2488. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
  2489. s->mv_dir = MV_DIR_BACKWARD;
  2490. s->mv_type = MV_TYPE_FIELD;
  2491. s->mb_intra= 0;
  2492. for(i=0; i<2; i++){
  2493. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  2494. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  2495. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  2496. }
  2497. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
  2498. &dmin, &next_block, 0, 0);
  2499. }
  2500. if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
  2501. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2502. s->mv_type = MV_TYPE_FIELD;
  2503. s->mb_intra= 0;
  2504. for(dir=0; dir<2; dir++){
  2505. for(i=0; i<2; i++){
  2506. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  2507. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  2508. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  2509. }
  2510. }
  2511. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
  2512. &dmin, &next_block, 0, 0);
  2513. }
  2514. if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  2515. s->mv_dir = 0;
  2516. s->mv_type = MV_TYPE_16X16;
  2517. s->mb_intra= 1;
  2518. s->mv[0][0][0] = 0;
  2519. s->mv[0][0][1] = 0;
  2520. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
  2521. &dmin, &next_block, 0, 0);
  2522. if(s->h263_pred || s->h263_aic){
  2523. if(best_s.mb_intra)
  2524. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  2525. else
  2526. ff_clean_intra_table_entries(s); //old mode?
  2527. }
  2528. }
  2529. if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
  2530. if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
  2531. const int last_qp= backup_s.qscale;
  2532. int qpi, qp, dc[6];
  2533. int16_t ac[6][16];
  2534. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  2535. static const int dquant_tab[4]={-1,1,-2,2};
  2536. assert(backup_s.dquant == 0);
  2537. //FIXME intra
  2538. s->mv_dir= best_s.mv_dir;
  2539. s->mv_type = MV_TYPE_16X16;
  2540. s->mb_intra= best_s.mb_intra;
  2541. s->mv[0][0][0] = best_s.mv[0][0][0];
  2542. s->mv[0][0][1] = best_s.mv[0][0][1];
  2543. s->mv[1][0][0] = best_s.mv[1][0][0];
  2544. s->mv[1][0][1] = best_s.mv[1][0][1];
  2545. qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
  2546. for(; qpi<4; qpi++){
  2547. int dquant= dquant_tab[qpi];
  2548. qp= last_qp + dquant;
  2549. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  2550. continue;
  2551. backup_s.dquant= dquant;
  2552. if(s->mb_intra && s->dc_val[0]){
  2553. for(i=0; i<6; i++){
  2554. dc[i]= s->dc_val[0][ s->block_index[i] ];
  2555. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
  2556. }
  2557. }
  2558. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  2559. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  2560. if(best_s.qscale != qp){
  2561. if(s->mb_intra && s->dc_val[0]){
  2562. for(i=0; i<6; i++){
  2563. s->dc_val[0][ s->block_index[i] ]= dc[i];
  2564. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
  2565. }
  2566. }
  2567. }
  2568. }
  2569. }
  2570. }
  2571. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
  2572. int mx= s->b_direct_mv_table[xy][0];
  2573. int my= s->b_direct_mv_table[xy][1];
  2574. backup_s.dquant = 0;
  2575. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2576. s->mb_intra= 0;
  2577. ff_mpeg4_set_direct_mv(s, mx, my);
  2578. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2579. &dmin, &next_block, mx, my);
  2580. }
  2581. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
  2582. backup_s.dquant = 0;
  2583. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2584. s->mb_intra= 0;
  2585. ff_mpeg4_set_direct_mv(s, 0, 0);
  2586. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2587. &dmin, &next_block, 0, 0);
  2588. }
  2589. if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
  2590. int coded=0;
  2591. for(i=0; i<6; i++)
  2592. coded |= s->block_last_index[i];
  2593. if(coded){
  2594. int mx,my;
  2595. memcpy(s->mv, best_s.mv, sizeof(s->mv));
  2596. if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
  2597. mx=my=0; //FIXME find the one we actually used
  2598. ff_mpeg4_set_direct_mv(s, mx, my);
  2599. }else if(best_s.mv_dir&MV_DIR_BACKWARD){
  2600. mx= s->mv[1][0][0];
  2601. my= s->mv[1][0][1];
  2602. }else{
  2603. mx= s->mv[0][0][0];
  2604. my= s->mv[0][0][1];
  2605. }
  2606. s->mv_dir= best_s.mv_dir;
  2607. s->mv_type = best_s.mv_type;
  2608. s->mb_intra= 0;
  2609. /* s->mv[0][0][0] = best_s.mv[0][0][0];
  2610. s->mv[0][0][1] = best_s.mv[0][0][1];
  2611. s->mv[1][0][0] = best_s.mv[1][0][0];
  2612. s->mv[1][0][1] = best_s.mv[1][0][1];*/
  2613. backup_s.dquant= 0;
  2614. s->skipdct=1;
  2615. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  2616. &dmin, &next_block, mx, my);
  2617. s->skipdct=0;
  2618. }
  2619. }
  2620. s->current_picture.qscale_table[xy] = best_s.qscale;
  2621. copy_context_after_encode(s, &best_s, -1);
  2622. pb_bits_count= put_bits_count(&s->pb);
  2623. flush_put_bits(&s->pb);
  2624. avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2625. s->pb= backup_s.pb;
  2626. if(s->data_partitioning){
  2627. pb2_bits_count= put_bits_count(&s->pb2);
  2628. flush_put_bits(&s->pb2);
  2629. avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2630. s->pb2= backup_s.pb2;
  2631. tex_pb_bits_count= put_bits_count(&s->tex_pb);
  2632. flush_put_bits(&s->tex_pb);
  2633. avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2634. s->tex_pb= backup_s.tex_pb;
  2635. }
  2636. s->last_bits= put_bits_count(&s->pb);
  2637. if (CONFIG_H263_ENCODER &&
  2638. s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
  2639. ff_h263_update_motion_val(s);
  2640. if(next_block==0){ //FIXME 16 vs linesize16
  2641. s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
  2642. s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
  2643. s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
  2644. }
  2645. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  2646. ff_mpv_decode_mb(s, s->block);
  2647. } else {
  2648. int motion_x = 0, motion_y = 0;
  2649. s->mv_type=MV_TYPE_16X16;
  2650. // only one MB-Type possible
  2651. switch(mb_type){
  2652. case CANDIDATE_MB_TYPE_INTRA:
  2653. s->mv_dir = 0;
  2654. s->mb_intra= 1;
  2655. motion_x= s->mv[0][0][0] = 0;
  2656. motion_y= s->mv[0][0][1] = 0;
  2657. break;
  2658. case CANDIDATE_MB_TYPE_INTER:
  2659. s->mv_dir = MV_DIR_FORWARD;
  2660. s->mb_intra= 0;
  2661. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2662. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2663. break;
  2664. case CANDIDATE_MB_TYPE_INTER_I:
  2665. s->mv_dir = MV_DIR_FORWARD;
  2666. s->mv_type = MV_TYPE_FIELD;
  2667. s->mb_intra= 0;
  2668. for(i=0; i<2; i++){
  2669. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  2670. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  2671. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  2672. }
  2673. break;
  2674. case CANDIDATE_MB_TYPE_INTER4V:
  2675. s->mv_dir = MV_DIR_FORWARD;
  2676. s->mv_type = MV_TYPE_8X8;
  2677. s->mb_intra= 0;
  2678. for(i=0; i<4; i++){
  2679. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  2680. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  2681. }
  2682. break;
  2683. case CANDIDATE_MB_TYPE_DIRECT:
  2684. if (CONFIG_MPEG4_ENCODER) {
  2685. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  2686. s->mb_intra= 0;
  2687. motion_x=s->b_direct_mv_table[xy][0];
  2688. motion_y=s->b_direct_mv_table[xy][1];
  2689. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  2690. }
  2691. break;
  2692. case CANDIDATE_MB_TYPE_DIRECT0:
  2693. if (CONFIG_MPEG4_ENCODER) {
  2694. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  2695. s->mb_intra= 0;
  2696. ff_mpeg4_set_direct_mv(s, 0, 0);
  2697. }
  2698. break;
  2699. case CANDIDATE_MB_TYPE_BIDIR:
  2700. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2701. s->mb_intra= 0;
  2702. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2703. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2704. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2705. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2706. break;
  2707. case CANDIDATE_MB_TYPE_BACKWARD:
  2708. s->mv_dir = MV_DIR_BACKWARD;
  2709. s->mb_intra= 0;
  2710. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2711. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2712. break;
  2713. case CANDIDATE_MB_TYPE_FORWARD:
  2714. s->mv_dir = MV_DIR_FORWARD;
  2715. s->mb_intra= 0;
  2716. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2717. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2718. break;
  2719. case CANDIDATE_MB_TYPE_FORWARD_I:
  2720. s->mv_dir = MV_DIR_FORWARD;
  2721. s->mv_type = MV_TYPE_FIELD;
  2722. s->mb_intra= 0;
  2723. for(i=0; i<2; i++){
  2724. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  2725. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  2726. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  2727. }
  2728. break;
  2729. case CANDIDATE_MB_TYPE_BACKWARD_I:
  2730. s->mv_dir = MV_DIR_BACKWARD;
  2731. s->mv_type = MV_TYPE_FIELD;
  2732. s->mb_intra= 0;
  2733. for(i=0; i<2; i++){
  2734. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  2735. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  2736. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  2737. }
  2738. break;
  2739. case CANDIDATE_MB_TYPE_BIDIR_I:
  2740. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2741. s->mv_type = MV_TYPE_FIELD;
  2742. s->mb_intra= 0;
  2743. for(dir=0; dir<2; dir++){
  2744. for(i=0; i<2; i++){
  2745. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  2746. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  2747. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  2748. }
  2749. }
  2750. break;
  2751. default:
  2752. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  2753. }
  2754. encode_mb(s, motion_x, motion_y);
  2755. // RAL: Update last macroblock type
  2756. s->last_mv_dir = s->mv_dir;
  2757. if (CONFIG_H263_ENCODER &&
  2758. s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
  2759. ff_h263_update_motion_val(s);
  2760. ff_mpv_decode_mb(s, s->block);
  2761. }
  2762. /* clean the MV table in IPS frames for direct mode in B frames */
  2763. if(s->mb_intra /* && I,P,S_TYPE */){
  2764. s->p_mv_table[xy][0]=0;
  2765. s->p_mv_table[xy][1]=0;
  2766. }
  2767. if (s->avctx->flags & AV_CODEC_FLAG_PSNR) {
  2768. int w= 16;
  2769. int h= 16;
  2770. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2771. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2772. s->current_picture.encoding_error[0] += sse(
  2773. s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2774. s->dest[0], w, h, s->linesize);
  2775. s->current_picture.encoding_error[1] += sse(
  2776. s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
  2777. s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
  2778. s->current_picture.encoding_error[2] += sse(
  2779. s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
  2780. s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
  2781. }
  2782. if(s->loop_filter){
  2783. if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
  2784. ff_h263_loop_filter(s);
  2785. }
  2786. ff_dlog(s->avctx, "MB %d %d bits\n",
  2787. s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
  2788. }
  2789. }
  2790. //not beautiful here but we must write it before flushing so it has to be here
  2791. if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
  2792. ff_msmpeg4_encode_ext_header(s);
  2793. write_slice_end(s);
  2794. /* Send the last GOB if RTP */
  2795. if (s->avctx->rtp_callback) {
  2796. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
  2797. pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
  2798. /* Call the RTP callback to send the last GOB */
  2799. emms_c();
  2800. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
  2801. }
  2802. return 0;
  2803. }
  2804. #define MERGE(field) dst->field += src->field; src->field=0
  2805. static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  2806. MERGE(me.scene_change_score);
  2807. MERGE(me.mc_mb_var_sum_temp);
  2808. MERGE(me.mb_var_sum_temp);
  2809. }
  2810. static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
  2811. int i;
  2812. MERGE(dct_count[0]); //note, the other dct vars are not part of the context
  2813. MERGE(dct_count[1]);
  2814. MERGE(mv_bits);
  2815. MERGE(i_tex_bits);
  2816. MERGE(p_tex_bits);
  2817. MERGE(i_count);
  2818. MERGE(f_count);
  2819. MERGE(b_count);
  2820. MERGE(skip_count);
  2821. MERGE(misc_bits);
  2822. MERGE(er.error_count);
  2823. MERGE(padding_bug_score);
  2824. MERGE(current_picture.encoding_error[0]);
  2825. MERGE(current_picture.encoding_error[1]);
  2826. MERGE(current_picture.encoding_error[2]);
  2827. if(dst->avctx->noise_reduction){
  2828. for(i=0; i<64; i++){
  2829. MERGE(dct_error_sum[0][i]);
  2830. MERGE(dct_error_sum[1][i]);
  2831. }
  2832. }
  2833. assert(put_bits_count(&src->pb) % 8 ==0);
  2834. assert(put_bits_count(&dst->pb) % 8 ==0);
  2835. avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
  2836. flush_put_bits(&dst->pb);
  2837. }
  2838. static int estimate_qp(MpegEncContext *s, int dry_run){
  2839. if (s->next_lambda){
  2840. s->current_picture_ptr->f->quality =
  2841. s->current_picture.f->quality = s->next_lambda;
  2842. if(!dry_run) s->next_lambda= 0;
  2843. } else if (!s->fixed_qscale) {
  2844. s->current_picture_ptr->f->quality =
  2845. s->current_picture.f->quality = ff_rate_estimate_qscale(s, dry_run);
  2846. if (s->current_picture.f->quality < 0)
  2847. return -1;
  2848. }
  2849. if(s->adaptive_quant){
  2850. switch(s->codec_id){
  2851. case AV_CODEC_ID_MPEG4:
  2852. if (CONFIG_MPEG4_ENCODER)
  2853. ff_clean_mpeg4_qscales(s);
  2854. break;
  2855. case AV_CODEC_ID_H263:
  2856. case AV_CODEC_ID_H263P:
  2857. case AV_CODEC_ID_FLV1:
  2858. if (CONFIG_H263_ENCODER)
  2859. ff_clean_h263_qscales(s);
  2860. break;
  2861. default:
  2862. ff_init_qscale_tab(s);
  2863. }
  2864. s->lambda= s->lambda_table[0];
  2865. //FIXME broken
  2866. }else
  2867. s->lambda = s->current_picture.f->quality;
  2868. update_qscale(s);
  2869. return 0;
  2870. }
  2871. /* must be called before writing the header */
  2872. static void set_frame_distances(MpegEncContext * s){
  2873. assert(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
  2874. s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
  2875. if(s->pict_type==AV_PICTURE_TYPE_B){
  2876. s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
  2877. assert(s->pb_time > 0 && s->pb_time < s->pp_time);
  2878. }else{
  2879. s->pp_time= s->time - s->last_non_b_time;
  2880. s->last_non_b_time= s->time;
  2881. assert(s->picture_number==0 || s->pp_time > 0);
  2882. }
  2883. }
  2884. static int encode_picture(MpegEncContext *s, int picture_number)
  2885. {
  2886. int i, ret;
  2887. int bits;
  2888. int context_count = s->slice_context_count;
  2889. s->picture_number = picture_number;
  2890. /* Reset the average MB variance */
  2891. s->me.mb_var_sum_temp =
  2892. s->me.mc_mb_var_sum_temp = 0;
  2893. /* we need to initialize some time vars before we can encode b-frames */
  2894. // RAL: Condition added for MPEG1VIDEO
  2895. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
  2896. set_frame_distances(s);
  2897. if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
  2898. ff_set_mpeg4_time(s);
  2899. s->me.scene_change_score=0;
  2900. // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
  2901. if(s->pict_type==AV_PICTURE_TYPE_I){
  2902. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  2903. else s->no_rounding=0;
  2904. }else if(s->pict_type!=AV_PICTURE_TYPE_B){
  2905. if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
  2906. s->no_rounding ^= 1;
  2907. }
  2908. if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
  2909. if (estimate_qp(s,1) < 0)
  2910. return -1;
  2911. ff_get_2pass_fcode(s);
  2912. } else if (!(s->avctx->flags & AV_CODEC_FLAG_QSCALE)) {
  2913. if(s->pict_type==AV_PICTURE_TYPE_B)
  2914. s->lambda= s->last_lambda_for[s->pict_type];
  2915. else
  2916. s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
  2917. update_qscale(s);
  2918. }
  2919. s->mb_intra=0; //for the rate distortion & bit compare functions
  2920. for(i=1; i<context_count; i++){
  2921. ret = ff_update_duplicate_context(s->thread_context[i], s);
  2922. if (ret < 0)
  2923. return ret;
  2924. }
  2925. if(ff_init_me(s)<0)
  2926. return -1;
  2927. /* Estimate motion for every MB */
  2928. if(s->pict_type != AV_PICTURE_TYPE_I){
  2929. s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
  2930. s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
  2931. if (s->pict_type != AV_PICTURE_TYPE_B) {
  2932. if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
  2933. s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  2934. }
  2935. }
  2936. s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  2937. }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
  2938. /* I-Frame */
  2939. for(i=0; i<s->mb_stride*s->mb_height; i++)
  2940. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  2941. if(!s->fixed_qscale){
  2942. /* finding spatial complexity for I-frame rate control */
  2943. s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  2944. }
  2945. }
  2946. for(i=1; i<context_count; i++){
  2947. merge_context_after_me(s, s->thread_context[i]);
  2948. }
  2949. s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
  2950. s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
  2951. emms_c();
  2952. if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
  2953. s->pict_type= AV_PICTURE_TYPE_I;
  2954. for(i=0; i<s->mb_stride*s->mb_height; i++)
  2955. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  2956. ff_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
  2957. s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  2958. }
  2959. if(!s->umvplus){
  2960. if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
  2961. s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  2962. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  2963. int a,b;
  2964. a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  2965. b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  2966. s->f_code= FFMAX3(s->f_code, a, b);
  2967. }
  2968. ff_fix_long_p_mvs(s);
  2969. ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
  2970. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  2971. int j;
  2972. for(i=0; i<2; i++){
  2973. for(j=0; j<2; j++)
  2974. ff_fix_long_mvs(s, s->p_field_select_table[i], j,
  2975. s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
  2976. }
  2977. }
  2978. }
  2979. if(s->pict_type==AV_PICTURE_TYPE_B){
  2980. int a, b;
  2981. a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  2982. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  2983. s->f_code = FFMAX(a, b);
  2984. a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  2985. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  2986. s->b_code = FFMAX(a, b);
  2987. ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  2988. ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  2989. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  2990. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  2991. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  2992. int dir, j;
  2993. for(dir=0; dir<2; dir++){
  2994. for(i=0; i<2; i++){
  2995. for(j=0; j<2; j++){
  2996. int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
  2997. : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  2998. ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
  2999. s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  3000. }
  3001. }
  3002. }
  3003. }
  3004. }
  3005. }
  3006. if (estimate_qp(s, 0) < 0)
  3007. return -1;
  3008. if (s->qscale < 3 && s->max_qcoeff <= 128 &&
  3009. s->pict_type == AV_PICTURE_TYPE_I &&
  3010. !(s->avctx->flags & AV_CODEC_FLAG_QSCALE))
  3011. s->qscale= 3; //reduce clipping problems
  3012. if (s->out_format == FMT_MJPEG) {
  3013. /* for mjpeg, we do include qscale in the matrix */
  3014. for(i=1;i<64;i++){
  3015. int j = s->idsp.idct_permutation[i];
  3016. s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  3017. }
  3018. s->y_dc_scale_table=
  3019. s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
  3020. s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
  3021. ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  3022. s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
  3023. s->qscale= 8;
  3024. }
  3025. //FIXME var duplication
  3026. s->current_picture_ptr->f->key_frame =
  3027. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
  3028. s->current_picture_ptr->f->pict_type =
  3029. s->current_picture.f->pict_type = s->pict_type;
  3030. if (s->current_picture.f->key_frame)
  3031. s->picture_in_gop_number=0;
  3032. s->last_bits= put_bits_count(&s->pb);
  3033. switch(s->out_format) {
  3034. case FMT_MJPEG:
  3035. if (CONFIG_MJPEG_ENCODER)
  3036. ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
  3037. s->intra_matrix);
  3038. break;
  3039. case FMT_H261:
  3040. if (CONFIG_H261_ENCODER)
  3041. ff_h261_encode_picture_header(s, picture_number);
  3042. break;
  3043. case FMT_H263:
  3044. if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
  3045. ff_wmv2_encode_picture_header(s, picture_number);
  3046. else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
  3047. ff_msmpeg4_encode_picture_header(s, picture_number);
  3048. else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
  3049. ff_mpeg4_encode_picture_header(s, picture_number);
  3050. else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
  3051. ret = ff_rv10_encode_picture_header(s, picture_number);
  3052. if (ret < 0)
  3053. return ret;
  3054. }
  3055. else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
  3056. ff_rv20_encode_picture_header(s, picture_number);
  3057. else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
  3058. ff_flv_encode_picture_header(s, picture_number);
  3059. else if (CONFIG_H263_ENCODER)
  3060. ff_h263_encode_picture_header(s, picture_number);
  3061. break;
  3062. case FMT_MPEG1:
  3063. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
  3064. ff_mpeg1_encode_picture_header(s, picture_number);
  3065. break;
  3066. default:
  3067. assert(0);
  3068. }
  3069. bits= put_bits_count(&s->pb);
  3070. s->header_bits= bits - s->last_bits;
  3071. for(i=1; i<context_count; i++){
  3072. update_duplicate_context_after_me(s->thread_context[i], s);
  3073. }
  3074. s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
  3075. for(i=1; i<context_count; i++){
  3076. merge_context_after_encode(s, s->thread_context[i]);
  3077. }
  3078. emms_c();
  3079. return 0;
  3080. }
  3081. static void denoise_dct_c(MpegEncContext *s, int16_t *block){
  3082. const int intra= s->mb_intra;
  3083. int i;
  3084. s->dct_count[intra]++;
  3085. for(i=0; i<64; i++){
  3086. int level= block[i];
  3087. if(level){
  3088. if(level>0){
  3089. s->dct_error_sum[intra][i] += level;
  3090. level -= s->dct_offset[intra][i];
  3091. if(level<0) level=0;
  3092. }else{
  3093. s->dct_error_sum[intra][i] -= level;
  3094. level += s->dct_offset[intra][i];
  3095. if(level>0) level=0;
  3096. }
  3097. block[i]= level;
  3098. }
  3099. }
  3100. }
  3101. static int dct_quantize_trellis_c(MpegEncContext *s,
  3102. int16_t *block, int n,
  3103. int qscale, int *overflow){
  3104. const int *qmat;
  3105. const uint8_t *scantable= s->intra_scantable.scantable;
  3106. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  3107. int max=0;
  3108. unsigned int threshold1, threshold2;
  3109. int bias=0;
  3110. int run_tab[65];
  3111. int level_tab[65];
  3112. int score_tab[65];
  3113. int survivor[65];
  3114. int survivor_count;
  3115. int last_run=0;
  3116. int last_level=0;
  3117. int last_score= 0;
  3118. int last_i;
  3119. int coeff[2][64];
  3120. int coeff_count[64];
  3121. int qmul, qadd, start_i, last_non_zero, i, dc;
  3122. const int esc_length= s->ac_esc_length;
  3123. uint8_t * length;
  3124. uint8_t * last_length;
  3125. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  3126. s->fdsp.fdct(block);
  3127. if(s->dct_error_sum)
  3128. s->denoise_dct(s, block);
  3129. qmul= qscale*16;
  3130. qadd= ((qscale-1)|1)*8;
  3131. if (s->mb_intra) {
  3132. int q;
  3133. if (!s->h263_aic) {
  3134. if (n < 4)
  3135. q = s->y_dc_scale;
  3136. else
  3137. q = s->c_dc_scale;
  3138. q = q << 3;
  3139. } else{
  3140. /* For AIC we skip quant/dequant of INTRADC */
  3141. q = 1 << 3;
  3142. qadd=0;
  3143. }
  3144. /* note: block[0] is assumed to be positive */
  3145. block[0] = (block[0] + (q >> 1)) / q;
  3146. start_i = 1;
  3147. last_non_zero = 0;
  3148. qmat = s->q_intra_matrix[qscale];
  3149. if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  3150. bias= 1<<(QMAT_SHIFT-1);
  3151. length = s->intra_ac_vlc_length;
  3152. last_length= s->intra_ac_vlc_last_length;
  3153. } else {
  3154. start_i = 0;
  3155. last_non_zero = -1;
  3156. qmat = s->q_inter_matrix[qscale];
  3157. length = s->inter_ac_vlc_length;
  3158. last_length= s->inter_ac_vlc_last_length;
  3159. }
  3160. last_i= start_i;
  3161. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3162. threshold2= (threshold1<<1);
  3163. for(i=63; i>=start_i; i--) {
  3164. const int j = scantable[i];
  3165. int level = block[j] * qmat[j];
  3166. if(((unsigned)(level+threshold1))>threshold2){
  3167. last_non_zero = i;
  3168. break;
  3169. }
  3170. }
  3171. for(i=start_i; i<=last_non_zero; i++) {
  3172. const int j = scantable[i];
  3173. int level = block[j] * qmat[j];
  3174. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3175. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3176. if(((unsigned)(level+threshold1))>threshold2){
  3177. if(level>0){
  3178. level= (bias + level)>>QMAT_SHIFT;
  3179. coeff[0][i]= level;
  3180. coeff[1][i]= level-1;
  3181. // coeff[2][k]= level-2;
  3182. }else{
  3183. level= (bias - level)>>QMAT_SHIFT;
  3184. coeff[0][i]= -level;
  3185. coeff[1][i]= -level+1;
  3186. // coeff[2][k]= -level+2;
  3187. }
  3188. coeff_count[i]= FFMIN(level, 2);
  3189. assert(coeff_count[i]);
  3190. max |=level;
  3191. }else{
  3192. coeff[0][i]= (level>>31)|1;
  3193. coeff_count[i]= 1;
  3194. }
  3195. }
  3196. *overflow= s->max_qcoeff < max; //overflow might have happened
  3197. if(last_non_zero < start_i){
  3198. memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
  3199. return last_non_zero;
  3200. }
  3201. score_tab[start_i]= 0;
  3202. survivor[0]= start_i;
  3203. survivor_count= 1;
  3204. for(i=start_i; i<=last_non_zero; i++){
  3205. int level_index, j, zero_distortion;
  3206. int dct_coeff= FFABS(block[ scantable[i] ]);
  3207. int best_score=256*256*256*120;
  3208. if (s->fdsp.fdct == ff_fdct_ifast)
  3209. dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
  3210. zero_distortion= dct_coeff*dct_coeff;
  3211. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3212. int distortion;
  3213. int level= coeff[level_index][i];
  3214. const int alevel= FFABS(level);
  3215. int unquant_coeff;
  3216. assert(level);
  3217. if(s->out_format == FMT_H263){
  3218. unquant_coeff= alevel*qmul + qadd;
  3219. }else{ //MPEG1
  3220. j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
  3221. if(s->mb_intra){
  3222. unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
  3223. unquant_coeff = (unquant_coeff - 1) | 1;
  3224. }else{
  3225. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3226. unquant_coeff = (unquant_coeff - 1) | 1;
  3227. }
  3228. unquant_coeff<<= 3;
  3229. }
  3230. distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
  3231. level+=64;
  3232. if((level&(~127)) == 0){
  3233. for(j=survivor_count-1; j>=0; j--){
  3234. int run= i - survivor[j];
  3235. int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3236. score += score_tab[i-run];
  3237. if(score < best_score){
  3238. best_score= score;
  3239. run_tab[i+1]= run;
  3240. level_tab[i+1]= level-64;
  3241. }
  3242. }
  3243. if(s->out_format == FMT_H263){
  3244. for(j=survivor_count-1; j>=0; j--){
  3245. int run= i - survivor[j];
  3246. int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3247. score += score_tab[i-run];
  3248. if(score < last_score){
  3249. last_score= score;
  3250. last_run= run;
  3251. last_level= level-64;
  3252. last_i= i+1;
  3253. }
  3254. }
  3255. }
  3256. }else{
  3257. distortion += esc_length*lambda;
  3258. for(j=survivor_count-1; j>=0; j--){
  3259. int run= i - survivor[j];
  3260. int score= distortion + score_tab[i-run];
  3261. if(score < best_score){
  3262. best_score= score;
  3263. run_tab[i+1]= run;
  3264. level_tab[i+1]= level-64;
  3265. }
  3266. }
  3267. if(s->out_format == FMT_H263){
  3268. for(j=survivor_count-1; j>=0; j--){
  3269. int run= i - survivor[j];
  3270. int score= distortion + score_tab[i-run];
  3271. if(score < last_score){
  3272. last_score= score;
  3273. last_run= run;
  3274. last_level= level-64;
  3275. last_i= i+1;
  3276. }
  3277. }
  3278. }
  3279. }
  3280. }
  3281. score_tab[i+1]= best_score;
  3282. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  3283. if(last_non_zero <= 27){
  3284. for(; survivor_count; survivor_count--){
  3285. if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  3286. break;
  3287. }
  3288. }else{
  3289. for(; survivor_count; survivor_count--){
  3290. if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  3291. break;
  3292. }
  3293. }
  3294. survivor[ survivor_count++ ]= i+1;
  3295. }
  3296. if(s->out_format != FMT_H263){
  3297. last_score= 256*256*256*120;
  3298. for(i= survivor[0]; i<=last_non_zero + 1; i++){
  3299. int score= score_tab[i];
  3300. if(i) score += lambda*2; //FIXME exacter?
  3301. if(score < last_score){
  3302. last_score= score;
  3303. last_i= i;
  3304. last_level= level_tab[i];
  3305. last_run= run_tab[i];
  3306. }
  3307. }
  3308. }
  3309. s->coded_score[n] = last_score;
  3310. dc= FFABS(block[0]);
  3311. last_non_zero= last_i - 1;
  3312. memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
  3313. if(last_non_zero < start_i)
  3314. return last_non_zero;
  3315. if(last_non_zero == 0 && start_i == 0){
  3316. int best_level= 0;
  3317. int best_score= dc * dc;
  3318. for(i=0; i<coeff_count[0]; i++){
  3319. int level= coeff[i][0];
  3320. int alevel= FFABS(level);
  3321. int unquant_coeff, score, distortion;
  3322. if(s->out_format == FMT_H263){
  3323. unquant_coeff= (alevel*qmul + qadd)>>3;
  3324. }else{ //MPEG1
  3325. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  3326. unquant_coeff = (unquant_coeff - 1) | 1;
  3327. }
  3328. unquant_coeff = (unquant_coeff + 4) >> 3;
  3329. unquant_coeff<<= 3 + 3;
  3330. distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
  3331. level+=64;
  3332. if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  3333. else score= distortion + esc_length*lambda;
  3334. if(score < best_score){
  3335. best_score= score;
  3336. best_level= level - 64;
  3337. }
  3338. }
  3339. block[0]= best_level;
  3340. s->coded_score[n] = best_score - dc*dc;
  3341. if(best_level == 0) return -1;
  3342. else return last_non_zero;
  3343. }
  3344. i= last_i;
  3345. assert(last_level);
  3346. block[ perm_scantable[last_non_zero] ]= last_level;
  3347. i -= last_run + 1;
  3348. for(; i>start_i; i -= run_tab[i] + 1){
  3349. block[ perm_scantable[i-1] ]= level_tab[i];
  3350. }
  3351. return last_non_zero;
  3352. }
  3353. //#define REFINE_STATS 1
  3354. static int16_t basis[64][64];
  3355. static void build_basis(uint8_t *perm){
  3356. int i, j, x, y;
  3357. emms_c();
  3358. for(i=0; i<8; i++){
  3359. for(j=0; j<8; j++){
  3360. for(y=0; y<8; y++){
  3361. for(x=0; x<8; x++){
  3362. double s= 0.25*(1<<BASIS_SHIFT);
  3363. int index= 8*i + j;
  3364. int perm_index= perm[index];
  3365. if(i==0) s*= sqrt(0.5);
  3366. if(j==0) s*= sqrt(0.5);
  3367. 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)));
  3368. }
  3369. }
  3370. }
  3371. }
  3372. }
  3373. static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
  3374. int16_t *block, int16_t *weight, int16_t *orig,
  3375. int n, int qscale){
  3376. int16_t rem[64];
  3377. LOCAL_ALIGNED_16(int16_t, d1, [64]);
  3378. const uint8_t *scantable= s->intra_scantable.scantable;
  3379. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  3380. // unsigned int threshold1, threshold2;
  3381. // int bias=0;
  3382. int run_tab[65];
  3383. int prev_run=0;
  3384. int prev_level=0;
  3385. int qmul, qadd, start_i, last_non_zero, i, dc;
  3386. uint8_t * length;
  3387. uint8_t * last_length;
  3388. int lambda;
  3389. int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
  3390. #ifdef REFINE_STATS
  3391. static int count=0;
  3392. static int after_last=0;
  3393. static int to_zero=0;
  3394. static int from_zero=0;
  3395. static int raise=0;
  3396. static int lower=0;
  3397. static int messed_sign=0;
  3398. #endif
  3399. if(basis[0][0] == 0)
  3400. build_basis(s->idsp.idct_permutation);
  3401. qmul= qscale*2;
  3402. qadd= (qscale-1)|1;
  3403. if (s->mb_intra) {
  3404. if (!s->h263_aic) {
  3405. if (n < 4)
  3406. q = s->y_dc_scale;
  3407. else
  3408. q = s->c_dc_scale;
  3409. } else{
  3410. /* For AIC we skip quant/dequant of INTRADC */
  3411. q = 1;
  3412. qadd=0;
  3413. }
  3414. q <<= RECON_SHIFT-3;
  3415. /* note: block[0] is assumed to be positive */
  3416. dc= block[0]*q;
  3417. // block[0] = (block[0] + (q >> 1)) / q;
  3418. start_i = 1;
  3419. // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  3420. // bias= 1<<(QMAT_SHIFT-1);
  3421. length = s->intra_ac_vlc_length;
  3422. last_length= s->intra_ac_vlc_last_length;
  3423. } else {
  3424. dc= 0;
  3425. start_i = 0;
  3426. length = s->inter_ac_vlc_length;
  3427. last_length= s->inter_ac_vlc_last_length;
  3428. }
  3429. last_non_zero = s->block_last_index[n];
  3430. #ifdef REFINE_STATS
  3431. {START_TIMER
  3432. #endif
  3433. dc += (1<<(RECON_SHIFT-1));
  3434. for(i=0; i<64; i++){
  3435. rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
  3436. }
  3437. #ifdef REFINE_STATS
  3438. STOP_TIMER("memset rem[]")}
  3439. #endif
  3440. sum=0;
  3441. for(i=0; i<64; i++){
  3442. int one= 36;
  3443. int qns=4;
  3444. int w;
  3445. w= FFABS(weight[i]) + qns*one;
  3446. w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
  3447. weight[i] = w;
  3448. // w=weight[i] = (63*qns + (w/2)) / w;
  3449. assert(w>0);
  3450. assert(w<(1<<6));
  3451. sum += w*w;
  3452. }
  3453. lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
  3454. #ifdef REFINE_STATS
  3455. {START_TIMER
  3456. #endif
  3457. run=0;
  3458. rle_index=0;
  3459. for(i=start_i; i<=last_non_zero; i++){
  3460. int j= perm_scantable[i];
  3461. const int level= block[j];
  3462. int coeff;
  3463. if(level){
  3464. if(level<0) coeff= qmul*level - qadd;
  3465. else coeff= qmul*level + qadd;
  3466. run_tab[rle_index++]=run;
  3467. run=0;
  3468. s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
  3469. }else{
  3470. run++;
  3471. }
  3472. }
  3473. #ifdef REFINE_STATS
  3474. if(last_non_zero>0){
  3475. STOP_TIMER("init rem[]")
  3476. }
  3477. }
  3478. {START_TIMER
  3479. #endif
  3480. for(;;){
  3481. int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
  3482. int best_coeff=0;
  3483. int best_change=0;
  3484. int run2, best_unquant_change=0, analyze_gradient;
  3485. #ifdef REFINE_STATS
  3486. {START_TIMER
  3487. #endif
  3488. analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
  3489. if(analyze_gradient){
  3490. #ifdef REFINE_STATS
  3491. {START_TIMER
  3492. #endif
  3493. for(i=0; i<64; i++){
  3494. int w= weight[i];
  3495. d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
  3496. }
  3497. #ifdef REFINE_STATS
  3498. STOP_TIMER("rem*w*w")}
  3499. {START_TIMER
  3500. #endif
  3501. s->fdsp.fdct(d1);
  3502. #ifdef REFINE_STATS
  3503. STOP_TIMER("dct")}
  3504. #endif
  3505. }
  3506. if(start_i){
  3507. const int level= block[0];
  3508. int change, old_coeff;
  3509. assert(s->mb_intra);
  3510. old_coeff= q*level;
  3511. for(change=-1; change<=1; change+=2){
  3512. int new_level= level + change;
  3513. int score, new_coeff;
  3514. new_coeff= q*new_level;
  3515. if(new_coeff >= 2048 || new_coeff < 0)
  3516. continue;
  3517. score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
  3518. new_coeff - old_coeff);
  3519. if(score<best_score){
  3520. best_score= score;
  3521. best_coeff= 0;
  3522. best_change= change;
  3523. best_unquant_change= new_coeff - old_coeff;
  3524. }
  3525. }
  3526. }
  3527. run=0;
  3528. rle_index=0;
  3529. run2= run_tab[rle_index++];
  3530. prev_level=0;
  3531. prev_run=0;
  3532. for(i=start_i; i<64; i++){
  3533. int j= perm_scantable[i];
  3534. const int level= block[j];
  3535. int change, old_coeff;
  3536. if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
  3537. break;
  3538. if(level){
  3539. if(level<0) old_coeff= qmul*level - qadd;
  3540. else old_coeff= qmul*level + qadd;
  3541. run2= run_tab[rle_index++]; //FIXME ! maybe after last
  3542. }else{
  3543. old_coeff=0;
  3544. run2--;
  3545. assert(run2>=0 || i >= last_non_zero );
  3546. }
  3547. for(change=-1; change<=1; change+=2){
  3548. int new_level= level + change;
  3549. int score, new_coeff, unquant_change;
  3550. score=0;
  3551. if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
  3552. continue;
  3553. if(new_level){
  3554. if(new_level<0) new_coeff= qmul*new_level - qadd;
  3555. else new_coeff= qmul*new_level + qadd;
  3556. if(new_coeff >= 2048 || new_coeff <= -2048)
  3557. continue;
  3558. //FIXME check for overflow
  3559. if(level){
  3560. if(level < 63 && level > -63){
  3561. if(i < last_non_zero)
  3562. score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
  3563. - length[UNI_AC_ENC_INDEX(run, level+64)];
  3564. else
  3565. score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
  3566. - last_length[UNI_AC_ENC_INDEX(run, level+64)];
  3567. }
  3568. }else{
  3569. assert(FFABS(new_level)==1);
  3570. if(analyze_gradient){
  3571. int g= d1[ scantable[i] ];
  3572. if(g && (g^new_level) >= 0)
  3573. continue;
  3574. }
  3575. if(i < last_non_zero){
  3576. int next_i= i + run2 + 1;
  3577. int next_level= block[ perm_scantable[next_i] ] + 64;
  3578. if(next_level&(~127))
  3579. next_level= 0;
  3580. if(next_i < last_non_zero)
  3581. score += length[UNI_AC_ENC_INDEX(run, 65)]
  3582. + length[UNI_AC_ENC_INDEX(run2, next_level)]
  3583. - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  3584. else
  3585. score += length[UNI_AC_ENC_INDEX(run, 65)]
  3586. + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  3587. - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  3588. }else{
  3589. score += last_length[UNI_AC_ENC_INDEX(run, 65)];
  3590. if(prev_level){
  3591. score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  3592. - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  3593. }
  3594. }
  3595. }
  3596. }else{
  3597. new_coeff=0;
  3598. assert(FFABS(level)==1);
  3599. if(i < last_non_zero){
  3600. int next_i= i + run2 + 1;
  3601. int next_level= block[ perm_scantable[next_i] ] + 64;
  3602. if(next_level&(~127))
  3603. next_level= 0;
  3604. if(next_i < last_non_zero)
  3605. score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  3606. - length[UNI_AC_ENC_INDEX(run2, next_level)]
  3607. - length[UNI_AC_ENC_INDEX(run, 65)];
  3608. else
  3609. score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  3610. - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  3611. - length[UNI_AC_ENC_INDEX(run, 65)];
  3612. }else{
  3613. score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
  3614. if(prev_level){
  3615. score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  3616. - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  3617. }
  3618. }
  3619. }
  3620. score *= lambda;
  3621. unquant_change= new_coeff - old_coeff;
  3622. assert((score < 100*lambda && score > -100*lambda) || lambda==0);
  3623. score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
  3624. unquant_change);
  3625. if(score<best_score){
  3626. best_score= score;
  3627. best_coeff= i;
  3628. best_change= change;
  3629. best_unquant_change= unquant_change;
  3630. }
  3631. }
  3632. if(level){
  3633. prev_level= level + 64;
  3634. if(prev_level&(~127))
  3635. prev_level= 0;
  3636. prev_run= run;
  3637. run=0;
  3638. }else{
  3639. run++;
  3640. }
  3641. }
  3642. #ifdef REFINE_STATS
  3643. STOP_TIMER("iterative step")}
  3644. #endif
  3645. if(best_change){
  3646. int j= perm_scantable[ best_coeff ];
  3647. block[j] += best_change;
  3648. if(best_coeff > last_non_zero){
  3649. last_non_zero= best_coeff;
  3650. assert(block[j]);
  3651. #ifdef REFINE_STATS
  3652. after_last++;
  3653. #endif
  3654. }else{
  3655. #ifdef REFINE_STATS
  3656. if(block[j]){
  3657. if(block[j] - best_change){
  3658. if(FFABS(block[j]) > FFABS(block[j] - best_change)){
  3659. raise++;
  3660. }else{
  3661. lower++;
  3662. }
  3663. }else{
  3664. from_zero++;
  3665. }
  3666. }else{
  3667. to_zero++;
  3668. }
  3669. #endif
  3670. for(; last_non_zero>=start_i; last_non_zero--){
  3671. if(block[perm_scantable[last_non_zero]])
  3672. break;
  3673. }
  3674. }
  3675. #ifdef REFINE_STATS
  3676. count++;
  3677. if(256*256*256*64 % count == 0){
  3678. 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);
  3679. }
  3680. #endif
  3681. run=0;
  3682. rle_index=0;
  3683. for(i=start_i; i<=last_non_zero; i++){
  3684. int j= perm_scantable[i];
  3685. const int level= block[j];
  3686. if(level){
  3687. run_tab[rle_index++]=run;
  3688. run=0;
  3689. }else{
  3690. run++;
  3691. }
  3692. }
  3693. s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
  3694. }else{
  3695. break;
  3696. }
  3697. }
  3698. #ifdef REFINE_STATS
  3699. if(last_non_zero>0){
  3700. STOP_TIMER("iterative search")
  3701. }
  3702. }
  3703. #endif
  3704. return last_non_zero;
  3705. }
  3706. /**
  3707. * Permute an 8x8 block according to permuatation.
  3708. * @param block the block which will be permuted according to
  3709. * the given permutation vector
  3710. * @param permutation the permutation vector
  3711. * @param last the last non zero coefficient in scantable order, used to
  3712. * speed the permutation up
  3713. * @param scantable the used scantable, this is only used to speed the
  3714. * permutation up, the block is not (inverse) permutated
  3715. * to scantable order!
  3716. */
  3717. static void block_permute(int16_t *block, uint8_t *permutation,
  3718. const uint8_t *scantable, int last)
  3719. {
  3720. int i;
  3721. int16_t temp[64];
  3722. if (last <= 0)
  3723. return;
  3724. //FIXME it is ok but not clean and might fail for some permutations
  3725. // if (permutation[1] == 1)
  3726. // return;
  3727. for (i = 0; i <= last; i++) {
  3728. const int j = scantable[i];
  3729. temp[j] = block[j];
  3730. block[j] = 0;
  3731. }
  3732. for (i = 0; i <= last; i++) {
  3733. const int j = scantable[i];
  3734. const int perm_j = permutation[j];
  3735. block[perm_j] = temp[j];
  3736. }
  3737. }
  3738. int ff_dct_quantize_c(MpegEncContext *s,
  3739. int16_t *block, int n,
  3740. int qscale, int *overflow)
  3741. {
  3742. int i, j, level, last_non_zero, q, start_i;
  3743. const int *qmat;
  3744. const uint8_t *scantable= s->intra_scantable.scantable;
  3745. int bias;
  3746. int max=0;
  3747. unsigned int threshold1, threshold2;
  3748. s->fdsp.fdct(block);
  3749. if(s->dct_error_sum)
  3750. s->denoise_dct(s, block);
  3751. if (s->mb_intra) {
  3752. if (!s->h263_aic) {
  3753. if (n < 4)
  3754. q = s->y_dc_scale;
  3755. else
  3756. q = s->c_dc_scale;
  3757. q = q << 3;
  3758. } else
  3759. /* For AIC we skip quant/dequant of INTRADC */
  3760. q = 1 << 3;
  3761. /* note: block[0] is assumed to be positive */
  3762. block[0] = (block[0] + (q >> 1)) / q;
  3763. start_i = 1;
  3764. last_non_zero = 0;
  3765. qmat = s->q_intra_matrix[qscale];
  3766. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3767. } else {
  3768. start_i = 0;
  3769. last_non_zero = -1;
  3770. qmat = s->q_inter_matrix[qscale];
  3771. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3772. }
  3773. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3774. threshold2= (threshold1<<1);
  3775. for(i=63;i>=start_i;i--) {
  3776. j = scantable[i];
  3777. level = block[j] * qmat[j];
  3778. if(((unsigned)(level+threshold1))>threshold2){
  3779. last_non_zero = i;
  3780. break;
  3781. }else{
  3782. block[j]=0;
  3783. }
  3784. }
  3785. for(i=start_i; i<=last_non_zero; i++) {
  3786. j = scantable[i];
  3787. level = block[j] * qmat[j];
  3788. // if( bias+level >= (1<<QMAT_SHIFT)
  3789. // || bias-level >= (1<<QMAT_SHIFT)){
  3790. if(((unsigned)(level+threshold1))>threshold2){
  3791. if(level>0){
  3792. level= (bias + level)>>QMAT_SHIFT;
  3793. block[j]= level;
  3794. }else{
  3795. level= (bias - level)>>QMAT_SHIFT;
  3796. block[j]= -level;
  3797. }
  3798. max |=level;
  3799. }else{
  3800. block[j]=0;
  3801. }
  3802. }
  3803. *overflow= s->max_qcoeff < max; //overflow might have happened
  3804. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  3805. if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
  3806. block_permute(block, s->idsp.idct_permutation,
  3807. scantable, last_non_zero);
  3808. return last_non_zero;
  3809. }
  3810. #define OFFSET(x) offsetof(MpegEncContext, x)
  3811. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  3812. static const AVOption h263_options[] = {
  3813. { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  3814. { "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},
  3815. { "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 },
  3816. FF_MPV_COMMON_OPTS
  3817. { NULL },
  3818. };
  3819. static const AVClass h263_class = {
  3820. .class_name = "H.263 encoder",
  3821. .item_name = av_default_item_name,
  3822. .option = h263_options,
  3823. .version = LIBAVUTIL_VERSION_INT,
  3824. };
  3825. AVCodec ff_h263_encoder = {
  3826. .name = "h263",
  3827. .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
  3828. .type = AVMEDIA_TYPE_VIDEO,
  3829. .id = AV_CODEC_ID_H263,
  3830. .priv_data_size = sizeof(MpegEncContext),
  3831. .init = ff_mpv_encode_init,
  3832. .encode2 = ff_mpv_encode_picture,
  3833. .close = ff_mpv_encode_end,
  3834. .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
  3835. .priv_class = &h263_class,
  3836. };
  3837. static const AVOption h263p_options[] = {
  3838. { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  3839. { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  3840. { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  3841. { "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},
  3842. FF_MPV_COMMON_OPTS
  3843. { NULL },
  3844. };
  3845. static const AVClass h263p_class = {
  3846. .class_name = "H.263p encoder",
  3847. .item_name = av_default_item_name,
  3848. .option = h263p_options,
  3849. .version = LIBAVUTIL_VERSION_INT,
  3850. };
  3851. AVCodec ff_h263p_encoder = {
  3852. .name = "h263p",
  3853. .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
  3854. .type = AVMEDIA_TYPE_VIDEO,
  3855. .id = AV_CODEC_ID_H263P,
  3856. .priv_data_size = sizeof(MpegEncContext),
  3857. .init = ff_mpv_encode_init,
  3858. .encode2 = ff_mpv_encode_picture,
  3859. .close = ff_mpv_encode_end,
  3860. .capabilities = AV_CODEC_CAP_SLICE_THREADS,
  3861. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  3862. .priv_class = &h263p_class,
  3863. };
  3864. static const AVClass msmpeg4v2_class = {
  3865. .class_name = "msmpeg4v2 encoder",
  3866. .item_name = av_default_item_name,
  3867. .option = ff_mpv_generic_options,
  3868. .version = LIBAVUTIL_VERSION_INT,
  3869. };
  3870. AVCodec ff_msmpeg4v2_encoder = {
  3871. .name = "msmpeg4v2",
  3872. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
  3873. .type = AVMEDIA_TYPE_VIDEO,
  3874. .id = AV_CODEC_ID_MSMPEG4V2,
  3875. .priv_data_size = sizeof(MpegEncContext),
  3876. .init = ff_mpv_encode_init,
  3877. .encode2 = ff_mpv_encode_picture,
  3878. .close = ff_mpv_encode_end,
  3879. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  3880. .priv_class = &msmpeg4v2_class,
  3881. };
  3882. static const AVClass msmpeg4v3_class = {
  3883. .class_name = "msmpeg4v3 encoder",
  3884. .item_name = av_default_item_name,
  3885. .option = ff_mpv_generic_options,
  3886. .version = LIBAVUTIL_VERSION_INT,
  3887. };
  3888. AVCodec ff_msmpeg4v3_encoder = {
  3889. .name = "msmpeg4",
  3890. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
  3891. .type = AVMEDIA_TYPE_VIDEO,
  3892. .id = AV_CODEC_ID_MSMPEG4V3,
  3893. .priv_data_size = sizeof(MpegEncContext),
  3894. .init = ff_mpv_encode_init,
  3895. .encode2 = ff_mpv_encode_picture,
  3896. .close = ff_mpv_encode_end,
  3897. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  3898. .priv_class = &msmpeg4v3_class,
  3899. };
  3900. static const AVClass wmv1_class = {
  3901. .class_name = "wmv1 encoder",
  3902. .item_name = av_default_item_name,
  3903. .option = ff_mpv_generic_options,
  3904. .version = LIBAVUTIL_VERSION_INT,
  3905. };
  3906. AVCodec ff_wmv1_encoder = {
  3907. .name = "wmv1",
  3908. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
  3909. .type = AVMEDIA_TYPE_VIDEO,
  3910. .id = AV_CODEC_ID_WMV1,
  3911. .priv_data_size = sizeof(MpegEncContext),
  3912. .init = ff_mpv_encode_init,
  3913. .encode2 = ff_mpv_encode_picture,
  3914. .close = ff_mpv_encode_end,
  3915. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  3916. .priv_class = &wmv1_class,
  3917. };