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.
36 #include "mpegvideo.h"
41 static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
42 const AVFrame *pict, int *got_packet)
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;
54 if (avctx->pix_fmt == PIX_FMT_BGRA)
55 max_pkt_size += width * height * 3 * 4;
57 max_pkt_size += mb_width * mb_height * 3 * 4
58 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
60 if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
63 init_put_bits(&s->pb, pkt->data, pkt->size);
66 p->pict_type= AV_PICTURE_TYPE_I;
69 ff_mjpeg_encode_picture_header(s);
71 s->header_bits= put_bits_count(&s->pb);
73 if(avctx->pix_fmt == PIX_FMT_BGR0
74 || avctx->pix_fmt == PIX_FMT_BGRA
75 || avctx->pix_fmt == PIX_FMT_BGR24){
77 const int linesize= p->linesize[0];
78 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
79 int left[3], top[3], topleft[3];
82 buffer[0][i]= 1 << (9 - 1);
85 for(y = 0; y < height; y++) {
86 const int modified_predictor= y ? predictor : 1;
87 uint8_t *ptr = p->data[0] + (linesize * y);
89 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
90 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
95 top[i]= left[i]= topleft[i]= buffer[0][i];
97 for(x = 0; x < width; x++) {
98 if(avctx->pix_fmt == PIX_FMT_BGR24){
99 buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
100 buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
101 buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
103 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
104 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
105 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
111 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
114 top[i]= buffer[x+1][i];
116 left[i]= buffer[x][i];
118 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
121 ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
123 ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
130 for(mb_y = 0; mb_y < mb_height; mb_y++) {
131 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]){
132 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
135 for(mb_x = 0; mb_x < mb_width; mb_x++) {
136 if(mb_x==0 || mb_y==0){
139 int x, y, h, v, linesize;
140 h = s->mjpeg_hsample[i];
141 v = s->mjpeg_vsample[i];
142 linesize= p->linesize[i];
148 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
157 pred= ptr[-linesize];
159 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
164 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
166 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
173 int x, y, h, v, linesize;
174 h = s->mjpeg_hsample[i];
175 v = s->mjpeg_vsample[i];
176 linesize= p->linesize[i];
182 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
183 //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
184 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
187 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
189 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
200 ff_mjpeg_encode_picture_trailer(s);
203 flush_put_bits(&s->pb);
204 pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
205 pkt->flags |= AV_PKT_FLAG_KEY;
209 // return (put_bits_count(&f->pb)+7)/8;
213 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
215 .type = AVMEDIA_TYPE_VIDEO,
216 .id = CODEC_ID_LJPEG,
217 .priv_data_size = sizeof(MpegEncContext),
218 .init = ff_MPV_encode_init,
219 .encode2 = encode_picture_lossless,
220 .close = ff_MPV_encode_end,
221 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),