File: movie.c

package info (click to toggle)
simage 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,908 kB
  • sloc: ansic: 14,231; sh: 12,587; cpp: 930; perl: 854; makefile: 617; lisp: 25
file content (227 lines) | stat: -rw-r--r-- 4,833 bytes parent folder | download | duplicates (5)
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
/*
  Note 20020923 thammer:
  movie.c should probably be rewritten to use stream.c. 
  If you are considering modifying movie.c, please discuss it
  with coin@sim.no first.

 */

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

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <simage.h>
#include <simage_private.h>
#include <string.h>

#ifdef SIMAGE_MPEG2ENC_SUPPORT
#include "../mpeg2enc/api.h"
#endif /* SIMAGE_MPEG2ENC_SUPPORT */

#ifdef SIMAGE_AVIENC_SUPPORT
#include "simage_avi.h"
#endif /* SIMAGE_AVIENC_SUPPORT */

struct simage_movie_s {
  char * filename;
  
  s_movie_open_func * open;
  s_movie_create_func * create;
  s_movie_get_func * get;
  s_movie_put_func * put;
  s_movie_close_func * close;
  
  s_params * params;
};

struct simage_movie_importer {
  s_movie_open_func * open;
  s_movie_get_func * get;
  s_movie_close_func * close;  

  struct simage_movie_importer * next;
};

struct simage_movie_exporter {
  s_movie_create_func * create;
  s_movie_put_func * put;
  s_movie_close_func * close;  
  
  struct simage_movie_exporter * next;
};

// FIXME: convert access to these variables into the singleton
// pattern. 20010917 mortene.
static struct simage_movie_importer * importers;
static struct simage_movie_exporter * exporters;

static void 
add_internal_importers(void)
{
  static int first = 1;
  if (first) {
    /* none yet */
    first = 0;
  }                
}

static void
add_internal_exporters(void)
{
  static int first = 1;
  if (first) {    
#ifdef SIMAGE_MPEG2ENC_SUPPORT
    s_movie_exporter_add(mpeg2enc_movie_create,
                         mpeg2enc_movie_put,
                         mpeg2enc_movie_close);
#endif
#ifdef SIMAGE_AVIENC_SUPPORT
    s_movie_exporter_add(avienc_movie_create,
                         avienc_movie_put,
                         avienc_movie_close);
#endif
   first = 0;
  }
}

s_movie * 
s_movie_open(const char * filename)
{
  struct simage_movie_importer * imp;
  s_movie * movie = (s_movie*) malloc(sizeof(s_movie));
  movie->params = NULL;
  movie->filename = NULL;

  add_internal_importers();

  imp = importers;
  while (imp) {
    if (imp->open(filename, movie)) break;
    imp = imp->next;
  }
  if (imp == NULL) {
    free((void*)movie);
    return NULL;
  }

  movie->filename = (char*) malloc(strlen(filename)+1);
  strcpy(movie->filename, filename);
  movie->open = imp->open;
  movie->get = imp->get;
  movie->close = imp->close;

  return movie;
}

s_movie * 
s_movie_create(const char * filename, s_params * params /* | NULL */)
{
  struct simage_movie_exporter * exp;
  s_movie * movie = (s_movie*) malloc(sizeof(s_movie));
  movie->params = NULL;
  movie->filename = NULL;
  
  add_internal_exporters();

  exp = exporters;
  while (exp) {
    if (exp->create(filename, movie, params)) break;
    exp = exp->next;
  }
  if (exp == NULL) {
    free((void*) movie);
    return NULL;
  }

  movie->filename = (char*) malloc(strlen(filename)+1);
  movie->create = exp->create;
  movie->put = exp->put;
  movie->close = exp->close;
  strcpy(movie->filename, filename);
  return movie;
}

s_image *
s_movie_get_image(s_movie * movie, s_image * prealloc, s_params * params)
{
  return movie->get(movie, prealloc, params);
}

int 
s_movie_put_image(s_movie * movie, s_image * image,
                  s_params * params)
{
  return movie->put(movie, image, params);
}

void 
s_movie_close(s_movie * movie)
{
  movie->close(movie);
}

void 
s_movie_destroy(s_movie * movie)
{
  if (movie->params) s_params_destroy(movie->params);
  if (movie->filename) free((void*) movie->filename);
  free((void*) movie);
}

s_params * 
s_movie_params(s_movie * movie)
{
  if (movie->params == NULL) {
    movie->params = s_params_create();
  }
  return movie->params;
}

void 
s_movie_importer_add(s_movie_open_func * open,
                     s_movie_get_func * get,
                     s_movie_close_func * close)
{
  struct simage_movie_importer * last, * imp = importers;
  last = NULL;
  while (imp) {
    last = imp;
    imp = imp->next;
  }
  imp = (struct simage_movie_importer*) malloc(sizeof(struct simage_movie_importer));
  imp->open = open;
  imp->get = get;
  imp->close = close;
  imp->next = NULL;

  if (last == NULL) {
    importers = imp;
  }
  else last->next = imp;
}

void 
s_movie_exporter_add(s_movie_create_func * create,
                     s_movie_put_func * put,
                     s_movie_close_func * close)
{
  struct simage_movie_exporter * last, * exp = exporters;
  last = NULL;
  while (exp) {
    last = exp;
    exp = exp->next;
  }
  exp = (struct simage_movie_exporter*) malloc(sizeof(struct simage_movie_exporter));
  exp->create = create;
  exp->put = put;
  exp->close = close;
  exp->next = NULL;

  if (last == NULL) {
    exporters = exp;
  }
  else last->next = exp;
}