All pastes #1788953 Raw Edit

Stuff

public text v1 · immutable
#1788953 ·published 2010-02-07 11:29 UTC
rendered paste body
diff --git a/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/DVDAudioEncoderAC3.cpp b/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/DVDAudioEncoderAC3.cpp
new file mode 100644
index 0000000..7615740
--- /dev/null
+++ b/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/DVDAudioEncoderAC3.cpp
@@ -0,0 +1,139 @@
+/*
+ *      Copyright (C) 2005-2010 Team XBMC
+ *      http://www.xbmc.org
+ *
+ *  This Program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This Program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with XBMC; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#include "DVDAudioEncoderAC3.h"
+#include <string.h>
+
+CDVDAudioEncoderAC3::CDVDAudioEncoderAC3():
+  m_codecCtx(NULL)
+{
+}
+
+CDVDAudioEncoderAC3::~CDVDAudioEncoderAC3()
+{
+}
+
+bool CDVDAudioEncoderAC3::Initialize(unsigned int channels, enum PCMChannels *channelMap, unsigned int bitsPerSample, unsigned int sampleRate)
+{
+  if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load())
+    return false;
+
+  AVCodec *codec;
+  codec = m_dllAvCodec.avcodec_find_encoder(CODEC_ID_AC3);
+  if (!codec)
+    return false;
+
+  m_codecCtx = m_dllAvCodec.avcodec_alloc_context();
+  m_codecCtx->bit_rate    = 448000;
+  m_codecCtx->sample_rate = sampleRate;
+  m_codecCtx->channels    = channels;
+
+  switch(bitsPerSample)
+  {
+    case 8 : m_codecCtx->sample_fmt = SAMPLE_FMT_U8 ; break;
+    case 16: m_codecCtx->sample_fmt = SAMPLE_FMT_S16; break;
+    case 32: m_codecCtx->sample_fmt = SAMPLE_FMT_S32; break;
+    default:
+      m_dllAvUtil.av_freep(&m_codecCtx);
+      return false;
+  }
+
+  if (m_dllAvCodec.avcodec_open(m_codecCtx, codec))
+  {
+    m_dllAvUtil.av_freep(&m_codecCtx);
+    return false;
+  }
+
+  return true;
+}
+
+void CDVDAudioEncoderAC3::Reset()
+{
+  while(!m_packets.empty())
+  {
+    DSPNullPacketInfo *info = m_packets.front();
+    delete[] info->data;
+    delete   info;
+  }
+}
+
+unsigned int CDVDAudioEncoderAC3::GetBitRate()
+{
+  return m_codecCtx->bit_rate;
+}
+
+CodecID CDVDAudioEncoderAC3::GetCodecID()
+{
+  return CODEC_ID_AC3;
+}
+
+int CDVDAudioEncoderAC3::Encode(void *data, int size)
+{
+  DSPNullPacketInfo *info;
+
+  uint8_t buffer[AC3_FRAME_SIZE];
+  unsigned int encodedBytes = m_dllAvCodec.avcodec_encode_audio(m_codecCtx, buffer, AC3_FRAME_SIZE, (short*)data);
+  if (encodedBytes == 0)
+    return 0;
+
+  /* push the encoded data into our buffer */
+  info          = new DSPNullPacketInfo();
+  info->size    = size;
+  info->data    = new int8_t[encodedBytes];
+  info->pos     = info->data;
+
+  memcpy(info->data, buffer, encodedBytes);
+  m_packets.push_back(info);
+  return size;
+}
+
+int CDVDAudioEncoderAC3::GetData(void *data, int size)
+{
+  int ret = 0;
+  while(ret < size && !m_packets.empty())
+  {
+    DSPNullPacketInfo *info = m_packets.front();
+
+    /* figure out how much data we can copy */
+    int available = info->size - (info->pos - info->data);
+    int use;
+    if (available > size)
+      use = size;
+    else
+      use = available;
+
+    /* copy it into the destination buffer */
+    memcpy(data, info->pos, use);
+    info->pos += use;
+    ret       += use;
+
+    /* if this list item is empty, then remove it */
+    if (info->size - (info->pos - info->data) == 0)
+    {
+      delete[] info->data;
+      delete   info;
+      m_packets.pop_front();
+    }
+  }
+
+  return ret;
+}
+
diff --git a/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/DVDAudioEncoderAC3.h b/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/DVDAudioEncoderAC3.h
new file mode 100644
index 0000000..86f5990
--- /dev/null
+++ b/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/DVDAudioEncoderAC3.h
@@ -0,0 +1,62 @@
+#pragma once
+#ifndef _DVDAUDIOENC_AC3_H_
+#define _DVDAUDIOENC_AC3_H_
+/*
+ *      Copyright (C) 2005-2010 Team XBMC
+ *      http://www.xbmc.org
+ *
+ *  This Program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This Program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with XBMC; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#include <list>
+#include "IDVDAudioEncoder.h"
+#include "Codecs/DllAvCodec.h"
+#include "Codecs/DllAvFormat.h"
+
+class CDVDAudioEncoderAC3: public IDVDAudioEncoder
+{
+public:
+  CDVDAudioEncoderAC3();
+  virtual ~CDVDAudioEncoderAC3();
+  virtual bool Initialize(unsigned int channels, enum PCMChannels *channelMap, unsigned int bitsPerSample, unsigned int sampleRate);
+  virtual void Reset();
+
+  /* returns this DSPs output format */
+  virtual unsigned int      GetChannels  ();
+  virtual unsigned int      GetBitRate   ();
+  virtual CodecID           GetCodecID   ();
+
+  /* add/get packets to/from the DSP */
+  virtual int Encode (void *data, int size);
+  virtual int GetData(void *data, int size);
+
+private:
+  typedef struct
+  {
+    unsigned int  size;
+    int8_t       *data;
+    int8_t       *pos;
+  } DSPNullPacketInfo;
+
+  std::list<DSPNullPacketInfo*> m_packets;
+  DllAvCodec m_dllAvCodec;
+  DllAvUtil  m_dllAvUtil;
+
+  AVCodecContext *m_codecCtx;
+};
+
+#endif
diff --git a/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/IDVDAudioEncoder.h b/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/IDVDAudioEncoder.h
new file mode 100644
index 0000000..3113ed0
--- /dev/null
+++ b/xbmc/cores/dvdplayer/DVDCodecs/Audio/Encoders/IDVDAudioEncoder.h
@@ -0,0 +1,46 @@
+#pragma once
+#ifndef _IDVDAUDIOENC_
+#define _IDVDAUDIOENC_
+/*
+ *      Copyright (C) 2005-2010 Team XBMC
+ *      http://www.xbmc.org
+ *
+ *  This Program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This Program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with XBMC; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#include "utils/PCMRemap.h"
+#include "DVDStreamInfo.cpp"
+
+class IDVDAudioEncoder
+{
+public:
+  IDVDAudioEncoder() {};
+  virtual ~IDVDAudioEncoder() {};
+  virtual bool Initialize(unsigned int channels, enum PCMChannels *channelMap, unsigned int bitsPerSample, unsigned int sampleRate) = 0;
+  virtual void Reset() = 0;
+
+  /* returns this DSPs output format */
+  virtual unsigned int      GetChannels     () = 0;
+  virtual unsigned int      GetBitRate      () = 0;
+  virtual CodecID           GetCodecID      () = 0;
+
+  /* add/get packets to/from the DSP */
+  virtual int Encode (void *data, int size) = 0;
+  virtual int GetData(void *data, int size) = 0;
+};
+
+#endif