Statistics
| Branch: | Revision:

ffmpeg / libavformat / jpeg.c @ 20cd83e2

History | View | Annotate | Download (6.26 KB)

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