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