comparison mjpegenc.c @ 5028:3d8a813666e4 libavcodec

split ljpeg encoder out of mjpeg.c
author aurel
date Fri, 18 May 2007 22:42:49 +0000
parents d73237575709
children dbaa06366c3c
comparison
equal deleted inserted replaced
5027:696cda281304 5028:3d8a813666e4
35 35
36 #include "avcodec.h" 36 #include "avcodec.h"
37 #include "dsputil.h" 37 #include "dsputil.h"
38 #include "mpegvideo.h" 38 #include "mpegvideo.h"
39 #include "mjpeg.h" 39 #include "mjpeg.h"
40 #include "mjpegenc.h"
40 41
41 /* use two quantizer tables (one for luminance and one for chrominance) */ 42 /* use two quantizer tables (one for luminance and one for chrominance) */
42 /* not yet working */ 43 /* not yet working */
43 #undef TWOMATRIXES 44 #undef TWOMATRIXES
44
45 typedef struct MJpegContext {
46 uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chrom, for easier addressing
47 uint16_t huff_code_dc_luminance[12];
48 uint8_t huff_size_dc_chrominance[12];
49 uint16_t huff_code_dc_chrominance[12];
50
51 uint8_t huff_size_ac_luminance[256];
52 uint16_t huff_code_ac_luminance[256];
53 uint8_t huff_size_ac_chrominance[256];
54 uint16_t huff_code_ac_chrominance[256];
55 } MJpegContext;
56 45
57 46
58 int mjpeg_init(MpegEncContext *s) 47 int mjpeg_init(MpegEncContext *s)
59 { 48 {
60 MJpegContext *m; 49 MJpegContext *m;
363 escape_FF(s, s->header_bits>>3); 352 escape_FF(s, s->header_bits>>3);
364 353
365 put_marker(&s->pb, EOI); 354 put_marker(&s->pb, EOI);
366 } 355 }
367 356
368 static inline void mjpeg_encode_dc(MpegEncContext *s, int val, 357 void mjpeg_encode_dc(MpegEncContext *s, int val,
369 uint8_t *huff_size, uint16_t *huff_code) 358 uint8_t *huff_size, uint16_t *huff_code)
370 { 359 {
371 int mant, nbits; 360 int mant, nbits;
372 361
373 if (val == 0) { 362 if (val == 0) {
458 encode_block(s, block[6], 6); 447 encode_block(s, block[6], 6);
459 encode_block(s, block[5], 5); 448 encode_block(s, block[5], 5);
460 encode_block(s, block[7], 7); 449 encode_block(s, block[7], 7);
461 } 450 }
462 } 451 }
463
464 static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
465 MpegEncContext * const s = avctx->priv_data;
466 MJpegContext * const m = s->mjpeg_ctx;
467 AVFrame *pict = data;
468 const int width= s->width;
469 const int height= s->height;
470 AVFrame * const p= (AVFrame*)&s->current_picture;
471 const int predictor= avctx->prediction_method+1;
472
473 init_put_bits(&s->pb, buf, buf_size);
474
475 *p = *pict;
476 p->pict_type= FF_I_TYPE;
477 p->key_frame= 1;
478
479 mjpeg_picture_header(s);
480
481 s->header_bits= put_bits_count(&s->pb);
482
483 if(avctx->pix_fmt == PIX_FMT_RGB32){
484 int x, y, i;
485 const int linesize= p->linesize[0];
486 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
487 int left[3], top[3], topleft[3];
488
489 for(i=0; i<3; i++){
490 buffer[0][i]= 1 << (9 - 1);
491 }
492
493 for(y = 0; y < height; y++) {
494 const int modified_predictor= y ? predictor : 1;
495 uint8_t *ptr = p->data[0] + (linesize * y);
496
497 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
498 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
499 return -1;
500 }
501
502 for(i=0; i<3; i++){
503 top[i]= left[i]= topleft[i]= buffer[0][i];
504 }
505 for(x = 0; x < width; x++) {
506 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
507 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
508 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
509
510 for(i=0;i<3;i++) {
511 int pred, diff;
512
513 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
514
515 topleft[i]= top[i];
516 top[i]= buffer[x+1][i];
517
518 left[i]= buffer[x][i];
519
520 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
521
522 if(i==0)
523 mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
524 else
525 mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
526 }
527 }
528 }
529 }else{
530 int mb_x, mb_y, i;
531 const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
532 const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
533
534 for(mb_y = 0; mb_y < mb_height; mb_y++) {
535 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
536 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
537 return -1;
538 }
539 for(mb_x = 0; mb_x < mb_width; mb_x++) {
540 if(mb_x==0 || mb_y==0){
541 for(i=0;i<3;i++) {
542 uint8_t *ptr;
543 int x, y, h, v, linesize;
544 h = s->mjpeg_hsample[i];
545 v = s->mjpeg_vsample[i];
546 linesize= p->linesize[i];
547
548 for(y=0; y<v; y++){
549 for(x=0; x<h; x++){
550 int pred;
551
552 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
553 if(y==0 && mb_y==0){
554 if(x==0 && mb_x==0){
555 pred= 128;
556 }else{
557 pred= ptr[-1];
558 }
559 }else{
560 if(x==0 && mb_x==0){
561 pred= ptr[-linesize];
562 }else{
563 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
564 }
565 }
566
567 if(i==0)
568 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
569 else
570 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
571 }
572 }
573 }
574 }else{
575 for(i=0;i<3;i++) {
576 uint8_t *ptr;
577 int x, y, h, v, linesize;
578 h = s->mjpeg_hsample[i];
579 v = s->mjpeg_vsample[i];
580 linesize= p->linesize[i];
581
582 for(y=0; y<v; y++){
583 for(x=0; x<h; x++){
584 int pred;
585
586 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
587 //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
588 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
589
590 if(i==0)
591 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
592 else
593 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
594 }
595 }
596 }
597 }
598 }
599 }
600 }
601
602 emms_c();
603
604 mjpeg_picture_trailer(s);
605 s->picture_number++;
606
607 flush_put_bits(&s->pb);
608 return pbBufPtr(&s->pb) - s->pb.buf;
609 // return (put_bits_count(&f->pb)+7)/8;
610 }
611
612
613 AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
614 "ljpeg",
615 CODEC_TYPE_VIDEO,
616 CODEC_ID_LJPEG,
617 sizeof(MpegEncContext),
618 MPV_encode_init,
619 encode_picture_lossless,
620 MPV_encode_end,
621 };