void
MediaAudioTest::testCreation() {
  const int32_t numSamples = 1024;
  const int32_t sampleRate = 22050;
  const int32_t channels = 8;
  const AudioChannel::Layout layout = AudioChannel::CH_LAYOUT_7POINT1;
  const AudioFormat::Type format = AudioFormat::SAMPLE_FMT_S16P;

  // now let's test invalid methods.
  RefPointer<MediaAudio> audio;

  {
    LoggerStack stack;
    stack.setGlobalLevel(Logger::LEVEL_ERROR, false);

    TS_ASSERT_THROWS(MediaAudio::make(-1, sampleRate, channels, layout, format),
        HumbleInvalidArgument);
  }

  {
    LoggerStack stack;
    stack.setGlobalLevel(Logger::LEVEL_ERROR, false);

    TS_ASSERT_THROWS(MediaAudio::make(numSamples, -1, channels, layout, format),
        HumbleInvalidArgument);
  }

  {
    LoggerStack stack;
    stack.setGlobalLevel(Logger::LEVEL_ERROR, false);

    TS_ASSERT_THROWS(MediaAudio::make(numSamples, sampleRate, -1, layout, format),
        HumbleInvalidArgument);
  }

  {
    LoggerStack stack;
    stack.setGlobalLevel(Logger::LEVEL_ERROR, false);

    TS_ASSERT_THROWS(MediaAudio::make(numSamples, sampleRate, channels + 1, layout,
        format), HumbleInvalidArgument);
  }

  {
    LoggerStack stack;
    stack.setGlobalLevel(Logger::LEVEL_ERROR, false);

    TS_ASSERT_THROWS(MediaAudio::make(numSamples, sampleRate, channels, layout,
        AudioFormat::SAMPLE_FMT_NONE), HumbleInvalidArgument);
  }

  // And this should be valid
  audio = MediaAudio::make(numSamples, sampleRate, channels + 1,
      AudioChannel::CH_LAYOUT_UNKNOWN, format);
  TS_ASSERT(audio);

  audio = MediaAudio::make(numSamples, sampleRate, channels, layout, format);
  TS_ASSERT(audio);

  // now let's try getting the data
  RefPointer<Buffer> buf;

  TS_ASSERT_EQUALS(numSamples, audio->getMaxNumSamples());
  TS_ASSERT_EQUALS(numSamples, audio->getNumSamples());
  TS_ASSERT(!audio->isComplete());
  TS_ASSERT_EQUALS(channels, audio->getChannels());
  TS_ASSERT_EQUALS(channels, audio->getNumDataPlanes());
  TS_ASSERT_EQUALS(layout, audio->getChannelLayout());
  TS_ASSERT_EQUALS(sampleRate, audio->getSampleRate());
  TS_ASSERT_EQUALS(format, audio->getFormat());

  {
    LoggerStack stack;
    stack.setGlobalLevel(Logger::LEVEL_ERROR, false);

    TS_ASSERT_THROWS(
        audio->setNumSamples(audio->getMaxNumSamples()+1),
        HumbleInvalidArgument);
    TS_ASSERT_THROWS(audio->setNumSamples(-1), HumbleInvalidArgument);
    TS_ASSERT_THROWS(audio->setNumSamples(0), HumbleInvalidArgument);
  }

  audio->setNumSamples(1);
  TS_ASSERT_EQUALS(1, audio->getNumSamples());
  audio->setComplete(true);
  TS_ASSERT(audio->isComplete());

  for (int i = 0; i < channels; i++) {
    buf = audio->getData(i);
    TS_ASSERT(buf);
    TS_ASSERT_EQUALS(audio->getDataPlaneSize(i),
        audio->getNumSamples()*AudioFormat::getBytesPerSample(audio->getFormat())*(audio->isPlanar()?1:audio->getChannels()));
  }
  // now let's try packed audio
  audio = MediaAudio::make(numSamples, sampleRate, channels, layout,
      AudioFormat::getPackedSampleFormat(format));
  TS_ASSERT(audio);
  TS_ASSERT_EQUALS(AudioFormat::getPackedSampleFormat(format),
      audio->getFormat());
  TS_ASSERT_EQUALS(channels, audio->getChannels());
  TS_ASSERT_EQUALS(1, audio->getNumDataPlanes());

  buf = audio->getData(0);
  TS_ASSERT(buf);
  for (int i = 1; i < channels; i++) {
    LoggerStack stack;
    stack.setGlobalLevel(Logger::LEVEL_ERROR, false);
    TS_ASSERT_THROWS(audio->getData(i), HumbleInvalidArgument);
  }

}
void
MediaAudioTest::testCopy() {
  const int32_t numSamples = 155; // I choose an odd number because HV will align up to 32
  const int32_t sampleRate = 22050;
  const int32_t channels = 15; // choose a large # of channels to make sure we expand the Frame
  const AudioChannel::Layout layout = AudioChannel::CH_LAYOUT_UNKNOWN;
  const AudioFormat::Type format = AudioFormat::SAMPLE_FMT_DBLP;

  int32_t bufSize = AudioFormat::getBufferSizeNeeded(numSamples, channels,
      format);
  // test that there is rounding up
  int32_t minSize = AudioFormat::getBytesPerSample(format) * numSamples
      * channels;
  TS_ASSERT_LESS_THAN(minSize, bufSize);

  RefPointer<Buffer> src = Buffer::make(0, bufSize);
  double* srcData = (double*) src->getBytes(0, bufSize);

  // now, let's go nuts!
  for (size_t i = 0; i < bufSize / sizeof(double); i++) {
    srcData[i] = i;
  }

  RefPointer<MediaAudio> audio;
  audio = MediaAudio::make(src.value(), numSamples, sampleRate, channels,
      layout, format);
  TS_ASSERT(audio);

  TS_ASSERT_EQUALS(channels, audio->getNumDataPlanes());

  bool tests[] =
    { true, false };
  for (size_t i = 0; i < sizeof(tests) / sizeof(*tests); i++) {

    // now let's make a copy
    RefPointer<MediaAudio> copy = MediaAudio::make(audio.value(), tests[i]);

    TS_ASSERT_EQUALS(copy->getMaxNumSamples(), audio->getMaxNumSamples());
    TS_ASSERT_EQUALS(copy->getNumSamples(), audio->getNumSamples());
    TS_ASSERT_EQUALS(copy->getChannels(), audio->getChannels());
    TS_ASSERT_EQUALS(copy->getNumDataPlanes(), audio->getNumDataPlanes());
    TS_ASSERT_EQUALS(copy->getChannelLayout(), audio->getChannelLayout());
    TS_ASSERT_EQUALS(copy->getSampleRate(), audio->getSampleRate());
    TS_ASSERT_EQUALS(copy->getFormat(), audio->getFormat());

    for (int32_t j = 0; j < audio->getNumDataPlanes(); j++) {
      RefPointer<Buffer> srcBuf = audio->getData(j);
      RefPointer<Buffer> dstBuf = copy->getData(j);

      int32_t planeSize = srcBuf->getBufferSize();
      TS_ASSERT_EQUALS(planeSize, dstBuf->getBufferSize());

      uint8_t* srcBytes = (uint8_t*) srcBuf->getBytes(0, planeSize);
      uint8_t* dstBytes = (uint8_t*) dstBuf->getBytes(0, planeSize);
      if (tests[i]) {
        for (int32_t k = 0; k < planeSize; k++) {
          // should be byte-by-byte the same
          TS_ASSERT_EQUALS(srcBytes[k], dstBytes[k]);
        }
      } else {
        TS_ASSERT_EQUALS(srcBytes, dstBytes);
      }
    }
  }

}
void
StreamCoderTest :: testTimestamps()
{
  LoggerStack stack;
  stack.setGlobalLevel(Logger::LEVEL_WARN, false);
  Helper hr;
  hr.setupReading("testfile_h264_mp4a_tmcd.mov");
  RefPointer<IStreamCoder> ac;
  RefPointer<IStreamCoder> vc;
  if (hr.first_input_audio_stream >= 0)
    ac = hr.coders[hr.first_input_audio_stream];

  if (hr.first_input_video_stream >= 0)
    vc = hr.coders[hr.first_input_video_stream];

  hw->setupWriting("StreamCoderTest_7_output.flv",
      vc ? "flv" : 0,
      ac ? "libmp3lame" : 0,
      "flv",
      vc ? vc->getPixelType() : IPixelFormat::YUV420P,
      vc ? vc->getWidth() : 0,
      vc ? vc->getHeight() : 0,
      false,
      ac ? ac->getSampleRate() : 0,
      ac ? ac->getChannels() : 0,
      true);

  RefPointer<IVideoPicture> frame;

  if (vc) {
    frame = IVideoPicture::make(
      vc->getPixelType(),
      vc->getWidth(),
      vc->getHeight());
  }
  RefPointer<IAudioSamples> samples;
  if (ac) {
    samples = IAudioSamples::make(10000,
        ac->getChannels());
  }
  int retval = -1;
  int numReads = 0;
  int maxReads = 10000;

  while ((retval = hr.readNextDecodedObject(samples.value(), frame.value())) > 0
      && numReads < maxReads)
  {
    int result = retval;
    numReads++;
    VS_TUT_ENSURE("should only return 1 or 2", retval == 1 || retval == 2);

    if (retval == 1)
    {
      VS_TUT_ENSURE("something should be complete...", samples->isComplete());
      retval = hw->writeSamples(samples.value());
      VS_TUT_ENSURE("could not write audio", retval >= 0);
    }

    if (retval == 2)
    {
      VS_TUT_ENSURE("something should be complete...", frame->isComplete());
      retval = hw->writeFrame(frame.value());
      VS_TUT_ENSURE("could not write video", retval >= 0);
    }

    {
      RefPointer<IRational> timebase = hr.coders[hr.packet->getStreamIndex()]->getTimeBase();
      ICodec::Type type = hr.coders[hr.packet->getStreamIndex()]->getCodecType();
      (void) type;
      (void) result;
      VS_LOG_TRACE("Packet stream: %d.%d; pts: %lld; dts: %lld; spts: %lld; fpts: %lld; tb: %d/%d; ret: %d,"
          "samps: %d",
          hr.packet->getStreamIndex(),
          type,
          hr.packet->getPts(),
          hr.packet->getDts(),
          samples ? samples->getPts() : -1,
          frame ? frame->getPts() : -1,
          timebase->getNumerator(), timebase->getDenominator(),
          result,
          samples ? samples->getNumSamples() : 0);
    }


  }
  VS_TUT_ENSURE("should return some data",
      numReads > 0);
}
void
AudioResamplerTest :: testResamplingAudio()
{
  RefPointer<IAudioResampler> resampler = IAudioResampler::make(2, 1,
      44100, 22050);
  RefPointer<IAudioSamples> samples = 0;
  RefPointer<IAudioSamples> resamples = 0;
  int numSamples = 0;
  int numPackets = 0;
  int retval = -1;
  samples = IAudioSamples::make(1024, 1);
  VS_TUT_ENSURE("got no samples", samples);
  resamples = IAudioSamples::make(1024, 2);
  VS_TUT_ENSURE("got no samples", samples);

  h->setupReading(h->SAMPLE_FILE);

  RefPointer<IPacket> packet = IPacket::make();

  hw->setupWriting("AudioResamplerTest_3_output.flv", 0, "libmp3lame", "flv");
  int outStream = hw->first_output_audio_stream;
  VS_TUT_ENSURE("Could not find an audio stream in the output", outStream >= 0);
  int inStream = h->first_input_audio_stream;
  VS_TUT_ENSURE("Could not find an audio stream in the input", inStream >= 0);

  RefPointer<IStreamCoder> ic = h->coders[inStream];
  RefPointer<IStreamCoder> oc = hw->coders[outStream];
  RefPointer<IPacket> opacket = IPacket::make();
  VS_TUT_ENSURE("! opacket", opacket);

  // Set the output coder correctly.
  int outChannels = 2;
  int outRate = 44100;
  resampler = IAudioResampler::make(outChannels, ic->getChannels(),
      outRate, ic->getSampleRate());
  oc->setSampleRate(outRate);
  oc->setChannels(outChannels);
  oc->setBitRate(ic->getBitRate());

  int maxSamples = 10 * ic->getSampleRate(); // 10 seconds

  retval = ic->open();
  VS_TUT_ENSURE("Could not open input coder", retval >= 0);
  retval = oc->open();
  VS_TUT_ENSURE("Could not open output coder", retval >= 0);

  // write header
  retval = hw->container->writeHeader();
  VS_TUT_ENSURE("could not write header", retval >= 0);

  while (h->container->readNextPacket(packet.value()) == 0
      && numSamples < maxSamples)
  {
    if (packet->getStreamIndex() == inStream)
    {
      int offset = 0;

      numPackets++;

      while (offset < packet->getSize())
      {
        retval = ic->decodeAudio(
            samples.value(),
            packet.value(),
            offset);
        VS_TUT_ENSURE("could not decode any audio",
            retval > 0);
        offset += retval;
        VS_TUT_ENSURE("could not write any samples",
            samples->getNumSamples() > 0);
        numSamples += samples->getNumSamples();
        resamples = IAudioSamples::make((samples->getNumSamples()*2),2);
        // now, resample the audio
        retval = resampler->resample(resamples.value(), samples.value(), 0);
        VS_TUT_ENSURE("could not resample", retval > 0);
        VS_TUT_ENSURE("no resamples", resamples->getNumSamples() > 0);
        VS_TUT_ENSURE_EQUALS("wrong sample rate", resamples->getSampleRate(),
            outRate);
        VS_TUT_ENSURE_EQUALS("wrong channels", resamples->getChannels(),
            outChannels);
        // now, write out the packets.
        unsigned int samplesConsumed = 0;
        do {
          retval = oc->encodeAudio(opacket.value(), resamples.value(),
              samplesConsumed);
          VS_TUT_ENSURE("Could not encode any audio", retval >= 0);
          samplesConsumed += (unsigned int)retval;
          VS_LOG_TRACE("packet: %d; is: %d; os: %d",
              numPackets, numSamples, samplesConsumed);

          if (opacket->isComplete())
          {
            VS_TUT_ENSURE("could not encode audio", opacket->getSize() > 0);
            RefPointer<IBuffer> encodedBuffer = opacket->getData();
            VS_TUT_ENSURE("no encoded data", encodedBuffer);
            VS_TUT_ENSURE("less data than there should be",
                encodedBuffer->getBufferSize() >=
                opacket->getSize());
            retval = hw->container->writePacket(opacket.value());
            VS_TUT_ENSURE("could not write packet", retval >= 0);
          }
          // keep going until we've encoded all samples in this buffer.
        } while (samplesConsumed < resamples->getNumSamples());
      }
    }
  }
  // sigh; it turns out that to flush the encoding buffers you need to
  // ask the encoder to encode a NULL set of samples.  So, let's do that.
  retval = oc->encodeAudio(opacket.value(), 0, 0);
  VS_TUT_ENSURE("Could not encode any audio", retval >= 0);
  if (retval > 0)
  {
    retval = hw->container->writePacket(opacket.value());
    VS_TUT_ENSURE("could not write packet", retval >= 0);
  }

  retval = hw->container->writeTrailer();
  VS_TUT_ENSURE("! writeTrailer", retval >= 0);

  retval = ic->close();
  VS_TUT_ENSURE("! close", retval >= 0);
  retval = oc->close();

  VS_TUT_ENSURE("! close", retval >= 0);
  VS_TUT_ENSURE("could not get any audio packets", numPackets > 0);
  VS_TUT_ENSURE("could not decode any audio", numSamples > 0);
}
示例#5
0
void
EncoderTest::testRegression36Internal (const Codec::ID codecId,
                                       const int32_t numSamples,
                                       const int32_t sampleRate,
                                       const int32_t channels,
                                       const AudioChannel::Layout channelLayout,
                                       const AudioFormat::Type audioFormat,
                                       const int64_t bitRate,
                                       const char* testOutputName)
{
  VS_LOG_DEBUG("Output filename: %s", testOutputName);
  RefPointer<Codec> codec = Codec::findEncodingCodec (codecId);
  RefPointer<Encoder> encoder = Encoder::make (codec.value ());
  RefPointer<FilterGraph> graph = FilterGraph::make ();
  RefPointer<MediaAudio> audio = MediaAudio::make (numSamples, sampleRate,
                                                   channels, channelLayout,
                                                   audioFormat);
  // set the encoder properties we need
  encoder->setSampleRate (audio->getSampleRate ());
  encoder->setSampleFormat (audio->getFormat ());
  encoder->setChannelLayout (audio->getChannelLayout ());
  encoder->setChannels (audio->getChannels ());
  encoder->setProperty ("b", (int64_t) (bitRate));
  RefPointer<Rational> tb = Rational::make (1, sampleRate);
  encoder->setTimeBase (tb.value ());
  // create an output muxer
  RefPointer<Muxer> muxer = Muxer::make (testOutputName, 0, 0);
  RefPointer<MuxerFormat> format = muxer->getFormat ();
  if (format->getFlag (MuxerFormat::GLOBAL_HEADER))
    encoder->setFlag (Encoder::FLAG_GLOBAL_HEADER, true);

  // open the encoder
  encoder->open (0, 0);
  RefPointer<FilterAudioSink> fsink = graph->addAudioSink (
      "out", audio->getSampleRate (), audio->getChannelLayout (),
      audio->getFormat ());
  // Generate a 220 Hz sine wave with a 880 Hz beep each second, for 10 seconds.
  graph->open ("sine=frequency=220:beep_factor=4:duration=11[out]");
  fsink->setFrameSize (numSamples);
  // add a stream for the encoded packets
  {
    RefPointer<MuxerStream> stream = muxer->addNewStream (encoder.value ());
  }      // and open the muxer
  muxer->open (0, 0);
  // now we're (in theory) ready to start writing data.
  int32_t numCompletePackets = 0;
  RefPointer<MediaPacket> packet;
  // Get one audio packet that is larger than the frame-size.
  fsink->getAudio (audio.value ());
  TS_ASSERT(audio->isComplete ());
  TS_ASSERT_EQUALS(audio->getNumSamples (), sampleRate);
  audio->setTimeStamp (0);
  // let's encode
  packet = MediaPacket::make ();
  encoder->encodeAudio (packet.value (), audio.value ());
  if (packet->isComplete ())
  {
    muxer->write (packet.value (), false);
  }
  // now flush the encoder
  do
  {
    packet = MediaPacket::make ();
    encoder->encodeAudio (packet.value (), 0);
    if (packet->isComplete ())
    {
      muxer->write (packet.value (), false);
      ++numCompletePackets;
    }
  }
  while (packet->isComplete ());
  muxer->close ();
  if (!(codec->getCapabilities() & Codec::CAP_VARIABLE_FRAME_SIZE)) {
    const int32_t numExpectedPackets = audio->getNumSamples()
        / encoder->getFrameSize();

    VS_LOG_DEBUG("%ld vs %ld; framesize: %ld", numCompletePackets, numExpectedPackets, encoder->getFrameSize());
    TS_ASSERT(numCompletePackets > 10);
  }
}
示例#6
0
void
EncoderTest::testEncodeAudio() {
  Logger::setGlobalIsLogging(Logger::LEVEL_TRACE, false);
  LoggerStack stack;
  stack.setGlobalLevel(Logger::LEVEL_INFO, false);

  const bool isMemCheck = getenv("VS_TEST_MEMCHECK") ? true : false;
  const int32_t sampleRate = 44100;
  const int32_t maxSamples = isMemCheck ? sampleRate*0.5 : sampleRate*10;
  const int32_t numSamples = 1024;
  const AudioChannel::Layout channelLayout = AudioChannel::CH_LAYOUT_STEREO;
  const int32_t channels = AudioChannel::getNumChannelsInLayout(channelLayout);
  const AudioFormat::Type audioFormat = AudioFormat::SAMPLE_FMT_S16;
  RefPointer<Codec> codec = Codec::findEncodingCodec(Codec::CODEC_ID_AAC);
  RefPointer<Encoder> encoder = Encoder::make(codec.value());

  RefPointer<FilterGraph> graph = FilterGraph::make();

  RefPointer<MediaAudio> audio = MediaAudio::make(numSamples, sampleRate, channels, channelLayout,
      audioFormat);

  // set the encoder properties we need
  encoder->setSampleRate(audio->getSampleRate());
  encoder->setSampleFormat(audio->getFormat());
  encoder->setChannelLayout(audio->getChannelLayout());
  encoder->setChannels(audio->getChannels());
  encoder->setProperty("b", (int64_t)64000); // bitrate
  RefPointer<Rational> tb = Rational::make(1,25);
  encoder->setTimeBase(tb.value());

  // create an output muxer
  RefPointer<Muxer> muxer = Muxer::make("EncoderTest_encodeAudio.mp4", 0, 0);
  RefPointer<MuxerFormat> format = muxer->getFormat();
  if (format->getFlag(MuxerFormat::GLOBAL_HEADER))
    encoder->setFlag(Encoder::FLAG_GLOBAL_HEADER, true);

  // open the encoder
  encoder->open(0, 0);

  RefPointer<FilterAudioSink> fsink = graph->addAudioSink("out", audio->getSampleRate(), audio->getChannelLayout(), audio->getFormat());

  // Generate a 220 Hz sine wave with a 880 Hz beep each second, for 10 seconds.
  graph->open("sine=frequency=660:beep_factor=4:duration=11[out]");
  // Generate an amplitude modulated signal
  //graph->open("aevalsrc=sin(10*2*PI*t)*sin(880*2*PI*t)[out]");

  // add a stream for the encoded packets
  {
    RefPointer<MuxerStream> stream = muxer->addNewStream(encoder.value());
  }

  // and open the muxer
  muxer->open(0, 0);

  // now we're (in theory) ready to start writing data.
  int32_t numFrames = 0;
  RefPointer<MediaPacket> packet;

  while(fsink->getAudio(audio.value()) >= 0 && audio->isComplete() && numFrames*audio->getNumSamples() < maxSamples) {
    audio->setTimeStamp(numFrames*audio->getNumSamples());

    // let's encode
    packet = MediaPacket::make();
    encoder->encodeAudio(packet.value(), audio.value());
    if (packet->isComplete()) {
      muxer->write(packet.value(), false);
    }
    ++numFrames;
  }
  // now flush the encoder
  do {
    packet = MediaPacket::make();
    encoder->encodeAudio(packet.value(), 0);
    if (packet->isComplete()) {
      muxer->write(packet.value(), false);
    }
  } while (packet->isComplete());

  muxer->close();
}