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.

4436 lines
159KB

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