comparison h263.c @ 1353:cfc80b3a4ada libavcodec

flash video (flv) support patch by (Garrick Meeker <gmeeker at theoryllc dot com>)
author michaelni
date Wed, 09 Jul 2003 23:10:59 +0000
parents e7c8051e1849
children 1f89adb69349
comparison
equal deleted inserted replaced
1352:e8ff4783f188 1353:cfc80b3a4ada
48 #define MB_TYPE_B_VLC_BITS 4 48 #define MB_TYPE_B_VLC_BITS 4
49 #define TEX_VLC_BITS 9 49 #define TEX_VLC_BITS 9
50 50
51 #ifdef CONFIG_ENCODERS 51 #ifdef CONFIG_ENCODERS
52 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, 52 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
53 int n); 53 int n);
54 static void h263_flv_encode_block(MpegEncContext * s, DCTELEM * block,
55 int n);
54 static void h263_encode_motion(MpegEncContext * s, int val, int fcode); 56 static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
55 static void h263p_encode_umotion(MpegEncContext * s, int val); 57 static void h263p_encode_umotion(MpegEncContext * s, int val);
56 static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, 58 static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
57 int n, int dc, uint8_t *scan_table, 59 int n, int dc, uint8_t *scan_table,
58 PutBitContext *dc_pb, PutBitContext *ac_pb); 60 PutBitContext *dc_pb, PutBitContext *ac_pb);
59 #endif 61 #endif
60 62
61 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode); 63 static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
62 static int h263p_decode_umotion(MpegEncContext * s, int pred); 64 static int h263p_decode_umotion(MpegEncContext * s, int pred);
153 void h263_encode_picture_header(MpegEncContext * s, int picture_number) 155 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
154 { 156 {
155 int format; 157 int format;
156 158
157 align_put_bits(&s->pb); 159 align_put_bits(&s->pb);
160
161 if (s->h263_flv) {
162 put_bits(&s->pb, 17, 1);
163 put_bits(&s->pb, 5, (s->h263_flv-1)); /* 0: h263 escape codes 1: 11-bit escape codes */
164 put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) /
165 s->avctx->frame_rate) & 0xff); /* TemporalReference */
166 if (s->width == 352 && s->height == 288)
167 format = 2;
168 else if (s->width == 176 && s->height == 144)
169 format = 3;
170 else if (s->width == 128 && s->height == 96)
171 format = 4;
172 else if (s->width == 320 && s->height == 240)
173 format = 5;
174 else if (s->width == 160 && s->height == 120)
175 format = 6;
176 else if (s->width <= 255 && s->height <= 255)
177 format = 0; /* use 1 byte width & height */
178 else
179 format = 1; /* use 2 bytes width & height */
180 put_bits(&s->pb, 3, format); /* PictureSize */
181 if (format == 0) {
182 put_bits(&s->pb, 8, s->width);
183 put_bits(&s->pb, 8, s->height);
184 } else if (format == 1) {
185 put_bits(&s->pb, 16, s->width);
186 put_bits(&s->pb, 16, s->height);
187 }
188 put_bits(&s->pb, 2, s->pict_type == P_TYPE); /* PictureType */
189 put_bits(&s->pb, 1, 0); /* DeblockingFlag: off */
190 put_bits(&s->pb, 5, s->qscale); /* Quantizer */
191 put_bits(&s->pb, 1, 0); /* ExtraInformation */
192
193 if(s->h263_aic){
194 s->y_dc_scale_table=
195 s->c_dc_scale_table= h263_aic_dc_scale_table;
196 }else{
197 s->y_dc_scale_table=
198 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
199 }
200 return;
201 }
158 202
159 /* Update the pointer to last GOB */ 203 /* Update the pointer to last GOB */
160 s->ptr_lastgob = pbBufPtr(&s->pb); 204 s->ptr_lastgob = pbBufPtr(&s->pb);
161 s->gob_number = 0; 205 s->gob_number = 0;
162 206
836 { 880 {
837 int cbpc, cbpy, i, cbp, pred_x, pred_y; 881 int cbpc, cbpy, i, cbp, pred_x, pred_y;
838 int16_t pred_dc; 882 int16_t pred_dc;
839 int16_t rec_intradc[6]; 883 int16_t rec_intradc[6];
840 uint16_t *dc_ptr[6]; 884 uint16_t *dc_ptr[6];
885 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
841 const int dquant_code[5]= {1,0,9,2,3}; 886 const int dquant_code[5]= {1,0,9,2,3};
842 887
843 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); 888 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
844 if (!s->mb_intra) { 889 if (!s->mb_intra) {
845 /* compute cbp */ 890 /* compute cbp */
849 cbp |= 1 << (5 - i); 894 cbp |= 1 << (5 - i);
850 } 895 }
851 if ((cbp | motion_x | motion_y | s->dquant) == 0) { 896 if ((cbp | motion_x | motion_y | s->dquant) == 0) {
852 /* skip macroblock */ 897 /* skip macroblock */
853 put_bits(&s->pb, 1, 1); 898 put_bits(&s->pb, 1, 1);
899 if(interleaved_stats){
900 s->misc_bits++;
901 s->last_bits++;
902 }
854 return; 903 return;
855 } 904 }
856 put_bits(&s->pb, 1, 0); /* mb coded */ 905 put_bits(&s->pb, 1, 0); /* mb coded */
857 cbpc = cbp & 3; 906 cbpc = cbp & 3;
858 if(s->dquant) cbpc+= 8; 907 if(s->dquant) cbpc+= 8;
863 cbpy ^= 0xf; 912 cbpy ^= 0xf;
864 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 913 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
865 if(s->dquant) 914 if(s->dquant)
866 put_bits(&s->pb, 2, dquant_code[s->dquant+2]); 915 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
867 916
917 if(interleaved_stats){
918 s->misc_bits+= get_bits_diff(s);
919 }
920
868 /* motion vectors: 16x16 mode only now */ 921 /* motion vectors: 16x16 mode only now */
869 h263_pred_motion(s, 0, &pred_x, &pred_y); 922 h263_pred_motion(s, 0, &pred_x, &pred_y);
870 923
871 if (!s->umvplus) { 924 if (!s->umvplus) {
872 h263_encode_motion(s, motion_x - pred_x, s->f_code); 925 h263_encode_motion(s, motion_x - pred_x, s->f_code);
876 h263p_encode_umotion(s, motion_x - pred_x); 929 h263p_encode_umotion(s, motion_x - pred_x);
877 h263p_encode_umotion(s, motion_y - pred_y); 930 h263p_encode_umotion(s, motion_y - pred_y);
878 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1)) 931 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
879 /* To prevent Start Code emulation */ 932 /* To prevent Start Code emulation */
880 put_bits(&s->pb,1,1); 933 put_bits(&s->pb,1,1);
934 }
935
936 if(interleaved_stats){
937 s->mv_bits+= get_bits_diff(s);
881 } 938 }
882 } else { 939 } else {
883 int li = s->h263_aic ? 0 : 1; 940 int li = s->h263_aic ? 0 : 1;
884 941
885 cbp = 0; 942 cbp = 0;
944 } 1001 }
945 cbpy = cbp >> 2; 1002 cbpy = cbp >> 2;
946 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); 1003 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
947 if(s->dquant) 1004 if(s->dquant)
948 put_bits(&s->pb, 2, dquant_code[s->dquant+2]); 1005 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1006
1007 if(interleaved_stats){
1008 s->misc_bits+= get_bits_diff(s);
1009 }
949 } 1010 }
950 1011
951 for(i=0; i<6; i++) { 1012 for(i=0; i<6; i++) {
952 /* encode each block */ 1013 /* encode each block */
953 h263_encode_block(s, block[i], i); 1014 if (s->h263_flv > 1)
1015 h263_flv_encode_block(s, block[i], i);
1016 else
1017 h263_encode_block(s, block[i], i);
954 1018
955 /* Update INTRADC for decoding */ 1019 /* Update INTRADC for decoding */
956 if (s->h263_aic && s->mb_intra) { 1020 if (s->h263_aic && s->mb_intra) {
957 block[i][0] = rec_intradc[i]; 1021 block[i][0] = rec_intradc[i];
958 1022
1023 }
1024 }
1025
1026 if(interleaved_stats){
1027 if (!s->mb_intra) {
1028 s->p_tex_bits+= get_bits_diff(s);
1029 s->f_count++;
1030 }else{
1031 s->i_tex_bits+= get_bits_diff(s);
1032 s->i_count++;
959 } 1033 }
960 } 1034 }
961 } 1035 }
962 #endif 1036 #endif
963 1037
1465 s->fcode_tab= umv_fcode_tab; 1539 s->fcode_tab= umv_fcode_tab;
1466 s->min_qcoeff= -127; 1540 s->min_qcoeff= -127;
1467 s->max_qcoeff= 127; 1541 s->max_qcoeff= 127;
1468 break; 1542 break;
1469 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later 1543 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
1544 case CODEC_ID_FLV1:
1545 if (s->h263_flv > 1) {
1546 s->min_qcoeff= -1023;
1547 s->max_qcoeff= 1023;
1548 } else {
1549 s->min_qcoeff= -127;
1550 s->max_qcoeff= 127;
1551 }
1552 s->y_dc_scale_table=
1553 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1554 break;
1470 default: //nothing needed default table allready set in mpegvideo.c 1555 default: //nothing needed default table allready set in mpegvideo.c
1471 s->min_qcoeff= -127; 1556 s->min_qcoeff= -127;
1472 s->max_qcoeff= 127; 1557 s->max_qcoeff= 127;
1473 s->y_dc_scale_table= 1558 s->y_dc_scale_table=
1474 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; 1559 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1543 } else { 1628 } else {
1544 put_bits(&s->pb, 1, sign); 1629 put_bits(&s->pb, 1, sign);
1545 } 1630 }
1546 last_non_zero = i; 1631 last_non_zero = i;
1547 } 1632 }
1633 }
1634 }
1635
1636 /**
1637 * encodes a 8x8 block.
1638 * @param block the 8x8 block
1639 * @param n block index (0-3 are luma, 4-5 are chroma)
1640 */
1641 static void h263_flv_encode_block(MpegEncContext * s, DCTELEM * block, int n)
1642 {
1643 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
1644 RLTable *rl;
1645
1646 rl = &rl_inter;
1647 if (s->mb_intra && !s->h263_aic) {
1648 /* DC coef */
1649 level = block[0];
1650 /* 255 cannot be represented, so we clamp */
1651 if (level > 254) {
1652 level = 254;
1653 block[0] = 254;
1654 }
1655 /* 0 cannot be represented also */
1656 else if (level < 1) {
1657 level = 1;
1658 block[0] = 1;
1659 }
1660 if (level == 128) //FIXME check rv10
1661 put_bits(&s->pb, 8, 0xff);
1662 else
1663 put_bits(&s->pb, 8, level & 0xff);
1664 i = 1;
1665 } else {
1666 i = 0;
1667 if (s->h263_aic && s->mb_intra)
1668 rl = &rl_intra_aic;
1669 }
1670
1671 /* AC coefs */
1672 last_index = s->block_last_index[n];
1673 last_non_zero = i - 1;
1674 for (; i <= last_index; i++) {
1675 j = s->intra_scantable.permutated[i];
1676 level = block[j];
1677 if (level) {
1678 run = i - last_non_zero - 1;
1679 last = (i == last_index);
1680 sign = 0;
1681 slevel = level;
1682 if (level < 0) {
1683 sign = 1;
1684 level = -level;
1685 }
1686 code = get_rl_index(rl, last, run, level);
1687 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1688 if (code == rl->n) {
1689 assert(slevel != 0);
1690 if(slevel < 64 && slevel > -64) {
1691 /* 7-bit level */
1692 put_bits(&s->pb, 1, 0);
1693 put_bits(&s->pb, 1, last);
1694 put_bits(&s->pb, 6, run);
1695
1696 put_bits(&s->pb, 7, slevel & 0x7f);
1697 } else {
1698 /* 11-bit level */
1699 put_bits(&s->pb, 1, 1);
1700 put_bits(&s->pb, 1, last);
1701 put_bits(&s->pb, 6, run);
1702
1703 put_bits(&s->pb, 11, slevel & 0x7ff);
1704 }
1705 } else {
1706 put_bits(&s->pb, 1, sign);
1707 }
1708 last_non_zero = i;
1709 }
1548 } 1710 }
1549 } 1711 }
1550 #endif 1712 #endif
1551 1713
1552 #ifdef CONFIG_ENCODERS 1714 #ifdef CONFIG_ENCODERS
3551 fprintf(stderr, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y); 3713 fprintf(stderr, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
3552 return -1; 3714 return -1;
3553 } 3715 }
3554 if (code == rl->n) { 3716 if (code == rl->n) {
3555 /* escape */ 3717 /* escape */
3556 last = get_bits1(&s->gb); 3718 if (s->h263_flv > 1) {
3557 run = get_bits(&s->gb, 6); 3719 int is11 = get_bits1(&s->gb);
3558 level = (int8_t)get_bits(&s->gb, 8); 3720 last = get_bits1(&s->gb);
3559 if(level == -128){ 3721 run = get_bits(&s->gb, 6);
3560 if (s->h263_rv10) { 3722 if(is11){
3561 /* XXX: should patch encoder too */ 3723 level = (int8_t)get_sbits(&s->gb, 11);
3562 level = get_sbits(&s->gb, 12); 3724 } else {
3563 }else{ 3725 level = (int8_t)get_sbits(&s->gb, 7);
3564 level = get_bits(&s->gb, 5); 3726 }
3565 level |= get_sbits(&s->gb, 6)<<5; 3727 } else {
3728 last = get_bits1(&s->gb);
3729 run = get_bits(&s->gb, 6);
3730 level = (int8_t)get_bits(&s->gb, 8);
3731 if(level == -128){
3732 if (s->h263_rv10) {
3733 /* XXX: should patch encoder too */
3734 level = get_sbits(&s->gb, 12);
3735 }else{
3736 level = get_bits(&s->gb, 5);
3737 level |= get_sbits(&s->gb, 6)<<5;
3738 }
3566 } 3739 }
3567 } 3740 }
3568 } else { 3741 } else {
3569 run = rl->table_run[code]; 3742 run = rl->table_run[code];
3570 level = rl->table_level[code]; 3743 level = rl->table_level[code];
4993 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; 5166 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
4994 5167
4995 return 0; 5168 return 0;
4996 } 5169 }
4997 5170
5171 int flv_h263_decode_picture_header(MpegEncContext *s)
5172 {
5173 int format, width, height;
5174
5175 /* picture header */
5176 if (get_bits_long(&s->gb, 17) != 1) {
5177 fprintf(stderr, "Bad picture start code\n");
5178 return -1;
5179 }
5180 format = get_bits(&s->gb, 5);
5181 if (format != 0 && format != 1) {
5182 fprintf(stderr, "Bad picture format\n");
5183 return -1;
5184 }
5185 s->h263_flv = format+1;
5186 s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
5187 format = get_bits(&s->gb, 3);
5188 switch (format) {
5189 case 0:
5190 width = get_bits(&s->gb, 8);
5191 height = get_bits(&s->gb, 8);
5192 break;
5193 case 1:
5194 width = get_bits(&s->gb, 16);
5195 height = get_bits(&s->gb, 16);
5196 break;
5197 case 2:
5198 width = 352;
5199 height = 288;
5200 break;
5201 case 3:
5202 width = 176;
5203 height = 144;
5204 break;
5205 case 4:
5206 width = 128;
5207 height = 96;
5208 break;
5209 case 5:
5210 width = 320;
5211 height = 240;
5212 break;
5213 case 6:
5214 width = 160;
5215 height = 120;
5216 break;
5217 default:
5218 width = height = 0;
5219 break;
5220 }
5221 if ((width == 0) || (height == 0))
5222 return -1;
5223 s->width = width;
5224 s->height = height;
5225
5226 s->pict_type = I_TYPE + get_bits(&s->gb, 2);
5227 if (s->pict_type > P_TYPE)
5228 s->pict_type = P_TYPE;
5229 skip_bits1(&s->gb); /* deblocking flag */
5230 s->qscale = get_bits(&s->gb, 5);
5231
5232 s->h263_plus = 0;
5233
5234 s->unrestricted_mv = 1;
5235 s->h263_long_vectors = s->unrestricted_mv;
5236
5237 /* PEI */
5238 while (get_bits1(&s->gb) != 0) {
5239 skip_bits(&s->gb, 8);
5240 }
5241 s->f_code = 1;
5242
5243 s->y_dc_scale_table=
5244 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
5245
5246 return 0;
5247 }