blob: 36065750559e7073f3db49364f63f143d5e1d18a [file] [log] [blame]
Fabrice Bellardde6d9b62001-07-22 14:18:561/*
2 * Common bit i/o utils
Diego Biurrun406792e2009-01-19 15:46:403 * Copyright (c) 2000, 2001 Fabrice Bellard
Michael Niedermayer8f2ab832004-01-10 16:04:554 * Copyright (c) 2002-2004 Michael Niedermayer <[email protected]>
Loren Merritt32240792010-03-29 02:50:235 * Copyright (c) 2010 Loren Merritt
Fabrice Bellardde6d9b62001-07-22 14:18:566 *
Diego Biurrun7b941772007-07-05 10:37:297 * alternative bitstream reader & writer by Michael Niedermayer <[email protected]>
8 *
Diego Biurrunb78e7192006-10-07 15:30:469 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
Fabrice Bellardff4ec492002-05-25 22:45:3312 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
Diego Biurrunb78e7192006-10-07 15:30:4614 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellardde6d9b62001-07-22 14:18:5615 *
Diego Biurrunb78e7192006-10-07 15:30:4616 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellardde6d9b62001-07-22 14:18:5617 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellardff4ec492002-05-25 22:45:3318 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
Fabrice Bellardde6d9b62001-07-22 14:18:5620 *
Fabrice Bellardff4ec492002-05-25 22:45:3321 * You should have received a copy of the GNU Lesser General Public
Diego Biurrunb78e7192006-10-07 15:30:4622 * License along with FFmpeg; if not, write to the Free Software
Diego Biurrun5509bff2006-01-12 22:43:2623 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellardde6d9b62001-07-22 14:18:5624 */
Michael Niedermayer983e3242003-03-06 11:32:0425
26/**
Diego Biurrunba87f082010-04-20 14:45:3427 * @file
Michael Niedermayercaa336b2004-12-29 17:50:2528 * bitstream api.
Michael Niedermayer983e3242003-03-06 11:32:0429 */
Diego Biurrun115329f2005-12-17 18:14:3830
Andreas Rheinhardt25c85072021-06-14 16:03:5531#include <stdint.h>
Andreas Rheinhardt25c85072021-06-14 16:03:5532#include <string.h>
33
34#include "config.h"
Michael Niedermayerbb1e0e82012-07-23 20:17:5235#include "libavutil/avassert.h"
Andreas Rheinhardt25c85072021-06-14 16:03:5536#include "libavutil/intreadwrite.h"
Stefano Sabatinib2755002009-04-12 08:35:2637#include "put_bits.h"
Michael Niedermayerc81f0342003-01-27 20:39:2938
Anton Khirnov944ba302020-10-26 12:41:3939void ff_put_string(PutBitContext *pb, const char *string, int terminate_string)
Michael Niedermayer9717dad2002-05-03 23:13:2440{
Luca Barbatof7768992013-06-15 08:19:5141 while (*string) {
Stefano Sabatini587edd62009-11-29 23:01:2942 put_bits(pb, 8, *string);
43 string++;
Michael Niedermayer9717dad2002-05-03 23:13:2444 }
Luca Barbatof7768992013-06-15 08:19:5145 if (terminate_string)
Stefano Sabatini587edd62009-11-29 23:01:2946 put_bits(pb, 8, 0);
Michael Niedermayer9717dad2002-05-03 23:13:2447}
48
Anton Khirnovdc109942020-10-26 12:41:3949void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Aurelien Jacobs98f7b562007-07-06 14:13:2550{
Luca Barbatof7768992013-06-15 08:19:5151 int words = length >> 4;
52 int bits = length & 15;
Aurelien Jacobs98f7b562007-07-06 14:13:2553 int i;
54
Luca Barbatof7768992013-06-15 08:19:5155 if (length == 0)
56 return;
Aurelien Jacobs98f7b562007-07-06 14:13:2557
Michael Niedermayer291ad5c2015-05-25 01:48:4558 av_assert0(length <= put_bits_left(pb));
59
Luca Barbatof7768992013-06-15 08:19:5160 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 Jacobs98f7b562007-07-06 14:13:2565 put_bits(pb, 8, src[i]);
66 flush_put_bits(pb);
Luca Barbatof7768992013-06-15 08:19:5167 memcpy(put_bits_ptr(pb), src + i, 2 * words - i);
68 skip_put_bytes(pb, 2 * words - i);
Aurelien Jacobs98f7b562007-07-06 14:13:2569 }
70
Luca Barbatof7768992013-06-15 08:19:5171 put_bits(pb, bits, AV_RB16(src + 2 * words) >> (16 - bits));
Aurelien Jacobs98f7b562007-07-06 14:13:2572}