Remove debug line.
[FFMpeg-mirror/ordered_chapters.git] / libavformat / img.c
blob5223c691e47e24db8c0a0eaec4f07294457a8da5
1 /*
2 * Image format
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "avformat.h"
23 typedef struct {
24 int width;
25 int height;
26 int img_first;
27 int img_last;
28 int img_number;
29 int img_count;
30 int img_size;
31 AVImageFormat *img_fmt;
32 int pix_fmt;
33 int is_pipe;
34 char path[1024];
35 /* temporary usage */
36 void *ptr;
37 } VideoData;
40 /* return -1 if no image found */
41 static int find_image_range(int *pfirst_index, int *plast_index,
42 const char *path)
44 char buf[1024];
45 int range, last_index, range1, first_index;
47 /* find the first image */
48 for(first_index = 0; first_index < 5; first_index++) {
49 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0)
50 goto fail;
51 if (url_exist(buf))
52 break;
54 if (first_index == 5)
55 goto fail;
57 /* find the last image */
58 last_index = first_index;
59 for(;;) {
60 range = 0;
61 for(;;) {
62 if (!range)
63 range1 = 1;
64 else
65 range1 = 2 * range;
66 if (av_get_frame_filename(buf, sizeof(buf), path,
67 last_index + range1) < 0)
68 goto fail;
69 if (!url_exist(buf))
70 break;
71 range = range1;
72 /* just in case... */
73 if (range >= (1 << 30))
74 goto fail;
76 /* we are sure than image last_index + range exists */
77 if (!range)
78 break;
79 last_index += range;
81 *pfirst_index = first_index;
82 *plast_index = last_index;
83 return 0;
84 fail:
85 return -1;
89 static int image_probe(AVProbeData *p)
91 if (av_filename_number_test(p->filename) && guess_image_format(p->filename))
92 return AVPROBE_SCORE_MAX-1;
93 else
94 return 0;
97 static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
99 VideoData *s = opaque;
101 s->width = info->width;
102 s->height = info->height;
103 s->pix_fmt = info->pix_fmt;
104 /* stop image reading but no error */
105 return 1;
108 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
110 VideoData *s = s1->priv_data;
111 int ret, first_index, last_index;
112 char buf[1024];
113 ByteIOContext pb1, *f = &pb1;
114 AVStream *st;
116 st = av_new_stream(s1, 0);
117 if (!st) {
118 return -ENOMEM;
121 if (ap->image_format)
122 s->img_fmt = ap->image_format;
124 pstrcpy(s->path, sizeof(s->path), s1->filename);
125 s->img_number = 0;
126 s->img_count = 0;
128 /* find format */
129 if (s1->iformat->flags & AVFMT_NOFILE)
130 s->is_pipe = 0;
131 else
132 s->is_pipe = 1;
134 if (!ap->time_base.num) {
135 st->codec->time_base= (AVRational){1,25};
136 } else {
137 st->codec->time_base= ap->time_base;
140 if (!s->is_pipe) {
141 if (find_image_range(&first_index, &last_index, s->path) < 0)
142 goto fail;
143 s->img_first = first_index;
144 s->img_last = last_index;
145 s->img_number = first_index;
146 /* compute duration */
147 st->start_time = 0;
148 st->duration = last_index - first_index + 1;
149 if (av_get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
150 goto fail;
151 if (url_fopen(f, buf, URL_RDONLY) < 0)
152 goto fail;
153 } else {
154 f = &s1->pb;
157 ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
158 if (ret < 0)
159 goto fail1;
161 if (!s->is_pipe) {
162 url_fclose(f);
163 } else {
164 url_fseek(f, 0, SEEK_SET);
167 st->codec->codec_type = CODEC_TYPE_VIDEO;
168 st->codec->codec_id = CODEC_ID_RAWVIDEO;
169 st->codec->width = s->width;
170 st->codec->height = s->height;
171 st->codec->pix_fmt = s->pix_fmt;
172 s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15));
174 return 0;
175 fail1:
176 if (!s->is_pipe)
177 url_fclose(f);
178 fail:
179 return AVERROR_IO;
182 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
184 VideoData *s = opaque;
186 if (info->width != s->width ||
187 info->height != s->height)
188 return -1;
189 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15));
190 return 0;
193 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
195 VideoData *s = s1->priv_data;
196 char filename[1024];
197 int ret;
198 ByteIOContext f1, *f;
200 if (!s->is_pipe) {
201 /* loop over input */
202 if (s1->loop_input && s->img_number > s->img_last) {
203 s->img_number = s->img_first;
205 if (av_get_frame_filename(filename, sizeof(filename),
206 s->path, s->img_number) < 0)
207 return AVERROR_IO;
208 f = &f1;
209 if (url_fopen(f, filename, URL_RDONLY) < 0)
210 return AVERROR_IO;
211 } else {
212 f = &s1->pb;
213 if (url_feof(f))
214 return AVERROR_IO;
217 av_new_packet(pkt, s->img_size);
218 pkt->stream_index = 0;
220 s->ptr = pkt->data;
221 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
222 if (!s->is_pipe) {
223 url_fclose(f);
226 if (ret < 0) {
227 av_free_packet(pkt);
228 return AVERROR_IO; /* signal EOF */
229 } else {
230 /* XXX: computing this pts is not necessary as it is done in
231 the generic code too */
232 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;
233 s->img_count++;
234 s->img_number++;
235 return 0;
239 static int img_read_close(AVFormatContext *s1)
241 return 0;
244 /******************************************************/
245 /* image output */
247 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
249 VideoData *img = s->priv_data;
250 AVStream *st;
251 AVImageFormat *img_fmt;
252 int i;
254 /* find output image format */
255 if (ap->image_format) {
256 img_fmt = ap->image_format;
257 } else {
258 img_fmt = guess_image_format(s->filename);
260 if (!img_fmt)
261 return -1;
263 if (s->nb_streams != 1)
264 return -1;
266 st = s->streams[0];
267 /* we select the first matching format */
268 for(i=0;i<PIX_FMT_NB;i++) {
269 if (img_fmt->supported_pixel_formats & (1 << i))
270 break;
272 if (i >= PIX_FMT_NB)
273 return -1;
274 img->img_fmt = img_fmt;
275 img->pix_fmt = i;
276 st->codec->pix_fmt = img->pix_fmt;
277 return 0;
280 static int img_write_header(AVFormatContext *s)
282 VideoData *img = s->priv_data;
284 img->img_number = 1;
285 pstrcpy(img->path, sizeof(img->path), s->filename);
287 /* find format */
288 if (s->oformat->flags & AVFMT_NOFILE)
289 img->is_pipe = 0;
290 else
291 img->is_pipe = 1;
293 return 0;
296 static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
298 VideoData *img = s->priv_data;
299 AVStream *st = s->streams[pkt->stream_index];
300 ByteIOContext pb1, *pb;
301 AVPicture *picture;
302 int width, height, ret;
303 char filename[1024];
304 AVImageInfo info;
306 width = st->codec->width;
307 height = st->codec->height;
309 picture = (AVPicture *)pkt->data;
311 if (!img->is_pipe) {
312 if (av_get_frame_filename(filename, sizeof(filename),
313 img->path, img->img_number) < 0)
314 return AVERROR_IO;
315 pb = &pb1;
316 if (url_fopen(pb, filename, URL_WRONLY) < 0)
317 return AVERROR_IO;
318 } else {
319 pb = &s->pb;
321 info.width = width;
322 info.height = height;
323 info.pix_fmt = st->codec->pix_fmt;
324 info.interleaved = 0; /* FIXME: there should be a way to set it right */
325 info.pict = *picture;
326 ret = av_write_image(pb, img->img_fmt, &info);
327 if (!img->is_pipe) {
328 url_fclose(pb);
331 img->img_number++;
332 return 0;
335 static int img_write_trailer(AVFormatContext *s)
337 return 0;
340 /* input */
341 #ifdef CONFIG_IMAGE_DEMUXER
342 AVInputFormat image_demuxer = {
343 "image",
344 "image sequence",
345 sizeof(VideoData),
346 image_probe,
347 img_read_header,
348 img_read_packet,
349 img_read_close,
350 NULL,
351 NULL,
352 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
354 #endif
355 #ifdef CONFIG_IMAGEPIPE_DEMUXER
356 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,
366 #endif
368 /* output */
369 #ifdef CONFIG_IMAGE_MUXER
370 AVOutputFormat image_muxer = {
371 "image",
372 "image sequence",
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,
384 #endif
385 #ifdef CONFIG_IMAGEPIPE_MUXER
386 AVOutputFormat imagepipe_muxer = {
387 "imagepipe",
388 "piped image sequence",
391 sizeof(VideoData),
392 CODEC_ID_NONE,
393 CODEC_ID_RAWVIDEO,
394 img_write_header,
395 img_write_packet,
396 img_write_trailer,
397 AVFMT_RAWPICTURE,
398 img_set_parameters,
400 #endif