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.

4317 lines
157KB

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