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