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.

4309 lines
157KB

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