obs-studio/libobs-opengl/gl-stagesurf.c

231 lines
6.0 KiB
C
Raw Normal View History

2013-10-10 17:39:56 -07:00
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
2013-10-10 17:39:56 -07:00
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "gl-subsystem.h"
static bool create_pixel_pack_buffer(struct gs_stage_surface *surf)
{
GLsizeiptr size;
bool success = true;
if (!gl_gen_buffers(1, &surf->pack_buffer))
return false;
if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, surf->pack_buffer))
return false;
size = surf->width * surf->bytes_per_pixel;
size = (size+3) & 0xFFFFFFFC; /* align width to 4-byte boundry */
size *= surf->height;
2013-10-10 17:39:56 -07:00
glBufferData(GL_PIXEL_PACK_BUFFER, size, 0, GL_DYNAMIC_READ);
if (!gl_success("glBufferData"))
success = false;
if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0))
success = false;
return success;
}
stagesurf_t device_create_stagesurface(device_t device, uint32_t width,
uint32_t height, enum gs_color_format color_format)
{
struct gs_stage_surface *surf;
surf = bzalloc(sizeof(struct gs_stage_surface));
surf->device = device;
2013-10-10 17:39:56 -07:00
surf->format = color_format;
surf->width = width;
surf->height = height;
surf->gl_format = convert_gs_format(color_format);
surf->gl_internal_format = convert_gs_internal_format(color_format);
2013-10-10 18:50:09 -07:00
surf->gl_type = get_gl_format_type(color_format);
2013-10-10 17:39:56 -07:00
surf->bytes_per_pixel = gs_get_format_bpp(color_format)/8;
if (!create_pixel_pack_buffer(surf)) {
2013-10-10 17:39:56 -07:00
blog(LOG_ERROR, "device_create_stagesurface (GL) failed");
stagesurface_destroy(surf);
return NULL;
}
return surf;
}
void stagesurface_destroy(stagesurf_t stagesurf)
{
if (stagesurf) {
2013-10-11 11:41:36 -07:00
if (stagesurf->pack_buffer)
gl_delete_buffers(1, &stagesurf->pack_buffer);
2013-10-10 17:39:56 -07:00
bfree(stagesurf);
}
}
2013-10-10 18:50:09 -07:00
static bool can_stage(struct gs_stage_surface *dst, struct gs_texture_2d *src)
2013-10-10 17:39:56 -07:00
{
if (!src) {
blog(LOG_ERROR, "Source texture is NULL");
return false;
}
2013-10-10 18:50:09 -07:00
if (src->base.type != GS_TEXTURE_2D) {
2013-10-10 17:39:56 -07:00
blog(LOG_ERROR, "Source texture must be a 2D texture");
2013-10-10 18:50:09 -07:00
return false;
2013-10-10 17:39:56 -07:00
}
if (!dst) {
blog(LOG_ERROR, "Destination surface is NULL");
2013-10-10 18:50:09 -07:00
return false;
2013-10-10 17:39:56 -07:00
}
2013-10-10 18:50:09 -07:00
if (src->base.format != dst->format) {
blog(LOG_ERROR, "Source and destination formats do not match");
return false;
}
if (src->width != dst->width || src->height != dst->height) {
blog(LOG_ERROR, "Source and destination must have the same "
"dimensions");
2013-10-10 18:50:09 -07:00
return false;
}
return true;
}
#ifdef __APPLE__
/* Apparently for mac, PBOs won't do an asynchronous transfer unless you use
* FBOs aong with glReadPixels, which is really dumb. */
2013-10-10 18:50:09 -07:00
void device_stage_texture(device_t device, stagesurf_t dst, texture_t src)
{
struct gs_texture_2d *tex2d = (struct gs_texture_2d*)src;
struct fbo_info *fbo;
GLint last_fbo;
bool success = false;
if (!can_stage(dst, tex2d))
goto failed;
if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
2013-10-10 17:39:56 -07:00
goto failed;
fbo = get_fbo(device, dst->width, dst->height, dst->format);
if (!gl_get_integer_v(GL_READ_FRAMEBUFFER_BINDING, &last_fbo))
goto failed_unbind_buffer;
if (!gl_bind_framebuffer(GL_READ_FRAMEBUFFER, fbo->fbo))
goto failed_unbind_buffer;
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0,
src->gl_target, src->texture, 0);
if (!gl_success("glFrameBufferTexture2D"))
goto failed_unbind_all;
glReadPixels(0, 0, dst->width, dst->height, dst->gl_format,
dst->gl_type, 0);
if (!gl_success("glReadPixels"))
goto failed_unbind_all;
success = true;
failed_unbind_all:
gl_bind_framebuffer(GL_READ_FRAMEBUFFER, last_fbo);
failed_unbind_buffer:
gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
failed:
if (!success)
blog(LOG_ERROR, "device_stage_texture (GL) failed");
}
#else
void device_stage_texture(device_t device, stagesurf_t dst, texture_t src)
{
struct gs_texture_2d *tex2d = (struct gs_texture_2d*)src;
if (!can_stage(dst, tex2d))
2013-10-10 18:50:09 -07:00
goto failed;
2013-10-10 18:50:09 -07:00
if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, dst->pack_buffer))
goto failed;
if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
goto failed;
2013-10-10 18:50:09 -07:00
glGetTexImage(GL_TEXTURE_2D, 0, dst->gl_format, dst->gl_type, 0);
if (!gl_success("glGetTexImage"))
goto failed;
gl_bind_texture(GL_TEXTURE_2D, 0);
gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
2013-10-10 17:39:56 -07:00
return;
failed:
2013-10-10 18:50:09 -07:00
gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
gl_bind_texture(GL_TEXTURE_2D, 0);
2013-10-10 17:39:56 -07:00
blog(LOG_ERROR, "device_stage_texture (GL) failed");
UNUSED_PARAMETER(device);
2013-10-10 17:39:56 -07:00
}
#endif
2013-10-10 17:39:56 -07:00
uint32_t stagesurface_getwidth(stagesurf_t stagesurf)
{
return stagesurf->width;
}
uint32_t stagesurface_getheight(stagesurf_t stagesurf)
{
return stagesurf->height;
}
enum gs_color_format stagesurface_getcolorformat(stagesurf_t stagesurf)
{
return stagesurf->format;
}
Implement encoder interface (still preliminary) - Implement OBS encoder interface. It was previously incomplete, but now is reaching some level of completion, though probably should still be considered preliminary. I had originally implemented it so that encoders only have a 'reset' function to reset their parameters, but I felt that having both a 'start' and 'stop' function would be useful. Encoders are now assigned to a specific video/audio media output each rather than implicitely assigned to the main obs video/audio contexts. This allows separate encoder contexts that aren't necessarily assigned to the main video/audio context (which is useful for things such as recording specific sources). Will probably have to do this for regular obs outputs as well. When creating an encoder, you must now explicitely state whether that encoder is an audio or video encoder. Audio and video can optionally be automatically converted depending on what the encoder specifies. When something 'attaches' to an encoder, the first attachment starts the encoder, and the encoder automatically attaches to the media output context associated with it. Subsequent attachments won't have the same effect, they will just start receiving the same encoder data when the next keyframe plays (along with SEI if any). When detaching from the encoder, the last detachment will fully stop the encoder and detach the encoder from the media output context associated with the encoder. SEI must actually be exported separately; because new encoder attachments may not always be at the beginning of the stream, the first keyframe they get must have that SEI data in it. If the encoder has SEI data, it needs only add one small function to simply query that SEI data, and then that data will be handled automatically by libobs for all subsequent encoder attachments. - Implement x264 encoder plugin, move x264 files to separate plugin to separate necessary dependencies. - Change video/audio frame output structures to not use const qualifiers to prevent issues with non-const function usage elsewhere. This was an issue when writing the x264 encoder, as the x264 encoder expects non-const frame data. Change stagesurf_map to return a non-const data type to prevent this as well. - Change full range parameter of video scaler to be an enum rather than boolean
2014-03-16 16:21:34 -07:00
bool stagesurface_map(stagesurf_t stagesurf, uint8_t **data, uint32_t *linesize)
2013-10-10 17:39:56 -07:00
{
if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
goto fail;
*data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
if (!gl_success("glMapBuffer"))
goto fail;
gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
*linesize = stagesurf->bytes_per_pixel * stagesurf->width;
2013-10-10 17:39:56 -07:00
return true;
fail:
blog(LOG_ERROR, "stagesurf_map (GL) failed");
return false;
}
void stagesurface_unmap(stagesurf_t stagesurf)
{
if (!gl_bind_buffer(GL_PIXEL_PACK_BUFFER, stagesurf->pack_buffer))
return;
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
gl_success("glUnmapBuffer");
2013-10-10 18:50:09 -07:00
2013-10-10 17:39:56 -07:00
gl_bind_buffer(GL_PIXEL_PACK_BUFFER, 0);
}