00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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= AV_PICTURE_TYPE_I;
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);
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);
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);
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);
00163
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);
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
00186 }
00187
00188
00189 AVCodec ff_ljpeg_encoder = {
00190 .name = "ljpeg",
00191 .type = AVMEDIA_TYPE_VIDEO,
00192 .id = CODEC_ID_LJPEG,
00193 .priv_data_size = sizeof(MpegEncContext),
00194 .init = MPV_encode_init,
00195 .encode = encode_picture_lossless,
00196 .close = MPV_encode_end,
00197 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
00198 };