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.

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