Mercurial > libavformat.hg
annotate img.c @ 922:7f20b3ce1100 libavformat
dynamically increase probe buffer until format is detected
author | michael |
---|---|
date | Thu, 02 Feb 2006 11:27:35 +0000 |
parents | edbe5c3717f9 |
children | 2d57ce58f576 |
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 | |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 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 */ | |
885 | 42 static int find_image_range(int *pfirst_index, int *plast_index, |
189 | 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; | |
885 | 57 |
189 | 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; | |
885 | 67 if (get_frame_filename(buf, sizeof(buf), path, |
189 | 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)) |
582 | 93 return AVPROBE_SCORE_MAX-1; |
20 | 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 | |
639 | 126 pstrcpy(s->path, sizeof(s->path), s1->filename); |
20 | 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; |
885 | 129 |
20 | 130 /* find format */ |
131 if (s1->iformat->flags & AVFMT_NOFILE) | |
132 s->is_pipe = 0; | |
133 else | |
134 s->is_pipe = 1; | |
743 | 135 |
136 if (!ap || !ap->time_base.num) { | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
137 st->codec->time_base= (AVRational){1,25}; |
189 | 138 } else { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
139 st->codec->time_base= ap->time_base; |
189 | 140 } |
885 | 141 |
20 | 142 if (!s->is_pipe) { |
189 | 143 if (find_image_range(&first_index, &last_index, s->path) < 0) |
144 goto fail; | |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
145 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
|
146 s->img_last = last_index; |
189 | 147 s->img_number = first_index; |
148 /* compute duration */ | |
149 st->start_time = 0; | |
743 | 150 st->duration = last_index - first_index + 1; |
189 | 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) | |
20 | 154 goto fail; |
155 } else { | |
156 f = &s1->pb; | |
0 | 157 } |
885 | 158 |
20 | 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 } | |
885 | 168 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
169 st->codec->codec_type = CODEC_TYPE_VIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
170 st->codec->codec_id = CODEC_ID_RAWVIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
171 st->codec->width = s->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
172 st->codec->height = s->height; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
173 st->codec->pix_fmt = s->pix_fmt; |
459 | 174 s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15)); |
20 | 175 |
0 | 176 return 0; |
20 | 177 fail1: |
178 if (!s->is_pipe) | |
179 url_fclose(f); | |
180 fail: | |
181 av_free(s); | |
482 | 182 return AVERROR_IO; |
0 | 183 } |
184 | |
20 | 185 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info) |
0 | 186 { |
20 | 187 VideoData *s = opaque; |
0 | 188 |
20 | 189 if (info->width != s->width || |
190 info->height != s->height) | |
191 return -1; | |
459 | 192 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15)); |
0 | 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) { | |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
204 /* loop over input */ |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
205 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
|
206 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
|
207 } |
20 | 208 if (get_frame_filename(filename, sizeof(filename), |
209 s->path, s->img_number) < 0) | |
482 | 210 return AVERROR_IO; |
0 | 211 f = &f1; |
212 if (url_fopen(f, filename, URL_RDONLY) < 0) | |
482 | 213 return AVERROR_IO; |
0 | 214 } else { |
215 f = &s1->pb; | |
216 if (url_feof(f)) | |
482 | 217 return AVERROR_IO; |
0 | 218 } |
219 | |
220 av_new_packet(pkt, s->img_size); | |
221 pkt->stream_index = 0; | |
222 | |
20 | 223 s->ptr = pkt->data; |
224 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s); | |
0 | 225 if (!s->is_pipe) { |
226 url_fclose(f); | |
227 } | |
228 | |
229 if (ret < 0) { | |
230 av_free_packet(pkt); | |
482 | 231 return AVERROR_IO; /* signal EOF */ |
0 | 232 } else { |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
233 /* 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
|
234 the generic code too */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
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; |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
236 s->img_count++; |
0 | 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 | |
20 | 250 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
0 | 251 { |
20 | 252 VideoData *img = s->priv_data; |
253 AVStream *st; | |
254 AVImageFormat *img_fmt; | |
0 | 255 int i; |
256 | |
20 | 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; | |
0 | 265 |
20 | 266 if (s->nb_streams != 1) |
267 return -1; | |
885 | 268 |
20 | 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; | |
0 | 274 } |
20 | 275 if (i >= PIX_FMT_NB) |
276 return -1; | |
277 img->img_fmt = img_fmt; | |
278 img->pix_fmt = i; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
279 st->codec->pix_fmt = img->pix_fmt; |
0 | 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; | |
639 | 288 pstrcpy(img->path, sizeof(img->path), s->filename); |
0 | 289 |
290 /* find format */ | |
291 if (s->oformat->flags & AVFMT_NOFILE) | |
292 img->is_pipe = 0; | |
293 else | |
294 img->is_pipe = 1; | |
885 | 295 |
0 | 296 return 0; |
297 } | |
298 | |
468 | 299 static int img_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 300 { |
301 VideoData *img = s->priv_data; | |
468 | 302 AVStream *st = s->streams[pkt->stream_index]; |
0 | 303 ByteIOContext pb1, *pb; |
20 | 304 AVPicture *picture; |
305 int width, height, ret; | |
0 | 306 char filename[1024]; |
20 | 307 AVImageInfo info; |
0 | 308 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
309 width = st->codec->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
310 height = st->codec->height; |
885 | 311 |
468 | 312 picture = (AVPicture *)pkt->data; |
0 | 313 |
20 | 314 if (!img->is_pipe) { |
885 | 315 if (get_frame_filename(filename, sizeof(filename), |
20 | 316 img->path, img->img_number) < 0) |
482 | 317 return AVERROR_IO; |
0 | 318 pb = &pb1; |
319 if (url_fopen(pb, filename, URL_WRONLY) < 0) | |
482 | 320 return AVERROR_IO; |
0 | 321 } else { |
322 pb = &s->pb; | |
323 } | |
20 | 324 info.width = width; |
325 info.height = height; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
326 info.pix_fmt = st->codec->pix_fmt; |
280 | 327 info.interleaved = 0; /* FIXME: there should be a way to set it right */ |
20 | 328 info.pict = *picture; |
329 ret = av_write_image(pb, img->img_fmt, &info); | |
0 | 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 | |
20 | 343 /* input */ |
0 | 344 |
20 | 345 static AVInputFormat image_iformat = { |
346 "image", | |
347 "image sequence", | |
0 | 348 sizeof(VideoData), |
20 | 349 image_probe, |
0 | 350 img_read_header, |
351 img_read_packet, | |
352 img_read_close, | |
353 NULL, | |
439 | 354 NULL, |
0 | 355 AVFMT_NOFILE | AVFMT_NEEDNUMBER, |
356 }; | |
357 | |
20 | 358 static AVInputFormat imagepipe_iformat = { |
359 "imagepipe", | |
360 "piped image sequence", | |
0 | 361 sizeof(VideoData), |
362 NULL, /* no probe */ | |
363 img_read_header, | |
364 img_read_packet, | |
365 img_read_close, | |
366 NULL, | |
367 }; | |
368 | |
20 | 369 |
370 /* output */ | |
0 | 371 |
20 | 372 static AVOutputFormat image_oformat = { |
373 "image", | |
374 "image sequence", | |
0 | 375 "", |
20 | 376 "", |
0 | 377 sizeof(VideoData), |
378 CODEC_ID_NONE, | |
379 CODEC_ID_RAWVIDEO, | |
380 img_write_header, | |
381 img_write_packet, | |
382 img_write_trailer, | |
20 | 383 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE, |
384 img_set_parameters, | |
0 | 385 }; |
386 | |
20 | 387 static AVOutputFormat imagepipe_oformat = { |
388 "imagepipe", | |
389 "piped image sequence", | |
0 | 390 "", |
20 | 391 "", |
0 | 392 sizeof(VideoData), |
393 CODEC_ID_NONE, | |
394 CODEC_ID_RAWVIDEO, | |
395 img_write_header, | |
396 img_write_packet, | |
397 img_write_trailer, | |
21 | 398 AVFMT_RAWPICTURE, |
20 | 399 img_set_parameters, |
0 | 400 }; |
401 | |
402 int img_init(void) | |
403 { | |
20 | 404 av_register_input_format(&image_iformat); |
405 av_register_output_format(&image_oformat); | |
0 | 406 |
20 | 407 av_register_input_format(&imagepipe_iformat); |
408 av_register_output_format(&imagepipe_oformat); | |
885 | 409 |
0 | 410 return 0; |
411 } |