about summary refs log tree commit diff
path: root/converter/other/fiasco/codec/dfiasco.c
blob: 3f5d3fcc34f15d8e9054b757e7a03468dcde971b (plain) (blame)
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
/*
 *  dfiasco.c:          Decoder public interface
 *
 *  Written by:         Ullrich Hafner
 *
 *  This file is part of FIASCO (Fractal Image And Sequence COdec)
 *  Copyright (C) 1994-2000 Ullrich Hafner
 */

/*
 *  $Date: 2000/10/28 17:39:30 $
 *  $Author: hafner $
 *  $Revision: 5.7 $
 *  $State: Exp $
 */

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

#include "pm_c_util.h"
#include "nstring.h"

#include "config.h"

#include "types.h"
#include "macros.h"
#include "error.h"

#include "dfiasco.h"
#include "wfa.h"
#include "read.h"
#include "misc.h"
#include "bit-io.h"
#include "decoder.h"
#include "options.h"
#include "wfalib.h"

/*****************************************************************************

                                prototypes

*****************************************************************************/

static dfiasco_t *
cast_dfiasco (fiasco_decoder_t *dfiasco);
static void
free_dfiasco (dfiasco_t *dfiasco);
static dfiasco_t *
alloc_dfiasco (wfa_t *wfa, video_t *video, bitfile_t *input,
               int enlarge_factor, int smoothing, format_e image_format);

/*****************************************************************************

                                public code

*****************************************************************************/

fiasco_decoder_t *
fiasco_decoder_new (const char *filename, const fiasco_d_options_t *options)
{
   try
   {
      bitfile_t          *input;        /* pointer to WFA FIASCO stream */
      wfa_t              *wfa;          /* wfa structure */
      video_t            *video;        /* information about decoder state */
      const d_options_t  *dop;          /* decoder additional options */
      dfiasco_t          *dfiasco;      /* decoder internal state */
      fiasco_decoder_t   *decoder;      /* public interface to decoder */
      fiasco_d_options_t *default_options = NULL;

      if (options)
      {
         dop = cast_d_options ((fiasco_d_options_t *) options);
         if (!dop)
            return NULL;
      }
      else
      {
         default_options = fiasco_d_options_new ();
         dop             = cast_d_options (default_options);
      }

      wfa   = alloc_wfa (NO);
      video = alloc_video (NO);
      input = open_wfa (filename, wfa->wfainfo);
      read_basis (wfa->wfainfo->basis_name, wfa);

      decoder              = Calloc (1, sizeof (fiasco_decoder_t));
      decoder->delete      = fiasco_decoder_delete;
      decoder->write_frame = fiasco_decoder_write_frame;
      decoder->get_frame   = fiasco_decoder_get_frame;
      decoder->get_length  = fiasco_decoder_get_length;
      decoder->get_rate    = fiasco_decoder_get_rate;
      decoder->get_width   = fiasco_decoder_get_width;
      decoder->get_height  = fiasco_decoder_get_height;
      decoder->get_title   = fiasco_decoder_get_title;
      decoder->get_comment = fiasco_decoder_get_comment;
      decoder->is_color    = fiasco_decoder_is_color;

      decoder->private = dfiasco
                       = alloc_dfiasco (wfa, video, input,
                                        dop->magnification,
                                        dop->smoothing,
                                        dop->image_format);

      if (default_options)
         fiasco_d_options_delete (default_options);
      if (dfiasco->enlarge_factor >= 0)
      {
         int           n;
         unsigned long pixels = wfa->wfainfo->width * wfa->wfainfo->height;

         for (n = 1; n <= (int) dfiasco->enlarge_factor; n++)
         {
            if (pixels << (n << 1) > 2048 * 2048)
            {
               set_error (_("Magnifaction factor `%d' is too large. "
                            "Maximum value is %d."),
                          dfiasco->enlarge_factor, MAX(0, n - 1));
               fiasco_decoder_delete (decoder);
               return NULL;
            }
         }
      }
      else
      {
         int n;

         for (n = 0; n <= (int) - dfiasco->enlarge_factor; n++)
         {
            if (wfa->wfainfo->width >> n < 32
                || wfa->wfainfo->height >> n < 32)
            {
               set_error (_("Magnifaction factor `%d' is too small. "
                            "Minimum value is %d."),
                          dfiasco->enlarge_factor, - MAX(0, n - 1));
               fiasco_decoder_delete (decoder);
               return NULL;
            }
         }
      }
      return (fiasco_decoder_t *) decoder;
   }
   catch
   {
      return NULL;
   }
}

int
fiasco_decoder_write_frame (fiasco_decoder_t *decoder,
                            const char *filename)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return 0;
   else
   {
      try
      {
         image_t *frame = get_next_frame (NO, dfiasco->enlarge_factor,
                                          dfiasco->smoothing, NULL,
                                          FORMAT_4_4_4, dfiasco->video, NULL,
                                          dfiasco->wfa, dfiasco->input);
         write_image (filename, frame);
      }
      catch
      {
         return 0;
      }
      return 1;
   }
}

fiasco_image_t *
fiasco_decoder_get_frame (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return NULL;
   else
   {
      try
      {
         fiasco_image_t *image = Calloc (1, sizeof (fiasco_image_t));
         image_t        *frame = get_next_frame (NO, dfiasco->enlarge_factor,
                                                 dfiasco->smoothing, NULL,
                                                 dfiasco->image_format,
                                                 dfiasco->video, NULL,
                                                 dfiasco->wfa, dfiasco->input);

         frame->reference_count++;      /* for motion compensation */
         image->private    = frame;
         image->delete     = fiasco_image_delete;
         image->get_width  = fiasco_image_get_width;
         image->get_height = fiasco_image_get_height;
         image->is_color   = fiasco_image_is_color;

         return image;
      }
      catch
      {
         return NULL;
      }
   }
}

unsigned
fiasco_decoder_get_length (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return 0;
   else
      return dfiasco->wfa->wfainfo->frames;
}

unsigned
fiasco_decoder_get_rate (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return 0;
   else
      return dfiasco->wfa->wfainfo->fps;
}

unsigned
fiasco_decoder_get_width (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return 0;
   else
   {
      unsigned width;

      if (dfiasco->enlarge_factor >= 0)
         width = dfiasco->wfa->wfainfo->width << dfiasco->enlarge_factor;
      else
         width = dfiasco->wfa->wfainfo->width >> - dfiasco->enlarge_factor;

      return width & 1 ? width + 1 : width;
   }
}

unsigned
fiasco_decoder_get_height (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return 0;
   else
   {
      unsigned height;

      if (dfiasco->enlarge_factor >= 0)
         height = dfiasco->wfa->wfainfo->height << dfiasco->enlarge_factor;
      else
         height = dfiasco->wfa->wfainfo->height >> - dfiasco->enlarge_factor;

      return height & 1 ? height + 1 : height;
   }
}

const char *
fiasco_decoder_get_title (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return NULL;
   else
      return dfiasco->wfa->wfainfo->title;
}

const char *
fiasco_decoder_get_comment (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return NULL;
   else
      return dfiasco->wfa->wfainfo->comment;
}

int
fiasco_decoder_is_color (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return 0;
   else
      return dfiasco->wfa->wfainfo->color;
}

int
fiasco_decoder_delete (fiasco_decoder_t *decoder)
{
   dfiasco_t *dfiasco = cast_dfiasco (decoder);

   if (!dfiasco)
      return 1;

   try
   {
      free_wfa (dfiasco->wfa);
      free_video (dfiasco->video);
      close_bitfile (dfiasco->input);
      strcpy (dfiasco->id, " ");
      free_dfiasco(dfiasco);
      Free (decoder);
   }
   catch
   {
      return 0;
   }

   return 1;
}

/*****************************************************************************

                                private code

*****************************************************************************/

static dfiasco_t *
alloc_dfiasco (wfa_t *wfa, video_t *video, bitfile_t *input,
               int enlarge_factor, int smoothing, format_e image_format)
/*
 *  FIASCO decoder constructor:
 *  Initialize decoder structure.
 *
 *  Return value:
 *      pointer to the new decoder structure
 */
{
   dfiasco_t *dfiasco = Calloc (1, sizeof (dfiasco_t));

   strcpy (dfiasco->id, "DFIASCO");

   dfiasco->wfa            = wfa;
   dfiasco->video          = video;
   dfiasco->input          = input;
   dfiasco->enlarge_factor = enlarge_factor;
   dfiasco->smoothing      = smoothing;
   dfiasco->image_format   = image_format;

   return dfiasco;
}

static void
free_dfiasco (dfiasco_t *dfiasco)
/*
 *  FIASCO decoder destructor:
 *  Free memory of given 'decoder' struct.
 *
 *  No return value.
 *
 *  Side effects:
 *      'video' struct is discarded.
 */
{
   Free (dfiasco);
}

static dfiasco_t *
cast_dfiasco (fiasco_decoder_t *dfiasco)
/*
 *  Cast pointer `dfiasco' to type dfiasco_t.
 *  Check whether `dfiasco' is a valid object of type dfiasco_t.
 *
 *  Return value:
 *      pointer to dfiasco_t struct on success
 *      NULL otherwise
 */
{
   dfiasco_t *this = (dfiasco_t *) dfiasco->private;
   if (this)
   {
      if (!streq (this->id, "DFIASCO"))
      {
         set_error (_("Parameter `dfiasco' doesn't match required type."));
         return NULL;
      }
   }
   else
   {
      set_error (_("Parameter `%s' not defined (NULL)."), "dfiasco");
   }

   return this;
}