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 "mpegvideo.h"
00036 #include "mjpeg.h"
00037 #include "mjpegenc.h"
00038
00039
00040
00041 #undef TWOMATRIXES
00042
00043
00044 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
00045 {
00046 MJpegContext *m;
00047
00048 if (s->width > 65500 || s->height > 65500) {
00049 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
00050 return -1;
00051 }
00052
00053 m = av_malloc(sizeof(MJpegContext));
00054 if (!m)
00055 return -1;
00056
00057 s->min_qcoeff=-1023;
00058 s->max_qcoeff= 1023;
00059
00060
00061 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
00062 m->huff_code_dc_luminance,
00063 avpriv_mjpeg_bits_dc_luminance,
00064 avpriv_mjpeg_val_dc);
00065 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
00066 m->huff_code_dc_chrominance,
00067 avpriv_mjpeg_bits_dc_chrominance,
00068 avpriv_mjpeg_val_dc);
00069 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
00070 m->huff_code_ac_luminance,
00071 avpriv_mjpeg_bits_ac_luminance,
00072 avpriv_mjpeg_val_ac_luminance);
00073 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
00074 m->huff_code_ac_chrominance,
00075 avpriv_mjpeg_bits_ac_chrominance,
00076 avpriv_mjpeg_val_ac_chrominance);
00077
00078 s->mjpeg_ctx = m;
00079 return 0;
00080 }
00081
00082 void ff_mjpeg_encode_close(MpegEncContext *s)
00083 {
00084 av_free(s->mjpeg_ctx);
00085 }
00086
00087
00088 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
00089 const uint8_t *bits_table, const uint8_t *value_table)
00090 {
00091 PutBitContext *p = &s->pb;
00092 int n, i;
00093
00094 put_bits(p, 4, table_class);
00095 put_bits(p, 4, table_id);
00096
00097 n = 0;
00098 for(i=1;i<=16;i++) {
00099 n += bits_table[i];
00100 put_bits(p, 8, bits_table[i]);
00101 }
00102
00103 for(i=0;i<n;i++)
00104 put_bits(p, 8, value_table[i]);
00105
00106 return n + 17;
00107 }
00108
00109 static void jpeg_table_header(MpegEncContext *s)
00110 {
00111 PutBitContext *p = &s->pb;
00112 int i, j, size;
00113 uint8_t *ptr;
00114
00115
00116 put_marker(p, DQT);
00117 #ifdef TWOMATRIXES
00118 put_bits(p, 16, 2 + 2 * (1 + 64));
00119 #else
00120 put_bits(p, 16, 2 + 1 * (1 + 64));
00121 #endif
00122 put_bits(p, 4, 0);
00123 put_bits(p, 4, 0);
00124 for(i=0;i<64;i++) {
00125 j = s->intra_scantable.permutated[i];
00126 put_bits(p, 8, s->intra_matrix[j]);
00127 }
00128 #ifdef TWOMATRIXES
00129 put_bits(p, 4, 0);
00130 put_bits(p, 4, 1);
00131 for(i=0;i<64;i++) {
00132 j = s->intra_scantable.permutated[i];
00133 put_bits(p, 8, s->chroma_intra_matrix[j]);
00134 }
00135 #endif
00136
00137 if(s->avctx->active_thread_type & FF_THREAD_SLICE){
00138 put_marker(p, DRI);
00139 put_bits(p, 16, 4);
00140 put_bits(p, 16, s->mb_width);
00141 }
00142
00143
00144 put_marker(p, DHT);
00145 flush_put_bits(p);
00146 ptr = put_bits_ptr(p);
00147 put_bits(p, 16, 0);
00148 size = 2;
00149 size += put_huffman_table(s, 0, 0, avpriv_mjpeg_bits_dc_luminance,
00150 avpriv_mjpeg_val_dc);
00151 size += put_huffman_table(s, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
00152 avpriv_mjpeg_val_dc);
00153
00154 size += put_huffman_table(s, 1, 0, avpriv_mjpeg_bits_ac_luminance,
00155 avpriv_mjpeg_val_ac_luminance);
00156 size += put_huffman_table(s, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
00157 avpriv_mjpeg_val_ac_chrominance);
00158 AV_WB16(ptr, size);
00159 }
00160
00161 static void jpeg_put_comments(MpegEncContext *s)
00162 {
00163 PutBitContext *p = &s->pb;
00164 int size;
00165 uint8_t *ptr;
00166
00167 if (s->avctx->sample_aspect_ratio.num )
00168 {
00169
00170 put_marker(p, APP0);
00171 put_bits(p, 16, 16);
00172 avpriv_put_string(p, "JFIF", 1);
00173 put_bits(p, 16, 0x0102);
00174 put_bits(p, 8, 0);
00175 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
00176 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
00177 put_bits(p, 8, 0);
00178 put_bits(p, 8, 0);
00179 }
00180
00181
00182 if(!(s->flags & CODEC_FLAG_BITEXACT)){
00183 put_marker(p, COM);
00184 flush_put_bits(p);
00185 ptr = put_bits_ptr(p);
00186 put_bits(p, 16, 0);
00187 avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
00188 size = strlen(LIBAVCODEC_IDENT)+3;
00189 AV_WB16(ptr, size);
00190 }
00191
00192 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
00193 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
00194 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
00195 put_marker(p, COM);
00196 flush_put_bits(p);
00197 ptr = put_bits_ptr(p);
00198 put_bits(p, 16, 0);
00199 avpriv_put_string(p, "CS=ITU601", 1);
00200 size = strlen("CS=ITU601")+3;
00201 AV_WB16(ptr, size);
00202 }
00203 }
00204
00205 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
00206 {
00207 const int lossless= s->avctx->codec_id != AV_CODEC_ID_MJPEG;
00208 int i;
00209
00210 put_marker(&s->pb, SOI);
00211
00212
00213 if(s->avctx->codec_id == AV_CODEC_ID_AMV) goto end;
00214
00215 jpeg_put_comments(s);
00216
00217 jpeg_table_header(s);
00218
00219 switch(s->avctx->codec_id){
00220 case AV_CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
00221 case AV_CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
00222 default: av_assert0(0);
00223 }
00224
00225 put_bits(&s->pb, 16, 17);
00226 if(lossless && (s->avctx->pix_fmt == PIX_FMT_BGR0
00227 || s->avctx->pix_fmt == PIX_FMT_BGRA
00228 || s->avctx->pix_fmt == PIX_FMT_BGR24))
00229 put_bits(&s->pb, 8, 9);
00230 else
00231 put_bits(&s->pb, 8, 8);
00232 put_bits(&s->pb, 16, s->height);
00233 put_bits(&s->pb, 16, s->width);
00234 put_bits(&s->pb, 8, 3);
00235
00236
00237 put_bits(&s->pb, 8, 1);
00238 put_bits(&s->pb, 4, s->mjpeg_hsample[0]);
00239 put_bits(&s->pb, 4, s->mjpeg_vsample[0]);
00240 put_bits(&s->pb, 8, 0);
00241
00242
00243 put_bits(&s->pb, 8, 2);
00244 put_bits(&s->pb, 4, s->mjpeg_hsample[1]);
00245 put_bits(&s->pb, 4, s->mjpeg_vsample[1]);
00246 #ifdef TWOMATRIXES
00247 put_bits(&s->pb, 8, lossless ? 0 : 1);
00248 #else
00249 put_bits(&s->pb, 8, 0);
00250 #endif
00251
00252
00253 put_bits(&s->pb, 8, 3);
00254 put_bits(&s->pb, 4, s->mjpeg_hsample[2]);
00255 put_bits(&s->pb, 4, s->mjpeg_vsample[2]);
00256 #ifdef TWOMATRIXES
00257 put_bits(&s->pb, 8, lossless ? 0 : 1);
00258 #else
00259 put_bits(&s->pb, 8, 0);
00260 #endif
00261
00262
00263 put_marker(&s->pb, SOS);
00264 put_bits(&s->pb, 16, 12);
00265 put_bits(&s->pb, 8, 3);
00266
00267
00268 put_bits(&s->pb, 8, 1);
00269 put_bits(&s->pb, 4, 0);
00270 put_bits(&s->pb, 4, 0);
00271
00272
00273 put_bits(&s->pb, 8, 2);
00274 put_bits(&s->pb, 4, 1);
00275 put_bits(&s->pb, 4, lossless ? 0 : 1);
00276
00277
00278 put_bits(&s->pb, 8, 3);
00279 put_bits(&s->pb, 4, 1);
00280 put_bits(&s->pb, 4, lossless ? 0 : 1);
00281
00282 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0);
00283
00284 switch(s->avctx->codec_id){
00285 case AV_CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break;
00286 case AV_CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break;
00287 default: av_assert0(0);
00288 }
00289
00290 put_bits(&s->pb, 8, 0);
00291
00292 end:
00293 s->esc_pos = put_bits_count(&s->pb) >> 3;
00294 for(i=1; i<s->slice_context_count; i++)
00295 s->thread_context[i]->esc_pos = 0;
00296 }
00297
00298 static void escape_FF(MpegEncContext *s, int start)
00299 {
00300 int size= put_bits_count(&s->pb) - start*8;
00301 int i, ff_count;
00302 uint8_t *buf= s->pb.buf + start;
00303 int align= (-(size_t)(buf))&3;
00304
00305 av_assert1((size&7) == 0);
00306 size >>= 3;
00307
00308 ff_count=0;
00309 for(i=0; i<size && i<align; i++){
00310 if(buf[i]==0xFF) ff_count++;
00311 }
00312 for(; i<size-15; i+=16){
00313 int acc, v;
00314
00315 v= *(uint32_t*)(&buf[i]);
00316 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00317 v= *(uint32_t*)(&buf[i+4]);
00318 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00319 v= *(uint32_t*)(&buf[i+8]);
00320 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00321 v= *(uint32_t*)(&buf[i+12]);
00322 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00323
00324 acc>>=4;
00325 acc+= (acc>>16);
00326 acc+= (acc>>8);
00327 ff_count+= acc&0xFF;
00328 }
00329 for(; i<size; i++){
00330 if(buf[i]==0xFF) ff_count++;
00331 }
00332
00333 if(ff_count==0) return;
00334
00335 flush_put_bits(&s->pb);
00336 skip_put_bytes(&s->pb, ff_count);
00337
00338 for(i=size-1; ff_count; i--){
00339 int v= buf[i];
00340
00341 if(v==0xFF){
00342
00343 buf[i+ff_count]= 0;
00344 ff_count--;
00345 }
00346
00347 buf[i+ff_count]= v;
00348 }
00349 }
00350
00351 void ff_mjpeg_encode_stuffing(MpegEncContext *s)
00352 {
00353 int length, i;
00354 PutBitContext *pbc = &s->pb;
00355 int mb_y = s->mb_y - !s->mb_x;
00356 length= (-put_bits_count(pbc))&7;
00357 if(length) put_bits(pbc, length, (1<<length)-1);
00358
00359 flush_put_bits(&s->pb);
00360 escape_FF(s, s->esc_pos);
00361
00362 if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
00363 put_marker(pbc, RST0 + (mb_y&7));
00364 s->esc_pos = put_bits_count(pbc) >> 3;
00365
00366 for(i=0; i<3; i++)
00367 s->last_dc[i] = 128 << s->intra_dc_precision;
00368 }
00369
00370 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
00371 {
00372
00373 av_assert1((s->header_bits&7)==0);
00374
00375
00376 put_marker(&s->pb, EOI);
00377 }
00378
00379 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
00380 uint8_t *huff_size, uint16_t *huff_code)
00381 {
00382 int mant, nbits;
00383
00384 if (val == 0) {
00385 put_bits(&s->pb, huff_size[0], huff_code[0]);
00386 } else {
00387 mant = val;
00388 if (val < 0) {
00389 val = -val;
00390 mant--;
00391 }
00392
00393 nbits= av_log2_16bit(val) + 1;
00394
00395 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
00396
00397 put_sbits(&s->pb, nbits, mant);
00398 }
00399 }
00400
00401 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
00402 {
00403 int mant, nbits, code, i, j;
00404 int component, dc, run, last_index, val;
00405 MJpegContext *m = s->mjpeg_ctx;
00406 uint8_t *huff_size_ac;
00407 uint16_t *huff_code_ac;
00408
00409
00410 component = (n <= 3 ? 0 : (n&1) + 1);
00411 dc = block[0];
00412 val = dc - s->last_dc[component];
00413 if (n < 4) {
00414 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00415 huff_size_ac = m->huff_size_ac_luminance;
00416 huff_code_ac = m->huff_code_ac_luminance;
00417 } else {
00418 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00419 huff_size_ac = m->huff_size_ac_chrominance;
00420 huff_code_ac = m->huff_code_ac_chrominance;
00421 }
00422 s->last_dc[component] = dc;
00423
00424
00425
00426 run = 0;
00427 last_index = s->block_last_index[n];
00428 for(i=1;i<=last_index;i++) {
00429 j = s->intra_scantable.permutated[i];
00430 val = block[j];
00431 if (val == 0) {
00432 run++;
00433 } else {
00434 while (run >= 16) {
00435 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
00436 run -= 16;
00437 }
00438 mant = val;
00439 if (val < 0) {
00440 val = -val;
00441 mant--;
00442 }
00443
00444 nbits= av_log2(val) + 1;
00445 code = (run << 4) | nbits;
00446
00447 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
00448
00449 put_sbits(&s->pb, nbits, mant);
00450 run = 0;
00451 }
00452 }
00453
00454
00455 if (last_index < 63 || run != 0)
00456 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
00457 }
00458
00459 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
00460 {
00461 int i;
00462 for(i=0;i<5;i++) {
00463 encode_block(s, block[i], i);
00464 }
00465 if (s->chroma_format == CHROMA_420) {
00466 encode_block(s, block[5], 5);
00467 } else {
00468 encode_block(s, block[6], 6);
00469 encode_block(s, block[5], 5);
00470 encode_block(s, block[7], 7);
00471 }
00472
00473 s->i_tex_bits += get_bits_diff(s);
00474 }
00475
00476
00477 #define V_MAX 2
00478 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
00479 const AVFrame *pic_arg, int *got_packet)
00480
00481 {
00482 MpegEncContext *s = avctx->priv_data;
00483 AVFrame pic = *pic_arg;
00484 int i;
00485
00486
00487 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
00488 return -1;
00489
00490
00491 for(i=0; i < 3; i++) {
00492 pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
00493 pic.linesize[i] *= -1;
00494 }
00495 return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
00496 }
00497
00498 #if CONFIG_MJPEG_ENCODER
00499 AVCodec ff_mjpeg_encoder = {
00500 .name = "mjpeg",
00501 .type = AVMEDIA_TYPE_VIDEO,
00502 .id = AV_CODEC_ID_MJPEG,
00503 .priv_data_size = sizeof(MpegEncContext),
00504 .init = ff_MPV_encode_init,
00505 .encode2 = ff_MPV_encode_picture,
00506 .close = ff_MPV_encode_end,
00507 .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
00508 .pix_fmts = (const enum PixelFormat[]){
00509 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
00510 },
00511 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
00512 };
00513 #endif
00514 #if CONFIG_AMV_ENCODER
00515 AVCodec ff_amv_encoder = {
00516 .name = "amv",
00517 .type = AVMEDIA_TYPE_VIDEO,
00518 .id = AV_CODEC_ID_AMV,
00519 .priv_data_size = sizeof(MpegEncContext),
00520 .init = ff_MPV_encode_init,
00521 .encode2 = amv_encode_picture,
00522 .close = ff_MPV_encode_end,
00523 .pix_fmts = (const enum PixelFormat[]){
00524 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
00525 },
00526 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
00527 };
00528 #endif