2 * lossless JPEG encoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
11 * This file is part of FFmpeg.
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.
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.
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
30 * lossless JPEG encoder.
35 #include "mpegvideo.h"
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
;
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;
49 init_put_bits(&s
->pb
, buf
, buf_size
);
52 p
->pict_type
= FF_I_TYPE
;
55 ff_mjpeg_encode_picture_header(s
);
57 s
->header_bits
= put_bits_count(&s
->pb
);
59 if(avctx
->pix_fmt
== PIX_FMT_RGB32
){
61 const int linesize
= p
->linesize
[0];
62 uint16_t (*buffer
)[4]= (void *) s
->rd_scratchpad
;
63 int left
[3], top
[3], topleft
[3];
66 buffer
[0][i
]= 1 << (9 - 1);
69 for(y
= 0; y
< height
; y
++) {
70 const int modified_predictor
= y
? predictor
: 1;
71 uint8_t *ptr
= p
->data
[0] + (linesize
* y
);
73 if(s
->pb
.buf_end
- s
->pb
.buf
- (put_bits_count(&s
->pb
)>>3) < width
*3*4){
74 av_log(s
->avctx
, AV_LOG_ERROR
, "encoded frame too large\n");
79 top
[i
]= left
[i
]= topleft
[i
]= buffer
[0][i
];
81 for(x
= 0; x
< width
; x
++) {
82 buffer
[x
][1] = ptr
[4*x
+0] - ptr
[4*x
+1] + 0x100;
83 buffer
[x
][2] = ptr
[4*x
+2] - ptr
[4*x
+1] + 0x100;
84 buffer
[x
][0] = (ptr
[4*x
+0] + 2*ptr
[4*x
+1] + ptr
[4*x
+2])>>2;
89 PREDICT(pred
, topleft
[i
], top
[i
], left
[i
], modified_predictor
);
92 top
[i
]= buffer
[x
+1][i
];
94 left
[i
]= buffer
[x
][i
];
96 diff
= ((left
[i
] - pred
+ 0x100)&0x1FF) - 0x100;
99 ff_mjpeg_encode_dc(s
, diff
, m
->huff_size_dc_luminance
, m
->huff_code_dc_luminance
); //FIXME ugly
101 ff_mjpeg_encode_dc(s
, diff
, m
->huff_size_dc_chrominance
, m
->huff_code_dc_chrominance
);
107 const int mb_width
= (width
+ s
->mjpeg_hsample
[0] - 1) / s
->mjpeg_hsample
[0];
108 const int mb_height
= (height
+ s
->mjpeg_vsample
[0] - 1) / s
->mjpeg_vsample
[0];
110 for(mb_y
= 0; mb_y
< mb_height
; mb_y
++) {
111 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]){
112 av_log(s
->avctx
, AV_LOG_ERROR
, "encoded frame too large\n");
115 for(mb_x
= 0; mb_x
< mb_width
; mb_x
++) {
116 if(mb_x
==0 || mb_y
==0){
119 int x
, y
, h
, v
, linesize
;
120 h
= s
->mjpeg_hsample
[i
];
121 v
= s
->mjpeg_vsample
[i
];
122 linesize
= p
->linesize
[i
];
128 ptr
= p
->data
[i
] + (linesize
* (v
* mb_y
+ y
)) + (h
* mb_x
+ x
); //FIXME optimize this crap
137 pred
= ptr
[-linesize
];
139 PREDICT(pred
, ptr
[-linesize
-1], ptr
[-linesize
], ptr
[-1], predictor
);
144 ff_mjpeg_encode_dc(s
, *ptr
- pred
, m
->huff_size_dc_luminance
, m
->huff_code_dc_luminance
); //FIXME ugly
146 ff_mjpeg_encode_dc(s
, *ptr
- pred
, m
->huff_size_dc_chrominance
, m
->huff_code_dc_chrominance
);
153 int x
, y
, h
, v
, linesize
;
154 h
= s
->mjpeg_hsample
[i
];
155 v
= s
->mjpeg_vsample
[i
];
156 linesize
= p
->linesize
[i
];
162 ptr
= p
->data
[i
] + (linesize
* (v
* mb_y
+ y
)) + (h
* mb_x
+ x
); //FIXME optimize this crap
163 //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
164 PREDICT(pred
, ptr
[-linesize
-1], ptr
[-linesize
], ptr
[-1], predictor
);
167 ff_mjpeg_encode_dc(s
, *ptr
- pred
, m
->huff_size_dc_luminance
, m
->huff_code_dc_luminance
); //FIXME ugly
169 ff_mjpeg_encode_dc(s
, *ptr
- pred
, m
->huff_size_dc_chrominance
, m
->huff_code_dc_chrominance
);
180 ff_mjpeg_encode_picture_trailer(s
);
183 flush_put_bits(&s
->pb
);
184 return pbBufPtr(&s
->pb
) - s
->pb
.buf
;
185 // return (put_bits_count(&f->pb)+7)/8;
189 AVCodec ljpeg_encoder
= { //FIXME avoid MPV_* lossless JPEG should not need them
193 sizeof(MpegEncContext
),
195 encode_picture_lossless
,
197 .long_name
= NULL_IF_CONFIG_SMALL("Lossless JPEG"),