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.

4420 lines
159KB

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