2016-08-27 19:10:27 +00:00
|
|
|
#if defined(CONF_VIDEORECORDER)
|
|
|
|
|
2016-08-27 15:51:23 +00:00
|
|
|
#include <engine/console.h>
|
2016-08-30 23:39:59 +00:00
|
|
|
#include <engine/shared/config.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <engine/storage.h>
|
2016-08-27 15:51:23 +00:00
|
|
|
|
|
|
|
#include "video.h"
|
|
|
|
|
2021-05-01 21:33:42 +00:00
|
|
|
#ifndef CONF_BACKEND_OPENGL_ES
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#else
|
|
|
|
#include <GLES3/gl3.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-30 23:39:59 +00:00
|
|
|
// This code is mostly stolen from https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/muxing.c
|
|
|
|
|
2016-08-27 19:10:27 +00:00
|
|
|
#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
const size_t FORMAT_NCHANNELS = 3;
|
2021-05-03 21:02:45 +00:00
|
|
|
const size_t FORMAT_GL_NCHANNELS = 4;
|
2020-12-02 18:11:19 +00:00
|
|
|
LOCK g_WriteLock = 0;
|
2020-01-03 20:42:53 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
CVideo::CVideo(CGraphics_Threaded *pGraphics, IStorage *pStorage, IConsole *pConsole, int Width, int Height, const char *pName) :
|
2016-08-30 23:39:59 +00:00
|
|
|
m_pGraphics(pGraphics),
|
2016-08-27 15:51:23 +00:00
|
|
|
m_pStorage(pStorage),
|
2016-08-27 19:10:27 +00:00
|
|
|
m_pConsole(pConsole),
|
|
|
|
m_VideoStream(),
|
|
|
|
m_AudioStream()
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
|
|
|
m_pPixels = 0;
|
|
|
|
|
2016-08-27 19:10:27 +00:00
|
|
|
m_pFormatContext = 0;
|
|
|
|
m_pFormat = 0;
|
2016-08-27 15:51:23 +00:00
|
|
|
m_pRGB = 0;
|
2016-08-27 19:10:27 +00:00
|
|
|
m_pOptDict = 0;
|
|
|
|
|
|
|
|
m_VideoCodec = 0;
|
|
|
|
m_AudioCodec = 0;
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
m_Width = Width;
|
|
|
|
m_Height = Height;
|
|
|
|
str_copy(m_Name, pName, sizeof(m_Name));
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2016-08-31 16:07:27 +00:00
|
|
|
m_FPS = g_Config.m_ClVideoRecorderFPS;
|
|
|
|
|
2016-08-27 15:51:23 +00:00
|
|
|
m_Recording = false;
|
2016-08-30 23:39:59 +00:00
|
|
|
m_Started = false;
|
|
|
|
m_ProcessingVideoFrame = false;
|
|
|
|
m_ProcessingAudioFrame = false;
|
|
|
|
|
|
|
|
m_NextFrame = false;
|
2020-06-22 21:59:37 +00:00
|
|
|
m_NextAudioFrame = false;
|
2016-08-31 20:04:40 +00:00
|
|
|
|
2016-08-30 23:39:59 +00:00
|
|
|
// TODO:
|
2019-10-17 05:38:00 +00:00
|
|
|
m_HasAudio = g_Config.m_ClVideoSndEnable;
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
m_SndBufferSize = g_Config.m_SndBufferSize;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
|
|
|
dbg_assert(ms_pCurrentVideo == 0, "ms_pCurrentVideo is NOT set to NULL while creating a new Video.");
|
|
|
|
|
2016-08-31 16:07:27 +00:00
|
|
|
ms_TickTime = time_freq() / m_FPS;
|
2016-08-27 15:51:23 +00:00
|
|
|
ms_pCurrentVideo = this;
|
2020-06-22 21:59:37 +00:00
|
|
|
g_WriteLock = lock_create();
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CVideo::~CVideo()
|
|
|
|
{
|
|
|
|
ms_pCurrentVideo = 0;
|
2020-06-22 21:59:37 +00:00
|
|
|
lock_destroy(g_WriteLock);
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::Start()
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2016-08-31 16:07:27 +00:00
|
|
|
char aDate[20];
|
|
|
|
str_timestamp(aDate, sizeof(aDate));
|
2019-09-27 03:06:02 +00:00
|
|
|
char aBuf[256];
|
2021-06-05 14:46:01 +00:00
|
|
|
if(str_length(m_Name) != 0)
|
2019-09-27 03:06:02 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), "videos/%s", m_Name);
|
|
|
|
else
|
|
|
|
str_format(aBuf, sizeof(aBuf), "videos/%s.mp4", aDate);
|
2016-08-31 16:07:27 +00:00
|
|
|
|
2016-08-27 19:10:27 +00:00
|
|
|
char aWholePath[1024];
|
2016-08-31 16:07:27 +00:00
|
|
|
IOHANDLE File = m_pStorage->OpenFile(aBuf, IOFLAG_WRITE, IStorage::TYPE_SAVE, aWholePath, sizeof(aWholePath));
|
2016-08-27 19:10:27 +00:00
|
|
|
|
|
|
|
if(File)
|
|
|
|
{
|
|
|
|
io_close(File);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Failed to open file for recoding video.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
avformat_alloc_output_context2(&m_pFormatContext, 0, "mp4", aWholePath);
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(!m_pFormatContext)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Failed to create formatcontext for recoding video.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pFormat = m_pFormatContext->oformat;
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
size_t NVals = FORMAT_NCHANNELS * m_Width * m_Height;
|
2021-05-03 21:02:45 +00:00
|
|
|
size_t GLNVals = FORMAT_GL_NCHANNELS * m_Width * m_Height;
|
|
|
|
m_pPixels = (uint8_t *)malloc(GLNVals * sizeof(TWGLubyte));
|
2020-06-22 21:59:37 +00:00
|
|
|
m_pRGB = (uint8_t *)malloc(NVals * sizeof(uint8_t));
|
2016-08-27 19:10:27 +00:00
|
|
|
|
|
|
|
/* Add the audio and video streams using the default format codecs
|
|
|
|
* and initialize the codecs. */
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_pFormat->video_codec != AV_CODEC_ID_NONE)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-09-13 20:49:50 +00:00
|
|
|
if(!AddStream(&m_VideoStream, m_pFormatContext, &m_VideoCodec, m_pFormat->video_codec))
|
|
|
|
return;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Failed to add VideoStream for recoding video.");
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_HasAudio && m_pFormat->audio_codec != AV_CODEC_ID_NONE)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-09-13 20:49:50 +00:00
|
|
|
if(!AddStream(&m_AudioStream, m_pFormatContext, &m_AudioCodec, m_pFormat->audio_codec))
|
|
|
|
return;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "No audio.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now that all the parameters are set, we can open the audio and
|
|
|
|
* video codecs and allocate the necessary encode buffers. */
|
2020-09-13 20:49:50 +00:00
|
|
|
if(!OpenVideo())
|
|
|
|
return;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_HasAudio)
|
2020-09-13 20:49:50 +00:00
|
|
|
if(!OpenAudio())
|
|
|
|
return;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2016-08-30 23:39:59 +00:00
|
|
|
// TODO: remove/comment:
|
2016-08-27 19:10:27 +00:00
|
|
|
av_dump_format(m_pFormatContext, 0, aWholePath, 1);
|
|
|
|
|
|
|
|
/* open the output file, if needed */
|
2020-06-22 21:59:37 +00:00
|
|
|
if(!(m_pFormat->flags & AVFMT_NOFILE))
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
int Ret = avio_open(&m_pFormatContext->pb, aWholePath, AVIO_FLAG_WRITE);
|
|
|
|
if(Ret < 0)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2016-08-31 12:09:06 +00:00
|
|
|
char aBuf[AV_ERROR_MAX_STRING_SIZE];
|
2020-06-22 21:59:37 +00:00
|
|
|
av_strerror(Ret, aBuf, sizeof(aBuf));
|
2016-08-31 12:09:06 +00:00
|
|
|
dbg_msg("video_recorder", "Could not open '%s': %s", aWholePath, aBuf);
|
2016-08-27 19:10:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(!m_VideoStream.pSwsCtx)
|
2020-01-03 20:42:53 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
m_VideoStream.pSwsCtx = sws_getCachedContext(
|
|
|
|
m_VideoStream.pSwsCtx,
|
|
|
|
m_VideoStream.pEnc->width, m_VideoStream.pEnc->height, AV_PIX_FMT_RGB24,
|
|
|
|
m_VideoStream.pEnc->width, m_VideoStream.pEnc->height, AV_PIX_FMT_YUV420P,
|
2020-09-26 19:41:58 +00:00
|
|
|
0, 0, 0, 0);
|
2020-01-03 20:42:53 +00:00
|
|
|
}
|
2016-08-27 19:10:27 +00:00
|
|
|
|
|
|
|
/* Write the stream header, if any. */
|
2020-06-22 21:59:37 +00:00
|
|
|
int Ret = avformat_write_header(m_pFormatContext, &m_pOptDict);
|
|
|
|
if(Ret < 0)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2016-08-31 12:09:06 +00:00
|
|
|
char aBuf[AV_ERROR_MAX_STRING_SIZE];
|
2020-06-22 21:59:37 +00:00
|
|
|
av_strerror(Ret, aBuf, sizeof(aBuf));
|
2016-08-31 12:09:06 +00:00
|
|
|
dbg_msg("video_recorder", "Error occurred when opening output file: %s", aBuf);
|
2016-08-27 19:10:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-27 15:51:23 +00:00
|
|
|
m_Recording = true;
|
2016-08-30 23:39:59 +00:00
|
|
|
m_Started = true;
|
|
|
|
ms_Time = time_get();
|
2020-06-22 21:59:37 +00:00
|
|
|
m_Vframe = 0;
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::Pause(bool Pause)
|
2020-02-27 10:50:18 +00:00
|
|
|
{
|
|
|
|
if(ms_pCurrentVideo)
|
2020-06-22 21:59:37 +00:00
|
|
|
m_Recording = !Pause;
|
2020-02-27 10:50:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::Stop()
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
|
|
|
m_Recording = false;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
while(m_ProcessingVideoFrame || m_ProcessingAudioFrame)
|
2016-08-30 23:39:59 +00:00
|
|
|
thread_sleep(10);
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
FinishFrames(&m_VideoStream);
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_HasAudio)
|
|
|
|
FinishFrames(&m_AudioStream);
|
2016-08-30 23:39:59 +00:00
|
|
|
|
|
|
|
av_write_trailer(m_pFormatContext);
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
CloseStream(&m_VideoStream);
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_HasAudio)
|
|
|
|
CloseStream(&m_AudioStream);
|
2020-09-26 19:41:58 +00:00
|
|
|
//fclose(m_dbgfile);
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(!(m_pFormat->flags & AVFMT_NOFILE))
|
2016-08-27 19:10:27 +00:00
|
|
|
avio_closep(&m_pFormatContext->pb);
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_pFormatContext)
|
2016-08-27 19:10:27 +00:00
|
|
|
avformat_free_context(m_pFormatContext);
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_pRGB)
|
2016-08-27 15:51:23 +00:00
|
|
|
free(m_pRGB);
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_pPixels)
|
2016-08-27 15:51:23 +00:00
|
|
|
free(m_pPixels);
|
2019-11-02 08:09:00 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(ms_pCurrentVideo)
|
2019-11-02 08:09:00 +00:00
|
|
|
delete ms_pCurrentVideo;
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::NextVideoFrameThread()
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_NextFrame && m_Recording)
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2021-02-12 12:40:29 +00:00
|
|
|
// #ifdef CONF_PLATFORM_MACOS
|
2016-08-30 23:39:59 +00:00
|
|
|
// CAutoreleasePool AutoreleasePool;
|
|
|
|
// #endif
|
2020-06-22 21:59:37 +00:00
|
|
|
m_Vseq += 1;
|
|
|
|
if(m_Vseq >= 2)
|
2019-09-28 13:22:25 +00:00
|
|
|
{
|
2020-01-06 03:57:24 +00:00
|
|
|
m_ProcessingVideoFrame = true;
|
2021-06-23 05:05:49 +00:00
|
|
|
m_VideoStream.pFrame->pts = (int64_t)m_VideoStream.pEnc->frame_number;
|
2020-09-04 23:16:12 +00:00
|
|
|
//dbg_msg("video_recorder", "vframe: %d", m_VideoStream.pEnc->frame_number);
|
2020-06-22 21:59:37 +00:00
|
|
|
|
|
|
|
ReadRGBFromGL();
|
|
|
|
FillVideoFrame();
|
|
|
|
lock_wait(g_WriteLock);
|
|
|
|
WriteFrame(&m_VideoStream);
|
|
|
|
lock_unlock(g_WriteLock);
|
2020-01-06 03:57:24 +00:00
|
|
|
m_ProcessingVideoFrame = false;
|
2019-09-28 13:22:25 +00:00
|
|
|
}
|
2016-08-30 23:39:59 +00:00
|
|
|
|
|
|
|
m_NextFrame = false;
|
|
|
|
// sync_barrier();
|
|
|
|
// m_Semaphore.signal();
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::NextVideoFrame()
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_Recording)
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2021-02-12 12:40:29 +00:00
|
|
|
// #ifdef CONF_PLATFORM_MACOS
|
2016-08-30 23:39:59 +00:00
|
|
|
// CAutoreleasePool AutoreleasePool;
|
|
|
|
// #endif
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2020-09-04 23:16:12 +00:00
|
|
|
//dbg_msg("video_recorder", "called");
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2016-08-30 23:39:59 +00:00
|
|
|
ms_Time += ms_TickTime;
|
2020-09-26 19:41:58 +00:00
|
|
|
ms_LocalTime = (ms_Time - ms_LocalStartTime) / (float)time_freq();
|
2016-08-30 23:39:59 +00:00
|
|
|
m_NextFrame = true;
|
2020-06-22 21:59:37 +00:00
|
|
|
m_Vframe += 1;
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2016-08-30 23:39:59 +00:00
|
|
|
// m_pGraphics->KickCommandBuffer();
|
|
|
|
//thread_sleep(500);
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2016-08-30 23:39:59 +00:00
|
|
|
// m_Semaphore.wait();
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::NextAudioFrameTimeline()
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_Recording && m_HasAudio)
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
//if(m_Vframe * m_AudioStream.pEnc->sample_rate / m_FPS >= m_AudioStream.pEnc->frame_number*m_AudioStream.pEnc->frame_size)
|
2020-09-26 19:41:58 +00:00
|
|
|
if(m_VideoStream.pEnc->frame_number * (double)m_AudioStream.pEnc->sample_rate / m_FPS >= (double)m_AudioStream.pEnc->frame_number * m_AudioStream.pEnc->frame_size)
|
2019-11-02 08:09:00 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
m_NextAudioFrame = true;
|
2019-11-02 08:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::NextAudioFrame(void (*Mix)(short *pFinalOut, unsigned Frames))
|
2019-11-02 08:09:00 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
if(m_NextAudioFrame && m_Recording && m_HasAudio)
|
2019-11-02 08:09:00 +00:00
|
|
|
{
|
|
|
|
m_ProcessingAudioFrame = true;
|
2020-10-26 08:57:41 +00:00
|
|
|
//dbg_msg("video_recorder", "video_frame: %lf", (double)(m_Vframe/m_FPS));
|
2020-06-22 21:59:37 +00:00
|
|
|
//if((double)(m_Vframe/m_FPS) < m_AudioStream.pEnc->frame_number*m_AudioStream.pEnc->frame_size/m_AudioStream.pEnc->sample_rate)
|
2020-09-26 19:41:58 +00:00
|
|
|
//return;
|
2019-11-02 08:09:00 +00:00
|
|
|
Mix(m_aBuffer, ALEN);
|
2020-06-22 21:59:37 +00:00
|
|
|
//m_AudioStream.pFrame->pts = m_AudioStream.pEnc->frame_number;
|
2020-09-04 23:16:12 +00:00
|
|
|
//dbg_msg("video_recorder", "aframe: %d", m_AudioStream.pEnc->frame_number);
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
// memcpy(m_AudioStream.pTmpFrame->data[0], pData, sizeof(int16_t) * m_SndBufferSize * 2);
|
2016-08-30 23:39:59 +00:00
|
|
|
//
|
2020-06-22 21:59:37 +00:00
|
|
|
// for(int i = 0; i < m_SndBufferSize; i++)
|
2016-08-30 23:39:59 +00:00
|
|
|
// {
|
|
|
|
// dbg_msg("video_recorder", "test: %d %d", ((int16_t*)pData)[i*2], ((int16_t*)pData)[i*2 + 1]);
|
|
|
|
// }
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
int DstNbSamples;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-01-25 11:14:45 +00:00
|
|
|
av_samples_fill_arrays(
|
2020-09-26 19:41:58 +00:00
|
|
|
(uint8_t **)m_AudioStream.pTmpFrame->data,
|
2020-01-25 11:14:45 +00:00
|
|
|
0, // pointer to linesize (int*)
|
2020-09-26 19:41:58 +00:00
|
|
|
(const uint8_t *)m_aBuffer,
|
2020-01-25 11:14:45 +00:00
|
|
|
2, // channels
|
2020-06-22 21:59:37 +00:00
|
|
|
m_AudioStream.pTmpFrame->nb_samples,
|
2020-01-25 11:14:45 +00:00
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
0 // align
|
|
|
|
);
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
DstNbSamples = av_rescale_rnd(
|
2019-10-31 07:36:38 +00:00
|
|
|
swr_get_delay(
|
2020-06-22 21:59:37 +00:00
|
|
|
m_AudioStream.pSwrCtx,
|
2020-09-26 19:41:58 +00:00
|
|
|
m_AudioStream.pEnc->sample_rate) +
|
|
|
|
m_AudioStream.pTmpFrame->nb_samples,
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
m_AudioStream.pEnc->sample_rate,
|
2020-09-26 19:41:58 +00:00
|
|
|
m_AudioStream.pEnc->sample_rate, AV_ROUND_UP);
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
// dbg_msg("video_recorder", "DstNbSamples: %d", DstNbSamples);
|
2019-10-31 07:36:38 +00:00
|
|
|
// fwrite(m_aBuffer, sizeof(short), 2048, m_dbgfile);
|
2016-08-27 15:51:23 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
int Ret = av_frame_make_writable(m_AudioStream.pFrame);
|
|
|
|
if(Ret < 0)
|
2020-09-13 20:49:50 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Error making frame writable");
|
|
|
|
return;
|
|
|
|
}
|
2016-08-30 23:39:59 +00:00
|
|
|
|
|
|
|
/* convert to destination format */
|
2020-06-22 21:59:37 +00:00
|
|
|
Ret = swr_convert(
|
|
|
|
m_AudioStream.pSwrCtx,
|
|
|
|
m_AudioStream.pFrame->data,
|
|
|
|
m_AudioStream.pFrame->nb_samples,
|
|
|
|
(const uint8_t **)m_AudioStream.pTmpFrame->data,
|
2020-09-26 19:41:58 +00:00
|
|
|
m_AudioStream.pTmpFrame->nb_samples);
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(Ret < 0)
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2016-08-30 23:39:59 +00:00
|
|
|
dbg_msg("video_recorder", "Error while converting");
|
2020-09-13 20:49:50 +00:00
|
|
|
return;
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 23:39:59 +00:00
|
|
|
// frame = ost->frame;
|
|
|
|
//
|
2020-06-22 21:59:37 +00:00
|
|
|
m_AudioStream.pFrame->pts = av_rescale_q(m_AudioStream.SamplesCount, AVRational{1, m_AudioStream.pEnc->sample_rate}, m_AudioStream.pEnc->time_base);
|
|
|
|
m_AudioStream.SamplesCount += DstNbSamples;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
|
|
|
// dbg_msg("video_recorder", "prewrite----");
|
2020-06-22 21:59:37 +00:00
|
|
|
lock_wait(g_WriteLock);
|
|
|
|
WriteFrame(&m_AudioStream);
|
|
|
|
lock_unlock(g_WriteLock);
|
2016-08-30 23:39:59 +00:00
|
|
|
|
|
|
|
m_ProcessingAudioFrame = false;
|
2020-06-22 21:59:37 +00:00
|
|
|
m_NextAudioFrame = false;
|
2016-08-30 23:39:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::FillAudioFrame()
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::FillVideoFrame()
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
const int InLinesize[1] = {3 * m_VideoStream.pEnc->width};
|
|
|
|
sws_scale(m_VideoStream.pSwsCtx, (const uint8_t *const *)&m_pRGB, InLinesize, 0,
|
|
|
|
m_VideoStream.pEnc->height, m_VideoStream.pFrame->data, m_VideoStream.pFrame->linesize);
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
void CVideo::ReadRGBFromGL()
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
|
|
|
/* Get RGBA to align to 32 bits instead of just 24 for RGB. May be faster for FFmpeg. */
|
2019-10-01 13:01:23 +00:00
|
|
|
glReadBuffer(GL_FRONT);
|
2020-01-03 20:43:15 +00:00
|
|
|
GLint Alignment;
|
|
|
|
glGetIntegerv(GL_PACK_ALIGNMENT, &Alignment);
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
2021-05-03 21:02:45 +00:00
|
|
|
glReadPixels(0, 0, m_Width, m_Height, GL_RGBA, GL_UNSIGNED_BYTE, m_pPixels);
|
2020-01-03 20:43:15 +00:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, Alignment);
|
2020-06-22 21:59:37 +00:00
|
|
|
for(int i = 0; i < m_Height; i++)
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
for(int j = 0; j < m_Width; j++)
|
2016-08-27 15:51:23 +00:00
|
|
|
{
|
2021-05-03 21:02:45 +00:00
|
|
|
size_t CurGL = FORMAT_GL_NCHANNELS * (m_Width * (m_Height - i - 1) + j);
|
2020-06-22 21:59:37 +00:00
|
|
|
size_t CurRGB = FORMAT_NCHANNELS * (m_Width * i + j);
|
|
|
|
for(int k = 0; k < (int)FORMAT_NCHANNELS; k++)
|
|
|
|
m_pRGB[CurRGB + k] = m_pPixels[CurGL + k];
|
2016-08-27 15:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
AVFrame *CVideo::AllocPicture(enum AVPixelFormat PixFmt, int Width, int Height)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
AVFrame *pPicture;
|
2020-06-22 21:59:37 +00:00
|
|
|
int Ret;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
pPicture = av_frame_alloc();
|
|
|
|
if(!pPicture)
|
2016-08-27 19:10:27 +00:00
|
|
|
return NULL;
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
pPicture->format = PixFmt;
|
2020-09-26 19:41:58 +00:00
|
|
|
pPicture->width = Width;
|
2020-06-22 21:59:37 +00:00
|
|
|
pPicture->height = Height;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
|
|
|
/* allocate the buffers for the frame data */
|
2020-06-22 21:59:37 +00:00
|
|
|
Ret = av_frame_get_buffer(pPicture, 32);
|
2020-09-13 20:49:50 +00:00
|
|
|
if(Ret < 0)
|
|
|
|
{
|
2016-08-27 19:10:27 +00:00
|
|
|
dbg_msg("video_recorder", "Could not allocate frame data.");
|
2020-09-13 20:49:50 +00:00
|
|
|
return nullptr;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
return pPicture;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 05:05:49 +00:00
|
|
|
AVFrame *CVideo::AllocAudioFrame(enum AVSampleFormat SampleFmt, uint64_t ChannelLayout, int SampleRate, int NbSamples)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
AVFrame *Frame = av_frame_alloc();
|
|
|
|
int Ret;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-09-13 20:49:50 +00:00
|
|
|
if(!Frame)
|
|
|
|
{
|
2016-08-27 19:10:27 +00:00
|
|
|
dbg_msg("video_recorder", "Error allocating an audio frame");
|
2020-09-13 20:49:50 +00:00
|
|
|
return nullptr;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
Frame->format = SampleFmt;
|
|
|
|
Frame->channel_layout = ChannelLayout;
|
|
|
|
Frame->sample_rate = SampleRate;
|
|
|
|
Frame->nb_samples = NbSamples;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-09-13 20:49:50 +00:00
|
|
|
if(NbSamples)
|
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
Ret = av_frame_get_buffer(Frame, 0);
|
2020-09-13 20:49:50 +00:00
|
|
|
if(Ret < 0)
|
|
|
|
{
|
2016-08-27 19:10:27 +00:00
|
|
|
dbg_msg("video_recorder", "Error allocating an audio buffer");
|
2020-09-13 20:49:50 +00:00
|
|
|
return nullptr;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
return Frame;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 20:49:50 +00:00
|
|
|
bool CVideo::OpenVideo()
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
int Ret;
|
2020-09-26 19:41:58 +00:00
|
|
|
AVCodecContext *c = m_VideoStream.pEnc;
|
|
|
|
AVDictionary *opt = 0;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
|
|
|
av_dict_copy(&opt, m_pOptDict, 0);
|
|
|
|
|
|
|
|
/* open the codec */
|
2020-06-22 21:59:37 +00:00
|
|
|
Ret = avcodec_open2(c, m_VideoCodec, &opt);
|
2016-08-27 19:10:27 +00:00
|
|
|
av_dict_free(&opt);
|
2020-06-22 21:59:37 +00:00
|
|
|
if(Ret < 0)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2016-08-31 12:09:06 +00:00
|
|
|
char aBuf[AV_ERROR_MAX_STRING_SIZE];
|
2020-06-22 21:59:37 +00:00
|
|
|
av_strerror(Ret, aBuf, sizeof(aBuf));
|
2016-08-31 12:09:06 +00:00
|
|
|
dbg_msg("video_recorder", "Could not open video codec: %s", aBuf);
|
2020-09-13 20:49:50 +00:00
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate and init a re-usable frame */
|
2020-06-22 21:59:37 +00:00
|
|
|
m_VideoStream.pFrame = AllocPicture(c->pix_fmt, c->width, c->height);
|
|
|
|
if(!m_VideoStream.pFrame)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Could not allocate video frame");
|
2020-09-13 20:49:50 +00:00
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the output format is not YUV420P, then a temporary YUV420P
|
|
|
|
* picture is needed too. It is then converted to the required
|
|
|
|
* output format. */
|
2020-06-22 21:59:37 +00:00
|
|
|
m_VideoStream.pTmpFrame = NULL;
|
|
|
|
if(c->pix_fmt != AV_PIX_FMT_YUV420P)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
m_VideoStream.pTmpFrame = AllocPicture(AV_PIX_FMT_YUV420P, c->width, c->height);
|
|
|
|
if(!m_VideoStream.pTmpFrame)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Could not allocate temporary picture");
|
2020-09-13 20:49:50 +00:00
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the stream parameters to the muxer */
|
2020-06-22 21:59:37 +00:00
|
|
|
Ret = avcodec_parameters_from_context(m_VideoStream.pSt->codecpar, c);
|
|
|
|
if(Ret < 0)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Could not copy the stream parameters");
|
2020-09-13 20:49:50 +00:00
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
2020-06-22 21:59:37 +00:00
|
|
|
m_Vseq = 0;
|
2020-09-13 20:49:50 +00:00
|
|
|
return true;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 20:49:50 +00:00
|
|
|
bool CVideo::OpenAudio()
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
AVCodecContext *c;
|
2020-06-22 21:59:37 +00:00
|
|
|
int NbSamples;
|
|
|
|
int Ret;
|
2016-08-27 19:10:27 +00:00
|
|
|
AVDictionary *opt = NULL;
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
c = m_AudioStream.pEnc;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
|
|
|
/* open it */
|
2019-10-31 14:01:12 +00:00
|
|
|
//m_dbgfile = fopen("/tmp/pcm_dbg", "wb");
|
2016-08-27 19:10:27 +00:00
|
|
|
av_dict_copy(&opt, m_pOptDict, 0);
|
2020-06-22 21:59:37 +00:00
|
|
|
Ret = avcodec_open2(c, m_AudioCodec, &opt);
|
2016-08-27 19:10:27 +00:00
|
|
|
av_dict_free(&opt);
|
2020-06-22 21:59:37 +00:00
|
|
|
if(Ret < 0)
|
2016-08-31 12:09:06 +00:00
|
|
|
{
|
|
|
|
char aBuf[AV_ERROR_MAX_STRING_SIZE];
|
2020-06-22 21:59:37 +00:00
|
|
|
av_strerror(Ret, aBuf, sizeof(aBuf));
|
2016-08-31 12:09:06 +00:00
|
|
|
dbg_msg("video_recorder", "Could not open audio codec: %s", aBuf);
|
2020-09-13 20:49:50 +00:00
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
|
|
|
|
NbSamples = 10000;
|
2016-08-27 19:10:27 +00:00
|
|
|
else
|
2020-06-22 21:59:37 +00:00
|
|
|
NbSamples = c->frame_size;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
m_AudioStream.pFrame = AllocAudioFrame(c->sample_fmt, c->channel_layout, c->sample_rate, NbSamples);
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
m_AudioStream.pTmpFrame = AllocAudioFrame(AV_SAMPLE_FMT_S16, AV_CH_LAYOUT_STEREO, g_Config.m_SndRate, m_SndBufferSize * 2);
|
2016-08-27 19:10:27 +00:00
|
|
|
|
|
|
|
/* copy the stream parameters to the muxer */
|
2020-06-22 21:59:37 +00:00
|
|
|
Ret = avcodec_parameters_from_context(m_AudioStream.pSt->codecpar, c);
|
2020-09-13 20:49:50 +00:00
|
|
|
if(Ret < 0)
|
|
|
|
{
|
2016-08-27 19:10:27 +00:00
|
|
|
dbg_msg("video_recorder", "Could not copy the stream parameters");
|
2020-09-13 20:49:50 +00:00
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create resampler context */
|
2020-09-13 20:49:50 +00:00
|
|
|
m_AudioStream.pSwrCtx = swr_alloc();
|
|
|
|
if(!m_AudioStream.pSwrCtx)
|
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Could not allocate resampler context");
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
/* set options */
|
|
|
|
av_opt_set_int(m_AudioStream.pSwrCtx, "in_channel_count", 2, 0);
|
|
|
|
av_opt_set_int(m_AudioStream.pSwrCtx, "in_sample_rate", g_Config.m_SndRate, 0);
|
|
|
|
av_opt_set_sample_fmt(m_AudioStream.pSwrCtx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
|
|
|
|
av_opt_set_int(m_AudioStream.pSwrCtx, "out_channel_count", c->channels, 0);
|
|
|
|
av_opt_set_int(m_AudioStream.pSwrCtx, "out_sample_rate", c->sample_rate, 0);
|
|
|
|
av_opt_set_sample_fmt(m_AudioStream.pSwrCtx, "out_sample_fmt", c->sample_fmt, 0);
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
/* initialize the resampling context */
|
2020-10-12 14:18:06 +00:00
|
|
|
if(swr_init(m_AudioStream.pSwrCtx) < 0)
|
2020-09-26 19:41:58 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Failed to initialize the resampling context");
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
return true;
|
2020-09-13 20:49:50 +00:00
|
|
|
}
|
2016-08-27 19:10:27 +00:00
|
|
|
|
|
|
|
/* Add an output stream. */
|
2020-09-13 20:49:50 +00:00
|
|
|
bool CVideo::AddStream(OutputStream *pStream, AVFormatContext *pOC, AVCodec **ppCodec, enum AVCodecID CodecId)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
AVCodecContext *c;
|
|
|
|
|
|
|
|
/* find the encoder */
|
2020-09-26 19:41:58 +00:00
|
|
|
*ppCodec = avcodec_find_encoder(CodecId);
|
2020-06-22 21:59:37 +00:00
|
|
|
if(!(*ppCodec))
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Could not find encoder for '%s'",
|
2020-09-13 20:49:50 +00:00
|
|
|
avcodec_get_name(CodecId));
|
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
pStream->pSt = avformat_new_stream(pOC, NULL);
|
|
|
|
if(!pStream->pSt)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Could not allocate stream");
|
2020-09-13 20:49:50 +00:00
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
pStream->pSt->id = pOC->nb_streams - 1;
|
2020-06-22 21:59:37 +00:00
|
|
|
c = avcodec_alloc_context3(*ppCodec);
|
|
|
|
if(!c)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Could not alloc an encoding context");
|
2020-09-13 20:49:50 +00:00
|
|
|
return false;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
2020-06-22 21:59:37 +00:00
|
|
|
pStream->pEnc = c;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
switch((*ppCodec)->type)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
// m_MixingRate = g_Config.m_SndRate;
|
|
|
|
//
|
|
|
|
// // Set 16-bit stereo audio at 22Khz
|
|
|
|
// Format.freq = g_Config.m_SndRate; // ignore_convention
|
|
|
|
// Format.format = AUDIO_S16; // ignore_convention
|
|
|
|
// Format.channels = 2; // ignore_convention
|
|
|
|
// Format.samples = g_Config.m_SndBufferSize; // ignore_convention
|
|
|
|
|
|
|
|
c->sample_fmt = (*ppCodec)->sample_fmts ? (*ppCodec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
|
|
|
|
c->bit_rate = g_Config.m_SndRate * 2 * 16;
|
|
|
|
c->frame_size = m_SndBufferSize;
|
|
|
|
c->sample_rate = g_Config.m_SndRate;
|
|
|
|
if((*ppCodec)->supported_samplerates)
|
|
|
|
{
|
|
|
|
c->sample_rate = (*ppCodec)->supported_samplerates[0];
|
|
|
|
for(int i = 0; (*ppCodec)->supported_samplerates[i]; i++)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if((*ppCodec)->supported_samplerates[i] == g_Config.m_SndRate)
|
|
|
|
c->sample_rate = g_Config.m_SndRate;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
}
|
|
|
|
c->channels = 2;
|
|
|
|
c->channel_layout = AV_CH_LAYOUT_STEREO;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
pStream->pSt->time_base.num = 1;
|
|
|
|
pStream->pSt->time_base.den = c->sample_rate;
|
|
|
|
break;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
c->codec_id = CodecId;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
c->bit_rate = 400000;
|
|
|
|
/* Resolution must be a multiple of two. */
|
|
|
|
c->width = m_Width;
|
|
|
|
c->height = m_Height % 2 == 0 ? m_Height : m_Height - 1;
|
|
|
|
/* timebase: This is the fundamental unit of time (in seconds) in terms
|
2016-08-30 23:39:59 +00:00
|
|
|
* of which frame timestamps are represented. For fixed-fps content,
|
|
|
|
* timebase should be 1/framerate and timestamp increments should be
|
|
|
|
* identical to 1. */
|
2020-09-26 19:41:58 +00:00
|
|
|
pStream->pSt->time_base.num = 1;
|
|
|
|
pStream->pSt->time_base.den = m_FPS;
|
|
|
|
c->time_base = pStream->pSt->time_base;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
|
|
|
|
c->pix_fmt = STREAM_PIX_FMT;
|
|
|
|
if(c->codec_id == AV_CODEC_ID_MPEG2VIDEO)
|
|
|
|
{
|
|
|
|
/* just for testing, we also add B-frames */
|
|
|
|
c->max_b_frames = 2;
|
|
|
|
}
|
|
|
|
if(c->codec_id == AV_CODEC_ID_MPEG1VIDEO)
|
|
|
|
{
|
|
|
|
/* Needed to avoid using macroblocks in which some coeffs overflow.
|
2016-08-30 23:39:59 +00:00
|
|
|
* This does not happen with normal video, it just happens here as
|
|
|
|
* the motion of the chroma plane does not match the luma plane. */
|
2020-09-26 19:41:58 +00:00
|
|
|
c->mb_decision = 2;
|
|
|
|
}
|
|
|
|
if(CodecId == AV_CODEC_ID_H264)
|
|
|
|
{
|
|
|
|
const char *presets[10] = {"ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow", "placebo"};
|
|
|
|
//av_opt_set(c->priv_data, "preset", "slow", 0);
|
|
|
|
//av_opt_set_int(c->priv_data, "crf", 22, 0);
|
|
|
|
av_opt_set(c->priv_data, "preset", presets[g_Config.m_ClVideoX264Preset], 0);
|
|
|
|
av_opt_set_int(c->priv_data, "crf", g_Config.m_ClVideoX264Crf, 0);
|
|
|
|
}
|
2016-08-27 19:10:27 +00:00
|
|
|
break;
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Some formats want stream headers to be separate. */
|
2020-06-22 21:59:37 +00:00
|
|
|
if(pOC->oformat->flags & AVFMT_GLOBALHEADER)
|
2016-08-27 19:10:27 +00:00
|
|
|
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
|
2020-09-13 20:49:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
void CVideo::WriteFrame(OutputStream *pStream)
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
int RetRecv = 0;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2021-05-06 10:25:09 +00:00
|
|
|
AVPacket *pPacket = av_packet_alloc();
|
|
|
|
if(pPacket == nullptr)
|
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Failed allocating packet");
|
|
|
|
return;
|
|
|
|
}
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2021-05-06 10:25:09 +00:00
|
|
|
pPacket->data = 0;
|
|
|
|
pPacket->size = 0;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
avcodec_send_frame(pStream->pEnc, pStream->pFrame);
|
2016-08-30 23:39:59 +00:00
|
|
|
do
|
|
|
|
{
|
2021-05-06 10:25:09 +00:00
|
|
|
RetRecv = avcodec_receive_packet(pStream->pEnc, pPacket);
|
2020-06-22 21:59:37 +00:00
|
|
|
if(!RetRecv)
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
|
|
|
/* rescale output packet timestamp values from codec to stream timebase */
|
2021-05-06 10:25:09 +00:00
|
|
|
av_packet_rescale_ts(pPacket, pStream->pEnc->time_base, pStream->pSt->time_base);
|
|
|
|
pPacket->stream_index = pStream->pSt->index;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2021-05-06 10:25:09 +00:00
|
|
|
if(int Ret = av_interleaved_write_frame(m_pFormatContext, pPacket))
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
2016-08-31 12:09:06 +00:00
|
|
|
char aBuf[AV_ERROR_MAX_STRING_SIZE];
|
2020-06-22 21:59:37 +00:00
|
|
|
av_strerror(Ret, aBuf, sizeof(aBuf));
|
2016-08-31 12:09:06 +00:00
|
|
|
dbg_msg("video_recorder", "Error while writing video frame: %s", aBuf);
|
2016-08-30 23:39:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2020-06-22 21:59:37 +00:00
|
|
|
} while(true);
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(RetRecv && RetRecv != AVERROR(EAGAIN))
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
dbg_msg("video_recorder", "Error encoding frame, error: %d", RetRecv);
|
2016-08-30 23:39:59 +00:00
|
|
|
}
|
2021-05-06 10:25:09 +00:00
|
|
|
|
|
|
|
av_packet_free(&pPacket);
|
2016-08-30 23:39:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
void CVideo::FinishFrames(OutputStream *pStream)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2016-08-30 23:39:59 +00:00
|
|
|
dbg_msg("video_recorder", "------------");
|
2020-06-22 21:59:37 +00:00
|
|
|
int RetRecv = 0;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2021-05-06 10:25:09 +00:00
|
|
|
AVPacket *pPacket = av_packet_alloc();
|
|
|
|
if(pPacket == nullptr)
|
|
|
|
{
|
|
|
|
dbg_msg("video_recorder", "Failed allocating packet");
|
|
|
|
return;
|
|
|
|
}
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2021-05-06 10:25:09 +00:00
|
|
|
pPacket->data = 0;
|
|
|
|
pPacket->size = 0;
|
2016-08-27 19:10:27 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
avcodec_send_frame(pStream->pEnc, 0);
|
2016-08-30 23:39:59 +00:00
|
|
|
do
|
|
|
|
{
|
2021-05-06 10:25:09 +00:00
|
|
|
RetRecv = avcodec_receive_packet(pStream->pEnc, pPacket);
|
2020-06-22 21:59:37 +00:00
|
|
|
if(!RetRecv)
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
|
|
|
/* rescale output packet timestamp values from codec to stream timebase */
|
2020-06-22 21:59:37 +00:00
|
|
|
//if(pStream->pSt->codec->codec_type == AVMEDIA_TYPE_AUDIO)
|
2021-05-06 10:25:09 +00:00
|
|
|
av_packet_rescale_ts(pPacket, pStream->pEnc->time_base, pStream->pSt->time_base);
|
|
|
|
pPacket->stream_index = pStream->pSt->index;
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2021-05-06 10:25:09 +00:00
|
|
|
if(int Ret = av_interleaved_write_frame(m_pFormatContext, pPacket))
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
2016-08-31 12:09:06 +00:00
|
|
|
char aBuf[AV_ERROR_MAX_STRING_SIZE];
|
2020-06-22 21:59:37 +00:00
|
|
|
av_strerror(Ret, aBuf, sizeof(aBuf));
|
2016-08-31 12:09:06 +00:00
|
|
|
dbg_msg("video_recorder", "Error while writing video frame: %s", aBuf);
|
2016-08-30 23:39:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2020-06-22 21:59:37 +00:00
|
|
|
} while(true);
|
2016-08-30 23:39:59 +00:00
|
|
|
|
2020-06-22 21:59:37 +00:00
|
|
|
if(RetRecv && RetRecv != AVERROR_EOF)
|
2016-08-30 23:39:59 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
dbg_msg("video_recorder", "failed to finish recoding, error: %d", RetRecv);
|
2016-08-30 23:39:59 +00:00
|
|
|
}
|
2021-05-06 10:25:09 +00:00
|
|
|
|
|
|
|
av_packet_free(&pPacket);
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
void CVideo::CloseStream(OutputStream *pStream)
|
2016-08-27 19:10:27 +00:00
|
|
|
{
|
2020-06-22 21:59:37 +00:00
|
|
|
avcodec_free_context(&pStream->pEnc);
|
|
|
|
av_frame_free(&pStream->pFrame);
|
|
|
|
av_frame_free(&pStream->pTmpFrame);
|
|
|
|
sws_freeContext(pStream->pSwsCtx);
|
|
|
|
swr_free(&pStream->pSwrCtx);
|
2016-08-27 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|