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.

4355 lines
159KB

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