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 #ifndef NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79 8 #define NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79 9 10 #include "vpx/vpx_integer.h" 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 16 /** @mainpage 17 18 @section intro Introduction 19 20 This is the documentation fot the <tt>libnestegg</tt> C API. 21 <tt>libnestegg</tt> is a demultiplexing library for <a 22 href="http://www.matroska.org/">Matroska</a> and <a 23 href="http://www.webmproject.org/">WebMedia</a> media files. 24 25 @section example Example code 26 27 @code 28 nestegg * demux_ctx; 29 nestegg_init(&demux_ctx, io, NULL); 30 31 nestegg_packet * pkt; 32 while ((r = nestegg_read_packet(demux_ctx, &pkt)) > 0) { 33 unsigned int track; 34 35 nestegg_packet_track(pkt, &track); 36 37 // This example decodes the first track only. 38 if (track == 0) { 39 unsigned int chunk, chunks; 40 41 nestegg_packet_count(pkt, &chunks); 42 43 // Decode each chunk of data. 44 for (chunk = 0; chunk < chunks; ++chunk) { 45 unsigned char * data; 46 size_t data_size; 47 48 nestegg_packet_data(pkt, chunk, &data, &data_size); 49 50 example_codec_decode(codec_ctx, data, data_size); 51 } 52 } 53 54 nestegg_free_packet(pkt); 55 } 56 57 nestegg_destroy(demux_ctx); 58 @endcode 59 */ 60 61 62 /** @file 63 The <tt>libnestegg</tt> C API. */ 64 65 #define NESTEGG_TRACK_VIDEO 0 /**< Track is of type video. */ 66 #define NESTEGG_TRACK_AUDIO 1 /**< Track is of type audio. */ 67 68 #define NESTEGG_CODEC_VP8 0 /**< Track uses Google On2 VP8 codec. */ 69 #define NESTEGG_CODEC_VORBIS 1 /**< Track uses Xiph Vorbis codec. */ 70 71 #define NESTEGG_SEEK_SET 0 /**< Seek offset relative to beginning of stream. */ 72 #define NESTEGG_SEEK_CUR 1 /**< Seek offset relative to current position in stream. */ 73 #define NESTEGG_SEEK_END 2 /**< Seek offset relative to end of stream. */ 74 75 #define NESTEGG_LOG_DEBUG 1 /**< Debug level log message. */ 76 #define NESTEGG_LOG_INFO 10 /**< Informational level log message. */ 77 #define NESTEGG_LOG_WARNING 100 /**< Warning level log message. */ 78 #define NESTEGG_LOG_ERROR 1000 /**< Error level log message. */ 79 #define NESTEGG_LOG_CRITICAL 10000 /**< Critical level log message. */ 80 81 typedef struct nestegg nestegg; /**< Opaque handle referencing the stream state. */ 82 typedef struct nestegg_packet nestegg_packet; /**< Opaque handle referencing a packet of data. */ 83 84 /** User supplied IO context. */ 85 typedef struct { 86 /** User supplied read callback. 87 @param buffer Buffer to read data into. 88 @param length Length of supplied buffer in bytes. 89 @param userdata The #userdata supplied by the user. 90 @retval 1 Read succeeded. 91 @retval 0 End of stream. 92 @retval -1 Error. */ 93 int (* read)(void * buffer, size_t length, void * userdata); 94 95 /** User supplied seek callback. 96 @param offset Offset within the stream to seek to. 97 @param whence Seek direction. One of #NESTEGG_SEEK_SET, 98 #NESTEGG_SEEK_CUR, or #NESTEGG_SEEK_END. 99 @param userdata The #userdata supplied by the user. 100 @retval 0 Seek succeeded. 101 @retval -1 Error. */ 102 int (* seek)(int64_t offset, int whence, void * userdata); 103 104 /** User supplied tell callback. 105 @param userdata The #userdata supplied by the user. 106 @returns Current position within the stream. 107 @retval -1 Error. */ 108 int64_t (* tell)(void * userdata); 109 110 /** User supplied pointer to be passed to the IO callbacks. */ 111 void * userdata; 112 } nestegg_io; 113 114 /** Parameters specific to a video track. */ 115 typedef struct { 116 unsigned int width; /**< Width of the video frame in pixels. */ 117 unsigned int height; /**< Height of the video frame in pixels. */ 118 unsigned int display_width; /**< Display width of the video frame in pixels. */ 119 unsigned int display_height; /**< Display height of the video frame in pixels. */ 120 unsigned int crop_bottom; /**< Pixels to crop from the bottom of the frame. */ 121 unsigned int crop_top; /**< Pixels to crop from the top of the frame. */ 122 unsigned int crop_left; /**< Pixels to crop from the left of the frame. */ 123 unsigned int crop_right; /**< Pixels to crop from the right of the frame. */ 124 } nestegg_video_params; 125 126 /** Parameters specific to an audio track. */ 127 typedef struct { 128 double rate; /**< Sampling rate in Hz. */ 129 unsigned int channels; /**< Number of audio channels. */ 130 unsigned int depth; /**< Bits per sample. */ 131 } nestegg_audio_params; 132 133 /** Logging callback function pointer. */ 134 typedef void (* nestegg_log)(nestegg * context, unsigned int severity, char const * format, ...); 135 136 /** Initialize a nestegg context. During initialization the parser will 137 read forward in the stream processing all elements until the first 138 block of media is reached. All track metadata has been processed at this point. 139 @param context Storage for the new nestegg context. @see nestegg_destroy 140 @param io User supplied IO context. 141 @param callback Optional logging callback function pointer. May be NULL. 142 @retval 0 Success. 143 @retval -1 Error. */ 144 int nestegg_init(nestegg ** context, nestegg_io io, nestegg_log callback); 145 146 /** Destroy a nestegg context and free associated memory. 147 @param context #nestegg context to be freed. @see nestegg_init */ 148 void nestegg_destroy(nestegg * context); 149 150 /** Query the duration of the media stream in nanoseconds. 151 @param context Stream context initialized by #nestegg_init. 152 @param duration Storage for the queried duration. 153 @retval 0 Success. 154 @retval -1 Error. */ 155 int nestegg_duration(nestegg * context, uint64_t * duration); 156 157 /** Query the tstamp scale of the media stream in nanoseconds. 158 Timecodes presented by nestegg have been scaled by this value 159 before presentation to the caller. 160 @param context Stream context initialized by #nestegg_init. 161 @param scale Storage for the queried scale factor. 162 @retval 0 Success. 163 @retval -1 Error. */ 164 int nestegg_tstamp_scale(nestegg * context, uint64_t * scale); 165 166 /** Query the number of tracks in the media stream. 167 @param context Stream context initialized by #nestegg_init. 168 @param tracks Storage for the queried track count. 169 @retval 0 Success. 170 @retval -1 Error. */ 171 int nestegg_track_count(nestegg * context, unsigned int * tracks); 172 173 /** Seek @a track to @a tstamp. Stream seek will terminate at the earliest 174 key point in the stream at or before @a tstamp. Other tracks in the 175 stream will output packets with unspecified but nearby timestamps. 176 @param context Stream context initialized by #nestegg_init. 177 @param track Zero based track number. 178 @param tstamp Absolute timestamp in nanoseconds. 179 @retval 0 Success. 180 @retval -1 Error. */ 181 int nestegg_track_seek(nestegg * context, unsigned int track, uint64_t tstamp); 182 183 /** Query the type specified by @a track. 184 @param context Stream context initialized by #nestegg_init. 185 @param track Zero based track number. 186 @retval #NESTEGG_TRACK_VIDEO Track type is video. 187 @retval #NESTEGG_TRACK_AUDIO Track type is audio. 188 @retval -1 Error. */ 189 int nestegg_track_type(nestegg * context, unsigned int track); 190 191 /** Query the codec ID specified by @a track. 192 @param context Stream context initialized by #nestegg_init. 193 @param track Zero based track number. 194 @retval #NESTEGG_CODEC_VP8 Track codec is VP8. 195 @retval #NESTEGG_CODEC_VORBIS Track codec is Vorbis. 196 @retval -1 Error. */ 197 int nestegg_track_codec_id(nestegg * context, unsigned int track); 198 199 /** Query the number of codec initialization chunks for @a track. Each 200 chunk of data should be passed to the codec initialization functions in 201 the order returned. 202 @param context Stream context initialized by #nestegg_init. 203 @param track Zero based track number. 204 @param count Storage for the queried chunk count. 205 @retval 0 Success. 206 @retval -1 Error. */ 207 int nestegg_track_codec_data_count(nestegg * context, unsigned int track, 208 unsigned int * count); 209 210 /** Get a pointer to chunk number @a item of codec initialization data for 211 @a track. 212 @param context Stream context initialized by #nestegg_init. 213 @param track Zero based track number. 214 @param item Zero based chunk item number. 215 @param data Storage for the queried data pointer. 216 The data is owned by the #nestegg context. 217 @param length Storage for the queried data size. 218 @retval 0 Success. 219 @retval -1 Error. */ 220 int nestegg_track_codec_data(nestegg * context, unsigned int track, unsigned int item, 221 unsigned char ** data, size_t * length); 222 223 /** Query the video parameters specified by @a track. 224 @param context Stream context initialized by #nestegg_init. 225 @param track Zero based track number. 226 @param params Storage for the queried video parameters. 227 @retval 0 Success. 228 @retval -1 Error. */ 229 int nestegg_track_video_params(nestegg * context, unsigned int track, 230 nestegg_video_params * params); 231 232 /** Query the audio parameters specified by @a track. 233 @param context Stream context initialized by #nestegg_init. 234 @param track Zero based track number. 235 @param params Storage for the queried audio parameters. 236 @retval 0 Success. 237 @retval -1 Error. */ 238 int nestegg_track_audio_params(nestegg * context, unsigned int track, 239 nestegg_audio_params * params); 240 241 /** Read a packet of media data. A packet consists of one or more chunks of 242 data associated with a single track. nestegg_read_packet should be 243 called in a loop while the return value is 1 to drive the stream parser 244 forward. @see nestegg_free_packet 245 @param context Context returned by #nestegg_init. 246 @param packet Storage for the returned nestegg_packet. 247 @retval 1 Additional packets may be read in subsequent calls. 248 @retval 0 End of stream. 249 @retval -1 Error. */ 250 int nestegg_read_packet(nestegg * context, nestegg_packet ** packet); 251 252 /** Destroy a nestegg_packet and free associated memory. 253 @param packet #nestegg_packet to be freed. @see nestegg_read_packet */ 254 void nestegg_free_packet(nestegg_packet * packet); 255 256 /** Query the track number of @a packet. 257 @param packet Packet initialized by #nestegg_read_packet. 258 @param track Storage for the queried zero based track index. 259 @retval 0 Success. 260 @retval -1 Error. */ 261 int nestegg_packet_track(nestegg_packet * packet, unsigned int * track); 262 263 /** Query the time stamp in nanoseconds of @a packet. 264 @param packet Packet initialized by #nestegg_read_packet. 265 @param tstamp Storage for the queried timestamp in nanoseconds. 266 @retval 0 Success. 267 @retval -1 Error. */ 268 int nestegg_packet_tstamp(nestegg_packet * packet, uint64_t * tstamp); 269 270 /** Query the number of data chunks contained in @a packet. 271 @param packet Packet initialized by #nestegg_read_packet. 272 @param count Storage for the queried timestamp in nanoseconds. 273 @retval 0 Success. 274 @retval -1 Error. */ 275 int nestegg_packet_count(nestegg_packet * packet, unsigned int * count); 276 277 /** Get a pointer to chunk number @a item of packet data. 278 @param packet Packet initialized by #nestegg_read_packet. 279 @param item Zero based chunk item number. 280 @param data Storage for the queried data pointer. 281 The data is owned by the #nestegg_packet packet. 282 @param length Storage for the queried data size. 283 @retval 0 Success. 284 @retval -1 Error. */ 285 int nestegg_packet_data(nestegg_packet * packet, unsigned int item, 286 unsigned char ** data, size_t * length); 287 288 #ifdef __cplusplus 289 } 290 #endif 291 292 #endif /* NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79 */ 293