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 encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
61 const AVFrame *pict, int *got_packet)
63 LJpegEncContext *s = avctx->priv_data;
65 const int width = avctx->width;
66 const int height = avctx->height;
67 const int predictor= avctx->prediction_method+1;
68 const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
69 const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
70 int max_pkt_size = FF_MIN_BUFFER_SIZE;
73 if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
74 max_pkt_size += width * height * 3 * 4;
76 max_pkt_size += mb_width * mb_height * 3 * 4
77 * s->hsample[0] * s->vsample[0];
79 if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
80 av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
84 init_put_bits(&pb, pkt->data, pkt->size);
86 ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
89 header_bits = put_bits_count(&pb);
91 if(avctx->pix_fmt == AV_PIX_FMT_BGRA){
93 const int linesize = pict->linesize[0];
94 uint16_t (*buffer)[4] = s->scratch;
95 int left[3], top[3], topleft[3];
98 buffer[0][i]= 1 << (9 - 1);
101 for(y = 0; y < height; y++) {
102 const int modified_predictor= y ? predictor : 1;
103 uint8_t *ptr = pict->data[0] + (linesize * y);
105 if(pb.buf_end - pb.buf - (put_bits_count(&pb) >> 3) < width * 3 * 4) {
106 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
111 top[i]= left[i]= topleft[i]= buffer[0][i];
113 for(x = 0; x < width; x++) {
114 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
115 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
116 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
121 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
124 top[i]= buffer[x+1][i];
126 left[i]= buffer[x][i];
128 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
131 ff_mjpeg_encode_dc(&pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
133 ff_mjpeg_encode_dc(&pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
140 for(mb_y = 0; mb_y < mb_height; mb_y++) {
141 if (pb.buf_end - pb.buf - (put_bits_count(&pb) >> 3) <
142 mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
143 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
146 for(mb_x = 0; mb_x < mb_width; mb_x++) {
147 if(mb_x==0 || mb_y==0){
150 int x, y, h, v, linesize;
153 linesize = pict->linesize[i];
159 ptr = pict->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
168 pred= ptr[-linesize];
170 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
175 ff_mjpeg_encode_dc(&pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
177 ff_mjpeg_encode_dc(&pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
184 int x, y, h, v, linesize;
187 linesize = pict->linesize[i];
193 ptr = pict->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
194 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
197 ff_mjpeg_encode_dc(&pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
199 ff_mjpeg_encode_dc(&pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
210 ff_mjpeg_encode_picture_trailer(&pb, header_bits);
213 pkt->size = put_bits_ptr(&pb) - pb.buf;
214 pkt->flags |= AV_PKT_FLAG_KEY;
220 static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
222 LJpegEncContext *s = avctx->priv_data;
224 av_frame_free(&avctx->coded_frame);
225 av_freep(&s->scratch);
230 static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
232 LJpegEncContext *s = avctx->priv_data;
233 int chroma_v_shift, chroma_h_shift;
235 if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
236 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
237 avctx->pix_fmt == AV_PIX_FMT_YUV444P) &&
238 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
239 av_log(avctx, AV_LOG_ERROR,
240 "Limited range YUV is non-standard, set strict_std_compliance to "
241 "at least unofficial to use it.\n");
242 return AVERROR(EINVAL);
245 avctx->coded_frame = av_frame_alloc();
246 if (!avctx->coded_frame)
247 return AVERROR(ENOMEM);
249 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
250 avctx->coded_frame->key_frame = 1;
252 s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
254 ff_dsputil_init(&s->dsp, avctx);
255 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
257 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
260 if (avctx->pix_fmt == AV_PIX_FMT_BGRA) {
261 s->vsample[0] = s->hsample[0] =
262 s->vsample[1] = s->hsample[1] =
263 s->vsample[2] = s->hsample[2] = 1;
266 s->vsample[1] = 2 >> chroma_v_shift;
267 s->vsample[2] = 2 >> chroma_v_shift;
269 s->hsample[1] = 2 >> chroma_h_shift;
270 s->hsample[2] = 2 >> chroma_h_shift;
273 ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
274 s->huff_code_dc_luminance,
275 avpriv_mjpeg_bits_dc_luminance,
276 avpriv_mjpeg_val_dc);
277 ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
278 s->huff_code_dc_chrominance,
279 avpriv_mjpeg_bits_dc_chrominance,
280 avpriv_mjpeg_val_dc);
285 AVCodec ff_ljpeg_encoder = {
287 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
288 .type = AVMEDIA_TYPE_VIDEO,
289 .id = AV_CODEC_ID_LJPEG,
290 .priv_data_size = sizeof(LJpegEncContext),
291 .init = ljpeg_encode_init,
292 .encode2 = encode_picture_lossless,
293 .close = ljpeg_encode_close,
294 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ420P,