vpx_encoder.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_VPX_ENCODER_H_
  11. #define VPX_VPX_ENCODER_H_
  12. /*!\defgroup encoder Encoder Algorithm Interface
  13. * \ingroup codec
  14. * This abstraction allows applications using this encoder to easily support
  15. * multiple video formats with minimal code duplication. This section describes
  16. * the interface common to all encoders.
  17. * @{
  18. */
  19. /*!\file
  20. * \brief Describes the encoder algorithm interface to applications.
  21. *
  22. * This file describes the interface between an application and a
  23. * video encoder algorithm.
  24. *
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include "./vpx_codec.h"
  30. /*! Temporal Scalability: Maximum length of the sequence defining frame
  31. * layer membership
  32. */
  33. #define VPX_TS_MAX_PERIODICITY 16
  34. /*! Temporal Scalability: Maximum number of coding layers */
  35. #define VPX_TS_MAX_LAYERS 5
  36. /*!\deprecated Use #VPX_TS_MAX_PERIODICITY instead. */
  37. #define MAX_PERIODICITY VPX_TS_MAX_PERIODICITY
  38. /*! Temporal+Spatial Scalability: Maximum number of coding layers */
  39. #define VPX_MAX_LAYERS 12 // 3 temporal + 4 spatial layers are allowed.
  40. /*!\deprecated Use #VPX_MAX_LAYERS instead. */
  41. #define MAX_LAYERS VPX_MAX_LAYERS // 3 temporal + 4 spatial layers allowed.
  42. /*! Spatial Scalability: Maximum number of coding layers */
  43. #define VPX_SS_MAX_LAYERS 5
  44. /*! Spatial Scalability: Default number of coding layers */
  45. #define VPX_SS_DEFAULT_LAYERS 1
  46. /*!\brief Current ABI version number
  47. *
  48. * \internal
  49. * If this file is altered in any way that changes the ABI, this value
  50. * must be bumped. Examples include, but are not limited to, changing
  51. * types, removing or reassigning enums, adding/removing/rearranging
  52. * fields to structures
  53. */
  54. #define VPX_ENCODER_ABI_VERSION (5 + VPX_CODEC_ABI_VERSION) /**<\hideinitializer*/
  55. /*! \brief Encoder capabilities bitfield
  56. *
  57. * Each encoder advertises the capabilities it supports as part of its
  58. * ::vpx_codec_iface_t interface structure. Capabilities are extra
  59. * interfaces or functionality, and are not required to be supported
  60. * by an encoder.
  61. *
  62. * The available flags are specified by VPX_CODEC_CAP_* defines.
  63. */
  64. #define VPX_CODEC_CAP_PSNR 0x10000 /**< Can issue PSNR packets */
  65. /*! Can output one partition at a time. Each partition is returned in its
  66. * own VPX_CODEC_CX_FRAME_PKT, with the FRAME_IS_FRAGMENT flag set for
  67. * every partition but the last. In this mode all frames are always
  68. * returned partition by partition.
  69. */
  70. #define VPX_CODEC_CAP_OUTPUT_PARTITION 0x20000
  71. /*! Can support input images at greater than 8 bitdepth.
  72. */
  73. #define VPX_CODEC_CAP_HIGHBITDEPTH 0x40000
  74. /*! \brief Initialization-time Feature Enabling
  75. *
  76. * Certain codec features must be known at initialization time, to allow
  77. * for proper memory allocation.
  78. *
  79. * The available flags are specified by VPX_CODEC_USE_* defines.
  80. */
  81. #define VPX_CODEC_USE_PSNR 0x10000 /**< Calculate PSNR on each frame */
  82. #define VPX_CODEC_USE_OUTPUT_PARTITION 0x20000 /**< Make the encoder output one
  83. partition at a time. */
  84. #define VPX_CODEC_USE_HIGHBITDEPTH 0x40000 /**< Use high bitdepth */
  85. /*!\brief Generic fixed size buffer structure
  86. *
  87. * This structure is able to hold a reference to any fixed size buffer.
  88. */
  89. typedef struct vpx_fixed_buf {
  90. void *buf; /**< Pointer to the data */
  91. size_t sz; /**< Length of the buffer, in chars */
  92. } vpx_fixed_buf_t; /**< alias for struct vpx_fixed_buf */
  93. /*!\brief Time Stamp Type
  94. *
  95. * An integer, which when multiplied by the stream's time base, provides
  96. * the absolute time of a sample.
  97. */
  98. typedef int64_t vpx_codec_pts_t;
  99. /*!\brief Compressed Frame Flags
  100. *
  101. * This type represents a bitfield containing information about a compressed
  102. * frame that may be useful to an application. The most significant 16 bits
  103. * can be used by an algorithm to provide additional detail, for example to
  104. * support frame types that are codec specific (MPEG-1 D-frames for example)
  105. */
  106. typedef uint32_t vpx_codec_frame_flags_t;
  107. #define VPX_FRAME_IS_KEY 0x1 /**< frame is the start of a GOP */
  108. #define VPX_FRAME_IS_DROPPABLE 0x2 /**< frame can be dropped without affecting
  109. the stream (no future frame depends on
  110. this one) */
  111. #define VPX_FRAME_IS_INVISIBLE 0x4 /**< frame should be decoded but will not
  112. be shown */
  113. #define VPX_FRAME_IS_FRAGMENT 0x8 /**< this is a fragment of the encoded
  114. frame */
  115. /*!\brief Error Resilient flags
  116. *
  117. * These flags define which error resilient features to enable in the
  118. * encoder. The flags are specified through the
  119. * vpx_codec_enc_cfg::g_error_resilient variable.
  120. */
  121. typedef uint32_t vpx_codec_er_flags_t;
  122. #define VPX_ERROR_RESILIENT_DEFAULT 0x1 /**< Improve resiliency against
  123. losses of whole frames */
  124. #define VPX_ERROR_RESILIENT_PARTITIONS 0x2 /**< The frame partitions are
  125. independently decodable by the
  126. bool decoder, meaning that
  127. partitions can be decoded even
  128. though earlier partitions have
  129. been lost. Note that intra
  130. prediction is still done over
  131. the partition boundary. */
  132. /*!\brief Encoder output packet variants
  133. *
  134. * This enumeration lists the different kinds of data packets that can be
  135. * returned by calls to vpx_codec_get_cx_data(). Algorithms \ref MAY
  136. * extend this list to provide additional functionality.
  137. */
  138. enum vpx_codec_cx_pkt_kind {
  139. VPX_CODEC_CX_FRAME_PKT, /**< Compressed video frame */
  140. VPX_CODEC_STATS_PKT, /**< Two-pass statistics for this frame */
  141. VPX_CODEC_FPMB_STATS_PKT, /**< first pass mb statistics for this frame */
  142. VPX_CODEC_PSNR_PKT, /**< PSNR statistics for this frame */
  143. // Spatial SVC is still experimental and may be removed before the next ABI
  144. // bump.
  145. #if VPX_ENCODER_ABI_VERSION > (5 + VPX_CODEC_ABI_VERSION)
  146. VPX_CODEC_SPATIAL_SVC_LAYER_SIZES, /**< Sizes for each layer in this frame*/
  147. VPX_CODEC_SPATIAL_SVC_LAYER_PSNR, /**< PSNR for each layer in this frame*/
  148. #endif
  149. VPX_CODEC_CUSTOM_PKT = 256 /**< Algorithm extensions */
  150. };
  151. /*!\brief Encoder output packet
  152. *
  153. * This structure contains the different kinds of output data the encoder
  154. * may produce while compressing a frame.
  155. */
  156. typedef struct vpx_codec_cx_pkt {
  157. enum vpx_codec_cx_pkt_kind kind; /**< packet variant */
  158. union {
  159. struct {
  160. void *buf; /**< compressed data buffer */
  161. size_t sz; /**< length of compressed data */
  162. vpx_codec_pts_t pts; /**< time stamp to show frame
  163. (in timebase units) */
  164. unsigned long duration; /**< duration to show frame
  165. (in timebase units) */
  166. vpx_codec_frame_flags_t flags; /**< flags for this frame */
  167. int partition_id; /**< the partition id
  168. defines the decoding order
  169. of the partitions. Only
  170. applicable when "output partition"
  171. mode is enabled. First partition
  172. has id 0.*/
  173. } frame; /**< data for compressed frame packet */
  174. vpx_fixed_buf_t twopass_stats; /**< data for two-pass packet */
  175. vpx_fixed_buf_t firstpass_mb_stats; /**< first pass mb packet */
  176. struct vpx_psnr_pkt {
  177. unsigned int samples[4]; /**< Number of samples, total/y/u/v */
  178. uint64_t sse[4]; /**< sum squared error, total/y/u/v */
  179. double psnr[4]; /**< PSNR, total/y/u/v */
  180. } psnr; /**< data for PSNR packet */
  181. vpx_fixed_buf_t raw; /**< data for arbitrary packets */
  182. // Spatial SVC is still experimental and may be removed before the next
  183. // ABI bump.
  184. #if VPX_ENCODER_ABI_VERSION > (5 + VPX_CODEC_ABI_VERSION)
  185. size_t layer_sizes[VPX_SS_MAX_LAYERS];
  186. struct vpx_psnr_pkt layer_psnr[VPX_SS_MAX_LAYERS];
  187. #endif
  188. /* This packet size is fixed to allow codecs to extend this
  189. * interface without having to manage storage for raw packets,
  190. * i.e., if it's smaller than 128 bytes, you can store in the
  191. * packet list directly.
  192. */
  193. char pad[128 - sizeof(enum vpx_codec_cx_pkt_kind)]; /**< fixed sz */
  194. } data; /**< packet data */
  195. } vpx_codec_cx_pkt_t; /**< alias for struct vpx_codec_cx_pkt */
  196. /*!\brief Encoder return output buffer callback
  197. *
  198. * This callback function, when registered, returns with packets when each
  199. * spatial layer is encoded.
  200. */
  201. // putting the definitions here for now. (agrange: find if there
  202. // is a better place for this)
  203. typedef void (* vpx_codec_enc_output_cx_pkt_cb_fn_t)(vpx_codec_cx_pkt_t *pkt,
  204. void *user_data);
  205. /*!\brief Callback function pointer / user data pair storage */
  206. typedef struct vpx_codec_enc_output_cx_cb_pair {
  207. vpx_codec_enc_output_cx_pkt_cb_fn_t output_cx_pkt; /**< Callback function */
  208. void *user_priv; /**< Pointer to private data */
  209. } vpx_codec_priv_output_cx_pkt_cb_pair_t;
  210. /*!\brief Rational Number
  211. *
  212. * This structure holds a fractional value.
  213. */
  214. typedef struct vpx_rational {
  215. int num; /**< fraction numerator */
  216. int den; /**< fraction denominator */
  217. } vpx_rational_t; /**< alias for struct vpx_rational */
  218. /*!\brief Multi-pass Encoding Pass */
  219. enum vpx_enc_pass {
  220. VPX_RC_ONE_PASS, /**< Single pass mode */
  221. VPX_RC_FIRST_PASS, /**< First pass of multi-pass mode */
  222. VPX_RC_LAST_PASS /**< Final pass of multi-pass mode */
  223. };
  224. /*!\brief Rate control mode */
  225. enum vpx_rc_mode {
  226. VPX_VBR, /**< Variable Bit Rate (VBR) mode */
  227. VPX_CBR, /**< Constant Bit Rate (CBR) mode */
  228. VPX_CQ, /**< Constrained Quality (CQ) mode */
  229. VPX_Q, /**< Constant Quality (Q) mode */
  230. };
  231. /*!\brief Keyframe placement mode.
  232. *
  233. * This enumeration determines whether keyframes are placed automatically by
  234. * the encoder or whether this behavior is disabled. Older releases of this
  235. * SDK were implemented such that VPX_KF_FIXED meant keyframes were disabled.
  236. * This name is confusing for this behavior, so the new symbols to be used
  237. * are VPX_KF_AUTO and VPX_KF_DISABLED.
  238. */
  239. enum vpx_kf_mode {
  240. VPX_KF_FIXED, /**< deprecated, implies VPX_KF_DISABLED */
  241. VPX_KF_AUTO, /**< Encoder determines optimal placement automatically */
  242. VPX_KF_DISABLED = 0 /**< Encoder does not place keyframes. */
  243. };
  244. /*!\brief Encoded Frame Flags
  245. *
  246. * This type indicates a bitfield to be passed to vpx_codec_encode(), defining
  247. * per-frame boolean values. By convention, bits common to all codecs will be
  248. * named VPX_EFLAG_*, and bits specific to an algorithm will be named
  249. * /algo/_eflag_*. The lower order 16 bits are reserved for common use.
  250. */
  251. typedef long vpx_enc_frame_flags_t;
  252. #define VPX_EFLAG_FORCE_KF (1<<0) /**< Force this frame to be a keyframe */
  253. /*!\brief Encoder configuration structure
  254. *
  255. * This structure contains the encoder settings that have common representations
  256. * across all codecs. This doesn't imply that all codecs support all features,
  257. * however.
  258. */
  259. typedef struct vpx_codec_enc_cfg {
  260. /*
  261. * generic settings (g)
  262. */
  263. /*!\brief Algorithm specific "usage" value
  264. *
  265. * Algorithms may define multiple values for usage, which may convey the
  266. * intent of how the application intends to use the stream. If this value
  267. * is non-zero, consult the documentation for the codec to determine its
  268. * meaning.
  269. */
  270. unsigned int g_usage;
  271. /*!\brief Maximum number of threads to use
  272. *
  273. * For multi-threaded implementations, use no more than this number of
  274. * threads. The codec may use fewer threads than allowed. The value
  275. * 0 is equivalent to the value 1.
  276. */
  277. unsigned int g_threads;
  278. /*!\brief Bitstream profile to use
  279. *
  280. * Some codecs support a notion of multiple bitstream profiles. Typically
  281. * this maps to a set of features that are turned on or off. Often the
  282. * profile to use is determined by the features of the intended decoder.
  283. * Consult the documentation for the codec to determine the valid values
  284. * for this parameter, or set to zero for a sane default.
  285. */
  286. unsigned int g_profile; /**< profile of bitstream to use */
  287. /*!\brief Width of the frame
  288. *
  289. * This value identifies the presentation resolution of the frame,
  290. * in pixels. Note that the frames passed as input to the encoder must
  291. * have this resolution. Frames will be presented by the decoder in this
  292. * resolution, independent of any spatial resampling the encoder may do.
  293. */
  294. unsigned int g_w;
  295. /*!\brief Height of the frame
  296. *
  297. * This value identifies the presentation resolution of the frame,
  298. * in pixels. Note that the frames passed as input to the encoder must
  299. * have this resolution. Frames will be presented by the decoder in this
  300. * resolution, independent of any spatial resampling the encoder may do.
  301. */
  302. unsigned int g_h;
  303. /*!\brief Bit-depth of the codec
  304. *
  305. * This value identifies the bit_depth of the codec,
  306. * Only certain bit-depths are supported as identified in the
  307. * vpx_bit_depth_t enum.
  308. */
  309. vpx_bit_depth_t g_bit_depth;
  310. /*!\brief Bit-depth of the input frames
  311. *
  312. * This value identifies the bit_depth of the input frames in bits.
  313. * Note that the frames passed as input to the encoder must have
  314. * this bit-depth.
  315. */
  316. unsigned int g_input_bit_depth;
  317. /*!\brief Stream timebase units
  318. *
  319. * Indicates the smallest interval of time, in seconds, used by the stream.
  320. * For fixed frame rate material, or variable frame rate material where
  321. * frames are timed at a multiple of a given clock (ex: video capture),
  322. * the \ref RECOMMENDED method is to set the timebase to the reciprocal
  323. * of the frame rate (ex: 1001/30000 for 29.970 Hz NTSC). This allows the
  324. * pts to correspond to the frame number, which can be handy. For
  325. * re-encoding video from containers with absolute time timestamps, the
  326. * \ref RECOMMENDED method is to set the timebase to that of the parent
  327. * container or multimedia framework (ex: 1/1000 for ms, as in FLV).
  328. */
  329. struct vpx_rational g_timebase;
  330. /*!\brief Enable error resilient modes.
  331. *
  332. * The error resilient bitfield indicates to the encoder which features
  333. * it should enable to take measures for streaming over lossy or noisy
  334. * links.
  335. */
  336. vpx_codec_er_flags_t g_error_resilient;
  337. /*!\brief Multi-pass Encoding Mode
  338. *
  339. * This value should be set to the current phase for multi-pass encoding.
  340. * For single pass, set to #VPX_RC_ONE_PASS.
  341. */
  342. enum vpx_enc_pass g_pass;
  343. /*!\brief Allow lagged encoding
  344. *
  345. * If set, this value allows the encoder to consume a number of input
  346. * frames before producing output frames. This allows the encoder to
  347. * base decisions for the current frame on future frames. This does
  348. * increase the latency of the encoding pipeline, so it is not appropriate
  349. * in all situations (ex: realtime encoding).
  350. *
  351. * Note that this is a maximum value -- the encoder may produce frames
  352. * sooner than the given limit. Set this value to 0 to disable this
  353. * feature.
  354. */
  355. unsigned int g_lag_in_frames;
  356. /*
  357. * rate control settings (rc)
  358. */
  359. /*!\brief Temporal resampling configuration, if supported by the codec.
  360. *
  361. * Temporal resampling allows the codec to "drop" frames as a strategy to
  362. * meet its target data rate. This can cause temporal discontinuities in
  363. * the encoded video, which may appear as stuttering during playback. This
  364. * trade-off is often acceptable, but for many applications is not. It can
  365. * be disabled in these cases.
  366. *
  367. * Note that not all codecs support this feature. All vpx VPx codecs do.
  368. * For other codecs, consult the documentation for that algorithm.
  369. *
  370. * This threshold is described as a percentage of the target data buffer.
  371. * When the data buffer falls below this percentage of fullness, a
  372. * dropped frame is indicated. Set the threshold to zero (0) to disable
  373. * this feature.
  374. */
  375. unsigned int rc_dropframe_thresh;
  376. /*!\brief Enable/disable spatial resampling, if supported by the codec.
  377. *
  378. * Spatial resampling allows the codec to compress a lower resolution
  379. * version of the frame, which is then upscaled by the encoder to the
  380. * correct presentation resolution. This increases visual quality at
  381. * low data rates, at the expense of CPU time on the encoder/decoder.
  382. */
  383. unsigned int rc_resize_allowed;
  384. /*!\brief Internal coded frame width.
  385. *
  386. * If spatial resampling is enabled this specifies the width of the
  387. * encoded frame.
  388. */
  389. unsigned int rc_scaled_width;
  390. /*!\brief Internal coded frame height.
  391. *
  392. * If spatial resampling is enabled this specifies the height of the
  393. * encoded frame.
  394. */
  395. unsigned int rc_scaled_height;
  396. /*!\brief Spatial resampling up watermark.
  397. *
  398. * This threshold is described as a percentage of the target data buffer.
  399. * When the data buffer rises above this percentage of fullness, the
  400. * encoder will step up to a higher resolution version of the frame.
  401. */
  402. unsigned int rc_resize_up_thresh;
  403. /*!\brief Spatial resampling down watermark.
  404. *
  405. * This threshold is described as a percentage of the target data buffer.
  406. * When the data buffer falls below this percentage of fullness, the
  407. * encoder will step down to a lower resolution version of the frame.
  408. */
  409. unsigned int rc_resize_down_thresh;
  410. /*!\brief Rate control algorithm to use.
  411. *
  412. * Indicates whether the end usage of this stream is to be streamed over
  413. * a bandwidth constrained link, indicating that Constant Bit Rate (CBR)
  414. * mode should be used, or whether it will be played back on a high
  415. * bandwidth link, as from a local disk, where higher variations in
  416. * bitrate are acceptable.
  417. */
  418. enum vpx_rc_mode rc_end_usage;
  419. /*!\brief Two-pass stats buffer.
  420. *
  421. * A buffer containing all of the stats packets produced in the first
  422. * pass, concatenated.
  423. */
  424. vpx_fixed_buf_t rc_twopass_stats_in;
  425. /*!\brief first pass mb stats buffer.
  426. *
  427. * A buffer containing all of the first pass mb stats packets produced
  428. * in the first pass, concatenated.
  429. */
  430. vpx_fixed_buf_t rc_firstpass_mb_stats_in;
  431. /*!\brief Target data rate
  432. *
  433. * Target bandwidth to use for this stream, in kilobits per second.
  434. */
  435. unsigned int rc_target_bitrate;
  436. /*
  437. * quantizer settings
  438. */
  439. /*!\brief Minimum (Best Quality) Quantizer
  440. *
  441. * The quantizer is the most direct control over the quality of the
  442. * encoded image. The range of valid values for the quantizer is codec
  443. * specific. Consult the documentation for the codec to determine the
  444. * values to use. To determine the range programmatically, call
  445. * vpx_codec_enc_config_default() with a usage value of 0.
  446. */
  447. unsigned int rc_min_quantizer;
  448. /*!\brief Maximum (Worst Quality) Quantizer
  449. *
  450. * The quantizer is the most direct control over the quality of the
  451. * encoded image. The range of valid values for the quantizer is codec
  452. * specific. Consult the documentation for the codec to determine the
  453. * values to use. To determine the range programmatically, call
  454. * vpx_codec_enc_config_default() with a usage value of 0.
  455. */
  456. unsigned int rc_max_quantizer;
  457. /*
  458. * bitrate tolerance
  459. */
  460. /*!\brief Rate control adaptation undershoot control
  461. *
  462. * This value, expressed as a percentage of the target bitrate,
  463. * controls the maximum allowed adaptation speed of the codec.
  464. * This factor controls the maximum amount of bits that can
  465. * be subtracted from the target bitrate in order to compensate
  466. * for prior overshoot.
  467. *
  468. * Valid values in the range 0-1000.
  469. */
  470. unsigned int rc_undershoot_pct;
  471. /*!\brief Rate control adaptation overshoot control
  472. *
  473. * This value, expressed as a percentage of the target bitrate,
  474. * controls the maximum allowed adaptation speed of the codec.
  475. * This factor controls the maximum amount of bits that can
  476. * be added to the target bitrate in order to compensate for
  477. * prior undershoot.
  478. *
  479. * Valid values in the range 0-1000.
  480. */
  481. unsigned int rc_overshoot_pct;
  482. /*
  483. * decoder buffer model parameters
  484. */
  485. /*!\brief Decoder Buffer Size
  486. *
  487. * This value indicates the amount of data that may be buffered by the
  488. * decoding application. Note that this value is expressed in units of
  489. * time (milliseconds). For example, a value of 5000 indicates that the
  490. * client will buffer (at least) 5000ms worth of encoded data. Use the
  491. * target bitrate (#rc_target_bitrate) to convert to bits/bytes, if
  492. * necessary.
  493. */
  494. unsigned int rc_buf_sz;
  495. /*!\brief Decoder Buffer Initial Size
  496. *
  497. * This value indicates the amount of data that will be buffered by the
  498. * decoding application prior to beginning playback. This value is
  499. * expressed in units of time (milliseconds). Use the target bitrate
  500. * (#rc_target_bitrate) to convert to bits/bytes, if necessary.
  501. */
  502. unsigned int rc_buf_initial_sz;
  503. /*!\brief Decoder Buffer Optimal Size
  504. *
  505. * This value indicates the amount of data that the encoder should try
  506. * to maintain in the decoder's buffer. This value is expressed in units
  507. * of time (milliseconds). Use the target bitrate (#rc_target_bitrate)
  508. * to convert to bits/bytes, if necessary.
  509. */
  510. unsigned int rc_buf_optimal_sz;
  511. /*
  512. * 2 pass rate control parameters
  513. */
  514. /*!\brief Two-pass mode CBR/VBR bias
  515. *
  516. * Bias, expressed on a scale of 0 to 100, for determining target size
  517. * for the current frame. The value 0 indicates the optimal CBR mode
  518. * value should be used. The value 100 indicates the optimal VBR mode
  519. * value should be used. Values in between indicate which way the
  520. * encoder should "lean."
  521. */
  522. unsigned int rc_2pass_vbr_bias_pct; /**< RC mode bias between CBR and VBR(0-100: 0->CBR, 100->VBR) */
  523. /*!\brief Two-pass mode per-GOP minimum bitrate
  524. *
  525. * This value, expressed as a percentage of the target bitrate, indicates
  526. * the minimum bitrate to be used for a single GOP (aka "section")
  527. */
  528. unsigned int rc_2pass_vbr_minsection_pct;
  529. /*!\brief Two-pass mode per-GOP maximum bitrate
  530. *
  531. * This value, expressed as a percentage of the target bitrate, indicates
  532. * the maximum bitrate to be used for a single GOP (aka "section")
  533. */
  534. unsigned int rc_2pass_vbr_maxsection_pct;
  535. /*
  536. * keyframing settings (kf)
  537. */
  538. /*!\brief Keyframe placement mode
  539. *
  540. * This value indicates whether the encoder should place keyframes at a
  541. * fixed interval, or determine the optimal placement automatically
  542. * (as governed by the #kf_min_dist and #kf_max_dist parameters)
  543. */
  544. enum vpx_kf_mode kf_mode;
  545. /*!\brief Keyframe minimum interval
  546. *
  547. * This value, expressed as a number of frames, prevents the encoder from
  548. * placing a keyframe nearer than kf_min_dist to the previous keyframe. At
  549. * least kf_min_dist frames non-keyframes will be coded before the next
  550. * keyframe. Set kf_min_dist equal to kf_max_dist for a fixed interval.
  551. */
  552. unsigned int kf_min_dist;
  553. /*!\brief Keyframe maximum interval
  554. *
  555. * This value, expressed as a number of frames, forces the encoder to code
  556. * a keyframe if one has not been coded in the last kf_max_dist frames.
  557. * A value of 0 implies all frames will be keyframes. Set kf_min_dist
  558. * equal to kf_max_dist for a fixed interval.
  559. */
  560. unsigned int kf_max_dist;
  561. /*
  562. * Spatial scalability settings (ss)
  563. */
  564. /*!\brief Number of spatial coding layers.
  565. *
  566. * This value specifies the number of spatial coding layers to be used.
  567. */
  568. unsigned int ss_number_layers;
  569. /*!\brief Enable auto alt reference flags for each spatial layer.
  570. *
  571. * These values specify if auto alt reference frame is enabled for each
  572. * spatial layer.
  573. */
  574. int ss_enable_auto_alt_ref[VPX_SS_MAX_LAYERS];
  575. /*!\brief Target bitrate for each spatial layer.
  576. *
  577. * These values specify the target coding bitrate to be used for each
  578. * spatial layer.
  579. */
  580. unsigned int ss_target_bitrate[VPX_SS_MAX_LAYERS];
  581. /*!\brief Number of temporal coding layers.
  582. *
  583. * This value specifies the number of temporal layers to be used.
  584. */
  585. unsigned int ts_number_layers;
  586. /*!\brief Target bitrate for each temporal layer.
  587. *
  588. * These values specify the target coding bitrate to be used for each
  589. * temporal layer.
  590. */
  591. unsigned int ts_target_bitrate[VPX_TS_MAX_LAYERS];
  592. /*!\brief Frame rate decimation factor for each temporal layer.
  593. *
  594. * These values specify the frame rate decimation factors to apply
  595. * to each temporal layer.
  596. */
  597. unsigned int ts_rate_decimator[VPX_TS_MAX_LAYERS];
  598. /*!\brief Length of the sequence defining frame temporal layer membership.
  599. *
  600. * This value specifies the length of the sequence that defines the
  601. * membership of frames to temporal layers. For example, if the
  602. * ts_periodicity = 8, then the frames are assigned to coding layers with a
  603. * repeated sequence of length 8.
  604. */
  605. unsigned int ts_periodicity;
  606. /*!\brief Template defining the membership of frames to temporal layers.
  607. *
  608. * This array defines the membership of frames to temporal coding layers.
  609. * For a 2-layer encoding that assigns even numbered frames to one temporal
  610. * layer (0) and odd numbered frames to a second temporal layer (1) with
  611. * ts_periodicity=8, then ts_layer_id = (0,1,0,1,0,1,0,1).
  612. */
  613. unsigned int ts_layer_id[VPX_TS_MAX_PERIODICITY];
  614. /*!\brief Target bitrate for each spatial/temporal layer.
  615. *
  616. * These values specify the target coding bitrate to be used for each
  617. * spatial/temporal layer.
  618. *
  619. */
  620. unsigned int layer_target_bitrate[VPX_MAX_LAYERS];
  621. /*!\brief Temporal layering mode indicating which temporal layering scheme to use.
  622. *
  623. * The value (refer to VP9E_TEMPORAL_LAYERING_MODE) specifies the
  624. * temporal layering mode to use.
  625. *
  626. */
  627. int temporal_layering_mode;
  628. } vpx_codec_enc_cfg_t; /**< alias for struct vpx_codec_enc_cfg */
  629. /*!\brief vp9 svc extra configure parameters
  630. *
  631. * This defines max/min quantizers and scale factors for each layer
  632. *
  633. */
  634. typedef struct vpx_svc_parameters {
  635. int max_quantizers[VPX_MAX_LAYERS]; /**< Max Q for each layer */
  636. int min_quantizers[VPX_MAX_LAYERS]; /**< Min Q for each layer */
  637. int scaling_factor_num[VPX_MAX_LAYERS]; /**< Scaling factor-numerator */
  638. int scaling_factor_den[VPX_MAX_LAYERS]; /**< Scaling factor-denominator */
  639. int temporal_layering_mode; /**< Temporal layering mode */
  640. } vpx_svc_extra_cfg_t;
  641. /*!\brief Initialize an encoder instance
  642. *
  643. * Initializes a encoder context using the given interface. Applications
  644. * should call the vpx_codec_enc_init convenience macro instead of this
  645. * function directly, to ensure that the ABI version number parameter
  646. * is properly initialized.
  647. *
  648. * If the library was configured with --disable-multithread, this call
  649. * is not thread safe and should be guarded with a lock if being used
  650. * in a multithreaded context.
  651. *
  652. * \param[in] ctx Pointer to this instance's context.
  653. * \param[in] iface Pointer to the algorithm interface to use.
  654. * \param[in] cfg Configuration to use, if known. May be NULL.
  655. * \param[in] flags Bitfield of VPX_CODEC_USE_* flags
  656. * \param[in] ver ABI version number. Must be set to
  657. * VPX_ENCODER_ABI_VERSION
  658. * \retval #VPX_CODEC_OK
  659. * The decoder algorithm initialized.
  660. * \retval #VPX_CODEC_MEM_ERROR
  661. * Memory allocation failed.
  662. */
  663. vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t *ctx,
  664. vpx_codec_iface_t *iface,
  665. const vpx_codec_enc_cfg_t *cfg,
  666. vpx_codec_flags_t flags,
  667. int ver);
  668. /*!\brief Convenience macro for vpx_codec_enc_init_ver()
  669. *
  670. * Ensures the ABI version parameter is properly set.
  671. */
  672. #define vpx_codec_enc_init(ctx, iface, cfg, flags) \
  673. vpx_codec_enc_init_ver(ctx, iface, cfg, flags, VPX_ENCODER_ABI_VERSION)
  674. /*!\brief Initialize multi-encoder instance
  675. *
  676. * Initializes multi-encoder context using the given interface.
  677. * Applications should call the vpx_codec_enc_init_multi convenience macro
  678. * instead of this function directly, to ensure that the ABI version number
  679. * parameter is properly initialized.
  680. *
  681. * \param[in] ctx Pointer to this instance's context.
  682. * \param[in] iface Pointer to the algorithm interface to use.
  683. * \param[in] cfg Configuration to use, if known. May be NULL.
  684. * \param[in] num_enc Total number of encoders.
  685. * \param[in] flags Bitfield of VPX_CODEC_USE_* flags
  686. * \param[in] dsf Pointer to down-sampling factors.
  687. * \param[in] ver ABI version number. Must be set to
  688. * VPX_ENCODER_ABI_VERSION
  689. * \retval #VPX_CODEC_OK
  690. * The decoder algorithm initialized.
  691. * \retval #VPX_CODEC_MEM_ERROR
  692. * Memory allocation failed.
  693. */
  694. vpx_codec_err_t vpx_codec_enc_init_multi_ver(vpx_codec_ctx_t *ctx,
  695. vpx_codec_iface_t *iface,
  696. vpx_codec_enc_cfg_t *cfg,
  697. int num_enc,
  698. vpx_codec_flags_t flags,
  699. vpx_rational_t *dsf,
  700. int ver);
  701. /*!\brief Convenience macro for vpx_codec_enc_init_multi_ver()
  702. *
  703. * Ensures the ABI version parameter is properly set.
  704. */
  705. #define vpx_codec_enc_init_multi(ctx, iface, cfg, num_enc, flags, dsf) \
  706. vpx_codec_enc_init_multi_ver(ctx, iface, cfg, num_enc, flags, dsf, \
  707. VPX_ENCODER_ABI_VERSION)
  708. /*!\brief Get a default configuration
  709. *
  710. * Initializes a encoder configuration structure with default values. Supports
  711. * the notion of "usages" so that an algorithm may offer different default
  712. * settings depending on the user's intended goal. This function \ref SHOULD
  713. * be called by all applications to initialize the configuration structure
  714. * before specializing the configuration with application specific values.
  715. *
  716. * \param[in] iface Pointer to the algorithm interface to use.
  717. * \param[out] cfg Configuration buffer to populate.
  718. * \param[in] reserved Must set to 0 for VP8 and VP9.
  719. *
  720. * \retval #VPX_CODEC_OK
  721. * The configuration was populated.
  722. * \retval #VPX_CODEC_INCAPABLE
  723. * Interface is not an encoder interface.
  724. * \retval #VPX_CODEC_INVALID_PARAM
  725. * A parameter was NULL, or the usage value was not recognized.
  726. */
  727. vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface,
  728. vpx_codec_enc_cfg_t *cfg,
  729. unsigned int reserved);
  730. /*!\brief Set or change configuration
  731. *
  732. * Reconfigures an encoder instance according to the given configuration.
  733. *
  734. * \param[in] ctx Pointer to this instance's context
  735. * \param[in] cfg Configuration buffer to use
  736. *
  737. * \retval #VPX_CODEC_OK
  738. * The configuration was populated.
  739. * \retval #VPX_CODEC_INCAPABLE
  740. * Interface is not an encoder interface.
  741. * \retval #VPX_CODEC_INVALID_PARAM
  742. * A parameter was NULL, or the usage value was not recognized.
  743. */
  744. vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx,
  745. const vpx_codec_enc_cfg_t *cfg);
  746. /*!\brief Get global stream headers
  747. *
  748. * Retrieves a stream level global header packet, if supported by the codec.
  749. *
  750. * \param[in] ctx Pointer to this instance's context
  751. *
  752. * \retval NULL
  753. * Encoder does not support global header
  754. * \retval Non-NULL
  755. * Pointer to buffer containing global header packet
  756. */
  757. vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx);
  758. #define VPX_DL_REALTIME (1) /**< deadline parameter analogous to
  759. * VPx REALTIME mode. */
  760. #define VPX_DL_GOOD_QUALITY (1000000) /**< deadline parameter analogous to
  761. * VPx GOOD QUALITY mode. */
  762. #define VPX_DL_BEST_QUALITY (0) /**< deadline parameter analogous to
  763. * VPx BEST QUALITY mode. */
  764. /*!\brief Encode a frame
  765. *
  766. * Encodes a video frame at the given "presentation time." The presentation
  767. * time stamp (PTS) \ref MUST be strictly increasing.
  768. *
  769. * The encoder supports the notion of a soft real-time deadline. Given a
  770. * non-zero value to the deadline parameter, the encoder will make a "best
  771. * effort" guarantee to return before the given time slice expires. It is
  772. * implicit that limiting the available time to encode will degrade the
  773. * output quality. The encoder can be given an unlimited time to produce the
  774. * best possible frame by specifying a deadline of '0'. This deadline
  775. * supercedes the VPx notion of "best quality, good quality, realtime".
  776. * Applications that wish to map these former settings to the new deadline
  777. * based system can use the symbols #VPX_DL_REALTIME, #VPX_DL_GOOD_QUALITY,
  778. * and #VPX_DL_BEST_QUALITY.
  779. *
  780. * When the last frame has been passed to the encoder, this function should
  781. * continue to be called, with the img parameter set to NULL. This will
  782. * signal the end-of-stream condition to the encoder and allow it to encode
  783. * any held buffers. Encoding is complete when vpx_codec_encode() is called
  784. * and vpx_codec_get_cx_data() returns no data.
  785. *
  786. * \param[in] ctx Pointer to this instance's context
  787. * \param[in] img Image data to encode, NULL to flush.
  788. * \param[in] pts Presentation time stamp, in timebase units.
  789. * \param[in] duration Duration to show frame, in timebase units.
  790. * \param[in] flags Flags to use for encoding this frame.
  791. * \param[in] deadline Time to spend encoding, in microseconds. (0=infinite)
  792. *
  793. * \retval #VPX_CODEC_OK
  794. * The configuration was populated.
  795. * \retval #VPX_CODEC_INCAPABLE
  796. * Interface is not an encoder interface.
  797. * \retval #VPX_CODEC_INVALID_PARAM
  798. * A parameter was NULL, the image format is unsupported, etc.
  799. */
  800. vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx,
  801. const vpx_image_t *img,
  802. vpx_codec_pts_t pts,
  803. unsigned long duration,
  804. vpx_enc_frame_flags_t flags,
  805. unsigned long deadline);
  806. /*!\brief Set compressed data output buffer
  807. *
  808. * Sets the buffer that the codec should output the compressed data
  809. * into. This call effectively sets the buffer pointer returned in the
  810. * next VPX_CODEC_CX_FRAME_PKT packet. Subsequent packets will be
  811. * appended into this buffer. The buffer is preserved across frames,
  812. * so applications must periodically call this function after flushing
  813. * the accumulated compressed data to disk or to the network to reset
  814. * the pointer to the buffer's head.
  815. *
  816. * `pad_before` bytes will be skipped before writing the compressed
  817. * data, and `pad_after` bytes will be appended to the packet. The size
  818. * of the packet will be the sum of the size of the actual compressed
  819. * data, pad_before, and pad_after. The padding bytes will be preserved
  820. * (not overwritten).
  821. *
  822. * Note that calling this function does not guarantee that the returned
  823. * compressed data will be placed into the specified buffer. In the
  824. * event that the encoded data will not fit into the buffer provided,
  825. * the returned packet \ref MAY point to an internal buffer, as it would
  826. * if this call were never used. In this event, the output packet will
  827. * NOT have any padding, and the application must free space and copy it
  828. * to the proper place. This is of particular note in configurations
  829. * that may output multiple packets for a single encoded frame (e.g., lagged
  830. * encoding) or if the application does not reset the buffer periodically.
  831. *
  832. * Applications may restore the default behavior of the codec providing
  833. * the compressed data buffer by calling this function with a NULL
  834. * buffer.
  835. *
  836. * Applications \ref MUSTNOT call this function during iteration of
  837. * vpx_codec_get_cx_data().
  838. *
  839. * \param[in] ctx Pointer to this instance's context
  840. * \param[in] buf Buffer to store compressed data into
  841. * \param[in] pad_before Bytes to skip before writing compressed data
  842. * \param[in] pad_after Bytes to skip after writing compressed data
  843. *
  844. * \retval #VPX_CODEC_OK
  845. * The buffer was set successfully.
  846. * \retval #VPX_CODEC_INVALID_PARAM
  847. * A parameter was NULL, the image format is unsupported, etc.
  848. */
  849. vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx,
  850. const vpx_fixed_buf_t *buf,
  851. unsigned int pad_before,
  852. unsigned int pad_after);
  853. /*!\brief Encoded data iterator
  854. *
  855. * Iterates over a list of data packets to be passed from the encoder to the
  856. * application. The different kinds of packets available are enumerated in
  857. * #vpx_codec_cx_pkt_kind.
  858. *
  859. * #VPX_CODEC_CX_FRAME_PKT packets should be passed to the application's
  860. * muxer. Multiple compressed frames may be in the list.
  861. * #VPX_CODEC_STATS_PKT packets should be appended to a global buffer.
  862. *
  863. * The application \ref MUST silently ignore any packet kinds that it does
  864. * not recognize or support.
  865. *
  866. * The data buffers returned from this function are only guaranteed to be
  867. * valid until the application makes another call to any vpx_codec_* function.
  868. *
  869. * \param[in] ctx Pointer to this instance's context
  870. * \param[in,out] iter Iterator storage, initialized to NULL
  871. *
  872. * \return Returns a pointer to an output data packet (compressed frame data,
  873. * two-pass statistics, etc.) or NULL to signal end-of-list.
  874. *
  875. */
  876. const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
  877. vpx_codec_iter_t *iter);
  878. /*!\brief Get Preview Frame
  879. *
  880. * Returns an image that can be used as a preview. Shows the image as it would
  881. * exist at the decompressor. The application \ref MUST NOT write into this
  882. * image buffer.
  883. *
  884. * \param[in] ctx Pointer to this instance's context
  885. *
  886. * \return Returns a pointer to a preview image, or NULL if no image is
  887. * available.
  888. *
  889. */
  890. const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx);
  891. /*!@} - end defgroup encoder*/
  892. #ifdef __cplusplus
  893. }
  894. #endif
  895. #endif // VPX_VPX_ENCODER_H_