ljpegenc: deMpegEncContextize
[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
24abd806
AK
33#include "libavutil/frame.h"
34#include "libavutil/mem.h"
35#include "libavutil/pixdesc.h"
36
d9c9259f 37#include "avcodec.h"
24abd806 38#include "dsputil.h"
8e8124e1 39#include "internal.h"
d9c9259f
AJ
40#include "mpegvideo.h"
41#include "mjpeg.h"
42#include "mjpegenc.h"
43
24abd806
AK
44typedef struct LJpegEncContext {
45 DSPContext dsp;
46 ScanTable scantable;
47 uint16_t matrix[64];
48
49 int vsample[3];
50 int hsample[3];
51
52 uint16_t huff_code_dc_luminance[12];
53 uint16_t huff_code_dc_chrominance[12];
54 uint8_t huff_size_dc_luminance[12];
55 uint8_t huff_size_dc_chrominance[12];
56
57 uint16_t (*scratch)[4];
58} LJpegEncContext;
d9c9259f 59
8e8124e1
AK
60static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
61 const AVFrame *pict, int *got_packet)
62{
24abd806
AK
63 LJpegEncContext *s = avctx->priv_data;
64 PutBitContext pb;
65 const int width = avctx->width;
66 const int height = avctx->height;
d9c9259f 67 const int predictor= avctx->prediction_method+1;
24abd806
AK
68 const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
69 const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
70 int max_pkt_size = FF_MIN_BUFFER_SIZE;
71 int ret, header_bits;
8e8124e1 72
716d413c 73 if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
8e8124e1
AK
74 max_pkt_size += width * height * 3 * 4;
75 else {
76 max_pkt_size += mb_width * mb_height * 3 * 4
24abd806 77 * s->hsample[0] * s->vsample[0];
8e8124e1
AK
78 }
79 if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
80 av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
81 return ret;
82 }
d9c9259f 83
24abd806 84 init_put_bits(&pb, pkt->data, pkt->size);
d9c9259f 85
24abd806
AK
86 ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
87 s->matrix);
d9c9259f 88
24abd806 89 header_bits = put_bits_count(&pb);
d9c9259f 90
716d413c 91 if(avctx->pix_fmt == AV_PIX_FMT_BGRA){
d9c9259f 92 int x, y, i;
24abd806
AK
93 const int linesize = pict->linesize[0];
94 uint16_t (*buffer)[4] = s->scratch;
d9c9259f
AJ
95 int left[3], top[3], topleft[3];
96
97 for(i=0; i<3; i++){
98 buffer[0][i]= 1 << (9 - 1);
99 }
100
101 for(y = 0; y < height; y++) {
102 const int modified_predictor= y ? predictor : 1;
24abd806 103 uint8_t *ptr = pict->data[0] + (linesize * y);
d9c9259f 104
24abd806
AK
105 if(pb.buf_end - pb.buf - (put_bits_count(&pb) >> 3) < width * 3 * 4) {
106 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
d9c9259f
AJ
107 return -1;
108 }
109
110 for(i=0; i<3; i++){
111 top[i]= left[i]= topleft[i]= buffer[0][i];
112 }
113 for(x = 0; x < width; x++) {
114 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
115 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
116 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
117
118 for(i=0;i<3;i++) {
119 int pred, diff;
120
121 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
122
123 topleft[i]= top[i];
124 top[i]= buffer[x+1][i];
125
126 left[i]= buffer[x][i];
127
128 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
129
130 if(i==0)
24abd806 131 ff_mjpeg_encode_dc(&pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
d9c9259f 132 else
24abd806 133 ff_mjpeg_encode_dc(&pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
d9c9259f
AJ
134 }
135 }
136 }
137 }else{
138 int mb_x, mb_y, i;
d9c9259f
AJ
139
140 for(mb_y = 0; mb_y < mb_height; mb_y++) {
24abd806
AK
141 if (pb.buf_end - pb.buf - (put_bits_count(&pb) >> 3) <
142 mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
143 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
d9c9259f
AJ
144 return -1;
145 }
146 for(mb_x = 0; mb_x < mb_width; mb_x++) {
147 if(mb_x==0 || mb_y==0){
148 for(i=0;i<3;i++) {
149 uint8_t *ptr;
150 int x, y, h, v, linesize;
24abd806
AK
151 h = s->hsample[i];
152 v = s->vsample[i];
153 linesize = pict->linesize[i];
d9c9259f
AJ
154
155 for(y=0; y<v; y++){
156 for(x=0; x<h; x++){
157 int pred;
158
24abd806 159 ptr = pict->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
d9c9259f
AJ
160 if(y==0 && mb_y==0){
161 if(x==0 && mb_x==0){
162 pred= 128;
163 }else{
164 pred= ptr[-1];
165 }
166 }else{
167 if(x==0 && mb_x==0){
168 pred= ptr[-linesize];
169 }else{
170 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
171 }
172 }
173
174 if(i==0)
24abd806 175 ff_mjpeg_encode_dc(&pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
d9c9259f 176 else
24abd806 177 ff_mjpeg_encode_dc(&pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
d9c9259f
AJ
178 }
179 }
180 }
181 }else{
182 for(i=0;i<3;i++) {
183 uint8_t *ptr;
184 int x, y, h, v, linesize;
24abd806
AK
185 h = s->hsample[i];
186 v = s->vsample[i];
86eb2eaa 187 linesize = pict->linesize[i];
d9c9259f
AJ
188
189 for(y=0; y<v; y++){
190 for(x=0; x<h; x++){
191 int pred;
192
86eb2eaa 193 ptr = pict->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
d9c9259f
AJ
194 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
195
196 if(i==0)
24abd806 197 ff_mjpeg_encode_dc(&pb, *ptr - pred, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
d9c9259f 198 else
24abd806 199 ff_mjpeg_encode_dc(&pb, *ptr - pred, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
d9c9259f
AJ
200 }
201 }
202 }
203 }
204 }
205 }
206 }
207
208 emms_c();
209
24abd806 210 ff_mjpeg_encode_picture_trailer(&pb, header_bits);
d9c9259f 211
24abd806
AK
212 flush_put_bits(&pb);
213 pkt->size = put_bits_ptr(&pb) - pb.buf;
8e8124e1
AK
214 pkt->flags |= AV_PKT_FLAG_KEY;
215 *got_packet = 1;
216
217 return 0;
d9c9259f
AJ
218// return (put_bits_count(&f->pb)+7)/8;
219}
220
24abd806
AK
221static av_cold int ljpeg_encode_close(AVCodecContext *avctx)
222{
223 LJpegEncContext *s = avctx->priv_data;
224
225 av_frame_free(&avctx->coded_frame);
226 av_freep(&s->scratch);
227
228 return 0;
229}
230
231static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
232{
233 LJpegEncContext *s = avctx->priv_data;
234 int chroma_v_shift, chroma_h_shift;
235
236 if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
237 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
238 avctx->pix_fmt == AV_PIX_FMT_YUV444P) &&
239 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
240 av_log(avctx, AV_LOG_ERROR,
241 "Limited range YUV is non-standard, set strict_std_compliance to "
242 "at least unofficial to use it.\n");
243 return AVERROR(EINVAL);
244 }
245
246 avctx->coded_frame = av_frame_alloc();
247 if (!avctx->coded_frame)
248 return AVERROR(ENOMEM);
249
250 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
251 avctx->coded_frame->key_frame = 1;
252
253 s->scratch = av_malloc_array(avctx->width + 1, sizeof(*s->scratch));
254
255 ff_dsputil_init(&s->dsp, avctx);
256 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
257
258 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
259 &chroma_v_shift);
260
261 if (avctx->pix_fmt == AV_PIX_FMT_BGRA) {
262 s->vsample[0] = s->hsample[0] =
263 s->vsample[1] = s->hsample[1] =
264 s->vsample[2] = s->hsample[2] = 1;
265 } else {
266 s->vsample[0] = 2;
267 s->vsample[1] = 2 >> chroma_v_shift;
268 s->vsample[2] = 2 >> chroma_v_shift;
269 s->hsample[0] = 2;
270 s->hsample[1] = 2 >> chroma_h_shift;
271 s->hsample[2] = 2 >> chroma_h_shift;
272 }
273
274 ff_mjpeg_build_huffman_codes(s->huff_size_dc_luminance,
275 s->huff_code_dc_luminance,
276 avpriv_mjpeg_bits_dc_luminance,
277 avpriv_mjpeg_val_dc);
278 ff_mjpeg_build_huffman_codes(s->huff_size_dc_chrominance,
279 s->huff_code_dc_chrominance,
280 avpriv_mjpeg_bits_dc_chrominance,
281 avpriv_mjpeg_val_dc);
282
283 return 0;
284}
d9c9259f 285
24abd806 286AVCodec ff_ljpeg_encoder = {
86714887 287 .name = "ljpeg",
b2bed932 288 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
86714887 289 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 290 .id = AV_CODEC_ID_LJPEG,
24abd806
AK
291 .priv_data_size = sizeof(LJpegEncContext),
292 .init = ljpeg_encode_init,
8e8124e1 293 .encode2 = encode_picture_lossless,
24abd806
AK
294 .close = ljpeg_encode_close,
295 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUVJ420P,
296 AV_PIX_FMT_YUVJ422P,
297 AV_PIX_FMT_YUVJ444P,
298 AV_PIX_FMT_BGRA,
299 AV_PIX_FMT_YUV420P,
300 AV_PIX_FMT_YUV422P,
301 AV_PIX_FMT_YUVJ444P,
302 AV_PIX_FMT_NONE },
d9c9259f 303};