-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
1404 lines (1140 loc) · 27.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* V4L2 Codec decoding example application
* Kamil Debski <[email protected]>
*
* Main file of the application
*
* Copyright 2012 Samsung Electronics Co., Ltd.
* Copyright (c) 2015 Linaro Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <string.h>
#include <linux/input.h>
#include <linux/videodev2.h>
#include <media/msm_vidc.h>
#include <sys/ioctl.h>
#include <sys/signalfd.h>
#include <signal.h>
#include <poll.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "args.h"
#include "common.h"
#include "video.h"
#include "display.h"
#define DBG_TAG " main"
#define av_err(errnum, fmt, ...) \
err(fmt ": %s", ##__VA_ARGS__, av_err2str(errnum))
/* This is the size of the buffer for the compressed stream.
* It limits the maximum compressed frame size. */
#define STREAM_BUUFER_SIZE (1024 * 1024)
static void stream_close(struct instance *i);
static const int event_type[] = {
V4L2_EVENT_MSM_VIDC_FLUSH_DONE,
V4L2_EVENT_MSM_VIDC_PORT_SETTINGS_CHANGED_SUFFICIENT,
V4L2_EVENT_MSM_VIDC_PORT_SETTINGS_CHANGED_INSUFFICIENT,
V4L2_EVENT_MSM_VIDC_SYS_ERROR,
V4L2_EVENT_MSM_VIDC_HW_OVERLOAD,
V4L2_EVENT_MSM_VIDC_HW_UNSUPPORTED,
V4L2_EVENT_MSM_VIDC_RELEASE_BUFFER_REFERENCE,
V4L2_EVENT_MSM_VIDC_RELEASE_UNQUEUED_BUFFER,
};
static int
subscribe_events(struct instance *i)
{
const int n_events = sizeof(event_type) / sizeof(event_type[0]);
int idx;
for (idx = 0; idx < n_events; idx++) {
if (video_subscribe_event(i, event_type[idx]))
return -1;
}
return 0;
}
static struct fb *
find_fb(struct instance *i, int group, int index)
{
struct fb *fb;
list_for_each_entry(fb, &i->fb_list, link) {
if (fb->group == group && fb->index == index && fb->buffer)
return fb;
}
return NULL;
}
static int
restart_capture(struct instance *i)
{
struct video *vid = &i->video;
struct fb *fb, *next;
int n;
/*
* Destroy window buffers that are not in use by the
* wayland compositor; buffers in use will be destroyed
* when the release callback is called
*/
list_for_each_entry_safe(fb, next, &i->fb_list, link) {
if (!fb->busy)
fb_destroy(fb);
}
/* Stop capture and release buffers */
if (vid->cap_buf_cnt > 0 && video_stop_capture(i))
return -1;
/* Setup capture queue with new parameters */
if (video_setup_capture(i, 4, i->width, i->height))
return -1;
/* Start streaming */
if (video_stream(i, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
VIDIOC_STREAMON))
return -1;
/* Queue all capture buffers */
for (n = 0; n < vid->cap_buf_cnt; n++) {
if (video_queue_buf_cap(i, n))
return -1;
}
/*
* Recreate the window frame buffers
*/
i->group++;
return 0;
}
static const char *
colorspace_to_string(int cspace)
{
switch (cspace) {
case MSM_VIDC_BT709_5:
return "bt709";
case MSM_VIDC_UNSPECIFIED:
return "unspecified";
case MSM_VIDC_BT470_6_M:
return "bt470m";
case MSM_VIDC_BT601_6_625:
return "bt601/625";
case MSM_VIDC_BT601_6_525:
return "bt601/525";
case MSM_VIDC_SMPTE_240M:
return "smpte240m";
case MSM_VIDC_GENERIC_FILM:
return "generic";
case MSM_VIDC_BT2020:
return "bt2020";
case MSM_VIDC_RESERVED_1:
return "reserved1";
case MSM_VIDC_RESERVED_2:
return "reserved2";
}
return "unknown";
}
static const char *
depth_to_string(int depth)
{
switch (depth) {
case MSM_VIDC_BIT_DEPTH_8:
return "8bits";
case MSM_VIDC_BIT_DEPTH_10:
return "10bits";
case MSM_VIDC_BIT_DEPTH_UNSUPPORTED:
return "unsupported";
}
return "unknown";
}
static const char *
pic_struct_to_string(int pic)
{
switch (pic) {
case MSM_VIDC_PIC_STRUCT_PROGRESSIVE:
return "progressive";
case MSM_VIDC_PIC_STRUCT_MAYBE_INTERLACED:
return "interlaced";
}
return "unknown";
}
static int
handle_video_event(struct instance *i)
{
struct v4l2_event event;
if (video_dequeue_event(i, &event))
return -1;
switch (event.type) {
case V4L2_EVENT_MSM_VIDC_PORT_SETTINGS_CHANGED_INSUFFICIENT: {
unsigned int *ptr = (unsigned int *)event.u.data;
unsigned int height = ptr[0];
unsigned int width = ptr[1];
info("Port Reconfig received insufficient, new size %ux%u",
width, height);
if (ptr[2] & V4L2_EVENT_BITDEPTH_FLAG) {
enum msm_vidc_pixel_depth depth = ptr[3];
info(" bit depth changed to %s",
depth_to_string(depth));
switch (depth) {
case MSM_VIDC_BIT_DEPTH_10:
i->depth = 10;
break;
case MSM_VIDC_BIT_DEPTH_8:
i->depth = 8;
break;
default:
i->depth = 0;
break;
}
}
if (ptr[2] & V4L2_EVENT_PICSTRUCT_FLAG) {
unsigned int pic_struct = ptr[4];
info(" interlacing changed to %s",
pic_struct_to_string(pic_struct));
if (pic_struct == MSM_VIDC_PIC_STRUCT_MAYBE_INTERLACED)
i->interlaced = 1;
else
i->interlaced = 0;
}
if (ptr[2] & V4L2_EVENT_COLOUR_SPACE_FLAG) {
unsigned int cspace = ptr[5];
info(" colorspace changed to %s",
colorspace_to_string(cspace));
}
i->width = width;
i->height = height;
i->reconfigure_pending = 1;
/* flush capture queue, we will reconfigure it when flush
* done event is received */
video_flush(i, V4L2_QCOM_CMD_FLUSH_CAPTURE);
break;
}
case V4L2_EVENT_MSM_VIDC_PORT_SETTINGS_CHANGED_SUFFICIENT:
dbg("Setting changed sufficient");
break;
case V4L2_EVENT_MSM_VIDC_FLUSH_DONE: {
unsigned int *ptr = (unsigned int *)event.u.data;
unsigned int flags = ptr[0];
if (flags & V4L2_QCOM_CMD_FLUSH_CAPTURE)
dbg("Flush Done received on CAPTURE queue");
if (flags & V4L2_QCOM_CMD_FLUSH_OUTPUT)
dbg("Flush Done received on OUTPUT queue");
if (i->reconfigure_pending) {
dbg("Reconfiguring output");
restart_capture(i);
i->reconfigure_pending = 0;
}
break;
}
case V4L2_EVENT_MSM_VIDC_SYS_ERROR:
dbg("SYS Error received");
break;
case V4L2_EVENT_MSM_VIDC_HW_OVERLOAD:
dbg("HW Overload received");
break;
case V4L2_EVENT_MSM_VIDC_HW_UNSUPPORTED:
dbg("HW Unsupported received");
break;
case V4L2_EVENT_MSM_VIDC_RELEASE_BUFFER_REFERENCE:
dbg("Release buffer reference");
break;
case V4L2_EVENT_MSM_VIDC_RELEASE_UNQUEUED_BUFFER:
dbg("Release unqueued buffer");
break;
default:
dbg("unknown event type occurred %x", event.type);
break;
}
return 0;
}
static void
cleanup(struct instance *i)
{
stream_close(i);
if (i->window)
window_destroy(i->window);
if (i->display)
display_destroy(i->display);
if (i->sigfd != 1)
close(i->sigfd);
if (i->video.fd)
video_close(i);
}
struct ts_entry {
uint64_t pts;
uint64_t dts;
uint64_t duration;
uint64_t base;
struct list_head link;
};
#define TIMESTAMP_NONE ((uint64_t)-1)
static struct ts_entry *
ts_insert(struct video *vid, uint64_t pts, uint64_t dts, uint64_t duration,
uint64_t base)
{
struct ts_entry *l;
l = malloc(sizeof (*l));
if (!l)
return NULL;
l->pts = pts;
l->dts = dts;
l->duration = duration;
l->base = base;
list_add_tail(&l->link, &vid->pending_ts_list);
return l;
}
static void
ts_remove(struct ts_entry *l)
{
list_del(&l->link);
free(l);
}
static int
parse_frame(struct instance *i, AVPacket *pkt)
{
int ret;
if (!i->bsf_data_pending) {
ret = av_read_frame(i->avctx, pkt);
if (ret < 0)
return ret;
if (pkt->stream_index != i->stream->index) {
av_packet_unref(pkt);
return AVERROR(EAGAIN);
}
if (i->bsf) {
ret = av_bsf_send_packet(i->bsf, pkt);
if (ret < 0)
return ret;
i->bsf_data_pending = 1;
}
}
if (i->bsf) {
ret = av_bsf_receive_packet(i->bsf, pkt);
if (ret == AVERROR(EAGAIN))
i->bsf_data_pending = 0;
if (ret < 0)
return ret;
}
return 0;
}
static void
finish(struct instance *i)
{
pthread_mutex_lock(&i->lock);
i->finish = 1;
pthread_cond_signal(&i->cond);
pthread_mutex_unlock(&i->lock);
}
static int
send_eos(struct instance *i, int buf_index)
{
struct video *vid = &i->video;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
if (video_queue_buf_out(i, buf_index, 0,
V4L2_QCOM_BUF_FLAG_EOS |
V4L2_QCOM_BUF_TIMESTAMP_INVALID, tv) < 0)
return -1;
vid->out_buf_flag[buf_index] = 1;
return 0;
}
static char *
dump_pkt(const uint8_t *data, size_t size)
{
static char *buf;
static size_t buf_size;
size_t s = size * 3 + 1;
if (!buf || buf_size < s) {
char *old = buf;
s = (s + 4095) & ~4095;
buf = realloc(old, s);
if (!buf) {
free(old);
return NULL;
}
buf_size = s;
}
for (size_t i = 0; i < size; i++) {
sprintf(buf + i * 3, "%c%02x",
i % 32 == 0 ? '\n' : ' ', data[i]);
}
buf[s - 1] = 0;
return buf;
}
/*
* Escape start codes in BDU
*/
static int
rbdu_escape(uint8_t *dst, int dst_size, const uint8_t *src, int src_size)
{
uint8_t *dstp = dst;
const uint8_t *srcp = src;
const uint8_t *end = src + src_size;
int count = 0;
while (srcp < end) {
if (count == 2 && *srcp <= 0x03) {
*dstp++ = 0x03;
count = 0;
}
if (*srcp == 0)
count++;
else
count = 0;
*dstp++ = *srcp++;
}
return dstp - dst;
}
/*
* Transform RBDU (raw bitstream decodable units)
* into an EBDU (encapsulated bitstream decodable units)
*/
static int
vc1_write_bdu(uint8_t *dst, int dst_size,
uint8_t *bdu, int bdu_size,
uint8_t type)
{
int len;
/* add start code */
dst[0] = 0x00;
dst[1] = 0x00;
dst[2] = 0x01;
dst[3] = type;
len = 4;
/* escape start codes */
len += rbdu_escape(dst + len, dst_size - len, bdu, bdu_size);
/* add flushing byte at the end of the BDU */
dst[len++] = 0x80;
return len;
}
static int
vc1_find_sc(const uint8_t *data, int size)
{
for (int i = 0; i < size - 4; i++) {
if (data[i + 0] == 0x00 &&
data[i + 1] == 0x00 &&
data[i + 2] == 0x01)
return i;
}
return -1;
}
static int
write_sequence_header_vc1(struct instance *i, uint8_t *data, int size)
{
AVCodecParameters *codecpar = i->stream->codecpar;
int n;
if (codecpar->extradata_size == 0) {
dbg("no codec data, skip sequence header generation");
return 0;
}
if (codecpar->extradata_size == 4 || codecpar->extradata_size == 5) {
/* Simple/Main Profile ASF header */
return vc1_write_bdu(data, size,
codecpar->extradata,
codecpar->extradata_size,
0x0f);
}
if (codecpar->extradata_size == 36 && codecpar->extradata[3] == 0xc5) {
/* Annex L Sequence Layer */
if (size < codecpar->extradata_size)
return -1;
memcpy(data, codecpar->extradata, codecpar->extradata_size);
return codecpar->extradata_size;
}
n = vc1_find_sc(codecpar->extradata, codecpar->extradata_size);
if (n >= 0) {
/* BDU in header */
if (size < codecpar->extradata_size - n)
return -1;
memcpy(data, codecpar->extradata + n,
codecpar->extradata_size - n);
return codecpar->extradata_size - n;
}
err("cannot parse VC1 codec data");
return -1;
}
static int
write_sequence_header(struct instance *i, uint8_t *data, int size)
{
AVCodecParameters *codecpar = i->stream->codecpar;
switch (codecpar->codec_id) {
case AV_CODEC_ID_WMV3:
case AV_CODEC_ID_VC1:
return write_sequence_header_vc1(i, data, size);
default:
return 0;
}
}
static int
send_pkt(struct instance *i, int buf_index, AVPacket *pkt)
{
struct video *vid = &i->video;
struct timeval tv;
uint64_t pts, dts, duration, start_time;
int flags;
int size;
uint8_t *data;
const char *hex;
AVRational vid_timebase;
AVRational v4l_timebase = { 1, 1000000 };
AVCodecParameters *codecpar = i->stream->codecpar;
data = (uint8_t *)vid->out_buf_addr[buf_index];
size = 0;
if (i->need_header) {
int n = write_sequence_header(i, data, vid->out_buf_size);
if (n > 0)
size += n;
switch (codecpar->codec_id) {
case AV_CODEC_ID_WMV3:
case AV_CODEC_ID_VC1:
if (vc1_find_sc(pkt->data, MIN(10, pkt->size)) < 0)
i->insert_sc = 1;
break;
default:
break;
}
i->need_header = 0;
}
if ((codecpar->codec_id == AV_CODEC_ID_WMV3 ||
codecpar->codec_id == AV_CODEC_ID_VC1) &&
i->insert_sc) {
size += vc1_write_bdu(data + size, vid->out_buf_size - size,
pkt->data, pkt->size, 0x0d);
} else {
memcpy(data + size, pkt->data, pkt->size);
size += pkt->size;
}
flags = 0;
vid_timebase = i->stream->time_base;
start_time = 0;
if (i->stream->start_time != AV_NOPTS_VALUE)
start_time = av_rescale_q(i->stream->start_time,
vid_timebase, v4l_timebase);
pts = TIMESTAMP_NONE;
if (pkt->pts != AV_NOPTS_VALUE)
pts = av_rescale_q(pkt->pts, vid_timebase, v4l_timebase);
dts = TIMESTAMP_NONE;
if (pkt->dts != AV_NOPTS_VALUE)
dts = av_rescale_q(pkt->dts, vid_timebase, v4l_timebase);
duration = TIMESTAMP_NONE;
if (pkt->duration) {
duration = av_rescale_q(pkt->duration,
vid_timebase, v4l_timebase);
}
if (debug_level > 3)
hex = dump_pkt(data, size);
else
hex = "";
dbg("input size=%d pts=%" PRIi64 " dts=%" PRIi64 " duration=%" PRIu64
" start_time=%" PRIi64 "%s", size, pts, dts, duration,
start_time, hex);
if (pts != TIMESTAMP_NONE) {
tv.tv_sec = pts / 1000000;
tv.tv_usec = pts % 1000000;
} else {
flags |= V4L2_QCOM_BUF_TIMESTAMP_INVALID;
tv.tv_sec = 0;
tv.tv_usec = 0;
}
if ((pkt->flags & AV_PKT_FLAG_KEY) &&
pts != TIMESTAMP_NONE && dts != TIMESTAMP_NONE)
vid->pts_dts_delta = pts - dts;
if (video_queue_buf_out(i, buf_index, size, flags, tv) < 0)
return -1;
pthread_mutex_lock(&i->lock);
ts_insert(vid, pts, dts, duration, start_time);
pthread_mutex_unlock(&i->lock);
vid->out_buf_flag[buf_index] = 1;
return 0;
}
static int
get_buffer_unlocked(struct instance *i)
{
struct video *vid = &i->video;
for (int n = 0; n < vid->out_buf_cnt; n++) {
if (!vid->out_buf_flag[n])
return n;
}
return -1;
}
/* This threads is responsible for parsing the stream and
* feeding video decoder with consecutive frames to decode */
static void *
parser_thread_func(void *args)
{
struct instance *i = (struct instance *)args;
AVPacket pkt;
int buf, parse_ret;
dbg("Parser thread started");
av_init_packet(&pkt);
while (1) {
parse_ret = parse_frame(i, &pkt);
if (parse_ret == AVERROR(EAGAIN))
continue;
buf = -1;
pthread_mutex_lock(&i->lock);
while (!i->finish && (buf = get_buffer_unlocked(i)) < 0)
pthread_cond_wait(&i->cond, &i->lock);
pthread_mutex_unlock(&i->lock);
if (buf < 0) {
/* decoding stopped before parsing ended, abort */
break;
}
if (parse_ret < 0) {
if (parse_ret == AVERROR_EOF)
dbg("Queue end of stream");
else
av_err(parse_ret, "Parsing failed");
send_eos(i, buf);
break;
}
if (send_pkt(i, buf, &pkt) < 0)
break;
av_packet_unref(&pkt);
}
av_packet_unref(&pkt);
dbg("Parser thread finished");
return NULL;
}
static void
buffer_released(struct fb *fb, void *data)
{
struct instance *i = data;
int n = fb->index;
if (fb->group != i->group) {
fb_destroy(fb);
return;
}
if (!i->reconfigure_pending)
video_queue_buf_cap(i, n);
}
static struct fb *
get_fb(struct instance *i, int n)
{
struct video *vid = &i->video;
struct fb *fb;
fb = find_fb(i, i->group, n);
if (!fb) {
fb = window_create_buffer(i->window, i->group, n,
vid->cap_buf_fd[n],
vid->cap_buf_format,
vid->cap_w, vid->cap_h,
vid->cap_planes_count,
vid->cap_plane_off,
vid->cap_plane_stride);
if (fb)
list_add_tail(&fb->link, &i->fb_list);
}
return fb;
}
static int
handle_video_capture(struct instance *i)
{
struct video *vid = &i->video;
struct timeval tv;
uint32_t flags;
uint64_t pts;
unsigned int bytesused;
struct msm_vidc_extradata_header *extradata;
bool busy;
int ret, n;
/* capture buffer is ready */
ret = video_dequeue_capture(i, &n, &bytesused, &flags, &tv, &extradata);
if (ret < 0) {
err("dequeue capture buffer fail");
return ret;
}
if (flags & V4L2_QCOM_BUF_TIMESTAMP_INVALID)
pts = TIMESTAMP_NONE;
else
pts = ((uint64_t)tv.tv_sec) * 1000000 + tv.tv_usec;
busy = false;
if (bytesused > 0) {
struct ts_entry *l, *min = NULL;
int pending = 0;
vid->total_captured++;
pthread_mutex_lock(&i->lock);
/* PTS are expected to be monotonically increasing,
* so when unknown use the lowest pending DTS */
list_for_each_entry(l, &vid->pending_ts_list, link) {
if (l->dts == TIMESTAMP_NONE)
continue;
if (min == NULL || min->dts > l->dts)
min = l;
pending++;
}
if (min) {
dbg("pending %d min pts %" PRIi64
" dts %" PRIi64
" duration %" PRIi64, pending,
min->pts, min->dts, min->duration);
}
if (pts == TIMESTAMP_NONE) {
dbg("no pts on frame");
if (min && vid->pts_dts_delta != TIMESTAMP_NONE) {
dbg("reuse dts %" PRIu64
" delta %" PRIu64,
min->dts, vid->pts_dts_delta);
pts = min->dts + vid->pts_dts_delta;
}
}
if (pts == TIMESTAMP_NONE) {
if (min && vid->cap_last_pts != TIMESTAMP_NONE)
pts = vid->cap_last_pts + min->duration;
else
pts = 0;
dbg("guessing pts %" PRIu64, pts);
}
vid->cap_last_pts = pts;
if (min != NULL) {
pts -= min->base;
ts_remove(min);
}
pthread_mutex_unlock(&i->lock);
if (i->window) {
struct fb *fb = get_fb(i, n);
if (!fb) {
err("could not get framebuffer for "
"video buffer %d", n);
return -1;
}
info("show buffer pts=%" PRIu64, pts);
fb_apply_extradata(fb, extradata);
window_show_buffer(i->window, fb,
buffer_released, i);
busy = true;
}
i->prerolled = 1;
}
if (!busy && !i->reconfigure_pending)
video_queue_buf_cap(i, n);
if (flags & V4L2_QCOM_BUF_FLAG_EOS) {
info("End of stream");
finish(i);
}
return 0;
}
static int
handle_video_output(struct instance *i)
{
struct video *vid = &i->video;
int ret, n;
ret = video_dequeue_output(i, &n);
if (ret < 0) {
err("dequeue output buffer fail");
return ret;
}
pthread_mutex_lock(&i->lock);
vid->out_buf_flag[n] = 0;
pthread_cond_signal(&i->cond);
pthread_mutex_unlock(&i->lock);
return 0;
}
static int
handle_signal(struct instance *i)
{
struct signalfd_siginfo siginfo;
sigset_t sigmask;
if (read(i->sigfd, &siginfo, sizeof (siginfo)) < 0) {
perror("signalfd/read");
return -1;
}
sigemptyset(&sigmask);
sigaddset(&sigmask, siginfo.ssi_signo);
sigprocmask(SIG_UNBLOCK, &sigmask, NULL);
finish(i);
return 0;
}
static int
setup_signal(struct instance *i)
{
sigset_t sigmask;
int fd;
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGINT);
sigaddset(&sigmask, SIGTERM);
fd = signalfd(-1, &sigmask, SFD_CLOEXEC);
if (fd < 0) {
perror("signalfd");
return -1;
}
sigprocmask(SIG_BLOCK, &sigmask, NULL);
i->sigfd = fd;
return 0;
}
enum {
EV_VIDEO,
EV_DISPLAY,
EV_STDIN,
EV_SIGNAL,
EV_COUNT
};
static int
kbd_init(struct instance *i)
{
struct termios newt;
if (tcgetattr(STDIN_FILENO, &i->stdin_termios) < 0)
return -1;
newt = i->stdin_termios;
newt.c_lflag &= ~ICANON;
newt.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSANOW, &newt) < 0)
return -1;
i->stdin_valid = 1;
return STDIN_FILENO;
}
static int
kbd_handle_key(struct instance *i)
{
uint8_t key[3];
int ret;
ret = read(STDIN_FILENO, key, 3);
if (ret < 0)
return -1;
if (key[0] == 's') {
info("Frame Step");
i->prerolled = 0;
}
return 0;
}
static void
kbd_shutdown(struct instance *i)
{
if (i->stdin_valid)
tcsetattr(STDIN_FILENO, TCSANOW, &i->stdin_termios);
}
void main_loop(struct instance *i)
{
struct video *vid = &i->video;
struct wl_display *wl_display = NULL;
struct pollfd pfd[EV_COUNT];
int ev[EV_COUNT];