ffmpeg / libavformat / img.c @ 5509bffa
History | View | Annotate | Download (9.9 KB)
1 |
/*
|
---|---|
2 |
* Image format
|
3 |
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
|
4 |
*
|
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.
|
9 |
*
|
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.
|
14 |
*
|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18 |
*/
|
19 |
#include "avformat.h" |
20 |
|
21 |
/* XXX: this is a hack */
|
22 |
int loop_input = 0; |
23 |
|
24 |
typedef struct { |
25 |
int width;
|
26 |
int height;
|
27 |
int img_first;
|
28 |
int img_last;
|
29 |
int img_number;
|
30 |
int img_count;
|
31 |
int img_size;
|
32 |
AVImageFormat *img_fmt; |
33 |
int pix_fmt;
|
34 |
int is_pipe;
|
35 |
char path[1024]; |
36 |
/* temporary usage */
|
37 |
void *ptr;
|
38 |
} VideoData; |
39 |
|
40 |
|
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 |
static int image_probe(AVProbeData *p) |
91 |
{ |
92 |
if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename)) |
93 |
return AVPROBE_SCORE_MAX-1; |
94 |
else
|
95 |
return 0; |
96 |
} |
97 |
|
98 |
static int read_header_alloc_cb(void *opaque, AVImageInfo *info) |
99 |
{ |
100 |
VideoData *s = opaque; |
101 |
|
102 |
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 |
} |
108 |
|
109 |
static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap) |
110 |
{ |
111 |
VideoData *s = s1->priv_data; |
112 |
int ret, first_index, last_index;
|
113 |
char buf[1024]; |
114 |
ByteIOContext pb1, *f = &pb1; |
115 |
AVStream *st; |
116 |
|
117 |
st = av_new_stream(s1, 0);
|
118 |
if (!st) {
|
119 |
av_free(s); |
120 |
return -ENOMEM;
|
121 |
} |
122 |
|
123 |
if (ap && ap->image_format)
|
124 |
s->img_fmt = ap->image_format; |
125 |
|
126 |
pstrcpy(s->path, sizeof(s->path), s1->filename);
|
127 |
s->img_number = 0;
|
128 |
s->img_count = 0;
|
129 |
|
130 |
/* find format */
|
131 |
if (s1->iformat->flags & AVFMT_NOFILE)
|
132 |
s->is_pipe = 0;
|
133 |
else
|
134 |
s->is_pipe = 1;
|
135 |
|
136 |
if (!ap || !ap->time_base.num) {
|
137 |
st->codec->time_base= (AVRational){1,25}; |
138 |
} else {
|
139 |
st->codec->time_base= ap->time_base; |
140 |
} |
141 |
|
142 |
if (!s->is_pipe) {
|
143 |
if (find_image_range(&first_index, &last_index, s->path) < 0) |
144 |
goto fail;
|
145 |
s->img_first = first_index; |
146 |
s->img_last = last_index; |
147 |
s->img_number = first_index; |
148 |
/* compute duration */
|
149 |
st->start_time = 0;
|
150 |
st->duration = last_index - first_index + 1;
|
151 |
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 |
goto fail;
|
155 |
} else {
|
156 |
f = &s1->pb; |
157 |
} |
158 |
|
159 |
ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s); |
160 |
if (ret < 0) |
161 |
goto fail1;
|
162 |
|
163 |
if (!s->is_pipe) {
|
164 |
url_fclose(f); |
165 |
} else {
|
166 |
url_fseek(f, 0, SEEK_SET);
|
167 |
} |
168 |
|
169 |
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 |
s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15)); |
175 |
|
176 |
return 0; |
177 |
fail1:
|
178 |
if (!s->is_pipe)
|
179 |
url_fclose(f); |
180 |
fail:
|
181 |
av_free(s); |
182 |
return AVERROR_IO;
|
183 |
} |
184 |
|
185 |
static int read_packet_alloc_cb(void *opaque, AVImageInfo *info) |
186 |
{ |
187 |
VideoData *s = opaque; |
188 |
|
189 |
if (info->width != s->width ||
|
190 |
info->height != s->height) |
191 |
return -1; |
192 |
avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15)); |
193 |
return 0; |
194 |
} |
195 |
|
196 |
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt) |
197 |
{ |
198 |
VideoData *s = s1->priv_data; |
199 |
char filename[1024]; |
200 |
int ret;
|
201 |
ByteIOContext f1, *f; |
202 |
|
203 |
if (!s->is_pipe) {
|
204 |
/* loop over input */
|
205 |
if (loop_input && s->img_number > s->img_last) {
|
206 |
s->img_number = s->img_first; |
207 |
} |
208 |
if (get_frame_filename(filename, sizeof(filename), |
209 |
s->path, s->img_number) < 0)
|
210 |
return AVERROR_IO;
|
211 |
f = &f1; |
212 |
if (url_fopen(f, filename, URL_RDONLY) < 0) |
213 |
return AVERROR_IO;
|
214 |
} else {
|
215 |
f = &s1->pb; |
216 |
if (url_feof(f))
|
217 |
return AVERROR_IO;
|
218 |
} |
219 |
|
220 |
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 |
} |
228 |
|
229 |
if (ret < 0) { |
230 |
av_free_packet(pkt); |
231 |
return AVERROR_IO; /* signal EOF */ |
232 |
} else {
|
233 |
/* XXX: computing this pts is not necessary as it is done in
|
234 |
the generic code too */
|
235 |
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 |
s->img_count++; |
237 |
s->img_number++; |
238 |
return 0; |
239 |
} |
240 |
} |
241 |
|
242 |
static int img_read_close(AVFormatContext *s1) |
243 |
{ |
244 |
return 0; |
245 |
} |
246 |
|
247 |
/******************************************************/
|
248 |
/* image output */
|
249 |
|
250 |
static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
251 |
{ |
252 |
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 |
} else {
|
261 |
img_fmt = guess_image_format(s->filename); |
262 |
} |
263 |
if (!img_fmt)
|
264 |
return -1; |
265 |
|
266 |
if (s->nb_streams != 1) |
267 |
return -1; |
268 |
|
269 |
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 |
} |
275 |
if (i >= PIX_FMT_NB)
|
276 |
return -1; |
277 |
img->img_fmt = img_fmt; |
278 |
img->pix_fmt = i; |
279 |
st->codec->pix_fmt = img->pix_fmt; |
280 |
return 0; |
281 |
} |
282 |
|
283 |
static int img_write_header(AVFormatContext *s) |
284 |
{ |
285 |
VideoData *img = s->priv_data; |
286 |
|
287 |
img->img_number = 1;
|
288 |
pstrcpy(img->path, sizeof(img->path), s->filename);
|
289 |
|
290 |
/* find format */
|
291 |
if (s->oformat->flags & AVFMT_NOFILE)
|
292 |
img->is_pipe = 0;
|
293 |
else
|
294 |
img->is_pipe = 1;
|
295 |
|
296 |
return 0; |
297 |
} |
298 |
|
299 |
static int img_write_packet(AVFormatContext *s, AVPacket *pkt) |
300 |
{ |
301 |
VideoData *img = s->priv_data; |
302 |
AVStream *st = s->streams[pkt->stream_index]; |
303 |
ByteIOContext pb1, *pb; |
304 |
AVPicture *picture; |
305 |
int width, height, ret;
|
306 |
char filename[1024]; |
307 |
AVImageInfo info; |
308 |
|
309 |
width = st->codec->width; |
310 |
height = st->codec->height; |
311 |
|
312 |
picture = (AVPicture *)pkt->data; |
313 |
|
314 |
if (!img->is_pipe) {
|
315 |
if (get_frame_filename(filename, sizeof(filename), |
316 |
img->path, img->img_number) < 0)
|
317 |
return AVERROR_IO;
|
318 |
pb = &pb1; |
319 |
if (url_fopen(pb, filename, URL_WRONLY) < 0) |
320 |
return AVERROR_IO;
|
321 |
} else {
|
322 |
pb = &s->pb; |
323 |
} |
324 |
info.width = width; |
325 |
info.height = height; |
326 |
info.pix_fmt = st->codec->pix_fmt; |
327 |
info.interleaved = 0; /* FIXME: there should be a way to set it right */ |
328 |
info.pict = *picture; |
329 |
ret = av_write_image(pb, img->img_fmt, &info); |
330 |
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 |
/* input */
|
344 |
|
345 |
static AVInputFormat image_iformat = {
|
346 |
"image",
|
347 |
"image sequence",
|
348 |
sizeof(VideoData),
|
349 |
image_probe, |
350 |
img_read_header, |
351 |
img_read_packet, |
352 |
img_read_close, |
353 |
NULL,
|
354 |
NULL,
|
355 |
AVFMT_NOFILE | AVFMT_NEEDNUMBER, |
356 |
}; |
357 |
|
358 |
static AVInputFormat imagepipe_iformat = {
|
359 |
"imagepipe",
|
360 |
"piped image sequence",
|
361 |
sizeof(VideoData),
|
362 |
NULL, /* no probe */ |
363 |
img_read_header, |
364 |
img_read_packet, |
365 |
img_read_close, |
366 |
NULL,
|
367 |
}; |
368 |
|
369 |
|
370 |
/* output */
|
371 |
|
372 |
static AVOutputFormat image_oformat = {
|
373 |
"image",
|
374 |
"image sequence",
|
375 |
"",
|
376 |
"",
|
377 |
sizeof(VideoData),
|
378 |
CODEC_ID_NONE, |
379 |
CODEC_ID_RAWVIDEO, |
380 |
img_write_header, |
381 |
img_write_packet, |
382 |
img_write_trailer, |
383 |
AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE, |
384 |
img_set_parameters, |
385 |
}; |
386 |
|
387 |
static AVOutputFormat imagepipe_oformat = {
|
388 |
"imagepipe",
|
389 |
"piped image sequence",
|
390 |
"",
|
391 |
"",
|
392 |
sizeof(VideoData),
|
393 |
CODEC_ID_NONE, |
394 |
CODEC_ID_RAWVIDEO, |
395 |
img_write_header, |
396 |
img_write_packet, |
397 |
img_write_trailer, |
398 |
AVFMT_RAWPICTURE, |
399 |
img_set_parameters, |
400 |
}; |
401 |
|
402 |
int img_init(void) |
403 |
{ |
404 |
av_register_input_format(&image_iformat); |
405 |
av_register_output_format(&image_oformat); |
406 |
|
407 |
av_register_input_format(&imagepipe_iformat); |
408 |
av_register_output_format(&imagepipe_oformat); |
409 |
|
410 |
return 0; |
411 |
} |