-
Notifications
You must be signed in to change notification settings - Fork 4
/
dec-vpout.c
1354 lines (1089 loc) · 35.3 KB
/
dec-vpout.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
/*
* Tool to decode video from H.264 using V4L2 device and output to file, file
* descriptor or display on screen.
*
* Copyright 2019 RnD Center "ELVEES", JSC
*
* SPDX-License-Identifier: GPL-2.0
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <error.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <pthread.h>
#include <linux/videodev2.h>
#include <libavdevice/avdevice.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/dict.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/pixdesc.h>
#include <libavutil/time.h>
#include <libswresample/swresample.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_thread.h>
#include "drmdisplay.h"
#include "log.h"
#include "v4l2-utils.h"
#define ROUND_DOWN(x, a) ((x) & ~((a)-1))
#define NUM_BUFS 4
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720
/* Some evident defines */
#define MSEC_IN_SEC 1000
#define USEC_IN_SEC 1000000
#define NSEC_IN_SEC 1000000000
#define SDL_AUDIO_BUFFER_SIZE 1024
#define MAX_AUDIO_FRAME_SIZE 192000
/* TODO: It seems that video queue size of 128 MiB is too high */
#define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
#define MAX_VIDEOQ_SIZE (128 * 4 * 256 * 1024)
/* no AV correction is done if too big error */
#define AV_NOSYNC_THRESHOLD 10.0
#define AV_SYNC_THRESHOLD 0.01
enum nalu_type {
NALU_SLICE = 1,
NALU_DPA,
NALU_DPB,
NALU_DPC,
NALU_IDR,
NALU_SEI,
NALU_SPS,
NALU_PPS,
NALU_AUD,
NALU_EOSEQ,
NALU_EOSTREAM,
NALU_FILL,
NALU_NONE = 32
};
enum nalu_priority {
NALU_PRIOR_DISPOSABLE,
NALU_PRIOR_LOW,
NALU_PRIOR_HIGH,
NALU_PRIOR_HIGHEST,
NALU_PRIOR_NONE
};
typedef struct packet_queue {
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
int abort_request;
SDL_mutex *mutex;
SDL_cond *cond;
} packet_queue;
struct video_data {
int width;
int height;
uint64_t size;
uint64_t frames;
};
struct sink_data {
pthread_t thread;
int outfd;
bool m420_to_i420;
struct video_data vd;
struct SwsContext *dsc;
uint8_t *fb_addr;
AVFrame *fb_frame;
struct drmdisplay drm;
struct drmdisplay_dumb dumb;
};
struct src_data {
pthread_t thread;
AVBSFContext *bsf;
struct video_data vd;
};
static struct video_state {
AVFormatContext *ic;
unsigned int frames;
unsigned int loops;
int quit;
int m2mfd;
int audio_stream_number;
SDL_AudioDeviceID audio_dev;
double audio_clock;
AVStream *audio_st;
AVCodecContext *audio_ctx;
packet_queue audioq;
unsigned int audio_buf_size;
unsigned int audio_buf_index;
struct SwrContext *swr_ctx;
int video_stream_number;
AVStream *video_st;
packet_queue videoq;
/* pts of last decoded frame / predicted pts of next decoded frame */
double video_clock;
double frame_timer;
struct src_data src;
struct sink_data sink;
} video_state;
static AVPacket flush_pkt;
static struct m2m_buffer {
struct v4l2_buffer v4l2;
void *buf;
AVFrame *frame;
} out_bufs[NUM_BUFS], cap_bufs[NUM_BUFS];
static void init_video_state(struct video_state *vs)
{
vs->frame_timer = av_gettime_relative() / 1000000.0;
vs->loops = 1;
vs->frames = 0;
vs->video_stream_number = -1;
vs->audio_stream_number = -1;
vs->sink.outfd = -1;
}
static inline bool is_valid_cap_buf(unsigned const bufn)
{
return bufn < NUM_BUFS && cap_bufs[bufn].buf;
}
static inline bool is_valid_out_buf(unsigned const bufn)
{
return bufn < NUM_BUFS && out_bufs[bufn].buf;
}
static void packet_queue_init(packet_queue *q)
{
memset(q, 0, sizeof(packet_queue));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
}
static void packet_queue_abort(packet_queue *q)
{
SDL_LockMutex(q->mutex);
q->abort_request = 1;
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
}
static int packet_queue_get(packet_queue *q, AVPacket *pkt, int block)
{
AVPacketList *pkt1;
int ret;
SDL_LockMutex(q->mutex);
for (;;) {
if (q->abort_request) {
ret = -1;
break;
}
pkt1 = q->first_pkt;
if (pkt1) {
q->first_pkt = pkt1->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--;
q->size -= pkt1->pkt.size;
*pkt = pkt1->pkt;
av_free(pkt1);
ret = 1;
break;
} else if (!block) {
ret = 0;
break;
} else {
SDL_CondWait(q->cond, q->mutex);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
static int packet_queue_put(packet_queue *q, AVPacket *pkt)
{
AVPacketList *pkt1 = av_malloc(sizeof(AVPacketList));
if (!pkt1)
return -1;
pkt1->pkt = *pkt;
pkt1->next = NULL;
SDL_LockMutex(q->mutex);
if (q->abort_request)
return -1;
if (!q->last_pkt)
q->first_pkt = pkt1;
else
q->last_pkt->next = pkt1;
q->last_pkt = pkt1;
q->nb_packets++;
q->size += pkt1->pkt.size;
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
return 0;
}
static void configure_yuv2rgb(struct sink_data *sink)
{
int ret;
if (sink->vd.width > sink->fb_frame->width)
error(EXIT_FAILURE, 0, "Width of decoded frame is greater than framebuffer's");
if (sink->vd.height > sink->fb_frame->height)
error(EXIT_FAILURE, 0, "Height of decoded frame is greater than framebuffer's");
ret = av_image_fill_arrays(sink->fb_frame->data, sink->fb_frame->linesize, sink->fb_addr,
sink->fb_frame->format, sink->fb_frame->width, sink->fb_frame->height, 1);
if (ret < 0)
error(EXIT_FAILURE, 0, "Error image fill destination frame arrays");
if (sink->dsc)
sws_freeContext(sink->dsc);
/* Set the same resolution as in video because scaling isn't supported */
sink->dsc = sws_getContext(sink->vd.width, sink->vd.height, AV_PIX_FMT_M420,
sink->vd.width, sink->vd.height, sink->fb_frame->format,
SWS_BILINEAR, NULL, NULL, NULL);
if (!sink->dsc)
error(EXIT_FAILURE, 0, "Can't allocate swscale context");
}
static void configure_capture_frames(int width, int height)
{
int i;
for (i = 0; is_valid_cap_buf(i); i++) {
AVFrame *frame;
int rc;
if (!cap_bufs[i].frame) {
cap_bufs[i].frame = av_frame_alloc();
if (!cap_bufs[i].frame) error(EXIT_FAILURE, 0, "Not enough memory");
}
frame = cap_bufs[i].frame;
frame->format = AV_PIX_FMT_M420;
frame->width = width;
frame->height = height;
rc = av_image_fill_arrays(frame->data, frame->linesize, cap_bufs[i].buf,
frame->format, frame->width, frame->height, 16);
if (rc < 0)
error(EXIT_FAILURE, 0, "Error image fill capture arrays");
}
}
static void queue_outbuf(struct video_state *vs, unsigned const index, AVPacket *pkt)
{
AVRational v4l2_timebase = { 1, USEC_IN_SEC };
int64_t v4l2_pts = av_rescale_q(pkt->pts, vs->video_st->time_base, v4l2_timebase);
out_bufs[index].v4l2.timestamp.tv_usec = v4l2_pts % USEC_IN_SEC;
out_bufs[index].v4l2.timestamp.tv_sec = v4l2_pts / USEC_IN_SEC;
out_bufs[index].v4l2.bytesused = pkt->size;
out_bufs[index].v4l2.flags = (pkt->flags & AV_PKT_FLAG_KEY ? V4L2_BUF_FLAG_KEYFRAME : 0);
v4l2_qbuf(vs->m2mfd, &out_bufs[index].v4l2);
}
static void dequeue_outbuf(int const fd, unsigned const index)
{
v4l2_dqbuf(fd, &out_bufs[index].v4l2);
if (index != out_bufs[index].v4l2.index)
error(EXIT_FAILURE, 0, "Error index of buffer.");
}
static int m420_to_i420_write(const void *const buf, unsigned int const linesize,
unsigned int const height, int const outfd) {
size_t const size = linesize * height * 3 / 2;
uint8_t *const temp = malloc(size);
const uint8_t *const m420 = (const uint8_t *const)buf;
int rc;
size_t i, j, k;
if (!temp) error(EXIT_FAILURE, 0, "Can not allocate memory for convertion buffer");
/* Luma */
for (i = 0, j = 0; i < height; i += 2, j += 3)
memcpy(temp + i * linesize, m420 + j * linesize, 2 * linesize);
/* Chroma */
for (i = 0, j = 2; i < height / 2; i++, j += 3) {
const uint8_t *const inp = &m420[j * linesize];
uint8_t *const incb = temp + height * linesize + i * linesize / 2;
uint8_t *const incr = temp + height * linesize + height / 4 * linesize + i * linesize / 2;
for (k = 0; k < linesize / 2; k++) {
incb[k] = inp[2 * k];
incr[k] = inp[2 * k + 1];
}
}
rc = write(outfd, temp, size);
free(temp);
return rc;
}
static void process_capbuf(struct sink_data *sink, int capn, size_t size)
{
int rc = 0;
if (sink->outfd >= 0) {
if (sink->m420_to_i420)
rc = m420_to_i420_write(cap_bufs[capn].buf, sink->vd.width,
sink->vd.height, sink->outfd);
else
rc = write(sink->outfd, cap_bufs[capn].buf, size);
if (rc < 0)
error(EXIT_FAILURE, errno, "Can not write to output");
}
if (sink->fb_addr) {
AVFrame *src_frame = cap_bufs[capn].frame;
AVFrame *dst_frame = sink->fb_frame;
sws_scale(sink->dsc, (uint8_t const * const*)src_frame->data,
src_frame->linesize, 0, src_frame->height,
dst_frame->data, dst_frame->linesize);
}
}
static void start_capture(struct video_state *vs)
{
struct sink_data *sink = &vs->sink;
struct v4l2_format f_dst = {
.fmt = {
.pix = {
.width = sink->vd.width,
.height = sink->vd.height,
.pixelformat = V4L2_PIX_FMT_M420,
.field = V4L2_FIELD_ANY,
.bytesperline = sink->vd.width
}
}
};
struct v4l2_requestbuffers capreqbuf = {
.count = NUM_BUFS,
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_MMAP
};
int ret;
v4l2_setformat(vs->m2mfd, V4L2_BUF_TYPE_VIDEO_CAPTURE, &f_dst);
v4l2_pix_fmt_validate(&f_dst.fmt.pix, V4L2_PIX_FMT_M420, sink->vd.width,
sink->vd.height, sink->vd.width);
ret = ioctl(vs->m2mfd, VIDIOC_REQBUFS, &capreqbuf);
if (ret != 0) error(EXIT_FAILURE, errno, "Can not request capture buffers");
if (capreqbuf.count == 0) error(EXIT_FAILURE, 0, "Device gives zero capture buffers");
pr_debug("M2M: Got %d capture buffers", capreqbuf.count);
for (int i = 0; i < capreqbuf.count; ++i) {
struct v4l2_buffer *vbuf = &cap_bufs[i].v4l2;
memset(vbuf, 0, sizeof(*vbuf));
vbuf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vbuf->memory = V4L2_MEMORY_MMAP;
vbuf->index = i;
vbuf->flags = 0;
ret = ioctl(vs->m2mfd, VIDIOC_QUERYBUF, vbuf);
if (ret != 0) error(EXIT_FAILURE, errno, "Can not query capture buffer");
pr_debug("M2M: Got capture buffer #%u: length = %u", i, vbuf->length);
cap_bufs[i].buf = mmap(NULL, vbuf->length, PROT_READ | PROT_WRITE, MAP_SHARED,
vs->m2mfd, vbuf->m.offset);
if (cap_bufs[i].buf == MAP_FAILED) error(EXIT_FAILURE, errno, "Can not mmap capture buffer");
}
pr_verb("Allocating AVFrames for obtained buffers...");
configure_capture_frames(sink->vd.width, sink->vd.height);
for (int i = 0; i < capreqbuf.count; ++i) {
struct v4l2_buffer buf = {
.index = i,
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_MMAP
};
v4l2_qbuf(vs->m2mfd, &buf);
}
v4l2_streamon(vs->m2mfd, V4L2_BUF_TYPE_VIDEO_CAPTURE);
}
static void stop_capture(struct video_state *vs)
{
struct v4l2_requestbuffers req = {
.memory = V4L2_MEMORY_MMAP,
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.count = 0, /* 0 -> unmaps buffers from the driver */
};
int i, rc;
v4l2_streamoff(vs->m2mfd, V4L2_BUF_TYPE_VIDEO_CAPTURE);
for (i = 0; is_valid_cap_buf(i); ++i) {
struct v4l2_buffer *vbuf = &cap_bufs[i].v4l2;
if (munmap(cap_bufs[i].buf, vbuf->length) < 0)
error(EXIT_FAILURE, errno, "Can not unmap capture buffer");
cap_bufs[i].buf = NULL;
av_frame_free(&cap_bufs[i].frame);
cap_bufs[i].frame = NULL;
memset(&cap_bufs[i].v4l2, 0, sizeof(cap_bufs[i].v4l2));
}
rc = ioctl(vs->m2mfd, VIDIOC_REQBUFS, &req);
if (rc != 0) error(EXIT_FAILURE, errno, "Can not request capture buffers");
}
static void start_output(struct video_state *vs)
{
struct src_data *src = &vs->src;
struct v4l2_format f_src = {
.fmt = {
.pix = {
.width = src->vd.width,
.height = src->vd.height,
.pixelformat = V4L2_PIX_FMT_H264,
.field = V4L2_FIELD_ANY
}
}
};
struct v4l2_requestbuffers outreqbuf = {
.count = NUM_BUFS,
.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
.memory = V4L2_MEMORY_MMAP
};
int rc;
v4l2_setformat(vs->m2mfd, V4L2_BUF_TYPE_VIDEO_OUTPUT, &f_src);
v4l2_pix_fmt_validate(&f_src.fmt.pix, V4L2_PIX_FMT_H264, src->vd.width, src->vd.height, 0);
rc = ioctl(vs->m2mfd, VIDIOC_REQBUFS, &outreqbuf);
if (rc != 0) error(EXIT_FAILURE, errno, "Can not request output buffers");
if (outreqbuf.count == 0) error(EXIT_FAILURE, 0, "Device gives zero output buffers");
pr_debug("M2M: Got %d output buffers", outreqbuf.count);
for (int i = 0; i < outreqbuf.count; ++i) {
struct v4l2_buffer *vbuf = &out_bufs[i].v4l2;
memset(vbuf, 0, sizeof(*vbuf));
vbuf->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
vbuf->memory = V4L2_MEMORY_MMAP;
vbuf->index = i;
vbuf->flags = 0;
rc = ioctl(vs->m2mfd, VIDIOC_QUERYBUF, vbuf);
if (rc != 0) error(EXIT_FAILURE, errno, "Can not query output buffer");
pr_debug("M2M: Got output buffer #%u: length = %u", i, vbuf->length);
out_bufs[i].buf = mmap(NULL, vbuf->length, PROT_READ | PROT_WRITE, MAP_SHARED,
vs->m2mfd, vbuf->m.offset);
if (out_bufs[i].buf == MAP_FAILED) error(EXIT_FAILURE, errno, "Can not mmap output buffer");
}
v4l2_streamon(vs->m2mfd, V4L2_BUF_TYPE_VIDEO_OUTPUT);
}
static bool handle_event(struct video_state *vs)
{
struct v4l2_event evt = { 0 };
if (ioctl(vs->m2mfd, VIDIOC_DQEVENT, &evt) < 0)
error(EXIT_FAILURE, errno, "VIDIOC_DQEVENT error");
if (evt.type == V4L2_EVENT_SOURCE_CHANGE) {
pr_debug("Got event source change");
return true;
} else {
pr_warn("Got unexpected V4L2 event");
}
return false;
}
static double get_audio_clock(struct video_state *vs)
{
double pts = vs->audio_clock; /* maintained in the audio thread */
int hw_buf_size = vs->audio_buf_size - vs->audio_buf_index;
int bytes_per_sec = 0;
int n = vs->audio_ctx->channels * av_get_bytes_per_sample(vs->audio_ctx->sample_fmt);
if (vs->audio_st)
bytes_per_sec = vs->audio_ctx->sample_rate * n;
if (bytes_per_sec != 0)
pts -= (double)hw_buf_size / bytes_per_sec;
return pts;
}
static double synchronize_video(struct video_state *vs, double pts)
{
if (pts != 0) /* if we have pts, set video clock to it */
vs->video_clock = pts;
else /* if we aren't given a pts, set it to the clock */
pts = vs->video_clock;
return pts;
}
/*
* Limitations: The next parts work synchronously and can influence
* each other and overall test performance:
* - conversion from M420 to BGRA
* - m420_to_i420_write()
*/
static void *start_video_sink(void *arg)
{
struct video_state *vs = (struct video_state *)arg;
struct sink_data *sink = &vs->sink;
struct pollfd fds[1] = {
{ vs->m2mfd, POLLIN }
};
double frame_last_pts = 0;
double frame_last_delay = 10e-3;
int ret = 0;
while (1) {
ret = poll(fds, 1, MSEC_IN_SEC);
if (ret < 0)
error(EXIT_FAILURE, errno, "Poll error");
if (ret == 0) {
pr_info("decoded and presented frames: %llu", sink->vd.frames);
error(EXIT_FAILURE, 0, "Timeout waiting for capture buffer...");
}
if (fds[0].revents & POLLIN) {
struct v4l2_buffer buf = {
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_MMAP
};
if (ioctl(vs->m2mfd, VIDIOC_DQBUF, &buf)) {
if (errno != EPIPE)
error(EXIT_FAILURE, errno, "VIDIOC_DQBUF capture buf");
/* The last capture buffer has been already dequeued */
break;
}
/*
* The last buffer may be empty (with v4l2_buffer bytesused = 0)
* and in that case it must be ignored by the client, as it does
* not contain a decoded frame.
*/
if (buf.bytesused != 0) {
int64_t v4l2_pts = (int64_t)buf.timestamp.tv_sec * USEC_IN_SEC +
buf.timestamp.tv_usec;
double pts = synchronize_video(vs, v4l2_pts / (double)USEC_IN_SEC);
double delay = pts - frame_last_pts;
double actual_delay;
/* if incorrect delay, use previous one */
if (delay <= 0 || delay >= 1.0)
delay = frame_last_delay;
/* save for next time */
frame_last_delay = delay;
frame_last_pts = pts;
if (vs->audio_stream_number != -1) {
double diff = pts - get_audio_clock(vs);
/* Skip or repeat the frame. Take delay into account" */
double sync_threshold = (delay > AV_SYNC_THRESHOLD) ?
delay : AV_SYNC_THRESHOLD;
if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
if (diff <= -sync_threshold) /* speed up */
delay = 0;
else if (diff >= sync_threshold) /* slow down */
delay = 2 * delay;
}
}
vs->frame_timer += delay;
/* compute the REAL delay */
actual_delay = vs->frame_timer - (av_gettime_relative() / 1000000.0);
/*
* If delay is too small don't sleep, display frame now.
* TODO: Add ability to skip frame displaying.
*/
if (actual_delay > 0.010)
av_usleep(actual_delay * 1000000.0 + 0.5);
process_capbuf(sink, buf.index, buf.bytesused);
pr_verb("Frame %u is decoded and processed", sink->vd.frames);
sink->vd.size += buf.bytesused;
sink->vd.frames += 1;
}
if (!(buf.flags & V4L2_BUF_FLAG_LAST)) {
buf.flags = 0;
buf.bytesused = 0;
v4l2_qbuf(vs->m2mfd, &buf);
} else {
break;
}
}
}
return NULL;
}
static int filter_mp4toannexb(struct video_state *vs, AVPacket *pkt)
{
if (vs->src.bsf) {
int ret = av_bsf_send_packet(vs->src.bsf, pkt);
if (ret < 0)
error(EXIT_FAILURE, 0, "h264_mp4toannexb filter failed to send input packet");
while (!ret)
ret = av_bsf_receive_packet(vs->src.bsf, pkt);
if (ret < 0 && (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF))
error(EXIT_FAILURE, 0, "h264_mp4toannexb filter failed to receive output packet");
}
return 0;
}
static bool try_send_access_unit(struct video_state *vs, unsigned const outn)
{
static AVPacket pkt;
int ret;
if (out_bufs[outn].v4l2.flags & V4L2_BUF_FLAG_QUEUED) {
struct pollfd fds[1] = {
{ vs->m2mfd, POLLOUT | POLLPRI }
};
while (1) {
ret = poll(fds, 1, MSEC_IN_SEC);
if (ret < 0)
error(EXIT_FAILURE, errno, "Poll error");
if (ret == 0)
error(EXIT_FAILURE, 0, "Timeout waiting for event/output buffer...");
if (fds[0].revents & POLLPRI) {
if (handle_event(vs)) {
struct v4l2_format f_dst = { 0 };
/* Wait for capture draining before STREAM_OFF */
ret = pthread_join(vs->sink.thread, NULL);
if (ret != 0)
error(EXIT_FAILURE, errno, "Can not join thread");
stop_capture(vs);
v4l2_getformat(vs->m2mfd, V4L2_BUF_TYPE_VIDEO_CAPTURE, &f_dst);
vs->sink.vd.width = f_dst.fmt.pix.width;
vs->sink.vd.height = f_dst.fmt.pix.height;
if (vs->sink.fb_addr)
configure_yuv2rgb(&vs->sink);
start_capture(vs);
/* TODO: reallocate output buffers */
ret = pthread_create(&vs->sink.thread, NULL, start_video_sink, vs);
if (ret != 0)
error(EXIT_FAILURE, errno, "Can not create thread");
}
}
if (fds[0].revents & POLLOUT) {
dequeue_outbuf(vs->m2mfd, outn);
break;
}
}
}
ret = packet_queue_get(&vs->videoq, &pkt, 1);
if (ret < 0 || pkt.data == flush_pkt.data)
return false;
filter_mp4toannexb(vs, &pkt);
if (pkt.size > out_bufs[outn].v4l2.length)
error(EXIT_FAILURE, 0, "Insufficient size of output buffer");
memcpy(out_bufs[outn].buf, pkt.data, pkt.size);
if (pkt.size == 0)
pr_warn("Got empty encoded frame");
queue_outbuf(vs, outn, &pkt);
av_packet_unref(&pkt);
return true;
}
static inline struct timespec timespec_subtract(struct timespec const start,
struct timespec const stop)
{
struct timespec res = {
.tv_sec = stop.tv_sec - start.tv_sec,
.tv_nsec = stop.tv_nsec - start.tv_nsec
};
if (res.tv_nsec < 0) {
res.tv_sec -= 1;
res.tv_nsec += NSEC_IN_SEC;
}
return res;
}
static inline float timespec2float(struct timespec const t)
{
return t.tv_sec + (float)t.tv_nsec / 1e9;
}
static inline bool checklimit(unsigned const value, unsigned const limit)
{
return limit == 0 || value < limit;
}
static int v4l2_stop_decode(int fd)
{
struct v4l2_decoder_cmd cmd = {
.cmd = V4L2_DEC_CMD_STOP,
.flags = 0,
};
int ret;
ret = ioctl(fd, VIDIOC_DECODER_CMD, &cmd);
if (ret) {
/* DECODER_CMD is optional */
if (errno == ENOTTY) {
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
return ioctl(fd, VIDIOC_STREAMOFF, &type);
}
}
return 0;
}
#ifndef VERSION
#define VERSION "unversioned"
#endif
static void help(const char *program_name)
{
puts("dec-vpout " VERSION " \n");
printf("Synopsys: %s [options] decode-device\n", program_name);
puts("Options:");
puts(" -a arg Name of the audio device");
puts(" -d Display video on screen");
puts(" -f arg Output file descriptor number");
puts(" -h arg display this help and exit");
puts(" -i arg Input file name (defaults to stdin)");
puts(" -l arg Loop over input file (-1 means infinitely)");
puts(" -m arg Input video is in MP4 format");
puts(" -n arg Specify how many frames should be processed");
puts(" -o arg Output file name (takes precedence over -f)");
printf(" -s arg Set desired screen resolution [defaults to %dx%d]\n",
DEFAULT_WIDTH, DEFAULT_HEIGHT);
puts(" -t Transform video to I420 before output");
puts(" -v Be more verbose. Can be specified multiple times");
}
static int audio_decode_frame(struct video_state *vs, uint8_t *buf, int buf_size, double *pts)
{
static AVPacket pkt;
static uint8_t *audio_pkt_data;
static int audio_pkt_size;
static AVFrame audio_frame;
static uint8_t converted_data[(192000 * 3) / 2];
for (;;) {
while (audio_pkt_size > 0) {
int len1, len2, got_frame = 0, data_size = 0, bytes_per_sec;
len1 = avcodec_decode_audio4(vs->audio_ctx, &audio_frame, &got_frame, &pkt);
/* if error, skip frame */
if (len1 < 0) {
audio_pkt_size = 0;
break;
}
audio_pkt_data += len1;
audio_pkt_size -= len1;
if (got_frame) {
uint8_t *converted = converted_data;
data_size = av_samples_get_buffer_size(NULL, vs->audio_ctx->channels,
audio_frame.nb_samples,
AV_SAMPLE_FMT_FLT, 1);
if (data_size < 0)
error(EXIT_FAILURE, 0,
"Can not get buffer size for audio parameters");
if (data_size > buf_size)
error(EXIT_FAILURE, 0, "Insufficient buffer size");
len2 = swr_convert(vs->swr_ctx, &converted, audio_frame.nb_samples,
(const uint8_t**)&audio_frame.data[0],
audio_frame.nb_samples);
if (len2 < 0)
error(EXIT_FAILURE, 0, "Can not convert audio");
memcpy(buf, converted_data, data_size);
}
if (data_size == 0)
continue; /* No data yet, get more frames */
*pts = vs->audio_clock;
bytes_per_sec = av_get_bytes_per_sample(vs->audio_ctx->sample_fmt) *
vs->audio_ctx->channels * vs->audio_ctx->sample_rate;
vs->audio_clock += (double)data_size / bytes_per_sec;
/* We have data, return it and come back for more later */
return data_size;
}
if (pkt.data)
av_packet_unref(&pkt);
if (vs->quit)
return -1;
/* next packet */
if (packet_queue_get(&vs->audioq, &pkt, 1) < 0)
return -1;
audio_pkt_data = pkt.data;
audio_pkt_size = pkt.size;
/* update the audio clock */
if (pkt.pts != AV_NOPTS_VALUE)
vs->audio_clock = av_q2d(vs->audio_st->time_base) * pkt.pts;
}
error(EXIT_FAILURE, errno, "This code must be unreachable");
return -1;
}
static void audio_callback(void *userdata, uint8_t *stream, int len)
{
static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
struct video_state *vs = (struct video_state *)userdata;
int len1, audio_size;
double pts;
while (len > 0) {
if (vs->audio_buf_index >= vs->audio_buf_size) {
/* We have already sent all our data; get more */
audio_size = audio_decode_frame(vs, audio_buf, sizeof(audio_buf), &pts);
/* If error, output silence */
if (audio_size < 0) {
vs->audio_buf_size = SDL_AUDIO_BUFFER_SIZE;
memset(audio_buf, 0, vs->audio_buf_size);
} else {
vs->audio_buf_size = audio_size;
}
vs->audio_buf_index = 0;
}
len1 = vs->audio_buf_size - vs->audio_buf_index;
if (len1 > len)
len1 = len;
memcpy(stream, (uint8_t *)(audio_buf + vs->audio_buf_index), len1);
len -= len1;
stream += len1;
vs->audio_buf_index += len1;
}
}
static void *start_video_src(void *arg)
{
struct video_state *vs = (struct video_state *)arg;
unsigned int outn = 0;
for (;;) {
if (!try_send_access_unit(vs, outn))
break;
if (!is_valid_out_buf(++outn))
outn = 0;
}
return NULL;
}
static int is_realtime(AVFormatContext *s)
{
if(!strcmp(s->iformat->name, "rtp") ||
!strcmp(s->iformat->name, "rtsp") ||
!strcmp(s->iformat->name, "sdp"))
return 1;
if(s->pb && (!strncmp(s->filename, "rtp:", 4) ||
!strncmp(s->filename, "udp:", 4)))
return 1;
return 0;
}
static void read_av(struct video_state *vs)
{
unsigned int loop = 0;
AVPacket pkt1, *pkt = &pkt1;
int ret;
while (checklimit(loop, vs->loops) && checklimit(vs->src.vd.frames, vs->frames)) {
pr_verb("Loop #%u", loop);
if (vs->audioq.size > MAX_AUDIOQ_SIZE || vs->videoq.size > MAX_VIDEOQ_SIZE) {
SDL_Delay(10);
continue;
}
if ((ret = av_read_frame(vs->ic, pkt)) < 0) {
if (ret == AVERROR_EOF || avio_feof(vs->ic->pb)) {
/* if it is streaming don't restart video */
if (is_realtime(vs->ic) || !checklimit(++loop, vs->loops))
break;
if (strcmp("h264", vs->ic->iformat->name) == 0)
ret = avformat_seek_file(vs->ic, vs->video_stream_number,
0, 0, 0, AVSEEK_FLAG_BYTE);
else
ret = avformat_seek_file(vs->ic, -1, INT64_MIN,