[FFmpeg-devel] [PATCH] WBMP (Wireless Application Protocol Bitmap) image format

2022-07-26 Thread Peter Ross
---

example: 
https://example-files.online-convert.com/raster%20image/wbmp/example.wbmp

 Changelog |  1 +
 doc/general_contents.texi |  2 +
 libavcodec/Makefile   |  2 +
 libavcodec/allcodecs.c|  2 +
 libavcodec/codec_desc.c   |  7 +++
 libavcodec/codec_id.h |  1 +
 libavcodec/wbmpdec.c  | 92 +++
 libavcodec/wbmpenc.c  | 91 ++
 libavformat/img2.c|  1 +
 libavformat/img2enc.c |  2 +-
 tests/fate/lavf-image.mak |  1 +
 tests/ref/lavf/wbmp   |  3 ++
 12 files changed, 204 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/wbmpdec.c
 create mode 100644 libavcodec/wbmpenc.c
 create mode 100644 tests/ref/lavf/wbmp

diff --git a/Changelog b/Changelog
index 92780c9953..83bc36297a 100644
--- a/Changelog
+++ b/Changelog
@@ -7,6 +7,7 @@ version :
 - ffmpeg -shortest_buf_duration option
 - ffmpeg now requires threading to be built
 - ffmpeg now runs every muxer in a separate thread
+- WBMP (Wireless Application Protocol Bitmap) image format
 
 
 version 5.1:
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index f25c784d3b..86ec6d606b 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -801,6 +801,8 @@ following image formats are supported:
 @tab Targa (.TGA) image format
 @item VBN  @tab X @tab X
 @tab Vizrt Binary Image format
+@item WBMP @tab X @tab X
+@tab Wireless Application Protocol Bitmap image format
 @item WebP @tab E @tab X
 @tab WebP image format, encoding supported through external library libwebp
 @item XBM  @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ef2318438b..727db20345 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -762,6 +762,8 @@ OBJS-$(CONFIG_VP9_V4L2M2M_DECODER) += v4l2_m2m_dec.o
 OBJS-$(CONFIG_VQA_DECODER) += vqavideo.o
 OBJS-$(CONFIG_WAVPACK_DECODER) += wavpack.o wavpackdata.o dsd.o
 OBJS-$(CONFIG_WAVPACK_ENCODER) += wavpackdata.o wavpackenc.o
+OBJS-$(CONFIG_WBMP_DECODER)+= wbmpdec.o
+OBJS-$(CONFIG_WBMP_ENCODER)+= wbmpenc.o
 OBJS-$(CONFIG_WCMV_DECODER)+= wcmv.o
 OBJS-$(CONFIG_WEBP_DECODER)+= webp.o
 OBJS-$(CONFIG_WEBVTT_DECODER)  += webvttdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 31d2c5979c..c94e2d5966 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -378,6 +378,8 @@ extern const FFCodec ff_vp9_decoder;
 extern const FFCodec ff_vp9_rkmpp_decoder;
 extern const FFCodec ff_vp9_v4l2m2m_decoder;
 extern const FFCodec ff_vqa_decoder;
+extern const FFCodec ff_wbmp_decoder;
+extern const FFCodec ff_wbmp_encoder;
 extern const FFCodec ff_webp_decoder;
 extern const FFCodec ff_wcmv_decoder;
 extern const FFCodec ff_wrapped_avframe_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index fdcf8198fe..c1a177c22d 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1900,6 +1900,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .long_name = NULL_IF_CONFIG_SMALL("HDR (Radiance RGBE format) image"),
 .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
 },
+{
+.id= AV_CODEC_ID_WBMP,
+.type  = AVMEDIA_TYPE_VIDEO,
+.name  = "wbmp",
+.long_name = NULL_IF_CONFIG_SMALL("WBMP (Wireless Application Protocol 
Bitmap) image"),
+.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+},
 
 /* various PCM "codecs" */
 {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index 27bf68ec1d..386a00a7ef 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -313,6 +313,7 @@ enum AVCodecID {
 AV_CODEC_ID_QOI,
 AV_CODEC_ID_PHM,
 AV_CODEC_ID_RADIANCE_HDR,
+AV_CODEC_ID_WBMP,
 
 /* various PCM "codecs" */
 AV_CODEC_ID_FIRST_AUDIO = 0x1, ///< A dummy id pointing at the 
start of audio codecs
diff --git a/libavcodec/wbmpdec.c b/libavcodec/wbmpdec.c
new file mode 100644
index 00..cb673459f1
--- /dev/null
+++ b/libavcodec/wbmpdec.c
@@ -0,0 +1,92 @@
+/*
+ * WBMP (Wireless Application Protocol Bitmap) image
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fi

Re: [FFmpeg-devel] [PATCH v3 0/6] ICC profile support in avcodec

2022-07-26 Thread Niklas Haas
On Sat, 23 Jul 2022 12:31:05 +0200 Niklas Haas  wrote:
> On Tue, 19 Jul 2022 14:26:02 +0200 Niklas Haas  wrote:
> > Changes in v3:
> > - switch from `int icc_profiles` to a new AV_CODEC_FLAG2 to avoid ABI
> >   break
> > - rebased onto master and fixed merge conflicts
> > 
> > Changes in v2:
> > - remove unnecessary import
> > - fixed assignment-instead-of-equality
> > - fixed #ifdef -> #if
> 
> Ping. Any objections to merging as-is?

Will apply in 48h if there are no further comments.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH 1/3] tools/target_dec_fuzzer: set some avctx->export_side_data flags

2022-07-26 Thread James Almer

On 7/19/2022 10:44 AM, James Almer wrote:

Signed-off-by: James Almer 
---
  tools/target_dec_fuzzer.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/target_dec_fuzzer.c b/tools/target_dec_fuzzer.c
index 1587045e02..7d71dd39e3 100644
--- a/tools/target_dec_fuzzer.c
+++ b/tools/target_dec_fuzzer.c
@@ -336,7 +336,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t 
size) {
  if ((flags & 0x10) && c->p.id != AV_CODEC_ID_H264)
  ctx->flags2 |= AV_CODEC_FLAG2_FAST;
  if (flags & 0x80)
-ctx->flags2 |= AV_CODEC_FLAG2_EXPORT_MVS;
+ctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
  
  if (flags & 0x40)

  av_force_cpu_flags(0);
@@ -404,6 +404,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t 
size) {
  ctx->debug |= FF_DEBUG_QP;
  if (flags64 &4)
  ctx->debug |= FF_DEBUG_MB_TYPE;
+if (flags64 & 8)
+ctx->export_side_data |= AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS;
  
  if (extradata_size < size) {

  ctx->extradata = av_mallocz(extradata_size + 
AV_INPUT_BUFFER_PADDING_SIZE);


Will apply the patchset.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH] avdevice/v4l2: fix leak of timefilter

2022-07-26 Thread Zhao Zhili
From: Zhao Zhili 

Fixes ticket #9844.
---
I don't have a camera for test. So I'm not sure if it's the only case
of memleak in ticket #9844.

 libavdevice/v4l2.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 5340382d57..be422d7c8c 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -1011,6 +1011,7 @@ static int v4l2_read_close(AVFormatContext *ctx)
 
 mmap_close(s);
 
+ff_timefilter_destroy(s->timefilter);
 v4l2_close(s->fd);
 return 0;
 }
-- 
2.34.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH 1/2] avformat/mov: Rework the AVIF parser to handle multiple items

2022-07-26 Thread James Zern
On Fri, Jul 22, 2022 at 11:21 AM Vignesh Venkatasubramanian
 wrote:
>
> On Wed, Jul 13, 2022 at 9:12 AM Vignesh Venkatasubramanian
>  wrote:
> >
> > On Mon, Jul 11, 2022 at 3:25 PM James Zern
> >  wrote:
> > >
> > > On Thu, Jun 30, 2022 at 2:04 PM Vignesh Venkatasubramanian
> > >  wrote:
> > > >
> > > > Stores the item ids of all the items found in the file and
> > > > processes the primary item at the end of the meta box. This patch
> > > > does not change any behavior. It sets up the code for parsing
> > > > alpha channel (and possibly images with 'grid') in follow up
> > > > patches.
> > > >
> > > > Signed-off-by: Vignesh Venkatasubramanian 
> > > > ---
> > > >  libavformat/isom.h |   4 ++
> > > >  libavformat/mov.c  | 148 -
> > > >  2 files changed, 97 insertions(+), 55 deletions(-)
> > > >
> > > > [...]
> > >
> > > @@ -4692,9 +4755,25 @@ static int mov_read_meta(MOVContext *c,
> > > AVIOContext *pb, MOVAtom atom)
> > >  tag = avio_rl32(pb);
> > >  atom.size -= 4;
> > >  if (tag == MKTAG('h','d','l','r')) {
> > > +int ret;
> > >  avio_seek(pb, -8, SEEK_CUR);
> > >  atom.size += 8;
> > > -return mov_read_default(c, pb, atom);
> > > +ret = mov_read_default(c, pb, atom);
> > > +if (ret < 0)
> > >
> > > In some other cases these two lines are combined, if ((ret = ...
> > >
> >
> > Done.
> >
> > > +return ret;
> > > +if (c->is_still_picture_avif) {
> > > +int ret;
> > > +// Add a stream for the YUV planes (primary item).
> > > +ret = avif_add_stream(c, c->primary_item_id);
> > > +if (ret)
> > >
> > > This could be updated too and use '< 0' to match other code.
> > >
> >
> > Done.
> >
> > > +return ret;
> > > +// For still AVIF images, the meta box contains all the
> > > +// necessary information that would generally be
> > > provided by the
> > > +// moov box. So simply mark that we have found the moov 
> > > box so
> > > +// that parsing can continue.
> > > +c->found_moov = 1;
> > > +}
> > > +return ret;
> > >  }
> > > ___
> > > ffmpeg-devel mailing list
> > > ffmpeg-devel@ffmpeg.org
> > > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> > >
> > > To unsubscribe, visit link above, or email
> > > ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
> >
> >
> >
> > --
> > Vignesh
>
> Any further comments on this one?
>
> Please note that i have abandoned the second patch in this list. But
> this one is still up for merging.
>

This looks like it needs to be rebased. I'll take a look after that.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 0/2] 32bps FLAC patches

2022-07-26 Thread Martijn van Beurden
Recently libFLAC gained the ability (not in any released version yet
though) to create FLAC files containing 32-bit int PCM samples. To
keep complexity reasonable, the choice was made to limit residuals
to 32-bit integers, which the encoder must make sure of. In case
the encoder cannot find any predictor of which the residuals fit
this limit, it must default to using a verbatim subframe. Tests have
shown that this does not happen often (<0.1% of subframes on a
music corpus of various styles). See also discussion here:
https://github.com/ietf-wg-cellar/flac-specification/pull/148

These two patches implement decoding and encoding following this
format. The decoding patch has been send in for review earlier,
but didn't get any feedback yet, so I'm sending it in again,
unchanged, this time with a patch for encoding too.

Martijn van Beurden (2):
  libavcodec/flacdec: Implement decoding of 32 bit-per-sample PCM
  libavcodec/flacenc: Implement encoding of 32 bit-per-sample PCM

 libavcodec/flac.c   |   4 +-
 libavcodec/flacdec.c| 248 ++--
 libavcodec/flacenc.c| 499 
 libavcodec/get_bits.h   |  12 +
 libavcodec/mathops.h|   9 +
 libavcodec/put_bits.h   |   7 +
 libavcodec/put_golomb.h |  19 +-
 7 files changed, 668 insertions(+), 130 deletions(-)

-- 
2.30.2

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 1/2] libavcodec/flacdec: Implement decoding of 32 bit-per-sample PCM

2022-07-26 Thread Martijn van Beurden
Add decoding of FLAC files coding for 32 bit-per-sample PCM to libavcodec.
---
 libavcodec/flac.c |   4 +-
 libavcodec/flacdec.c  | 248 ++
 libavcodec/get_bits.h |  12 ++
 libavcodec/mathops.h  |   9 ++
 4 files changed, 250 insertions(+), 23 deletions(-)

diff --git a/libavcodec/flac.c b/libavcodec/flac.c
index dd68830622..f326d8fa5c 100644
--- a/libavcodec/flac.c
+++ b/libavcodec/flac.c
@@ -27,7 +27,7 @@
 #include "flac.h"
 #include "flacdata.h"
 
-static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
+static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 32 };
 
 static const AVChannelLayout flac_channel_layouts[8] = {
 AV_CHANNEL_LAYOUT_MONO,
@@ -81,7 +81,7 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, 
GetBitContext *gb,
 
 /* bits per sample */
 bps_code = get_bits(gb, 3);
-if (bps_code == 3 || bps_code == 7) {
+if (bps_code == 3) {
 av_log(avctx, AV_LOG_ERROR + log_level_offset,
"invalid sample size code (%d)\n",
bps_code);
diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c
index 17f1821c50..403cfa419e 100644
--- a/libavcodec/flacdec.c
+++ b/libavcodec/flacdec.c
@@ -63,6 +63,9 @@ typedef struct FLACContext {
 int32_t *decoded[FLAC_MAX_CHANNELS];///< decoded samples
 uint8_t *decoded_buffer;
 unsigned int decoded_buffer_size;
+int64_t *decoded_33bps;  ///< decoded samples for a 33 bps 
subframe
+uint8_t *decoded_buffer_33bps;
+unsigned int decoded_buffer_size_33bps;
 int buggy_lpc;  ///< use workaround for old lavc 
encoded files
 
 FLACDSPContext dsp;
@@ -154,6 +157,24 @@ static int allocate_buffers(FLACContext *s)
  s->flac_stream_info.channels,
  s->flac_stream_info.max_blocksize,
  AV_SAMPLE_FMT_S32P, 0);
+if (ret >= 0 && s->flac_stream_info.bps == 32 && 
s->flac_stream_info.channels == 2) {
+buf_size = av_samples_get_buffer_size(NULL, 1,
+  
s->flac_stream_info.max_blocksize,
+  AV_SAMPLE_FMT_S64P, 0);
+if (buf_size < 0)
+return buf_size;
+
+av_fast_malloc(&s->decoded_buffer_33bps, 
&s->decoded_buffer_size_33bps, buf_size);
+if (!s->decoded_buffer)
+return AVERROR(ENOMEM);
+
+ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL,
+ s->decoded_buffer_33bps,
+ 1,
+ s->flac_stream_info.max_blocksize,
+ AV_SAMPLE_FMT_S64P, 0);
+
+}
 return ret < 0 ? ret : 0;
 }
 
@@ -331,6 +352,94 @@ static int decode_subframe_fixed(FLACContext *s, int32_t 
*decoded,
 return 0;
 }
 
+static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded,
+  int pred_order, int bps)
+{
+const int blocksize = s->blocksize;
+int i;
+int ret;
+
+/* warm up samples */
+for (i = 0; i < pred_order; i++) {
+decoded[i] = get_sbits_long(&s->gb, bps);
+}
+
+if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
+return ret;
+
+switch (pred_order) {
+case 0:
+break;
+case 1:
+for (i = pred_order; i < blocksize; i++)
+decoded[i] += decoded[i-1];
+break;
+case 2:
+for (i = pred_order; i < blocksize; i++)
+decoded[i] = (int64_t)decoded[i] + 2*(int64_t)decoded[i-1] - 
(int64_t)decoded[i-2];
+break;
+case 3:
+for (i = pred_order; i < blocksize; i++)
+decoded[i] = (int64_t)decoded[i] + 3*(int64_t)decoded[i-1] - 
3*(int64_t)decoded[i-2] + (int64_t)decoded[i-3];
+break;
+case 4:
+for (i = pred_order; i < blocksize; i++)
+decoded[i] = (int64_t)decoded[i] + 4*(int64_t)decoded[i-1] - 
6*(int64_t)decoded[i-2] + 4*(int64_t)decoded[i-3] - (int64_t)decoded[i-4];
+break;
+default:
+av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
+return AVERROR_INVALIDDATA;
+}
+
+return 0;
+}
+
+
+static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded,
+   int32_t *residual, int pred_order)
+{
+const int blocksize = s->blocksize;
+int i;
+int ret;
+
+/* warm up samples */
+for (i = 0; i < pred_order; i++) {
+decoded[i] = get_sbits64(&s->gb, 33);
+av_log(s->avctx, AV_LOG_DEBUG, "warm-up %d = %" PRId64 "\n", i, 
decoded[i]);
+}
+
+if ((ret = decode_residuals(s, residual, pred_order)) < 0)
+return ret;
+
+switch (pred_order) {
+case 0:
+for (i = pred_order; i < blocksize; i++)
+decoded[i] = residual[i];
+break;
+

[FFmpeg-devel] [PATCH 2/2] libavcodec/flacenc: Implement encoding of 32 bit-per-sample PCM

2022-07-26 Thread Martijn van Beurden
Add encoding of 32 bit-per-sample PCM to FLAC files to libavcodec.
Coding to this format is at this point considered experimental and
-strict -2 is needed to get ffmpeg to encode such files.
---
 libavcodec/flacenc.c| 499 
 libavcodec/put_bits.h   |   7 +
 libavcodec/put_golomb.h |  19 +-
 3 files changed, 418 insertions(+), 107 deletions(-)

diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index 9350e42dbc..4ed0a3961a 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -96,6 +96,7 @@ typedef struct FlacSubframe {
 
 typedef struct FlacFrame {
 FlacSubframe subframes[FLAC_MAX_CHANNELS];
+int64_t samples_33bps[FLAC_MAX_BLOCKSIZE];
 int blocksize;
 int bs_code[2];
 uint8_t crc8;
@@ -255,10 +256,23 @@ static av_cold int flac_encode_init(AVCodecContext *avctx)
 s->bps_code= 4;
 break;
 case AV_SAMPLE_FMT_S32:
-if (avctx->bits_per_raw_sample != 24)
-av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
-avctx->bits_per_raw_sample = 24;
-s->bps_code= 6;
+if (avctx->bits_per_raw_sample <= 24) {
+if (avctx->bits_per_raw_sample < 24)
+av_log(avctx, AV_LOG_WARNING, "encoding as 24 
bits-per-sample\n");
+avctx->bits_per_raw_sample = 24;
+s->bps_code= 6;
+} else if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
+av_log(avctx, AV_LOG_WARNING,
+   "encoding as 24 bits-per-sample, more is considered "
+   "experimental. Add -strict %d if you want to encode "
+   "more than 24 bits-per-sample\n",
+   FF_COMPLIANCE_EXPERIMENTAL);
+avctx->bits_per_raw_sample = 24;
+s->bps_code= 6;
+} else {
+avctx->bits_per_raw_sample = 32;
+s->bps_code = 7;
+}
 break;
 }
 
@@ -507,8 +521,7 @@ static uint64_t rice_count_exact(const int32_t *res, int n, 
int k)
 uint64_t count = 0;
 
 for (i = 0; i < n; i++) {
-int32_t v = -2 * res[i] - 1;
-v ^= v >> 31;
+unsigned v = ((unsigned)(res[i]) << 1) ^ (res[i] >> 31);
 count += (v >> k) + 1 + k;
 }
 return count;
@@ -687,8 +700,8 @@ static uint64_t calc_rice_params(RiceContext *rc,
 
 tmp_rc.coding_mode = rc->coding_mode;
 
-for (i = 0; i < n; i++)
-udata[i] = (2 * data[i]) ^ (data[i] >> 31);
+for (i = pred_order; i < n; i++)
+udata[i] = ((unsigned)(data[i]) << 1) ^ (data[i] >> 31);
 
 calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
 
@@ -786,6 +799,180 @@ static void encode_residual_fixed(int32_t *res, const 
int32_t *smp, int n,
 }
 
 
+static int encode_residual_fixed_with_residual_limit(int32_t *res, const 
int32_t *smp,
+  int n, int order)
+{
+/* This function checks for every residual whether it can be
+ * contained in  INT32_MAX)
+return 1;
+res[i] = res64;
+}
+} else if (order == 2) {
+for (i = order; i < n; i++) {
+res64 = (int64_t)smp[i] - 2*(int64_t)smp[i-1] + smp[i-2];
+if (res64 <= INT32_MIN || res64 > INT32_MAX)
+return 1;
+res[i] = res64;
+}
+} else if (order == 3) {
+for (i = order; i < n; i++) {
+res64 = (int64_t)smp[i] - 3*(int64_t)smp[i-1] + 
3*(int64_t)smp[i-2] - smp[i-3];
+if (res64 <= INT32_MIN || res64 > INT32_MAX)
+return 1;
+res[i] = res64;
+}
+} else {
+for (i = order; i < n; i++) {
+res64 = (int64_t)smp[i] - 4*(int64_t)smp[i-1] + 
6*(int64_t)smp[i-2] - 4*(int64_t)smp[i-3] + smp[i-4];
+if (res64 <= INT32_MIN || res64 > INT32_MAX)
+return 1;
+res[i] = res64;
+}
+}
+return 0;
+}
+
+
+static int encode_residual_fixed_with_residual_limit_33bps(int32_t *res, const 
int64_t *smp,
+   int n, int order)
+{
+/* This function checks for every residual whether it can be
+ * contained in  INT32_MAX)
+return 1;
+res[i] = smp[i];
+}
+} else if (order == 1) {
+for (i = order; i < n; i++) {
+res64 = smp[i] - smp[i-1];
+if (res64 <= INT32_MIN || res64 > INT32_MAX)
+return 1;
+res[i] = res64;
+}
+} else if (order == 2) {
+for (i = order; i < n; i++) {
+res64 = smp[i] - 2*smp[i-1] + smp[i-2];
+if (res64 <= INT32_MIN || res64 > INT32_MAX)
+return 1;
+res[i] = res64;
+}
+} else if (order == 3) {
+for (i = order; i < n; i++) {
+res64 = smp[i] - 3*smp[i-1] + 3*smp[i-2] - smp[i-3