-
Notifications
You must be signed in to change notification settings - Fork 15
/
vshow.c
185 lines (153 loc) · 4.48 KB
/
vshow.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
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
typedef enum PixelFormat PixelFormat;
struct Ctx
{
Display *display;
int screen;
Window window;
GC gc;
XVisualInfo vinfo;
XImage *image;
XShmSegmentInfo segment;
struct SwsContext *sws;
PixelFormat target_pixfmt;
AVPicture pic_target;
int v_width, v_height;
int curr_width, curr_height;
};
typedef struct Ctx Ctx;
void *vs_open (int v_width, int v_height)
{
Ctx *ctx = malloc(sizeof(Ctx));
ctx->v_width = v_width;
ctx->v_height = v_height;
// window
ctx->display = XOpenDisplay(0);
if (!ctx->display) {
fprintf(stderr, "%s: XOpenDisplay err\n", __func__);
exit(-1);
}
ctx->window = XCreateSimpleWindow(ctx->display, RootWindow(ctx->display, 0),
100, 100, v_width, v_height, 0, BlackPixel(ctx->display, 0),
WhitePixel(ctx->display, 0));
ctx->screen = 0;
ctx->gc = XCreateGC(ctx->display, ctx->window, 0, 0);
XMapWindow(ctx->display, ctx->window);
// current screen pix fmt
Window root;
unsigned int cx, cy, border, depth;
int x, y;
XGetGeometry(ctx->display, ctx->window, &root, &x, &y, &cx, &cy, &border, &depth);
// visual info
XMatchVisualInfo(ctx->display, ctx->screen, depth, DirectColor, &ctx->vinfo);
// image
ctx->image = XShmCreateImage(ctx->display, ctx->vinfo.visual, depth, ZPixmap, 0,
&ctx->segment, cx, cy);
if (!ctx->image) {
fprintf(stderr, "%s: can't XShmCreateImage !\n", __func__);
exit(-1);
}
ctx->segment.shmid = shmget(IPC_PRIVATE,
ctx->image->bytes_per_line * ctx->image->height,
IPC_CREAT | 0777);
if (ctx->segment.shmid < 0) {
fprintf(stderr, "%s: shmget err\n", __func__);
exit(-1);
}
ctx->segment.shmaddr = (char*)shmat(ctx->segment.shmid, 0, 0);
if (ctx->segment.shmaddr == (char*)-1) {
fprintf(stderr, "%s: shmat err\n", __func__);
exit(-1);
}
ctx->image->data = ctx->segment.shmaddr;
ctx->segment.readOnly = 0;
XShmAttach(ctx->display, &ctx->segment);
PixelFormat target_pix_fmt = PIX_FMT_NONE;
switch (ctx->image->bits_per_pixel) {
case 32:
target_pix_fmt = PIX_FMT_RGB32;
break;
case 24:
target_pix_fmt = PIX_FMT_RGB24;
break;
default:
break;
}
if (target_pix_fmt == PIX_FMT_NONE) {
fprintf(stderr, "%s: screen depth format err\n", __func__);
free(ctx);
return 0;
}
// sws
ctx->target_pixfmt = target_pix_fmt;
ctx->curr_width = cx;
ctx->curr_height = cy;
ctx->sws = sws_getContext(v_width, v_height, PIX_FMT_YUV420P,
cx, cy, target_pix_fmt,
SWS_FAST_BILINEAR, 0, 0, 0);
avpicture_alloc(&ctx->pic_target, target_pix_fmt, cx, cy);
XFlush(ctx->display);
return ctx;
}
int vs_close (void *ctx)
{
return 1;
}
int MIN(int a, int b)
{
return a < b ? a : b;
}
int vs_show (void *ctx, const uint8_t *const data[4], int stride[4])
{
// 首选检查 sws 是否有效, 根据当前窗口大小决定
Ctx *c = (Ctx*)ctx;
Window root;
int x, y;
unsigned int cx, cy, border, depth;
XGetGeometry(c->display, c->window, &root, &x, &y, &cx, &cy, &border, &depth);
if (cx != c->curr_width || cy != c->curr_height) {
avpicture_free(&c->pic_target);
sws_freeContext(c->sws);
c->sws = sws_getContext(c->v_width, c->v_height, PIX_FMT_YUV420P,
cx, cy, c->target_pixfmt,
SWS_FAST_BILINEAR, 0, 0, 0);
avpicture_alloc(&c->pic_target, c->target_pixfmt, cx, cy);
c->curr_width = cx;
c->curr_height = cy;
// re create image
XShmDetach(c->display, &c->segment);
shmdt(c->segment.shmaddr);
shmctl(c->segment.shmid, IPC_RMID, 0);
XDestroyImage(c->image);
c->image = XShmCreateImage(c->display, c->vinfo.visual, depth, ZPixmap, 0,
&c->segment, cx, cy);
c->segment.shmid = shmget(IPC_PRIVATE,
c->image->bytes_per_line * c->image->height,
IPC_CREAT | 0777);
c->segment.shmaddr = (char*)shmat(c->segment.shmid, 0, 0);
c->image->data = c->segment.shmaddr;
c->segment.readOnly = 0;
XShmAttach(c->display, &c->segment);
}
//
sws_scale(c->sws, data, stride, 0, c->v_height, c->pic_target.data, c->pic_target.linesize);
// cp to image
unsigned char *p = c->pic_target.data[0], *q = (unsigned char*)c->image->data;
int xx = MIN(c->image->bytes_per_line, c->pic_target.linesize[0]);
for (int i = 0; i < c->curr_height; i++) {
memcpy(q, p, xx);
p += c->image->bytes_per_line;
q += c->pic_target.linesize[0];
}
// 显示到 X 上
XShmPutImage(c->display, c->window, c->gc, c->image, 0, 0, 0, 0, c->curr_width, c->curr_height, 1);
return 1;
}