vpx_encoder.h 36 KB

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