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.

4302 lines
157KB

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