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.

4316 lines
157KB

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