3 * Copyright (c) 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
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
23 static int jpeg_probe(AVProbeData *pd)
25 if (pd->buf_size >= 64 &&
26 pd->buf[0] == 0xff && pd->buf[1] == 0xd8 && pd->buf[2] == 0xff)
27 return AVPROBE_SCORE_MAX;
32 typedef struct JpegOpaque {
33 int (*alloc_cb)(void *opaque, AVImageInfo *info);
38 /* called by the codec to allocate the image */
39 static int jpeg_get_buffer(AVCodecContext *c, AVFrame *picture)
41 JpegOpaque *jctx = c->opaque;
42 AVImageInfo info1, *info = &info1;
45 info->width = c->width;
46 info->height = c->height;
49 info->pix_fmt = PIX_FMT_YUVJ420P;
52 info->pix_fmt = PIX_FMT_YUVJ422P;
55 info->pix_fmt = PIX_FMT_YUVJ444P;
60 ret = jctx->alloc_cb(jctx->opaque, info);
66 picture->data[i] = info->pict.data[i];
67 picture->linesize[i] = info->pict.linesize[i];
73 static void jpeg_img_copy(uint8_t *dst, int dst_wrap,
74 uint8_t *src, int src_wrap,
75 int width, int height)
77 for(;height > 0; height--) {
78 memcpy(dst, src, width);
84 /* XXX: libavcodec is broken for truncated jpegs! */
85 #define IO_BUF_SIZE (1024*1024)
87 static int jpeg_read(ByteIOContext *f,
88 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
91 AVFrame *picture, picture1;
92 int len, size, got_picture, i;
93 uint8_t *inbuf_ptr, inbuf[IO_BUF_SIZE];
96 jctx.alloc_cb = alloc_cb;
98 jctx.ret_code = -1; /* default return code is error */
100 c = avcodec_alloc_context();
103 picture= avcodec_alloc_frame();
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)
114 size = get_buffer(f, inbuf, sizeof(inbuf));
119 len = avcodec_decode_video(c, &picture1, &got_picture,
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)
141 case PIX_FMT_YUV420P:
145 case PIX_FMT_YUV422P:
148 case PIX_FMT_YUV444P:
152 jpeg_img_copy(picture->data[i], picture->linesize[i],
153 picture1.data[i], picture1.linesize[i],
162 return jctx.ret_code;
165 #if defined(CONFIG_MUXERS) && defined(CONFIG_MJPEG_ENCODER)
166 static int jpeg_write(ByteIOContext *pb, AVImageInfo *info)
169 uint8_t *outbuf = NULL;
170 int outbuf_size, ret, size, i;
174 c = avcodec_alloc_context();
177 picture = avcodec_alloc_frame();
180 c->width = info->width;
181 c->height = info->height;
182 /* XXX: currently move that to the codec ? */
183 switch(info->pix_fmt) {
184 case PIX_FMT_YUVJ420P:
185 c->pix_fmt = PIX_FMT_YUV420P;
187 case PIX_FMT_YUVJ422P:
188 c->pix_fmt = PIX_FMT_YUV422P;
190 case PIX_FMT_YUVJ444P:
191 c->pix_fmt = PIX_FMT_YUV444P;
197 picture->data[i] = info->pict.data[i];
198 picture->linesize[i] = info->pict.linesize[i];
200 /* set the quality */
201 picture->quality = 3; /* XXX: a parameter should be used */
202 c->flags |= CODEC_FLAG_QSCALE;
204 if (avcodec_open(c, &mjpeg_encoder) < 0)
207 /* XXX: needs to sort out that size problem */
208 outbuf_size = 1000000;
209 outbuf = av_malloc(outbuf_size);
211 size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
214 put_buffer(pb, outbuf, size);
215 put_flush_packet(pb);
227 #endif //CONFIG_MUXERS
229 AVImageFormat jpeg_image_format = {
234 (1 << PIX_FMT_YUVJ420P) | (1 << PIX_FMT_YUVJ422P) | (1 << PIX_FMT_YUVJ444P),
235 #if defined(CONFIG_MUXERS) && defined(CONFIG_MJPEG_ENCODER)
239 #endif //CONFIG_MUXERS