FFmpeg
lzf.c
Go to the documentation of this file.
1 /*
2  * lzf decompression algorithm
3  * Copyright (c) 2015 Luca Barbato
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * lzf decompression
25  *
26  * LZF is a fast compression/decompression algorithm that takes very little
27  * code space and working memory, ideal for real-time and block compression.
28  *
29  * https://en.wikibooks.org/wiki/Data_Compression/Dictionary_compression#LZF
30  */
31 
32 #include "libavutil/mem.h"
33 
34 #include "bytestream.h"
35 #include "lzf.h"
36 
37 #define LZF_LITERAL_MAX (1 << 5)
38 #define LZF_LONG_BACKREF 7 + 2
39 
40 
41 static inline int lzf_realloc(uint8_t **buf, size_t *size, int addition, unsigned *allocated_size)
42 {
43  void *ptr = av_fast_realloc(*buf, allocated_size, *size + addition);
44 
45  if (!ptr) {
46  av_freep(buf); //probably not needed
47  return AVERROR(ENOMEM);
48  }
49  *buf = ptr;
50  *size += addition;
51 
52  return 0;
53 }
54 
55 int ff_lzf_uncompress(GetByteContext *gb, uint8_t **buf, size_t *size, unsigned *allocated_size)
56 {
57  int ret = 0;
58  uint8_t *p = *buf;
59  int64_t len = 0;
60 
61  while (bytestream2_get_bytes_left(gb) > 2) {
62  uint8_t s = bytestream2_get_byte(gb);
63 
64  if (s < LZF_LITERAL_MAX) {
65  s++;
66  if (s > *size - len) {
67  ret = lzf_realloc(buf, size, s, allocated_size);
68  if (ret < 0)
69  return ret;
70  p = *buf + len;
71  }
72 
73  int s2 = bytestream2_get_buffer(gb, p, s);
74  if (s2 != s)
75  return AVERROR_INVALIDDATA;
76 
77  p += s;
78  len += s;
79  } else {
80  int l = 2 + (s >> 5);
81  int off = ((s & 0x1f) << 8) + 1;
82 
83  if (l == LZF_LONG_BACKREF)
84  l += bytestream2_get_byte(gb);
85 
86  off += bytestream2_get_byte(gb);
87 
88  if (off > len)
89  return AVERROR_INVALIDDATA;
90 
91  if (l > *size - len) {
92  ret = lzf_realloc(buf, size, l, allocated_size);
93  if (ret < 0)
94  return ret;
95  p = *buf + len;
96  }
97 
98  av_memcpy_backptr(p, off, l);
99 
100  p += l;
101  len += l;
102  }
103  }
104 
105  *size = len;
106 
107  return 0;
108 }
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
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
GetByteContext
Definition: bytestream.h:33
int64_t
long long int64_t
Definition: coverity.c:34
av_memcpy_backptr
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:447
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
s
#define s(width, name)
Definition: cbs_vp9.c:198
lzf_realloc
static int lzf_realloc(uint8_t **buf, size_t *size, int addition, unsigned *allocated_size)
Definition: lzf.c:41
LZF_LITERAL_MAX
#define LZF_LITERAL_MAX
Definition: lzf.c:37
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
ff_lzf_uncompress
int ff_lzf_uncompress(GetByteContext *gb, uint8_t **buf, size_t *size, unsigned *allocated_size)
Definition: lzf.c:55
size
int size
Definition: twinvq_data.h:10344
LZF_LONG_BACKREF
#define LZF_LONG_BACKREF
Definition: lzf.c:38
len
int len
Definition: vorbis_enc_data.h:426
ret
ret
Definition: filter_design.txt:187
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
bytestream.h
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
lzf.h