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>
26 extern HTTPD *global_httpd;
30 string read_file(const string &filename)
32 FILE *fp = fopen(filename.c_str(), "rb");
34 perror(filename.c_str());
38 fseek(fp, 0, SEEK_END);
44 fread(&ret[0], len, 1, fp);
51 struct VectorDestinationManager {
52 jpeg_destination_mgr pub;
53 std::vector<uint8_t> dest;
55 VectorDestinationManager()
57 pub.init_destination = init_destination_thunk;
58 pub.empty_output_buffer = empty_output_buffer_thunk;
59 pub.term_destination = term_destination_thunk;
62 static void init_destination_thunk(j_compress_ptr ptr)
64 ((VectorDestinationManager *)(ptr->dest))->init_destination();
67 inline void init_destination()
72 static boolean empty_output_buffer_thunk(j_compress_ptr ptr)
74 return ((VectorDestinationManager *)(ptr->dest))->empty_output_buffer();
77 inline bool empty_output_buffer()
79 make_room(dest.size()); // Should ignore pub.free_in_buffer!
83 inline void make_room(size_t bytes_used)
85 dest.resize(bytes_used + 4096);
86 dest.resize(dest.capacity());
87 pub.next_output_byte = dest.data() + bytes_used;
88 pub.free_in_buffer = dest.size() - bytes_used;
91 static void term_destination_thunk(j_compress_ptr ptr)
93 ((VectorDestinationManager *)(ptr->dest))->term_destination();
96 inline void term_destination()
98 dest.resize(dest.size() - pub.free_in_buffer);
101 static_assert(std::is_standard_layout<VectorDestinationManager>::value, "");
103 vector<uint8_t> encode_jpeg(const uint8_t *y_data, const uint8_t *cb_data, const uint8_t *cr_data, unsigned width, unsigned height)
105 VectorDestinationManager dest;
107 jpeg_compress_struct cinfo;
109 cinfo.err = jpeg_std_error(&jerr);
110 jpeg_create_compress(&cinfo);
112 cinfo.dest = (jpeg_destination_mgr *)&dest;
113 cinfo.input_components = 3;
114 cinfo.in_color_space = JCS_RGB;
115 jpeg_set_defaults(&cinfo);
116 constexpr int quality = 90;
117 jpeg_set_quality(&cinfo, quality, /*force_baseline=*/false);
119 cinfo.image_width = width;
120 cinfo.image_height = height;
121 cinfo.raw_data_in = true;
122 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
123 cinfo.comp_info[0].h_samp_factor = 2;
124 cinfo.comp_info[0].v_samp_factor = 1;
125 cinfo.comp_info[1].h_samp_factor = 1;
126 cinfo.comp_info[1].v_samp_factor = 1;
127 cinfo.comp_info[2].h_samp_factor = 1;
128 cinfo.comp_info[2].v_samp_factor = 1;
129 cinfo.CCIR601_sampling = true; // Seems to be mostly ignored by libjpeg, though.
130 jpeg_start_compress(&cinfo, true);
132 JSAMPROW yptr[8], cbptr[8], crptr[8];
133 JSAMPARRAY data[3] = { yptr, cbptr, crptr };
134 for (unsigned y = 0; y < height; y += 8) {
135 for (unsigned yy = 0; yy < 8; ++yy) {
136 yptr[yy] = const_cast<JSAMPROW>(&y_data[(y + yy) * width]);
137 cbptr[yy] = const_cast<JSAMPROW>(&cb_data[(y + yy) * width / 2]);
138 crptr[yy] = const_cast<JSAMPROW>(&cr_data[(y + yy) * width / 2]);
141 jpeg_write_raw_data(&cinfo, data, /*num_lines=*/8);
144 jpeg_finish_compress(&cinfo);
145 jpeg_destroy_compress(&cinfo);
147 return move(dest.dest);
150 VideoStream::VideoStream()
152 ycbcr_converter.reset(new YCbCrConverter(YCbCrConverter::OUTPUT_TO_DUAL_YCBCR, /*resource_pool=*/nullptr));
153 ycbcr_semiplanar_converter.reset(new YCbCrConverter(YCbCrConverter::OUTPUT_TO_SEMIPLANAR, /*resource_pool=*/nullptr));
155 GLuint input_tex[num_interpolate_slots], gray_tex[num_interpolate_slots];
156 GLuint fade_y_output_tex[num_interpolate_slots], fade_cbcr_output_tex[num_interpolate_slots];
157 GLuint cb_tex[num_interpolate_slots], cr_tex[num_interpolate_slots];
159 glCreateTextures(GL_TEXTURE_2D_ARRAY, 10, input_tex);
160 glCreateTextures(GL_TEXTURE_2D_ARRAY, 10, gray_tex);
161 glCreateTextures(GL_TEXTURE_2D, 10, fade_y_output_tex);
162 glCreateTextures(GL_TEXTURE_2D, 10, fade_cbcr_output_tex);
163 glCreateTextures(GL_TEXTURE_2D, 10, cb_tex);
164 glCreateTextures(GL_TEXTURE_2D, 10, cr_tex);
167 constexpr size_t width = 1280, height = 720; // FIXME: adjustable width, height
168 int levels = find_num_levels(width, height);
169 for (size_t i = 0; i < num_interpolate_slots; ++i) {
170 glTextureStorage3D(input_tex[i], levels, GL_RGBA8, width, height, 2);
172 glTextureStorage3D(gray_tex[i], levels, GL_R8, width, height, 2);
174 glTextureStorage2D(fade_y_output_tex[i], 1, GL_R8, width, height);
176 glTextureStorage2D(fade_cbcr_output_tex[i], 1, GL_RG8, width, height);
178 glTextureStorage2D(cb_tex[i], 1, GL_R8, width / 2, height);
180 glTextureStorage2D(cr_tex[i], 1, GL_R8, width / 2, height);
183 InterpolatedFrameResources resource;
184 resource.input_tex = input_tex[i];
185 resource.gray_tex = gray_tex[i];
186 resource.fade_y_output_tex = fade_y_output_tex[i];
187 resource.fade_cbcr_output_tex = fade_cbcr_output_tex[i];
188 resource.cb_tex = cb_tex[i];
189 resource.cr_tex = cr_tex[i];
190 glCreateFramebuffers(2, resource.input_fbos);
192 glCreateFramebuffers(1, &resource.fade_fbo);
195 glNamedFramebufferTextureLayer(resource.input_fbos[0], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 0);
197 glNamedFramebufferTextureLayer(resource.input_fbos[0], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 0);
199 glNamedFramebufferTextureLayer(resource.input_fbos[1], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 1);
201 glNamedFramebufferTextureLayer(resource.input_fbos[1], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 1);
203 glNamedFramebufferTexture(resource.fade_fbo, GL_COLOR_ATTACHMENT0, fade_y_output_tex[i], 0);
205 glNamedFramebufferTexture(resource.fade_fbo, GL_COLOR_ATTACHMENT1, fade_cbcr_output_tex[i], 0);
208 GLuint bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
209 glNamedFramebufferDrawBuffers(resource.input_fbos[0], 2, bufs);
211 glNamedFramebufferDrawBuffers(resource.input_fbos[1], 2, bufs);
213 glNamedFramebufferDrawBuffers(resource.fade_fbo, 2, bufs);
216 glCreateBuffers(1, &resource.pbo);
218 glNamedBufferStorage(resource.pbo, width * height * 4, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
220 resource.pbo_contents = glMapNamedBufferRange(resource.pbo, 0, width * height * 4, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
221 interpolate_resources.push_back(resource);
227 if (global_flags.interpolation_quality == 1) {
228 op = operating_point1;
229 } else if (global_flags.interpolation_quality == 2) {
230 op = operating_point2;
231 } else if (global_flags.interpolation_quality == 3) {
232 op = operating_point3;
233 } else if (global_flags.interpolation_quality == 4) {
234 op = operating_point4;
239 compute_flow.reset(new DISComputeFlow(width, height, op));
240 interpolate.reset(new Interpolate(op, /*split_ycbcr_output=*/true));
241 interpolate_no_split.reset(new Interpolate(op, /*split_ycbcr_output=*/false));
242 chroma_subsampler.reset(new ChromaSubsampler);
245 // The “last frame” is initially black.
246 unique_ptr<uint8_t[]> y(new uint8_t[1280 * 720]);
247 unique_ptr<uint8_t[]> cb_or_cr(new uint8_t[640 * 720]);
248 memset(y.get(), 16, 1280 * 720);
249 memset(cb_or_cr.get(), 128, 640 * 720);
250 last_frame = encode_jpeg(y.get(), cb_or_cr.get(), cb_or_cr.get(), 1280, 720);
253 VideoStream::~VideoStream() {}
255 void VideoStream::start()
257 AVFormatContext *avctx = avformat_alloc_context();
258 avctx->oformat = av_guess_format("nut", nullptr, nullptr);
260 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
261 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, nullptr, nullptr);
262 avctx->pb->write_data_type = &VideoStream::write_packet2_thunk;
263 avctx->pb->ignore_boundary_point = 1;
265 Mux::Codec video_codec = Mux::CODEC_MJPEG;
267 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
269 string video_extradata;
271 constexpr int width = 1280, height = 720; // Doesn't matter for MJPEG.
272 stream_mux.reset(new Mux(avctx, width, height, video_codec, video_extradata, /*audio_codec_parameters=*/nullptr, COARSE_TIMEBASE,
273 /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, {}));
276 encode_thread = thread(&VideoStream::encode_thread_func, this);
279 void VideoStream::stop()
281 encode_thread.join();
284 void VideoStream::schedule_original_frame(int64_t output_pts, unsigned stream_idx, int64_t input_pts)
286 fprintf(stderr, "output_pts=%ld original input_pts=%ld\n", output_pts, input_pts);
289 qf.type = QueuedFrame::ORIGINAL;
290 qf.output_pts = output_pts;
291 qf.stream_idx = stream_idx;
292 qf.input_first_pts = input_pts;
294 unique_lock<mutex> lock(queue_lock);
295 frame_queue.push_back(qf);
296 queue_nonempty.notify_all();
299 void VideoStream::schedule_faded_frame(int64_t output_pts, unsigned stream_idx, int64_t input_pts, int secondary_stream_idx, int64_t secondary_input_pts, float fade_alpha)
301 fprintf(stderr, "output_pts=%ld faded input_pts=%ld,%ld fade_alpha=%.2f\n", output_pts, input_pts, secondary_input_pts, fade_alpha);
303 // Get the temporary OpenGL resources we need for doing the fade.
304 // (We share these with interpolated frames, which is slightly
305 // overkill, but there's no need to waste resources on keeping
306 // separate pools around.)
307 InterpolatedFrameResources resources;
309 unique_lock<mutex> lock(queue_lock);
310 if (interpolate_resources.empty()) {
311 fprintf(stderr, "WARNING: Too many interpolated frames already in transit; dropping one.\n");
314 resources = interpolate_resources.front();
315 interpolate_resources.pop_front();
321 jpeg_id1.stream_idx = stream_idx;
322 jpeg_id1.pts = input_pts;
323 jpeg_id1.interpolated = false;
324 shared_ptr<Frame> frame1 = decode_jpeg_with_cache(jpeg_id1, DECODE_IF_NOT_IN_CACHE, &did_decode);
327 jpeg_id2.stream_idx = secondary_stream_idx;
328 jpeg_id2.pts = secondary_input_pts;
329 jpeg_id2.interpolated = false;
330 shared_ptr<Frame> frame2 = decode_jpeg_with_cache(jpeg_id2, DECODE_IF_NOT_IN_CACHE, &did_decode);
332 ycbcr_semiplanar_converter->prepare_chain_for_fade(frame1, frame2, fade_alpha)->render_to_fbo(resources.fade_fbo, 1280, 720);
335 qf.type = QueuedFrame::FADED;
336 qf.output_pts = output_pts;
337 qf.stream_idx = stream_idx;
338 qf.resources = resources;
339 qf.input_first_pts = input_pts;
341 qf.secondary_stream_idx = secondary_stream_idx;
342 qf.secondary_input_pts = secondary_input_pts;
344 // Subsample and split Cb/Cr.
345 chroma_subsampler->subsample_chroma(resources.fade_cbcr_output_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
347 // Read it down (asynchronously) to the CPU.
348 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
349 glBindBuffer(GL_PIXEL_PACK_BUFFER, resources.pbo);
351 glGetTextureImage(resources.fade_y_output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
353 glGetTextureImage(resources.cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720));
355 glGetTextureImage(resources.cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720));
357 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
359 // Set a fence we can wait for to make sure the CPU sees the read.
360 glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
362 qf.fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
365 unique_lock<mutex> lock(queue_lock);
366 frame_queue.push_back(qf);
367 queue_nonempty.notify_all();
370 void VideoStream::schedule_interpolated_frame(int64_t output_pts, 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)
372 if (secondary_stream_idx != -1) {
373 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);
375 fprintf(stderr, "output_pts=%ld interpolated input_pts1=%ld input_pts2=%ld alpha=%.3f\n", output_pts, input_first_pts, input_second_pts, alpha);
379 if (secondary_stream_idx == -1) {
380 id = JPEGID{ stream_idx, output_pts, /*interpolated=*/true };
382 id = create_jpegid_for_interpolated_fade(stream_idx, output_pts, secondary_stream_idx, secondary_input_pts);
385 // Get the temporary OpenGL resources we need for doing the interpolation.
386 InterpolatedFrameResources resources;
388 unique_lock<mutex> lock(queue_lock);
389 if (interpolate_resources.empty()) {
390 fprintf(stderr, "WARNING: Too many interpolated frames already in transit; dropping one.\n");
391 JPEGFrameView::insert_interpolated_frame(id, nullptr);
394 resources = interpolate_resources.front();
395 interpolate_resources.pop_front();
399 qf.type = (secondary_stream_idx == -1) ? QueuedFrame::INTERPOLATED : QueuedFrame::FADED_INTERPOLATED;
400 qf.output_pts = output_pts;
401 qf.stream_idx = stream_idx;
402 qf.resources = resources;
407 // Convert frame0 and frame1 to OpenGL textures.
408 for (size_t frame_no = 0; frame_no < 2; ++frame_no) {
410 jpeg_id.stream_idx = stream_idx;
411 jpeg_id.pts = frame_no == 1 ? input_second_pts : input_first_pts;
412 jpeg_id.interpolated = false;
414 shared_ptr<Frame> frame = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode);
415 ycbcr_converter->prepare_chain_for_conversion(frame)->render_to_fbo(resources.input_fbos[frame_no], 1280, 720);
418 glGenerateTextureMipmap(resources.input_tex);
420 glGenerateTextureMipmap(resources.gray_tex);
423 // Compute the interpolated frame.
424 qf.flow_tex = compute_flow->exec(resources.gray_tex, DISComputeFlow::FORWARD_AND_BACKWARD, DISComputeFlow::DO_NOT_RESIZE_FLOW);
427 if (secondary_stream_idx != -1) {
428 // Fade. First kick off the interpolation.
429 tie(qf.output_tex, ignore) = interpolate_no_split->exec(resources.input_tex, resources.gray_tex, qf.flow_tex, 1280, 720, alpha);
432 // Now decode the image we are fading against.
434 jpeg_id.stream_idx = secondary_stream_idx;
435 jpeg_id.pts = secondary_input_pts;
436 jpeg_id.interpolated = false;
438 shared_ptr<Frame> frame2 = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode);
440 // Then fade against it, putting it into the fade Y' and CbCr textures.
441 ycbcr_semiplanar_converter->prepare_chain_for_fade_from_texture(qf.output_tex, frame2, fade_alpha)->render_to_fbo(resources.fade_fbo, 1280, 720);
443 // Subsample and split Cb/Cr.
444 chroma_subsampler->subsample_chroma(resources.fade_cbcr_output_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
446 tie(qf.output_tex, qf.cbcr_tex) = interpolate->exec(resources.input_tex, resources.gray_tex, qf.flow_tex, 1280, 720, alpha);
449 // Subsample and split Cb/Cr.
450 chroma_subsampler->subsample_chroma(qf.cbcr_tex, 1280, 720, resources.cb_tex, resources.cr_tex);
453 // We could have released qf.flow_tex here, but to make sure we don't cause a stall
454 // when trying to reuse it for the next frame, we can just as well hold on to it
455 // and release it only when the readback is done.
457 // Read it down (asynchronously) to the CPU.
458 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
459 glBindBuffer(GL_PIXEL_PACK_BUFFER, resources.pbo);
461 if (secondary_stream_idx != -1) {
462 glGetTextureImage(resources.fade_y_output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
464 glGetTextureImage(qf.output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0));
467 glGetTextureImage(resources.cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720));
469 glGetTextureImage(resources.cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720));
471 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
473 // Set a fence we can wait for to make sure the CPU sees the read.
474 glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
476 qf.fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
479 unique_lock<mutex> lock(queue_lock);
480 frame_queue.push_back(qf);
481 queue_nonempty.notify_all();
484 void VideoStream::schedule_refresh_frame(int64_t output_pts)
487 av_init_packet(&pkt);
488 pkt.stream_index = 0;
489 pkt.data = (uint8_t *)last_frame.data();
490 pkt.size = last_frame.size();
491 stream_mux->add_packet(pkt, output_pts, output_pts);
496 shared_ptr<Frame> frame_from_pbo(void *contents, size_t width, size_t height)
498 size_t chroma_width = width / 2;
500 const uint8_t *y = (const uint8_t *)contents;
501 const uint8_t *cb = (const uint8_t *)contents + width * height;
502 const uint8_t *cr = (const uint8_t *)contents + width * height + chroma_width * height;
504 shared_ptr<Frame> frame(new Frame);
505 frame->y.reset(new uint8_t[width * height]);
506 frame->cb.reset(new uint8_t[chroma_width * height]);
507 frame->cr.reset(new uint8_t[chroma_width * height]);
508 for (unsigned yy = 0; yy < height; ++yy) {
509 memcpy(frame->y.get() + width * yy, y + width * yy, width);
510 memcpy(frame->cb.get() + chroma_width * yy, cb + chroma_width * yy, chroma_width);
511 memcpy(frame->cr.get() + chroma_width * yy, cr + chroma_width * yy, chroma_width);
513 frame->is_semiplanar = false;
514 frame->width = width;
515 frame->height = height;
516 frame->chroma_subsampling_x = 2;
517 frame->chroma_subsampling_y = 1;
518 frame->pitch_y = width;
519 frame->pitch_chroma = chroma_width;
525 void VideoStream::encode_thread_func()
527 pthread_setname_np(pthread_self(), "VideoStream");
528 QSurface *surface = create_surface();
529 QOpenGLContext *context = create_context(surface);
530 bool ok = make_current(context, surface);
532 fprintf(stderr, "Video stream couldn't get an OpenGL context\n");
539 unique_lock<mutex> lock(queue_lock);
540 queue_nonempty.wait(lock, [this]{
541 return !frame_queue.empty();
543 qf = frame_queue.front();
544 frame_queue.pop_front();
547 if (qf.type == QueuedFrame::ORIGINAL) {
548 // Send the JPEG frame on, unchanged.
549 string jpeg = read_file(filename_for_frame(qf.stream_idx, qf.input_first_pts));
551 av_init_packet(&pkt);
552 pkt.stream_index = 0;
553 pkt.data = (uint8_t *)jpeg.data();
554 pkt.size = jpeg.size();
555 stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
557 last_frame.assign(&jpeg[0], &jpeg[0] + jpeg.size());
558 } else if (qf.type == QueuedFrame::FADED) {
559 glClientWaitSync(qf.fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
561 shared_ptr<Frame> frame = frame_from_pbo(qf.resources.pbo_contents, 1280, 720);
563 // Now JPEG encode it, and send it on to the stream.
564 vector<uint8_t> jpeg = encode_jpeg(frame->y.get(), frame->cb.get(), frame->cr.get(), 1280, 720);
567 av_init_packet(&pkt);
568 pkt.stream_index = 0;
569 pkt.data = (uint8_t *)jpeg.data();
570 pkt.size = jpeg.size();
571 stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
572 last_frame = move(jpeg);
574 // Put the frame resources back.
575 unique_lock<mutex> lock(queue_lock);
576 interpolate_resources.push_back(qf.resources);
577 } else if (qf.type == QueuedFrame::INTERPOLATED || qf.type == QueuedFrame::FADED_INTERPOLATED) {
578 glClientWaitSync(qf.fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
580 // Send a copy of the frame on to display.
581 shared_ptr<Frame> frame = frame_from_pbo(qf.resources.pbo_contents, 1280, 720);
582 JPEGFrameView::insert_interpolated_frame(qf.id, frame);
584 // Now JPEG encode it, and send it on to the stream.
585 vector<uint8_t> jpeg = encode_jpeg(frame->y.get(), frame->cb.get(), frame->cr.get(), 1280, 720);
586 compute_flow->release_texture(qf.flow_tex);
587 if (qf.type != QueuedFrame::FADED_INTERPOLATED) {
588 interpolate->release_texture(qf.output_tex);
589 interpolate->release_texture(qf.cbcr_tex);
593 av_init_packet(&pkt);
594 pkt.stream_index = 0;
595 pkt.data = (uint8_t *)jpeg.data();
596 pkt.size = jpeg.size();
597 stream_mux->add_packet(pkt, qf.output_pts, qf.output_pts);
598 last_frame = move(jpeg);
600 // Put the frame resources back.
601 unique_lock<mutex> lock(queue_lock);
602 interpolate_resources.push_back(qf.resources);
609 int VideoStream::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
611 VideoStream *video_stream = (VideoStream *)opaque;
612 return video_stream->write_packet2(buf, buf_size, type, time);
615 int VideoStream::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
617 if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
618 seen_sync_markers = true;
619 } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
620 // We don't know if this is a keyframe or not (the muxer could
621 // avoid marking it), so we just have to make the best of it.
622 type = AVIO_DATA_MARKER_SYNC_POINT;
625 if (type == AVIO_DATA_MARKER_HEADER) {
626 stream_mux_header.append((char *)buf, buf_size);
627 global_httpd->set_header(stream_mux_header);
629 global_httpd->add_data((char *)buf, buf_size, type == AVIO_DATA_MARKER_SYNC_POINT, time, AVRational{ AV_TIME_BASE, 1 });