/src/aom/aom_dsp/bitwriter.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016, Alliance for Open Media. All rights reserved. |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 2 Clause License and |
5 | | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
6 | | * was not distributed with this source code in the LICENSE file, you can |
7 | | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
8 | | * Media Patent License 1.0 was not distributed with this source code in the |
9 | | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
10 | | */ |
11 | | |
12 | | #include <string.h> |
13 | | #include "aom_dsp/bitwriter.h" |
14 | | |
15 | 0 | void aom_start_encode(aom_writer *w, uint8_t *source) { |
16 | | // TODO: bug 42302568 - During the transition period, size=0 means the |
17 | | // buffer size is unknown. |
18 | 0 | aom_start_encode_with_size(w, source, /*size=*/0); |
19 | 0 | } |
20 | | |
21 | 0 | void aom_start_encode_with_size(aom_writer *w, uint8_t *source, size_t size) { |
22 | 0 | w->buffer = source; |
23 | 0 | w->size = size; |
24 | 0 | w->pos = 0; |
25 | 0 | od_ec_enc_init(&w->ec, 62025); |
26 | 0 | } |
27 | | |
28 | 0 | int aom_stop_encode(aom_writer *w) { |
29 | 0 | int nb_bits; |
30 | 0 | uint32_t bytes; |
31 | 0 | unsigned char *data; |
32 | 0 | data = od_ec_enc_done(&w->ec, &bytes); |
33 | | // TODO: bug 42302568 - Remove "w->size != 0 &&" after all aom_start_encode() |
34 | | // calls have been converted to aom_start_encode_with_size(). |
35 | 0 | if (!data || (w->size != 0 && bytes > w->size)) { |
36 | 0 | od_ec_enc_clear(&w->ec); |
37 | 0 | return -1; |
38 | 0 | } |
39 | 0 | nb_bits = od_ec_enc_tell(&w->ec); |
40 | 0 | memcpy(w->buffer, data, bytes); |
41 | 0 | w->pos = bytes; |
42 | 0 | od_ec_enc_clear(&w->ec); |
43 | 0 | return nb_bits; |
44 | 0 | } |
45 | | |
46 | 0 | int aom_tell_size(aom_writer *w) { |
47 | 0 | const int nb_bits = od_ec_enc_tell(&w->ec); |
48 | 0 | return nb_bits; |
49 | 0 | } |