]> git.sesse.net Git - ffmpeg/blob - libavcodec/ljpegenc.c
ppc: Centralize compiler-specific altivec.h #include handling in one place
[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 "idctdsp.h"
39 #include "internal.h"
40 #include "jpegtables.h"
41 #include "mjpegenc_common.h"
42 #include "mjpeg.h"
43 #include "mjpegenc.h"
44
45 typedef struct LJpegEncContext {
46     AVClass *class;
47     IDCTDSPContext idsp;
48     ScanTable scantable;
49     uint16_t matrix[64];
50
51     int vsample[3];
52     int hsample[3];
53
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];
58
59     uint16_t (*scratch)[4];
60     int pred;
61 } LJpegEncContext;
62
63 static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
64                             const AVFrame *frame)
65 {
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[3], top[3], topleft[3];
72     int x, y, i;
73
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
79 #endif
80
81     for (i = 0; i < 3; i++)
82         buffer[0][i] = 1 << (9 - 1);
83
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);
87
88         if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 3 * 3) {
89             av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
90             return -1;
91         }
92
93         for (i = 0; i < 3; i++)
94             top[i]= left[i]= topleft[i]= buffer[0][i];
95
96         for (x = 0; x < width; x++) {
97             buffer[x][1] =  ptr[3 * x + 0] -     ptr[3 * x + 1] + 0x100;
98             buffer[x][2] =  ptr[3 * x + 2] -     ptr[3 * x + 1] + 0x100;
99             buffer[x][0] = (ptr[3 * x + 0] + 2 * ptr[3 * x + 1] + ptr[3 * x + 2]) >> 2;
100
101             for (i = 0; i < 3; i++) {
102                 int pred, diff;
103
104                 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
105
106                 topleft[i] = top[i];
107                 top[i]     = buffer[x+1][i];
108
109                 left[i]    = buffer[x][i];
110
111                 diff       = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
112
113                 if (i == 0)
114                     ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
115                 else
116                     ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
117             }
118         }
119     }
120
121     return 0;
122 }
123
124 static inline void ljpeg_encode_yuv_mb(LJpegEncContext *s, PutBitContext *pb,
125                                        const AVFrame *frame, int predictor,
126                                        int mb_x, int mb_y)
127 {
128     int i;
129
130     if (mb_x == 0 || mb_y == 0) {
131         for (i = 0; i < 3; i++) {
132             uint8_t *ptr;
133             int x, y, h, v, linesize;
134             h = s->hsample[i];
135             v = s->vsample[i];
136             linesize = frame->linesize[i];
137
138             for (y = 0; y < v; y++) {
139                 for (x = 0; x < h; x++) {
140                     int pred;
141
142                     ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
143                     if (y == 0 && mb_y == 0) {
144                         if (x == 0 && mb_x == 0)
145                             pred = 128;
146                         else
147                             pred = ptr[-1];
148                     } else {
149                         if (x == 0 && mb_x == 0) {
150                             pred = ptr[-linesize];
151                         } else {
152                             PREDICT(pred, ptr[-linesize - 1], ptr[-linesize],
153                                     ptr[-1], predictor);
154                         }
155                     }
156
157                     if (i == 0)
158                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
159                     else
160                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
161                 }
162             }
163         }
164     } else {
165         for (i = 0; i < 3; i++) {
166             uint8_t *ptr;
167             int x, y, h, v, linesize;
168             h = s->hsample[i];
169             v = s->vsample[i];
170             linesize = frame->linesize[i];
171
172             for (y = 0; y < v; y++) {
173                 for (x = 0; x < h; x++) {
174                     int pred;
175
176                     ptr = frame->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
177                     PREDICT(pred, ptr[-linesize - 1], ptr[-linesize], ptr[-1], predictor);
178
179                     if (i == 0)
180                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
181                     else
182                         ff_mjpeg_encode_dc(pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
183                 }
184             }
185         }
186     }
187 }
188
189 static int ljpeg_encode_yuv(AVCodecContext *avctx, PutBitContext *pb,
190                             const AVFrame *frame)
191 {
192     LJpegEncContext *s  = avctx->priv_data;
193     const int mb_width  = (avctx->width  + s->hsample[0] - 1) / s->hsample[0];
194     const int mb_height = (avctx->height + s->vsample[0] - 1) / s->vsample[0];
195     int mb_x, mb_y;
196
197 #if FF_API_PRIVATE_OPT
198 FF_DISABLE_DEPRECATION_WARNINGS
199     if (avctx->prediction_method)
200         s->pred = avctx->prediction_method + 1;
201 FF_ENABLE_DEPRECATION_WARNINGS
202 #endif
203
204     for (mb_y = 0; mb_y < mb_height; mb_y++) {
205         if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
206             mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
207             av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
208             return -1;
209         }
210
211         for (mb_x = 0; mb_x < mb_width; mb_x++)
212             ljpeg_encode_yuv_mb(s, pb, frame, s->pred, mb_x, mb_y);
213     }
214
215     return 0;
216 }
217
218 static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
219                               const AVFrame *pict, int *got_packet)
220 {
221     LJpegEncContext *s = avctx->priv_data;
222     PutBitContext pb;
223     const int width  = avctx->width;
224     const int height = avctx->height;
225     const int mb_width  = (width  + s->hsample[0] - 1) / s->hsample[0];
226     const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
227     int max_pkt_size = AV_INPUT_BUFFER_MIN_SIZE;
228     int ret, header_bits;
229
230     if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
231         max_pkt_size += width * height * 3 * 3;
232     else {
233         max_pkt_size += mb_width * mb_height * 3 * 4
234                         * s->hsample[0] * s->vsample[0];
235     }
236     if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
237         av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
238         return ret;
239     }
240
241     init_put_bits(&pb, pkt->data, pkt->size);
242
243     ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
244                                    s->pred, s->matrix);
245
246     header_bits = put_bits_count(&pb);
247
248     if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
249         ret = ljpeg_encode_bgr(avctx, &pb, pict);
250     else
251         ret = ljpeg_encode_yuv(avctx, &pb, pict);
252     if (ret < 0)
253         return ret;
254
255     emms_c();
256
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     LJpegEncContext *s = avctx->priv_data;
279     int chroma_v_shift, chroma_h_shift;
280
281     if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
282          avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
283          avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
284          avctx->color_range == AVCOL_RANGE_MPEG) &&
285         avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
286         av_log(avctx, AV_LOG_ERROR,
287                "Limited range YUV is non-standard, set strict_std_compliance to "
288                "at least unofficial to use it.\n");
289         return AVERROR(EINVAL);
290     }
291
292 #if FF_API_CODED_FRAME
293 FF_DISABLE_DEPRECATION_WARNINGS
294     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
295     avctx->coded_frame->key_frame = 1;
296 FF_ENABLE_DEPRECATION_WARNINGS
297 #endif
298
299     s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
300
301     ff_idctdsp_init(&s->idsp, avctx);
302     ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
303                       ff_zigzag_direct);
304
305     av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
306                                      &chroma_v_shift);
307
308     if (avctx->pix_fmt   == AV_PIX_FMT_BGR24) {
309         s->vsample[0] = s->hsample[0] =
310         s->vsample[1] = s->hsample[1] =
311         s->vsample[2] = s->hsample[2] = 1;
312     } else {
313         s->vsample[0] = 2;
314         s->vsample[1] = 2 >> chroma_v_shift;
315         s->vsample[2] = 2 >> chroma_v_shift;
316         s->hsample[0] = 2;
317         s->hsample[1] = 2 >> chroma_h_shift;
318         s->hsample[2] = 2 >> chroma_h_shift;
319     }
320
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);
329
330     return 0;
331 }
332
333 #define OFFSET(x) offsetof(LJpegEncContext, x)
334 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
335 static const AVOption options[] = {
336 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
337     { "left",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
338     { "plane",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
339     { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
340
341     { NULL},
342 };
343
344 static const AVClass ljpeg_class = {
345     .class_name = "ljpeg",
346     .item_name  = av_default_item_name,
347     .option     = options,
348     .version    = LIBAVUTIL_VERSION_INT,
349 };
350
351 AVCodec ff_ljpeg_encoder = {
352     .name           = "ljpeg",
353     .long_name      = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
354     .type           = AVMEDIA_TYPE_VIDEO,
355     .id             = AV_CODEC_ID_LJPEG,
356     .priv_data_size = sizeof(LJpegEncContext),
357     .priv_class     = &ljpeg_class,
358     .init           = ljpeg_encode_init,
359     .encode2        = ljpeg_encode_frame,
360     .close          = ljpeg_encode_close,
361     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ420P,
362                                                     AV_PIX_FMT_YUVJ422P,
363                                                     AV_PIX_FMT_YUVJ444P,
364                                                     AV_PIX_FMT_BGR24,
365                                                     AV_PIX_FMT_YUV420P,
366                                                     AV_PIX_FMT_YUV422P,
367                                                     AV_PIX_FMT_YUV444P,
368                                                     AV_PIX_FMT_NONE },
369 };