1 /* 2 * Copyright © 2010 Mozilla Foundation 3 * 4 * This program is made available under an ISC-style license. See the 5 * accompanying file LICENSE for details. 6 */ 7 #if !defined(NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79) 8 #define NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79 9 10 #include <limits.h> 11 #include <stdint.h> 12 13 #if defined(__cplusplus) 14 extern "C" { 15 #endif 16 17 /** @mainpage 18 19 @section intro Introduction 20 21 This is the documentation for the <tt>libnestegg</tt> C API. 22 <tt>libnestegg</tt> is a demultiplexing library for <a 23 href="http://www.webmproject.org/code/specs/container/">WebM</a> 24 media files. 25 26 @section example Example code 27 28 @code 29 nestegg * demux_ctx; 30 nestegg_init(&demux_ctx, io, NULL); 31 32 nestegg_packet * pkt; 33 while ((r = nestegg_read_packet(demux_ctx, &pkt)) > 0) { 34 unsigned int track; 35 36 nestegg_packet_track(pkt, &track); 37 38 // This example decodes the first track only. 39 if (track == 0) { 40 unsigned int chunk, chunks; 41 42 nestegg_packet_count(pkt, &chunks); 43 44 // Decode each chunk of data. 45 for (chunk = 0; chunk < chunks; ++chunk) { 46 unsigned char * data; 47 size_t data_size; 48 49 nestegg_packet_data(pkt, chunk, &data, &data_size); 50 51 example_codec_decode(codec_ctx, data, data_size); 52 } 53 } 54 55 nestegg_free_packet(pkt); 56 } 57 58 nestegg_destroy(demux_ctx); 59 @endcode 60 */ 61 62 63 /** @file 64 The <tt>libnestegg</tt> C API. */ 65 66 #define NESTEGG_TRACK_VIDEO 0 /**< Track is of type video. */ 67 #define NESTEGG_TRACK_AUDIO 1 /**< Track is of type audio. */ 68 #define NESTEGG_TRACK_UNKNOWN INT_MAX /**< Track is of type unknown. */ 69 70 #define NESTEGG_CODEC_VP8 0 /**< Track uses Google On2 VP8 codec. */ 71 #define NESTEGG_CODEC_VORBIS 1 /**< Track uses Xiph Vorbis codec. */ 72 #define NESTEGG_CODEC_VP9 2 /**< Track uses Google On2 VP9 codec. */ 73 #define NESTEGG_CODEC_OPUS 3 /**< Track uses Xiph Opus codec. */ 74 #define NESTEGG_CODEC_UNKNOWN INT_MAX /**< Track uses unknown codec. */ 75 76 #define NESTEGG_VIDEO_MONO 0 /**< Track is mono video. */ 77 #define NESTEGG_VIDEO_STEREO_LEFT_RIGHT 1 /**< Track is side-by-side stereo video. Left first. */ 78 #define NESTEGG_VIDEO_STEREO_BOTTOM_TOP 2 /**< Track is top-bottom stereo video. Right first. */ 79 #define NESTEGG_VIDEO_STEREO_TOP_BOTTOM 3 /**< Track is top-bottom stereo video. Left first. */ 80 #define NESTEGG_VIDEO_STEREO_RIGHT_LEFT 11 /**< Track is side-by-side stereo video. Right first. */ 81 82 #define NESTEGG_SEEK_SET 0 /**< Seek offset relative to beginning of stream. */ 83 #define NESTEGG_SEEK_CUR 1 /**< Seek offset relative to current position in stream. */ 84 #define NESTEGG_SEEK_END 2 /**< Seek offset relative to end of stream. */ 85 86 #define NESTEGG_LOG_DEBUG 1 /**< Debug level log message. */ 87 #define NESTEGG_LOG_INFO 10 /**< Informational level log message. */ 88 #define NESTEGG_LOG_WARNING 100 /**< Warning level log message. */ 89 #define NESTEGG_LOG_ERROR 1000 /**< Error level log message. */ 90 #define NESTEGG_LOG_CRITICAL 10000 /**< Critical level log message. */ 91 92 #define NESTEGG_ENCODING_COMPRESSION 0 /**< Content encoding type is compression. */ 93 #define NESTEGG_ENCODING_ENCRYPTION 1 /**< Content encoding type is encryption. */ 94 95 #define NESTEGG_PACKET_HAS_SIGNAL_BYTE_FALSE 0 /**< Packet does not have signal byte */ 96 #define NESTEGG_PACKET_HAS_SIGNAL_BYTE_UNENCRYPTED 1 /**< Packet has signal byte and is unencrypted */ 97 #define NESTEGG_PACKET_HAS_SIGNAL_BYTE_ENCRYPTED 2 /**< Packet has signal byte and is encrypted */ 98 99 #define NESTEGG_PACKET_HAS_KEYFRAME_FALSE 0 /**< Packet contains only keyframes. */ 100 #define NESTEGG_PACKET_HAS_KEYFRAME_TRUE 1 /**< Packet does not contain any keyframes */ 101 #define NESTEGG_PACKET_HAS_KEYFRAME_UNKNOWN 2 /**< Packet may or may not contain keyframes */ 102 103 typedef struct nestegg nestegg; /**< Opaque handle referencing the stream state. */ 104 typedef struct nestegg_packet nestegg_packet; /**< Opaque handle referencing a packet of data. */ 105 106 /** User supplied IO context. */ 107 typedef struct { 108 /** User supplied read callback. 109 @param buffer Buffer to read data into. 110 @param length Length of supplied buffer in bytes. 111 @param userdata The #userdata supplied by the user. 112 @retval 1 Read succeeded. 113 @retval 0 End of stream. 114 @retval -1 Error. */ 115 int (* read)(void * buffer, size_t length, void * userdata); 116 117 /** User supplied seek callback. 118 @param offset Offset within the stream to seek to. 119 @param whence Seek direction. One of #NESTEGG_SEEK_SET, 120 #NESTEGG_SEEK_CUR, or #NESTEGG_SEEK_END. 121 @param userdata The #userdata supplied by the user. 122 @retval 0 Seek succeeded. 123 @retval -1 Error. */ 124 int (* seek)(int64_t offset, int whence, void * userdata); 125 126 /** User supplied tell callback. 127 @param userdata The #userdata supplied by the user. 128 @returns Current position within the stream. 129 @retval -1 Error. */ 130 int64_t (* tell)(void * userdata); 131 132 /** User supplied pointer to be passed to the IO callbacks. */ 133 void * userdata; 134 } nestegg_io; 135 136 /** Parameters specific to a video track. */ 137 typedef struct { 138 unsigned int stereo_mode; /**< Video mode. One of #NESTEGG_VIDEO_MONO, 139 #NESTEGG_VIDEO_STEREO_LEFT_RIGHT, 140 #NESTEGG_VIDEO_STEREO_BOTTOM_TOP, or 141 #NESTEGG_VIDEO_STEREO_TOP_BOTTOM. */ 142 unsigned int width; /**< Width of the video frame in pixels. */ 143 unsigned int height; /**< Height of the video frame in pixels. */ 144 unsigned int display_width; /**< Display width of the video frame in pixels. */ 145 unsigned int display_height; /**< Display height of the video frame in pixels. */ 146 unsigned int crop_bottom; /**< Pixels to crop from the bottom of the frame. */ 147 unsigned int crop_top; /**< Pixels to crop from the top of the frame. */ 148 unsigned int crop_left; /**< Pixels to crop from the left of the frame. */ 149 unsigned int crop_right; /**< Pixels to crop from the right of the frame. */ 150 unsigned int alpha_mode; /**< 1 if an additional opacity stream is available, otherwise 0. */ 151 } nestegg_video_params; 152 153 /** Parameters specific to an audio track. */ 154 typedef struct { 155 double rate; /**< Sampling rate in Hz. */ 156 unsigned int channels; /**< Number of audio channels. */ 157 unsigned int depth; /**< Bits per sample. */ 158 uint64_t codec_delay; /**< Nanoseconds that must be discarded from the start. */ 159 uint64_t seek_preroll;/**< Nanoseconds that must be discarded after a seek. */ 160 } nestegg_audio_params; 161 162 /** Logging callback function pointer. */ 163 typedef void (* nestegg_log)(nestegg * context, unsigned int severity, char const * format, ...); 164 165 /** Initialize a nestegg context. During initialization the parser will 166 read forward in the stream processing all elements until the first 167 block of media is reached. All track metadata has been processed at this point. 168 @param context Storage for the new nestegg context. @see nestegg_destroy 169 @param io User supplied IO context. 170 @param callback Optional logging callback function pointer. May be NULL. 171 @param max_offset Optional maximum offset to be read. Set -1 to ignore. 172 @retval 0 Success. 173 @retval -1 Error. */ 174 int nestegg_init(nestegg ** context, nestegg_io io, nestegg_log callback, int64_t max_offset); 175 176 /** Destroy a nestegg context and free associated memory. 177 @param context #nestegg context to be freed. @see nestegg_init */ 178 void nestegg_destroy(nestegg * context); 179 180 /** Query the duration of the media stream in nanoseconds. 181 @param context Stream context initialized by #nestegg_init. 182 @param duration Storage for the queried duration. 183 @retval 0 Success. 184 @retval -1 Error. */ 185 int nestegg_duration(nestegg * context, uint64_t * duration); 186 187 /** Query the tstamp scale of the media stream in nanoseconds. 188 @note Timecodes presented by nestegg have been scaled by this value 189 before presentation to the caller. 190 @param context Stream context initialized by #nestegg_init. 191 @param scale Storage for the queried scale factor. 192 @retval 0 Success. 193 @retval -1 Error. */ 194 int nestegg_tstamp_scale(nestegg * context, uint64_t * scale); 195 196 /** Query the number of tracks in the media stream. 197 @param context Stream context initialized by #nestegg_init. 198 @param tracks Storage for the queried track count. 199 @retval 0 Success. 200 @retval -1 Error. */ 201 int nestegg_track_count(nestegg * context, unsigned int * tracks); 202 203 /** Query the start and end offset for a particular cluster. 204 @param context Stream context initialized by #nestegg_init. 205 @param cluster_num Zero-based cluster number; order they appear in cues. 206 @param max_offset Optional maximum offset to be read. Set -1 to ignore. 207 @param start_pos Starting offset of the cluster. -1 means non-existant. 208 @param end_pos Starting offset of the cluster. -1 means non-existant or 209 final cluster. 210 @param tstamp Starting timestamp of the cluster. 211 @retval 0 Success. 212 @retval -1 Error. */ 213 int nestegg_get_cue_point(nestegg * context, unsigned int cluster_num, 214 int64_t max_offset, int64_t * start_pos, 215 int64_t * end_pos, uint64_t * tstamp); 216 217 /** Seek to @a offset. Stream will seek directly to offset. 218 Must be used to seek to the start of a cluster; the parser will not be 219 able to understand other offsets. 220 @param context Stream context initialized by #nestegg_init. 221 @param offset Absolute offset in bytes. 222 @retval 0 Success. 223 @retval -1 Error. */ 224 int nestegg_offset_seek(nestegg * context, uint64_t offset); 225 226 /** Seek @a track to @a tstamp. Stream seek will terminate at the earliest 227 key point in the stream at or before @a tstamp. Other tracks in the 228 stream will output packets with unspecified but nearby timestamps. 229 @param context Stream context initialized by #nestegg_init. 230 @param track Zero based track number. 231 @param tstamp Absolute timestamp in nanoseconds. 232 @retval 0 Success. 233 @retval -1 Error. */ 234 int nestegg_track_seek(nestegg * context, unsigned int track, uint64_t tstamp); 235 236 /** Query the type specified by @a track. 237 @param context Stream context initialized by #nestegg_init. 238 @param track Zero based track number. 239 @retval #NESTEGG_TRACK_VIDEO Track type is video. 240 @retval #NESTEGG_TRACK_AUDIO Track type is audio. 241 @retval #NESTEGG_TRACK_UNKNOWN Track type is unknown. 242 @retval -1 Error. */ 243 int nestegg_track_type(nestegg * context, unsigned int track); 244 245 /** Query the codec ID specified by @a track. 246 @param context Stream context initialized by #nestegg_init. 247 @param track Zero based track number. 248 @retval #NESTEGG_CODEC_VP8 Track codec is VP8. 249 @retval #NESTEGG_CODEC_VP9 Track codec is VP9. 250 @retval #NESTEGG_CODEC_VORBIS Track codec is Vorbis. 251 @retval #NESTEGG_CODEC_OPUS Track codec is Opus. 252 @retval #NESTEGG_CODEC_UNKNOWN Track codec is unknown. 253 @retval -1 Error. */ 254 int nestegg_track_codec_id(nestegg * context, unsigned int track); 255 256 /** Query the number of codec initialization chunks for @a track. Each 257 chunk of data should be passed to the codec initialization functions in 258 the order returned. 259 @param context Stream context initialized by #nestegg_init. 260 @param track Zero based track number. 261 @param count Storage for the queried chunk count. 262 @retval 0 Success. 263 @retval -1 Error. */ 264 int nestegg_track_codec_data_count(nestegg * context, unsigned int track, 265 unsigned int * count); 266 267 /** Get a pointer to chunk number @a item of codec initialization data for 268 @a track. 269 @param context Stream context initialized by #nestegg_init. 270 @param track Zero based track number. 271 @param item Zero based chunk item number. 272 @param data Storage for the queried data pointer. 273 The data is owned by the #nestegg context. 274 @param length Storage for the queried data size. 275 @retval 0 Success. 276 @retval -1 Error. */ 277 int nestegg_track_codec_data(nestegg * context, unsigned int track, unsigned int item, 278 unsigned char ** data, size_t * length); 279 280 /** Query the video parameters specified by @a track. 281 @param context Stream context initialized by #nestegg_init. 282 @param track Zero based track number. 283 @param params Storage for the queried video parameters. 284 @retval 0 Success. 285 @retval -1 Error. */ 286 int nestegg_track_video_params(nestegg * context, unsigned int track, 287 nestegg_video_params * params); 288 289 /** Query the audio parameters specified by @a track. 290 @param context Stream context initialized by #nestegg_init. 291 @param track Zero based track number. 292 @param params Storage for the queried audio parameters. 293 @retval 0 Success. 294 @retval -1 Error. */ 295 int nestegg_track_audio_params(nestegg * context, unsigned int track, 296 nestegg_audio_params * params); 297 298 /** Query the encoding status for @a track. If a track has multiple encodings 299 the first will be returned. 300 @param context Stream context initialized by #nestegg_init. 301 @param track Zero based track number. 302 @retval #NESTEGG_ENCODING_COMPRESSION The track is compressed, but not encrypted. 303 @retval #NESTEGG_ENCODING_ENCRYPTION The track is encrypted and compressed. 304 @retval -1 Error. */ 305 int nestegg_track_encoding(nestegg * context, unsigned int track); 306 307 /** Query the ContentEncKeyId for @a track. Will return an error if the track 308 in not encrypted, or is not recognized. 309 @param context Stream context initialized by #nestegg_init. 310 @param track Zero based track number. 311 @param content_enc_key_id Storage for queried id. The content encryption key used. 312 Owned by nestegg and will be freed separately. 313 @param content_enc_key_id_length Length of the queried ContentEncKeyId in bytes. 314 @retval 0 Success. 315 @retval -1 Error. */ 316 int nestegg_track_content_enc_key_id(nestegg * context, unsigned int track, 317 unsigned char const ** content_enc_key_id, 318 size_t * content_enc_key_id_length); 319 320 /** Query the default frame duration for @a track. For a video track, this 321 is typically the inverse of the video frame rate. 322 @param context Stream context initialized by #nestegg_init. 323 @param track Zero based track number. 324 @param duration Storage for the default duration in nanoseconds. 325 @retval 0 Success. 326 @retval -1 Error. */ 327 int nestegg_track_default_duration(nestegg * context, unsigned int track, 328 uint64_t * duration); 329 330 /** Reset parser state to the last valid state before nestegg_read_packet failed. 331 @param context Stream context initialized by #nestegg_init. 332 @retval 0 Success. 333 @retval -1 Error. */ 334 int nestegg_read_reset(nestegg * context); 335 336 /** Read a packet of media data. A packet consists of one or more chunks of 337 data associated with a single track. nestegg_read_packet should be 338 called in a loop while the return value is 1 to drive the stream parser 339 forward. @see nestegg_free_packet 340 @param context Context returned by #nestegg_init. 341 @param packet Storage for the returned nestegg_packet. 342 @retval 1 Additional packets may be read in subsequent calls. 343 @retval 0 End of stream. 344 @retval -1 Error. */ 345 int nestegg_read_packet(nestegg * context, nestegg_packet ** packet); 346 347 /** Destroy a nestegg_packet and free associated memory. 348 @param packet #nestegg_packet to be freed. @see nestegg_read_packet */ 349 void nestegg_free_packet(nestegg_packet * packet); 350 351 /** Query the keyframe status for a given packet. 352 @param packet Packet initialized by #nestegg_read_packet. 353 @retval #NESTEGG_PACKET_HAS_KEYFRAME_FALSE Packet contains no keyframes. 354 @retval #NESTEGG_PACKET_HAS_KEYFRAME_TRUE Packet contains keyframes. 355 @retval #NESTEGG_PACKET_HAS_KEYFRAME_UNKNOWN Unknown packet keyframe content. 356 @retval -1 Error. */ 357 int nestegg_packet_has_keyframe(nestegg_packet * packet); 358 359 /** Query the track number of @a packet. 360 @param packet Packet initialized by #nestegg_read_packet. 361 @param track Storage for the queried zero based track index. 362 @retval 0 Success. 363 @retval -1 Error. */ 364 int nestegg_packet_track(nestegg_packet * packet, unsigned int * track); 365 366 /** Query the time stamp in nanoseconds of @a packet. 367 @param packet Packet initialized by #nestegg_read_packet. 368 @param tstamp Storage for the queried timestamp in nanoseconds. 369 @retval 0 Success. 370 @retval -1 Error. */ 371 int nestegg_packet_tstamp(nestegg_packet * packet, uint64_t * tstamp); 372 373 /** Query the duration in nanoseconds of @a packet. 374 @param packet Packet initialized by #nestegg_read_packet. 375 @param duration Storage for the queried duration in nanoseconds. 376 @retval 0 Success. 377 @retval -1 Error. */ 378 int nestegg_packet_duration(nestegg_packet * packet, uint64_t * duration); 379 380 /** Query the number of data chunks contained in @a packet. 381 @param packet Packet initialized by #nestegg_read_packet. 382 @param count Storage for the queried chunk count. 383 @retval 0 Success. 384 @retval -1 Error. */ 385 int nestegg_packet_count(nestegg_packet * packet, unsigned int * count); 386 387 /** Get a pointer to chunk number @a item of packet data. 388 @param packet Packet initialized by #nestegg_read_packet. 389 @param item Zero based chunk item number. 390 @param data Storage for the queried data pointer. 391 The data is owned by the #nestegg_packet packet. 392 @param length Storage for the queried data size. 393 @retval 0 Success. 394 @retval -1 Error. */ 395 int nestegg_packet_data(nestegg_packet * packet, unsigned int item, 396 unsigned char ** data, size_t * length); 397 398 /** Get a pointer to additional data with identifier @a id of additional packet 399 data. If @a id isn't present in the packet, returns -1. 400 @param packet Packet initialized by #nestegg_read_packet. 401 @param id Codec specific identifer. For VP8, use 1 to get a VP8 encoded 402 frame containing an alpha channel in its Y plane. 403 @param data Storage for the queried data pointer. 404 The data is owned by the #nestegg_packet packet. 405 @param length Storage for the queried data size. 406 @retval 0 Success. 407 @retval -1 Error. */ 408 int nestegg_packet_additional_data(nestegg_packet * packet, unsigned int id, 409 unsigned char ** data, size_t * length); 410 411 /** Returns discard_padding for given packet 412 @param packet Packet initialized by #nestegg_read_packet. 413 @param discard_padding pointer to store discard padding in. 414 @retval 0 Success. 415 @retval -1 Error. */ 416 int nestegg_packet_discard_padding(nestegg_packet * packet, 417 int64_t * discard_padding); 418 419 /** Query if a packet is encrypted. 420 @param packet Packet initialized by #nestegg_read_packet. 421 @retval #NESTEGG_PACKET_HAS_SIGNAL_BYTE_FALSE No signal byte, encryption 422 information not read from packet. 423 @retval #NESTEGG_PACKET_HAS_SIGNAL_BYTE_UNENCRYPTED Encrypted bit not 424 set, encryption information not read from packet. 425 @retval #NESTEGG_PACKET_HAS_SIGNAL_BYTE_ENCRYPTED Encrypted bit set, 426 encryption infomation read from packet. 427 @retval -1 Error.*/ 428 int nestegg_packet_encryption(nestegg_packet * packet); 429 430 /** Query the IV for an encrypted packet. Expects a packet from an encrypted 431 track, and will return error if given a packet that has no signal btye. 432 @param packet Packet initialized by #nestegg_read_packet. 433 @param iv Storage for queried iv. 434 @param length Length of returned iv, may be 0. 435 The data is owned by the #nestegg_packet packet. 436 @retval 0 Success. 437 @retval -1 Error. 438 */ 439 int nestegg_packet_iv(nestegg_packet * packet, unsigned char const ** iv, 440 size_t * length); 441 442 /** Returns reference_block given packet 443 @param packet Packet initialized by #nestegg_read_packet. 444 @param reference_block pointer to store reference block in. 445 @retval 0 Success. 446 @retval -1 Error. */ 447 int nestegg_packet_reference_block(nestegg_packet * packet, 448 int64_t * reference_block); 449 450 /** Query the presence of cues. 451 @param context Stream context initialized by #nestegg_init. 452 @retval 0 The media has no cues. 453 @retval 1 The media has cues. */ 454 int nestegg_has_cues(nestegg * context); 455 456 /** Try to determine if the buffer looks like the beginning of a WebM file. 457 @param buffer A buffer containing the beginning of a media file. 458 @param length The size of the buffer. 459 @retval 0 The file is not a WebM file. 460 @retval 1 The file is a WebM file. */ 461 int nestegg_sniff(unsigned char const * buffer, size_t length); 462 463 #if defined(__cplusplus) 464 } 465 #endif 466 467 #endif /* NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79 */ 468