| File: | root/firefox-clang/media/ffvpx/libavcodec/mathops.h |
| Warning: | line 138, column 55 The result of left shift is undefined because the right operand '2147483680' is not smaller than 32, the capacity of 'unsigned int' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* | ||||||
| 2 | * FLAC (Free Lossless Audio Codec) decoder | ||||||
| 3 | * Copyright (c) 2003 Alex Beregszaszi | ||||||
| 4 | * | ||||||
| 5 | * This file is part of FFmpeg. | ||||||
| 6 | * | ||||||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||||||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||||||
| 9 | * License as published by the Free Software Foundation; either | ||||||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||||||
| 11 | * | ||||||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||||||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||
| 15 | * Lesser General Public License for more details. | ||||||
| 16 | * | ||||||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||||||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||||||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||||
| 20 | */ | ||||||
| 21 | |||||||
| 22 | /** | ||||||
| 23 | * @file | ||||||
| 24 | * FLAC (Free Lossless Audio Codec) decoder | ||||||
| 25 | * @author Alex Beregszaszi | ||||||
| 26 | * @see http://flac.sourceforge.net/ | ||||||
| 27 | * | ||||||
| 28 | * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed | ||||||
| 29 | * through, starting from the initial 'fLaC' signature; or by passing the | ||||||
| 30 | * 34-byte streaminfo structure through avctx->extradata[_size] followed | ||||||
| 31 | * by data starting with the 0xFFF8 marker. | ||||||
| 32 | */ | ||||||
| 33 | |||||||
| 34 | #include <limits.h> | ||||||
| 35 | |||||||
| 36 | #include "libavutil/avassert.h" | ||||||
| 37 | #include "libavutil/crc.h" | ||||||
| 38 | #include "libavutil/mem.h" | ||||||
| 39 | #include "libavutil/opt.h" | ||||||
| 40 | #include "avcodec.h" | ||||||
| 41 | #include "codec_internal.h" | ||||||
| 42 | #include "get_bits.h" | ||||||
| 43 | #include "golomb.h" | ||||||
| 44 | #include "flac.h" | ||||||
| 45 | #include "flacdsp.h" | ||||||
| 46 | #include "flac_parse.h" | ||||||
| 47 | #include "thread.h" | ||||||
| 48 | #include "unary.h" | ||||||
| 49 | |||||||
| 50 | |||||||
| 51 | typedef struct FLACContext { | ||||||
| 52 | AVClass *class; | ||||||
| 53 | FLACStreaminfo stream_info; | ||||||
| 54 | |||||||
| 55 | AVCodecContext *avctx; ///< parent AVCodecContext | ||||||
| 56 | GetBitContext gb; ///< GetBitContext initialized to start at the current frame | ||||||
| 57 | |||||||
| 58 | int blocksize; ///< number of samples in the current frame | ||||||
| 59 | int sample_shift; ///< shift required to make output samples 16-bit or 32-bit | ||||||
| 60 | int ch_mode; ///< channel decorrelation type in the current frame | ||||||
| 61 | int got_streaminfo; ///< indicates if the STREAMINFO has been read | ||||||
| 62 | |||||||
| 63 | int32_t *decoded[FLAC_MAX_CHANNELS8]; ///< decoded samples | ||||||
| 64 | uint8_t *decoded_buffer; | ||||||
| 65 | unsigned int decoded_buffer_size; | ||||||
| 66 | int64_t *decoded_33bps; ///< decoded samples for a 33 bps subframe | ||||||
| 67 | uint8_t *decoded_buffer_33bps; | ||||||
| 68 | unsigned int decoded_buffer_size_33bps; | ||||||
| 69 | int buggy_lpc; ///< use workaround for old lavc encoded files | ||||||
| 70 | |||||||
| 71 | FLACDSPContext dsp; | ||||||
| 72 | } FLACContext; | ||||||
| 73 | |||||||
| 74 | static int allocate_buffers(FLACContext *s); | ||||||
| 75 | |||||||
| 76 | static void flac_set_bps(FLACContext *s) | ||||||
| 77 | { | ||||||
| 78 | enum AVSampleFormat req = s->avctx->request_sample_fmt; | ||||||
| 79 | int need32 = s->stream_info.bps > 16; | ||||||
| 80 | int want32 = av_get_bytes_per_sample(req) > 2; | ||||||
| 81 | int planar = av_sample_fmt_is_planar(req); | ||||||
| 82 | |||||||
| 83 | if (need32 || want32) { | ||||||
| 84 | if (planar) | ||||||
| 85 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P; | ||||||
| 86 | else | ||||||
| 87 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S32; | ||||||
| 88 | s->sample_shift = 32 - s->stream_info.bps; | ||||||
| 89 | } else { | ||||||
| 90 | if (planar) | ||||||
| 91 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P; | ||||||
| 92 | else | ||||||
| 93 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S16; | ||||||
| 94 | s->sample_shift = 16 - s->stream_info.bps; | ||||||
| 95 | } | ||||||
| 96 | } | ||||||
| 97 | |||||||
| 98 | static av_cold__attribute__((cold)) int flac_decode_init(AVCodecContext *avctx) | ||||||
| 99 | { | ||||||
| 100 | uint8_t *streaminfo; | ||||||
| 101 | int ret; | ||||||
| 102 | FLACContext *s = avctx->priv_data; | ||||||
| 103 | s->avctx = avctx; | ||||||
| 104 | |||||||
| 105 | /* for now, the raw FLAC header is allowed to be passed to the decoder as | ||||||
| 106 | frame data instead of extradata. */ | ||||||
| 107 | if (!avctx->extradata) | ||||||
| 108 | return 0; | ||||||
| 109 | |||||||
| 110 | if (!ff_flac_is_extradata_valid(avctx, &streaminfo)) | ||||||
| 111 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 112 | |||||||
| 113 | /* initialize based on the demuxer-supplied streamdata header */ | ||||||
| 114 | ret = ff_flac_parse_streaminfo(avctx, &s->stream_info, streaminfo); | ||||||
| 115 | if (ret < 0) | ||||||
| 116 | return ret; | ||||||
| 117 | ret = allocate_buffers(s); | ||||||
| 118 | if (ret < 0) | ||||||
| 119 | return ret; | ||||||
| 120 | flac_set_bps(s); | ||||||
| 121 | ff_flacdsp_init(&s->dsp, avctx->sample_fmt, | ||||||
| 122 | s->stream_info.channels); | ||||||
| 123 | s->got_streaminfo = 1; | ||||||
| 124 | |||||||
| 125 | return 0; | ||||||
| 126 | } | ||||||
| 127 | |||||||
| 128 | static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s) | ||||||
| 129 | { | ||||||
| 130 | av_log(avctx, AV_LOG_DEBUG48, " Max Blocksize: %d\n", s->max_blocksize); | ||||||
| 131 | av_log(avctx, AV_LOG_DEBUG48, " Max Framesize: %d\n", s->max_framesize); | ||||||
| 132 | av_log(avctx, AV_LOG_DEBUG48, " Samplerate: %d\n", s->samplerate); | ||||||
| 133 | av_log(avctx, AV_LOG_DEBUG48, " Channels: %d\n", s->channels); | ||||||
| 134 | av_log(avctx, AV_LOG_DEBUG48, " Bits: %d\n", s->bps); | ||||||
| 135 | } | ||||||
| 136 | |||||||
| 137 | static int allocate_buffers(FLACContext *s) | ||||||
| 138 | { | ||||||
| 139 | int buf_size; | ||||||
| 140 | int ret; | ||||||
| 141 | |||||||
| 142 | av_assert0(s->stream_info.max_blocksize)do { if (!(s->stream_info.max_blocksize)) { av_log(((void* )0), 0, "Assertion %s failed at %s:%d\n", "s->stream_info.max_blocksize" , "/root/firefox-clang/media/ffvpx/libavcodec/flacdec.c", 142 ); abort(); } } while (0); | ||||||
| 143 | |||||||
| 144 | buf_size = av_samples_get_buffer_size(NULL((void*)0), s->stream_info.channels, | ||||||
| 145 | s->stream_info.max_blocksize, | ||||||
| 146 | AV_SAMPLE_FMT_S32P, 0); | ||||||
| 147 | if (buf_size < 0) | ||||||
| 148 | return buf_size; | ||||||
| 149 | |||||||
| 150 | av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size); | ||||||
| 151 | if (!s->decoded_buffer) { | ||||||
| 152 | memset(s->decoded, 0, sizeof(s->decoded)); | ||||||
| 153 | return AVERROR(ENOMEM)(-(12)); | ||||||
| 154 | } | ||||||
| 155 | |||||||
| 156 | ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL((void*)0), | ||||||
| 157 | s->decoded_buffer, | ||||||
| 158 | s->stream_info.channels, | ||||||
| 159 | s->stream_info.max_blocksize, | ||||||
| 160 | AV_SAMPLE_FMT_S32P, 0); | ||||||
| 161 | if (ret >= 0 && s->stream_info.bps == 32 && s->stream_info.channels == 2) { | ||||||
| 162 | buf_size = av_samples_get_buffer_size(NULL((void*)0), 1, | ||||||
| 163 | s->stream_info.max_blocksize, | ||||||
| 164 | AV_SAMPLE_FMT_S64P, 0); | ||||||
| 165 | if (buf_size < 0) | ||||||
| 166 | return buf_size; | ||||||
| 167 | |||||||
| 168 | av_fast_malloc(&s->decoded_buffer_33bps, &s->decoded_buffer_size_33bps, buf_size); | ||||||
| 169 | if (!s->decoded_buffer_33bps) { | ||||||
| 170 | s->decoded_33bps = NULL((void*)0); | ||||||
| 171 | return AVERROR(ENOMEM)(-(12)); | ||||||
| 172 | } | ||||||
| 173 | |||||||
| 174 | ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL((void*)0), | ||||||
| 175 | s->decoded_buffer_33bps, | ||||||
| 176 | 1, | ||||||
| 177 | s->stream_info.max_blocksize, | ||||||
| 178 | AV_SAMPLE_FMT_S64P, 0); | ||||||
| 179 | |||||||
| 180 | } | ||||||
| 181 | return ret < 0 ? ret : 0; | ||||||
| 182 | } | ||||||
| 183 | |||||||
| 184 | /** | ||||||
| 185 | * Parse the STREAMINFO from an inline header. | ||||||
| 186 | * @param s the flac decoding context | ||||||
| 187 | * @param buf input buffer, starting with the "fLaC" marker | ||||||
| 188 | * @param buf_size buffer size | ||||||
| 189 | * @return non-zero if metadata is invalid | ||||||
| 190 | */ | ||||||
| 191 | static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size) | ||||||
| 192 | { | ||||||
| 193 | int metadata_type, metadata_size, ret; | ||||||
| 194 | |||||||
| 195 | if (buf_size < FLAC_STREAMINFO_SIZE34+8) { | ||||||
| 196 | /* need more data */ | ||||||
| 197 | return 0; | ||||||
| 198 | } | ||||||
| 199 | flac_parse_block_header(&buf[4], NULL((void*)0), &metadata_type, &metadata_size); | ||||||
| 200 | if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO || | ||||||
| 201 | metadata_size != FLAC_STREAMINFO_SIZE34) { | ||||||
| 202 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 203 | } | ||||||
| 204 | ret = ff_flac_parse_streaminfo(s->avctx, &s->stream_info, &buf[8]); | ||||||
| 205 | if (ret < 0) | ||||||
| 206 | return ret; | ||||||
| 207 | ret = allocate_buffers(s); | ||||||
| 208 | if (ret < 0) | ||||||
| 209 | return ret; | ||||||
| 210 | flac_set_bps(s); | ||||||
| 211 | ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, | ||||||
| 212 | s->stream_info.channels); | ||||||
| 213 | s->got_streaminfo = 1; | ||||||
| 214 | |||||||
| 215 | return 0; | ||||||
| 216 | } | ||||||
| 217 | |||||||
| 218 | /** | ||||||
| 219 | * Determine the size of an inline header. | ||||||
| 220 | * @param buf input buffer, starting with the "fLaC" marker | ||||||
| 221 | * @param buf_size buffer size | ||||||
| 222 | * @return number of bytes in the header, or 0 if more data is needed | ||||||
| 223 | */ | ||||||
| 224 | static int get_metadata_size(const uint8_t *buf, int buf_size) | ||||||
| 225 | { | ||||||
| 226 | int metadata_last, metadata_size; | ||||||
| 227 | const uint8_t *buf_end = buf + buf_size; | ||||||
| 228 | |||||||
| 229 | buf += 4; | ||||||
| 230 | do { | ||||||
| 231 | if (buf_end - buf < 4) | ||||||
| 232 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 233 | flac_parse_block_header(buf, &metadata_last, NULL((void*)0), &metadata_size); | ||||||
| 234 | buf += 4; | ||||||
| 235 | if (buf_end - buf < metadata_size) { | ||||||
| 236 | /* need more data in order to read the complete header */ | ||||||
| 237 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 238 | } | ||||||
| 239 | buf += metadata_size; | ||||||
| 240 | } while (!metadata_last); | ||||||
| 241 | |||||||
| 242 | return buf_size - (buf_end - buf); | ||||||
| 243 | } | ||||||
| 244 | |||||||
| 245 | static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order) | ||||||
| 246 | { | ||||||
| 247 | GetBitContext gb = s->gb; | ||||||
| 248 | int i, tmp, partition, method_type, rice_order; | ||||||
| 249 | int rice_bits, rice_esc; | ||||||
| 250 | int samples; | ||||||
| 251 | |||||||
| 252 | method_type = get_bits(&gb, 2); | ||||||
| 253 | rice_order = get_bits(&gb, 4); | ||||||
| 254 | |||||||
| 255 | samples = s->blocksize >> rice_order; | ||||||
| 256 | rice_bits = 4 + method_type; | ||||||
| 257 | rice_esc = (1 << rice_bits) - 1; | ||||||
| 258 | |||||||
| 259 | decoded += pred_order; | ||||||
| 260 | i = pred_order; | ||||||
| 261 | |||||||
| 262 | if (method_type > 1) { | ||||||
| 263 | av_log(s->avctx, AV_LOG_ERROR16, "illegal residual coding method %d\n", | ||||||
| 264 | method_type); | ||||||
| 265 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 266 | } | ||||||
| 267 | |||||||
| 268 | if (samples << rice_order != s->blocksize) { | ||||||
| 269 | av_log(s->avctx, AV_LOG_ERROR16, "invalid rice order: %i blocksize %i\n", | ||||||
| 270 | rice_order, s->blocksize); | ||||||
| 271 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 272 | } | ||||||
| 273 | |||||||
| 274 | if (pred_order > samples) { | ||||||
| 275 | av_log(s->avctx, AV_LOG_ERROR16, "invalid predictor order: %i > %i\n", | ||||||
| 276 | pred_order, samples); | ||||||
| 277 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 278 | } | ||||||
| 279 | |||||||
| 280 | for (partition = 0; partition < (1 << rice_order); partition++) { | ||||||
| 281 | tmp = get_bits(&gb, rice_bits); | ||||||
| 282 | if (tmp == rice_esc) { | ||||||
| 283 | tmp = get_bits(&gb, 5); | ||||||
| 284 | for (; i < samples; i++) | ||||||
| 285 | *decoded++ = get_sbits_long(&gb, tmp); | ||||||
| 286 | } else { | ||||||
| 287 | int real_limit = (tmp > 1) ? (INT_MAX2147483647 >> (tmp - 1)) + 2 : INT_MAX2147483647; | ||||||
| 288 | for (; i < samples; i++) { | ||||||
| 289 | int v = get_sr_golomb_flac(&gb, tmp, real_limit, 1); | ||||||
| 290 | if (v == 0x80000000){ | ||||||
| 291 | av_log(s->avctx, AV_LOG_ERROR16, "invalid residual\n"); | ||||||
| 292 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 293 | } | ||||||
| 294 | |||||||
| 295 | *decoded++ = v; | ||||||
| 296 | } | ||||||
| 297 | } | ||||||
| 298 | i= 0; | ||||||
| 299 | } | ||||||
| 300 | |||||||
| 301 | s->gb = gb; | ||||||
| 302 | |||||||
| 303 | return 0; | ||||||
| 304 | } | ||||||
| 305 | |||||||
| 306 | static int decode_subframe_fixed(FLACContext *s, int32_t *decoded, | ||||||
| 307 | int pred_order, int bps) | ||||||
| 308 | { | ||||||
| 309 | const int blocksize = s->blocksize; | ||||||
| 310 | unsigned av_uninit(a)a=a, av_uninit(b)b=b, av_uninit(c)c=c, av_uninit(d)d=d; | ||||||
| 311 | int i; | ||||||
| 312 | int ret; | ||||||
| 313 | |||||||
| 314 | /* warm up samples */ | ||||||
| 315 | for (i = 0; i < pred_order; i++) { | ||||||
| 316 | decoded[i] = get_sbits_long(&s->gb, bps); | ||||||
| 317 | } | ||||||
| 318 | |||||||
| 319 | if ((ret = decode_residuals(s, decoded, pred_order)) < 0) | ||||||
| 320 | return ret; | ||||||
| 321 | |||||||
| 322 | if (pred_order > 0) | ||||||
| 323 | a = decoded[pred_order-1]; | ||||||
| 324 | if (pred_order > 1) | ||||||
| 325 | b = a - decoded[pred_order-2]; | ||||||
| 326 | if (pred_order > 2) | ||||||
| 327 | c = b - decoded[pred_order-2] + decoded[pred_order-3]; | ||||||
| 328 | if (pred_order > 3) | ||||||
| 329 | d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4]; | ||||||
| 330 | |||||||
| 331 | switch (pred_order) { | ||||||
| 332 | case 0: | ||||||
| 333 | break; | ||||||
| 334 | case 1: | ||||||
| 335 | for (i = pred_order; i < blocksize; i++) | ||||||
| 336 | decoded[i] = a += decoded[i]; | ||||||
| 337 | break; | ||||||
| 338 | case 2: | ||||||
| 339 | for (i = pred_order; i < blocksize; i++) | ||||||
| 340 | decoded[i] = a += b += decoded[i]; | ||||||
| 341 | break; | ||||||
| 342 | case 3: | ||||||
| 343 | for (i = pred_order; i < blocksize; i++) | ||||||
| 344 | decoded[i] = a += b += c += decoded[i]; | ||||||
| 345 | break; | ||||||
| 346 | case 4: | ||||||
| 347 | for (i = pred_order; i < blocksize; i++) | ||||||
| 348 | decoded[i] = a += b += c += d += decoded[i]; | ||||||
| 349 | break; | ||||||
| 350 | default: | ||||||
| 351 | av_log(s->avctx, AV_LOG_ERROR16, "illegal pred order %d\n", pred_order); | ||||||
| 352 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 353 | } | ||||||
| 354 | |||||||
| 355 | return 0; | ||||||
| 356 | } | ||||||
| 357 | |||||||
| 358 | #define DECODER_SUBFRAME_FIXED_WIDE(residual){ const int blocksize = s->blocksize; int ret; if ((ret = decode_residuals (s, residual, pred_order)) < 0) return ret; switch (pred_order ) { case 0: for (int i = pred_order; i < blocksize; i++) decoded [i] = residual[i]; break; case 1: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t)residual[i] + (uint64_t )decoded[i-1]; break; case 2: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t)residual[i] + 2*(uint64_t )decoded[i-1] - (uint64_t)decoded[i-2]; break; case 3: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t )residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded [i-2] + (uint64_t)decoded[i-3]; break; case 4: for (int i = pred_order ; i < blocksize; i++) decoded[i] = (uint64_t)residual[i] + 4*(uint64_t)decoded[i-1] - 6*(uint64_t)decoded[i-2] + 4*(uint64_t )decoded[i-3] - (uint64_t)decoded[i-4]; break; default: av_log (s->avctx, 16, "illegal pred order %d\n", pred_order); return (-(int)(('I') | (('N') << 8) | (('D') << 16) | ( (unsigned)('A') << 24))); } return 0; } { \ | ||||||
| 359 | const int blocksize = s->blocksize; \ | ||||||
| 360 | int ret; \ | ||||||
| 361 | \ | ||||||
| 362 | if ((ret = decode_residuals(s, residual, pred_order)) < 0) \ | ||||||
| 363 | return ret; \ | ||||||
| 364 | \ | ||||||
| 365 | switch (pred_order) { \ | ||||||
| 366 | case 0: \ | ||||||
| 367 | for (int i = pred_order; i < blocksize; i++) \ | ||||||
| 368 | decoded[i] = residual[i]; \ | ||||||
| 369 | break; \ | ||||||
| 370 | case 1: \ | ||||||
| 371 | for (int i = pred_order; i < blocksize; i++) \ | ||||||
| 372 | decoded[i] = (uint64_t)residual[i] + (uint64_t)decoded[i-1];\ | ||||||
| 373 | break; \ | ||||||
| 374 | case 2: \ | ||||||
| 375 | for (int i = pred_order; i < blocksize; i++) \ | ||||||
| 376 | decoded[i] = (uint64_t)residual[i] + 2*(uint64_t)decoded[i-1] - (uint64_t)decoded[i-2]; \ | ||||||
| 377 | break; \ | ||||||
| 378 | case 3: \ | ||||||
| 379 | for (int i = pred_order; i < blocksize; i++) \ | ||||||
| 380 | decoded[i] = (uint64_t)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[i-2] + (uint64_t)decoded[i-3]; \ | ||||||
| 381 | break; \ | ||||||
| 382 | case 4: \ | ||||||
| 383 | for (int i = pred_order; i < blocksize; i++) \ | ||||||
| 384 | decoded[i] = (uint64_t)residual[i] + 4*(uint64_t)decoded[i-1] - 6*(uint64_t)decoded[i-2] + 4*(uint64_t)decoded[i-3] - (uint64_t)decoded[i-4]; \ | ||||||
| 385 | break; \ | ||||||
| 386 | default: \ | ||||||
| 387 | av_log(s->avctx, AV_LOG_ERROR16, "illegal pred order %d\n", pred_order); \ | ||||||
| 388 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); \ | ||||||
| 389 | } \ | ||||||
| 390 | return 0; \ | ||||||
| 391 | } | ||||||
| 392 | |||||||
| 393 | static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded, | ||||||
| 394 | int pred_order, int bps) | ||||||
| 395 | { | ||||||
| 396 | /* warm up samples */ | ||||||
| 397 | for (int i = 0; i < pred_order; i++) { | ||||||
| 398 | decoded[i] = get_sbits_long(&s->gb, bps); | ||||||
| 399 | } | ||||||
| 400 | DECODER_SUBFRAME_FIXED_WIDE(decoded){ const int blocksize = s->blocksize; int ret; if ((ret = decode_residuals (s, decoded, pred_order)) < 0) return ret; switch (pred_order ) { case 0: for (int i = pred_order; i < blocksize; i++) decoded [i] = decoded[i]; break; case 1: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t)decoded[i] + (uint64_t )decoded[i-1]; break; case 2: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t)decoded[i] + 2*(uint64_t )decoded[i-1] - (uint64_t)decoded[i-2]; break; case 3: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t )decoded[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[ i-2] + (uint64_t)decoded[i-3]; break; case 4: for (int i = pred_order ; i < blocksize; i++) decoded[i] = (uint64_t)decoded[i] + 4 *(uint64_t)decoded[i-1] - 6*(uint64_t)decoded[i-2] + 4*(uint64_t )decoded[i-3] - (uint64_t)decoded[i-4]; break; default: av_log (s->avctx, 16, "illegal pred order %d\n", pred_order); return (-(int)(('I') | (('N') << 8) | (('D') << 16) | ( (unsigned)('A') << 24))); } return 0; }; | ||||||
| 401 | } | ||||||
| 402 | |||||||
| 403 | |||||||
| 404 | static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded, | ||||||
| 405 | int32_t *residual, int pred_order) | ||||||
| 406 | { | ||||||
| 407 | /* warm up samples */ \ | ||||||
| 408 | for (int i = 0; i < pred_order; i++) { \ | ||||||
| 409 | decoded[i] = get_sbits64(&s->gb, 33); \ | ||||||
| 410 | } \ | ||||||
| 411 | DECODER_SUBFRAME_FIXED_WIDE(residual){ const int blocksize = s->blocksize; int ret; if ((ret = decode_residuals (s, residual, pred_order)) < 0) return ret; switch (pred_order ) { case 0: for (int i = pred_order; i < blocksize; i++) decoded [i] = residual[i]; break; case 1: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t)residual[i] + (uint64_t )decoded[i-1]; break; case 2: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t)residual[i] + 2*(uint64_t )decoded[i-1] - (uint64_t)decoded[i-2]; break; case 3: for (int i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t )residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded [i-2] + (uint64_t)decoded[i-3]; break; case 4: for (int i = pred_order ; i < blocksize; i++) decoded[i] = (uint64_t)residual[i] + 4*(uint64_t)decoded[i-1] - 6*(uint64_t)decoded[i-2] + 4*(uint64_t )decoded[i-3] - (uint64_t)decoded[i-4]; break; default: av_log (s->avctx, 16, "illegal pred order %d\n", pred_order); return (-(int)(('I') | (('N') << 8) | (('D') << 16) | ( (unsigned)('A') << 24))); } return 0; }; | ||||||
| 412 | } | ||||||
| 413 | |||||||
| 414 | static void lpc_analyze_remodulate(SUINT32uint32_t *decoded, const int coeffs[32], | ||||||
| 415 | int order, int qlevel, int len, int bps) | ||||||
| 416 | { | ||||||
| 417 | int i, j; | ||||||
| 418 | int ebps = 1 << (bps-1); | ||||||
| 419 | unsigned sigma = 0; | ||||||
| 420 | |||||||
| 421 | for (i = order; i < len; i++) | ||||||
| 422 | sigma |= decoded[i] + ebps; | ||||||
| 423 | |||||||
| 424 | if (sigma < 2*ebps) | ||||||
| 425 | return; | ||||||
| 426 | |||||||
| 427 | for (i = len - 1; i >= order; i--) { | ||||||
| 428 | int64_t p = 0; | ||||||
| 429 | for (j = 0; j < order; j++) | ||||||
| 430 | p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j]; | ||||||
| 431 | decoded[i] -= p >> qlevel; | ||||||
| 432 | } | ||||||
| 433 | for (i = order; i < len; i++, decoded++) { | ||||||
| 434 | int32_t p = 0; | ||||||
| 435 | for (j = 0; j < order; j++) | ||||||
| 436 | p += coeffs[j] * (uint32_t)decoded[j]; | ||||||
| 437 | decoded[j] += p >> qlevel; | ||||||
| 438 | } | ||||||
| 439 | } | ||||||
| 440 | |||||||
| 441 | static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order, | ||||||
| 442 | int bps) | ||||||
| 443 | { | ||||||
| 444 | int i, ret; | ||||||
| 445 | int coeff_prec, qlevel; | ||||||
| 446 | int coeffs[32]; | ||||||
| 447 | |||||||
| 448 | /* warm up samples */ | ||||||
| 449 | for (i = 0; i < pred_order; i++) { | ||||||
| 450 | decoded[i] = get_sbits_long(&s->gb, bps); | ||||||
| 451 | } | ||||||
| 452 | |||||||
| 453 | coeff_prec = get_bits(&s->gb, 4) + 1; | ||||||
| 454 | if (coeff_prec == 16) { | ||||||
| 455 | av_log(s->avctx, AV_LOG_ERROR16, "invalid coeff precision\n"); | ||||||
| 456 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 457 | } | ||||||
| 458 | qlevel = get_sbits(&s->gb, 5); | ||||||
| 459 | if (qlevel < 0) { | ||||||
| 460 | av_log(s->avctx, AV_LOG_ERROR16, "qlevel %d not supported, maybe buggy stream\n", | ||||||
| 461 | qlevel); | ||||||
| 462 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 463 | } | ||||||
| 464 | |||||||
| 465 | for (i = 0; i < pred_order; i++) { | ||||||
| 466 | coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); | ||||||
| 467 | } | ||||||
| 468 | |||||||
| 469 | if ((ret = decode_residuals(s, decoded, pred_order)) < 0) | ||||||
| 470 | return ret; | ||||||
| 471 | |||||||
| 472 | if ( ( s->buggy_lpc && s->stream_info.bps <= 16) | ||||||
| 473 | || ( !s->buggy_lpc && bps <= 16 | ||||||
| 474 | && bps + coeff_prec + av_log2(pred_order)(31 - __builtin_clz((pred_order)|1)) <= 32)) { | ||||||
| 475 | s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize); | ||||||
| 476 | } else { | ||||||
| 477 | s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize); | ||||||
| 478 | if (s->stream_info.bps <= 16) | ||||||
| 479 | lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps); | ||||||
| 480 | } | ||||||
| 481 | |||||||
| 482 | return 0; | ||||||
| 483 | } | ||||||
| 484 | |||||||
| 485 | static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded, | ||||||
| 486 | int32_t *residual, int pred_order) | ||||||
| 487 | { | ||||||
| 488 | int i, ret; | ||||||
| 489 | int coeff_prec, qlevel; | ||||||
| 490 | int coeffs[32]; | ||||||
| 491 | |||||||
| 492 | /* warm up samples */ | ||||||
| 493 | for (i = 0; i < pred_order; i++) { | ||||||
| 494 | decoded[i] = get_sbits64(&s->gb, 33); | ||||||
| 495 | } | ||||||
| 496 | |||||||
| 497 | coeff_prec = get_bits(&s->gb, 4) + 1; | ||||||
| 498 | if (coeff_prec == 16) { | ||||||
| 499 | av_log(s->avctx, AV_LOG_ERROR16, "invalid coeff precision\n"); | ||||||
| 500 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 501 | } | ||||||
| 502 | qlevel = get_sbits(&s->gb, 5); | ||||||
| 503 | if (qlevel < 0) { | ||||||
| 504 | av_log(s->avctx, AV_LOG_ERROR16, "qlevel %d not supported, maybe buggy stream\n", | ||||||
| 505 | qlevel); | ||||||
| 506 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 507 | } | ||||||
| 508 | |||||||
| 509 | for (i = 0; i < pred_order; i++) { | ||||||
| 510 | coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); | ||||||
| 511 | } | ||||||
| 512 | |||||||
| 513 | if ((ret = decode_residuals(s, residual, pred_order)) < 0) | ||||||
| 514 | return ret; | ||||||
| 515 | |||||||
| 516 | s->dsp.lpc33(decoded, residual, coeffs, pred_order, qlevel, s->blocksize); | ||||||
| 517 | |||||||
| 518 | return 0; | ||||||
| 519 | } | ||||||
| 520 | |||||||
| 521 | static inline int decode_subframe(FLACContext *s, int channel) | ||||||
| 522 | { | ||||||
| 523 | int32_t *decoded = s->decoded[channel]; | ||||||
| 524 | int type, wasted = 0; | ||||||
| 525 | int bps = s->stream_info.bps; | ||||||
| 526 | int i, ret; | ||||||
| 527 | |||||||
| 528 | if (channel
| ||||||
| 529 | if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE) | ||||||
| 530 | bps++; | ||||||
| 531 | } else { | ||||||
| 532 | if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE) | ||||||
| 533 | bps++; | ||||||
| 534 | } | ||||||
| 535 | |||||||
| 536 | if (get_bits1(&s->gb)) { | ||||||
| 537 | av_log(s->avctx, AV_LOG_ERROR16, "invalid subframe padding\n"); | ||||||
| 538 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 539 | } | ||||||
| 540 | type = get_bits(&s->gb, 6); | ||||||
| 541 | |||||||
| 542 | if (get_bits1(&s->gb)) { | ||||||
| 543 | int left = get_bits_left(&s->gb); | ||||||
| 544 | if ( left <= 0 || | ||||||
| 545 | (left < bps && !show_bits_long(&s->gb, left)) || | ||||||
| 546 | !show_bits_long(&s->gb, bps-1)) { | ||||||
| 547 | av_log(s->avctx, AV_LOG_ERROR16, | ||||||
| 548 | "Invalid number of wasted bits > available bits (%d) - left=%d\n", | ||||||
| 549 | bps, left); | ||||||
| 550 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 551 | } | ||||||
| 552 | wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb)); | ||||||
| 553 | bps -= wasted; | ||||||
| 554 | } | ||||||
| 555 | |||||||
| 556 | //FIXME use av_log2 for types | ||||||
| 557 | if (type == 0) { | ||||||
| 558 | if (bps < 33) { | ||||||
| 559 | int32_t tmp = get_sbits_long(&s->gb, bps); | ||||||
| 560 | for (i = 0; i < s->blocksize; i++) | ||||||
| 561 | decoded[i] = tmp; | ||||||
| 562 | } else { | ||||||
| 563 | int64_t tmp = get_sbits64(&s->gb, 33); | ||||||
| 564 | for (i = 0; i < s->blocksize; i++) | ||||||
| 565 | s->decoded_33bps[i] = tmp; | ||||||
| 566 | } | ||||||
| 567 | } else if (type == 1) { | ||||||
| 568 | if (bps < 33) { | ||||||
| 569 | for (i = 0; i < s->blocksize; i++) | ||||||
| 570 | decoded[i] = get_sbits_long(&s->gb, bps); | ||||||
| 571 | } else { | ||||||
| 572 | for (i = 0; i < s->blocksize; i++) | ||||||
| 573 | s->decoded_33bps[i] = get_sbits64(&s->gb, 33); | ||||||
| 574 | } | ||||||
| 575 | } else if ((type >= 8) && (type <= 12)) { | ||||||
| 576 | int order = type & ~0x8; | ||||||
| 577 | if (bps < 33) { | ||||||
| 578 | if (bps + order <= 32) { | ||||||
| 579 | if ((ret = decode_subframe_fixed(s, decoded, order, bps)) < 0) | ||||||
| 580 | return ret; | ||||||
| 581 | } else { | ||||||
| 582 | if ((ret = decode_subframe_fixed_wide(s, decoded, order, bps)) < 0) | ||||||
| 583 | return ret; | ||||||
| 584 | } | ||||||
| 585 | } else { | ||||||
| 586 | if ((ret = decode_subframe_fixed_33bps(s, s->decoded_33bps, decoded, order)) < 0) | ||||||
| 587 | return ret; | ||||||
| 588 | } | ||||||
| 589 | } else if (type >= 32) { | ||||||
| 590 | if (bps < 33) { | ||||||
| 591 | if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0) | ||||||
| 592 | return ret; | ||||||
| 593 | } else { | ||||||
| 594 | if ((ret = decode_subframe_lpc_33bps(s, s->decoded_33bps, decoded, (type & ~0x20)+1)) < 0) | ||||||
| 595 | return ret; | ||||||
| 596 | } | ||||||
| 597 | } else { | ||||||
| 598 | av_log(s->avctx, AV_LOG_ERROR16, "invalid coding type\n"); | ||||||
| 599 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 600 | } | ||||||
| 601 | |||||||
| 602 | if (wasted) { | ||||||
| 603 | if (wasted+bps == 33) { | ||||||
| 604 | s->dsp.wasted33(s->decoded_33bps, decoded, wasted, s->blocksize); | ||||||
| 605 | } else if (wasted < 32) { | ||||||
| 606 | s->dsp.wasted32(decoded, wasted, s->blocksize); | ||||||
| 607 | } | ||||||
| 608 | } | ||||||
| 609 | |||||||
| 610 | return 0; | ||||||
| 611 | } | ||||||
| 612 | |||||||
| 613 | static int decode_frame(FLACContext *s) | ||||||
| 614 | { | ||||||
| 615 | int i, ret; | ||||||
| 616 | GetBitContext *gb = &s->gb; | ||||||
| 617 | FLACFrameInfo fi; | ||||||
| 618 | |||||||
| 619 | if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) { | ||||||
| 620 | av_log(s->avctx, AV_LOG_ERROR16, "invalid frame header\n"); | ||||||
| 621 | return ret; | ||||||
| 622 | } | ||||||
| 623 | |||||||
| 624 | if ( s->stream_info.channels | ||||||
| 625 | && fi.channels != s->stream_info.channels | ||||||
| 626 | && s->got_streaminfo) { | ||||||
| 627 | s->stream_info.channels = fi.channels; | ||||||
| 628 | ff_flac_set_channel_layout(s->avctx, fi.channels); | ||||||
| 629 | ret = allocate_buffers(s); | ||||||
| 630 | if (ret < 0) | ||||||
| 631 | return ret; | ||||||
| 632 | } | ||||||
| 633 | s->stream_info.channels = fi.channels; | ||||||
| 634 | ff_flac_set_channel_layout(s->avctx, fi.channels); | ||||||
| 635 | s->ch_mode = fi.ch_mode; | ||||||
| 636 | |||||||
| 637 | if (!s->stream_info.bps && !fi.bps) { | ||||||
| 638 | av_log(s->avctx, AV_LOG_ERROR16, "bps not found in STREAMINFO or frame header\n"); | ||||||
| 639 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 640 | } | ||||||
| 641 | if (!fi.bps) { | ||||||
| 642 | fi.bps = s->stream_info.bps; | ||||||
| 643 | } else if (s->stream_info.bps
| ||||||
| 644 | av_log(s->avctx, AV_LOG_ERROR16, "switching bps mid-stream is not " | ||||||
| 645 | "supported\n"); | ||||||
| 646 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 647 | } | ||||||
| 648 | |||||||
| 649 | if (!s->stream_info.bps
| ||||||
| 650 | s->stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps; | ||||||
| 651 | flac_set_bps(s); | ||||||
| 652 | } | ||||||
| 653 | |||||||
| 654 | if (!s->stream_info.max_blocksize) | ||||||
| 655 | s->stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE65535; | ||||||
| 656 | if (fi.blocksize > s->stream_info.max_blocksize) { | ||||||
| 657 | av_log(s->avctx, AV_LOG_ERROR16, "blocksize %d > %d\n", fi.blocksize, | ||||||
| 658 | s->stream_info.max_blocksize); | ||||||
| 659 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 660 | } | ||||||
| 661 | s->blocksize = fi.blocksize; | ||||||
| 662 | |||||||
| 663 | if (!s->stream_info.samplerate && !fi.samplerate) { | ||||||
| 664 | av_log(s->avctx, AV_LOG_ERROR16, "sample rate not found in STREAMINFO" | ||||||
| 665 | " or frame header\n"); | ||||||
| 666 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 667 | } | ||||||
| 668 | if (fi.samplerate == 0) | ||||||
| 669 | fi.samplerate = s->stream_info.samplerate; | ||||||
| 670 | s->stream_info.samplerate = s->avctx->sample_rate = fi.samplerate; | ||||||
| 671 | |||||||
| 672 | if (!s->got_streaminfo || !s->decoded_buffer) { | ||||||
| 673 | ret = allocate_buffers(s); | ||||||
| 674 | if (ret < 0) | ||||||
| 675 | return ret; | ||||||
| 676 | s->got_streaminfo = 1; | ||||||
| 677 | dump_headers(s->avctx, &s->stream_info); | ||||||
| 678 | } | ||||||
| 679 | ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, | ||||||
| 680 | s->stream_info.channels); | ||||||
| 681 | |||||||
| 682 | // dump_headers(s->avctx, &s->stream_info); | ||||||
| 683 | |||||||
| 684 | /* subframes */ | ||||||
| 685 | for (i = 0; i < s->stream_info.channels; i++) { | ||||||
| 686 | if ((ret = decode_subframe(s, i)) < 0) | ||||||
| 687 | return ret; | ||||||
| 688 | } | ||||||
| 689 | |||||||
| 690 | align_get_bits(gb); | ||||||
| 691 | |||||||
| 692 | /* frame footer */ | ||||||
| 693 | skip_bits(gb, 16); /* data crc */ | ||||||
| 694 | |||||||
| 695 | return 0; | ||||||
| 696 | } | ||||||
| 697 | |||||||
| 698 | static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len) | ||||||
| 699 | { | ||||||
| 700 | int i; | ||||||
| 701 | if (ch_mode == FLAC_CHMODE_LEFT_SIDE ) { | ||||||
| 702 | for (i = 0; i < len; i++) | ||||||
| 703 | decoded[1][i] = decoded[0][i] - (uint64_t)decoded_33bps[i]; | ||||||
| 704 | } else if (ch_mode == FLAC_CHMODE_RIGHT_SIDE ) { | ||||||
| 705 | for (i = 0; i < len; i++) | ||||||
| 706 | decoded[0][i] = decoded[1][i] + (uint64_t)decoded_33bps[i]; | ||||||
| 707 | } else if (ch_mode == FLAC_CHMODE_MID_SIDE ) { | ||||||
| 708 | for (i = 0; i < len; i++) { | ||||||
| 709 | uint64_t a = decoded[0][i]; | ||||||
| 710 | int64_t b = decoded_33bps[i]; | ||||||
| 711 | a -= b >> 1; | ||||||
| 712 | decoded[0][i] = (a + b); | ||||||
| 713 | decoded[1][i] = a; | ||||||
| 714 | } | ||||||
| 715 | } | ||||||
| 716 | } | ||||||
| 717 | |||||||
| 718 | static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame, | ||||||
| 719 | int *got_frame_ptr, AVPacket *avpkt) | ||||||
| 720 | { | ||||||
| 721 | const uint8_t *buf = avpkt->data; | ||||||
| 722 | int buf_size = avpkt->size; | ||||||
| 723 | FLACContext *s = avctx->priv_data; | ||||||
| 724 | int bytes_read = 0; | ||||||
| 725 | int ret; | ||||||
| 726 | |||||||
| 727 | *got_frame_ptr = 0; | ||||||
| 728 | |||||||
| 729 | if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) { | ||||||
| |||||||
| 730 | av_log(s->avctx, AV_LOG_DEBUG48, "skipping flac header packet 1\n"); | ||||||
| 731 | return buf_size; | ||||||
| 732 | } | ||||||
| 733 | |||||||
| 734 | if (buf_size
| ||||||
| 735 | av_log(s->avctx, AV_LOG_DEBUG48, "skipping vorbis comment\n"); | ||||||
| 736 | return buf_size; | ||||||
| 737 | } | ||||||
| 738 | |||||||
| 739 | /* check that there is at least the smallest decodable amount of data. | ||||||
| 740 | this amount corresponds to the smallest valid FLAC frame possible. | ||||||
| 741 | FF F8 69 02 00 00 9A 00 00 34 */ | ||||||
| 742 | if (buf_size < FLAC_MIN_FRAME_SIZE10) | ||||||
| 743 | return buf_size; | ||||||
| 744 | |||||||
| 745 | /* check for inline header */ | ||||||
| 746 | if (AV_RB32(buf)av_bswap32((((const union unaligned_32 *) (buf))->l)) == MKBETAG('f','L','a','C')(('C') | (('a') << 8) | (('L') << 16) | ((unsigned )('f') << 24))) { | ||||||
| 747 | if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) { | ||||||
| 748 | av_log(s->avctx, AV_LOG_ERROR16, "invalid header\n"); | ||||||
| 749 | return ret; | ||||||
| 750 | } | ||||||
| 751 | return get_metadata_size(buf, buf_size); | ||||||
| 752 | } | ||||||
| 753 | |||||||
| 754 | /* decode frame */ | ||||||
| 755 | if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0) | ||||||
| 756 | return ret; | ||||||
| 757 | if ((ret = decode_frame(s)) < 0) { | ||||||
| 758 | av_log(s->avctx, AV_LOG_ERROR16, "decode_frame() failed\n"); | ||||||
| 759 | return ret; | ||||||
| 760 | } | ||||||
| 761 | bytes_read = get_bits_count(&s->gb)/8; | ||||||
| 762 | |||||||
| 763 | if ((s->avctx->err_recognition & (AV_EF_CRCCHECK(1<<0)|AV_EF_COMPLIANT(1<<17))) && | ||||||
| 764 | av_crc(av_crc_get_table(AV_CRC_16_ANSI), | ||||||
| 765 | 0, buf, bytes_read)) { | ||||||
| 766 | av_log(s->avctx, AV_LOG_ERROR16, "CRC error at PTS %"PRId64"l" "d""\n", avpkt->pts); | ||||||
| 767 | if (s->avctx->err_recognition & AV_EF_EXPLODE(1<<3)) | ||||||
| 768 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 769 | } | ||||||
| 770 | |||||||
| 771 | /* get output buffer */ | ||||||
| 772 | frame->nb_samples = s->blocksize; | ||||||
| 773 | if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0) | ||||||
| 774 | return ret; | ||||||
| 775 | |||||||
| 776 | if (s->stream_info.bps == 32 && s->ch_mode > 0) { | ||||||
| 777 | decorrelate_33bps(s->ch_mode, s->decoded, s->decoded_33bps, s->blocksize); | ||||||
| 778 | s->dsp.decorrelate[0](frame->data, s->decoded, s->stream_info.channels, | ||||||
| 779 | s->blocksize, s->sample_shift); | ||||||
| 780 | } else { | ||||||
| 781 | s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, | ||||||
| 782 | s->stream_info.channels, | ||||||
| 783 | s->blocksize, s->sample_shift); | ||||||
| 784 | } | ||||||
| 785 | |||||||
| 786 | if (bytes_read > buf_size) { | ||||||
| 787 | av_log(s->avctx, AV_LOG_ERROR16, "overread: %d\n", bytes_read - buf_size); | ||||||
| 788 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 789 | } | ||||||
| 790 | if (bytes_read < buf_size) { | ||||||
| 791 | av_log(s->avctx, AV_LOG_DEBUG48, "underread: %d orig size: %d\n", | ||||||
| 792 | buf_size - bytes_read, buf_size); | ||||||
| 793 | } | ||||||
| 794 | |||||||
| 795 | *got_frame_ptr = 1; | ||||||
| 796 | |||||||
| 797 | return bytes_read; | ||||||
| 798 | } | ||||||
| 799 | |||||||
| 800 | static av_cold__attribute__((cold)) int flac_decode_close(AVCodecContext *avctx) | ||||||
| 801 | { | ||||||
| 802 | FLACContext *s = avctx->priv_data; | ||||||
| 803 | |||||||
| 804 | av_freep(&s->decoded_buffer); | ||||||
| 805 | av_freep(&s->decoded_buffer_33bps); | ||||||
| 806 | |||||||
| 807 | return 0; | ||||||
| 808 | } | ||||||
| 809 | |||||||
| 810 | static const AVOption options[] = { | ||||||
| 811 | { "use_buggy_lpc", "emulate old buggy lavc behavior", offsetof(FLACContext, buggy_lpc)__builtin_offsetof(FLACContext, buggy_lpc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM(1 << 1) | AV_OPT_FLAG_AUDIO_PARAM(1 << 3) }, | ||||||
| 812 | { NULL((void*)0) }, | ||||||
| 813 | }; | ||||||
| 814 | |||||||
| 815 | static const AVClass flac_decoder_class = { | ||||||
| 816 | .class_name = "FLAC decoder", | ||||||
| 817 | .item_name = av_default_item_name, | ||||||
| 818 | .option = options, | ||||||
| 819 | .version = LIBAVUTIL_VERSION_INT((60)<<16 | (29)<<8 | (100)), | ||||||
| 820 | }; | ||||||
| 821 | |||||||
| 822 | const FFCodec ff_flac_decoder = { | ||||||
| 823 | .p.name = "flac", | ||||||
| 824 | CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)").p.long_name = "FLAC (Free Lossless Audio Codec)", | ||||||
| 825 | .p.type = AVMEDIA_TYPE_AUDIO, | ||||||
| 826 | .p.id = AV_CODEC_ID_FLAC, | ||||||
| 827 | .priv_data_size = sizeof(FLACContext), | ||||||
| 828 | .init = flac_decode_init, | ||||||
| 829 | .close = flac_decode_close, | ||||||
| 830 | FF_CODEC_DECODE_CB(flac_decode_frame).is_decoder = 1, .cb_type = FF_CODEC_CB_TYPE_DECODE, .cb.decode = (flac_decode_frame), | ||||||
| 831 | .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF(1 << 10) | | ||||||
| 832 | AV_CODEC_CAP_DR1(1 << 1) | | ||||||
| 833 | AV_CODEC_CAP_FRAME_THREADS(1 << 12), | ||||||
| 834 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,GCC diagnostic push
GCC diagnostic ignored "-Wdeprecated-declarations" .p.sample_fmts = ((((const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16 , AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE }))) GCC diagnostic pop | ||||||
| 835 | AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P)GCC diagnostic push
GCC diagnostic ignored "-Wdeprecated-declarations" .p.sample_fmts = ((((const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16 , AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE }))) GCC diagnostic pop , | ||||||
| 836 | .p.priv_class = &flac_decoder_class, | ||||||
| 837 | }; |
| 1 | /* | ||||||
| 2 | * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | ||||||
| 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 | * bitstream reader API header. | ||||||
| 24 | */ | ||||||
| 25 | |||||||
| 26 | #ifndef AVCODEC_GET_BITS_H | ||||||
| 27 | #define AVCODEC_GET_BITS_H | ||||||
| 28 | |||||||
| 29 | #include <stdint.h> | ||||||
| 30 | |||||||
| 31 | #include "libavutil/common.h" | ||||||
| 32 | #include "libavutil/intreadwrite.h" | ||||||
| 33 | #include "libavutil/avassert.h" | ||||||
| 34 | |||||||
| 35 | #include "defs.h" | ||||||
| 36 | #include "mathops.h" | ||||||
| 37 | #include "vlc.h" | ||||||
| 38 | |||||||
| 39 | /* | ||||||
| 40 | * Safe bitstream reading: | ||||||
| 41 | * optionally, the get_bits API can check to ensure that we | ||||||
| 42 | * don't read past input buffer boundaries. This is protected | ||||||
| 43 | * with CONFIG_SAFE_BITSTREAM_READER at the global level, and | ||||||
| 44 | * then below that with UNCHECKED_BITSTREAM_READER at the per- | ||||||
| 45 | * decoder level. This means that decoders that check internally | ||||||
| 46 | * can "#define UNCHECKED_BITSTREAM_READER 1" to disable | ||||||
| 47 | * overread checks. | ||||||
| 48 | * Boundary checking causes a minor performance penalty so for | ||||||
| 49 | * applications that won't want/need this, it can be disabled | ||||||
| 50 | * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0". | ||||||
| 51 | */ | ||||||
| 52 | #ifndef UNCHECKED_BITSTREAM_READER!1 | ||||||
| 53 | #define UNCHECKED_BITSTREAM_READER!1 !CONFIG_SAFE_BITSTREAM_READER1 | ||||||
| 54 | #endif | ||||||
| 55 | |||||||
| 56 | #ifndef CACHED_BITSTREAM_READER0 | ||||||
| 57 | #define CACHED_BITSTREAM_READER0 0 | ||||||
| 58 | #endif | ||||||
| 59 | |||||||
| 60 | #if CACHED_BITSTREAM_READER0 | ||||||
| 61 | |||||||
| 62 | // we always want the LE implementation, to provide get_bits_le() | ||||||
| 63 | #define BITSTREAM_LE | ||||||
| 64 | |||||||
| 65 | #ifndef BITSTREAM_READER_LE | ||||||
| 66 | # define BITSTREAM_BE | ||||||
| 67 | # define BITSTREAM_DEFAULT_BE | ||||||
| 68 | #endif | ||||||
| 69 | |||||||
| 70 | #include "bitstream.h" | ||||||
| 71 | |||||||
| 72 | #undef BITSTREAM_LE | ||||||
| 73 | #undef BITSTREAM_BE | ||||||
| 74 | #undef BITSTREAM_DEFAULT_BE | ||||||
| 75 | |||||||
| 76 | typedef BitstreamContext GetBitContext; | ||||||
| 77 | |||||||
| 78 | #define get_bits_count bits_tell | ||||||
| 79 | #define get_bits_bytesize bits_bytesize | ||||||
| 80 | #define get_bits_left bits_left | ||||||
| 81 | #define skip_bits_long bits_skip | ||||||
| 82 | #define skip_bits bits_skip | ||||||
| 83 | #define get_bits bits_read_nz | ||||||
| 84 | #define get_bitsz bits_read | ||||||
| 85 | #define get_bits_long bits_read | ||||||
| 86 | #define get_bits1 bits_read_bit | ||||||
| 87 | #define get_bits64 bits_read_64 | ||||||
| 88 | #define get_xbits bits_read_xbits | ||||||
| 89 | #define get_sbits bits_read_signed_nz | ||||||
| 90 | #define get_sbits_long bits_read_signed | ||||||
| 91 | #define show_bits bits_peek | ||||||
| 92 | #define show_bits_long bits_peek | ||||||
| 93 | #define init_get_bits bits_init | ||||||
| 94 | #define init_get_bits8 bits_init8 | ||||||
| 95 | #define align_get_bits bits_align | ||||||
| 96 | #define get_vlc2 bits_read_vlc | ||||||
| 97 | #define get_vlc_multi bits_read_vlc_multi | ||||||
| 98 | |||||||
| 99 | #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size) | ||||||
| 100 | #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n) | ||||||
| 101 | |||||||
| 102 | #define show_bits1(s) bits_peek(s, 1) | ||||||
| 103 | #define skip_bits1(s) bits_skip(s, 1) | ||||||
| 104 | |||||||
| 105 | #define skip_1stop_8data_bits bits_skip_1stop_8data | ||||||
| 106 | |||||||
| 107 | #else // CACHED_BITSTREAM_READER | ||||||
| 108 | |||||||
| 109 | typedef struct GetBitContext { | ||||||
| 110 | const uint8_t *buffer; | ||||||
| 111 | int index; | ||||||
| 112 | int size_in_bits; | ||||||
| 113 | int size_in_bits_plus8; | ||||||
| 114 | } GetBitContext; | ||||||
| 115 | |||||||
| 116 | static inline unsigned int get_bits(GetBitContext *s, int n); | ||||||
| 117 | static inline void skip_bits(GetBitContext *s, int n); | ||||||
| 118 | static inline unsigned int show_bits(GetBitContext *s, int n); | ||||||
| 119 | |||||||
| 120 | /* Bitstream reader API docs: | ||||||
| 121 | * name | ||||||
| 122 | * arbitrary name which is used as prefix for the internal variables | ||||||
| 123 | * | ||||||
| 124 | * gb | ||||||
| 125 | * getbitcontext | ||||||
| 126 | * | ||||||
| 127 | * OPEN_READER(name, gb) | ||||||
| 128 | * load gb into local variables | ||||||
| 129 | * | ||||||
| 130 | * CLOSE_READER(name, gb) | ||||||
| 131 | * store local vars in gb | ||||||
| 132 | * | ||||||
| 133 | * UPDATE_CACHE(name, gb) | ||||||
| 134 | * Refill the internal cache from the bitstream. | ||||||
| 135 | * After this call at least MIN_CACHE_BITS will be available. | ||||||
| 136 | * | ||||||
| 137 | * GET_CACHE(name, gb) | ||||||
| 138 | * Will output the contents of the internal cache, | ||||||
| 139 | * next bit is MSB of 32 or 64 bits (FIXME 64 bits). | ||||||
| 140 | * | ||||||
| 141 | * SHOW_UBITS(name, gb, num) | ||||||
| 142 | * Will return the next num bits. | ||||||
| 143 | * | ||||||
| 144 | * SHOW_SBITS(name, gb, num) | ||||||
| 145 | * Will return the next num bits and do sign extension. | ||||||
| 146 | * | ||||||
| 147 | * SKIP_BITS(name, gb, num) | ||||||
| 148 | * Will skip over the next num bits. | ||||||
| 149 | * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER. | ||||||
| 150 | * | ||||||
| 151 | * SKIP_CACHE(name, gb, num) | ||||||
| 152 | * Will remove the next num bits from the cache (note SKIP_COUNTER | ||||||
| 153 | * MUST be called before UPDATE_CACHE / CLOSE_READER). | ||||||
| 154 | * | ||||||
| 155 | * SKIP_COUNTER(name, gb, num) | ||||||
| 156 | * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS). | ||||||
| 157 | * | ||||||
| 158 | * LAST_SKIP_BITS(name, gb, num) | ||||||
| 159 | * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER. | ||||||
| 160 | * | ||||||
| 161 | * BITS_LEFT(name, gb) | ||||||
| 162 | * Return the number of bits left | ||||||
| 163 | * | ||||||
| 164 | * For examples see get_bits, show_bits, skip_bits, get_vlc. | ||||||
| 165 | */ | ||||||
| 166 | |||||||
| 167 | #define MIN_CACHE_BITS25 25 | ||||||
| 168 | |||||||
| 169 | #define OPEN_READER_NOSIZE_NOCACHE(name, gb)unsigned int name_index = (gb)->index \ | ||||||
| 170 | unsigned int name ## _index = (gb)->index | ||||||
| 171 | |||||||
| 172 | #define OPEN_READER_NOSIZE(name, gb)unsigned int name_index = (gb)->index; unsigned int name_cache \ | ||||||
| 173 | OPEN_READER_NOSIZE_NOCACHE(name, gb)unsigned int name_index = (gb)->index; \ | ||||||
| 174 | unsigned int name ## _cache | ||||||
| 175 | |||||||
| 176 | #if UNCHECKED_BITSTREAM_READER!1 | ||||||
| 177 | #define OPEN_READER(name, gb)unsigned int name_index = (gb)->index; unsigned int name_cache ; unsigned int name_size_plus8 = (gb)->size_in_bits_plus8 OPEN_READER_NOSIZE(name, gb)unsigned int name_index = (gb)->index; unsigned int name_cache | ||||||
| 178 | #define OPEN_READER_SIZE(name, gb)unsigned int name_size_plus8 = (gb)->size_in_bits_plus8 ((void)0) | ||||||
| 179 | #define BITS_AVAILABLE(name, gb)name_index < name_size_plus8 1 | ||||||
| 180 | #else | ||||||
| 181 | #define OPEN_READER_SIZE(name, gb)unsigned int name_size_plus8 = (gb)->size_in_bits_plus8 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8 | ||||||
| 182 | #define OPEN_READER(name, gb)unsigned int name_index = (gb)->index; unsigned int name_cache ; unsigned int name_size_plus8 = (gb)->size_in_bits_plus8 \ | ||||||
| 183 | OPEN_READER_NOSIZE(name, gb)unsigned int name_index = (gb)->index; unsigned int name_cache; \ | ||||||
| 184 | OPEN_READER_SIZE(name, gb)unsigned int name_size_plus8 = (gb)->size_in_bits_plus8 | ||||||
| 185 | |||||||
| 186 | #define BITS_AVAILABLE(name, gb)name_index < name_size_plus8 name ## _index < name ## _size_plus8 | ||||||
| 187 | #endif | ||||||
| 188 | |||||||
| 189 | #define CLOSE_READER(name, gb)(gb)->index = name_index (gb)->index = name ## _index | ||||||
| 190 | |||||||
| 191 | #define UPDATE_CACHE_BE_EXT(name, gb, bits, dst_bits)name_cache = AV_RBbits((gb)->buffer + (name_index >> 3)) << (name_index & 7) >> (bits - dst_bits) name ## _cache = \ | ||||||
| 192 | AV_RB ## bits((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) >> (bits - dst_bits) | ||||||
| 193 | |||||||
| 194 | #define UPDATE_CACHE_LE_EXT(name, gb, bits, dst_bits)name_cache = (uintdst_bits_t)(AV_RLbits((gb)->buffer + (name_index >> 3)) >> (name_index & 7)) name ## _cache = \ | ||||||
| 195 | (uint ## dst_bits ## _t)(AV_RL ## bits((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)) | ||||||
| 196 | |||||||
| 197 | /* Using these two macros ensures that 32 bits are available. */ | ||||||
| 198 | # define UPDATE_CACHE_LE_32(name, gb)name_cache = (uint32_t)((((const union unaligned_64 *) (((gb) )->buffer + (name_index >> 3)))->l) >> (name_index & 7)) UPDATE_CACHE_LE_EXT(name, (gb), 64, 32)name_cache = (uint32_t)((((const union unaligned_64 *) (((gb) )->buffer + (name_index >> 3)))->l) >> (name_index & 7)) | ||||||
| 199 | # define UPDATE_CACHE_BE_32(name, gb)name_cache = av_bswap64((((const union unaligned_64 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (64 - 32) UPDATE_CACHE_BE_EXT(name, (gb), 64, 32)name_cache = av_bswap64((((const union unaligned_64 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (64 - 32) | ||||||
| 200 | |||||||
| 201 | # define UPDATE_CACHE_LE(name, gb)name_cache = (uint32_t)((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l) >> (name_index & 7)) UPDATE_CACHE_LE_EXT(name, (gb), 32, 32)name_cache = (uint32_t)((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l) >> (name_index & 7)) | ||||||
| 202 | # define UPDATE_CACHE_BE(name, gb)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32) UPDATE_CACHE_BE_EXT(name, (gb), 32, 32)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32) | ||||||
| 203 | |||||||
| 204 | #ifdef BITSTREAM_READER_LE | ||||||
| 205 | |||||||
| 206 | # define UPDATE_CACHE(name, gb)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32) UPDATE_CACHE_LE(name, gb)name_cache = (uint32_t)((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l) >> (name_index & 7)) | ||||||
| 207 | # define UPDATE_CACHE_32(name, gb)name_cache = av_bswap64((((const union unaligned_64 *) ((((gb )))->buffer + (name_index >> 3)))->l)) << ( name_index & 7) >> (64 - 32) UPDATE_CACHE_LE_32(name, (gb))name_cache = (uint32_t)((((const union unaligned_64 *) ((((gb )))->buffer + (name_index >> 3)))->l) >> (name_index & 7)) | ||||||
| 208 | |||||||
| 209 | # define SKIP_CACHE(name, gb, num)name_cache <<= (num) name ## _cache >>= (num) | ||||||
| 210 | |||||||
| 211 | #else | ||||||
| 212 | |||||||
| 213 | # define UPDATE_CACHE(name, gb)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32) UPDATE_CACHE_BE(name, gb)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32) | ||||||
| 214 | # define UPDATE_CACHE_32(name, gb)name_cache = av_bswap64((((const union unaligned_64 *) ((((gb )))->buffer + (name_index >> 3)))->l)) << ( name_index & 7) >> (64 - 32) UPDATE_CACHE_BE_32(name, (gb))name_cache = av_bswap64((((const union unaligned_64 *) ((((gb )))->buffer + (name_index >> 3)))->l)) << ( name_index & 7) >> (64 - 32) | ||||||
| 215 | |||||||
| 216 | # define SKIP_CACHE(name, gb, num)name_cache <<= (num) name ## _cache <<= (num) | ||||||
| 217 | |||||||
| 218 | #endif | ||||||
| 219 | |||||||
| 220 | #if UNCHECKED_BITSTREAM_READER!1 | ||||||
| 221 | # define SKIP_COUNTER(name, gb, num)name_index = ((name_size_plus8) > (name_index + (num)) ? ( name_index + (num)) : (name_size_plus8)) name ## _index += (num) | ||||||
| 222 | #else | ||||||
| 223 | # define SKIP_COUNTER(name, gb, num)name_index = ((name_size_plus8) > (name_index + (num)) ? ( name_index + (num)) : (name_size_plus8)) \ | ||||||
| 224 | name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))((name ## _size_plus8) > (name ## _index + (num)) ? (name ## _index + (num)) : (name ## _size_plus8)) | ||||||
| 225 | #endif | ||||||
| 226 | |||||||
| 227 | #define BITS_LEFT(name, gb)((int)((gb)->size_in_bits - name_index)) ((int)((gb)->size_in_bits - name ## _index)) | ||||||
| 228 | |||||||
| 229 | #define SKIP_BITS(name, gb, num)do { name_cache <<= (num); name_index = ((name_size_plus8 ) > (name_index + (num)) ? (name_index + (num)) : (name_size_plus8 )); } while (0) \ | ||||||
| 230 | do { \ | ||||||
| 231 | SKIP_CACHE(name, gb, num)name_cache <<= (num); \ | ||||||
| 232 | SKIP_COUNTER(name, gb, num)name_index = ((name_size_plus8) > (name_index + (num)) ? ( name_index + (num)) : (name_size_plus8)); \ | ||||||
| 233 | } while (0) | ||||||
| 234 | |||||||
| 235 | #define LAST_SKIP_BITS(name, gb, num)name_index = ((name_size_plus8) > (name_index + (num)) ? ( name_index + (num)) : (name_size_plus8)) SKIP_COUNTER(name, gb, num)name_index = ((name_size_plus8) > (name_index + (num)) ? ( name_index + (num)) : (name_size_plus8)) | ||||||
| 236 | |||||||
| 237 | #define SHOW_UBITS_LE(name, gb, num)zero_extend(name_cache, num) zero_extend(name ## _cache, num) | ||||||
| 238 | #define SHOW_SBITS_LE(name, gb, num)sign_extend(name_cache, num) sign_extend(name ## _cache, num) | ||||||
| 239 | |||||||
| 240 | #define SHOW_UBITS_BE(name, gb, num)NEG_USR32(name_cache, num) NEG_USR32NEG_USR32(name ## _cache, num) | ||||||
| 241 | #define SHOW_SBITS_BE(name, gb, num)NEG_SSR32(name_cache, num) NEG_SSR32NEG_SSR32(name ## _cache, num) | ||||||
| 242 | |||||||
| 243 | #ifdef BITSTREAM_READER_LE | ||||||
| 244 | # define SHOW_UBITS(name, gb, num)NEG_USR32(name_cache, num) SHOW_UBITS_LE(name, gb, num)zero_extend(name_cache, num) | ||||||
| 245 | # define SHOW_SBITS(name, gb, num)NEG_SSR32(name_cache, num) SHOW_SBITS_LE(name, gb, num)sign_extend(name_cache, num) | ||||||
| 246 | #else | ||||||
| 247 | # define SHOW_UBITS(name, gb, num)NEG_USR32(name_cache, num) SHOW_UBITS_BE(name, gb, num)NEG_USR32(name_cache, num) | ||||||
| 248 | # define SHOW_SBITS(name, gb, num)NEG_SSR32(name_cache, num) SHOW_SBITS_BE(name, gb, num)NEG_SSR32(name_cache, num) | ||||||
| 249 | #endif | ||||||
| 250 | |||||||
| 251 | #define GET_CACHE(name, gb)((uint32_t) name_cache) ((uint32_t) name ## _cache) | ||||||
| 252 | |||||||
| 253 | |||||||
| 254 | static inline int get_bits_count(const GetBitContext *s) | ||||||
| 255 | { | ||||||
| 256 | return s->index; | ||||||
| 257 | } | ||||||
| 258 | |||||||
| 259 | /** | ||||||
| 260 | * Get the size of the GetBitContext's buffer in bytes. | ||||||
| 261 | * | ||||||
| 262 | * @param s the GetBitContext | ||||||
| 263 | * @param round_up If set, the number of bits will be rounded up to full bytes; | ||||||
| 264 | * this does not matter if the number of bits is known to be | ||||||
| 265 | * a multiple of eight, e.g. if the GetBitContext has been | ||||||
| 266 | * initialized with init_get_bits8. | ||||||
| 267 | */ | ||||||
| 268 | static inline int get_bits_bytesize(const GetBitContext *s, int round_up) | ||||||
| 269 | { | ||||||
| 270 | return (s->size_in_bits + (round_up ? 7 : 0)) >> 3; | ||||||
| 271 | } | ||||||
| 272 | |||||||
| 273 | /** | ||||||
| 274 | * Skips the specified number of bits. | ||||||
| 275 | * @param n the number of bits to skip, | ||||||
| 276 | * For the UNCHECKED_BITSTREAM_READER this must not cause the distance | ||||||
| 277 | * from the start to overflow int32_t. Staying within the bitstream + padding | ||||||
| 278 | * is sufficient, too. | ||||||
| 279 | */ | ||||||
| 280 | static inline void skip_bits_long(GetBitContext *s, int n) | ||||||
| 281 | { | ||||||
| 282 | #if UNCHECKED_BITSTREAM_READER!1 | ||||||
| 283 | s->index += n; | ||||||
| 284 | #else | ||||||
| 285 | s->index += av_clipav_clip_c(n, -s->index, s->size_in_bits_plus8 - s->index); | ||||||
| 286 | #endif | ||||||
| 287 | } | ||||||
| 288 | |||||||
| 289 | /** | ||||||
| 290 | * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). | ||||||
| 291 | * if MSB not set it is negative | ||||||
| 292 | * @param n length in bits | ||||||
| 293 | */ | ||||||
| 294 | static inline int get_xbits(GetBitContext *s, int n) | ||||||
| 295 | { | ||||||
| 296 | register int sign; | ||||||
| 297 | register int32_t cache; | ||||||
| 298 | OPEN_READER(re, s)unsigned int re_index = (s)->index; unsigned int re_cache; unsigned int re_size_plus8 = (s)->size_in_bits_plus8; | ||||||
| 299 | av_assert2(n>0 && n<=25)do { if (!(n>0 && n<=25)) { av_log(((void*)0), 0 , "Assertion %s failed at %s:%d\n", "n>0 && n<=25" , "/root/firefox-clang/media/ffvpx/libavcodec/get_bits.h", 299 ); abort(); } } while (0); | ||||||
| 300 | UPDATE_CACHE(re, s)re_cache = av_bswap32((((const union unaligned_32 *) (((s))-> buffer + (re_index >> 3)))->l)) << (re_index & 7) >> (32 - 32); | ||||||
| 301 | cache = GET_CACHE(re, s)((uint32_t) re_cache); | ||||||
| 302 | sign = ~cache >> 31; | ||||||
| 303 | LAST_SKIP_BITS(re, s, n)re_index = ((re_size_plus8) > (re_index + (n)) ? (re_index + (n)) : (re_size_plus8)); | ||||||
| 304 | CLOSE_READER(re, s)(s)->index = re_index; | ||||||
| 305 | return (NEG_USR32NEG_USR32(sign ^ cache, n) ^ sign) - sign; | ||||||
| 306 | } | ||||||
| 307 | |||||||
| 308 | static inline int get_xbits_le(GetBitContext *s, int n) | ||||||
| 309 | { | ||||||
| 310 | register int sign; | ||||||
| 311 | register int32_t cache; | ||||||
| 312 | OPEN_READER(re, s)unsigned int re_index = (s)->index; unsigned int re_cache; unsigned int re_size_plus8 = (s)->size_in_bits_plus8; | ||||||
| 313 | av_assert2(n>0 && n<=25)do { if (!(n>0 && n<=25)) { av_log(((void*)0), 0 , "Assertion %s failed at %s:%d\n", "n>0 && n<=25" , "/root/firefox-clang/media/ffvpx/libavcodec/get_bits.h", 313 ); abort(); } } while (0); | ||||||
| 314 | UPDATE_CACHE_LE(re, s)re_cache = (uint32_t)((((const union unaligned_32 *) (((s))-> buffer + (re_index >> 3)))->l) >> (re_index & 7)); | ||||||
| 315 | cache = GET_CACHE(re, s)((uint32_t) re_cache); | ||||||
| 316 | sign = sign_extend(~cache, n) >> 31; | ||||||
| 317 | LAST_SKIP_BITS(re, s, n)re_index = ((re_size_plus8) > (re_index + (n)) ? (re_index + (n)) : (re_size_plus8)); | ||||||
| 318 | CLOSE_READER(re, s)(s)->index = re_index; | ||||||
| 319 | return (zero_extend(sign ^ cache, n) ^ sign) - sign; | ||||||
| 320 | } | ||||||
| 321 | |||||||
| 322 | static inline int get_sbits(GetBitContext *s, int n) | ||||||
| 323 | { | ||||||
| 324 | register int tmp; | ||||||
| 325 | OPEN_READER(re, s)unsigned int re_index = (s)->index; unsigned int re_cache; unsigned int re_size_plus8 = (s)->size_in_bits_plus8; | ||||||
| 326 | av_assert2(n>0 && n<=25)do { if (!(n>0 && n<=25)) { av_log(((void*)0), 0 , "Assertion %s failed at %s:%d\n", "n>0 && n<=25" , "/root/firefox-clang/media/ffvpx/libavcodec/get_bits.h", 326 ); abort(); } } while (0); | ||||||
| 327 | UPDATE_CACHE(re, s)re_cache = av_bswap32((((const union unaligned_32 *) (((s))-> buffer + (re_index >> 3)))->l)) << (re_index & 7) >> (32 - 32); | ||||||
| 328 | tmp = SHOW_SBITS(re, s, n)NEG_SSR32(re_cache, n); | ||||||
| 329 | LAST_SKIP_BITS(re, s, n)re_index = ((re_size_plus8) > (re_index + (n)) ? (re_index + (n)) : (re_size_plus8)); | ||||||
| 330 | CLOSE_READER(re, s)(s)->index = re_index; | ||||||
| 331 | return tmp; | ||||||
| 332 | } | ||||||
| 333 | |||||||
| 334 | /** | ||||||
| 335 | * Read 1-25 bits. | ||||||
| 336 | */ | ||||||
| 337 | static inline unsigned int get_bits(GetBitContext *s, int n) | ||||||
| 338 | { | ||||||
| 339 | register unsigned int tmp; | ||||||
| 340 | OPEN_READER(re, s)unsigned int re_index = (s)->index; unsigned int re_cache; unsigned int re_size_plus8 = (s)->size_in_bits_plus8; | ||||||
| 341 | av_assert2(n>0 && n<=25)do { if (!(n>0 && n<=25)) { av_log(((void*)0), 0 , "Assertion %s failed at %s:%d\n", "n>0 && n<=25" , "/root/firefox-clang/media/ffvpx/libavcodec/get_bits.h", 341 ); abort(); } } while (0); | ||||||
| 342 | UPDATE_CACHE(re, s)re_cache = av_bswap32((((const union unaligned_32 *) (((s))-> buffer + (re_index >> 3)))->l)) << (re_index & 7) >> (32 - 32); | ||||||
| 343 | tmp = SHOW_UBITS(re, s, n)NEG_USR32(re_cache, n); | ||||||
| 344 | LAST_SKIP_BITS(re, s, n)re_index = ((re_size_plus8) > (re_index + (n)) ? (re_index + (n)) : (re_size_plus8)); | ||||||
| 345 | CLOSE_READER(re, s)(s)->index = re_index; | ||||||
| 346 | av_assert2(tmp < UINT64_C(1) << n)do { if (!(tmp < 1UL << n)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n" , "tmp < 1UL << n", "/root/firefox-clang/media/ffvpx/libavcodec/get_bits.h" , 346); abort(); } } while (0); | ||||||
| 347 | return tmp; | ||||||
| 348 | } | ||||||
| 349 | |||||||
| 350 | /** | ||||||
| 351 | * Read 0-25 bits. | ||||||
| 352 | */ | ||||||
| 353 | static av_always_inline__attribute__((always_inline)) inline int get_bitsz(GetBitContext *s, int n) | ||||||
| 354 | { | ||||||
| 355 | return n ? get_bits(s, n) : 0; | ||||||
| 356 | } | ||||||
| 357 | |||||||
| 358 | static inline unsigned int get_bits_le(GetBitContext *s, int n) | ||||||
| 359 | { | ||||||
| 360 | register int tmp; | ||||||
| 361 | OPEN_READER(re, s)unsigned int re_index = (s)->index; unsigned int re_cache; unsigned int re_size_plus8 = (s)->size_in_bits_plus8; | ||||||
| 362 | av_assert2(n>0 && n<=25)do { if (!(n>0 && n<=25)) { av_log(((void*)0), 0 , "Assertion %s failed at %s:%d\n", "n>0 && n<=25" , "/root/firefox-clang/media/ffvpx/libavcodec/get_bits.h", 362 ); abort(); } } while (0); | ||||||
| 363 | UPDATE_CACHE_LE(re, s)re_cache = (uint32_t)((((const union unaligned_32 *) (((s))-> buffer + (re_index >> 3)))->l) >> (re_index & 7)); | ||||||
| 364 | tmp = SHOW_UBITS_LE(re, s, n)zero_extend(re_cache, n); | ||||||
| 365 | LAST_SKIP_BITS(re, s, n)re_index = ((re_size_plus8) > (re_index + (n)) ? (re_index + (n)) : (re_size_plus8)); | ||||||
| 366 | CLOSE_READER(re, s)(s)->index = re_index; | ||||||
| 367 | return tmp; | ||||||
| 368 | } | ||||||
| 369 | |||||||
| 370 | /** | ||||||
| 371 | * Show 1-25 bits. | ||||||
| 372 | */ | ||||||
| 373 | static inline unsigned int show_bits(GetBitContext *s, int n) | ||||||
| 374 | { | ||||||
| 375 | register unsigned int tmp; | ||||||
| 376 | OPEN_READER_NOSIZE(re, s)unsigned int re_index = (s)->index; unsigned int re_cache; | ||||||
| 377 | av_assert2(n>0 && n<=25)do { if (!(n>0 && n<=25)) { av_log(((void*)0), 0 , "Assertion %s failed at %s:%d\n", "n>0 && n<=25" , "/root/firefox-clang/media/ffvpx/libavcodec/get_bits.h", 377 ); abort(); } } while (0); | ||||||
| 378 | UPDATE_CACHE(re, s)re_cache = av_bswap32((((const union unaligned_32 *) (((s))-> buffer + (re_index >> 3)))->l)) << (re_index & 7) >> (32 - 32); | ||||||
| 379 | tmp = SHOW_UBITS(re, s, n)NEG_USR32(re_cache, n); | ||||||
| 380 | return tmp; | ||||||
| 381 | } | ||||||
| 382 | |||||||
| 383 | static inline void skip_bits(GetBitContext *s, int n) | ||||||
| 384 | { | ||||||
| 385 | OPEN_READER_NOSIZE_NOCACHE(re, s)unsigned int re_index = (s)->index; | ||||||
| 386 | OPEN_READER_SIZE(re, s)unsigned int re_size_plus8 = (s)->size_in_bits_plus8; | ||||||
| 387 | LAST_SKIP_BITS(re, s, n)re_index = ((re_size_plus8) > (re_index + (n)) ? (re_index + (n)) : (re_size_plus8)); | ||||||
| 388 | CLOSE_READER(re, s)(s)->index = re_index; | ||||||
| 389 | } | ||||||
| 390 | |||||||
| 391 | static inline unsigned int get_bits1(GetBitContext *s) | ||||||
| 392 | { | ||||||
| 393 | unsigned int index = s->index; | ||||||
| 394 | uint8_t result = s->buffer[index >> 3]; | ||||||
| 395 | #ifdef BITSTREAM_READER_LE | ||||||
| 396 | result >>= index & 7; | ||||||
| 397 | result &= 1; | ||||||
| 398 | #else | ||||||
| 399 | result <<= index & 7; | ||||||
| 400 | result >>= 8 - 1; | ||||||
| 401 | #endif | ||||||
| 402 | #if !UNCHECKED_BITSTREAM_READER!1 | ||||||
| 403 | if (s->index < s->size_in_bits_plus8) | ||||||
| 404 | #endif | ||||||
| 405 | index++; | ||||||
| 406 | s->index = index; | ||||||
| 407 | |||||||
| 408 | return result; | ||||||
| 409 | } | ||||||
| 410 | |||||||
| 411 | static inline unsigned int show_bits1(GetBitContext *s) | ||||||
| 412 | { | ||||||
| 413 | return show_bits(s, 1); | ||||||
| 414 | } | ||||||
| 415 | |||||||
| 416 | static inline void skip_bits1(GetBitContext *s) | ||||||
| 417 | { | ||||||
| 418 | skip_bits(s, 1); | ||||||
| 419 | } | ||||||
| 420 | |||||||
| 421 | /** | ||||||
| 422 | * Read 0-32 bits. | ||||||
| 423 | */ | ||||||
| 424 | static inline unsigned int get_bits_long(GetBitContext *s, int n) | ||||||
| 425 | { | ||||||
| 426 | av_assert2(n>=0 && n<=32)do { if (!(n>=0 && n<=32)) { av_log(((void*)0), 0, "Assertion %s failed at %s:%d\n", "n>=0 && n<=32" , "/root/firefox-clang/media/ffvpx/libavcodec/get_bits.h", 426 ); abort(); } } while (0); | ||||||
| 427 | if (!n) { | ||||||
| 428 | return 0; | ||||||
| 429 | } else if ((!HAVE_FAST_64BIT1 || av_builtin_constant_p__builtin_constant_p(n <= MIN_CACHE_BITS25)) | ||||||
| 430 | && n <= MIN_CACHE_BITS25) { | ||||||
| 431 | return get_bits(s, n); | ||||||
| 432 | } else { | ||||||
| 433 | #if HAVE_FAST_64BIT1 | ||||||
| 434 | unsigned tmp; | ||||||
| 435 | OPEN_READER(re, s)unsigned int re_index = (s)->index; unsigned int re_cache; unsigned int re_size_plus8 = (s)->size_in_bits_plus8; | ||||||
| 436 | UPDATE_CACHE_32(re, s)re_cache = av_bswap64((((const union unaligned_64 *) ((((s))) ->buffer + (re_index >> 3)))->l)) << (re_index & 7) >> (64 - 32); | ||||||
| 437 | tmp = SHOW_UBITS(re, s, n)NEG_USR32(re_cache, n); | ||||||
| 438 | LAST_SKIP_BITS(re, s, n)re_index = ((re_size_plus8) > (re_index + (n)) ? (re_index + (n)) : (re_size_plus8)); | ||||||
| 439 | CLOSE_READER(re, s)(s)->index = re_index; | ||||||
| 440 | return tmp; | ||||||
| 441 | #else | ||||||
| 442 | #ifdef BITSTREAM_READER_LE | ||||||
| 443 | unsigned ret = get_bits(s, 16); | ||||||
| 444 | return ret | (get_bits(s, n - 16) << 16); | ||||||
| 445 | #else | ||||||
| 446 | unsigned ret = get_bits(s, 16) << (n - 16); | ||||||
| 447 | return ret | get_bits(s, n - 16); | ||||||
| 448 | #endif | ||||||
| 449 | #endif | ||||||
| 450 | } | ||||||
| 451 | } | ||||||
| 452 | |||||||
| 453 | /** | ||||||
| 454 | * Read 0-64 bits. | ||||||
| 455 | */ | ||||||
| 456 | static inline uint64_t get_bits64(GetBitContext *s, int n) | ||||||
| 457 | { | ||||||
| 458 | if (n <= 32) { | ||||||
| 459 | return get_bits_long(s, n); | ||||||
| 460 | } else { | ||||||
| 461 | #ifdef BITSTREAM_READER_LE | ||||||
| 462 | uint64_t ret = get_bits_long(s, 32); | ||||||
| 463 | return ret | (uint64_t) get_bits_long(s, n - 32) << 32; | ||||||
| 464 | #else | ||||||
| 465 | uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32; | ||||||
| 466 | return ret | get_bits_long(s, 32); | ||||||
| 467 | #endif | ||||||
| 468 | } | ||||||
| 469 | } | ||||||
| 470 | |||||||
| 471 | /** | ||||||
| 472 | * Read 0-32 bits as a signed integer. | ||||||
| 473 | */ | ||||||
| 474 | static inline int get_sbits_long(GetBitContext *s, int n) | ||||||
| 475 | { | ||||||
| 476 | // sign_extend(x, 0) is undefined | ||||||
| 477 | if (!n
| ||||||
| 478 | return 0; | ||||||
| 479 | |||||||
| 480 | return sign_extend(get_bits_long(s, n), n); | ||||||
| 481 | } | ||||||
| 482 | |||||||
| 483 | /** | ||||||
| 484 | * Read 0-64 bits as a signed integer. | ||||||
| 485 | */ | ||||||
| 486 | static inline int64_t get_sbits64(GetBitContext *s, int n) | ||||||
| 487 | { | ||||||
| 488 | // sign_extend(x, 0) is undefined | ||||||
| 489 | if (!n) | ||||||
| 490 | return 0; | ||||||
| 491 | |||||||
| 492 | return sign_extend64(get_bits64(s, n), n); | ||||||
| 493 | } | ||||||
| 494 | |||||||
| 495 | /** | ||||||
| 496 | * Show 0-32 bits. | ||||||
| 497 | */ | ||||||
| 498 | static inline unsigned int show_bits_long(GetBitContext *s, int n) | ||||||
| 499 | { | ||||||
| 500 | if (n <= MIN_CACHE_BITS25) { | ||||||
| 501 | return show_bits(s, n); | ||||||
| 502 | } else { | ||||||
| 503 | GetBitContext gb = *s; | ||||||
| 504 | return get_bits_long(&gb, n); | ||||||
| 505 | } | ||||||
| 506 | } | ||||||
| 507 | |||||||
| 508 | |||||||
| 509 | /** | ||||||
| 510 | * Initialize GetBitContext. | ||||||
| 511 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||||||
| 512 | * larger than the actual read bits because some optimized bitstream | ||||||
| 513 | * readers read 32 or 64 bit at once and could read over the end | ||||||
| 514 | * @param bit_size the size of the buffer in bits | ||||||
| 515 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||||||
| 516 | */ | ||||||
| 517 | static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer, | ||||||
| 518 | int bit_size) | ||||||
| 519 | { | ||||||
| 520 | int ret = 0; | ||||||
| 521 | |||||||
| 522 | if (bit_size >= INT_MAX2147483647 - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8)((7) > (64*8) ? (7) : (64*8)) || bit_size < 0 || !buffer) { | ||||||
| 523 | bit_size = 0; | ||||||
| 524 | buffer = NULL((void*)0); | ||||||
| 525 | ret = AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 526 | } | ||||||
| 527 | |||||||
| 528 | s->buffer = buffer; | ||||||
| 529 | s->size_in_bits = bit_size; | ||||||
| 530 | s->size_in_bits_plus8 = bit_size + 8; | ||||||
| 531 | s->index = 0; | ||||||
| 532 | |||||||
| 533 | return ret; | ||||||
| 534 | } | ||||||
| 535 | |||||||
| 536 | /** | ||||||
| 537 | * Initialize GetBitContext. | ||||||
| 538 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||||||
| 539 | * larger than the actual read bits because some optimized bitstream | ||||||
| 540 | * readers read 32 or 64 bit at once and could read over the end | ||||||
| 541 | * @param byte_size the size of the buffer in bytes | ||||||
| 542 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||||||
| 543 | */ | ||||||
| 544 | static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer, | ||||||
| 545 | int byte_size) | ||||||
| 546 | { | ||||||
| 547 | if (byte_size > INT_MAX2147483647 / 8 || byte_size < 0) | ||||||
| 548 | byte_size = -1; | ||||||
| 549 | return init_get_bits(s, buffer, byte_size * 8); | ||||||
| 550 | } | ||||||
| 551 | |||||||
| 552 | static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer, | ||||||
| 553 | int byte_size) | ||||||
| 554 | { | ||||||
| 555 | if (byte_size > INT_MAX2147483647 / 8 || byte_size < 0) | ||||||
| 556 | byte_size = -1; | ||||||
| 557 | return init_get_bits(s, buffer, byte_size * 8); | ||||||
| 558 | } | ||||||
| 559 | |||||||
| 560 | static inline const uint8_t *align_get_bits(GetBitContext *s) | ||||||
| 561 | { | ||||||
| 562 | int n = -get_bits_count(s) & 7; | ||||||
| 563 | if (n) | ||||||
| 564 | skip_bits(s, n); | ||||||
| 565 | return s->buffer + (s->index >> 3); | ||||||
| 566 | } | ||||||
| 567 | |||||||
| 568 | /** | ||||||
| 569 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. | ||||||
| 570 | * If the vlc code is invalid and max_depth>1, then the number of bits removed | ||||||
| 571 | * is undefined. | ||||||
| 572 | */ | ||||||
| 573 | #define GET_VLC(code, name, gb, table, bits, max_depth)do { unsigned idx_ = NEG_USR32(name_cache, bits); code = table [idx_].sym; int n_ = table[idx_].len; if (max_depth > 1 && n_ < 0) { name_index = ((name_size_plus8) > (name_index + (bits)) ? (name_index + (bits)) : (name_size_plus8)); name_cache = av_bswap32((((const union unaligned_32 *) (((gb))->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32); int nb__bits = -n_; idx_ = NEG_USR32( name_cache, nb__bits) + code; code = table[idx_].sym; n_ = table [idx_].len; if (max_depth > 2 && n_ < 0) { name_index = ((name_size_plus8) > (name_index + (nb__bits)) ? (name_index + (nb__bits)) : (name_size_plus8)); name_cache = av_bswap32( (((const union unaligned_32 *) (((gb))->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32); nb__bits = -n_; idx_ = NEG_USR32(name_cache, nb__bits ) + code; code = table[idx_].sym; n_ = table[idx_].len; } } do { name_cache <<= (n_); name_index = ((name_size_plus8) > (name_index + (n_)) ? (name_index + (n_)) : (name_size_plus8 )); } while (0); } while (0) \ | ||||||
| 574 | do { \ | ||||||
| 575 | unsigned idx_ = SHOW_UBITS(name, gb, bits)NEG_USR32(name_cache, bits); \ | ||||||
| 576 | code = table[idx_].sym; \ | ||||||
| 577 | int n_ = table[idx_].len; \ | ||||||
| 578 | \ | ||||||
| 579 | if (max_depth > 1 && n_ < 0) { \ | ||||||
| 580 | LAST_SKIP_BITS(name, gb, bits)name_index = ((name_size_plus8) > (name_index + (bits)) ? ( name_index + (bits)) : (name_size_plus8)); \ | ||||||
| 581 | UPDATE_CACHE(name, gb)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32); \ | ||||||
| 582 | \ | ||||||
| 583 | int nb__bits = -n_; \ | ||||||
| 584 | \ | ||||||
| 585 | idx_ = SHOW_UBITS(name, gb, nb__bits)NEG_USR32(name_cache, nb__bits) + code; \ | ||||||
| 586 | code = table[idx_].sym; \ | ||||||
| 587 | n_ = table[idx_].len; \ | ||||||
| 588 | if (max_depth > 2 && n_ < 0) { \ | ||||||
| 589 | LAST_SKIP_BITS(name, gb, nb__bits)name_index = ((name_size_plus8) > (name_index + (nb__bits) ) ? (name_index + (nb__bits)) : (name_size_plus8)); \ | ||||||
| 590 | UPDATE_CACHE(name, gb)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32); \ | ||||||
| 591 | \ | ||||||
| 592 | nb__bits = -n_; \ | ||||||
| 593 | \ | ||||||
| 594 | idx_ = SHOW_UBITS(name, gb, nb__bits)NEG_USR32(name_cache, nb__bits) + code; \ | ||||||
| 595 | code = table[idx_].sym; \ | ||||||
| 596 | n_ = table[idx_].len; \ | ||||||
| 597 | } \ | ||||||
| 598 | } \ | ||||||
| 599 | SKIP_BITS(name, gb, n_)do { name_cache <<= (n_); name_index = ((name_size_plus8 ) > (name_index + (n_)) ? (name_index + (n_)) : (name_size_plus8 )); } while (0); \ | ||||||
| 600 | } while (0) | ||||||
| 601 | |||||||
| 602 | #define GET_RL_VLC(level, run, name, gb, table, bits, \do { unsigned idx_ = NEG_USR32(name_cache, bits); level = table [idx_].level; int n_ = table[idx_].len8; if (max_depth > 1 && n_ < 0) { do { name_cache <<= (bits); name_index = ((name_size_plus8) > (name_index + (bits)) ? (name_index + (bits)) : (name_size_plus8)); } while (0); if (need_update ) { name_cache = av_bswap32((((const union unaligned_32 *) (( (gb))->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32); } int nb__bits = -n_ ; idx_ = NEG_USR32(name_cache, nb__bits) + level; level = table [idx_].level; n_ = table[idx_].len8; if (max_depth > 2 && n_ < 0) { name_index = ((name_size_plus8) > (name_index + (nb__bits)) ? (name_index + (nb__bits)) : (name_size_plus8 )); if (need_update) { name_cache = av_bswap32((((const union unaligned_32 *) (((gb))->buffer + (name_index >> 3) ))->l)) << (name_index & 7) >> (32 - 32); } nb__bits = -n_; idx_ = NEG_USR32(name_cache, nb__bits) + level ; level = table[idx_].level; n_ = table[idx_].len8; } } run = table[idx_].run; do { name_cache <<= (n_); name_index = ((name_size_plus8) > (name_index + (n_)) ? (name_index + ( n_)) : (name_size_plus8)); } while (0); } while (0) | ||||||
| 603 | max_depth, need_update)do { unsigned idx_ = NEG_USR32(name_cache, bits); level = table [idx_].level; int n_ = table[idx_].len8; if (max_depth > 1 && n_ < 0) { do { name_cache <<= (bits); name_index = ((name_size_plus8) > (name_index + (bits)) ? (name_index + (bits)) : (name_size_plus8)); } while (0); if (need_update ) { name_cache = av_bswap32((((const union unaligned_32 *) (( (gb))->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32); } int nb__bits = -n_ ; idx_ = NEG_USR32(name_cache, nb__bits) + level; level = table [idx_].level; n_ = table[idx_].len8; if (max_depth > 2 && n_ < 0) { name_index = ((name_size_plus8) > (name_index + (nb__bits)) ? (name_index + (nb__bits)) : (name_size_plus8 )); if (need_update) { name_cache = av_bswap32((((const union unaligned_32 *) (((gb))->buffer + (name_index >> 3) ))->l)) << (name_index & 7) >> (32 - 32); } nb__bits = -n_; idx_ = NEG_USR32(name_cache, nb__bits) + level ; level = table[idx_].level; n_ = table[idx_].len8; } } run = table[idx_].run; do { name_cache <<= (n_); name_index = ((name_size_plus8) > (name_index + (n_)) ? (name_index + ( n_)) : (name_size_plus8)); } while (0); } while (0) \ | ||||||
| 604 | do { \ | ||||||
| 605 | unsigned idx_ = SHOW_UBITS(name, gb, bits)NEG_USR32(name_cache, bits); \ | ||||||
| 606 | level = table[idx_].level; \ | ||||||
| 607 | int n_ = table[idx_].len8; \ | ||||||
| 608 | \ | ||||||
| 609 | if (max_depth > 1 && n_ < 0) { \ | ||||||
| 610 | SKIP_BITS(name, gb, bits)do { name_cache <<= (bits); name_index = ((name_size_plus8 ) > (name_index + (bits)) ? (name_index + (bits)) : (name_size_plus8 )); } while (0); \ | ||||||
| 611 | if (need_update) { \ | ||||||
| 612 | UPDATE_CACHE(name, gb)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32); \ | ||||||
| 613 | } \ | ||||||
| 614 | \ | ||||||
| 615 | int nb__bits = -n_; \ | ||||||
| 616 | \ | ||||||
| 617 | idx_ = SHOW_UBITS(name, gb, nb__bits)NEG_USR32(name_cache, nb__bits) + level; \ | ||||||
| 618 | level = table[idx_].level; \ | ||||||
| 619 | n_ = table[idx_].len8; \ | ||||||
| 620 | if (max_depth > 2 && n_ < 0) { \ | ||||||
| 621 | LAST_SKIP_BITS(name, gb, nb__bits)name_index = ((name_size_plus8) > (name_index + (nb__bits) ) ? (name_index + (nb__bits)) : (name_size_plus8)); \ | ||||||
| 622 | if (need_update) { \ | ||||||
| 623 | UPDATE_CACHE(name, gb)name_cache = av_bswap32((((const union unaligned_32 *) (((gb) )->buffer + (name_index >> 3)))->l)) << (name_index & 7) >> (32 - 32); \ | ||||||
| 624 | } \ | ||||||
| 625 | nb__bits = -n_; \ | ||||||
| 626 | \ | ||||||
| 627 | idx_ = SHOW_UBITS(name, gb, nb__bits)NEG_USR32(name_cache, nb__bits) + level; \ | ||||||
| 628 | level = table[idx_].level; \ | ||||||
| 629 | n_ = table[idx_].len8; \ | ||||||
| 630 | } \ | ||||||
| 631 | } \ | ||||||
| 632 | run = table[idx_].run; \ | ||||||
| 633 | SKIP_BITS(name, gb, n_)do { name_cache <<= (n_); name_index = ((name_size_plus8 ) > (name_index + (n_)) ? (name_index + (n_)) : (name_size_plus8 )); } while (0); \ | ||||||
| 634 | } while (0) | ||||||
| 635 | |||||||
| 636 | /** | ||||||
| 637 | * Parse a vlc code. | ||||||
| 638 | * @param bits is the number of bits which will be read at once, must be | ||||||
| 639 | * identical to nb_bits in vlc_init() | ||||||
| 640 | * @param max_depth is the number of times bits bits must be read to completely | ||||||
| 641 | * read the longest vlc code | ||||||
| 642 | * = (max_vlc_length + bits - 1) / bits | ||||||
| 643 | * @returns the code parsed or -1 if no vlc matches | ||||||
| 644 | */ | ||||||
| 645 | static av_always_inline__attribute__((always_inline)) inline int get_vlc2(GetBitContext *s, const VLCElem *table, | ||||||
| 646 | int bits, int max_depth) | ||||||
| 647 | { | ||||||
| 648 | int code; | ||||||
| 649 | |||||||
| 650 | OPEN_READER(re, s)unsigned int re_index = (s)->index; unsigned int re_cache; unsigned int re_size_plus8 = (s)->size_in_bits_plus8; | ||||||
| 651 | UPDATE_CACHE(re, s)re_cache = av_bswap32((((const union unaligned_32 *) (((s))-> buffer + (re_index >> 3)))->l)) << (re_index & 7) >> (32 - 32); | ||||||
| 652 | |||||||
| 653 | GET_VLC(code, re, s, table, bits, max_depth)do { unsigned idx_ = NEG_USR32(re_cache, bits); code = table[ idx_].sym; int n_ = table[idx_].len; if (max_depth > 1 && n_ < 0) { re_index = ((re_size_plus8) > (re_index + (bits )) ? (re_index + (bits)) : (re_size_plus8)); re_cache = av_bswap32 ((((const union unaligned_32 *) (((s))->buffer + (re_index >> 3)))->l)) << (re_index & 7) >> ( 32 - 32); int nb__bits = -n_; idx_ = NEG_USR32(re_cache, nb__bits ) + code; code = table[idx_].sym; n_ = table[idx_].len; if (max_depth > 2 && n_ < 0) { re_index = ((re_size_plus8) > (re_index + (nb__bits)) ? (re_index + (nb__bits)) : (re_size_plus8 )); re_cache = av_bswap32((((const union unaligned_32 *) (((s ))->buffer + (re_index >> 3)))->l)) << (re_index & 7) >> (32 - 32); nb__bits = -n_; idx_ = NEG_USR32 (re_cache, nb__bits) + code; code = table[idx_].sym; n_ = table [idx_].len; } } do { re_cache <<= (n_); re_index = ((re_size_plus8 ) > (re_index + (n_)) ? (re_index + (n_)) : (re_size_plus8 )); } while (0); } while (0); | ||||||
| 654 | |||||||
| 655 | CLOSE_READER(re, s)(s)->index = re_index; | ||||||
| 656 | |||||||
| 657 | return code; | ||||||
| 658 | } | ||||||
| 659 | |||||||
| 660 | static inline int get_vlc_multi(GetBitContext *s, uint8_t *dst, | ||||||
| 661 | av_unused__attribute__((unused)) const VLC_MULTI_ELEM *const Jtable, | ||||||
| 662 | const VLCElem *const table, | ||||||
| 663 | const int bits, const int max_depth, | ||||||
| 664 | av_unused__attribute__((unused)) const int symbols_size) | ||||||
| 665 | { | ||||||
| 666 | dst[0] = get_vlc2(s, table, bits, max_depth); | ||||||
| 667 | return 1; | ||||||
| 668 | } | ||||||
| 669 | |||||||
| 670 | static inline int decode012(GetBitContext *gb) | ||||||
| 671 | { | ||||||
| 672 | int n; | ||||||
| 673 | n = get_bits1(gb); | ||||||
| 674 | if (n == 0) | ||||||
| 675 | return 0; | ||||||
| 676 | else | ||||||
| 677 | return get_bits1(gb) + 1; | ||||||
| 678 | } | ||||||
| 679 | |||||||
| 680 | static inline int decode210(GetBitContext *gb) | ||||||
| 681 | { | ||||||
| 682 | if (get_bits1(gb)) | ||||||
| 683 | return 0; | ||||||
| 684 | else | ||||||
| 685 | return 2 - get_bits1(gb); | ||||||
| 686 | } | ||||||
| 687 | |||||||
| 688 | static inline int get_bits_left(GetBitContext *gb) | ||||||
| 689 | { | ||||||
| 690 | return gb->size_in_bits - get_bits_count(gb); | ||||||
| 691 | } | ||||||
| 692 | |||||||
| 693 | static inline int skip_1stop_8data_bits(GetBitContext *gb) | ||||||
| 694 | { | ||||||
| 695 | if (get_bits_left(gb) <= 0) | ||||||
| 696 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 697 | |||||||
| 698 | while (get_bits1(gb)) { | ||||||
| 699 | skip_bits(gb, 8); | ||||||
| 700 | if (get_bits_left(gb) <= 0) | ||||||
| 701 | return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | (( unsigned)('A') << 24))); | ||||||
| 702 | } | ||||||
| 703 | |||||||
| 704 | return 0; | ||||||
| 705 | } | ||||||
| 706 | |||||||
| 707 | #endif // CACHED_BITSTREAM_READER | ||||||
| 708 | |||||||
| 709 | #endif /* AVCODEC_GET_BITS_H */ |
| 1 | /* | |||
| 2 | * simple math operations | |||
| 3 | * Copyright (c) 2001, 2002 Fabrice Bellard | |||
| 4 | * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> et al | |||
| 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 | #ifndef AVCODEC_MATHOPS_H | |||
| 23 | #define AVCODEC_MATHOPS_H | |||
| 24 | ||||
| 25 | #include <stdint.h> | |||
| 26 | ||||
| 27 | #include "libavutil/attributes_internal.h" | |||
| 28 | #include "libavutil/common.h" | |||
| 29 | #include "config.h" | |||
| 30 | ||||
| 31 | #define MAX_NEG_CROP1024 1024 | |||
| 32 | ||||
| 33 | extern const uint32_t ff_inverse[257]; | |||
| 34 | extern const uint8_t ff_log2_run[41]; | |||
| 35 | EXTERNextern __attribute__((visibility("hidden"))) const uint32_t ff_square_tab[512]; | |||
| 36 | extern const uint8_t ff_sqrt_tab[256]; | |||
| 37 | EXTERNextern __attribute__((visibility("hidden"))) const uint8_t ff_crop_tab[256 + 2 * MAX_NEG_CROP1024]; | |||
| 38 | extern const uint8_t ff_zigzag_direct[64]; | |||
| 39 | extern const uint8_t ff_zigzag_scan[16+1]; | |||
| 40 | ||||
| 41 | #if ARCH_ARM0 | |||
| 42 | # include "arm/mathops.h" | |||
| 43 | #elif ARCH_MIPS0 | |||
| 44 | # include "mips/mathops.h" | |||
| 45 | #elif ARCH_PPC0 | |||
| 46 | # include "ppc/mathops.h" | |||
| 47 | #elif ARCH_RISCV0 | |||
| 48 | # include "riscv/mathops.h" | |||
| 49 | #elif ARCH_X861 | |||
| 50 | # include "x86/mathops.h" | |||
| 51 | #endif | |||
| 52 | ||||
| 53 | /* generic implementation */ | |||
| 54 | ||||
| 55 | #ifndef MUL64 | |||
| 56 | # define MUL64(a,b)((int64_t)(a) * (int64_t)(b)) ((int64_t)(a) * (int64_t)(b)) | |||
| 57 | #endif | |||
| 58 | ||||
| 59 | #ifndef MULL | |||
| 60 | # define MULL(a,b,s)(((int64_t)(a) * (int64_t)(b)) >> (s)) (MUL64(a, b)((int64_t)(a) * (int64_t)(b)) >> (s)) | |||
| 61 | #endif | |||
| 62 | ||||
| 63 | #ifndef MULH | |||
| 64 | static av_always_inline__attribute__((always_inline)) inline int MULH(int a, int b){ | |||
| 65 | return MUL64(a, b)((int64_t)(a) * (int64_t)(b)) >> 32; | |||
| 66 | } | |||
| 67 | #endif | |||
| 68 | ||||
| 69 | #ifndef UMULH | |||
| 70 | static av_always_inline__attribute__((always_inline)) inline unsigned UMULH(unsigned a, unsigned b){ | |||
| 71 | return ((uint64_t)(a) * (uint64_t)(b))>>32; | |||
| 72 | } | |||
| 73 | #endif | |||
| 74 | ||||
| 75 | #ifndef MAC64 | |||
| 76 | # define MAC64(d, a, b)((d) += ((int64_t)(a) * (int64_t)(b))) ((d) += MUL64(a, b)((int64_t)(a) * (int64_t)(b))) | |||
| 77 | #endif | |||
| 78 | ||||
| 79 | #ifndef MLS64 | |||
| 80 | # define MLS64(d, a, b)((d) -= ((int64_t)(a) * (int64_t)(b))) ((d) -= MUL64(a, b)((int64_t)(a) * (int64_t)(b))) | |||
| 81 | #endif | |||
| 82 | ||||
| 83 | /* signed 16x16 -> 32 multiply add accumulate */ | |||
| 84 | #ifndef MAC16 | |||
| 85 | # define MAC16(rt, ra, rb)rt += (ra) * (rb) rt += (ra) * (rb) | |||
| 86 | #endif | |||
| 87 | ||||
| 88 | /* signed 16x16 -> 32 multiply */ | |||
| 89 | #ifndef MUL16 | |||
| 90 | # define MUL16(ra, rb)((ra) * (rb)) ((ra) * (rb)) | |||
| 91 | #endif | |||
| 92 | ||||
| 93 | #ifndef MLS16 | |||
| 94 | # define MLS16(rt, ra, rb)((rt) -= (ra) * (rb)) ((rt) -= (ra) * (rb)) | |||
| 95 | #endif | |||
| 96 | ||||
| 97 | /* median of 3 */ | |||
| 98 | static inline av_const__attribute__((const)) int median3_c(int a, int b, int c) | |||
| 99 | { | |||
| 100 | int max2, min2, m; | |||
| 101 | ||||
| 102 | if (a >= b) { | |||
| 103 | max2 = a; | |||
| 104 | min2 = b; | |||
| 105 | } else { | |||
| 106 | max2 = b; | |||
| 107 | min2 = a; | |||
| 108 | } | |||
| 109 | m = (c >= max2) ? max2 : c; | |||
| 110 | ||||
| 111 | return (m >= min2) ? m : min2; | |||
| 112 | } | |||
| 113 | ||||
| 114 | #ifndef mid_predmid_pred | |||
| 115 | #define mid_predmid_pred median3_c | |||
| 116 | #endif | |||
| 117 | ||||
| 118 | #ifndef median4median4 | |||
| 119 | #define median4median4 median4median4 | |||
| 120 | static inline av_const__attribute__((const)) int median4median4(int a, int b, int c, int d) | |||
| 121 | { | |||
| 122 | if (a < b) { | |||
| 123 | if (c < d) return (FFMIN(b, d)((b) > (d) ? (d) : (b)) + FFMAX(a, c)((a) > (c) ? (a) : (c))) / 2; | |||
| 124 | else return (FFMIN(b, c)((b) > (c) ? (c) : (b)) + FFMAX(a, d)((a) > (d) ? (a) : (d))) / 2; | |||
| 125 | } else { | |||
| 126 | if (c < d) return (FFMIN(a, d)((a) > (d) ? (d) : (a)) + FFMAX(b, c)((b) > (c) ? (b) : (c))) / 2; | |||
| 127 | else return (FFMIN(a, c)((a) > (c) ? (c) : (a)) + FFMAX(b, d)((b) > (d) ? (b) : (d))) / 2; | |||
| 128 | } | |||
| 129 | } | |||
| 130 | #endif | |||
| 131 | ||||
| 132 | #define FF_SIGNBIT(x)((x) >> 8 * sizeof(x) - 1) ((x) >> CHAR_BIT8 * sizeof(x) - 1) | |||
| 133 | ||||
| 134 | #ifndef sign_extend | |||
| 135 | static inline av_const__attribute__((const)) int sign_extend(int val, unsigned bits) | |||
| 136 | { | |||
| 137 | unsigned shift = 8 * sizeof(int) - bits; | |||
| 138 | union { unsigned u; int s; } v = { (unsigned) val << shift }; | |||
| ||||
| 139 | return v.s >> shift; | |||
| 140 | } | |||
| 141 | #endif | |||
| 142 | ||||
| 143 | #ifndef sign_extend64 | |||
| 144 | static inline av_const__attribute__((const)) int64_t sign_extend64(int64_t val, unsigned bits) | |||
| 145 | { | |||
| 146 | unsigned shift = 8 * sizeof(int64_t) - bits; | |||
| 147 | union { uint64_t u; int64_t s; } v = { (uint64_t) val << shift }; | |||
| 148 | return v.s >> shift; | |||
| 149 | } | |||
| 150 | #endif | |||
| 151 | ||||
| 152 | #ifndef zero_extend | |||
| 153 | static inline av_const__attribute__((const)) unsigned zero_extend(unsigned val, unsigned bits) | |||
| 154 | { | |||
| 155 | return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits); | |||
| 156 | } | |||
| 157 | #endif | |||
| 158 | ||||
| 159 | #ifndef COPY3_IF_LT | |||
| 160 | #define COPY3_IF_LT(x, y, a, b, c, d)__asm__ volatile( "cmpl %0, %3 \n\t" "cmovl %3, %0 \n\t" "cmovl %4, %1 \n\t" "cmovl %5, %2 \n\t" : "+&r" (x), "+&r" (a), "+r" (c) : "r" (y), "r" (b), "r" (d));\ | |||
| 161 | if ((y) < (x)) {\ | |||
| 162 | (x) = (y);\ | |||
| 163 | (a) = (b);\ | |||
| 164 | (c) = (d);\ | |||
| 165 | } | |||
| 166 | #endif | |||
| 167 | ||||
| 168 | #ifndef MASK_ABS | |||
| 169 | #define MASK_ABS(mask, level)__asm__ ("cdq \n\t" "xorl %1, %0 \n\t" "subl %1, %0 \n\t" : "+a"(level), "=&d"(mask) ) do { \ | |||
| 170 | mask = level >> 31; \ | |||
| 171 | level = (level ^ mask) - mask; \ | |||
| 172 | } while (0) | |||
| 173 | #endif | |||
| 174 | ||||
| 175 | #ifndef NEG_SSR32NEG_SSR32 | |||
| 176 | # define NEG_SSR32NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s))) | |||
| 177 | #endif | |||
| 178 | ||||
| 179 | #ifndef NEG_USR32NEG_USR32 | |||
| 180 | # define NEG_USR32NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) | |||
| 181 | #endif | |||
| 182 | ||||
| 183 | #if HAVE_BIGENDIAN0 | |||
| 184 | # ifndef PACK_2U8 | |||
| 185 | # define PACK_2U8(a,b)(((b) << 8) | (a)) (((a) << 8) | (b)) | |||
| 186 | # endif | |||
| 187 | # ifndef PACK_4U8 | |||
| 188 | # define PACK_4U8(a,b,c,d)(((d) << 24) | ((c) << 16) | ((b) << 8) | ( a)) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) | |||
| 189 | # endif | |||
| 190 | # ifndef PACK_2U16 | |||
| 191 | # define PACK_2U16(a,b)(((b) << 16) | (a)) (((a) << 16) | (b)) | |||
| 192 | # endif | |||
| 193 | #else | |||
| 194 | # ifndef PACK_2U8 | |||
| 195 | # define PACK_2U8(a,b)(((b) << 8) | (a)) (((b) << 8) | (a)) | |||
| 196 | # endif | |||
| 197 | # ifndef PACK_4U2 | |||
| 198 | # define PACK_4U8(a,b,c,d)(((d) << 24) | ((c) << 16) | ((b) << 8) | ( a)) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a)) | |||
| 199 | # endif | |||
| 200 | # ifndef PACK_2U16 | |||
| 201 | # define PACK_2U16(a,b)(((b) << 16) | (a)) (((b) << 16) | (a)) | |||
| 202 | # endif | |||
| 203 | #endif | |||
| 204 | ||||
| 205 | #ifndef PACK_2S8 | |||
| 206 | # define PACK_2S8(a,b)((((b)&255) << 8) | ((a)&255)) PACK_2U8((a)&255, (b)&255)((((b)&255) << 8) | ((a)&255)) | |||
| 207 | #endif | |||
| 208 | #ifndef PACK_4S8 | |||
| 209 | # define PACK_4S8(a,b,c,d)((((d)&255) << 24) | (((c)&255) << 16) | ( ((b)&255) << 8) | ((a)&255)) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)((((d)&255) << 24) | (((c)&255) << 16) | ( ((b)&255) << 8) | ((a)&255)) | |||
| 210 | #endif | |||
| 211 | #ifndef PACK_2S16 | |||
| 212 | # define PACK_2S16(a,b)((((b)&0xffff) << 16) | ((a)&0xffff)) PACK_2U16((a)&0xffff, (b)&0xffff)((((b)&0xffff) << 16) | ((a)&0xffff)) | |||
| 213 | #endif | |||
| 214 | ||||
| 215 | #ifndef FASTDIV | |||
| 216 | # define FASTDIV(a,b)((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32)) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32)) | |||
| 217 | #endif /* FASTDIV */ | |||
| 218 | ||||
| 219 | #ifndef ff_sqrtff_sqrt | |||
| 220 | #define ff_sqrtff_sqrt ff_sqrtff_sqrt | |||
| 221 | static inline av_const__attribute__((const)) unsigned int ff_sqrtff_sqrt(unsigned int a) | |||
| 222 | { | |||
| 223 | unsigned int b; | |||
| 224 | ||||
| 225 | if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4; | |||
| 226 | else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2; | |||
| 227 | #if !CONFIG_SMALL0 | |||
| 228 | else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1; | |||
| 229 | else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ; | |||
| 230 | #endif | |||
| 231 | else { | |||
| 232 | int s = av_log2_16bit(a >> 16)(31 - __builtin_clz((a >> 16)|1)) >> 1; | |||
| 233 | unsigned int c = a >> (s + 2); | |||
| 234 | b = ff_sqrt_tab[c >> (s + 8)]; | |||
| 235 | b = FASTDIV(c,b)((uint32_t)((((uint64_t)c) * ff_inverse[b]) >> 32)) + (b << s); | |||
| 236 | } | |||
| 237 | ||||
| 238 | return b - (a < b * b); | |||
| 239 | } | |||
| 240 | #endif | |||
| 241 | ||||
| 242 | static inline av_const__attribute__((const)) float ff_sqrf(float a) | |||
| 243 | { | |||
| 244 | return a*a; | |||
| 245 | } | |||
| 246 | ||||
| 247 | static inline int8_t ff_u8_to_s8(uint8_t a) | |||
| 248 | { | |||
| 249 | union { | |||
| 250 | uint8_t u8; | |||
| 251 | int8_t s8; | |||
| 252 | } b; | |||
| 253 | b.u8 = a; | |||
| 254 | return b.s8; | |||
| 255 | } | |||
| 256 | ||||
| 257 | #endif /* AVCODEC_MATHOPS_H */ |