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_BGR0
00218 || s->avctx->pix_fmt == PIX_FMT_BGRA
00219 || s->avctx->pix_fmt == PIX_FMT_BGR24))
00220 put_bits(&s->pb, 8, 9);
00221 else
00222 put_bits(&s->pb, 8, 8);
00223 put_bits(&s->pb, 16, s->height);
00224 put_bits(&s->pb, 16, s->width);
00225 put_bits(&s->pb, 8, 3);
00226
00227
00228 put_bits(&s->pb, 8, 1);
00229 put_bits(&s->pb, 4, s->mjpeg_hsample[0]);
00230 put_bits(&s->pb, 4, s->mjpeg_vsample[0]);
00231 put_bits(&s->pb, 8, 0);
00232
00233
00234 put_bits(&s->pb, 8, 2);
00235 put_bits(&s->pb, 4, s->mjpeg_hsample[1]);
00236 put_bits(&s->pb, 4, s->mjpeg_vsample[1]);
00237 #ifdef TWOMATRIXES
00238 put_bits(&s->pb, 8, lossless ? 0 : 1);
00239 #else
00240 put_bits(&s->pb, 8, 0);
00241 #endif
00242
00243
00244 put_bits(&s->pb, 8, 3);
00245 put_bits(&s->pb, 4, s->mjpeg_hsample[2]);
00246 put_bits(&s->pb, 4, s->mjpeg_vsample[2]);
00247 #ifdef TWOMATRIXES
00248 put_bits(&s->pb, 8, lossless ? 0 : 1);
00249 #else
00250 put_bits(&s->pb, 8, 0);
00251 #endif
00252
00253
00254 put_marker(&s->pb, SOS);
00255 put_bits(&s->pb, 16, 12);
00256 put_bits(&s->pb, 8, 3);
00257
00258
00259 put_bits(&s->pb, 8, 1);
00260 put_bits(&s->pb, 4, 0);
00261 put_bits(&s->pb, 4, 0);
00262
00263
00264 put_bits(&s->pb, 8, 2);
00265 put_bits(&s->pb, 4, 1);
00266 put_bits(&s->pb, 4, lossless ? 0 : 1);
00267
00268
00269 put_bits(&s->pb, 8, 3);
00270 put_bits(&s->pb, 4, 1);
00271 put_bits(&s->pb, 4, lossless ? 0 : 1);
00272
00273 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0);
00274
00275 switch(s->avctx->codec_id){
00276 case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break;
00277 case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break;
00278 default: assert(0);
00279 }
00280
00281 put_bits(&s->pb, 8, 0);
00282 }
00283
00284 static void escape_FF(MpegEncContext *s, int start)
00285 {
00286 int size= put_bits_count(&s->pb) - start*8;
00287 int i, ff_count;
00288 uint8_t *buf= s->pb.buf + start;
00289 int align= (-(size_t)(buf))&3;
00290
00291 assert((size&7) == 0);
00292 size >>= 3;
00293
00294 ff_count=0;
00295 for(i=0; i<size && i<align; i++){
00296 if(buf[i]==0xFF) ff_count++;
00297 }
00298 for(; i<size-15; i+=16){
00299 int acc, v;
00300
00301 v= *(uint32_t*)(&buf[i]);
00302 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00303 v= *(uint32_t*)(&buf[i+4]);
00304 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00305 v= *(uint32_t*)(&buf[i+8]);
00306 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00307 v= *(uint32_t*)(&buf[i+12]);
00308 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00309
00310 acc>>=4;
00311 acc+= (acc>>16);
00312 acc+= (acc>>8);
00313 ff_count+= acc&0xFF;
00314 }
00315 for(; i<size; i++){
00316 if(buf[i]==0xFF) ff_count++;
00317 }
00318
00319 if(ff_count==0) return;
00320
00321 flush_put_bits(&s->pb);
00322 skip_put_bytes(&s->pb, ff_count);
00323
00324 for(i=size-1; ff_count; i--){
00325 int v= buf[i];
00326
00327 if(v==0xFF){
00328
00329 buf[i+ff_count]= 0;
00330 ff_count--;
00331 }
00332
00333 buf[i+ff_count]= v;
00334 }
00335 }
00336
00337 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
00338 {
00339 int length;
00340 length= (-put_bits_count(pbc))&7;
00341 if(length) put_bits(pbc, length, (1<<length)-1);
00342 }
00343
00344 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
00345 {
00346 ff_mjpeg_encode_stuffing(&s->pb);
00347 flush_put_bits(&s->pb);
00348
00349 assert((s->header_bits&7)==0);
00350
00351 escape_FF(s, s->header_bits>>3);
00352
00353 put_marker(&s->pb, EOI);
00354 }
00355
00356 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
00357 uint8_t *huff_size, uint16_t *huff_code)
00358 {
00359 int mant, nbits;
00360
00361 if (val == 0) {
00362 put_bits(&s->pb, huff_size[0], huff_code[0]);
00363 } else {
00364 mant = val;
00365 if (val < 0) {
00366 val = -val;
00367 mant--;
00368 }
00369
00370 nbits= av_log2_16bit(val) + 1;
00371
00372 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
00373
00374 put_sbits(&s->pb, nbits, mant);
00375 }
00376 }
00377
00378 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
00379 {
00380 int mant, nbits, code, i, j;
00381 int component, dc, run, last_index, val;
00382 MJpegContext *m = s->mjpeg_ctx;
00383 uint8_t *huff_size_ac;
00384 uint16_t *huff_code_ac;
00385
00386
00387 component = (n <= 3 ? 0 : (n&1) + 1);
00388 dc = block[0];
00389 val = dc - s->last_dc[component];
00390 if (n < 4) {
00391 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00392 huff_size_ac = m->huff_size_ac_luminance;
00393 huff_code_ac = m->huff_code_ac_luminance;
00394 } else {
00395 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00396 huff_size_ac = m->huff_size_ac_chrominance;
00397 huff_code_ac = m->huff_code_ac_chrominance;
00398 }
00399 s->last_dc[component] = dc;
00400
00401
00402
00403 run = 0;
00404 last_index = s->block_last_index[n];
00405 for(i=1;i<=last_index;i++) {
00406 j = s->intra_scantable.permutated[i];
00407 val = block[j];
00408 if (val == 0) {
00409 run++;
00410 } else {
00411 while (run >= 16) {
00412 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
00413 run -= 16;
00414 }
00415 mant = val;
00416 if (val < 0) {
00417 val = -val;
00418 mant--;
00419 }
00420
00421 nbits= av_log2(val) + 1;
00422 code = (run << 4) | nbits;
00423
00424 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
00425
00426 put_sbits(&s->pb, nbits, mant);
00427 run = 0;
00428 }
00429 }
00430
00431
00432 if (last_index < 63 || run != 0)
00433 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
00434 }
00435
00436 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
00437 {
00438 int i;
00439 for(i=0;i<5;i++) {
00440 encode_block(s, block[i], i);
00441 }
00442 if (s->chroma_format == CHROMA_420) {
00443 encode_block(s, block[5], 5);
00444 } else {
00445 encode_block(s, block[6], 6);
00446 encode_block(s, block[5], 5);
00447 encode_block(s, block[7], 7);
00448 }
00449
00450 s->i_tex_bits += get_bits_diff(s);
00451 }
00452
00453
00454 #define V_MAX 2
00455 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
00456 const AVFrame *pic_arg, int *got_packet)
00457
00458 {
00459 MpegEncContext *s = avctx->priv_data;
00460 AVFrame pic = *pic_arg;
00461 int i;
00462
00463
00464 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
00465 return -1;
00466
00467
00468 for(i=0; i < 3; i++) {
00469 pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
00470 pic.linesize[i] *= -1;
00471 }
00472 return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
00473 }
00474
00475 AVCodec ff_mjpeg_encoder = {
00476 .name = "mjpeg",
00477 .type = AVMEDIA_TYPE_VIDEO,
00478 .id = CODEC_ID_MJPEG,
00479 .priv_data_size = sizeof(MpegEncContext),
00480 .init = ff_MPV_encode_init,
00481 .encode2 = ff_MPV_encode_picture,
00482 .close = ff_MPV_encode_end,
00483 .pix_fmts = (const enum PixelFormat[]){
00484 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
00485 },
00486 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
00487 };
00488
00489 AVCodec ff_amv_encoder = {
00490 .name = "amv",
00491 .type = AVMEDIA_TYPE_VIDEO,
00492 .id = CODEC_ID_AMV,
00493 .priv_data_size = sizeof(MpegEncContext),
00494 .init = ff_MPV_encode_init,
00495 .encode2 = amv_encode_picture,
00496 .close = ff_MPV_encode_end,
00497 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
00498 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
00499 };