LOTS OF CHANGES. was hoping to get revision 5000 perfect, but really that's never going to happen. this has gone on for too long now.

vulkan, wasapi, quake injector features added.
irc, avplug, cef plugins/drivers reworked/updated/added
openal reverb, doppler effects added.
'dir' console command now attempts to view clicked files.
lots of warning fixes, should now only be deprecation warnings for most targets (depending on compiler version anyway...).
SendEntity finally reworked to use flags properly.
effectinfo improved, other smc-targetted fixes.
mapcluster stuff now has support for linux.
.basebone+.baseframe now exist in ssqc.
qcc: -Fqccx supports qccx syntax, including qccx hacks. don't expect these to work in fteqw nor dp though.
qcc: rewrote function call handling to use refs rather than defs. this makes struct passing more efficient and makes the __out keyword usable with fields etc.
qccgui: can cope a little better with non-unicode files. can now represent most quake chars.
qcc: suppressed warnings from *extensions.qc

git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5000 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
Spoike 2016-07-12 00:40:13 +00:00
parent 5920bf05fb
commit 27a59a0cbc
271 changed files with 101001 additions and 64352 deletions

View file

@ -4,6 +4,7 @@
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
//between av 52.31 and 54.35, lots of constants etc got renamed to gain an extra AV_ prefix.
/*
@ -44,18 +45,18 @@ struct decctx
AVCodecContext *pVCodecCtx;
AVFrame *pVFrame;
int64_t num, denum;
int64_t lasttime;
AVPicture pFrameRGB;
uint8_t *rgb_data;
int rgb_linesize;
struct SwsContext *pScaleCtx;
unsigned int starttime;
unsigned int lastframe;
};
static qboolean AVDec_SetSize (void *vctx, int width, int height)
{
struct decctx *ctx = (struct decctx*)vctx;
AVPicture newscaled;
uint8_t *rgb_data[1];
int rgb_linesize[1];
//colourspace conversions will be fastest if we
// if (width > ctx->pCodecCtx->width)
@ -67,15 +68,16 @@ static qboolean AVDec_SetSize (void *vctx, int width, int height)
if (width == ctx->width && height == ctx->height && ctx->pScaleCtx)
return true;
if (avpicture_alloc(&newscaled, AV_PIX_FMT_BGRA, width, height) >= 0)
if (av_image_alloc(rgb_data, rgb_linesize, width, height, AV_PIX_FMT_BGRA, 16) >= 0)
{
//update the scale context as required
//clear the old stuff out
avpicture_free(&ctx->pFrameRGB);
av_free(ctx->rgb_data);
ctx->width = width;
ctx->height = height;
ctx->pFrameRGB = newscaled;
ctx->rgb_data = rgb_data[0];
ctx->rgb_linesize = rgb_linesize[0];
return qtrue;
}
return qfalse; //unsupported
@ -93,12 +95,12 @@ static int AVIO_Read(void *opaque, uint8_t *buf, int buf_size)
static int64_t AVIO_Seek(void *opaque, int64_t offset, int whence)
{
struct decctx *ctx = opaque;
int64_t ret = ctx->fileofs;
whence &= ~AVSEEK_FORCE;
switch(whence)
{
case SEEK_SET:
default:
return -1;
case SEEK_SET:
ctx->fileofs = offset;
break;
case SEEK_CUR:
@ -119,7 +121,7 @@ static void AVDec_Destroy(void *vctx)
struct decctx *ctx = (struct decctx*)vctx;
// Free the video stuff
avpicture_free(&ctx->pFrameRGB);
av_free(ctx->rgb_data);
if (ctx->pVCodecCtx)
avcodec_close(ctx->pVCodecCtx);
av_free(ctx->pVFrame);
@ -138,7 +140,7 @@ static void AVDec_Destroy(void *vctx)
free(ctx);
}
static void *AVDec_Create(char *medianame)
static void *AVDec_Create(const char *medianame)
{
struct decctx *ctx;
@ -163,16 +165,13 @@ static void *AVDec_Create(char *medianame)
ctx = malloc(sizeof(*ctx));
memset(ctx, 0, sizeof(*ctx));
//so we always decode the first frame instantly.
ctx->starttime = pSys_Milliseconds();
ctx->lasttime = -1;
ctx->file = -1;
if (useioctx)
{
// Create internal Buffer for FFmpeg:
const int iBufSize = 32 * 1024;
char *pBuffer = malloc(iBufSize);
char *pBuffer = av_malloc(iBufSize);
AVIOContext *ioctx;
ctx->filelen = pFS_Open(medianame, &ctx->file, 1);
@ -180,7 +179,7 @@ static void *AVDec_Create(char *medianame)
{
Con_Printf("Unable to open %s\n", medianame);
free(ctx);
free(pBuffer);
av_free(pBuffer);
return NULL;
}
@ -230,7 +229,7 @@ having them tied to the libavformat network IO.
ctx->pACodecCtx=ctx->pFormatCtx->streams[ctx->audioStream]->codec;
pCodec=avcodec_find_decoder(ctx->pACodecCtx->codec_id);
ctx->pAFrame=avcodec_alloc_frame();
ctx->pAFrame=av_frame_alloc();
if(pCodec!=NULL && ctx->pAFrame && avcodec_open2(ctx->pACodecCtx, pCodec, NULL) >= 0)
{
@ -260,7 +259,7 @@ having them tied to the libavformat network IO.
if(pCodec!=NULL && avcodec_open2(ctx->pVCodecCtx, pCodec, NULL) >= 0)
{
// Allocate video frame
ctx->pVFrame=avcodec_alloc_frame();
ctx->pVFrame=av_frame_alloc();
if(ctx->pVFrame!=NULL)
{
if (AVDec_SetSize(ctx, ctx->pVCodecCtx->width, ctx->pVCodecCtx->height))
@ -276,30 +275,27 @@ having them tied to the libavformat network IO.
return NULL;
}
static void *AVDec_DisplayFrame(void *vctx, qboolean nosound, uploadfmt_t *fmt, int *width, int *height)
static qboolean VARGS AVDec_DisplayFrame(void *vctx, qboolean nosound, qboolean forcevideo, double mediatime, void (QDECL *uploadtexture)(void *ectx, uploadfmt_t fmt, int width, int height, void *data, void *palette), void *ectx)
{
struct decctx *ctx = (struct decctx*)vctx;
AVPacket packet;
int frameFinished;
qboolean repainted = false;
int64_t curtime, lasttime;
int64_t curtime;
curtime = ((pSys_Milliseconds() - ctx->starttime) * ctx->denum);
curtime /= (ctx->num * 1000);
curtime = (mediatime * ctx->denum) / ctx->num;
*fmt = TF_BGRA32;
while (1)
{
lasttime = av_frame_get_best_effort_timestamp(ctx->pVFrame);
if (lasttime > curtime)
if (ctx->lasttime > curtime)
break;
// We're ahead of the previous frame. try and read the next.
if (av_read_frame(ctx->pFormatCtx, &packet) < 0)
{
*fmt = TF_INVALID;
break;
if (repainted)
break;
return false;
}
// Is this a packet from the video stream?
@ -314,10 +310,11 @@ static void *AVDec_DisplayFrame(void *vctx, qboolean nosound, uploadfmt_t *fmt,
ctx->pScaleCtx = sws_getCachedContext(ctx->pScaleCtx, ctx->pVCodecCtx->width, ctx->pVCodecCtx->height, ctx->pVCodecCtx->pix_fmt, ctx->width, ctx->height, AV_PIX_FMT_BGRA, SWS_POINT, 0, 0, 0);
// Convert the image from its native format to RGB
sws_scale(ctx->pScaleCtx, (void*)ctx->pVFrame->data, ctx->pVFrame->linesize, 0, ctx->pVCodecCtx->height, ctx->pFrameRGB.data, ctx->pFrameRGB.linesize);
sws_scale(ctx->pScaleCtx, (void*)ctx->pVFrame->data, ctx->pVFrame->linesize, 0, ctx->pVCodecCtx->height, &ctx->rgb_data, &ctx->rgb_linesize);
repainted = true;
}
ctx->lasttime = av_frame_get_best_effort_timestamp(ctx->pVFrame);
}
else if(packet.stream_index==ctx->audioStream && !nosound)
{
@ -335,6 +332,7 @@ static void *AVDec_DisplayFrame(void *vctx, qboolean nosound, uploadfmt_t *fmt,
if (okay)
{
int width = 2;
int channels = ctx->pACodecCtx->channels;
unsigned int auddatasize = av_samples_get_buffer_size(NULL, ctx->pACodecCtx->channels, ctx->pAFrame->nb_samples, ctx->pACodecCtx->sample_fmt, 1);
void *auddata = ctx->pAFrame->data[0];
switch(ctx->pACodecCtx->sample_fmt)
@ -342,13 +340,24 @@ static void *AVDec_DisplayFrame(void *vctx, qboolean nosound, uploadfmt_t *fmt,
default:
auddatasize = 0;
break;
case AV_SAMPLE_FMT_U8P:
auddatasize /= channels;
channels = 1;
case AV_SAMPLE_FMT_U8:
width = 1;
break;
case AV_SAMPLE_FMT_S16P:
auddatasize /= channels;
channels = 1;
case AV_SAMPLE_FMT_S16:
width = 2;
break;
case AV_SAMPLE_FMT_FLTP:
auddatasize /= channels;
channels = 1;
case AV_SAMPLE_FMT_FLT:
//FIXME: support float audio internally.
{
float *in = (void*)auddata;
signed short *out = (void*)auddata;
@ -366,23 +375,43 @@ static void *AVDec_DisplayFrame(void *vctx, qboolean nosound, uploadfmt_t *fmt,
auddatasize/=2;
width = 2;
}
case AV_SAMPLE_FMT_DBLP:
auddatasize /= channels;
channels = 1;
case AV_SAMPLE_FMT_DBL:
{
double *in = (double*)auddata;
signed short *out = (void*)auddata;
int v;
unsigned int i;
for (i = 0; i < auddatasize/sizeof(*in); i++)
{
v = (short)(in[i]*32767);
if (v < -32767)
v = -32767;
else if (v > 32767)
v = 32767;
out[i] = v;
}
auddatasize/=4;
width = 2;
}
break;
}
pS_RawAudio(-1, auddata, ctx->pACodecCtx->sample_rate, auddatasize/(ctx->pACodecCtx->channels*width), ctx->pACodecCtx->channels, width, 1);
pS_RawAudio(-1, auddata, ctx->pACodecCtx->sample_rate, auddatasize/(channels*width), channels, width, 1);
}
}
packet.data = odata;
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
av_packet_unref(&packet);
}
*width = ctx->width;
*height = ctx->height;
if (!repainted)
return NULL;
return ctx->pFrameRGB.data[0];
if (forcevideo || repainted)
uploadtexture(ectx, TF_BGRA32, ctx->width, ctx->height, ctx->rgb_data, NULL);
return true;
}
static void AVDec_GetSize (void *vctx, int *width, int *height)
{
@ -407,11 +436,16 @@ static void AVDec_Rewind(void *vctx)
{
struct decctx *ctx = (struct decctx*)vctx;
if (ctx->videoStream >= 0)
av_seek_frame(ctx->pFormatCtx, ctx->videoStream, 0, AVSEEK_FLAG_BACKWARD);
{
av_seek_frame(ctx->pFormatCtx, ctx->videoStream, 0, AVSEEK_FLAG_FRAME|AVSEEK_FLAG_BACKWARD);
avcodec_flush_buffers(ctx->pVCodecCtx);
}
if (ctx->audioStream >= 0)
av_seek_frame(ctx->pFormatCtx, ctx->audioStream, 0, AVSEEK_FLAG_BACKWARD);
ctx->starttime = pSys_Milliseconds();
{
av_seek_frame(ctx->pFormatCtx, ctx->audioStream, 0, AVSEEK_FLAG_FRAME|AVSEEK_FLAG_BACKWARD);
avcodec_flush_buffers(ctx->pACodecCtx);
}
ctx->lasttime = -1;
}
/*
@ -424,10 +458,10 @@ static qintptr_t AVDec_Shutdown(qintptr_t *args)
static media_decoder_funcs_t decoderfuncs =
{
sizeof(media_decoder_funcs_t),
"avplug",
AVDec_Create,
AVDec_DisplayFrame,
NULL,//doneframe
AVDec_Destroy,
AVDec_Rewind,
@ -452,6 +486,15 @@ static qboolean AVDec_Init(void)
return true;
}
static void AVLogCallback(void *avcl, int level, const char *fmt, va_list vl)
{ //needs to be reenterant
#ifdef _DEBUG
char string[1024];
Q_vsnprintf (string, sizeof(string), fmt, vl);
pCon_Print(string);
#endif
}
//get the encoder/decoders to register themselves with the engine, then make sure avformat/avcodec have registered all they have to give.
qboolean AVEnc_Init(void);
qintptr_t Plug_Init(qintptr_t *args)
@ -464,6 +507,9 @@ qintptr_t Plug_Init(qintptr_t *args)
{
av_register_all();
avcodec_register_all();
av_log_set_level(AV_LOG_WARNING);
av_log_set_callback(AVLogCallback);
}
return okay;
}

View file

@ -5,6 +5,8 @@
#include "libavformat/avio.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
//#include <libavutil/channel_layout.h>
/*
@ -14,6 +16,7 @@ http://svn.gnumonks.org/tags/21c3-video/upstream/ffmpeg-0.4.9-pre1/output_exampl
struct encctx
{
char abspath[MAX_OSPATH];
AVFormatContext *fc;
qboolean doneheaders;
@ -25,27 +28,33 @@ struct encctx
AVStream *audio_st;
AVFrame *audio;
uint8_t *audio_outbuf;
uint32_t audio_outcount;
int64_t audio_pts;
};
#define VARIABLE_AUDIO_FRAME_MIN_SIZE 512 //audio frames smaller than a certain size are just wasteful
#define VARIABLE_AUDIO_FRAME_MAX_SIZE 1024
static void AVEnc_End (void *ctx);
static AVFrame *alloc_frame(enum PixelFormat pix_fmt, int width, int height)
static AVFrame *alloc_frame(enum AVPixelFormat pix_fmt, int width, int height)
{
AVFrame *picture;
uint8_t *picture_buf;
int size;
picture = avcodec_alloc_frame();
picture = av_frame_alloc();
if(!picture)
return NULL;
size = avpicture_get_size(pix_fmt, width, height);
size = av_image_get_buffer_size(pix_fmt, width, height, 1);
picture_buf = (uint8_t*)(av_malloc(size));
if (!picture_buf)
{
av_free(picture);
return NULL;
}
avpicture_fill((AVPicture *) picture, picture_buf, pix_fmt, width, height);
av_image_fill_arrays(picture->data, picture->linesize, picture_buf, pix_fmt, width, height, 1/*fixme: align*/);
picture->width = width;
picture->height = height;
return picture;
@ -61,10 +70,7 @@ AVStream *add_video_stream(struct encctx *ctx, AVCodec *codec, int fps, int widt
st = avformat_new_stream(ctx->fc, codec);
if (!st)
{
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
return NULL;
c = st->codec;
c->codec_id = codec->id;
@ -78,14 +84,14 @@ AVStream *add_video_stream(struct encctx *ctx, AVCodec *codec, int fps, int widt
/* frames per second */
c->time_base.num = 1;
c->time_base.den = fps;
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = PIX_FMT_YUV420P;
if (c->codec_id == CODEC_ID_MPEG2VIDEO)
//c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = AV_PIX_FMT_YUV420P;
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 == CODEC_ID_MPEG1VIDEO)
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO)
{
/* needed to avoid using macroblocks in which some coeffs overflow
this doesnt happen with normal video, it just happens here as the
@ -96,9 +102,13 @@ AVStream *add_video_stream(struct encctx *ctx, AVCodec *codec, int fps, int widt
if (ctx->fc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
*prof = 0;
pCvar_GetString("avplug_format", prof, sizeof(prof));
// av_opt_set(c->priv_data, "profile", prof, AV_OPT_SEARCH_CHILDREN);
pCvar_GetString("avplug_videopreset", prof, sizeof(prof));
if (*prof)
av_opt_set(c->priv_data, "preset", prof, AV_OPT_SEARCH_CHILDREN);
pCvar_GetString("avplug_video_crf", prof, sizeof(prof));
if (*prof)
av_opt_set(c->priv_data, "crf", prof, AV_OPT_SEARCH_CHILDREN);
return st;
}
@ -124,6 +134,7 @@ static void AVEnc_Video (void *vctx, void *data, int frame, int width, int heigh
AVPacket pkt;
int avpfmt;
int inbpp;
int err;
if (!ctx->video_st)
return;
@ -146,28 +157,39 @@ static void AVEnc_Video (void *vctx, void *data, int frame, int width, int heigh
//convert RGB to whatever the codec needs (ie: yuv...).
//also rescales, but only if the user resizes the video while recording. which is a stupid thing to do.
ctx->scale_ctx = sws_getCachedContext(ctx->scale_ctx, width, height, qpfmt, ctx->picture->width, ctx->picture->height, ctx->video_st->codec->pix_fmt, SWS_POINT, 0, 0, 0);
ctx->scale_ctx = sws_getCachedContext(ctx->scale_ctx, width, height, avpfmt, ctx->picture->width, ctx->picture->height, ctx->video_st->codec->pix_fmt, SWS_POINT, 0, 0, 0);
sws_scale(ctx->scale_ctx, srcslices, srcstride, 0, height, ctx->picture->data, ctx->picture->linesize);
av_init_packet(&pkt);
ctx->picture->pts = av_rescale_q(frame, ctx->video_st->codec->time_base, ctx->video_st->time_base);
ctx->picture->pts = frame;
success = 0;
pkt.data = ctx->video_outbuf;
pkt.size = ctx->video_outbuf_size;
if (avcodec_encode_video2(ctx->video_st->codec, &pkt, ctx->picture, &success) == 0 && success)
ctx->picture->format = ctx->video_st->codec->pix_fmt;
err = avcodec_encode_video2(ctx->video_st->codec, &pkt, ctx->picture, &success);
if (err)
{
pkt.pts = ctx->video_st->codec->coded_frame->pts;
if(ctx->video_st->codec->coded_frame->key_frame)
pkt.flags |= AV_PKT_FLAG_KEY;
char buf[512];
Con_Printf("avcodec_encode_video2: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
}
else if (err == 0 && success)
{
// pkt.pts = ctx->video_st->codec->coded_frame->pts;
// if(ctx->video_st->codec->coded_frame->key_frame)
// pkt.flags |= AV_PKT_FLAG_KEY;
av_packet_rescale_ts(&pkt, ctx->video_st->codec->time_base, ctx->video_st->time_base);
pkt.stream_index = ctx->video_st->index;
pkt.data = ctx->video_outbuf;
// pkt.size = psize;
av_write_frame(ctx->fc, &pkt);
err = av_interleaved_write_frame(ctx->fc, &pkt);
if (err)
{
char buf[512];
Con_Printf("av_interleaved_write_frame: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
}
}
}
AVStream *add_audio_stream(struct encctx *ctx, AVCodec *codec, int samplerate, int bits, int channels)
AVStream *add_audio_stream(struct encctx *ctx, AVCodec *codec, int samplerate, int *bits, int channels)
{
AVCodecContext *c;
AVStream *st;
@ -175,11 +197,9 @@ AVStream *add_audio_stream(struct encctx *ctx, AVCodec *codec, int samplerate, i
st = avformat_new_stream(ctx->fc, codec);
if (!st)
{
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
return NULL;
st->id = ctx->fc->nb_streams-1;
c = st->codec;
c->codec_id = codec->id;
c->codec_type = codec->type;
@ -187,25 +207,46 @@ AVStream *add_audio_stream(struct encctx *ctx, AVCodec *codec, int samplerate, i
/* put sample parameters */
c->bit_rate = bitrate;
/* frames per second */
c->sample_fmt = ((bits==16)?AV_SAMPLE_FMT_S16:AV_SAMPLE_FMT_U8);
c->time_base.num = 1;
c->time_base.den = samplerate;
c->sample_rate = samplerate;
c->channels = channels;
#if 0
switch(channels)
{
case 1:
c->channel_layout = AV_CH_FRONT_CENTER;
c->channel_layout = AV_CH_LAYOUT_MONO;
break;
case 2:
c->channel_layout = AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT;
c->channel_layout = AV_CH_LAYOUT_STEREO;
break;
default:
break;
}
#else
c->channel_layout = av_get_default_channel_layout(c->channels);
#endif
c->channel_layout = av_get_default_channel_layout(c->channels);
c->sample_fmt = codec->sample_fmts[0];
// if (c->sample_fmt == AV_SAMPLE_FMT_FLTP || c->sample_fmt == AV_SAMPLE_FMT_FLT)
// *bits = 32; //get the engine to mix 32bit audio instead of whatever its currently set to.
// else if (c->sample_fmt == AV_SAMPLE_FMT_U8P || c->sample_fmt == AV_SAMPLE_FMT_U8)
// *bits = 8; //get the engine to mix 32bit audio instead of whatever its currently set to.
// else if (c->sample_fmt == AV_SAMPLE_FMT_S16P || c->sample_fmt == AV_SAMPLE_FMT_S16)
// *bits = 16;
// else
*bits = 32;
c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
// some formats want stream headers to be seperate
if (ctx->fc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
// avcodec_parameters_from_context(st->codecpar, c);
return st;
}
void close_audio(struct encctx *ctx)
@ -220,25 +261,159 @@ static void AVEnc_Audio (void *vctx, void *data, int bytes)
struct encctx *ctx = vctx;
int success;
AVPacket pkt;
int err;
ctx->audio->nb_samples = ctx->audio_st->codec->frame_size;
if (avcodec_fill_audio_frame(ctx->audio, ctx->audio_st->codec->channels, ctx->audio_st->codec->sample_fmt, data, bytes, 0) < 0)
if (!ctx->audio_st)
return;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
success = 0;
if (avcodec_encode_audio2(ctx->audio_st->codec, &pkt, ctx->audio, &success) == 0 && success)
while (bytes)
{
pkt.pts = ctx->audio_st->codec->coded_frame->pts;
if(ctx->audio_st->codec->coded_frame->key_frame)
pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = ctx->audio_st->index;
// pkt.data = ctx->video_outbuf;
// pkt.size = psize;
int i, p, chans = ctx->audio_st->codec->channels;
int blocksize = sizeof(float)*chans;
int count = bytes / blocksize;
int planesize = ctx->audio_st->codec->frame_size;
float *in;
int offset;
av_write_frame(ctx->fc, &pkt);
if (!planesize) //variable-sized frames. yay
{
planesize = count;
if (count > VARIABLE_AUDIO_FRAME_MAX_SIZE - ctx->audio_outcount)
count = VARIABLE_AUDIO_FRAME_MAX_SIZE - ctx->audio_outcount;
}
else if (count > ctx->audio_st->codec->frame_size - ctx->audio_outcount)
count = ctx->audio_st->codec->frame_size - ctx->audio_outcount;
in = (float*)data;
offset = ctx->audio_outcount;
ctx->audio_outcount += count;
data = (qbyte*)data + count * blocksize;
bytes -= count * blocksize;
//input is always float audio, because I'm lazy.
//output is whatever the codec needs (may be packed or planar, gah).
//the engine's mixer will do all rate scaling for us, as well as channel selection
switch(ctx->audio_st->codec->sample_fmt)
{
case AV_SAMPLE_FMT_DBL:
offset *= chans;
count *= chans;
planesize *= chans;
chans = 1;
case AV_SAMPLE_FMT_DBLP:
for (p = 0; p < chans; p++)
{
double *f = (double*)ctx->audio_outbuf + p*planesize + offset;
for (i = 0; i < count*chans; i+=chans)
*f++ = in[i];
in++;
}
break;
case AV_SAMPLE_FMT_FLT:
offset *= chans;
count *= chans;
planesize *= chans;
chans = 1;
case AV_SAMPLE_FMT_FLTP:
for (p = 0; p < chans; p++)
{
float *f = (float *)ctx->audio_outbuf + p*planesize + offset;
for (i = 0; i < count*chans; i+=chans)
*f++ = in[i];
in++;
}
break;
case AV_SAMPLE_FMT_S32:
offset *= chans;
count *= chans;
planesize *= chans;
chans = 1;
case AV_SAMPLE_FMT_S32P:
for (p = 0; p < chans; p++)
{
int32_t *f = (int32_t *)ctx->audio_outbuf + p*planesize + offset;
for (i = 0; i < count*chans; i+=chans)
*f++ = bound(0x80000000, (in[i] * 0x7fffffff), 0x7fffffff);
in++;
}
break;
case AV_SAMPLE_FMT_S16:
offset *= chans;
count *= chans;
planesize *= chans;
chans = 1;
case AV_SAMPLE_FMT_S16P:
for (p = 0; p < chans; p++)
{
int16_t *f = (int16_t *)ctx->audio_outbuf + p*planesize + offset;
for (i = 0; i < count*chans; i+=chans)
*f++ = bound(-32768, (int)(in[i] * 32767), 32767);
//sin((ctx->audio_pts+ctx->audio_outcount-count+i/chans)*0.1) * 32767;//
in++;
}
break;
case AV_SAMPLE_FMT_U8:
offset *= chans;
count *= chans;
planesize *= chans;
chans = 1;
case AV_SAMPLE_FMT_U8P:
for (p = 0; p < chans; p++)
{
uint8_t *f = (uint8_t*)ctx->audio_outbuf + p*planesize + offset;
for (i = 0; i < count*chans; i+=chans)
*f++ = bound(0, 128+(int)(in[i] * 127), 255);
in++;
}
break;
default:
return;
}
if (ctx->audio_st->codec->frame_size)
{
if (ctx->audio_outcount < ctx->audio_st->codec->frame_size)
break; //not enough data yet.
}
else
{
if (ctx->audio_outcount < VARIABLE_AUDIO_FRAME_MIN_SIZE)
break; //not enough data yet.
}
ctx->audio->nb_samples = ctx->audio_outcount;
avcodec_fill_audio_frame(ctx->audio, ctx->audio_st->codec->channels, ctx->audio_st->codec->sample_fmt, ctx->audio_outbuf, av_get_bytes_per_sample(ctx->audio_st->codec->sample_fmt)*ctx->audio_outcount*ctx->audio_st->codec->channels, 1);
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
success = 0;
ctx->audio->pts = ctx->audio_pts;
ctx->audio_pts += ctx->audio_outcount;
ctx->audio_outcount = 0;
err = avcodec_encode_audio2(ctx->audio_st->codec, &pkt, ctx->audio, &success);
if (err)
{
char buf[512];
Con_Printf("avcodec_encode_audio2: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
}
else if (success)
{
// pkt.pts = ctx->audio_st->codec->coded_frame->pts;
// if(ctx->audio_st->codec->coded_frame->key_frame)
// pkt.flags |= AV_PKT_FLAG_KEY;
av_packet_rescale_ts(&pkt, ctx->audio_st->codec->time_base, ctx->audio_st->time_base);
pkt.stream_index = ctx->audio_st->index;
err = av_interleaved_write_frame(ctx->fc, &pkt);
if (err)
{
char buf[512];
Con_Printf("av_interleaved_write_frame: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
}
}
}
}
@ -249,8 +424,9 @@ static void *AVEnc_Begin (char *streamname, int videorate, int width, int height
AVCodec *videocodec = NULL;
AVCodec *audiocodec = NULL;
char formatname[64];
int err;
formatname[0] = 0;
pCvar_GetString("avplug_format", formatname, sizeof(formatname));
pCvar_GetString("avplug_format_force", formatname, sizeof(formatname));
if (*formatname)
{
@ -349,49 +525,56 @@ static void *AVEnc_Begin (char *streamname, int videorate, int width, int height
//pick default codecs
ctx->video_st = NULL;
if (videocodec)
{
ctx->video_st = add_video_stream(ctx, videocodec, videorate, width, height);
if (ctx->video_st)
{
AVCodecContext *c = ctx->video_st->codec;
if (avcodec_open2(c, videocodec, NULL) < 0)
{
Con_Printf("avplug: Could not init codec instance \"%s\". Maybe try a different framerate/resolution/bitrate\n", videocodec->name);
AVEnc_End(ctx);
return NULL;
}
ctx->picture = alloc_frame(c->pix_fmt, c->width, c->height);
ctx->video_outbuf_size = 200000;
ctx->video_outbuf = av_malloc(ctx->video_outbuf_size);
if (!ctx->video_outbuf)
ctx->video_outbuf_size = 0;
}
}
if (audiocodec)
{
ctx->audio_st = add_audio_stream(ctx, audiocodec, *sndkhz, *sndbits, *sndchannels);
if (ctx->audio_st)
{
AVCodecContext *c = ctx->audio_st->codec;
if (avcodec_open2(c, audiocodec, NULL) < 0)
{
Con_Printf("avplug: Could not init codec instance \"%s\".\n", audiocodec->name);
AVEnc_End(ctx);
return NULL;
}
ctx->audio_st = add_audio_stream(ctx, audiocodec, *sndkhz, sndbits, *sndchannels);
ctx->audio = avcodec_alloc_frame();
if (ctx->video_st)
{
AVCodecContext *c = ctx->video_st->codec;
err = avcodec_open2(c, videocodec, NULL);
if (err < 0)
{
char buf[512];
Con_Printf("avplug: Could not init codec instance \"%s\" - %s\nMaybe try a different framerate/resolution/bitrate\n", videocodec->name, av_make_error_string(buf, sizeof(buf), err));
AVEnc_End(ctx);
return NULL;
}
ctx->picture = alloc_frame(c->pix_fmt, c->width, c->height);
ctx->video_outbuf_size = 200000;
ctx->video_outbuf = av_malloc(ctx->video_outbuf_size);
if (!ctx->video_outbuf)
ctx->video_outbuf_size = 0;
}
if (ctx->audio_st)
{
int sz;
AVCodecContext *c = ctx->audio_st->codec;
err = avcodec_open2(c, audiocodec, NULL);
if (err < 0)
{
char buf[512];
Con_Printf("avplug: Could not init codec instance \"%s\" - %s\n", audiocodec->name, av_make_error_string(buf, sizeof(buf), err));
AVEnc_End(ctx);
return NULL;
}
ctx->audio = av_frame_alloc();
sz = ctx->audio_st->codec->frame_size;
if (!sz)
sz = VARIABLE_AUDIO_FRAME_MAX_SIZE;
sz *= av_get_bytes_per_sample(ctx->audio_st->codec->sample_fmt) * ctx->audio_st->codec->channels;
ctx->audio_outbuf = av_malloc(sz);
}
av_dump_format(ctx->fc, 0, streamname, 1);
if (!(fmt->flags & AVFMT_NOFILE))
{
if (avio_open(&ctx->fc->pb, streamname, AVIO_FLAG_WRITE) < 0)
//okay, this is annoying, but I'm too lazy to figure out the issue I was having with avio stuff.
if (!pFS_NativePath(streamname, FS_GAMEONLY, ctx->abspath, sizeof(ctx->abspath)) || avio_open(&ctx->fc->pb, ctx->abspath, AVIO_FLAG_WRITE) < 0)
{
Con_Printf("Could not open '%s'\n", streamname);
AVEnc_End(ctx);
@ -400,7 +583,14 @@ static void *AVEnc_Begin (char *streamname, int videorate, int width, int height
}
//nearly complete, can make the file dirty now.
avformat_write_header(ctx->fc, NULL);
err = avformat_write_header(ctx->fc, NULL);
if (err < 0)
{
char buf[512];
Con_Printf("avformat_write_header: failed %s\n", av_make_error_string(buf, sizeof(buf), err));
AVEnc_End(ctx);
return NULL;
}
ctx->doneheaders = true;
return ctx;
}
@ -413,18 +603,26 @@ static void AVEnc_End (void *vctx)
//don't write trailers if this is an error case and we never even wrote the headers.
if (ctx->doneheaders)
{
av_write_trailer(ctx->fc);
if (*ctx->abspath)
Con_Printf("Finished writing %s\n", ctx->abspath);
}
for(i = 0; i < ctx->fc->nb_streams; i++)
av_freep(&ctx->fc->streams[i]);
// if (!(fmt->flags & AVFMT_NOFILE))
avio_close(ctx->fc->pb);
av_free(ctx->audio_outbuf);
av_free(ctx->fc);
free(ctx);
}
static media_encoder_funcs_t encoderfuncs =
{
sizeof(media_encoder_funcs_t),
"avplug",
"Use ffmpeg's various codecs. Various settings are configured with the avplug_* cvars.",
".mp4",
AVEnc_Begin,
AVEnc_Video,
AVEnc_Audio,
@ -457,22 +655,28 @@ menutext 0 24 "Cancel"
qboolean AVEnc_Init(void)
{
pCvar_Register("avplug_format", "mp4", 0, "avplug");
pCvar_Register("avplug_videocodec", "mpeg4", 0, "avplug");
pCvar_Register("avplug_videocodecprofile", "", 0, "avplug");
pCvar_Register("avplug_videobitrate", "4000000", 0, "avplug");
pCvar_Register("avplug_videoforcewidth", "", 0, "avplug");
pCvar_Register("avplug_videoforceheight", "", 0, "avplug");
pCvar_Register("avplug_audiocodec", "libmp3lame", 0, "avplug");
pCvar_Register("avplug_audiobitrate", "64000", 0, "avplug");
CHECKBUILTIN(FS_NativePath);
if (!BUILTINISVALID(FS_NativePath))
{
Con_Printf("avplug: Engine too old\n");
return false;
}
if (!pPlug_ExportNative("Media_VideoEncoder", &encoderfuncs))
{
Con_Printf("avplug: Engine doesn't support media encoder plugins\n");
return false;
}
pCvar_Register("avplug_format_force", "", 0, "avplug");
pCvar_Register("avplug_videocodec", "", 0, "avplug");
pCvar_Register("avplug_videobitrate", "4000000", 0, "avplug");
pCvar_Register("avplug_videoforcewidth", "", 0, "avplug");
pCvar_Register("avplug_videoforceheight", "", 0, "avplug");
pCvar_Register("avplug_videopreset", "veryfast", 0, "avplug");
pCvar_Register("avplug_audiocodec", "", 0, "avplug");
pCvar_Register("avplug_audiobitrate", "64000", 0, "avplug");
// if (Plug_Export("ExecuteCommand", AVEnc_ExecuteCommand))
// Cmd_AddCommand("avcapture");