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.

4450 lines
160KB

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