void
StreamCoderTest :: testGetters()
{
  int refcount=0;
  h->setupReading(h->SAMPLE_FILE);

  for(int i = 0; i< h->num_streams; i++)
  {
    RefPointer<IStream> stream;
    RefPointer<ICodec> codec;
    RefPointer<IRational> rational;
    stream = h->streams[i];
    coder = h->coders[i];

    VS_TUT_ENSURE_EQUALS("invalid direction", coder->getDirection(),
        IStreamCoder::DECODING);

    codec = coder->getCodec();
    refcount = codec->getCurrentRefCount();
    {
      LoggerStack stack;
      stack.setGlobalLevel(Logger::LEVEL_ERROR, false);
      coder->setCodec(codec.value());
    }
    // ensure that one release and one acquire happens
    VS_TUT_ENSURE_EQUALS("invalid releasing or acquiring of codec",
        codec->getCurrentRefCount(),
        refcount);

    VS_TUT_ENSURE_EQUALS("wrong codec type",
        codec->getType(),
        h->expected_codec_types[i]);
    VS_TUT_ENSURE_EQUALS("wrong codec id",
        codec->getID(),
        h->expected_codec_ids[i]);

    if (codec->getType() == ICodec::CODEC_TYPE_AUDIO)
    {
      if (h->expected_sample_rate)
        VS_TUT_ENSURE_EQUALS("unexpected sample rate",
            coder->getSampleRate(),
            h->expected_sample_rate
        );
      if (h->expected_channels)
        VS_TUT_ENSURE_EQUALS("unexpected sample rate",
            coder->getChannels(),
            h->expected_channels
        );

    } else if (codec->getType() == ICodec::CODEC_TYPE_VIDEO)
    {
      if (h->expected_width)
        VS_TUT_ENSURE_EQUALS("unexpected width",
            coder->getWidth(),
            h->expected_width
        );
      if (h->expected_height)
              VS_TUT_ENSURE_EQUALS("unexpected height",
                  coder->getHeight(),
                  h->expected_height
              );
      if (h->expected_gops)
        VS_TUT_ENSURE_EQUALS("unexpected group of pictures",
            coder->getNumPicturesInGroupOfPictures(),
            h->expected_gops
        );
      if (h->expected_pixel_format != IPixelFormat::NONE)
        VS_TUT_ENSURE_EQUALS("unexpected group of pictures",
            coder->getPixelType(),
            h->expected_pixel_format
        );
      if (h->expected_time_base)
      {
        rational = coder->getTimeBase();
        VS_TUT_ENSURE_DISTANCE("unexpected time base",
            rational->getDouble(),
            h->expected_time_base,
            0.0001
        );
        rational = stream->getTimeBase();
        VS_TUT_ENSURE_DISTANCE("unexpected time base",
            rational->getDouble(),
            h->expected_time_base,
            0.0001
        );

      }
    }
    else
    {
      VS_LOG_ERROR("Unexpected type of codec");
      TS_ASSERT(false);
    }
  }
}
void
MediaPictureResamplerTest::testRescale() {

  TestData::Fixture* fixture=mFixtures.getFixture("testfile_h264_mp4a_tmcd.mov");
  TS_ASSERT(fixture);
  char filepath[2048];
  mFixtures.fillPath(fixture, filepath, sizeof(filepath));

  RefPointer<Demuxer> source = Demuxer::make();

  source->open(filepath, 0, false, true, 0, 0);

  int32_t numStreams = source->getNumStreams();
  TS_ASSERT_EQUALS(fixture->num_streams, numStreams);

  int32_t streamToDecode = 0;
  RefPointer<DemuxerStream> stream = source->getStream(streamToDecode);
  TS_ASSERT(stream);
  RefPointer<Decoder> decoder = stream->getDecoder();
  TS_ASSERT(decoder);
  RefPointer<Codec> codec = decoder->getCodec();
  TS_ASSERT(codec);
  TS_ASSERT_EQUALS(Codec::CODEC_ID_H264, codec->getID());

  decoder->open(0, 0);

  TS_ASSERT_EQUALS(PixelFormat::PIX_FMT_YUV420P, decoder->getPixelFormat());

  // now, let's start a decoding loop.
  RefPointer<MediaPacket> packet = MediaPacket::make();

  RefPointer<MediaPicture> picture = MediaPicture::make(
      decoder->getWidth(),
      decoder->getHeight(),
      decoder->getPixelFormat());

  int32_t rescaleW = decoder->getWidth()/4;
  int32_t rescaleH = (int32_t)(decoder->getHeight()*5.22);
  PixelFormat::Type rescaleFmt = PixelFormat::PIX_FMT_RGBA;
  RefPointer<MediaPicture> rescaled = MediaPicture::make(
      rescaleW, rescaleH, rescaleFmt);
  RefPointer<MediaPictureResampler> resampler =
      MediaPictureResampler::make(rescaled->getWidth(),
          rescaled->getHeight(),
          rescaled->getFormat(),
          picture->getWidth(),
          picture->getHeight(),
          picture->getFormat(),
          0);
  resampler->open();

  int32_t frameNo = 0;
  while(source->read(packet.value()) >= 0) {
    // got a packet; now we try to decode it.
    if (packet->getStreamIndex() == streamToDecode &&
        packet->isComplete()) {
      int32_t bytesRead = 0;
      int32_t byteOffset=0;
      do {
        bytesRead = decoder->decodeVideo(picture.value(), packet.value(), byteOffset);
        if (picture->isComplete()) {
          writePicture("MediaPictureResamplerTest_testRescaleVideo",
              &frameNo, picture.value(), resampler.value(), rescaled.value());
        }
        byteOffset += bytesRead;
      } while(byteOffset < packet->getSize());
      // now, handle the case where bytesRead is 0; we need to flush any
      // cached packets
      do {
        decoder->decodeVideo(picture.value(), 0, 0);
        if (picture->isComplete()) {
          writePicture("MediaPictureResamplerTest_testRescaleVideo", &frameNo, picture.value(),
              resampler.value(), rescaled.value());
        }
      } while (picture->isComplete());
    }
    if ((int32_t)(frameNo/30) > 10)
      // 20 pictures should be enough to see if it's working.
      break;
  }
  source->close();
}