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.

4379 lines
160KB

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