3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
5 * 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.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * 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
21 #include "os_support.h"
28 AVImageFormat
*img_fmt
;
36 static int image_probe(AVProbeData
*p
)
38 if (filename_number_test(p
->filename
) >= 0 && guess_image_format(p
->filename
))
39 return AVPROBE_SCORE_MAX
;
44 static int read_header_alloc_cb(void *opaque
, AVImageInfo
*info
)
46 VideoData
*s
= opaque
;
48 s
->width
= info
->width
;
49 s
->height
= info
->height
;
50 s
->pix_fmt
= info
->pix_fmt
;
51 /* stop image reading but no error */
55 static int img_read_header(AVFormatContext
*s1
, AVFormatParameters
*ap
)
57 VideoData
*s
= s1
->priv_data
;
60 ByteIOContext pb1
, *f
= &pb1
;
63 st
= av_new_stream(s1
, 0);
69 if (ap
&& ap
->image_format
)
70 s
->img_fmt
= ap
->image_format
;
72 strcpy(s
->path
, s1
->filename
);
76 if (s1
->iformat
->flags
& AVFMT_NOFILE
)
82 /* try to find the first image */
84 if (get_frame_filename(buf
, sizeof(buf
), s
->path
, s
->img_number
) < 0)
86 if (url_fopen(f
, buf
, URL_RDONLY
) >= 0)
96 ret
= av_read_image(f
, s1
->filename
, s
->img_fmt
, read_header_alloc_cb
, s
);
103 url_fseek(f
, 0, SEEK_SET
);
106 st
->codec
.codec_type
= CODEC_TYPE_VIDEO
;
107 st
->codec
.codec_id
= CODEC_ID_RAWVIDEO
;
108 st
->codec
.width
= s
->width
;
109 st
->codec
.height
= s
->height
;
110 st
->codec
.pix_fmt
= s
->pix_fmt
;
111 s
->img_size
= avpicture_get_size(s
->pix_fmt
, s
->width
, s
->height
);
113 if (!ap
|| !ap
->frame_rate
){
114 st
->codec
.frame_rate
= 25;
115 st
->codec
.frame_rate_base
= 1;
117 st
->codec
.frame_rate
= ap
->frame_rate
;
118 st
->codec
.frame_rate_base
= ap
->frame_rate_base
;
130 static int read_packet_alloc_cb(void *opaque
, AVImageInfo
*info
)
132 VideoData
*s
= opaque
;
134 if (info
->width
!= s
->width
||
135 info
->height
!= s
->height
)
137 avpicture_fill(&info
->pict
, s
->ptr
, info
->pix_fmt
, info
->width
, info
->height
);
141 static int img_read_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
143 VideoData
*s
= s1
->priv_data
;
146 ByteIOContext f1
, *f
;
149 if (get_frame_filename(filename
, sizeof(filename
),
150 s
->path
, s
->img_number
) < 0)
153 if (url_fopen(f
, filename
, URL_RDONLY
) < 0)
161 av_new_packet(pkt
, s
->img_size
);
162 pkt
->stream_index
= 0;
165 ret
= av_read_image(f
, filename
, s
->img_fmt
, read_packet_alloc_cb
, s
);
172 return -EIO
; /* signal EOF */
174 pkt
->pts
= av_rescale((int64_t)s
->img_number
* s1
->streams
[0]->codec
.frame_rate_base
, s1
->pts_den
, s1
->streams
[0]->codec
.frame_rate
) / s1
->pts_num
;
180 static int img_read_close(AVFormatContext
*s1
)
185 /******************************************************/
188 static int img_set_parameters(AVFormatContext
*s
, AVFormatParameters
*ap
)
190 VideoData
*img
= s
->priv_data
;
192 AVImageFormat
*img_fmt
;
195 /* find output image format */
196 if (ap
&& ap
->image_format
) {
197 img_fmt
= ap
->image_format
;
199 img_fmt
= guess_image_format(s
->filename
);
204 if (s
->nb_streams
!= 1)
208 /* we select the first matching format */
209 for(i
=0;i
<PIX_FMT_NB
;i
++) {
210 if (img_fmt
->supported_pixel_formats
& (1 << i
))
215 img
->img_fmt
= img_fmt
;
217 st
->codec
.pix_fmt
= img
->pix_fmt
;
221 static int img_write_header(AVFormatContext
*s
)
223 VideoData
*img
= s
->priv_data
;
226 strcpy(img
->path
, s
->filename
);
229 if (s
->oformat
->flags
& AVFMT_NOFILE
)
237 static int img_write_packet(AVFormatContext
*s
, int stream_index
,
238 uint8_t *buf
, int size
, int force_pts
)
240 VideoData
*img
= s
->priv_data
;
241 AVStream
*st
= s
->streams
[stream_index
];
242 ByteIOContext pb1
, *pb
;
244 int width
, height
, ret
;
248 width
= st
->codec
.width
;
249 height
= st
->codec
.height
;
251 picture
= (AVPicture
*)buf
;
254 if (get_frame_filename(filename
, sizeof(filename
),
255 img
->path
, img
->img_number
) < 0)
258 if (url_fopen(pb
, filename
, URL_WRONLY
) < 0)
264 info
.height
= height
;
265 info
.pix_fmt
= st
->codec
.pix_fmt
;
266 info
.pict
= *picture
;
267 ret
= av_write_image(pb
, img
->img_fmt
, &info
);
276 static int img_write_trailer(AVFormatContext
*s
)
283 static AVInputFormat image_iformat
= {
292 AVFMT_NOFILE
| AVFMT_NEEDNUMBER
,
295 static AVInputFormat imagepipe_iformat
= {
297 "piped image sequence",
309 static AVOutputFormat image_oformat
= {
320 AVFMT_NOFILE
| AVFMT_NEEDNUMBER
| AVFMT_RAWPICTURE
,
324 static AVOutputFormat imagepipe_oformat
= {
326 "piped image sequence",
341 av_register_input_format(&image_iformat
);
342 av_register_output_format(&image_oformat
);
344 av_register_input_format(&imagepipe_iformat
);
345 av_register_output_format(&imagepipe_oformat
);