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.

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