1 #include "video_stream.h"
4 #include <libavformat/avformat.h>
5 #include <libavformat/avio.h>
8 #include "chroma_subsampler.h"
13 #include "jpeg_frame_view.h"
14 #include "movit/util.h"
18 #include "ycbcr_converter.h"
20 #include <epoxy/glx.h>
25 using namespace std::chrono;
27 extern HTTPD *global_httpd;
31 string read_file(const string &filename)
33 FILE *fp = fopen(filename.c_str(), "rb");
35 perror(filename.c_str());
39 fseek(fp, 0, SEEK_END);
45 fread(&ret[0], len, 1, fp);
52 struct VectorDestinationManager {
53 jpeg_destination_mgr pub;
54 std::vector<uint8_t> dest;
56 VectorDestinationManager()
58 pub.init_destination = init_destination_thunk;
59 pub.empty_output_buffer = empty_output_buffer_thunk;
60 pub.term_destination = term_destination_thunk;
63 static void init_destination_thunk(j_compress_ptr ptr)
65 ((VectorDestinationManager *)(ptr->dest))->init_destination();
68 inline void init_destination()
73 static boolean empty_output_buffer_thunk(j_compress_ptr ptr)
75 return ((VectorDestinationManager *)(ptr->dest))->empty_output_buffer();
78 inline bool empty_output_buffer()
80 make_room(dest.size()); // Should ignore pub.free_in_buffer!
84 inline void make_room(size_t bytes_used)
86 dest.resize(bytes_used + 4096);
87 dest.resize(dest.capacity());
88 pub.next_output_byte = dest.data() + bytes_used;
89 pub.free_in_buffer = dest.size() - bytes_used;
92 static void term_destination_thunk(j_compress_ptr ptr)
94 ((VectorDestinationManager *)(ptr->dest))->term_destination();
97 inline void term_destination()
99 dest.resize(dest.size() - pub.free_in_buffer);
102 static_assert(std::is_standard_layout<VectorDestinationManager>::value, "");
104 vector<uint8_t> encode_jpeg(const uint8_t *y_data, const uint8_t *cb_data, const uint8_t *cr_data, unsigned width, unsigned height)
106 VectorDestinationManager dest;
108 jpeg_compress_struct cinfo;
110 cinfo.err = jpeg_std_error(&jerr);
111 jpeg_create_compress(&cinfo);
113 cinfo.dest = (jpeg_destination_mgr *)&dest;
114 cinfo.input_components = 3;
115 cinfo.in_color_space = JCS_RGB;
116 jpeg_set_defaults(&cinfo);
117 constexpr int quality = 90;
118 jpeg_set_quality(&cinfo, quality, /*force_baseline=*/false);
120 cinfo.image_width = width;
121 cinfo.image_height = height;
122 cinfo.raw_data_in = true;
123 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
124 cinfo.comp_info[0].h_samp_factor = 2;
125 cinfo.comp_info[0].v_samp_factor = 1;
126 cinfo.comp_info[1].h_samp_factor = 1;
127 cinfo.comp_info[1].v_samp_factor = 1;
128 cinfo.comp_info[2].h_samp_factor = 1;
129 cinfo.comp_info[2].v_samp_factor = 1;
130 cinfo.CCIR601_sampling = true; // Seems to be mostly ignored by libjpeg, though.
131 jpeg_start_compress(&cinfo, true);
133 JSAMPROW yptr[8], cbptr[8], crptr[8];
134 JSAMPARRAY data[3] = { yptr, cbptr, crptr };
135 for (unsigned y = 0; y < height; y += 8) {
136 for (unsigned yy = 0; yy < 8; ++yy) {
137 yptr[yy] = const_cast<JSAMPROW>(&y_data[(y + yy) * width]);
138 cbptr[yy] = const_cast<JSAMPROW>(&cb_data[(y + yy) * width / 2]);
139 crptr[yy] = const_cast<JSAMPROW>(&cr_data[(y + yy) * width / 2]);
142 jpeg_write_raw_data(&cinfo, data, /*num_lines=*/8);
145 jpeg_finish_compress(&cinfo);
146 jpeg_destroy_compress(&cinfo);
148 return move(dest.dest);
151 VideoStream::VideoStream()
153 ycbcr_converter.reset(new YCbCrConverter(YCbCrConverter::OUTPUT_TO_DUAL_YCBCR, /*resource_pool=*/nullptr));
154 ycbcr_semiplanar_converter.reset(new YCbCrConverter(YCbCrConverter::OUTPUT_TO_SEMIPLANAR, /*resource_pool=*/nullptr));
156 GLuint input_tex[num_interpolate_slots], gray_tex[num_interpolate_slots];
157 GLuint fade_y_output_tex[num_interpolate_slots], fade_cbcr_output_tex[num_interpolate_slots];
158 GLuint cb_tex[num_interpolate_slots], cr_tex[num_interpolate_slots];
160 glCreateTextures(GL_TEXTURE_2D_ARRAY, num_interpolate_slots, input_tex);
161 glCreateTextures(GL_TEXTURE_2D_ARRAY, num_interpolate_slots, gray_tex);
162 glCreateTextures(GL_TEXTURE_2D, num_interpolate_slots, fade_y_output_tex);
163 glCreateTextures(GL_TEXTURE_2D, num_interpolate_slots, fade_cbcr_output_tex);
164 glCreateTextures(GL_TEXTURE_2D, num_interpolate_slots, cb_tex);
165 glCreateTextures(GL_TEXTURE_2D, num_interpolate_slots, cr_tex);
168 constexpr size_t width = 1280, height = 720; // FIXME: adjustable width, height
169 int levels = find_num_levels(width, height);
170 for (size_t i = 0; i < num_interpolate_slots; ++i) {
171 glTextureStorage3D(input_tex[i], levels, GL_RGBA8, width, height, 2);
173 glTextureStorage3D(gray_tex[i], levels, GL_R8, width, height, 2);
175 glTextureStorage2D(fade_y_output_tex[i], 1, GL_R8, width, height);
177 glTextureStorage2D(fade_cbcr_output_tex[i], 1, GL_RG8, width, height);
179 glTextureStorage2D(cb_tex[i], 1, GL_R8, width / 2, height);
181 glTextureStorage2D(cr_tex[i], 1, GL_R8, width / 2, height);
184 InterpolatedFrameResources resource;
185 resource.input_tex = input_tex[i];
186 resource.gray_tex = gray_tex[i];
187 resource.fade_y_output_tex = fade_y_output_tex[i];
188 resource.fade_cbcr_output_tex = fade_cbcr_output_tex[i];
189 resource.cb_tex = cb_tex[i];
190 resource.cr_tex = cr_tex[i];
191 glCreateFramebuffers(2, resource.input_fbos);
193 glCreateFramebuffers(1, &resource.fade_fbo);
196 glNamedFramebufferTextureLayer(resource.input_fbos[0], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 0);
198 glNamedFramebufferTextureLayer(resource.input_fbos[0], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 0);
200 glNamedFramebufferTextureLayer(resource.input_fbos[1], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 1);
202 glNamedFramebufferTextureLayer(resource.input_fbos[1], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 1);
204 glNamedFramebufferTexture(resource.fade_fbo, GL_COLOR_ATTACHMENT0, fade_y_output_tex[i], 0);
206 glNamedFramebufferTexture(resource.fade_fbo, GL_COLOR_ATTACHMENT1, fade_cbcr_output_tex[i], 0);
209 GLuint bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
210 glNamedFramebufferDrawBuffers(resource.input_fbos[0], 2, bufs);
212 glNamedFramebufferDrawBuffers(resource.input_fbos[1], 2, bufs);
214 glNamedFramebufferDrawBuffers(resource.fade_fbo, 2, bufs);
217 glCreateBuffers(1, &resource.pbo);
219 glNamedBufferStorage(resource.pbo, width * height * 4, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
221 resource.pbo_contents = glMapNamedBufferRange(resource.pbo, 0, width * height * 4, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
222 interpolate_resources.push_back(resource);
228 if (global_flags.interpolation_quality == 1) {
229 op = operating_point1;
230 } else if (global_flags.interpolation_quality == 2) {
231 op = operating_point2;
232 } else if (global_flags.interpolation_quality == 3) {
233 op = operating_point3;
234 } else if (global_flags.interpolation_quality == 4) {
235 op = operating_point4;
240 compute_flow.reset(new DISComputeFlow(width, height, op));
241 interpolate.reset(new Interpolate(op, /*split_ycbcr_output=*/true));
242 interpolate_no_split.reset(new Interpolate(op, /*split_ycbcr_output=*/false));
243 chroma_subsampler.reset(new ChromaSubsampler);
246 // The “last frame” is initially black.
247 unique_ptr<uint8_t[]> y(new uint8_t[1280 * 720]);
248 unique_ptr<uint8_t[]> cb_or_cr(new uint8_t[640 * 720]);
249 memset(y.get(), 16, 1280 * 720);
250 memset(cb_or_cr.get(), 128, 640 * 720);
251 last_frame = encode_jpeg(y.get(), cb_or_cr.get(), cb_or_cr.get(), 1280, 720);
254 VideoStream::~VideoStream() {}
256 void VideoStream::start()
258 AVFormatContext *avctx = avformat_alloc_context();
259 avctx->oformat = av_guess_format("nut", nullptr, nullptr);
261 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
262 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, nullptr, nullptr);
263 avctx->pb->write_data_type = &VideoStream::write_packet2_thunk;
264 avctx->pb->ignore_boundary_point = 1;
266 Mux::Codec video_codec = Mux::CODEC_MJPEG;
268 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
270 string video_extradata;
272 constexpr int width = 1280, height = 720; // Doesn't matter for MJPEG.
273 stream_mux.reset(new Mux(avctx, width, height, video_codec, video_extradata, /*audio_codec_parameters=*/nullptr, COARSE_TIMEBASE,
274 /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, {}));
277 encode_thread = thread(&VideoStream::encode_thread_func, this);
280 void VideoStream::stop()
282 encode_thread.join();
285 void VideoStream::clear_queue()
287 unique_lock<mutex> lock(queue_lock);
291 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)
293 fprintf(stderr, "output_pts=%ld original input_pts=%ld\n", output_pts, input_pts);
296 qf.local_pts = local_pts;
297 qf.type = QueuedFrame::ORIGINAL;
298 qf.output_pts = output_pts;
299 qf.stream_idx = stream_idx;
300 qf.input_first_pts = input_pts;
301 qf.display_func = move(display_func);
303 unique_lock<mutex> lock(queue_lock);
304 frame_queue.push_back(qf);
305 queue_changed.notify_all();
308 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)
310 fprintf(stderr, "output_pts=%ld faded input_pts=%ld,%ld fade_alpha=%.2f\n", output_pts, input_pts, secondary_input_pts, fade_alpha);
312 // Get the temporary OpenGL resources we need for doing the fade.
313 // (We share these with interpolated frames, which is slightly
314 // overkill, but there's no need to waste resources on keeping
315 // separate pools around.)
316 InterpolatedFrameResources resources;
318 unique_lock<mutex> lock(queue_lock);
319 if (interpolate_resources.empty()) {
320 fprintf(stderr, "WARNING: Too many interpolated frames already in transit; dropping one.\n");
323 resources = interpolate_resources.front();
324 interpolate_resources.pop_front();
330 jpeg_id1.stream_idx = stream_idx;
331 jpeg_id1.pts = input_pts;
332 jpeg_id1.interpolated = false;
333 shared_ptr<Frame> frame1 = decode_jpeg_with_cache(jpeg_id1, DECODE_IF_NOT_IN_CACHE, &did_decode);
336 jpeg_id2.stream_idx = secondary_stream_idx;
337 jpeg_id2.pts = secondary_input_pts;
338 jpeg_id2.interpolated = false;
339 shared_ptr<Frame> frame2 = decode_jpeg_with_cache(jpeg_id2, DECODE_IF_NOT_IN_CACHE, &did_decode);
341 ycbcr_semiplanar_converter->prepare_chain_for_fade(frame1, frame2, fade_alpha)->render_to_fbo(resources.fade_fbo, 1280, 720);
344 qf.local_pts = local_pts;
345 qf.type = QueuedFrame::FADED;
346 qf.output_pts = output_pts;
347 qf.stream_idx = stream_idx;
348 qf.resources = resources;
349 qf.input_first_pts = input_pts;
350 qf.display_func = move(display_func);
352 qf.secondary_stream_idx = secondary_stream_idx;
353 qf.secondary_input_pts = secondary_input_pts;
355 // Subsample and split Cb/Cr.
356 chroma_subsampler->subsample_chroma(resources.fade_cbcr_output_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
358 // Read it down (asynchronously) to the CPU.
359 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
360 glBindBuffer(GL_PIXEL_PACK_BUFFER, resources.pbo);
362 glGetTextureImage(resources.fade_y_output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
364 glGetTextureImage(resources.cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720));
366 glGetTextureImage(resources.cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720));
368 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
370 // Set a fence we can wait for to make sure the CPU sees the read.
371 glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
373 qf.fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
376 unique_lock<mutex> lock(queue_lock);
377 frame_queue.push_back(qf);
378 queue_changed.notify_all();
382 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)
384 if (secondary_stream_idx != -1) {
385 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);
387 fprintf(stderr, "output_pts=%ld interpolated input_pts1=%ld input_pts2=%ld alpha=%.3f\n", output_pts, input_first_pts, input_second_pts, alpha);
391 if (secondary_stream_idx == -1) {
392 id = JPEGID{ stream_idx, output_pts, /*interpolated=*/true };
394 id = create_jpegid_for_interpolated_fade(stream_idx, output_pts, secondary_stream_idx, secondary_input_pts);
397 // Get the temporary OpenGL resources we need for doing the interpolation.
398 InterpolatedFrameResources resources;
400 unique_lock<mutex> lock(queue_lock);
401 if (interpolate_resources.empty()) {
402 fprintf(stderr, "WARNING: Too many interpolated frames already in transit; dropping one.\n");
405 resources = interpolate_resources.front();
406 interpolate_resources.pop_front();
410 qf.type = (secondary_stream_idx == -1) ? QueuedFrame::INTERPOLATED : QueuedFrame::FADED_INTERPOLATED;
411 qf.output_pts = output_pts;
412 qf.stream_idx = stream_idx;
413 qf.resources = resources;
415 qf.display_func = move(display_func);
419 // Convert frame0 and frame1 to OpenGL textures.
420 for (size_t frame_no = 0; frame_no < 2; ++frame_no) {
422 jpeg_id.stream_idx = stream_idx;
423 jpeg_id.pts = frame_no == 1 ? input_second_pts : input_first_pts;
424 jpeg_id.interpolated = false;
426 shared_ptr<Frame> frame = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode);
427 ycbcr_converter->prepare_chain_for_conversion(frame)->render_to_fbo(resources.input_fbos[frame_no], 1280, 720);
430 glGenerateTextureMipmap(resources.input_tex);
432 glGenerateTextureMipmap(resources.gray_tex);
435 // Compute the interpolated frame.
436 qf.flow_tex = compute_flow->exec(resources.gray_tex, DISComputeFlow::FORWARD_AND_BACKWARD, DISComputeFlow::DO_NOT_RESIZE_FLOW);
439 if (secondary_stream_idx != -1) {
440 // Fade. First kick off the interpolation.
441 tie(qf.output_tex, ignore) = interpolate_no_split->exec(resources.input_tex, resources.gray_tex, qf.flow_tex, 1280, 720, alpha);
444 // Now decode the image we are fading against.
446 jpeg_id.stream_idx = secondary_stream_idx;
447 jpeg_id.pts = secondary_input_pts;
448 jpeg_id.interpolated = false;
450 shared_ptr<Frame> frame2 = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode);
452 // Then fade against it, putting it into the fade Y' and CbCr textures.
453 ycbcr_semiplanar_converter->prepare_chain_for_fade_from_texture(qf.output_tex, frame2, fade_alpha)->render_to_fbo(resources.fade_fbo, 1280, 720);
455 // Subsample and split Cb/Cr.
456 chroma_subsampler->subsample_chroma(resources.fade_cbcr_output_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
458 tie(qf.output_tex, qf.cbcr_tex) = interpolate->exec(resources.input_tex, resources.gray_tex, qf.flow_tex, 1280, 720, alpha);
461 // Subsample and split Cb/Cr.
462 chroma_subsampler->subsample_chroma(qf.cbcr_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
465 // We could have released qf.flow_tex here, but to make sure we don't cause a stall
466 // when trying to reuse it for the next frame, we can just as well hold on to it
467 // and release it only when the readback is done.
469 // Read it down (asynchronously) to the CPU.
470 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
471 glBindBuffer(GL_PIXEL_PACK_BUFFER, resources.pbo);
473 if (secondary_stream_idx != -1) {
474 glGetTextureImage(resources.fade_y_output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
476 glGetTextureImage(qf.output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
479 glGetTextureImage(resources.cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720));
481 glGetTextureImage(resources.cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720));
483 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
485 // Set a fence we can wait for to make sure the CPU sees the read.
486 glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
488 qf.fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
491 unique_lock<mutex> lock(queue_lock);
492 frame_queue.push_back(qf);
493 queue_changed.notify_all();
497 void VideoStream::schedule_refresh_frame(steady_clock::time_point local_pts, int64_t output_pts, function<void()> &&display_func)
500 qf.type = QueuedFrame::REFRESH;
501 qf.output_pts = output_pts;
502 qf.display_func = move(display_func);
504 unique_lock<mutex> lock(queue_lock);
505 frame_queue.push_back(qf);
506 queue_changed.notify_all();
511 shared_ptr<Frame> frame_from_pbo(void *contents, size_t width, size_t height)
513 size_t chroma_width = width / 2;
515 const uint8_t *y = (const uint8_t *)contents;
516 const uint8_t *cb = (const uint8_t *)contents + width * height;
517 const uint8_t *cr = (const uint8_t *)contents + width * height + chroma_width * height;
519 shared_ptr<Frame> frame(new Frame);
520 frame->y.reset(new uint8_t[width * height]);
521 frame->cb.reset(new uint8_t[chroma_width * height]);
522 frame->cr.reset(new uint8_t[chroma_width * height]);
523 for (unsigned yy = 0; yy < height; ++yy) {
524 memcpy(frame->y.get() + width * yy, y + width * yy, width);
525 memcpy(frame->cb.get() + chroma_width * yy, cb + chroma_width * yy, chroma_width);
526 memcpy(frame->cr.get() + chroma_width * yy, cr + chroma_width * yy, chroma_width);
528 frame->is_semiplanar = false;
529 frame->width = width;
530 frame->height = height;
531 frame->chroma_subsampling_x = 2;
532 frame->chroma_subsampling_y = 1;
533 frame->pitch_y = width;
534 frame->pitch_chroma = chroma_width;
540 void VideoStream::encode_thread_func()
542 pthread_setname_np(pthread_self(), "VideoStream");
543 QSurface *surface = create_surface();
544 QOpenGLContext *context = create_context(surface);
545 bool ok = make_current(context, surface);
547 fprintf(stderr, "Video stream couldn't get an OpenGL context\n");
554 unique_lock<mutex> lock(queue_lock);
556 // Wait until we have a frame to play.
557 queue_changed.wait(lock, [this]{
558 return !frame_queue.empty();
560 steady_clock::time_point frame_start = frame_queue.front().local_pts;
562 // Now sleep until the frame is supposed to start (the usual case),
563 // _or_ clear_queue() happened.
564 bool aborted = queue_changed.wait_until(lock, frame_start, [this, frame_start]{
565 return frame_queue.empty() || frame_queue.front().local_pts != frame_start;
568 // clear_queue() happened, so don't play this frame after all.
571 qf = frame_queue.front();
572 frame_queue.pop_front();
575 if (qf.type == QueuedFrame::ORIGINAL) {
576 // Send the JPEG frame on, unchanged.
577 string jpeg = read_file(filename_for_frame(qf.stream_idx, qf.input_first_pts));
579 av_init_packet(&pkt);
580 pkt.stream_index = 0;
581 pkt.data = (uint8_t *)jpeg.data();
582 pkt.size = jpeg.size();
583 stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
585 last_frame.assign(&jpeg[0], &jpeg[0] + jpeg.size());
586 } else if (qf.type == QueuedFrame::FADED) {
587 glClientWaitSync(qf.fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
589 shared_ptr<Frame> frame = frame_from_pbo(qf.resources.pbo_contents, 1280, 720);
591 // Now JPEG encode it, and send it on to the stream.
592 vector<uint8_t> jpeg = encode_jpeg(frame->y.get(), frame->cb.get(), frame->cr.get(), 1280, 720);
595 av_init_packet(&pkt);
596 pkt.stream_index = 0;
597 pkt.data = (uint8_t *)jpeg.data();
598 pkt.size = jpeg.size();
599 stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
600 last_frame = move(jpeg);
602 // Put the frame resources back.
603 unique_lock<mutex> lock(queue_lock);
604 interpolate_resources.push_back(qf.resources);
605 } else if (qf.type == QueuedFrame::INTERPOLATED || qf.type == QueuedFrame::FADED_INTERPOLATED) {
606 glClientWaitSync(qf.fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
608 // Send a copy of the frame on to display.
609 shared_ptr<Frame> frame = frame_from_pbo(qf.resources.pbo_contents, 1280, 720);
610 JPEGFrameView::insert_interpolated_frame(qf.id, frame);
612 // Now JPEG encode it, and send it on to the stream.
613 vector<uint8_t> jpeg = encode_jpeg(frame->y.get(), frame->cb.get(), frame->cr.get(), 1280, 720);
614 compute_flow->release_texture(qf.flow_tex);
615 if (qf.type != QueuedFrame::FADED_INTERPOLATED) {
616 interpolate->release_texture(qf.output_tex);
617 interpolate->release_texture(qf.cbcr_tex);
621 av_init_packet(&pkt);
622 pkt.stream_index = 0;
623 pkt.data = (uint8_t *)jpeg.data();
624 pkt.size = jpeg.size();
625 stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
626 last_frame = move(jpeg);
628 // Put the frame resources back.
629 unique_lock<mutex> lock(queue_lock);
630 interpolate_resources.push_back(qf.resources);
631 } else if (qf.type == QueuedFrame::REFRESH) {
633 av_init_packet(&pkt);
634 pkt.stream_index = 0;
635 pkt.data = (uint8_t *)last_frame.data();
636 pkt.size = last_frame.size();
637 stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
641 if (qf.display_func != nullptr) {
647 int VideoStream::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
649 VideoStream *video_stream = (VideoStream *)opaque;
650 return video_stream->write_packet2(buf, buf_size, type, time);
653 int VideoStream::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
655 if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
656 seen_sync_markers = true;
657 } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
658 // We don't know if this is a keyframe or not (the muxer could
659 // avoid marking it), so we just have to make the best of it.
660 type = AVIO_DATA_MARKER_SYNC_POINT;
663 if (type == AVIO_DATA_MARKER_HEADER) {
664 stream_mux_header.append((char *)buf, buf_size);
665 global_httpd->set_header(stream_mux_header);
667 global_httpd->add_data((char *)buf, buf_size, type == AVIO_DATA_MARKER_SYNC_POINT, time, AVRational{ AV_TIME_BASE, 1 });