3 * Copyright (c) 2000, 2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define BOUNDARY_TAG "ffserver"
25 static int mpjpeg_write_header(AVFormatContext *s)
29 snprintf(buf1, sizeof(buf1), "--%s\n", BOUNDARY_TAG);
30 put_buffer(&s->pb, buf1, strlen(buf1));
31 put_flush_packet(&s->pb);
35 static int mpjpeg_write_packet(AVFormatContext *s,
36 int stream_index, UINT8 *buf, int size)
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);
44 snprintf(buf1, sizeof(buf1), "\n--%s\n", BOUNDARY_TAG);
45 put_buffer(&s->pb, buf1, strlen(buf1));
46 put_flush_packet(&s->pb);
50 static int mpjpeg_write_trailer(AVFormatContext *s)
55 AVFormat mpjpeg_format = {
57 "Mime multipart JPEG format",
58 "multipart/x-mixed-replace;boundary=" BOUNDARY_TAG,
68 /*************************************/
69 /* single frame JPEG */
71 static int single_jpeg_write_header(AVFormatContext *s)
76 static int single_jpeg_write_packet(AVFormatContext *s, int stream_index,
79 put_buffer(&s->pb, buf, size);
80 put_flush_packet(&s->pb);
81 return 1; /* no more data can be sent */
84 static int single_jpeg_write_trailer(AVFormatContext *s)
89 AVFormat single_jpeg_format = {
96 single_jpeg_write_header,
97 single_jpeg_write_packet,
98 single_jpeg_write_trailer,
101 /*************************************/
102 /* multiple jpeg images */
104 typedef struct JpegContext {
109 static int jpeg_write_header(AVFormatContext *s1)
113 s = av_mallocz(sizeof(JpegContext));
117 nstrcpy(s->path, sizeof(s->path), s1->filename);
122 static int jpeg_write_packet(AVFormatContext *s1, int stream_index,
123 UINT8 *buf, int size)
125 JpegContext *s = s1->priv_data;
127 ByteIOContext f1, *pb = &f1;
129 if (get_frame_filename(filename, sizeof(filename),
130 s->path, s->img_number) < 0)
132 if (url_fopen(pb, filename, URL_WRONLY) < 0)
135 put_buffer(pb, buf, size);
136 put_flush_packet(pb);
144 static int jpeg_write_trailer(AVFormatContext *s1)
146 JpegContext *s = s1->priv_data;
153 static int jpeg_read_header(AVFormatContext *s1, AVFormatParameters *ap)
158 ByteIOContext pb1, *f = &pb1;
161 s = av_mallocz(sizeof(JpegContext));
165 nstrcpy(s->path, sizeof(s->path), s1->filename);
168 st = av_mallocz(sizeof(AVStream));
176 /* try to find the first image */
178 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
180 if (url_fopen(f, buf, URL_RDONLY) >= 0)
187 st->codec.codec_type = CODEC_TYPE_VIDEO;
188 st->codec.codec_id = CODEC_ID_MJPEG;
190 if (!ap || !ap->frame_rate)
191 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
193 st->codec.frame_rate = ap->frame_rate;
200 static int jpeg_read_packet(AVFormatContext *s1, AVPacket *pkt)
202 JpegContext *s = s1->priv_data;
205 ByteIOContext f1, *f = &f1;
207 if (get_frame_filename(filename, sizeof(filename),
208 s->path, s->img_number) < 0)
212 if (url_fopen(f, filename, URL_RDONLY) < 0)
215 size = url_seek(url_fileno(f), 0, SEEK_END);
216 url_seek(url_fileno(f), 0, SEEK_SET);
218 av_new_packet(pkt, size);
219 pkt->stream_index = 0;
220 get_buffer(f, pkt->data, size);
227 static int jpeg_read_close(AVFormatContext *s1)
229 JpegContext *s = s1->priv_data;
234 AVFormat jpeg_format = {
249 AVFMT_NOFILE | AVFMT_NEEDNUMBER,