]> git.sesse.net Git - pkanalytics/blob - video_widget.cpp
Fix so that we can change existing events to pulls.
[pkanalytics] / video_widget.cpp
1 #define GL_GLEXT_PROTOTYPES
2
3 #include "video_widget.h"
4
5 #include <assert.h>
6 #include <pthread.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13
14 extern "C" {
15 #include <libavcodec/avcodec.h>
16 #include <libavformat/avformat.h>
17 #include <libavutil/avutil.h>
18 #include <libavutil/error.h>
19 #include <libavutil/frame.h>
20 #include <libavutil/imgutils.h>
21 #include <libavutil/mem.h>
22 #include <libavutil/pixfmt.h>
23 #include <libavutil/opt.h>
24 #include <libswscale/swscale.h>
25 }
26
27 #include <chrono>
28 #include <cstdint>
29 #include <utility>
30 #include <vector>
31 #include <unordered_set>
32
33 #include "post_to_main_thread.h"
34
35 #include <QOpenGLFunctions>
36 #include <QWheelEvent>
37 #include <QMouseEvent>
38 #include <QMouseEvent>
39 #include <QHBoxLayout>
40
41 #define BUFFER_OFFSET(i) ((char *)nullptr + (i))
42
43 using namespace std;
44 using namespace std::chrono;
45
46 namespace {
47
48 bool is_full_range(const AVPixFmtDescriptor *desc)
49 {
50         // This is horrible, but there's no better way that I know of.
51         return (strchr(desc->name, 'j') != nullptr);
52 }
53
54 AVPixelFormat decide_dst_format(AVPixelFormat src_format)
55 {
56         // If this is a non-Y'CbCr format, just convert to 4:4:4 Y'CbCr
57         // and be done with it. It's too strange to spend a lot of time on.
58         // (Let's hope there's no alpha.)
59         const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_format);
60         if (src_desc == nullptr ||
61             src_desc->nb_components != 3 ||
62             (src_desc->flags & AV_PIX_FMT_FLAG_RGB)) {
63                 return AV_PIX_FMT_YUV444P;
64         }
65
66         // The best for us would be Cb and Cr together if possible,
67         // but FFmpeg doesn't support that except in the special case of
68         // NV12, so we need to go to planar even for the case of NV12.
69         // Thus, look for the closest (but no worse) 8-bit planar Y'CbCr format
70         // that matches in color range. (This will also include the case of
71         // the source format already being acceptable.)
72         bool src_full_range = is_full_range(src_desc);
73         const char *best_format = "yuv444p";
74         unsigned best_score = numeric_limits<unsigned>::max();
75         for (const AVPixFmtDescriptor *desc = av_pix_fmt_desc_next(nullptr);
76              desc;
77              desc = av_pix_fmt_desc_next(desc)) {
78                 // Find planar Y'CbCr formats only.
79                 if (desc->nb_components != 3) continue;
80                 if (desc->flags & AV_PIX_FMT_FLAG_RGB) continue;
81                 if (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) continue;
82                 if (desc->comp[0].plane != 0 ||
83                     desc->comp[1].plane != 1 ||
84                     desc->comp[2].plane != 2) continue;
85
86                 // 8-bit formats only.
87                 if (desc->flags & AV_PIX_FMT_FLAG_BE) continue;
88                 if (desc->comp[0].depth != 8) continue;
89
90                 // Same or better chroma resolution only.
91                 int chroma_w_diff = src_desc->log2_chroma_w - desc->log2_chroma_w;
92                 int chroma_h_diff = src_desc->log2_chroma_h - desc->log2_chroma_h;
93                 if (chroma_w_diff < 0 || chroma_h_diff < 0)
94                         continue;
95
96                 // Matching full/limited range only.
97                 if (is_full_range(desc) != src_full_range)
98                         continue;
99
100                 // Pick something with as little excess chroma resolution as possible.
101                 unsigned score = (1 << (chroma_w_diff)) << chroma_h_diff;
102                 if (score < best_score) {
103                         best_score = score;
104                         best_format = desc->name;
105                 }
106         }
107         return av_get_pix_fmt(best_format);
108 }
109
110 }  // namespace
111
112 bool VideoWidget::process_queued_commands(AVFormatContext *format_ctx, AVCodecContext *video_codec_ctx, int video_stream_index, bool *seeked)
113 {
114         // Process any queued commands from other threads.
115         vector<QueuedCommand> commands;
116         {
117                 lock_guard<mutex> lock(queue_mu);
118                 swap(commands, command_queue);
119         }
120
121         for (const QueuedCommand &cmd : commands) {
122                 switch (cmd.command) {
123                 case QueuedCommand::PAUSE:
124                         paused = true;
125                         break;
126                 case QueuedCommand::RESUME:
127                         paused = false;
128                         pts_origin = last_pts;
129                         start = next_frame_start = steady_clock::now();
130                         break;
131                 case QueuedCommand::SEEK:
132                 case QueuedCommand::SEEK_ABSOLUTE:
133                         // Dealt with below.
134                         break;
135                 }
136         }
137
138         // Combine all seeks into one big one. (There are edge cases where this is probably
139         // subtly wrong, but we'll live with it.)
140         int64_t base_pts = last_pts;
141         int64_t relative_seek_ms = 0;
142         int64_t relative_seek_frames = 0;
143         for (const QueuedCommand &cmd : commands) {
144                 if (cmd.command == QueuedCommand::SEEK) {
145                         relative_seek_ms += cmd.relative_seek_ms;
146                         relative_seek_frames += cmd.relative_seek_frames;
147                 } else if (cmd.command == QueuedCommand::SEEK_ABSOLUTE) {
148                         base_pts = av_rescale_q(cmd.seek_ms, AVRational{ 1, 1000 }, video_timebase);
149                         relative_seek_ms = 0;
150                         relative_seek_frames = 0;
151                 }
152         }
153         int64_t relative_seek_pts = av_rescale_q(relative_seek_ms, AVRational{ 1, 1000 }, video_timebase);
154         if (relative_seek_ms != 0 && relative_seek_pts == 0) {
155                 // Just to be sure rounding errors don't move us into nothingness.
156                 relative_seek_pts = (relative_seek_ms > 0) ? 1 : -1;
157         }
158         int64_t goal_pts = base_pts + relative_seek_pts;
159         if (goal_pts != last_pts || relative_seek_frames < 0) {
160                 avcodec_flush_buffers(video_codec_ctx);
161                 queued_frames.clear();
162
163                 // Seek to the last keyframe before this point.
164                 int64_t seek_pts = goal_pts;
165                 if (relative_seek_frames < 0) {
166                         // If we're frame-skipping backwards, add 100 ms of slop for each frame
167                         // so we're fairly certain we are able to see the ones we want.
168                         seek_pts -= av_rescale_q(-relative_seek_frames, AVRational{ 1, 10 }, video_timebase);
169                 }
170                 av_seek_frame(format_ctx, video_stream_index, seek_pts, AVSEEK_FLAG_BACKWARD);
171
172                 // Decode frames until EOF, or until we see something past our seek point.
173                 std::deque<AVFrameWithDeleter> queue;
174                 for ( ;; ) {
175                         bool error = false;
176                         AVFrameWithDeleter frame = decode_frame(format_ctx, video_codec_ctx,
177                                 pathname, video_stream_index, &error);
178                         if (frame == nullptr || error) {
179                                 break;
180                         }
181
182                         int64_t frame_pts = frame->pts;
183                         if (relative_seek_frames < 0) {
184                                 // Buffer this frame; don't display it unless we know it's the Nth-latest.
185                                 queue.push_back(std::move(frame));
186                                 if (queue.size() > uint64_t(-relative_seek_frames) + 1) {
187                                         queue.pop_front();
188                                 }
189                         }
190                         if (frame_pts >= goal_pts) {
191                                 if (relative_seek_frames > 0) {
192                                         --relative_seek_frames;
193                                 } else {
194                                         if (relative_seek_frames < 0) {
195                                                 // Hope we have the right amount.
196                                                 // The rest will remain in the queue for when we play forward again.
197                                                 frame = std::move(queue.front());
198                                                 queue.pop_front();
199                                                 queued_frames = std::move(queue);
200                                         }
201                                         video_window->set_current_frame(make_video_frame(frame.get()));
202                                         update();
203                                         store_pts(frame->pts);
204                                         break;
205                                 }
206                         }
207                 }
208
209                 // NOTE: We keep pause status as-is.
210
211                 pts_origin = last_pts;
212                 start = next_frame_start = last_frame = steady_clock::now();
213                 if (seeked) {
214                         *seeked = true;
215                 }
216         } else if (relative_seek_frames > 0) {
217                 // The base PTS is fine, we only need to skip a few frames forwards.
218                 while (relative_seek_frames > 1) {
219                         // Eat a frame (ignore errors).
220                         bool error;
221                         decode_frame(format_ctx, video_codec_ctx, pathname, video_stream_index, &error);
222                         --relative_seek_frames;
223                 }
224
225                 // Display the last one.
226                 bool error;
227                 AVFrameWithDeleter frame = decode_frame(format_ctx, video_codec_ctx,
228                         pathname, video_stream_index, &error);
229                 if (frame == nullptr || error) {
230                         return true;
231                 }
232                 video_window->set_current_frame(make_video_frame(frame.get()));
233                 update();
234                 store_pts(frame->pts);
235         }
236         return false;
237 }
238
239 VideoWidget::VideoWidget(QWidget *parent)
240         : QWidget(parent),
241           video_window(new VideoWindow(this)) {
242         setLayout(new QHBoxLayout);
243         layout()->addWidget(QWidget::createWindowContainer(video_window));
244
245         // ...
246         connect(video_window, &VideoWindow::mouse_wheel, this, &VideoWidget::wheelEvent);
247         connect(video_window, &VideoWindow::mouse_pressed, this, &VideoWidget::mousePressEvent);
248         connect(video_window, &VideoWindow::mouse_released, this, &VideoWidget::mouseReleaseEvent);
249         connect(video_window, &VideoWindow::mouse_moved, this, &VideoWidget::mouseMoveEvent);
250 }
251
252 VideoWidget::~VideoWidget()
253 {
254         stop();
255
256         // Qt will delete video_window for us after we're gone,
257         // so make sure its destructor does not try to mess with
258         // our freelist. The actual freelist frames will leak.
259         video_window->set_current_frame(nullptr);
260 }
261
262 GLuint compile_shader(const string &shader_src, GLenum type)
263 {
264         GLuint obj = glCreateShader(type);
265         const GLchar* source[] = { shader_src.data() };
266         const GLint length[] = { (GLint)shader_src.size() };
267         glShaderSource(obj, 1, source, length);
268         glCompileShader(obj);
269
270         GLchar info_log[4096];
271         GLsizei log_length = sizeof(info_log) - 1;
272         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
273         info_log[log_length] = 0;
274         if (strlen(info_log) > 0) {
275                 fprintf(stderr, "Shader compile log: %s\n", info_log);
276         }
277
278         GLint status;
279         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
280         if (status == GL_FALSE) {
281                 // Add some line numbers to easier identify compile errors.
282                 string src_with_lines = "/*   1 */ ";
283                 size_t lineno = 1;
284                 for (char ch : shader_src) {
285                         src_with_lines.push_back(ch);
286                         if (ch == '\n') {
287                                 char buf[32];
288                                 snprintf(buf, sizeof(buf), "/* %3zu */ ", ++lineno);
289                                 src_with_lines += buf;
290                         }
291                 }
292
293                 fprintf(stderr, "Failed to compile shader:\n%s\n", src_with_lines.c_str());
294                 exit(1);
295         }
296
297         return obj;
298 }
299
300 void VideoWindow::initializeGL()
301 {
302         glDisable(GL_BLEND);
303         glDisable(GL_DEPTH_TEST);
304         glDepthMask(GL_FALSE);
305         glCreateTextures(GL_TEXTURE_2D, 3, tex);
306
307         ycbcr_vertex_shader = compile_shader(R"(
308 #version 460 core
309
310 layout(location = 0) in vec2 position;
311 layout(location = 1) in vec2 texcoord;
312 out vec2 tc;
313
314 void main()
315 {
316         // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is:
317         //
318         //   2.000  0.000  0.000 -1.000
319         //   0.000  2.000  0.000 -1.000
320         //   0.000  0.000 -2.000 -1.000
321         //   0.000  0.000  0.000  1.000
322         gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0);
323         tc = texcoord;
324         tc.y = 1.0f - tc.y;
325 }
326 )", GL_VERTEX_SHADER);
327         ycbcr_fragment_shader = compile_shader(R"(
328 #version 460 core
329
330 layout(location = 0) uniform sampler2D tex_y;
331 layout(location = 1) uniform sampler2D tex_cb;
332 layout(location = 2) uniform sampler2D tex_cr;
333 layout(location = 3) uniform vec2 cbcr_offset;
334
335 in vec2 tc;
336 out vec4 FragColor;
337
338 // Computed statically by Movit, for limited-range BT.709.
339 // (We don't check whether the input could be BT.601 or BT.2020 currently, or full-range)
340 const mat3 inv_ycbcr_matrix = mat3(
341         1.16438f, 1.16438f, 1.16438f,
342         0.0f, -0.21325f, 2.11240f,
343         1.79274f, -0.53291f, 0.0f
344 );
345
346 void main()
347 {
348         if (tc.x < 0.0 || tc.x > 1.0 || tc.y < 0.0 || tc.y > 1.0) {
349                 FragColor.rgba = vec4(0.0f, 0.0f, 0.0f, 1.0f);
350                 return;
351         }
352
353         vec3 ycbcr;
354         ycbcr.r = texture(tex_y, tc).r;
355         ycbcr.g = texture(tex_cb, tc + cbcr_offset).r;
356         ycbcr.b = texture(tex_cr, tc + cbcr_offset).r;
357         ycbcr -= vec3(16.0f / 255.0f, 128.0f / 255.0f, 128.0f / 255.0f);
358         FragColor.rgb = inv_ycbcr_matrix * ycbcr;
359         FragColor.a = 1.0f;
360 }
361 )", GL_FRAGMENT_SHADER);
362         ycbcr_program = glCreateProgram();
363         glAttachShader(ycbcr_program, ycbcr_vertex_shader);
364         glAttachShader(ycbcr_program, ycbcr_fragment_shader);
365         glLinkProgram(ycbcr_program);
366
367         GLint success;
368         glGetProgramiv(ycbcr_program, GL_LINK_STATUS, &success);
369         if (success == GL_FALSE) {
370                 GLchar error_log[1024] = {0};
371                 glGetProgramInfoLog(ycbcr_program, 1024, nullptr, error_log);
372                 fprintf(stderr, "Error linking program: %s\n", error_log);
373                 exit(1);
374         }
375
376         glCreateSamplers(1, &bilinear_sampler);
377         glSamplerParameteri(bilinear_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
378         glSamplerParameteri(bilinear_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
379         glSamplerParameteri(bilinear_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
380         glSamplerParameteri(bilinear_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
381 }
382
383 void VideoWindow::resizeGL(int w, int h)
384 {
385         glViewport(0, 0, w, h);
386         display_aspect = double(w) / h;
387 }
388
389 int num_levels(GLuint width, GLuint height)
390 {
391         int levels = 1;
392         while (width > 1 || height > 1) {
393                 width = max(width / 2, 1u);
394                 height = max(height / 2, 1u);
395                 ++levels;
396         }
397         return levels;
398 }
399
400 void VideoWindow::paintGL()
401 {
402         std::shared_ptr<VideoWidget::Frame> frame;
403         {
404                 lock_guard lock(current_frame_mu);
405                 frame = current_frame;
406         }
407         if (frame == nullptr) {
408                 glClear(GL_COLOR_BUFFER_BIT);
409                 return;
410         }
411
412         glUseProgram(ycbcr_program);
413         if (frame->width != last_width || frame->height != last_height) {
414                 glTextureStorage2D(tex[0], num_levels(frame->width, frame->height), GL_R8, frame->width, frame->height);
415         }
416         if (frame->chroma_width != last_chroma_width || frame->chroma_height != last_chroma_height) {
417                 for (GLuint num : { tex[1], tex[2] }) {
418                         glTextureStorage2D(num, num_levels(frame->chroma_width, frame->chroma_height), GL_R8, frame->chroma_width, frame->chroma_height);
419                 }
420         }
421
422         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, frame->pbo);
423
424         if (frame->need_flush_len > 0) {
425                 glFlushMappedNamedBufferRange(frame->pbo, 0, frame->need_flush_len);
426                 frame->need_flush_len = 0;
427         }
428
429         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
430         glTextureSubImage2D(tex[0], 0, 0, 0, frame->width, frame->height, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
431         glGenerateTextureMipmap(tex[0]);
432
433         glTextureSubImage2D(tex[1], 0, 0, 0, frame->chroma_width, frame->chroma_height, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(frame->width * frame->height));
434         glGenerateTextureMipmap(tex[1]);
435
436         glTextureSubImage2D(tex[2], 0, 0, 0, frame->chroma_width, frame->chroma_height, GL_RED, GL_UNSIGNED_BYTE, BUFFER_OFFSET(frame->width * frame->height + frame->chroma_width * frame->chroma_height));
437         glGenerateTextureMipmap(tex[2]);
438
439         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
440
441         glBindTextureUnit(0, tex[0]);
442         glBindTextureUnit(1, tex[1]);
443         glBindTextureUnit(2, tex[2]);
444         glBindSampler(0, bilinear_sampler);
445         glBindSampler(1, bilinear_sampler);
446         glBindSampler(2, bilinear_sampler);
447         glProgramUniform1i(ycbcr_program, 0, 0);
448         glProgramUniform1i(ycbcr_program, 1, 1);
449         glProgramUniform1i(ycbcr_program, 2, 2);
450         glProgramUniform2f(ycbcr_program, 3, cbcr_offset[0], -cbcr_offset[1]);
451
452         float tx1 = 0.0f;
453         float tx2 = 1.0f;
454         float ty1 = 0.0f;
455         float ty2 = 1.0f;
456
457         double video_aspect = double(frame->width) / frame->height;
458         if (display_aspect > video_aspect) {
459                 double extra_width = frame->height * display_aspect - frame->width;
460                 tx1 = -0.5 * extra_width / frame->width;
461                 tx2 = 1.0 + 0.5 * extra_width / frame->width;
462         } else if (display_aspect < video_aspect) {
463                 double extra_height = frame->width / display_aspect - frame->height;
464                 ty1 = -0.5 * extra_height / frame->height;
465                 ty2 = 1.0 + 0.5 * extra_height / frame->height;
466         }
467
468         glBegin(GL_QUADS);
469
470         // (0,0)
471         glVertexAttrib2f(1, tx1, ty1);
472         glVertex2f(zoom_matrix[2 * 3 + 0], zoom_matrix[2 * 3 + 1]);
473
474         // (0,1)
475         glVertexAttrib2f(1, tx1, ty2);
476         glVertex2f(zoom_matrix[1 * 3 + 0] + zoom_matrix[2 * 3 + 0], zoom_matrix[1 * 3 + 1] + zoom_matrix[2 * 3 + 1]);
477
478         // (1,1)
479         glVertexAttrib2f(1, tx2, ty2);
480         glVertex2f(zoom_matrix[0 * 3 + 0] + zoom_matrix[1 * 3 + 0] + zoom_matrix[2 * 3 + 0],
481                    zoom_matrix[1 * 3 + 0] + zoom_matrix[1 * 3 + 1] + zoom_matrix[2 * 3 + 1]);
482
483         // (1,0)
484         glVertexAttrib2f(1, tx2, ty1);
485         glVertex2f(zoom_matrix[0 * 3 + 0] + zoom_matrix[2 * 3 + 0],
486                    zoom_matrix[1 * 3 + 0] + zoom_matrix[2 * 3 + 1]);
487
488         glEnd();
489 }
490
491 void VideoWindow::set_current_frame(shared_ptr<VideoWidget::Frame> new_frame)
492 {
493         {
494                 lock_guard lock(current_frame_mu);
495                 current_frame = std::move(new_frame);
496         }
497         update();
498 }
499
500 void matmul3x3(const double a[9], const double b[9], double res[9])
501 {
502         for (int i = 0; i < 3; ++i) {
503                 for (int j = 0; j < 3; ++j) {
504                         double sum = 0.0;
505                         for (int k = 0; k < 3; ++k) {
506                                 sum += a[i * 3 + k] * b[k * 3 + j];
507                         }
508                         res[i * 3 + j] = sum;
509                 }
510         }
511 }
512
513 void VideoWidget::wheelEvent(QWheelEvent *event)
514 {
515         int delta = event->angleDelta().y();
516         if (delta == 0) {
517                 return;
518         }
519         double x = event->position().x() / width();
520         double y = 1.0 - event->position().y() / height();
521         double zoom = delta > 0 ? pow(1.005, delta) : pow(1/1.005, -delta);
522
523         const double inv_translation_matrix[9] = {
524                 1.0, 0.0, 0.0,
525                 0.0, 1.0, 0.0,
526                 -x, -y, 1.0
527         };
528         const double scale_matrix[9] = {
529                 zoom, 0.0, 0.0,
530                 0.0, zoom, 0.0,
531                 0.0, 0.0, 1.0
532         };
533         const double translation_matrix[9] = {
534                 1.0, 0.0, 0.0,
535                 0.0, 1.0, 0.0,
536                 x, y, 1.0
537         };
538         double tmp1[9], tmp2[9];
539         matmul3x3(zoom_matrix, inv_translation_matrix, tmp1);
540         matmul3x3(tmp1, scale_matrix, tmp2);
541         matmul3x3(tmp2, translation_matrix, zoom_matrix);
542
543         fixup_zoom_matrix();
544         video_window->set_zoom_matrix(zoom_matrix);
545         update();
546 }
547
548 void VideoWidget::mousePressEvent(QMouseEvent *e)
549 {
550         if (e->button() == Qt::BackButton) {
551                 emit mouse_back_clicked();
552         } else if (e->button() == Qt::ForwardButton) {
553                 emit mouse_forward_clicked();
554         } else if (e->button() == Qt::LeftButton) {
555                 dragging = true;
556                 last_drag_x = e->position().x();
557                 last_drag_y = e->position().y();
558         }
559 }
560
561 void VideoWidget::mouseReleaseEvent(QMouseEvent *e)
562 {
563         if (e->button() == Qt::LeftButton) {
564                 dragging = false;
565         }
566 }
567
568 void VideoWidget::mouseMoveEvent(QMouseEvent *e)
569 {
570         if (!dragging) {
571                 return;
572         }
573         float dx = (e->position().x() - last_drag_x) / width();
574         float dy = (e->position().y() - last_drag_y) / height();
575
576         //zoom_matrix[6] += dx * zoom_matrix[0];
577         //zoom_matrix[7] += dy * zoom_matrix[4];
578         zoom_matrix[6] += dx;
579         zoom_matrix[7] -= dy;
580         fixup_zoom_matrix();
581         video_window->set_zoom_matrix(zoom_matrix);
582
583         last_drag_x = e->position().x();
584         last_drag_y = e->position().y();
585
586         update();
587 }
588
589 // Normalize the matrix so that we never get skew or similar,
590 // and also never can zoom or pan too far out.
591 void VideoWidget::fixup_zoom_matrix()
592 {
593         // Correct for any numerical errors (we know the matrix must be orthogonal
594         // and have zero rotation).
595         zoom_matrix[4] = zoom_matrix[0];
596         zoom_matrix[1] = zoom_matrix[2] = zoom_matrix[3] = zoom_matrix[5] = 0.0;
597         zoom_matrix[8] = 1.0;
598
599         // We can't zoom further out than 1:1. (Perhaps it would be nice to
600         // reuse the last zoom-in point to do this, but the center will have to do
601         // for now.)
602         if (zoom_matrix[0] < 1.0) {
603                 const double zoom = 1.0 / zoom_matrix[0];
604                 const double inv_translation_matrix[9] = {
605                         1.0, 0.0, 0.0,
606                         0.0, 1.0, 0.0,
607                         -0.5, -0.5, 1.0
608                 };
609                 const double scale_matrix[9] = {
610                         zoom, 0.0, 0.0,
611                         0.0, zoom, 0.0,
612                         0.0, 0.0, 1.0
613                 };
614                 const double translation_matrix[9] = {
615                         1.0, 0.0, 0.0,
616                         0.0, 1.0, 0.0,
617                         0.5, 0.5, 1.0
618                 };
619                 double tmp1[9], tmp2[9];
620                 matmul3x3(zoom_matrix, inv_translation_matrix, tmp1);
621                 matmul3x3(tmp1, scale_matrix, tmp2);
622                 matmul3x3(tmp2, translation_matrix, zoom_matrix);
623         }
624
625         // Looking at the points we'll draw with glVertex2f(), make sure none of them are
626         // inside the square (which would generally mean we've panned ourselves out-of-bounds).
627         // We simply adjust the translation, which is possible because we fixed scaling above.
628         zoom_matrix[6] = min(zoom_matrix[6], 0.0);  // Left side (x=0).
629         zoom_matrix[7] = min(zoom_matrix[7], 0.0);  // Bottom side (y=0).
630         zoom_matrix[6] = std::max(zoom_matrix[6], 1.0 - zoom_matrix[0]);  // Right side (x=1).
631         zoom_matrix[7] = std::max(zoom_matrix[7], 1.0 - zoom_matrix[4]);  // Top side (y=1).
632 }
633
634 bool VideoWidget::open(const string &filename)
635 {
636         stop();
637         internal_rewind();
638         pathname = filename;
639         play();
640
641         while (running == STARTING) {
642                 // Poor man's condition variable...
643                 usleep(10000);
644                 sched_yield();
645         }
646         return (running != VIDEO_FILE_ERROR);   
647 }
648
649 void VideoWidget::play()
650 {
651         if (running != NOT_RUNNING && running != VIDEO_FILE_ERROR) {
652                 std::lock_guard<std::mutex> lock(queue_mu);
653                 command_queue.push_back(QueuedCommand { QueuedCommand::RESUME });
654                 producer_thread_should_quit.wakeup();
655                 return;
656         }
657         running = STARTING;
658         producer_thread_should_quit.unquit();
659         if (producer_thread.joinable()) {
660                 producer_thread.join();
661         }
662         producer_thread = std::thread(&VideoWidget::producer_thread_func, this);
663 }
664
665 void VideoWidget::pause()
666 {
667         if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
668                 return;
669         }
670         std::lock_guard<std::mutex> lock(queue_mu);
671         command_queue.push_back(QueuedCommand { QueuedCommand::PAUSE });
672         producer_thread_should_quit.wakeup();
673 }
674
675 void VideoWidget::seek(int64_t relative_seek_ms)
676 {
677         if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
678                 return;
679         }
680         std::lock_guard<std::mutex> lock(queue_mu);
681         command_queue.push_back(QueuedCommand { QueuedCommand::SEEK, relative_seek_ms, 0, 0 });
682         producer_thread_should_quit.wakeup();
683 }
684
685 void VideoWidget::seek_frames(int64_t relative_seek_frames)
686 {
687         if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
688                 return;
689         }
690         std::lock_guard<std::mutex> lock(queue_mu);
691         command_queue.push_back(QueuedCommand { QueuedCommand::SEEK, 0, relative_seek_frames, 0 });
692         producer_thread_should_quit.wakeup();
693 }
694
695 void VideoWidget::seek_absolute(int64_t position_ms)
696 {
697         if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
698                 return;
699         }
700         std::lock_guard<std::mutex> lock(queue_mu);
701         command_queue.push_back(QueuedCommand { QueuedCommand::SEEK_ABSOLUTE, 0, 0, position_ms });
702         producer_thread_should_quit.wakeup();
703 }
704
705 void VideoWidget::stop()
706 {
707         if (running == NOT_RUNNING || running == VIDEO_FILE_ERROR) {
708                 return;
709         }
710         producer_thread_should_quit.quit();
711         producer_thread.join();
712 }
713
714 void VideoWidget::producer_thread_func()
715 {
716         if (!producer_thread_should_quit.should_quit()) {
717                 if (!play_video(pathname)) {
718                         running = VIDEO_FILE_ERROR;
719                 } else {
720                         running = NOT_RUNNING;
721                 }
722         }
723 }
724
725 void VideoWidget::internal_rewind()
726 {
727         pts_origin = last_pts = 0;
728         last_position = 0;
729         start = next_frame_start = steady_clock::now();
730 }
731
732 template<AVHWDeviceType type>
733 AVPixelFormat get_hw_format(AVCodecContext *ctx, const AVPixelFormat *fmt)
734 {
735         bool found_config_of_right_type = false;
736         for (int i = 0;; ++i) {  // Termination condition inside loop.
737                 const AVCodecHWConfig *config = avcodec_get_hw_config(ctx->codec, i);
738                 if (config == nullptr) {  // End of list.
739                         break;
740                 }
741                 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) ||
742                     config->device_type != type) {
743                         // Not interesting for us.
744                         continue;
745                 }
746
747                 // We have a config of the right type, but does it actually support
748                 // the pixel format we want? (Seemingly, FFmpeg's way of signaling errors
749                 // is to just replace the pixel format with a software-decoded one,
750                 // such as yuv420p.)
751                 found_config_of_right_type = true;
752                 for (const AVPixelFormat *fmt_ptr = fmt; *fmt_ptr != -1; ++fmt_ptr) {
753                         if (config->pix_fmt == *fmt_ptr) {
754                                 fprintf(stderr, "Initialized '%s' hardware decoding for codec '%s'.\n",
755                                         av_hwdevice_get_type_name(type), ctx->codec->name);
756                                 if (ctx->profile == FF_PROFILE_H264_BASELINE) {
757                                         fprintf(stderr, "WARNING: Stream claims to be H.264 Baseline, which is generally poorly supported in hardware decoders.\n");
758                                         fprintf(stderr, "         Consider encoding it as Constrained Baseline, Main or High instead.\n");
759                                         fprintf(stderr, "         Decoding might fail and fall back to software.\n");
760                                 }
761                                 return config->pix_fmt;
762                         }
763                 }
764                 fprintf(stderr, "Decoder '%s' supports only these pixel formats:", ctx->codec->name);
765                 unordered_set<AVPixelFormat> seen;
766                 for (const AVPixelFormat *fmt_ptr = fmt; *fmt_ptr != -1; ++fmt_ptr) {
767                         if (!seen.count(*fmt_ptr)) {
768                                 fprintf(stderr, " %s", av_get_pix_fmt_name(*fmt_ptr));
769                                 seen.insert(*fmt_ptr);
770                         }
771                 }
772                 fprintf(stderr, " (wanted %s for hardware acceleration)\n", av_get_pix_fmt_name(config->pix_fmt));
773
774         }
775
776         if (!found_config_of_right_type) {
777                 fprintf(stderr, "Decoder '%s' does not support device type '%s'.\n", ctx->codec->name, av_hwdevice_get_type_name(type));
778         }
779
780         // We found no VA-API formats, so take the first software format.
781         for (const AVPixelFormat *fmt_ptr = fmt; *fmt_ptr != -1; ++fmt_ptr) {
782                 if ((av_pix_fmt_desc_get(*fmt_ptr)->flags & AV_PIX_FMT_FLAG_HWACCEL) == 0) {
783                         fprintf(stderr, "Falling back to software format %s.\n", av_get_pix_fmt_name(*fmt_ptr));
784                         return *fmt_ptr;
785                 }
786         }
787
788         // Fallback: Just return anything. (Should never really happen.)
789         return fmt[0];
790 }
791
792 AVFrameWithDeleter VideoWidget::decode_frame(AVFormatContext *format_ctx, AVCodecContext *video_codec_ctx,
793         const std::string &pathname, int video_stream_index,
794         bool *error)
795 {
796         *error = false;
797
798         if (!queued_frames.empty()) {
799                 AVFrameWithDeleter frame = std::move(queued_frames.front());
800                 queued_frames.pop_front();
801                 return frame;
802         }
803
804         // Read packets until we have a frame or there are none left.
805         bool frame_finished = false;
806         AVFrameWithDeleter video_avframe = av_frame_alloc_unique();
807         bool eof = false;
808         do {
809                 AVPacket *pkt = av_packet_alloc();
810                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
811                         pkt, av_packet_unref);
812                 pkt->data = nullptr;
813                 pkt->size = 0;
814                 if (av_read_frame(format_ctx, pkt) == 0) {
815                         if (pkt->stream_index == video_stream_index) {
816                                 if (avcodec_send_packet(video_codec_ctx, pkt) < 0) {
817                                         fprintf(stderr, "%s: Cannot send packet to video codec.\n", pathname.c_str());
818                                         *error = true;
819                                         return AVFrameWithDeleter(nullptr);
820                                 }
821                         }
822                 } else {
823                         eof = true;  // Or error, but ignore that for the time being.
824                 }
825
826                 // Decode video, if we have a frame.
827                 int err = avcodec_receive_frame(video_codec_ctx, video_avframe.get());
828                 if (err == 0) {
829                         frame_finished = true;
830                         break;
831                 } else if (err != AVERROR(EAGAIN)) {
832                         fprintf(stderr, "%s: Cannot receive frame from video codec.\n", pathname.c_str());
833                         *error = true;
834                         return AVFrameWithDeleter(nullptr);
835                 }
836         } while (!eof);
837
838         if (frame_finished)
839                 return video_avframe;
840         else
841                 return AVFrameWithDeleter(nullptr);
842 }
843
844 int find_stream_index(AVFormatContext *ctx, AVMediaType media_type)
845 {
846         for (unsigned i = 0; i < ctx->nb_streams; ++i) {
847                 if (ctx->streams[i]->codecpar->codec_type == media_type) {
848                         return i;
849                 }
850         }
851         return -1;
852 }
853
854 steady_clock::time_point compute_frame_start(int64_t frame_pts, int64_t pts_origin, const AVRational &video_timebase, const steady_clock::time_point &origin, double rate)
855 {
856         const duration<double> pts((frame_pts - pts_origin) * double(video_timebase.num) / double(video_timebase.den));
857         return origin + duration_cast<steady_clock::duration>(pts / rate);
858 }
859
860 bool VideoWidget::play_video(const string &pathname)
861 {
862         queued_frames.clear();
863         AVFormatContextWithCloser format_ctx = avformat_open_input_unique(pathname.c_str(), /*fmt=*/nullptr,
864                 /*options=*/nullptr);
865         if (format_ctx == nullptr) {
866                 fprintf(stderr, "%s: Error opening file\n", pathname.c_str());
867                 return false;
868         }
869
870         if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
871                 fprintf(stderr, "%s: Error finding stream info\n", pathname.c_str());
872                 return false;
873         }
874
875         int video_stream_index = find_stream_index(format_ctx.get(), AVMEDIA_TYPE_VIDEO);
876         if (video_stream_index == -1) {
877                 fprintf(stderr, "%s: No video stream found\n", pathname.c_str());
878                 return false;
879         }
880
881         // Open video decoder.
882         const AVCodecParameters *video_codecpar = format_ctx->streams[video_stream_index]->codecpar;
883         const AVCodec *video_codec = avcodec_find_decoder(video_codecpar->codec_id);
884
885         video_timebase = format_ctx->streams[video_stream_index]->time_base;
886         AVCodecContextWithDeleter video_codec_ctx = avcodec_alloc_context3_unique(nullptr);
887         if (avcodec_parameters_to_context(video_codec_ctx.get(), video_codecpar) < 0) {
888                 fprintf(stderr, "%s: Cannot fill video codec parameters\n", pathname.c_str());
889                 return false;
890         }
891         if (video_codec == nullptr) {
892                 fprintf(stderr, "%s: Cannot find video decoder\n", pathname.c_str());
893                 return false;
894         }
895
896         // Seemingly, it's not too easy to make something that just initializes
897         // “whatever goes”, so we don't get CUDA or VULKAN or whatever here
898         // without enumerating through several different types.
899         // VA-API and VDPAU will do for now. We prioritize VDPAU for the
900         // simple reason that there's a VA-API-via-VDPAU emulation for NVidia
901         // cards that seems to work, but just hangs when trying to transfer the frame.
902         //
903         // Note that we don't actually check codec support beforehand,
904         // so if you have a low-end VDPAU device but a high-end VA-API device,
905         // you lose out on the extra codec support from the latter.
906         AVBufferRef *hw_device_ctx = nullptr;
907         if (av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VDPAU, nullptr, nullptr, 0) >= 0) {
908                 video_codec_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
909                 video_codec_ctx->get_format = get_hw_format<AV_HWDEVICE_TYPE_VDPAU>;
910         } else if (av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, nullptr, nullptr, 0) >= 0) {
911                 video_codec_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
912                 video_codec_ctx->get_format = get_hw_format<AV_HWDEVICE_TYPE_VAAPI>;
913         } else {
914                 fprintf(stderr, "Failed to initialize VA-API or VDPAU for FFmpeg acceleration. Decoding video in software.\n");
915         }
916
917         if (avcodec_open2(video_codec_ctx.get(), video_codec, nullptr) < 0) {
918                 fprintf(stderr, "%s: Cannot open video decoder\n", pathname.c_str());
919                 return false;
920         }
921         unique_ptr<AVCodecContext, decltype(avcodec_close)*> video_codec_ctx_cleanup(
922                 video_codec_ctx.get(), avcodec_close);
923
924         internal_rewind();
925
926         running = RUNNING;
927
928         // Main loop.
929         int consecutive_errors = 0;
930         double rate = 1.0;
931         while (!producer_thread_should_quit.should_quit()) {
932                 if (process_queued_commands(format_ctx.get(), video_codec_ctx.get(), video_stream_index, /*seeked=*/nullptr)) {
933                         return true;
934                 }
935                 if (paused) {
936                         producer_thread_should_quit.sleep_for(hours(1));
937                         continue;
938                 }
939
940                 bool error;
941                 AVFrameWithDeleter frame = decode_frame(format_ctx.get(), video_codec_ctx.get(),
942                         pathname, video_stream_index, &error);
943                 if (error) {
944                         if (++consecutive_errors >= 100) {
945                                 fprintf(stderr, "More than 100 consecutive video frames, aborting playback.\n");
946                                 return false;
947                         } else {
948                                 continue;
949                         }
950                 } else {
951                         consecutive_errors = 0;
952                 }
953                 if (frame == nullptr) {
954                         // EOF.
955                         return false;
956                 }
957
958                 // Sleep until it's time to present this frame.
959                 for ( ;; ) {
960                         if (last_pts == 0 && pts_origin == 0) {
961                                 pts_origin = frame->pts;
962                         }
963                         steady_clock::time_point now = steady_clock::now();
964                         next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate);
965
966                         if (duration<double>(now - next_frame_start).count() >= 0.1) {
967                                 // If we don't have enough CPU to keep up, or if we have a live stream
968                                 // where the initial origin was somehow wrong, we could be behind indefinitely.
969                                 fprintf(stderr, "%s: Playback %.0f ms behind, resetting time scale\n",
970                                         pathname.c_str(),
971                                         1e3 * duration<double>(now - next_frame_start).count());
972                                 pts_origin = frame->pts;
973                                 start = next_frame_start = now;
974                         }
975                         bool finished_wakeup;
976                         finished_wakeup = producer_thread_should_quit.sleep_until(next_frame_start);
977                         if (finished_wakeup) {
978                                 video_window->set_current_frame(make_video_frame(frame.get()));
979                                 last_frame = steady_clock::now();
980                                 update();
981                                 break;
982                         } else {
983                                 if (producer_thread_should_quit.should_quit()) break;
984
985                                 bool seeked = false;
986                                 if (process_queued_commands(format_ctx.get(), video_codec_ctx.get(), video_stream_index, &seeked)) {
987                                         return true;
988                                 }
989
990                                 if (paused) {
991                                         // Just paused, so present the frame immediately and then go into deep sleep.
992                                         video_window->set_current_frame(make_video_frame(frame.get()));
993                                         last_frame = steady_clock::now();
994                                         update();
995                                         break;
996                                 }
997
998                                 // If we just seeked, drop this frame on the floor and be done.
999                                 if (seeked) {
1000                                         break;
1001                                 }
1002                         }
1003                 }
1004                 store_pts(frame->pts);
1005         }
1006         return true;
1007 }
1008
1009 void VideoWidget::store_pts(int64_t pts)
1010 {
1011         last_pts = pts;
1012         last_position = lrint(pts * double(video_timebase.num) / double(video_timebase.den) * 1000);
1013         post_to_main_thread([this, last_position{last_position.load()}] {
1014                 emit position_changed(last_position);
1015         });
1016 }
1017
1018 // Taken from Movit (see the comment there for explanation)
1019 float compute_chroma_offset(float pos, unsigned subsampling_factor, unsigned resolution)
1020 {
1021         float local_chroma_pos = (0.5 + pos * (subsampling_factor - 1)) / subsampling_factor;
1022         if (fabs(local_chroma_pos - 0.5) < 1e-10) {
1023                 // x + (-0) can be optimized away freely, as opposed to x + 0.
1024                 return -0.0;
1025         } else {
1026                 return (0.5 - local_chroma_pos) / resolution;
1027         }
1028 }
1029
1030 shared_ptr<VideoWidget::Frame> VideoWidget::alloc_frame(unsigned width, unsigned height, unsigned chroma_width, unsigned chroma_height)
1031 {
1032         lock_guard lock(freelist_mu);
1033         for (auto it = frame_freelist.begin(); it != frame_freelist.end(); ++it) {
1034                 if ((*it)->width == width &&
1035                     (*it)->height == height &&
1036                     (*it)->chroma_width == chroma_width &&
1037                     (*it)->chroma_height == chroma_height) {
1038                         Frame *frame = *it;
1039                         frame_freelist.erase(it);
1040                         return shared_ptr<Frame>{frame, free_frame};
1041                 }
1042         }
1043
1044         Frame *frame = new Frame;
1045         frame->owner = this;
1046         frame->width = width;
1047         frame->height = height;
1048         frame->chroma_width = chroma_width;
1049         frame->chroma_height = chroma_height;
1050
1051         size_t len = frame->width * frame->height + 2 * frame->chroma_width * frame->chroma_height;
1052
1053         while (!video_window->isValid()) {
1054                 usleep(100000);
1055         }
1056
1057         // Augh :-)
1058         mutex mu;
1059         condition_variable done_cv;
1060         bool done = false;
1061
1062         post_to_main_thread([this, &frame, len, &done, &mu, &done_cv]{
1063                 video_window->makeCurrent();
1064                 glCreateBuffers(1, &frame->pbo);
1065                 glNamedBufferStorage(frame->pbo, len, nullptr, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT);
1066                 frame->data = (uint8_t *)glMapNamedBufferRange(frame->pbo, 0, len, GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT);
1067                 video_window->doneCurrent();
1068
1069                 lock_guard lock(mu);
1070                 done = true;
1071                 done_cv.notify_all();
1072         });
1073         {
1074                 unique_lock lock(mu);
1075                 done_cv.wait(lock, [&done]{ return done; });
1076         }
1077
1078         return shared_ptr<Frame>{frame, free_frame};
1079 }
1080
1081 void VideoWidget::free_frame(VideoWidget::Frame *frame)
1082 {
1083         VideoWidget *self = frame->owner;
1084         lock_guard lock(self->freelist_mu);
1085         if (self->frame_freelist.size() >= 16) {
1086                 GLuint pbo = frame->pbo;
1087                 post_to_main_thread([self, pbo]{
1088                         self->video_window->makeCurrent();
1089                         glUnmapNamedBuffer(pbo);
1090                         glDeleteBuffers(1, &pbo);
1091                         self->video_window->doneCurrent();
1092                 });
1093                 delete self->frame_freelist.front();
1094                 self->frame_freelist.pop_front();
1095         }
1096         self->frame_freelist.push_back(frame);
1097 }
1098
1099 shared_ptr<VideoWidget::Frame> VideoWidget::make_video_frame(const AVFrame *frame)
1100 {
1101         AVFrameWithDeleter sw_frame;
1102
1103         if (frame->format == AV_PIX_FMT_VAAPI ||
1104             frame->format == AV_PIX_FMT_VDPAU) {
1105                 // Get the frame down to the CPU. (TODO: See if we can keep it
1106                 // on the GPU all the way, since it will be going up again later.
1107                 // However, this only works if the OpenGL GPU is the same one.)
1108                 sw_frame = av_frame_alloc_unique();
1109                 int err = av_hwframe_transfer_data(sw_frame.get(), frame, 0);
1110                 if (err != 0) {
1111                         fprintf(stderr, "%s: Cannot transfer hardware video frame to software.\n", pathname.c_str());
1112                 } else {
1113                         sw_frame->pts = frame->pts;
1114                         sw_frame->pkt_duration = frame->pkt_duration;
1115                         frame = sw_frame.get();
1116                 }
1117         }
1118
1119         if (sws_ctx == nullptr ||
1120             sws_last_width != frame->width ||
1121             sws_last_height != frame->height ||
1122             sws_last_src_format != frame->format) {
1123                 sws_dst_format = decide_dst_format(AVPixelFormat(frame->format));
1124                 sws_ctx.reset(
1125                         sws_getContext(frame->width, frame->height, AVPixelFormat(frame->format),
1126                                 frame->width, frame->height, sws_dst_format,
1127                                 SWS_BICUBIC, nullptr, nullptr, nullptr));
1128                 sws_last_width = frame->width;
1129                 sws_last_height = frame->height;
1130                 sws_last_src_format = frame->format;
1131         }
1132         if (sws_ctx == nullptr) {
1133                 fprintf(stderr, "Could not create scaler context\n");
1134                 abort();
1135         }
1136
1137         uint8_t *pic_data[4] = { nullptr, nullptr, nullptr, nullptr };
1138         int linesizes[4] = { 0, 0, 0, 0 };
1139         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(sws_dst_format);
1140
1141         shared_ptr<Frame> video_frame = alloc_frame(
1142                 frame->width,
1143                 frame->height,
1144                 AV_CEIL_RSHIFT(int(frame->width), desc->log2_chroma_w),
1145                 AV_CEIL_RSHIFT(int(frame->height), desc->log2_chroma_h));
1146
1147         // We always assume left chroma placement for now.
1148         video_window->set_cbcr_offset(
1149                 compute_chroma_offset(0.0f, 1 << desc->log2_chroma_w, video_frame->chroma_width),
1150                 compute_chroma_offset(0.5f, 1 << desc->log2_chroma_h, video_frame->chroma_height)
1151         );
1152
1153         pic_data[0] = video_frame->data;
1154         linesizes[0] = frame->width;
1155
1156         pic_data[1] = pic_data[0] + frame->width * frame->height;
1157         linesizes[1] = video_frame->chroma_width;
1158
1159         pic_data[2] = pic_data[1] + video_frame->chroma_width * video_frame->chroma_height;
1160         linesizes[2] = video_frame->chroma_width;
1161
1162         sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
1163
1164         video_frame->need_flush_len = video_frame->width * video_frame->height + 2 * video_frame->chroma_width * video_frame->chroma_height;
1165
1166         return video_frame;
1167 }
1168