3
Copyright (C) 2000 Martin Vogt
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU Library General Public License as published by
7
the Free Software Foundation.
9
For more information look at the file COPYRIGHT in this package
14
#include "mpegVideoBitWindow.h"
19
MpegVideoBitWindow::MpegVideoBitWindow() {
20
// Make buffer length multiple of 4.
26
if ((size % 4) != 0) {
27
cout << "MpegVideoStream not multiple of 4"<<endl;
31
/* Create MpegVideoStream. */
32
buf_start = (unsigned int*) malloc (sizeof(unsigned int)*(size*4));
35
* Set max_buf_length to one less than actual length to deal with messy
36
* data without proper seq. end codes.
38
max_buf_length=size-1;
41
/* Initialize bitstream i/o fields. */
53
nBitMask[0] = 0x00000000;
54
nBitMask[1] = 0x80000000;
55
nBitMask[2] = 0xc0000000;
56
nBitMask[3] = 0xe0000000;
57
nBitMask[4] = 0xf0000000;
58
nBitMask[5] = 0xf8000000;
59
nBitMask[6] = 0xfc000000;
60
nBitMask[7] = 0xfe000000;
61
nBitMask[8] = 0xff000000;
62
nBitMask[9] = 0xff800000;
63
nBitMask[10] = 0xffc00000;
64
nBitMask[11] = 0xffe00000;
65
nBitMask[12] = 0xfff00000;
66
nBitMask[13] = 0xfff80000;
67
nBitMask[14] = 0xfffc0000;
68
nBitMask[15] = 0xfffe0000;
69
nBitMask[16] = 0xffff0000;
70
nBitMask[17] = 0xffff8000;
71
nBitMask[18] = 0xffffc000;
72
nBitMask[19] = 0xffffe000;
73
nBitMask[20] = 0xfffff000;
74
nBitMask[21] = 0xfffff800;
75
nBitMask[22] = 0xfffffc00;
76
nBitMask[23] = 0xfffffe00;
77
nBitMask[24] = 0xffffff00;
78
nBitMask[25] = 0xffffff80;
79
nBitMask[26] = 0xffffffc0;
80
nBitMask[27] = 0xffffffe0;
81
nBitMask[28] = 0xfffffff0;
82
nBitMask[29] = 0xfffffff8;
83
nBitMask[30] = 0xfffffffc;
84
nBitMask[31] = 0xfffffffe;
85
nBitMask[32] = 0xffffffff;
91
MpegVideoBitWindow::~MpegVideoBitWindow() {
97
int MpegVideoBitWindow::appendToBuffer(unsigned char* ptr,int len) {
98
int byte_length = getLength()*4;
104
byte_length += num_left;
105
*(buffer+buf_length)=leftover_bytes;
107
memcpy(((unsigned char *)buffer)+byte_length,ptr,len);
109
if ((unsigned long int)1 != ntohl(1)) {
110
unsigned int *mark = buffer+buf_length;
112
int n=(len+num_left)&0xfffffffc;
113
for (i=0; i < n; i+=4) {
118
byte_length = byte_length + len;
119
num_left = byte_length % 4;
120
buf_length = byte_length / 4;
123
leftover_bytes = *(buffer +buf_length);
127
int MpegVideoBitWindow::getLinearFree() {
128
unsigned int* endPos=buf_start+size;
129
unsigned int* currPos=buffer+buf_length;
131
int back=endPos-currPos;
139
void MpegVideoBitWindow::flushByteOffset() {
142
byteoff = bit_offset % 8;
145
flushBitsDirect((8-byteoff));
150
void MpegVideoBitWindow::appendToBuffer(unsigned int startCode) {
151
unsigned int startCodeRaw=htonl(startCode);
153
appendToBuffer((unsigned char*)&startCodeRaw,4);
157
void MpegVideoBitWindow::clear() {
165
int MpegVideoBitWindow::getLength() {
175
void MpegVideoBitWindow::printChar(int bytes) {
179
mark=(unsigned char *)buffer;
181
for(i=0;i<bytes;i++) {
182
printf("i:%d read=%x\n",i,mark[i]);
184
printf("*********\n");
189
void MpegVideoBitWindow::printInt(int bytes) {
194
mark=(unsigned int*)buf_start;
198
printf("i:%d read=%x\n",i,mark[i]);
200
printf("*********\n");
205
void MpegVideoBitWindow::print() {
206
int byte_length = getLength()*4;
208
printf("bit_offset:%x\n",bit_offset);
209
printf("num_left:%x\n",num_left);
210
printf("leftover_bytes:%x\n",leftover_bytes);
211
printf("buf_length:%x\n",buf_length);
212
printf("curBits:%x\n",curBits);
213
printf("pos:%8x\n",byte_length);
218
void MpegVideoBitWindow::resizeBuffer(int insertBytes) {
219
/* Read all the headers, now make room for packet */
220
if (buf_start+max_buf_length < buffer+insertBytes/4+buf_length) {
222
if (max_buf_length - buf_length < (int) insertBytes/4) {
224
/* Buffer too small for a packet (plus whats there),
225
* time to enlarge it!
228
unsigned int *old = buf_start;
229
max_buf_length=buf_length+insertBytes/4+1;
231
buf_start=(unsigned int*) malloc(sizeof(unsigned int)*max_buf_length);
232
if (buf_start == NULL) {
233
cout << "allocation of:"<<max_buf_length<<" bytes failed"<<endl;
236
memcpy((unsigned char *)buf_start,buffer,(unsigned int)buf_length*4);
239
cout << "enlarge buffer-1 end***********"<<endl;
242
memcpy((unsigned char *)buf_start,
243
buffer, (unsigned int) buf_length*4);
250
void MpegVideoBitWindow::fillWithIsoEndCode(int bytes) {
254
appendToBuffer(ISO_11172_END_CODE);