/src/ffmpeg/libavcodec/ljpegenc.c
Line | Count | Source |
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 | | |
44 | | typedef struct LJpegEncContext { |
45 | | AVClass *class; |
46 | | |
47 | | int vsample[4]; |
48 | | int hsample[4]; |
49 | | |
50 | | uint16_t huff_code_dc_luminance[12]; |
51 | | uint16_t huff_code_dc_chrominance[12]; |
52 | | uint8_t huff_size_dc_luminance[12]; |
53 | | uint8_t huff_size_dc_chrominance[12]; |
54 | | |
55 | | uint16_t (*scratch)[4]; |
56 | | int pred; |
57 | | } LJpegEncContext; |
58 | | |
59 | | static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb, |
60 | | const AVFrame *frame) |
61 | 8.67k | { |
62 | 8.67k | LJpegEncContext *s = avctx->priv_data; |
63 | 8.67k | const int width = frame->width; |
64 | 8.67k | const int height = frame->height; |
65 | 8.67k | const int linesize = frame->linesize[0]; |
66 | 8.67k | uint16_t (*buffer)[4] = s->scratch; |
67 | 8.67k | int left[4], top[4], topleft[4]; |
68 | 8.67k | int x, y, i; |
69 | | |
70 | 43.3k | for (i = 0; i < 4; i++) |
71 | 34.7k | buffer[0][i] = 1 << (9 - 1); |
72 | | |
73 | 448k | for (y = 0; y < height; y++) { |
74 | 440k | const int modified_predictor = y ? s->pred : 1; |
75 | 440k | const uint8_t *ptr = frame->data[0] + (linesize * y); |
76 | | |
77 | 440k | if (put_bytes_left(pb, 0) < width * 4 * 4) { |
78 | 15 | av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n"); |
79 | 15 | return -1; |
80 | 15 | } |
81 | | |
82 | 2.20M | for (i = 0; i < 4; i++) |
83 | 1.76M | top[i]= left[i]= topleft[i]= buffer[0][i]; |
84 | | |
85 | 17.4M | for (x = 0; x < width; x++) { |
86 | 16.9M | if(avctx->pix_fmt == AV_PIX_FMT_BGR24){ |
87 | 2.49M | buffer[x][1] = ptr[3 * x + 0] - ptr[3 * x + 1] + 0x100; |
88 | 2.49M | buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100; |
89 | 2.49M | buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2; |
90 | 14.4M | }else{ |
91 | 14.4M | buffer[x][1] = ptr[4 * x + 0] - ptr[4 * x + 1] + 0x100; |
92 | 14.4M | buffer[x][2] = ptr[4 * x + 2] - ptr[4 * x + 1] + 0x100; |
93 | 14.4M | buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2; |
94 | 14.4M | if (avctx->pix_fmt == AV_PIX_FMT_BGRA) |
95 | 13.3M | buffer[x][3] = ptr[4 * x + 3]; |
96 | 14.4M | } |
97 | | |
98 | 81.3M | for (i = 0; i < 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA); i++) { |
99 | 64.3M | int pred, diff; |
100 | | |
101 | 64.3M | PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); |
102 | | |
103 | 64.3M | topleft[i] = top[i]; |
104 | 64.3M | top[i] = buffer[x+1][i]; |
105 | | |
106 | 64.3M | left[i] = buffer[x][i]; |
107 | | |
108 | 64.3M | diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100; |
109 | | |
110 | 64.3M | if (i == 0 || i == 3) |
111 | 30.3M | ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly |
112 | 33.9M | else |
113 | 33.9M | ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance); |
114 | 64.3M | } |
115 | 16.9M | } |
116 | 440k | } |
117 | | |
118 | 8.66k | return 0; |
119 | 8.67k | } |
120 | | |
121 | | static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb, |
122 | | const AVFrame *frame, int predictor, |
123 | | int mb_x, int mb_y) |
124 | 2.08M | { |
125 | 2.08M | int i; |
126 | | |
127 | 2.08M | if (mb_x == 0 || mb_y == 0) { |
128 | 3.63M | for (i = 0; i < 3; i++) { |
129 | 2.72M | const uint8_t *ptr; |
130 | 2.72M | int x, y, h, v, linesize; |
131 | 2.72M | h = s->hsample[i]; |
132 | 2.72M | v = s->vsample[i]; |
133 | 2.72M | linesize = frame->linesize[i]; |
134 | | |
135 | 7.80M | for (y = 0; y < v; y++) { |
136 | 11.2M | for (x = 0; x < h; x++) { |
137 | 6.17M | int pred; |
138 | | |
139 | 6.17M | ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap |
140 | 6.17M | if (y == 0 && mb_y == 0) { |
141 | 1.85M | if (x == 0 && mb_x == 0) |
142 | 8.01k | pred = 128; |
143 | 1.84M | else |
144 | 1.84M | pred = ptr[-1]; |
145 | 4.32M | } else { |
146 | 4.32M | if (x == 0 && mb_x == 0) { |
147 | 2.09M | pred = ptr[-linesize]; |
148 | 2.23M | } else { |
149 | 2.23M | PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], |
150 | 2.23M | ptr[-1], predictor); |
151 | 2.23M | } |
152 | 4.32M | } |
153 | | |
154 | 6.17M | if (i == 0) |
155 | 2.91M | ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly |
156 | 3.26M | else |
157 | 3.26M | ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance); |
158 | 6.17M | } |
159 | 5.08M | } |
160 | 2.72M | } |
161 | 1.18M | } else { |
162 | 4.72M | for (i = 0; i < 3; i++) { |
163 | 3.54M | const uint8_t *ptr; |
164 | 3.54M | int x, y, h, v, linesize; |
165 | 3.54M | h = s->hsample[i]; |
166 | 3.54M | v = s->vsample[i]; |
167 | 3.54M | linesize = frame->linesize[i]; |
168 | | |
169 | 10.1M | for (y = 0; y < v; y++) { |
170 | 14.2M | for (x = 0; x < h; x++) { |
171 | 7.69M | int pred; |
172 | | |
173 | 7.69M | ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap |
174 | 7.69M | PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor); |
175 | | |
176 | 7.69M | if (i == 0) |
177 | 3.47M | ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly |
178 | 4.21M | else |
179 | 4.21M | ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance); |
180 | 7.69M | } |
181 | 6.57M | } |
182 | 3.54M | } |
183 | 1.18M | } |
184 | 2.08M | } |
185 | | |
186 | | static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb, |
187 | | const AVFrame *frame) |
188 | 2.67k | { |
189 | 2.67k | LJpegEncContext *s = avctx->priv_data; |
190 | 2.67k | const int mb_width = (avctx->width + s->hsample[0] - 1) / s->hsample[0]; |
191 | 2.67k | const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0]; |
192 | 2.67k | int mb_x, mb_y; |
193 | | |
194 | 390k | for (mb_y = 0; mb_y < mb_height; mb_y++) { |
195 | 387k | if (put_bytes_left(pb, 0) < |
196 | 387k | mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) { |
197 | 0 | av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n"); |
198 | 0 | return -1; |
199 | 0 | } |
200 | | |
201 | 2.47M | for (mb_x = 0; mb_x < mb_width; mb_x++) |
202 | 2.08M | ljpeg_encode_yuv_mb(s, pb, frame, s->pred, mb_x, mb_y); |
203 | 387k | } |
204 | | |
205 | 2.67k | return 0; |
206 | 2.67k | } |
207 | | |
208 | | static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
209 | | const AVFrame *pict, int *got_packet) |
210 | 11.3k | { |
211 | 11.3k | LJpegEncContext *s = avctx->priv_data; |
212 | 11.3k | PutBitContext pb; |
213 | 11.3k | const int width = avctx->width; |
214 | 11.3k | const int height = avctx->height; |
215 | 11.3k | const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0]; |
216 | 11.3k | const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0]; |
217 | 11.3k | size_t max_pkt_size = FF_INPUT_BUFFER_MIN_SIZE; |
218 | 11.3k | int ret, header_bits; |
219 | | |
220 | 11.3k | if( avctx->pix_fmt == AV_PIX_FMT_BGR0 |
221 | 8.84k | || avctx->pix_fmt == AV_PIX_FMT_BGR24) |
222 | 2.77k | max_pkt_size += width * height * 3 * 4; |
223 | 8.57k | else if(avctx->pix_fmt == AV_PIX_FMT_BGRA) |
224 | 5.90k | max_pkt_size += width * height * 4 * 4; |
225 | 2.67k | else { |
226 | 2.67k | max_pkt_size += mb_width * mb_height * 3 * 4 |
227 | 2.67k | * s->hsample[0] * s->vsample[0]; |
228 | 2.67k | } |
229 | | |
230 | 11.3k | if ((ret = ff_mjpeg_add_icc_profile_size(avctx, pict, &max_pkt_size)) < 0) |
231 | 0 | return ret; |
232 | 11.3k | if ((ret = ff_alloc_packet(avctx, pkt, max_pkt_size)) < 0) |
233 | 0 | return ret; |
234 | | |
235 | 11.3k | init_put_bits(&pb, pkt->data, pkt->size); |
236 | | |
237 | 11.3k | ff_mjpeg_encode_picture_header(avctx, &pb, pict, NULL, NULL, |
238 | 11.3k | s->pred, NULL, NULL, 0); |
239 | | |
240 | 11.3k | header_bits = put_bits_count(&pb); |
241 | | |
242 | 11.3k | if( avctx->pix_fmt == AV_PIX_FMT_BGR0 |
243 | 8.84k | || avctx->pix_fmt == AV_PIX_FMT_BGRA |
244 | 2.93k | || avctx->pix_fmt == AV_PIX_FMT_BGR24) |
245 | 8.67k | ret = ljpeg_encode_bgr(avctx, &pb, pict); |
246 | 2.67k | else |
247 | 2.67k | ret = ljpeg_encode_yuv(avctx, &pb, pict); |
248 | 11.3k | if (ret < 0) |
249 | 15 | return ret; |
250 | | |
251 | 11.3k | ff_mjpeg_escape_FF(&pb, header_bits >> 3); |
252 | 11.3k | ff_mjpeg_encode_picture_trailer(&pb, header_bits); |
253 | | |
254 | 11.3k | flush_put_bits(&pb); |
255 | 11.3k | pkt->size = put_bytes_output(&pb); |
256 | 11.3k | *got_packet = 1; |
257 | | |
258 | 11.3k | return 0; |
259 | 11.3k | } |
260 | | |
261 | | static av_cold int ljpeg_encode_close(AVCodecContext *avctx) |
262 | 400 | { |
263 | 400 | LJpegEncContext *s = avctx->priv_data; |
264 | | |
265 | 400 | av_freep(&s->scratch); |
266 | | |
267 | 400 | return 0; |
268 | 400 | } |
269 | | |
270 | | static av_cold int ljpeg_encode_init(AVCodecContext *avctx) |
271 | 405 | { |
272 | 405 | int ret = ff_mjpeg_encode_check_pix_fmt(avctx); |
273 | 405 | LJpegEncContext *s = avctx->priv_data; |
274 | | |
275 | 405 | if (ret < 0) |
276 | 5 | return ret; |
277 | | |
278 | 400 | s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch)); |
279 | 400 | if (!s->scratch) |
280 | 0 | return AVERROR(ENOMEM); |
281 | | |
282 | 400 | ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample); |
283 | | |
284 | 400 | ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance, |
285 | 400 | s->huff_code_dc_luminance, |
286 | 400 | ff_mjpeg_bits_dc_luminance, |
287 | 400 | ff_mjpeg_val_dc); |
288 | 400 | ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance, |
289 | 400 | s->huff_code_dc_chrominance, |
290 | 400 | ff_mjpeg_bits_dc_chrominance, |
291 | 400 | ff_mjpeg_val_dc); |
292 | | |
293 | 400 | return 0; |
294 | 400 | } |
295 | | |
296 | | #define OFFSET(x) offsetof(LJpegEncContext, x) |
297 | | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM |
298 | | static 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 | | |
307 | | static 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 | | |
314 | | const FFCodec ff_ljpeg_encoder = { |
315 | | .p.name = "ljpeg", |
316 | | CODEC_LONG_NAME("Lossless JPEG"), |
317 | | .p.type = AVMEDIA_TYPE_VIDEO, |
318 | | .p.id = AV_CODEC_ID_LJPEG, |
319 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | |
320 | | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
321 | | .priv_data_size = sizeof(LJpegEncContext), |
322 | | .p.priv_class = &ljpeg_class, |
323 | | .init = ljpeg_encode_init, |
324 | | FF_CODEC_ENCODE_CB(ljpeg_encode_frame), |
325 | | .close = ljpeg_encode_close, |
326 | | CODEC_PIXFMTS(AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0, |
327 | | AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, |
328 | | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P), |
329 | | .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG, |
330 | | }; |