Libav
|
00001 /* 00002 * lossless JPEG encoder 00003 * Copyright (c) 2000, 2001 Fabrice Bellard 00004 * Copyright (c) 2003 Alex Beregszaszi 00005 * Copyright (c) 2003-2004 Michael Niedermayer 00006 * 00007 * Support for external huffman table, various fixes (AVID workaround), 00008 * aspecting, new decode_frame mechanism and apple mjpeg-b support 00009 * by Alex Beregszaszi 00010 * 00011 * This file is part of FFmpeg. 00012 * 00013 * FFmpeg is free software; you can redistribute it and/or 00014 * modify it under the terms of the GNU Lesser General Public 00015 * License as published by the Free Software Foundation; either 00016 * version 2.1 of the License, or (at your option) any later version. 00017 * 00018 * FFmpeg is distributed in the hope that it will be useful, 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00021 * Lesser General Public License for more details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public 00024 * License along with FFmpeg; if not, write to the Free Software 00025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00026 */ 00027 00033 #include "avcodec.h" 00034 #include "dsputil.h" 00035 #include "mpegvideo.h" 00036 #include "mjpeg.h" 00037 #include "mjpegenc.h" 00038 00039 00040 static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ 00041 MpegEncContext * const s = avctx->priv_data; 00042 MJpegContext * const m = s->mjpeg_ctx; 00043 AVFrame *pict = data; 00044 const int width= s->width; 00045 const int height= s->height; 00046 AVFrame * const p= (AVFrame*)&s->current_picture; 00047 const int predictor= avctx->prediction_method+1; 00048 00049 init_put_bits(&s->pb, buf, buf_size); 00050 00051 *p = *pict; 00052 p->pict_type= FF_I_TYPE; 00053 p->key_frame= 1; 00054 00055 ff_mjpeg_encode_picture_header(s); 00056 00057 s->header_bits= put_bits_count(&s->pb); 00058 00059 if(avctx->pix_fmt == PIX_FMT_BGRA){ 00060 int x, y, i; 00061 const int linesize= p->linesize[0]; 00062 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad; 00063 int left[3], top[3], topleft[3]; 00064 00065 for(i=0; i<3; i++){ 00066 buffer[0][i]= 1 << (9 - 1); 00067 } 00068 00069 for(y = 0; y < height; y++) { 00070 const int modified_predictor= y ? predictor : 1; 00071 uint8_t *ptr = p->data[0] + (linesize * y); 00072 00073 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){ 00074 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); 00075 return -1; 00076 } 00077 00078 for(i=0; i<3; i++){ 00079 top[i]= left[i]= topleft[i]= buffer[0][i]; 00080 } 00081 for(x = 0; x < width; x++) { 00082 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100; 00083 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100; 00084 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2; 00085 00086 for(i=0;i<3;i++) { 00087 int pred, diff; 00088 00089 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); 00090 00091 topleft[i]= top[i]; 00092 top[i]= buffer[x+1][i]; 00093 00094 left[i]= buffer[x][i]; 00095 00096 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100; 00097 00098 if(i==0) 00099 ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly 00100 else 00101 ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); 00102 } 00103 } 00104 } 00105 }else{ 00106 int mb_x, mb_y, i; 00107 const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0]; 00108 const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0]; 00109 00110 for(mb_y = 0; mb_y < mb_height; mb_y++) { 00111 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]){ 00112 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); 00113 return -1; 00114 } 00115 for(mb_x = 0; mb_x < mb_width; mb_x++) { 00116 if(mb_x==0 || mb_y==0){ 00117 for(i=0;i<3;i++) { 00118 uint8_t *ptr; 00119 int x, y, h, v, linesize; 00120 h = s->mjpeg_hsample[i]; 00121 v = s->mjpeg_vsample[i]; 00122 linesize= p->linesize[i]; 00123 00124 for(y=0; y<v; y++){ 00125 for(x=0; x<h; x++){ 00126 int pred; 00127 00128 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap 00129 if(y==0 && mb_y==0){ 00130 if(x==0 && mb_x==0){ 00131 pred= 128; 00132 }else{ 00133 pred= ptr[-1]; 00134 } 00135 }else{ 00136 if(x==0 && mb_x==0){ 00137 pred= ptr[-linesize]; 00138 }else{ 00139 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); 00140 } 00141 } 00142 00143 if(i==0) 00144 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly 00145 else 00146 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); 00147 } 00148 } 00149 } 00150 }else{ 00151 for(i=0;i<3;i++) { 00152 uint8_t *ptr; 00153 int x, y, h, v, linesize; 00154 h = s->mjpeg_hsample[i]; 00155 v = s->mjpeg_vsample[i]; 00156 linesize= p->linesize[i]; 00157 00158 for(y=0; y<v; y++){ 00159 for(x=0; x<h; x++){ 00160 int pred; 00161 00162 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap 00163 //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr); 00164 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); 00165 00166 if(i==0) 00167 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly 00168 else 00169 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); 00170 } 00171 } 00172 } 00173 } 00174 } 00175 } 00176 } 00177 00178 emms_c(); 00179 00180 ff_mjpeg_encode_picture_trailer(s); 00181 s->picture_number++; 00182 00183 flush_put_bits(&s->pb); 00184 return put_bits_ptr(&s->pb) - s->pb.buf; 00185 // return (put_bits_count(&f->pb)+7)/8; 00186 } 00187 00188 00189 AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them 00190 "ljpeg", 00191 AVMEDIA_TYPE_VIDEO, 00192 CODEC_ID_LJPEG, 00193 sizeof(MpegEncContext), 00194 MPV_encode_init, 00195 encode_picture_lossless, 00196 MPV_encode_end, 00197 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"), 00198 };