You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4317 lines
157KB

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