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
00034 #include <assert.h>
00035
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 #include "mpegvideo.h"
00039 #include "mjpeg.h"
00040 #include "mjpegenc.h"
00041
00042
00043
00044 #undef TWOMATRIXES
00045
00046
00047 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
00048 {
00049 MJpegContext *m;
00050
00051 m = av_malloc(sizeof(MJpegContext));
00052 if (!m)
00053 return -1;
00054
00055 s->min_qcoeff=-1023;
00056 s->max_qcoeff= 1023;
00057
00058
00059 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
00060 m->huff_code_dc_luminance,
00061 ff_mjpeg_bits_dc_luminance,
00062 ff_mjpeg_val_dc);
00063 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
00064 m->huff_code_dc_chrominance,
00065 ff_mjpeg_bits_dc_chrominance,
00066 ff_mjpeg_val_dc);
00067 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
00068 m->huff_code_ac_luminance,
00069 ff_mjpeg_bits_ac_luminance,
00070 ff_mjpeg_val_ac_luminance);
00071 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
00072 m->huff_code_ac_chrominance,
00073 ff_mjpeg_bits_ac_chrominance,
00074 ff_mjpeg_val_ac_chrominance);
00075
00076 s->mjpeg_ctx = m;
00077 return 0;
00078 }
00079
00080 void ff_mjpeg_encode_close(MpegEncContext *s)
00081 {
00082 av_free(s->mjpeg_ctx);
00083 }
00084
00085
00086 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
00087 const uint8_t *bits_table, const uint8_t *value_table)
00088 {
00089 PutBitContext *p = &s->pb;
00090 int n, i;
00091
00092 put_bits(p, 4, table_class);
00093 put_bits(p, 4, table_id);
00094
00095 n = 0;
00096 for(i=1;i<=16;i++) {
00097 n += bits_table[i];
00098 put_bits(p, 8, bits_table[i]);
00099 }
00100
00101 for(i=0;i<n;i++)
00102 put_bits(p, 8, value_table[i]);
00103
00104 return n + 17;
00105 }
00106
00107 static void jpeg_table_header(MpegEncContext *s)
00108 {
00109 PutBitContext *p = &s->pb;
00110 int i, j, size;
00111 uint8_t *ptr;
00112
00113
00114 put_marker(p, DQT);
00115 #ifdef TWOMATRIXES
00116 put_bits(p, 16, 2 + 2 * (1 + 64));
00117 #else
00118 put_bits(p, 16, 2 + 1 * (1 + 64));
00119 #endif
00120 put_bits(p, 4, 0);
00121 put_bits(p, 4, 0);
00122 for(i=0;i<64;i++) {
00123 j = s->intra_scantable.permutated[i];
00124 put_bits(p, 8, s->intra_matrix[j]);
00125 }
00126 #ifdef TWOMATRIXES
00127 put_bits(p, 4, 0);
00128 put_bits(p, 4, 1);
00129 for(i=0;i<64;i++) {
00130 j = s->intra_scantable.permutated[i];
00131 put_bits(p, 8, s->chroma_intra_matrix[j]);
00132 }
00133 #endif
00134
00135
00136 put_marker(p, DHT);
00137 flush_put_bits(p);
00138 ptr = pbBufPtr(p);
00139 put_bits(p, 16, 0);
00140 size = 2;
00141 size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
00142 ff_mjpeg_val_dc);
00143 size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
00144 ff_mjpeg_val_dc);
00145
00146 size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
00147 ff_mjpeg_val_ac_luminance);
00148 size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
00149 ff_mjpeg_val_ac_chrominance);
00150 AV_WB16(ptr, size);
00151 }
00152
00153 static void jpeg_put_comments(MpegEncContext *s)
00154 {
00155 PutBitContext *p = &s->pb;
00156 int size;
00157 uint8_t *ptr;
00158
00159 if (s->aspect_ratio_info )
00160 {
00161
00162 put_marker(p, APP0);
00163 put_bits(p, 16, 16);
00164 ff_put_string(p, "JFIF", 1);
00165 put_bits(p, 16, 0x0201);
00166 put_bits(p, 8, 0);
00167 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
00168 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
00169 put_bits(p, 8, 0);
00170 put_bits(p, 8, 0);
00171 }
00172
00173
00174 if(!(s->flags & CODEC_FLAG_BITEXACT)){
00175 put_marker(p, COM);
00176 flush_put_bits(p);
00177 ptr = pbBufPtr(p);
00178 put_bits(p, 16, 0);
00179 ff_put_string(p, LIBAVCODEC_IDENT, 1);
00180 size = strlen(LIBAVCODEC_IDENT)+3;
00181 AV_WB16(ptr, size);
00182 }
00183
00184 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
00185 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
00186 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
00187 put_marker(p, COM);
00188 flush_put_bits(p);
00189 ptr = pbBufPtr(p);
00190 put_bits(p, 16, 0);
00191 ff_put_string(p, "CS=ITU601", 1);
00192 size = strlen("CS=ITU601")+3;
00193 AV_WB16(ptr, size);
00194 }
00195 }
00196
00197 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
00198 {
00199 const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
00200
00201 put_marker(&s->pb, SOI);
00202
00203 jpeg_put_comments(s);
00204
00205 jpeg_table_header(s);
00206
00207 switch(s->avctx->codec_id){
00208 case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
00209 case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
00210 default: assert(0);
00211 }
00212
00213 put_bits(&s->pb, 16, 17);
00214 if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32)
00215 put_bits(&s->pb, 8, 9);
00216 else
00217 put_bits(&s->pb, 8, 8);
00218 put_bits(&s->pb, 16, s->height);
00219 put_bits(&s->pb, 16, s->width);
00220 put_bits(&s->pb, 8, 3);
00221
00222
00223 put_bits(&s->pb, 8, 1);
00224 put_bits(&s->pb, 4, s->mjpeg_hsample[0]);
00225 put_bits(&s->pb, 4, s->mjpeg_vsample[0]);
00226 put_bits(&s->pb, 8, 0);
00227
00228
00229 put_bits(&s->pb, 8, 2);
00230 put_bits(&s->pb, 4, s->mjpeg_hsample[1]);
00231 put_bits(&s->pb, 4, s->mjpeg_vsample[1]);
00232 #ifdef TWOMATRIXES
00233 put_bits(&s->pb, 8, lossless ? 0 : 1);
00234 #else
00235 put_bits(&s->pb, 8, 0);
00236 #endif
00237
00238
00239 put_bits(&s->pb, 8, 3);
00240 put_bits(&s->pb, 4, s->mjpeg_hsample[2]);
00241 put_bits(&s->pb, 4, s->mjpeg_vsample[2]);
00242 #ifdef TWOMATRIXES
00243 put_bits(&s->pb, 8, lossless ? 0 : 1);
00244 #else
00245 put_bits(&s->pb, 8, 0);
00246 #endif
00247
00248
00249 put_marker(&s->pb, SOS);
00250 put_bits(&s->pb, 16, 12);
00251 put_bits(&s->pb, 8, 3);
00252
00253
00254 put_bits(&s->pb, 8, 1);
00255 put_bits(&s->pb, 4, 0);
00256 put_bits(&s->pb, 4, 0);
00257
00258
00259 put_bits(&s->pb, 8, 2);
00260 put_bits(&s->pb, 4, 1);
00261 put_bits(&s->pb, 4, lossless ? 0 : 1);
00262
00263
00264 put_bits(&s->pb, 8, 3);
00265 put_bits(&s->pb, 4, 1);
00266 put_bits(&s->pb, 4, lossless ? 0 : 1);
00267
00268 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0);
00269
00270 switch(s->avctx->codec_id){
00271 case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break;
00272 case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break;
00273 default: assert(0);
00274 }
00275
00276 put_bits(&s->pb, 8, 0);
00277 }
00278
00279 static void escape_FF(MpegEncContext *s, int start)
00280 {
00281 int size= put_bits_count(&s->pb) - start*8;
00282 int i, ff_count;
00283 uint8_t *buf= s->pb.buf + start;
00284 int align= (-(size_t)(buf))&3;
00285
00286 assert((size&7) == 0);
00287 size >>= 3;
00288
00289 ff_count=0;
00290 for(i=0; i<size && i<align; i++){
00291 if(buf[i]==0xFF) ff_count++;
00292 }
00293 for(; i<size-15; i+=16){
00294 int acc, v;
00295
00296 v= *(uint32_t*)(&buf[i]);
00297 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00298 v= *(uint32_t*)(&buf[i+4]);
00299 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00300 v= *(uint32_t*)(&buf[i+8]);
00301 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00302 v= *(uint32_t*)(&buf[i+12]);
00303 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00304
00305 acc>>=4;
00306 acc+= (acc>>16);
00307 acc+= (acc>>8);
00308 ff_count+= acc&0xFF;
00309 }
00310 for(; i<size; i++){
00311 if(buf[i]==0xFF) ff_count++;
00312 }
00313
00314 if(ff_count==0) return;
00315
00316
00317 for(i=0; i<ff_count-3; i+=4)
00318 put_bits(&s->pb, 32, 0);
00319 put_bits(&s->pb, (ff_count-i)*8, 0);
00320 flush_put_bits(&s->pb);
00321
00322 for(i=size-1; ff_count; i--){
00323 int v= buf[i];
00324
00325 if(v==0xFF){
00326
00327 buf[i+ff_count]= 0;
00328 ff_count--;
00329 }
00330
00331 buf[i+ff_count]= v;
00332 }
00333 }
00334
00335 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
00336 {
00337 int length;
00338 length= (-put_bits_count(pbc))&7;
00339 if(length) put_bits(pbc, length, (1<<length)-1);
00340 }
00341
00342 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
00343 {
00344 ff_mjpeg_encode_stuffing(&s->pb);
00345 flush_put_bits(&s->pb);
00346
00347 assert((s->header_bits&7)==0);
00348
00349 escape_FF(s, s->header_bits>>3);
00350
00351 put_marker(&s->pb, EOI);
00352 }
00353
00354 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
00355 uint8_t *huff_size, uint16_t *huff_code)
00356 {
00357 int mant, nbits;
00358
00359 if (val == 0) {
00360 put_bits(&s->pb, huff_size[0], huff_code[0]);
00361 } else {
00362 mant = val;
00363 if (val < 0) {
00364 val = -val;
00365 mant--;
00366 }
00367
00368 nbits= av_log2_16bit(val) + 1;
00369
00370 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
00371
00372 put_sbits(&s->pb, nbits, mant);
00373 }
00374 }
00375
00376 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
00377 {
00378 int mant, nbits, code, i, j;
00379 int component, dc, run, last_index, val;
00380 MJpegContext *m = s->mjpeg_ctx;
00381 uint8_t *huff_size_ac;
00382 uint16_t *huff_code_ac;
00383
00384
00385 component = (n <= 3 ? 0 : (n&1) + 1);
00386 dc = block[0];
00387 val = dc - s->last_dc[component];
00388 if (n < 4) {
00389 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00390 huff_size_ac = m->huff_size_ac_luminance;
00391 huff_code_ac = m->huff_code_ac_luminance;
00392 } else {
00393 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00394 huff_size_ac = m->huff_size_ac_chrominance;
00395 huff_code_ac = m->huff_code_ac_chrominance;
00396 }
00397 s->last_dc[component] = dc;
00398
00399
00400
00401 run = 0;
00402 last_index = s->block_last_index[n];
00403 for(i=1;i<=last_index;i++) {
00404 j = s->intra_scantable.permutated[i];
00405 val = block[j];
00406 if (val == 0) {
00407 run++;
00408 } else {
00409 while (run >= 16) {
00410 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
00411 run -= 16;
00412 }
00413 mant = val;
00414 if (val < 0) {
00415 val = -val;
00416 mant--;
00417 }
00418
00419 nbits= av_log2(val) + 1;
00420 code = (run << 4) | nbits;
00421
00422 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
00423
00424 put_sbits(&s->pb, nbits, mant);
00425 run = 0;
00426 }
00427 }
00428
00429
00430 if (last_index < 63 || run != 0)
00431 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
00432 }
00433
00434 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
00435 {
00436 int i;
00437 for(i=0;i<5;i++) {
00438 encode_block(s, block[i], i);
00439 }
00440 if (s->chroma_format == CHROMA_420) {
00441 encode_block(s, block[5], 5);
00442 } else {
00443 encode_block(s, block[6], 6);
00444 encode_block(s, block[5], 5);
00445 encode_block(s, block[7], 7);
00446 }
00447 }
00448
00449 AVCodec mjpeg_encoder = {
00450 "mjpeg",
00451 CODEC_TYPE_VIDEO,
00452 CODEC_ID_MJPEG,
00453 sizeof(MpegEncContext),
00454 MPV_encode_init,
00455 MPV_encode_picture,
00456 MPV_encode_end,
00457 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
00458 .long_name= NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
00459 };