FFmpeg
golomb.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdint.h>
22 #include <stdio.h>
23 
24 #include "libavutil/internal.h"
25 #include "libavutil/mem.h"
26 
27 #include "libavcodec/get_bits.h"
28 #include "libavcodec/golomb.h"
29 #include "libavcodec/put_bits.h"
30 
31 #define COUNT 8191
32 #define SIZE (COUNT * 4)
33 
34 int main(void)
35 {
36  int i, ret = 0;
37  uint8_t *temp;
38  PutBitContext pb;
39  GetBitContext gb;
40 
41  temp = av_malloc(SIZE);
42  if (!temp)
43  return 2;
44 
45  init_put_bits(&pb, temp, SIZE);
46  for (i = 0; i < COUNT; i++)
47  set_ue_golomb(&pb, i);
48  flush_put_bits(&pb);
49 
50  init_get_bits(&gb, temp, 8 * SIZE);
51  for (i = 0; i < COUNT; i++) {
52  int j, s = show_bits(&gb, 25);
53 
54  j = get_ue_golomb(&gb);
55  if (j != i) {
56  fprintf(stderr, "get_ue_golomb: expected %d, got %d. bits: %7x\n",
57  i, j, s);
58  ret = 1;
59  }
60  }
61 
62 #define EXTEND(i) ((i) << 3 | (i) & 7)
63  init_put_bits(&pb, temp, SIZE);
64  for (i = 0; i < COUNT; i++)
65  set_ue_golomb(&pb, EXTEND(i));
66  flush_put_bits(&pb);
67 
68  init_get_bits(&gb, temp, 8 * SIZE);
69  for (i = 0; i < COUNT; i++) {
70  int j, s = show_bits_long(&gb, 32);
71 
72  j = get_ue_golomb_long(&gb);
73  if (j != EXTEND(i)) {
74  fprintf(stderr, "get_ue_golomb_long: expected %d, got %d. "
75  "bits: %8x\n", EXTEND(i), j, s);
76  ret = 1;
77  }
78  }
79 
80 #define EXTEND_L(i) ((i) << 4 | (i) & 15)
81  init_put_bits(&pb, temp, SIZE);
82  for (i = 0; i < COUNT; i++)
84  flush_put_bits(&pb);
85 
86  init_get_bits(&gb, temp, 8 * SIZE);
87  for (i = 0; i < COUNT; i++) {
88  int j, s = show_bits_long(&gb, 32);
89 
90  j = get_ue_golomb_long(&gb);
91  if (j != EXTEND_L(i)) {
92  fprintf(stderr, "get_ue_golomb_long: expected %d, got %d. "
93  "bits: %8x\n", EXTEND_L(i), j, s);
94  ret = 1;
95  }
96  }
97 
98  init_put_bits(&pb, temp, SIZE);
99  for (i = 0; i < COUNT; i++)
100  set_se_golomb(&pb, i - COUNT / 2);
101  flush_put_bits(&pb);
102 
103  init_get_bits(&gb, temp, 8 * SIZE);
104  for (i = 0; i < COUNT; i++) {
105  int j, s = show_bits(&gb, 25);
106 
107  j = get_se_golomb(&gb);
108  if (j != i - COUNT / 2) {
109  fprintf(stderr, "get_se_golomb: expected %d, got %d. bits: %7x\n",
110  i - COUNT / 2, j, s);
111  ret = 1;
112  }
113  }
114 
115  av_free(temp);
116 
117  return ret;
118 }
show_bits_long
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:602
SIZE
#define SIZE
Definition: golomb.c:32
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
get_ue_golomb
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
Definition: golomb.h:55
set_ue_golomb_long
static void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
write unsigned exp golomb code.
Definition: golomb.h:638
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
golomb.h
exp golomb vlc stuff
EXTEND
#define EXTEND(i)
GetBitContext
Definition: get_bits.h:61
s
#define s(width, name)
Definition: cbs_vp9.c:257
get_bits.h
get_se_golomb
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:241
PutBitContext
Definition: put_bits.h:44
COUNT
#define COUNT
Definition: golomb.c:31
set_ue_golomb
static void set_ue_golomb(PutBitContext *pb, int i)
write unsigned exp golomb code.
Definition: golomb.h:622
i
int i
Definition: input.c:407
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
internal.h
uint8_t
uint8_t
Definition: audio_convert.c:194
ret
ret
Definition: filter_design.txt:187
main
int main(void)
Definition: golomb.c:34
temp
else temp
Definition: vf_mcdeint.c:259
mem.h
get_ue_golomb_long
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:106
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
EXTEND_L
#define EXTEND_L(i)
put_bits.h
set_se_golomb
static void set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.
Definition: golomb.h:667