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/pixdesc.h"
40 #include "jpegtables.h"
41 #include "mjpegenc_common.h"
45 typedef struct LJpegEncContext {
54 uint16_t huff_code_dc_luminance[12];
55 uint16_t huff_code_dc_chrominance[12];
56 uint8_t huff_size_dc_luminance[12];
57 uint8_t huff_size_dc_chrominance[12];
59 uint16_t (*scratch)[4];
63 static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
66 LJpegEncContext *s = avctx->priv_data;
67 const int width = frame->width;
68 const int height = frame->height;
69 const int linesize = frame->linesize[0];
70 uint16_t (*buffer)[4] = s->scratch;
71 int left[4], top[4], topleft[4];
74 #if FF_API_PRIVATE_OPT
75 FF_DISABLE_DEPRECATION_WARNINGS
76 if (avctx->prediction_method)
77 s->pred = avctx->prediction_method + 1;
78 FF_ENABLE_DEPRECATION_WARNINGS
81 for (i = 0; i < 4; i++)
82 buffer[0][i] = 1 << (9 - 1);
84 for (y = 0; y < height; y++) {
85 const int modified_predictor = y ? s->pred : 1;
86 uint8_t *ptr = frame->data[0] + (linesize * y);
88 if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 4 * 4) {
89 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
93 for (i = 0; i < 4; i++)
94 top[i]= left[i]= topleft[i]= buffer[0][i];
96 for (x = 0; x < width; x++) {
97 if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
98 buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
99 buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
100 buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
102 buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100;
103 buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100;
104 buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
105 if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
106 buffer[x][3] = ptr[4 * x + 3];
109 for (i = 0; i < 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA); i++) {
112 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
115 top[i] = buffer[x+1][i];
117 left[i] = buffer[x][i];
119 diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
121 if (i == 0 || i == 3)
122 ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
124 ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
132 static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
133 const AVFrame *frame, int predictor,
138 if (mb_x == 0 || mb_y == 0) {
139 for (i = 0; i < 3; i++) {
141 int x, y, h, v, linesize;
144 linesize = frame->linesize[i];
146 for (y = 0; y < v; y++) {
147 for (x = 0; x < h; x++) {
150 ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
151 if (y == 0 && mb_y == 0) {
152 if (x == 0 && mb_x == 0)
157 if (x == 0 && mb_x == 0) {
158 pred = ptr[-linesize];
160 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
166 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
168 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
173 for (i = 0; i < 3; i++) {
175 int x, y, h, v, linesize;
178 linesize = frame->linesize[i];
180 for (y = 0; y < v; y++) {
181 for (x = 0; x < h; x++) {
184 ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
185 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
188 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
190 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
197 static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
198 const AVFrame *frame)
200 LJpegEncContext *s = avctx->priv_data;
201 const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
202 const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
205 #if FF_API_PRIVATE_OPT
206 FF_DISABLE_DEPRECATION_WARNINGS
207 if (avctx->prediction_method)
208 s->pred = avctx->prediction_method + 1;
209 FF_ENABLE_DEPRECATION_WARNINGS
212 for (mb_y = 0; mb_y < mb_height; mb_y++) {
213 if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
214 mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
215 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
219 for (mb_x = 0; mb_x < mb_width; mb_x++)
220 ljpeg_encode_yuv_mb(s, pb, frame, s->pred, mb_x, mb_y);
226 static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
227 const AVFrame *pict, int *got_packet)
229 LJpegEncContext *s = avctx->priv_data;
231 const int width = avctx->width;
232 const int height = avctx->height;
233 const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
234 const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
235 int max_pkt_size = AV_INPUT_BUFFER_MIN_SIZE;
236 int ret, header_bits;
238 if( avctx->pix_fmt == AV_PIX_FMT_BGR0
239 || avctx->pix_fmt == AV_PIX_FMT_BGR24)
240 max_pkt_size += width * height * 3 * 4;
241 else if(avctx->pix_fmt == AV_PIX_FMT_BGRA)
242 max_pkt_size += width * height * 4 * 4;
244 max_pkt_size += mb_width * mb_height * 3 * 4
245 * s->hsample[0] * s->vsample[0];
248 if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size, 0)) < 0)
251 init_put_bits(&pb, pkt->data, pkt->size);
253 ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
254 s->pred, s->matrix, s->matrix);
256 header_bits = put_bits_count(&pb);
258 if( avctx->pix_fmt == AV_PIX_FMT_BGR0
259 || avctx->pix_fmt == AV_PIX_FMT_BGRA
260 || avctx->pix_fmt == AV_PIX_FMT_BGR24)
261 ret = ljpeg_encode_bgr(avctx, &pb, pict);
263 ret = ljpeg_encode_yuv(avctx, &pb, pict);
269 ff_mjpeg_escape_FF(&pb, header_bits >> 3);
270 ff_mjpeg_encode_picture_trailer(&pb, header_bits);
273 pkt->size = put_bits_ptr(&pb) - pb.buf;
274 pkt->flags |= AV_PKT_FLAG_KEY;
280 static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
282 LJpegEncContext *s = avctx->priv_data;
284 av_freep(&s->scratch);
289 static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
291 LJpegEncContext *s = avctx->priv_data;
293 if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
294 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
295 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
296 avctx->color_range == AVCOL_RANGE_MPEG) &&
297 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
298 av_log(avctx, AV_LOG_ERROR,
299 "Limited range YUV is non-standard, set strict_std_compliance to "
300 "at least unofficial to use it.\n");
301 return AVERROR(EINVAL);
304 #if FF_API_CODED_FRAME
305 FF_DISABLE_DEPRECATION_WARNINGS
306 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
307 avctx->coded_frame->key_frame = 1;
308 FF_ENABLE_DEPRECATION_WARNINGS
311 s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
315 ff_idctdsp_init(&s->idsp, avctx);
316 ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
319 ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
321 ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
322 s->huff_code_dc_luminance,
323 avpriv_mjpeg_bits_dc_luminance,
324 avpriv_mjpeg_val_dc);
325 ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
326 s->huff_code_dc_chrominance,
327 avpriv_mjpeg_bits_dc_chrominance,
328 avpriv_mjpeg_val_dc);
332 ljpeg_encode_close(avctx);
333 return AVERROR(ENOMEM);
336 #define OFFSET(x) offsetof(LJpegEncContext, x)
337 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
338 static const AVOption options[] = {
339 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
340 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
341 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
342 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
347 static const AVClass ljpeg_class = {
348 .class_name = "ljpeg",
349 .item_name = av_default_item_name,
351 .version = LIBAVUTIL_VERSION_INT,
354 AVCodec ff_ljpeg_encoder = {
356 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
357 .type = AVMEDIA_TYPE_VIDEO,
358 .id = AV_CODEC_ID_LJPEG,
359 .priv_data_size = sizeof(LJpegEncContext),
360 .priv_class = &ljpeg_class,
361 .init = ljpeg_encode_init,
362 .encode2 = ljpeg_encode_frame,
363 .close = ljpeg_encode_close,
364 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
365 .pix_fmts = (const enum AVPixelFormat[]){
366 AV_PIX_FMT_BGR24 , AV_PIX_FMT_BGRA , AV_PIX_FMT_BGR0,
367 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
368 AV_PIX_FMT_YUV420P , AV_PIX_FMT_YUV444P , AV_PIX_FMT_YUV422P,