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->avctx->sample_aspect_ratio.num )
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, 0x0102);
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
00204 if(s->avctx->codec_id == CODEC_ID_AMV) return;
00205
00206 jpeg_put_comments(s);
00207
00208 jpeg_table_header(s);
00209
00210 switch(s->avctx->codec_id){
00211 case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
00212 case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
00213 default: assert(0);
00214 }
00215
00216 put_bits(&s->pb, 16, 17);
00217 if(lossless && s->avctx->pix_fmt == PIX_FMT_BGRA)
00218 put_bits(&s->pb, 8, 9);
00219 else
00220 put_bits(&s->pb, 8, 8);
00221 put_bits(&s->pb, 16, s->height);
00222 put_bits(&s->pb, 16, s->width);
00223 put_bits(&s->pb, 8, 3);
00224
00225
00226 put_bits(&s->pb, 8, 1);
00227 put_bits(&s->pb, 4, s->mjpeg_hsample[0]);
00228 put_bits(&s->pb, 4, s->mjpeg_vsample[0]);
00229 put_bits(&s->pb, 8, 0);
00230
00231
00232 put_bits(&s->pb, 8, 2);
00233 put_bits(&s->pb, 4, s->mjpeg_hsample[1]);
00234 put_bits(&s->pb, 4, s->mjpeg_vsample[1]);
00235 #ifdef TWOMATRIXES
00236 put_bits(&s->pb, 8, lossless ? 0 : 1);
00237 #else
00238 put_bits(&s->pb, 8, 0);
00239 #endif
00240
00241
00242 put_bits(&s->pb, 8, 3);
00243 put_bits(&s->pb, 4, s->mjpeg_hsample[2]);
00244 put_bits(&s->pb, 4, s->mjpeg_vsample[2]);
00245 #ifdef TWOMATRIXES
00246 put_bits(&s->pb, 8, lossless ? 0 : 1);
00247 #else
00248 put_bits(&s->pb, 8, 0);
00249 #endif
00250
00251
00252 put_marker(&s->pb, SOS);
00253 put_bits(&s->pb, 16, 12);
00254 put_bits(&s->pb, 8, 3);
00255
00256
00257 put_bits(&s->pb, 8, 1);
00258 put_bits(&s->pb, 4, 0);
00259 put_bits(&s->pb, 4, 0);
00260
00261
00262 put_bits(&s->pb, 8, 2);
00263 put_bits(&s->pb, 4, 1);
00264 put_bits(&s->pb, 4, lossless ? 0 : 1);
00265
00266
00267 put_bits(&s->pb, 8, 3);
00268 put_bits(&s->pb, 4, 1);
00269 put_bits(&s->pb, 4, lossless ? 0 : 1);
00270
00271 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0);
00272
00273 switch(s->avctx->codec_id){
00274 case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break;
00275 case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break;
00276 default: assert(0);
00277 }
00278
00279 put_bits(&s->pb, 8, 0);
00280 }
00281
00282 static void escape_FF(MpegEncContext *s, int start)
00283 {
00284 int size= put_bits_count(&s->pb) - start*8;
00285 int i, ff_count;
00286 uint8_t *buf= s->pb.buf + start;
00287 int align= (-(size_t)(buf))&3;
00288
00289 assert((size&7) == 0);
00290 size >>= 3;
00291
00292 ff_count=0;
00293 for(i=0; i<size && i<align; i++){
00294 if(buf[i]==0xFF) ff_count++;
00295 }
00296 for(; i<size-15; i+=16){
00297 int acc, v;
00298
00299 v= *(uint32_t*)(&buf[i]);
00300 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00301 v= *(uint32_t*)(&buf[i+4]);
00302 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00303 v= *(uint32_t*)(&buf[i+8]);
00304 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00305 v= *(uint32_t*)(&buf[i+12]);
00306 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00307
00308 acc>>=4;
00309 acc+= (acc>>16);
00310 acc+= (acc>>8);
00311 ff_count+= acc&0xFF;
00312 }
00313 for(; i<size; i++){
00314 if(buf[i]==0xFF) ff_count++;
00315 }
00316
00317 if(ff_count==0) return;
00318
00319 flush_put_bits(&s->pb);
00320 skip_put_bytes(&s->pb, ff_count);
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 s->i_tex_bits += get_bits_diff(s);
00449 }
00450
00451
00452 #define V_MAX 2
00453 static int amv_encode_picture(AVCodecContext *avctx,
00454 unsigned char *buf, int buf_size, void *data)
00455 {
00456
00457 AVFrame* pic=data;
00458 MpegEncContext *s = avctx->priv_data;
00459 int i;
00460
00461
00462 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
00463 return -1;
00464
00465
00466 for(i=0; i < 3; i++) {
00467 pic->data[i] += (pic->linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
00468 pic->linesize[i] *= -1;
00469 }
00470 return MPV_encode_picture(avctx,buf, buf_size, pic);
00471 }
00472
00473 AVCodec ff_mjpeg_encoder = {
00474 .name = "mjpeg",
00475 .type = AVMEDIA_TYPE_VIDEO,
00476 .id = CODEC_ID_MJPEG,
00477 .priv_data_size = sizeof(MpegEncContext),
00478 .init = MPV_encode_init,
00479 .encode = MPV_encode_picture,
00480 .close = MPV_encode_end,
00481 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
00482 .long_name= NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
00483 };
00484
00485 AVCodec ff_amv_encoder = {
00486 .name = "amv",
00487 .type = AVMEDIA_TYPE_VIDEO,
00488 .id = CODEC_ID_AMV,
00489 .priv_data_size = sizeof(MpegEncContext),
00490 .init = MPV_encode_init,
00491 .encode = amv_encode_picture,
00492 .close = MPV_encode_end,
00493 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, -1},
00494 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
00495 };