]> git.sesse.net Git - ffmpeg/blob - libavcodec/ljpegenc.c
avcodec: Constify AVCodecs
[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 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 #include "libavutil/pixdesc.h"
37
38 #include "avcodec.h"
39 #include "idctdsp.h"
40 #include "internal.h"
41 #include "jpegtables.h"
42 #include "mathops.h"
43 #include "mjpegenc_common.h"
44 #include "mjpeg.h"
45
46 typedef struct LJpegEncContext {
47     AVClass *class;
48     IDCTDSPContext idsp;
49     ScanTable scantable;
50     uint16_t matrix[64];
51
52     int vsample[4];
53     int hsample[4];
54
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];
59
60     uint16_t (*scratch)[4];
61     int pred;
62 } LJpegEncContext;
63
64 static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
65                             const AVFrame *frame)
66 {
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];
73     int x, y, i;
74
75     for (i = 0; i < 4; i++)
76         buffer[0][i] = 1 << (9 - 1);
77
78     for (y = 0; y < height; y++) {
79         const int modified_predictor = y ? s->pred : 1;
80         uint8_t *ptr = frame->data[0] + (linesize * y);
81
82         if (put_bytes_left(pb, 0) < width * 4 * 4) {
83             av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
84             return -1;
85         }
86
87         for (i = 0; i < 4; i++)
88             top[i]= left[i]= topleft[i]= buffer[0][i];
89
90         for (x = 0; x < width; x++) {
91             if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
92                 buffer[x][1] =  ptr[3 * x + 0] -     ptr[3 * x + 1] + 0x100;
93                 buffer[x][2] =  ptr[3 * x + 2] -     ptr[3 * x + 1] + 0x100;
94                 buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
95             }else{
96                 buffer[x][1] =  ptr[4 * x + 0] -     ptr[4 * x + 1] + 0x100;
97                 buffer[x][2] =  ptr[4 * x + 2] -     ptr[4 * x + 1] + 0x100;
98                 buffer[x][0] = (ptr[4 * x + 0] + 2 * ptr[4 * x + 1] + ptr[4 * x + 2]) >> 2;
99                 if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
100                     buffer[x][3] =  ptr[4 * x + 3];
101             }
102
103             for (i = 0; i < 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA); i++) {
104                 int pred, diff;
105
106                 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
107
108                 topleft[i] = top[i];
109                 top[i]     = buffer[x+1][i];
110
111                 left[i]    = buffer[x][i];
112
113                 diff       = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
114
115                 if (i == 0 || i == 3)
116                     ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
117                 else
118                     ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
119             }
120         }
121     }
122
123     return 0;
124 }
125
126 static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
127                                        const AVFrame *frame, int predictor,
128                                        int mb_x, int mb_y)
129 {
130     int i;
131
132     if (mb_x == 0 || mb_y == 0) {
133         for (i = 0; i < 3; i++) {
134             uint8_t *ptr;
135             int x, y, h, v, linesize;
136             h = s->hsample[i];
137             v = s->vsample[i];
138             linesize = frame->linesize[i];
139
140             for (y = 0; y < v; y++) {
141                 for (x = 0; x < h; x++) {
142                     int pred;
143
144                     ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
145                     if (y == 0 && mb_y == 0) {
146                         if (x == 0 && mb_x == 0)
147                             pred = 128;
148                         else
149                             pred = ptr[-1];
150                     } else {
151                         if (x == 0 && mb_x == 0) {
152                             pred = ptr[-linesize];
153                         } else {
154                             PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
155                                     ptr[-1], predictor);
156                         }
157                     }
158
159                     if (i == 0)
160                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
161                     else
162                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
163                 }
164             }
165         }
166     } else {
167         for (i = 0; i < 3; i++) {
168             uint8_t *ptr;
169             int x, y, h, v, linesize;
170             h = s->hsample[i];
171             v = s->vsample[i];
172             linesize = frame->linesize[i];
173
174             for (y = 0; y < v; y++) {
175                 for (x = 0; x < h; x++) {
176                     int pred;
177
178                     ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
179                     PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
180
181                     if (i == 0)
182                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
183                     else
184                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
185                 }
186             }
187         }
188     }
189 }
190
191 static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
192                             const AVFrame *frame)
193 {
194     LJpegEncContext *s  = avctx->priv_data;
195     const int mb_width  = (avctx->width  + s->hsample[0] - 1) / s->hsample[0];
196     const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
197     int mb_x, mb_y;
198
199     for (mb_y = 0; mb_y < mb_height; mb_y++) {
200         if (put_bytes_left(pb, 0) <
201             mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
202             av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
203             return -1;
204         }
205
206         for (mb_x = 0; mb_x < mb_width; mb_x++)
207             ljpeg_encode_yuv_mb(s, pb, frame, s->pred, mb_x, mb_y);
208     }
209
210     return 0;
211 }
212
213 static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
214                               const AVFrame *pict, int *got_packet)
215 {
216     LJpegEncContext *s = avctx->priv_data;
217     PutBitContext pb;
218     const int width  = avctx->width;
219     const int height = avctx->height;
220     const int mb_width  = (width  + s->hsample[0] - 1) / s->hsample[0];
221     const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
222     int max_pkt_size = AV_INPUT_BUFFER_MIN_SIZE;
223     int ret, header_bits;
224
225     if(    avctx->pix_fmt == AV_PIX_FMT_BGR0
226         || avctx->pix_fmt == AV_PIX_FMT_BGR24)
227         max_pkt_size += width * height * 3 * 4;
228     else if(avctx->pix_fmt == AV_PIX_FMT_BGRA)
229         max_pkt_size += width * height * 4 * 4;
230     else {
231         max_pkt_size += mb_width * mb_height * 3 * 4
232                         * s->hsample[0] * s->vsample[0];
233     }
234
235     if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size, 0)) < 0)
236         return ret;
237
238     init_put_bits(&pb, pkt->data, pkt->size);
239
240     ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
241                                    s->pred, s->matrix, s->matrix);
242
243     header_bits = put_bits_count(&pb);
244
245     if(    avctx->pix_fmt == AV_PIX_FMT_BGR0
246         || avctx->pix_fmt == AV_PIX_FMT_BGRA
247         || avctx->pix_fmt == AV_PIX_FMT_BGR24)
248         ret = ljpeg_encode_bgr(avctx, &pb, pict);
249     else
250         ret = ljpeg_encode_yuv(avctx, &pb, pict);
251     if (ret < 0)
252         return ret;
253
254     emms_c();
255
256     ff_mjpeg_escape_FF(&pb, header_bits >> 3);
257     ff_mjpeg_encode_picture_trailer(&pb, header_bits);
258
259     flush_put_bits(&pb);
260     pkt->size   = put_bits_ptr(&pb) - pb.buf;
261     pkt->flags |= AV_PKT_FLAG_KEY;
262     *got_packet = 1;
263
264     return 0;
265 }
266
267 static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
268 {
269     LJpegEncContext *s = avctx->priv_data;
270
271     av_freep(&s->scratch);
272
273     return 0;
274 }
275
276 static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
277 {
278     int ret = ff_mjpeg_encode_check_pix_fmt(avctx);
279     LJpegEncContext *s = avctx->priv_data;
280
281     if (ret < 0)
282         return ret;
283
284     s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
285     if (!s->scratch)
286         return AVERROR(ENOMEM);
287
288     ff_idctdsp_init(&s->idsp, avctx);
289     ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
290                       ff_zigzag_direct);
291
292     ff_mjpeg_init_hvsample(avctx, s->hsample, s->vsample);
293
294     ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
295                                  s->huff_code_dc_luminance,
296                                  avpriv_mjpeg_bits_dc_luminance,
297                                  avpriv_mjpeg_val_dc);
298     ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
299                                  s->huff_code_dc_chrominance,
300                                  avpriv_mjpeg_bits_dc_chrominance,
301                                  avpriv_mjpeg_val_dc);
302
303     return 0;
304 }
305
306 #define OFFSET(x) offsetof(LJpegEncContext, x)
307 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
308 static const AVOption options[] = {
309 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
310     { "left",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
311     { "plane",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
312     { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
313
314     { NULL},
315 };
316
317 static const AVClass ljpeg_class = {
318     .class_name = "ljpeg",
319     .item_name  = av_default_item_name,
320     .option     = options,
321     .version    = LIBAVUTIL_VERSION_INT,
322 };
323
324 const AVCodec ff_ljpeg_encoder = {
325     .name           = "ljpeg",
326     .long_name      = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
327     .type           = AVMEDIA_TYPE_VIDEO,
328     .id             = AV_CODEC_ID_LJPEG,
329     .priv_data_size = sizeof(LJpegEncContext),
330     .priv_class     = &ljpeg_class,
331     .init           = ljpeg_encode_init,
332     .encode2        = ljpeg_encode_frame,
333     .close          = ljpeg_encode_close,
334     .capabilities   = AV_CODEC_CAP_FRAME_THREADS,
335     .pix_fmts       = (const enum AVPixelFormat[]){
336         AV_PIX_FMT_BGR24   , AV_PIX_FMT_BGRA    , AV_PIX_FMT_BGR0,
337         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
338         AV_PIX_FMT_YUV420P , AV_PIX_FMT_YUV444P , AV_PIX_FMT_YUV422P,
339         AV_PIX_FMT_NONE},
340 };