Mercurial > libavformat.hg
annotate img.c @ 506:74ad3aa6749c libavformat
2000/00/00 to 2001/03/01 QT fileformat documentation patch by (Sebastien Bechet <s.bechet at av7 dot net>)
author | michael |
---|---|
date | Wed, 28 Jul 2004 10:04:14 +0000 |
parents | 0fdc96c2f2fe |
children | c5ff083848b4 |
rev | line source |
---|---|
0 | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 #include "avformat.h" | |
11
932b59c66c60
mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents:
10
diff
changeset
|
20 |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
21 /* XXX: this is a hack */ |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
22 int loop_input = 0; |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
23 |
0 | 24 typedef struct { |
25 int width; | |
26 int height; | |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
27 int img_first; |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
28 int img_last; |
0 | 29 int img_number; |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
30 int img_count; |
0 | 31 int img_size; |
20 | 32 AVImageFormat *img_fmt; |
33 int pix_fmt; | |
0 | 34 int is_pipe; |
35 char path[1024]; | |
20 | 36 /* temporary usage */ |
37 void *ptr; | |
0 | 38 } VideoData; |
39 | |
189 | 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 | |
20 | 90 static int image_probe(AVProbeData *p) |
0 | 91 { |
21 | 92 if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename)) |
20 | 93 return AVPROBE_SCORE_MAX; |
94 else | |
95 return 0; | |
0 | 96 } |
97 | |
20 | 98 static int read_header_alloc_cb(void *opaque, AVImageInfo *info) |
0 | 99 { |
20 | 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; | |
0 | 107 } |
108 | |
20 | 109 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap) |
0 | 110 { |
20 | 111 VideoData *s = s1->priv_data; |
189 | 112 int ret, first_index, last_index; |
20 | 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 strcpy(s->path, s1->filename); | |
127 s->img_number = 0; | |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
128 s->img_count = 0; |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
129 |
20 | 130 /* find format */ |
131 if (s1->iformat->flags & AVFMT_NOFILE) | |
132 s->is_pipe = 0; | |
133 else | |
134 s->is_pipe = 1; | |
135 | |
189 | 136 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 | |
20 | 144 if (!s->is_pipe) { |
189 | 145 if (find_image_range(&first_index, &last_index, s->path) < 0) |
146 goto fail; | |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
147 s->img_first = first_index; |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
148 s->img_last = last_index; |
189 | 149 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) | |
20 | 158 goto fail; |
159 } else { | |
160 f = &s1->pb; | |
0 | 161 } |
162 | |
20 | 163 ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s); |
164 if (ret < 0) | |
165 goto fail1; | |
166 | |
167 if (!s->is_pipe) { | |
168 url_fclose(f); | |
169 } else { | |
170 url_fseek(f, 0, SEEK_SET); | |
171 } | |
0 | 172 |
20 | 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 st->codec.pix_fmt = s->pix_fmt; | |
459 | 178 s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15)); |
20 | 179 |
0 | 180 return 0; |
20 | 181 fail1: |
182 if (!s->is_pipe) | |
183 url_fclose(f); | |
184 fail: | |
185 av_free(s); | |
482 | 186 return AVERROR_IO; |
0 | 187 } |
188 | |
20 | 189 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info) |
0 | 190 { |
20 | 191 VideoData *s = opaque; |
0 | 192 |
20 | 193 if (info->width != s->width || |
194 info->height != s->height) | |
195 return -1; | |
459 | 196 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15)); |
0 | 197 return 0; |
198 } | |
199 | |
200 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt) | |
201 { | |
202 VideoData *s = s1->priv_data; | |
203 char filename[1024]; | |
204 int ret; | |
205 ByteIOContext f1, *f; | |
206 | |
207 if (!s->is_pipe) { | |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
208 /* loop over input */ |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
209 if (loop_input && s->img_number > s->img_last) { |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
210 s->img_number = s->img_first; |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
211 } |
20 | 212 if (get_frame_filename(filename, sizeof(filename), |
213 s->path, s->img_number) < 0) | |
482 | 214 return AVERROR_IO; |
0 | 215 f = &f1; |
216 if (url_fopen(f, filename, URL_RDONLY) < 0) | |
482 | 217 return AVERROR_IO; |
0 | 218 } else { |
219 f = &s1->pb; | |
220 if (url_feof(f)) | |
482 | 221 return AVERROR_IO; |
0 | 222 } |
223 | |
224 av_new_packet(pkt, s->img_size); | |
225 pkt->stream_index = 0; | |
226 | |
20 | 227 s->ptr = pkt->data; |
228 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s); | |
0 | 229 if (!s->is_pipe) { |
230 url_fclose(f); | |
231 } | |
232 | |
233 if (ret < 0) { | |
234 av_free_packet(pkt); | |
482 | 235 return AVERROR_IO; /* signal EOF */ |
0 | 236 } else { |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
237 /* XXX: computing this pts is not necessary as it is done in |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
238 the generic code too */ |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
459
diff
changeset
|
239 pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec.frame_rate_base, s1->streams[0]->time_base.den, s1->streams[0]->codec.frame_rate) / s1->streams[0]->time_base.num; |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
240 s->img_count++; |
0 | 241 s->img_number++; |
242 return 0; | |
243 } | |
244 } | |
245 | |
246 static int img_read_close(AVFormatContext *s1) | |
247 { | |
248 return 0; | |
249 } | |
250 | |
251 /******************************************************/ | |
252 /* image output */ | |
253 | |
20 | 254 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
0 | 255 { |
20 | 256 VideoData *img = s->priv_data; |
257 AVStream *st; | |
258 AVImageFormat *img_fmt; | |
0 | 259 int i; |
260 | |
20 | 261 /* find output image format */ |
262 if (ap && ap->image_format) { | |
263 img_fmt = ap->image_format; | |
264 } else { | |
265 img_fmt = guess_image_format(s->filename); | |
266 } | |
267 if (!img_fmt) | |
268 return -1; | |
0 | 269 |
20 | 270 if (s->nb_streams != 1) |
271 return -1; | |
0 | 272 |
20 | 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; | |
0 | 278 } |
20 | 279 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; | |
0 | 284 return 0; |
285 } | |
286 | |
287 static int img_write_header(AVFormatContext *s) | |
288 { | |
289 VideoData *img = s->priv_data; | |
290 | |
291 img->img_number = 1; | |
292 strcpy(img->path, s->filename); | |
293 | |
294 /* find format */ | |
295 if (s->oformat->flags & AVFMT_NOFILE) | |
296 img->is_pipe = 0; | |
297 else | |
298 img->is_pipe = 1; | |
299 | |
300 return 0; | |
301 } | |
302 | |
468 | 303 static int img_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 304 { |
305 VideoData *img = s->priv_data; | |
468 | 306 AVStream *st = s->streams[pkt->stream_index]; |
0 | 307 ByteIOContext pb1, *pb; |
20 | 308 AVPicture *picture; |
309 int width, height, ret; | |
0 | 310 char filename[1024]; |
20 | 311 AVImageInfo info; |
0 | 312 |
313 width = st->codec.width; | |
314 height = st->codec.height; | |
315 | |
468 | 316 picture = (AVPicture *)pkt->data; |
0 | 317 |
20 | 318 if (!img->is_pipe) { |
319 if (get_frame_filename(filename, sizeof(filename), | |
320 img->path, img->img_number) < 0) | |
482 | 321 return AVERROR_IO; |
0 | 322 pb = &pb1; |
323 if (url_fopen(pb, filename, URL_WRONLY) < 0) | |
482 | 324 return AVERROR_IO; |
0 | 325 } else { |
326 pb = &s->pb; | |
327 } | |
20 | 328 info.width = width; |
329 info.height = height; | |
330 info.pix_fmt = st->codec.pix_fmt; | |
280 | 331 info.interleaved = 0; /* FIXME: there should be a way to set it right */ |
20 | 332 info.pict = *picture; |
333 ret = av_write_image(pb, img->img_fmt, &info); | |
0 | 334 if (!img->is_pipe) { |
335 url_fclose(pb); | |
336 } | |
337 | |
338 img->img_number++; | |
339 return 0; | |
340 } | |
341 | |
342 static int img_write_trailer(AVFormatContext *s) | |
343 { | |
344 return 0; | |
345 } | |
346 | |
20 | 347 /* input */ |
0 | 348 |
20 | 349 static AVInputFormat image_iformat = { |
350 "image", | |
351 "image sequence", | |
0 | 352 sizeof(VideoData), |
20 | 353 image_probe, |
0 | 354 img_read_header, |
355 img_read_packet, | |
356 img_read_close, | |
357 NULL, | |
439 | 358 NULL, |
0 | 359 AVFMT_NOFILE | AVFMT_NEEDNUMBER, |
360 }; | |
361 | |
20 | 362 static AVInputFormat imagepipe_iformat = { |
363 "imagepipe", | |
364 "piped image sequence", | |
0 | 365 sizeof(VideoData), |
366 NULL, /* no probe */ | |
367 img_read_header, | |
368 img_read_packet, | |
369 img_read_close, | |
370 NULL, | |
371 }; | |
372 | |
20 | 373 |
374 /* output */ | |
0 | 375 |
20 | 376 static AVOutputFormat image_oformat = { |
377 "image", | |
378 "image sequence", | |
0 | 379 "", |
20 | 380 "", |
0 | 381 sizeof(VideoData), |
382 CODEC_ID_NONE, | |
383 CODEC_ID_RAWVIDEO, | |
384 img_write_header, | |
385 img_write_packet, | |
386 img_write_trailer, | |
20 | 387 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE, |
388 img_set_parameters, | |
0 | 389 }; |
390 | |
20 | 391 static AVOutputFormat imagepipe_oformat = { |
392 "imagepipe", | |
393 "piped image sequence", | |
0 | 394 "", |
20 | 395 "", |
0 | 396 sizeof(VideoData), |
397 CODEC_ID_NONE, | |
398 CODEC_ID_RAWVIDEO, | |
399 img_write_header, | |
400 img_write_packet, | |
401 img_write_trailer, | |
21 | 402 AVFMT_RAWPICTURE, |
20 | 403 img_set_parameters, |
0 | 404 }; |
405 | |
406 int img_init(void) | |
407 { | |
20 | 408 av_register_input_format(&image_iformat); |
409 av_register_output_format(&image_oformat); | |
0 | 410 |
20 | 411 av_register_input_format(&imagepipe_iformat); |
412 av_register_output_format(&imagepipe_oformat); | |
0 | 413 |
414 return 0; | |
415 } |