Support encoding BGR24 and BGR0 in ljpeg.
[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 "dsputil.h"
35 #include "mpegvideo.h"
36 #include "mjpeg.h"
37 #include "mjpegenc.h"
38
39
40 static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
41     MpegEncContext * const s = avctx->priv_data;
42     MJpegContext * const m = s->mjpeg_ctx;
43     AVFrame *pict = data;
44     const int width= s->width;
45     const int height= s->height;
46     AVFrame * const p= (AVFrame*)&s->current_picture;
47     const int predictor= avctx->prediction_method+1;
48
49     init_put_bits(&s->pb, buf, buf_size);
50
51     *p = *pict;
52     p->pict_type= AV_PICTURE_TYPE_I;
53     p->key_frame= 1;
54
55     ff_mjpeg_encode_picture_header(s);
56
57     s->header_bits= put_bits_count(&s->pb);
58
59     if(avctx->pix_fmt == PIX_FMT_BGR0
60         || avctx->pix_fmt == PIX_FMT_BGRA
61         || avctx->pix_fmt == PIX_FMT_BGR24){
62         int x, y, i;
63         const int linesize= p->linesize[0];
64         uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
65         int left[3], top[3], topleft[3];
66
67         for(i=0; i<3; i++){
68             buffer[0][i]= 1 << (9 - 1);
69         }
70
71         for(y = 0; y < height; y++) {
72             const int modified_predictor= y ? predictor : 1;
73             uint8_t *ptr = p->data[0] + (linesize * y);
74
75             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
76                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
77                 return -1;
78             }
79
80             for(i=0; i<3; i++){
81                 top[i]= left[i]= topleft[i]= buffer[0][i];
82             }
83             for(x = 0; x < width; x++) {
84                 if(avctx->pix_fmt == PIX_FMT_BGR24){
85                     buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
86                     buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
87                     buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
88                 }else{
89                 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
90                 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
91                 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
92                 }
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(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
108                     else
109                         ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
110                 }
111             }
112         }
113     }else{
114         int mb_x, mb_y, i;
115         const int mb_width  = (width  + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
116         const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
117
118         for(mb_y = 0; mb_y < mb_height; mb_y++) {
119             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]){
120                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
121                 return -1;
122             }
123             for(mb_x = 0; mb_x < mb_width; mb_x++) {
124                 if(mb_x==0 || mb_y==0){
125                     for(i=0;i<3;i++) {
126                         uint8_t *ptr;
127                         int x, y, h, v, linesize;
128                         h = s->mjpeg_hsample[i];
129                         v = s->mjpeg_vsample[i];
130                         linesize= p->linesize[i];
131
132                         for(y=0; y<v; y++){
133                             for(x=0; x<h; x++){
134                                 int pred;
135
136                                 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
137                                 if(y==0 && mb_y==0){
138                                     if(x==0 && mb_x==0){
139                                         pred= 128;
140                                     }else{
141                                         pred= ptr[-1];
142                                     }
143                                 }else{
144                                     if(x==0 && mb_x==0){
145                                         pred= ptr[-linesize];
146                                     }else{
147                                         PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
148                                     }
149                                 }
150
151                                 if(i==0)
152                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
153                                 else
154                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
155                             }
156                         }
157                     }
158                 }else{
159                     for(i=0;i<3;i++) {
160                         uint8_t *ptr;
161                         int x, y, h, v, linesize;
162                         h = s->mjpeg_hsample[i];
163                         v = s->mjpeg_vsample[i];
164                         linesize= p->linesize[i];
165
166                         for(y=0; y<v; y++){
167                             for(x=0; x<h; x++){
168                                 int pred;
169
170                                 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
171 //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
172                                 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
173
174                                 if(i==0)
175                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
176                                 else
177                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
178                             }
179                         }
180                     }
181                 }
182             }
183         }
184     }
185
186     emms_c();
187
188     ff_mjpeg_encode_picture_trailer(s);
189     s->picture_number++;
190
191     flush_put_bits(&s->pb);
192     return put_bits_ptr(&s->pb) - s->pb.buf;
193 //    return (put_bits_count(&f->pb)+7)/8;
194 }
195
196
197 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
198     .name           = "ljpeg",
199     .type           = AVMEDIA_TYPE_VIDEO,
200     .id             = CODEC_ID_LJPEG,
201     .priv_data_size = sizeof(MpegEncContext),
202     .init           = MPV_encode_init,
203     .encode         = encode_picture_lossless,
204     .close          = MPV_encode_end,
205     .long_name      = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
206 };