MatroskaParser.h

00001 00002 /* 00003 00004 * Copyright (c) 2004-2005 Mike Matsnev. All Rights Reserved. 00005 00006 * 00007 00008 * Redistribution and use in source and binary forms, with or without 00009 00010 * modification, are permitted provided that the following conditions 00011 00012 * are met: 00013 00014 * 00015 00016 * 1. Redistributions of source code must retain the above copyright 00017 00018 * notice immediately at the beginning of the file, without modification, 00019 00020 * this list of conditions, and the following disclaimer. 00021 00022 * 2. Redistributions in binary form must reproduce the above copyright 00023 00024 * notice, this list of conditions and the following disclaimer in the 00025 00026 * documentation and/or other materials provided with the distribution. 00027 00028 * 3. Absolutely no warranty of function or purpose is made by the author 00029 00030 * Mike Matsnev. 00031 00032 * 00033 00034 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00035 00036 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00037 00038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00039 00040 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00041 00042 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00043 00044 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00045 00046 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00047 00048 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00049 00050 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00051 00052 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00053 00054 * 00055 00056 * $Id: MatroskaParser.h,v 1.6 2006/03/07 17:50:47 mkropfbe Exp $ 00057 00058 * 00059 00060 */ 00061 00062 00063 00064 #ifndef MATROSKA_PARSER_H 00065 00066 #define MATROSKA_PARSER_H 00067 00068 00069 00070 /* Random notes: 00071 00072 * 00073 00074 * The parser does not process frame data in any way and does not read it into 00075 00076 * the queue. The app should read it via mkv_ReadData if it is interested. 00077 00078 * 00079 00080 * The code here is 64-bit clean and was tested on FreeBSD/sparc 64-bit big endian 00081 00082 * system 00083 00084 */ 00085 00086 00087 00088 #ifdef MPDLLBUILD 00089 00090 #define X __declspec(dllexport) 00091 00092 #else 00093 00094 #ifdef MPDLL 00095 00096 #define X __declspec(dllimport) 00097 00098 #pragma comment(lib,"MatroskaParser") 00099 00100 #else 00101 00102 #define X 00103 00104 #endif 00105 00106 #endif 00107 00108 00109 00110 #define MATROSKA_COMPRESSION_SUPPORT 00111 00112 //#define MATROSKA_INTEGER_ONLY 00113 00114 00115 00116 #ifdef __cplusplus 00117 00118 extern "C" { 00119 00120 #endif 00121 00122 00123 00124 /* 64-bit integers */ 00125 00126 #if (defined(WINCE) || defined(WIN32)) 00127 00128 typedef signed __int64 longlong; 00129 00130 typedef unsigned __int64 ulonglong; 00131 00132 #else 00133 00134 typedef signed long long longlong; 00135 00136 typedef unsigned long long ulonglong; 00137 00138 #endif 00139 00140 00141 00142 /* MKFLOATing point */ 00143 00144 #ifdef MATROSKA_INTEGER_ONLY 00145 00146 typedef struct { 00147 00148 longlong v; 00149 00150 } MKFLOAT; 00151 00152 #else 00153 00154 typedef longlong MKFLOAT; 00155 00156 #endif 00157 00158 00159 00160 /* generic I/O */ 00161 00162 struct InputStream { 00163 00164 /* read bytes from stream */ 00165 00166 int (*read)(struct InputStream *cc,ulonglong pos,void *buffer,int count); 00167 00168 /* scan for a four byte signature, bytes must be nonzero */ 00169 00170 longlong (*scan)(struct InputStream *cc,ulonglong start,unsigned signature); 00171 00172 /* get cache size, this is used to cap readahead */ 00173 00174 unsigned (*getcachesize)(struct InputStream *cc); 00175 00176 /* fetch last error message */ 00177 00178 const char *(*geterror)(struct InputStream *cc); 00179 00180 /* memory allocation */ 00181 00182 void *(*memalloc)(struct InputStream *cc,size_t size); 00183 00184 void *(*memrealloc)(struct InputStream *cc,void *mem,size_t newsize); 00185 00186 void (*memfree)(struct InputStream *cc,void *mem); 00187 00188 /* zero return causes parser to abort open */ 00189 00190 int (*progress)(struct InputStream *cc,ulonglong cur,ulonglong max); 00191 00192 /* get file size, optional, can be NULL or return -1 if filesize is unknown */ 00193 00194 longlong (*getfilesize)(struct InputStream *cc); 00195 00196 }; 00197 00198 00199 00200 typedef struct InputStream InputStream; 00201 00202 00203 00204 /* matroska file */ 00205 00206 struct MatroskaFile; /* opaque */ 00207 00208 00209 00210 typedef struct MatroskaFile MatroskaFile; 00211 00212 00213 00214 #define COMP_ZLIB 0 00215 00216 #define COMP_BZIP 1 00217 00218 #define COMP_LZO1X 2 00219 00220 #define COMP_PREPEND 3 00221 00222 00223 00224 #define TT_VIDEO 1 00225 00226 #define TT_AUDIO 2 00227 00228 #define TT_SUB 17 00229 00230 00231 00232 struct TrackInfo { 00233 00234 unsigned char Number; 00235 00236 unsigned char Type; 00237 00238 unsigned char TrackOverlay; 00239 00240 ulonglong UID; 00241 00242 ulonglong MinCache; 00243 00244 ulonglong MaxCache; 00245 00246 ulonglong DefaultDuration; 00247 00248 MKFLOAT TimecodeScale; 00249 00250 void *CodecPrivate; 00251 00252 unsigned CodecPrivateSize; 00253 00254 unsigned CompMethod; 00255 00256 void *CompMethodPrivate; 00257 00258 unsigned CompMethodPrivateSize; 00259 00260 unsigned MaxBlockAdditionID; 00261 00262 struct { 00263 00264 unsigned int Enabled:1; 00265 00266 unsigned int Default:1; 00267 00268 unsigned int Lacing:1; 00269 00270 unsigned int DecodeAll:1; 00271 00272 unsigned int CompEnabled:1; 00273 00274 }; 00275 00276 00277 00278 union { 00279 00280 struct { 00281 00282 unsigned char StereoMode; 00283 00284 unsigned char DisplayUnit; 00285 00286 unsigned char AspectRatioType; 00287 00288 unsigned int PixelWidth; 00289 00290 unsigned int PixelHeight; 00291 00292 unsigned int DisplayWidth; 00293 00294 unsigned int DisplayHeight; 00295 00296 unsigned int CropL, CropT, CropR, CropB; 00297 00298 unsigned int ColourSpace; 00299 00300 MKFLOAT GammaValue; 00301 00302 struct { 00303 00304 unsigned int Interlaced:1; 00305 00306 }; 00307 00308 } Video; 00309 00310 struct { 00311 00312 MKFLOAT SamplingFreq; 00313 00314 MKFLOAT OutputSamplingFreq; 00315 00316 unsigned char Channels; 00317 00318 unsigned char BitDepth; 00319 00320 } Audio; 00321 00322 }; 00323 00324 00325 00326 /* various strings */ 00327 00328 char *Name; 00329 00330 char *Language; 00331 00332 char *CodecID; 00333 00334 }; 00335 00336 00337 00338 typedef struct TrackInfo TrackInfo; 00339 00340 00341 00342 struct SegmentInfo { 00343 00344 char UID[16]; 00345 00346 char PrevUID[16]; 00347 00348 char NextUID[16]; 00349 00350 char *Filename; 00351 00352 char *PrevFilename; 00353 00354 char *NextFilename; 00355 00356 char *Title; 00357 00358 char *MuxingApp; 00359 00360 char *WritingApp; 00361 00362 ulonglong TimecodeScale; 00363 00364 ulonglong Duration; 00365 00366 ulonglong DateUTC; 00367 00368 }; 00369 00370 00371 00372 typedef struct SegmentInfo SegmentInfo; 00373 00374 00375 00376 struct Attachment { 00377 00378 ulonglong Position; 00379 00380 ulonglong Length; 00381 00382 ulonglong UID; 00383 00384 char *Name; 00385 00386 char *Description; 00387 00388 char *MimeType; 00389 00390 }; 00391 00392 00393 00394 typedef struct Attachment Attachment; 00395 00396 00397 00398 struct ChapterDisplay { 00399 00400 char *String; 00401 00402 char *Language; 00403 00404 char *Country; 00405 00406 }; 00407 00408 00409 00410 struct ChapterCommand { 00411 00412 unsigned Time; 00413 00414 unsigned CommandLength; 00415 00416 void *Command; 00417 00418 }; 00419 00420 00421 00422 struct ChapterProcess { 00423 00424 unsigned CodecID; 00425 00426 unsigned CodecPrivateLength; 00427 00428 void *CodecPrivate; 00429 00430 unsigned nCommands,nCommandsSize; 00431 00432 struct ChapterCommand *Commands; 00433 00434 }; 00435 00436 00437 00438 struct Chapter { 00439 00440 ulonglong UID; 00441 00442 ulonglong Start; 00443 00444 ulonglong End; 00445 00446 00447 00448 unsigned nTracks,nTracksSize; 00449 00450 ulonglong *Tracks; 00451 00452 unsigned nDisplay,nDisplaySize; 00453 00454 struct ChapterDisplay *Display; 00455 00456 unsigned nChildren,nChildrenSize; 00457 00458 struct Chapter *Children; 00459 00460 unsigned nProcess,nProcessSize; 00461 00462 struct ChapterProcess *Process; 00463 00464 00465 00466 char SegmentUID[16]; 00467 00468 00469 00470 struct { 00471 00472 unsigned int Hidden:1; 00473 00474 unsigned int Enabled:1; 00475 00476 00477 00478 // Editions 00479 00480 unsigned int Default:1; 00481 00482 unsigned int Ordered:1; 00483 00484 }; 00485 00486 }; 00487 00488 00489 00490 typedef struct Chapter Chapter; 00491 00492 00493 00494 #define TARGET_TRACK 0 00495 00496 #define TARGET_CHAPTER 1 00497 00498 #define TARGET_ATTACHMENT 2 00499 00500 #define TARGET_EDITION 3 00501 00502 struct Target { 00503 00504 ulonglong UID; 00505 00506 unsigned Type; 00507 00508 }; 00509 00510 00511 00512 struct SimpleTag { 00513 00514 char *Name; 00515 00516 char *Value; 00517 00518 char *Language; 00519 00520 unsigned Default:1; 00521 00522 }; 00523 00524 00525 00526 struct Tag { 00527 00528 unsigned nTargets,nTargetsSize; 00529 00530 struct Target *Targets; 00531 00532 00533 00534 unsigned nSimpleTags,nSimpleTagsSize; 00535 00536 struct SimpleTag *SimpleTags; 00537 00538 }; 00539 00540 00541 00542 typedef struct Tag Tag; 00543 00544 00545 00546 /* Open a matroska file 00547 00548 * io pointer is recorded inside MatroskaFile 00549 00550 */ 00551 00552 X MatroskaFile *mkv_Open(/* in */ InputStream *io, 00553 00554 /* out */ char *err_msg, 00555 00556 /* in */ unsigned msgsize); 00557 00558 00559 00560 #define MKVF_AVOID_SEEKS 1 /* use sequential reading only */ 00561 00562 00563 00564 X MatroskaFile *mkv_OpenEx(/* in */ InputStream *io, 00565 00566 /* in */ ulonglong base, 00567 00568 /* in */ unsigned flags, 00569 00570 /* out */ char *err_msg, 00571 00572 /* in */ unsigned msgsize); 00573 00574 00575 00576 /* Close and deallocate mf 00577 00578 * NULL pointer is ok and is simply ignored 00579 00580 */ 00581 00582 X void mkv_Close(/* in */ MatroskaFile *mf); 00583 00584 00585 00586 /* Fetch the error message of the last failed operation */ 00587 00588 X const char *mkv_GetLastError(/* in */ MatroskaFile *mf); 00589 00590 00591 00592 /* Get file information */ 00593 00594 X SegmentInfo *mkv_GetFileInfo(/* in */ MatroskaFile *mf); 00595 00596 00597 00598 /* Get track information */ 00599 00600 X unsigned int mkv_GetNumTracks(/* in */ MatroskaFile *mf); 00601 00602 X TrackInfo *mkv_GetTrackInfo(/* in */ MatroskaFile *mf,/* in */ unsigned track); 00603 00604 00605 00606 /* chapters, tags and attachments */ 00607 00608 X void mkv_GetAttachments(/* in */ MatroskaFile *mf, 00609 00610 /* out */ Attachment **at, 00611 00612 /* out */ unsigned *count); 00613 00614 X void mkv_GetChapters(/* in */ MatroskaFile *mf, 00615 00616 /* out */ Chapter **ch, 00617 00618 /* out */ unsigned *count); 00619 00620 X void mkv_GetTags(/* in */ MatroskaFile *mf, 00621 00622 /* out */ Tag **tag, 00623 00624 /* out */ unsigned *count); 00625 00626 00627 00628 X ulonglong mkv_GetSegmentTop(MatroskaFile *mf); 00629 00630 00631 00632 /* Seek to specified timecode, 00633 00634 * if timecode is past end of file, 00635 00636 * all tracks are set to return EOF 00637 00638 * on next read 00639 00640 */ 00641 00642 #define MKVF_SEEK_TO_PREV_KEYFRAME 1 00643 00644 00645 00646 X void mkv_Seek(/* in */ MatroskaFile *mf, 00647 00648 /* in */ ulonglong timecode /* in ns */, 00649 00650 /* in */ unsigned flags); 00651 00652 00653 00654 X void mkv_SkipToKeyframe(MatroskaFile *mf); 00655 00656 00657 00658 X ulonglong mkv_GetLowestQTimecode(MatroskaFile *mf); 00659 00660 00661 00662 X int mkv_TruncFloat(MKFLOAT f); 00663 00664 00665 00666 /************************************************************************* 00667 00668 * reading data, pull model 00669 00670 */ 00671 00672 00673 00674 /* frame flags */ 00675 00676 #define FRAME_KF 0x00000001 00677 00678 #define FRAME_UNKNOWN_START 0x00000002 00679 00680 #define FRAME_UNKNOWN_END 0x00000004 00681 00682 #define FRAME_GAP 0x00000008 00683 00684 #define FRAME_STREAM_MASK 0xff000000 00685 00686 #define FRAME_STREAM_SHIFT 24 00687 00688 00689 00690 /* This sets the masking flags for the parser, 00691 00692 * masked tracks [with 1s in their bit positions] 00693 00694 * will be ignored when reading file data. 00695 00696 * This call discards all parsed and queued frames 00697 00698 */ 00699 00700 X void mkv_SetTrackMask(/* in */ MatroskaFile *mf,/* in */ unsigned int mask); 00701 00702 00703 00704 /* Read one frame from the queue. 00705 00706 * mask specifies what tracks to ignore. 00707 00708 * Returns -1 if there are no more frames in the specified 00709 00710 * set of tracks, 0 on success 00711 00712 */ 00713 00714 X int mkv_ReadFrame(/* in */ MatroskaFile *mf, 00715 00716 /* in */ unsigned int mask, 00717 00718 /* out */ unsigned int *track, 00719 00720 /* out */ ulonglong *StartTime /* in ns */, 00721 00722 /* out */ ulonglong *EndTime /* in ns */, 00723 00724 /* out */ ulonglong *FilePos /* in bytes from start of file */, 00725 00726 /* out */ unsigned int *FrameSize /* in bytes */, 00727 00728 /* out */ unsigned int *FrameFlags); 00729 00730 00731 00732 /* Read raw frame data, invokes underlying io methods, 00733 00734 * but keeps track of the file pointer for internal buffering. 00735 00736 * Returns the -1 if error occurred, or 0 on success 00737 00738 */ 00739 00740 X int mkv_ReadData(/* in */ MatroskaFile *mf, 00741 00742 /* in */ ulonglong FilePos, 00743 00744 /* out */ void *Buffer, 00745 00746 /* in */ unsigned int Count); 00747 00748 00749 00750 #ifdef MATROSKA_COMPRESSION_SUPPORT 00751 00752 /* Compressed streams support */ 00753 00754 struct CompressedStream; 00755 00756 00757 00758 typedef struct CompressedStream CompressedStream; 00759 00760 00761 00762 X CompressedStream *cs_Create(/* in */ MatroskaFile *mf, 00763 00764 /* in */ unsigned tracknum, 00765 00766 /* out */ char *errormsg, 00767 00768 /* in */ unsigned msgsize); 00769 00770 X void cs_Destroy(/* in */ CompressedStream *cs); 00771 00772 00773 00774 /* advance to the next frame in matroska stream, you need to pass values returned 00775 00776 * by mkv_ReadFrame */ 00777 00778 X void cs_NextFrame(/* in */ CompressedStream *cs, 00779 00780 /* in */ ulonglong pos, 00781 00782 /* in */ unsigned size); 00783 00784 00785 00786 /* read and decode more data from current frame, return number of bytes decoded, 00787 00788 * 0 on end of frame, or -1 on error */ 00789 00790 X int cs_ReadData(CompressedStream *cs,char *buffer,unsigned bufsize); 00791 00792 00793 00794 /* return error message for the last error */ 00795 00796 X const char *cs_GetLastError(CompressedStream *cs); 00797 00798 #endif 00799 00800 00801 00802 #ifdef __cplusplus 00803 00804 } 00805 00806 #endif 00807 00808 00809 00810 #undef X 00811 00812 00813 00814 #endif 00815 00816 00817 00818 00819