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

4342 lines
158KB

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