Mercurial > libavcodec.hg
comparison dv.c @ 1493:ad7e62df9962 libavcodec
* preAlpha DV encoding support -- there's still a truckload
of work to do, but it least people can try it out and share
ideas. Please don't hesitate to give it a spin:
$ ffmpeg -i file.avi file.dv
is all you need.
* fix for a deallocation bug in DV muxer
author | romansh |
---|---|
date | Wed, 01 Oct 2003 23:34:46 +0000 |
parents | 337d13aee605 |
children | f4cf270b98ae |
comparison
equal
deleted
inserted
replaced
1492:3518325146fc | 1493:ad7e62df9962 |
---|---|
1 /* | 1 /* |
2 * DV decoder | 2 * DV decoder |
3 * Copyright (c) 2002 Fabrice Bellard. | 3 * Copyright (c) 2002 Fabrice Bellard. |
4 * | |
5 * DV encoder | |
6 * Copyright (c) 2003 Roman Shaposhnik. | |
7 * | |
8 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth | |
9 * of DV technical info. | |
4 * | 10 * |
5 * This library is free software; you can redistribute it and/or | 11 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | 12 * modify it under the terms of the GNU Lesser General Public |
7 * License as published by the Free Software Foundation; either | 13 * License as published by the Free Software Foundation; either |
8 * version 2 of the License, or (at your option) any later version. | 14 * version 2 of the License, or (at your option) any later version. |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 */ | 24 */ |
19 | 25 |
20 /** | 26 /** |
21 * @file dv.c | 27 * @file dv.c |
22 * DV decoder. | 28 * DV codec. |
23 */ | 29 */ |
24 #include "avcodec.h" | 30 #include "avcodec.h" |
25 #include "dsputil.h" | 31 #include "dsputil.h" |
26 #include "mpegvideo.h" | 32 #include "mpegvideo.h" |
27 #include "simple_idct.h" | 33 #include "simple_idct.h" |
70 dv_quant_shifts[q][dv_248_areas[i]] + 1; | 76 dv_quant_shifts[q][dv_248_areas[i]] + 1; |
71 } | 77 } |
72 } | 78 } |
73 } | 79 } |
74 | 80 |
75 static int dvvideo_decode_init(AVCodecContext *avctx) | 81 static int dvvideo_init(AVCodecContext *avctx) |
76 { | 82 { |
77 DVVideoDecodeContext *s = avctx->priv_data; | 83 DVVideoDecodeContext *s = avctx->priv_data; |
78 MpegEncContext s2; | 84 MpegEncContext s2; |
79 static int done=0; | 85 static int done=0; |
80 | 86 |
138 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64); | 144 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64); |
139 | 145 |
140 /* XXX: do it only for constant case */ | 146 /* XXX: do it only for constant case */ |
141 dv_build_unquantize_tables(s); | 147 dv_build_unquantize_tables(s); |
142 | 148 |
149 /* FIXME: I really don't think this should be here */ | |
150 if (dv_codec_profile(avctx)) | |
151 avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt; | |
152 | |
143 return 0; | 153 return 0; |
144 } | 154 } |
145 | 155 |
146 // #define VLC_DEBUG | 156 // #define VLC_DEBUG |
147 | 157 |
366 last_index = block_sizes[j]; | 376 last_index = block_sizes[j]; |
367 buf_ptr += last_index >> 3; | 377 buf_ptr += last_index >> 3; |
368 mb->pos = 0; | 378 mb->pos = 0; |
369 mb->partial_bit_count = 0; | 379 mb->partial_bit_count = 0; |
370 | 380 |
381 #ifdef VLC_DEBUG | |
382 printf("MB block: %d, %d ", mb_index, j); | |
383 #endif | |
371 dv_decode_ac(s, mb, block, last_index); | 384 dv_decode_ac(s, mb, block, last_index); |
372 | 385 |
373 /* write the remaining bits in a new buffer only if the | 386 /* write the remaining bits in a new buffer only if the |
374 block is finished */ | 387 block is finished */ |
375 bits_left = last_index - get_bits_count(&s->gb); | 388 bits_left = last_index - get_bits_count(&s->gb); |
389 | 402 |
390 flush_put_bits(&pb); | 403 flush_put_bits(&pb); |
391 | 404 |
392 /* pass 2 : we can do it just after */ | 405 /* pass 2 : we can do it just after */ |
393 #ifdef VLC_DEBUG | 406 #ifdef VLC_DEBUG |
394 printf("***pass 2 size=%d\n", mb_bit_count); | 407 printf("***pass 2 size=%d MB#=%d\n", mb_bit_count, mb_index); |
395 #endif | 408 #endif |
396 block = block1; | 409 block = block1; |
397 mb = mb1; | 410 mb = mb1; |
398 init_get_bits(&s->gb, mb_bit_buffer, 80*8); | 411 init_get_bits(&s->gb, mb_bit_buffer, 80*8); |
399 for(j = 0;j < 6; j++) { | 412 for(j = 0;j < 6; j++) { |
499 mb++; | 512 mb++; |
500 } | 513 } |
501 } | 514 } |
502 } | 515 } |
503 | 516 |
517 /* Converts run and level (where level != 0) pair into vlc, returning bit size */ | |
518 static inline int dv_rl2vlc(int run, int l, uint32_t* vlc) | |
519 { | |
520 int sign = l >> 8; | |
521 int level = (l ^ sign) - sign; | |
522 int size; | |
523 | |
524 sign = (sign & 1); | |
525 | |
526 if (run < 15 && level < 23 && dv_vlc_codes[run][level] != -1) { | |
527 *vlc = (dv_vlc_bits[dv_vlc_codes[run][level]] << 1) | sign; | |
528 size = dv_vlc_len[dv_vlc_codes[run][level]] + 1; | |
529 } | |
530 else { | |
531 if (level < 23) { | |
532 *vlc = (dv_vlc_bits[dv_vlc_codes[0][level]] << 1) | sign; | |
533 size = dv_vlc_len[dv_vlc_codes[0][level]] + 1; | |
534 } else { | |
535 *vlc = 0xfe00 | (level << 1) | sign; | |
536 size = 16; | |
537 } | |
538 | |
539 switch(run) { | |
540 case 0: | |
541 break; | |
542 case 1: | |
543 case 2: | |
544 *vlc |= ((0x7ce | (run - 1)) << size); | |
545 size += 11; | |
546 break; | |
547 case 3: | |
548 case 4: | |
549 case 5: | |
550 case 6: | |
551 *vlc |= ((0xfac | (run - 3)) << size); | |
552 size += 12; | |
553 break; | |
554 default: | |
555 *vlc |= ((0x1f80 | (run - 1)) << size); | |
556 size += 13; | |
557 break; | |
558 } | |
559 } | |
560 | |
561 return size; | |
562 } | |
563 | |
564 typedef struct EncBlockInfo { | |
565 int qno; | |
566 int cno; | |
567 int dct_mode; | |
568 int block_size; | |
569 DCTELEM *mb; | |
570 PutBitContext pb; | |
571 } EncBlockInfo; | |
572 | |
573 static inline int dv_bits_left(EncBlockInfo* bi) | |
574 { | |
575 return (bi->block_size - get_bit_count(&bi->pb)); | |
576 } | |
577 | |
578 static inline void dv_encode_ac(EncBlockInfo* bi, PutBitContext* heap) | |
579 { | |
580 int i, level, size, run = 0; | |
581 uint32_t vlc; | |
582 PutBitContext* cpb = &bi->pb; | |
583 | |
584 for (i=1; i<64; i++) { | |
585 level = bi->mb[ff_zigzag_direct[i]] / | |
586 (1<<(dv_quant_shifts[bi->qno + dv_quant_offset[bi->cno]] | |
587 [dv_88_areas[ff_zigzag_direct[i]]] + 4 + (bi->cno == 3))); | |
588 if (level != 0) { | |
589 size = dv_rl2vlc(run, level, &vlc); | |
590 put_vlc: | |
591 | |
592 #ifdef VLC_DEBUG | |
593 printf(" %3d:%3d", run, level); | |
594 #endif | |
595 if (cpb == &bi->pb && size > dv_bits_left(bi)) { | |
596 size -= dv_bits_left(bi); | |
597 put_bits(cpb, dv_bits_left(bi), vlc >> size); | |
598 vlc = vlc & ((1<<size)-1); | |
599 cpb = heap; | |
600 } | |
601 put_bits(cpb, size, vlc); | |
602 run = 0; | |
603 } else | |
604 run++; | |
605 } | |
606 | |
607 if (i == 64) { | |
608 size = 4; vlc = 6; /* End Of Block stamp */ | |
609 goto put_vlc; | |
610 } | |
611 } | |
612 | |
613 static inline void dv_redistr_bits(EncBlockInfo* bi, int count, uint8_t* extra_data, int extra_bits, PutBitContext* heap) | |
614 { | |
615 int i; | |
616 GetBitContext gb; | |
617 | |
618 init_get_bits(&gb, extra_data, extra_bits); | |
619 | |
620 for (i=0; i<count; i++) { | |
621 int bits_left = dv_bits_left(bi); | |
622 #ifdef VLC_DEBUG | |
623 if (bits_left) | |
624 printf("------------> inserting %d bytes in %d:%d\n", bits_left, i/6, i%6); | |
625 #endif | |
626 if (bits_left > extra_bits) { | |
627 bit_copy(&bi->pb, &gb, extra_bits); | |
628 extra_bits = 0; | |
629 break; | |
630 } else | |
631 bit_copy(&bi->pb, &gb, bits_left); | |
632 | |
633 extra_bits -= bits_left; | |
634 bi++; | |
635 } | |
636 | |
637 if (extra_bits > 0 && heap) | |
638 bit_copy(heap, &gb, extra_bits); | |
639 } | |
640 | |
641 static inline void dv_set_class_number(EncBlockInfo* bi, int j) | |
642 { | |
643 int i, max_ac = 0; | |
644 | |
645 for (i=1; i<64; i++) { | |
646 int ac = abs(bi->mb[ff_zigzag_direct[i]]) / 4; | |
647 if (max_ac < ac) | |
648 max_ac = ac; | |
649 } | |
650 if (max_ac < 12) | |
651 bi->cno = j; | |
652 else if (max_ac < 24) | |
653 bi->cno = j + 1; | |
654 else if (max_ac < 36) | |
655 bi->cno = j + 2; | |
656 else | |
657 bi->cno = j + 3; | |
658 | |
659 if (bi->cno > 3) | |
660 bi->cno = 3; | |
661 } | |
662 | |
663 /* | |
664 * This is a very rough initial implementaion. The performance is | |
665 * horrible and some features are missing, mainly 2-4-8 DCT encoding. | |
666 * The weighting is missing as well, but it's missing from the decoding | |
667 * step also -- so at least we're on the same page with decoder ;-) | |
668 */ | |
669 static inline void dv_encode_video_segment(DVVideoDecodeContext *s, | |
670 uint8_t *dif, | |
671 const uint16_t *mb_pos_ptr) | |
672 { | |
673 int mb_index, i, j, v; | |
674 int mb_x, mb_y, c_offset, linesize; | |
675 uint8_t* y_ptr; | |
676 uint8_t* data; | |
677 int do_edge_wrap; | |
678 DCTELEM *block; | |
679 EncBlockInfo enc_blks[5*6]; | |
680 EncBlockInfo* enc_blk; | |
681 int free_vs_bits; | |
682 int extra_bits; | |
683 PutBitContext extra_vs; | |
684 uint8_t extra_vs_data[5*6*128]; | |
685 uint8_t extra_mb_data[6*128]; | |
686 | |
687 int QNO = 15; | |
688 | |
689 /* Stage 1 -- doing DCT on 5 MBs */ | |
690 block = &s->block[0][0]; | |
691 for(mb_index = 0; mb_index < 5; mb_index++) { | |
692 v = *mb_pos_ptr++; | |
693 mb_x = v & 0xff; | |
694 mb_y = v >> 8; | |
695 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8); | |
696 c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ? | |
697 ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) : | |
698 (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8)); | |
699 do_edge_wrap = 0; | |
700 for(j = 0;j < 6; j++) { | |
701 if (j < 4) { /* Four Y blocks */ | |
702 /* NOTE: at end of line, the macroblock is handled as 420 */ | |
703 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) { | |
704 data = y_ptr + (j * 8); | |
705 } else { | |
706 data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]); | |
707 } | |
708 linesize = s->picture.linesize[0]; | |
709 } else { /* Cr and Cb blocks */ | |
710 /* don't ask Fabrice why they inverted Cb and Cr ! */ | |
711 data = s->picture.data[6 - j] + c_offset; | |
712 linesize = s->picture.linesize[6 - j]; | |
713 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) | |
714 do_edge_wrap = 1; | |
715 } | |
716 | |
717 /* Everything is set up -- now just copy data -> DCT block */ | |
718 if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */ | |
719 uint8_t* d; | |
720 DCTELEM *b = block; | |
721 for (i=0;i<8;i++) { | |
722 d = data + 8 * linesize; | |
723 b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3]; | |
724 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3]; | |
725 data += linesize; | |
726 b += 8; | |
727 } | |
728 } else { /* Simple copy: 8x8 -> 8x8 */ | |
729 s->get_pixels(block, data, linesize); | |
730 } | |
731 | |
732 s->fdct(block); | |
733 | |
734 block += 64; | |
735 } | |
736 } | |
737 | |
738 /* Stage 2 -- setup for encoding phase */ | |
739 enc_blk = &enc_blks[0]; | |
740 block = &s->block[0][0]; | |
741 for (i=0; i<5; i++) { | |
742 for (j=0; j<6; j++) { | |
743 enc_blk->mb = block; | |
744 enc_blk->dct_mode = 0; | |
745 enc_blk->block_size = block_sizes[j]; | |
746 | |
747 dv_set_class_number(enc_blk, j/4*(j%2)); | |
748 | |
749 block += 64; | |
750 enc_blk++; | |
751 } | |
752 } | |
753 | |
754 /* Stage 3 -- encoding by trial-and-error */ | |
755 encode_vs: | |
756 enc_blk = &enc_blks[0]; | |
757 for (i=0; i<5; i++) { | |
758 uint8_t* p = dif + i*80 + 4; | |
759 for (j=0; j<6; j++) { | |
760 enc_blk->qno = QNO; | |
761 init_put_bits(&enc_blk->pb, p, block_sizes[j]/8, NULL, NULL); | |
762 enc_blk++; | |
763 p += block_sizes[j]/8; | |
764 } | |
765 } | |
766 | |
767 init_put_bits(&extra_vs, extra_vs_data, sizeof(extra_vs_data), NULL, NULL); | |
768 free_vs_bits = 0; | |
769 enc_blk = &enc_blks[0]; | |
770 for (i=0; i<5; i++) { | |
771 PutBitContext extra_mb; | |
772 EncBlockInfo* enc_blk2 = enc_blk; | |
773 int free_mb_bits = 0; | |
774 | |
775 init_put_bits(&extra_mb, extra_mb_data, sizeof(extra_mb_data), NULL, NULL); | |
776 dif[i*80 + 3] = enc_blk->qno; | |
777 | |
778 for (j=0; j<6; j++) { | |
779 uint16_t dc = ((enc_blk->mb[0] >> 3) - 1024) >> 2; | |
780 | |
781 put_bits(&enc_blk->pb, 9, dc); | |
782 put_bits(&enc_blk->pb, 1, enc_blk->dct_mode); | |
783 put_bits(&enc_blk->pb, 2, enc_blk->cno); | |
784 | |
785 #ifdef VLC_DEBUG | |
786 printf("[%d, %d]: ", i, j); | |
787 #endif | |
788 dv_encode_ac(enc_blk, &extra_mb); | |
789 #ifdef VLC_DEBUG | |
790 printf("\n"); | |
791 #endif | |
792 | |
793 free_mb_bits += dv_bits_left(enc_blk); | |
794 enc_blk++; | |
795 } | |
796 | |
797 /* We can't flush extra_mb just yet -- since it'll round up bit number */ | |
798 extra_bits = get_bit_count(&extra_mb); | |
799 if (free_mb_bits > extra_bits) | |
800 free_vs_bits += free_mb_bits - extra_bits; | |
801 | |
802 if (extra_bits) { /* FIXME: speed up things when free_mb_bits == 0 */ | |
803 flush_put_bits(&extra_mb); | |
804 dv_redistr_bits(enc_blk2, 6, extra_mb_data, extra_bits, &extra_vs); | |
805 } | |
806 } | |
807 | |
808 /* We can't flush extra_mb just yet -- since it'll round up bit number */ | |
809 extra_bits = get_bit_count(&extra_vs); | |
810 if (extra_bits > free_vs_bits && QNO) { /* FIXME: very crude trial-and-error */ | |
811 QNO--; | |
812 goto encode_vs; | |
813 } | |
814 | |
815 if (extra_bits) { | |
816 flush_put_bits(&extra_vs); | |
817 dv_redistr_bits(&enc_blks[0], 5*6, extra_vs_data, extra_bits, NULL); | |
818 } | |
819 | |
820 for (i=0; i<6*5; i++) { | |
821 flush_put_bits(&enc_blks[i].pb); | |
822 #ifdef VLC_DEBUG | |
823 printf("[%d:%d] qno=%d cno=%d\n", i/6, i%6, enc_blks[i].qno, enc_blks[i].cno); | |
824 #endif | |
825 } | |
826 } | |
827 | |
504 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, | 828 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, |
505 144000 bytes for PAL) */ | 829 144000 bytes for PAL) */ |
506 static int dvvideo_decode_frame(AVCodecContext *avctx, | 830 static int dvvideo_decode_frame(AVCodecContext *avctx, |
507 void *data, int *data_size, | 831 void *data, int *data_size, |
508 uint8_t *buf, int buf_size) | 832 uint8_t *buf, int buf_size) |
533 | 857 |
534 for(vs = 0; vs < 27; vs++) { | 858 for(vs = 0; vs < 27; vs++) { |
535 if ((vs % 3) == 0) | 859 if ((vs % 3) == 0) |
536 buf += 80; /* skip audio block */ | 860 buf += 80; /* skip audio block */ |
537 | 861 |
862 #ifdef VLC_DEBUG | |
863 printf("********************* %d, %d **********************\n", ds, vs); | |
864 #endif | |
538 dv_decode_video_segment(s, buf, mb_pos_ptr); | 865 dv_decode_video_segment(s, buf, mb_pos_ptr); |
539 buf += 5 * 80; | 866 buf += 5 * 80; |
540 mb_pos_ptr += 5; | 867 mb_pos_ptr += 5; |
541 } | 868 } |
542 } | 869 } |
548 *(AVFrame*)data= s->picture; | 875 *(AVFrame*)data= s->picture; |
549 | 876 |
550 return s->sys->frame_size; | 877 return s->sys->frame_size; |
551 } | 878 } |
552 | 879 |
553 static int dvvideo_decode_end(AVCodecContext *avctx) | 880 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size, |
881 void *data) | |
882 { | |
883 DVVideoDecodeContext *s = c->priv_data; | |
884 const uint16_t *mb_pos_ptr; | |
885 int ds, vs; | |
886 | |
887 s->sys = dv_codec_profile(c); | |
888 if (!s->sys) | |
889 return -1; | |
890 | |
891 c->pix_fmt = s->sys->pix_fmt; | |
892 s->picture = *((AVFrame *)data); | |
893 | |
894 /* for each DIF segment */ | |
895 mb_pos_ptr = s->sys->video_place; | |
896 for (ds = 0; ds < s->sys->difseg_size; ds++) { | |
897 buf += 6 * 80; /* skip DIF segment header */ | |
898 | |
899 for(vs = 0; vs < 27; vs++) { | |
900 if ((vs % 3) == 0) | |
901 buf += 80; /* skip audio block */ | |
902 | |
903 #ifdef VLC_DEBUG | |
904 printf("********************* %d, %d **********************\n", ds, vs); | |
905 #endif | |
906 dv_encode_video_segment(s, buf, mb_pos_ptr); | |
907 buf += 5 * 80; | |
908 mb_pos_ptr += 5; | |
909 } | |
910 } | |
911 | |
912 emms_c(); | |
913 return s->sys->frame_size; | |
914 } | |
915 | |
916 static int dvvideo_end(AVCodecContext *avctx) | |
554 { | 917 { |
555 avcodec_default_free_buffers(avctx); | 918 avcodec_default_free_buffers(avctx); |
556 | |
557 return 0; | 919 return 0; |
558 } | 920 } |
559 | 921 |
560 AVCodec dvvideo_decoder = { | 922 AVCodec dvvideo_decoder = { |
561 "dvvideo", | 923 "dvvideo", |
562 CODEC_TYPE_VIDEO, | 924 CODEC_TYPE_VIDEO, |
563 CODEC_ID_DVVIDEO, | 925 CODEC_ID_DVVIDEO, |
564 sizeof(DVVideoDecodeContext), | 926 sizeof(DVVideoDecodeContext), |
565 dvvideo_decode_init, | 927 dvvideo_init, |
566 NULL, | 928 dvvideo_encode_frame, |
567 dvvideo_decode_end, | 929 dvvideo_end, |
568 dvvideo_decode_frame, | 930 dvvideo_decode_frame, |
569 CODEC_CAP_DR1, | 931 CODEC_CAP_DR1, |
570 NULL | 932 NULL |
571 }; | 933 }; |