vpx_encoder.h 37 KB

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