7e536a88df32b9f720601d94f550261a282e4851
[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 "avcodec.h"
34 #include "internal.h"
35 #include "mpegvideo.h"
36 #include "mjpeg.h"
37 #include "mjpegenc.h"
38
39
40 static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
41                                    const AVFrame *pict, int *got_packet)
42 {
43     MpegEncContext * const s = avctx->priv_data;
44     MJpegContext * const m = s->mjpeg_ctx;
45     const int width= s->width;
46     const int height= s->height;
47     AVFrame * const p = &s->current_picture.f;
48     const int predictor= avctx->prediction_method+1;
49     const int mb_width  = (width  + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
50     const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
51     int ret, max_pkt_size = FF_MIN_BUFFER_SIZE;
52
53     if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
54         max_pkt_size += width * height * 3 * 4;
55     else {
56         max_pkt_size += mb_width * mb_height * 3 * 4
57                         * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
58     }
59
60     if (!s->rd_scratchpad) {
61         int alloc_size = FFALIGN(FFABS(pict->linesize[0]) + 64, 32);
62         s->me.scratchpad =
63         s->rd_scratchpad = av_mallocz(alloc_size * 4 * 16 * 2);
64         if (!s->rd_scratchpad) {
65             av_log(avctx, AV_LOG_ERROR, "failed to allocate context scratch buffers.\n");
66             return AVERROR(ENOMEM);
67         }
68     }
69
70     if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
71         return ret;
72
73     init_put_bits(&s->pb, pkt->data, pkt->size);
74
75     av_frame_unref(p);
76     ret = av_frame_ref(p, pict);
77     if (ret < 0)
78         return ret;
79     p->pict_type= AV_PICTURE_TYPE_I;
80     p->key_frame= 1;
81
82     ff_mjpeg_encode_picture_header(avctx, &s->pb, &s->intra_scantable,
83                                    s->intra_matrix);
84
85     s->header_bits= put_bits_count(&s->pb);
86
87     if(avctx->pix_fmt == AV_PIX_FMT_BGR0
88         || avctx->pix_fmt == AV_PIX_FMT_BGRA
89         || avctx->pix_fmt == AV_PIX_FMT_BGR24){
90         int x, y, i;
91         const int linesize= p->linesize[0];
92         uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
93         int left[3], top[3], topleft[3];
94
95         for(i=0; i<3; i++){
96             buffer[0][i]= 1 << (9 - 1);
97         }
98
99         for(y = 0; y < height; y++) {
100             const int modified_predictor= y ? predictor : 1;
101             uint8_t *ptr = p->data[0] + (linesize * y);
102
103             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
104                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
105                 return -1;
106             }
107
108             for(i=0; i<3; i++){
109                 top[i]= left[i]= topleft[i]= buffer[0][i];
110             }
111             for(x = 0; x < width; x++) {
112                 if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
113                     buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
114                     buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
115                     buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
116                 }else{
117                 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
118                 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
119                 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
120                 }
121
122                 for(i=0;i<3;i++) {
123                     int pred, diff;
124
125                     PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
126
127                     topleft[i]= top[i];
128                     top[i]= buffer[x+1][i];
129
130                     left[i]= buffer[x][i];
131
132                     diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
133
134                     if(i==0)
135                         ff_mjpeg_encode_dc(&s->pb, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
136                     else
137                         ff_mjpeg_encode_dc(&s->pb, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
138                 }
139             }
140         }
141     }else{
142         int mb_x, mb_y, i;
143
144         for(mb_y = 0; mb_y < mb_height; mb_y++) {
145             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
146                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
147                 return -1;
148             }
149             for(mb_x = 0; mb_x < mb_width; mb_x++) {
150                 if(mb_x==0 || mb_y==0){
151                     for(i=0;i<3;i++) {
152                         uint8_t *ptr;
153                         int x, y, h, v, linesize;
154                         h = s->mjpeg_hsample[i];
155                         v = s->mjpeg_vsample[i];
156                         linesize= p->linesize[i];
157
158                         for(y=0; y<v; y++){
159                             for(x=0; x<h; x++){
160                                 int pred;
161
162                                 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
163                                 if(y==0 && mb_y==0){
164                                     if(x==0 && mb_x==0){
165                                         pred= 128;
166                                     }else{
167                                         pred= ptr[-1];
168                                     }
169                                 }else{
170                                     if(x==0 && mb_x==0){
171                                         pred= ptr[-linesize];
172                                     }else{
173                                         PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
174                                     }
175                                 }
176
177                                 if(i==0)
178                                     ff_mjpeg_encode_dc(&s->pb, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
179                                 else
180                                     ff_mjpeg_encode_dc(&s->pb, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
181                             }
182                         }
183                     }
184                 }else{
185                     for(i=0;i<3;i++) {
186                         uint8_t *ptr;
187                         int x, y, h, v, linesize;
188                         h = s->mjpeg_hsample[i];
189                         v = s->mjpeg_vsample[i];
190                         linesize = pict->linesize[i];
191
192                         for(y=0; y<v; y++){
193                             for(x=0; x<h; x++){
194                                 int pred;
195
196                                 ptr = pict->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
197                                 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
198
199                                 if(i==0)
200                                     ff_mjpeg_encode_dc(&s->pb, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
201                                 else
202                                     ff_mjpeg_encode_dc(&s->pb, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
203                             }
204                         }
205                     }
206                 }
207             }
208         }
209     }
210
211     emms_c();
212     av_assert0(s->esc_pos == s->header_bits >> 3);
213
214     ff_mjpeg_encode_stuffing(s);
215     ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
216     s->picture_number++;
217
218     flush_put_bits(&s->pb);
219     pkt->size   = put_bits_ptr(&s->pb) - s->pb.buf;
220     pkt->flags |= AV_PKT_FLAG_KEY;
221     *got_packet = 1;
222
223     return 0;
224 //    return (put_bits_count(&f->pb)+7)/8;
225 }
226
227
228 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
229     .name           = "ljpeg",
230     .long_name      = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
231     .type           = AVMEDIA_TYPE_VIDEO,
232     .id             = AV_CODEC_ID_LJPEG,
233     .priv_data_size = sizeof(MpegEncContext),
234     .init           = ff_MPV_encode_init,
235     .encode2        = encode_picture_lossless,
236     .close          = ff_MPV_encode_end,
237     .pix_fmts       = (const enum AVPixelFormat[]){
238         AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0,
239         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
240         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
241         AV_PIX_FMT_NONE},
242 };