]> git.sesse.net Git - ffmpeg/blob - libavcodec/mlz.c
avformat/mov: aax: pass proper AVClass to av_log()
[ffmpeg] / libavcodec / mlz.c
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 "mlz.h"
22
23 av_cold void ff_mlz_init_dict(void* context, MLZ *mlz) {
24     mlz->dict = av_malloc_array(TABLE_SIZE, sizeof(*mlz->dict));
25
26     mlz->flush_code            = FLUSH_CODE;
27     mlz->current_dic_index_max = DIC_INDEX_INIT;
28     mlz->dic_code_bit          = CODE_BIT_INIT;
29     mlz->bump_code             = (DIC_INDEX_INIT - 1);
30     mlz->next_code             = FIRST_CODE;
31     mlz->freeze_flag           = 0;
32     mlz->context               = context;
33 }
34
35 av_cold void ff_mlz_flush_dict(MLZ *mlz) {
36     MLZDict *dict = mlz->dict;
37     int i;
38     for ( i = 0; i < TABLE_SIZE; i++ ) {
39         dict[i].string_code = CODE_UNSET;
40         dict[i].parent_code = CODE_UNSET;
41         dict[i].match_len = 0;
42     }
43     mlz->current_dic_index_max = DIC_INDEX_INIT;
44     mlz->dic_code_bit          = CODE_BIT_INIT;  // DicCodeBitInit;
45     mlz->bump_code             = mlz->current_dic_index_max - 1;
46     mlz->next_code             = FIRST_CODE;
47     mlz->freeze_flag           = 0;
48 }
49
50 static void set_new_entry_dict(MLZDict* dict, int string_code, int parent_code, int char_code) {
51     dict[string_code].parent_code = parent_code;
52     dict[string_code].string_code = string_code;
53     dict[string_code].char_code   = char_code;
54     if (parent_code < FIRST_CODE) {
55         dict[string_code].match_len = 2;
56     } else {
57         dict[string_code].match_len = (dict[parent_code].match_len) + 1;
58     }
59 }
60
61 static int decode_string(MLZ* mlz, unsigned char *buff, int string_code, int *first_char_code, unsigned long bufsize) {
62     MLZDict* dict = mlz->dict;
63     unsigned long count, offset;
64     int current_code, parent_code, tmp_code;
65
66     count            = 0;
67     current_code     = string_code;
68     *first_char_code = CODE_UNSET;
69
70     while (count < bufsize) {
71         switch (current_code) {
72         case CODE_UNSET:
73             return count;
74             break;
75         default:
76             if (current_code < FIRST_CODE) {
77                 *first_char_code = current_code;
78                 buff[0] = current_code;
79                 count++;
80                 return count;
81             } else {
82                 offset  = dict[current_code].match_len - 1;
83                 tmp_code = dict[current_code].char_code;
84                 buff[offset] = tmp_code;
85                 count++;
86             }
87             current_code = dict[current_code].parent_code;
88             if ((current_code < 0) || (current_code > (DIC_INDEX_MAX - 1))) {
89                 av_log(mlz->context, AV_LOG_ERROR, "MLZ dic index error.\n");
90                 return count;
91             }
92             if (current_code > FIRST_CODE) {
93                 parent_code = dict[current_code].parent_code;
94                 offset = (dict[current_code].match_len) - 1;
95                 if (parent_code < 0 || parent_code > DIC_INDEX_MAX-1) {
96                     av_log(mlz->context, AV_LOG_ERROR, "MLZ dic index error.\n");
97                     return count;
98                 }
99                 if (( offset > (DIC_INDEX_MAX - 1))) {
100                     av_log(mlz->context, AV_LOG_ERROR, "MLZ dic offset error.\n");
101                     return count;
102                 }
103             }
104             break;
105         }
106     }
107     return count;
108 }
109
110 static int input_code(GetBitContext* gb, int len) {
111     int tmp_code = 0;
112     int i;
113     for (i = 0; i < len; ++i) {
114         tmp_code |= get_bits1(gb) << i;
115     }
116     return tmp_code;
117 }
118
119 int ff_mlz_decompression(MLZ* mlz, GetBitContext* gb, int size, unsigned char *buff) {
120     MLZDict *dict = mlz->dict;
121     unsigned long output_chars;
122     int string_code, last_string_code, char_code;
123
124     string_code = 0;
125     char_code   = -1;
126     last_string_code = -1;
127     output_chars = 0;
128
129     while (output_chars < size) {
130         string_code = input_code(gb, mlz->dic_code_bit);
131         switch (string_code) {
132             case FLUSH_CODE:
133             case MAX_CODE:
134                 ff_mlz_flush_dict(mlz);
135                 char_code = -1;
136                 last_string_code = -1;
137                 break;
138             case FREEZE_CODE:
139                 mlz->freeze_flag = 1;
140                 break;
141             default:
142                 if (string_code > mlz->current_dic_index_max) {
143                     av_log(mlz->context, AV_LOG_ERROR, "String code %d exceeds maximum value of %d.\n", string_code, mlz->current_dic_index_max);
144                     return output_chars;
145                 }
146                 if (string_code == (int) mlz->bump_code) {
147                     ++mlz->dic_code_bit;
148                     mlz->current_dic_index_max *= 2;
149                     mlz->bump_code = mlz->current_dic_index_max - 1;
150                 } else {
151                     if (string_code >= mlz->next_code) {
152                         output_chars += decode_string(mlz, &buff[output_chars], last_string_code, &char_code, size - output_chars);
153                         output_chars += decode_string(mlz, &buff[output_chars], char_code, &char_code, size - output_chars);
154                         set_new_entry_dict(dict, mlz->next_code, last_string_code, char_code);
155                         mlz->next_code++;
156                     } else {
157                         output_chars += decode_string(mlz, &buff[output_chars], string_code, &char_code, size - output_chars);
158                         if (output_chars <= size && !mlz->freeze_flag) {
159                             if (last_string_code != -1) {
160                                 set_new_entry_dict(dict, mlz->next_code, last_string_code, char_code);
161                                 mlz->next_code++;
162                             }
163                         } else {
164                             break;
165                         }
166                     }
167                     last_string_code = string_code;
168                 }
169                 break;
170         }
171     }
172     return output_chars;
173 }