Libav
golomb-test.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdint.h>
20 #include <stdio.h>
21 
22 #include "libavutil/mem.h"
23 
24 #include "get_bits.h"
25 #include "golomb.h"
26 #include "put_bits.h"
27 
28 #define COUNT 8191
29 #define SIZE (COUNT * 4)
30 
31 int main(void)
32 {
33  int i, ret = 0;
34  uint8_t *temp;
35  PutBitContext pb;
36  GetBitContext gb;
37 
38  temp = av_malloc(SIZE);
39  if (!temp)
40  return 2;
41 
42  init_put_bits(&pb, temp, SIZE);
43  for (i = 0; i < COUNT; i++)
44  set_ue_golomb(&pb, i);
45  flush_put_bits(&pb);
46 
47  init_get_bits(&gb, temp, 8 * SIZE);
48  for (i = 0; i < COUNT; i++) {
49  int j, s = show_bits(&gb, 25);
50 
51  j = get_ue_golomb(&gb);
52  if (j != i) {
53  fprintf(stderr, "get_ue_golomb: expected %d, got %d. bits: %7x\n",
54  i, j, s);
55  ret = 1;
56  }
57  }
58 
59 #define EXTEND(i) (i << 3 | i & 7)
60  init_put_bits(&pb, temp, SIZE);
61  for (i = 0; i < COUNT; i++)
62  set_ue_golomb(&pb, EXTEND(i));
63  flush_put_bits(&pb);
64 
65  init_get_bits(&gb, temp, 8 * SIZE);
66  for (i = 0; i < COUNT; i++) {
67  int j, s = show_bits_long(&gb, 32);
68 
69  j = get_ue_golomb_long(&gb);
70  if (j != EXTEND(i)) {
71  fprintf(stderr, "get_ue_golomb_long: expected %d, got %d. "
72  "bits: %8x\n", EXTEND(i), j, s);
73  ret = 1;
74  }
75  }
76 
77  init_put_bits(&pb, temp, SIZE);
78  for (i = 0; i < COUNT; i++)
79  set_se_golomb(&pb, i - COUNT / 2);
80  flush_put_bits(&pb);
81 
82  init_get_bits(&gb, temp, 8 * SIZE);
83  for (i = 0; i < COUNT; i++) {
84  int j, s = show_bits(&gb, 25);
85 
86  j = get_se_golomb(&gb);
87  if (j != i - COUNT / 2) {
88  fprintf(stderr, "get_se_golomb: expected %d, got %d. bits: %7x\n",
89  i - COUNT / 2, j, s);
90  ret = 1;
91  }
92  }
93 
94  av_free(temp);
95 
96  return ret;
97 }
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:348
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:179
static void set_ue_golomb(PutBitContext *pb, int i)
write unsigned exp golomb code.
Definition: golomb.h:468
int main(void)
Definition: golomb-test.c:31
uint8_t
#define SIZE
Definition: golomb-test.c:29
bitstream reader API header.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
#define COUNT
Definition: golomb-test.c:28
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:254
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:81
static void set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.
Definition: golomb.h:503
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
exp golomb vlc stuff
#define EXTEND(i)
bitstream writer API