jpeg.c 6.26 KB
Newer Older
1
/*
2 3
 * JPEG image format
 * Copyright (c) 2003 Fabrice Bellard.
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 22
 */
#include "avformat.h"

23
static int jpeg_probe(AVProbeData *pd)
24
{
25 26 27 28 29
    if (pd->buf_size >= 64 &&
        pd->buf[0] == 0xff && pd->buf[1] == 0xd8 && pd->buf[2] == 0xff)
        return AVPROBE_SCORE_MAX;
    else
        return 0;
30 31
}

32 33 34 35 36
typedef struct JpegOpaque {
    int (*alloc_cb)(void *opaque, AVImageInfo *info);
    void *opaque;
    int ret_code;
} JpegOpaque;
37

38 39
/* called by the codec to allocate the image */
static int jpeg_get_buffer(AVCodecContext *c, AVFrame *picture)
40
{
41 42 43 44 45 46
    JpegOpaque *jctx = c->opaque;
    AVImageInfo info1, *info = &info1;
    int ret, i;

    info->width = c->width;
    info->height = c->height;
47 48 49 50 51 52 53 54 55 56 57 58 59
    switch(c->pix_fmt) {
    case PIX_FMT_YUV420P:
        info->pix_fmt = PIX_FMT_YUVJ420P;
        break;
    case PIX_FMT_YUV422P:
        info->pix_fmt = PIX_FMT_YUVJ422P;
        break;
    case PIX_FMT_YUV444P:
        info->pix_fmt = PIX_FMT_YUVJ444P;
        break;
    default:
        return -1;
    }
60 61 62
    ret = jctx->alloc_cb(jctx->opaque, info);
    if (ret) {
        jctx->ret_code = ret;
63
        return -1;
64 65 66 67 68 69 70
    } else {
        for(i=0;i<3;i++) {
            picture->data[i] = info->pict.data[i];
            picture->linesize[i] = info->pict.linesize[i];
        }
        return 0;
    }
71 72
}

73
static void jpeg_img_copy(uint8_t *dst, int dst_wrap,
74
                     uint8_t *src, int src_wrap,
75
                     int width, int height)
76
{
77 78 79 80 81
    for(;height > 0; height--) {
        memcpy(dst, src, width);
        dst += dst_wrap;
        src += src_wrap;
    }
82 83
}

84 85
/* XXX: libavcodec is broken for truncated jpegs! */
#define IO_BUF_SIZE (1024*1024)
86

87
static int jpeg_read(ByteIOContext *f,
88
                     int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
89
{
90 91 92 93 94 95 96 97 98
    AVCodecContext *c;
    AVFrame *picture, picture1;
    int len, size, got_picture, i;
    uint8_t *inbuf_ptr, inbuf[IO_BUF_SIZE];
    JpegOpaque jctx;

    jctx.alloc_cb = alloc_cb;
    jctx.opaque = opaque;
    jctx.ret_code = -1; /* default return code is error */
99

100 101 102 103 104 105
    c = avcodec_alloc_context();
    if (!c)
        return -1;
    picture= avcodec_alloc_frame();
    if (!picture) {
        av_free(c);
106 107
        return -1;
    }
108 109 110 111 112 113 114 115
    c->opaque = &jctx;
    c->get_buffer = jpeg_get_buffer;
    c->flags |= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
    if (avcodec_open(c, &mjpeg_decoder) < 0)
        goto fail1;
    for(;;) {
        size = get_buffer(f, inbuf, sizeof(inbuf));
        if (size == 0)
116
            break;
117 118
        inbuf_ptr = inbuf;
        while (size > 0) {
119
            len = avcodec_decode_video(c, &picture1, &got_picture,
120 121 122 123 124 125 126 127
                                       inbuf_ptr, size);
            if (len < 0)
                goto fail;
            if (got_picture)
                goto the_end;
            size -= len;
            inbuf_ptr += len;
        }
128
    }
129 130 131 132
 the_end:
    /* XXX: currently, the mjpeg decoder does not use AVFrame, so we
       must do it by hand */
    if (jpeg_get_buffer(c, picture) < 0)
133
        goto fail;
134 135 136 137 138 139 140 141
    for(i=0;i<3;i++) {
        int w, h;
        w = c->width;
        h = c->height;
        if (i >= 1) {
            switch(c->pix_fmt) {
            default:
            case PIX_FMT_YUV420P:
Fabrice Bellard's avatar
Fabrice Bellard committed
142 143
                w = (w + 1) >> 1;
                h = (h + 1) >> 1;
144 145
                break;
            case PIX_FMT_YUV422P:
Fabrice Bellard's avatar
Fabrice Bellard committed
146
                w = (w + 1) >> 1;
147 148 149 150 151
                break;
            case PIX_FMT_YUV444P:
                break;
            }
        }
152
        jpeg_img_copy(picture->data[i], picture->linesize[i],
153 154 155 156
                 picture1.data[i], picture1.linesize[i],
                 w, h);
    }
    jctx.ret_code = 0;
157
 fail:
158 159 160 161 162
    avcodec_close(c);
 fail1:
    av_free(picture);
    av_free(c);
    return jctx.ret_code;
163 164
}

165
#if defined(CONFIG_MUXERS) && defined(CONFIG_MJPEG_ENCODER)
166
static int jpeg_write(ByteIOContext *pb, AVImageInfo *info)
167
{
168 169 170 171 172 173 174 175 176 177 178 179 180 181
    AVCodecContext *c;
    uint8_t *outbuf = NULL;
    int outbuf_size, ret, size, i;
    AVFrame *picture;

    ret = -1;
    c = avcodec_alloc_context();
    if (!c)
        return -1;
    picture = avcodec_alloc_frame();
    if (!picture)
        goto fail2;
    c->width = info->width;
    c->height = info->height;
182 183 184 185 186 187 188 189 190 191 192 193 194 195
    /* XXX: currently move that to the codec ? */
    switch(info->pix_fmt) {
    case PIX_FMT_YUVJ420P:
        c->pix_fmt = PIX_FMT_YUV420P;
        break;
    case PIX_FMT_YUVJ422P:
        c->pix_fmt = PIX_FMT_YUV422P;
        break;
    case PIX_FMT_YUVJ444P:
        c->pix_fmt = PIX_FMT_YUV444P;
        break;
    default:
        goto fail1;
    }
196 197 198 199 200 201 202
    for(i=0;i<3;i++) {
        picture->data[i] = info->pict.data[i];
        picture->linesize[i] = info->pict.linesize[i];
    }
    /* set the quality */
    picture->quality = 3; /* XXX: a parameter should be used */
    c->flags |= CODEC_FLAG_QSCALE;
203

204 205
    if (avcodec_open(c, &mjpeg_encoder) < 0)
        goto fail1;
206

207 208 209
    /* XXX: needs to sort out that size problem */
    outbuf_size = 1000000;
    outbuf = av_malloc(outbuf_size);
210

211 212 213 214 215 216
    size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
    if (size < 0)
        goto fail;
    put_buffer(pb, outbuf, size);
    put_flush_packet(pb);
    ret = 0;
217

218 219 220 221 222 223 224 225
 fail:
    avcodec_close(c);
    av_free(outbuf);
 fail1:
    av_free(picture);
 fail2:
    av_free(c);
    return ret;
226
}
227
#endif //CONFIG_MUXERS
228

229
AVImageFormat jpeg_image_format = {
230 231
    "jpeg",
    "jpg,jpeg",
232 233
    jpeg_probe,
    jpeg_read,
234
    (1 << PIX_FMT_YUVJ420P) | (1 << PIX_FMT_YUVJ422P) | (1 << PIX_FMT_YUVJ444P),
235
#if defined(CONFIG_MUXERS) && defined(CONFIG_MJPEG_ENCODER)
236
    jpeg_write,
237 238
#else
    NULL,
239
#endif //CONFIG_MUXERS
240
};