Revision 3abf2c27

View differences:

libav/jpeg.c
1
/*
2
 * Miscellaneous MJPEG based formats
3
 * Copyright (c) 2000 Gerard Lantau.
4
 *
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.
9
 *
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.
14
 *
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.
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, 
36
                               int stream_index, UINT8 *buf, int size)
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
AVFormat mpjpeg_format = {
56
    "mpjpeg",
57
    "Mime multipart JPEG format",
58
    "multipart/x-mixed-replace;boundary=" BOUNDARY_TAG,
59
    "mjpg",
60
    CODEC_ID_NONE,
61
    CODEC_ID_MJPEG,
62
    mpjpeg_write_header,
63
    mpjpeg_write_packet,
64
    mpjpeg_write_trailer,
65
};
66

  
67

  
68
/*************************************/
69
/* single frame JPEG */
70

  
71
static int single_jpeg_write_header(AVFormatContext *s)
72
{
73
    return 0;
74
}
75

  
76
static int single_jpeg_write_packet(AVFormatContext *s, int stream_index,
77
                            UINT8 *buf, int size)
78
{
79
    put_buffer(&s->pb, buf, size);
80
    put_flush_packet(&s->pb);
81
    return 1; /* no more data can be sent */
82
}
83

  
84
static int single_jpeg_write_trailer(AVFormatContext *s)
85
{
86
    return 0;
87
}
88

  
89
AVFormat single_jpeg_format = {
90
    "singlejpeg",
91
    "single JPEG image",
92
    "image/jpeg",
93
    "jpg,jpeg",
94
    CODEC_ID_NONE,
95
    CODEC_ID_MJPEG,
96
    single_jpeg_write_header,
97
    single_jpeg_write_packet,
98
    single_jpeg_write_trailer,
99
};
100

  
101
/*************************************/
102
/* multiple jpeg images */
103

  
104
typedef struct JpegContext {
105
    char path[1024];
106
    int img_number;
107
} JpegContext;
108

  
109
static int jpeg_write_header(AVFormatContext *s1)
110
{
111
    JpegContext *s;
112

  
113
    s = av_mallocz(sizeof(JpegContext));
114
    if (!s)
115
        return -1;
116
    s1->priv_data = s;
117
    nstrcpy(s->path, sizeof(s->path), s1->filename);
118
    s->img_number = 1;
119
    return 0;
120
}
121

  
122
static int jpeg_write_packet(AVFormatContext *s1, int stream_index,
123
                            UINT8 *buf, int size)
124
{
125
    JpegContext *s = s1->priv_data;
126
    char filename[1024];
127
    ByteIOContext f1, *pb = &f1;
128

  
129
    snprintf(filename, sizeof(filename), s->path, s->img_number);
130
    if (url_fopen(pb, filename, URL_WRONLY) < 0)
131
        return -EIO;
132

  
133
    put_buffer(pb, buf, size);
134
    put_flush_packet(pb);
135

  
136
    url_fclose(pb);
137
    s->img_number++;
138

  
139
    return 0;
140
}
141

  
142
static int jpeg_write_trailer(AVFormatContext *s1)
143
{
144
    JpegContext *s = s1->priv_data;
145
    free(s);
146
    return 0;
147
}
148

  
149
/***/
150

  
151
static int jpeg_read_header(AVFormatContext *s1, AVFormatParameters *ap)
152
{
153
    JpegContext *s;
154
    int i;
155
    char buf[1024];
156
    ByteIOContext pb1, *f = &pb1;
157
    AVStream *st;
158

  
159
    s = av_mallocz(sizeof(JpegContext));
160
    if (!s)
161
        return -1;
162
    s1->priv_data = s;
163
    nstrcpy(s->path, sizeof(s->path), s1->filename);
164

  
165
    s1->nb_streams = 1;
166
    st = av_mallocz(sizeof(AVStream));
167
    if (!st) {
168
        free(s);
169
        return -ENOMEM;
170
    }
171
    s1->streams[0] = st;
172
    s->img_number = 0;
173

  
174
    /* try to find the first image */
175
    for(i=0;i<5;i++) {
176
        snprintf(buf, sizeof(buf), s->path, s->img_number);
177
        if (url_fopen(f, buf, URL_RDONLY) >= 0)
178
            break;
179
        s->img_number++;
180
    }
181
    if (i == 5)
182
        goto fail;
183
    url_fclose(f);
184
    st->codec.codec_type = CODEC_TYPE_VIDEO;
185
    st->codec.codec_id = CODEC_ID_MJPEG;
186
    
187
    if (!ap || !ap->frame_rate)
188
        st->codec.frame_rate = 25 * FRAME_RATE_BASE;
189
    else
190
        st->codec.frame_rate = ap->frame_rate;
191
    return 0;
192
 fail:
193
    free(s);
194
    return -EIO;
195
}
196

  
197
static int jpeg_read_packet(AVFormatContext *s1, AVPacket *pkt)
198
{
199
    JpegContext *s = s1->priv_data;
200
    char filename[1024];
201
    int size;
202
    ByteIOContext f1, *f = &f1;
203

  
204
    snprintf(filename, sizeof(filename), s->path, s->img_number);
205
    
206
    f = &f1;
207
    if (url_fopen(f, filename, URL_RDONLY) < 0)
208
        return -EIO;
209
    
210
    size = url_seek(url_fileno(f), 0, SEEK_END);
211
    url_seek(url_fileno(f), 0, SEEK_SET);
212

  
213
    av_new_packet(pkt, size);
214
    pkt->stream_index = 0;
215
    get_buffer(f, pkt->data, size);
216

  
217
    url_fclose(f);
218
    s->img_number++;
219
    return 0;
220
}
221

  
222
static int jpeg_read_close(AVFormatContext *s1)
223
{
224
    JpegContext *s = s1->priv_data;
225
    free(s);
226
    return 0;
227
}
228

  
229
AVFormat jpeg_format = {
230
    "jpeg",
231
    "JPEG image",
232
    "image/jpeg",
233
    "jpg,jpeg",
234
    CODEC_ID_NONE,
235
    CODEC_ID_MJPEG,
236
    jpeg_write_header,
237
    jpeg_write_packet,
238
    jpeg_write_trailer,
239

  
240
    jpeg_read_header,
241
    jpeg_read_packet,
242
    jpeg_read_close,
243
    NULL,
244
    AVFMT_NOFILE,
245
};
libav/jpegenc.c
1
/*
2
 * Miscellaneous MJPEG based formats
3
 * Copyright (c) 2000 Gerard Lantau.
4
 *
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.
9
 *
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.
14
 *
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.
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, 
36
                               int stream_index, UINT8 *buf, int size)
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
AVFormat mpjpeg_format = {
56
    "mpjpeg",
57
    "Mime multipart JPEG format",
58
    "multipart/x-mixed-replace;boundary=" BOUNDARY_TAG,
59
    "mjpg",
60
    CODEC_ID_NONE,
61
    CODEC_ID_MJPEG,
62
    mpjpeg_write_header,
63
    mpjpeg_write_packet,
64
    mpjpeg_write_trailer,
65
};
66

  
67

  
68
/* single frame JPEG */
69

  
70
static int jpeg_write_header(AVFormatContext *s)
71
{
72
    return 0;
73
}
74

  
75
static int jpeg_write_packet(AVFormatContext *s, int stream_index,
76
                            UINT8 *buf, int size)
77
{
78
    put_buffer(&s->pb, buf, size);
79
    put_flush_packet(&s->pb);
80
    return 1; /* no more data can be sent */
81
}
82

  
83
static int jpeg_write_trailer(AVFormatContext *s)
84
{
85
    return 0;
86
}
87

  
88
AVFormat jpeg_format = {
89
    "jpeg",
90
    "JPEG image",
91
    "image/jpeg",
92
    "jpg,jpeg",
93
    CODEC_ID_NONE,
94
    CODEC_ID_MJPEG,
95
    jpeg_write_header,
96
    jpeg_write_packet,
97
    jpeg_write_trailer,
98
};

Also available in: Unified diff