forked from hzeller/rpi-rgb-led-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video-viewer.cc
292 lines (250 loc) · 8.4 KB
/
video-viewer.cc
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
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
//
// Quick hack based on ffmpeg
// tutorial http://dranger.com/ffmpeg/tutorial01.html
// in turn based on a tutorial by
// Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)
//
// Ancient AV versions forgot to set this.
#define __STDC_CONSTANT_MACROS
// libav: "U NO extern C in header ?"
extern "C" {
# include <libavcodec/avcodec.h>
# include <libavformat/avformat.h>
# include <libswscale/swscale.h>
}
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "led-matrix.h"
#include "content-streamer.h"
using rgb_matrix::FrameCanvas;
using rgb_matrix::RGBMatrix;
using rgb_matrix::StreamWriter;
using rgb_matrix::StreamIO;
volatile bool interrupt_received = false;
static void InterruptHandler(int) {
interrupt_received = true;
}
// compatibility with newer API
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
# define av_frame_alloc avcodec_alloc_frame
# define av_frame_free avcodec_free_frame
#endif
struct LedPixel {
uint8_t r, g, b;
};
void CopyFrame(AVFrame *pFrame, FrameCanvas *canvas) {
// Write pixel data
const int height = canvas->height();
const int width = canvas->width();
for(int y = 0; y < height; ++y) {
LedPixel *pix = (LedPixel*) (pFrame->data[0] + y*pFrame->linesize[0]);
for(int x = 0; x < width; ++x, ++pix) {
canvas->SetPixel(x, y, pix->r, pix->g, pix->b);
}
}
}
static int usage(const char *progname) {
fprintf(stderr, "usage: %s [options] <video>\n", progname);
fprintf(stderr, "Options:\n"
"\t-O<streamfile> : Output to stream-file instead of matrix (don't need to be root).\n"
"\t-v : verbose.\n");
fprintf(stderr, "\nGeneral LED matrix options:\n");
rgb_matrix::PrintMatrixFlags(stderr);
return 1;
}
int main(int argc, char *argv[]) {
RGBMatrix::Options matrix_options;
rgb_matrix::RuntimeOptions runtime_opt;
if (!rgb_matrix::ParseOptionsFromFlags(&argc, &argv,
&matrix_options, &runtime_opt)) {
return usage(argv[0]);
}
bool verbose = false;
const char *stream_output = NULL;
int opt;
while ((opt = getopt(argc, argv, "vO:R:L")) != -1) {
switch (opt) {
case 'v':
verbose = true;
break;
case 'O':
stream_output = strdup(optarg);
break;
case 'L':
fprintf(stderr, "-L is deprecated. Use\n\t--led-pixel-mapper=\"U-mapper\" --led-chain=4\ninstead.\n");
return 1;
break;
case 'R':
fprintf(stderr, "-R is deprecated. "
"Use --led-pixel-mapper=\"Rotate:%s\" instead.\n", optarg);
return 1;
break;
default:
return usage(argv[0]);
}
}
if (optind >= argc) {
fprintf(stderr, "Expected video filename.\n");
return usage(argv[0]);
}
// Initalizing these to NULL prevents segfaults!
AVFormatContext *pFormatCtx = NULL;
int i, videoStream;
AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVFrame *pFrameRGB = NULL;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer = NULL;
struct SwsContext *sws_ctx = NULL;
const char *movie_file = argv[optind];
// Register all formats and codecs
av_register_all();
avformat_network_init();
// Open video file
if(avformat_open_input(&pFormatCtx, movie_file, NULL, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
return -1; // Couldn't find stream information
// Dump information about file onto standard error
if (verbose) {
av_dump_format(pFormatCtx, 0, movie_file, 0);
}
long frame_count = 0;
runtime_opt.do_gpio_init = (stream_output == NULL);
RGBMatrix *matrix = CreateMatrixFromOptions(matrix_options, runtime_opt);
if (matrix == NULL) {
return 1;
}
FrameCanvas *offscreen_canvas = matrix->CreateFrameCanvas();
StreamIO *stream_io = NULL;
StreamWriter *stream_writer = NULL;
if (stream_output) {
int fd = open(stream_output, O_CREAT|O_WRONLY, 0644);
if (fd < 0) {
perror("Couldn't open output stream");
return 1;
}
stream_io = new rgb_matrix::FileStreamIO(fd);
stream_writer = new StreamWriter(stream_io);
}
// Find the first video stream
videoStream=-1;
for (i=0; i < (int)pFormatCtx->nb_streams; ++i) {
if (pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
}
if (videoStream == -1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec;
double fps = av_q2d(pFormatCtx->streams[videoStream]->avg_frame_rate);
if (fps < 0) {
fps = 1.0 / av_q2d(pFormatCtx->streams[videoStream]->codec->time_base);
}
if (verbose) fprintf(stderr, "FPS: %f\n", fps);
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtxOrig->codec_id);
if (pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1;
}
// Copy context
pCodecCtx = avcodec_alloc_context3(pCodec);
if (avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
fprintf(stderr, "Couldn't copy codec context");
return -1;
}
// Open codec
if (avcodec_open2(pCodecCtx, pCodec, NULL)<0)
return -1;
// Allocate video frame
pFrame=av_frame_alloc();
// Allocate an AVFrame structure
pFrameRGB=av_frame_alloc();
if (pFrameRGB==NULL)
return -1;
// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height);
// initialize SWS context for software scaling
sws_ctx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
matrix->width(), matrix->height(),
AV_PIX_FMT_RGB24,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
if (sws_ctx == 0) {
fprintf(stderr, "Trouble doing scaling to %dx%d :(\n",
matrix->width(), matrix->height());
return 1;
}
signal(SIGTERM, InterruptHandler);
signal(SIGINT, InterruptHandler);
const int frame_wait_micros = 1e6 / fps;
while (!interrupt_received && av_read_frame(pFormatCtx, &packet) >= 0) {
// Is this a packet from the video stream?
if (packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
// Did we get a video frame?
if (frameFinished) {
// Convert the image from its native format to RGB
sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
pFrame->linesize, 0, pCodecCtx->height,
pFrameRGB->data, pFrameRGB->linesize);
CopyFrame(pFrameRGB, offscreen_canvas);
frame_count++;
if (stream_writer) {
stream_writer->Stream(*offscreen_canvas, frame_wait_micros);
} else {
offscreen_canvas = matrix->SwapOnVSync(offscreen_canvas);
}
}
if (!stream_writer) usleep(frame_wait_micros);
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
if (interrupt_received) {
// Feedback for Ctrl-C, but most importantly, force a newline
// at the output, so that commandline-shell editing is not messed up.
fprintf(stderr, "Got interrupt. Exiting\n");
}
// Free the RGB image
av_free(buffer);
av_frame_free(&pFrameRGB);
// Free the YUV frame
av_frame_free(&pFrame);
// Close the codecs
avcodec_close(pCodecCtx);
avcodec_close(pCodecCtxOrig);
// Close the video file
avformat_close_input(&pFormatCtx);
delete stream_writer;
delete stream_io;
fprintf(stderr, "Total of %ld frames decoded\n", frame_count);
return 0;
}