FFmpeg
mlz.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Umair Khan <omerjerk@gmail.com>
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 "libavutil/mem.h"
22 #include "mlz.h"
23 
25 {
26  mlz->dict = av_mallocz(TABLE_SIZE * sizeof(*mlz->dict));
27  if (!mlz->dict)
28  return AVERROR(ENOMEM);
29 
30  mlz->flush_code = FLUSH_CODE;
33  mlz->bump_code = (DIC_INDEX_INIT - 1);
34  mlz->next_code = FIRST_CODE;
35  mlz->freeze_flag = 0;
36  mlz->context = context;
37 
38  return 0;
39 }
40 
42  MLZDict *dict = mlz->dict;
43  int i;
44  for ( i = 0; i < TABLE_SIZE; i++ ) {
45  dict[i].string_code = CODE_UNSET;
46  dict[i].parent_code = CODE_UNSET;
47  dict[i].match_len = 0;
48  }
50  mlz->dic_code_bit = CODE_BIT_INIT; // DicCodeBitInit;
51  mlz->bump_code = mlz->current_dic_index_max - 1;
52  mlz->next_code = FIRST_CODE;
53  mlz->freeze_flag = 0;
54 }
55 
56 static void set_new_entry_dict(MLZDict* dict, int string_code, int parent_code, int char_code) {
57  dict[string_code].parent_code = parent_code;
58  dict[string_code].string_code = string_code;
59  dict[string_code].char_code = char_code;
60  if (parent_code < FIRST_CODE) {
61  dict[string_code].match_len = 2;
62  } else {
63  dict[string_code].match_len = (dict[parent_code].match_len) + 1;
64  }
65 }
66 
67 static int decode_string(MLZ* mlz, unsigned char *buff, int string_code, int *first_char_code, unsigned long bufsize) {
68  MLZDict* dict = mlz->dict;
69  unsigned long count, offset;
70  int current_code, parent_code, tmp_code;
71 
72  count = 0;
73  current_code = string_code;
74  *first_char_code = CODE_UNSET;
75 
76  while (count < bufsize) {
77  switch (current_code) {
78  case CODE_UNSET:
79  return count;
80  break;
81  default:
82  if (current_code < FIRST_CODE) {
83  *first_char_code = current_code;
84  buff[0] = current_code;
85  count++;
86  return count;
87  } else {
88  offset = dict[current_code].match_len - 1;
89  tmp_code = dict[current_code].char_code;
90  if (offset >= bufsize) {
91  av_log(mlz->context, AV_LOG_ERROR, "MLZ offset error.\n");
92  return count;
93  }
94  buff[offset] = tmp_code;
95  count++;
96  }
97  current_code = dict[current_code].parent_code;
98  if ((current_code < 0) || (current_code > (DIC_INDEX_MAX - 1))) {
99  av_log(mlz->context, AV_LOG_ERROR, "MLZ dic index error.\n");
100  return count;
101  }
102  if (current_code > FIRST_CODE) {
103  parent_code = dict[current_code].parent_code;
104  offset = (dict[current_code].match_len) - 1;
105  if (parent_code < 0 || parent_code > DIC_INDEX_MAX-1) {
106  av_log(mlz->context, AV_LOG_ERROR, "MLZ dic index error.\n");
107  return count;
108  }
109  if (( offset > (DIC_INDEX_MAX - 1))) {
110  av_log(mlz->context, AV_LOG_ERROR, "MLZ dic offset error.\n");
111  return count;
112  }
113  }
114  break;
115  }
116  }
117  return count;
118 }
119 
120 static int input_code(GetBitContext* gb, int len) {
121  int tmp_code = 0;
122  int i;
123  for (i = 0; i < len; ++i) {
124  tmp_code |= get_bits1(gb) << i;
125  }
126  return tmp_code;
127 }
128 
129 int ff_mlz_decompression(MLZ* mlz, GetBitContext* gb, int size, unsigned char *buff) {
130  MLZDict *dict = mlz->dict;
131  unsigned long output_chars;
132  int string_code, last_string_code, char_code;
133 
134  string_code = 0;
135  char_code = -1;
136  last_string_code = -1;
137  output_chars = 0;
138 
139  while (output_chars < size) {
140  string_code = input_code(gb, mlz->dic_code_bit);
141  switch (string_code) {
142  case FLUSH_CODE:
143  case MAX_CODE:
144  ff_mlz_flush_dict(mlz);
145  char_code = -1;
146  last_string_code = -1;
147  break;
148  case FREEZE_CODE:
149  mlz->freeze_flag = 1;
150  break;
151  default:
152  if (string_code > mlz->current_dic_index_max) {
153  av_log(mlz->context, AV_LOG_ERROR, "String code %d exceeds maximum value of %d.\n", string_code, mlz->current_dic_index_max);
154  return output_chars;
155  }
156  if (string_code == (int) mlz->bump_code) {
157  ++mlz->dic_code_bit;
158  mlz->current_dic_index_max *= 2;
159  mlz->bump_code = mlz->current_dic_index_max - 1;
160  } else {
161  if (string_code >= mlz->next_code) {
162  int ret = decode_string(mlz, &buff[output_chars], last_string_code, &char_code, size - output_chars);
163  if (ret < 0 || ret > size - output_chars) {
164  av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
165  return output_chars;
166  }
167  output_chars += ret;
168  ret = decode_string(mlz, &buff[output_chars], char_code, &char_code, size - output_chars);
169  if (ret < 0 || ret > size - output_chars) {
170  av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
171  return output_chars;
172  }
173  output_chars += ret;
174  set_new_entry_dict(dict, mlz->next_code, last_string_code, char_code);
175  if (mlz->next_code >= TABLE_SIZE - 1) {
176  av_log(mlz->context, AV_LOG_ERROR, "Too many MLZ codes\n");
177  return output_chars;
178  }
179  mlz->next_code++;
180  } else {
181  int ret = decode_string(mlz, &buff[output_chars], string_code, &char_code, size - output_chars);
182  if (ret < 0 || ret > size - output_chars) {
183  av_log(mlz->context, AV_LOG_ERROR, "output chars overflow\n");
184  return output_chars;
185  }
186  output_chars += ret;
187  if (output_chars <= size && !mlz->freeze_flag) {
188  if (last_string_code != -1) {
189  set_new_entry_dict(dict, mlz->next_code, last_string_code, char_code);
190  if (mlz->next_code >= TABLE_SIZE - 1) {
191  av_log(mlz->context, AV_LOG_ERROR, "Too many MLZ codes\n");
192  return output_chars;
193  }
194  mlz->next_code++;
195  }
196  } else {
197  break;
198  }
199  }
200  last_string_code = string_code;
201  }
202  break;
203  }
204  }
205  return output_chars;
206 }
MLZDict::string_code
int string_code
Definition: mlz.h:39
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_mlz_flush_dict
av_cold void ff_mlz_flush_dict(MLZ *mlz)
Flush the dictionary.
Definition: mlz.c:41
set_new_entry_dict
static void set_new_entry_dict(MLZDict *dict, int string_code, int parent_code, int char_code)
Definition: mlz.c:56
FREEZE_CODE
#define FREEZE_CODE
Definition: mlz.h:31
DIC_INDEX_INIT
#define DIC_INDEX_INIT
Definition: mlz.h:28
DIC_INDEX_MAX
#define DIC_INDEX_MAX
Definition: mlz.h:29
MLZ
MLZ data strucure.
Definition: mlz.h:47
GetBitContext
Definition: get_bits.h:108
MLZDict::parent_code
int parent_code
Definition: mlz.h:40
MLZDict::char_code
int char_code
Definition: mlz.h:41
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
MLZ::freeze_flag
int freeze_flag
Definition: mlz.h:53
MLZ::context
void * context
Definition: mlz.h:55
MLZ::current_dic_index_max
int current_dic_index_max
Definition: mlz.h:49
decode_string
static int decode_string(MLZ *mlz, unsigned char *buff, int string_code, int *first_char_code, unsigned long bufsize)
Definition: mlz.c:67
mlz.h
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
MLZ::next_code
int next_code
Definition: mlz.h:52
MLZDict
Dictionary structure for mlz decompression.
Definition: mlz.h:38
FIRST_CODE
#define FIRST_CODE
Definition: mlz.h:32
size
int size
Definition: twinvq_data.h:10344
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
MLZDict::match_len
int match_len
Definition: mlz.h:42
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
CODE_BIT_INIT
#define CODE_BIT_INIT
Definition: mlz.h:27
FLUSH_CODE
#define FLUSH_CODE
Definition: mlz.h:30
MLZ::dic_code_bit
int dic_code_bit
Definition: mlz.h:48
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
len
int len
Definition: vorbis_enc_data.h:426
TABLE_SIZE
#define TABLE_SIZE
Definition: mlz.h:34
ret
ret
Definition: filter_design.txt:187
MLZ::dict
MLZDict * dict
Definition: mlz.h:54
ff_mlz_decompression
int ff_mlz_decompression(MLZ *mlz, GetBitContext *gb, int size, unsigned char *buff)
Run mlz decompression on the next size bits and the output will be stored in buff.
Definition: mlz.c:129
MLZ::bump_code
unsigned int bump_code
Definition: mlz.h:50
mem.h
CODE_UNSET
#define CODE_UNSET
Definition: mlz.h:26
ff_mlz_init_dict
av_cold int ff_mlz_init_dict(void *context, MLZ *mlz)
Initialize the dictionary.
Definition: mlz.c:24
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MAX_CODE
#define MAX_CODE
Definition: mlz.h:33
MLZ::flush_code
unsigned int flush_code
Definition: mlz.h:51
input_code
static int input_code(GetBitContext *gb, int len)
Definition: mlz.c:120