First import.

This commit is contained in:
Damiano Galassi committed 2013-11-01 10:20:09 +01:00
1 parent 4e61250dc6
commit 796e9691ce
114 files changed
+37004 -44

No files matched your search

+143
View File
@@ -0,0 +1,143 @@
/*
* MatroskaFile.c
* Subler
*
* Created by Ryan Walklin on 23/09/09.
* Copyright 2009 Test Toast. All rights reserved.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include "MatroskaParser.h"
#include "MatroskaFile.h"
#pragma mark Parser callbacks
#define CACHESIZE 0xFFFFFFF
/* StdIoStream methods */
/* read count bytes into buffer starting at file position pos
* return the number of bytes read, -1 on error or 0 on EOF
*/
int StdIoRead(StdIoStream *st, uint64_t pos, void *buffer, int count) {
size_t rd;
if (fseeko(st->fp, pos, SEEK_SET) == -1) {
st->error = errno;
return -1;
}
rd = fread(buffer, 1, count, st->fp);
if (rd == 0) {
if (feof(st->fp))
return 0;
st->error = errno;
return -1;
}
return rd;
}
/* scan for a signature sig(big-endian) starting at file position pos
* return position of the first byte of signature or -1 if error/not found
*/
longlong StdIoScan(StdIoStream *st, uint64_t start, uint32_t signature) {
uint32_t c;
uint32_t cmp = 0;
FILE *fp = st->fp;
if (fseeko(fp, start, SEEK_SET))
return -1;
while ((c = getc(fp)) != EOF) {
cmp = ((cmp << 8) | c) & 0xffffffff;
if (cmp == signature)
return ftell(fp) - 4;
}
return -1;
}
/* return cache size, this is used to limit readahead */
unsigned StdIoGetCacheSize(StdIoStream *st) {
return CACHESIZE;
}
/* return last error message */
const char *StdIoGetLastError(StdIoStream *st) {
return strerror(st->error);
}
/* memory allocation, this is done via stdlib */
void *StdIoMalloc(StdIoStream *st, size_t size) {
return malloc(size);
}
void *StdIoRealloc(StdIoStream *st, void *mem, size_t size) {
return realloc(mem,size);
}
void StdIoFree(StdIoStream *st, void *mem) {
free(mem);
}
/* progress report handler for lengthy operations
* returns 0 to abort operation, nonzero to continue
*/
int StdIoProgress(StdIoStream *st, uint64_t cur, uint64_t max) {
return 1;
}
MatroskaFile *openMatroskaFile(char *filePath, StdIoStream *ioStream)
{
char err_msg[256];
/* fill in I/O object */
ioStream->base.read = (int(*)(struct InputStream *, ulonglong, void *, int))StdIoRead;
ioStream->base.scan = (longlong(*)(struct InputStream *, ulonglong, unsigned int))StdIoScan;
ioStream->base.getcachesize = (unsigned int (*)(struct InputStream *))StdIoGetCacheSize;
ioStream->base.geterror = (const char *(*)(struct InputStream *))StdIoGetLastError;
ioStream->base.memalloc = (void *(*)(struct InputStream *, size_t))StdIoMalloc;
ioStream->base.memrealloc = (void *(*)(struct InputStream *, void *, size_t))StdIoRealloc;
ioStream->base.memfree = (void(*)(struct InputStream *, void *))StdIoFree;
ioStream->base.progress = (int (*)(struct InputStream *, uint64_t, uint64_t))StdIoProgress;
/* open source file */
ioStream->fp = fopen(filePath,"r");
if (ioStream->fp == NULL) {
fprintf(stderr, "Can't open '%s': %s\n", filePath, strerror(errno));
return NULL;
}
/* Disable ram cache */
//fcntl (ioStream->fp->_file, F_RDAHEAD, 1);
//fcntl (ioStream->fp->_file, F_NOCACHE, 1);
/* initialize matroska parser */
MatroskaFile *mf = mkv_Open(&ioStream->base, /* pointer to I/O object */
err_msg, sizeof(err_msg)); /* error message is returned here */
if (mf == NULL) {
fclose(ioStream->fp);
fprintf(stderr, "Can't parse Matroska file: %s\n", err_msg);
return NULL;
}
return mf;
}
void closeMatroskaFile(MatroskaFile *matroskaFile, StdIoStream *ioStream)
{
/* close matroska parser */
mkv_Close(matroskaFile);
/* close file */
if (ioStream) {
fclose(ioStream->fp);
free(ioStream);
}
}
+23
View File
@@ -0,0 +1,23 @@
/*
* MatroskaFile.h
* Subler
*
* Created by Ryan Walklin on 23/09/09.
* Copyright 2009 Test Toast. All rights reserved.
*
*/
/* first we need to create an I/O object that the parser will use to read the
* source file
*/
struct StdIoStream {
struct InputStream base;
FILE *fp;
int error;
};
typedef struct StdIoStream StdIoStream;
MatroskaFile *openMatroskaFile(char *filePath, StdIoStream *ioStream);
void closeMatroskaFile(MatroskaFile *matroskaFile, StdIoStream *ioStream);
File diff suppressed because it is too large. Load diff
+396
View File
@@ -0,0 +1,396 @@
/*
* Copyright (c) 2004-2008 Mike Matsnev. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice immediately at the beginning of the file, without modification,
* this list of conditions, and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Absolutely no warranty of function or purpose is made by the author
* Mike Matsnev.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: MatroskaParser.h,v 1.22 2008/04/29 21:03:09 mike Exp $
*
*/
#ifndef MATROSKA_PARSER_H
#define MATROSKA_PARSER_H
/* Random notes:
*
* The parser does not process frame data in any way and does not read it into
* the queue. The app should read it via mkv_ReadData if it is interested.
*
* The code here is 64-bit clean and was tested on FreeBSD/sparc 64-bit big endian
* system
*/
#ifdef MPDLLBUILD
#define X __declspec(dllexport)
#else
#ifdef MPDLL
#define X __declspec(dllimport)
#pragma comment(lib,"MatroskaParser")
#else
#define X
#endif
#endif
#define MATROSKA_COMPRESSION_SUPPORT
#define MATROSKA_INTEGER_ONLY
#ifdef __cplusplus
extern "C" {
#endif
/* 64-bit integers */
#ifdef _WIN32_WCE
typedef signed __int64 longlong;
typedef unsigned __int64 ulonglong;
#else
typedef signed long long longlong;
typedef unsigned long long ulonglong;
#endif
/* MKFLOATing point */
#ifdef MATROSKA_INTEGER_ONLY
typedef struct {
longlong v;
} MKFLOAT;
#else
typedef double MKFLOAT;
#endif
/* generic I/O */
struct InputStream {
/* read bytes from stream */
int (*read)(struct InputStream *cc,ulonglong pos,void *buffer,int count);
/* scan for a four byte signature, bytes must be nonzero */
longlong (*scan)(struct InputStream *cc,ulonglong start,unsigned signature);
/* get cache size, this is used to cap readahead */
unsigned (*getcachesize)(struct InputStream *cc);
/* fetch last error message */
const char *(*geterror)(struct InputStream *cc);
/* memory allocation */
void *(*memalloc)(struct InputStream *cc,size_t size);
void *(*memrealloc)(struct InputStream *cc,void *mem,size_t newsize);
void (*memfree)(struct InputStream *cc,void *mem);
/* zero return causes parser to abort open */
int (*progress)(struct InputStream *cc,ulonglong cur,ulonglong max);
/* get file size, optional, can be NULL or return -1 if filesize is unknown */
longlong (*getfilesize)(struct InputStream *cc);
};
typedef struct InputStream InputStream;
/* matroska file */
struct MatroskaFile; /* opaque */
typedef struct MatroskaFile MatroskaFile;
#define COMP_ZLIB 0
#define COMP_BZIP 1
#define COMP_LZO1X 2
#define COMP_PREPEND 3
#define TT_VIDEO 1
#define TT_AUDIO 2
#define TT_SUB 17
struct TrackInfo {
unsigned char Number;
unsigned char Type;
unsigned char TrackOverlay;
ulonglong UID;
ulonglong MinCache;
ulonglong MaxCache;
ulonglong DefaultDuration;
MKFLOAT TimecodeScale;
void *CodecPrivate;
unsigned CodecPrivateSize;
unsigned CompMethod;
void *CompMethodPrivate;
unsigned CompMethodPrivateSize;
unsigned MaxBlockAdditionID;
unsigned int Enabled:1;
unsigned int Default:1;
unsigned int Lacing:1;
unsigned int DecodeAll:1;
unsigned int CompEnabled:1;
union {
struct {
unsigned char StereoMode;
unsigned char DisplayUnit;
unsigned char AspectRatioType;
unsigned int PixelWidth;
unsigned int PixelHeight;
unsigned int DisplayWidth;
unsigned int DisplayHeight;
unsigned int CropL, CropT, CropR, CropB;
unsigned int ColourSpace;
MKFLOAT GammaValue;
unsigned int Interlaced:1;
} Video;
struct {
MKFLOAT SamplingFreq;
MKFLOAT OutputSamplingFreq;
unsigned char Channels;
unsigned char BitDepth;
} Audio;
} AV;
/* various strings */
char *Name;
char Language[4];
char *CodecID;
};
typedef struct TrackInfo TrackInfo;
struct SegmentInfo {
char UID[16];
char PrevUID[16];
char NextUID[16];
char *Filename;
char *PrevFilename;
char *NextFilename;
char *Title;
char *MuxingApp;
char *WritingApp;
ulonglong TimecodeScale;
ulonglong Duration;
longlong DateUTC;
char DateUTCValid;
};
typedef struct SegmentInfo SegmentInfo;
struct Attachment {
ulonglong Position;
ulonglong Length;
ulonglong UID;
char *Name;
char *Description;
char *MimeType;
};
typedef struct Attachment Attachment;
struct ChapterDisplay {
char *String;
char Language[4];
char Country[4];
};
struct ChapterCommand {
unsigned Time;
unsigned CommandLength;
void *Command;
};
struct ChapterProcess {
unsigned CodecID;
unsigned CodecPrivateLength;
void *CodecPrivate;
unsigned nCommands,nCommandsSize;
struct ChapterCommand *Commands;
};
struct Chapter {
ulonglong UID;
ulonglong Start;
ulonglong End;
unsigned nTracks,nTracksSize;
ulonglong *Tracks;
unsigned nDisplay,nDisplaySize;
struct ChapterDisplay *Display;
unsigned nChildren,nChildrenSize;
struct Chapter *Children;
unsigned nProcess,nProcessSize;
struct ChapterProcess *Process;
char SegmentUID[16];
unsigned int Hidden:1;
unsigned int Enabled:1;
// Editions
unsigned int Default:1;
unsigned int Ordered:1;
};
typedef struct Chapter Chapter;
#define TARGET_TRACK 0
#define TARGET_CHAPTER 1
#define TARGET_ATTACHMENT 2
#define TARGET_EDITION 3
struct Target {
ulonglong UID;
unsigned Type;
};
struct SimpleTag {
char *Name;
char *Value;
char Language[4];
unsigned Default:1;
};
struct Tag {
unsigned nTargets,nTargetsSize;
struct Target *Targets;
unsigned nSimpleTags,nSimpleTagsSize;
struct SimpleTag *SimpleTags;
};
typedef struct Tag Tag;
/* Open a matroska file
* io pointer is recorded inside MatroskaFile
*/
X MatroskaFile *mkv_Open(/* in */ InputStream *io,
/* out */ char *err_msg,
/* in */ unsigned msgsize);
#define MKVF_AVOID_SEEKS 1 /* use sequential reading only */
X MatroskaFile *mkv_OpenEx(/* in */ InputStream *io,
/* in */ ulonglong base,
/* in */ unsigned flags,
/* out */ char *err_msg,
/* in */ unsigned msgsize);
/* Close and deallocate mf
* NULL pointer is ok and is simply ignored
*/
X void mkv_Close(/* in */ MatroskaFile *mf);
/* Fetch the error message of the last failed operation */
X const char *mkv_GetLastError(/* in */ MatroskaFile *mf);
/* Get file information */
X SegmentInfo *mkv_GetFileInfo(/* in */ MatroskaFile *mf);
/* Get track information */
X unsigned int mkv_GetNumTracks(/* in */ MatroskaFile *mf);
X TrackInfo *mkv_GetTrackInfo(/* in */ MatroskaFile *mf,/* in */ unsigned track);
/* chapters, tags and attachments */
X void mkv_GetAttachments(/* in */ MatroskaFile *mf,
/* out */ Attachment **at,
/* out */ unsigned *count);
X void mkv_GetChapters(/* in */ MatroskaFile *mf,
/* out */ Chapter **ch,
/* out */ unsigned *count);
X void mkv_GetTags(/* in */ MatroskaFile *mf,
/* out */ Tag **tag,
/* out */ unsigned *count);
X ulonglong mkv_GetSegmentTop(MatroskaFile *mf);
/* Seek to specified timecode,
* if timecode is past end of file,
* all tracks are set to return EOF
* on next read
*/
#define MKVF_SEEK_TO_PREV_KEYFRAME 1
#define MKVF_SEEK_TO_PREV_KEYFRAME_STRICT 2
X void mkv_Seek(/* in */ MatroskaFile *mf,
/* in */ ulonglong timecode /* in ns */,
/* in */ unsigned flags);
X void mkv_SkipToKeyframe(MatroskaFile *mf);
X ulonglong mkv_GetLowestQTimecode(MatroskaFile *mf);
X int mkv_TruncFloat(MKFLOAT f);
/*************************************************************************
* reading data, pull model
*/
/* frame flags */
#define FRAME_UNKNOWN_START 0x00000001
#define FRAME_UNKNOWN_END 0x00000002
#define FRAME_KF 0x00000004
#define FRAME_GAP 0x00800000
#define FRAME_STREAM_MASK 0xff000000
#define FRAME_STREAM_SHIFT 24
/* This sets the masking flags for the parser,
* masked tracks [with 1s in their bit positions]
* will be ignored when reading file data.
* This call discards all parsed and queued frames
*/
X void mkv_SetTrackMask(/* in */ MatroskaFile *mf,/* in */ unsigned int mask);
/* Read one frame from the queue.
* mask specifies what tracks to ignore.
* Returns -1 if there are no more frames in the specified
* set of tracks, 0 on success
*/
X int mkv_ReadFrame(/* in */ MatroskaFile *mf,
/* in */ unsigned int mask,
/* out */ unsigned int *track,
/* out */ ulonglong *StartTime /* in ns */,
/* out */ ulonglong *EndTime /* in ns */,
/* out */ ulonglong *FilePos /* in bytes from start of file */,
/* out */ unsigned int *FrameSize /* in bytes */,
/* out */ unsigned int *FrameFlags);
#ifdef MATROSKA_COMPRESSION_SUPPORT
/* Compressed streams support */
struct CompressedStream;
typedef struct CompressedStream CompressedStream;
X CompressedStream *cs_Create(/* in */ MatroskaFile *mf,
/* in */ unsigned tracknum,
/* out */ char *errormsg,
/* in */ unsigned msgsize);
X void cs_Destroy(/* in */ CompressedStream *cs);
/* advance to the next frame in matroska stream, you need to pass values returned
* by mkv_ReadFrame */
X void cs_NextFrame(/* in */ CompressedStream *cs,
/* in */ ulonglong pos,
/* in */ unsigned size);
/* read and decode more data from current frame, return number of bytes decoded,
* 0 on end of frame, or -1 on error */
X int cs_ReadData(CompressedStream *cs,char *buffer,unsigned bufsize);
/* return error message for the last error */
X const char *cs_GetLastError(CompressedStream *cs);
#endif
#ifdef __cplusplus
}
#endif
#undef X
#endif
+1610
View File
File diff suppressed because it is too large. Load diff
+108
View File
@@ -0,0 +1,108 @@
/* $Id: downmix.h,v 1.51 2005/11/04 13:09:40 stebbins Exp $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#ifndef DOWNMIX_H
#define DOWNMIX_H
/* Input Channel Layout */
/* define some masks, used to extract the various information from the HB_AMIXDOWN_XXXX values */
#define HB_INPUT_CH_LAYOUT_DISCRETE_FRONT_MASK 0x00F0000
#define HB_INPUT_CH_LAYOUT_DISCRETE_REAR_MASK 0x000F000
#define HB_INPUT_CH_LAYOUT_DISCRETE_LFE_MASK 0x0000F00
#define HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK 0xFFFF0FF
#define HB_INPUT_CH_LAYOUT_ENCODED_FRONT_MASK 0x00000F0
#define HB_INPUT_CH_LAYOUT_ENCODED_REAR_MASK 0x000000F
/* define the input channel layouts used to describe the channel layout of this audio */
#define HB_INPUT_CH_LAYOUT_MONO 0x0110010
#define HB_INPUT_CH_LAYOUT_STEREO 0x0220020
#define HB_INPUT_CH_LAYOUT_DOLBY 0x0320031
#define HB_INPUT_CH_LAYOUT_3F 0x0430030
#define HB_INPUT_CH_LAYOUT_2F1R 0x0521021
#define HB_INPUT_CH_LAYOUT_3F1R 0x0631031
#define HB_INPUT_CH_LAYOUT_2F2R 0x0722022
#define HB_INPUT_CH_LAYOUT_3F2R 0x0832032
#define HB_INPUT_CH_LAYOUT_4F2R 0x0942042
#define HB_INPUT_CH_LAYOUT_3F4R 0x0a34034
#define HB_INPUT_CH_LAYOUT_HAS_LFE 0x0000100
#define HB_AMIXDOWN_MONO 0x01000001
// DCA_FORMAT of DCA_MONO = 0 = 0x000
// A52_FORMAT of A52_MONO = 1 = 0x01
// discrete channel count of 1
#define HB_AMIXDOWN_STEREO 0x02002022
// DCA_FORMAT of DCA_STEREO = 2 = 0x002
// A52_FORMAT of A52_STEREO = 2 = 0x02
// discrete channel count of 2
#define HB_AMIXDOWN_DOLBY 0x042070A2
// DCA_FORMAT of DCA_3F1R | DCA_OUT_DPLI = 519 = 0x207
// A52_FORMAT of A52_DOLBY = 10 = 0x0A
// discrete channel count of 2
#define HB_AMIXDOWN_DOLBYPLII 0x084094A2
// DCA_FORMAT of DCA_3F2R | DCA_OUT_DPLII = 1033 = 0x409
// A52_FORMAT of A52_DOLBY | A52_USE_DPLII = 74 = 0x4A
// discrete channel count of 2
#define HB_AMIXDOWN_6CH 0x10089176
// DCA_FORMAT of DCA_3F2R | DCA_LFE = 137 = 0x089
// A52_FORMAT of A52_3F2R | A52_LFE = 23 = 0x17
// discrete channel count of 6
typedef float hb_sample_t;
typedef struct
{
int chan_map[10][2][8];
int inv_chan_map[10][2][8];
} hb_chan_map_t;
typedef struct
{
int mode_in;
int mode_out;
int nchans_in;
int nchans_out;
hb_sample_t matrix[8][8];
int matrix_initialized;
hb_sample_t clev;
hb_sample_t slev;
hb_sample_t level;
hb_sample_t bias;
hb_chan_map_t map_in;
hb_chan_map_t map_out;
int center;
int left_surround;
int right_surround;
int rear_left_surround;
int rear_right_surround;
} hb_downmix_t;
// For convenience, a map to convert smpte channel layout
// to QuickTime channel layout.
// Map Indicies are mode, lfe, channel respectively
extern hb_chan_map_t hb_smpte_chan_map;
extern hb_chan_map_t hb_ac3_chan_map;
extern hb_chan_map_t hb_qt_chan_map;
hb_downmix_t * hb_downmix_init(int layout, int mixdown);
void hb_downmix_close( hb_downmix_t **downmix );
int hb_downmix_set_mode( hb_downmix_t * downmix, int layout, int mixdown );
void hb_downmix_set_level( hb_downmix_t * downmix, hb_sample_t clev, hb_sample_t slev, hb_sample_t level );
void hb_downmix_adjust_level( hb_downmix_t * downmix );
void hb_downmix_set_bias( hb_downmix_t * downmix, hb_sample_t bias );
void hb_downmix_set_chan_map(
hb_downmix_t * downmix,
hb_chan_map_t * map_in,
hb_chan_map_t * map_out );
void hb_downmix( hb_downmix_t * downmix, hb_sample_t * dst, hb_sample_t * src, int nsamples);
void hb_layout_remap(
hb_chan_map_t * map_in,
hb_chan_map_t * map_out,
int layout,
hb_sample_t * samples,
int nsamples );
int hb_need_downmix( int layout, int mixdown );
#endif /* DOWNMIX_H */
+94
View File
@@ -0,0 +1,94 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is MPEG4IP.
*
* The Initial Developer of the Original Code is Cisco Systems Inc.
* Portions created by Cisco Systems Inc. are
* Copyright (C) Cisco Systems Inc. 2001-2002. All Rights Reserved.
*
* Contributor(s):
* Dave Mackie dmackie@cisco.com
*/
#include "mbs.h"
void CMemoryBitstream::AllocBytes(u_int32_t numBytes)
{
m_pBuf = (u_int8_t*)calloc(numBytes, 1);
if (!m_pBuf) {
throw ENOMEM;
}
m_alloced = true;
m_bitPos = 0;
m_numBits = numBytes << 3;
}
void CMemoryBitstream::SetBytes(u_int8_t* pBytes, u_int32_t numBytes)
{
m_pBuf = pBytes;
m_bitPos = 0;
m_numBits = numBytes << 3;
}
void CMemoryBitstream::PutBytes(u_int8_t* pBytes, u_int32_t numBytes)
{
u_int32_t numBits = numBytes << 3;
if (numBits + m_bitPos > m_numBits) {
throw EIO;
}
if ((m_bitPos & 7) == 0) {
memcpy(&m_pBuf[m_bitPos >> 3], pBytes, numBytes);
m_bitPos += numBits;
} else {
for (u_int32_t i = 0; i < numBytes; i++) {
PutBits(pBytes[i], 8);
}
}
}
void CMemoryBitstream::PutBits(u_int32_t bits, u_int32_t numBits)
{
if (numBits + m_bitPos > m_numBits) {
throw EIO;
}
if (numBits > 32) {
throw EIO;
}
for (int8_t i = numBits - 1; i >= 0; i--) {
m_pBuf[m_bitPos >> 3] |= ((bits >> i) & 1) << (7 - (m_bitPos & 7));
m_bitPos++;
}
}
u_int32_t CMemoryBitstream::GetBits(u_int32_t numBits)
{
if (numBits + m_bitPos > m_numBits) {
throw EIO;
}
if (numBits > 32) {
throw EIO;
}
u_int32_t bits = 0;
for (u_int8_t i = 0; i < numBits; i++) {
bits <<= 1;
bits |= (m_pBuf[m_bitPos >> 3] >> (7 - (m_bitPos & 7))) & 1;
m_bitPos++;
}
return bits;
}
+96
View File
@@ -0,0 +1,96 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is MPEG4IP.
*
* The Initial Developer of the Original Code is Cisco Systems Inc.
* Portions created by Cisco Systems Inc. are
* Copyright (C) Cisco Systems Inc. 2001-2002. All Rights Reserved.
*
* Contributor(s):
* Dave Mackie dmackie@cisco.com
*/
#include "mp4v2.h"
#include <assert.h>
#include <ctype.h> /* isdigit, isprint, isspace */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#ifndef __MBS_INCLUDED__
#define __MBS_INCLUDED__
class CMemoryBitstream {
public:
CMemoryBitstream() {
m_pBuf = NULL;
m_bitPos = 0;
m_numBits = 0;
m_alloced = false;
}
~CMemoryBitstream(void) {
if (m_alloced) free(m_pBuf);
}
void AllocBytes(u_int32_t numBytes);
void SetBytes(u_int8_t* pBytes, u_int32_t numBytes);
void PutBytes(u_int8_t* pBytes, u_int32_t numBytes);
void PutBits(u_int32_t bits, u_int32_t numBits);
u_int32_t GetBits(u_int32_t numBits);
void SkipBytes(u_int32_t numBytes) {
SkipBits(numBytes << 3);
}
void SkipBits(u_int32_t numBits) {
SetBitPosition(GetBitPosition() + numBits);
}
u_int32_t GetBitPosition() {
return m_bitPos;
}
void SetBitPosition(u_int32_t bitPos) {
if (bitPos > m_numBits) {
throw;
}
m_bitPos = bitPos;
}
u_int8_t* GetBuffer() {
m_alloced = false;
return m_pBuf;
}
u_int32_t GetNumberOfBytes() {
return (GetNumberOfBits() + 7) / 8;
}
u_int32_t GetNumberOfBits() {
return m_numBits;
}
protected:
u_int8_t* m_pBuf;
u_int32_t m_bitPos;
u_int32_t m_numBits;
bool m_alloced;
};
#endif /* __MBS_INCLUDED__ */
+207
View File
@@ -0,0 +1,207 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is MPEG4IP.
*
* The Initial Developer of the Original Code is Cisco Systems Inc.
* Portions created by Cisco Systems Inc. are
* Copyright (C) Cisco Systems Inc. 2001-2005. All Rights Reserved.
*
* Contributor(s):
* Bill May wmay@cisco.com
*/
#ifndef __BITSTREAM_H__
#define __BITSTREAM_H__ 1
//#include "mpeg4ip.h"
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
typedef enum BitstreamErr_t {
BITSTREAM_TOO_MANY_BITS,
BITSTREAM_PAST_END,
} BitstreamErr_t;
class CBitstream {
public:
CBitstream(void) { m_verbose = 0;};
CBitstream(const uint8_t *buffer, uint32_t bit_len) {
m_verbose = 0;
init(buffer, bit_len);
};
~CBitstream (void) {};
void init(const uint8_t *buffer, uint32_t bit_len) {
m_chDecBuffer = buffer;
m_chDecBufferSize = bit_len;
m_bBookmarkOn = 0;
m_uNumOfBitsInBuffer = 0;
};
void init(const char *buffer, uint32_t bit_len) {
init((const uint8_t *)buffer, (uint32_t)bit_len);
};
void init(const char *buffer, int bit_len) {
init((const uint8_t *)buffer, (uint32_t)bit_len);
};
void init(const uint8_t *buffer, int bit_len) {
init(buffer, (uint32_t)bit_len);
};
void init(const char *buffer, unsigned short bit_len) {
init((const uint8_t *)buffer, (uint32_t)bit_len);
};
void init(const uint8_t *buffer, unsigned short bit_len) {
init(buffer, (uint32_t)bit_len);
};
uint32_t GetBits(uint32_t numBits) {
uint32_t retData;
static const uint32_t msk[33] = {
0x00000000, 0x00000001, 0x00000003, 0x00000007,
0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
0xffffffff
};
if (numBits > 32) {
throw BITSTREAM_TOO_MANY_BITS;
}
if (numBits == 0) {
return 0;
}
if (m_uNumOfBitsInBuffer >= numBits) { // don't need to read from FILE
m_uNumOfBitsInBuffer -= numBits;
retData = m_chDecData >> m_uNumOfBitsInBuffer;
// wmay - this gets done below...retData &= msk[numBits];
} else {
uint32_t nbits;
nbits = numBits - m_uNumOfBitsInBuffer;
if (nbits == 32)
retData = 0;
else
retData = m_chDecData << nbits;
switch ((nbits - 1) / 8) {
case 3:
nbits -= 8;
if (m_chDecBufferSize < 8) throw BITSTREAM_PAST_END;
retData |= *m_chDecBuffer++ << nbits;
m_chDecBufferSize -= 8;
// fall through
case 2:
nbits -= 8;
if (m_chDecBufferSize < 8) throw BITSTREAM_PAST_END;
retData |= *m_chDecBuffer++ << nbits;
m_chDecBufferSize -= 8;
case 1:
nbits -= 8;
if (m_chDecBufferSize < 8) throw BITSTREAM_PAST_END;
retData |= *m_chDecBuffer++ << nbits;
m_chDecBufferSize -= 8;
case 0:
break;
}
if (m_chDecBufferSize < nbits) {
throw BITSTREAM_PAST_END;
}
m_chDecData = *m_chDecBuffer++;
m_uNumOfBitsInBuffer = MIN(8, m_chDecBufferSize) - nbits;
m_chDecBufferSize -= MIN(8, m_chDecBufferSize);
retData |= (m_chDecData >> m_uNumOfBitsInBuffer) & msk[nbits];
}
if (m_verbose)
printf("bits %d value %x\n", numBits, retData&msk[numBits]);
return (retData & msk[numBits]);
};
int getbits(uint32_t bits, uint32_t *retvalue) {
try {
*retvalue = GetBits(bits);
} catch (...) {
return -1;
}
return 0;
}
int peekbits(uint32_t bits, uint32_t *retvalue) {
int ret;
bookmark(1);
ret = getbits(bits, retvalue);
bookmark(0);
return (ret);
}
uint32_t PeekBits(uint32_t bits) {
uint32_t ret;
bookmark(1);
ret = GetBits(bits);
bookmark(0);
return ret;
}
void bookmark(int bSet) {
if (m_verbose) {
printf("bookmark\n");
}
if (bSet) {
m_uNumOfBitsInBuffer_bookmark = m_uNumOfBitsInBuffer;
m_chDecBuffer_bookmark = m_chDecBuffer;
m_chDecBufferSize_bookmark = m_chDecBufferSize;
m_bBookmarkOn = 1;
m_chDecData_bookmark = m_chDecData;
} else {
m_uNumOfBitsInBuffer = m_uNumOfBitsInBuffer_bookmark;
m_chDecBuffer = m_chDecBuffer_bookmark;
m_chDecBufferSize = m_chDecBufferSize_bookmark;
m_chDecData = m_chDecData_bookmark;
m_bBookmarkOn = 0;
}
};
int bits_remain (void) {
return m_chDecBufferSize + m_uNumOfBitsInBuffer;
};
int byte_align(void) {
int temp = 0;
if (m_uNumOfBitsInBuffer != 0) {
temp = GetBits(m_uNumOfBitsInBuffer);
#if 0
temp = m_uNumOfBitsInBuffer;
m_uNumOfBitsInBuffer = 0;
m_chDecBuffer++;
m_chDecBufferSize -= MIN(m_chDecBufferSize,8);
#endif
} else {
// if we are byte aligned, check for 0x7f value - this will indicate
// we need to skip those bits
uint8_t readval;
readval = PeekBits(8);
if (readval == 0x7f) {
GetBits(8);
}
}
return (temp);
};
void set_verbose(int verbose) { m_verbose = verbose; };
private:
uint32_t m_uNumOfBitsInBuffer;
const uint8_t *m_chDecBuffer;
uint8_t m_chDecData, m_chDecData_bookmark;
uint32_t m_chDecBufferSize;
int m_bBookmarkOn;
uint32_t m_uNumOfBitsInBuffer_bookmark;
const uint8_t *m_chDecBuffer_bookmark;
uint32_t m_chDecBufferSize_bookmark;
int m_verbose;
};
#endif