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.

4315 lines
156KB

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