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 FFmpeg.
13 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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/opt.h"
36 #include "libavutil/pixdesc.h"
41 #include "jpegtables.h"
43 #include "mjpegenc_common.h"
46 typedef struct LJpegEncContext {
55 uint16_t huff_code_dc_luminance[12];
56 uint16_t huff_code_dc_chrominance[12];
57 uint8_t huff_size_dc_luminance[12];
58 uint8_t huff_size_dc_chrominance[12];
60 uint16_t (*scratch)[4];
64 static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
67 LJpegEncContext *s = avctx->priv_data;
68 const int width = frame->width;
69 const int height = frame->height;
70 const int linesize = frame->linesize[0];
71 uint16_t (*buffer)[4] = s->scratch;
72 int left[4], top[4], topleft[4];
75 #if FF_API_PRIVATE_OPT
76 FF_DISABLE_DEPRECATION_WARNINGS
77 if (avctx->prediction_method)
78 s->pred = avctx->prediction_method + 1;
79 FF_ENABLE_DEPRECATION_WARNINGS
82 for (i = 0; i < 4; i++)
83 buffer[0][i] = 1 << (9 - 1);
85 for (y = 0; y < height; y++) {
86 const int modified_predictor = y ? s->pred : 1;
87 uint8_t *ptr = frame->data[0] + (linesize * y);
89 if (put_bytes_left(pb, 0) < width * 4 * 4) {
90 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
94 for (i = 0; i < 4; i++)
95 top[i]= left[i]= topleft[i]= buffer[0][i];
97 for (x = 0; x < width; x++) {
98 if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
99 buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
100 buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
101 buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
103 buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100;
104 buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100;
105 buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
106 if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
107 buffer[x][3] = ptr[4 * x + 3];
110 for (i = 0; i < 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA); i++) {
113 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
116 top[i] = buffer[x+1][i];
118 left[i] = buffer[x][i];
120 diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
122 if (i == 0 || i == 3)
123 ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
125 ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
133 static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
134 const AVFrame *frame, int predictor,
139 if (mb_x == 0 || mb_y == 0) {
140 for (i = 0; i < 3; i++) {
142 int x, y, h, v, linesize;
145 linesize = frame->linesize[i];
147 for (y = 0; y < v; y++) {
148 for (x = 0; x < h; x++) {
151 ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
152 if (y == 0 && mb_y == 0) {
153 if (x == 0 && mb_x == 0)
158 if (x == 0 && mb_x == 0) {
159 pred = ptr[-linesize];
161 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
167 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
169 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
174 for (i = 0; i < 3; i++) {
176 int x, y, h, v, linesize;
179 linesize = frame->linesize[i];
181 for (y = 0; y < v; y++) {
182 for (x = 0; x < h; x++) {
185 ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
186 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
189 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
191 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
198 static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
199 const AVFrame *frame)
201 LJpegEncContext *s = avctx->priv_data;
202 const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
203 const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
206 #if FF_API_PRIVATE_OPT
207 FF_DISABLE_DEPRECATION_WARNINGS
208 if (avctx->prediction_method)
209 s->pred = avctx->prediction_method + 1;
210 FF_ENABLE_DEPRECATION_WARNINGS
213 for (mb_y = 0; mb_y < mb_height; mb_y++) {
214 if (put_bytes_left(pb, 0) <
215 mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
216 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
220 for (mb_x = 0; mb_x < mb_width; mb_x++)
221 ljpeg_encode_yuv_mb(s, pb, frame, s->pred, mb_x, mb_y);
227 static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
228 const AVFrame *pict, int *got_packet)
230 LJpegEncContext *s = avctx->priv_data;
232 const int width = avctx->width;
233 const int height = avctx->height;
234 const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
235 const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
236 int max_pkt_size = AV_INPUT_BUFFER_MIN_SIZE;
237 int ret, header_bits;
239 if( avctx->pix_fmt == AV_PIX_FMT_BGR0
240 || avctx->pix_fmt == AV_PIX_FMT_BGR24)
241 max_pkt_size += width * height * 3 * 4;
242 else if(avctx->pix_fmt == AV_PIX_FMT_BGRA)
243 max_pkt_size += width * height * 4 * 4;
245 max_pkt_size += mb_width * mb_height * 3 * 4
246 * s->hsample[0] * s->vsample[0];
249 if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size, 0)) < 0)
252 init_put_bits(&pb, pkt->data, pkt->size);
254 ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
255 s->pred, s->matrix, s->matrix);
257 header_bits = put_bits_count(&pb);
259 if( avctx->pix_fmt == AV_PIX_FMT_BGR0
260 || avctx->pix_fmt == AV_PIX_FMT_BGRA
261 || avctx->pix_fmt == AV_PIX_FMT_BGR24)
262 ret = ljpeg_encode_bgr(avctx, &pb, pict);
264 ret = ljpeg_encode_yuv(avctx, &pb, pict);
270 ff_mjpeg_escape_FF(&pb, header_bits >> 3);
271 ff_mjpeg_encode_picture_trailer(&pb, header_bits);
274 pkt->size = put_bits_ptr(&pb) - pb.buf;
275 pkt->flags |= AV_PKT_FLAG_KEY;
281 static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
283 LJpegEncContext *s = avctx->priv_data;
285 av_freep(&s->scratch);
290 static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
292 int ret = ff_mjpeg_encode_check_pix_fmt(avctx);
293 LJpegEncContext *s = avctx->priv_data;
298 #if FF_API_CODED_FRAME
299 FF_DISABLE_DEPRECATION_WARNINGS
300 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
301 avctx->coded_frame->key_frame = 1;
302 FF_ENABLE_DEPRECATION_WARNINGS
305 s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
307 return AVERROR(ENOMEM);
309 ff_idctdsp_init(&s->idsp, avctx);
310 ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
313 ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
315 ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
316 s->huff_code_dc_luminance,
317 avpriv_mjpeg_bits_dc_luminance,
318 avpriv_mjpeg_val_dc);
319 ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
320 s->huff_code_dc_chrominance,
321 avpriv_mjpeg_bits_dc_chrominance,
322 avpriv_mjpeg_val_dc);
327 #define OFFSET(x) offsetof(LJpegEncContext, x)
328 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
329 static const AVOption options[] = {
330 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
331 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
332 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
333 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
338 static const AVClass ljpeg_class = {
339 .class_name = "ljpeg",
340 .item_name = av_default_item_name,
342 .version = LIBAVUTIL_VERSION_INT,
345 AVCodec ff_ljpeg_encoder = {
347 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
348 .type = AVMEDIA_TYPE_VIDEO,
349 .id = AV_CODEC_ID_LJPEG,
350 .priv_data_size = sizeof(LJpegEncContext),
351 .priv_class = &ljpeg_class,
352 .init = ljpeg_encode_init,
353 .encode2 = ljpeg_encode_frame,
354 .close = ljpeg_encode_close,
355 .capabilities = AV_CODEC_CAP_FRAME_THREADS,
356 .pix_fmts = (const enum AVPixelFormat[]){
357 AV_PIX_FMT_BGR24 , AV_PIX_FMT_BGRA , AV_PIX_FMT_BGR0,
358 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
359 AV_PIX_FMT_YUV420P , AV_PIX_FMT_YUV444P , AV_PIX_FMT_YUV422P,