2016-02-23 23:16:51 +00:00
|
|
|
#include "graphics.h"
|
|
|
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
|
|
|
|
#include "../obs-ffmpeg-compat.h"
|
|
|
|
|
|
|
|
struct ffmpeg_image {
|
2019-09-22 21:19:10 +00:00
|
|
|
const char *file;
|
|
|
|
AVFormatContext *fmt_ctx;
|
|
|
|
AVCodecContext *decoder_ctx;
|
|
|
|
AVCodec *decoder;
|
|
|
|
AVStream *stream;
|
|
|
|
int stream_idx;
|
|
|
|
|
|
|
|
int cx, cy;
|
2016-02-23 23:16:51 +00:00
|
|
|
enum AVPixelFormat format;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool ffmpeg_image_open_decoder_context(struct ffmpeg_image *info)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
int ret = av_find_best_stream(info->fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, 1,
|
|
|
|
NULL, 0);
|
2016-02-23 23:16:51 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_WARNING, "Couldn't find video stream in file '%s': %s",
|
2019-09-22 21:19:10 +00:00
|
|
|
info->file, av_err2str(ret));
|
2016-02-23 23:16:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
info->stream_idx = ret;
|
|
|
|
info->stream = info->fmt_ctx->streams[ret];
|
2016-02-23 23:16:51 +00:00
|
|
|
info->decoder_ctx = info->stream->codec;
|
2019-09-22 21:19:10 +00:00
|
|
|
info->decoder = avcodec_find_decoder(info->decoder_ctx->codec_id);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
if (!info->decoder) {
|
|
|
|
blog(LOG_WARNING, "Failed to find decoder for file '%s'",
|
2019-09-22 21:19:10 +00:00
|
|
|
info->file);
|
2016-02-23 23:16:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avcodec_open2(info->decoder_ctx, info->decoder, NULL);
|
|
|
|
if (ret < 0) {
|
2019-09-22 21:19:10 +00:00
|
|
|
blog(LOG_WARNING,
|
|
|
|
"Failed to open video codec for file '%s': "
|
|
|
|
"%s",
|
|
|
|
info->file, av_err2str(ret));
|
2016-02-23 23:16:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_image_free(struct ffmpeg_image *info)
|
|
|
|
{
|
|
|
|
avcodec_close(info->decoder_ctx);
|
|
|
|
avformat_close_input(&info->fmt_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ffmpeg_image_init(struct ffmpeg_image *info, const char *file)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!file || !*file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memset(info, 0, sizeof(struct ffmpeg_image));
|
2019-09-22 21:19:10 +00:00
|
|
|
info->file = file;
|
2016-02-23 23:16:51 +00:00
|
|
|
info->stream_idx = -1;
|
|
|
|
|
|
|
|
ret = avformat_open_input(&info->fmt_ctx, file, NULL, NULL);
|
|
|
|
if (ret < 0) {
|
2019-09-22 21:19:10 +00:00
|
|
|
blog(LOG_WARNING, "Failed to open file '%s': %s", info->file,
|
|
|
|
av_err2str(ret));
|
2016-02-23 23:16:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avformat_find_stream_info(info->fmt_ctx, NULL);
|
|
|
|
if (ret < 0) {
|
2019-09-22 21:19:10 +00:00
|
|
|
blog(LOG_WARNING,
|
|
|
|
"Could not find stream info for file '%s':"
|
|
|
|
" %s",
|
|
|
|
info->file, av_err2str(ret));
|
2016-02-23 23:16:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ffmpeg_image_open_decoder_context(info))
|
|
|
|
goto fail;
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
info->cx = info->decoder_ctx->width;
|
|
|
|
info->cy = info->decoder_ctx->height;
|
2016-02-23 23:16:51 +00:00
|
|
|
info->format = info->decoder_ctx->pix_fmt;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
ffmpeg_image_free(info);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ffmpeg_image_reformat_frame(struct ffmpeg_image *info,
|
2019-09-22 21:19:10 +00:00
|
|
|
AVFrame *frame, uint8_t *out,
|
|
|
|
int linesize)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
|
|
|
struct SwsContext *sws_ctx = NULL;
|
2019-09-22 21:19:10 +00:00
|
|
|
int ret = 0;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
if (info->format == AV_PIX_FMT_RGBA ||
|
|
|
|
info->format == AV_PIX_FMT_BGRA ||
|
|
|
|
info->format == AV_PIX_FMT_BGR0) {
|
|
|
|
|
|
|
|
if (linesize != frame->linesize[0]) {
|
2019-09-22 21:19:10 +00:00
|
|
|
int min_line = linesize < frame->linesize[0]
|
|
|
|
? linesize
|
|
|
|
: frame->linesize[0];
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < info->cy; y++)
|
|
|
|
memcpy(out + y * linesize,
|
|
|
|
frame->data[0] + y * frame->linesize[0],
|
|
|
|
min_line);
|
|
|
|
} else {
|
|
|
|
memcpy(out, frame->data[0], linesize * info->cy);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
sws_ctx = sws_getContext(info->cx, info->cy, info->format,
|
2019-09-22 21:19:10 +00:00
|
|
|
info->cx, info->cy, AV_PIX_FMT_BGRA,
|
|
|
|
SWS_POINT, NULL, NULL, NULL);
|
2016-02-23 23:16:51 +00:00
|
|
|
if (!sws_ctx) {
|
2019-09-22 21:19:10 +00:00
|
|
|
blog(LOG_WARNING,
|
|
|
|
"Failed to create scale context "
|
|
|
|
"for '%s'",
|
|
|
|
info->file);
|
2016-02-23 23:16:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
ret = sws_scale(sws_ctx, (const uint8_t *const *)frame->data,
|
2016-02-23 23:16:51 +00:00
|
|
|
frame->linesize, 0, info->cy, &out, &linesize);
|
|
|
|
sws_freeContext(sws_ctx);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_WARNING, "sws_scale failed for '%s': %s",
|
2019-09-22 21:19:10 +00:00
|
|
|
info->file, av_err2str(ret));
|
2016-02-23 23:16:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->format = AV_PIX_FMT_BGRA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ffmpeg_image_decode(struct ffmpeg_image *info, uint8_t *out,
|
2019-09-22 21:19:10 +00:00
|
|
|
int linesize)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
AVPacket packet = {0};
|
|
|
|
bool success = false;
|
|
|
|
AVFrame *frame = av_frame_alloc();
|
|
|
|
int got_frame = 0;
|
|
|
|
int ret;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
blog(LOG_WARNING, "Failed to create frame data for '%s'",
|
2019-09-22 21:19:10 +00:00
|
|
|
info->file);
|
2016-02-23 23:16:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_read_frame(info->fmt_ctx, &packet);
|
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_WARNING, "Failed to read image frame from '%s': %s",
|
2019-09-22 21:19:10 +00:00
|
|
|
info->file, av_err2str(ret));
|
2016-02-23 23:16:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!got_frame) {
|
2018-02-19 19:54:37 +00:00
|
|
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
|
|
|
|
ret = avcodec_send_packet(info->decoder_ctx, &packet);
|
|
|
|
if (ret == 0)
|
|
|
|
ret = avcodec_receive_frame(info->decoder_ctx, frame);
|
|
|
|
|
|
|
|
got_frame = (ret == 0);
|
|
|
|
|
|
|
|
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
|
|
|
|
ret = 0;
|
|
|
|
#else
|
2016-02-23 23:16:51 +00:00
|
|
|
ret = avcodec_decode_video2(info->decoder_ctx, frame,
|
2019-09-22 21:19:10 +00:00
|
|
|
&got_frame, &packet);
|
2018-02-19 19:54:37 +00:00
|
|
|
#endif
|
2016-02-23 23:16:51 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_WARNING, "Failed to decode frame for '%s': %s",
|
2019-09-22 21:19:10 +00:00
|
|
|
info->file, av_err2str(ret));
|
2016-02-23 23:16:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
success = ffmpeg_image_reformat_frame(info, frame, out, linesize);
|
|
|
|
|
|
|
|
fail:
|
2018-02-19 19:54:37 +00:00
|
|
|
av_packet_unref(&packet);
|
2016-02-23 23:16:51 +00:00
|
|
|
av_frame_free(&frame);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gs_init_image_deps(void)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
|
2016-02-23 23:16:51 +00:00
|
|
|
av_register_all();
|
2019-09-22 21:19:10 +00:00
|
|
|
#endif
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
void gs_free_image_deps(void) {}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
static inline enum gs_color_format convert_format(enum AVPixelFormat format)
|
|
|
|
{
|
|
|
|
switch ((int)format) {
|
2019-09-22 21:19:10 +00:00
|
|
|
case AV_PIX_FMT_RGBA:
|
|
|
|
return GS_RGBA;
|
|
|
|
case AV_PIX_FMT_BGRA:
|
|
|
|
return GS_BGRA;
|
|
|
|
case AV_PIX_FMT_BGR0:
|
|
|
|
return GS_BGRX;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GS_BGRX;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *gs_create_texture_file_data(const char *file,
|
2019-09-22 21:19:10 +00:00
|
|
|
enum gs_color_format *format,
|
|
|
|
uint32_t *cx_out, uint32_t *cy_out)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
|
|
|
struct ffmpeg_image image;
|
|
|
|
uint8_t *data = NULL;
|
|
|
|
|
|
|
|
if (ffmpeg_image_init(&image, file)) {
|
|
|
|
data = bmalloc(image.cx * image.cy * 4);
|
|
|
|
|
|
|
|
if (ffmpeg_image_decode(&image, data, image.cx * 4)) {
|
|
|
|
*format = convert_format(image.format);
|
|
|
|
*cx_out = (uint32_t)image.cx;
|
|
|
|
*cy_out = (uint32_t)image.cy;
|
|
|
|
} else {
|
|
|
|
bfree(data);
|
|
|
|
data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ffmpeg_image_free(&image);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|