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.

237 lines
7.7 KiB

  1. /**********************************************************************
  2. Copyright (c) 1994 - 1998 Microsoft Corporation. All Rights Reserved.
  3. LtcDecode, Ken Greenebaum, November 1996
  4. A generic linear timecode decoder. Pass it 16 bit mono PCM raw audio
  5. samples and it will decode the LTC timecode encoded therein.
  6. This code based on the LTC description in John Ratcliff's Timecode; A user's
  7. guide by (second edition)
  8. **********************************************************************/
  9. #include <stdio.h>
  10. #include <wtypes.h>
  11. #include "ltcdcode.h"
  12. /**********************************************************************
  13. helper function to decode raw LTC bits found in the circular bit buffer
  14. **********************************************************************/
  15. int
  16. compute(int begin, int bitNumber, int numBits, bitData ringBuffer[])
  17. {
  18. int value = 0;
  19. int count;
  20. int index;
  21. index = (begin + bitNumber) % 80;
  22. for(count = 0; count < numBits; count++) {
  23. value+= ringBuffer[index].value << count;
  24. index = (index+1)%80;
  25. }
  26. return(value);
  27. }
  28. /**********************************************************************
  29. initialize internal state
  30. **********************************************************************/
  31. LTCdecoder::LTCdecoder()
  32. {
  33. // XXX these should all be initialy computed based on format!
  34. _bitWidth = 0; // this will get set at the first bit...
  35. _verticalEpsilon = 1024;
  36. // values to reset
  37. _waveState = 0; // doesn't really matter where we start it
  38. _sampleWidth = 0; // reset bit width
  39. _lastBit = 0; // must be zero so we can legaly have a one!
  40. _onesCount = 0; // reset the sync detector
  41. _samplesProcessed= 0; // total number of audio samples processed
  42. _bitsRecorded = 0;
  43. _bufferIndex = 0;
  44. _syncSample = 0;
  45. _validTimeCode = 0; // no, we don't have a valid timecode, yet
  46. }
  47. /**********************************************************************
  48. Decode and return the user bits from the raw LTC decoded bits in the
  49. ring buffer.
  50. Returns True if there is a valid timecode in the ring buffer
  51. In general this is only true immediately after the decode method returns
  52. a non-zero timecode sample sync
  53. See timecode page 33
  54. **********************************************************************/
  55. int
  56. LTCdecoder::getUserBits(LTCuserBits *bits)
  57. {
  58. if(_validTimeCode) { // verify that valid timecode is in buffer
  59. int begin = (_bufferIndex - 78 + 80) % 80; // point to beggining of timecode
  60. bits->user1 = compute(begin, 4, 4, _ringBuffer);
  61. bits->user2 = compute(begin, 12, 4, _ringBuffer);
  62. bits->user3 = compute(begin, 20, 4, _ringBuffer);
  63. bits->user4 = compute(begin, 28, 4, _ringBuffer);
  64. bits->user5 = compute(begin, 36, 4, _ringBuffer);
  65. bits->user6 = compute(begin, 44, 4, _ringBuffer);
  66. bits->user7 = compute(begin, 52, 4, _ringBuffer);
  67. bits->user8 = compute(begin, 60, 4, _ringBuffer);
  68. }
  69. return(_validTimeCode);
  70. }
  71. /**********************************************************************
  72. Return the audio sample corresponding to the beggining and the end of
  73. the timecode.
  74. We should return a double when we want to get sub-sample accuracy and
  75. split hairs!
  76. **********************************************************************/
  77. int
  78. LTCdecoder::getStartStopSample(LONGLONG *start, LONGLONG *stop)
  79. {
  80. if(_validTimeCode) { // verify that valid timecode is in buffer
  81. *start = _syncSample;
  82. *stop = _endSample;
  83. }
  84. return(_validTimeCode);
  85. }
  86. /**********************************************************************
  87. Decode and return the TimeCode from the raw LTC decoded bits in the
  88. ring buffer.
  89. Returns True if there is a valid timecode in the ring buffer
  90. In general this is only true immediately after the decode method returns
  91. a non-zero timecode sample sync
  92. See timecode page 33
  93. **********************************************************************/
  94. int
  95. LTCdecoder::getTimeCode(TimeCode *tc)
  96. {
  97. if(_validTimeCode) { // verify that valid timecode is in buffer
  98. int hour, minute, second, frame;
  99. int begin = (_bufferIndex - 78 + 80) % 80; // point to beggining of timecode
  100. frame = compute(begin, 0, 4, _ringBuffer); // frame units 0-3 1,2,4,8
  101. frame += 10*compute(begin, 8, 2, _ringBuffer); // frame tens 0-1 1,2
  102. second = compute(begin, 16, 4, _ringBuffer);
  103. second+= 10*compute(begin, 24, 3, _ringBuffer);
  104. minute = compute(begin, 32, 4, _ringBuffer);
  105. minute+= 10*compute(begin, 40, 3, _ringBuffer);
  106. hour = compute(begin, 48, 4, _ringBuffer);
  107. hour += 10*compute(begin, 56, 2, _ringBuffer);
  108. tc->SetTime(hour, minute, second, frame); // XXX for now
  109. }
  110. return(_validTimeCode);
  111. }
  112. /**********************************************************************
  113. add a bit to the ringbuffer.
  114. Will return true if the sync bits at the end of a timecode are encountered.
  115. NOTE: we don't have to reset _onesCount after finding sync, the next 0 will!
  116. **********************************************************************/
  117. int LTCdecoder::_addBuffer(int bit, LONGLONG sample)
  118. {
  119. _bitsRecorded++;
  120. _ringBuffer[_bufferIndex].value = bit;
  121. _ringBuffer[_bufferIndex++].sample = sample;
  122. _bufferIndex%=80;
  123. _onesCount = bit ? (_onesCount + 1) : 0;
  124. return((_onesCount==12)&&(_bitsRecorded>77));
  125. }
  126. /**********************************************************************
  127. Accept a new hunk of audio, decode the bits present in it, and return
  128. the sample number corresponding to the timeCode synch (Rising edge of Bit
  129. Zero) if a timeCode was completed being decoded.
  130. **********************************************************************/
  131. int
  132. LTCdecoder::decodeBuffer(short **buf, int *bufferSize)
  133. {
  134. int index;
  135. int timeCodeFound = 0;
  136. short *buffer = *buf;
  137. int newWaveState;
  138. int bit;
  139. for(index = 0; (index < *bufferSize) && (!timeCodeFound); index++) {
  140. _samplesProcessed++; // another day another sample
  141. newWaveState = _waveState?(buffer[index]+_verticalEpsilon)>0
  142. :(buffer[index]-_verticalEpsilon)>0;
  143. if(newWaveState==_waveState) {
  144. _sampleWidth++;
  145. }
  146. else { // new bit
  147. _waveState = newWaveState;
  148. if(4*_sampleWidth > 3 * _bitWidth) { // long?
  149. bit = 0;
  150. _bitWidth = _sampleWidth;
  151. }
  152. else { // short
  153. bit = (_lastBit==1)?2:1; // only a valid one if following 0
  154. _bitWidth = 2*_sampleWidth;
  155. }
  156. _lastBit = bit;
  157. #ifdef DEBUG
  158. #if 0
  159. if(bit!=2)
  160. printf("%8d %d width= %d samples\n",
  161. _bitsRecorded, bit, _sampleWidth);
  162. #endif
  163. #endif
  164. _sampleWidth = 0;
  165. if(bit!=2) { // valid bit
  166. if(this->_addBuffer(bit, _samplesProcessed-2)) { // add bit to the buffer
  167. // found sync!
  168. int begin = (_bufferIndex - 78 + 80) % 80; // beggining of timecode
  169. _syncSample = _ringBuffer[begin].sample; // return the sample at bit zero!
  170. // XXX this is not terribly accurate!
  171. // Since we greedy decode tc before the final 2 bits are read
  172. // we add an approximate 2 bits of samples back in!
  173. // Since at 44.1KHz 2 bits is approximately 60 samples we
  174. // have definitely lost some accuracy.
  175. // (If people care about this value, we should wait until the
  176. // entire TC is decoded and accurately determine this value!)
  177. _endSample = _samplesProcessed + 2 * _bitWidth;
  178. timeCodeFound = 1;
  179. _validTimeCode = 1;
  180. } /* sync found */
  181. } /* valid bit */
  182. else {
  183. _validTimeCode = 0;
  184. }
  185. } /* new bit */
  186. }
  187. *bufferSize-= index;
  188. (*buf)+= index;
  189. return(timeCodeFound);
  190. }