2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2012-01-08 00:08:18 +00:00
|
|
|
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
|
2011-12-24 12:46:12 +00:00
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2011-12-24 12:46:12 +00:00
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2011-12-24 12:46:12 +00:00
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
2012-04-21 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-12-24 12:46:12 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-12-25 11:47:47 +00:00
|
|
|
#include "../msvc/msvc_compat.h"
|
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2011-01-03 16:51:17 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavutil/mathematics.h>
|
2012-08-27 12:56:42 +00:00
|
|
|
#include <libavutil/avutil.h>
|
|
|
|
#include <libavutil/avstring.h>
|
2011-06-14 16:45:30 +00:00
|
|
|
#include <libavutil/opt.h>
|
2011-01-03 16:51:17 +00:00
|
|
|
#include <libavformat/avformat.h>
|
2011-08-11 03:25:31 +00:00
|
|
|
#include <libavutil/avconfig.h>
|
2011-12-24 12:46:12 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-03 16:51:17 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-12-24 12:46:12 +00:00
|
|
|
#include "../boolean.h"
|
2011-12-25 11:47:47 +00:00
|
|
|
#include "../fifo_buffer.h"
|
|
|
|
#include "../thread.h"
|
2012-03-02 21:19:49 +00:00
|
|
|
#include "../general.h"
|
2012-09-02 13:12:00 +00:00
|
|
|
#include "../gfx/scaler/scaler.h"
|
2012-03-02 21:19:49 +00:00
|
|
|
#include "ffemu.h"
|
2011-12-25 11:56:21 +00:00
|
|
|
#include <assert.h>
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-09-02 21:49:30 +00:00
|
|
|
#ifdef FFEMU_PERF
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-23 20:57:41 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-12-25 11:47:47 +00:00
|
|
|
struct ff_video_info
|
2011-01-03 16:51:17 +00:00
|
|
|
{
|
|
|
|
AVCodecContext *codec;
|
2011-10-22 20:54:36 +00:00
|
|
|
AVCodec *encoder;
|
2011-01-03 19:46:50 +00:00
|
|
|
|
|
|
|
AVFrame *conv_frame;
|
|
|
|
uint8_t *conv_frame_buf;
|
2011-01-04 14:44:05 +00:00
|
|
|
int64_t frame_cnt;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
|
|
|
uint8_t *outbuf;
|
|
|
|
size_t outbuf_size;
|
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
enum PixelFormat pix_fmt;
|
2011-11-05 11:25:09 +00:00
|
|
|
size_t pix_size;
|
2011-08-11 03:25:31 +00:00
|
|
|
|
2011-01-04 14:44:05 +00:00
|
|
|
AVFormatContext *format;
|
2011-06-14 16:52:02 +00:00
|
|
|
|
2012-09-02 13:12:00 +00:00
|
|
|
struct scaler_ctx scaler;
|
2011-12-25 15:11:48 +00:00
|
|
|
};
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2011-12-25 11:47:47 +00:00
|
|
|
struct ff_audio_info
|
2011-01-03 16:51:17 +00:00
|
|
|
{
|
|
|
|
AVCodecContext *codec;
|
2011-10-22 20:54:36 +00:00
|
|
|
AVCodec *encoder;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
|
|
|
int16_t *buffer;
|
|
|
|
size_t frames_in_buffer;
|
|
|
|
|
2011-01-04 15:57:55 +00:00
|
|
|
int64_t frame_cnt;
|
|
|
|
|
2011-12-24 14:57:43 +00:00
|
|
|
uint8_t *outbuf;
|
2011-01-03 16:51:17 +00:00
|
|
|
size_t outbuf_size;
|
2011-12-25 15:11:48 +00:00
|
|
|
};
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2011-12-25 11:47:47 +00:00
|
|
|
struct ff_muxer_info
|
2011-01-04 14:44:05 +00:00
|
|
|
{
|
|
|
|
AVFormatContext *ctx;
|
|
|
|
AVStream *astream;
|
|
|
|
AVStream *vstream;
|
|
|
|
};
|
|
|
|
|
2011-01-03 16:51:17 +00:00
|
|
|
struct ffemu
|
|
|
|
{
|
2011-12-25 11:47:47 +00:00
|
|
|
struct ff_video_info video;
|
|
|
|
struct ff_audio_info audio;
|
|
|
|
struct ff_muxer_info muxer;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
|
|
|
struct ffemu_params params;
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
scond_t *cond;
|
|
|
|
slock_t *cond_lock;
|
|
|
|
slock_t *lock;
|
2011-06-14 19:35:31 +00:00
|
|
|
fifo_buffer_t *audio_fifo;
|
|
|
|
fifo_buffer_t *video_fifo;
|
|
|
|
fifo_buffer_t *attr_fifo;
|
2011-10-06 18:31:39 +00:00
|
|
|
sthread_t *thread;
|
|
|
|
|
2011-06-14 19:35:31 +00:00
|
|
|
volatile bool alive;
|
|
|
|
volatile bool can_sleep;
|
2011-01-03 16:51:17 +00:00
|
|
|
};
|
|
|
|
|
2012-03-02 21:19:49 +00:00
|
|
|
static bool ffemu_init_audio(struct ff_audio_info *audio, struct ffemu_params *param)
|
2011-01-03 16:51:17 +00:00
|
|
|
{
|
2012-03-07 23:29:32 +00:00
|
|
|
AVCodec *codec = avcodec_find_encoder_by_name("flac");
|
2011-01-03 16:51:17 +00:00
|
|
|
if (!codec)
|
2011-06-14 19:35:31 +00:00
|
|
|
return false;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2011-10-22 20:54:36 +00:00
|
|
|
audio->encoder = codec;
|
|
|
|
|
2011-09-23 20:57:41 +00:00
|
|
|
// FFmpeg just loves to deprecate stuff :)
|
|
|
|
#ifdef HAVE_FFMPEG_ALLOC_CONTEXT3
|
2011-08-19 00:06:13 +00:00
|
|
|
audio->codec = avcodec_alloc_context3(codec);
|
2011-09-23 20:57:41 +00:00
|
|
|
#else
|
|
|
|
audio->codec = avcodec_alloc_context();
|
|
|
|
avcodec_get_context_defaults(audio->codec);
|
|
|
|
#endif
|
2011-01-04 22:55:29 +00:00
|
|
|
|
2011-12-25 11:47:47 +00:00
|
|
|
audio->codec->sample_rate = (int)roundf(param->samplerate);
|
2011-11-16 17:56:42 +00:00
|
|
|
audio->codec->time_base = av_d2q(1.0 / param->samplerate, 1000000);
|
2011-01-03 16:51:17 +00:00
|
|
|
audio->codec->channels = param->channels;
|
2011-01-04 20:21:15 +00:00
|
|
|
audio->codec->sample_fmt = AV_SAMPLE_FMT_S16;
|
2011-09-23 20:57:41 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FFMPEG_AVCODEC_OPEN2
|
2011-08-19 00:06:13 +00:00
|
|
|
if (avcodec_open2(audio->codec, codec, NULL) != 0)
|
2011-09-23 20:57:41 +00:00
|
|
|
#else
|
|
|
|
if (avcodec_open(audio->codec, codec) != 0)
|
|
|
|
#endif
|
|
|
|
{
|
2011-06-14 19:35:31 +00:00
|
|
|
return false;
|
2011-09-23 20:57:41 +00:00
|
|
|
}
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-01-31 17:06:20 +00:00
|
|
|
audio->buffer = (int16_t*)av_malloc(
|
|
|
|
audio->codec->frame_size *
|
|
|
|
audio->codec->channels *
|
|
|
|
sizeof(int16_t));
|
2012-01-29 17:13:20 +00:00
|
|
|
|
2011-01-03 16:51:17 +00:00
|
|
|
if (!audio->buffer)
|
2011-06-14 19:35:31 +00:00
|
|
|
return false;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
audio->outbuf_size = FF_MIN_BUFFER_SIZE;
|
2011-12-24 14:57:43 +00:00
|
|
|
audio->outbuf = (uint8_t*)av_malloc(audio->outbuf_size);
|
2011-01-03 16:51:17 +00:00
|
|
|
if (!audio->outbuf)
|
2011-06-14 19:35:31 +00:00
|
|
|
return false;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2011-06-14 19:35:31 +00:00
|
|
|
return true;
|
2011-01-03 16:51:17 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 21:19:49 +00:00
|
|
|
static bool ffemu_init_video(struct ff_video_info *video, const struct ffemu_params *param)
|
2011-01-03 19:46:50 +00:00
|
|
|
{
|
2012-03-07 23:29:32 +00:00
|
|
|
AVCodec *codec = NULL;
|
|
|
|
if (g_settings.video.h264_record)
|
|
|
|
{
|
|
|
|
codec = avcodec_find_encoder_by_name("libx264rgb");
|
|
|
|
// Older versions of FFmpeg have RGB encoding in libx264.
|
|
|
|
if (!codec)
|
|
|
|
codec = avcodec_find_encoder_by_name("libx264");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
codec = avcodec_find_encoder_by_name("ffv1");
|
2012-08-16 07:25:13 +00:00
|
|
|
|
2011-01-03 19:46:50 +00:00
|
|
|
if (!codec)
|
2011-06-14 19:35:31 +00:00
|
|
|
return false;
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2011-10-22 20:54:36 +00:00
|
|
|
video->encoder = codec;
|
|
|
|
|
2012-08-25 20:38:49 +00:00
|
|
|
switch (param->pix_fmt)
|
|
|
|
{
|
|
|
|
case FFEMU_PIX_XRGB1555:
|
2012-09-02 13:12:00 +00:00
|
|
|
video->scaler.in_fmt = SCALER_FMT_0RGB1555;
|
2012-08-25 20:38:49 +00:00
|
|
|
video->pix_size = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FFEMU_PIX_BGR24:
|
2012-09-02 13:12:00 +00:00
|
|
|
video->scaler.in_fmt = SCALER_FMT_BGR24;
|
2012-08-25 20:38:49 +00:00
|
|
|
video->pix_size = 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FFEMU_PIX_ARGB8888:
|
2012-09-02 13:12:00 +00:00
|
|
|
video->scaler.in_fmt = SCALER_FMT_ARGB8888;
|
2012-08-25 20:38:49 +00:00
|
|
|
video->pix_size = 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
2011-08-11 03:25:31 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 13:12:00 +00:00
|
|
|
if (g_settings.video.h264_record)
|
|
|
|
{
|
|
|
|
video->pix_fmt = PIX_FMT_BGR24;
|
|
|
|
video->scaler.out_fmt = SCALER_FMT_BGR24;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
video->pix_fmt = PIX_FMT_RGB32;
|
|
|
|
video->scaler.out_fmt = SCALER_FMT_ARGB8888;
|
|
|
|
}
|
2011-10-09 10:27:02 +00:00
|
|
|
|
2011-09-23 20:57:41 +00:00
|
|
|
#ifdef HAVE_FFMPEG_ALLOC_CONTEXT3
|
2011-08-19 00:06:13 +00:00
|
|
|
video->codec = avcodec_alloc_context3(codec);
|
2011-09-23 20:57:41 +00:00
|
|
|
#else
|
|
|
|
video->codec = avcodec_alloc_context();
|
|
|
|
avcodec_get_context_defaults(video->codec);
|
|
|
|
#endif
|
|
|
|
|
2011-01-03 19:46:50 +00:00
|
|
|
video->codec->width = param->out_width;
|
|
|
|
video->codec->height = param->out_height;
|
2011-11-16 17:56:42 +00:00
|
|
|
video->codec->time_base = av_d2q(1.0 / param->fps, 1000000); // Arbitrary big number.
|
2011-01-04 15:57:55 +00:00
|
|
|
video->codec->sample_aspect_ratio = av_d2q(param->aspect_ratio * param->out_height / param->out_width, 255);
|
2011-10-09 10:27:02 +00:00
|
|
|
video->codec->pix_fmt = video->pix_fmt;
|
|
|
|
|
2011-11-02 19:33:37 +00:00
|
|
|
#ifdef HAVE_FFMPEG_AVCODEC_OPEN2
|
2011-10-09 10:27:02 +00:00
|
|
|
AVDictionary *opts = NULL;
|
2011-11-02 19:33:37 +00:00
|
|
|
#endif
|
2011-10-09 10:27:02 +00:00
|
|
|
|
2012-03-02 21:19:49 +00:00
|
|
|
if (g_settings.video.h264_record)
|
|
|
|
{
|
|
|
|
video->codec->thread_count = 3;
|
|
|
|
av_dict_set(&opts, "qp", "0", 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
video->codec->thread_count = 2;
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2011-09-23 20:57:41 +00:00
|
|
|
#ifdef HAVE_FFMPEG_AVCODEC_OPEN2
|
2011-10-09 10:27:02 +00:00
|
|
|
if (avcodec_open2(video->codec, codec, &opts) != 0)
|
2011-09-23 20:57:41 +00:00
|
|
|
#else
|
|
|
|
if (avcodec_open(video->codec, codec) != 0)
|
|
|
|
#endif
|
2011-06-14 19:35:31 +00:00
|
|
|
return false;
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2011-10-09 10:27:02 +00:00
|
|
|
#ifdef HAVE_FFMPEG_AVCODEC_OPEN2
|
|
|
|
if (opts)
|
|
|
|
av_dict_free(&opts);
|
|
|
|
#endif
|
|
|
|
|
2011-01-04 22:55:29 +00:00
|
|
|
// Allocate a big buffer :p ffmpeg API doesn't seem to give us some clues how big this buffer should be.
|
2011-08-11 03:25:31 +00:00
|
|
|
video->outbuf_size = 1 << 23;
|
2011-12-24 12:46:12 +00:00
|
|
|
video->outbuf = (uint8_t*)av_malloc(video->outbuf_size);
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2011-10-09 10:27:02 +00:00
|
|
|
size_t size = avpicture_get_size(video->pix_fmt, param->out_width, param->out_height);
|
2011-12-24 12:46:12 +00:00
|
|
|
video->conv_frame_buf = (uint8_t*)av_malloc(size);
|
2011-01-03 19:46:50 +00:00
|
|
|
video->conv_frame = avcodec_alloc_frame();
|
2011-10-09 10:27:02 +00:00
|
|
|
avpicture_fill((AVPicture*)video->conv_frame, video->conv_frame_buf, video->pix_fmt, param->out_width, param->out_height);
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2011-06-14 19:35:31 +00:00
|
|
|
return true;
|
2011-01-04 14:44:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 21:19:49 +00:00
|
|
|
static bool ffemu_init_muxer(ffemu_t *handle)
|
2011-01-04 14:44:05 +00:00
|
|
|
{
|
|
|
|
AVFormatContext *ctx = avformat_alloc_context();
|
|
|
|
av_strlcpy(ctx->filename, handle->params.filename, sizeof(ctx->filename));
|
|
|
|
ctx->oformat = av_guess_format(NULL, ctx->filename, NULL);
|
2011-09-23 20:57:41 +00:00
|
|
|
|
2012-03-23 22:47:34 +00:00
|
|
|
if (!ctx->oformat)
|
|
|
|
return false;
|
|
|
|
|
2011-09-23 20:57:41 +00:00
|
|
|
// FFmpeg sure likes to make things difficult.
|
|
|
|
#if defined(AVIO_FLAG_WRITE)
|
|
|
|
#define FFMPEG_FLAG_RW AVIO_FLAG_WRITE
|
|
|
|
#elif defined(AVIO_WRONLY)
|
|
|
|
#define FFMPEG_FLAG_RW AVIO_WRONLY
|
|
|
|
#elif defined(URL_WRONLY)
|
|
|
|
#define FFMPEG_FLAG_RW URL_WRONLY
|
|
|
|
#else
|
|
|
|
#define FFMPEG_FLAG_RW 2 // Seems to be consistent, but you never know.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_FFMPEG_AVIO_OPEN
|
|
|
|
if (avio_open(&ctx->pb, ctx->filename, FFMPEG_FLAG_RW) < 0)
|
|
|
|
#else
|
|
|
|
if (url_fopen(&ctx->pb, ctx->filename, FFMPEG_FLAG_RW) < 0)
|
|
|
|
#endif
|
2011-01-04 14:44:05 +00:00
|
|
|
{
|
|
|
|
av_free(ctx);
|
2011-06-14 19:35:31 +00:00
|
|
|
return false;
|
2011-01-04 14:44:05 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 20:54:36 +00:00
|
|
|
#ifdef HAVE_FFMPEG_AVFORMAT_NEW_STREAM
|
|
|
|
AVStream *stream = avformat_new_stream(ctx, handle->video.encoder);
|
|
|
|
#else
|
2011-11-05 10:37:04 +00:00
|
|
|
unsigned stream_cnt = 0;
|
2011-06-14 16:45:30 +00:00
|
|
|
AVStream *stream = av_new_stream(ctx, stream_cnt++);
|
2011-10-22 20:54:36 +00:00
|
|
|
#endif
|
2011-06-14 16:45:30 +00:00
|
|
|
stream->codec = handle->video.codec;
|
2011-01-04 14:44:05 +00:00
|
|
|
|
2011-06-14 16:45:30 +00:00
|
|
|
if (ctx->oformat->flags & AVFMT_GLOBALHEADER)
|
|
|
|
handle->video.codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
handle->muxer.vstream = stream;
|
|
|
|
handle->muxer.vstream->sample_aspect_ratio = handle->video.codec->sample_aspect_ratio;
|
2011-01-04 14:44:05 +00:00
|
|
|
|
2011-10-22 20:54:36 +00:00
|
|
|
#ifdef HAVE_FFMPEG_AVFORMAT_NEW_STREAM
|
|
|
|
stream = avformat_new_stream(ctx, handle->audio.encoder);
|
|
|
|
#else
|
2011-06-14 16:45:30 +00:00
|
|
|
stream = av_new_stream(ctx, stream_cnt++);
|
2011-10-22 20:54:36 +00:00
|
|
|
#endif
|
2011-06-14 16:45:30 +00:00
|
|
|
stream->codec = handle->audio.codec;
|
2011-01-04 14:44:05 +00:00
|
|
|
|
2011-06-14 16:45:30 +00:00
|
|
|
if (ctx->oformat->flags & AVFMT_GLOBALHEADER)
|
|
|
|
handle->audio.codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
handle->muxer.astream = stream;
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2012-08-27 12:54:53 +00:00
|
|
|
#ifdef AVFMT_TS_NONSTRICT
|
2012-08-16 07:25:13 +00:00
|
|
|
// Avoids a warning at end about non-monotonically increasing DTS values.
|
|
|
|
// It seems to be harmless to disable this.
|
2012-03-02 21:19:49 +00:00
|
|
|
if (g_settings.video.h264_record)
|
|
|
|
ctx->oformat->flags |= AVFMT_TS_NONSTRICT;
|
2012-08-27 12:54:53 +00:00
|
|
|
#endif
|
2011-10-09 10:27:02 +00:00
|
|
|
|
2012-04-21 21:13:50 +00:00
|
|
|
av_dict_set(&ctx->metadata, "title", "RetroArch video dump", 0);
|
2011-11-05 11:25:09 +00:00
|
|
|
|
2011-09-23 20:57:41 +00:00
|
|
|
#ifdef HAVE_FFMPEG_AVFORMAT_WRITE_HEADER
|
2011-08-19 00:06:13 +00:00
|
|
|
if (avformat_write_header(ctx, NULL) < 0)
|
2011-09-23 20:57:41 +00:00
|
|
|
#else
|
|
|
|
if (av_write_header(ctx) != 0)
|
|
|
|
#endif
|
2011-06-14 19:35:31 +00:00
|
|
|
return false;
|
2011-01-04 14:44:05 +00:00
|
|
|
|
|
|
|
handle->muxer.ctx = ctx;
|
2011-06-14 19:35:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-14 09:27:02 +00:00
|
|
|
#define MAX_FRAMES 32
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
static void ffemu_thread(void *data);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
static bool init_thread(ffemu_t *handle)
|
|
|
|
{
|
2011-12-25 11:56:21 +00:00
|
|
|
handle->lock = slock_new();
|
|
|
|
handle->cond_lock = slock_new();
|
|
|
|
handle->cond = scond_new();
|
|
|
|
handle->audio_fifo = fifo_new(32000 * sizeof(int16_t) * handle->params.channels * MAX_FRAMES / 60);
|
|
|
|
handle->attr_fifo = fifo_new(sizeof(struct ffemu_video_data) * MAX_FRAMES);
|
|
|
|
handle->video_fifo = fifo_new(handle->params.fb_width * handle->params.fb_height *
|
|
|
|
handle->video.pix_size * MAX_FRAMES);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
handle->alive = true;
|
|
|
|
handle->can_sleep = true;
|
2011-12-25 11:56:21 +00:00
|
|
|
handle->thread = sthread_create(ffemu_thread, handle);
|
|
|
|
|
|
|
|
assert(handle->lock && handle->cond_lock &&
|
|
|
|
handle->cond && handle->audio_fifo &&
|
|
|
|
handle->attr_fifo && handle->video_fifo && handle->thread);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void deinit_thread(ffemu_t *handle)
|
|
|
|
{
|
|
|
|
if (handle->thread)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
handle->alive = false;
|
|
|
|
handle->can_sleep = false;
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
scond_signal(handle->cond);
|
|
|
|
sthread_join(handle->thread);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_free(handle->lock);
|
|
|
|
slock_free(handle->cond_lock);
|
|
|
|
scond_free(handle->cond);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-06-14 19:52:31 +00:00
|
|
|
handle->thread = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void deinit_thread_buf(ffemu_t *handle)
|
|
|
|
{
|
|
|
|
if (handle->audio_fifo)
|
|
|
|
{
|
2011-06-14 19:35:31 +00:00
|
|
|
fifo_free(handle->audio_fifo);
|
2011-06-14 19:52:31 +00:00
|
|
|
handle->audio_fifo = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle->attr_fifo)
|
|
|
|
{
|
2011-06-14 19:35:31 +00:00
|
|
|
fifo_free(handle->attr_fifo);
|
2011-06-14 19:52:31 +00:00
|
|
|
handle->attr_fifo = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle->video_fifo)
|
|
|
|
{
|
2011-06-14 19:35:31 +00:00
|
|
|
fifo_free(handle->video_fifo);
|
2011-06-14 19:52:31 +00:00
|
|
|
handle->video_fifo = NULL;
|
2011-06-14 19:35:31 +00:00
|
|
|
}
|
2011-01-03 19:46:50 +00:00
|
|
|
}
|
|
|
|
|
2011-01-03 16:51:17 +00:00
|
|
|
ffemu_t *ffemu_new(const struct ffemu_params *params)
|
|
|
|
{
|
|
|
|
av_register_all();
|
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
ffemu_t *handle = (ffemu_t*)calloc(1, sizeof(*handle));
|
2011-01-03 16:51:17 +00:00
|
|
|
if (!handle)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
handle->params = *params;
|
|
|
|
|
2012-03-02 21:19:49 +00:00
|
|
|
if (!ffemu_init_video(&handle->video, &handle->params))
|
2011-06-14 16:45:30 +00:00
|
|
|
goto error;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-03-02 21:19:49 +00:00
|
|
|
if (!ffemu_init_audio(&handle->audio, &handle->params))
|
2011-06-14 16:45:30 +00:00
|
|
|
goto error;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-03-02 21:19:49 +00:00
|
|
|
if (!ffemu_init_muxer(handle))
|
2011-06-14 19:35:31 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!init_thread(handle))
|
2011-01-04 14:44:05 +00:00
|
|
|
goto error;
|
|
|
|
|
2011-01-03 16:51:17 +00:00
|
|
|
return handle;
|
|
|
|
|
|
|
|
error:
|
|
|
|
ffemu_free(handle);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ffemu_free(ffemu_t *handle)
|
|
|
|
{
|
|
|
|
if (handle)
|
|
|
|
{
|
2011-06-14 19:35:31 +00:00
|
|
|
deinit_thread(handle);
|
2011-06-14 19:52:31 +00:00
|
|
|
deinit_thread_buf(handle);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-01-03 16:51:17 +00:00
|
|
|
if (handle->audio.codec)
|
|
|
|
{
|
|
|
|
avcodec_close(handle->audio.codec);
|
|
|
|
av_free(handle->audio.codec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle->audio.buffer)
|
|
|
|
av_free(handle->audio.buffer);
|
|
|
|
|
|
|
|
if (handle->video.codec)
|
|
|
|
{
|
|
|
|
avcodec_close(handle->video.codec);
|
|
|
|
av_free(handle->video.codec);
|
|
|
|
}
|
|
|
|
|
2011-01-03 19:46:50 +00:00
|
|
|
if (handle->video.conv_frame)
|
|
|
|
av_free(handle->video.conv_frame);
|
|
|
|
|
|
|
|
if (handle->video.conv_frame_buf)
|
|
|
|
av_free(handle->video.conv_frame_buf);
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-09-02 13:12:00 +00:00
|
|
|
scaler_ctx_gen_reset(&handle->video.scaler);
|
2011-06-14 16:52:02 +00:00
|
|
|
|
2011-01-03 16:51:17 +00:00
|
|
|
free(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 17:56:42 +00:00
|
|
|
bool ffemu_push_video(ffemu_t *handle, const struct ffemu_video_data *data)
|
2011-06-14 19:35:31 +00:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
unsigned avail = fifo_write_avail(handle->attr_fifo);
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
if (!handle->alive)
|
2011-11-16 17:56:42 +00:00
|
|
|
return false;
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
if (avail >= sizeof(*data))
|
|
|
|
break;
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
if (handle->can_sleep)
|
|
|
|
{
|
|
|
|
handle->can_sleep = false;
|
2011-10-06 18:31:39 +00:00
|
|
|
scond_wait(handle->cond, handle->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
handle->can_sleep = true;
|
|
|
|
}
|
|
|
|
else
|
2011-10-06 18:31:39 +00:00
|
|
|
scond_signal(handle->cond);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->lock);
|
2011-10-06 22:04:52 +00:00
|
|
|
|
2012-07-07 15:08:55 +00:00
|
|
|
// Tightly pack our frame to conserve memory. libretro tends to use a very large pitch.
|
2011-10-06 22:04:52 +00:00
|
|
|
struct ffemu_video_data attr_data = *data;
|
2011-11-22 16:27:02 +00:00
|
|
|
|
|
|
|
if (attr_data.is_dupe)
|
|
|
|
attr_data.width = attr_data.height = attr_data.pitch = 0;
|
|
|
|
else
|
|
|
|
attr_data.pitch = attr_data.width * handle->video.pix_size;
|
2011-10-14 09:27:02 +00:00
|
|
|
|
2011-10-06 22:04:52 +00:00
|
|
|
fifo_write(handle->attr_fifo, &attr_data, sizeof(attr_data));
|
|
|
|
|
2012-08-25 20:38:49 +00:00
|
|
|
int offset = 0;
|
2011-11-22 16:27:02 +00:00
|
|
|
for (unsigned y = 0; y < attr_data.height; y++, offset += data->pitch)
|
2011-10-06 22:04:52 +00:00
|
|
|
fifo_write(handle->video_fifo, (const uint8_t*)data->data + offset, attr_data.pitch);
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->lock);
|
|
|
|
scond_signal(handle->cond);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-11-16 17:56:42 +00:00
|
|
|
return true;
|
2011-06-14 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
2011-11-16 17:56:42 +00:00
|
|
|
bool ffemu_push_audio(ffemu_t *handle, const struct ffemu_audio_data *data)
|
2011-06-14 19:35:31 +00:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
unsigned avail = fifo_write_avail(handle->audio_fifo);
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
if (!handle->alive)
|
2011-11-16 17:56:42 +00:00
|
|
|
return false;
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
if (avail >= data->frames * handle->params.channels * sizeof(int16_t))
|
|
|
|
break;
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
if (handle->can_sleep)
|
|
|
|
{
|
|
|
|
handle->can_sleep = false;
|
2011-10-06 18:31:39 +00:00
|
|
|
scond_wait(handle->cond, handle->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
handle->can_sleep = true;
|
|
|
|
}
|
|
|
|
else
|
2011-10-06 18:31:39 +00:00
|
|
|
scond_signal(handle->cond);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
fifo_write(handle->audio_fifo, data->data, data->frames * handle->params.channels * sizeof(int16_t));
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->lock);
|
|
|
|
scond_signal(handle->cond);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-11-16 17:56:42 +00:00
|
|
|
return true;
|
2011-06-14 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
static bool encode_video(ffemu_t *handle, AVPacket *pkt, AVFrame *frame)
|
2011-01-03 16:51:17 +00:00
|
|
|
{
|
2012-03-07 23:29:32 +00:00
|
|
|
av_init_packet(pkt);
|
|
|
|
pkt->data = handle->video.outbuf;
|
|
|
|
pkt->size = handle->video.outbuf_size;
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
#ifdef HAVE_FFMPEG_AVCODEC_ENCODE_VIDEO2
|
|
|
|
int got_packet = 0;
|
|
|
|
if (avcodec_encode_video2(handle->video.codec, pkt, frame, &got_packet) < 0)
|
|
|
|
return false;
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (!got_packet)
|
|
|
|
{
|
|
|
|
pkt->size = 0;
|
|
|
|
pkt->pts = AV_NOPTS_VALUE;
|
|
|
|
pkt->dts = AV_NOPTS_VALUE;
|
|
|
|
return true;
|
2011-11-22 16:27:02 +00:00
|
|
|
}
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (pkt->pts != (int64_t)AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt->pts = av_rescale_q(pkt->pts, handle->video.codec->time_base,
|
|
|
|
handle->muxer.vstream->time_base);
|
|
|
|
}
|
2011-01-04 21:35:49 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt->dts = av_rescale_q(pkt->dts, handle->video.codec->time_base,
|
|
|
|
handle->muxer.vstream->time_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2011-08-11 03:25:31 +00:00
|
|
|
int outsize = avcodec_encode_video(handle->video.codec, handle->video.outbuf,
|
2012-03-07 23:29:32 +00:00
|
|
|
handle->video.outbuf_size, frame);
|
2011-01-03 19:46:50 +00:00
|
|
|
|
2011-01-04 22:55:29 +00:00
|
|
|
if (outsize < 0)
|
2011-11-16 17:56:42 +00:00
|
|
|
return false;
|
2011-01-04 22:55:29 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
pkt->size = outsize;
|
2011-01-04 20:21:15 +00:00
|
|
|
|
2012-01-29 18:12:29 +00:00
|
|
|
if (handle->video.codec->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
|
2012-01-29 18:10:58 +00:00
|
|
|
{
|
2012-03-07 23:29:32 +00:00
|
|
|
pkt->pts = av_rescale_q(handle->video.codec->coded_frame->pts, handle->video.codec->time_base,
|
2012-01-29 18:10:58 +00:00
|
|
|
handle->muxer.vstream->time_base);
|
|
|
|
}
|
|
|
|
else
|
2012-03-07 23:29:32 +00:00
|
|
|
pkt->pts = AV_NOPTS_VALUE;
|
2011-01-04 20:21:15 +00:00
|
|
|
|
|
|
|
if (handle->video.codec->coded_frame->key_frame)
|
2012-03-07 23:29:32 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pkt->stream_index = handle->muxer.vstream->index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ffemu_push_video_thread(ffemu_t *handle, const struct ffemu_video_data *data)
|
|
|
|
{
|
|
|
|
if (!data->is_dupe)
|
|
|
|
{
|
2012-09-02 13:12:00 +00:00
|
|
|
if (data->width != handle->video.scaler.in_width || data->height != handle->video.scaler.in_height)
|
|
|
|
{
|
|
|
|
handle->video.scaler.in_width = data->width;
|
|
|
|
handle->video.scaler.in_height = data->height;
|
|
|
|
handle->video.scaler.in_stride = data->pitch;
|
2012-03-07 23:29:32 +00:00
|
|
|
|
2012-09-02 13:30:15 +00:00
|
|
|
// Attempt to preserve more information if we scale down.
|
|
|
|
bool shrunk = handle->params.out_width < data->width || handle->params.out_height < data->height;
|
|
|
|
handle->video.scaler.scaler_type = shrunk ? SCALER_TYPE_BILINEAR : SCALER_TYPE_POINT;
|
2012-09-02 13:12:00 +00:00
|
|
|
|
|
|
|
handle->video.scaler.out_width = handle->params.out_width;
|
|
|
|
handle->video.scaler.out_height = handle->params.out_height;
|
|
|
|
handle->video.scaler.out_stride = handle->video.conv_frame->linesize[0];
|
|
|
|
|
|
|
|
scaler_ctx_gen_filter(&handle->video.scaler);
|
|
|
|
}
|
2012-03-07 23:29:32 +00:00
|
|
|
|
2012-09-02 13:12:00 +00:00
|
|
|
scaler_ctx_scale(&handle->video.scaler, handle->video.conv_frame->data[0], data->data);
|
2012-03-07 23:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handle->video.conv_frame->pts = handle->video.frame_cnt;
|
|
|
|
|
|
|
|
AVPacket pkt;
|
|
|
|
if (!encode_video(handle, &pkt, handle->video.conv_frame))
|
|
|
|
return false;
|
2011-01-04 14:44:05 +00:00
|
|
|
|
2012-01-29 17:01:10 +00:00
|
|
|
if (pkt.size)
|
2011-01-04 21:35:49 +00:00
|
|
|
{
|
|
|
|
if (av_interleaved_write_frame(handle->muxer.ctx, &pkt) < 0)
|
2011-11-16 17:56:42 +00:00
|
|
|
return false;
|
2011-01-04 21:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handle->video.frame_cnt++;
|
2011-11-16 17:56:42 +00:00
|
|
|
return true;
|
2011-01-03 16:51:17 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
static bool encode_audio(ffemu_t *handle, AVPacket *pkt, bool dry)
|
2011-01-03 16:51:17 +00:00
|
|
|
{
|
2012-03-07 23:29:32 +00:00
|
|
|
av_init_packet(pkt);
|
|
|
|
pkt->data = handle->audio.outbuf;
|
|
|
|
pkt->size = handle->audio.outbuf_size;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
#ifdef HAVE_FFMPEG_AVCODEC_ENCODE_AUDIO2
|
|
|
|
AVFrame frame;
|
|
|
|
avcodec_get_frame_defaults(&frame);
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
frame.nb_samples = handle->audio.frames_in_buffer;
|
|
|
|
frame.pts = handle->audio.frame_cnt;
|
2011-01-03 16:51:17 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
int samples_size = frame.nb_samples *
|
|
|
|
handle->audio.codec->channels *
|
|
|
|
sizeof(int16_t);
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
avcodec_fill_audio_frame(&frame, handle->audio.codec->channels,
|
|
|
|
handle->audio.codec->sample_fmt, (const uint8_t*)handle->audio.buffer,
|
|
|
|
samples_size, 1);
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
int got_packet = 0;
|
|
|
|
if (avcodec_encode_audio2(handle->audio.codec,
|
|
|
|
pkt, dry ? NULL : &frame, &got_packet) < 0)
|
|
|
|
return false;
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (!got_packet)
|
|
|
|
{
|
|
|
|
pkt->size = 0;
|
|
|
|
pkt->pts = AV_NOPTS_VALUE;
|
|
|
|
pkt->dts = AV_NOPTS_VALUE;
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (pkt->pts != (int64_t)AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt->pts = av_rescale_q(pkt->pts,
|
|
|
|
handle->audio.codec->time_base,
|
|
|
|
handle->muxer.astream->time_base);
|
|
|
|
}
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt->dts = av_rescale_q(pkt->dts,
|
|
|
|
handle->audio.codec->time_base,
|
|
|
|
handle->muxer.astream->time_base);
|
|
|
|
}
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
#else
|
|
|
|
if (dry)
|
|
|
|
return false;
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
memset(handle->audio.buffer + handle->audio.frames_in_buffer * handle->audio.codec->channels, 0,
|
|
|
|
(handle->audio.codec->frame_size - handle->audio.frames_in_buffer) *
|
|
|
|
handle->audio.codec->channels * sizeof(int16_t));
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
int out_size = avcodec_encode_audio(handle->audio.codec,
|
|
|
|
handle->audio.outbuf, handle->audio.outbuf_size, handle->audio.buffer);
|
2011-01-04 22:55:29 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (out_size < 0)
|
|
|
|
return false;
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (out_size == 0)
|
|
|
|
{
|
|
|
|
pkt->size = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-29 17:59:56 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
pkt->size = out_size;
|
2012-01-29 17:59:56 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (handle->audio.codec->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt->pts = av_rescale_q(handle->audio.codec->coded_frame->pts,
|
|
|
|
handle->audio.codec->time_base,
|
|
|
|
handle->muxer.astream->time_base);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pkt->pts = AV_NOPTS_VALUE;
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (handle->audio.codec->coded_frame->key_frame)
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2012-01-29 17:01:10 +00:00
|
|
|
#endif
|
2011-10-06 22:54:47 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
pkt->stream_index = handle->muxer.astream->index;
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-04 20:21:15 +00:00
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
static bool ffemu_push_audio_thread(ffemu_t *handle, const struct ffemu_audio_data *data, bool require_block)
|
|
|
|
{
|
|
|
|
size_t written_frames = 0;
|
|
|
|
while (written_frames < data->frames)
|
|
|
|
{
|
|
|
|
size_t can_write = handle->audio.codec->frame_size - handle->audio.frames_in_buffer;
|
|
|
|
size_t write_left = data->frames - written_frames;
|
|
|
|
size_t write_frames = write_left > can_write ? can_write : write_left;
|
|
|
|
size_t write_size = write_frames * handle->params.channels * sizeof(int16_t);
|
|
|
|
|
|
|
|
size_t samples_in_buffer = handle->audio.frames_in_buffer * handle->params.channels;
|
|
|
|
size_t written_samples = written_frames * handle->params.channels;
|
|
|
|
|
|
|
|
memcpy(handle->audio.buffer + samples_in_buffer,
|
|
|
|
data->data + written_samples,
|
|
|
|
write_size);
|
|
|
|
|
|
|
|
written_frames += write_frames;
|
|
|
|
handle->audio.frames_in_buffer += write_frames;
|
|
|
|
|
|
|
|
if ((handle->audio.frames_in_buffer < (size_t)handle->audio.codec->frame_size) && require_block)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
AVPacket pkt;
|
|
|
|
if (!encode_audio(handle, &pkt, false))
|
|
|
|
return false;
|
2012-01-29 17:01:10 +00:00
|
|
|
|
|
|
|
handle->audio.frames_in_buffer = 0;
|
|
|
|
handle->audio.frame_cnt += handle->audio.codec->frame_size;
|
|
|
|
|
2012-03-07 23:29:32 +00:00
|
|
|
if (pkt.size)
|
2012-01-29 17:01:10 +00:00
|
|
|
{
|
|
|
|
if (av_interleaved_write_frame(handle->muxer.ctx, &pkt) < 0)
|
|
|
|
return false;
|
2011-01-03 16:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-29 17:01:10 +00:00
|
|
|
|
2011-11-16 17:56:42 +00:00
|
|
|
return true;
|
2011-01-03 16:51:17 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
static void ffemu_flush_audio(ffemu_t *handle, int16_t *audio_buf, size_t audio_buf_size)
|
2011-01-03 16:51:17 +00:00
|
|
|
{
|
2012-01-29 17:59:56 +00:00
|
|
|
size_t avail = fifo_read_avail(handle->audio_fifo);
|
2012-03-07 23:45:03 +00:00
|
|
|
if (avail)
|
|
|
|
{
|
|
|
|
fifo_read(handle->audio_fifo, audio_buf, avail);
|
2012-03-07 23:29:32 +00:00
|
|
|
|
2012-03-07 23:45:03 +00:00
|
|
|
struct ffemu_audio_data aud = {0};
|
|
|
|
aud.frames = avail / (sizeof(int16_t) * handle->params.channels);
|
|
|
|
aud.data = audio_buf;
|
2012-01-29 17:59:56 +00:00
|
|
|
|
2012-03-07 23:45:03 +00:00
|
|
|
ffemu_push_audio_thread(handle, &aud, false);
|
|
|
|
}
|
2012-01-29 17:59:56 +00:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2012-03-07 23:29:32 +00:00
|
|
|
AVPacket pkt;
|
2012-03-07 23:45:03 +00:00
|
|
|
if (!encode_audio(handle, &pkt, true) || !pkt.size ||
|
2012-03-07 23:29:32 +00:00
|
|
|
av_interleaved_write_frame(handle->muxer.ctx, &pkt) < 0)
|
|
|
|
break;
|
2012-01-29 17:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ffemu_flush_video(ffemu_t *handle)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
2012-03-07 23:29:32 +00:00
|
|
|
AVPacket pkt;
|
|
|
|
if (!encode_video(handle, &pkt, NULL) || !pkt.size ||
|
|
|
|
av_interleaved_write_frame(handle->muxer.ctx, &pkt) < 0)
|
|
|
|
break;
|
2012-01-29 17:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
static void ffemu_flush_buffers(ffemu_t *handle)
|
|
|
|
{
|
|
|
|
void *video_buf = av_malloc(2 * handle->params.fb_width * handle->params.fb_height * handle->video.pix_size);
|
2011-10-06 22:54:47 +00:00
|
|
|
size_t audio_buf_size = 512 * handle->params.channels * sizeof(int16_t);
|
2011-12-24 12:46:12 +00:00
|
|
|
int16_t *audio_buf = (int16_t*)av_malloc(audio_buf_size);
|
2011-11-20 01:44:21 +00:00
|
|
|
|
|
|
|
// Try pushing data in an interleaving pattern to ease the work of the muxer a bit.
|
|
|
|
bool did_work;
|
|
|
|
do
|
2011-06-14 19:52:31 +00:00
|
|
|
{
|
2011-11-20 01:44:21 +00:00
|
|
|
did_work = false;
|
|
|
|
|
|
|
|
if (fifo_read_avail(handle->audio_fifo) >= audio_buf_size)
|
2011-06-15 01:04:20 +00:00
|
|
|
{
|
2011-11-20 01:44:21 +00:00
|
|
|
fifo_read(handle->audio_fifo, audio_buf, audio_buf_size);
|
2011-06-14 19:52:31 +00:00
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
struct ffemu_audio_data aud = {0};
|
|
|
|
aud.frames = 512;
|
|
|
|
aud.data = audio_buf;
|
2011-06-14 19:52:31 +00:00
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
ffemu_push_audio_thread(handle, &aud, true);
|
2011-11-20 01:44:21 +00:00
|
|
|
did_work = true;
|
2011-06-15 01:04:20 +00:00
|
|
|
}
|
2011-10-06 22:54:47 +00:00
|
|
|
|
|
|
|
struct ffemu_video_data attr_buf;
|
2011-11-20 01:44:21 +00:00
|
|
|
if (fifo_read_avail(handle->attr_fifo) >= sizeof(attr_buf))
|
2011-10-06 22:54:47 +00:00
|
|
|
{
|
|
|
|
fifo_read(handle->attr_fifo, &attr_buf, sizeof(attr_buf));
|
|
|
|
fifo_read(handle->video_fifo, video_buf, attr_buf.height * attr_buf.pitch);
|
|
|
|
attr_buf.data = video_buf;
|
|
|
|
ffemu_push_video_thread(handle, &attr_buf);
|
2011-11-22 16:27:02 +00:00
|
|
|
|
2011-11-20 01:44:21 +00:00
|
|
|
did_work = true;
|
2011-10-06 22:54:47 +00:00
|
|
|
}
|
2011-11-20 01:44:21 +00:00
|
|
|
} while (did_work);
|
2011-10-06 22:54:47 +00:00
|
|
|
|
2011-11-20 02:18:33 +00:00
|
|
|
// Flush out last audio.
|
2012-01-29 17:59:56 +00:00
|
|
|
ffemu_flush_audio(handle, audio_buf, audio_buf_size);
|
2011-06-14 19:52:31 +00:00
|
|
|
|
2011-11-20 02:18:33 +00:00
|
|
|
// Flush out last video.
|
2012-01-29 17:59:56 +00:00
|
|
|
ffemu_flush_video(handle);
|
2011-01-04 14:44:05 +00:00
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
av_free(video_buf);
|
|
|
|
av_free(audio_buf);
|
|
|
|
}
|
2011-10-09 10:27:02 +00:00
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
bool ffemu_finalize(ffemu_t *handle)
|
|
|
|
{
|
|
|
|
deinit_thread(handle);
|
2011-01-04 14:44:05 +00:00
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
// Flush out data still in buffers (internal, and FFmpeg internal).
|
|
|
|
ffemu_flush_buffers(handle);
|
2011-01-04 20:21:15 +00:00
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
deinit_thread_buf(handle);
|
2011-01-04 14:44:05 +00:00
|
|
|
|
2011-01-04 22:55:29 +00:00
|
|
|
// Write final data.
|
2011-01-04 14:44:05 +00:00
|
|
|
av_write_trailer(handle->muxer.ctx);
|
|
|
|
|
2011-11-16 17:56:42 +00:00
|
|
|
return true;
|
2011-01-03 16:51:17 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
static void ffemu_thread(void *data)
|
2011-06-14 19:35:31 +00:00
|
|
|
{
|
2011-12-24 12:46:12 +00:00
|
|
|
ffemu_t *ff = (ffemu_t*)data;
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-08-11 03:25:31 +00:00
|
|
|
// For some reason, FFmpeg has a tendency to crash if we don't overallocate a bit. :s
|
|
|
|
void *video_buf = av_malloc(2 * ff->params.fb_width * ff->params.fb_height * ff->video.pix_size);
|
2011-12-25 11:56:21 +00:00
|
|
|
assert(video_buf);
|
2011-06-15 01:04:20 +00:00
|
|
|
|
2011-10-06 22:54:47 +00:00
|
|
|
size_t audio_buf_size = 512 * ff->params.channels * sizeof(int16_t);
|
2011-12-24 12:46:12 +00:00
|
|
|
int16_t *audio_buf = (int16_t*)av_malloc(audio_buf_size);
|
2011-12-25 11:56:21 +00:00
|
|
|
assert(audio_buf);
|
2011-06-15 01:04:20 +00:00
|
|
|
|
2011-06-14 19:35:31 +00:00
|
|
|
while (ff->alive)
|
|
|
|
{
|
2011-10-06 22:54:47 +00:00
|
|
|
struct ffemu_video_data attr_buf;
|
|
|
|
|
2011-06-14 19:35:31 +00:00
|
|
|
bool avail_video = false;
|
|
|
|
bool avail_audio = false;
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(ff->lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
if (fifo_read_avail(ff->attr_fifo) >= sizeof(attr_buf))
|
|
|
|
avail_video = true;
|
|
|
|
|
2011-06-15 01:04:20 +00:00
|
|
|
if (fifo_read_avail(ff->audio_fifo) >= audio_buf_size)
|
2011-06-14 19:35:31 +00:00
|
|
|
avail_audio = true;
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(ff->lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
if (!avail_video && !avail_audio)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(ff->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
if (ff->can_sleep)
|
|
|
|
{
|
|
|
|
ff->can_sleep = false;
|
2011-10-06 18:31:39 +00:00
|
|
|
scond_wait(ff->cond, ff->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
ff->can_sleep = true;
|
|
|
|
}
|
|
|
|
else
|
2011-10-06 18:31:39 +00:00
|
|
|
scond_signal(ff->cond);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(ff->cond_lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (avail_video)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(ff->lock);
|
2011-06-14 19:35:31 +00:00
|
|
|
fifo_read(ff->attr_fifo, &attr_buf, sizeof(attr_buf));
|
|
|
|
fifo_read(ff->video_fifo, video_buf, attr_buf.height * attr_buf.pitch);
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(ff->lock);
|
|
|
|
scond_signal(ff->cond);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
|
|
|
attr_buf.data = video_buf;
|
|
|
|
ffemu_push_video_thread(ff, &attr_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avail_audio)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(ff->lock);
|
2011-06-15 01:04:20 +00:00
|
|
|
fifo_read(ff->audio_fifo, audio_buf, audio_buf_size);
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(ff->lock);
|
|
|
|
scond_signal(ff->cond);
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
struct ffemu_audio_data aud = {0};
|
|
|
|
aud.frames = 512;
|
|
|
|
aud.data = audio_buf;
|
2011-06-14 19:35:31 +00:00
|
|
|
|
2012-01-29 17:59:56 +00:00
|
|
|
ffemu_push_audio_thread(ff, &aud, true);
|
2011-06-14 19:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 01:04:20 +00:00
|
|
|
av_free(video_buf);
|
|
|
|
av_free(audio_buf);
|
2011-06-14 19:35:31 +00:00
|
|
|
}
|
2011-10-06 18:31:39 +00:00
|
|
|
|