2 * lossless JPEG encoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
11 * This file is part of Libav.
13 * Libav is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * Libav is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with Libav; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 * lossless JPEG encoder.
33 #include "libavutil/frame.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/pixdesc.h"
40 #include "mpegvideo.h"
44 typedef struct LJpegEncContext {
52 uint16_t huff_code_dc_luminance[12];
53 uint16_t huff_code_dc_chrominance[12];
54 uint8_t huff_size_dc_luminance[12];
55 uint8_t huff_size_dc_chrominance[12];
57 uint16_t (*scratch)[4];
60 static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
63 LJpegEncContext *s = avctx->priv_data;
64 const int width = frame->width;
65 const int height = frame->height;
66 const int linesize = frame->linesize[0];
67 uint16_t (*buffer)[4] = s->scratch;
68 const int predictor = avctx->prediction_method+1;
69 int left[3], top[3], topleft[3];
72 for (i = 0; i < 3; i++)
73 buffer[0][i] = 1 << (9 - 1);
75 for (y = 0; y < height; y++) {
76 const int modified_predictor = y ? predictor : 1;
77 uint8_t *ptr = frame->data[0] + (linesize * y);
79 if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 3 * 3) {
80 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
84 for (i = 0; i < 3; i++)
85 top[i]= left[i]= topleft[i]= buffer[0][i];
87 for (x = 0; x < width; x++) {
88 buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
89 buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
90 buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
92 for (i = 0; i < 3; i++) {
95 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
98 top[i] = buffer[x+1][i];
100 left[i] = buffer[x][i];
102 diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
105 ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
107 ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
115 static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
116 const AVFrame *frame, int predictor,
121 if (mb_x == 0 || mb_y == 0) {
122 for (i = 0; i < 3; i++) {
124 int x, y, h, v, linesize;
127 linesize = frame->linesize[i];
129 for (y = 0; y < v; y++) {
130 for (x = 0; x < h; x++) {
133 ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
134 if (y == 0 && mb_y == 0) {
135 if (x == 0 && mb_x == 0)
140 if (x == 0 && mb_x == 0) {
141 pred = ptr[-linesize];
143 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
149 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
151 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
156 for (i = 0; i < 3; i++) {
158 int x, y, h, v, linesize;
161 linesize = frame->linesize[i];
163 for (y = 0; y < v; y++) {
164 for (x = 0; x < h; x++) {
167 ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
168 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
171 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
173 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
180 static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
181 const AVFrame *frame)
183 const int predictor = avctx->prediction_method + 1;
184 LJpegEncContext *s = avctx->priv_data;
185 const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
186 const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
189 for (mb_y = 0; mb_y < mb_height; mb_y++) {
190 if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
191 mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
192 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
196 for (mb_x = 0; mb_x < mb_width; mb_x++)
197 ljpeg_encode_yuv_mb(s, pb, frame, predictor, mb_x, mb_y);
203 static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
204 const AVFrame *pict, int *got_packet)
206 LJpegEncContext *s = avctx->priv_data;
208 const int width = avctx->width;
209 const int height = avctx->height;
210 const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
211 const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
212 int max_pkt_size = FF_MIN_BUFFER_SIZE;
213 int ret, header_bits;
215 if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
216 max_pkt_size += width * height * 3 * 3;
218 max_pkt_size += mb_width * mb_height * 3 * 4
219 * s->hsample[0] * s->vsample[0];
221 if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
222 av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
226 init_put_bits(&pb, pkt->data, pkt->size);
228 ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
231 header_bits = put_bits_count(&pb);
233 if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
234 ret = ljpeg_encode_bgr(avctx, &pb, pict);
236 ret = ljpeg_encode_yuv(avctx, &pb, pict);
242 ff_mjpeg_encode_picture_trailer(&pb, header_bits);
245 pkt->size = put_bits_ptr(&pb) - pb.buf;
246 pkt->flags |= AV_PKT_FLAG_KEY;
252 static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
254 LJpegEncContext *s = avctx->priv_data;
256 av_frame_free(&avctx->coded_frame);
257 av_freep(&s->scratch);
262 static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
264 LJpegEncContext *s = avctx->priv_data;
265 int chroma_v_shift, chroma_h_shift;
267 if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
268 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
269 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
270 avctx->color_range == AVCOL_RANGE_MPEG) &&
271 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
272 av_log(avctx, AV_LOG_ERROR,
273 "Limited range YUV is non-standard, set strict_std_compliance to "
274 "at least unofficial to use it.\n");
275 return AVERROR(EINVAL);
278 avctx->coded_frame = av_frame_alloc();
279 if (!avctx->coded_frame)
280 return AVERROR(ENOMEM);
282 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
283 avctx->coded_frame->key_frame = 1;
285 s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
287 ff_dsputil_init(&s->dsp, avctx);
288 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
290 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
293 if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
294 s->vsample[0] = s->hsample[0] =
295 s->vsample[1] = s->hsample[1] =
296 s->vsample[2] = s->hsample[2] = 1;
299 s->vsample[1] = 2 >> chroma_v_shift;
300 s->vsample[2] = 2 >> chroma_v_shift;
302 s->hsample[1] = 2 >> chroma_h_shift;
303 s->hsample[2] = 2 >> chroma_h_shift;
306 ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
307 s->huff_code_dc_luminance,
308 avpriv_mjpeg_bits_dc_luminance,
309 avpriv_mjpeg_val_dc);
310 ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
311 s->huff_code_dc_chrominance,
312 avpriv_mjpeg_bits_dc_chrominance,
313 avpriv_mjpeg_val_dc);
318 AVCodec ff_ljpeg_encoder = {
320 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
321 .type = AVMEDIA_TYPE_VIDEO,
322 .id = AV_CODEC_ID_LJPEG,
323 .priv_data_size = sizeof(LJpegEncContext),
324 .init = ljpeg_encode_init,
325 .encode2 = ljpeg_encode_frame,
326 .close = ljpeg_encode_close,
327 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ420P,