]> git.sesse.net Git - ffmpeg/blob - libavcodec/ljpegenc.c
Remove unnecessary dsputil.h #includes
[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 "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     if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
60         av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
61         return ret;
62     }
63
64     init_put_bits(&s->pb, pkt->data, pkt->size);
65
66     *p = *pict;
67     p->pict_type= AV_PICTURE_TYPE_I;
68     p->key_frame= 1;
69
70     ff_mjpeg_encode_picture_header(s);
71
72     s->header_bits= put_bits_count(&s->pb);
73
74     if(avctx->pix_fmt == AV_PIX_FMT_BGRA){
75         int x, y, i;
76         const int linesize= p->linesize[0];
77         uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
78         int left[3], top[3], topleft[3];
79
80         for(i=0; i<3; i++){
81             buffer[0][i]= 1 << (9 - 1);
82         }
83
84         for(y = 0; y < height; y++) {
85             const int modified_predictor= y ? predictor : 1;
86             uint8_t *ptr = p->data[0] + (linesize * y);
87
88             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
89                 av_log(s->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[4*x+0] - ptr[4*x+1] + 0x100;
98                 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
99                 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*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(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
115                     else
116                         ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
117                 }
118             }
119         }
120     }else{
121         int mb_x, mb_y, i;
122
123         for(mb_y = 0; mb_y < mb_height; mb_y++) {
124             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]){
125                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
126                 return -1;
127             }
128             for(mb_x = 0; mb_x < mb_width; mb_x++) {
129                 if(mb_x==0 || mb_y==0){
130                     for(i=0;i<3;i++) {
131                         uint8_t *ptr;
132                         int x, y, h, v, linesize;
133                         h = s->mjpeg_hsample[i];
134                         v = s->mjpeg_vsample[i];
135                         linesize= p->linesize[i];
136
137                         for(y=0; y<v; y++){
138                             for(x=0; x<h; x++){
139                                 int pred;
140
141                                 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
142                                 if(y==0 && mb_y==0){
143                                     if(x==0 && mb_x==0){
144                                         pred= 128;
145                                     }else{
146                                         pred= ptr[-1];
147                                     }
148                                 }else{
149                                     if(x==0 && mb_x==0){
150                                         pred= ptr[-linesize];
151                                     }else{
152                                         PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
153                                     }
154                                 }
155
156                                 if(i==0)
157                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
158                                 else
159                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
160                             }
161                         }
162                     }
163                 }else{
164                     for(i=0;i<3;i++) {
165                         uint8_t *ptr;
166                         int x, y, h, v, linesize;
167                         h = s->mjpeg_hsample[i];
168                         v = s->mjpeg_vsample[i];
169                         linesize= p->linesize[i];
170
171                         for(y=0; y<v; y++){
172                             for(x=0; x<h; x++){
173                                 int pred;
174
175                                 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
176                                 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
177
178                                 if(i==0)
179                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
180                                 else
181                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
182                             }
183                         }
184                     }
185                 }
186             }
187         }
188     }
189
190     emms_c();
191
192     ff_mjpeg_encode_picture_trailer(s);
193     s->picture_number++;
194
195     flush_put_bits(&s->pb);
196     pkt->size   = put_bits_ptr(&s->pb) - s->pb.buf;
197     pkt->flags |= AV_PKT_FLAG_KEY;
198     *got_packet = 1;
199
200     return 0;
201 //    return (put_bits_count(&f->pb)+7)/8;
202 }
203
204
205 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
206     .name           = "ljpeg",
207     .type           = AVMEDIA_TYPE_VIDEO,
208     .id             = AV_CODEC_ID_LJPEG,
209     .priv_data_size = sizeof(MpegEncContext),
210     .init           = ff_MPV_encode_init,
211     .encode2        = encode_picture_lossless,
212     .close          = ff_MPV_encode_end,
213     .long_name      = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
214 };