vpx_encoder.h 37 KB

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