Statistics
| Branch: | Revision:

ffmpeg / libavformat / jpeg.c @ 2d2f443d

History | View | Annotate | Download (6.18 KB)

1 3abf2c27 Fabrice Bellard
/*
2 0250738f Fabrice Bellard
 * JPEG image format
3
 * Copyright (c) 2003 Fabrice Bellard.
4 3abf2c27 Fabrice Bellard
 *
5 0726b2d1 Fabrice Bellard
 * 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 3abf2c27 Fabrice Bellard
 *
10 0726b2d1 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 3abf2c27 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 0726b2d1 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 3abf2c27 Fabrice Bellard
 *
15 0726b2d1 Fabrice Bellard
 * 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 3abf2c27 Fabrice Bellard
 */
19
#include "avformat.h"
20
21 0250738f Fabrice Bellard
static int jpeg_probe(AVProbeData *pd)
22 3abf2c27 Fabrice Bellard
{
23 0250738f Fabrice Bellard
    if (pd->buf_size >= 64 &&
24
        pd->buf[0] == 0xff && pd->buf[1] == 0xd8 && pd->buf[2] == 0xff)
25
        return AVPROBE_SCORE_MAX;
26
    else
27
        return 0;
28 3abf2c27 Fabrice Bellard
}
29
30 0250738f Fabrice Bellard
typedef struct JpegOpaque {
31
    int (*alloc_cb)(void *opaque, AVImageInfo *info);
32
    void *opaque;
33
    int ret_code;
34
} JpegOpaque;
35 3abf2c27 Fabrice Bellard
36 0250738f Fabrice Bellard
/* called by the codec to allocate the image */
37
static int jpeg_get_buffer(AVCodecContext *c, AVFrame *picture)
38 3abf2c27 Fabrice Bellard
{
39 0250738f Fabrice Bellard
    JpegOpaque *jctx = c->opaque;
40
    AVImageInfo info1, *info = &info1;
41
    int ret, i;
42
43
    info->width = c->width;
44
    info->height = c->height;
45 41f53f86 Fabrice Bellard
    switch(c->pix_fmt) {
46
    case PIX_FMT_YUV420P:
47
        info->pix_fmt = PIX_FMT_YUVJ420P;
48
        break;
49
    case PIX_FMT_YUV422P:
50
        info->pix_fmt = PIX_FMT_YUVJ422P;
51
        break;
52
    case PIX_FMT_YUV444P:
53
        info->pix_fmt = PIX_FMT_YUVJ444P;
54
        break;
55
    default:
56
        return -1;
57
    }
58 0250738f Fabrice Bellard
    ret = jctx->alloc_cb(jctx->opaque, info);
59
    if (ret) {
60
        jctx->ret_code = ret;
61 3abf2c27 Fabrice Bellard
        return -1;
62 0250738f Fabrice Bellard
    } else {
63
        for(i=0;i<3;i++) {
64
            picture->data[i] = info->pict.data[i];
65
            picture->linesize[i] = info->pict.linesize[i];
66
        }
67
        return 0;
68
    }
69 3abf2c27 Fabrice Bellard
}
70
71 e1c2a5a0 Roberto Togni
static void jpeg_img_copy(uint8_t *dst, int dst_wrap, 
72 0c1a9eda Zdenek Kabelac
                     uint8_t *src, int src_wrap,
73 0250738f Fabrice Bellard
                     int width, int height)
74 3abf2c27 Fabrice Bellard
{
75 0250738f Fabrice Bellard
    for(;height > 0; height--) {
76
        memcpy(dst, src, width);
77
        dst += dst_wrap;
78
        src += src_wrap;
79
    }
80 3abf2c27 Fabrice Bellard
}
81
82 0250738f Fabrice Bellard
/* XXX: libavcodec is broken for truncated jpegs! */
83
#define IO_BUF_SIZE (1024*1024)
84 3abf2c27 Fabrice Bellard
85 0250738f Fabrice Bellard
static int jpeg_read(ByteIOContext *f, 
86
                     int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
87 3abf2c27 Fabrice Bellard
{
88 0250738f Fabrice Bellard
    AVCodecContext *c;
89
    AVFrame *picture, picture1;
90
    int len, size, got_picture, i;
91
    uint8_t *inbuf_ptr, inbuf[IO_BUF_SIZE];
92
    JpegOpaque jctx;
93
94
    jctx.alloc_cb = alloc_cb;
95
    jctx.opaque = opaque;
96
    jctx.ret_code = -1; /* default return code is error */
97
    
98
    c = avcodec_alloc_context();
99
    if (!c)
100
        return -1;
101
    picture= avcodec_alloc_frame();
102
    if (!picture) {
103
        av_free(c);
104 3abf2c27 Fabrice Bellard
        return -1;
105
    }
106 0250738f Fabrice Bellard
    c->opaque = &jctx;
107
    c->get_buffer = jpeg_get_buffer;
108
    c->flags |= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
109
    if (avcodec_open(c, &mjpeg_decoder) < 0)
110
        goto fail1;
111
    for(;;) {
112
        size = get_buffer(f, inbuf, sizeof(inbuf));
113
        if (size == 0)
114 3abf2c27 Fabrice Bellard
            break;
115 0250738f Fabrice Bellard
        inbuf_ptr = inbuf;
116
        while (size > 0) {
117
            len = avcodec_decode_video(c, &picture1, &got_picture, 
118
                                       inbuf_ptr, size);
119
            if (len < 0)
120
                goto fail;
121
            if (got_picture)
122
                goto the_end;
123
            size -= len;
124
            inbuf_ptr += len;
125
        }
126 3abf2c27 Fabrice Bellard
    }
127 0250738f Fabrice Bellard
 the_end:
128
    /* XXX: currently, the mjpeg decoder does not use AVFrame, so we
129
       must do it by hand */
130
    if (jpeg_get_buffer(c, picture) < 0)
131 3abf2c27 Fabrice Bellard
        goto fail;
132 0250738f Fabrice Bellard
    for(i=0;i<3;i++) {
133
        int w, h;
134
        w = c->width;
135
        h = c->height;
136
        if (i >= 1) {
137
            switch(c->pix_fmt) {
138
            default:
139
            case PIX_FMT_YUV420P:
140 fe2a4ca3 Fabrice Bellard
                w = (w + 1) >> 1;
141
                h = (h + 1) >> 1;
142 0250738f Fabrice Bellard
                break;
143
            case PIX_FMT_YUV422P:
144 fe2a4ca3 Fabrice Bellard
                w = (w + 1) >> 1;
145 0250738f Fabrice Bellard
                break;
146
            case PIX_FMT_YUV444P:
147
                break;
148
            }
149
        }
150 e1c2a5a0 Roberto Togni
        jpeg_img_copy(picture->data[i], picture->linesize[i],
151 0250738f Fabrice Bellard
                 picture1.data[i], picture1.linesize[i],
152
                 w, h);
153
    }
154
    jctx.ret_code = 0;
155 3abf2c27 Fabrice Bellard
 fail:
156 0250738f Fabrice Bellard
    avcodec_close(c);
157
 fail1:
158
    av_free(picture);
159
    av_free(c);
160
    return jctx.ret_code;
161 3abf2c27 Fabrice Bellard
}
162
163 076342fa Michael Niedermayer
#ifdef CONFIG_ENCODERS
164 0250738f Fabrice Bellard
static int jpeg_write(ByteIOContext *pb, AVImageInfo *info)
165 3abf2c27 Fabrice Bellard
{
166 0250738f Fabrice Bellard
    AVCodecContext *c;
167
    uint8_t *outbuf = NULL;
168
    int outbuf_size, ret, size, i;
169
    AVFrame *picture;
170
171
    ret = -1;
172
    c = avcodec_alloc_context();
173
    if (!c)
174
        return -1;
175
    picture = avcodec_alloc_frame();
176
    if (!picture)
177
        goto fail2;
178
    c->width = info->width;
179
    c->height = info->height;
180 41f53f86 Fabrice Bellard
    /* XXX: currently move that to the codec ? */
181
    switch(info->pix_fmt) {
182
    case PIX_FMT_YUVJ420P:
183
        c->pix_fmt = PIX_FMT_YUV420P;
184
        break;
185
    case PIX_FMT_YUVJ422P:
186
        c->pix_fmt = PIX_FMT_YUV422P;
187
        break;
188
    case PIX_FMT_YUVJ444P:
189
        c->pix_fmt = PIX_FMT_YUV444P;
190
        break;
191
    default:
192
        goto fail1;
193
    }
194 0250738f Fabrice Bellard
    for(i=0;i<3;i++) {
195
        picture->data[i] = info->pict.data[i];
196
        picture->linesize[i] = info->pict.linesize[i];
197
    }
198
    /* set the quality */
199
    picture->quality = 3; /* XXX: a parameter should be used */
200
    c->flags |= CODEC_FLAG_QSCALE;
201 3abf2c27 Fabrice Bellard
    
202 0250738f Fabrice Bellard
    if (avcodec_open(c, &mjpeg_encoder) < 0)
203
        goto fail1;
204 3abf2c27 Fabrice Bellard
    
205 0250738f Fabrice Bellard
    /* XXX: needs to sort out that size problem */
206
    outbuf_size = 1000000;
207
    outbuf = av_malloc(outbuf_size);
208 3abf2c27 Fabrice Bellard
209 0250738f Fabrice Bellard
    size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
210
    if (size < 0)
211
        goto fail;
212
    put_buffer(pb, outbuf, size);
213
    put_flush_packet(pb);
214
    ret = 0;
215 3abf2c27 Fabrice Bellard
216 0250738f Fabrice Bellard
 fail:
217
    avcodec_close(c);
218
    av_free(outbuf);
219
 fail1:
220
    av_free(picture);
221
 fail2:
222
    av_free(c);
223
    return ret;
224 3abf2c27 Fabrice Bellard
}
225 076342fa Michael Niedermayer
#endif //CONFIG_ENCODERS
226 3abf2c27 Fabrice Bellard
227 0250738f Fabrice Bellard
AVImageFormat jpeg_image_format = {
228 3abf2c27 Fabrice Bellard
    "jpeg",
229
    "jpg,jpeg",
230 0250738f Fabrice Bellard
    jpeg_probe,
231
    jpeg_read,
232 41f53f86 Fabrice Bellard
    (1 << PIX_FMT_YUVJ420P) | (1 << PIX_FMT_YUVJ422P) | (1 << PIX_FMT_YUVJ444P),
233 076342fa Michael Niedermayer
#ifdef CONFIG_ENCODERS
234 0250738f Fabrice Bellard
    jpeg_write,
235 076342fa Michael Niedermayer
#else
236
    NULL,
237
#endif //CONFIG_ENCODERS
238 3abf2c27 Fabrice Bellard
};