| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @file | ||
| 23 | * Common VP5-VP9 range decoder stuff | ||
| 24 | */ | ||
| 25 | |||
| 26 | #ifndef AVCODEC_VPX_RAC_H | ||
| 27 | #define AVCODEC_VPX_RAC_H | ||
| 28 | |||
| 29 | #include <stdint.h> | ||
| 30 | |||
| 31 | #include "config.h" | ||
| 32 | #include "libavutil/attributes.h" | ||
| 33 | #include "bytestream.h" | ||
| 34 | |||
| 35 | typedef struct VPXRangeCoder { | ||
| 36 | int high; | ||
| 37 | int bits; /* stored negated (i.e. negative "bits" is a positive number of | ||
| 38 | bits left) in order to eliminate a negate in cache refilling */ | ||
| 39 | const uint8_t *buffer; | ||
| 40 | const uint8_t *end; | ||
| 41 | unsigned int code_word; | ||
| 42 | int end_reached; | ||
| 43 | } VPXRangeCoder; | ||
| 44 | |||
| 45 | extern const uint8_t ff_vpx_norm_shift[256]; | ||
| 46 | int ff_vpx_init_range_decoder(VPXRangeCoder *c, const uint8_t *buf, int buf_size); | ||
| 47 | |||
| 48 | /** | ||
| 49 | * returns 1 if the end of the stream has been reached, 0 otherwise. | ||
| 50 | */ | ||
| 51 | 836073 | static av_always_inline int vpx_rac_is_end(VPXRangeCoder *c) | |
| 52 | { | ||
| 53 |
4/4✓ Branch 0 taken 32239 times.
✓ Branch 1 taken 803834 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 32228 times.
|
836073 | if (c->end <= c->buffer && c->bits >= 0) |
| 54 | 11 | c->end_reached ++; | |
| 55 | 836073 | return c->end_reached > 10; | |
| 56 | } | ||
| 57 | |||
| 58 | 100058055 | static av_always_inline unsigned int vpx_rac_renorm(VPXRangeCoder *c) | |
| 59 | { | ||
| 60 | 100058055 | int shift = ff_vpx_norm_shift[c->high]; | |
| 61 | 100058055 | int bits = c->bits; | |
| 62 | 100058055 | unsigned int code_word = c->code_word; | |
| 63 | |||
| 64 | 100058055 | c->high <<= shift; | |
| 65 | 100058055 | code_word <<= shift; | |
| 66 | 100058055 | bits += shift; | |
| 67 |
4/4✓ Branch 0 taken 4329052 times.
✓ Branch 1 taken 95729003 times.
✓ Branch 2 taken 4328515 times.
✓ Branch 3 taken 537 times.
|
100058055 | if(bits >= 0 && c->buffer < c->end) { |
| 68 | 4328515 | code_word |= bytestream_get_be16(&c->buffer) << bits; | |
| 69 | 4328515 | bits -= 16; | |
| 70 | } | ||
| 71 | 100058055 | c->bits = bits; | |
| 72 | 100058055 | return code_word; | |
| 73 | } | ||
| 74 | |||
| 75 | #if ARCH_ARM | ||
| 76 | #include "arm/vpx_arith.h" | ||
| 77 | #elif ARCH_X86 | ||
| 78 | #include "x86/vpx_arith.h" | ||
| 79 | #endif | ||
| 80 | |||
| 81 | #ifndef vpx_rac_get_prob | ||
| 82 | #define vpx_rac_get_prob vpx_rac_get_prob | ||
| 83 | static av_always_inline int vpx_rac_get_prob(VPXRangeCoder *c, uint8_t prob) | ||
| 84 | { | ||
| 85 | unsigned int code_word = vpx_rac_renorm(c); | ||
| 86 | unsigned int low = 1 + (((c->high - 1) * prob) >> 8); | ||
| 87 | unsigned int low_shift = low << 16; | ||
| 88 | int bit = code_word >= low_shift; | ||
| 89 | |||
| 90 | c->high = bit ? c->high - low : low; | ||
| 91 | c->code_word = bit ? code_word - low_shift : code_word; | ||
| 92 | |||
| 93 | return bit; | ||
| 94 | } | ||
| 95 | #endif | ||
| 96 | |||
| 97 | #ifndef vpx_rac_get_prob_branchy | ||
| 98 | // branchy variant, to be used where there's a branch based on the bit decoded | ||
| 99 | 68662611 | static av_always_inline int vpx_rac_get_prob_branchy(VPXRangeCoder *c, int prob) | |
| 100 | { | ||
| 101 | 68662611 | unsigned long code_word = vpx_rac_renorm(c); | |
| 102 | 68662611 | unsigned low = 1 + (((c->high - 1) * prob) >> 8); | |
| 103 | 68662611 | unsigned low_shift = low << 16; | |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 31584863 times.
✓ Branch 1 taken 37077748 times.
|
68662611 | if (code_word >= low_shift) { |
| 106 | 31584863 | c->high -= low; | |
| 107 | 31584863 | c->code_word = code_word - low_shift; | |
| 108 | 31584863 | return 1; | |
| 109 | } | ||
| 110 | |||
| 111 | 37077748 | c->high = low; | |
| 112 | 37077748 | c->code_word = code_word; | |
| 113 | 37077748 | return 0; | |
| 114 | } | ||
| 115 | #endif | ||
| 116 | |||
| 117 | 1593448 | static av_always_inline int vpx_rac_get(VPXRangeCoder *c) | |
| 118 | { | ||
| 119 | 1593448 | unsigned int code_word = vpx_rac_renorm(c); | |
| 120 | /* equiprobable */ | ||
| 121 | 1593448 | int low = (c->high + 1) >> 1; | |
| 122 | 1593448 | unsigned int low_shift = low << 16; | |
| 123 | 1593448 | int bit = code_word >= low_shift; | |
| 124 |
2/2✓ Branch 0 taken 799591 times.
✓ Branch 1 taken 793857 times.
|
1593448 | if (bit) { |
| 125 | 799591 | c->high -= low; | |
| 126 | 799591 | code_word -= low_shift; | |
| 127 | } else { | ||
| 128 | 793857 | c->high = low; | |
| 129 | } | ||
| 130 | |||
| 131 | 1593448 | c->code_word = code_word; | |
| 132 | 1593448 | return bit; | |
| 133 | } | ||
| 134 | |||
| 135 | #endif /* AVCODEC_VPX_RAC_H */ | ||
| 136 |