3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 AVImageFormat
*img_fmt
;
40 /* return -1 if no image found */
41 static int find_image_range(int *pfirst_index
, int *plast_index
,
45 int range
, last_index
, range1
, first_index
;
47 /* find the first image */
48 for(first_index
= 0; first_index
< 5; first_index
++) {
49 if (av_get_frame_filename(buf
, sizeof(buf
), path
, first_index
) < 0)
57 /* find the last image */
58 last_index
= first_index
;
66 if (av_get_frame_filename(buf
, sizeof(buf
), path
,
67 last_index
+ range1
) < 0)
73 if (range
>= (1 << 30))
76 /* we are sure than image last_index + range exists */
81 *pfirst_index
= first_index
;
82 *plast_index
= last_index
;
89 static int image_probe(AVProbeData
*p
)
91 if (av_filename_number_test(p
->filename
) && guess_image_format(p
->filename
))
92 return AVPROBE_SCORE_MAX
-1;
97 static int read_header_alloc_cb(void *opaque
, AVImageInfo
*info
)
99 VideoData
*s
= opaque
;
101 s
->width
= info
->width
;
102 s
->height
= info
->height
;
103 s
->pix_fmt
= info
->pix_fmt
;
104 /* stop image reading but no error */
108 static int img_read_header(AVFormatContext
*s1
, AVFormatParameters
*ap
)
110 VideoData
*s
= s1
->priv_data
;
111 int ret
, first_index
, last_index
;
113 ByteIOContext pb1
, *f
= &pb1
;
116 st
= av_new_stream(s1
, 0);
121 if (ap
->image_format
)
122 s
->img_fmt
= ap
->image_format
;
124 pstrcpy(s
->path
, sizeof(s
->path
), s1
->filename
);
129 if (s1
->iformat
->flags
& AVFMT_NOFILE
)
134 if (!ap
->time_base
.num
) {
135 st
->codec
->time_base
= (AVRational
){1,25};
137 st
->codec
->time_base
= ap
->time_base
;
141 if (find_image_range(&first_index
, &last_index
, s
->path
) < 0)
143 s
->img_first
= first_index
;
144 s
->img_last
= last_index
;
145 s
->img_number
= first_index
;
146 /* compute duration */
148 st
->duration
= last_index
- first_index
+ 1;
149 if (av_get_frame_filename(buf
, sizeof(buf
), s
->path
, s
->img_number
) < 0)
151 if (url_fopen(f
, buf
, URL_RDONLY
) < 0)
157 ret
= av_read_image(f
, s1
->filename
, s
->img_fmt
, read_header_alloc_cb
, s
);
164 url_fseek(f
, 0, SEEK_SET
);
167 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
168 st
->codec
->codec_id
= CODEC_ID_RAWVIDEO
;
169 st
->codec
->width
= s
->width
;
170 st
->codec
->height
= s
->height
;
171 st
->codec
->pix_fmt
= s
->pix_fmt
;
172 s
->img_size
= avpicture_get_size(s
->pix_fmt
, (s
->width
+15)&(~15), (s
->height
+15)&(~15));
182 static int read_packet_alloc_cb(void *opaque
, AVImageInfo
*info
)
184 VideoData
*s
= opaque
;
186 if (info
->width
!= s
->width
||
187 info
->height
!= s
->height
)
189 avpicture_fill(&info
->pict
, s
->ptr
, info
->pix_fmt
, (info
->width
+15)&(~15), (info
->height
+15)&(~15));
193 static int img_read_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
195 VideoData
*s
= s1
->priv_data
;
198 ByteIOContext f1
, *f
;
201 /* loop over input */
202 if (s1
->loop_input
&& s
->img_number
> s
->img_last
) {
203 s
->img_number
= s
->img_first
;
205 if (av_get_frame_filename(filename
, sizeof(filename
),
206 s
->path
, s
->img_number
) < 0)
209 if (url_fopen(f
, filename
, URL_RDONLY
) < 0)
217 av_new_packet(pkt
, s
->img_size
);
218 pkt
->stream_index
= 0;
221 ret
= av_read_image(f
, filename
, s
->img_fmt
, read_packet_alloc_cb
, s
);
228 return AVERROR_IO
; /* signal EOF */
230 /* XXX: computing this pts is not necessary as it is done in
231 the generic code too */
232 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
;
239 static int img_read_close(AVFormatContext
*s1
)
244 /******************************************************/
247 static int img_set_parameters(AVFormatContext
*s
, AVFormatParameters
*ap
)
249 VideoData
*img
= s
->priv_data
;
251 AVImageFormat
*img_fmt
;
254 /* find output image format */
255 if (ap
->image_format
) {
256 img_fmt
= ap
->image_format
;
258 img_fmt
= guess_image_format(s
->filename
);
263 if (s
->nb_streams
!= 1)
267 /* we select the first matching format */
268 for(i
=0;i
<PIX_FMT_NB
;i
++) {
269 if (img_fmt
->supported_pixel_formats
& (1 << i
))
274 img
->img_fmt
= img_fmt
;
276 st
->codec
->pix_fmt
= img
->pix_fmt
;
280 static int img_write_header(AVFormatContext
*s
)
282 VideoData
*img
= s
->priv_data
;
285 pstrcpy(img
->path
, sizeof(img
->path
), s
->filename
);
288 if (s
->oformat
->flags
& AVFMT_NOFILE
)
296 static int img_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
298 VideoData
*img
= s
->priv_data
;
299 AVStream
*st
= s
->streams
[pkt
->stream_index
];
300 ByteIOContext pb1
, *pb
;
302 int width
, height
, ret
;
306 width
= st
->codec
->width
;
307 height
= st
->codec
->height
;
309 picture
= (AVPicture
*)pkt
->data
;
312 if (av_get_frame_filename(filename
, sizeof(filename
),
313 img
->path
, img
->img_number
) < 0)
316 if (url_fopen(pb
, filename
, URL_WRONLY
) < 0)
322 info
.height
= height
;
323 info
.pix_fmt
= st
->codec
->pix_fmt
;
324 info
.interleaved
= 0; /* FIXME: there should be a way to set it right */
325 info
.pict
= *picture
;
326 ret
= av_write_image(pb
, img
->img_fmt
, &info
);
335 static int img_write_trailer(AVFormatContext
*s
)
341 #ifdef CONFIG_IMAGE_DEMUXER
342 AVInputFormat image_demuxer
= {
352 AVFMT_NOFILE
| AVFMT_NEEDNUMBER
,
355 #ifdef CONFIG_IMAGEPIPE_DEMUXER
356 AVInputFormat imagepipe_demuxer
= {
358 "piped image sequence",
369 #ifdef CONFIG_IMAGE_MUXER
370 AVOutputFormat image_muxer
= {
381 AVFMT_NOFILE
| AVFMT_NEEDNUMBER
| AVFMT_RAWPICTURE
,
385 #ifdef CONFIG_IMAGEPIPE_MUXER
386 AVOutputFormat imagepipe_muxer
= {
388 "piped image sequence",