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.

4326 lines
157KB

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