]> git.sesse.net Git - ffmpeg/blob - libavcodec/ljpegenc.c
mjpeg: Split off bits shared by MJPEG and LJPEG encoders
[ffmpeg] / libavcodec / ljpegenc.c
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 Libav.
12  *
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.
17  *
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.
22  *
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
26  */
27
28 /**
29  * @file
30  * lossless JPEG encoder.
31  */
32
33 #include "libavutil/frame.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/pixdesc.h"
36
37 #include "avcodec.h"
38 #include "dsputil.h"
39 #include "internal.h"
40 #include "mjpegenc_common.h"
41 #include "mpegvideo.h"
42 #include "mjpeg.h"
43 #include "mjpegenc.h"
44
45 typedef struct LJpegEncContext {
46     DSPContext dsp;
47     ScanTable scantable;
48     uint16_t matrix[64];
49
50     int vsample[3];
51     int hsample[3];
52
53     uint16_t huff_code_dc_luminance[12];
54     uint16_t huff_code_dc_chrominance[12];
55     uint8_t  huff_size_dc_luminance[12];
56     uint8_t  huff_size_dc_chrominance[12];
57
58     uint16_t (*scratch)[4];
59 } LJpegEncContext;
60
61 static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
62                             const AVFrame *frame)
63 {
64     LJpegEncContext *s    = avctx->priv_data;
65     const int width       = frame->width;
66     const int height      = frame->height;
67     const int linesize    = frame->linesize[0];
68     uint16_t (*buffer)[4] = s->scratch;
69     const int predictor   = avctx->prediction_method+1;
70     int left[3], top[3], topleft[3];
71     int x, y, i;
72
73     for (i = 0; i < 3; i++)
74         buffer[0][i] = 1 << (9 - 1);
75
76     for (y = 0; y < height; y++) {
77         const int modified_predictor = y ? predictor : 1;
78         uint8_t *ptr = frame->data[0] + (linesize * y);
79
80         if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 3 * 3) {
81             av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
82             return -1;
83         }
84
85         for (i = 0; i < 3; i++)
86             top[i]= left[i]= topleft[i]= buffer[0][i];
87
88         for (x = 0; x < width; x++) {
89             buffer[x][1] =  ptr[3 * x + 0] -     ptr[3 * x + 1] + 0x100;
90             buffer[x][2] =  ptr[3 * x + 2] -     ptr[3 * x + 1] + 0x100;
91             buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
92
93             for (i = 0; i < 3; i++) {
94                 int pred, diff;
95
96                 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
97
98                 topleft[i] = top[i];
99                 top[i]     = buffer[x+1][i];
100
101                 left[i]    = buffer[x][i];
102
103                 diff       = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
104
105                 if (i == 0)
106                     ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
107                 else
108                     ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
109             }
110         }
111     }
112
113     return 0;
114 }
115
116 static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
117                                        const AVFrame *frame, int predictor,
118                                        int mb_x, int mb_y)
119 {
120     int i;
121
122     if (mb_x == 0 || mb_y == 0) {
123         for (i = 0; i < 3; i++) {
124             uint8_t *ptr;
125             int x, y, h, v, linesize;
126             h = s->hsample[i];
127             v = s->vsample[i];
128             linesize = frame->linesize[i];
129
130             for (y = 0; y < v; y++) {
131                 for (x = 0; x < h; x++) {
132                     int pred;
133
134                     ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
135                     if (y == 0 && mb_y == 0) {
136                         if (x == 0 && mb_x == 0)
137                             pred = 128;
138                         else
139                             pred = ptr[-1];
140                     } else {
141                         if (x == 0 && mb_x == 0) {
142                             pred = ptr[-linesize];
143                         } else {
144                             PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
145                                     ptr[-1], predictor);
146                         }
147                     }
148
149                     if (i == 0)
150                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
151                     else
152                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
153                 }
154             }
155         }
156     } else {
157         for (i = 0; i < 3; i++) {
158             uint8_t *ptr;
159             int x, y, h, v, linesize;
160             h = s->hsample[i];
161             v = s->vsample[i];
162             linesize = frame->linesize[i];
163
164             for (y = 0; y < v; y++) {
165                 for (x = 0; x < h; x++) {
166                     int pred;
167
168                     ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
169                     PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
170
171                     if (i == 0)
172                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
173                     else
174                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
175                 }
176             }
177         }
178     }
179 }
180
181 static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
182                             const AVFrame *frame)
183 {
184     const int predictor = avctx->prediction_method + 1;
185     LJpegEncContext *s  = avctx->priv_data;
186     const int mb_width  = (avctx->width  + s->hsample[0] - 1) / s->hsample[0];
187     const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
188     int mb_x, mb_y;
189
190     for (mb_y = 0; mb_y < mb_height; mb_y++) {
191         if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
192             mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
193             av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
194             return -1;
195         }
196
197         for (mb_x = 0; mb_x < mb_width; mb_x++)
198             ljpeg_encode_yuv_mb(s, pb, frame, predictor, mb_x, mb_y);
199     }
200
201     return 0;
202 }
203
204 static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
205                               const AVFrame *pict, int *got_packet)
206 {
207     LJpegEncContext *s = avctx->priv_data;
208     PutBitContext pb;
209     const int width  = avctx->width;
210     const int height = avctx->height;
211     const int mb_width  = (width  + s->hsample[0] - 1) / s->hsample[0];
212     const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
213     int max_pkt_size = FF_MIN_BUFFER_SIZE;
214     int ret, header_bits;
215
216     if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
217         max_pkt_size += width * height * 3 * 3;
218     else {
219         max_pkt_size += mb_width * mb_height * 3 * 4
220                         * s->hsample[0] * s->vsample[0];
221     }
222     if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
223         av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
224         return ret;
225     }
226
227     init_put_bits(&pb, pkt->data, pkt->size);
228
229     ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
230                                    s->matrix);
231
232     header_bits = put_bits_count(&pb);
233
234     if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
235         ret = ljpeg_encode_bgr(avctx, &pb, pict);
236     else
237         ret = ljpeg_encode_yuv(avctx, &pb, pict);
238     if (ret < 0)
239         return ret;
240
241     emms_c();
242
243     ff_mjpeg_encode_picture_trailer(&pb, header_bits);
244
245     flush_put_bits(&pb);
246     pkt->size   = put_bits_ptr(&pb) - pb.buf;
247     pkt->flags |= AV_PKT_FLAG_KEY;
248     *got_packet = 1;
249
250     return 0;
251 }
252
253 static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
254 {
255     LJpegEncContext *s = avctx->priv_data;
256
257     av_frame_free(&avctx->coded_frame);
258     av_freep(&s->scratch);
259
260     return 0;
261 }
262
263 static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
264 {
265     LJpegEncContext *s = avctx->priv_data;
266     int chroma_v_shift, chroma_h_shift;
267
268     if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
269          avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
270          avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
271          avctx->color_range == AVCOL_RANGE_MPEG) &&
272         avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
273         av_log(avctx, AV_LOG_ERROR,
274                "Limited range YUV is non-standard, set strict_std_compliance to "
275                "at least unofficial to use it.\n");
276         return AVERROR(EINVAL);
277     }
278
279     avctx->coded_frame = av_frame_alloc();
280     if (!avctx->coded_frame)
281         return AVERROR(ENOMEM);
282
283     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
284     avctx->coded_frame->key_frame = 1;
285
286     s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
287
288     ff_dsputil_init(&s->dsp, avctx);
289     ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
290
291     av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
292                                      &chroma_v_shift);
293
294     if (avctx->pix_fmt   == AV_PIX_FMT_BGR24) {
295         s->vsample[0] = s->hsample[0] =
296         s->vsample[1] = s->hsample[1] =
297         s->vsample[2] = s->hsample[2] = 1;
298     } else {
299         s->vsample[0] = 2;
300         s->vsample[1] = 2 >> chroma_v_shift;
301         s->vsample[2] = 2 >> chroma_v_shift;
302         s->hsample[0] = 2;
303         s->hsample[1] = 2 >> chroma_h_shift;
304         s->hsample[2] = 2 >> chroma_h_shift;
305     }
306
307     ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
308                                  s->huff_code_dc_luminance,
309                                  avpriv_mjpeg_bits_dc_luminance,
310                                  avpriv_mjpeg_val_dc);
311     ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
312                                  s->huff_code_dc_chrominance,
313                                  avpriv_mjpeg_bits_dc_chrominance,
314                                  avpriv_mjpeg_val_dc);
315
316     return 0;
317 }
318
319 AVCodec ff_ljpeg_encoder = {
320     .name           = "ljpeg",
321     .long_name      = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
322     .type           = AVMEDIA_TYPE_VIDEO,
323     .id             = AV_CODEC_ID_LJPEG,
324     .priv_data_size = sizeof(LJpegEncContext),
325     .init           = ljpeg_encode_init,
326     .encode2        = ljpeg_encode_frame,
327     .close          = ljpeg_encode_close,
328     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ420P,
329                                                     AV_PIX_FMT_YUVJ422P,
330                                                     AV_PIX_FMT_YUVJ444P,
331                                                     AV_PIX_FMT_BGR24,
332                                                     AV_PIX_FMT_YUV420P,
333                                                     AV_PIX_FMT_YUV422P,
334                                                     AV_PIX_FMT_YUV444P,
335                                                     AV_PIX_FMT_NONE },
336 };