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 "internal.h"
00036 #include "mpegvideo.h"
00037 #include "mjpeg.h"
00038 #include "mjpegenc.h"
00039
00040
00041 static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
00042 const AVFrame *pict, int *got_packet)
00043 {
00044 MpegEncContext * const s = avctx->priv_data;
00045 MJpegContext * const m = s->mjpeg_ctx;
00046 const int width= s->width;
00047 const int height= s->height;
00048 AVFrame * const p = &s->current_picture.f;
00049 const int predictor= avctx->prediction_method+1;
00050 const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
00051 const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
00052 int ret, max_pkt_size = FF_MIN_BUFFER_SIZE;
00053
00054 if (avctx->pix_fmt == PIX_FMT_BGRA)
00055 max_pkt_size += width * height * 3 * 4;
00056 else {
00057 max_pkt_size += mb_width * mb_height * 3 * 4
00058 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
00059 }
00060 if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
00061 return ret;
00062
00063 init_put_bits(&s->pb, pkt->data, pkt->size);
00064
00065 *p = *pict;
00066 p->pict_type= AV_PICTURE_TYPE_I;
00067 p->key_frame= 1;
00068
00069 ff_mjpeg_encode_picture_header(s);
00070
00071 s->header_bits= put_bits_count(&s->pb);
00072
00073 if(avctx->pix_fmt == PIX_FMT_BGR0
00074 || avctx->pix_fmt == PIX_FMT_BGRA
00075 || avctx->pix_fmt == PIX_FMT_BGR24){
00076 int x, y, i;
00077 const int linesize= p->linesize[0];
00078 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
00079 int left[3], top[3], topleft[3];
00080
00081 for(i=0; i<3; i++){
00082 buffer[0][i]= 1 << (9 - 1);
00083 }
00084
00085 for(y = 0; y < height; y++) {
00086 const int modified_predictor= y ? predictor : 1;
00087 uint8_t *ptr = p->data[0] + (linesize * y);
00088
00089 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
00090 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00091 return -1;
00092 }
00093
00094 for(i=0; i<3; i++){
00095 top[i]= left[i]= topleft[i]= buffer[0][i];
00096 }
00097 for(x = 0; x < width; x++) {
00098 if(avctx->pix_fmt == PIX_FMT_BGR24){
00099 buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
00100 buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
00101 buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
00102 }else{
00103 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
00104 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
00105 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
00106 }
00107
00108 for(i=0;i<3;i++) {
00109 int pred, diff;
00110
00111 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
00112
00113 topleft[i]= top[i];
00114 top[i]= buffer[x+1][i];
00115
00116 left[i]= buffer[x][i];
00117
00118 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
00119
00120 if(i==0)
00121 ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00122 else
00123 ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00124 }
00125 }
00126 }
00127 }else{
00128 int mb_x, mb_y, i;
00129
00130 for(mb_y = 0; mb_y < mb_height; mb_y++) {
00131 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]){
00132 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00133 return -1;
00134 }
00135 for(mb_x = 0; mb_x < mb_width; mb_x++) {
00136 if(mb_x==0 || mb_y==0){
00137 for(i=0;i<3;i++) {
00138 uint8_t *ptr;
00139 int x, y, h, v, linesize;
00140 h = s->mjpeg_hsample[i];
00141 v = s->mjpeg_vsample[i];
00142 linesize= p->linesize[i];
00143
00144 for(y=0; y<v; y++){
00145 for(x=0; x<h; x++){
00146 int pred;
00147
00148 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x);
00149 if(y==0 && mb_y==0){
00150 if(x==0 && mb_x==0){
00151 pred= 128;
00152 }else{
00153 pred= ptr[-1];
00154 }
00155 }else{
00156 if(x==0 && mb_x==0){
00157 pred= ptr[-linesize];
00158 }else{
00159 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
00160 }
00161 }
00162
00163 if(i==0)
00164 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00165 else
00166 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00167 }
00168 }
00169 }
00170 }else{
00171 for(i=0;i<3;i++) {
00172 uint8_t *ptr;
00173 int x, y, h, v, linesize;
00174 h = s->mjpeg_hsample[i];
00175 v = s->mjpeg_vsample[i];
00176 linesize= p->linesize[i];
00177
00178 for(y=0; y<v; y++){
00179 for(x=0; x<h; x++){
00180 int pred;
00181
00182 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x);
00183
00184 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
00185
00186 if(i==0)
00187 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00188 else
00189 ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00190 }
00191 }
00192 }
00193 }
00194 }
00195 }
00196 }
00197
00198 emms_c();
00199 av_assert0(s->esc_pos == s->header_bits >> 3);
00200 ff_mjpeg_encode_stuffing(s);
00201 ff_mjpeg_encode_picture_trailer(s);
00202 s->picture_number++;
00203
00204 flush_put_bits(&s->pb);
00205 pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
00206 pkt->flags |= AV_PKT_FLAG_KEY;
00207 *got_packet = 1;
00208
00209 return 0;
00210
00211 }
00212
00213
00214 AVCodec ff_ljpeg_encoder = {
00215 .name = "ljpeg",
00216 .type = AVMEDIA_TYPE_VIDEO,
00217 .id = AV_CODEC_ID_LJPEG,
00218 .priv_data_size = sizeof(MpegEncContext),
00219 .init = ff_MPV_encode_init,
00220 .encode2 = encode_picture_lossless,
00221 .close = ff_MPV_encode_end,
00222 .pix_fmts = (const enum PixelFormat[]){
00223 PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_BGR0,
00224 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
00225 PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P,
00226 PIX_FMT_NONE},
00227 .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
00228 };