497
|
1 /*
|
|
2 * Image format
|
|
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
|
|
4 * Copyright (c) 2004 Michael Niedermayer
|
|
5 *
|
|
6 * This library is free software; you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU Lesser General Public
|
|
8 * License as published by the Free Software Foundation; either
|
|
9 * version 2 of the License, or (at your option) any later version.
|
|
10 *
|
|
11 * This library is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * Lesser General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU Lesser General Public
|
|
17 * License along with this library; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20 #include "avformat.h"
|
|
21
|
590
|
22 /* XXX: this is a hack */
|
|
23 extern int loop_input;
|
|
24
|
497
|
25 typedef struct {
|
|
26 int img_first;
|
|
27 int img_last;
|
|
28 int img_number;
|
|
29 int img_count;
|
|
30 int is_pipe;
|
|
31 char path[1024];
|
|
32 } VideoData;
|
|
33
|
|
34 typedef struct {
|
|
35 enum CodecID id;
|
|
36 const char *str;
|
|
37 } IdStrMap;
|
|
38
|
|
39 static const IdStrMap img_tags[] = {
|
|
40 { CODEC_ID_MJPEG , "jpeg"},
|
|
41 { CODEC_ID_MJPEG , "jpg"},
|
|
42 { CODEC_ID_LJPEG , "ljpg"},
|
581
|
43 { CODEC_ID_PNG , "png"},
|
583
|
44 { CODEC_ID_PPM , "ppm"},
|
|
45 { CODEC_ID_PGM , "pgm"},
|
|
46 { CODEC_ID_PGMYUV , "pgmyuv"},
|
|
47 { CODEC_ID_PBM , "pbm"},
|
|
48 { CODEC_ID_PAM , "pam"},
|
497
|
49 { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
|
|
50 { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
|
|
51 { CODEC_ID_MPEG4 , "mpg4-img"},
|
|
52 { CODEC_ID_FFV1 , "ffv1-img"},
|
635
|
53 { CODEC_ID_RAWVIDEO , "y"},
|
497
|
54 {0, NULL}
|
|
55 };
|
|
56
|
635
|
57 static int sizes[][2] = {
|
|
58 { 640, 480 },
|
|
59 { 720, 480 },
|
|
60 { 720, 576 },
|
|
61 { 352, 288 },
|
|
62 { 352, 240 },
|
|
63 { 160, 128 },
|
|
64 { 512, 384 },
|
|
65 { 640, 352 },
|
|
66 { 640, 240 },
|
|
67 };
|
|
68
|
|
69 static int infer_size(int *width_ptr, int *height_ptr, int size)
|
|
70 {
|
|
71 int i;
|
|
72
|
|
73 for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
|
|
74 if ((sizes[i][0] * sizes[i][1]) == size) {
|
|
75 *width_ptr = sizes[i][0];
|
|
76 *height_ptr = sizes[i][1];
|
|
77 return 0;
|
|
78 }
|
|
79 }
|
|
80 return -1;
|
|
81 }
|
497
|
82 static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
|
|
83 {
|
530
|
84 str= strrchr(str, '.');
|
|
85 if(!str) return CODEC_ID_NONE;
|
|
86 str++;
|
497
|
87
|
|
88 while (tags->id) {
|
|
89 int i;
|
|
90 for(i=0; toupper(tags->str[i]) == toupper(str[i]); i++){
|
|
91 if(tags->str[i]==0 && str[i]==0)
|
|
92 return tags->id;
|
|
93 }
|
|
94
|
|
95 tags++;
|
|
96 }
|
|
97 return CODEC_ID_NONE;
|
|
98 }
|
|
99
|
|
100 /* return -1 if no image found */
|
|
101 static int find_image_range(int *pfirst_index, int *plast_index,
|
|
102 const char *path)
|
|
103 {
|
|
104 char buf[1024];
|
|
105 int range, last_index, range1, first_index;
|
|
106
|
|
107 /* find the first image */
|
|
108 for(first_index = 0; first_index < 5; first_index++) {
|
498
|
109 if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
|
|
110 *pfirst_index =
|
|
111 *plast_index = 1;
|
|
112 return 0;
|
|
113 }
|
497
|
114 if (url_exist(buf))
|
|
115 break;
|
|
116 }
|
|
117 if (first_index == 5)
|
|
118 goto fail;
|
|
119
|
|
120 /* find the last image */
|
|
121 last_index = first_index;
|
|
122 for(;;) {
|
|
123 range = 0;
|
|
124 for(;;) {
|
|
125 if (!range)
|
|
126 range1 = 1;
|
|
127 else
|
|
128 range1 = 2 * range;
|
|
129 if (get_frame_filename(buf, sizeof(buf), path,
|
|
130 last_index + range1) < 0)
|
|
131 goto fail;
|
|
132 if (!url_exist(buf))
|
|
133 break;
|
|
134 range = range1;
|
|
135 /* just in case... */
|
|
136 if (range >= (1 << 30))
|
|
137 goto fail;
|
|
138 }
|
|
139 /* we are sure than image last_index + range exists */
|
|
140 if (!range)
|
|
141 break;
|
|
142 last_index += range;
|
|
143 }
|
|
144 *pfirst_index = first_index;
|
|
145 *plast_index = last_index;
|
|
146 return 0;
|
|
147 fail:
|
|
148 return -1;
|
|
149 }
|
|
150
|
|
151
|
|
152 static int image_probe(AVProbeData *p)
|
|
153 {
|
|
154 if (filename_number_test(p->filename) >= 0 && av_str2id(img_tags, p->filename))
|
|
155 return AVPROBE_SCORE_MAX;
|
|
156 else
|
|
157 return 0;
|
|
158 }
|
|
159
|
583
|
160 enum CodecID av_guess_image2_codec(const char *filename){
|
|
161 return av_str2id(img_tags, filename);
|
|
162 }
|
|
163
|
497
|
164 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
|
|
165 {
|
|
166 VideoData *s = s1->priv_data;
|
|
167 int first_index, last_index;
|
|
168 AVStream *st;
|
|
169
|
|
170 s1->ctx_flags |= AVFMTCTX_NOHEADER;
|
|
171
|
|
172 st = av_new_stream(s1, 0);
|
|
173 if (!st) {
|
|
174 return -ENOMEM;
|
|
175 }
|
|
176
|
639
|
177 pstrcpy(s->path, sizeof(s->path), s1->filename);
|
497
|
178 s->img_number = 0;
|
|
179 s->img_count = 0;
|
|
180
|
|
181 /* find format */
|
|
182 if (s1->iformat->flags & AVFMT_NOFILE)
|
|
183 s->is_pipe = 0;
|
574
|
184 else{
|
497
|
185 s->is_pipe = 1;
|
574
|
186 st->need_parsing= 1;
|
|
187 }
|
497
|
188
|
|
189 if (!ap || !ap->frame_rate) {
|
|
190 st->codec.frame_rate = 25;
|
|
191 st->codec.frame_rate_base = 1;
|
|
192 } else {
|
|
193 st->codec.frame_rate = ap->frame_rate;
|
|
194 st->codec.frame_rate_base = ap->frame_rate_base;
|
|
195 }
|
|
196
|
635
|
197 if(ap && ap->width && ap->height){
|
|
198 st->codec.width = ap->width;
|
|
199 st->codec.height= ap->height;
|
|
200 }
|
|
201
|
497
|
202 if (!s->is_pipe) {
|
|
203 if (find_image_range(&first_index, &last_index, s->path) < 0)
|
622
|
204 return AVERROR_IO;
|
497
|
205 s->img_first = first_index;
|
|
206 s->img_last = last_index;
|
|
207 s->img_number = first_index;
|
|
208 /* compute duration */
|
|
209 st->start_time = 0;
|
|
210 st->duration = ((int64_t)AV_TIME_BASE *
|
|
211 (last_index - first_index + 1) *
|
|
212 st->codec.frame_rate_base) / st->codec.frame_rate;
|
|
213 }
|
|
214
|
583
|
215 if(ap->video_codec_id){
|
|
216 st->codec.codec_type = CODEC_TYPE_VIDEO;
|
|
217 st->codec.codec_id = ap->video_codec_id;
|
|
218 }else if(ap->audio_codec_id){
|
|
219 st->codec.codec_type = CODEC_TYPE_AUDIO;
|
|
220 st->codec.codec_id = ap->audio_codec_id;
|
|
221 }else{
|
|
222 st->codec.codec_type = CODEC_TYPE_VIDEO;
|
|
223 st->codec.codec_id = av_str2id(img_tags, s->path);
|
|
224 }
|
497
|
225
|
|
226 return 0;
|
|
227 }
|
|
228
|
|
229 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
|
230 {
|
|
231 VideoData *s = s1->priv_data;
|
|
232 char filename[1024];
|
635
|
233 int i;
|
|
234 int size[3]={0}, ret[3]={0};
|
|
235 ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]};
|
|
236 AVCodecContext *codec= &s1->streams[0]->codec;
|
497
|
237
|
|
238 if (!s->is_pipe) {
|
|
239 /* loop over input */
|
590
|
240 if (loop_input && s->img_number > s->img_last) {
|
497
|
241 s->img_number = s->img_first;
|
590
|
242 }
|
497
|
243 if (get_frame_filename(filename, sizeof(filename),
|
498
|
244 s->path, s->img_number)<0 && s->img_number > 1)
|
497
|
245 return AVERROR_IO;
|
635
|
246 for(i=0; i<3; i++){
|
|
247 if (url_fopen(f[i], filename, URL_RDONLY) < 0)
|
|
248 return AVERROR_IO;
|
|
249 size[i]= url_filesize(url_fileno(f[i]));
|
|
250
|
|
251 if(codec->codec_id != CODEC_ID_RAWVIDEO)
|
|
252 break;
|
|
253 filename[ strlen(filename) - 1 ]= 'U' + i;
|
|
254 }
|
|
255
|
|
256 if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
|
|
257 infer_size(&codec->width, &codec->height, size[0]);
|
497
|
258 } else {
|
635
|
259 f[0] = &s1->pb;
|
|
260 if (url_feof(f[0]))
|
497
|
261 return AVERROR_IO;
|
635
|
262 size[0]= 4096;
|
497
|
263 }
|
|
264
|
635
|
265 av_new_packet(pkt, size[0] + size[1] + size[2]);
|
497
|
266 pkt->stream_index = 0;
|
|
267 pkt->flags |= PKT_FLAG_KEY;
|
|
268
|
635
|
269 pkt->size= 0;
|
|
270 for(i=0; i<3; i++){
|
|
271 if(size[i]){
|
|
272 ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
|
|
273 if (!s->is_pipe)
|
|
274 url_fclose(f[i]);
|
|
275 if(ret[i]>0)
|
|
276 pkt->size += ret[i];
|
|
277 }
|
497
|
278 }
|
|
279
|
635
|
280 if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
|
497
|
281 av_free_packet(pkt);
|
|
282 return AVERROR_IO; /* signal EOF */
|
|
283 } else {
|
|
284 s->img_count++;
|
|
285 s->img_number++;
|
|
286 return 0;
|
|
287 }
|
|
288 }
|
|
289
|
|
290 static int img_read_close(AVFormatContext *s1)
|
|
291 {
|
|
292 return 0;
|
|
293 }
|
|
294
|
|
295 /******************************************************/
|
|
296 /* image output */
|
|
297
|
|
298 static int img_write_header(AVFormatContext *s)
|
|
299 {
|
|
300 VideoData *img = s->priv_data;
|
|
301
|
|
302 img->img_number = 1;
|
639
|
303 pstrcpy(img->path, sizeof(img->path), s->filename);
|
497
|
304
|
|
305 /* find format */
|
|
306 if (s->oformat->flags & AVFMT_NOFILE)
|
|
307 img->is_pipe = 0;
|
|
308 else
|
|
309 img->is_pipe = 1;
|
|
310
|
|
311 return 0;
|
|
312 }
|
|
313
|
|
314 static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
315 {
|
|
316 VideoData *img = s->priv_data;
|
635
|
317 ByteIOContext pb1[3], *pb[3]= {&pb1[0], &pb1[1], &pb1[2]};
|
497
|
318 char filename[1024];
|
635
|
319 AVCodecContext *codec= &s->streams[ pkt->stream_index ]->codec;
|
|
320 int i;
|
497
|
321
|
|
322 if (!img->is_pipe) {
|
|
323 if (get_frame_filename(filename, sizeof(filename),
|
498
|
324 img->path, img->img_number) < 0 && img->img_number>1)
|
497
|
325 return AVERROR_IO;
|
635
|
326 for(i=0; i<3; i++){
|
|
327 if (url_fopen(pb[i], filename, URL_WRONLY) < 0)
|
|
328 return AVERROR_IO;
|
|
329
|
|
330 if(codec->codec_id != CODEC_ID_RAWVIDEO)
|
|
331 break;
|
|
332 filename[ strlen(filename) - 1 ]= 'U' + i;
|
|
333 }
|
497
|
334 } else {
|
635
|
335 pb[0] = &s->pb;
|
497
|
336 }
|
635
|
337
|
|
338 if(codec->codec_id == CODEC_ID_RAWVIDEO){
|
|
339 int size = (codec->width * codec->height)>>2;
|
|
340 put_buffer(pb[0], pkt->data , 4*size);
|
|
341 put_buffer(pb[1], pkt->data + 4*size, size);
|
|
342 put_buffer(pb[2], pkt->data + 5*size, size);
|
|
343 put_flush_packet(pb[1]);
|
|
344 put_flush_packet(pb[2]);
|
|
345 url_fclose(pb[1]);
|
|
346 url_fclose(pb[2]);
|
|
347 }else{
|
|
348 put_buffer(pb[0], pkt->data, pkt->size);
|
|
349 }
|
|
350 put_flush_packet(pb[0]);
|
497
|
351 if (!img->is_pipe) {
|
635
|
352 url_fclose(pb[0]);
|
497
|
353 }
|
|
354
|
|
355 img->img_number++;
|
|
356 return 0;
|
|
357 }
|
|
358
|
|
359 static int img_write_trailer(AVFormatContext *s)
|
|
360 {
|
|
361 return 0;
|
|
362 }
|
|
363
|
|
364 /* input */
|
|
365
|
|
366 static AVInputFormat image2_iformat = {
|
|
367 "image2",
|
|
368 "image2 sequence",
|
|
369 sizeof(VideoData),
|
|
370 image_probe,
|
|
371 img_read_header,
|
|
372 img_read_packet,
|
|
373 img_read_close,
|
|
374 NULL,
|
|
375 NULL,
|
498
|
376 AVFMT_NOFILE,
|
497
|
377 };
|
|
378
|
|
379 static AVInputFormat image2pipe_iformat = {
|
|
380 "image2pipe",
|
|
381 "piped image2 sequence",
|
|
382 sizeof(VideoData),
|
|
383 NULL, /* no probe */
|
|
384 img_read_header,
|
|
385 img_read_packet,
|
|
386 img_read_close,
|
|
387 NULL,
|
|
388 };
|
|
389
|
|
390
|
|
391 /* output */
|
|
392
|
|
393 static AVOutputFormat image2_oformat = {
|
|
394 "image2",
|
|
395 "image2 sequence",
|
|
396 "",
|
|
397 "",
|
|
398 sizeof(VideoData),
|
|
399 CODEC_ID_NONE,
|
|
400 CODEC_ID_MJPEG,
|
|
401 img_write_header,
|
|
402 img_write_packet,
|
|
403 img_write_trailer,
|
498
|
404 AVFMT_NOFILE,
|
497
|
405 };
|
|
406
|
|
407 static AVOutputFormat image2pipe_oformat = {
|
|
408 "image2pipe",
|
|
409 "piped image2 sequence",
|
|
410 "",
|
|
411 "",
|
|
412 sizeof(VideoData),
|
|
413 CODEC_ID_NONE,
|
|
414 CODEC_ID_MJPEG,
|
|
415 img_write_header,
|
|
416 img_write_packet,
|
|
417 img_write_trailer,
|
|
418 };
|
|
419
|
|
420 int img2_init(void)
|
|
421 {
|
|
422 av_register_input_format(&image2_iformat);
|
|
423 av_register_output_format(&image2_oformat);
|
|
424
|
|
425 av_register_input_format(&image2pipe_iformat);
|
|
426 av_register_output_format(&image2pipe_oformat);
|
|
427
|
|
428 return 0;
|
|
429 }
|