changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext...
[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 */
de6d9b64 19#include "avformat.h"
9b2e001f 20
22b37f5d
FB
21/* XXX: this is a hack */
22int loop_input = 0;
23
de6d9b64
FB
24typedef struct {
25 int width;
26 int height;
22b37f5d
FB
27 int img_first;
28 int img_last;
de6d9b64 29 int img_number;
22b37f5d 30 int img_count;
de6d9b64 31 int img_size;
87a0a681
FB
32 AVImageFormat *img_fmt;
33 int pix_fmt;
de6d9b64
FB
34 int is_pipe;
35 char path[1024];
87a0a681
FB
36 /* temporary usage */
37 void *ptr;
de6d9b64
FB
38} VideoData;
39
ca1538f8
FB
40
41/* return -1 if no image found */
42static 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
87a0a681 90static int image_probe(AVProbeData *p)
de6d9b64 91{
94d883e8 92 if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
f4824c0f 93 return AVPROBE_SCORE_MAX-1;
87a0a681 94 else
de6d9b64 95 return 0;
de6d9b64
FB
96}
97
87a0a681 98static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
de6d9b64 99{
87a0a681 100 VideoData *s = opaque;
de6d9b64 101
87a0a681
FB
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;
de6d9b64
FB
107}
108
109static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
110{
c9a65ca8 111 VideoData *s = s1->priv_data;
ca1538f8 112 int ret, first_index, last_index;
de6d9b64 113 char buf[1024];
de6d9b64
FB
114 ByteIOContext pb1, *f = &pb1;
115 AVStream *st;
116
c9a65ca8 117 st = av_new_stream(s1, 0);
de6d9b64 118 if (!st) {
1ea4f593 119 av_free(s);
de6d9b64
FB
120 return -ENOMEM;
121 }
c9a65ca8 122
87a0a681
FB
123 if (ap && ap->image_format)
124 s->img_fmt = ap->image_format;
125
568e18b1 126 pstrcpy(s->path, sizeof(s->path), s1->filename);
de6d9b64 127 s->img_number = 0;
22b37f5d
FB
128 s->img_count = 0;
129
de6d9b64 130 /* find format */
c9a65ca8 131 if (s1->iformat->flags & AVFMT_NOFILE)
de6d9b64
FB
132 s->is_pipe = 0;
133 else
134 s->is_pipe = 1;
c0df9d75
MN
135
136 if (!ap || !ap->time_base.num) {
01f4895c 137 st->codec->time_base= (AVRational){1,25};
ca1538f8 138 } else {
01f4895c 139 st->codec->time_base= ap->time_base;
ca1538f8 140 }
c0df9d75 141
de6d9b64 142 if (!s->is_pipe) {
ca1538f8
FB
143 if (find_image_range(&first_index, &last_index, s->path) < 0)
144 goto fail;
22b37f5d
FB
145 s->img_first = first_index;
146 s->img_last = last_index;
ca1538f8
FB
147 s->img_number = first_index;
148 /* compute duration */
149 st->start_time = 0;
c0df9d75 150 st->duration = last_index - first_index + 1;
ca1538f8
FB
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)
de6d9b64
FB
154 goto fail;
155 } else {
156 f = &s1->pb;
157 }
158
87a0a681
FB
159 ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
160 if (ret < 0)
161 goto fail1;
de6d9b64
FB
162
163 if (!s->is_pipe) {
164 url_fclose(f);
165 } else {
166 url_fseek(f, 0, SEEK_SET);
167 }
168
01f4895c
MN
169 st->codec->codec_type = CODEC_TYPE_VIDEO;
170 st->codec->codec_id = CODEC_ID_RAWVIDEO;
171 st->codec->width = s->width;
172 st->codec->height = s->height;
173 st->codec->pix_fmt = s->pix_fmt;
7f2ca68a 174 s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15));
87a0a681 175
de6d9b64
FB
176 return 0;
177 fail1:
178 if (!s->is_pipe)
179 url_fclose(f);
180 fail:
1ea4f593 181 av_free(s);
0bd586c5 182 return AVERROR_IO;
de6d9b64
FB
183}
184
87a0a681 185static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
de6d9b64 186{
87a0a681
FB
187 VideoData *s = opaque;
188
189 if (info->width != s->width ||
190 info->height != s->height)
191 return -1;
7f2ca68a 192 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15));
de6d9b64
FB
193 return 0;
194}
195
87a0a681 196static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
de6d9b64 197{
87a0a681
FB
198 VideoData *s = s1->priv_data;
199 char filename[1024];
200 int ret;
201 ByteIOContext f1, *f;
de6d9b64 202
87a0a681 203 if (!s->is_pipe) {
22b37f5d
FB
204 /* loop over input */
205 if (loop_input && s->img_number > s->img_last) {
206 s->img_number = s->img_first;
207 }
87a0a681
FB
208 if (get_frame_filename(filename, sizeof(filename),
209 s->path, s->img_number) < 0)
0bd586c5 210 return AVERROR_IO;
87a0a681
FB
211 f = &f1;
212 if (url_fopen(f, filename, URL_RDONLY) < 0)
0bd586c5 213 return AVERROR_IO;
87a0a681
FB
214 } else {
215 f = &s1->pb;
216 if (url_feof(f))
0bd586c5 217 return AVERROR_IO;
87a0a681 218 }
6775c758 219
87a0a681
FB
220 av_new_packet(pkt, s->img_size);
221 pkt->stream_index = 0;
222
223 s->ptr = pkt->data;
224 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
225 if (!s->is_pipe) {
226 url_fclose(f);
6775c758
FB
227 }
228
87a0a681
FB
229 if (ret < 0) {
230 av_free_packet(pkt);
0bd586c5 231 return AVERROR_IO; /* signal EOF */
87a0a681 232 } else {
22b37f5d
FB
233 /* XXX: computing this pts is not necessary as it is done in
234 the generic code too */
01f4895c 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;
22b37f5d 236 s->img_count++;
87a0a681
FB
237 s->img_number++;
238 return 0;
239 }
6775c758
FB
240}
241
87a0a681 242static int img_read_close(AVFormatContext *s1)
de6d9b64 243{
de6d9b64
FB
244 return 0;
245}
246
87a0a681
FB
247/******************************************************/
248/* image output */
249
250static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
ad436907 251{
87a0a681
FB
252 VideoData *img = s->priv_data;
253 AVStream *st;
254 AVImageFormat *img_fmt;
255 int i;
256
257 /* find output image format */
258 if (ap && ap->image_format) {
259 img_fmt = ap->image_format;
ad436907 260 } else {
87a0a681 261 img_fmt = guess_image_format(s->filename);
ad436907 262 }
87a0a681
FB
263 if (!img_fmt)
264 return -1;
ad436907 265
87a0a681
FB
266 if (s->nb_streams != 1)
267 return -1;
268
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;
ad436907 274 }
87a0a681
FB
275 if (i >= PIX_FMT_NB)
276 return -1;
277 img->img_fmt = img_fmt;
278 img->pix_fmt = i;
01f4895c 279 st->codec->pix_fmt = img->pix_fmt;
ad436907
HM
280 return 0;
281}
282
de6d9b64
FB
283static int img_write_header(AVFormatContext *s)
284{
c9a65ca8 285 VideoData *img = s->priv_data;
de6d9b64 286
de6d9b64 287 img->img_number = 1;
568e18b1 288 pstrcpy(img->path, sizeof(img->path), s->filename);
de6d9b64
FB
289
290 /* find format */
c9a65ca8 291 if (s->oformat->flags & AVFMT_NOFILE)
de6d9b64
FB
292 img->is_pipe = 0;
293 else
294 img->is_pipe = 1;
295
de6d9b64 296 return 0;
de6d9b64
FB
297}
298
e928649b 299static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64
FB
300{
301 VideoData *img = s->priv_data;
e928649b 302 AVStream *st = s->streams[pkt->stream_index];
de6d9b64 303 ByteIOContext pb1, *pb;
87a0a681
FB
304 AVPicture *picture;
305 int width, height, ret;
de6d9b64 306 char filename[1024];
87a0a681 307 AVImageInfo info;
de6d9b64 308
01f4895c
MN
309 width = st->codec->width;
310 height = st->codec->height;
ad436907 311
e928649b 312 picture = (AVPicture *)pkt->data;
de6d9b64 313
de6d9b64 314 if (!img->is_pipe) {
87a0a681
FB
315 if (get_frame_filename(filename, sizeof(filename),
316 img->path, img->img_number) < 0)
0bd586c5 317 return AVERROR_IO;
de6d9b64
FB
318 pb = &pb1;
319 if (url_fopen(pb, filename, URL_WRONLY) < 0)
0bd586c5 320 return AVERROR_IO;
de6d9b64
FB
321 } else {
322 pb = &s->pb;
323 }
87a0a681
FB
324 info.width = width;
325 info.height = height;
01f4895c 326 info.pix_fmt = st->codec->pix_fmt;
ffaa816e 327 info.interleaved = 0; /* FIXME: there should be a way to set it right */
87a0a681
FB
328 info.pict = *picture;
329 ret = av_write_image(pb, img->img_fmt, &info);
de6d9b64
FB
330 if (!img->is_pipe) {
331 url_fclose(pb);
332 }
333
334 img->img_number++;
335 return 0;
336}
337
338static int img_write_trailer(AVFormatContext *s)
339{
de6d9b64
FB
340 return 0;
341}
342
87a0a681 343/* input */
de6d9b64 344
87a0a681
FB
345static AVInputFormat image_iformat = {
346 "image",
347 "image sequence",
c9a65ca8 348 sizeof(VideoData),
87a0a681 349 image_probe,
de6d9b64
FB
350 img_read_header,
351 img_read_packet,
352 img_read_close,
353 NULL,
e5d64d68 354 NULL,
9150f42e 355 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
de6d9b64
FB
356};
357
87a0a681
FB
358static AVInputFormat imagepipe_iformat = {
359 "imagepipe",
360 "piped image sequence",
c9a65ca8
FB
361 sizeof(VideoData),
362 NULL, /* no probe */
de6d9b64
FB
363 img_read_header,
364 img_read_packet,
365 img_read_close,
366 NULL,
de6d9b64
FB
367};
368
de6d9b64 369
87a0a681 370/* output */
6775c758 371
87a0a681
FB
372static AVOutputFormat image_oformat = {
373 "image",
374 "image sequence",
6775c758 375 "",
6775c758 376 "",
c9a65ca8 377 sizeof(VideoData),
6775c758
FB
378 CODEC_ID_NONE,
379 CODEC_ID_RAWVIDEO,
380 img_write_header,
381 img_write_packet,
382 img_write_trailer,
87a0a681
FB
383 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
384 img_set_parameters,
6775c758 385};
c9a65ca8 386
87a0a681
FB
387static AVOutputFormat imagepipe_oformat = {
388 "imagepipe",
389 "piped image sequence",
390 "",
ad436907 391 "",
ad436907
HM
392 sizeof(VideoData),
393 CODEC_ID_NONE,
394 CODEC_ID_RAWVIDEO,
395 img_write_header,
396 img_write_packet,
397 img_write_trailer,
94d883e8 398 AVFMT_RAWPICTURE,
87a0a681 399 img_set_parameters,
ad436907
HM
400};
401
c9a65ca8
FB
402int img_init(void)
403{
87a0a681
FB
404 av_register_input_format(&image_iformat);
405 av_register_output_format(&image_oformat);
c9a65ca8 406
87a0a681
FB
407 av_register_input_format(&imagepipe_iformat);
408 av_register_output_format(&imagepipe_oformat);
ad436907 409
c9a65ca8
FB
410 return 0;
411}