| OLD | NEW |
| (Empty) | |
| 1 commit bab197002b561fb53abfa6e24def881f6ec86c9b |
| 2 Author: conrad <conrad@9553f0bf-9b14-0410-a0b8-cfaf0461ba5b> |
| 3 Date: Fri May 29 21:43:22 2009 +0000 |
| 4 |
| 5 Support block-level quantization in Theora |
| 6 |
| 7 git-svn-id: file:///var/local/repositories/ffmpeg/trunk@18986 9553f0bf-9b14-
0410-a0b8-cfaf0461ba5b |
| 8 |
| 9 diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c |
| 10 index 9f7dfb6..1646dd9 100644 |
| 11 --- a/libavcodec/vp3.c |
| 12 +++ b/libavcodec/vp3.c |
| 13 @@ -60,6 +60,7 @@ typedef struct Vp3Fragment { |
| 14 uint8_t coding_method; |
| 15 int8_t motion_x; |
| 16 int8_t motion_y; |
| 17 + uint8_t qpi; |
| 18 } Vp3Fragment; |
| 19 |
| 20 #define SB_NOT_CODED 0 |
| 21 @@ -134,10 +135,9 @@ typedef struct Vp3DecodeContext { |
| 22 DSPContext dsp; |
| 23 int flipped_image; |
| 24 |
| 25 - int qis[3]; |
| 26 - int nqis; |
| 27 - int quality_index; |
| 28 - int last_quality_index; |
| 29 + int qps[3]; |
| 30 + int nqps; |
| 31 + int last_qps[3]; |
| 32 |
| 33 int superblock_count; |
| 34 int y_superblock_width; |
| 35 @@ -191,7 +191,7 @@ typedef struct Vp3DecodeContext { |
| 36 |
| 37 /* these arrays need to be on 16-byte boundaries since SSE2 operations |
| 38 * index into them */ |
| 39 - DECLARE_ALIGNED_16(int16_t, qmat[2][4][64]); //<qmat[is_inter][plane
] |
| 40 + DECLARE_ALIGNED_16(int16_t, qmat[3][2][3][64]); //<qmat[qpi][is_inter][
plane] |
| 41 |
| 42 /* This table contains superblock_count * 16 entries. Each set of 16 |
| 43 * numbers corresponds to the fragment indexes 0..15 of the superblock. |
| 44 @@ -467,6 +467,7 @@ static void init_frame(Vp3DecodeContext *s, GetBitContext *g
b) |
| 45 s->all_fragments[i].motion_x = 127; |
| 46 s->all_fragments[i].motion_y = 127; |
| 47 s->all_fragments[i].next_coeff= NULL; |
| 48 + s->all_fragments[i].qpi = 0; |
| 49 s->coeffs[i].index= |
| 50 s->coeffs[i].coeff=0; |
| 51 s->coeffs[i].next= NULL; |
| 52 @@ -477,10 +478,10 @@ static void init_frame(Vp3DecodeContext *s, GetBitContext
*gb) |
| 53 * This function sets up the dequantization tables used for a particular |
| 54 * frame. |
| 55 */ |
| 56 -static void init_dequantizer(Vp3DecodeContext *s) |
| 57 +static void init_dequantizer(Vp3DecodeContext *s, int qpi) |
| 58 { |
| 59 - int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index]; |
| 60 - int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index]; |
| 61 + int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]]; |
| 62 + int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]]; |
| 63 int i, plane, inter, qri, bmi, bmj, qistart; |
| 64 |
| 65 for(inter=0; inter<2; inter++){ |
| 66 @@ -488,27 +489,29 @@ static void init_dequantizer(Vp3DecodeContext *s) |
| 67 int sum=0; |
| 68 for(qri=0; qri<s->qr_count[inter][plane]; qri++){ |
| 69 sum+= s->qr_size[inter][plane][qri]; |
| 70 - if(s->quality_index <= sum) |
| 71 + if(s->qps[qpi] <= sum) |
| 72 break; |
| 73 } |
| 74 qistart= sum - s->qr_size[inter][plane][qri]; |
| 75 bmi= s->qr_base[inter][plane][qri ]; |
| 76 bmj= s->qr_base[inter][plane][qri+1]; |
| 77 for(i=0; i<64; i++){ |
| 78 - int coeff= ( 2*(sum -s->quality_index)*s->base_matrix[bmi][
i] |
| 79 - - 2*(qistart-s->quality_index)*s->base_matrix[bmj][
i] |
| 80 + int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i] |
| 81 + - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i] |
| 82 + s->qr_size[inter][plane][qri]) |
| 83 / (2*s->qr_size[inter][plane][qri]); |
| 84 |
| 85 int qmin= 8<<(inter + !i); |
| 86 int qscale= i ? ac_scale_factor : dc_scale_factor; |
| 87 |
| 88 - s->qmat[inter][plane][s->dsp.idct_permutation[i]]= av_clip((qsc
ale * coeff)/100 * 4, qmin, 4096); |
| 89 + s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip
((qscale * coeff)/100 * 4, qmin, 4096); |
| 90 } |
| 91 + // all DC coefficients use the same quant so as not to interfere wi
th DC prediction |
| 92 + s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0]; |
| 93 } |
| 94 } |
| 95 |
| 96 - memset(s->qscale_table, (FFMAX(s->qmat[0][0][1], s->qmat[0][1][1])+8)/16, 5
12); //FIXME finetune |
| 97 + memset(s->qscale_table, (FFMAX(s->qmat[0][0][0][1], s->qmat[0][0][1][1])+8)
/16, 512); //FIXME finetune |
| 98 } |
| 99 |
| 100 /* |
| 101 @@ -521,7 +524,7 @@ static void init_loop_filter(Vp3DecodeContext *s) |
| 102 int filter_limit; |
| 103 int x; |
| 104 |
| 105 - filter_limit = s->filter_limit_values[s->quality_index]; |
| 106 + filter_limit = s->filter_limit_values[s->qps[0]]; |
| 107 |
| 108 /* set up the bounding values */ |
| 109 memset(s->bounding_values_array, 0, 256 * sizeof(int)); |
| 110 @@ -963,6 +966,47 @@ static int unpack_vectors(Vp3DecodeContext *s, GetBitContex
t *gb) |
| 111 return 0; |
| 112 } |
| 113 |
| 114 +static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb) |
| 115 +{ |
| 116 + int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi; |
| 117 + int num_blocks = s->coded_fragment_list_index; |
| 118 + |
| 119 + for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) { |
| 120 + i = blocks_decoded = num_blocks_at_qpi = 0; |
| 121 + |
| 122 + bit = get_bits1(gb); |
| 123 + |
| 124 + do { |
| 125 + run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2)
+ 1; |
| 126 + if (run_length == 34) |
| 127 + run_length += get_bits(gb, 12); |
| 128 + blocks_decoded += run_length; |
| 129 + |
| 130 + if (!bit) |
| 131 + num_blocks_at_qpi += run_length; |
| 132 + |
| 133 + for (j = 0; j < run_length; i++) { |
| 134 + if (i > s->coded_fragment_list_index) |
| 135 + return -1; |
| 136 + |
| 137 + if (s->all_fragments[s->coded_fragment_list[i]].qpi == qpi) { |
| 138 + s->all_fragments[s->coded_fragment_list[i]].qpi += bit; |
| 139 + j++; |
| 140 + } |
| 141 + } |
| 142 + |
| 143 + if (run_length == 4129) |
| 144 + bit = get_bits1(gb); |
| 145 + else |
| 146 + bit ^= 1; |
| 147 + } while (blocks_decoded < num_blocks); |
| 148 + |
| 149 + num_blocks -= num_blocks_at_qpi; |
| 150 + } |
| 151 + |
| 152 + return 0; |
| 153 +} |
| 154 + |
| 155 /* |
| 156 * This function is called by unpack_dct_coeffs() to extract the VLCs from |
| 157 * the bitstream. The VLCs encode tokens which are used to unpack DCT |
| 158 @@ -1394,9 +1438,9 @@ static void render_slice(Vp3DecodeContext *s, int slice) |
| 159 motion_source + stride + 1 + d, |
| 160 stride, 8); |
| 161 } |
| 162 - dequantizer = s->qmat[1][plane]; |
| 163 + dequantizer = s->qmat[s->all_fragments[i].qpi][1][plane
]; |
| 164 }else{ |
| 165 - dequantizer = s->qmat[0][plane]; |
| 166 + dequantizer = s->qmat[s->all_fragments[i].qpi][0][plane
]; |
| 167 } |
| 168 |
| 169 /* dequantize the DCT coefficients */ |
| 170 @@ -1648,7 +1692,8 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx) |
| 171 |
| 172 /* initialize to an impossible value which will force a recalculation |
| 173 * in the first frame decode */ |
| 174 - s->quality_index = -1; |
| 175 + for (i = 0; i < 3; i++) |
| 176 + s->qps[i] = -1; |
| 177 |
| 178 s->y_superblock_width = (s->width + 31) / 32; |
| 179 s->y_superblock_height = (s->height + 31) / 32; |
| 180 @@ -1819,24 +1864,29 @@ static int vp3_decode_frame(AVCodecContext *avctx, |
| 181 s->keyframe = !get_bits1(&gb); |
| 182 if (!s->theora) |
| 183 skip_bits(&gb, 1); |
| 184 - s->last_quality_index = s->quality_index; |
| 185 + for (i = 0; i < 3; i++) |
| 186 + s->last_qps[i] = s->qps[i]; |
| 187 |
| 188 - s->nqis=0; |
| 189 + s->nqps=0; |
| 190 do{ |
| 191 - s->qis[s->nqis++]= get_bits(&gb, 6); |
| 192 - } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb)); |
| 193 - |
| 194 - s->quality_index= s->qis[0]; |
| 195 + s->qps[s->nqps++]= get_bits(&gb, 6); |
| 196 + } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb)); |
| 197 + for (i = s->nqps; i < 3; i++) |
| 198 + s->qps[i] = -1; |
| 199 |
| 200 if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 201 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n", |
| 202 - s->keyframe?"key":"", counter, s->quality_index); |
| 203 + s->keyframe?"key":"", counter, s->qps[0]); |
| 204 counter++; |
| 205 |
| 206 - if (s->quality_index != s->last_quality_index) { |
| 207 - init_dequantizer(s); |
| 208 + if (s->qps[0] != s->last_qps[0]) |
| 209 init_loop_filter(s); |
| 210 - } |
| 211 + |
| 212 + for (i = 0; i < s->nqps; i++) |
| 213 + // reinit all dequantizers if the first one changed, because |
| 214 + // the DC of the first quantizer must be used for all matrices |
| 215 + if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0]) |
| 216 + init_dequantizer(s, i); |
| 217 |
| 218 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe) |
| 219 return buf_size; |
| 220 @@ -1916,6 +1966,10 @@ static int vp3_decode_frame(AVCodecContext *avctx, |
| 221 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n"); |
| 222 return -1; |
| 223 } |
| 224 + if (unpack_block_qpis(s, &gb)){ |
| 225 + av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n"); |
| 226 + return -1; |
| 227 + } |
| 228 if (unpack_dct_coeffs(s, &gb)){ |
| 229 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); |
| 230 return -1; |
| OLD | NEW |