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