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.

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