xref: /aosp_15_r20/external/libvpx/vpx_dsp/bitwriter.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <assert.h>
12 #include <limits.h>
13 
14 #include "./bitwriter.h"
15 
16 #if CONFIG_BITSTREAM_DEBUG
17 #include "vpx_util/vpx_debug_util.h"
18 #endif
19 
vpx_start_encode(vpx_writer * br,uint8_t * source,size_t size)20 void vpx_start_encode(vpx_writer *br, uint8_t *source, size_t size) {
21   br->lowvalue = 0;
22   br->range = 255;
23   br->count = -24;
24   br->error = 0;
25   br->pos = 0;
26   // Make sure it is safe to cast br->pos to int in vpx_write().
27   if (size > INT_MAX) size = INT_MAX;
28   br->size = (unsigned int)size;
29   br->buffer = source;
30   vpx_write_bit(br, 0);
31 }
32 
vpx_stop_encode(vpx_writer * br)33 int vpx_stop_encode(vpx_writer *br) {
34   int i;
35 
36 #if CONFIG_BITSTREAM_DEBUG
37   bitstream_queue_set_skip_write(1);
38 #endif
39   for (i = 0; i < 32; i++) vpx_write_bit(br, 0);
40 
41   // Ensure there's no ambigous collision with any index marker bytes
42   if (!br->error && (br->buffer[br->pos - 1] & 0xe0) == 0xc0) {
43     if (br->pos < br->size) {
44       br->buffer[br->pos++] = 0;
45     } else {
46       br->error = 1;
47     }
48   }
49 
50 #if CONFIG_BITSTREAM_DEBUG
51   bitstream_queue_set_skip_write(0);
52 #endif
53 
54   return br->error ? -1 : 0;
55 }
56