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, AVPacket *pkt,
41 const AVFrame *pict, int *got_packet)
43 MpegEncContext * const s = avctx->priv_data;
44 MJpegContext * const m = s->mjpeg_ctx;
45 const int width= s->width;
46 const int height= s->height;
47 AVFrame * const p = &s->current_picture.f;
48 const int predictor= avctx->prediction_method+1;
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;
53 if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
54 max_pkt_size += width * height * 3 * 4;
56 max_pkt_size += mb_width * mb_height * 3 * 4
57 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
60 if (!s->edge_emu_buffer &&
61 (ret = ff_mpv_frame_size_alloc(s, pict->linesize[0])) < 0) {
62 av_log(avctx, AV_LOG_ERROR, "failed to allocate context scratch buffers.\n");
66 if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
69 init_put_bits(&s->pb, pkt->data, pkt->size);
72 p->pict_type= AV_PICTURE_TYPE_I;
75 ff_mjpeg_encode_picture_header(s);
77 s->header_bits= put_bits_count(&s->pb);
79 if(avctx->pix_fmt == AV_PIX_FMT_BGR0
80 || avctx->pix_fmt == AV_PIX_FMT_BGRA
81 || avctx->pix_fmt == AV_PIX_FMT_BGR24){
83 const int linesize= p->linesize[0];
84 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
85 int left[3], top[3], topleft[3];
88 buffer[0][i]= 1 << (9 - 1);
91 for(y = 0; y < height; y++) {
92 const int modified_predictor= y ? predictor : 1;
93 uint8_t *ptr = p->data[0] + (linesize * y);
95 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
96 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
101 top[i]= left[i]= topleft[i]= buffer[0][i];
103 for(x = 0; x < width; x++) {
104 if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
105 buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
106 buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
107 buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
109 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
110 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
111 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
117 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
120 top[i]= buffer[x+1][i];
122 left[i]= buffer[x][i];
124 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
127 ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
129 ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
136 for(mb_y = 0; mb_y < mb_height; mb_y++) {
137 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]){
138 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
141 for(mb_x = 0; mb_x < mb_width; mb_x++) {
142 if(mb_x==0 || mb_y==0){
145 int x, y, h, v, linesize;
146 h = s->mjpeg_hsample[i];
147 v = s->mjpeg_vsample[i];
148 linesize= p->linesize[i];
154 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
163 pred= ptr[-linesize];
165 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
170 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
172 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
179 int x, y, h, v, linesize;
180 h = s->mjpeg_hsample[i];
181 v = s->mjpeg_vsample[i];
182 linesize= p->linesize[i];
188 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
189 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
192 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
194 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
204 av_assert0(s->esc_pos == s->header_bits >> 3);
205 ff_mjpeg_encode_stuffing(s);
206 ff_mjpeg_encode_picture_trailer(s);
209 flush_put_bits(&s->pb);
210 pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
211 pkt->flags |= AV_PKT_FLAG_KEY;
215 // return (put_bits_count(&f->pb)+7)/8;
219 AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
221 .type = AVMEDIA_TYPE_VIDEO,
222 .id = AV_CODEC_ID_LJPEG,
223 .priv_data_size = sizeof(MpegEncContext),
224 .init = ff_MPV_encode_init,
225 .encode2 = encode_picture_lossless,
226 .close = ff_MPV_encode_end,
227 .pix_fmts = (const enum AVPixelFormat[]){
228 AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0,
229 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
230 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
232 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),