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.

4312 lines
157KB

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