FFmpeg
Loading...
Searching...
No Matches
ljpegenc.c
Go to the documentation of this file.
1/*
2 * lossless JPEG encoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
6 *
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
9 * by Alex Beregszaszi
10 *
11 * This file is part of FFmpeg.
12 *
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.
17 *
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.
22 *
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
26 */
27
28/**
29 * @file
30 * lossless JPEG encoder.
31 */
32
33#include "libavutil/frame.h"
34#include "libavutil/mem.h"
35#include "libavutil/opt.h"
36
37#include "avcodec.h"
38#include "codec_internal.h"
39#include "encode.h"
40#include "jpegtables.h"
41#include "mjpegenc_common.h"
42#include "mjpeg.h"
43
44typedef struct LJpegEncContext {
45 AVClass *class;
46
47 int vsample[4];
48 int hsample[4];
49
54
55 uint16_t (*scratch)[4];
56 int pred;
58
60 const AVFrame *frame)
61{
62 LJpegEncContext *s = avctx->priv_data;
63 const int width = frame->width;
64 const int height = frame->height;
65 const int linesize = frame->linesize[0];
66 uint16_t (*buffer)[4] = s->scratch;
67 int left[4], top[4], topleft[4];
68 int x, y, i;
69
70 for (i = 0; i < 4; i++)
71 buffer[0][i] = 1 << (9 - 1);
72
73 for (y = 0; y < height; y++) {
74 const int modified_predictor = y ? s->pred : 1;
75 const uint8_t *ptr = frame->data[0] + (linesize * y);
76
77 if (put_bytes_left(pb, 0) < width * 4 * 4) {
78 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
79 return -1;
80 }
81
82 for (i = 0; i < 4; i++)
83 top[i]= left[i]= topleft[i]= buffer[0][i];
84
85 for (x = 0; x < width; x++) {
86 if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
87 buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100;
88 buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
89 buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
90 }else{
91 buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100;
92 buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100;
93 buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
94 if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
95 buffer[x][3] = ptr[4 * x + 3];
96 }
97
98 for (i = 0; i < 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA); i++) {
99 int pred, diff;
100
101 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
102
103 topleft[i] = top[i];
104 top[i] = buffer[x+1][i];
105
106 left[i] = buffer[x][i];
107
108 diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
109
110 if (i == 0 || i == 3)
111 ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
112 else
113 ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
114 }
115 }
116 }
117
118 return 0;
119}
120
122 const AVFrame *frame, int predictor,
123 int mb_x, int mb_y)
124{
125 int i;
126
127 if (mb_x == 0 || mb_y == 0) {
128 for (i = 0; i < 3; i++) {
129 const uint8_t *ptr;
130 int x, y, h, v, linesize;
131 h = s->hsample[i];
132 v = s->vsample[i];
133 linesize = frame->linesize[i];
134
135 for (y = 0; y < v; y++) {
136 for (x = 0; x < h; x++) {
137 int pred;
138
139 ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
140 if (y == 0 && mb_y == 0) {
141 if (x == 0 && mb_x == 0)
142 pred = 128;
143 else
144 pred = ptr[-1];
145 } else {
146 if (x == 0 && mb_x == 0) {
147 pred = ptr[-linesize];
148 } else {
149 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
150 ptr[-1], predictor);
151 }
152 }
153
154 if (i == 0)
155 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
156 else
157 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
158 }
159 }
160 }
161 } else {
162 for (i = 0; i < 3; i++) {
163 const uint8_t *ptr;
164 int x, y, h, v, linesize;
165 h = s->hsample[i];
166 v = s->vsample[i];
167 linesize = frame->linesize[i];
168
169 for (y = 0; y < v; y++) {
170 for (x = 0; x < h; x++) {
171 int pred;
172
173 ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
174 PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
175
176 if (i == 0)
177 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
178 else
179 ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
180 }
181 }
182 }
183 }
184}
185
187 const AVFrame *frame)
188{
189 LJpegEncContext *s = avctx->priv_data;
190 const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0];
191 const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
192 int mb_x, mb_y;
193
194 for (mb_y = 0; mb_y < mb_height; mb_y++) {
195 if (put_bytes_left(pb, 0) <
196 mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
197 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
198 return -1;
199 }
200
201 for (mb_x = 0; mb_x < mb_width; mb_x++)
202 ljpeg_encode_yuv_mb(s, pb, frame, s->pred, mb_x, mb_y);
203 }
204
205 return 0;
206}
207
209 const AVFrame *pict, int *got_packet)
210{
211 LJpegEncContext *s = avctx->priv_data;
212 PutBitContext pb;
213 const int width = avctx->width;
214 const int height = avctx->height;
215 const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
216 const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
217 size_t max_pkt_size = FF_INPUT_BUFFER_MIN_SIZE;
218 int ret, header_bits;
219
220 if( avctx->pix_fmt == AV_PIX_FMT_BGR0
221 || avctx->pix_fmt == AV_PIX_FMT_BGR24)
222 max_pkt_size += width * height * 3 * 4;
223 else if(avctx->pix_fmt == AV_PIX_FMT_BGRA)
224 max_pkt_size += width * height * 4 * 4;
225 else {
226 max_pkt_size += mb_width * mb_height * 3 * 4
227 * s->hsample[0] * s->vsample[0];
228 }
229
230 if ((ret = ff_mjpeg_add_icc_profile_size(avctx, pict, &max_pkt_size)) < 0)
231 return ret;
232 if ((ret = ff_alloc_packet(avctx, pkt, max_pkt_size)) < 0)
233 return ret;
234
235 init_put_bits(&pb, pkt->data, pkt->size);
236
237 ff_mjpeg_encode_picture_header(avctx, &pb, pict, NULL, NULL,
238 s->pred, NULL, NULL, 0);
239
240 header_bits = put_bits_count(&pb);
241
242 if( avctx->pix_fmt == AV_PIX_FMT_BGR0
243 || avctx->pix_fmt == AV_PIX_FMT_BGRA
244 || avctx->pix_fmt == AV_PIX_FMT_BGR24)
245 ret = ljpeg_encode_bgr(avctx, &pb, pict);
246 else
247 ret = ljpeg_encode_yuv(avctx, &pb, pict);
248 if (ret < 0)
249 return ret;
250
251 ff_mjpeg_escape_FF(&pb, header_bits >> 3);
252 ff_mjpeg_encode_picture_trailer(&pb, header_bits);
253
254 flush_put_bits(&pb);
255 pkt->size = put_bytes_output(&pb);
256 *got_packet = 1;
257
258 return 0;
259}
260
262{
263 LJpegEncContext *s = avctx->priv_data;
264
265 av_freep(&s->scratch);
266
267 return 0;
268}
269
271{
272 int ret = ff_mjpeg_encode_check_pix_fmt(avctx);
273 LJpegEncContext *s = avctx->priv_data;
274
275 if (ret < 0)
276 return ret;
277
278 s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
279 if (!s->scratch)
280 return AVERROR(ENOMEM);
281
282 ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
283
284 ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
285 s->huff_code_dc_luminance,
288 ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
289 s->huff_code_dc_chrominance,
292
293 return 0;
294}
295
296#define OFFSET(x) offsetof(LJpegEncContext, x)
297#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
298static const AVOption options[] = {
299{ "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, .unit = "pred" },
300 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "pred" },
301 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, .unit = "pred" },
302 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, .unit = "pred" },
303
304 { NULL},
305};
306
307static const AVClass ljpeg_class = {
308 .class_name = "ljpeg",
309 .item_name = av_default_item_name,
310 .option = options,
311 .version = LIBAVUTIL_VERSION_INT,
312};
313
const FFCodec ff_ljpeg_encoder
Definition ljpegenc.c:314
#define VE
Definition amfenc_av1.c:30
Libavcodec external API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static AVFrame * frame
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
Definition encode.h:34
static void predictor(uint8_t *src, ptrdiff_t size)
Definition exrenc.c:170
reference-counted frame API
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_LJPEG
Definition codec_id.h:59
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
FF_VISIBILITY_PUSH_HIDDEN const uint8_t ff_mjpeg_bits_dc_luminance[]
Definition jpegtabs.h:32
const uint8_t ff_mjpeg_bits_dc_chrominance[]
Definition jpegtabs.h:37
const uint8_t ff_mjpeg_val_dc[]
Definition jpegtabs.h:34
#define av_cold
Definition attributes.h:117
static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
Definition ljpegenc.c:59
static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
Definition ljpegenc.c:261
static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
Definition ljpegenc.c:270
static void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb, const AVFrame *frame, int predictor, int mb_x, int mb_y)
Definition ljpegenc.c:121
static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame)
Definition ljpegenc.c:186
static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition ljpegenc.c:208
static const AVClass ljpeg_class
Definition ljpegenc.c:307
#define OFFSET(x)
Definition ljpegenc.c:296
Memory handling functions.
MJPEG encoder and decoder.
#define PREDICT(ret, topleft, top, left, predictor)
Definition mjpeg.h:118
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, const uint8_t huff_size[], const uint16_t huff_code[])
void ff_mjpeg_init_hvsample(const AVCodecContext *avctx, int hsample[4], int vsample[4])
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, const AVFrame *frame, const struct MJpegContext *m, const uint8_t intra_matrix_permutation[64], int pred, const uint16_t luma_intra_matrix[64], const uint16_t chroma_intra_matrix[64], int use_slices)
int ff_mjpeg_add_icc_profile_size(AVCodecContext *avctx, const AVFrame *frame, size_t *max_pkt_size)
int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx)
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table, const uint8_t *val_table)
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
AVOptions.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition pixfmt.h:265
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static int put_bits_count(PutBitContext *s)
Definition put_bits.h:90
static int put_bytes_left(const PutBitContext *s, int round_up)
Definition put_bits.h:145
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static const float pred[4]
Definition siprdata.h:259
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
uint16_t huff_code_dc_chrominance[12]
Definition ljpegenc.c:51
int hsample[4]
Definition ljpegenc.c:48
uint8_t huff_size_dc_luminance[12]
Definition ljpegenc.c:52
uint16_t(* scratch)[4]
Definition ljpegenc.c:55
int vsample[4]
Definition ljpegenc.c:47
uint8_t huff_size_dc_chrominance[12]
Definition ljpegenc.c:53
uint16_t huff_code_dc_luminance[12]
Definition ljpegenc.c:50
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
static char buffer[20]
Definition seek.c:32
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)