format code
This commit is contained in:
parent
e74278a042
commit
eff9ff10cc
2
.clang-format
Normal file
2
.clang-format
Normal file
@ -0,0 +1,2 @@
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
216
src/main.cpp
216
src/main.cpp
@ -1,23 +1,23 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define GLX_GLXEXT_PROTOTYPES
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <GL/glx.h>
|
||||
#include <GL/glxext.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <X11/extensions/Xcomposite.h>
|
||||
#include <X11/extensions/Xdamage.h>
|
||||
|
||||
extern "C" {
|
||||
#include <libavutil/hwcontext_cuda.h>
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/hwcontext.h>
|
||||
#include <libavutil/hwcontext_cuda.h>
|
||||
}
|
||||
#include <cudaGL.h>
|
||||
|
||||
@ -37,9 +37,9 @@ struct ScopedGLXFBConfig {
|
||||
};
|
||||
|
||||
struct WindowPixmap {
|
||||
WindowPixmap() : pixmap(None), glx_pixmap(None), texture_id(0), target_texture_id(0), texture_width(0), texture_height(0) {
|
||||
|
||||
}
|
||||
WindowPixmap()
|
||||
: pixmap(None), glx_pixmap(None), texture_id(0), target_texture_id(0),
|
||||
texture_width(0), texture_height(0) {}
|
||||
|
||||
Pixmap pixmap;
|
||||
GLXPixmap glx_pixmap;
|
||||
@ -58,7 +58,8 @@ static bool x11_supports_composite_named_window_pixmap(Display *dpy) {
|
||||
|
||||
int major_version;
|
||||
int minor_version;
|
||||
return XCompositeQueryVersion(dpy, &major_version, &minor_version) && (major_version > 0 || minor_version >= 2);
|
||||
return XCompositeQueryVersion(dpy, &major_version, &minor_version) &&
|
||||
(major_version > 0 || minor_version >= 2);
|
||||
}
|
||||
|
||||
static void cleanup_window_pixmap(Display *dpy, WindowPixmap &pixmap) {
|
||||
@ -86,27 +87,26 @@ static void cleanup_window_pixmap(Display *dpy, WindowPixmap &pixmap) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool recreate_window_pixmap(Display *dpy, Window window_id, WindowPixmap &pixmap) {
|
||||
static bool recreate_window_pixmap(Display *dpy, Window window_id,
|
||||
WindowPixmap &pixmap) {
|
||||
cleanup_window_pixmap(dpy, pixmap);
|
||||
|
||||
const int pixmap_config[] = {
|
||||
GLX_BIND_TO_TEXTURE_RGBA_EXT, True,
|
||||
GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
|
||||
GLX_BIND_TO_TEXTURE_RGBA_EXT, True, GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
|
||||
GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
|
||||
GLX_BIND_TO_MIPMAP_TEXTURE_EXT, True,
|
||||
GLX_DOUBLEBUFFER, False,
|
||||
GLX_BIND_TO_MIPMAP_TEXTURE_EXT, True, GLX_DOUBLEBUFFER, False,
|
||||
// GLX_Y_INVERTED_EXT, (int)GLX_DONT_CARE,
|
||||
None
|
||||
};
|
||||
None};
|
||||
|
||||
// Note that mipmap is generated even though its not used.
|
||||
// glCopyImageSubData fails if the texture doesn't have mipmap.
|
||||
const int pixmap_attribs[] = {
|
||||
GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
|
||||
GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
|
||||
GLX_MIPMAP_TEXTURE_EXT, 1,
|
||||
None
|
||||
};
|
||||
const int pixmap_attribs[] = {GLX_TEXTURE_TARGET_EXT,
|
||||
GLX_TEXTURE_2D_EXT,
|
||||
GLX_TEXTURE_FORMAT_EXT,
|
||||
GLX_TEXTURE_FORMAT_RGBA_EXT,
|
||||
GLX_MIPMAP_TEXTURE_EXT,
|
||||
1,
|
||||
None};
|
||||
|
||||
int c;
|
||||
GLXFBConfig *configs = glXChooseFBConfig(dpy, 0, pixmap_config, &c);
|
||||
@ -123,7 +123,8 @@ static bool recreate_window_pixmap(Display *dpy, Window window_id, WindowPixmap
|
||||
return false;
|
||||
}
|
||||
|
||||
GLXPixmap glx_pixmap = glXCreatePixmap(dpy, *configs, new_window_pixmap, pixmap_attribs);
|
||||
GLXPixmap glx_pixmap =
|
||||
glXCreatePixmap(dpy, *configs, new_window_pixmap, pixmap_attribs);
|
||||
if (!glx_pixmap) {
|
||||
fprintf(stderr, "Failed to create glx pixmap\n");
|
||||
XFreePixmap(dpy, new_window_pixmap);
|
||||
@ -142,25 +143,32 @@ static bool recreate_window_pixmap(Display *dpy, Window window_id, WindowPixmap
|
||||
|
||||
glXBindTexImageEXT(dpy, pixmap.glx_pixmap, GLX_FRONT_EXT, NULL);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &pixmap.texture_width);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &pixmap.texture_height);
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR );
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);//GL_LINEAR_MIPMAP_LINEAR );
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
|
||||
&pixmap.texture_width);
|
||||
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
|
||||
&pixmap.texture_height);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
||||
GL_NEAREST); // GL_LINEAR );
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
GL_NEAREST); // GL_LINEAR);//GL_LINEAR_MIPMAP_LINEAR );
|
||||
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
printf("texture width: %d, height: %d\n", pixmap.texture_width, pixmap.texture_height);
|
||||
printf("texture width: %d, height: %d\n", pixmap.texture_width,
|
||||
pixmap.texture_height);
|
||||
|
||||
// Generating this second texture is needed because cuGraphicsGLRegisterImage
|
||||
// cant be used with the texture that is mapped directly to the pixmap.
|
||||
// TODO: Investigate if it's somehow possible to use the pixmap texture directly,
|
||||
// this should improve performance since only less image copy is then needed every frame.
|
||||
// Generating this second texture is needed because
|
||||
// cuGraphicsGLRegisterImage cant be used with the texture that is mapped
|
||||
// directly to the pixmap.
|
||||
// TODO: Investigate if it's somehow possible to use the pixmap texture
|
||||
// directly, this should improve performance since only less image copy is
|
||||
// then needed every frame.
|
||||
glGenTextures(1, &pixmap.target_texture_id);
|
||||
glBindTexture(GL_TEXTURE_2D, pixmap.target_texture_id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.texture_width, pixmap.texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.texture_width,
|
||||
pixmap.texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
int err2 = glGetError();
|
||||
printf("error: %d\n", err2);
|
||||
glCopyImageSubData(
|
||||
pixmap.texture_id, GL_TEXTURE_2D, 0, 0, 0, 0,
|
||||
glCopyImageSubData(pixmap.texture_id, GL_TEXTURE_2D, 0, 0, 0, 0,
|
||||
pixmap.target_texture_id, GL_TEXTURE_2D, 0, 0, 0, 0,
|
||||
pixmap.texture_width, pixmap.texture_height, 1);
|
||||
int err = glGetError();
|
||||
@ -173,9 +181,10 @@ static bool recreate_window_pixmap(Display *dpy, Window window_id, WindowPixmap
|
||||
// glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
|
||||
// glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
|
||||
|
||||
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR );
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);//GL_LINEAR_MIPMAP_LINEAR );
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
||||
GL_NEAREST); // GL_LINEAR );
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
GL_NEAREST); // GL_LINEAR);//GL_LINEAR_MIPMAP_LINEAR );
|
||||
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
@ -200,7 +209,8 @@ std::vector<std::string> get_hardware_acceleration_device_names() {
|
||||
return {deviceName};
|
||||
}
|
||||
|
||||
static void receive_frames(AVCodecContext *av_codec_context, AVStream *stream, AVFormatContext *av_format_context) {
|
||||
static void receive_frames(AVCodecContext *av_codec_context, AVStream *stream,
|
||||
AVFormatContext *av_format_context) {
|
||||
AVPacket av_packet;
|
||||
av_init_packet(&av_packet);
|
||||
for (;;) {
|
||||
@ -208,7 +218,8 @@ static void receive_frames(AVCodecContext *av_codec_context, AVStream *stream, A
|
||||
av_packet.size = 0;
|
||||
int res = avcodec_receive_packet(av_codec_context, &av_packet);
|
||||
if (res == 0) { // we have a packet, send the packet to the muxer
|
||||
av_packet_rescale_ts(&av_packet, av_codec_context->time_base, stream->time_base);
|
||||
av_packet_rescale_ts(&av_packet, av_codec_context->time_base,
|
||||
stream->time_base);
|
||||
av_packet.stream_index = stream->index;
|
||||
if (av_write_frame(av_format_context, &av_packet) < 0) {
|
||||
fprintf(stderr, "Error: Failed to write frame to muxer\n");
|
||||
@ -228,14 +239,19 @@ static void receive_frames(AVCodecContext *av_codec_context, AVStream *stream, A
|
||||
av_packet_unref(&av_packet);
|
||||
}
|
||||
|
||||
static AVStream* add_stream(AVFormatContext *av_format_context, AVCodec **codec, enum AVCodecID codec_id, const WindowPixmap &window_pixmap) {
|
||||
static AVStream *add_stream(AVFormatContext *av_format_context, AVCodec **codec,
|
||||
enum AVCodecID codec_id,
|
||||
const WindowPixmap &window_pixmap) {
|
||||
//*codec = avcodec_find_encoder(codec_id);
|
||||
*codec = avcodec_find_encoder_by_name("h264_nvenc");
|
||||
if (!*codec) {
|
||||
*codec = avcodec_find_encoder_by_name("nvenc_h264");
|
||||
}
|
||||
if (!*codec) {
|
||||
fprintf(stderr, "Error: Could not find h264_nvenc or nvenc_h264 encoder for %s\n", avcodec_get_name(codec_id));
|
||||
fprintf(
|
||||
stderr,
|
||||
"Error: Could not find h264_nvenc or nvenc_h264 encoder for %s\n",
|
||||
avcodec_get_name(codec_id));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -249,7 +265,9 @@ static AVStream* add_stream(AVFormatContext *av_format_context, AVCodec **codec,
|
||||
|
||||
switch ((*codec)->type) {
|
||||
case AVMEDIA_TYPE_AUDIO: {
|
||||
codec_context->sample_fmt = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
|
||||
codec_context->sample_fmt = (*codec)->sample_fmts
|
||||
? (*codec)->sample_fmts[0]
|
||||
: AV_SAMPLE_FMT_FLTP;
|
||||
codec_context->bit_rate = 64000;
|
||||
codec_context->sample_rate = 44100;
|
||||
codec_context->channels = 2;
|
||||
@ -261,16 +279,18 @@ static AVStream* add_stream(AVFormatContext *av_format_context, AVCodec **codec,
|
||||
// Resolution must be a multiple of two
|
||||
codec_context->width = window_pixmap.texture_width & ~1;
|
||||
codec_context->height = window_pixmap.texture_height & ~1;
|
||||
// Timebase: This is the fundamental unit of time (in seconds) in terms of
|
||||
// which frame timestamps are represented. For fixed-fps content,
|
||||
// timebase should be 1/framerate and timestamp increments should be identical to 1
|
||||
// Timebase: This is the fundamental unit of time (in seconds) in terms
|
||||
// of which frame timestamps are represented. For fixed-fps content,
|
||||
// timebase should be 1/framerate and timestamp increments should be
|
||||
// identical to 1
|
||||
codec_context->time_base.num = 1;
|
||||
codec_context->time_base.den = 60;
|
||||
// codec_context->framerate.num = 60;
|
||||
// codec_context->framerate.den = 1;
|
||||
codec_context->sample_aspect_ratio.num = 1;
|
||||
codec_context->sample_aspect_ratio.den = 1;
|
||||
codec_context->gop_size = 12; // Emit one intra frame every twelve frames at most
|
||||
codec_context->gop_size =
|
||||
12; // Emit one intra frame every twelve frames at most
|
||||
codec_context->pix_fmt = AV_PIX_FMT_CUDA;
|
||||
if (codec_context->codec_id == AV_CODEC_ID_MPEG1VIDEO)
|
||||
codec_context->mb_decision = 2;
|
||||
@ -290,18 +310,27 @@ static AVStream* add_stream(AVFormatContext *av_format_context, AVCodec **codec,
|
||||
return stream;
|
||||
}
|
||||
|
||||
static void open_video(AVCodec *codec, AVStream *stream, WindowPixmap &window_pixmap, AVBufferRef **device_ctx, CUgraphicsResource *cuda_graphics_resource) {
|
||||
static void open_video(AVCodec *codec, AVStream *stream,
|
||||
WindowPixmap &window_pixmap, AVBufferRef **device_ctx,
|
||||
CUgraphicsResource *cuda_graphics_resource) {
|
||||
int ret;
|
||||
AVCodecContext *codec_context = stream->codec;
|
||||
|
||||
std::vector<std::string> hardware_accelerated_devices = get_hardware_acceleration_device_names();
|
||||
std::vector<std::string> hardware_accelerated_devices =
|
||||
get_hardware_acceleration_device_names();
|
||||
if (hardware_accelerated_devices.empty()) {
|
||||
fprintf(stderr, "Error: No hardware accelerated device was found on your system\n");
|
||||
fprintf(
|
||||
stderr,
|
||||
"Error: No hardware accelerated device was found on your system\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if(av_hwdevice_ctx_create(device_ctx, AV_HWDEVICE_TYPE_CUDA, hardware_accelerated_devices[0].c_str(), NULL, 0) < 0) {
|
||||
fprintf(stderr, "Error: Failed to create hardware device context for gpu: %s\n", hardware_accelerated_devices[0].c_str());
|
||||
if (av_hwdevice_ctx_create(device_ctx, AV_HWDEVICE_TYPE_CUDA,
|
||||
hardware_accelerated_devices[0].c_str(), NULL,
|
||||
0) < 0) {
|
||||
fprintf(stderr,
|
||||
"Error: Failed to create hardware device context for gpu: %s\n",
|
||||
hardware_accelerated_devices[0].c_str());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -311,7 +340,8 @@ static void open_video(AVCodec *codec, AVStream *stream, WindowPixmap &window_pi
|
||||
exit(1);
|
||||
}
|
||||
|
||||
AVHWFramesContext *hw_frame_context = (AVHWFramesContext*)frame_context->data;
|
||||
AVHWFramesContext *hw_frame_context =
|
||||
(AVHWFramesContext *)frame_context->data;
|
||||
hw_frame_context->width = codec_context->width;
|
||||
hw_frame_context->height = codec_context->height;
|
||||
hw_frame_context->sw_format = AV_PIX_FMT_0BGR32;
|
||||
@ -320,7 +350,8 @@ static void open_video(AVCodec *codec, AVStream *stream, WindowPixmap &window_pi
|
||||
hw_frame_context->device_ctx = (AVHWDeviceContext *)(*device_ctx)->data;
|
||||
|
||||
if (av_hwframe_ctx_init(frame_context) < 0) {
|
||||
fprintf(stderr, "Error: Failed to initialize hardware frame context (note: ffmpeg version needs to be > 4.0\n");
|
||||
fprintf(stderr, "Error: Failed to initialize hardware frame context "
|
||||
"(note: ffmpeg version needs to be > 4.0\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -329,12 +360,15 @@ static void open_video(AVCodec *codec, AVStream *stream, WindowPixmap &window_pi
|
||||
|
||||
ret = avcodec_open2(codec_context, codec, nullptr);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error: Could not open video codec: %s\n", "blabla");//av_err2str(ret));
|
||||
fprintf(stderr, "Error: Could not open video codec: %s\n",
|
||||
"blabla"); // av_err2str(ret));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
AVHWDeviceContext *hw_device_context = (AVHWDeviceContext*)(*device_ctx)->data;
|
||||
AVCUDADeviceContext *cuda_device_context = (AVCUDADeviceContext*)hw_device_context->hwctx;
|
||||
AVHWDeviceContext *hw_device_context =
|
||||
(AVHWDeviceContext *)(*device_ctx)->data;
|
||||
AVCUDADeviceContext *cuda_device_context =
|
||||
(AVCUDADeviceContext *)hw_device_context->hwctx;
|
||||
CUcontext *cuda_context = &(cuda_device_context->cuda_ctx);
|
||||
if (!cuda_context) {
|
||||
fprintf(stderr, "Error: No cuda context\n");
|
||||
@ -345,10 +379,15 @@ static void open_video(AVCodec *codec, AVStream *stream, WindowPixmap &window_pi
|
||||
CUcontext old_ctx;
|
||||
res = cuCtxPopCurrent(&old_ctx);
|
||||
res = cuCtxPushCurrent(*cuda_context);
|
||||
res = cuGraphicsGLRegisterImage(cuda_graphics_resource, window_pixmap.target_texture_id, GL_TEXTURE_2D, CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY);
|
||||
res = cuGraphicsGLRegisterImage(
|
||||
cuda_graphics_resource, window_pixmap.target_texture_id, GL_TEXTURE_2D,
|
||||
CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY);
|
||||
// cuGraphicsUnregisterResource(*cuda_graphics_resource);
|
||||
if (res != CUDA_SUCCESS) {
|
||||
fprintf(stderr, "Error: cuGraphicsGLRegisterImage failed, error %d, texture id: %u\n", res, window_pixmap.target_texture_id);
|
||||
fprintf(stderr,
|
||||
"Error: cuGraphicsGLRegisterImage failed, error %d, texture "
|
||||
"id: %u\n",
|
||||
res, window_pixmap.target_texture_id);
|
||||
exit(1);
|
||||
}
|
||||
res = cuCtxPopCurrent(&old_ctx);
|
||||
@ -375,14 +414,16 @@ int main(int argc, char **argv) {
|
||||
|
||||
bool has_name_pixmap = x11_supports_composite_named_window_pixmap(dpy);
|
||||
if (!has_name_pixmap) {
|
||||
fprintf(stderr, "Error: XCompositeNameWindowPixmap is not supported by your X11 server\n");
|
||||
fprintf(stderr, "Error: XCompositeNameWindowPixmap is not supported by "
|
||||
"your X11 server\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO: Verify if this is needed
|
||||
int screen_count = ScreenCount(dpy);
|
||||
for (int i = 0; i < screen_count; ++i) {
|
||||
XCompositeRedirectSubwindows(dpy, RootWindow(dpy, i), CompositeRedirectAutomatic);
|
||||
XCompositeRedirectSubwindows(dpy, RootWindow(dpy, i),
|
||||
CompositeRedirectAutomatic);
|
||||
}
|
||||
|
||||
XWindowAttributes attr;
|
||||
@ -391,7 +432,8 @@ int main(int argc, char **argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
//glXMakeContextCurrent(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx)
|
||||
// glXMakeContextCurrent(Display *dpy, GLXDrawable draw, GLXDrawable read,
|
||||
// GLXContext ctx)
|
||||
if (!glfwInit()) {
|
||||
fprintf(stderr, "Error: Failed to initialize glfw\n");
|
||||
return 1;
|
||||
@ -399,7 +441,8 @@ int main(int argc, char **argv) {
|
||||
|
||||
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(1280, 720, "Hello world", nullptr, nullptr);
|
||||
GLFWwindow *window =
|
||||
glfwCreateWindow(1280, 720, "Hello world", nullptr, nullptr);
|
||||
if (!window) {
|
||||
fprintf(stderr, "Error: Failed to create glfw window\n");
|
||||
glfwTerminate();
|
||||
@ -412,32 +455,38 @@ int main(int argc, char **argv) {
|
||||
glewExperimental = GL_TRUE;
|
||||
GLenum nGlewError = glewInit();
|
||||
if (nGlewError != GLEW_OK) {
|
||||
fprintf(stderr, "%s - Error initializing GLEW! %s\n", __FUNCTION__, glewGetErrorString(nGlewError));
|
||||
fprintf(stderr, "%s - Error initializing GLEW! %s\n", __FUNCTION__,
|
||||
glewGetErrorString(nGlewError));
|
||||
return 1;
|
||||
}
|
||||
glGetError(); // to clear the error caused deep in GLEW
|
||||
|
||||
WindowPixmap window_pixmap;
|
||||
if (!recreate_window_pixmap(dpy, src_window_id, window_pixmap)) {
|
||||
fprintf(stderr, "Error: Failed to create glx pixmap for window: %lu\n", src_window_id);
|
||||
fprintf(stderr, "Error: Failed to create glx pixmap for window: %lu\n",
|
||||
src_window_id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *filename = "test_video.mp4";
|
||||
|
||||
|
||||
// Video start
|
||||
AVFormatContext *av_format_context;
|
||||
// The output format is automatically guessed by the file extension
|
||||
avformat_alloc_output_context2(&av_format_context, nullptr, nullptr, filename);
|
||||
avformat_alloc_output_context2(&av_format_context, nullptr, nullptr,
|
||||
filename);
|
||||
if (!av_format_context) {
|
||||
fprintf(stderr, "Error: Failed to deduce output format from file extension .mp4\n");
|
||||
fprintf(
|
||||
stderr,
|
||||
"Error: Failed to deduce output format from file extension .mp4\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
AVOutputFormat *output_format = av_format_context->oformat;
|
||||
AVCodec *video_codec;
|
||||
AVStream *video_stream = add_stream(av_format_context, &video_codec, output_format->video_codec, window_pixmap);
|
||||
AVStream *video_stream =
|
||||
add_stream(av_format_context, &video_codec, output_format->video_codec,
|
||||
window_pixmap);
|
||||
if (!video_stream) {
|
||||
fprintf(stderr, "Error: Failed to create video stream\n");
|
||||
return 1;
|
||||
@ -450,25 +499,30 @@ int main(int argc, char **argv) {
|
||||
|
||||
AVBufferRef *device_ctx;
|
||||
CUgraphicsResource cuda_graphics_resource;
|
||||
open_video(video_codec, video_stream, window_pixmap, &device_ctx, &cuda_graphics_resource);
|
||||
open_video(video_codec, video_stream, window_pixmap, &device_ctx,
|
||||
&cuda_graphics_resource);
|
||||
av_dump_format(av_format_context, 0, filename, 1);
|
||||
|
||||
if (!(output_format->flags & AVFMT_NOFILE)) {
|
||||
int ret = avio_open(&av_format_context->pb, filename, AVIO_FLAG_WRITE);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error: Could not open '%s': %s\n", filename, "blabla");//av_err2str(ret));
|
||||
fprintf(stderr, "Error: Could not open '%s': %s\n", filename,
|
||||
"blabla"); // av_err2str(ret));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int ret = avformat_write_header(av_format_context, nullptr);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error occurred when opening output file: %s\n", "blabla");//av_err2str(ret));
|
||||
fprintf(stderr, "Error occurred when opening output file: %s\n",
|
||||
"blabla"); // av_err2str(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
AVHWDeviceContext *hw_device_context = (AVHWDeviceContext*)device_ctx->data;
|
||||
AVCUDADeviceContext *cuda_device_context = (AVCUDADeviceContext*)hw_device_context->hwctx;
|
||||
AVHWDeviceContext *hw_device_context =
|
||||
(AVHWDeviceContext *)device_ctx->data;
|
||||
AVCUDADeviceContext *cuda_device_context =
|
||||
(AVCUDADeviceContext *)hw_device_context->hwctx;
|
||||
CUcontext *cuda_context = &(cuda_device_context->cuda_ctx);
|
||||
if (!cuda_context) {
|
||||
fprintf(stderr, "Error: No cuda context\n");
|
||||
@ -497,12 +551,14 @@ int main(int argc, char **argv) {
|
||||
res = cuCtxPushCurrent(*cuda_context);
|
||||
|
||||
// Get texture
|
||||
res = cuGraphicsResourceSetMapFlags(cuda_graphics_resource, CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY);
|
||||
res = cuGraphicsResourceSetMapFlags(
|
||||
cuda_graphics_resource, CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY);
|
||||
res = cuGraphicsMapResources(1, &cuda_graphics_resource, 0);
|
||||
|
||||
// Map texture to cuda array
|
||||
CUarray mapped_array;
|
||||
res = cuGraphicsSubResourceGetMappedArray(&mapped_array, cuda_graphics_resource, 0, 0);
|
||||
res = cuGraphicsSubResourceGetMappedArray(&mapped_array,
|
||||
cuda_graphics_resource, 0, 0);
|
||||
|
||||
// Release texture
|
||||
// res = cuGraphicsUnmapResources(1, &cuda_graphics_resource, 0);
|
||||
@ -521,7 +577,8 @@ int main(int argc, char **argv) {
|
||||
frame->width = video_stream->codec->width;
|
||||
frame->height = video_stream->codec->height;
|
||||
|
||||
if(av_hwframe_get_buffer(video_stream->codec->hw_frames_ctx, frame, 0) < 0) {
|
||||
if (av_hwframe_get_buffer(video_stream->codec->hw_frames_ctx, frame, 0) <
|
||||
0) {
|
||||
fprintf(stderr, "Error: av_hwframe_get_buffer failed\n");
|
||||
exit(1);
|
||||
}
|
||||
@ -547,7 +604,8 @@ int main(int argc, char **argv) {
|
||||
XDamageSubtract(dpy, de->damage, None, region);
|
||||
XFixesDestroyRegion(dpy, region);
|
||||
|
||||
// TODO: Use a framebuffer instead. glCopyImageSubData requires opengl 4.2
|
||||
// TODO: Use a framebuffer instead. glCopyImageSubData requires
|
||||
// opengl 4.2
|
||||
glCopyImageSubData(
|
||||
window_pixmap.texture_id, GL_TEXTURE_2D, 0, 0, 0, 0,
|
||||
window_pixmap.target_texture_id, GL_TEXTURE_2D, 0, 0, 0, 0,
|
||||
@ -595,7 +653,8 @@ int main(int argc, char **argv) {
|
||||
if (avcodec_send_frame(video_stream->codec, frame) < 0) {
|
||||
fprintf(stderr, "Error: avcodec_send_frame failed\n");
|
||||
} else {
|
||||
receive_frames(video_stream->codec, video_stream, av_format_context);
|
||||
receive_frames(video_stream->codec, video_stream,
|
||||
av_format_context);
|
||||
}
|
||||
}
|
||||
|
||||
@ -615,7 +674,8 @@ int main(int argc, char **argv) {
|
||||
|
||||
// cleanup_window_pixmap(dpy, window_pixmap);
|
||||
for (int i = 0; i < screen_count; ++i) {
|
||||
XCompositeUnredirectSubwindows(dpy, RootWindow(dpy, i), CompositeRedirectAutomatic);
|
||||
XCompositeUnredirectSubwindows(dpy, RootWindow(dpy, i),
|
||||
CompositeRedirectAutomatic);
|
||||
}
|
||||
XCloseDisplay(dpy);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user