annotate img.c @ 547:b832a27355ec libavformat

initial dts fix
author michael
date Sun, 03 Oct 2004 12:58:14 +0000
parents 0fdc96c2f2fe
children c5ff083848b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * Image format
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
18 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
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
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24 typedef struct {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25 int width;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
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
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
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
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 int img_size;
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
32 AVImageFormat *img_fmt;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
33 int pix_fmt;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34 int is_pipe;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 char path[1024];
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
36 /* temporary usage */
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
37 void *ptr;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 } VideoData;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39
189
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
40
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
41 /* return -1 if no image found */
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
42 static int find_image_range(int *pfirst_index, int *plast_index,
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
43 const char *path)
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
44 {
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
45 char buf[1024];
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
46 int range, last_index, range1, first_index;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
47
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
48 /* find the first image */
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
49 for(first_index = 0; first_index < 5; first_index++) {
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
50 if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0)
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
51 goto fail;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
52 if (url_exist(buf))
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
53 break;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
54 }
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
55 if (first_index == 5)
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
56 goto fail;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
57
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
58 /* find the last image */
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
59 last_index = first_index;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
60 for(;;) {
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
61 range = 0;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
62 for(;;) {
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
63 if (!range)
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
64 range1 = 1;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
65 else
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
66 range1 = 2 * range;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
67 if (get_frame_filename(buf, sizeof(buf), path,
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
68 last_index + range1) < 0)
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
69 goto fail;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
70 if (!url_exist(buf))
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
71 break;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
72 range = range1;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
73 /* just in case... */
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
74 if (range >= (1 << 30))
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
75 goto fail;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
76 }
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
77 /* we are sure than image last_index + range exists */
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
78 if (!range)
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
79 break;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
80 last_index += range;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
81 }
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
82 *pfirst_index = first_index;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
83 *plast_index = last_index;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
84 return 0;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
85 fail:
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
86 return -1;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
87 }
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
88
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
89
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
90 static int image_probe(AVProbeData *p)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 {
21
3d2d2f8725f9 jpeg detection fix - fixed imagepipe output
bellard
parents: 20
diff changeset
92 if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
93 return AVPROBE_SCORE_MAX;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
94 else
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
95 return 0;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
98 static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 {
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
100 VideoData *s = opaque;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
101
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
102 s->width = info->width;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
103 s->height = info->height;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
104 s->pix_fmt = info->pix_fmt;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
105 /* stop image reading but no error */
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
106 return 1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
109 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 {
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
111 VideoData *s = s1->priv_data;
189
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
112 int ret, first_index, last_index;
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
113 char buf[1024];
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
114 ByteIOContext pb1, *f = &pb1;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
115 AVStream *st;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
116
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
117 st = av_new_stream(s1, 0);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
118 if (!st) {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
119 av_free(s);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
120 return -ENOMEM;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
121 }
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
122
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
123 if (ap && ap->image_format)
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
124 s->img_fmt = ap->image_format;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
125
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
126 strcpy(s->path, s1->filename);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
127 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
128 s->img_count = 0;
66a05c4f8350 suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents: 189
diff changeset
129
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
130 /* find format */
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
131 if (s1->iformat->flags & AVFMT_NOFILE)
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
132 s->is_pipe = 0;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
133 else
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
134 s->is_pipe = 1;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
135
189
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
136 if (!ap || !ap->frame_rate) {
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
137 st->codec.frame_rate = 25;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
138 st->codec.frame_rate_base = 1;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
139 } else {
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
140 st->codec.frame_rate = ap->frame_rate;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
141 st->codec.frame_rate_base = ap->frame_rate_base;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
142 }
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
143
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
144 if (!s->is_pipe) {
189
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
145 if (find_image_range(&first_index, &last_index, s->path) < 0)
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
146 goto fail;
199
66a05c4f8350 suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents: 189
diff changeset
147 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
148 s->img_last = last_index;
189
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
149 s->img_number = first_index;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
150 /* compute duration */
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
151 st->start_time = 0;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
152 st->duration = ((int64_t)AV_TIME_BASE *
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
153 (last_index - first_index + 1) *
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
154 st->codec.frame_rate_base) / st->codec.frame_rate;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
155 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
156 goto fail;
e3cfb9131dfa update duration and start_time
bellard
parents: 88
diff changeset
157 if (url_fopen(f, buf, URL_RDONLY) < 0)
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
158 goto fail;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
159 } else {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
160 f = &s1->pb;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
163 ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
164 if (ret < 0)
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
165 goto fail1;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
166
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
167 if (!s->is_pipe) {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
168 url_fclose(f);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
169 } else {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
170 url_fseek(f, 0, SEEK_SET);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
171 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
173 st->codec.codec_type = CODEC_TYPE_VIDEO;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
174 st->codec.codec_id = CODEC_ID_RAWVIDEO;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
175 st->codec.width = s->width;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
176 st->codec.height = s->height;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
177 st->codec.pix_fmt = s->pix_fmt;
459
ce1a3e82328b segfault fix
michael
parents: 439
diff changeset
178 s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15));
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
179
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 return 0;
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
181 fail1:
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
182 if (!s->is_pipe)
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
183 url_fclose(f);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
184 fail:
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
185 av_free(s);
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 468
diff changeset
186 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
189 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 {
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
191 VideoData *s = opaque;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
193 if (info->width != s->width ||
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
194 info->height != s->height)
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
195 return -1;
459
ce1a3e82328b segfault fix
michael
parents: 439
diff changeset
196 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
202 VideoData *s = s1->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 char filename[1024];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 int ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 ByteIOContext f1, *f;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 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
208 /* loop over input */
66a05c4f8350 suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents: 189
diff changeset
209 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
210 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
211 }
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
212 if (get_frame_filename(filename, sizeof(filename),
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
213 s->path, s->img_number) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 468
diff changeset
214 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 f = &f1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 if (url_fopen(f, filename, URL_RDONLY) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 468
diff changeset
217 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 f = &s1->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 if (url_feof(f))
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 468
diff changeset
221 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224 av_new_packet(pkt, s->img_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 pkt->stream_index = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
227 s->ptr = pkt->data;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
228 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 if (!s->is_pipe) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230 url_fclose(f);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
232
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 if (ret < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 av_free_packet(pkt);
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 468
diff changeset
235 return AVERROR_IO; /* signal EOF */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 } else {
199
66a05c4f8350 suppressed frame number modulus hack - added loop_input hack which I find easier to understand
bellard
parents: 189
diff changeset
237 /* 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
238 the generic code too */
462
b69898ffc92a move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents: 459
diff changeset
239 pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec.frame_rate_base, s1->streams[0]->time_base.den, s1->streams[0]->codec.frame_rate) / 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
240 s->img_count++;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 s->img_number++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 static int img_read_close(AVFormatContext *s1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 /******************************************************/
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 /* image output */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
254 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 {
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
256 VideoData *img = s->priv_data;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
257 AVStream *st;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
258 AVImageFormat *img_fmt;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
259 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
261 /* find output image format */
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
262 if (ap && ap->image_format) {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
263 img_fmt = ap->image_format;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
264 } else {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
265 img_fmt = guess_image_format(s->filename);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
266 }
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
267 if (!img_fmt)
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
268 return -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
270 if (s->nb_streams != 1)
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
271 return -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
273 st = s->streams[0];
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
274 /* we select the first matching format */
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
275 for(i=0;i<PIX_FMT_NB;i++) {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
276 if (img_fmt->supported_pixel_formats & (1 << i))
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
277 break;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 }
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
279 if (i >= PIX_FMT_NB)
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
280 return -1;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
281 img->img_fmt = img_fmt;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
282 img->pix_fmt = i;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
283 st->codec.pix_fmt = img->pix_fmt;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 static int img_write_header(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 VideoData *img = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291 img->img_number = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 strcpy(img->path, s->filename);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294 /* find format */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
295 if (s->oformat->flags & AVFMT_NOFILE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
296 img->is_pipe = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 img->is_pipe = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
303 static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 VideoData *img = s->priv_data;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
306 AVStream *st = s->streams[pkt->stream_index];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307 ByteIOContext pb1, *pb;
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
308 AVPicture *picture;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
309 int width, height, ret;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
310 char filename[1024];
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
311 AVImageInfo info;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 width = st->codec.width;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 height = st->codec.height;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
316 picture = (AVPicture *)pkt->data;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
317
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
318 if (!img->is_pipe) {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
319 if (get_frame_filename(filename, sizeof(filename),
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
320 img->path, img->img_number) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 468
diff changeset
321 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
322 pb = &pb1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
323 if (url_fopen(pb, filename, URL_WRONLY) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 468
diff changeset
324 return AVERROR_IO;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
325 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
326 pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
327 }
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
328 info.width = width;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
329 info.height = height;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
330 info.pix_fmt = st->codec.pix_fmt;
280
75e061728d92 recommit of
michael
parents: 241
diff changeset
331 info.interleaved = 0; /* FIXME: there should be a way to set it right */
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
332 info.pict = *picture;
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
333 ret = av_write_image(pb, img->img_fmt, &info);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
334 if (!img->is_pipe) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
335 url_fclose(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
336 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
337
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
338 img->img_number++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
339 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
340 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
341
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
342 static int img_write_trailer(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
343 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
344 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
345 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
346
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
347 /* input */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
348
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
349 static AVInputFormat image_iformat = {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
350 "image",
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
351 "image sequence",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
352 sizeof(VideoData),
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
353 image_probe,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
354 img_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
355 img_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
356 img_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
357 NULL,
439
michael
parents: 280
diff changeset
358 NULL,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
359 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
360 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
361
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
362 static AVInputFormat imagepipe_iformat = {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
363 "imagepipe",
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
364 "piped image sequence",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
365 sizeof(VideoData),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
366 NULL, /* no probe */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
367 img_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
368 img_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
369 img_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
370 NULL,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
371 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
372
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
373
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
374 /* output */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
375
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
376 static AVOutputFormat image_oformat = {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
377 "image",
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
378 "image sequence",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
379 "",
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
380 "",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
381 sizeof(VideoData),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
382 CODEC_ID_NONE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
383 CODEC_ID_RAWVIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
384 img_write_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
385 img_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
386 img_write_trailer,
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
387 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
388 img_set_parameters,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
389 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
390
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
391 static AVOutputFormat imagepipe_oformat = {
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
392 "imagepipe",
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
393 "piped image sequence",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
394 "",
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
395 "",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
396 sizeof(VideoData),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
397 CODEC_ID_NONE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
398 CODEC_ID_RAWVIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
399 img_write_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
400 img_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
401 img_write_trailer,
21
3d2d2f8725f9 jpeg detection fix - fixed imagepipe output
bellard
parents: 20
diff changeset
402 AVFMT_RAWPICTURE,
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
403 img_set_parameters,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
404 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
405
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
406 int img_init(void)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
407 {
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
408 av_register_input_format(&image_iformat);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
409 av_register_output_format(&image_oformat);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
410
20
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
411 av_register_input_format(&imagepipe_iformat);
3d52de18ecc3 added still image support
bellard
parents: 12
diff changeset
412 av_register_output_format(&imagepipe_oformat);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
413
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
414 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
415 }