comparison matroskadec.c @ 6454:9ba950e0e021 libavformat

Optimize/simplify ebml_read_num.
author reimar
date Thu, 02 Sep 2010 19:17:46 +0000
parents a4e249f79b66
children c12416642843
comparison
equal deleted inserted replaced
6453:ad3c08bb5e68 6454:9ba950e0e021
536 * Returns: number of bytes read, < 0 on error 536 * Returns: number of bytes read, < 0 on error
537 */ 537 */
538 static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb, 538 static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb,
539 int max_size, uint64_t *number) 539 int max_size, uint64_t *number)
540 { 540 {
541 int len_mask = 0x80, read = 1, n = 1; 541 int read = 1, n = 1;
542 int64_t total = 0; 542 uint64_t total = 0;
543 543
544 /* The first byte tells us the length in bytes - get_byte() can normally 544 /* The first byte tells us the length in bytes - get_byte() can normally
545 * return 0, but since that's not a valid first ebmlID byte, we can 545 * return 0, but since that's not a valid first ebmlID byte, we can
546 * use it safely here to catch EOS. */ 546 * use it safely here to catch EOS. */
547 if (!(total = get_byte(pb))) { 547 if (!(total = get_byte(pb))) {
554 } 554 }
555 return AVERROR(EIO); /* EOS or actual I/O error */ 555 return AVERROR(EIO); /* EOS or actual I/O error */
556 } 556 }
557 557
558 /* get the length of the EBML number */ 558 /* get the length of the EBML number */
559 while (read <= max_size && !(total & len_mask)) { 559 read = 8 - ff_log2_tab[total];
560 read++;
561 len_mask >>= 1;
562 }
563 if (read > max_size) { 560 if (read > max_size) {
564 int64_t pos = url_ftell(pb) - 1; 561 int64_t pos = url_ftell(pb) - 1;
565 av_log(matroska->ctx, AV_LOG_ERROR, 562 av_log(matroska->ctx, AV_LOG_ERROR,
566 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n", 563 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
567 (uint8_t) total, pos, pos); 564 (uint8_t) total, pos, pos);
568 return AVERROR_INVALIDDATA; 565 return AVERROR_INVALIDDATA;
569 } 566 }
570 567
571 /* read out length */ 568 /* read out length */
572 total &= ~len_mask; 569 total ^= 1 << ff_log2_tab[total];
573 while (n++ < read) 570 while (n++ < read)
574 total = (total << 8) | get_byte(pb); 571 total = (total << 8) | get_byte(pb);
575 572
576 *number = total; 573 *number = total;
577 574