os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
[libav.git] / libavformat / img.c
CommitLineData
de6d9b64
FB
1/*
2 * Image format
19720f15 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
38758ab0 19#include <unistd.h>
de6d9b64
FB
20#include "avformat.h"
21
9b2e001f
BE
22#ifdef __MINGW32__
23# include <windows.h>
8ff32388
FR
24# define usleep(t) Sleep((t) / 1000)
25#endif
26#ifdef __BEOS__
27# ifndef usleep
28# include <OS.h>
f3ec2d46 29# define usleep(t) snooze((bigtime_t)(t))
8ff32388 30# endif
9b2e001f 31#endif
f3ec2d46
SG
32#if defined(CONFIG_OS2)
33# include <stdlib.h>
34# define usleep(t) _sleep2((t) / 1000)
35#endif
9b2e001f 36
de6d9b64
FB
37typedef struct {
38 int width;
39 int height;
40 int img_number;
41 int img_size;
87a0a681
FB
42 AVImageFormat *img_fmt;
43 int pix_fmt;
de6d9b64
FB
44 int is_pipe;
45 char path[1024];
87a0a681
FB
46 /* temporary usage */
47 void *ptr;
de6d9b64
FB
48} VideoData;
49
38758ab0
PG
50int emulate_frame_rate;
51
87a0a681 52static int image_probe(AVProbeData *p)
de6d9b64 53{
94d883e8 54 if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
87a0a681
FB
55 return AVPROBE_SCORE_MAX;
56 else
de6d9b64 57 return 0;
de6d9b64
FB
58}
59
87a0a681 60static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
de6d9b64 61{
87a0a681 62 VideoData *s = opaque;
de6d9b64 63
87a0a681
FB
64 s->width = info->width;
65 s->height = info->height;
66 s->pix_fmt = info->pix_fmt;
67 /* stop image reading but no error */
68 return 1;
de6d9b64
FB
69}
70
71static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
72{
c9a65ca8 73 VideoData *s = s1->priv_data;
87a0a681 74 int i, ret;
de6d9b64 75 char buf[1024];
de6d9b64
FB
76 ByteIOContext pb1, *f = &pb1;
77 AVStream *st;
78
c9a65ca8 79 st = av_new_stream(s1, 0);
de6d9b64 80 if (!st) {
1ea4f593 81 av_free(s);
de6d9b64
FB
82 return -ENOMEM;
83 }
c9a65ca8 84
87a0a681
FB
85 if (ap && ap->image_format)
86 s->img_fmt = ap->image_format;
87
de6d9b64
FB
88 strcpy(s->path, s1->filename);
89 s->img_number = 0;
90
91 /* find format */
c9a65ca8 92 if (s1->iformat->flags & AVFMT_NOFILE)
de6d9b64
FB
93 s->is_pipe = 0;
94 else
95 s->is_pipe = 1;
96
de6d9b64
FB
97 if (!s->is_pipe) {
98 /* try to find the first image */
99 for(i=0;i<5;i++) {
9150f42e
FB
100 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
101 goto fail;
de6d9b64
FB
102 if (url_fopen(f, buf, URL_RDONLY) >= 0)
103 break;
104 s->img_number++;
105 }
106 if (i == 5)
107 goto fail;
108 } else {
109 f = &s1->pb;
110 }
111
87a0a681
FB
112 ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
113 if (ret < 0)
114 goto fail1;
de6d9b64
FB
115
116 if (!s->is_pipe) {
117 url_fclose(f);
118 } else {
119 url_fseek(f, 0, SEEK_SET);
120 }
121
de6d9b64
FB
122 st->codec.codec_type = CODEC_TYPE_VIDEO;
123 st->codec.codec_id = CODEC_ID_RAWVIDEO;
124 st->codec.width = s->width;
125 st->codec.height = s->height;
87a0a681
FB
126 st->codec.pix_fmt = s->pix_fmt;
127 s->img_size = avpicture_get_size(s->pix_fmt, s->width, s->height);
128
de6d9b64
FB
129 if (!ap || !ap->frame_rate)
130 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
131 else
132 st->codec.frame_rate = ap->frame_rate;
133
134 return 0;
135 fail1:
136 if (!s->is_pipe)
137 url_fclose(f);
138 fail:
1ea4f593 139 av_free(s);
de6d9b64
FB
140 return -EIO;
141}
142
87a0a681 143static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
de6d9b64 144{
87a0a681
FB
145 VideoData *s = opaque;
146
147 if (info->width != s->width ||
148 info->height != s->height)
149 return -1;
150 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, info->width, info->height);
de6d9b64
FB
151 return 0;
152}
153
87a0a681 154static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
de6d9b64 155{
87a0a681
FB
156 VideoData *s = s1->priv_data;
157 char filename[1024];
158 int ret;
159 ByteIOContext f1, *f;
160 static INT64 first_frame;
de6d9b64 161
87a0a681
FB
162 if (emulate_frame_rate) {
163 if (!first_frame) {
164 first_frame = av_gettime();
165 } else {
166 INT64 pts;
167 INT64 nowus;
168
169 nowus = av_gettime() - first_frame;
170
171 pts = ((INT64)s->img_number * FRAME_RATE_BASE * 1000000) / (s1->streams[0]->codec.frame_rate);
172
173 if (pts > nowus)
174 usleep(pts - nowus);
de6d9b64
FB
175 }
176 }
de6d9b64 177
87a0a681
FB
178 if (!s->is_pipe) {
179 if (get_frame_filename(filename, sizeof(filename),
180 s->path, s->img_number) < 0)
181 return -EIO;
182 f = &f1;
183 if (url_fopen(f, filename, URL_RDONLY) < 0)
184 return -EIO;
185 } else {
186 f = &s1->pb;
187 if (url_feof(f))
188 return -EIO;
189 }
6775c758 190
87a0a681
FB
191 av_new_packet(pkt, s->img_size);
192 pkt->stream_index = 0;
193
194 s->ptr = pkt->data;
195 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
196 if (!s->is_pipe) {
197 url_fclose(f);
6775c758
FB
198 }
199
87a0a681
FB
200 if (ret < 0) {
201 av_free_packet(pkt);
202 return -EIO; /* signal EOF */
203 } else {
204 pkt->pts = ((INT64)s->img_number * s1->pts_den * FRAME_RATE_BASE) / (s1->streams[0]->codec.frame_rate * s1->pts_num);
205 s->img_number++;
206 return 0;
207 }
6775c758
FB
208}
209
87a0a681 210static int img_read_close(AVFormatContext *s1)
de6d9b64 211{
de6d9b64
FB
212 return 0;
213}
214
87a0a681
FB
215/******************************************************/
216/* image output */
217
218static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
ad436907 219{
87a0a681
FB
220 VideoData *img = s->priv_data;
221 AVStream *st;
222 AVImageFormat *img_fmt;
223 int i;
224
225 /* find output image format */
226 if (ap && ap->image_format) {
227 img_fmt = ap->image_format;
ad436907 228 } else {
87a0a681 229 img_fmt = guess_image_format(s->filename);
ad436907 230 }
87a0a681
FB
231 if (!img_fmt)
232 return -1;
ad436907 233
87a0a681
FB
234 if (s->nb_streams != 1)
235 return -1;
236
237 st = s->streams[0];
238 /* we select the first matching format */
239 for(i=0;i<PIX_FMT_NB;i++) {
240 if (img_fmt->supported_pixel_formats & (1 << i))
241 break;
ad436907 242 }
87a0a681
FB
243 if (i >= PIX_FMT_NB)
244 return -1;
245 img->img_fmt = img_fmt;
246 img->pix_fmt = i;
247 st->codec.pix_fmt = img->pix_fmt;
ad436907
HM
248 return 0;
249}
250
de6d9b64
FB
251static int img_write_header(AVFormatContext *s)
252{
c9a65ca8 253 VideoData *img = s->priv_data;
de6d9b64 254
de6d9b64
FB
255 img->img_number = 1;
256 strcpy(img->path, s->filename);
257
258 /* find format */
c9a65ca8 259 if (s->oformat->flags & AVFMT_NOFILE)
de6d9b64
FB
260 img->is_pipe = 0;
261 else
262 img->is_pipe = 1;
263
de6d9b64 264 return 0;
de6d9b64
FB
265}
266
267static int img_write_packet(AVFormatContext *s, int stream_index,
10bb7023 268 UINT8 *buf, int size, int force_pts)
de6d9b64
FB
269{
270 VideoData *img = s->priv_data;
271 AVStream *st = s->streams[stream_index];
272 ByteIOContext pb1, *pb;
87a0a681
FB
273 AVPicture *picture;
274 int width, height, ret;
de6d9b64 275 char filename[1024];
87a0a681 276 AVImageInfo info;
de6d9b64
FB
277
278 width = st->codec.width;
279 height = st->codec.height;
ad436907 280
87a0a681 281 picture = (AVPicture *)buf;
de6d9b64 282
de6d9b64 283 if (!img->is_pipe) {
87a0a681
FB
284 if (get_frame_filename(filename, sizeof(filename),
285 img->path, img->img_number) < 0)
286 return -EIO;
de6d9b64
FB
287 pb = &pb1;
288 if (url_fopen(pb, filename, URL_WRONLY) < 0)
289 return -EIO;
290 } else {
291 pb = &s->pb;
292 }
87a0a681
FB
293 info.width = width;
294 info.height = height;
295 info.pix_fmt = st->codec.pix_fmt;
296 info.pict = *picture;
297 ret = av_write_image(pb, img->img_fmt, &info);
de6d9b64
FB
298 if (!img->is_pipe) {
299 url_fclose(pb);
300 }
301
302 img->img_number++;
303 return 0;
304}
305
306static int img_write_trailer(AVFormatContext *s)
307{
de6d9b64
FB
308 return 0;
309}
310
87a0a681 311/* input */
de6d9b64 312
87a0a681
FB
313static AVInputFormat image_iformat = {
314 "image",
315 "image sequence",
c9a65ca8 316 sizeof(VideoData),
87a0a681 317 image_probe,
de6d9b64
FB
318 img_read_header,
319 img_read_packet,
320 img_read_close,
321 NULL,
9150f42e 322 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
de6d9b64
FB
323};
324
87a0a681
FB
325static AVInputFormat imagepipe_iformat = {
326 "imagepipe",
327 "piped image sequence",
c9a65ca8
FB
328 sizeof(VideoData),
329 NULL, /* no probe */
de6d9b64
FB
330 img_read_header,
331 img_read_packet,
332 img_read_close,
333 NULL,
de6d9b64
FB
334};
335
de6d9b64 336
87a0a681 337/* output */
6775c758 338
87a0a681
FB
339static AVOutputFormat image_oformat = {
340 "image",
341 "image sequence",
6775c758 342 "",
6775c758 343 "",
c9a65ca8 344 sizeof(VideoData),
6775c758
FB
345 CODEC_ID_NONE,
346 CODEC_ID_RAWVIDEO,
347 img_write_header,
348 img_write_packet,
349 img_write_trailer,
87a0a681
FB
350 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
351 img_set_parameters,
6775c758 352};
c9a65ca8 353
87a0a681
FB
354static AVOutputFormat imagepipe_oformat = {
355 "imagepipe",
356 "piped image sequence",
357 "",
ad436907 358 "",
ad436907
HM
359 sizeof(VideoData),
360 CODEC_ID_NONE,
361 CODEC_ID_RAWVIDEO,
362 img_write_header,
363 img_write_packet,
364 img_write_trailer,
94d883e8 365 AVFMT_RAWPICTURE,
87a0a681 366 img_set_parameters,
ad436907
HM
367};
368
c9a65ca8
FB
369int img_init(void)
370{
87a0a681
FB
371 av_register_input_format(&image_iformat);
372 av_register_output_format(&image_oformat);
c9a65ca8 373
87a0a681
FB
374 av_register_input_format(&imagepipe_iformat);
375 av_register_output_format(&imagepipe_oformat);
ad436907 376
c9a65ca8
FB
377 return 0;
378}