Mercurial > libavcodec.hg
comparison dv.c @ 1886:fbcf02596520 libavcodec
* DV decoder simplifications. Now it looks to be 6% faster. At least
on my desktop.
* Misc. DV cleanups and fixes.
author | romansh |
---|---|
date | Sun, 14 Mar 2004 22:09:58 +0000 |
parents | 45a1592dadca |
children | 85fe2f4633ec |
comparison
equal
deleted
inserted
replaced
1885:6588eda7d107 | 1886:fbcf02596520 |
---|---|
57 #define DV_VLC_MAP_RUN_SIZE 64 | 57 #define DV_VLC_MAP_RUN_SIZE 64 |
58 #define DV_VLC_MAP_LEV_SIZE 512 | 58 #define DV_VLC_MAP_LEV_SIZE 512 |
59 #endif | 59 #endif |
60 | 60 |
61 /* XXX: also include quantization */ | 61 /* XXX: also include quantization */ |
62 static RL_VLC_ELEM *dv_rl_vlc[1]; | 62 static RL_VLC_ELEM *dv_rl_vlc; |
63 /* VLC encoding lookup table */ | 63 /* VLC encoding lookup table */ |
64 static struct dv_vlc_pair { | 64 static struct dv_vlc_pair { |
65 uint32_t vlc; | 65 uint32_t vlc; |
66 uint8_t size; | 66 uint8_t size; |
67 } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL; | 67 } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL; |
109 /* NOTE: as a trick, we use the fact the no codes are unused | 109 /* NOTE: as a trick, we use the fact the no codes are unused |
110 to accelerate the parsing of partial codes */ | 110 to accelerate the parsing of partial codes */ |
111 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, | 111 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, |
112 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2); | 112 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2); |
113 | 113 |
114 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM)); | 114 dv_rl_vlc = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM)); |
115 if (!dv_rl_vlc[0]) { | 115 if (!dv_rl_vlc) { |
116 av_free(dv_vlc_map); | 116 av_free(dv_vlc_map); |
117 return -ENOMEM; | 117 return -ENOMEM; |
118 } | 118 } |
119 for(i = 0; i < dv_vlc.table_size; i++){ | 119 for(i = 0; i < dv_vlc.table_size; i++){ |
120 int code= dv_vlc.table[i][0]; | 120 int code= dv_vlc.table[i][0]; |
122 int level, run; | 122 int level, run; |
123 | 123 |
124 if(len<0){ //more bits needed | 124 if(len<0){ //more bits needed |
125 run= 0; | 125 run= 0; |
126 level= code; | 126 level= code; |
127 } else if (code == (NB_DV_VLC - 1)) { | |
128 /* EOB */ | |
129 run = 0; | |
130 level = 256; | |
131 } else { | 127 } else { |
132 run= dv_vlc_run[code] + 1; | 128 run= dv_vlc_run[code] + 1; |
133 level= dv_vlc_level[code]; | 129 level= dv_vlc_level[code]; |
134 } | 130 } |
135 dv_rl_vlc[0][i].len = len; | 131 dv_rl_vlc[i].len = len; |
136 dv_rl_vlc[0][i].level = level; | 132 dv_rl_vlc[i].level = level; |
137 dv_rl_vlc[0][i].run = run; | 133 dv_rl_vlc[i].run = run; |
138 } | 134 } |
135 free_vlc(&dv_vlc); | |
139 | 136 |
140 for (i = 0; i < NB_DV_VLC - 1; i++) { | 137 for (i = 0; i < NB_DV_VLC - 1; i++) { |
141 if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE || dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE) | 138 if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE || dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE) |
142 continue; | 139 continue; |
143 | 140 |
206 | 203 |
207 typedef struct BlockInfo { | 204 typedef struct BlockInfo { |
208 const uint8_t *shift_table; | 205 const uint8_t *shift_table; |
209 const uint8_t *scan_table; | 206 const uint8_t *scan_table; |
210 uint8_t pos; /* position in block */ | 207 uint8_t pos; /* position in block */ |
211 uint8_t eob_reached; /* true if EOB has been reached */ | |
212 uint8_t dct_mode; | 208 uint8_t dct_mode; |
213 uint8_t partial_bit_count; | 209 uint8_t partial_bit_count; |
214 uint16_t partial_bit_buffer; | 210 uint16_t partial_bit_buffer; |
215 int shift_offset; | 211 int shift_offset; |
216 } BlockInfo; | 212 } BlockInfo; |
227 #ifndef ALT_BITSTREAM_READER | 223 #ifndef ALT_BITSTREAM_READER |
228 #warning only works with ALT_BITSTREAM_READER | 224 #warning only works with ALT_BITSTREAM_READER |
229 #endif | 225 #endif |
230 | 226 |
231 /* decode ac coefs */ | 227 /* decode ac coefs */ |
232 static void dv_decode_ac(DVVideoDecodeContext *s, | 228 static void dv_decode_ac(DVVideoDecodeContext *s, BlockInfo *mb, DCTELEM *block) |
233 BlockInfo *mb, DCTELEM *block, int last_index) | 229 { |
234 { | 230 int last_index = get_bits_size(&s->gb); |
235 int last_re_index; | 231 int last_re_index; |
236 int shift_offset = mb->shift_offset; | 232 int shift_offset = mb->shift_offset; |
237 const uint8_t *scan_table = mb->scan_table; | 233 const uint8_t *scan_table = mb->scan_table; |
238 const uint8_t *shift_table = mb->shift_table; | 234 const uint8_t *shift_table = mb->shift_table; |
239 int pos = mb->pos; | 235 int pos = mb->pos; |
240 int level, pos1, sign, run; | 236 int level, pos1, run; |
241 int partial_bit_count; | 237 int partial_bit_count; |
238 int sign = 0; | |
242 #ifndef ALT_BITSTREAM_READER //FIXME | 239 #ifndef ALT_BITSTREAM_READER //FIXME |
243 int re_index=0; | 240 int re_index=0; |
244 int re1_index=0; | 241 int re1_index=0; |
245 #endif | 242 #endif |
246 OPEN_READER(re, &s->gb); | 243 OPEN_READER(re, &s->gb); |
252 /* if we must parse a partial vlc, we do it here */ | 249 /* if we must parse a partial vlc, we do it here */ |
253 partial_bit_count = mb->partial_bit_count; | 250 partial_bit_count = mb->partial_bit_count; |
254 if (partial_bit_count > 0) { | 251 if (partial_bit_count > 0) { |
255 uint8_t buf[4]; | 252 uint8_t buf[4]; |
256 uint32_t v; | 253 uint32_t v; |
257 int l, l1; | 254 int l; |
258 GetBitContext gb1; | 255 GetBitContext gb1; |
259 | 256 |
260 /* build the dummy bit buffer */ | 257 /* build the dummy bit buffer */ |
261 l = 16 - partial_bit_count; | 258 l = 16 - partial_bit_count; |
262 UPDATE_CACHE(re, &s->gb); | 259 UPDATE_CACHE(re, &s->gb); |
273 /* try to read the codeword */ | 270 /* try to read the codeword */ |
274 init_get_bits(&gb1, buf, 4*8); | 271 init_get_bits(&gb1, buf, 4*8); |
275 { | 272 { |
276 OPEN_READER(re1, &gb1); | 273 OPEN_READER(re1, &gb1); |
277 UPDATE_CACHE(re1, &gb1); | 274 UPDATE_CACHE(re1, &gb1); |
278 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0], | 275 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc, |
279 TEX_VLC_BITS, 2); | 276 TEX_VLC_BITS, 2); |
280 l = re1_index; | 277 l = re1_index; |
281 CLOSE_READER(re1, &gb1); | 278 CLOSE_READER(re1, &gb1); |
282 } | 279 } |
283 #ifdef VLC_DEBUG | 280 #ifdef VLC_DEBUG |
284 printf("****run=%d level=%d size=%d\n", run, level, l); | 281 printf("****run=%d level=%d size=%d\n", run, level, l); |
285 #endif | 282 #endif |
286 /* compute codeword length */ | 283 /* compute codeword length -- if too long, we cannot parse */ |
287 l1 = (level != 256 && level != 0); | |
288 /* if too long, we cannot parse */ | |
289 l -= partial_bit_count; | 284 l -= partial_bit_count; |
290 if ((re_index + l + l1) > last_index) | 285 if ((re_index + l + (level != 0)) > last_index) { |
286 mb->partial_bit_count += (last_index - re_index); | |
287 mb->partial_bit_buffer = v >> (16 - mb->partial_bit_count); | |
291 return; | 288 return; |
289 } | |
290 | |
292 /* skip read bits */ | 291 /* skip read bits */ |
293 last_re_index = 0; /* avoid warning */ | 292 last_re_index = 0; /* avoid warning */ |
294 re_index += l; | 293 re_index += l; |
295 /* by definition, if we can read the vlc, all partial bits | 294 /* by definition, if we can read the vlc, all partial bits |
296 will be read (otherwise we could have read the vlc before) */ | 295 will be read (otherwise we could have read the vlc before) */ |
305 #ifdef VLC_DEBUG | 304 #ifdef VLC_DEBUG |
306 printf("%2d: bits=%04x index=%d\n", | 305 printf("%2d: bits=%04x index=%d\n", |
307 pos, SHOW_UBITS(re, &s->gb, 16), re_index); | 306 pos, SHOW_UBITS(re, &s->gb, 16), re_index); |
308 #endif | 307 #endif |
309 last_re_index = re_index; | 308 last_re_index = re_index; |
310 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0], | 309 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc, |
311 TEX_VLC_BITS, 2); | 310 TEX_VLC_BITS, 2); |
312 handle_vlc: | 311 handle_vlc: |
313 #ifdef VLC_DEBUG | 312 #ifdef VLC_DEBUG |
314 printf("run=%d level=%d\n", run, level); | 313 printf("run=%d level=%d\n", run, level); |
315 #endif | 314 #endif |
316 if (level == 256) { | 315 if (level) { |
317 if (re_index > last_index) { | 316 sign = SHOW_SBITS(re, &s->gb, 1); |
318 cannot_read: | 317 LAST_SKIP_BITS(re, &s->gb, 1); |
319 /* put position before read code */ | 318 } |
320 re_index = last_re_index; | 319 if (re_index > last_index) { |
321 mb->eob_reached = 0; | 320 /* should be < 16 bits otherwise a codeword could have been parsed */ |
322 break; | 321 re_index = last_re_index; |
323 } | 322 UPDATE_CACHE(re, &s->gb); |
324 /* EOB */ | 323 mb->partial_bit_count = last_index - re_index; |
325 mb->eob_reached = 1; | 324 mb->partial_bit_buffer = SHOW_UBITS(re, &s->gb, mb->partial_bit_count); |
326 break; | 325 re_index = last_index; |
327 } else if (level != 0) { | 326 break; |
328 if ((re_index + 1) > last_index) | 327 } |
329 goto cannot_read; | 328 |
330 sign = SHOW_SBITS(re, &s->gb, 1); | 329 pos += run; |
330 if (pos >= 64) | |
331 break; | |
332 | |
333 if (level) { | |
331 level = (level ^ sign) - sign; | 334 level = (level ^ sign) - sign; |
332 LAST_SKIP_BITS(re, &s->gb, 1); | |
333 pos += run; | |
334 /* error */ | |
335 if (pos >= 64) { | |
336 goto read_error; | |
337 } | |
338 pos1 = scan_table[pos]; | 335 pos1 = scan_table[pos]; |
339 level = level << (shift_table[pos1] + shift_offset); | 336 level = level << (shift_table[pos1] + shift_offset); |
340 block[pos1] = level; | 337 block[pos1] = level; |
341 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]); | 338 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]); |
342 } else { | 339 } |
343 if (re_index > last_index) | |
344 goto cannot_read; | |
345 /* level is zero: means run without coding. No | |
346 sign is coded */ | |
347 pos += run; | |
348 /* error */ | |
349 if (pos >= 64) { | |
350 read_error: | |
351 #if defined(VLC_DEBUG) || 1 | |
352 av_log(NULL, AV_LOG_ERROR, "error pos=%d\n", pos); | |
353 #endif | |
354 /* for errors, we consider the eob is reached */ | |
355 mb->eob_reached = 1; | |
356 break; | |
357 } | |
358 } | |
359 } | 340 } |
360 CLOSE_READER(re, &s->gb); | 341 CLOSE_READER(re, &s->gb); |
361 mb->pos = pos; | 342 mb->pos = pos; |
362 } | 343 } |
363 | 344 |
364 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left) | 345 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb) |
365 { | 346 { |
347 int bits_left = get_bits_left(gb); | |
366 while (bits_left >= 16) { | 348 while (bits_left >= 16) { |
367 put_bits(pb, 16, get_bits(gb, 16)); | 349 put_bits(pb, 16, get_bits(gb, 16)); |
368 bits_left -= 16; | 350 bits_left -= 16; |
369 } | 351 } |
370 if (bits_left > 0) { | 352 if (bits_left > 0) { |
378 const uint16_t *mb_pos_ptr) | 360 const uint16_t *mb_pos_ptr) |
379 { | 361 { |
380 int quant, dc, dct_mode, class1, j; | 362 int quant, dc, dct_mode, class1, j; |
381 int mb_index, mb_x, mb_y, v, last_index; | 363 int mb_index, mb_x, mb_y, v, last_index; |
382 DCTELEM *block, *block1; | 364 DCTELEM *block, *block1; |
383 int c_offset, bits_left; | 365 int c_offset; |
384 uint8_t *y_ptr; | 366 uint8_t *y_ptr; |
385 BlockInfo mb_data[5 * 6], *mb, *mb1; | 367 BlockInfo mb_data[5 * 6], *mb, *mb1; |
386 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block); | 368 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block); |
387 uint8_t *buf_ptr; | 369 uint8_t *buf_ptr; |
388 PutBitContext pb, vs_pb; | 370 PutBitContext pb, vs_pb; |
389 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */ | 371 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */ |
390 int mb_bit_count; | |
391 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */ | 372 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */ |
392 int vs_bit_count; | |
393 | 373 |
394 memset(s->block, 0, sizeof(s->block)); | 374 memset(s->block, 0, sizeof(s->block)); |
395 | 375 |
396 /* pass 1 : read DC and AC coefficients in blocks */ | 376 /* pass 1 : read DC and AC coefficients in blocks */ |
397 buf_ptr = buf_ptr1; | 377 buf_ptr = buf_ptr1; |
398 block1 = &s->block[0][0]; | 378 block1 = &s->block[0][0]; |
399 mb1 = mb_data; | 379 mb1 = mb_data; |
400 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80); | 380 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80); |
401 vs_bit_count = 0; | 381 for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) { |
402 for(mb_index = 0; mb_index < 5; mb_index++) { | |
403 /* skip header */ | 382 /* skip header */ |
404 quant = buf_ptr[3] & 0x0f; | 383 quant = buf_ptr[3] & 0x0f; |
405 buf_ptr += 4; | 384 buf_ptr += 4; |
406 init_put_bits(&pb, mb_bit_buffer, 80); | 385 init_put_bits(&pb, mb_bit_buffer, 80); |
407 mb_bit_count = 0; | |
408 mb = mb1; | 386 mb = mb1; |
409 block = block1; | 387 block = block1; |
410 for(j = 0;j < 6; j++) { | 388 for(j = 0;j < 6; j++) { |
411 /* NOTE: size is not important here */ | 389 last_index = block_sizes[j]; |
412 init_get_bits(&s->gb, buf_ptr, 14*8); | 390 init_get_bits(&s->gb, buf_ptr, last_index); |
413 | 391 |
414 /* get the dc */ | 392 /* get the dc */ |
415 dc = get_bits(&s->gb, 9); | 393 dc = get_bits(&s->gb, 9); |
416 dc = (dc << (32 - 9)) >> (32 - 9); | 394 dc = (dc << (32 - 9)) >> (32 - 9); |
417 dct_mode = get_bits1(&s->gb); | 395 dct_mode = get_bits1(&s->gb); |
424 dc = dc << 2; | 402 dc = dc << 2; |
425 /* convert to unsigned because 128 is not added in the | 403 /* convert to unsigned because 128 is not added in the |
426 standard IDCT */ | 404 standard IDCT */ |
427 dc += 1024; | 405 dc += 1024; |
428 block[0] = dc; | 406 block[0] = dc; |
429 last_index = block_sizes[j]; | |
430 buf_ptr += last_index >> 3; | 407 buf_ptr += last_index >> 3; |
431 mb->pos = 0; | 408 mb->pos = 0; |
432 mb->partial_bit_count = 0; | 409 mb->partial_bit_count = 0; |
433 | 410 |
434 #ifdef VLC_DEBUG | 411 #ifdef VLC_DEBUG |
435 printf("MB block: %d, %d ", mb_index, j); | 412 printf("MB block: %d, %d ", mb_index, j); |
436 #endif | 413 #endif |
437 dv_decode_ac(s, mb, block, last_index); | 414 dv_decode_ac(s, mb, block); |
438 | 415 |
439 /* write the remaining bits in a new buffer only if the | 416 /* write the remaining bits in a new buffer only if the |
440 block is finished */ | 417 block is finished */ |
441 bits_left = last_index - get_bits_count(&s->gb); | 418 if (mb->pos >= 64) |
442 if (mb->eob_reached) { | 419 bit_copy(&pb, &s->gb); |
443 mb->partial_bit_count = 0; | 420 |
444 mb_bit_count += bits_left; | |
445 bit_copy(&pb, &s->gb, bits_left); | |
446 } else { | |
447 /* should be < 16 bits otherwise a codeword could have | |
448 been parsed */ | |
449 mb->partial_bit_count = bits_left; | |
450 mb->partial_bit_buffer = get_bits(&s->gb, bits_left); | |
451 } | |
452 block += 64; | 421 block += 64; |
453 mb++; | 422 mb++; |
454 } | 423 } |
455 | 424 |
456 flush_put_bits(&pb); | |
457 | |
458 /* pass 2 : we can do it just after */ | 425 /* pass 2 : we can do it just after */ |
459 #ifdef VLC_DEBUG | 426 #ifdef VLC_DEBUG |
460 printf("***pass 2 size=%d MB#=%d\n", mb_bit_count, mb_index); | 427 printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index); |
461 #endif | 428 #endif |
462 block = block1; | 429 block = block1; |
463 mb = mb1; | 430 mb = mb1; |
464 init_get_bits(&s->gb, mb_bit_buffer, 80*8); | 431 init_get_bits(&s->gb, mb_bit_buffer, put_bits_count(&pb)); |
465 for(j = 0;j < 6; j++) { | 432 flush_put_bits(&pb); |
466 if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) { | 433 for(j = 0;j < 6; j++, block += 64, mb++) { |
467 dv_decode_ac(s, mb, block, mb_bit_count); | 434 if (mb->pos < 64 && get_bits_left(&s->gb) > 0) { |
435 dv_decode_ac(s, mb, block); | |
468 /* if still not finished, no need to parse other blocks */ | 436 /* if still not finished, no need to parse other blocks */ |
469 if (!mb->eob_reached) { | 437 if (mb->pos < 64) |
470 /* we could not parse the current AC coefficient, | 438 break; |
471 so we add the remaining bytes */ | |
472 bits_left = mb_bit_count - get_bits_count(&s->gb); | |
473 if (bits_left > 0) { | |
474 mb->partial_bit_count += bits_left; | |
475 mb->partial_bit_buffer = | |
476 (mb->partial_bit_buffer << bits_left) | | |
477 get_bits(&s->gb, bits_left); | |
478 } | |
479 goto next_mb; | |
480 } | |
481 } | 439 } |
482 block += 64; | |
483 mb++; | |
484 } | 440 } |
485 /* all blocks are finished, so the extra bytes can be used at | 441 /* all blocks are finished, so the extra bytes can be used at |
486 the video segment level */ | 442 the video segment level */ |
487 bits_left = mb_bit_count - get_bits_count(&s->gb); | 443 if (j >= 6) |
488 vs_bit_count += bits_left; | 444 bit_copy(&vs_pb, &s->gb); |
489 bit_copy(&vs_pb, &s->gb, bits_left); | |
490 next_mb: | |
491 mb1 += 6; | |
492 block1 += 6 * 64; | |
493 } | 445 } |
494 | 446 |
495 /* we need a pass other the whole video segment */ | 447 /* we need a pass other the whole video segment */ |
496 flush_put_bits(&vs_pb); | 448 #ifdef VLC_DEBUG |
497 | 449 printf("***pass 3 size=%d\n", put_bits_count(&vs_pb)); |
498 #ifdef VLC_DEBUG | |
499 printf("***pass 3 size=%d\n", vs_bit_count); | |
500 #endif | 450 #endif |
501 block = &s->block[0][0]; | 451 block = &s->block[0][0]; |
502 mb = mb_data; | 452 mb = mb_data; |
503 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8); | 453 init_get_bits(&s->gb, vs_bit_buffer, put_bits_count(&vs_pb)); |
454 flush_put_bits(&vs_pb); | |
504 for(mb_index = 0; mb_index < 5; mb_index++) { | 455 for(mb_index = 0; mb_index < 5; mb_index++) { |
505 for(j = 0;j < 6; j++) { | 456 for(j = 0;j < 6; j++) { |
506 if (!mb->eob_reached) { | 457 if (mb->pos < 64) { |
507 #ifdef VLC_DEBUG | 458 #ifdef VLC_DEBUG |
508 printf("start %d:%d\n", mb_index, j); | 459 printf("start %d:%d\n", mb_index, j); |
509 #endif | 460 #endif |
510 dv_decode_ac(s, mb, block, vs_bit_count); | 461 dv_decode_ac(s, mb, block); |
511 } | 462 } |
463 if (mb->pos >= 64 && mb->pos < 127) | |
464 av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos); | |
512 block += 64; | 465 block += 64; |
513 mb++; | 466 mb++; |
514 } | 467 } |
515 } | 468 } |
516 | 469 |