3 * Copyright (c) 2000, 2001 Fabrice Bellard.
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.
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.
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
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, int stream_index,
36 UINT8 *buf, int size, int force_pts)
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 static AVOutputFormat mpjpeg_format = {
57 "Mime multipart JPEG format",
58 "multipart/x-mixed-replace;boundary=" BOUNDARY_TAG,
69 /*************************************/
70 /* single frame JPEG */
72 static int single_jpeg_write_header(AVFormatContext *s)
77 static int single_jpeg_write_packet(AVFormatContext *s, int stream_index,
78 UINT8 *buf, int size, int force_pts)
80 put_buffer(&s->pb, buf, size);
81 put_flush_packet(&s->pb);
82 return 1; /* no more data can be sent */
85 static int single_jpeg_write_trailer(AVFormatContext *s)
90 static AVOutputFormat single_jpeg_format = {
94 NULL, /* note: no extension to favorize jpeg multiple images match */
98 single_jpeg_write_header,
99 single_jpeg_write_packet,
100 single_jpeg_write_trailer,
103 /*************************************/
104 /* multiple jpeg images */
106 typedef struct JpegContext {
111 static int jpeg_write_header(AVFormatContext *s1)
115 s = av_mallocz(sizeof(JpegContext));
119 pstrcpy(s->path, sizeof(s->path), s1->filename);
124 static int jpeg_write_packet(AVFormatContext *s1, int stream_index,
125 UINT8 *buf, int size, int force_pts)
127 JpegContext *s = s1->priv_data;
129 ByteIOContext f1, *pb = &f1;
131 if (get_frame_filename(filename, sizeof(filename),
132 s->path, s->img_number) < 0)
134 if (url_fopen(pb, filename, URL_WRONLY) < 0)
137 put_buffer(pb, buf, size);
138 put_flush_packet(pb);
146 static int jpeg_write_trailer(AVFormatContext *s1)
153 static int jpeg_read_header(AVFormatContext *s1, AVFormatParameters *ap)
158 ByteIOContext pb1, *f = &pb1;
161 s = av_mallocz(sizeof(JpegContext));
165 pstrcpy(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)
232 static AVInputFormat jpeg_iformat = {
241 flags: AVFMT_NOFILE | AVFMT_NEEDNUMBER,
242 extensions: "jpg,jpeg",
245 static AVOutputFormat jpeg_oformat = {
256 flags: AVFMT_NOFILE | AVFMT_NEEDNUMBER,
261 av_register_output_format(&mpjpeg_format);
262 av_register_output_format(&single_jpeg_format);
263 av_register_input_format(&jpeg_iformat);
264 av_register_output_format(&jpeg_oformat);