Mercurial > libavformat.hg
annotate img2.c @ 2086:0cd7cfbee2e8 libavformat
Don't forget about audio remaining in buffer, in case there is enough
left to make another DV frame.
author | corey |
---|---|
date | Wed, 16 May 2007 02:02:22 +0000 |
parents | 5a9a03da8fba |
children | da71207a7cf1 |
rev | line source |
---|---|
497 | 1 /* |
2 * Image format | |
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard. | |
4 * Copyright (c) 2004 Michael Niedermayer | |
5 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1291
diff
changeset
|
6 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1291
diff
changeset
|
7 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1291
diff
changeset
|
8 * FFmpeg is free software; you can redistribute it and/or |
497 | 9 * modify it under the terms of the GNU Lesser General Public |
10 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1291
diff
changeset
|
11 * version 2.1 of the License, or (at your option) any later version. |
497 | 12 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1291
diff
changeset
|
13 * FFmpeg is distributed in the hope that it will be useful, |
497 | 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1291
diff
changeset
|
19 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
497 | 21 */ |
22 #include "avformat.h" | |
23 | |
24 typedef struct { | |
25 int img_first; | |
26 int img_last; | |
27 int img_number; | |
28 int img_count; | |
29 int is_pipe; | |
30 char path[1024]; | |
31 } VideoData; | |
32 | |
33 typedef struct { | |
34 enum CodecID id; | |
35 const char *str; | |
36 } IdStrMap; | |
37 | |
38 static const IdStrMap img_tags[] = { | |
39 { CODEC_ID_MJPEG , "jpeg"}, | |
40 { CODEC_ID_MJPEG , "jpg"}, | |
41 { CODEC_ID_LJPEG , "ljpg"}, | |
581 | 42 { CODEC_ID_PNG , "png"}, |
583 | 43 { CODEC_ID_PPM , "ppm"}, |
44 { CODEC_ID_PGM , "pgm"}, | |
45 { CODEC_ID_PGMYUV , "pgmyuv"}, | |
46 { CODEC_ID_PBM , "pbm"}, | |
47 { CODEC_ID_PAM , "pam"}, | |
497 | 48 { CODEC_ID_MPEG1VIDEO, "mpg1-img"}, |
49 { CODEC_ID_MPEG2VIDEO, "mpg2-img"}, | |
50 { CODEC_ID_MPEG4 , "mpg4-img"}, | |
51 { CODEC_ID_FFV1 , "ffv1-img"}, | |
635 | 52 { CODEC_ID_RAWVIDEO , "y"}, |
875 | 53 { CODEC_ID_BMP , "bmp"}, |
1409 | 54 { CODEC_ID_GIF , "gif"}, |
1416 | 55 { CODEC_ID_TARGA , "tga"}, |
56 { CODEC_ID_TIFF , "tiff"}, | |
1985 | 57 { CODEC_ID_SGI , "sgi"}, |
2074 | 58 { CODEC_ID_PTX , "ptx"}, |
497 | 59 {0, NULL} |
60 }; | |
61 | |
635 | 62 static int sizes[][2] = { |
63 { 640, 480 }, | |
64 { 720, 480 }, | |
65 { 720, 576 }, | |
66 { 352, 288 }, | |
67 { 352, 240 }, | |
68 { 160, 128 }, | |
69 { 512, 384 }, | |
70 { 640, 352 }, | |
71 { 640, 240 }, | |
72 }; | |
73 | |
74 static int infer_size(int *width_ptr, int *height_ptr, int size) | |
75 { | |
76 int i; | |
77 | |
78 for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) { | |
79 if ((sizes[i][0] * sizes[i][1]) == size) { | |
80 *width_ptr = sizes[i][0]; | |
81 *height_ptr = sizes[i][1]; | |
82 return 0; | |
83 } | |
84 } | |
85 return -1; | |
86 } | |
497 | 87 static enum CodecID av_str2id(const IdStrMap *tags, const char *str) |
88 { | |
530 | 89 str= strrchr(str, '.'); |
90 if(!str) return CODEC_ID_NONE; | |
91 str++; | |
497 | 92 |
93 while (tags->id) { | |
94 int i; | |
95 for(i=0; toupper(tags->str[i]) == toupper(str[i]); i++){ | |
96 if(tags->str[i]==0 && str[i]==0) | |
97 return tags->id; | |
98 } | |
99 | |
100 tags++; | |
101 } | |
102 return CODEC_ID_NONE; | |
103 } | |
104 | |
105 /* return -1 if no image found */ | |
885 | 106 static int find_image_range(int *pfirst_index, int *plast_index, |
497 | 107 const char *path) |
108 { | |
109 char buf[1024]; | |
110 int range, last_index, range1, first_index; | |
111 | |
112 /* find the first image */ | |
113 for(first_index = 0; first_index < 5; first_index++) { | |
1291
185190bdc185
Clarified API for numbered sequences, patch by Michel Bardiaux % mbardiaux A mediaxim P be %
gpoirier
parents:
1175
diff
changeset
|
114 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){ |
885 | 115 *pfirst_index = |
498 | 116 *plast_index = 1; |
117 return 0; | |
118 } | |
497 | 119 if (url_exist(buf)) |
120 break; | |
121 } | |
122 if (first_index == 5) | |
123 goto fail; | |
885 | 124 |
497 | 125 /* find the last image */ |
126 last_index = first_index; | |
127 for(;;) { | |
128 range = 0; | |
129 for(;;) { | |
130 if (!range) | |
131 range1 = 1; | |
132 else | |
133 range1 = 2 * range; | |
1291
185190bdc185
Clarified API for numbered sequences, patch by Michel Bardiaux % mbardiaux A mediaxim P be %
gpoirier
parents:
1175
diff
changeset
|
134 if (av_get_frame_filename(buf, sizeof(buf), path, |
185190bdc185
Clarified API for numbered sequences, patch by Michel Bardiaux % mbardiaux A mediaxim P be %
gpoirier
parents:
1175
diff
changeset
|
135 last_index + range1) < 0) |
497 | 136 goto fail; |
137 if (!url_exist(buf)) | |
138 break; | |
139 range = range1; | |
140 /* just in case... */ | |
141 if (range >= (1 << 30)) | |
142 goto fail; | |
143 } | |
144 /* we are sure than image last_index + range exists */ | |
145 if (!range) | |
146 break; | |
147 last_index += range; | |
148 } | |
149 *pfirst_index = first_index; | |
150 *plast_index = last_index; | |
151 return 0; | |
152 fail: | |
153 return -1; | |
154 } | |
155 | |
156 | |
157 static int image_probe(AVProbeData *p) | |
158 { | |
1594
ffb64cb62cc9
Fix a crash when probing img2 format with a NULL filename.
aurel
parents:
1551
diff
changeset
|
159 if (p->filename && av_str2id(img_tags, p->filename)) { |
1551
ee4ef413497e
probe with some success image files not containing number pattern but having recognized image extension
bcoudurier
parents:
1416
diff
changeset
|
160 if (av_filename_number_test(p->filename)) |
ee4ef413497e
probe with some success image files not containing number pattern but having recognized image extension
bcoudurier
parents:
1416
diff
changeset
|
161 return AVPROBE_SCORE_MAX; |
ee4ef413497e
probe with some success image files not containing number pattern but having recognized image extension
bcoudurier
parents:
1416
diff
changeset
|
162 else |
ee4ef413497e
probe with some success image files not containing number pattern but having recognized image extension
bcoudurier
parents:
1416
diff
changeset
|
163 return AVPROBE_SCORE_MAX/2; |
ee4ef413497e
probe with some success image files not containing number pattern but having recognized image extension
bcoudurier
parents:
1416
diff
changeset
|
164 } |
ee4ef413497e
probe with some success image files not containing number pattern but having recognized image extension
bcoudurier
parents:
1416
diff
changeset
|
165 return 0; |
497 | 166 } |
167 | |
583 | 168 enum CodecID av_guess_image2_codec(const char *filename){ |
169 return av_str2id(img_tags, filename); | |
170 } | |
171 | |
497 | 172 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap) |
173 { | |
174 VideoData *s = s1->priv_data; | |
175 int first_index, last_index; | |
176 AVStream *st; | |
177 | |
178 s1->ctx_flags |= AVFMTCTX_NOHEADER; | |
179 | |
180 st = av_new_stream(s1, 0); | |
181 if (!st) { | |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1594
diff
changeset
|
182 return AVERROR(ENOMEM); |
497 | 183 } |
184 | |
639 | 185 pstrcpy(s->path, sizeof(s->path), s1->filename); |
497 | 186 s->img_number = 0; |
187 s->img_count = 0; | |
885 | 188 |
497 | 189 /* find format */ |
190 if (s1->iformat->flags & AVFMT_NOFILE) | |
191 s->is_pipe = 0; | |
574 | 192 else{ |
497 | 193 s->is_pipe = 1; |
2023 | 194 st->need_parsing = AVSTREAM_PARSE_FULL; |
574 | 195 } |
885 | 196 |
1003 | 197 if (!ap->time_base.num) { |
743 | 198 av_set_pts_info(st, 60, 1, 25); |
497 | 199 } else { |
743 | 200 av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den); |
497 | 201 } |
885 | 202 |
1003 | 203 if(ap->width && ap->height){ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
204 st->codec->width = ap->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
205 st->codec->height= ap->height; |
635 | 206 } |
885 | 207 |
497 | 208 if (!s->is_pipe) { |
209 if (find_image_range(&first_index, &last_index, s->path) < 0) | |
622 | 210 return AVERROR_IO; |
497 | 211 s->img_first = first_index; |
212 s->img_last = last_index; | |
213 s->img_number = first_index; | |
214 /* compute duration */ | |
215 st->start_time = 0; | |
743 | 216 st->duration = last_index - first_index + 1; |
497 | 217 } |
885 | 218 |
583 | 219 if(ap->video_codec_id){ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
220 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:
764
diff
changeset
|
221 st->codec->codec_id = ap->video_codec_id; |
583 | 222 }else if(ap->audio_codec_id){ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
223 st->codec->codec_type = CODEC_TYPE_AUDIO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
224 st->codec->codec_id = ap->audio_codec_id; |
583 | 225 }else{ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
226 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:
764
diff
changeset
|
227 st->codec->codec_id = av_str2id(img_tags, s->path); |
583 | 228 } |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
229 if(st->codec->codec_type == CODEC_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE) |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
230 st->codec->pix_fmt = ap->pix_fmt; |
497 | 231 |
232 return 0; | |
233 } | |
234 | |
235 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt) | |
236 { | |
237 VideoData *s = s1->priv_data; | |
238 char filename[1024]; | |
635 | 239 int i; |
240 int size[3]={0}, ret[3]={0}; | |
241 ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]}; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
242 AVCodecContext *codec= s1->streams[0]->codec; |
497 | 243 |
244 if (!s->is_pipe) { | |
245 /* loop over input */ | |
1175
8b53c0f3e7ad
add loop_input to AVFormatContext, getting rid of old hack
mru
parents:
1169
diff
changeset
|
246 if (s1->loop_input && s->img_number > s->img_last) { |
497 | 247 s->img_number = s->img_first; |
590 | 248 } |
1291
185190bdc185
Clarified API for numbered sequences, patch by Michel Bardiaux % mbardiaux A mediaxim P be %
gpoirier
parents:
1175
diff
changeset
|
249 if (av_get_frame_filename(filename, sizeof(filename), |
185190bdc185
Clarified API for numbered sequences, patch by Michel Bardiaux % mbardiaux A mediaxim P be %
gpoirier
parents:
1175
diff
changeset
|
250 s->path, s->img_number)<0 && s->img_number > 1) |
497 | 251 return AVERROR_IO; |
635 | 252 for(i=0; i<3; i++){ |
253 if (url_fopen(f[i], filename, URL_RDONLY) < 0) | |
254 return AVERROR_IO; | |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
743
diff
changeset
|
255 size[i]= url_fsize(f[i]); |
885 | 256 |
635 | 257 if(codec->codec_id != CODEC_ID_RAWVIDEO) |
258 break; | |
259 filename[ strlen(filename) - 1 ]= 'U' + i; | |
260 } | |
885 | 261 |
635 | 262 if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width) |
263 infer_size(&codec->width, &codec->height, size[0]); | |
497 | 264 } else { |
635 | 265 f[0] = &s1->pb; |
266 if (url_feof(f[0])) | |
497 | 267 return AVERROR_IO; |
635 | 268 size[0]= 4096; |
497 | 269 } |
270 | |
635 | 271 av_new_packet(pkt, size[0] + size[1] + size[2]); |
497 | 272 pkt->stream_index = 0; |
273 pkt->flags |= PKT_FLAG_KEY; | |
274 | |
635 | 275 pkt->size= 0; |
276 for(i=0; i<3; i++){ | |
277 if(size[i]){ | |
278 ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]); | |
279 if (!s->is_pipe) | |
280 url_fclose(f[i]); | |
281 if(ret[i]>0) | |
282 pkt->size += ret[i]; | |
283 } | |
497 | 284 } |
285 | |
635 | 286 if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) { |
497 | 287 av_free_packet(pkt); |
288 return AVERROR_IO; /* signal EOF */ | |
289 } else { | |
290 s->img_count++; | |
291 s->img_number++; | |
292 return 0; | |
293 } | |
294 } | |
295 | |
296 static int img_read_close(AVFormatContext *s1) | |
297 { | |
298 return 0; | |
299 } | |
300 | |
903
68bc3ca12e79
Put muxer-specific code parts in #ifdef CONFIG_MUXERS.
diego
parents:
896
diff
changeset
|
301 #ifdef CONFIG_MUXERS |
497 | 302 /******************************************************/ |
303 /* image output */ | |
304 | |
305 static int img_write_header(AVFormatContext *s) | |
306 { | |
307 VideoData *img = s->priv_data; | |
308 | |
309 img->img_number = 1; | |
639 | 310 pstrcpy(img->path, sizeof(img->path), s->filename); |
497 | 311 |
312 /* find format */ | |
313 if (s->oformat->flags & AVFMT_NOFILE) | |
314 img->is_pipe = 0; | |
315 else | |
316 img->is_pipe = 1; | |
885 | 317 |
497 | 318 return 0; |
319 } | |
320 | |
321 static int img_write_packet(AVFormatContext *s, AVPacket *pkt) | |
322 { | |
323 VideoData *img = s->priv_data; | |
635 | 324 ByteIOContext pb1[3], *pb[3]= {&pb1[0], &pb1[1], &pb1[2]}; |
497 | 325 char filename[1024]; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
764
diff
changeset
|
326 AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec; |
635 | 327 int i; |
497 | 328 |
329 if (!img->is_pipe) { | |
1291
185190bdc185
Clarified API for numbered sequences, patch by Michel Bardiaux % mbardiaux A mediaxim P be %
gpoirier
parents:
1175
diff
changeset
|
330 if (av_get_frame_filename(filename, sizeof(filename), |
185190bdc185
Clarified API for numbered sequences, patch by Michel Bardiaux % mbardiaux A mediaxim P be %
gpoirier
parents:
1175
diff
changeset
|
331 img->path, img->img_number) < 0 && img->img_number>1) |
497 | 332 return AVERROR_IO; |
635 | 333 for(i=0; i<3; i++){ |
334 if (url_fopen(pb[i], filename, URL_WRONLY) < 0) | |
335 return AVERROR_IO; | |
885 | 336 |
635 | 337 if(codec->codec_id != CODEC_ID_RAWVIDEO) |
338 break; | |
339 filename[ strlen(filename) - 1 ]= 'U' + i; | |
340 } | |
497 | 341 } else { |
635 | 342 pb[0] = &s->pb; |
497 | 343 } |
885 | 344 |
635 | 345 if(codec->codec_id == CODEC_ID_RAWVIDEO){ |
731 | 346 int ysize = codec->width * codec->height; |
347 put_buffer(pb[0], pkt->data , ysize); | |
348 put_buffer(pb[1], pkt->data + ysize, (pkt->size - ysize)/2); | |
349 put_buffer(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2); | |
635 | 350 put_flush_packet(pb[1]); |
351 put_flush_packet(pb[2]); | |
352 url_fclose(pb[1]); | |
353 url_fclose(pb[2]); | |
354 }else{ | |
355 put_buffer(pb[0], pkt->data, pkt->size); | |
356 } | |
357 put_flush_packet(pb[0]); | |
497 | 358 if (!img->is_pipe) { |
635 | 359 url_fclose(pb[0]); |
497 | 360 } |
361 | |
362 img->img_number++; | |
363 return 0; | |
364 } | |
365 | |
366 static int img_write_trailer(AVFormatContext *s) | |
367 { | |
368 return 0; | |
369 } | |
370 | |
903
68bc3ca12e79
Put muxer-specific code parts in #ifdef CONFIG_MUXERS.
diego
parents:
896
diff
changeset
|
371 #endif /* CONFIG_MUXERS */ |
68bc3ca12e79
Put muxer-specific code parts in #ifdef CONFIG_MUXERS.
diego
parents:
896
diff
changeset
|
372 |
497 | 373 /* input */ |
1169 | 374 #ifdef CONFIG_IMAGE2_DEMUXER |
375 AVInputFormat image2_demuxer = { | |
497 | 376 "image2", |
377 "image2 sequence", | |
378 sizeof(VideoData), | |
379 image_probe, | |
380 img_read_header, | |
381 img_read_packet, | |
382 img_read_close, | |
383 NULL, | |
384 NULL, | |
498 | 385 AVFMT_NOFILE, |
497 | 386 }; |
1169 | 387 #endif |
388 #ifdef CONFIG_IMAGE2PIPE_DEMUXER | |
389 AVInputFormat image2pipe_demuxer = { | |
497 | 390 "image2pipe", |
391 "piped image2 sequence", | |
392 sizeof(VideoData), | |
393 NULL, /* no probe */ | |
394 img_read_header, | |
395 img_read_packet, | |
396 img_read_close, | |
397 NULL, | |
398 }; | |
1169 | 399 #endif |
497 | 400 |
401 /* output */ | |
1169 | 402 #ifdef CONFIG_IMAGE2_MUXER |
403 AVOutputFormat image2_muxer = { | |
497 | 404 "image2", |
405 "image2 sequence", | |
406 "", | |
407 "", | |
408 sizeof(VideoData), | |
409 CODEC_ID_NONE, | |
410 CODEC_ID_MJPEG, | |
411 img_write_header, | |
412 img_write_packet, | |
413 img_write_trailer, | |
498 | 414 AVFMT_NOFILE, |
497 | 415 }; |
1169 | 416 #endif |
417 #ifdef CONFIG_IMAGE2PIPE_MUXER | |
418 AVOutputFormat image2pipe_muxer = { | |
497 | 419 "image2pipe", |
420 "piped image2 sequence", | |
421 "", | |
422 "", | |
423 sizeof(VideoData), | |
424 CODEC_ID_NONE, | |
425 CODEC_ID_MJPEG, | |
426 img_write_header, | |
427 img_write_packet, | |
428 img_write_trailer, | |
429 }; | |
903
68bc3ca12e79
Put muxer-specific code parts in #ifdef CONFIG_MUXERS.
diego
parents:
896
diff
changeset
|
430 #endif |