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.

4342 lines
158KB

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