Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 1 | /* |
| 2 | * Common bit i/o utils |
Diego Biurrun | 406792e | 2009-01-19 15:46:40 | [diff] [blame] | 3 | * Copyright (c) 2000, 2001 Fabrice Bellard |
Michael Niedermayer | 8f2ab83 | 2004-01-10 16:04:55 | [diff] [blame] | 4 | * Copyright (c) 2002-2004 Michael Niedermayer <[email protected]> |
Loren Merritt | 3224079 | 2010-03-29 02:50:23 | [diff] [blame] | 5 | * Copyright (c) 2010 Loren Merritt |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 6 | * |
Diego Biurrun | 7b94177 | 2007-07-05 10:37:29 | [diff] [blame] | 7 | * alternative bitstream reader & writer by Michael Niedermayer <[email protected]> |
| 8 | * |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 9 | * This file is part of FFmpeg. |
| 10 | * |
| 11 | * FFmpeg is free software; you can redistribute it and/or |
Fabrice Bellard | ff4ec49 | 2002-05-25 22:45:33 | [diff] [blame] | 12 | * modify it under the terms of the GNU Lesser General Public |
| 13 | * License as published by the Free Software Foundation; either |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 14 | * version 2.1 of the License, or (at your option) any later version. |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 15 | * |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 16 | * FFmpeg is distributed in the hope that it will be useful, |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Fabrice Bellard | ff4ec49 | 2002-05-25 22:45:33 | [diff] [blame] | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Lesser General Public License for more details. |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 20 | * |
Fabrice Bellard | ff4ec49 | 2002-05-25 22:45:33 | [diff] [blame] | 21 | * You should have received a copy of the GNU Lesser General Public |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 22 | * License along with FFmpeg; if not, write to the Free Software |
Diego Biurrun | 5509bff | 2006-01-12 22:43:26 | [diff] [blame] | 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Fabrice Bellard | de6d9b6 | 2001-07-22 14:18:56 | [diff] [blame] | 24 | */ |
Michael Niedermayer | 983e324 | 2003-03-06 11:32:04 | [diff] [blame] | 25 | |
| 26 | /** |
Diego Biurrun | ba87f08 | 2010-04-20 14:45:34 | [diff] [blame] | 27 | * @file |
Michael Niedermayer | caa336b | 2004-12-29 17:50:25 | [diff] [blame] | 28 | * bitstream api. |
Michael Niedermayer | 983e324 | 2003-03-06 11:32:04 | [diff] [blame] | 29 | */ |
Diego Biurrun | 115329f | 2005-12-17 18:14:38 | [diff] [blame] | 30 | |
Andreas Rheinhardt | 25c8507 | 2021-06-14 16:03:55 | [diff] [blame] | 31 | #include <stdint.h> |
Andreas Rheinhardt | 25c8507 | 2021-06-14 16:03:55 | [diff] [blame] | 32 | #include <string.h> |
| 33 | |
| 34 | #include "config.h" |
Michael Niedermayer | bb1e0e8 | 2012-07-23 20:17:52 | [diff] [blame] | 35 | #include "libavutil/avassert.h" |
Andreas Rheinhardt | 25c8507 | 2021-06-14 16:03:55 | [diff] [blame] | 36 | #include "libavutil/intreadwrite.h" |
Stefano Sabatini | b275500 | 2009-04-12 08:35:26 | [diff] [blame] | 37 | #include "put_bits.h" |
Michael Niedermayer | c81f034 | 2003-01-27 20:39:29 | [diff] [blame] | 38 | |
Anton Khirnov | 944ba30 | 2020-10-26 12:41:39 | [diff] [blame] | 39 | void ff_put_string(PutBitContext *pb, const char *string, int terminate_string) |
Michael Niedermayer | 9717dad | 2002-05-03 23:13:24 | [diff] [blame] | 40 | { |
Luca Barbato | f776899 | 2013-06-15 08:19:51 | [diff] [blame] | 41 | while (*string) { |
Stefano Sabatini | 587edd6 | 2009-11-29 23:01:29 | [diff] [blame] | 42 | put_bits(pb, 8, *string); |
| 43 | string++; |
Michael Niedermayer | 9717dad | 2002-05-03 23:13:24 | [diff] [blame] | 44 | } |
Luca Barbato | f776899 | 2013-06-15 08:19:51 | [diff] [blame] | 45 | if (terminate_string) |
Stefano Sabatini | 587edd6 | 2009-11-29 23:01:29 | [diff] [blame] | 46 | put_bits(pb, 8, 0); |
Michael Niedermayer | 9717dad | 2002-05-03 23:13:24 | [diff] [blame] | 47 | } |
| 48 | |
Anton Khirnov | dc10994 | 2020-10-26 12:41:39 | [diff] [blame] | 49 | void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length) |
Aurelien Jacobs | 98f7b56 | 2007-07-06 14:13:25 | [diff] [blame] | 50 | { |
Luca Barbato | f776899 | 2013-06-15 08:19:51 | [diff] [blame] | 51 | int words = length >> 4; |
| 52 | int bits = length & 15; |
Aurelien Jacobs | 98f7b56 | 2007-07-06 14:13:25 | [diff] [blame] | 53 | int i; |
| 54 | |
Luca Barbato | f776899 | 2013-06-15 08:19:51 | [diff] [blame] | 55 | if (length == 0) |
| 56 | return; |
Aurelien Jacobs | 98f7b56 | 2007-07-06 14:13:25 | [diff] [blame] | 57 | |
Michael Niedermayer | 291ad5c | 2015-05-25 01:48:45 | [diff] [blame] | 58 | av_assert0(length <= put_bits_left(pb)); |
| 59 | |
Luca Barbato | f776899 | 2013-06-15 08:19:51 | [diff] [blame] | 60 | if (CONFIG_SMALL || words < 16 || put_bits_count(pb) & 7) { |
| 61 | for (i = 0; i < words; i++) |
| 62 | put_bits(pb, 16, AV_RB16(src + 2 * i)); |
| 63 | } else { |
| 64 | for (i = 0; put_bits_count(pb) & 31; i++) |
Aurelien Jacobs | 98f7b56 | 2007-07-06 14:13:25 | [diff] [blame] | 65 | put_bits(pb, 8, src[i]); |
| 66 | flush_put_bits(pb); |
Luca Barbato | f776899 | 2013-06-15 08:19:51 | [diff] [blame] | 67 | memcpy(put_bits_ptr(pb), src + i, 2 * words - i); |
| 68 | skip_put_bytes(pb, 2 * words - i); |
Aurelien Jacobs | 98f7b56 | 2007-07-06 14:13:25 | [diff] [blame] | 69 | } |
| 70 | |
Luca Barbato | f776899 | 2013-06-15 08:19:51 | [diff] [blame] | 71 | put_bits(pb, bits, AV_RB16(src + 2 * words) >> (16 - bits)); |
Aurelien Jacobs | 98f7b56 | 2007-07-06 14:13:25 | [diff] [blame] | 72 | } |