void CJpeg::LoadJPG(LPCSTR FileName) { // File Open & Load Entire Contents // HFILE hFile = _lopen(FileName, OF_READWRITE); int FileSize = GetFileSize((HANDLE)hFile, NULL); m_pBuf = new BYTE[FileSize]; _lread(hFile, m_pBuf, FileSize); _lclose(hFile); m_Index = 0; int Count=0; FindSOI(); pByte = &m_pBuf[m_Index]; while(TRUE) { FindETC(); // DRI(Define Restart Interval) FindDHT(); // Huffman Table Loading FindDQT(); // Quantization Table Loading FindSOF(); // Frame Header Loading FindSOS(); // Scan Header Loading & Decoding if( (*pByte == 0xff) && (*(pByte+1) == 0xd9) ) // 끝을 만났을 때 break; Count++; if(Count > 50) // Loop가 끝날 가능성이 거의 없을 때 break; } delete [] m_pBuf; }
ssize_t CamBufferingDeframer::Write(const void *buffer, size_t size) { uint8 *b; int l; int i, s, e; int which; fMinFrameSize = fDevice->MinRawFrameSize(); fMaxFrameSize = fDevice->MaxRawFrameSize(); IB.Write(buffer, size); b = (uint8 *)IB.Buffer(); l = IB.BufferLength(); PRINT((CH "(%p, %d), IB: %d" CT, buffer, size, IB.BufferLength())); if (l < (int)(fMinFrameSize + fSkipSOFTags + fSkipEOFTags)) return size; // not enough data anyway if (!fCurrentFrame) { BAutolock l(fLocker); if (fFrames.CountItems() < MAXFRAMEBUF) fCurrentFrame = AllocFrame(); else { PRINT((CH "DROPPED %d bytes! (too many queued frames)" CT, size)); return size; // drop XXX } } for (s = 0; (l - s > (int)fMinFrameSize) && ((i = FindSOF(b + s, l - fMinFrameSize - s, &which)) > -1); s++) { s += i; if ((int)(s + fSkipSOFTags + fMinFrameSize + fSkipEOFTags) > l) break; if (!fDevice->ValidateStartOfFrameTag(b + s, fSkipSOFTags)) continue; PRINT((CH ": SOF[%d] at offset %d" CT, which, s)); PRINT((CH ": SOF: ... %02x %02x %02x %02x %02x %02x" CT, b[s+6], b[s+7], b[s+8], b[s+9], b[s+10], b[s+11])); for (e = s + fSkipSOFTags + fMinFrameSize; ((e <= (int)(s + fSkipSOFTags + fMaxFrameSize)) && (e < l) && ((i = 0*FindEOF(b + e, l - e, &which)) > -1)); e++) { e += i; //PRINT((CH ": EOF[%d] at offset %d" CT, which, s)); if (!fDevice->ValidateEndOfFrameTag(b + e, fSkipEOFTags, e - s - fSkipSOFTags)) continue; PRINT((CH ": SOF= ... %02x %02x %02x %02x %02x %02x" CT, b[s+6], b[s+7], b[s+8], b[s+9], b[s+10], b[s+11])); // we have one! s += fSkipSOFTags; // fill it fCurrentFrame->Write(b + s, e - s); // queue it BAutolock f(fLocker); PRINT((CH ": Detaching a frame (%d bytes, %d to %d / %d)" CT, (size_t)fCurrentFrame->Position(), s, e, l)); fCurrentFrame->Seek(0LL, SEEK_SET); fFrames.AddItem(fCurrentFrame); release_sem(fFrameSem); // next Write() will allocate a new one fCurrentFrame = NULL; // discard the frame and everything before it. DiscardFromInput(e + fSkipEOFTags); return size; } } return size; }
ssize_t CamStreamingDeframer::Write(const void *buffer, size_t size) { int i = -1; int j; int end = size; int which; const uint8 *buf = (const uint8 *)buffer; int bufsize = size; bool detach = false; bool discard = false; //PRINT((CH "(%p, %d); state=%s framesz=%u queued=%u" CT, buffer, size, (fState==ST_SYNC)?"sync":"frame", (size_t)(fCurrentFrame?(fCurrentFrame->Position()):-1), (size_t)fInputBuff.Position())); if (!fCurrentFrame) { BAutolock l(fLocker); if (fFrames.CountItems() < MAXFRAMEBUF) fCurrentFrame = AllocFrame(); else { PRINT((CH "DROPPED %d bytes! (too many queued frames)" CT, size)); return size; // drop XXX } } // update in case resolution changed fMinFrameSize = fDevice->MinRawFrameSize(); fMaxFrameSize = fDevice->MaxRawFrameSize(); if (fInputBuff.Position()) { // residual data ? append to it fInputBuff.Write(buffer, size); // and use it as input buf buf = (uint8 *)fInputBuff.Buffer(); bufsize = fInputBuff.BufferLength(); end = bufsize; } // whole buffer belongs to a frame, simple if ((fState == ST_FRAME) && (fCurrentFrame->Position() + bufsize < fMinFrameSize)) { // no residual data, and fCurrentFrame->Write(buf, bufsize); fInputBuff.Seek(0LL, SEEK_SET); fInputBuff.SetSize(0); return size; } // waiting for a frame... if (fState == ST_SYNC) { i = 0; while ((j = FindSOF(buf+i, bufsize-i, &which)) > -1) { i += j; if (fDevice->ValidateStartOfFrameTag(buf+i, fSkipSOFTags)) break; i++; } // got one if (j >= 0) { PRINT((CH ": SOF[%d] at offset %d" CT, which, i)); //PRINT((CH ": SOF: ... %02x %02x %02x %02x %02x %02x" CT, buf[i+6], buf[i+7], buf[i+8], buf[i+9], buf[i+10], buf[i+11])); int start = i + fSkipSOFTags; buf += start; bufsize -= start; end = bufsize; fState = ST_FRAME; } } // check for end of frame if (fState == ST_FRAME) { #if 0 int j, k; i = -1; k = 0; while ((j = FindEOF(buf + k, bufsize - k, &which)) > -1) { k += j; //PRINT((CH "| EOF[%d] at offset %d; pos %Ld" CT, which, k, fCurrentFrame->Position())); if (fCurrentFrame->Position()+k >= fMinFrameSize) { i = k; break; } k++; if (k >= bufsize) break; } #endif #if 1 i = 0; if (fCurrentFrame->Position() < fMinFrameSize) { if (fCurrentFrame->Position() + bufsize >= fMinFrameSize) i = (fMinFrameSize - (size_t)fCurrentFrame->Position()); else i = bufsize; } PRINT((CH ": checking for EOF; bufsize=%d i=%d" CT, bufsize, i)); if (i + (int)fSkipEOFTags > bufsize) { // not enough room to check for EOF, leave it for next time end = i; i = -1; // don't detach yet } else { PRINT((CH ": EOF? %02x [%02x %02x %02x %02x] %02x" CT, buf[i-1], buf[i], buf[i+1], buf[i+2], buf[i+3], buf[i+4])); while ((j = FindEOF(buf + i, bufsize - i, &which)) > -1) { i += j; PRINT((CH "| EOF[%d] at offset %d; pos %Ld" CT, which, i, fCurrentFrame->Position())); if (fCurrentFrame->Position()+i >= fMaxFrameSize) { // too big: discard //i = -1; discard = true; break; } if (fDevice->ValidateEndOfFrameTag(buf+i, fSkipEOFTags, fCurrentFrame->Position()+i)) break; i++; if (i >= bufsize) { i = -1; break; } } if (j < 0) i = -1; } #endif if (i >= 0) { PRINT((CH ": EOF[%d] at offset %d" CT, which, i)); end = i; detach = true; } PRINT((CH ": writing %d bytes" CT, end)); if (end <= bufsize) fCurrentFrame->Write(buf, end); if (fCurrentFrame->Position() > fMaxFrameSize) { fCurrentFrame->SetSize(fMaxFrameSize); detach = true; } if (detach) { BAutolock f(fLocker); PRINT((CH ": Detaching a frame (%d bytes, end = %d, )" CT, (size_t)fCurrentFrame->Position(), end)); fCurrentFrame->Seek(0LL, SEEK_SET); if (discard) { delete fCurrentFrame; } else { fFrames.AddItem(fCurrentFrame); release_sem(fFrameSem); } fCurrentFrame = NULL; if (fFrames.CountItems() < MAXFRAMEBUF) { fCurrentFrame = AllocFrame(); } fState = ST_SYNC; } } // put the remainder in input buff, discarding old data #if 0 fInputBuff.Seek(0LL, SEEK_SET); if (bufsize - end > 0) fInputBuff.Write(buf+end, bufsize - end); #endif BMallocIO m; m.Write(buf+end, bufsize - end); fInputBuff.Seek(0LL, SEEK_SET); if (bufsize - end > 0) fInputBuff.Write(m.Buffer(), bufsize - end); fInputBuff.SetSize(bufsize - end); return size; }