File: videoplugin.c

package info (click to toggle)
gr-framework 0.73.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,600 kB
  • sloc: ansic: 87,413; cpp: 45,348; objc: 3,057; javascript: 2,647; makefile: 1,129; python: 1,000; sh: 991; yacc: 855; pascal: 554; fortran: 228
file content (339 lines) | stat: -rw-r--r-- 7,860 bytes parent folder | download
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
#if !defined(NO_AV) && !defined(NO_CAIRO)

#define _POSIX_C_SOURCE 200112L

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif

#endif

#include "gks.h"
#include "gkscore.h"

#if !defined(NO_AV)
#include "vc.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _WIN32

#include <windows.h>
#ifndef DLLEXPORT
#define DLLEXPORT __declspec(dllexport)
#endif

#endif

DLLEXPORT void gks_videoplugin(int fctid, int dx, int dy, int dimx, int *i_arr, int len_f_arr_1, double *f_arr_1,
                               int len_f_arr_2, double *f_arr_2, int len_c_arr, char *c_arr, void **ptr);

#ifdef __cplusplus
}
#endif

#ifndef GKS_UNUSED
#define GKS_UNUSED(x) (void)(x)
#endif

#if !defined(NO_AV) && !defined(NO_CAIRO)
static gks_state_list_t *gkss;

typedef struct ws_state_list_t
{
  char *path;
  char *mem_path;
  int *mem;
  int width, height, framerate;
  int wtype;
  movie_t movie;
  frame_t frame;
  void *cairo_ws_state_list;
  int video_plugin_initialized;
  int user_defined_resolution;
  int video_flags;
} ws_state_list;

static ws_state_list *p;

static void close_page(void)
{
  if ((p->wtype == 120 || p->wtype == 121 || p->wtype == 130 || p->wtype == 131 || p->wtype == 160 || p->wtype == 161 ||
       p->wtype == 162) &&
      p->movie)
    {
      vc_movie_finish(p->movie);
    }
  gks_free(p->movie);
  gks_free(p->frame);
}

static void open_page()
{
  int width, height;
  width = p->mem[0];
  height = p->mem[1];
  char path[MAXPATHLEN];

  if (p->wtype == 120 || p->wtype == 121)
    {
      gks_filepath(path, p->path, "mov", 0, 0);
    }
  else if (p->wtype == 130)
    {
      gks_filepath(path, p->path, "gif", 0, 0);
    }
  else if (p->wtype == 131)
    {
      gks_filepath(path, p->path, "png", 0, 0);
    }
  else if (p->wtype == 160)
    {
      gks_filepath(path, p->path, "mp4", 0, 0);
    }
  else if (p->wtype == 161)
    {
      gks_filepath(path, p->path, "webm", 0, 0);
    }
  else if (p->wtype == 162)
    {
      gks_filepath(path, p->path, "ogg", 0, 0);
    }
  p->movie = vc_movie_create(path, p->framerate, 4000000, width, height, p->video_flags);
  p->frame = (frame_t)gks_malloc(sizeof(struct frame_t_));
}

static void write_page(void)
{
  int bg[3] = {255, 255, 255};
  int i, j, k;
  int width, height;
  unsigned char *mem;

  if (!p->movie)
    {
      open_page();
    }

  width = p->mem[0];
  height = p->mem[1];

  mem = *((unsigned char **)(p->mem + 3));
  for (i = 0; i < height; i++)
    {
      for (j = 0; j < width; j++)
        {
          long ind = (i * width + j) * 4;
          double alpha = mem[ind + 3] / 255.0;
          for (k = 0; k < 3; k++)
            {
              double col = mem[ind + k] * alpha + bg[k] * (1 - alpha) + 0.5;
              if (col > 255)
                {
                  col = 255;
                }
              mem[ind + k] = (unsigned char)col;
            }
        }
    }
  if (p->movie)
    {
      p->frame->data = mem;
      p->frame->width = width;
      p->frame->height = height;
      vc_movie_append_frame(p->movie, p->frame);
    }
  else
    {
      fprintf(stderr, "Failed to append video frame\n");
    }
}

void gks_videoplugin(int fctid, int dx, int dy, int dimx, int *ia, int lr1, double *r1, int lr2, double *r2, int lc,
                     char *chars, void **ptr)
{
  p = (ws_state_list *)*ptr;

  switch (fctid)
    {
    case 2:
      /* open workstation */

      gkss = (gks_state_list_t *)*ptr;

      p = (ws_state_list *)gks_malloc(sizeof(ws_state_list));
      p->cairo_ws_state_list = *ptr;
      p->video_plugin_initialized = 0;
      p->movie = NULL;
      p->mem = NULL;
      p->wtype = ia[2];
      p->path = chars;
      p->video_flags = 0;
      *ptr = p;

      long width, height, framerate, num_args;
      char *env;
      width = height = framerate = -1;
      env = (char *)gks_getenv("GKS_VIDEO_OPTS");
      if (env)
        {
          /* GKS_VIDEO_OPTS=<width>x<height>@<framerate> */
          num_args = sscanf(env, "%ldx%ld@%ld", &width, &height, &framerate);
          if (num_args == 0 && !(strlen(env) >= 3 && strcmp(env + strlen(env) - 3, "@x2") == 0))
            {
              /* GKS_VIDEO_OPTS invalid */
              fprintf(stderr, "Failed to parse GKS_VIDEO_OPTS. Expected '<width>x<height>@<framerate>', "
                              "'<width>x<height>' or '<framerate>'\n");
              exit(1);
            }
          else if (num_args == 1)
            {
              /* GKS_VIDEO_OPTS=<framerate> */
              framerate = width;
              width = -1;
              height = -1;
            }
          else if (num_args == 2)
            {
              /* GKS_VIDEO_OPTS=<width>x<height> */
              framerate = -1;
            }
          if (strlen(env) >= 3 && strcmp(env + strlen(env) - 3, "@x2") == 0)
            {
              /* Allow @x2 suffix to GKS_VIDEO_OPTS enabling HiDPI mov videos */
              if (p->wtype == 120)
                {
                  p->video_flags |= VC_FLAGS_MOV_HIDPI;
                }
              else
                {
                  fprintf(stderr, "HiDPI is only supported when using mov files.\n");
                  exit(1);
                }
            }
        }
      if (p->wtype == 121)
        {
          p->video_flags |= VC_FLAGS_MOV_HIDPI;
        }

      p->framerate = 24;
      p->width = 720;
      p->height = 720;
      p->user_defined_resolution = 0;

      if (framerate > 0)
        {
          p->framerate = (int)framerate;
        }
      if (width > 0)
        {
          p->width = (int)width;
          p->user_defined_resolution = 1;
        }
      if (height > 0)
        {
          p->height = (int)height;
          p->user_defined_resolution = 1;
        }

      p->mem_path = (char *)gks_malloc(MAXPATHLEN);
      p->mem = (int *)gks_malloc(3 * sizeof(int) + sizeof(unsigned char *));
      p->mem[0] = p->width;
      p->mem[1] = p->height;
      p->mem[2] = 144;
      *((unsigned char **)(p->mem + 3)) = NULL;

      snprintf(p->mem_path, MAXPATHLEN, "!resizable@%p.mem", (void *)p->mem);
      chars = p->mem_path;
      /* set wstype for cairo png in memory */
      ia[2] = 143;

      p->video_plugin_initialized = 1;
      break;
    default:;
    }

  if (p && p->user_defined_resolution && fctid == 55)
    {
      /* Ignore setwsviewport if the video resolution is user defined */
      return;
    }

  if (p && p->video_plugin_initialized)
    {
      gks_cairo_plugin(fctid, dx, dy, dimx, ia, lr1, r1, lr2, r2, lc, chars, &(p->cairo_ws_state_list));
    }

  switch (fctid)
    {
    case 2:
      /* open workstation */
      if (p->mem_path)
        {
          gks_free(p->mem_path);
          p->mem_path = NULL;
        }
      break;
    case 3:
      /* close workstation */
      if (!p)
        {
          break;
        }
      p->video_plugin_initialized = 0;
      close_page();
      if (p->mem)
        {
          unsigned char *mem_ptr = *((unsigned char **)(p->mem + 3));
          if (mem_ptr)
            {
              gks_free(mem_ptr);
            }
          gks_free(p->mem);
        }
      gks_free(p);
      break;
    case 8:
      /* update workstation */
      if (ia[1] & GKS_K_WRITE_PAGE_FLAG)
        {
          write_page();
        }
      break;
    default:;
    }
}

#else

void gks_videoplugin(int fctid, int dx, int dy, int dimx, int *ia, int lr1, double *r1, int lr2, double *r2, int lc,
                     char *chars, void **ptr)
{
  GKS_UNUSED(dx);
  GKS_UNUSED(dy);
  GKS_UNUSED(dimx);
  GKS_UNUSED(ia);
  GKS_UNUSED(lr1);
  GKS_UNUSED(r1);
  GKS_UNUSED(lr2);
  GKS_UNUSED(r2);
  GKS_UNUSED(lc);
  GKS_UNUSED(chars);
  GKS_UNUSED(ptr);

  if (fctid == 2)
    {
      gks_perror("Video support not compiled in");
      ia[0] = 0;
    }
}

#endif