ljpegenc: switch to ff_alloc_packet2().
[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 "internal.h"
36 #include "mpegvideo.h"
37 #include "mjpeg.h"
38 #include "mjpegenc.h"
39
40
41 static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
42                                    const AVFrame *pict, int *got_packet)
43 {
44     MpegEncContext * const s = avctx->priv_data;
45     MJpegContext * const m = s->mjpeg_ctx;
46     const int width= s->width;
47     const int height= s->height;
48     AVFrame * const p = &s->current_picture.f;
49     const int predictor= avctx->prediction_method+1;
50     const int mb_width  = (width  + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
51     const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
52     int ret, max_pkt_size = FF_MIN_BUFFER_SIZE;
53
54     if (avctx->pix_fmt == PIX_FMT_BGRA)
55         max_pkt_size += width * height * 3 * 4;
56     else {
57         max_pkt_size += mb_width * mb_height * 3 * 4
58                         * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
59     }
60     if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0) {
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 == PIX_FMT_BGR0
75         || avctx->pix_fmt == PIX_FMT_BGRA
76         || avctx->pix_fmt == PIX_FMT_BGR24){
77         int x, y, i;
78         const int linesize= p->linesize[0];
79         uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
80         int left[3], top[3], topleft[3];
81
82         for(i=0; i<3; i++){
83             buffer[0][i]= 1 << (9 - 1);
84         }
85
86         for(y = 0; y < height; y++) {
87             const int modified_predictor= y ? predictor : 1;
88             uint8_t *ptr = p->data[0] + (linesize * y);
89
90             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
91                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
92                 return -1;
93             }
94
95             for(i=0; i<3; i++){
96                 top[i]= left[i]= topleft[i]= buffer[0][i];
97             }
98             for(x = 0; x < width; x++) {
99                 if(avctx->pix_fmt == PIX_FMT_BGR24){
100                     buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
101                     buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
102                     buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
103                 }else{
104                 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
105                 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
106                 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
107                 }
108
109                 for(i=0;i<3;i++) {
110                     int pred, diff;
111
112                     PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
113
114                     topleft[i]= top[i];
115                     top[i]= buffer[x+1][i];
116
117                     left[i]= buffer[x][i];
118
119                     diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
120
121                     if(i==0)
122                         ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
123                     else
124                         ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
125                 }
126             }
127         }
128     }else{
129         int mb_x, mb_y, i;
130
131         for(mb_y = 0; mb_y < mb_height; mb_y++) {
132             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]){
133                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
134                 return -1;
135             }
136             for(mb_x = 0; mb_x < mb_width; mb_x++) {
137                 if(mb_x==0 || mb_y==0){
138                     for(i=0;i<3;i++) {
139                         uint8_t *ptr;
140                         int x, y, h, v, linesize;
141                         h = s->mjpeg_hsample[i];
142                         v = s->mjpeg_vsample[i];
143                         linesize= p->linesize[i];
144
145                         for(y=0; y<v; y++){
146                             for(x=0; x<h; x++){
147                                 int pred;
148
149                                 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
150                                 if(y==0 && mb_y==0){
151                                     if(x==0 && mb_x==0){
152                                         pred= 128;
153                                     }else{
154                                         pred= ptr[-1];
155                                     }
156                                 }else{
157                                     if(x==0 && mb_x==0){
158                                         pred= ptr[-linesize];
159                                     }else{
160                                         PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
161                                     }
162                                 }
163
164                                 if(i==0)
165                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
166                                 else
167                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
168                             }
169                         }
170                     }
171                 }else{
172                     for(i=0;i<3;i++) {
173                         uint8_t *ptr;
174                         int x, y, h, v, linesize;
175                         h = s->mjpeg_hsample[i];
176                         v = s->mjpeg_vsample[i];
177                         linesize= p->linesize[i];
178
179                         for(y=0; y<v; y++){
180                             for(x=0; x<h; x++){
181                                 int pred;
182
183                                 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
184 //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
185                                 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
186
187                                 if(i==0)
188                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
189                                 else
190                                     ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
191                             }
192                         }
193                     }
194                 }
195             }
196         }
197     }
198
199     emms_c();
200
201     ff_mjpeg_encode_picture_trailer(s);
202     s->picture_number++;
203
204     flush_put_bits(&s->pb);
205     pkt->size   = put_bits_ptr(&s->pb) - s->pb.buf;
206     pkt->flags |= AV_PKT_FLAG_KEY;
207     *got_packet = 1;
208
209     return 0;
210 //    return (put_bits_count(&f->pb)+7)/8;
211 }
212
213
214 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
215     .name           = "ljpeg",
216     .type           = AVMEDIA_TYPE_VIDEO,
217     .id             = CODEC_ID_LJPEG,
218     .priv_data_size = sizeof(MpegEncContext),
219     .init           = ff_MPV_encode_init,
220     .encode2        = encode_picture_lossless,
221     .close          = ff_MPV_encode_end,
222     .long_name      = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
223 };