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.

4375 lines
160KB

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