Mercurial > libavformat.hg
annotate img.c @ 1062:008ee39dd182 libavformat
add AVID DV fourcc
author | bcoudurier |
---|---|
date | Wed, 19 Apr 2006 12:56:28 +0000 |
parents | 5c750fb6b8cc |
children | d89d7ef290da |
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 | |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 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 */ | |
885 | 42 static int find_image_range(int *pfirst_index, int *plast_index, |
189 | 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; | |
885 | 57 |
189 | 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; | |
885 | 67 if (get_frame_filename(buf, sizeof(buf), path, |
189 | 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)) |
582 | 93 return AVPROBE_SCORE_MAX-1; |
20 | 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 return -ENOMEM; | |
120 } | |
121 | |
1003 | 122 if (ap->image_format) |
20 | 123 s->img_fmt = ap->image_format; |
124 | |
639 | 125 pstrcpy(s->path, sizeof(s->path), s1->filename); |
20 | 126 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
|
127 s->img_count = 0; |
885 | 128 |
20 | 129 /* find format */ |
130 if (s1->iformat->flags & AVFMT_NOFILE) | |
131 s->is_pipe = 0; | |
132 else | |
133 s->is_pipe = 1; | |
743 | 134 |
1003 | 135 if (!ap->time_base.num) { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
136 st->codec->time_base= (AVRational){1,25}; |
189 | 137 } else { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
138 st->codec->time_base= ap->time_base; |
189 | 139 } |
885 | 140 |
20 | 141 if (!s->is_pipe) { |
189 | 142 if (find_image_range(&first_index, &last_index, s->path) < 0) |
143 goto fail; | |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
144 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
|
145 s->img_last = last_index; |
189 | 146 s->img_number = first_index; |
147 /* compute duration */ | |
148 st->start_time = 0; | |
743 | 149 st->duration = last_index - first_index + 1; |
189 | 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) | |
20 | 153 goto fail; |
154 } else { | |
155 f = &s1->pb; | |
0 | 156 } |
885 | 157 |
20 | 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 } | |
885 | 167 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
168 st->codec->codec_type = CODEC_TYPE_VIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
169 st->codec->codec_id = CODEC_ID_RAWVIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
170 st->codec->width = s->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
171 st->codec->height = s->height; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
172 st->codec->pix_fmt = s->pix_fmt; |
459 | 173 s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15)); |
20 | 174 |
0 | 175 return 0; |
20 | 176 fail1: |
177 if (!s->is_pipe) | |
178 url_fclose(f); | |
179 fail: | |
482 | 180 return AVERROR_IO; |
0 | 181 } |
182 | |
20 | 183 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info) |
0 | 184 { |
20 | 185 VideoData *s = opaque; |
0 | 186 |
20 | 187 if (info->width != s->width || |
188 info->height != s->height) | |
189 return -1; | |
459 | 190 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15)); |
0 | 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) { | |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
202 /* loop over input */ |
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
203 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
|
204 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
|
205 } |
20 | 206 if (get_frame_filename(filename, sizeof(filename), |
207 s->path, s->img_number) < 0) | |
482 | 208 return AVERROR_IO; |
0 | 209 f = &f1; |
210 if (url_fopen(f, filename, URL_RDONLY) < 0) | |
482 | 211 return AVERROR_IO; |
0 | 212 } else { |
213 f = &s1->pb; | |
214 if (url_feof(f)) | |
482 | 215 return AVERROR_IO; |
0 | 216 } |
217 | |
218 av_new_packet(pkt, s->img_size); | |
219 pkt->stream_index = 0; | |
220 | |
20 | 221 s->ptr = pkt->data; |
222 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s); | |
0 | 223 if (!s->is_pipe) { |
224 url_fclose(f); | |
225 } | |
226 | |
227 if (ret < 0) { | |
228 av_free_packet(pkt); | |
482 | 229 return AVERROR_IO; /* signal EOF */ |
0 | 230 } else { |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
231 /* 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
|
232 the generic code too */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
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; |
199
66a05c4f8350
suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents:
189
diff
changeset
|
234 s->img_count++; |
0 | 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 | |
20 | 248 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
0 | 249 { |
20 | 250 VideoData *img = s->priv_data; |
251 AVStream *st; | |
252 AVImageFormat *img_fmt; | |
0 | 253 int i; |
254 | |
20 | 255 /* find output image format */ |
1003 | 256 if (ap->image_format) { |
20 | 257 img_fmt = ap->image_format; |
258 } else { | |
259 img_fmt = guess_image_format(s->filename); | |
260 } | |
261 if (!img_fmt) | |
262 return -1; | |
0 | 263 |
20 | 264 if (s->nb_streams != 1) |
265 return -1; | |
885 | 266 |
20 | 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; | |
0 | 272 } |
20 | 273 if (i >= PIX_FMT_NB) |
274 return -1; | |
275 img->img_fmt = img_fmt; | |
276 img->pix_fmt = i; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
277 st->codec->pix_fmt = img->pix_fmt; |
0 | 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; | |
639 | 286 pstrcpy(img->path, sizeof(img->path), s->filename); |
0 | 287 |
288 /* find format */ | |
289 if (s->oformat->flags & AVFMT_NOFILE) | |
290 img->is_pipe = 0; | |
291 else | |
292 img->is_pipe = 1; | |
885 | 293 |
0 | 294 return 0; |
295 } | |
296 | |
468 | 297 static int img_write_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 298 { |
299 VideoData *img = s->priv_data; | |
468 | 300 AVStream *st = s->streams[pkt->stream_index]; |
0 | 301 ByteIOContext pb1, *pb; |
20 | 302 AVPicture *picture; |
303 int width, height, ret; | |
0 | 304 char filename[1024]; |
20 | 305 AVImageInfo info; |
0 | 306 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
307 width = st->codec->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
308 height = st->codec->height; |
885 | 309 |
468 | 310 picture = (AVPicture *)pkt->data; |
0 | 311 |
20 | 312 if (!img->is_pipe) { |
885 | 313 if (get_frame_filename(filename, sizeof(filename), |
20 | 314 img->path, img->img_number) < 0) |
482 | 315 return AVERROR_IO; |
0 | 316 pb = &pb1; |
317 if (url_fopen(pb, filename, URL_WRONLY) < 0) | |
482 | 318 return AVERROR_IO; |
0 | 319 } else { |
320 pb = &s->pb; | |
321 } | |
20 | 322 info.width = width; |
323 info.height = height; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
743
diff
changeset
|
324 info.pix_fmt = st->codec->pix_fmt; |
280 | 325 info.interleaved = 0; /* FIXME: there should be a way to set it right */ |
20 | 326 info.pict = *picture; |
327 ret = av_write_image(pb, img->img_fmt, &info); | |
0 | 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 | |
20 | 341 /* input */ |
0 | 342 |
20 | 343 static AVInputFormat image_iformat = { |
344 "image", | |
345 "image sequence", | |
0 | 346 sizeof(VideoData), |
20 | 347 image_probe, |
0 | 348 img_read_header, |
349 img_read_packet, | |
350 img_read_close, | |
351 NULL, | |
439 | 352 NULL, |
0 | 353 AVFMT_NOFILE | AVFMT_NEEDNUMBER, |
354 }; | |
355 | |
20 | 356 static AVInputFormat imagepipe_iformat = { |
357 "imagepipe", | |
358 "piped image sequence", | |
0 | 359 sizeof(VideoData), |
360 NULL, /* no probe */ | |
361 img_read_header, | |
362 img_read_packet, | |
363 img_read_close, | |
364 NULL, | |
365 }; | |
366 | |
20 | 367 |
368 /* output */ | |
0 | 369 |
20 | 370 static AVOutputFormat image_oformat = { |
371 "image", | |
372 "image sequence", | |
0 | 373 "", |
20 | 374 "", |
0 | 375 sizeof(VideoData), |
376 CODEC_ID_NONE, | |
377 CODEC_ID_RAWVIDEO, | |
378 img_write_header, | |
379 img_write_packet, | |
380 img_write_trailer, | |
20 | 381 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE, |
382 img_set_parameters, | |
0 | 383 }; |
384 | |
20 | 385 static AVOutputFormat imagepipe_oformat = { |
386 "imagepipe", | |
387 "piped image sequence", | |
0 | 388 "", |
20 | 389 "", |
0 | 390 sizeof(VideoData), |
391 CODEC_ID_NONE, | |
392 CODEC_ID_RAWVIDEO, | |
393 img_write_header, | |
394 img_write_packet, | |
395 img_write_trailer, | |
21 | 396 AVFMT_RAWPICTURE, |
20 | 397 img_set_parameters, |
0 | 398 }; |
399 | |
400 int img_init(void) | |
401 { | |
20 | 402 av_register_input_format(&image_iformat); |
403 av_register_output_format(&image_oformat); | |
0 | 404 |
20 | 405 av_register_input_format(&imagepipe_iformat); |
406 av_register_output_format(&imagepipe_oformat); | |
885 | 407 |
0 | 408 return 0; |
409 } |