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 | return -ENOMEM;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (ap->image_format)
|
---|
123 | s->img_fmt = ap->image_format;
|
---|
124 |
|
---|
125 | pstrcpy(s->path, sizeof(s->path), s1->filename);
|
---|
126 | s->img_number = 0;
|
---|
127 | s->img_count = 0;
|
---|
128 |
|
---|
129 | /* find format */
|
---|
130 | if (s1->iformat->flags & AVFMT_NOFILE)
|
---|
131 | s->is_pipe = 0;
|
---|
132 | else
|
---|
133 | s->is_pipe = 1;
|
---|
134 |
|
---|
135 | if (!ap->time_base.num) {
|
---|
136 | st->codec->time_base= (AVRational){1,25};
|
---|
137 | } else {
|
---|
138 | st->codec->time_base= ap->time_base;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (!s->is_pipe) {
|
---|
142 | if (find_image_range(&first_index, &last_index, s->path) < 0)
|
---|
143 | goto fail;
|
---|
144 | s->img_first = first_index;
|
---|
145 | s->img_last = last_index;
|
---|
146 | s->img_number = first_index;
|
---|
147 | /* compute duration */
|
---|
148 | st->start_time = 0;
|
---|
149 | st->duration = last_index - first_index + 1;
|
---|
150 | if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
|
---|
151 | goto fail;
|
---|
152 | if (url_fopen(f, buf, URL_RDONLY) < 0)
|
---|
153 | goto fail;
|
---|
154 | } else {
|
---|
155 | f = &s1->pb;
|
---|
156 | }
|
---|
157 |
|
---|
158 | ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
|
---|
159 | if (ret < 0)
|
---|
160 | goto fail1;
|
---|
161 |
|
---|
162 | if (!s->is_pipe) {
|
---|
163 | url_fclose(f);
|
---|
164 | } else {
|
---|
165 | url_fseek(f, 0, SEEK_SET);
|
---|
166 | }
|
---|
167 |
|
---|
168 | st->codec->codec_type = CODEC_TYPE_VIDEO;
|
---|
169 | st->codec->codec_id = CODEC_ID_RAWVIDEO;
|
---|
170 | st->codec->width = s->width;
|
---|
171 | st->codec->height = s->height;
|
---|
172 | st->codec->pix_fmt = s->pix_fmt;
|
---|
173 | s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15));
|
---|
174 |
|
---|
175 | return 0;
|
---|
176 | fail1:
|
---|
177 | if (!s->is_pipe)
|
---|
178 | url_fclose(f);
|
---|
179 | fail:
|
---|
180 | return AVERROR_IO;
|
---|
181 | }
|
---|
182 |
|
---|
183 | static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
|
---|
184 | {
|
---|
185 | VideoData *s = opaque;
|
---|
186 |
|
---|
187 | if (info->width != s->width ||
|
---|
188 | info->height != s->height)
|
---|
189 | return -1;
|
---|
190 | avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15));
|
---|
191 | return 0;
|
---|
192 | }
|
---|
193 |
|
---|
194 | static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
---|
195 | {
|
---|
196 | VideoData *s = s1->priv_data;
|
---|
197 | char filename[1024];
|
---|
198 | int ret;
|
---|
199 | ByteIOContext f1, *f;
|
---|
200 |
|
---|
201 | if (!s->is_pipe) {
|
---|
202 | /* loop over input */
|
---|
203 | if (loop_input && s->img_number > s->img_last) {
|
---|
204 | s->img_number = s->img_first;
|
---|
205 | }
|
---|
206 | if (get_frame_filename(filename, sizeof(filename),
|
---|
207 | s->path, s->img_number) < 0)
|
---|
208 | return AVERROR_IO;
|
---|
209 | f = &f1;
|
---|
210 | if (url_fopen(f, filename, URL_RDONLY) < 0)
|
---|
211 | return AVERROR_IO;
|
---|
212 | } else {
|
---|
213 | f = &s1->pb;
|
---|
214 | if (url_feof(f))
|
---|
215 | return AVERROR_IO;
|
---|
216 | }
|
---|
217 |
|
---|
218 | av_new_packet(pkt, s->img_size);
|
---|
219 | pkt->stream_index = 0;
|
---|
220 |
|
---|
221 | s->ptr = pkt->data;
|
---|
222 | ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
|
---|
223 | if (!s->is_pipe) {
|
---|
224 | url_fclose(f);
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (ret < 0) {
|
---|
228 | av_free_packet(pkt);
|
---|
229 | return AVERROR_IO; /* signal EOF */
|
---|
230 | } else {
|
---|
231 | /* XXX: computing this pts is not necessary as it is done in
|
---|
232 | the generic code too */
|
---|
233 | 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;
|
---|
234 | s->img_count++;
|
---|
235 | s->img_number++;
|
---|
236 | return 0;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | static int img_read_close(AVFormatContext *s1)
|
---|
241 | {
|
---|
242 | return 0;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /******************************************************/
|
---|
246 | /* image output */
|
---|
247 |
|
---|
248 | static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
|
---|
249 | {
|
---|
250 | VideoData *img = s->priv_data;
|
---|
251 | AVStream *st;
|
---|
252 | AVImageFormat *img_fmt;
|
---|
253 | int i;
|
---|
254 |
|
---|
255 | /* find output image format */
|
---|
256 | if (ap->image_format) {
|
---|
257 | img_fmt = ap->image_format;
|
---|
258 | } else {
|
---|
259 | img_fmt = guess_image_format(s->filename);
|
---|
260 | }
|
---|
261 | if (!img_fmt)
|
---|
262 | return -1;
|
---|
263 |
|
---|
264 | if (s->nb_streams != 1)
|
---|
265 | return -1;
|
---|
266 |
|
---|
267 | st = s->streams[0];
|
---|
268 | /* we select the first matching format */
|
---|
269 | for(i=0;i<PIX_FMT_NB;i++) {
|
---|
270 | if (img_fmt->supported_pixel_formats & (1 << i))
|
---|
271 | break;
|
---|
272 | }
|
---|
273 | if (i >= PIX_FMT_NB)
|
---|
274 | return -1;
|
---|
275 | img->img_fmt = img_fmt;
|
---|
276 | img->pix_fmt = i;
|
---|
277 | st->codec->pix_fmt = img->pix_fmt;
|
---|
278 | return 0;
|
---|
279 | }
|
---|
280 |
|
---|
281 | static int img_write_header(AVFormatContext *s)
|
---|
282 | {
|
---|
283 | VideoData *img = s->priv_data;
|
---|
284 |
|
---|
285 | img->img_number = 1;
|
---|
286 | pstrcpy(img->path, sizeof(img->path), s->filename);
|
---|
287 |
|
---|
288 | /* find format */
|
---|
289 | if (s->oformat->flags & AVFMT_NOFILE)
|
---|
290 | img->is_pipe = 0;
|
---|
291 | else
|
---|
292 | img->is_pipe = 1;
|
---|
293 |
|
---|
294 | return 0;
|
---|
295 | }
|
---|
296 |
|
---|
297 | static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
298 | {
|
---|
299 | VideoData *img = s->priv_data;
|
---|
300 | AVStream *st = s->streams[pkt->stream_index];
|
---|
301 | ByteIOContext pb1, *pb;
|
---|
302 | AVPicture *picture;
|
---|
303 | int width, height, ret;
|
---|
304 | char filename[1024];
|
---|
305 | AVImageInfo info;
|
---|
306 |
|
---|
307 | width = st->codec->width;
|
---|
308 | height = st->codec->height;
|
---|
309 |
|
---|
310 | picture = (AVPicture *)pkt->data;
|
---|
311 |
|
---|
312 | if (!img->is_pipe) {
|
---|
313 | if (get_frame_filename(filename, sizeof(filename),
|
---|
314 | img->path, img->img_number) < 0)
|
---|
315 | return AVERROR_IO;
|
---|
316 | pb = &pb1;
|
---|
317 | if (url_fopen(pb, filename, URL_WRONLY) < 0)
|
---|
318 | return AVERROR_IO;
|
---|
319 | } else {
|
---|
320 | pb = &s->pb;
|
---|
321 | }
|
---|
322 | info.width = width;
|
---|
323 | info.height = height;
|
---|
324 | info.pix_fmt = st->codec->pix_fmt;
|
---|
325 | info.interleaved = 0; /* FIXME: there should be a way to set it right */
|
---|
326 | info.pict = *picture;
|
---|
327 | ret = av_write_image(pb, img->img_fmt, &info);
|
---|
328 | if (!img->is_pipe) {
|
---|
329 | url_fclose(pb);
|
---|
330 | }
|
---|
331 |
|
---|
332 | img->img_number++;
|
---|
333 | return 0;
|
---|
334 | }
|
---|
335 |
|
---|
336 | static int img_write_trailer(AVFormatContext *s)
|
---|
337 | {
|
---|
338 | return 0;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /* input */
|
---|
342 |
|
---|
343 | static AVInputFormat image_demuxer = {
|
---|
344 | "image",
|
---|
345 | "image sequence",
|
---|
346 | sizeof(VideoData),
|
---|
347 | image_probe,
|
---|
348 | img_read_header,
|
---|
349 | img_read_packet,
|
---|
350 | img_read_close,
|
---|
351 | NULL,
|
---|
352 | NULL,
|
---|
353 | AVFMT_NOFILE | AVFMT_NEEDNUMBER,
|
---|
354 | };
|
---|
355 |
|
---|
356 | static AVInputFormat imagepipe_demuxer = {
|
---|
357 | "imagepipe",
|
---|
358 | "piped image sequence",
|
---|
359 | sizeof(VideoData),
|
---|
360 | NULL, /* no probe */
|
---|
361 | img_read_header,
|
---|
362 | img_read_packet,
|
---|
363 | img_read_close,
|
---|
364 | NULL,
|
---|
365 | };
|
---|
366 |
|
---|
367 |
|
---|
368 | /* output */
|
---|
369 |
|
---|
370 | static AVOutputFormat image_muxer = {
|
---|
371 | "image",
|
---|
372 | "image sequence",
|
---|
373 | "",
|
---|
374 | "",
|
---|
375 | sizeof(VideoData),
|
---|
376 | CODEC_ID_NONE,
|
---|
377 | CODEC_ID_RAWVIDEO,
|
---|
378 | img_write_header,
|
---|
379 | img_write_packet,
|
---|
380 | img_write_trailer,
|
---|
381 | AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
|
---|
382 | img_set_parameters,
|
---|
383 | };
|
---|
384 |
|
---|
385 | static AVOutputFormat imagepipe_muxer = {
|
---|
386 | "imagepipe",
|
---|
387 | "piped image sequence",
|
---|
388 | "",
|
---|
389 | "",
|
---|
390 | sizeof(VideoData),
|
---|
391 | CODEC_ID_NONE,
|
---|
392 | CODEC_ID_RAWVIDEO,
|
---|
393 | img_write_header,
|
---|
394 | img_write_packet,
|
---|
395 | img_write_trailer,
|
---|
396 | AVFMT_RAWPICTURE,
|
---|
397 | img_set_parameters,
|
---|
398 | };
|
---|
399 |
|
---|
400 | int img_init(void)
|
---|
401 | {
|
---|
402 | av_register_input_format(&image_demuxer);
|
---|
403 | av_register_output_format(&image_muxer);
|
---|
404 |
|
---|
405 | av_register_input_format(&imagepipe_demuxer);
|
---|
406 | av_register_output_format(&imagepipe_muxer);
|
---|
407 |
|
---|
408 | return 0;
|
---|
409 | }
|
---|