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.

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