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.

4442 lines
160KB

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