Mercurial > libavformat.hg
annotate img2.c @ 783:2e8b5a7d7e02 libavformat
DVD subtitle parsing - show mpeg component IDs by default
author | bellard |
---|---|
date | Fri, 03 Jun 2005 14:01:49 +0000 |
parents | cdb845a57ae4 |
children | feca73904e67 |
rev | line source |
---|---|
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 |
743 | 189 if (!ap || !ap->time_base.num) { |
190 av_set_pts_info(st, 60, 1, 25); | |
497 | 191 } else { |
743 | 192 av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den); |
497 | 193 } |
194 | |
635 | 195 if(ap && ap->width && ap->height){ |
196 st->codec.width = ap->width; | |
197 st->codec.height= ap->height; | |
198 } | |
199 | |
497 | 200 if (!s->is_pipe) { |
201 if (find_image_range(&first_index, &last_index, s->path) < 0) | |
622 | 202 return AVERROR_IO; |
497 | 203 s->img_first = first_index; |
204 s->img_last = last_index; | |
205 s->img_number = first_index; | |
206 /* compute duration */ | |
207 st->start_time = 0; | |
743 | 208 st->duration = last_index - first_index + 1; |
497 | 209 } |
210 | |
583 | 211 if(ap->video_codec_id){ |
212 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
213 st->codec.codec_id = ap->video_codec_id; | |
214 }else if(ap->audio_codec_id){ | |
215 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
216 st->codec.codec_id = ap->audio_codec_id; | |
217 }else{ | |
218 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
219 st->codec.codec_id = av_str2id(img_tags, s->path); | |
220 } | |
737 | 221 if(st->codec.codec_type == CODEC_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE) |
731 | 222 st->codec.pix_fmt = ap->pix_fmt; |
497 | 223 |
224 return 0; | |
225 } | |
226 | |
227 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt) | |
228 { | |
229 VideoData *s = s1->priv_data; | |
230 char filename[1024]; | |
635 | 231 int i; |
232 int size[3]={0}, ret[3]={0}; | |
233 ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]}; | |
234 AVCodecContext *codec= &s1->streams[0]->codec; | |
497 | 235 |
236 if (!s->is_pipe) { | |
237 /* loop over input */ | |
590 | 238 if (loop_input && s->img_number > s->img_last) { |
497 | 239 s->img_number = s->img_first; |
590 | 240 } |
497 | 241 if (get_frame_filename(filename, sizeof(filename), |
498 | 242 s->path, s->img_number)<0 && s->img_number > 1) |
497 | 243 return AVERROR_IO; |
635 | 244 for(i=0; i<3; i++){ |
245 if (url_fopen(f[i], filename, URL_RDONLY) < 0) | |
246 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
|
247 size[i]= url_fsize(f[i]); |
635 | 248 |
249 if(codec->codec_id != CODEC_ID_RAWVIDEO) | |
250 break; | |
251 filename[ strlen(filename) - 1 ]= 'U' + i; | |
252 } | |
253 | |
254 if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width) | |
255 infer_size(&codec->width, &codec->height, size[0]); | |
497 | 256 } else { |
635 | 257 f[0] = &s1->pb; |
258 if (url_feof(f[0])) | |
497 | 259 return AVERROR_IO; |
635 | 260 size[0]= 4096; |
497 | 261 } |
262 | |
635 | 263 av_new_packet(pkt, size[0] + size[1] + size[2]); |
497 | 264 pkt->stream_index = 0; |
265 pkt->flags |= PKT_FLAG_KEY; | |
266 | |
635 | 267 pkt->size= 0; |
268 for(i=0; i<3; i++){ | |
269 if(size[i]){ | |
270 ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]); | |
271 if (!s->is_pipe) | |
272 url_fclose(f[i]); | |
273 if(ret[i]>0) | |
274 pkt->size += ret[i]; | |
275 } | |
497 | 276 } |
277 | |
635 | 278 if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) { |
497 | 279 av_free_packet(pkt); |
280 return AVERROR_IO; /* signal EOF */ | |
281 } else { | |
282 s->img_count++; | |
283 s->img_number++; | |
284 return 0; | |
285 } | |
286 } | |
287 | |
288 static int img_read_close(AVFormatContext *s1) | |
289 { | |
290 return 0; | |
291 } | |
292 | |
293 /******************************************************/ | |
294 /* image output */ | |
295 | |
296 static int img_write_header(AVFormatContext *s) | |
297 { | |
298 VideoData *img = s->priv_data; | |
299 | |
300 img->img_number = 1; | |
639 | 301 pstrcpy(img->path, sizeof(img->path), s->filename); |
497 | 302 |
303 /* find format */ | |
304 if (s->oformat->flags & AVFMT_NOFILE) | |
305 img->is_pipe = 0; | |
306 else | |
307 img->is_pipe = 1; | |
308 | |
309 return 0; | |
310 } | |
311 | |
312 static int img_write_packet(AVFormatContext *s, AVPacket *pkt) | |
313 { | |
314 VideoData *img = s->priv_data; | |
635 | 315 ByteIOContext pb1[3], *pb[3]= {&pb1[0], &pb1[1], &pb1[2]}; |
497 | 316 char filename[1024]; |
635 | 317 AVCodecContext *codec= &s->streams[ pkt->stream_index ]->codec; |
318 int i; | |
497 | 319 |
320 if (!img->is_pipe) { | |
321 if (get_frame_filename(filename, sizeof(filename), | |
498 | 322 img->path, img->img_number) < 0 && img->img_number>1) |
497 | 323 return AVERROR_IO; |
635 | 324 for(i=0; i<3; i++){ |
325 if (url_fopen(pb[i], filename, URL_WRONLY) < 0) | |
326 return AVERROR_IO; | |
327 | |
328 if(codec->codec_id != CODEC_ID_RAWVIDEO) | |
329 break; | |
330 filename[ strlen(filename) - 1 ]= 'U' + i; | |
331 } | |
497 | 332 } else { |
635 | 333 pb[0] = &s->pb; |
497 | 334 } |
635 | 335 |
336 if(codec->codec_id == CODEC_ID_RAWVIDEO){ | |
731 | 337 int ysize = codec->width * codec->height; |
338 put_buffer(pb[0], pkt->data , ysize); | |
339 put_buffer(pb[1], pkt->data + ysize, (pkt->size - ysize)/2); | |
340 put_buffer(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2); | |
635 | 341 put_flush_packet(pb[1]); |
342 put_flush_packet(pb[2]); | |
343 url_fclose(pb[1]); | |
344 url_fclose(pb[2]); | |
345 }else{ | |
346 put_buffer(pb[0], pkt->data, pkt->size); | |
347 } | |
348 put_flush_packet(pb[0]); | |
497 | 349 if (!img->is_pipe) { |
635 | 350 url_fclose(pb[0]); |
497 | 351 } |
352 | |
353 img->img_number++; | |
354 return 0; | |
355 } | |
356 | |
357 static int img_write_trailer(AVFormatContext *s) | |
358 { | |
359 return 0; | |
360 } | |
361 | |
362 /* input */ | |
363 | |
364 static AVInputFormat image2_iformat = { | |
365 "image2", | |
366 "image2 sequence", | |
367 sizeof(VideoData), | |
368 image_probe, | |
369 img_read_header, | |
370 img_read_packet, | |
371 img_read_close, | |
372 NULL, | |
373 NULL, | |
498 | 374 AVFMT_NOFILE, |
497 | 375 }; |
376 | |
377 static AVInputFormat image2pipe_iformat = { | |
378 "image2pipe", | |
379 "piped image2 sequence", | |
380 sizeof(VideoData), | |
381 NULL, /* no probe */ | |
382 img_read_header, | |
383 img_read_packet, | |
384 img_read_close, | |
385 NULL, | |
386 }; | |
387 | |
388 | |
389 /* output */ | |
390 | |
391 static AVOutputFormat image2_oformat = { | |
392 "image2", | |
393 "image2 sequence", | |
394 "", | |
395 "", | |
396 sizeof(VideoData), | |
397 CODEC_ID_NONE, | |
398 CODEC_ID_MJPEG, | |
399 img_write_header, | |
400 img_write_packet, | |
401 img_write_trailer, | |
498 | 402 AVFMT_NOFILE, |
497 | 403 }; |
404 | |
405 static AVOutputFormat image2pipe_oformat = { | |
406 "image2pipe", | |
407 "piped image2 sequence", | |
408 "", | |
409 "", | |
410 sizeof(VideoData), | |
411 CODEC_ID_NONE, | |
412 CODEC_ID_MJPEG, | |
413 img_write_header, | |
414 img_write_packet, | |
415 img_write_trailer, | |
416 }; | |
417 | |
418 int img2_init(void) | |
419 { | |
420 av_register_input_format(&image2_iformat); | |
421 av_register_output_format(&image2_oformat); | |
422 | |
423 av_register_input_format(&image2pipe_iformat); | |
424 av_register_output_format(&image2pipe_oformat); | |
425 | |
426 return 0; | |
427 } |