0
|
1 /*
|
|
2 * JPEG based formats
|
|
3 * Copyright (c) 2000, 2001 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
|
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 */
|
|
19 #include "avformat.h"
|
|
20
|
|
21 /* Multipart JPEG */
|
|
22
|
|
23 #define BOUNDARY_TAG "ffserver"
|
|
24
|
|
25 static int mpjpeg_write_header(AVFormatContext *s)
|
|
26 {
|
|
27 UINT8 buf1[256];
|
|
28
|
|
29 snprintf(buf1, sizeof(buf1), "--%s\n", BOUNDARY_TAG);
|
|
30 put_buffer(&s->pb, buf1, strlen(buf1));
|
|
31 put_flush_packet(&s->pb);
|
|
32 return 0;
|
|
33 }
|
|
34
|
|
35 static int mpjpeg_write_packet(AVFormatContext *s, int stream_index,
|
|
36 UINT8 *buf, int size, int force_pts)
|
|
37 {
|
|
38 UINT8 buf1[256];
|
|
39
|
|
40 snprintf(buf1, sizeof(buf1), "Content-type: image/jpeg\n\n");
|
|
41 put_buffer(&s->pb, buf1, strlen(buf1));
|
|
42 put_buffer(&s->pb, buf, size);
|
|
43
|
|
44 snprintf(buf1, sizeof(buf1), "\n--%s\n", BOUNDARY_TAG);
|
|
45 put_buffer(&s->pb, buf1, strlen(buf1));
|
|
46 put_flush_packet(&s->pb);
|
|
47 return 0;
|
|
48 }
|
|
49
|
|
50 static int mpjpeg_write_trailer(AVFormatContext *s)
|
|
51 {
|
|
52 return 0;
|
|
53 }
|
|
54
|
|
55 static AVOutputFormat mpjpeg_format = {
|
|
56 "mpjpeg",
|
|
57 "Mime multipart JPEG format",
|
|
58 "multipart/x-mixed-replace;boundary=" BOUNDARY_TAG,
|
|
59 "mjpg",
|
|
60 0,
|
|
61 CODEC_ID_NONE,
|
|
62 CODEC_ID_MJPEG,
|
|
63 mpjpeg_write_header,
|
|
64 mpjpeg_write_packet,
|
|
65 mpjpeg_write_trailer,
|
|
66 };
|
|
67
|
|
68
|
|
69 /*************************************/
|
|
70 /* single frame JPEG */
|
|
71
|
|
72 static int single_jpeg_write_header(AVFormatContext *s)
|
|
73 {
|
|
74 return 0;
|
|
75 }
|
|
76
|
|
77 static int single_jpeg_write_packet(AVFormatContext *s, int stream_index,
|
|
78 UINT8 *buf, int size, int force_pts)
|
|
79 {
|
|
80 put_buffer(&s->pb, buf, size);
|
|
81 put_flush_packet(&s->pb);
|
|
82 return 1; /* no more data can be sent */
|
|
83 }
|
|
84
|
|
85 static int single_jpeg_write_trailer(AVFormatContext *s)
|
|
86 {
|
|
87 return 0;
|
|
88 }
|
|
89
|
|
90 static AVOutputFormat single_jpeg_format = {
|
|
91 "singlejpeg",
|
|
92 "single JPEG image",
|
|
93 "image/jpeg",
|
|
94 NULL, /* note: no extension to favorize jpeg multiple images match */
|
|
95 0,
|
|
96 CODEC_ID_NONE,
|
|
97 CODEC_ID_MJPEG,
|
|
98 single_jpeg_write_header,
|
|
99 single_jpeg_write_packet,
|
|
100 single_jpeg_write_trailer,
|
|
101 };
|
|
102
|
|
103 /*************************************/
|
|
104 /* multiple jpeg images */
|
|
105
|
|
106 typedef struct JpegContext {
|
|
107 char path[1024];
|
|
108 int img_number;
|
|
109 } JpegContext;
|
|
110
|
|
111 static int jpeg_write_header(AVFormatContext *s1)
|
|
112 {
|
|
113 JpegContext *s;
|
|
114
|
|
115 s = av_mallocz(sizeof(JpegContext));
|
|
116 if (!s)
|
|
117 return -1;
|
|
118 s1->priv_data = s;
|
|
119 pstrcpy(s->path, sizeof(s->path), s1->filename);
|
|
120 s->img_number = 1;
|
|
121 return 0;
|
|
122 }
|
|
123
|
|
124 static int jpeg_write_packet(AVFormatContext *s1, int stream_index,
|
|
125 UINT8 *buf, int size, int force_pts)
|
|
126 {
|
|
127 JpegContext *s = s1->priv_data;
|
|
128 char filename[1024];
|
|
129 ByteIOContext f1, *pb = &f1;
|
|
130
|
|
131 if (get_frame_filename(filename, sizeof(filename),
|
|
132 s->path, s->img_number) < 0)
|
|
133 return -EIO;
|
|
134 if (url_fopen(pb, filename, URL_WRONLY) < 0)
|
|
135 return -EIO;
|
|
136
|
|
137 put_buffer(pb, buf, size);
|
|
138 put_flush_packet(pb);
|
|
139
|
|
140 url_fclose(pb);
|
|
141 s->img_number++;
|
|
142
|
|
143 return 0;
|
|
144 }
|
|
145
|
|
146 static int jpeg_write_trailer(AVFormatContext *s1)
|
|
147 {
|
|
148 return 0;
|
|
149 }
|
|
150
|
|
151 /***/
|
|
152
|
|
153 static int jpeg_read_header(AVFormatContext *s1, AVFormatParameters *ap)
|
|
154 {
|
|
155 JpegContext *s;
|
|
156 int i;
|
|
157 char buf[1024];
|
|
158 ByteIOContext pb1, *f = &pb1;
|
|
159 AVStream *st;
|
|
160
|
|
161 s = av_mallocz(sizeof(JpegContext));
|
|
162 if (!s)
|
|
163 return -1;
|
|
164 s1->priv_data = s;
|
|
165 pstrcpy(s->path, sizeof(s->path), s1->filename);
|
|
166
|
|
167 s1->nb_streams = 1;
|
|
168 st = av_mallocz(sizeof(AVStream));
|
|
169 if (!st) {
|
|
170 av_free(s);
|
|
171 return -ENOMEM;
|
|
172 }
|
5
|
173 avcodec_get_context_defaults(&st->codec);
|
|
174
|
0
|
175 s1->streams[0] = st;
|
|
176 s->img_number = 0;
|
|
177
|
|
178 /* try to find the first image */
|
|
179 for(i=0;i<5;i++) {
|
|
180 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
|
|
181 goto fail;
|
|
182 if (url_fopen(f, buf, URL_RDONLY) >= 0)
|
|
183 break;
|
|
184 s->img_number++;
|
|
185 }
|
|
186 if (i == 5)
|
|
187 goto fail;
|
|
188 url_fclose(f);
|
|
189 st->codec.codec_type = CODEC_TYPE_VIDEO;
|
|
190 st->codec.codec_id = CODEC_ID_MJPEG;
|
|
191
|
|
192 if (!ap || !ap->frame_rate)
|
|
193 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
|
|
194 else
|
|
195 st->codec.frame_rate = ap->frame_rate;
|
|
196 return 0;
|
|
197 fail:
|
|
198 av_free(s);
|
|
199 return -EIO;
|
|
200 }
|
|
201
|
|
202 static int jpeg_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
|
203 {
|
|
204 JpegContext *s = s1->priv_data;
|
|
205 char filename[1024];
|
|
206 int size;
|
|
207 ByteIOContext f1, *f = &f1;
|
|
208
|
|
209 if (get_frame_filename(filename, sizeof(filename),
|
|
210 s->path, s->img_number) < 0)
|
|
211 return -EIO;
|
|
212
|
|
213 f = &f1;
|
|
214 if (url_fopen(f, filename, URL_RDONLY) < 0)
|
|
215 return -EIO;
|
|
216
|
|
217 size = url_seek(url_fileno(f), 0, SEEK_END);
|
|
218 url_seek(url_fileno(f), 0, SEEK_SET);
|
|
219
|
|
220 av_new_packet(pkt, size);
|
|
221 pkt->stream_index = 0;
|
|
222 get_buffer(f, pkt->data, size);
|
|
223
|
|
224 url_fclose(f);
|
|
225 s->img_number++;
|
|
226 return 0;
|
|
227 }
|
|
228
|
|
229 static int jpeg_read_close(AVFormatContext *s1)
|
|
230 {
|
|
231 return 0;
|
|
232 }
|
|
233
|
|
234 static AVInputFormat jpeg_iformat = {
|
|
235 "jpeg",
|
|
236 "JPEG image",
|
|
237 sizeof(JpegContext),
|
|
238 NULL,
|
|
239 jpeg_read_header,
|
|
240 jpeg_read_packet,
|
|
241 jpeg_read_close,
|
|
242 NULL,
|
|
243 .flags = AVFMT_NOFILE | AVFMT_NEEDNUMBER,
|
|
244 .extensions = "jpg,jpeg",
|
|
245 };
|
|
246
|
|
247 static AVOutputFormat jpeg_oformat = {
|
|
248 "jpeg",
|
|
249 "JPEG image",
|
|
250 "image/jpeg",
|
|
251 "jpg,jpeg",
|
|
252 sizeof(JpegContext),
|
|
253 CODEC_ID_NONE,
|
|
254 CODEC_ID_MJPEG,
|
|
255 jpeg_write_header,
|
|
256 jpeg_write_packet,
|
|
257 jpeg_write_trailer,
|
|
258 .flags = AVFMT_NOFILE | AVFMT_NEEDNUMBER,
|
|
259 };
|
|
260
|
|
261 int jpeg_init(void)
|
|
262 {
|
|
263 av_register_output_format(&mpjpeg_format);
|
|
264 av_register_output_format(&single_jpeg_format);
|
|
265 av_register_input_format(&jpeg_iformat);
|
|
266 av_register_output_format(&jpeg_oformat);
|
|
267 return 0;
|
|
268 }
|