home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / Riza / source / capaudiocomp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  3.9 KB  |  133 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    A/V interface library
  3. //    Copyright (C) 1998-2004 Avery Lee
  4. //
  5. //    This program is free software; you can redistribute it and/or modify
  6. //    it under the terms of the GNU General Public License as published by
  7. //    the Free Software Foundation; either version 2 of the License, or
  8. //    (at your option) any later version.
  9. //
  10. //    This program is distributed in the hope that it will be useful,
  11. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //    GNU General Public License for more details.
  14. //
  15. //    You should have received a copy of the GNU General Public License
  16. //    along with this program; if not, write to the Free Software
  17. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include <vd2/system/thread.h>
  20. #include <vd2/system/cpuaccel.h>
  21. #include <vd2/system/math.h>
  22. #include <vd2/system/profile.h>
  23. #include <vd2/Riza/capaudiocomp.h>
  24. #include <vd2/Riza/w32audiocodec.h>
  25. #include <vd2/Priss/convert.h>
  26.  
  27. ///////////////////////////////////////////////////////////////////////////
  28.  
  29. class VDCaptureAudioCompFilter : public IVDCaptureAudioCompFilter {
  30. public:
  31.     VDCaptureAudioCompFilter();
  32.     ~VDCaptureAudioCompFilter();
  33.  
  34.     void SetChildCallback(IVDCaptureDriverCallback *pChild);
  35.     void SetSourceSplit(bool enable);
  36.  
  37.     void Init(const WAVEFORMATEX *srcFormat, const WAVEFORMATEX *dstFormat, const char *pShortNameHint);
  38.  
  39.     void GetStatus(VDCaptureAudioCompStatus&);
  40.  
  41.     void CapBegin(sint64 global_clock);
  42.     void CapEnd(const MyError *pError);
  43.     bool CapEvent(nsVDCapture::DriverEvent event, int data);
  44.     void CapProcessData(int stream, const void *data, uint32 size, sint64 timestamp, bool key, sint64 global_clock);
  45.  
  46. protected:
  47.     IVDCaptureDriverCallback *mpCB;
  48.     bool    mbSplitSource;
  49.  
  50.     VDAudioCodecW32    mCodec;
  51.     VDRTProfileChannel    mProfileChannel;
  52. };
  53.  
  54. VDCaptureAudioCompFilter::VDCaptureAudioCompFilter()
  55.     : mpCB(NULL)
  56.     , mProfileChannel("Audio compressor")
  57. {
  58. }
  59.  
  60. VDCaptureAudioCompFilter::~VDCaptureAudioCompFilter() {
  61. }
  62.  
  63. IVDCaptureAudioCompFilter *VDCreateCaptureAudioCompFilter() {
  64.     return new VDCaptureAudioCompFilter;
  65. }
  66.  
  67. void VDCaptureAudioCompFilter::SetChildCallback(IVDCaptureDriverCallback *pChild) {
  68.     mpCB = pChild;
  69. }
  70.  
  71. void VDCaptureAudioCompFilter::SetSourceSplit(bool enable) {
  72.     mbSplitSource = enable;
  73. }
  74.  
  75. void VDCaptureAudioCompFilter::Init(const WAVEFORMATEX *srcFormat, const WAVEFORMATEX *dstFormat, const char *pShortNameHint) {
  76.     mCodec.Init(srcFormat, dstFormat, true, pShortNameHint);
  77. }
  78.  
  79. void VDCaptureAudioCompFilter::GetStatus(VDCaptureAudioCompStatus& status) {
  80. }
  81.  
  82. void VDCaptureAudioCompFilter::CapBegin(sint64 global_clock) {
  83.     mCodec.Restart();
  84.  
  85.     mpCB->CapBegin(global_clock);
  86. }
  87.  
  88. void VDCaptureAudioCompFilter::CapEnd(const MyError *pError) {
  89.     mpCB->CapEnd(pError);
  90. }
  91.  
  92. bool VDCaptureAudioCompFilter::CapEvent(nsVDCapture::DriverEvent event, int data) {
  93.     return mpCB->CapEvent(event, data);
  94. }
  95.  
  96. void VDCaptureAudioCompFilter::CapProcessData(int stream, const void *data, uint32 size, sint64 timestamp, bool key, sint64 global_clock)  {
  97.     if (stream != 1) {
  98.         mpCB->CapProcessData(stream, data, size, timestamp, key, global_clock);
  99.         return;
  100.     }
  101.  
  102.     if (mbSplitSource)
  103.         mpCB->CapProcessData(-2, data, size, timestamp, key, global_clock);
  104.  
  105.     while(size > 0) {
  106.         unsigned bytes;
  107.         void *dst = mCodec.LockInputBuffer(bytes);
  108.  
  109.         if (bytes > 0) {
  110.             if (bytes > size)
  111.                 bytes = size;
  112.  
  113.             memcpy(dst, data, bytes);
  114.             data = (const char *)data + bytes;
  115.  
  116.             size -= bytes;
  117.         }
  118.  
  119.         mCodec.UnlockInputBuffer(bytes);
  120.  
  121.         mProfileChannel.Begin(0xffe0e0, "Compress");
  122.         bool progressMade = mCodec.Convert(false, !bytes);
  123.         mProfileChannel.End();
  124.         if (!progressMade)
  125.             break;
  126.  
  127.         const void *src = mCodec.LockOutputBuffer(bytes);
  128.         if (bytes > 0)
  129.             mpCB->CapProcessData(1, src, bytes, 0, true, global_clock);
  130.         mCodec.UnlockOutputBuffer(bytes);
  131.     }
  132. }
  133.