Statistics
| Branch: | Revision:

ffmpeg / libavformat / img.c @ 5509bffa

History | View | Annotate | Download (9.9 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 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 115329f1 Diego Biurrun
static int find_image_range(int *pfirst_index, int *plast_index,
43 ca1538f8 Fabrice Bellard
                            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 115329f1 Diego Biurrun
58 ca1538f8 Fabrice Bellard
    /* 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 115329f1 Diego Biurrun
            if (get_frame_filename(buf, sizeof(buf), path,
68 ca1538f8 Fabrice Bellard
                                   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 f4824c0f Michael Niedermayer
        return AVPROBE_SCORE_MAX-1;
94 87a0a681 Fabrice Bellard
    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 568e18b1 Michael Niedermayer
    pstrcpy(s->path, sizeof(s->path), s1->filename);
127 de6d9b64 Fabrice Bellard
    s->img_number = 0;
128 22b37f5d Fabrice Bellard
    s->img_count = 0;
129 115329f1 Diego Biurrun
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 c0df9d75 Michael Niedermayer
136
    if (!ap || !ap->time_base.num) {
137 01f4895c Michael Niedermayer
        st->codec->time_base= (AVRational){1,25};
138 ca1538f8 Fabrice Bellard
    } else {
139 01f4895c Michael Niedermayer
        st->codec->time_base= ap->time_base;
140 ca1538f8 Fabrice Bellard
    }
141 115329f1 Diego Biurrun
142 de6d9b64 Fabrice Bellard
    if (!s->is_pipe) {
143 ca1538f8 Fabrice Bellard
        if (find_image_range(&first_index, &last_index, s->path) < 0)
144
            goto fail;
145 22b37f5d Fabrice Bellard
        s->img_first = first_index;
146
        s->img_last = last_index;
147 ca1538f8 Fabrice Bellard
        s->img_number = first_index;
148
        /* compute duration */
149
        st->start_time = 0;
150 c0df9d75 Michael Niedermayer
        st->duration = last_index - first_index + 1;
151 ca1538f8 Fabrice Bellard
        if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
152
            goto fail;
153
        if (url_fopen(f, buf, URL_RDONLY) < 0)
154 de6d9b64 Fabrice Bellard
            goto fail;
155
    } else {
156
        f = &s1->pb;
157
    }
158 115329f1 Diego Biurrun
159 87a0a681 Fabrice Bellard
    ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
160
    if (ret < 0)
161
        goto fail1;
162 de6d9b64 Fabrice Bellard
163
    if (!s->is_pipe) {
164
        url_fclose(f);
165
    } else {
166
        url_fseek(f, 0, SEEK_SET);
167
    }
168 115329f1 Diego Biurrun
169 01f4895c Michael Niedermayer
    st->codec->codec_type = CODEC_TYPE_VIDEO;
170
    st->codec->codec_id = CODEC_ID_RAWVIDEO;
171
    st->codec->width = s->width;
172
    st->codec->height = s->height;
173
    st->codec->pix_fmt = s->pix_fmt;
174 7f2ca68a Michael Niedermayer
    s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15));
175 87a0a681 Fabrice Bellard
176 de6d9b64 Fabrice Bellard
    return 0;
177
 fail1:
178
    if (!s->is_pipe)
179
        url_fclose(f);
180
 fail:
181 1ea4f593 Fabrice Bellard
    av_free(s);
182 0bd586c5 Mike Melanson
    return AVERROR_IO;
183 de6d9b64 Fabrice Bellard
}
184
185 87a0a681 Fabrice Bellard
static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
186 de6d9b64 Fabrice Bellard
{
187 87a0a681 Fabrice Bellard
    VideoData *s = opaque;
188
189
    if (info->width != s->width ||
190
        info->height != s->height)
191
        return -1;
192 7f2ca68a Michael Niedermayer
    avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15));
193 de6d9b64 Fabrice Bellard
    return 0;
194
}
195
196 87a0a681 Fabrice Bellard
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
197 de6d9b64 Fabrice Bellard
{
198 87a0a681 Fabrice Bellard
    VideoData *s = s1->priv_data;
199
    char filename[1024];
200
    int ret;
201
    ByteIOContext f1, *f;
202 de6d9b64 Fabrice Bellard
203 87a0a681 Fabrice Bellard
    if (!s->is_pipe) {
204 22b37f5d Fabrice Bellard
        /* loop over input */
205
        if (loop_input && s->img_number > s->img_last) {
206
            s->img_number = s->img_first;
207
        }
208 87a0a681 Fabrice Bellard
        if (get_frame_filename(filename, sizeof(filename),
209
                               s->path, s->img_number) < 0)
210 0bd586c5 Mike Melanson
            return AVERROR_IO;
211 87a0a681 Fabrice Bellard
        f = &f1;
212
        if (url_fopen(f, filename, URL_RDONLY) < 0)
213 0bd586c5 Mike Melanson
            return AVERROR_IO;
214 87a0a681 Fabrice Bellard
    } else {
215
        f = &s1->pb;
216
        if (url_feof(f))
217 0bd586c5 Mike Melanson
            return AVERROR_IO;
218 87a0a681 Fabrice Bellard
    }
219 6775c758 Fabrice Bellard
220 87a0a681 Fabrice Bellard
    av_new_packet(pkt, s->img_size);
221
    pkt->stream_index = 0;
222
223
    s->ptr = pkt->data;
224
    ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
225
    if (!s->is_pipe) {
226
        url_fclose(f);
227 6775c758 Fabrice Bellard
    }
228
229 87a0a681 Fabrice Bellard
    if (ret < 0) {
230
        av_free_packet(pkt);
231 0bd586c5 Mike Melanson
        return AVERROR_IO; /* signal EOF */
232 87a0a681 Fabrice Bellard
    } else {
233 22b37f5d Fabrice Bellard
        /* XXX: computing this pts is not necessary as it is done in
234
           the generic code too */
235 01f4895c Michael Niedermayer
        pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec->time_base.num, s1->streams[0]->time_base.den, s1->streams[0]->codec->time_base.den) / s1->streams[0]->time_base.num;
236 22b37f5d Fabrice Bellard
        s->img_count++;
237 87a0a681 Fabrice Bellard
        s->img_number++;
238
        return 0;
239
    }
240 6775c758 Fabrice Bellard
}
241
242 87a0a681 Fabrice Bellard
static int img_read_close(AVFormatContext *s1)
243 de6d9b64 Fabrice Bellard
{
244
    return 0;
245
}
246
247 87a0a681 Fabrice Bellard
/******************************************************/
248
/* image output */
249
250
static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
251 ad436907 Henry Mason
{
252 87a0a681 Fabrice Bellard
    VideoData *img = s->priv_data;
253
    AVStream *st;
254
    AVImageFormat *img_fmt;
255
    int i;
256
257
    /* find output image format */
258
    if (ap && ap->image_format) {
259
        img_fmt = ap->image_format;
260 ad436907 Henry Mason
    } else {
261 87a0a681 Fabrice Bellard
        img_fmt = guess_image_format(s->filename);
262 ad436907 Henry Mason
    }
263 87a0a681 Fabrice Bellard
    if (!img_fmt)
264
        return -1;
265 ad436907 Henry Mason
266 87a0a681 Fabrice Bellard
    if (s->nb_streams != 1)
267
        return -1;
268 115329f1 Diego Biurrun
269 87a0a681 Fabrice Bellard
    st = s->streams[0];
270
    /* we select the first matching format */
271
    for(i=0;i<PIX_FMT_NB;i++) {
272
        if (img_fmt->supported_pixel_formats & (1 << i))
273
            break;
274 ad436907 Henry Mason
    }
275 87a0a681 Fabrice Bellard
    if (i >= PIX_FMT_NB)
276
        return -1;
277
    img->img_fmt = img_fmt;
278
    img->pix_fmt = i;
279 01f4895c Michael Niedermayer
    st->codec->pix_fmt = img->pix_fmt;
280 ad436907 Henry Mason
    return 0;
281
}
282
283 de6d9b64 Fabrice Bellard
static int img_write_header(AVFormatContext *s)
284
{
285 c9a65ca8 Fabrice Bellard
    VideoData *img = s->priv_data;
286 de6d9b64 Fabrice Bellard
287
    img->img_number = 1;
288 568e18b1 Michael Niedermayer
    pstrcpy(img->path, sizeof(img->path), s->filename);
289 de6d9b64 Fabrice Bellard
290
    /* find format */
291 c9a65ca8 Fabrice Bellard
    if (s->oformat->flags & AVFMT_NOFILE)
292 de6d9b64 Fabrice Bellard
        img->is_pipe = 0;
293
    else
294
        img->is_pipe = 1;
295 115329f1 Diego Biurrun
296 de6d9b64 Fabrice Bellard
    return 0;
297
}
298
299 e928649b Michael Niedermayer
static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
300 de6d9b64 Fabrice Bellard
{
301
    VideoData *img = s->priv_data;
302 e928649b Michael Niedermayer
    AVStream *st = s->streams[pkt->stream_index];
303 de6d9b64 Fabrice Bellard
    ByteIOContext pb1, *pb;
304 87a0a681 Fabrice Bellard
    AVPicture *picture;
305
    int width, height, ret;
306 de6d9b64 Fabrice Bellard
    char filename[1024];
307 87a0a681 Fabrice Bellard
    AVImageInfo info;
308 de6d9b64 Fabrice Bellard
309 01f4895c Michael Niedermayer
    width = st->codec->width;
310
    height = st->codec->height;
311 115329f1 Diego Biurrun
312 e928649b Michael Niedermayer
    picture = (AVPicture *)pkt->data;
313 de6d9b64 Fabrice Bellard
314
    if (!img->is_pipe) {
315 115329f1 Diego Biurrun
        if (get_frame_filename(filename, sizeof(filename),
316 87a0a681 Fabrice Bellard
                               img->path, img->img_number) < 0)
317 0bd586c5 Mike Melanson
            return AVERROR_IO;
318 de6d9b64 Fabrice Bellard
        pb = &pb1;
319
        if (url_fopen(pb, filename, URL_WRONLY) < 0)
320 0bd586c5 Mike Melanson
            return AVERROR_IO;
321 de6d9b64 Fabrice Bellard
    } else {
322
        pb = &s->pb;
323
    }
324 87a0a681 Fabrice Bellard
    info.width = width;
325
    info.height = height;
326 01f4895c Michael Niedermayer
    info.pix_fmt = st->codec->pix_fmt;
327 ffaa816e Michael Niedermayer
    info.interleaved = 0;    /* FIXME: there should be a way to set it right */
328 87a0a681 Fabrice Bellard
    info.pict = *picture;
329
    ret = av_write_image(pb, img->img_fmt, &info);
330 de6d9b64 Fabrice Bellard
    if (!img->is_pipe) {
331
        url_fclose(pb);
332
    }
333
334
    img->img_number++;
335
    return 0;
336
}
337
338
static int img_write_trailer(AVFormatContext *s)
339
{
340
    return 0;
341
}
342
343 87a0a681 Fabrice Bellard
/* input */
344 de6d9b64 Fabrice Bellard
345 87a0a681 Fabrice Bellard
static AVInputFormat image_iformat = {
346
    "image",
347
    "image sequence",
348 c9a65ca8 Fabrice Bellard
    sizeof(VideoData),
349 87a0a681 Fabrice Bellard
    image_probe,
350 de6d9b64 Fabrice Bellard
    img_read_header,
351
    img_read_packet,
352
    img_read_close,
353
    NULL,
354 e5d64d68 Michael Niedermayer
    NULL,
355 9150f42e Fabrice Bellard
    AVFMT_NOFILE | AVFMT_NEEDNUMBER,
356 de6d9b64 Fabrice Bellard
};
357
358 87a0a681 Fabrice Bellard
static AVInputFormat imagepipe_iformat = {
359
    "imagepipe",
360
    "piped image sequence",
361 c9a65ca8 Fabrice Bellard
    sizeof(VideoData),
362
    NULL, /* no probe */
363 de6d9b64 Fabrice Bellard
    img_read_header,
364
    img_read_packet,
365
    img_read_close,
366
    NULL,
367
};
368
369
370 87a0a681 Fabrice Bellard
/* output */
371 6775c758 Fabrice Bellard
372 87a0a681 Fabrice Bellard
static AVOutputFormat image_oformat = {
373
    "image",
374
    "image sequence",
375 6775c758 Fabrice Bellard
    "",
376
    "",
377 c9a65ca8 Fabrice Bellard
    sizeof(VideoData),
378 6775c758 Fabrice Bellard
    CODEC_ID_NONE,
379
    CODEC_ID_RAWVIDEO,
380
    img_write_header,
381
    img_write_packet,
382
    img_write_trailer,
383 87a0a681 Fabrice Bellard
    AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
384
    img_set_parameters,
385 6775c758 Fabrice Bellard
};
386 c9a65ca8 Fabrice Bellard
387 87a0a681 Fabrice Bellard
static AVOutputFormat imagepipe_oformat = {
388
    "imagepipe",
389
    "piped image sequence",
390
    "",
391 ad436907 Henry Mason
    "",
392
    sizeof(VideoData),
393
    CODEC_ID_NONE,
394
    CODEC_ID_RAWVIDEO,
395
    img_write_header,
396
    img_write_packet,
397
    img_write_trailer,
398 94d883e8 Fabrice Bellard
    AVFMT_RAWPICTURE,
399 87a0a681 Fabrice Bellard
    img_set_parameters,
400 ad436907 Henry Mason
};
401
402 c9a65ca8 Fabrice Bellard
int img_init(void)
403
{
404 87a0a681 Fabrice Bellard
    av_register_input_format(&image_iformat);
405
    av_register_output_format(&image_oformat);
406 c9a65ca8 Fabrice Bellard
407 87a0a681 Fabrice Bellard
    av_register_input_format(&imagepipe_iformat);
408
    av_register_output_format(&imagepipe_oformat);
409 115329f1 Diego Biurrun
410 c9a65ca8 Fabrice Bellard
    return 0;
411
}