comparison dnxhdenc.c @ 6978:9774d160f014 libavcodec

optimize, merge offset bits in vlc code
author bcoudurier
date Mon, 02 Jun 2008 03:07:32 +0000
parents 0f63fc62ea8b
children e897f60e5048
comparison
equal deleted inserted replaced
6977:de032bcdeff9 6978:9774d160f014
65 uint16_t (*qmatrix_c16)[2][64]; 65 uint16_t (*qmatrix_c16)[2][64];
66 66
67 unsigned frame_bits; 67 unsigned frame_bits;
68 uint8_t *src[3]; 68 uint8_t *src[3];
69 69
70 uint16_t *table_vlc_codes; 70 uint32_t *table_vlc_codes;
71 uint8_t *table_vlc_bits; 71 uint8_t *table_vlc_bits;
72 uint16_t *table_run_codes; 72 uint16_t *table_run_codes;
73 uint8_t *table_run_bits; 73 uint8_t *table_run_bits;
74 74
75 /** Rate control */ 75 /** Rate control */
88 88
89 #define LAMBDA_FRAC_BITS 10 89 #define LAMBDA_FRAC_BITS 10
90 90
91 static int dnxhd_init_vlc(DNXHDEncContext *ctx) 91 static int dnxhd_init_vlc(DNXHDEncContext *ctx)
92 { 92 {
93 int i; 93 int i, j, level, run;
94 94 int max_level = 1<<(ctx->cid_table->bit_depth+2);
95 CHECKED_ALLOCZ(ctx->table_vlc_codes, 449*2); 95
96 CHECKED_ALLOCZ(ctx->table_vlc_bits, 449); 96 CHECKED_ALLOCZ(ctx->table_vlc_codes, max_level*4*sizeof(*ctx->table_vlc_codes));
97 CHECKED_ALLOCZ(ctx->table_run_codes, 63*2); 97 CHECKED_ALLOCZ(ctx->table_vlc_bits, max_level*4*sizeof(*ctx->table_vlc_bits));
98 CHECKED_ALLOCZ(ctx->table_run_bits, 63); 98 CHECKED_ALLOCZ(ctx->table_run_codes, 63*2);
99 99 CHECKED_ALLOCZ(ctx->table_run_bits, 63);
100 for (i = 0; i < 257; i++) { 100
101 int level = ctx->cid_table->ac_level[i] + 101 ctx->table_vlc_codes += max_level*2;
102 (ctx->cid_table->ac_run_flag[i] << 7) + (ctx->cid_table->ac_index_flag[i] << 8); 102 ctx->table_vlc_bits += max_level*2;
103 assert(level < 449); 103 for (level = -max_level; level < max_level; level++) {
104 if (ctx->cid_table->ac_level[i] == 64 && ctx->cid_table->ac_index_flag[i]) 104 for (run = 0; run < 2; run++) {
105 level -= 64; // use 0+(1<<8) level 105 int index = (level<<1)|run;
106 ctx->table_vlc_codes[level] = ctx->cid_table->ac_codes[i]; 106 int sign, offset = 0, alevel = level;
107 ctx->table_vlc_bits [level] = ctx->cid_table->ac_bits[i]; 107
108 MASK_ABS(sign, alevel);
109 if (alevel > 64) {
110 offset = (alevel-1)>>6;
111 alevel -= offset<<6;
112 }
113 for (j = 0; j < 257; j++) {
114 if (ctx->cid_table->ac_level[j] == alevel &&
115 (!offset || (ctx->cid_table->ac_index_flag[j] && offset)) &&
116 (!run || (ctx->cid_table->ac_run_flag [j] && run))) {
117 assert(!ctx->table_vlc_codes[index]);
118 if (alevel) {
119 ctx->table_vlc_codes[index] = (ctx->cid_table->ac_codes[j]<<1)|(sign&1);
120 ctx->table_vlc_bits [index] = ctx->cid_table->ac_bits[j]+1;
121 } else {
122 ctx->table_vlc_codes[index] = ctx->cid_table->ac_codes[j];
123 ctx->table_vlc_bits [index] = ctx->cid_table->ac_bits [j];
124 }
125 break;
126 }
127 }
128 assert(!alevel || j < 257);
129 if (offset) {
130 ctx->table_vlc_codes[index] = (ctx->table_vlc_codes[index]<<ctx->cid_table->index_bits)|offset;
131 ctx->table_vlc_bits [index]+= ctx->cid_table->index_bits;
132 }
133 }
108 } 134 }
109 for (i = 0; i < 62; i++) { 135 for (i = 0; i < 62; i++) {
110 int run = ctx->cid_table->run[i]; 136 int run = ctx->cid_table->run[i];
111 assert(run < 63); 137 assert(run < 63);
112 ctx->table_run_codes[run] = ctx->cid_table->run_codes[i]; 138 ctx->table_run_codes[run] = ctx->cid_table->run_codes[i];
282 } 308 }
283 309
284 static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, DCTELEM *block, int last_index, int n) 310 static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, DCTELEM *block, int last_index, int n)
285 { 311 {
286 int last_non_zero = 0; 312 int last_non_zero = 0;
287 int offset = 0;
288 int slevel, i, j; 313 int slevel, i, j;
289 314
290 dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]); 315 dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
291 ctx->m.last_dc[n] = block[0]; 316 ctx->m.last_dc[n] = block[0];
292 317
293 for (i = 1; i <= last_index; i++) { 318 for (i = 1; i <= last_index; i++) {
294 j = ctx->m.intra_scantable.permutated[i]; 319 j = ctx->m.intra_scantable.permutated[i];
295 slevel = block[j]; 320 slevel = block[j];
296 if (slevel) { 321 if (slevel) {
297 int run_level = i - last_non_zero - 1; 322 int run_level = i - last_non_zero - 1;
298 int sign; 323 int rlevel = (slevel<<1)|!!run_level;
299 MASK_ABS(sign, slevel); 324 put_bits(&ctx->m.pb, ctx->table_vlc_bits[rlevel], ctx->table_vlc_codes[rlevel]);
300 if (slevel > 64) {
301 offset = (slevel-1) >> 6;
302 slevel = 256 | (slevel & 63); // level 64 is treated as 0
303 }
304 if (run_level)
305 slevel |= 128;
306 put_bits(&ctx->m.pb, ctx->table_vlc_bits[slevel]+1, (ctx->table_vlc_codes[slevel]<<1)|(sign&1));
307 if (offset) {
308 put_bits(&ctx->m.pb, 4, offset);
309 offset = 0;
310 }
311 if (run_level) 325 if (run_level)
312 put_bits(&ctx->m.pb, ctx->table_run_bits[run_level], ctx->table_run_codes[run_level]); 326 put_bits(&ctx->m.pb, ctx->table_run_bits[run_level], ctx->table_run_codes[run_level]);
313 last_non_zero = i; 327 last_non_zero = i;
314 } 328 }
315 } 329 }
362 for (i = 1; i <= last_index; i++) { 376 for (i = 1; i <= last_index; i++) {
363 j = ctx->m.intra_scantable.permutated[i]; 377 j = ctx->m.intra_scantable.permutated[i];
364 level = block[j]; 378 level = block[j];
365 if (level) { 379 if (level) {
366 int run_level = i - last_non_zero - 1; 380 int run_level = i - last_non_zero - 1;
367 level = FFABS(level); 381 bits += ctx->table_vlc_bits[(level<<1)|!!run_level]+ctx->table_run_bits[run_level];
368 if (level > 64) {
369 level = 256 | (level & 63); // level 64 is treated as 0
370 bits += 4;
371 }
372 level |= (!!run_level)<<7;
373 bits += ctx->table_vlc_bits[level]+1 + ctx->table_run_bits[run_level];
374 last_non_zero = i; 382 last_non_zero = i;
375 } 383 }
376 } 384 }
377 return bits; 385 return bits;
378 } 386 }
510 for (i = 0; i < 8; i++) { 518 for (i = 0; i < 8; i++) {
511 DCTELEM *block = ctx->blocks[i]; 519 DCTELEM *block = ctx->blocks[i];
512 int last_index, overflow; 520 int last_index, overflow;
513 int n = dnxhd_switch_matrix(ctx, i); 521 int n = dnxhd_switch_matrix(ctx, i);
514 last_index = ctx->m.dct_quantize((MpegEncContext*)ctx, block, i, qscale, &overflow); 522 last_index = ctx->m.dct_quantize((MpegEncContext*)ctx, block, i, qscale, &overflow);
523 //START_TIMER;
515 dnxhd_encode_block(ctx, block, last_index, n); 524 dnxhd_encode_block(ctx, block, last_index, n);
525 //STOP_TIMER("encode_block");
516 } 526 }
517 } 527 }
518 if (put_bits_count(&ctx->m.pb)&31) 528 if (put_bits_count(&ctx->m.pb)&31)
519 put_bits(&ctx->m.pb, 32-(put_bits_count(&ctx->m.pb)&31), 0); 529 put_bits(&ctx->m.pb, 32-(put_bits_count(&ctx->m.pb)&31), 0);
520 } 530 }
811 } 821 }
812 822
813 static int dnxhd_encode_end(AVCodecContext *avctx) 823 static int dnxhd_encode_end(AVCodecContext *avctx)
814 { 824 {
815 DNXHDEncContext *ctx = avctx->priv_data; 825 DNXHDEncContext *ctx = avctx->priv_data;
826 int max_level = 1<<(ctx->cid_table->bit_depth+2);
816 int i; 827 int i;
817 828
818 av_freep(&ctx->table_vlc_codes); 829 av_free(ctx->table_vlc_codes-max_level*2);
819 av_freep(&ctx->table_vlc_bits); 830 av_free(ctx->table_vlc_bits -max_level*2);
820 av_freep(&ctx->table_run_codes); 831 av_freep(&ctx->table_run_codes);
821 av_freep(&ctx->table_run_bits); 832 av_freep(&ctx->table_run_bits);
822 833
823 av_freep(&ctx->mb_bits); 834 av_freep(&ctx->mb_bits);
824 av_freep(&ctx->mb_qscale); 835 av_freep(&ctx->mb_qscale);