mpegvideoenc.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Generic DCT based hybrid video encoder
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * mpegvideo header.
  25. */
  26. #ifndef AVCODEC_MPEGVIDEOENC_H
  27. #define AVCODEC_MPEGVIDEOENC_H
  28. #include <float.h>
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/mem_internal.h"
  31. #include "libavutil/opt.h"
  32. #include "fdctdsp.h"
  33. #include "motion_est.h"
  34. #include "mpegvideo.h"
  35. #include "mpegvideoencdsp.h"
  36. #include "pixblockdsp.h"
  37. #include "put_bits.h"
  38. #include "ratecontrol.h"
  39. #define MPVENC_MAX_B_FRAMES 16
  40. typedef struct MPVEncContext {
  41. MpegEncContext c; ///< the common base context
  42. /** bit output */
  43. PutBitContext pb;
  44. unsigned int lambda; ///< Lagrange multiplier used in rate distortion
  45. unsigned int lambda2; ///< (lambda*lambda) >> FF_LAMBDA_SHIFT
  46. int *lambda_table;
  47. int adaptive_quant; ///< use adaptive quantization
  48. int dquant; ///< qscale difference to prev qscale
  49. int skipdct; ///< skip dct and code zero residual
  50. int quantizer_noise_shaping;
  51. int luma_elim_threshold;
  52. int chroma_elim_threshold;
  53. int mpv_flags; ///< flags set by private options
  54. /// Bitfield containing information which frames to reconstruct.
  55. int frame_reconstruction_bitfield;
  56. /**
  57. * Reference to the source picture.
  58. */
  59. AVFrame *new_pic;
  60. struct MPVMainEncContext *parent;
  61. FDCTDSPContext fdsp;
  62. MpegvideoEncDSPContext mpvencdsp;
  63. PixblockDSPContext pdsp;
  64. MotionEstContext me;
  65. int f_code; ///< forward MV resolution
  66. int b_code; ///< backward MV resolution for B-frames
  67. int16_t (*p_mv_table)[2]; ///< MV table (1MV per MB) P-frame
  68. int16_t (*b_forw_mv_table)[2]; ///< MV table (1MV per MB) forward mode B-frame
  69. int16_t (*b_back_mv_table)[2]; ///< MV table (1MV per MB) backward mode B-frame
  70. int16_t (*b_bidir_forw_mv_table)[2]; ///< MV table (1MV per MB) bidir mode B-frame
  71. int16_t (*b_bidir_back_mv_table)[2]; ///< MV table (1MV per MB) bidir mode B-frame
  72. int16_t (*b_direct_mv_table)[2]; ///< MV table (1MV per MB) direct mode B-frame
  73. int16_t (*b_field_mv_table[2][2][2])[2];///< MV table (4MV per MB) interlaced B-frame
  74. uint8_t (*p_field_select_table[2]); ///< Only the first element is allocated
  75. uint8_t (*b_field_select_table[2][2]); ///< allocated jointly with p_field_select_table
  76. uint16_t *mb_type; ///< Table for candidate MB types
  77. uint16_t *mb_var; ///< Table for MB variances
  78. uint16_t *mc_mb_var; ///< Table for motion compensated MB variances
  79. uint8_t *mb_mean; ///< Table for MB luminance
  80. uint64_t encoding_error[MPV_MAX_PLANES];
  81. int intra_quant_bias; ///< bias for the quantizer
  82. int inter_quant_bias; ///< bias for the quantizer
  83. int min_qcoeff; ///< minimum encodable coefficient
  84. int max_qcoeff; ///< maximum encodable coefficient
  85. int ac_esc_length; ///< num of bits needed to encode the longest esc
  86. const uint8_t *intra_ac_vlc_length;
  87. const uint8_t *intra_ac_vlc_last_length;
  88. const uint8_t *intra_chroma_ac_vlc_length;
  89. const uint8_t *intra_chroma_ac_vlc_last_length;
  90. const uint8_t *inter_ac_vlc_length;
  91. const uint8_t *inter_ac_vlc_last_length;
  92. const uint8_t *luma_dc_vlc_length;
  93. int coded_score[12];
  94. int16_t (*block)[64]; ///< points into blocks below
  95. /** precomputed matrix (combine qscale and DCT renorm) */
  96. int (*q_intra_matrix)[64];
  97. int (*q_chroma_intra_matrix)[64];
  98. int (*q_inter_matrix)[64];
  99. /** identical to the above but for MMX & these are not permutated, second 64 entries are bias*/
  100. uint16_t (*q_intra_matrix16)[2][64];
  101. uint16_t (*q_chroma_intra_matrix16)[2][64];
  102. uint16_t (*q_inter_matrix16)[2][64];
  103. /* noise reduction */
  104. int (*dct_error_sum)[64];
  105. int dct_count[2];
  106. uint16_t (*dct_offset)[64];
  107. int picture_number;
  108. /* statistics, used for 2-pass encoding */
  109. int mv_bits;
  110. int i_tex_bits;
  111. int p_tex_bits;
  112. int i_count;
  113. int misc_bits; ///< cbp, mb_type
  114. int last_bits; ///< temp var used for calculating the above vars
  115. int mb_skip_run;
  116. int last_dc[3]; ///< last DC values
  117. /* H.263 specific */
  118. int gob_index;
  119. int mb_info; ///< interval for outputting info about mb offsets as side data
  120. int prev_mb_info, last_mb_info;
  121. int mb_info_size;
  122. uint8_t *mb_info_ptr;
  123. /* H.263+ specific */
  124. int umvplus; ///< == H.263+ && unrestricted_mv
  125. int h263_slice_structured;
  126. int alt_inter_vlc; ///< alternative inter vlc
  127. int modified_quant;
  128. int loop_filter;
  129. /* MJPEG specific */
  130. struct MJpegContext *mjpeg_ctx;
  131. int esc_pos;
  132. /* MPEG-1 specific */
  133. int last_mv_dir; ///< last mv_dir, used for B-frame encoding
  134. /* MPEG-4 specific */
  135. int data_partitioning; ///< data partitioning flag, set via option
  136. int partitioned_frame; ///< is current frame partitioned
  137. int mpeg_quant;
  138. PutBitContext tex_pb; ///< used for data partitioned VOPs
  139. PutBitContext pb2; ///< used for data partitioned VOPs
  140. /* MSMPEG4 specific */
  141. int slice_height; ///< in macroblocks
  142. int flipflop_rounding; ///< also used for MPEG-4, H.263+
  143. int esc3_level_length;
  144. /* RTP specific */
  145. int rtp_mode;
  146. int rtp_payload_size;
  147. int error_rate;
  148. uint8_t *ptr_lastgob;
  149. void (*encode_mb)(struct MPVEncContext *s, int16_t block[][64],
  150. int motion_x, int motion_y);
  151. int (*dct_quantize)(struct MPVEncContext *s, int16_t *block/*align 16*/, int n, int qscale, int *overflow);
  152. me_cmp_func ildct_cmp[2]; ///< 0 = intra, 1 = non-intra
  153. me_cmp_func n_sse_cmp[2]; ///< either SSE or NSSE cmp func
  154. me_cmp_func sad_cmp[2];
  155. me_cmp_func sse_cmp[2];
  156. int (*sum_abs_dctelem)(const int16_t *block);
  157. int intra_penalty;
  158. DECLARE_ALIGNED_32(int16_t, blocks)[2][12][64]; // for HQ mode we need to keep the best block
  159. } MPVEncContext;
  160. typedef struct MPVMainEncContext {
  161. MPVEncContext s; ///< The main slicecontext
  162. int intra_only; ///< if true, only intra pictures are generated
  163. int gop_size;
  164. int max_b_frames; ///< max number of B-frames
  165. int picture_in_gop_number; ///< 0-> first pic in gop, ...
  166. int input_picture_number; ///< used to set pic->display_picture_number
  167. int coded_picture_number; ///< used to set pic->coded_picture_number
  168. MPVPicture *input_picture[MPVENC_MAX_B_FRAMES + 1]; ///< next pictures in display order
  169. MPVPicture *reordered_input_picture[MPVENC_MAX_B_FRAMES + 1]; ///< next pictures in coded order
  170. int64_t user_specified_pts; ///< last non-zero pts from user-supplied AVFrame
  171. /**
  172. * pts difference between the first and second input frame, used for
  173. * calculating dts of the first frame when there's a delay */
  174. int64_t dts_delta;
  175. /**
  176. * reordered pts to be used as dts for the next output frame when there's
  177. * a delay */
  178. int64_t reordered_pts;
  179. /// temporary frames used by b_frame_strategy = 2
  180. AVFrame *tmp_frames[MPVENC_MAX_B_FRAMES + 2];
  181. int b_frame_strategy;
  182. int b_sensitivity;
  183. int brd_scale;
  184. int scenechange_threshold;
  185. int noise_reduction;
  186. float border_masking;
  187. int lmin, lmax;
  188. int vbv_ignore_qmax;
  189. /* MPEG-1/2 specific */
  190. int vbv_delay_pos; ///< offset of vbv_delay in the bitstream
  191. const uint8_t *fcode_tab; ///< smallest fcode needed for each MV
  192. /* frame skip options */
  193. int frame_skip_threshold;
  194. int frame_skip_factor;
  195. int frame_skip_exp;
  196. int frame_skip_cmp;
  197. me_cmp_func frame_skip_cmp_fn;
  198. int (*encode_picture_header)(struct MPVMainEncContext *m);
  199. /* bit rate control */
  200. int64_t bit_rate;
  201. int64_t total_bits;
  202. int frame_bits; ///< bits used for the current frame
  203. int header_bits;
  204. int stuffing_bits; ///< bits used for stuffing
  205. int next_lambda; ///< next lambda used for retrying to encode a frame
  206. int fixed_qscale; ///< fixed qscale if non zero
  207. int last_lambda_for[5]; ///< last lambda for a specific pict type
  208. int last_pict_type; //FIXME removes
  209. int last_non_b_pict_type; ///< used for MPEG-4 gmc B-frames & ratecontrol
  210. RateControlContext rc_context; ///< contains stuff only accessed in ratecontrol.c
  211. int me_penalty_compensation;
  212. int me_pre; ///< prepass for motion estimation
  213. int64_t mb_var_sum; ///< sum of MB variance for current frame
  214. int64_t mc_mb_var_sum; ///< motion compensated MB variance for current frame
  215. char *me_map_base; ///< backs MotionEstContext.(map|score_map)
  216. char *dct_error_sum_base; ///< backs dct_error_sum
  217. int16_t (*mv_table_base)[2];
  218. } MPVMainEncContext;
  219. static inline const MPVMainEncContext *slice_to_mainenc(const MPVEncContext *s)
  220. {
  221. #ifdef NO_SLICE_THREADING_HERE
  222. av_assert2(s->c.slice_context_count <= 1 &&
  223. !(s->c.avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS));
  224. return (const MPVMainEncContext*)s;
  225. #else
  226. return s->parent;
  227. #endif
  228. }
  229. #define MAX_FCODE 7
  230. #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
  231. #define INPLACE_OFFSET 16
  232. /* MB types for encoding */
  233. #define CANDIDATE_MB_TYPE_INTRA (1 << 0)
  234. #define CANDIDATE_MB_TYPE_INTER (1 << 1)
  235. #define CANDIDATE_MB_TYPE_INTER4V (1 << 2)
  236. #define CANDIDATE_MB_TYPE_SKIPPED (1 << 3)
  237. #define CANDIDATE_MB_TYPE_DIRECT (1 << 4)
  238. #define CANDIDATE_MB_TYPE_FORWARD (1 << 5)
  239. #define CANDIDATE_MB_TYPE_BACKWARD (1 << 6)
  240. #define CANDIDATE_MB_TYPE_BIDIR (1 << 7)
  241. #define CANDIDATE_MB_TYPE_INTER_I (1 << 8)
  242. #define CANDIDATE_MB_TYPE_FORWARD_I (1 << 9)
  243. #define CANDIDATE_MB_TYPE_BACKWARD_I (1 << 10)
  244. #define CANDIDATE_MB_TYPE_BIDIR_I (1 << 11)
  245. #define CANDIDATE_MB_TYPE_DIRECT0 (1 << 12)
  246. /* mpegvideo_enc common options */
  247. #define FF_MPV_FLAG_SKIP_RD 0x0001
  248. #define FF_MPV_FLAG_STRICT_GOP 0x0002
  249. #define FF_MPV_FLAG_QP_RD 0x0004
  250. #define FF_MPV_FLAG_CBP_RD 0x0008
  251. #define FF_MPV_FLAG_NAQ 0x0010
  252. #define FF_MPV_FLAG_MV0 0x0020
  253. #define FF_MPV_OPT_CMP_FUNC \
  254. { "sad", "Sum of absolute differences, fast", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_SAD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  255. { "sse", "Sum of squared errors", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_SSE }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  256. { "satd", "Sum of absolute Hadamard transformed differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_SATD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  257. { "dct", "Sum of absolute DCT transformed differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_DCT }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  258. { "psnr", "Sum of squared quantization errors, low quality", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_PSNR }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  259. { "bit", "Number of bits needed for the block", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_BIT }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  260. { "rd", "Rate distortion optimal, slow", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_RD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  261. { "zero", "Zero", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_ZERO }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  262. { "vsad", "Sum of absolute vertical differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_VSAD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  263. { "vsse", "Sum of squared vertical differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_VSSE }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  264. { "nsse", "Noise preserving sum of squared differences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_NSSE }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  265. { "dct264", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_DCT264 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  266. { "dctmax", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_DCTMAX }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  267. { "chroma", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_CHROMA }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  268. { "msad", "Sum of absolute differences, median predicted", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_MEDIAN_SAD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }
  269. #define FF_MPV_OFFSET(x) offsetof(MPVEncContext, x)
  270. #define FF_MPV_MAIN_OFFSET(x) offsetof(MPVMainEncContext, x)
  271. #define FF_RC_OFFSET(x) offsetof(MPVMainEncContext, rc_context.x)
  272. #define FF_MPV_OPT_FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  273. #define FF_MPV_COMMON_OPTS \
  274. FF_MPV_OPT_CMP_FUNC, \
  275. { "mpv_flags", "Flags common for all mpegvideo-based encoders.", FF_MPV_OFFSET(mpv_flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "mpv_flags" },\
  276. { "skip_rd", "RD optimal MB level residual skipping", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_SKIP_RD }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "mpv_flags" },\
  277. { "strict_gop", "Strictly enforce gop size", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_STRICT_GOP }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "mpv_flags" },\
  278. { "qp_rd", "Use rate distortion optimization for qp selection", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_QP_RD }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "mpv_flags" },\
  279. { "cbp_rd", "use rate distortion optimization for CBP", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_CBP_RD }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "mpv_flags" },\
  280. { "naq", "normalize adaptive quantization", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_NAQ }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "mpv_flags" },\
  281. { "luma_elim_threshold", "single coefficient elimination threshold for luminance (negative values also consider dc coefficient)",\
  282. FF_MPV_OFFSET(luma_elim_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS },\
  283. { "chroma_elim_threshold", "single coefficient elimination threshold for chrominance (negative values also consider dc coefficient)",\
  284. FF_MPV_OFFSET(chroma_elim_threshold), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS },\
  285. { "quantizer_noise_shaping", NULL, FF_MPV_OFFSET(quantizer_noise_shaping), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FF_MPV_OPT_FLAGS },\
  286. { "error_rate", "Simulate errors in the bitstream to test error concealment.", \
  287. FF_MPV_OFFSET(error_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FF_MPV_OPT_FLAGS },\
  288. {"qsquish", "how to keep quantizer between qmin and qmax (0 = clip, 1 = use differentiable function)", \
  289. FF_RC_OFFSET(qsquish), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, 0, 99, FF_MPV_OPT_FLAGS}, \
  290. {"rc_qmod_amp", "experimental quantizer modulation", FF_RC_OFFSET(qmod_amp), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
  291. {"rc_qmod_freq", "experimental quantizer modulation", FF_RC_OFFSET(qmod_freq), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS}, \
  292. {"rc_eq", "Set rate control equation. When computing the expression, besides the standard functions " \
  293. "defined in the section 'Expression Evaluation', the following functions are available: " \
  294. "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv " \
  295. "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.", \
  296. FF_RC_OFFSET(rc_eq), AV_OPT_TYPE_STRING, .flags = FF_MPV_OPT_FLAGS }, \
  297. {"rc_init_cplx", "initial complexity for 1-pass encoding", FF_RC_OFFSET(initial_cplx), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
  298. {"rc_buf_aggressivity", "currently useless", FF_RC_OFFSET(buffer_aggressivity), AV_OPT_TYPE_FLOAT, {.dbl = 1.0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
  299. {"border_mask", "increase the quantizer for macroblocks close to borders", FF_MPV_MAIN_OFFSET(border_masking), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
  300. {"lmin", "minimum Lagrange factor (VBR)", FF_MPV_MAIN_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 = 2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
  301. {"lmax", "maximum Lagrange factor (VBR)", FF_MPV_MAIN_OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
  302. {"skip_threshold", "Frame skip threshold", FF_MPV_MAIN_OFFSET(frame_skip_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
  303. {"skip_factor", "Frame skip factor", FF_MPV_MAIN_OFFSET(frame_skip_factor), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
  304. {"skip_exp", "Frame skip exponent", FF_MPV_MAIN_OFFSET(frame_skip_exp), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
  305. {"skip_cmp", "Frame skip compare function", FF_MPV_MAIN_OFFSET(frame_skip_cmp), AV_OPT_TYPE_INT, {.i64 = FF_CMP_DCTMAX }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
  306. {"noise_reduction", "Noise reduction", FF_MPV_MAIN_OFFSET(noise_reduction), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
  307. {"ps", "RTP payload size in bytes", FF_MPV_OFFSET(rtp_payload_size), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
  308. #define FF_MPV_COMMON_BFRAME_OPTS \
  309. {"b_strategy", "Strategy to choose between I/P/B-frames", FF_MPV_MAIN_OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, FF_MPV_OPT_FLAGS }, \
  310. {"b_sensitivity", "Adjust sensitivity of b_frame_strategy 1", FF_MPV_MAIN_OFFSET(b_sensitivity), AV_OPT_TYPE_INT, {.i64 = 40 }, 1, INT_MAX, FF_MPV_OPT_FLAGS }, \
  311. {"brd_scale", "Downscale frames for dynamic B-frame decision", FF_MPV_MAIN_OFFSET(brd_scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 3, FF_MPV_OPT_FLAGS },
  312. #define FF_MPV_COMMON_MOTION_EST_OPTS \
  313. { "mv0", "always try a mb with mv=<0,0>", 0, AV_OPT_TYPE_CONST, { .i64 = FF_MPV_FLAG_MV0 }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "mpv_flags" },\
  314. {"motion_est", "motion estimation algorithm", FF_MPV_OFFSET(me.motion_est), AV_OPT_TYPE_INT, {.i64 = FF_ME_EPZS }, FF_ME_ZERO, FF_ME_XONE, FF_MPV_OPT_FLAGS, .unit = "motion_est" }, \
  315. { "zero", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_ZERO }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "motion_est" }, \
  316. { "epzs", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_EPZS }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "motion_est" }, \
  317. { "xone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_ME_XONE }, 0, 0, FF_MPV_OPT_FLAGS, .unit = "motion_est" }, \
  318. {"mepc", "Motion estimation bitrate penalty compensation (1.0 = 256)", FF_MPV_MAIN_OFFSET(me_penalty_compensation), AV_OPT_TYPE_INT, {.i64 = 256 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
  319. {"mepre", "pre motion estimation", FF_MPV_MAIN_OFFSET(me_pre), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
  320. {"intra_penalty", "Penalty for intra blocks in block decision", FF_MPV_OFFSET(intra_penalty), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX/2, FF_MPV_OPT_FLAGS }, \
  321. {"sc_threshold", "Scene change threshold", FF_MPV_MAIN_OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
  322. extern const AVClass ff_mpv_enc_class;
  323. int ff_mpv_encode_init(AVCodecContext *avctx);
  324. int ff_mpv_encode_end(AVCodecContext *avctx);
  325. int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  326. const AVFrame *frame, int *got_packet);
  327. int ff_mpv_reallocate_putbitbuffer(MPVEncContext *s, size_t threshold, size_t size_increase);
  328. void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix);
  329. void ff_dct_encode_init(MPVEncContext *s);
  330. void ff_dct_encode_init_x86(MPVEncContext *s);
  331. void ff_convert_matrix(MPVEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[2][64],
  332. const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra);
  333. void ff_block_permute(int16_t *block, const uint8_t *permutation,
  334. const uint8_t *scantable, int last);
  335. static inline int get_bits_diff(MPVEncContext *s)
  336. {
  337. const int bits = put_bits_count(&s->pb);
  338. const int last = s->last_bits;
  339. s->last_bits = bits;
  340. return bits - last;
  341. }
  342. #endif