Statistics
| Branch: | Revision:

ffmpeg / libavformat / img.c @ 5a757507

History | View | Annotate | Download (10 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Image format
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 de6d9b64 Fabrice Bellard
 */
19
#include "avformat.h"
20 9b2e001f Bill Eldridge
21 22b37f5d Fabrice Bellard
/* XXX: this is a hack */
22
int loop_input = 0;
23
24 de6d9b64 Fabrice Bellard
typedef struct {
25
    int width;
26
    int height;
27 22b37f5d Fabrice Bellard
    int img_first;
28
    int img_last;
29 de6d9b64 Fabrice Bellard
    int img_number;
30 22b37f5d Fabrice Bellard
    int img_count;
31 de6d9b64 Fabrice Bellard
    int img_size;
32 87a0a681 Fabrice Bellard
    AVImageFormat *img_fmt;
33
    int pix_fmt;
34 de6d9b64 Fabrice Bellard
    int is_pipe;
35
    char path[1024];
36 87a0a681 Fabrice Bellard
    /* temporary usage */
37
    void *ptr;
38 de6d9b64 Fabrice Bellard
} VideoData;
39
40 ca1538f8 Fabrice Bellard
41
/* return -1 if no image found */
42
static int find_image_range(int *pfirst_index, int *plast_index, 
43
                            const char *path)
44
{
45
    char buf[1024];
46
    int range, last_index, range1, first_index;
47
48
    /* find the first image */
49
    for(first_index = 0; first_index < 5; first_index++) {
50
        if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0)
51
            goto fail;
52
        if (url_exist(buf))
53
            break;
54
    }
55
    if (first_index == 5)
56
        goto fail;
57
    
58
    /* find the last image */
59
    last_index = first_index;
60
    for(;;) {
61
        range = 0;
62
        for(;;) {
63
            if (!range)
64
                range1 = 1;
65
            else
66
                range1 = 2 * range;
67
            if (get_frame_filename(buf, sizeof(buf), path, 
68
                                   last_index + range1) < 0)
69
                goto fail;
70
            if (!url_exist(buf))
71
                break;
72
            range = range1;
73
            /* just in case... */
74
            if (range >= (1 << 30))
75
                goto fail;
76
        }
77
        /* we are sure than image last_index + range exists */
78
        if (!range)
79
            break;
80
        last_index += range;
81
    }
82
    *pfirst_index = first_index;
83
    *plast_index = last_index;
84
    return 0;
85
 fail:
86
    return -1;
87
}
88
89
90 87a0a681 Fabrice Bellard
static int image_probe(AVProbeData *p)
91 de6d9b64 Fabrice Bellard
{
92 94d883e8 Fabrice Bellard
    if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
93 87a0a681 Fabrice Bellard
        return AVPROBE_SCORE_MAX;
94
    else
95 de6d9b64 Fabrice Bellard
        return 0;
96
}
97
98 87a0a681 Fabrice Bellard
static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
99 de6d9b64 Fabrice Bellard
{
100 87a0a681 Fabrice Bellard
    VideoData *s = opaque;
101 de6d9b64 Fabrice Bellard
102 87a0a681 Fabrice Bellard
    s->width = info->width;
103
    s->height = info->height;
104
    s->pix_fmt = info->pix_fmt;
105
    /* stop image reading but no error */
106
    return 1;
107 de6d9b64 Fabrice Bellard
}
108
109
static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
110
{
111 c9a65ca8 Fabrice Bellard
    VideoData *s = s1->priv_data;
112 ca1538f8 Fabrice Bellard
    int ret, first_index, last_index;
113 de6d9b64 Fabrice Bellard
    char buf[1024];
114
    ByteIOContext pb1, *f = &pb1;
115
    AVStream *st;
116
117 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s1, 0);
118 de6d9b64 Fabrice Bellard
    if (!st) {
119 1ea4f593 Fabrice Bellard
        av_free(s);
120 de6d9b64 Fabrice Bellard
        return -ENOMEM;
121
    }
122 c9a65ca8 Fabrice Bellard
123 87a0a681 Fabrice Bellard
    if (ap && ap->image_format)
124
        s->img_fmt = ap->image_format;
125
126 de6d9b64 Fabrice Bellard
    strcpy(s->path, s1->filename);
127
    s->img_number = 0;
128 22b37f5d Fabrice Bellard
    s->img_count = 0;
129
    
130 de6d9b64 Fabrice Bellard
    /* find format */
131 c9a65ca8 Fabrice Bellard
    if (s1->iformat->flags & AVFMT_NOFILE)
132 de6d9b64 Fabrice Bellard
        s->is_pipe = 0;
133
    else
134
        s->is_pipe = 1;
135
        
136 ca1538f8 Fabrice Bellard
    if (!ap || !ap->frame_rate) {
137
        st->codec.frame_rate      = 25;
138
        st->codec.frame_rate_base = 1;
139
    } else {
140
        st->codec.frame_rate      = ap->frame_rate;
141
        st->codec.frame_rate_base = ap->frame_rate_base;
142
    }
143
    
144 de6d9b64 Fabrice Bellard
    if (!s->is_pipe) {
145 ca1538f8 Fabrice Bellard
        if (find_image_range(&first_index, &last_index, s->path) < 0)
146
            goto fail;
147 22b37f5d Fabrice Bellard
        s->img_first = first_index;
148
        s->img_last = last_index;
149 ca1538f8 Fabrice Bellard
        s->img_number = first_index;
150
        /* compute duration */
151
        st->start_time = 0;
152
        st->duration = ((int64_t)AV_TIME_BASE * 
153
                        (last_index - first_index + 1) * 
154
                        st->codec.frame_rate_base) / st->codec.frame_rate;
155
        if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
156
            goto fail;
157
        if (url_fopen(f, buf, URL_RDONLY) < 0)
158 de6d9b64 Fabrice Bellard
            goto fail;
159
    } else {
160
        f = &s1->pb;
161
    }
162
    
163 87a0a681 Fabrice Bellard
    ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
164
    if (ret < 0)
165
        goto fail1;
166 de6d9b64 Fabrice Bellard
167
    if (!s->is_pipe) {
168
        url_fclose(f);
169
    } else {
170
        url_fseek(f, 0, SEEK_SET);
171
    }
172
    
173
    st->codec.codec_type = CODEC_TYPE_VIDEO;
174
    st->codec.codec_id = CODEC_ID_RAWVIDEO;
175
    st->codec.width = s->width;
176
    st->codec.height = s->height;
177 87a0a681 Fabrice Bellard
    st->codec.pix_fmt = s->pix_fmt;
178
    s->img_size = avpicture_get_size(s->pix_fmt, s->width, s->height);
179
180 de6d9b64 Fabrice Bellard
    return 0;
181
 fail1:
182
    if (!s->is_pipe)
183
        url_fclose(f);
184
 fail:
185 1ea4f593 Fabrice Bellard
    av_free(s);
186 de6d9b64 Fabrice Bellard
    return -EIO;
187
}
188
189 87a0a681 Fabrice Bellard
static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
190 de6d9b64 Fabrice Bellard
{
191 87a0a681 Fabrice Bellard
    VideoData *s = opaque;
192
193
    if (info->width != s->width ||
194
        info->height != s->height)
195
        return -1;
196
    avpicture_fill(&info->pict, s->ptr, info->pix_fmt, info->width, info->height);
197 de6d9b64 Fabrice Bellard
    return 0;
198
}
199
200 87a0a681 Fabrice Bellard
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
201 de6d9b64 Fabrice Bellard
{
202 87a0a681 Fabrice Bellard
    VideoData *s = s1->priv_data;
203
    char filename[1024];
204
    int ret;
205
    ByteIOContext f1, *f;
206 de6d9b64 Fabrice Bellard
207 87a0a681 Fabrice Bellard
    if (!s->is_pipe) {
208 22b37f5d Fabrice Bellard
        /* loop over input */
209
        if (loop_input && s->img_number > s->img_last) {
210
            s->img_number = s->img_first;
211
        }
212 87a0a681 Fabrice Bellard
        if (get_frame_filename(filename, sizeof(filename),
213
                               s->path, s->img_number) < 0)
214
            return -EIO;
215
        f = &f1;
216
        if (url_fopen(f, filename, URL_RDONLY) < 0)
217
            return -EIO;
218
    } else {
219
        f = &s1->pb;
220
        if (url_feof(f))
221
            return -EIO;
222
    }
223 6775c758 Fabrice Bellard
224 87a0a681 Fabrice Bellard
    av_new_packet(pkt, s->img_size);
225
    pkt->stream_index = 0;
226
227
    s->ptr = pkt->data;
228
    ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
229
    if (!s->is_pipe) {
230
        url_fclose(f);
231 6775c758 Fabrice Bellard
    }
232
233 87a0a681 Fabrice Bellard
    if (ret < 0) {
234
        av_free_packet(pkt);
235
        return -EIO; /* signal EOF */
236
    } else {
237 22b37f5d Fabrice Bellard
        /* XXX: computing this pts is not necessary as it is done in
238
           the generic code too */
239
        pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec.frame_rate_base, s1->pts_den, s1->streams[0]->codec.frame_rate) / s1->pts_num;
240
        s->img_count++;
241 87a0a681 Fabrice Bellard
        s->img_number++;
242
        return 0;
243
    }
244 6775c758 Fabrice Bellard
}
245
246 87a0a681 Fabrice Bellard
static int img_read_close(AVFormatContext *s1)
247 de6d9b64 Fabrice Bellard
{
248
    return 0;
249
}
250
251 87a0a681 Fabrice Bellard
/******************************************************/
252
/* image output */
253
254
static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
255 ad436907 Henry Mason
{
256 87a0a681 Fabrice Bellard
    VideoData *img = s->priv_data;
257
    AVStream *st;
258
    AVImageFormat *img_fmt;
259
    int i;
260
261
    /* find output image format */
262
    if (ap && ap->image_format) {
263
        img_fmt = ap->image_format;
264 ad436907 Henry Mason
    } else {
265 87a0a681 Fabrice Bellard
        img_fmt = guess_image_format(s->filename);
266 ad436907 Henry Mason
    }
267 87a0a681 Fabrice Bellard
    if (!img_fmt)
268
        return -1;
269 ad436907 Henry Mason
270 87a0a681 Fabrice Bellard
    if (s->nb_streams != 1)
271
        return -1;
272
    
273
    st = s->streams[0];
274
    /* we select the first matching format */
275
    for(i=0;i<PIX_FMT_NB;i++) {
276
        if (img_fmt->supported_pixel_formats & (1 << i))
277
            break;
278 ad436907 Henry Mason
    }
279 87a0a681 Fabrice Bellard
    if (i >= PIX_FMT_NB)
280
        return -1;
281
    img->img_fmt = img_fmt;
282
    img->pix_fmt = i;
283
    st->codec.pix_fmt = img->pix_fmt;
284 ad436907 Henry Mason
    return 0;
285
}
286
287 de6d9b64 Fabrice Bellard
static int img_write_header(AVFormatContext *s)
288
{
289 c9a65ca8 Fabrice Bellard
    VideoData *img = s->priv_data;
290 de6d9b64 Fabrice Bellard
291
    img->img_number = 1;
292
    strcpy(img->path, s->filename);
293
294
    /* find format */
295 c9a65ca8 Fabrice Bellard
    if (s->oformat->flags & AVFMT_NOFILE)
296 de6d9b64 Fabrice Bellard
        img->is_pipe = 0;
297
    else
298
        img->is_pipe = 1;
299
        
300
    return 0;
301
}
302
303
static int img_write_packet(AVFormatContext *s, int stream_index,
304 49057904 Fabrice Bellard
                            const uint8_t *buf, int size, int64_t pts)
305 de6d9b64 Fabrice Bellard
{
306
    VideoData *img = s->priv_data;
307
    AVStream *st = s->streams[stream_index];
308
    ByteIOContext pb1, *pb;
309 87a0a681 Fabrice Bellard
    AVPicture *picture;
310
    int width, height, ret;
311 de6d9b64 Fabrice Bellard
    char filename[1024];
312 87a0a681 Fabrice Bellard
    AVImageInfo info;
313 de6d9b64 Fabrice Bellard
314
    width = st->codec.width;
315
    height = st->codec.height;
316 ad436907 Henry Mason
    
317 87a0a681 Fabrice Bellard
    picture = (AVPicture *)buf;
318 de6d9b64 Fabrice Bellard
319
    if (!img->is_pipe) {
320 87a0a681 Fabrice Bellard
        if (get_frame_filename(filename, sizeof(filename), 
321
                               img->path, img->img_number) < 0)
322
            return -EIO;
323 de6d9b64 Fabrice Bellard
        pb = &pb1;
324
        if (url_fopen(pb, filename, URL_WRONLY) < 0)
325
            return -EIO;
326
    } else {
327
        pb = &s->pb;
328
    }
329 87a0a681 Fabrice Bellard
    info.width = width;
330
    info.height = height;
331
    info.pix_fmt = st->codec.pix_fmt;
332 ffaa816e Michael Niedermayer
    info.interleaved = 0;    /* FIXME: there should be a way to set it right */
333 87a0a681 Fabrice Bellard
    info.pict = *picture;
334
    ret = av_write_image(pb, img->img_fmt, &info);
335 de6d9b64 Fabrice Bellard
    if (!img->is_pipe) {
336
        url_fclose(pb);
337
    }
338
339
    img->img_number++;
340
    return 0;
341
}
342
343
static int img_write_trailer(AVFormatContext *s)
344
{
345
    return 0;
346
}
347
348 87a0a681 Fabrice Bellard
/* input */
349 de6d9b64 Fabrice Bellard
350 87a0a681 Fabrice Bellard
static AVInputFormat image_iformat = {
351
    "image",
352
    "image sequence",
353 c9a65ca8 Fabrice Bellard
    sizeof(VideoData),
354 87a0a681 Fabrice Bellard
    image_probe,
355 de6d9b64 Fabrice Bellard
    img_read_header,
356
    img_read_packet,
357
    img_read_close,
358
    NULL,
359 9150f42e Fabrice Bellard
    AVFMT_NOFILE | AVFMT_NEEDNUMBER,
360 de6d9b64 Fabrice Bellard
};
361
362 87a0a681 Fabrice Bellard
static AVInputFormat imagepipe_iformat = {
363
    "imagepipe",
364
    "piped image sequence",
365 c9a65ca8 Fabrice Bellard
    sizeof(VideoData),
366
    NULL, /* no probe */
367 de6d9b64 Fabrice Bellard
    img_read_header,
368
    img_read_packet,
369
    img_read_close,
370
    NULL,
371
};
372
373
374 87a0a681 Fabrice Bellard
/* output */
375 6775c758 Fabrice Bellard
376 87a0a681 Fabrice Bellard
static AVOutputFormat image_oformat = {
377
    "image",
378
    "image sequence",
379 6775c758 Fabrice Bellard
    "",
380
    "",
381 c9a65ca8 Fabrice Bellard
    sizeof(VideoData),
382 6775c758 Fabrice Bellard
    CODEC_ID_NONE,
383
    CODEC_ID_RAWVIDEO,
384
    img_write_header,
385
    img_write_packet,
386
    img_write_trailer,
387 87a0a681 Fabrice Bellard
    AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
388
    img_set_parameters,
389 6775c758 Fabrice Bellard
};
390 c9a65ca8 Fabrice Bellard
391 87a0a681 Fabrice Bellard
static AVOutputFormat imagepipe_oformat = {
392
    "imagepipe",
393
    "piped image sequence",
394
    "",
395 ad436907 Henry Mason
    "",
396
    sizeof(VideoData),
397
    CODEC_ID_NONE,
398
    CODEC_ID_RAWVIDEO,
399
    img_write_header,
400
    img_write_packet,
401
    img_write_trailer,
402 94d883e8 Fabrice Bellard
    AVFMT_RAWPICTURE,
403 87a0a681 Fabrice Bellard
    img_set_parameters,
404 ad436907 Henry Mason
};
405
406 c9a65ca8 Fabrice Bellard
int img_init(void)
407
{
408 87a0a681 Fabrice Bellard
    av_register_input_format(&image_iformat);
409
    av_register_output_format(&image_oformat);
410 c9a65ca8 Fabrice Bellard
411 87a0a681 Fabrice Bellard
    av_register_input_format(&imagepipe_iformat);
412
    av_register_output_format(&imagepipe_oformat);
413 ad436907 Henry Mason
    
414 c9a65ca8 Fabrice Bellard
    return 0;
415
}