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 = put_bits_ptr(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 = put_bits_ptr(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 = put_bits_ptr(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_BGRA)
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 flush_put_bits(&s->pb);
00317 skip_put_bytes(&s->pb, ff_count);
00318
00319 for(i=size-1; ff_count; i--){
00320 int v= buf[i];
00321
00322 if(v==0xFF){
00323
00324 buf[i+ff_count]= 0;
00325 ff_count--;
00326 }
00327
00328 buf[i+ff_count]= v;
00329 }
00330 }
00331
00332 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
00333 {
00334 int length;
00335 length= (-put_bits_count(pbc))&7;
00336 if(length) put_bits(pbc, length, (1<<length)-1);
00337 }
00338
00339 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
00340 {
00341 ff_mjpeg_encode_stuffing(&s->pb);
00342 flush_put_bits(&s->pb);
00343
00344 assert((s->header_bits&7)==0);
00345
00346 escape_FF(s, s->header_bits>>3);
00347
00348 put_marker(&s->pb, EOI);
00349 }
00350
00351 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
00352 uint8_t *huff_size, uint16_t *huff_code)
00353 {
00354 int mant, nbits;
00355
00356 if (val == 0) {
00357 put_bits(&s->pb, huff_size[0], huff_code[0]);
00358 } else {
00359 mant = val;
00360 if (val < 0) {
00361 val = -val;
00362 mant--;
00363 }
00364
00365 nbits= av_log2_16bit(val) + 1;
00366
00367 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
00368
00369 put_sbits(&s->pb, nbits, mant);
00370 }
00371 }
00372
00373 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
00374 {
00375 int mant, nbits, code, i, j;
00376 int component, dc, run, last_index, val;
00377 MJpegContext *m = s->mjpeg_ctx;
00378 uint8_t *huff_size_ac;
00379 uint16_t *huff_code_ac;
00380
00381
00382 component = (n <= 3 ? 0 : (n&1) + 1);
00383 dc = block[0];
00384 val = dc - s->last_dc[component];
00385 if (n < 4) {
00386 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00387 huff_size_ac = m->huff_size_ac_luminance;
00388 huff_code_ac = m->huff_code_ac_luminance;
00389 } else {
00390 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00391 huff_size_ac = m->huff_size_ac_chrominance;
00392 huff_code_ac = m->huff_code_ac_chrominance;
00393 }
00394 s->last_dc[component] = dc;
00395
00396
00397
00398 run = 0;
00399 last_index = s->block_last_index[n];
00400 for(i=1;i<=last_index;i++) {
00401 j = s->intra_scantable.permutated[i];
00402 val = block[j];
00403 if (val == 0) {
00404 run++;
00405 } else {
00406 while (run >= 16) {
00407 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
00408 run -= 16;
00409 }
00410 mant = val;
00411 if (val < 0) {
00412 val = -val;
00413 mant--;
00414 }
00415
00416 nbits= av_log2(val) + 1;
00417 code = (run << 4) | nbits;
00418
00419 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
00420
00421 put_sbits(&s->pb, nbits, mant);
00422 run = 0;
00423 }
00424 }
00425
00426
00427 if (last_index < 63 || run != 0)
00428 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
00429 }
00430
00431 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
00432 {
00433 int i;
00434 for(i=0;i<5;i++) {
00435 encode_block(s, block[i], i);
00436 }
00437 if (s->chroma_format == CHROMA_420) {
00438 encode_block(s, block[5], 5);
00439 } else {
00440 encode_block(s, block[6], 6);
00441 encode_block(s, block[5], 5);
00442 encode_block(s, block[7], 7);
00443 }
00444 }
00445
00446 AVCodec mjpeg_encoder = {
00447 "mjpeg",
00448 AVMEDIA_TYPE_VIDEO,
00449 CODEC_ID_MJPEG,
00450 sizeof(MpegEncContext),
00451 MPV_encode_init,
00452 MPV_encode_picture,
00453 MPV_encode_end,
00454 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
00455 .long_name= NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
00456 };