mjpegenc: do not pass MpegEncContext to ff_mjpeg_encode_dc()
[libav.git] / libavcodec / ljpegenc.c
CommitLineData
d9c9259f
AJ
1/*
2 * lossless JPEG encoder
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
d9c9259f
AJ
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
6 *
7b94177e
DB
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 *
2912e87a 11 * This file is part of Libav.
d9c9259f 12 *
2912e87a 13 * Libav is free software; you can redistribute it and/or
d9c9259f
AJ
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 *
2912e87a 18 * Libav is distributed in the hope that it will be useful,
d9c9259f
AJ
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
2912e87a 24 * License along with Libav; if not, write to the Free Software
d9c9259f 25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d9c9259f
AJ
26 */
27
28/**
ba87f080 29 * @file
d9c9259f
AJ
30 * lossless JPEG encoder.
31 */
32
33#include "avcodec.h"
8e8124e1 34#include "internal.h"
d9c9259f
AJ
35#include "mpegvideo.h"
36#include "mjpeg.h"
37#include "mjpegenc.h"
38
39
8e8124e1
AK
40static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
41 const AVFrame *pict, int *got_packet)
42{
d9c9259f
AJ
43 MpegEncContext * const s = avctx->priv_data;
44 MJpegContext * const m = s->mjpeg_ctx;
d9c9259f
AJ
45 const int width= s->width;
46 const int height= s->height;
47c0ac96 47 AVFrame * const p = &s->current_picture.f;
d9c9259f 48 const int predictor= avctx->prediction_method+1;
8e8124e1
AK
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
716d413c 53 if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
8e8124e1
AK
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 }
d9c9259f 63
8e8124e1 64 init_put_bits(&s->pb, pkt->data, pkt->size);
d9c9259f 65
759001c5
AK
66 av_frame_unref(p);
67 ret = av_frame_ref(p, pict);
68 if (ret < 0)
69 return ret;
975a1447 70 p->pict_type= AV_PICTURE_TYPE_I;
d9c9259f
AJ
71 p->key_frame= 1;
72
058d5f2f
AK
73 ff_mjpeg_encode_picture_header(avctx, &s->pb, &s->intra_scantable,
74 s->intra_matrix);
d9c9259f
AJ
75
76 s->header_bits= put_bits_count(&s->pb);
77
716d413c 78 if(avctx->pix_fmt == AV_PIX_FMT_BGRA){
d9c9259f
AJ
79 int x, y, i;
80 const int linesize= p->linesize[0];
81 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
82 int left[3], top[3], topleft[3];
83
84 for(i=0; i<3; i++){
85 buffer[0][i]= 1 << (9 - 1);
86 }
87
88 for(y = 0; y < height; y++) {
89 const int modified_predictor= y ? predictor : 1;
90 uint8_t *ptr = p->data[0] + (linesize * y);
91
92 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
93 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
94 return -1;
95 }
96
97 for(i=0; i<3; i++){
98 top[i]= left[i]= topleft[i]= buffer[0][i];
99 }
100 for(x = 0; x < width; x++) {
101 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
102 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
103 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
104
105 for(i=0;i<3;i++) {
106 int pred, diff;
107
108 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
109
110 topleft[i]= top[i];
111 top[i]= buffer[x+1][i];
112
113 left[i]= buffer[x][i];
114
115 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
116
117 if(i==0)
86eb2eaa 118 ff_mjpeg_encode_dc(&s->pb, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
d9c9259f 119 else
86eb2eaa 120 ff_mjpeg_encode_dc(&s->pb, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
d9c9259f
AJ
121 }
122 }
123 }
124 }else{
125 int mb_x, mb_y, i;
d9c9259f
AJ
126
127 for(mb_y = 0; mb_y < mb_height; mb_y++) {
128 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]){
129 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
130 return -1;
131 }
132 for(mb_x = 0; mb_x < mb_width; mb_x++) {
133 if(mb_x==0 || mb_y==0){
134 for(i=0;i<3;i++) {
135 uint8_t *ptr;
136 int x, y, h, v, linesize;
137 h = s->mjpeg_hsample[i];
138 v = s->mjpeg_vsample[i];
139 linesize= p->linesize[i];
140
141 for(y=0; y<v; y++){
142 for(x=0; x<h; x++){
143 int pred;
144
145 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
146 if(y==0 && mb_y==0){
147 if(x==0 && mb_x==0){
148 pred= 128;
149 }else{
150 pred= ptr[-1];
151 }
152 }else{
153 if(x==0 && mb_x==0){
154 pred= ptr[-linesize];
155 }else{
156 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
157 }
158 }
159
160 if(i==0)
86eb2eaa 161 ff_mjpeg_encode_dc(&s->pb, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
d9c9259f 162 else
86eb2eaa 163 ff_mjpeg_encode_dc(&s->pb, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
d9c9259f
AJ
164 }
165 }
166 }
167 }else{
168 for(i=0;i<3;i++) {
169 uint8_t *ptr;
170 int x, y, h, v, linesize;
171 h = s->mjpeg_hsample[i];
172 v = s->mjpeg_vsample[i];
86eb2eaa 173 linesize = pict->linesize[i];
d9c9259f
AJ
174
175 for(y=0; y<v; y++){
176 for(x=0; x<h; x++){
177 int pred;
178
86eb2eaa 179 ptr = pict->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
d9c9259f
AJ
180 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
181
182 if(i==0)
86eb2eaa 183 ff_mjpeg_encode_dc(&s->pb, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
d9c9259f 184 else
86eb2eaa 185 ff_mjpeg_encode_dc(&s->pb, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
d9c9259f
AJ
186 }
187 }
188 }
189 }
190 }
191 }
192 }
193
194 emms_c();
195
3360ad99 196 ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
d9c9259f
AJ
197 s->picture_number++;
198
199 flush_put_bits(&s->pb);
8e8124e1
AK
200 pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
201 pkt->flags |= AV_PKT_FLAG_KEY;
202 *got_packet = 1;
203
204 return 0;
d9c9259f
AJ
205// return (put_bits_count(&f->pb)+7)/8;
206}
207
208
d36beb3f 209AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
86714887 210 .name = "ljpeg",
b2bed932 211 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
86714887 212 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 213 .id = AV_CODEC_ID_LJPEG,
86714887 214 .priv_data_size = sizeof(MpegEncContext),
efd29844 215 .init = ff_MPV_encode_init,
8e8124e1 216 .encode2 = encode_picture_lossless,
efd29844 217 .close = ff_MPV_encode_end,
d9c9259f 218};