comparison dv.c @ 8705:412e06100577 libavcodec

factoring code into dv_init_enc_block
author romansh
date Sat, 31 Jan 2009 01:36:38 +0000
parents 199fdc232f13
children ddcd5db645e3
comparison
equal deleted inserted replaced
8704:199fdc232f13 8705:412e06100577
828 } 828 }
829 829
830 return (score88 - score248 > -10); 830 return (score88 - score248 > -10);
831 } 831 }
832 832
833 static av_always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi, 833 static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize, DVVideoContext *s, int bias)
834 const uint8_t* zigzag_scan, 834 {
835 const int *weight, int bias) 835 const int *weight;
836 { 836 const uint8_t* zigzag_scan;
837 DECLARE_ALIGNED_16(DCTELEM, blk[64]);
837 int i, area; 838 int i, area;
838 /* We offer two different methods for class number assignment: the 839 /* We offer two different methods for class number assignment: the
839 method suggested in SMPTE 314M Table 22, and an improved 840 method suggested in SMPTE 314M Table 22, and an improved
840 method. The SMPTE method is very conservative; it assigns class 841 method. The SMPTE method is very conservative; it assigns class
841 3 (i.e. severe quantization) to any block where the largest AC 842 3 (i.e. severe quantization) to any block where the largest AC
851 static const int classes[] = {-1, -1, 255, 0xffff}; 852 static const int classes[] = {-1, -1, 255, 0xffff};
852 #endif 853 #endif
853 int max = classes[0]; 854 int max = classes[0];
854 int prev = 0; 855 int prev = 0;
855 856
857 assert((((int)blk) & 15) == 0);
858
859 bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0;
860 bi->partial_bit_count = 0;
861 bi->partial_bit_buffer = 0;
862 bi->cur_ac = 0;
863 if (data) {
864 s->get_pixels(blk, data, linesize);
865 bi->dct_mode = (s->avctx->flags & CODEC_FLAG_INTERLACED_DCT) &&
866 dv_guess_dct_mode(blk);
867 s->fdct[bi->dct_mode](blk);
868 } else {
869 /* We rely on the fact that encoding all zeros leads to an immediate EOB,
870 which is precisely what the spec calls for in the "dummy" blocks. */
871 memset(blk, 0, sizeof(blk));
872 bi->dct_mode = 0;
873 }
856 bi->mb[0] = blk[0]; 874 bi->mb[0] = blk[0];
875
876 zigzag_scan = bi->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct;
877 weight = bi->dct_mode ? dv_weight_248 : dv_weight_88;
857 878
858 for (area = 0; area < 4; area++) { 879 for (area = 0; area < 4; area++) {
859 bi->prev[area] = prev; 880 bi->prev[area] = prev;
860 bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :) 881 bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
861 for (i = mb_area_start[area]; i < mb_area_start[area+1]; i++) { 882 for (i = mb_area_start[area]; i < mb_area_start[area+1]; i++) {
898 } 919 }
899 } 920 }
900 } 921 }
901 bi->next[prev]= i; 922 bi->next[prev]= i;
902 } 923 }
924
925 return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3];
903 } 926 }
904 927
905 static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos) 928 static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
906 { 929 {
907 int size[5]; 930 int size[5];
978 int mb_x, mb_y, c_offset, linesize; 1001 int mb_x, mb_y, c_offset, linesize;
979 uint8_t* y_ptr; 1002 uint8_t* y_ptr;
980 uint8_t* data; 1003 uint8_t* data;
981 uint8_t* ptr; 1004 uint8_t* ptr;
982 uint8_t* dif; 1005 uint8_t* dif;
983 int do_edge_wrap; 1006 uint8_t scratch[64];
984 DECLARE_ALIGNED_16(DCTELEM, block[64]);
985 EncBlockInfo enc_blks[5*DV_MAX_BPM]; 1007 EncBlockInfo enc_blks[5*DV_MAX_BPM];
986 PutBitContext pbs[5*DV_MAX_BPM]; 1008 PutBitContext pbs[5*DV_MAX_BPM];
987 PutBitContext* pb; 1009 PutBitContext* pb;
988 EncBlockInfo* enc_blk; 1010 EncBlockInfo* enc_blk;
989 int vs_bit_size = 0; 1011 int vs_bit_size = 0;
990 int qnos[5]; 1012 int qnos[5];
991
992 assert((((int)block) & 15) == 0);
993 1013
994 dif = &s->buf[work_chunk->buf_offset*80]; 1014 dif = &s->buf[work_chunk->buf_offset*80];
995 enc_blk = &enc_blks[0]; 1015 enc_blk = &enc_blks[0];
996 pb = &pbs[0]; 1016 pb = &pbs[0];
997 for (mb_index = 0; mb_index < 5; mb_index++) { 1017 for (mb_index = 0; mb_index < 5; mb_index++) {
998 dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y); 1018 dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
999 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << 3); 1019 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << 3);
1000 c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] + 1020 c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
1001 (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << 3); 1021 (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << 3);
1002 do_edge_wrap = 0;
1003 qnos[mb_index] = 15; /* No quantization */ 1022 qnos[mb_index] = 15; /* No quantization */
1004 ptr = dif + mb_index*80 + 4; 1023 ptr = dif + mb_index*80 + 4;
1005 for (j = 0; j < 6; j++) { 1024 for (j = 0; j < 6; j++) {
1006 int dummy = 0;
1007 if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */ 1025 if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
1008 if (j == 0 || j == 2) { 1026 if (j == 0 || j == 2) {
1009 /* Y0 Y1 */ 1027 /* Y0 Y1 */
1010 data = y_ptr + ((j >> 1) * 8); 1028 data = y_ptr + ((j >> 1) * 8);
1011 linesize = s->picture.linesize[0]; 1029 linesize = s->picture.linesize[0];
1015 linesize = s->picture.linesize[6 - j]; 1033 linesize = s->picture.linesize[6 - j];
1016 } else { 1034 } else {
1017 /* j=1 and j=3 are "dummy" blocks, used for AC data only */ 1035 /* j=1 and j=3 are "dummy" blocks, used for AC data only */
1018 data = NULL; 1036 data = NULL;
1019 linesize = 0; 1037 linesize = 0;
1020 dummy = 1;
1021 } 1038 }
1022 } else { /* 4:1:1 or 4:2:0 */ 1039 } else { /* 4:1:1 or 4:2:0 */
1023 if (j < 4) { /* Four Y blocks */ 1040 if (j < 4) { /* Four Y blocks */
1024 /* NOTE: at end of line, the macroblock is handled as 420 */ 1041 /* NOTE: at end of line, the macroblock is handled as 420 */
1025 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) { 1042 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
1030 linesize = s->picture.linesize[0]; 1047 linesize = s->picture.linesize[0];
1031 } else { /* Cr and Cb blocks */ 1048 } else { /* Cr and Cb blocks */
1032 /* don't ask Fabrice why they inverted Cb and Cr ! */ 1049 /* don't ask Fabrice why they inverted Cb and Cr ! */
1033 data = s->picture.data [6 - j] + c_offset; 1050 data = s->picture.data [6 - j] + c_offset;
1034 linesize = s->picture.linesize[6 - j]; 1051 linesize = s->picture.linesize[6 - j];
1035 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) 1052 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
1036 do_edge_wrap = 1;
1037 }
1038 }
1039
1040 /* Everything is set up -- now just copy data -> DCT block */
1041 if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
1042 uint8_t* d; 1053 uint8_t* d;
1043 DCTELEM *b = block; 1054 uint8_t* b = scratch;
1044 for (i = 0; i < 8; i++) { 1055 for (i = 0; i < 8; i++) {
1045 d = data + 8 * linesize; 1056 d = data + 8 * linesize;
1046 b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3]; 1057 b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
1047 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3]; 1058 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
1048 data += linesize; 1059 data += linesize;
1049 b += 8; 1060 b += 8;
1050 } 1061 }
1051 } else { /* Simple copy: 8x8 -> 8x8 */ 1062 data = scratch;
1052 if (!dummy) 1063 linesize = 8;
1053 s->get_pixels(block, data, linesize); 1064 }
1054 } 1065 }
1055 1066 }
1056 if (s->avctx->flags & CODEC_FLAG_INTERLACED_DCT) 1067
1057 enc_blk->dct_mode = dv_guess_dct_mode(block); 1068 vs_bit_size += dv_init_enc_block(enc_blk, data, linesize, s, j>>2);
1058 else
1059 enc_blk->dct_mode = 0;
1060 enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
1061 enc_blk->partial_bit_count = 0;
1062 enc_blk->partial_bit_buffer = 0;
1063 enc_blk->cur_ac = 0;
1064
1065 if (dummy) {
1066 /* We rely on the fact that encoding all zeros leads to an immediate EOB,
1067 which is precisely what the spec calls for in the "dummy" blocks. */
1068 memset(block, 0, sizeof(block));
1069 } else {
1070 s->fdct[enc_blk->dct_mode](block);
1071 }
1072
1073 dv_set_class_number(block, enc_blk,
1074 enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
1075 enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
1076 j/4);
1077 1069
1078 init_put_bits(pb, ptr, s->sys->block_sizes[j]/8); 1070 init_put_bits(pb, ptr, s->sys->block_sizes[j]/8);
1079 put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2)); 1071 put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));
1080 put_bits(pb, 1, enc_blk->dct_mode); 1072 put_bits(pb, 1, enc_blk->dct_mode);
1081 put_bits(pb, 2, enc_blk->cno); 1073 put_bits(pb, 2, enc_blk->cno);
1082 1074
1083 vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
1084 enc_blk->bit_size[2] + enc_blk->bit_size[3];
1085 ++enc_blk; 1075 ++enc_blk;
1086 ++pb; 1076 ++pb;
1087 ptr += s->sys->block_sizes[j]/8; 1077 ptr += s->sys->block_sizes[j]/8;
1088 } 1078 }
1089 } 1079 }