3 * Copyright (c) 2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 static int jpeg_probe(AVProbeData *pd)
23 if (pd->buf_size >= 64 &&
24 pd->buf[0] == 0xff && pd->buf[1] == 0xd8 && pd->buf[2] == 0xff)
25 return AVPROBE_SCORE_MAX;
30 typedef struct JpegOpaque {
31 int (*alloc_cb)(void *opaque, AVImageInfo *info);
36 /* called by the codec to allocate the image */
37 static int jpeg_get_buffer(AVCodecContext *c, AVFrame *picture)
39 JpegOpaque *jctx = c->opaque;
40 AVImageInfo info1, *info = &info1;
43 info->width = c->width;
44 info->height = c->height;
47 info->pix_fmt = PIX_FMT_YUVJ420P;
50 info->pix_fmt = PIX_FMT_YUVJ422P;
53 info->pix_fmt = PIX_FMT_YUVJ444P;
58 ret = jctx->alloc_cb(jctx->opaque, info);
64 picture->data[i] = info->pict.data[i];
65 picture->linesize[i] = info->pict.linesize[i];
71 static void jpeg_img_copy(uint8_t *dst, int dst_wrap,
72 uint8_t *src, int src_wrap,
73 int width, int height)
75 for(;height > 0; height--) {
76 memcpy(dst, src, width);
82 /* XXX: libavcodec is broken for truncated jpegs! */
83 #define IO_BUF_SIZE (1024*1024)
85 static int jpeg_read(ByteIOContext *f,
86 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
89 AVFrame *picture, picture1;
90 int len, size, got_picture, i;
91 uint8_t *inbuf_ptr, inbuf[IO_BUF_SIZE];
94 jctx.alloc_cb = alloc_cb;
96 jctx.ret_code = -1; /* default return code is error */
98 c = avcodec_alloc_context();
101 picture= avcodec_alloc_frame();
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)
112 size = get_buffer(f, inbuf, sizeof(inbuf));
117 len = avcodec_decode_video(c, &picture1, &got_picture,
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)
139 case PIX_FMT_YUV420P:
143 case PIX_FMT_YUV422P:
146 case PIX_FMT_YUV444P:
150 jpeg_img_copy(picture->data[i], picture->linesize[i],
151 picture1.data[i], picture1.linesize[i],
160 return jctx.ret_code;
163 #if defined(CONFIG_MUXERS) && defined(CONFIG_MJPEG_ENCODER)
164 static int jpeg_write(ByteIOContext *pb, AVImageInfo *info)
167 uint8_t *outbuf = NULL;
168 int outbuf_size, ret, size, i;
172 c = avcodec_alloc_context();
175 picture = avcodec_alloc_frame();
178 c->width = info->width;
179 c->height = info->height;
180 /* XXX: currently move that to the codec ? */
181 switch(info->pix_fmt) {
182 case PIX_FMT_YUVJ420P:
183 c->pix_fmt = PIX_FMT_YUV420P;
185 case PIX_FMT_YUVJ422P:
186 c->pix_fmt = PIX_FMT_YUV422P;
188 case PIX_FMT_YUVJ444P:
189 c->pix_fmt = PIX_FMT_YUV444P;
195 picture->data[i] = info->pict.data[i];
196 picture->linesize[i] = info->pict.linesize[i];
198 /* set the quality */
199 picture->quality = 3; /* XXX: a parameter should be used */
200 c->flags |= CODEC_FLAG_QSCALE;
202 if (avcodec_open(c, &mjpeg_encoder) < 0)
205 /* XXX: needs to sort out that size problem */
206 outbuf_size = 1000000;
207 outbuf = av_malloc(outbuf_size);
209 size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
212 put_buffer(pb, outbuf, size);
213 put_flush_packet(pb);
225 #endif //CONFIG_MUXERS
227 AVImageFormat jpeg_image_format = {
232 (1 << PIX_FMT_YUVJ420P) | (1 << PIX_FMT_YUVJ422P) | (1 << PIX_FMT_YUVJ444P),
233 #if defined(CONFIG_MUXERS) && defined(CONFIG_MJPEG_ENCODER)
237 #endif //CONFIG_MUXERS