glTextureStorage2D(cr_tex[i], 1, GL_R8, width / 2, height);
check_error();
- InterpolatedFrameResources resource;
- resource.input_tex = input_tex[i];
- resource.gray_tex = gray_tex[i];
- resource.fade_y_output_tex = fade_y_output_tex[i];
- resource.fade_cbcr_output_tex = fade_cbcr_output_tex[i];
- resource.cb_tex = cb_tex[i];
- resource.cr_tex = cr_tex[i];
- glCreateFramebuffers(2, resource.input_fbos);
+ unique_ptr<InterpolatedFrameResources> resource(new InterpolatedFrameResources);
+ resource->owner = this;
+ resource->input_tex = input_tex[i];
+ resource->gray_tex = gray_tex[i];
+ resource->fade_y_output_tex = fade_y_output_tex[i];
+ resource->fade_cbcr_output_tex = fade_cbcr_output_tex[i];
+ resource->cb_tex = cb_tex[i];
+ resource->cr_tex = cr_tex[i];
+ glCreateFramebuffers(2, resource->input_fbos);
check_error();
- glCreateFramebuffers(1, &resource.fade_fbo);
+ glCreateFramebuffers(1, &resource->fade_fbo);
check_error();
- glNamedFramebufferTextureLayer(resource.input_fbos[0], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 0);
+ glNamedFramebufferTextureLayer(resource->input_fbos[0], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 0);
check_error();
- glNamedFramebufferTextureLayer(resource.input_fbos[0], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 0);
+ glNamedFramebufferTextureLayer(resource->input_fbos[0], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 0);
check_error();
- glNamedFramebufferTextureLayer(resource.input_fbos[1], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 1);
+ glNamedFramebufferTextureLayer(resource->input_fbos[1], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 1);
check_error();
- glNamedFramebufferTextureLayer(resource.input_fbos[1], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 1);
+ glNamedFramebufferTextureLayer(resource->input_fbos[1], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 1);
check_error();
- glNamedFramebufferTexture(resource.fade_fbo, GL_COLOR_ATTACHMENT0, fade_y_output_tex[i], 0);
+ glNamedFramebufferTexture(resource->fade_fbo, GL_COLOR_ATTACHMENT0, fade_y_output_tex[i], 0);
check_error();
- glNamedFramebufferTexture(resource.fade_fbo, GL_COLOR_ATTACHMENT1, fade_cbcr_output_tex[i], 0);
+ glNamedFramebufferTexture(resource->fade_fbo, GL_COLOR_ATTACHMENT1, fade_cbcr_output_tex[i], 0);
check_error();
GLuint bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
- glNamedFramebufferDrawBuffers(resource.input_fbos[0], 2, bufs);
+ glNamedFramebufferDrawBuffers(resource->input_fbos[0], 2, bufs);
check_error();
- glNamedFramebufferDrawBuffers(resource.input_fbos[1], 2, bufs);
+ glNamedFramebufferDrawBuffers(resource->input_fbos[1], 2, bufs);
check_error();
- glNamedFramebufferDrawBuffers(resource.fade_fbo, 2, bufs);
+ glNamedFramebufferDrawBuffers(resource->fade_fbo, 2, bufs);
check_error();
- glCreateBuffers(1, &resource.pbo);
+ glCreateBuffers(1, &resource->pbo);
check_error();
- glNamedBufferStorage(resource.pbo, width * height * 4, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
+ glNamedBufferStorage(resource->pbo, width * height * 4, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
check_error();
- resource.pbo_contents = glMapNamedBufferRange(resource.pbo, 0, width * height * 4, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
- interpolate_resources.push_back(resource);
+ resource->pbo_contents = glMapNamedBufferRange(resource->pbo, 0, width * height * 4, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
+ interpolate_resources.push_back(move(resource));
}
check_error();
void VideoStream::clear_queue()
{
- unique_lock<mutex> lock(queue_lock);
- frame_queue.clear();
+ deque<QueuedFrame> q;
+
+ {
+ unique_lock<mutex> lock(queue_lock);
+ q = move(frame_queue);
+ }
+
+ // These are not RAII-ed, unfortunately, so we'll need to clean them ourselves.
+ // Note that release_texture() is thread-safe.
+ for (const QueuedFrame &qf : q) {
+ if (qf.type == QueuedFrame::INTERPOLATED ||
+ qf.type == QueuedFrame::FADED_INTERPOLATED) {
+ compute_flow->release_texture(qf.flow_tex);
+ }
+ if (qf.type == QueuedFrame::INTERPOLATED) {
+ interpolate->release_texture(qf.output_tex);
+ interpolate->release_texture(qf.cbcr_tex);
+ }
+ }
+
+ // Destroy q outside the mutex, as that would be a double-lock.
}
-void VideoStream::schedule_original_frame(steady_clock::time_point local_pts, int64_t output_pts, function<void()> &&display_func, unsigned stream_idx, int64_t input_pts)
+void VideoStream::schedule_original_frame(steady_clock::time_point local_pts,
+ int64_t output_pts, function<void()> &&display_func,
+ QueueSpotHolder &&queue_spot_holder,
+ unsigned stream_idx, int64_t input_pts)
{
fprintf(stderr, "output_pts=%ld original input_pts=%ld\n", output_pts, input_pts);
qf.stream_idx = stream_idx;
qf.input_first_pts = input_pts;
qf.display_func = move(display_func);
+ qf.queue_spot_holder = move(queue_spot_holder);
unique_lock<mutex> lock(queue_lock);
- frame_queue.push_back(qf);
+ frame_queue.push_back(move(qf));
queue_changed.notify_all();
}
-bool VideoStream::schedule_faded_frame(steady_clock::time_point local_pts, int64_t output_pts, function<void()> &&display_func, unsigned stream_idx, int64_t input_pts, int secondary_stream_idx, int64_t secondary_input_pts, float fade_alpha)
+void VideoStream::schedule_faded_frame(steady_clock::time_point local_pts, int64_t output_pts,
+ function<void()> &&display_func,
+ QueueSpotHolder &&queue_spot_holder,
+ unsigned stream_idx, int64_t input_pts, int secondary_stream_idx,
+ int64_t secondary_input_pts, float fade_alpha)
{
fprintf(stderr, "output_pts=%ld faded input_pts=%ld,%ld fade_alpha=%.2f\n", output_pts, input_pts, secondary_input_pts, fade_alpha);
// (We share these with interpolated frames, which is slightly
// overkill, but there's no need to waste resources on keeping
// separate pools around.)
- InterpolatedFrameResources resources;
+ BorrowedInterpolatedFrameResources resources;
{
unique_lock<mutex> lock(queue_lock);
if (interpolate_resources.empty()) {
fprintf(stderr, "WARNING: Too many interpolated frames already in transit; dropping one.\n");
- return false;
+ return;
}
- resources = interpolate_resources.front();
+ resources = BorrowedInterpolatedFrameResources(interpolate_resources.front().release());
interpolate_resources.pop_front();
}
JPEGID jpeg_id1;
jpeg_id1.stream_idx = stream_idx;
jpeg_id1.pts = input_pts;
- jpeg_id1.interpolated = false;
shared_ptr<Frame> frame1 = decode_jpeg_with_cache(jpeg_id1, DECODE_IF_NOT_IN_CACHE, &did_decode);
JPEGID jpeg_id2;
jpeg_id2.stream_idx = secondary_stream_idx;
jpeg_id2.pts = secondary_input_pts;
- jpeg_id2.interpolated = false;
shared_ptr<Frame> frame2 = decode_jpeg_with_cache(jpeg_id2, DECODE_IF_NOT_IN_CACHE, &did_decode);
- ycbcr_semiplanar_converter->prepare_chain_for_fade(frame1, frame2, fade_alpha)->render_to_fbo(resources.fade_fbo, 1280, 720);
+ ycbcr_semiplanar_converter->prepare_chain_for_fade(frame1, frame2, fade_alpha)->render_to_fbo(resources->fade_fbo, 1280, 720);
QueuedFrame qf;
qf.local_pts = local_pts;
qf.type = QueuedFrame::FADED;
qf.output_pts = output_pts;
qf.stream_idx = stream_idx;
- qf.resources = resources;
qf.input_first_pts = input_pts;
qf.display_func = move(display_func);
+ qf.queue_spot_holder = move(queue_spot_holder);
qf.secondary_stream_idx = secondary_stream_idx;
qf.secondary_input_pts = secondary_input_pts;
// Subsample and split Cb/Cr.
- chroma_subsampler->subsample_chroma(resources.fade_cbcr_output_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
+ chroma_subsampler->subsample_chroma(resources->fade_cbcr_output_tex, 1280, 720, resources->cb_tex, resources->cr_tex);
// Read it down (asynchronously) to the CPU.
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
- glBindBuffer(GL_PIXEL_PACK_BUFFER, resources.pbo);
+ glBindBuffer(GL_PIXEL_PACK_BUFFER, resources->pbo);
check_error();
- glGetTextureImage(resources.fade_y_output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
+ glGetTextureImage(resources->fade_y_output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
check_error();
- glGetTextureImage(resources.cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720));
+ glGetTextureImage(resources->cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720));
check_error();
- glGetTextureImage(resources.cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720));
+ glGetTextureImage(resources->cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720));
check_error();
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
check_error();
qf.fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
check_error();
+ qf.resources = move(resources);
+ qf.local_pts = local_pts;
unique_lock<mutex> lock(queue_lock);
- frame_queue.push_back(qf);
+ frame_queue.push_back(move(qf));
queue_changed.notify_all();
- return true;
}
-bool VideoStream::schedule_interpolated_frame(steady_clock::time_point local_pts, int64_t output_pts, function<void()> &&display_func, unsigned stream_idx, int64_t input_first_pts, int64_t input_second_pts, float alpha, int secondary_stream_idx, int64_t secondary_input_pts, float fade_alpha)
+void VideoStream::schedule_interpolated_frame(steady_clock::time_point local_pts,
+ int64_t output_pts, function<void(shared_ptr<Frame>)> &&display_func,
+ QueueSpotHolder &&queue_spot_holder,
+ unsigned stream_idx, int64_t input_first_pts,
+ int64_t input_second_pts, float alpha,
+ int secondary_stream_idx, int64_t secondary_input_pts,
+ float fade_alpha)
{
if (secondary_stream_idx != -1) {
fprintf(stderr, "output_pts=%ld interpolated input_pts1=%ld input_pts2=%ld alpha=%.3f secondary_pts=%ld fade_alpha=%.2f\n", output_pts, input_first_pts, input_second_pts, alpha, secondary_input_pts, fade_alpha);
fprintf(stderr, "output_pts=%ld interpolated input_pts1=%ld input_pts2=%ld alpha=%.3f\n", output_pts, input_first_pts, input_second_pts, alpha);
}
- JPEGID id;
- if (secondary_stream_idx == -1) {
- id = JPEGID{ stream_idx, output_pts, /*interpolated=*/true };
- } else {
- id = create_jpegid_for_interpolated_fade(stream_idx, output_pts, secondary_stream_idx, secondary_input_pts);
- }
-
// Get the temporary OpenGL resources we need for doing the interpolation.
- InterpolatedFrameResources resources;
+ BorrowedInterpolatedFrameResources resources;
{
unique_lock<mutex> lock(queue_lock);
if (interpolate_resources.empty()) {
fprintf(stderr, "WARNING: Too many interpolated frames already in transit; dropping one.\n");
- return false;
+ return;
}
- resources = interpolate_resources.front();
+ resources = BorrowedInterpolatedFrameResources(interpolate_resources.front().release());
interpolate_resources.pop_front();
}
qf.type = (secondary_stream_idx == -1) ? QueuedFrame::INTERPOLATED : QueuedFrame::FADED_INTERPOLATED;
qf.output_pts = output_pts;
qf.stream_idx = stream_idx;
- qf.resources = resources;
- qf.id = id;
- qf.display_func = move(display_func);
+ qf.display_decoded_func = move(display_func);
+ qf.queue_spot_holder = move(queue_spot_holder);
+ qf.local_pts = local_pts;
check_error();
JPEGID jpeg_id;
jpeg_id.stream_idx = stream_idx;
jpeg_id.pts = frame_no == 1 ? input_second_pts : input_first_pts;
- jpeg_id.interpolated = false;
bool did_decode;
shared_ptr<Frame> frame = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode);
- ycbcr_converter->prepare_chain_for_conversion(frame)->render_to_fbo(resources.input_fbos[frame_no], 1280, 720);
+ ycbcr_converter->prepare_chain_for_conversion(frame)->render_to_fbo(resources->input_fbos[frame_no], 1280, 720);
}
- glGenerateTextureMipmap(resources.input_tex);
+ glGenerateTextureMipmap(resources->input_tex);
check_error();
- glGenerateTextureMipmap(resources.gray_tex);
+ glGenerateTextureMipmap(resources->gray_tex);
check_error();
// Compute the interpolated frame.
- qf.flow_tex = compute_flow->exec(resources.gray_tex, DISComputeFlow::FORWARD_AND_BACKWARD, DISComputeFlow::DO_NOT_RESIZE_FLOW);
+ qf.flow_tex = compute_flow->exec(resources->gray_tex, DISComputeFlow::FORWARD_AND_BACKWARD, DISComputeFlow::DO_NOT_RESIZE_FLOW);
check_error();
if (secondary_stream_idx != -1) {
// Fade. First kick off the interpolation.
- tie(qf.output_tex, ignore) = interpolate_no_split->exec(resources.input_tex, resources.gray_tex, qf.flow_tex, 1280, 720, alpha);
+ tie(qf.output_tex, ignore) = interpolate_no_split->exec(resources->input_tex, resources->gray_tex, qf.flow_tex, 1280, 720, alpha);
check_error();
// Now decode the image we are fading against.
JPEGID jpeg_id;
jpeg_id.stream_idx = secondary_stream_idx;
jpeg_id.pts = secondary_input_pts;
- jpeg_id.interpolated = false;
bool did_decode;
shared_ptr<Frame> frame2 = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode);
// Then fade against it, putting it into the fade Y' and CbCr textures.
- ycbcr_semiplanar_converter->prepare_chain_for_fade_from_texture(qf.output_tex, frame2, fade_alpha)->render_to_fbo(resources.fade_fbo, 1280, 720);
+ ycbcr_semiplanar_converter->prepare_chain_for_fade_from_texture(qf.output_tex, frame2, fade_alpha)->render_to_fbo(resources->fade_fbo, 1280, 720);
// Subsample and split Cb/Cr.
- chroma_subsampler->subsample_chroma(resources.fade_cbcr_output_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
+ chroma_subsampler->subsample_chroma(resources->fade_cbcr_output_tex, 1280, 720, resources->cb_tex, resources->cr_tex);
+
+ interpolate_no_split->release_texture(qf.output_tex);
} else {
- tie(qf.output_tex, qf.cbcr_tex) = interpolate->exec(resources.input_tex, resources.gray_tex, qf.flow_tex, 1280, 720, alpha);
+ tie(qf.output_tex, qf.cbcr_tex) = interpolate->exec(resources->input_tex, resources->gray_tex, qf.flow_tex, 1280, 720, alpha);
check_error();
// Subsample and split Cb/Cr.
- chroma_subsampler->subsample_chroma(qf.cbcr_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
+ chroma_subsampler->subsample_chroma(qf.cbcr_tex, 1280, 720, resources->cb_tex, resources->cr_tex);
}
// We could have released qf.flow_tex here, but to make sure we don't cause a stall
// Read it down (asynchronously) to the CPU.
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
- glBindBuffer(GL_PIXEL_PACK_BUFFER, resources.pbo);
+ glBindBuffer(GL_PIXEL_PACK_BUFFER, resources->pbo);
check_error();
if (secondary_stream_idx != -1) {
- glGetTextureImage(resources.fade_y_output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
+ glGetTextureImage(resources->fade_y_output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
} else {
glGetTextureImage(qf.output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
}
check_error();
- glGetTextureImage(resources.cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720));
+ glGetTextureImage(resources->cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720));
check_error();
- glGetTextureImage(resources.cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720));
+ glGetTextureImage(resources->cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720));
check_error();
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
check_error();
qf.fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
check_error();
+ qf.resources = move(resources);
unique_lock<mutex> lock(queue_lock);
- frame_queue.push_back(qf);
+ frame_queue.push_back(move(qf));
queue_changed.notify_all();
- return true;
}
-void VideoStream::schedule_refresh_frame(steady_clock::time_point local_pts, int64_t output_pts, function<void()> &&display_func)
+void VideoStream::schedule_refresh_frame(steady_clock::time_point local_pts,
+ int64_t output_pts, function<void()> &&display_func,
+ QueueSpotHolder &&queue_spot_holder)
{
QueuedFrame qf;
qf.type = QueuedFrame::REFRESH;
qf.output_pts = output_pts;
qf.display_func = move(display_func);
+ qf.queue_spot_holder = move(queue_spot_holder);
unique_lock<mutex> lock(queue_lock);
- frame_queue.push_back(qf);
+ frame_queue.push_back(move(qf));
queue_changed.notify_all();
}
// clear_queue() happened, so don't play this frame after all.
continue;
}
- qf = frame_queue.front();
+ qf = move(frame_queue.front());
frame_queue.pop_front();
}
} else if (qf.type == QueuedFrame::FADED) {
glClientWaitSync(qf.fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
- shared_ptr<Frame> frame = frame_from_pbo(qf.resources.pbo_contents, 1280, 720);
+ shared_ptr<Frame> frame = frame_from_pbo(qf.resources->pbo_contents, 1280, 720);
// Now JPEG encode it, and send it on to the stream.
vector<uint8_t> jpeg = encode_jpeg(frame->y.get(), frame->cb.get(), frame->cr.get(), 1280, 720);
pkt.size = jpeg.size();
stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
last_frame = move(jpeg);
-
- // Put the frame resources back.
- unique_lock<mutex> lock(queue_lock);
- interpolate_resources.push_back(qf.resources);
} else if (qf.type == QueuedFrame::INTERPOLATED || qf.type == QueuedFrame::FADED_INTERPOLATED) {
glClientWaitSync(qf.fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
- // Send a copy of the frame on to display.
- shared_ptr<Frame> frame = frame_from_pbo(qf.resources.pbo_contents, 1280, 720);
- JPEGFrameView::insert_interpolated_frame(qf.id, frame);
+ // Send it on to display.
+ shared_ptr<Frame> frame = frame_from_pbo(qf.resources->pbo_contents, 1280, 720);
+ if (qf.display_decoded_func != nullptr) {
+ qf.display_decoded_func(frame);
+ }
// Now JPEG encode it, and send it on to the stream.
vector<uint8_t> jpeg = encode_jpeg(frame->y.get(), frame->cb.get(), frame->cr.get(), 1280, 720);
pkt.size = jpeg.size();
stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
last_frame = move(jpeg);
-
- // Put the frame resources back.
- unique_lock<mutex> lock(queue_lock);
- interpolate_resources.push_back(qf.resources);
} else if (qf.type == QueuedFrame::REFRESH) {
AVPacket pkt;
av_init_packet(&pkt);