summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/import/import_im.c
blob: feee0763add3e461e6e47dfedca39fe603900ed1 (plain)
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
/*
 *  import_im.c
 *
 *  Copyright (C) Thomas Oestreich - June 2001
 *  port to MagickWand API:
 *  Copyright (C) Francesco Romani - July 2007
 *
 *  This file is part of transcode, a video stream processing tool
 *
 *  transcode is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  transcode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#define MOD_NAME    "import_im.so"
#define MOD_VERSION "v0.1.3 (2008-10-07)"
#define MOD_CODEC   "(video) RGB"

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

/* Note: because of ImageMagick bogosity, this must be included first, so
 * we can undefine the PACKAGE_* symbols it splats into our namespace */
#ifdef HAVE_BROKEN_WAND
#include <wand/magick-wand.h>
#else /* we have a SANE wand header */
#include <wand/MagickWand.h>
#endif /* HAVE_BROKEN_WAND */

#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION

#include "transcode.h"
#include "libtc/optstr.h"

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

/*%*
 *%* DESCRIPTION 
 *%*   This module reads single images from disk using ImageMagick;
 *%*   a stream of correlated images can be automatically read if
 *%*   their filenames contains a common prefix and a serial number.
 *%*   All formats supported by ImageMagick are supported as well.
 *%*
 *%* BUILD-DEPENDS
 *%*   libMagick >= 6.2.4.0
 *%*
 *%* DEPENDS
 *%*   libMagick >= 6.2.4.0
 *%*
 *%* PROCESSING
 *%*   import/demuxer
 *%*
 *%* MEDIA
 *%*   video
 *%*
 *%* #INPUT
 *%*
 *%* OUTPUT
 *%*   RGB24
 *%*
 *%* OPTION
 *%*   noseq (flag)
 *%*     disable internal auto loading of images with similar names.
 *%*/

static int verbose_flag = TC_QUIET;
static int capability_flag = TC_CAP_RGB|TC_CAP_VID;

#define MOD_PRE im
#include "import_def.h"

#include <time.h>
#include <sys/types.h>
#include <regex.h>


static char *head = NULL, *tail = NULL;
static int first_frame = 0, current_frame = 0, decoded_frame = 0, pad = 0;
static int total_frame = 0;
static int width = 0, height = 0;
static MagickWand *wand = NULL;
static int auto_seq_read = TC_TRUE; 
/* 
 * automagically read further images with filename like the first one 
 * enabled by default for backward compatibility, but obsoleted
 * by core option --multi_input
 */

static int TCHandleMagickError(MagickWand *wand)
{
    ExceptionType severity;
    const char *description = MagickGetException(wand, &severity);

    tc_log_error(MOD_NAME, "%s", description);

    MagickRelinquishMemory((void*)description);
    return TC_IMPORT_ERROR;
}


/* ------------------------------------------------------------
 *
 * open stream
 *
 * ------------------------------------------------------------*/


/* I suspect we have a lot of potential memleaks in here -- FRomani */
MOD_open
{
    int result, slen = 0;
    char *regex = NULL, *frame = NULL;
    regex_t preg;
    regmatch_t pmatch[4];

    if (param->flag == TC_AUDIO) {
        return TC_IMPORT_OK;
    }

    if (param->flag == TC_VIDEO) {
        param->fd = NULL;

        // get the frame name and range
        regex = "\\([^0-9]\\+[-._]\\?\\)\\?\\([0-9]\\+\\)\\([-._].\\+\\)\\?";
        result = regcomp(&preg, regex, 0);
        if (result) {
            tc_log_perror(MOD_NAME, "ERROR:  Regex compile failed.\n");
            return TC_IMPORT_ERROR;
        }

        result = regexec(&preg, vob->video_in_file, 4, pmatch, 0);
        if (result) {
            tc_log_warn(MOD_NAME, "Regex match failed: no image sequence");
            slen = strlen(vob->video_in_file) + 1;
            head = tc_malloc(slen);
            if (head == NULL) {
                tc_log_perror(MOD_NAME, "filename head");
                return TC_IMPORT_ERROR;
            }
            strlcpy(head, vob->video_in_file, slen);
            tail = tc_malloc(1); /* URGH -- FRomani */
            tail[0] = 0;
            first_frame = -1;
        } else {
            // split the name into head, frame number, and tail
            slen = pmatch[1].rm_eo - pmatch[1].rm_so + 1;
            head = tc_malloc(slen);
            if (head == NULL) {
                tc_log_perror(MOD_NAME, "filename head");
                return TC_IMPORT_ERROR;
            }
            strlcpy(head, vob->video_in_file, slen);

            slen = pmatch[2].rm_eo - pmatch[2].rm_so + 1;
            frame = tc_malloc(slen);
            if (frame == NULL) {
                tc_log_perror(MOD_NAME, "filename frame");
                return TC_IMPORT_ERROR;
            }
            strlcpy(frame, vob->video_in_file + pmatch[2].rm_so, slen);

            // If the frame number is padded with zeros, record how many digits
            // are actually being used.
            if (frame[0] == '0') {
                pad = pmatch[2].rm_eo - pmatch[2].rm_so;
            }
            first_frame = atoi(frame);

            slen = pmatch[3].rm_eo - pmatch[3].rm_so + 1;
            tail = tc_malloc(slen);
            if (tail == NULL) {
                tc_log_perror(MOD_NAME, "filename tail");
                return TC_IMPORT_ERROR;
            }
            strlcpy(tail, vob->video_in_file + pmatch[3].rm_so, slen);

            tc_free(frame);
        }

        if (vob->im_v_string != NULL) {
            if (optstr_lookup(vob->im_v_string, "noseq")) {
                auto_seq_read = TC_FALSE;
                if (verbose > TC_INFO) {
                    tc_log_info(MOD_NAME, "automagic image sequential read disabled");
                }
            }
        }
 
        current_frame = first_frame;
        decoded_frame = 0;
        width         = vob->im_v_width;
        height        = vob->im_v_height;

        if (total_frame == 0) {
            /* only the very first time */
            MagickWandGenesis();
        }

        wand = NewMagickWand();
        if (wand == NULL) {
            tc_log_error(MOD_NAME, "cannot create magick wand");
            return TC_IMPORT_ERROR;
        }

        return TC_IMPORT_OK;
    }

    return TC_IMPORT_ERROR;
}


/* ------------------------------------------------------------
 *
 * decode  stream
 *
 * ------------------------------------------------------------*/

MOD_decode
{
    char *frame = NULL, *filename = NULL;
    int slen;
    MagickBooleanType status;

    if (param->flag == TC_AUDIO) {
        return TC_IMPORT_OK;
    }

    if (param->flag == TC_VIDEO) {
        if (!auto_seq_read) {
            if (decoded_frame > 0) {
                return TC_IMPORT_ERROR;
            }
            filename = tc_strdup(vob->video_in_file);
        } else {
            // build the filename for the current frame
            slen = strlen(head) + pad + strlen(tail) + 1;
            filename = tc_malloc(slen);
            if (pad) {
                char framespec[10];
                frame = tc_malloc(pad+1);
                tc_snprintf(framespec, 10, "%%0%dd", pad);
                tc_snprintf(frame, pad+1, framespec, current_frame);
                frame[pad] = '\0';
            } else if (first_frame >= 0) {
                frame = tc_malloc(10);
                tc_snprintf(frame, 10, "%d", current_frame);
            }
            strlcpy(filename, head, slen);
            if (frame != NULL) {
                strlcat(filename, frame, slen);
                tc_free(frame);
                frame = NULL;
            }
            strlcat(filename, tail, slen);
        }

        ClearMagickWand(wand);
        /* 
         * This avoids IM to buffer all read images.
         * I'm quite sure that this can be done in a smarter way,
         * but I haven't yet figured out how. -- FRomani
         */

        status = MagickReadImage(wand, filename);
        if (status == MagickFalse) {
            if (auto_seq_read) {
                /* let's assume that image sequence ends here */
                return TC_IMPORT_ERROR;
            }
            return TCHandleMagickError(wand);
        }

        MagickSetLastIterator(wand);

        status = MagickGetImagePixels(wand,
                                      0, 0, width, height,
                                      "RGB", CharPixel,
                                      param->buffer);
        /* param->size already set correctly by caller */
        if (status == MagickFalse) {
            return TCHandleMagickError(wand);
        }

        param->attributes |= TC_FRAME_IS_KEYFRAME;

        total_frame++;
        current_frame++;
        decoded_frame++;
    
        tc_free(filename);

        return TC_IMPORT_OK;
    }
    return TC_IMPORT_ERROR;
}

/* ------------------------------------------------------------
 *
 * close stream
 *
 * ------------------------------------------------------------*/

MOD_close
{
    if (param->flag == TC_AUDIO) {
        return TC_IMPORT_OK;
    }

    if (param->flag == TC_VIDEO) {
        vob_t *vob = tc_get_vob();

        if (param->fd != NULL)
            pclose(param->fd);
        if (head != NULL)
            tc_free(head);
        if (tail != NULL)
            tc_free(tail);

        if (wand != NULL) {
            DestroyMagickWand(wand);
            wand = NULL;

            if (!tc_has_more_video_in_file(vob)) {
                /* ...can you hear that? is the sound of the ugliness... */
                MagickWandTerminus();
            }
        }
        return TC_IMPORT_OK;
    }
    return TC_IMPORT_ERROR;
}

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

/*
 * Local variables:
 *   c-file-style: "stroustrup"
 *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
 *   indent-tabs-mode: nil
 * End:
 *
 * vim: expandtab shiftwidth=4:
 */