Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1407 lines
42 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // INTEL Corporation Proprietary Information
  3. // This listing is supplied under the terms of a license agreement with Intel
  4. // Corporation and many not be copied nor disclosed except in accordance
  5. // with the terms of that agreement.
  6. // Copyright (c) 1995, 1996 Intel Corporation.
  7. //
  8. //
  9. // Module Name: h261rcv.cpp
  10. // Environment: MSVC 4.0, OLE 2
  11. /////////////////////////////////////////////////////////////////////////////////
  12. // $Header: J:\rtp\src\ppm\h261rcv.cpv 1.43 29 May 1997 16:42:08 lscline $
  13. #include "ppmerr.h"
  14. #include "h261rcv.h"
  15. #include "ippmcb.h"
  16. #define H261_HDR_T int // for now
  17. //////////////////////////////////////////////////////////////////////////////
  18. // Private global data
  19. #ifdef REBUILD_EXBITSTREAM
  20. static const struct { DWORD dw[2]; } s_leadFragBitPatternQCIF =
  21. {
  22. MAKELONG(MAKEWORD(0, 1), MAKEWORD(0, 0)), // 4 bytes {0,1, 0,0}, big-endian
  23. MAKELONG(MAKEWORD(0, 1), MAKEWORD(16, 0)) // 4 bytes {0,1,16,0}, big-endian
  24. };
  25. static const struct { DWORD dw[2]; } s_leadFragBitPatternCIF =
  26. {
  27. MAKELONG(MAKEWORD(0, 1), MAKEWORD(0, 40)), // 4 bytes {0,1, 0,40}, big-endian
  28. MAKELONG(MAKEWORD(0, 1), MAKEWORD(16, 0)) // 4 bytes {0,1,16, 0}, big-endian
  29. };
  30. static const struct { char ch[4]; } s_nonLeadFragBitPattern = {0, 0, 0, 0};
  31. #endif
  32. /////////////////////////////////////////////////////////////////////////////
  33. // H.261 utility function
  34. /////////////////////////////////////////////////////////////////////////////
  35. // getH261payloadType(): If successful, returns the payload type (QCIF or CIF)
  36. // of H.261 buffer, otherwise returns unknown.
  37. /////////////////////////////////////////////////////////////////////////////
  38. // inline
  39. RTPh261SourceFormat getH261payloadType(const void* pBuffer)
  40. {
  41. CBitstream bitstream(pBuffer);
  42. // PSC
  43. DWORD uResult = bitstream.getFixedBits(BITS_PICTURE_STARTCODE);
  44. for (
  45. int iLookAhead = 0;
  46. (uResult != PSC_VALUE)
  47. && (iLookAhead <= MAX_LOOKAHEAD_NUMBER);
  48. ++ iLookAhead)
  49. {
  50. bitstream.getNextBit(uResult);
  51. uResult &= GetBitsMask(BITS_PICTURE_STARTCODE);
  52. }
  53. if (uResult != PSC_VALUE)
  54. {
  55. return rtph261SourceFormatUnknown;
  56. }
  57. // Ignore BITS_TR (Temporal Reference)
  58. bitstream.getFixedBits(BITS_TR);
  59. // Ignore 3 bits of PTYPE (Picture Type)
  60. bitstream.getFixedBits(3);
  61. // Return source format. Only one bit, can't be invalid, unless we run
  62. // out of buffer, which isn't checked for here.
  63. return (RTPh261SourceFormat) bitstream.getFixedBits(1);
  64. }
  65. //////////////////////////////////////////////////////////////////////////////
  66. // H261_ppmReceive implementation
  67. H261_ppmReceive::H261_ppmReceive(IUnknown* pUnkOuter, IUnknown** ppUnkInner)
  68. : ppmReceive(H261_PT, H261_BUFFER_SIZE, sizeof(unsigned long), pUnkOuter, ppUnkInner)
  69. {
  70. m_rtph261SourceFormat = rtph261SourceFormatUnknown;
  71. m_GlobalLastMarkerBitIn = FALSE;
  72. #ifdef REBUILD_EXBITSTREAM
  73. m_ExtendedBitstream = TRUE;
  74. #endif
  75. // Allocate memory for the H261 headers;
  76. // Note: We do not use ReadProfileHeaderSize for this, since this header structure
  77. // contains padding so it can be in a FreeList; here we use the real padded size
  78. HRESULT hr;
  79. m_pH261Headers = new FreeList(
  80. FREELIST_INIT_COUNT_RCV,
  81. sizeof(H261_Header),
  82. FREELIST_HIGH_WATER_MARK,
  83. FREELIST_INCREMENT,
  84. &hr); // Not really used here
  85. if (! m_pH261Headers)
  86. {
  87. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::H261_ppmReceive: ERROR - m_pH261Headers == NULL"));
  88. }
  89. // Verify assumption made in H261_ppmReceive::BuildExtendedBitstream() wrt
  90. // handling of underflow.
  91. DWORD dwBufSize = 0;
  92. int nLastEbit = 0;
  93. ASSERT(
  94. (((dwBufSize - 1) * BITSPERBYTE) +
  95. (BITSPERBYTE - nLastEbit)) ==
  96. (BITSPERBYTE * dwBufSize));
  97. }
  98. H261_ppmReceive::~H261_ppmReceive()
  99. {
  100. if (m_pH261Headers) {
  101. delete m_pH261Headers;
  102. m_pH261Headers = NULL;
  103. }
  104. }
  105. IMPLEMENT_CREATEPROC(H261_ppmReceive);
  106. //////////////////////////////////////////////////////////////////////////////
  107. // ppmReceive Functions (Overrides)
  108. //////////////////////////////////////////////////////////////////////////////
  109. #ifdef REBUILD_EXBITSTREAM
  110. //////////////////////////////////////////////////////////////////////////////////////////
  111. // SetSession:
  112. //////////////////////////////////////////////////////////////////////////////////////////
  113. STDMETHODIMP H261_ppmReceive::SetSession(PPMSESSPARAM_T* pSessparam)
  114. {
  115. // ccp - note unsafe downcast
  116. m_ExtendedBitstream = ((H26XPPMSESSPARAM_T*) pSessparam)->ExtendedBitstream;
  117. return ppmReceive::SetSession(pSessparam);
  118. }
  119. //////////////////////////////////////////////////////////////////////////////
  120. // IPPMReceiveSession Functions (Overrides)
  121. //////////////////////////////////////////////////////////////////////////////
  122. ////////////////////////////////////////////////////////////////////////////////////////
  123. //GetResiliency: Gets the boolean for whether resiliency is on or off
  124. ////////////////////////////////////////////////////////////////////////////////////////
  125. STDMETHODIMP H261_ppmReceive::GetResiliency(LPBOOL lpbResiliency)
  126. {
  127. if (!lpbResiliency) return E_POINTER;
  128. *lpbResiliency = m_ExtendedBitstream;
  129. return NOERROR;
  130. }
  131. ////////////////////////////////////////////////////////////////////////////////////////
  132. //SetResiliency: Sets the boolean for whether resiliency is on or off
  133. ////////////////////////////////////////////////////////////////////////////////////////
  134. STDMETHODIMP H261_ppmReceive::SetResiliency(BOOL pbResiliency)
  135. {
  136. m_ExtendedBitstream = pbResiliency;
  137. return NOERROR;
  138. }
  139. #endif
  140. //////////////////////////////////////////////////////////////////////////////////////////
  141. // TimeToProcessMessages:
  142. //////////////////////////////////////////////////////////////////////////////////////////
  143. BOOL H261_ppmReceive::TimeToProcessMessages(FragDescriptor* pFragDescrip, MsgHeader* pMsgHdr)
  144. {
  145. return (pMsgHdr == m_pMsgHeadersHead);
  146. }
  147. //////////////////////////////////////////////////////////////////////////////////////////
  148. // CheckMessageComplete:
  149. //////////////////////////////////////////////////////////////////////////////////////////
  150. BOOL H261_ppmReceive::CheckMessageComplete(MsgHeader* pMsgHdr)
  151. {
  152. // if there is no header then return false.
  153. if (pMsgHdr == NULL)
  154. {
  155. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::CheckMessageComplete: ERROR - pMsgHdr == NULL"));
  156. return FALSE;
  157. }
  158. // should there be a critical section in this function.
  159. // check to make sure we have the first packet in the message.
  160. if (pMsgHdr->m_pPrev == NULL) // if first message in list, then look at a variable
  161. {
  162. if (m_GlobalLastSeqNum != pMsgHdr->m_pFragList->FirstSeqNum()-1)
  163. {
  164. return FALSE;
  165. }
  166. }
  167. else
  168. {
  169. if (pMsgHdr->m_pPrev->m_pFragList->LastSeqNum() != pMsgHdr->m_pFragList->FirstSeqNum()-1)
  170. {
  171. return FALSE;
  172. }
  173. }
  174. // check to make sure we have the last packet in the message.
  175. // For IETF compliance, marker bit must be set, but in H.225, the marker bit
  176. // may optionally be omitted if setting it could cause additional end-to-end delay
  177. // Thus, we check for the marker bit, but if it is not present, we also check to
  178. // see if the next packet is in and has a different timestamp (ala generic).
  179. if (! pMsgHdr->m_MarkerBitIn)
  180. {
  181. if (pMsgHdr->m_pNext == NULL) // if we don't have the next message,
  182. { // we don't know if the current message
  183. // is done.
  184. return FALSE;
  185. }
  186. if (pMsgHdr->m_pNext->m_pFragList->FirstSeqNum() !=
  187. pMsgHdr->m_pFragList->LastSeqNum() + 1)
  188. {
  189. return FALSE;
  190. }
  191. }
  192. // Check for a packet missing in the middle->
  193. if ((int)pMsgHdr->m_pFragList->SeqNumSpan() != pMsgHdr->m_NumFragments)
  194. {
  195. return FALSE;
  196. }
  197. return TRUE;
  198. }
  199. //////////////////////////////////////////////////////////////////////////////////////////
  200. // PrepMessage: Sets H261 global variables, calls base PrepMessage. If any error
  201. // checks are added, you MUST make a call to LeaveCriticalSection.
  202. //////////////////////////////////////////////////////////////////////////////////////////
  203. HRESULT H261_ppmReceive::PrepMessage(BOOL Complete)
  204. {
  205. EnterCriticalSection(&m_CritSec);
  206. // Can't hurt to check although we should never get here if there is no head.
  207. if (m_pMsgHeadersHead == NULL)
  208. {
  209. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::PrepMessage: ERROR - m_pMsgHeadersHead == NULL"));
  210. LeaveCriticalSection(&m_CritSec);
  211. return PPMERR(PPM_E_CORRUPTED);
  212. }
  213. // Save marker bit flag.
  214. BOOL bMarkerBitIn = m_pMsgHeadersHead->m_MarkerBitIn;
  215. LeaveCriticalSection(&m_CritSec);
  216. HRESULT hErr = ppmReceive::PrepMessage(Complete);
  217. // Update the H261 global variable.
  218. m_GlobalLastMarkerBitIn = bMarkerBitIn;
  219. DBG_MSG(DBG_TRACE,
  220. ("H261_ppmReceive::PrepMessage: m_GlobalLastMarkerBitIn=%d",
  221. m_GlobalLastMarkerBitIn));
  222. return hErr;
  223. }
  224. #ifdef REBUILD_EXBITSTREAM
  225. //////////////////////////////////////////////////////////////////////////////////////////
  226. // setBSInfoTrailer(): Inline helper to build EBS trailer.
  227. //////////////////////////////////////////////////////////////////////////////////////////
  228. void
  229. setH261BSInfoTrailer(
  230. BSINFO_TRAILER& rBS_trailer,
  231. int iFrame,
  232. DWORD dwBufSize,
  233. int nNumFrags,
  234. RTPh261SourceFormat rtph261SourceFormat)
  235. {
  236. // complete info for the trailer
  237. rBS_trailer.dwVersion = H261_VERSION;
  238. rBS_trailer.dwUniqueCode = H261_CODE;
  239. rBS_trailer.dwFlags = 0;
  240. if (iFrame)
  241. rBS_trailer.dwFlags |= RTP_H26X_INTRA_CODED;
  242. rBS_trailer.dwCompressedSize = dwBufSize;
  243. rBS_trailer.dwNumberOfPackets = nNumFrags;
  244. // Note: We may send unknown source format here if frame PSC was corrupt
  245. // or missing, but we'll let the codec deal with that. Codec will
  246. // typically just toss the frame, but sending unknown might facilitate
  247. // more advanced error receover (i.e. attempting to deterimine source
  248. // format from frame content). This would be more likely in an off-line
  249. // (vs. real time) decoder. If codec doesn't support "unknown", it can
  250. // use source format indicated by PSC
  251. rBS_trailer.SourceFormat = rtph261SourceFormat;
  252. // For H.261, we just put zeroes in these fields
  253. rBS_trailer.TR = 0;
  254. rBS_trailer.TRB = 0;
  255. rBS_trailer.DBQ = 0;
  256. }
  257. #endif // REBUILD_EXBITSTREAM
  258. // lsc - Note: we are not rebuilding the extended bitstream
  259. // for complete frames.
  260. // For now I am assuming that we only rebuild the extended bitstream for frames that
  261. // have not received all packets (handled by PartialMessageHandler), and for complete
  262. // frames we hand them up as usual. However, this routine is overridden to handle the
  263. // data ORing for the byte overlap between packets.
  264. #pragma optimize ( "", off ) // BUGBUG: Work around complier bug
  265. //////////////////////////////////////////////////////////////////////////////////////////
  266. // DataCopy: Copies data fragments into client's buffer. If any error checks with returns
  267. // are added they MUST call LeaveCriticalSection.
  268. //
  269. //////////////////////////////////////////////////////////////////////////////////////////
  270. HRESULT H261_ppmReceive::DataCopy(MsgHeader* const pMsgHdr)
  271. {
  272. if (pMsgHdr == NULL)
  273. {
  274. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::DataCopy: ERROR - pMsgHdr == NULL"));
  275. return PPMERR(PPM_E_EMPTYQUE);
  276. }
  277. ASSERT(! pMsgHdr->m_pFragList->Is_Empty());
  278. MsgDescriptor* pMsgDesc = DequeueBuffer(1); // Get a buffer to hold the message.
  279. if (pMsgDesc == NULL)
  280. {
  281. FreeFragList(pMsgHdr);
  282. FreeMsgHeader(pMsgHdr);
  283. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::DataCopy: ERROR - Couldn't get a reassembly buffer"));
  284. // Make a callback into the app to let it know what happened.
  285. ppm::PPMNotification(PPM_E_DROPFRAME, SEVERITY_NORMAL, NULL, 0);
  286. m_GlobalLastFrameDropped = TRUE;
  287. return PPMERR(PPM_E_DROPFRAME);
  288. }
  289. EnterCriticalSection(&m_CritSec);
  290. #ifdef GIVE_SEQNUM
  291. // Do this before frag list exhausted below
  292. pMsgDesc->m_TimeStamp = pMsgHdr->m_pFragList->LastSeqNum();
  293. #else
  294. pMsgDesc->m_TimeStamp = pMsgHdr->GetMsgID();
  295. #endif
  296. // Get payload type from PSC for BuildExtendedBitstream(). Do this even
  297. // when not currently building the extended bitstream, since that mode can
  298. // be toggled during the session.
  299. RTPh261SourceFormat currentSourceFormat =
  300. getH261payloadType(
  301. ((FragDescriptor*) pMsgHdr->m_pFragList->GetFirst())->m_pData);
  302. if (currentSourceFormat != rtph261SourceFormatUnknown)
  303. // Remember valid format for use by next frame.
  304. m_rtph261SourceFormat = currentSourceFormat;
  305. #ifdef REBUILD_EXBITSTREAM
  306. BITSTREAM_INFO_H261* pEBS = NULL;
  307. int nNumFrags = 0, nCurrentEBS = 0, iFrame = 0;
  308. // Get local copy of extended bitstream flag to protect against
  309. // possible change via H261_ppmReceive::SetSession() while building
  310. // this frame. OK to change between frames.
  311. BOOL bExtendedBitstream = m_ExtendedBitstream;
  312. if (bExtendedBitstream)
  313. {
  314. nNumFrags = pMsgHdr->m_pFragList->SeqNumSpan();
  315. // allocate structures for the extended bitstream information
  316. pEBS = new BITSTREAM_INFO_H261[nNumFrags];
  317. if (pEBS == NULL)
  318. {
  319. FreeFragList(pMsgHdr);
  320. EnqueueBuffer(pMsgDesc);
  321. FreeMsgHeader(pMsgHdr);
  322. LeaveCriticalSection(&m_CritSec);
  323. DBG_MSG(DBG_ERROR,
  324. ("H261_ppmReceive::DataCopy: ERROR - memory allocation failure"));
  325. // Make a callback into the app to let it know what happened.
  326. ppm::PPMNotification(PPM_E_OUTOFMEMORY, SEVERITY_NORMAL, NULL, 0);
  327. m_GlobalLastFrameDropped = TRUE;
  328. return PPMERR(PPM_E_DROPFRAME);
  329. }
  330. memset(pEBS, 0, nNumFrags * sizeof(*pEBS));
  331. }
  332. #endif
  333. // Loop state variables
  334. LPBYTE pbCurrentOffset = (LPBYTE) pMsgDesc->m_pBuffer; // start copying into front of buffer.
  335. DWORD dwBufSize = 0;
  336. UCHAR chLastByte = 0;
  337. int nLastEbit = 0; // prior packet's ebit, for error checking
  338. // There are three cases to check for overlapping bytes between
  339. // packets:
  340. // 1) There is overlap between packets, and this is first packet.
  341. // 2) There is overlap between packets, and this is not first packet.
  342. // 3) There is no overlap between packets.
  343. // We'll check case 1 now, before getting into packet loop.
  344. if (((H261_Header*)
  345. (((FragDescriptor*) pMsgHdr->m_pFragList->GetFirst())->m_pProfileHeader)
  346. )->sbit())
  347. {
  348. // First packet overlaps prior frame (case 1). Add a byte to hold
  349. // first sbits. No need to clear byte now, since it'll be overwritten
  350. // by chFirstByte below.
  351. pbCurrentOffset++;
  352. dwBufSize++;
  353. }
  354. // Process all rcvd packets
  355. while (! pMsgHdr->m_pFragList->Is_Empty())
  356. {
  357. // get next fragment (const to prevent unintentional modification)
  358. FragDescriptor* const pFragDesc = (FragDescriptor*)
  359. pMsgHdr->m_pFragList->TakeFromList();
  360. m_PacketsHold--;
  361. // Verify that TakeFromList() didn't return NULL and that we
  362. // won't overrun the buffer.
  363. BOOL bExit;
  364. #ifdef REBUILD_EXBITSTREAM
  365. if (bExtendedBitstream && (pFragDesc != NULL))
  366. {
  367. bExit =
  368. ((dwBufSize +
  369. pFragDesc->m_BytesOfData +
  370. (nNumFrags * sizeof(*pEBS)) +
  371. sizeof(BSINFO_TRAILER) +
  372. offsetNextDword(pbCurrentOffset))
  373. > pMsgDesc->m_Size);
  374. }
  375. else
  376. #endif
  377. {
  378. bExit =
  379. ((pFragDesc == NULL) ||
  380. (dwBufSize + pFragDesc->m_BytesOfData >= pMsgDesc->m_Size));
  381. }
  382. if (bExit)
  383. {
  384. FreeFragList(pMsgHdr);
  385. EnqueueBuffer(pMsgDesc);
  386. FreeMsgHeader(pMsgHdr);
  387. #ifdef REBUILD_EXBITSTREAM
  388. if (bExtendedBitstream)
  389. {
  390. if (pEBS) {
  391. delete [] pEBS;
  392. pEBS = NULL;
  393. }
  394. }
  395. #endif
  396. LeaveCriticalSection(&m_CritSec);
  397. DBG_MSG(DBG_ERROR,
  398. ("H261_ppmReceive::DataCopy: ERROR - null pFragDesc or buffer overrun"));
  399. if (pFragDesc != NULL)
  400. {
  401. // Release the CRTPSample to receive more data
  402. m_pSubmitCallback->SubmitComplete(pFragDesc->m_pFragCookie,
  403. NOERROR);
  404. // Make a callback into the app to let it know what happened.
  405. ppm::PPMNotification(PPM_E_RECVSIZE, SEVERITY_NORMAL, NULL, 0);
  406. // BUGBUG We are here because
  407. // m_pFragList->Is_Empty() == FALSE
  408. // then the pFragDesc should always be != NULL
  409. if (pFragDesc->m_pProfileHeader)
  410. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  411. FreeFragDescriptor(pFragDesc);
  412. }
  413. m_GlobalLastFrameDropped = TRUE;
  414. return PPMERR(PPM_E_DROPFRAME);
  415. }
  416. // Assign immutable reference to profile header
  417. H261_Header& rProfileHdr =
  418. *(H261_Header*) pFragDesc->m_pProfileHeader;
  419. #ifdef REBUILD_EXBITSTREAM
  420. if (bExtendedBitstream)
  421. {
  422. // Write BS struct for current packet. Do this _before_
  423. // advancing dwBufSize.
  424. pEBS[nCurrentEBS].dwFlags = 0;
  425. if (rProfileHdr.sbit())
  426. {
  427. // Adjust EBS bit offset for this packet's sbits
  428. pEBS[nCurrentEBS].dwBitOffset =
  429. ((dwBufSize - 1) * BITSPERBYTE)
  430. + rProfileHdr.sbit();
  431. }
  432. else
  433. {
  434. pEBS[nCurrentEBS].dwBitOffset = dwBufSize * BITSPERBYTE;
  435. }
  436. pEBS[nCurrentEBS].MBAP = rProfileHdr.mbap();
  437. pEBS[nCurrentEBS].Quant = rProfileHdr.quant();
  438. pEBS[nCurrentEBS].GOBN = rProfileHdr.gobn();
  439. pEBS[nCurrentEBS].HMV = rProfileHdr.hmvd();
  440. pEBS[nCurrentEBS].VMV = rProfileHdr.vmvd();
  441. nCurrentEBS++;
  442. }
  443. #endif /* REBUILD_EXBITSTREAM */
  444. if (rProfileHdr.sbit())
  445. {
  446. // This packet has missing sbits (case 1 or 2)
  447. UCHAR chFirstByte =
  448. *((LPBYTE) pFragDesc->m_pData)
  449. & GetSMask(rProfileHdr.sbit());
  450. // Either this is first packet (thus no ebits), or the prior
  451. // and current packets had better overlap properly.
  452. if ((nLastEbit != 0)
  453. && ((nLastEbit + rProfileHdr.sbit()) != BITSPERBYTE))
  454. {
  455. FreeFragList(pMsgHdr);
  456. EnqueueBuffer(pMsgDesc);
  457. FreeMsgHeader(pMsgHdr);
  458. #ifdef REBUILD_EXBITSTREAM
  459. if (bExtendedBitstream)
  460. {
  461. if (pEBS) {
  462. delete [] pEBS;
  463. pEBS = NULL;
  464. }
  465. }
  466. #endif
  467. LeaveCriticalSection(&m_CritSec);
  468. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::DataCopy: ERROR - Received packets with sbit/ebit mismatch"));
  469. if (pFragDesc != NULL)
  470. {
  471. // Release the CRTPSample to receive more data
  472. m_pSubmitCallback->SubmitComplete(pFragDesc->m_pFragCookie,
  473. NOERROR);
  474. // Make a callback into the app to let it know what happened.
  475. ppm::PPMNotification(PPM_E_DROPFRAME, SEVERITY_NORMAL, NULL, 0);
  476. // BUGBUG We are here because
  477. // m_pFragList->Is_Empty() == FALSE
  478. // then the pFragDesc should always be != NULL
  479. if (pFragDesc->m_pProfileHeader)
  480. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  481. FreeFragDescriptor(pFragDesc);
  482. }
  483. m_GlobalLastFrameDropped = TRUE;
  484. return PPMERR(PPM_E_DROPFRAME);
  485. }
  486. // combine sbits with ebits from prior packet
  487. chFirstByte |= chLastByte;
  488. // Copy packet to buffer
  489. pbCurrentOffset[-1] = chFirstByte;
  490. dwBufSize +=
  491. copyAndAdvance(
  492. pbCurrentOffset,
  493. (LPBYTE) pFragDesc->m_pData + 1,
  494. pFragDesc->m_BytesOfData - 1);
  495. }
  496. else
  497. {
  498. // This packet has no missing sbits (case 3).
  499. if (nLastEbit != 0)
  500. {
  501. // Prior packet has missing ebits; possible encoding error.
  502. FreeFragList(pMsgHdr);
  503. EnqueueBuffer(pMsgDesc);
  504. FreeMsgHeader(pMsgHdr);
  505. #ifdef REBUILD_EXBITSTREAM
  506. if (bExtendedBitstream)
  507. {
  508. if (pEBS) {
  509. delete [] pEBS;
  510. pEBS = NULL;
  511. }
  512. }
  513. #endif
  514. LeaveCriticalSection(&m_CritSec);
  515. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::DataCopy: ERROR - Received packets with sbit/ebit mismatch"));
  516. if (pFragDesc != NULL)
  517. {
  518. // Release the CRTPSample to receive more data
  519. m_pSubmitCallback->SubmitComplete(pFragDesc->m_pFragCookie,
  520. NOERROR);
  521. // Make a callback into the app to let it know what happened.
  522. ppm::PPMNotification(PPM_E_DROPFRAME, SEVERITY_NORMAL, NULL, 0);
  523. // BUGBUG We are here because
  524. // m_pFragList->Is_Empty() == FALSE
  525. // then the pFragDesc should always be != NULL
  526. if (pFragDesc->m_pProfileHeader)
  527. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  528. FreeFragDescriptor(pFragDesc);
  529. }
  530. m_GlobalLastFrameDropped = TRUE;
  531. return PPMERR(PPM_E_DROPFRAME);
  532. }
  533. // Copy packet to buffer
  534. dwBufSize +=
  535. copyAndAdvance(
  536. pbCurrentOffset,
  537. (LPBYTE) pFragDesc->m_pData,
  538. pFragDesc->m_BytesOfData);
  539. #ifdef REBUILD_EXBITSTREAM
  540. if (bExtendedBitstream)
  541. {
  542. iFrame = rProfileHdr.i();
  543. }
  544. #endif
  545. }
  546. // Always shift the ignored bits out of the last byte and patch it
  547. // back into the buffer, since there is no harm in doing so.
  548. chLastByte =
  549. (*((LPBYTE)pFragDesc->m_pData + pFragDesc->m_BytesOfData-1))
  550. & GetEMask(rProfileHdr.ebit());
  551. // Overwrite last byte in buffer
  552. pbCurrentOffset[-1] = chLastByte;
  553. // Save for packet misalignment detection.
  554. nLastEbit = rProfileHdr.ebit();
  555. // send the frag buffer back down to receive more data and free the frag header.
  556. m_pSubmitCallback->SubmitComplete(pFragDesc->m_pFragCookie, NOERROR);
  557. if (pFragDesc->m_pProfileHeader)
  558. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  559. FreeFragDescriptor(pFragDesc);
  560. } // End of packet processing loop
  561. #ifdef REBUILD_EXBITSTREAM
  562. if (bExtendedBitstream)
  563. {
  564. {
  565. // Test if the buffer size is enough to avoid
  566. // writing past the buffer size
  567. unsigned char *ptr;
  568. ptr = pbCurrentOffset +
  569. offsetNextDword(pbCurrentOffset) +
  570. nNumFrags * sizeof(*pEBS) +
  571. sizeof(BSINFO_TRAILER);
  572. if (ptr > ((unsigned char *)(pMsgDesc->m_pBuffer) +
  573. pMsgDesc->m_Size) ) {
  574. // Make a callback into the app to let it know what happened.
  575. ppm::PPMNotification(PPM_E_RECVSIZE, SEVERITY_NORMAL, NULL, 0);
  576. #if DEBUG_FREELIST > 2
  577. char str[128];
  578. wsprintf(str,"DataCopy[0x%X]: "
  579. "About to corrupt buffer "
  580. "at: 0x%X size=%d, drop frame\n",
  581. pMsgDesc->m_pBuffer, ptr-1, pMsgDesc->m_Size);
  582. OutputDebugString(str);
  583. #endif
  584. if (pEBS)
  585. delete [] pEBS;
  586. // Drop frame
  587. FreeFragList(pMsgHdr);
  588. EnqueueBuffer(pMsgDesc);
  589. FreeMsgHeader(pMsgHdr);
  590. LeaveCriticalSection(&m_CritSec);
  591. m_GlobalLastFrameDropped = TRUE;
  592. return PPMERR(PPM_E_DROPFRAME);
  593. }
  594. }
  595. // Note that we don't increment dwBufSize here, since it doesn't include EBS or trailer.
  596. // pad with zeros to next dword boundary
  597. static const DWORD dwZero = 0;
  598. copyAndAdvance(pbCurrentOffset, &dwZero, offsetNextDword(pbCurrentOffset));
  599. // copy the extended bitstream structures into the buffer
  600. copyAndAdvance(pbCurrentOffset, pEBS, nNumFrags * sizeof(*pEBS));
  601. // Delete now to prevent erroneous late update
  602. delete [] pEBS;
  603. pEBS = NULL;
  604. setH261BSInfoTrailer(
  605. *(BSINFO_TRAILER*) pbCurrentOffset,
  606. iFrame,
  607. dwBufSize,
  608. nNumFrags,
  609. currentSourceFormat);
  610. pbCurrentOffset += sizeof(BSINFO_TRAILER);
  611. }
  612. #endif /* REBUILD_EXBITSTREAM */
  613. LeaveCriticalSection(&m_CritSec);
  614. // When we are done. Call Client's submit with full Message
  615. WSABUF tmpWSABUF[2];
  616. tmpWSABUF[0].buf = (char*) pMsgDesc->m_pBuffer;
  617. #ifdef REBUILD_EXBITSTREAM
  618. if (bExtendedBitstream)
  619. {
  620. // we report the size including the extended bitstream + trailer + padding
  621. tmpWSABUF[0].len = (ULONG) (pbCurrentOffset -
  622. (LPBYTE) pMsgDesc->m_pBuffer);
  623. }
  624. else
  625. #endif
  626. {
  627. tmpWSABUF[0].len = dwBufSize;
  628. }
  629. tmpWSABUF[1].buf = (char*) &(pMsgDesc->m_TimeStamp);
  630. tmpWSABUF[1].len = sizeof(pMsgDesc->m_TimeStamp);
  631. FreeMsgHeader(pMsgHdr);
  632. HRESULT status =
  633. m_pSubmit->Submit(
  634. tmpWSABUF,
  635. 2,
  636. pMsgDesc,
  637. m_GlobalLastFrameDropped ? PPMERR(PPM_E_DROPFRAME) : NOERROR);
  638. m_GlobalLastFrameDropped = FALSE;
  639. if (FAILED(status))
  640. {
  641. #if DEBUG_FREELIST > 2
  642. char str[128];
  643. wsprintf(str,"DataCopy: Submit failed %d (0x%X)\n", status, status);
  644. OutputDebugString(str);
  645. #endif
  646. // no SubmitComplete should be called, so take care of resources now
  647. //pMsgDesc->m_Size = m_MaxBufferSize; // reset the data buffer size
  648. //EnqueueBuffer(pMsgDesc);
  649. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::DataCopy: ERROR - Client Submit failed"));
  650. status = PPMERR(PPM_E_CLIENTERR);
  651. // Make a callback into the app to let it know what happened.
  652. ppm::PPMNotification(PPM_E_CLIENTERR, SEVERITY_NORMAL, NULL, 0);
  653. m_GlobalLastFrameDropped = TRUE;
  654. }
  655. return status;
  656. }
  657. #pragma optimize ( "", on ) // BUGBUG: Work around complier bug
  658. #ifdef REBUILD_EXBITSTREAM
  659. //////////////////////////////////////////////////////////////////////////////////////////
  660. // initBitstreamInfoH261: Prepares bitstream info vector element. This function assumes
  661. // that bitstream info vector was zero-filed on allocation, and that elements are never
  662. // reused. If this turns out to be dangerous, this function should memset BS_info to zero.
  663. //////////////////////////////////////////////////////////////////////////////////////////
  664. inline void
  665. initBitstreamInfoH261(BITSTREAM_INFO_H261& BS_info, DWORD dwBitOffset = 0)
  666. {
  667. BS_info.dwFlags = RTP_H26X_PACKET_LOST;
  668. BS_info.dwBitOffset = dwBitOffset;
  669. }
  670. //////////////////////////////////////////////////////////////////////////////////////////
  671. // BuildExtendedBitstream: build extended bitstream
  672. //////////////////////////////////////////////////////////////////////////////////////////
  673. HRESULT H261_ppmReceive::BuildExtendedBitstream(MsgHeader* const pMsgHdr)
  674. {
  675. MsgDescriptor* pMsgDesc = DequeueBuffer(1); // Get a buffer to hold the message.
  676. if (pMsgDesc == NULL)
  677. {
  678. FreeFragList(pMsgHdr);
  679. FreeMsgHeader(pMsgHdr);
  680. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::BuildExtendedBitstream: ERROR - Couldn't get a reassembly buffer"));
  681. // Make a callback into the app to let it know what happened.
  682. ppm::PPMNotification(PPM_E_DROPFRAME, SEVERITY_NORMAL, NULL, 0);
  683. m_GlobalLastFrameDropped = TRUE;
  684. return PPMERR(PPM_E_DROPFRAME);
  685. }
  686. EnterCriticalSection(&m_CritSec);
  687. #ifdef GIVE_SEQNUM
  688. // Do this before frag list exhausted below
  689. pMsgDesc->m_TimeStamp = pMsgHdr->m_pFragList->LastSeqNum();
  690. #else
  691. pMsgDesc->m_TimeStamp = pMsgHdr->GetMsgID();
  692. #endif
  693. int nExtraFrags = 0;
  694. // Use presence/absence of valid picture start code to determine whether
  695. // or not first fragment has been rcvd.
  696. RTPh261SourceFormat currentSourceFormat =
  697. getH261payloadType(
  698. ((FragDescriptor*) pMsgHdr->m_pFragList->GetFirst())->m_pData);
  699. if (currentSourceFormat == rtph261SourceFormatUnknown)
  700. {
  701. nExtraFrags++;
  702. }
  703. else
  704. {
  705. // Remember valid format for use by next frame
  706. m_rtph261SourceFormat = currentSourceFormat;
  707. }
  708. // Check for last packet missing; if so, add 1 to nNumFrags.
  709. if (! pMsgHdr->m_MarkerBitIn)
  710. {
  711. nExtraFrags++;
  712. }
  713. // Compute frame fragment count (const for safety)
  714. const int nNumFrags =
  715. pMsgHdr->m_pFragList->SeqNumSpan() + nExtraFrags;
  716. DBG_MSG(DBG_TRACE,
  717. ("H261_ppmReceive::BuildExtendedBitstream: "
  718. "m_GlobalLastMarkerBitIn=%d, "
  719. "m_GlobalLastSeqNum=%d",
  720. m_GlobalLastMarkerBitIn,
  721. m_GlobalLastSeqNum));
  722. DBG_MSG(DBG_TRACE,
  723. ("H261_ppmReceive::BuildExtendedBitstream: "
  724. "MsgHeader*=0x%08lx, FirstSeqNum()=%d",
  725. pMsgHdr,
  726. pMsgHdr->m_pFragList->FirstSeqNum()));
  727. DBG_MSG(DBG_TRACE,
  728. (" LastSeqNum()=%d, nNumFrags=%d",
  729. pMsgHdr->m_pFragList->LastSeqNum(),
  730. nNumFrags));
  731. // allocate structures for the extended bitstream information
  732. BITSTREAM_INFO_H261* pEBS = new BITSTREAM_INFO_H261[nNumFrags];
  733. if (pEBS == NULL)
  734. {
  735. FreeFragList(pMsgHdr);
  736. EnqueueBuffer(pMsgDesc);
  737. FreeMsgHeader(pMsgHdr);
  738. LeaveCriticalSection(&m_CritSec);
  739. DBG_MSG(DBG_ERROR,
  740. ("H261_ppmReceive::BuildExtendedBitstream: ERROR - memory allocation failure"));
  741. // Make a callback into the app to let it know what happened.
  742. ppm::PPMNotification(PPM_E_OUTOFMEMORY, SEVERITY_NORMAL, NULL, 0);
  743. m_GlobalLastFrameDropped = TRUE;
  744. return PPMERR(PPM_E_DROPFRAME);
  745. }
  746. memset(pEBS, 0, nNumFrags * sizeof(*pEBS));
  747. LPBYTE pbCurrentOffset = (LPBYTE) pMsgDesc->m_pBuffer; // start copying into front of buffer.
  748. DWORD dwBufSize = 0;
  749. int nCurrentEBS = 0; // EBS vector index
  750. if (currentSourceFormat == rtph261SourceFormatUnknown)
  751. {
  752. // Source format is unknown, assume first packet is missing.
  753. // Add EBS info for the lost first packet
  754. initBitstreamInfoH261(pEBS[nCurrentEBS ++]);
  755. // Add new padding values for lost first packet. Use PSC which
  756. // corresponds to source format of last seen valid PSC.
  757. if (m_rtph261SourceFormat == rtph261SourceFormatCIF)
  758. {
  759. dwBufSize +=
  760. copyAndAdvance(
  761. pbCurrentOffset,
  762. &s_leadFragBitPatternCIF,
  763. sizeof(s_leadFragBitPatternCIF));
  764. }
  765. else
  766. {
  767. // Either QCIF or unknown
  768. dwBufSize +=
  769. copyAndAdvance(
  770. pbCurrentOffset,
  771. &s_leadFragBitPatternQCIF,
  772. sizeof(s_leadFragBitPatternQCIF));
  773. }
  774. }
  775. // Loop state variables
  776. UCHAR chLastByte = 0;
  777. int iFrame = 0, nLastEbit = 0;
  778. int nCurrentSeqNum = pMsgHdr->m_pFragList->FirstSeqNum();
  779. // Process all rcvd packets
  780. while (! pMsgHdr->m_pFragList->Is_Empty())
  781. {
  782. // get next fragment (const to prevent unintentional modification)
  783. FragDescriptor* const pFragDesc = (FragDescriptor*)
  784. pMsgHdr->m_pFragList->TakeFromList();
  785. m_PacketsHold--;
  786. // Note: remember that m_pFragList will be empty after the last
  787. // iteration of this loop, so LList::methods which expect a
  788. // non-empty list should not be called below.
  789. // check to see if TakeFromList() returned NULL. Also, make sure
  790. // we won't overrun the buffer (including extended bitstream,
  791. // the trailer and any dword alignment bits that need to be added).
  792. if ((pFragDesc == NULL) ||
  793. ((dwBufSize +
  794. pFragDesc->m_BytesOfData +
  795. (nNumFrags * sizeof(*pEBS)) +
  796. sizeof(BSINFO_TRAILER) +
  797. offsetNextDword(pbCurrentOffset))
  798. > pMsgDesc->m_Size))
  799. {
  800. FreeFragList(pMsgHdr);
  801. EnqueueBuffer(pMsgDesc);
  802. FreeMsgHeader(pMsgHdr);
  803. if (pEBS) {
  804. delete [] pEBS;
  805. pEBS = NULL;
  806. }
  807. LeaveCriticalSection(&m_CritSec);
  808. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::BuildExtendedBitstream: ERROR - null pFragDesc or buffer overrun"));
  809. if (pFragDesc != NULL)
  810. {
  811. // Make a callback into the app to let it know what happened.
  812. ppm::PPMNotification(PPM_E_RECVSIZE, SEVERITY_NORMAL, NULL, 0);
  813. // BUGBUG We are here because
  814. // m_pFragList->Is_Empty() == FALSE
  815. // then the pFragDesc should always be != NULL
  816. if (pFragDesc->m_pProfileHeader)
  817. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  818. FreeFragDescriptor(pFragDesc);
  819. }
  820. m_GlobalLastFrameDropped = TRUE;
  821. return PPMERR(PPM_E_DROPFRAME);
  822. }
  823. DBG_MSG(DBG_TRACE,
  824. ("H261_ppmReceive::BuildExtendedBitstream: "
  825. "FragDescriptor*=0x%08lx, "
  826. "seq=%d, "
  827. "ts=%lu, "
  828. "frag[0-3]=%1X%1X%1X%1X",
  829. pFragDesc,
  830. pFragDesc->m_pRTPHeader->seq(),
  831. pFragDesc->m_pRTPHeader->ts(),
  832. ((char*) pFragDesc->m_pData)[0],
  833. ((char*) pFragDesc->m_pData)[1],
  834. ((char*) pFragDesc->m_pData)[2],
  835. ((char*) pFragDesc->m_pData)[3]));
  836. // see if packets are skipped; if so, put padding into buffer and add BS struct
  837. #ifdef RTP_CLASS
  838. for (; nCurrentSeqNum < pFragDesc->m_pRTPHeader->seq(); nCurrentSeqNum++)
  839. #else
  840. for (; nCurrentSeqNum < ntohs(pFragDesc->m_pRTPHeader->seq); nCurrentSeqNum++)
  841. #endif
  842. {
  843. if (nCurrentEBS >= nNumFrags) { //ERROR, bail out
  844. FreeFragList(pMsgHdr);
  845. EnqueueBuffer(pMsgDesc);
  846. FreeMsgHeader(pMsgHdr);
  847. if (pEBS) {
  848. delete [] pEBS;
  849. pEBS = NULL;
  850. }
  851. LeaveCriticalSection(&m_CritSec);
  852. DBG_MSG(DBG_ERROR, ("H263_ppmReceive::BuildExtendedBitstream: ERROR - buffer overrun"));
  853. if (pFragDesc != NULL)
  854. {
  855. // Make a callback into the app to let it know what happened.
  856. ppm::PPMNotification(PPM_E_RECVSIZE, SEVERITY_NORMAL, NULL, 0);
  857. // BUGBUG We are here because
  858. // m_pFragList->Is_Empty() == FALSE
  859. // then the pFragDesc should always be != NULL
  860. if (pFragDesc->m_pProfileHeader)
  861. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  862. FreeFragDescriptor(pFragDesc);
  863. }
  864. m_GlobalLastFrameDropped = TRUE;
  865. return PPMERR(PPM_E_DROPFRAME);
  866. }
  867. // If prior rcvd packet had missing ebits, adjust bit
  868. // offset accordingly. The missing bits are added to the
  869. // tail of the padding (by forcing bit offset of _next_
  870. // segment to be byte-aligned). If there was no prior rcvd
  871. // packet, or prior packet had no missing ebits, nLastEbit ==
  872. // 0, so that the bit offset expression is equivalent to
  873. // (dwBufSize * BITSPERBYTE).
  874. initBitstreamInfoH261(
  875. pEBS[nCurrentEBS++],
  876. ((dwBufSize - 1) * BITSPERBYTE)
  877. + (BITSPERBYTE - nLastEbit));
  878. // we just consumed the last ebits (if any), and are now back
  879. // to byte alignment.
  880. nLastEbit = 0;
  881. chLastByte = 0;
  882. // for packets other than the first of the frame, we pad with 4
  883. // bytes of zeroes
  884. dwBufSize +=
  885. copyAndAdvance(
  886. pbCurrentOffset,
  887. &s_nonLeadFragBitPattern,
  888. sizeof(s_nonLeadFragBitPattern));
  889. }
  890. // Assign immutable reference to profile header
  891. H261_Header& rProfileHdr =
  892. *(H261_Header*) pFragDesc->m_pProfileHeader;
  893. // Handle overlapping (shared byte) between current and prior
  894. // between packets. There are three cases to deal with:
  895. // 1) Current packet overlaps the prior packet, and the prior
  896. // packet was received.
  897. // 2) Current packet overlaps the prior packet, and this is first
  898. // packet or prior packet was lost (there are no pending ebits).
  899. // 3) Current packet doesn't overlap the prior packet.
  900. if (rProfileHdr.sbit())
  901. {
  902. // This packet had missing sbits (case 1 or 2).
  903. // Mask off missing sbits and combin with ebits from prior packet.
  904. const UCHAR chFirstByte =
  905. (*(LPBYTE) pFragDesc->m_pData
  906. & GetSMask(rProfileHdr.sbit()))
  907. | chLastByte;
  908. if (nLastEbit == 0)
  909. {
  910. // Case 2: There are no pending ebits, so we need an
  911. // extra byte into which to stuff the current packet's
  912. // sbits. No need to clear the byte, since it'll be
  913. // overwritten by chFirstByte below.
  914. pbCurrentOffset++;
  915. dwBufSize++;
  916. }
  917. else if (nLastEbit + rProfileHdr.sbit() != BITSPERBYTE)
  918. {
  919. // The prior and current packets don't overlap properly.
  920. FreeFragList(pMsgHdr);
  921. EnqueueBuffer(pMsgDesc);
  922. FreeMsgHeader(pMsgHdr);
  923. if (pEBS) {
  924. delete [] pEBS;
  925. pEBS = NULL;
  926. }
  927. LeaveCriticalSection(&m_CritSec);
  928. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::BuildExtendedBitstream: ERROR - Received packets with sbit/ebit mismatch"));
  929. if (pFragDesc != NULL)
  930. {
  931. // Make a callback into the app to let it know what happened.
  932. ppm::PPMNotification(PPM_E_DROPFRAME, SEVERITY_NORMAL, NULL, 0);
  933. // BUGBUG We are here because
  934. // m_pFragList->Is_Empty() == FALSE
  935. // then the pFragDesc should always be != NULL
  936. if (pFragDesc->m_pProfileHeader)
  937. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  938. FreeFragDescriptor(pFragDesc);
  939. }
  940. m_GlobalLastFrameDropped = TRUE;
  941. return PPMERR(PPM_E_DROPFRAME);
  942. }
  943. pEBS[nCurrentEBS].dwBitOffset =
  944. ((dwBufSize - 1) * BITSPERBYTE)
  945. + rProfileHdr.sbit();
  946. // Copy the packet to the frame buffer.
  947. pbCurrentOffset[-1] = chFirstByte;
  948. dwBufSize +=
  949. copyAndAdvance(
  950. pbCurrentOffset,
  951. (LPBYTE) pFragDesc->m_pData + 1,
  952. pFragDesc->m_BytesOfData - 1);
  953. // dwBufSize now points to _next_ packet slot
  954. }
  955. else
  956. {
  957. // This packet doesn't overlap prior packet (case 3).
  958. if (nLastEbit != 0)
  959. {
  960. // Prior packet has missing ebits; possible encoding error.
  961. FreeFragList(pMsgHdr);
  962. EnqueueBuffer(pMsgDesc);
  963. FreeMsgHeader(pMsgHdr);
  964. if (pEBS) {
  965. delete [] pEBS;
  966. pEBS = NULL;
  967. }
  968. LeaveCriticalSection(&m_CritSec);
  969. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::BuildExtendedBitstream: ERROR - Received packets with sbit/ebit mismatch"));
  970. if (pFragDesc != NULL)
  971. {
  972. // Make a callback into the app to let it know what happened.
  973. ppm::PPMNotification(PPM_E_DROPFRAME, SEVERITY_NORMAL, NULL, 0);
  974. // BUGBUG We are here because
  975. // m_pFragList->Is_Empty() == FALSE
  976. // then the pFragDesc should always be != NULL
  977. if (pFragDesc->m_pProfileHeader)
  978. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  979. FreeFragDescriptor(pFragDesc);
  980. }
  981. m_GlobalLastFrameDropped = TRUE;
  982. return PPMERR(PPM_E_DROPFRAME);
  983. }
  984. pEBS[nCurrentEBS].dwBitOffset = dwBufSize * BITSPERBYTE;
  985. // Copy the packet to the frame buffer.
  986. dwBufSize +=
  987. copyAndAdvance(
  988. pbCurrentOffset,
  989. pFragDesc->m_pData,
  990. pFragDesc->m_BytesOfData);
  991. // dwBufSize now points to _next_ packet slot
  992. iFrame = rProfileHdr.i();
  993. }
  994. // We need to store the ebit for the last received packet to give the
  995. // dwBitOffset for the last real packet if lost.
  996. nLastEbit = rProfileHdr.ebit();
  997. // Always shift the ignored bits out of the last byte and patch it
  998. // back into the buffer, since there is no harm in doing so.
  999. chLastByte =
  1000. (*((LPBYTE)pFragDesc->m_pData + pFragDesc->m_BytesOfData - 1))
  1001. & GetEMask(nLastEbit);
  1002. // Overwrite last byte to clear missing bits
  1003. pbCurrentOffset[-1] = chLastByte;
  1004. // Update BS struct for current packet and save off values for
  1005. // next lost packet
  1006. pEBS[nCurrentEBS].dwFlags = 0;
  1007. pEBS[nCurrentEBS].MBAP = rProfileHdr.mbap();
  1008. pEBS[nCurrentEBS].Quant = rProfileHdr.quant();
  1009. pEBS[nCurrentEBS].GOBN = rProfileHdr.gobn();
  1010. pEBS[nCurrentEBS].HMV = rProfileHdr.hmvd();
  1011. pEBS[nCurrentEBS].VMV = rProfileHdr.vmvd();
  1012. nCurrentEBS++;
  1013. nCurrentSeqNum++;
  1014. // Send the frag buffer back down to receive more data and free
  1015. // the frag header. always pass zero because we never allocated
  1016. // the buffers and therefore have no idea how big the buffers
  1017. // are.
  1018. m_pSubmitCallback->SubmitComplete(pFragDesc->m_pFragCookie, NOERROR);
  1019. if (pFragDesc->m_pProfileHeader)
  1020. FreeProfileHeader(pFragDesc->m_pProfileHeader);
  1021. FreeFragDescriptor(pFragDesc);
  1022. } // End of packet processing loop
  1023. {
  1024. // Test if the buffer size is enough to avoid
  1025. // writing past the buffer size
  1026. unsigned char *ptr;
  1027. ptr = pbCurrentOffset +
  1028. offsetNextDword(pbCurrentOffset) +
  1029. nNumFrags * sizeof(*pEBS) +
  1030. sizeof(BSINFO_TRAILER);
  1031. if (!pMsgHdr->m_MarkerBitIn)
  1032. ptr += sizeof(s_nonLeadFragBitPattern);
  1033. if ( ptr >
  1034. ((unsigned char *)(pMsgDesc->m_pBuffer) + pMsgDesc->m_Size) ) {
  1035. // Make a callback into the app to let it know what happened.
  1036. ppm::PPMNotification(PPM_E_DROPFRAME, SEVERITY_NORMAL, NULL, 0);
  1037. #if DEBUG_FREELIST > 2
  1038. char str[128];
  1039. wsprintf(str,"BuildExtendedBitstream[0x%X]: "
  1040. "About to corrupt buffer "
  1041. "at: 0x%X size=%d, drop frame\n",
  1042. pMsgDesc->m_pBuffer, ptr-1, pMsgDesc->m_Size);
  1043. OutputDebugString(str);
  1044. #endif
  1045. if (pEBS)
  1046. delete [] pEBS;
  1047. // Drop frame
  1048. FreeFragList(pMsgHdr);
  1049. EnqueueBuffer(pMsgDesc);
  1050. FreeMsgHeader(pMsgHdr);
  1051. LeaveCriticalSection(&m_CritSec);
  1052. m_GlobalLastFrameDropped = TRUE;
  1053. return PPMERR(PPM_E_DROPFRAME);
  1054. }
  1055. }
  1056. // check and handling for last packet if missing
  1057. if (! pMsgHdr->m_MarkerBitIn)
  1058. {
  1059. // Yes, it's last packet, but increment nCurrentEBS to trap
  1060. // unintentional reference later.
  1061. initBitstreamInfoH261(
  1062. pEBS[nCurrentEBS ++],
  1063. ((dwBufSize - 1) * BITSPERBYTE) + (BITSPERBYTE - nLastEbit));
  1064. // for packets other than the first of the frame, we pad with
  1065. // 4 bytes of zeroes
  1066. dwBufSize +=
  1067. copyAndAdvance(
  1068. pbCurrentOffset,
  1069. &s_nonLeadFragBitPattern,
  1070. sizeof(s_nonLeadFragBitPattern));
  1071. }
  1072. // pad with zeros to next dword boundary
  1073. static const DWORD dwZero = 0;
  1074. copyAndAdvance(pbCurrentOffset, &dwZero, offsetNextDword(pbCurrentOffset));
  1075. // copy the extended bitstream structures into the buffer
  1076. copyAndAdvance(pbCurrentOffset, pEBS, nNumFrags * sizeof(*pEBS));
  1077. // Delete now to prevent erroneous late update
  1078. delete [] pEBS;
  1079. pEBS = NULL;
  1080. // Note that dwBufSize doesn't include the extended bitstream or the trailer.
  1081. setH261BSInfoTrailer(
  1082. *(BSINFO_TRAILER*) pbCurrentOffset,
  1083. iFrame,
  1084. dwBufSize,
  1085. nNumFrags,
  1086. m_rtph261SourceFormat);
  1087. pbCurrentOffset += sizeof(BSINFO_TRAILER);
  1088. LeaveCriticalSection(&m_CritSec);
  1089. // When we are done. Call Client's submit with full Message
  1090. // we report the size including the extended bitstream + trailer + padding
  1091. WSABUF tmpWSABUF[2];
  1092. tmpWSABUF[0].buf = (char*) pMsgDesc->m_pBuffer;
  1093. tmpWSABUF[0].len = (DWORD)(pbCurrentOffset - (LPBYTE) pMsgDesc->m_pBuffer);
  1094. tmpWSABUF[1].buf = (char*) &(pMsgDesc->m_TimeStamp);
  1095. tmpWSABUF[1].len = sizeof(pMsgDesc->m_TimeStamp);
  1096. // Make a callback into the app to let it know about this partial frame
  1097. // We also pass the timestamp of the frame
  1098. ppm::PPMNotification(
  1099. PPM_E_PARTIALFRAME,
  1100. SEVERITY_NORMAL,
  1101. (LPBYTE) tmpWSABUF[1].buf,
  1102. tmpWSABUF[1].len);
  1103. FreeMsgHeader(pMsgHdr);
  1104. HRESULT status =
  1105. m_pSubmit->Submit(tmpWSABUF, 2, pMsgDesc, PPMERR(PPM_E_PARTIALFRAME));
  1106. m_GlobalLastFrameDropped = FALSE;
  1107. if (FAILED(status))
  1108. {
  1109. // no SubmitComplete should be called, so take care of resources now
  1110. //pMsgDesc->m_Size = m_MaxBufferSize; // reset the data buffer size
  1111. //EnqueueBuffer(pMsgDesc);
  1112. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::BuildExtendedBitstream: ERROR - Client Submit failed"));
  1113. status = PPMERR(PPM_E_CLIENTERR);
  1114. // Make a callback into the app to let it know what happened.
  1115. ppm::PPMNotification(PPM_E_CLIENTERR, SEVERITY_NORMAL, NULL, 0);
  1116. m_GlobalLastFrameDropped = TRUE;
  1117. }
  1118. return status;
  1119. }
  1120. #endif // REBUILD_EXBITSTREAM
  1121. //////////////////////////////////////////////////////////////////////////////////////////
  1122. // PartialMessageHandler: deals with partial messages
  1123. //////////////////////////////////////////////////////////////////////////////////////////
  1124. HRESULT H261_ppmReceive::PartialMessageHandler(MsgHeader* pMsgHdr)
  1125. {
  1126. if (pMsgHdr == NULL)
  1127. {
  1128. DBG_MSG(DBG_ERROR, ("H261_ppmReceive::PartialMessageHandler: ERROR - pMsgHdr == NULL"));
  1129. return PPMERR(PPM_E_EMPTYQUE);
  1130. }
  1131. ASSERT(! pMsgHdr->m_pFragList->Is_Empty());
  1132. #ifdef REBUILD_EXBITSTREAM
  1133. if (m_ExtendedBitstream)
  1134. {
  1135. return BuildExtendedBitstream(pMsgHdr);
  1136. }
  1137. else
  1138. #endif /* REBUILD_EXBITSTREAM */
  1139. {
  1140. // Make a callback into the app to let it know about this dropped frame
  1141. // We also pass the timestamp of the frame
  1142. DWORD tmpTS = pMsgHdr->GetMsgID();
  1143. ppm::PPMNotification(
  1144. PPM_E_DROPFRAME,
  1145. SEVERITY_NORMAL,
  1146. (LPBYTE) &tmpTS,
  1147. sizeof(tmpTS));
  1148. m_GlobalLastFrameDropped = TRUE;
  1149. FreeFragList(pMsgHdr);
  1150. FreeMsgHeader(pMsgHdr);
  1151. return NOERROR;
  1152. }
  1153. }
  1154. //////////////////////////////////////////////////////////////////////////////////////////
  1155. // InitProfileHeader: Given a buffer as type void, sets up a profile header. Does nothing
  1156. // for the Generic case. Intended for overrides for various payloads.
  1157. // Companion member function FreeProfileHeader provided so that if payload
  1158. // header memory is allocated in this function, it can be freed there.
  1159. //////////////////////////////////////////////////////////////////////////////////////////
  1160. void* H261_ppmReceive::InitProfileHeader(void* pBuffer)
  1161. {
  1162. // return new H261_Header ( (char*) pBuffer );
  1163. return new ( m_pH261Headers )H261_Header ( (char*) pBuffer );
  1164. }
  1165. //////////////////////////////////////////////////////////////////////////////////////////
  1166. // FreeProfileHeader: Given a buffer as type void, frees up a profile header. Does nothing
  1167. // for the Generic case. Intended for overrides for various payloads.
  1168. // Companion member function InitProfileHeader may allocate memory for
  1169. // payload header which needs to be freed here. No return value.
  1170. //////////////////////////////////////////////////////////////////////////////////////////
  1171. void H261_ppmReceive::FreeProfileHeader(void* pBuffer)
  1172. {
  1173. // delete (H261_Header*)pBuffer;
  1174. m_pH261Headers->Free( pBuffer );
  1175. return;
  1176. }
  1177. // [EOF]