Mercurial > libavformat.hg
annotate img.c @ 237:35231c0be8e5 libavformat
memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
author | michaelni |
---|---|
date | Tue, 09 Sep 2003 19:32:52 +0000 |
parents | 8ca8b6bc24a5 |
children | 3d92f793fd67 |
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; | |
178 s->img_size = avpicture_get_size(s->pix_fmt, s->width, s->height); | |
179 | |
0 | 180 return 0; |
20 | 181 fail1: |
182 if (!s->is_pipe) | |
183 url_fclose(f); | |
184 fail: | |
185 av_free(s); | |
186 return -EIO; | |
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; | |
196 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, info->width, info->height); | |
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) | |
214 return -EIO; | |
0 | 215 f = &f1; |
216 if (url_fopen(f, filename, URL_RDONLY) < 0) | |
217 return -EIO; | |
218 } else { | |
219 f = &s1->pb; | |
220 if (url_feof(f)) | |
221 return -EIO; | |
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); | |
235 return -EIO; /* signal EOF */ | |
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 */ |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
239 pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec.frame_rate_base, s1->pts_den, s1->streams[0]->codec.frame_rate) / s1->pts_num; |
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 | |
303 static int img_write_packet(AVFormatContext *s, int stream_index, | |
65 | 304 uint8_t *buf, int size, int force_pts) |
0 | 305 { |
306 VideoData *img = s->priv_data; | |
307 AVStream *st = s->streams[stream_index]; | |
308 ByteIOContext pb1, *pb; | |
20 | 309 AVPicture *picture; |
310 int width, height, ret; | |
0 | 311 char filename[1024]; |
20 | 312 AVImageInfo info; |
0 | 313 |
314 width = st->codec.width; | |
315 height = st->codec.height; | |
316 | |
20 | 317 picture = (AVPicture *)buf; |
0 | 318 |
20 | 319 if (!img->is_pipe) { |
320 if (get_frame_filename(filename, sizeof(filename), | |
321 img->path, img->img_number) < 0) | |
0 | 322 return -EIO; |
323 pb = &pb1; | |
324 if (url_fopen(pb, filename, URL_WRONLY) < 0) | |
325 return -EIO; | |
326 } else { | |
327 pb = &s->pb; | |
328 } | |
20 | 329 info.width = width; |
330 info.height = height; | |
331 info.pix_fmt = st->codec.pix_fmt; | |
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, | |
358 AVFMT_NOFILE | AVFMT_NEEDNUMBER, | |
359 }; | |
360 | |
20 | 361 static AVInputFormat imagepipe_iformat = { |
362 "imagepipe", | |
363 "piped image sequence", | |
0 | 364 sizeof(VideoData), |
365 NULL, /* no probe */ | |
366 img_read_header, | |
367 img_read_packet, | |
368 img_read_close, | |
369 NULL, | |
370 }; | |
371 | |
20 | 372 |
373 /* output */ | |
0 | 374 |
20 | 375 static AVOutputFormat image_oformat = { |
376 "image", | |
377 "image sequence", | |
0 | 378 "", |
20 | 379 "", |
0 | 380 sizeof(VideoData), |
381 CODEC_ID_NONE, | |
382 CODEC_ID_RAWVIDEO, | |
383 img_write_header, | |
384 img_write_packet, | |
385 img_write_trailer, | |
20 | 386 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE, |
387 img_set_parameters, | |
0 | 388 }; |
389 | |
20 | 390 static AVOutputFormat imagepipe_oformat = { |
391 "imagepipe", | |
392 "piped image sequence", | |
0 | 393 "", |
20 | 394 "", |
0 | 395 sizeof(VideoData), |
396 CODEC_ID_NONE, | |
397 CODEC_ID_RAWVIDEO, | |
398 img_write_header, | |
399 img_write_packet, | |
400 img_write_trailer, | |
21 | 401 AVFMT_RAWPICTURE, |
20 | 402 img_set_parameters, |
0 | 403 }; |
404 | |
405 int img_init(void) | |
406 { | |
20 | 407 av_register_input_format(&image_iformat); |
408 av_register_output_format(&image_oformat); | |
0 | 409 |
20 | 410 av_register_input_format(&imagepipe_iformat); |
411 av_register_output_format(&imagepipe_oformat); | |
0 | 412 |
413 return 0; | |
414 } |