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.

4467 lines
161KB

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