| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * exp golomb vlc writing stuff | ||
| 3 | * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> | ||
| 4 | * Copyright (c) 2004 Alex Beregszaszi | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * @brief | ||
| 26 | * exp golomb vlc writing stuff | ||
| 27 | * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi | ||
| 28 | */ | ||
| 29 | |||
| 30 | #ifndef AVCODEC_PUT_GOLOMB_H | ||
| 31 | #define AVCODEC_PUT_GOLOMB_H | ||
| 32 | |||
| 33 | #include <stdint.h> | ||
| 34 | #include "put_bits.h" | ||
| 35 | |||
| 36 | extern const uint8_t ff_ue_golomb_len[256]; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * write unsigned exp golomb code. 2^16 - 2 at most | ||
| 40 | */ | ||
| 41 | 24573 | static inline void set_ue_golomb(PutBitContext *pb, int i) | |
| 42 | { | ||
| 43 | av_assert2(i >= 0); | ||
| 44 | av_assert2(i <= 0xFFFE); | ||
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 544 times.
✓ Branch 1 taken 24029 times.
|
24573 | if (i < 256) |
| 47 | 544 | put_bits(pb, ff_ue_golomb_len[i], i + 1); | |
| 48 | else { | ||
| 49 | 24029 | int e = av_log2(i + 1); | |
| 50 | 24029 | put_bits(pb, 2 * e + 1, i + 1); | |
| 51 | } | ||
| 52 | 24573 | } | |
| 53 | |||
| 54 | /** | ||
| 55 | * write unsigned exp golomb code. 2^32-2 at most. | ||
| 56 | */ | ||
| 57 | 8191 | static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i) | |
| 58 | { | ||
| 59 | av_assert2(i <= (UINT32_MAX - 1)); | ||
| 60 | |||
| 61 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8175 times.
|
8191 | if (i < 256) |
| 62 | 16 | put_bits(pb, ff_ue_golomb_len[i], i + 1); | |
| 63 | else { | ||
| 64 | 8175 | int e = av_log2(i + 1); | |
| 65 | 8175 | put_bits63(pb, 2 * e + 1, i + 1); | |
| 66 | } | ||
| 67 | 8191 | } | |
| 68 | |||
| 69 | /** | ||
| 70 | * write truncated unsigned exp golomb code. | ||
| 71 | */ | ||
| 72 | static inline void set_te_golomb(PutBitContext *pb, int i, int range) | ||
| 73 | { | ||
| 74 | av_assert2(range >= 1); | ||
| 75 | av_assert2(i <= range); | ||
| 76 | |||
| 77 | if (range == 2) | ||
| 78 | put_bits(pb, 1, i ^ 1); | ||
| 79 | else | ||
| 80 | set_ue_golomb(pb, i); | ||
| 81 | } | ||
| 82 | |||
| 83 | /** | ||
| 84 | * write signed exp golomb code. 16 bits at most. | ||
| 85 | */ | ||
| 86 | 8191 | static inline void set_se_golomb(PutBitContext *pb, int i) | |
| 87 | { | ||
| 88 | 8191 | i = 2 * i - 1; | |
| 89 |
2/2✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 4095 times.
|
8191 | if (i < 0) |
| 90 | 4096 | i ^= -1; //FIXME check if gcc does the right thing | |
| 91 | 8191 | set_ue_golomb(pb, i); | |
| 92 | 8191 | } | |
| 93 | |||
| 94 | /** | ||
| 95 | * write unsigned golomb rice code (ffv1). | ||
| 96 | */ | ||
| 97 | 163659991 | static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, | |
| 98 | int esc_len) | ||
| 99 | { | ||
| 100 | int e; | ||
| 101 | |||
| 102 | av_assert2(i >= 0); | ||
| 103 | |||
| 104 | 163659991 | e = i >> k; | |
| 105 |
2/2✓ Branch 0 taken 162901613 times.
✓ Branch 1 taken 758378 times.
|
163659991 | if (e < limit) |
| 106 | 162901613 | put_bits(pb, e + k + 1, (1 << k) + av_zero_extend(i, k)); | |
| 107 | else | ||
| 108 | 758378 | put_bits(pb, limit + esc_len, i - limit + 1); | |
| 109 | 163659991 | } | |
| 110 | |||
| 111 | /** | ||
| 112 | * write unsigned golomb rice code (jpegls). | ||
| 113 | */ | ||
| 114 | 43593349 | static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, | |
| 115 | int limit, int esc_len) | ||
| 116 | { | ||
| 117 | int e; | ||
| 118 | |||
| 119 | av_assert2(i >= 0); | ||
| 120 | |||
| 121 | 43593349 | e = (i >> k) + 1; | |
| 122 |
2/2✓ Branch 0 taken 43507065 times.
✓ Branch 1 taken 86284 times.
|
43593349 | if (e < limit) { |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43507065 times.
|
43507065 | while (e > 31) { |
| 124 | ✗ | put_bits(pb, 31, 0); | |
| 125 | ✗ | e -= 31; | |
| 126 | } | ||
| 127 | 43507065 | put_bits(pb, e, 1); | |
| 128 |
2/2✓ Branch 0 taken 41631807 times.
✓ Branch 1 taken 1875258 times.
|
43507065 | if (k) |
| 129 | 41631807 | put_sbits(pb, k, i); | |
| 130 | } else { | ||
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86284 times.
|
86284 | while (limit > 31) { |
| 132 | ✗ | put_bits(pb, 31, 0); | |
| 133 | ✗ | limit -= 31; | |
| 134 | } | ||
| 135 | 86284 | put_bits(pb, limit, 1); | |
| 136 | 86284 | put_bits(pb, esc_len, i - 1); | |
| 137 | } | ||
| 138 | 43593349 | } | |
| 139 | |||
| 140 | /** | ||
| 141 | * write signed golomb rice code (ffv1). | ||
| 142 | */ | ||
| 143 | 163659991 | static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, | |
| 144 | int esc_len) | ||
| 145 | { | ||
| 146 | int v; | ||
| 147 | |||
| 148 | 163659991 | v = -2 * i - 1; | |
| 149 | 163659991 | v ^= (v >> 31); | |
| 150 | |||
| 151 | 163659991 | set_ur_golomb(pb, v, k, limit, esc_len); | |
| 152 | 163659991 | } | |
| 153 | |||
| 154 | #endif /* AVCODEC_PUT_GOLOMB_H */ | ||
| 155 |