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.

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