[FFmpeg-devel] [PATCH] avcodec: add a native BBC Dirac VC-2 HQ encoder

Rostislav Pehlivanov atomnuker at gmail.com
Thu Jan 14 20:00:48 CET 2016


This commit adds a new encoder capable of creating BBC Dirac VC-2 HQ
profile files.

Dirac is a wavelet based codec created by the BBC a little more than 10
years ago. Since then, wavelets have mostly gone out of style as they
did not provide adequate encoding gains at lower bitrates. Dirac was a
fully featured video codec equipped with perceptual masking, support for
most popular pixel formats, interlacing, overlapped-block motion
compensation, and other features. It found new life after being stripped
of various features and standardized as the VC-2 codec by the SMPTE with
an extra profile, the HQ profile that this encoder supports, added.

The HQ profile was based off of the Low-Delay profile previously
existing in Dirac. The profile forbids DC prediction and arithmetic
coding to focus on high performance and low delay at higher bitrates.
The standard bitrates for this profile vary but generally 1:4
compression is expected (~525 Mbps vs the 2200 Mbps for uncompressed
1080p50). The codec only supports I-frames, hence the high bitrates.

The structure of this encoder is simple: do a DWT transform on the
entire image, split it into multiple slices (specified by the user) and
encode them in parallel. All of the slices are of the same size, making
rate control and threading very trivial. Although only in C, this encoder
is capable of 30 frames per second on an 4 core 8 threads Ivy Bridge.
A lookup table is used to encode most of the coefficients.

No code was used from the GSoC encoder from 2007 except for the 2
transform functions in diracenc_transforms.c. All other code was written
from scratch.

This encoder outperforms any other encoders in quality, usability and in
features. Other existing implementations do not support 4 level
transforms or 64x64 blocks (slices), which greatly increase compression.

As previously said, the codec is meant for broadcasting, hence support
for non-broadcasting image widths, heights, bit depths, aspect ratios,
etc. are limited by the "level". Although this codec supports a few
chroma subsamplings (420, 422, 444), signalling those is generally
outside the specifications of the level used (3) and the reference
decoder will outright refuse to read any image with such a flag
signalled (it only supports 1920x1080 yuv422p10). However, most
implementations will happily read files with alternate dimensions,
framerates and formats signalled.

Therefore, in order to encode files other than 1080p50 yuv422p10le, you
need to provide an "-strict -2" argument to the command line. The FFmpeg
decoder will happily read any files made with non-standard parameters,
dimensions and subsamplings, and so will other implementations. IMO this
should be "-strict -1", but I'll leave that up for discussion.

There are still plenty of stuff to implement, for instance 5 more
wavelet transforms are still in the specs and supported by the decoder.

Signed-off-by: Rostislav Pehlivanov <atomnuker at gmail.com>
---
 libavcodec/Makefile              |   1 +
 libavcodec/allcodecs.c           |   2 +-
 libavcodec/diracenc.c            | 906 +++++++++++++++++++++++++++++++++++++++
 libavcodec/diracenc_transforms.c | 243 +++++++++++
 libavcodec/diracenc_transforms.h |  53 +++
 5 files changed, 1204 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/diracenc.c
 create mode 100644 libavcodec/diracenc_transforms.c
 create mode 100644 libavcodec/diracenc_transforms.h

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index b9ffdb9..b8753de 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -228,6 +228,7 @@ OBJS-$(CONFIG_DCA_ENCODER)             += dcaenc.o dca.o dcadata.o
 OBJS-$(CONFIG_DDS_DECODER)             += dds.o
 OBJS-$(CONFIG_DIRAC_DECODER)           += diracdec.o dirac.o diracdsp.o \
                                           dirac_arith.o mpeg12data.o dirac_dwt.o
+OBJS-$(CONFIG_DIRAC_ENCODER)           += diracenc.o diracenc_transforms.o
 OBJS-$(CONFIG_DFA_DECODER)             += dfa.o
 OBJS-$(CONFIG_DNXHD_DECODER)           += dnxhddec.o dnxhddata.o
 OBJS-$(CONFIG_DNXHD_ENCODER)           += dnxhdenc.o dnxhddata.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 2128546..68fb24c 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -156,7 +156,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER(CYUV,              cyuv);
     REGISTER_DECODER(DDS,               dds);
     REGISTER_DECODER(DFA,               dfa);
-    REGISTER_DECODER(DIRAC,             dirac);
+    REGISTER_ENCDEC (DIRAC,             dirac);
     REGISTER_ENCDEC (DNXHD,             dnxhd);
     REGISTER_ENCDEC (DPX,               dpx);
     REGISTER_DECODER(DSICINVIDEO,       dsicinvideo);
diff --git a/libavcodec/diracenc.c b/libavcodec/diracenc.c
new file mode 100644
index 0000000..910cf2f
--- /dev/null
+++ b/libavcodec/diracenc.c
@@ -0,0 +1,906 @@
+/*
+ * Copyright (C) 2016 Open Broadcast Systemd Ltd.
+ * Author    (C) 2016 Rostislav Pehlivanov <atomnuker at gmail.com>
+ *
+ * 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, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
+#include "dirac.h"
+#include "put_bits.h"
+#include "internal.h"
+#include "golomb.h"
+
+#include "diracenc_transforms.h"
+
+#define COEF_LUT_TAB 1024
+
+typedef struct SubBand {
+    int stride;
+    int width;
+    int height;
+    dwtcoef *buf;
+} SubBand;
+
+typedef struct Plane {
+    int width;
+    int height;
+    int dwt_width;
+    int dwt_height;
+    int coef_stride;
+    dwtcoef *coef_buf;
+    SubBand band[4][4];
+} Plane;
+
+typedef struct SliceArgs {
+    void *ctx;
+    PutBitContext pb;
+    int x;
+    int y;
+} SliceArgs;
+
+typedef struct DiracEncContext {
+    AVCodecContext *avctx;
+    PutBitContext pb;
+    DiracTransforms t;
+    Plane plane[3];
+
+    /* For conversion from unsigned pixel values to signed */
+    int diff_offset;
+
+    uint32_t *coef_lut_val;
+    uint8_t *coef_lut_len;
+
+    int num_x; /* #slices horizontally */
+    int num_y; /* #slices vertically */
+    int prefix_bytes;
+    int size_scaler;
+    int chroma_x_shift;
+    int chroma_y_shift;
+
+    SliceArgs *slice_args;
+
+    /* Rate control stuff */
+    int64_t slice_max_bits;
+    int64_t frame_max_bits;
+    int q_ceil;
+    int q_start;
+
+    /* Options */
+    double tolerance;
+    int wavelet_idx;
+    int wavelet_depth;
+    int strict_compliance;
+    int slice_height;
+    int slice_width;
+
+    /* Parse code state */
+    uint64_t next_parse_offset;
+    uint64_t last_parse_offset;
+    enum DiracParseCodes last_parse_code;
+
+    /* Quantization matrix */
+    uint8_t custom_quant_matrix;
+    uint8_t quant[4][4];
+} DiracEncContext;
+
+/* [wavelet_idx][level][orientation] */
+static const uint8_t default_qmat[][4][4] = {
+    { { 5,  3,  3,  0}, { 0,  4,  4,  1}, { 0,  5,  5,  2}, { 0,  6,  6,  3} },
+    { { 4,  2,  2,  0}, { 0,  4,  4,  2}, { 0,  5,  5,  3}, { 0,  7,  7,  5} },
+    { { 5,  3,  3,  0}, { 0,  4,  4,  1}, { 0,  5,  5,  2}, { 0,  6,  6,  3} },
+    { { 8,  4,  4,  0}, { 0,  4,  4,  0}, { 0,  4,  4,  0}, { 0,  4,  4,  0} },
+    { { 8,  4,  4,  0}, { 0,  4,  4,  0}, { 0,  4,  4,  0}, { 0,  4,  4,  0} },
+    { { 0,  4,  4,  8}, { 0,  8,  8, 12}, { 0, 13, 13, 17}, { 0, 17, 17, 21} },
+    { { 3,  1,  1,  0}, { 0,  4,  4,  2}, { 0,  6,  6,  5}, { 0,  9,  9,  7} },
+};
+
+static const int32_t qscale_tab[116] = {
+         4,         5,         6,         7,         8,        10,        11,        13,
+        16,        19,        23,        27,        32,        38,        45,        54,
+        64,        76,        91,       108,       128,       152,       181,       215,
+       256,       304,       362,       431,       512,       609,       724,       861,
+      1024,      1218,      1448,      1722,      2048,      2435,      2896,      3444,
+      4096,      4871,      5793,      6889,      8192,      9742,     11585,     13777,
+     16384,     19484,     23170,     27554,     32768,     38968,     46341,     55109,
+     65536,     77936,     92682,    110218,    131072,    155872,    185364,    220436,
+    262144,    311744,    370728,    440872,    524288,    623487,    741455,    881744,
+   1048576,   1246974,   1482910,   1763488,   2097152,   2493948,   2965821,   3526975,
+   4194304,   4987896,   5931642,   7053950,   8388608,   9975792,  11863283,  14107901,
+  16777216,  19951585,  23726566,  28215802,  33554432,  39903169,  47453133,  56431603,
+  67108864,  79806339,  94906266, 112863206, 134217728, 159612677, 189812531, 225726413,
+ 268435456, 319225354, 379625062, 451452825, 536870912, 638450708, 759250125, 902905651,
+1073741824,1276901417,1518500250,1805811301
+};
+
+static const int32_t qoffset_tab[128] = {
+        1,         2,         3,         4,         4,         5,         6,         7,
+        8,        10,        12,        14,        16,        19,        23,        27,
+       32,        38,        46,        54,        64,        76,        91,       108,
+      128,       152,       181,       216,       256,       305,       362,       431,
+      512,       609,       724,       861,      1024,      1218,      1448,      1722,
+     2048,      2436,      2897,      3445,      4096,      4871,      5793,      6889,
+     8192,      9742,     11585,     13777,     16384,     19484,     23171,     27555,
+    32768,     38968,     46341,     55109,     65536,     77936,     92682,    110218,
+   131072,    155872,    185364,    220436,    262144,    311744,    370728,    440872,
+   524288,    623487,    741455,    881744,   1048576,   1246974,   1482911,   1763488,
+  2097152,   2493948,   2965821,   3526975,   4194304,   4987896,   5931642,   7053951,
+  8388608,   9975793,  11863283,  14107901,  16777216,  19951585,  23726567,  28215802,
+ 33554432,  39903170,  47453133,  56431603,  67108864,  79806339,  94906266, 112863207,
+134217728, 159612677, 189812531, 225726413, 268435456, 319225354, 379625063, 451452826,
+536870912, 638450709, 759250125, 902905651,1073741824,1276901417,1518500250,1805811302
+};
+
+static av_always_inline void put_dirac_ue_uint(PutBitContext *pb, uint32_t val)
+{
+    int i;
+    int pbits = 0, bits = 0, topbit = 1, maxval = 1;
+
+    if (!val++) {
+        put_bits(pb, 1, 1);
+        return;
+    }
+
+    while (val > maxval) {
+        topbit <<= 1;
+        maxval <<= 1;
+        maxval |=  1;
+    }
+
+    bits = ff_log2(topbit);
+
+    for (i = 0; i < bits; i++) {
+        topbit >>= 1;
+        pbits <<= 2;
+        if (val & topbit)
+            pbits |= 0x1;
+    }
+
+    put_bits(pb, bits*2 + 1, (pbits << 1) | 1);
+}
+
+static av_always_inline int count_dirac_ue_uint(uint16_t val)
+{
+    int topbit = 1, maxval = 1;
+
+    if (!val++)
+        return 1;
+
+    while (val > maxval) {
+        topbit <<= 1;
+        maxval <<= 1;
+        maxval |=  1;
+    }
+
+    return ff_log2(topbit)*2 + 1;
+}
+
+static av_always_inline void get_dirac_ue_uint(uint16_t val, uint8_t *nbits,
+                                               uint32_t *eval)
+{
+    int i;
+    int pbits = 0, bits = 0, topbit = 1, maxval = 1;
+
+    if (!val++) {
+        *nbits = 1;
+        *eval = 1;
+        return;
+    }
+
+    while (val > maxval) {
+        topbit <<= 1;
+        maxval <<= 1;
+        maxval |=  1;
+    }
+
+    bits = ff_log2(topbit);
+
+    for (i = 0; i < bits; i++) {
+        topbit >>= 1;
+        pbits <<= 2;
+        if (val & topbit)
+            pbits |= 0x1;
+    }
+
+    *nbits = bits*2 + 1;
+    *eval = (pbits << 1) | 1;
+}
+
+/* VC-2 10.4 - parse_info() */
+static void encode_parse_info(DiracEncContext *s, enum DiracParseCodes pcode)
+{
+    int64_t cur_pos;
+
+    avpriv_align_put_bits(&s->pb);
+
+    cur_pos = put_bits_count(&s->pb) >> 3;
+
+    /* Magic string */
+    avpriv_put_string(&s->pb, "BBCD", 0);
+
+    /* Parse code */
+    put_bits(&s->pb, 8, pcode);
+
+    /* Next parse offset */
+    s->last_parse_offset = cur_pos - s->next_parse_offset;
+    AV_WB32(&s->pb.buf[s->next_parse_offset+5], s->last_parse_offset);
+    s->next_parse_offset = cur_pos;
+    put_bits32(&s->pb, pcode == DIRAC_PCODE_END_SEQ ? 0xd : 0);
+
+    /* Last parse offset */
+    put_bits32(&s->pb, s->last_parse_code != DIRAC_PCODE_END_SEQ ?
+                       s->last_parse_offset : 0xd);
+
+    s->last_parse_code = pcode;
+}
+
+/* VC-2 11.1 - parse_parameters() */
+static void encode_parse_params(DiracEncContext *s)
+{
+    put_dirac_ue_uint(&s->pb, 2);    /* VC-2 demands this to be 2 */
+    put_dirac_ue_uint(&s->pb, 0);    /* ^^ and this to be 0       */
+    put_dirac_ue_uint(&s->pb, 3);    /* 3 to signal HQ profile    */
+    put_dirac_ue_uint(&s->pb, 3);    /* Again 3 is required here  */
+}
+
+/* VC-2 11.3 - frame_size() */
+static void encode_frame_size(DiracEncContext *s)
+{
+    put_bits(&s->pb, 1, !s->strict_compliance);
+    if (!s->strict_compliance) {
+        AVCodecContext *avctx = s->avctx;
+        put_dirac_ue_uint(&s->pb, avctx->width);
+        put_dirac_ue_uint(&s->pb, avctx->height);
+    }
+}
+
+/* VC-2 11.3.3 - color_doff_sampling_format() */
+static void encode_sample_fmt(DiracEncContext *s)
+{
+    put_bits(&s->pb, 1, !s->strict_compliance);
+    if (!s->strict_compliance) {
+        int idx;
+        if (s->chroma_x_shift == 1 && s->chroma_y_shift == 0)
+            idx = 1; /* 422 */
+        else if (s->chroma_x_shift == 1 && s->chroma_y_shift == 1)
+            idx = 2; /* 420 */
+        else
+            idx = 0; /* 444 */
+        put_dirac_ue_uint(&s->pb, idx);
+    }
+}
+
+/* VC-2 11.3.4 - scan_format() */
+static void encode_scan_format(DiracEncContext *s)
+{
+    put_bits(&s->pb, 1, !s->strict_compliance);
+    if (!s->strict_compliance) {
+        AVCodecContext *avctx = s->avctx;
+        put_dirac_ue_uint(&s->pb, avctx->field_order == AV_FIELD_PROGRESSIVE);
+    }
+}
+
+/* VC-2 11.3.5 - frame_rate() */
+static void encode_frame_rate(DiracEncContext *s)
+{
+    put_bits(&s->pb, 1, !s->strict_compliance);
+    if (!s->strict_compliance) {
+        AVCodecContext *avctx = s->avctx;
+        put_dirac_ue_uint(&s->pb, 0);
+        put_dirac_ue_uint(&s->pb, avctx->time_base.den);
+        put_dirac_ue_uint(&s->pb, avctx->time_base.num);
+    }
+}
+
+/* VC-2 11.3.6 - aspect_ratio() */
+static void encode_aspect_ratio(DiracEncContext *s)
+{
+    put_bits(&s->pb, 1, !s->strict_compliance);
+    if (!s->strict_compliance) {
+        AVCodecContext *avctx = s->avctx;
+        put_dirac_ue_uint(&s->pb, 0);
+        put_dirac_ue_uint(&s->pb, avctx->sample_aspect_ratio.num);
+        put_dirac_ue_uint(&s->pb, avctx->sample_aspect_ratio.den);
+    }
+}
+
+/* VC-2 11.3.7 - clean_area() */
+static void encode_clean_area(DiracEncContext *s)
+{
+    put_bits(&s->pb, 1, 0);
+}
+
+/* VC-2 11.3.8 - signal_range() */
+static void encode_signal_range(DiracEncContext *s)
+{
+    int idx;
+    AVCodecContext *avctx = s->avctx;
+    const AVPixFmtDescriptor *fmt = av_pix_fmt_desc_get(avctx->pix_fmt);
+    const int depth = fmt->comp[0].depth;
+    if (depth == 8 && avctx->color_range == AVCOL_RANGE_JPEG) {
+        idx = 1;
+        s->diff_offset = 128;
+    } else if (depth == 8 && avctx->color_range == AVCOL_RANGE_MPEG) {
+        idx = 2;
+        s->diff_offset = 128;
+    } else if (depth == 10) {
+        idx = 3;
+        s->diff_offset = 512;
+    } else {
+        idx = 4;
+        s->diff_offset = 2048;
+    }
+    put_bits(&s->pb, 1, !s->strict_compliance);
+    if (!s->strict_compliance)
+        put_dirac_ue_uint(&s->pb, idx);
+}
+
+/* VC-2 11.3.9 - color_spec() */
+static void encode_color_spec(DiracEncContext *s)
+{
+    put_bits(&s->pb, 1, 0);
+}
+
+/* VC-2 11.3 - source_parameters() */
+static void encode_source_params(DiracEncContext *s)
+{
+    encode_frame_size(s);
+    encode_sample_fmt(s);
+    encode_scan_format(s);
+    encode_frame_rate(s);
+    encode_aspect_ratio(s);
+    encode_clean_area(s);
+    encode_signal_range(s);
+    encode_color_spec(s);
+}
+
+/* VC-2 11 - sequence_header() */
+static void encode_seq_header(DiracEncContext *s)
+{
+    int base_vf = !s->strict_compliance ? 0 : 14;
+    avpriv_align_put_bits(&s->pb);
+    encode_parse_params(s);
+    put_dirac_ue_uint(&s->pb, base_vf);
+    encode_source_params(s);
+    put_dirac_ue_uint(&s->pb, 0); /* Frame encoding only for now, fields == 1 */
+}
+
+/* VC-2 12.1 - picture_header() */
+static void encode_picture_header(DiracEncContext *s)
+{
+    AVCodecContext *avctx = s->avctx;
+    avpriv_align_put_bits(&s->pb);
+    put_bits32(&s->pb, avctx->frame_number);
+}
+
+/* VC-2 12.3.4.1 - slice_parameters() */
+static void encode_slice_params(DiracEncContext *s)
+{
+    put_dirac_ue_uint(&s->pb, s->num_x);
+    put_dirac_ue_uint(&s->pb, s->num_y);
+    put_dirac_ue_uint(&s->pb, s->prefix_bytes);
+    put_dirac_ue_uint(&s->pb, s->size_scaler);
+}
+
+/* VC-2 12.3.4.2 - quant_matrix() */
+static void encode_quant_matrix(DiracEncContext *s)
+{
+    int level;
+    put_bits(&s->pb, 1, s->custom_quant_matrix);
+    if (s->custom_quant_matrix) {
+        put_dirac_ue_uint(&s->pb, s->quant[0][0]);
+        for (level = 1; level < s->wavelet_depth; level++) {
+            put_dirac_ue_uint(&s->pb, s->quant[level][1]);
+            put_dirac_ue_uint(&s->pb, s->quant[level][2]);
+            put_dirac_ue_uint(&s->pb, s->quant[level][3]);
+        }
+    } else {
+        for (level = 0; level < s->wavelet_depth; level++) {
+            s->quant[level][0] = default_qmat[s->wavelet_idx][level][0];
+            s->quant[level][1] = default_qmat[s->wavelet_idx][level][1];
+            s->quant[level][2] = default_qmat[s->wavelet_idx][level][2];
+            s->quant[level][3] = default_qmat[s->wavelet_idx][level][3];
+        }
+    }
+}
+
+/* VC-2 12.3 - transform_parameters() */
+static void encode_transform_params(DiracEncContext *s)
+{
+    put_dirac_ue_uint(&s->pb, s->wavelet_idx);
+    put_dirac_ue_uint(&s->pb, s->wavelet_depth);
+
+    if (s->wavelet_depth > 4)
+        s->custom_quant_matrix = 1;
+
+    /* If !low delay, print codeblock params right here instead of the following */
+    encode_slice_params(s);
+    encode_quant_matrix(s);
+}
+
+/* VC-2 12.2 - wavelet_transform() */
+static void encode_wavelet_transform(DiracEncContext *s)
+{
+    encode_transform_params(s);
+    avpriv_align_put_bits(&s->pb);
+    /* Continued after DWT in encode_transform_data() */
+}
+
+/* VC-2 12 - picture_parse() */
+static void encode_picture_start(DiracEncContext *s)
+{
+    avpriv_align_put_bits(&s->pb);
+    encode_picture_header(s);
+    avpriv_align_put_bits(&s->pb);
+    encode_wavelet_transform(s);
+}
+
+static av_always_inline void coeff_quantize_encode(PutBitContext *pb, qcoef coeff,
+                                                   int qfactor, int qoffset)
+{
+    uint16_t acoef;
+    int sign = coeff < 0;
+    coeff -= sign;
+    coeff ^= -sign;
+    coeff <<= 2;
+    coeff = (coeff - qoffset)/qfactor;
+    acoef = abs(coeff);
+    put_dirac_ue_uint(pb, acoef);
+    if (acoef)
+        put_bits(pb, 1, sign);
+}
+
+static av_always_inline void coeff_quantize_get(qcoef coeff, int qfactor, int qoffset,
+                                                uint8_t *len, uint32_t *eval)
+{
+    uint16_t acoef;
+    int sign = coeff < 0;
+    coeff -= sign;
+    coeff ^= -sign;
+    coeff <<= 2;
+    coeff = (coeff - qoffset)/qfactor;
+    acoef = abs(coeff);
+    get_dirac_ue_uint(acoef, len, eval);
+    if (acoef) {
+        *eval = (*eval << 1) | sign;
+        *len += 1;
+    }
+}
+
+/* VC-2 13.5.5.2 - slice_band() */
+static void encode_subband(DiracEncContext *s, PutBitContext *pb, int sx, int sy,
+                           SubBand *b, int quant)
+{
+    int x, y;
+
+    int left   = b->width  * (sx+0) / s->num_x;
+    int right  = b->width  * (sx+1) / s->num_x;
+    int top    = b->height * (sy+0) / s->num_y;
+    int bottom = b->height * (sy+1) / s->num_y;
+
+    int qfactor = qscale_tab[quant];
+    int qoffset = qoffset_tab[quant] + 2;
+
+    dwtcoef *coeff = b->buf + top * b->stride;
+
+    for (y = top; y < bottom; y++) {
+        for (x = left; x < right; x++) {
+            qcoef coef_t = (qcoef)coeff[x];
+            if (coef_t >= -COEF_LUT_TAB && coef_t < COEF_LUT_TAB)
+                put_bits(pb,
+                         s->coef_lut_len[2*quant*COEF_LUT_TAB + coef_t + COEF_LUT_TAB],
+                         s->coef_lut_val[2*quant*COEF_LUT_TAB + coef_t + COEF_LUT_TAB]);
+            else
+                coeff_quantize_encode(pb, coef_t, qfactor, qoffset);
+        }
+        coeff += b->stride;
+    }
+}
+
+static int count_hq_slice(DiracEncContext *s, int slice_x,
+                          int slice_y, int quant_idx)
+{
+    int x, y, left, right, top, bottom, qfactor, qoffset;
+    uint8_t quants[4][4];
+    int p, level, orientation;
+    int bits = 8*s->prefix_bytes + 8 + 24 + 8;
+
+    for (level = 0; level < s->wavelet_depth; level++)
+        for (orientation = !!level; orientation < 4; orientation++)
+            quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
+
+    for (p = 0; p < 3; p++) {
+        int bytes_start, bytes_end, bytes_len, bytes_pad;
+        bytes_start = bits >> 3;
+        for (level = 0; level < s->wavelet_depth; level++) {
+            for (orientation = !!level; orientation < 4; orientation++) {
+                SubBand *b = &s->plane[p].band[level][orientation];
+
+                quant_idx = quants[level][orientation];
+                qfactor = qscale_tab[quant_idx];
+                qoffset = qoffset_tab[quant_idx] + 2;
+
+                left   = b->width  * slice_x    / s->num_x;
+                right  = b->width  *(slice_x+1) / s->num_x;
+                top    = b->height * slice_y    / s->num_y;
+                bottom = b->height *(slice_y+1) / s->num_y;
+
+                dwtcoef *buf = b->buf + top * b->stride;
+
+                for (y = top; y < bottom; y++) {
+                    for (x = left; x < right; x++) {
+                        qcoef coeff = (qcoef)buf[x];
+                        if (coeff >= -COEF_LUT_TAB && coeff < COEF_LUT_TAB) {
+                            bits += s->coef_lut_len[2*quant_idx*COEF_LUT_TAB + coeff + COEF_LUT_TAB];
+                        } else {
+                            uint16_t acoef;
+                            const int sign = coeff < 0;
+                            coeff -= sign;
+                            coeff ^= -sign;
+                            coeff <<= 2;
+                            coeff = (coeff - qoffset)/qfactor;
+                            acoef = abs(coeff);
+                            bits += count_dirac_ue_uint(acoef);
+                            bits += !!coeff;
+                        }
+                    }
+                    buf += b->stride;
+                }
+            }
+        }
+        bytes_end = bits >> 3;
+        bytes_len = FFALIGN((bytes_end - bytes_start), 64)/s->size_scaler;
+        bytes_pad = (bytes_len*s->size_scaler) - (bytes_end - bytes_start);
+        bits += bytes_pad*8;
+    }
+
+    return bits;
+}
+
+static int rate_control(DiracEncContext *s, int sx, int sy)
+{
+    int quant_buf[2], bits_buf[2], quant = s->q_start, range = s->q_ceil/4;
+    const int64_t top = s->slice_max_bits;
+    const double percent = s->tolerance;
+    const double bottom = top - top*(percent/100.0f);
+    int bits = count_hq_slice(s, sx, sy, quant);
+    while ((bits > top) || (bits < bottom)) {
+        range *= bits > top ? +1 : -1;
+        quant = av_clip(quant + range, 0, s->q_ceil);
+        bits = count_hq_slice(s, sx, sy, quant);
+        range = av_clip(range/2, 1, s->q_ceil);
+        if (quant_buf[1] == quant) {
+            quant = bits_buf[0] < bits ? quant_buf[0] : quant;
+            bits = bits_buf[0] < bits ? bits_buf[0] : bits;
+            break;
+        }
+        quant_buf[1] = quant_buf[0];
+        quant_buf[0] = quant;
+        bits_buf[1] = bits_buf[0];
+        bits_buf[0] = bits;
+    }
+    if (top > bits)
+        quant++;
+    return av_clip(quant, 0, s->q_ceil);
+}
+
+/* VC-2 13.5.3 - hq_slice */
+static int encode_hq_slice(AVCodecContext *avctx, void *arg)
+{
+    SliceArgs *slice_dat = arg;
+    DiracEncContext *s = slice_dat->ctx;
+    PutBitContext *pb = &slice_dat->pb;
+    const int slice_x = slice_dat->x;
+    const int slice_y = slice_dat->y;
+    uint8_t quants[4][4];
+    int p, j, level, orientation;
+    int slice_bytes_start = put_bits_count(pb) >> 3;
+    int quant_idx = rate_control(s, slice_x, slice_y);
+
+    avpriv_align_put_bits(pb);
+    put_bits(pb, 8*s->prefix_bytes, 0);
+    put_bits(pb, 8, quant_idx);
+
+    /* Slice quantization (slice_quantizers() in the specs) */
+    for (level = 0; level < s->wavelet_depth; level++)
+        for (orientation = !!level; orientation < 4; orientation++)
+            quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);
+
+    /* Luma + 2 Chroma planes */
+    for (p = 0; p < 3; p++) {
+        int bytes_start, bytes_len, pad_s, pad_c;
+        avpriv_align_put_bits(pb);
+        bytes_start = put_bits_count(pb) >> 3;
+        put_bits(pb, 8, 0);
+        for (level = 0; level < s->wavelet_depth; level++) {
+            for (orientation = !!level; orientation < 4; orientation++) {
+                encode_subband(s, pb, slice_x, slice_y,
+                               &s->plane[p].band[level][orientation],
+                               quants[level][orientation]);
+            }
+        }
+        avpriv_align_put_bits(pb);
+        bytes_len = (put_bits_count(pb) >> 3) - bytes_start - 1;
+        if (p == 2) {
+            int slice_bytes_max = FFALIGN(s->slice_max_bits >> 3, s->size_scaler);
+            int slice_bytes_end = (put_bits_count(pb) >> 3);
+            int slice_bytes_len = (slice_bytes_end - slice_bytes_start);
+            int len_diff = slice_bytes_max - slice_bytes_len;
+            pad_s = FFALIGN((bytes_len + len_diff), s->size_scaler)/s->size_scaler;
+            pad_c = (pad_s*s->size_scaler) - bytes_len;
+        } else {
+            pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
+            pad_c = (pad_s*s->size_scaler) - bytes_len;
+        }
+        av_assert0(pad_s < 256);
+        pb->buf[bytes_start] = pad_s;
+        for (j = 0; j < pad_c; j++)
+            put_bits(pb, 8, 0);
+    }
+
+    return 0;
+}
+
+/* VC-2 13.5.1 - low_delay_transform_data() */
+static int encode_slices(DiracEncContext *s)
+{
+    uint8_t *buf;
+    int slice_x, slice_y;
+    int slice_spacing = FFALIGN(s->slice_max_bits >> 3, s->size_scaler) + 4;
+    SliceArgs *enc_args = s->slice_args;
+
+    avpriv_align_put_bits(&s->pb);
+
+    flush_put_bits(&s->pb);
+
+    buf = put_bits_ptr(&s->pb);
+
+    for (slice_y = 0; slice_y < s->num_y; slice_y++) {
+        for (slice_x = 0; slice_x < s->num_x; slice_x++) {
+            SliceArgs *args = &enc_args[s->num_x*slice_y + slice_x];
+            args->ctx = s;
+            args->x = slice_x;
+            args->y = slice_y;
+            init_put_bits(&args->pb, buf, slice_spacing);
+            buf += slice_spacing;
+        }
+    }
+
+    s->avctx->execute(s->avctx, encode_hq_slice, enc_args, NULL, s->num_x*s->num_y,
+                      sizeof(SliceArgs));
+
+    skip_put_bytes(&s->pb, slice_spacing*s->num_y*s->num_x);
+
+    return 0;
+}
+
+static void dwt_planes(DiracEncContext *s, const AVFrame *frame)
+{
+    int i, x, y, level;
+    const int idx = s->wavelet_idx;
+
+    for (i = 0; i < 3; i++) {
+        Plane *p = &s->plane[i];
+        dwtcoef *buf = p->coef_buf;
+        uint16_t *pix = (uint16_t *)frame->data[i];
+
+        for (y = 0; y < p->height; y++) {
+            for (x = 0; x < p->width; x++) {
+                buf[x] = pix[x] - s->diff_offset;
+            }
+            buf += p->coef_stride;
+            pix += frame->linesize[i] >> 1;
+        }
+        memset(buf, 0, (p->coef_stride*p->dwt_height - p->height*p->width)*sizeof(dwtcoef));
+
+        /* DWT */
+        for (level = s->wavelet_depth-1; level >= 0; level--) {
+            SubBand *b = &p->band[level][0];
+            s->t.dirac_subband_dwt[idx](&s->t, p->coef_buf, p->coef_stride,
+                                        b->width, b->height);
+        }
+    }
+}
+
+static av_cold int dirac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
+                                      const AVFrame *frame, int *got_packet_ptr)
+{
+    int ret;
+    double tpf;
+    DiracEncContext *s = avctx->priv_data;
+
+    ret = ff_alloc_packet2(avctx, avpkt, avctx->width*avctx->height*sizeof(int32_t)*2, 0);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
+        return ret;
+    }
+
+    s->avctx = avctx;
+    s->prefix_bytes = 0;
+    s->size_scaler = 32;
+    s->custom_quant_matrix = 0;
+    s->next_parse_offset = 0;
+
+    /* Rate control */
+    tpf = s->avctx->time_base.num/(double)s->avctx->time_base.den;
+    s->frame_max_bits = s->avctx->bit_rate*tpf;
+    s->slice_max_bits = s->frame_max_bits/(s->num_x*s->num_y);
+
+    /* Init PB */
+    init_put_bits(&s->pb, avpkt->data, avpkt->size);
+
+    /* Sequence header */
+    encode_parse_info(s, DIRAC_PCODE_SEQ_HEADER);
+    encode_seq_header(s);
+
+    /* Picture header */
+    encode_parse_info(s, DIRAC_PCODE_PICTURE_HQ);
+    encode_picture_start(s);
+
+    /* Do a DWT transform */
+    dwt_planes(s, frame);
+
+    /* Init planes and encode slices */
+    encode_slices(s);
+
+    /* End sequence */
+    encode_parse_info(s, DIRAC_PCODE_END_SEQ);
+
+    flush_put_bits(&s->pb);
+    avpkt->size = put_bits_count(&s->pb) >> 3;
+
+    *got_packet_ptr = 1;
+
+    return 0;
+}
+
+static av_cold int dirac_encode_end(AVCodecContext *avctx)
+{
+    int i;
+    DiracEncContext *s = avctx->priv_data;
+
+    diracenc_deinit_transforms(&s->t);
+
+    for (i = 0; i < 3; i++)
+        av_freep(&s->plane[i].coef_buf);
+
+    av_freep(&s->slice_args);
+    av_freep(&s->coef_lut_len);
+    av_freep(&s->coef_lut_val);
+
+    return 0;
+}
+
+
+static av_cold int dirac_encode_init(AVCodecContext *avctx)
+{
+    Plane *p;
+    SubBand *b;
+    int i, j, level, o, shift;
+    DiracEncContext *s = avctx->priv_data;
+
+    s->q_ceil = FF_ARRAY_ELEMS(qscale_tab);
+    s->next_parse_offset = 0;
+    s->last_parse_offset = 0;
+    s->strict_compliance = 1;
+    s->q_start = lrint(s->q_ceil/2.3f);
+
+    if (avctx->pix_fmt != AV_PIX_FMT_YUV422P10 ||
+        avctx->width != 1920 || avctx->height != 1080) {
+        if (avctx->strict_std_compliance <= FF_COMPLIANCE_EXPERIMENTAL) {
+            s->strict_compliance = 0;
+            av_log(avctx, AV_LOG_WARNING, "Disabling strict compliance\n");
+        } else {
+            av_log(avctx, AV_LOG_ERROR, "Pixel formats other than yuv422p10le "
+            "and sizes other than 1920x1080 are not within the standard, but are "
+            "still decodable by most decoders, add -strict -2 to enable support.\n");
+            return AVERROR_UNKNOWN;
+        }
+    }
+
+    av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
+                                     &s->chroma_x_shift,
+                                     &s->chroma_y_shift);
+
+    /* Planes initialization */
+    for (i = 0; i < 3; i++) {
+        int w, h;
+        p = &s->plane[i];
+        p->width      = avctx->width  >> (i ? s->chroma_x_shift : 0);
+        p->height     = avctx->height >> (i ? s->chroma_y_shift : 0);
+        p->dwt_width  = w = FFALIGN(p->width,  (1 << s->wavelet_depth));
+        p->dwt_height = h = FFALIGN(p->height, (1 << s->wavelet_depth));
+        p->coef_stride = FFALIGN(p->dwt_width, 32);
+        p->coef_buf = av_malloc(p->coef_stride*p->dwt_height*sizeof(dwtcoef));
+        if (!p->coef_buf)
+            av_log(avctx, AV_LOG_ERROR, "Unable to allocate memory! - %i %i %i\n", w, h, p->coef_stride);
+        av_log(avctx, AV_LOG_WARNING, "Padded size = %i %i %i\n", p->dwt_width, p->dwt_height, p->coef_stride);
+        for (level = s->wavelet_depth-1; level >= 0; level--) {
+            w = w >> 1;
+            h = h >> 1;
+            for (o = 0; o < 4; o++) {
+                b = &p->band[level][o];
+                b->width  = w;
+                b->height = h;
+                b->stride = p->coef_stride;
+                shift = (o > 1)*b->height*b->stride + (o & 1)*b->width;
+                b->buf = p->coef_buf + shift;
+            }
+        }
+    }
+
+    s->coef_lut_len = av_malloc(2*COEF_LUT_TAB*s->q_ceil*sizeof(*s->coef_lut_len));
+    s->coef_lut_val = av_malloc(2*COEF_LUT_TAB*s->q_ceil*sizeof(*s->coef_lut_val));
+
+    for (i = 0; i < s->q_ceil; i++) {
+        for (j = -COEF_LUT_TAB; j < COEF_LUT_TAB; j++) {
+            coeff_quantize_get(j, qscale_tab[i], qoffset_tab[i] + 2,
+                               &s->coef_lut_len[2*i*COEF_LUT_TAB + j + COEF_LUT_TAB],
+                               &s->coef_lut_val[2*i*COEF_LUT_TAB + j + COEF_LUT_TAB]);
+        }
+    }
+
+    if (diracenc_init_transforms(&s->t, s->plane[0].coef_stride, s->plane[0].dwt_height))
+        av_log(avctx, AV_LOG_ERROR, "Unable to allocate memory!\n");
+
+    /* Slices */
+    s->num_x = s->plane[0].dwt_width/s->slice_width;
+    s->num_y = s->plane[0].dwt_height/s->slice_height;
+
+    s->slice_args = av_malloc(s->num_x*s->num_y*sizeof(SliceArgs));
+
+    return 0;
+}
+
+#define DIRACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption diracenc_options[] = {
+    {"tolerance",     "Max undershoot in %", offsetof(DiracEncContext, tolerance), AV_OPT_TYPE_DOUBLE, {.dbl = 2.0f}, 0.1f, 99.0f, DIRACENC_FLAGS, "tolerance"},
+    {"slice_width",   "Slice width",  offsetof(DiracEncContext, slice_width), AV_OPT_TYPE_INT, {.i64 = 64}, 4, 64, DIRACENC_FLAGS, "slice_width"},
+    {"slice_height",  "Slice height", offsetof(DiracEncContext, slice_height), AV_OPT_TYPE_INT, {.i64 = 64}, 4, 64, DIRACENC_FLAGS, "slice_height"},
+    {"wavelet_depth", "Transform depth", offsetof(DiracEncContext, wavelet_depth), AV_OPT_TYPE_INT, {.i64 = 4}, 1, 4, DIRACENC_FLAGS, "wavelet_depth"},
+    {"wavelet_type",  "Transform type",  offsetof(DiracEncContext, wavelet_idx), AV_OPT_TYPE_INT, {.i64 = DIRAC_TRANSFORM_9_7}, 0, DIRAC_TRANSFORMS_NB, DIRACENC_FLAGS, "wavelet_idx"},
+        {"9_7",       "Deslauriers-Dubuc (9,7)", 0, AV_OPT_TYPE_CONST, {.i64 = DIRAC_TRANSFORM_9_7},   INT_MIN, INT_MAX, DIRACENC_FLAGS, "wavelet_idx"},
+        {"5_3",       "LeGall (5,3)",            0, AV_OPT_TYPE_CONST, {.i64 = DIRAC_TRANSFORM_5_3},   INT_MIN, INT_MAX, DIRACENC_FLAGS, "wavelet_idx"},
+    {NULL}
+};
+
+static const AVClass diracenc_class = {
+    "BBC Dirac VC-2 encoder",
+    av_default_item_name,
+    diracenc_options,
+    LIBAVUTIL_VERSION_INT,
+};
+
+static const AVCodecDefault diracenc_defaults[] = {
+    { "b",              "500000000"   },
+    { NULL },
+};
+
+AVCodec ff_dirac_encoder = {
+    .name = "dirac",
+    .long_name = NULL_IF_CONFIG_SMALL("BBC Dirac VC-2"),
+    .type = AVMEDIA_TYPE_VIDEO,
+    .id = AV_CODEC_ID_DIRAC,
+    .priv_data_size = sizeof(DiracEncContext),
+    .init = dirac_encode_init,
+    .close = dirac_encode_end,
+    .capabilities = AV_CODEC_CAP_SLICE_THREADS,
+    .encode2 = dirac_encode_frame,
+    .priv_class = &diracenc_class,
+    .defaults = diracenc_defaults,
+};
diff --git a/libavcodec/diracenc_transforms.c b/libavcodec/diracenc_transforms.c
new file mode 100644
index 0000000..1180a08
--- /dev/null
+++ b/libavcodec/diracenc_transforms.c
@@ -0,0 +1,243 @@
+/*
+ * Copyright (C) 2007 Marco Gerards <marco at gnu.org>
+ * Copyright (C) 2016 Open Broadcast Systemd Ltd.
+ * Author    (C) 2016 Rostislav Pehlivanov <atomnuker at gmail.com>
+ *
+ * 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, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "diracenc_transforms.h"
+#include "libavutil/avassert.h"
+
+static av_always_inline void deinterleave(dwtcoef *linell, int stride,
+                                          int width, int height, dwtcoef *synthl)
+{
+    int x, y;
+    int synthw = width << 1;
+    dwtcoef *linehl = linell + width;
+    dwtcoef *linelh = linell + height*stride;
+    dwtcoef *linehh = linelh + width;
+
+    /* Deinterleave the coefficients. */
+    for (y = 0; y < height; y++) {
+        for (x = 0; x < width; x++) {
+            linell[x] = synthl[(x << 1)];
+            linehl[x] = synthl[(x << 1) + 1];
+            linelh[x] = synthl[(x << 1) + synthw];
+            linehh[x] = synthl[(x << 1) + synthw + 1];
+        }
+        synthl += synthw << 1;
+        linell += stride;
+        linelh += stride;
+        linehl += stride;
+        linehh += stride;
+    }
+}
+
+static void dirac_subband_dwt_97(DiracTransforms *t,
+                                 dwtcoef *data, int stride,
+                                 int width, int height)
+{
+    int x, y;
+    dwtcoef *datal = data, *synth = t->buffer, *synthl = synth;
+    const int synth_width = width  << 1;
+    const int synth_height = height << 1;
+
+    /*
+     * Shift in one bit that is used for additional precision and copy
+     * the data to the buffer.
+     */
+    for (y = 0; y < synth_height; y++) {
+        for (x = 0; x < synth_width; x++)
+            synthl[x] = datal[x] << 1;
+        synthl += synth_width;
+        datal += stride;
+    }
+
+    /* Horizontal synthesis. */
+    synthl = synth;
+    for (y = 0; y < synth_height; y++) {
+        /* Lifting stage 2. */
+        synthl[1] -= (8*synthl[0] + 9*synthl[2] - synthl[4] + 8) >> 4;
+        for (x = 1; x < width - 2; x++)
+            synthl[2*x + 1] -= (9*synthl[2*x] + 9*synthl[2*x + 2] - synthl[2*x + 4] -
+            synthl[2 * x - 2] + 8) >> 4;
+        synthl[synth_width - 1] -= (17*synthl[synth_width - 2] -
+        synthl[synth_width - 4] + 8) >> 4;
+        synthl[synth_width - 3] -= (8*synthl[synth_width - 2] +
+        9*synthl[synth_width - 4] -
+        synthl[synth_width - 6] + 8) >> 4;
+        /* Lifting stage 1. */
+        synthl[0] += (synthl[1] + synthl[1] + 2) >> 2;
+        for (x = 1; x < width - 1; x++)
+            synthl[2*x] += (synthl[2*x - 1] + synthl[2*x + 1] + 2) >> 2;
+
+        synthl[synth_width - 2] += (synthl[synth_width - 3] +
+        synthl[synth_width - 1] + 2) >> 2;
+        synthl += synth_width;
+    }
+
+    /* Vertical synthesis: Lifting stage 2. */
+    synthl = synth + synth_width;
+    for (x = 0; x < synth_width; x++)
+        synthl[x] -= (8*synthl[x - synth_width] + 9*synthl[x + synth_width] -
+        synthl[x + 3 * synth_width] + 8) >> 4;
+
+    synthl = synth + (synth_width << 1);
+    for (y = 1; y < height - 2; y++) {
+        for (x = 0; x < synth_width; x++)
+            synthl[x + synth_width] -= (9*synthl[x] +
+            9*synthl[x + 2 * synth_width] -
+            synthl[x - 2 * synth_width] -
+            synthl[x + 4 * synth_width] + 8) >> 4;
+        synthl += synth_width << 1;
+    }
+
+    synthl = synth + (synth_height - 1) * synth_width;
+    for (x = 0; x < synth_width; x++) {
+        synthl[x] -= (17*synthl[x - synth_width] -
+        synthl[x - 3*synth_width] + 8) >> 4;
+        synthl[x - 2*synth_width] -= (9*synthl[x - 3*synth_width] +
+        8*synthl[x - 1*synth_width] -
+        synthl[x - 5*synth_width] + 8) >> 4;
+    }
+
+    /* Vertical synthesis: Lifting stage 1. */
+    synthl = synth;
+    for (x = 0; x < synth_width; x++) {
+        synthl[x] += (synthl[x + synth_width] + synthl[x + synth_width] + 2) >> 2;
+    }
+
+    synthl = synth + (synth_width << 1);
+    for (y = 1; y < height - 1; y++) {
+        for (x = 0; x < synth_width; x++)
+            synthl[x] += (synthl[x - synth_width] + synthl[x + synth_width] + 2) >> 2;
+        synthl += synth_width << 1;
+    }
+
+    synthl = synth + (synth_height - 2) * synth_width;
+    for (x = 0; x < synth_width; x++)
+        synthl[x] += (synthl[x - synth_width] + synthl[x + synth_width] + 2) >> 2;
+
+    deinterleave(data, stride, width, height, synth);
+}
+
+static void dirac_subband_dwt_53(DiracTransforms *t,
+                                 dwtcoef *data, int stride,
+                                 int width, int height)
+{
+    int x, y;
+    dwtcoef *synth = t->buffer, *synthl = synth, *datal = data;
+    const int synth_width = width  << 1;
+    const int synth_height = height << 1;
+
+    /*
+     * Shift in one bit that is used for additional precision and copy
+     * the data to the buffer.
+     */
+    for (y = 0; y < synth_height; y++) {
+        for (x = 0; x < synth_width; x++)
+            synthl[x] = datal[x] << 1;
+        synthl += synth_width;
+        datal  += stride;
+    }
+
+    /* Horizontal synthesis. */
+    synthl = synth;
+    for (y = 0; y < synth_height; y++) {
+        /* Lifting stage 2. */
+        for (x = 0; x < width - 1; x++)
+            synthl[2 * x + 1] -= (synthl[2 * x] + synthl[2 * x + 2] + 1) >> 1;
+
+        synthl[synth_width - 1] -= (2*synthl[synth_width - 2] + 1) >> 1;
+
+        /* Lifting stage 1. */
+        synthl[0] += (2*synthl[1] + 2) >> 2;
+        for (x = 1; x < width - 1; x++)
+            synthl[2 * x] += (synthl[2 * x - 1] + synthl[2 * x + 1] + 2) >> 2;
+
+        synthl[synth_width - 2] += (synthl[synth_width - 3] + synthl[synth_width - 1] + 2) >> 2;
+
+        synthl += synth_width;
+    }
+
+    /* Vertical synthesis: Lifting stage 2. */
+    synthl = synth + synth_width;
+    for (x = 0; x < synth_width; x++)
+        synthl[x] -= (synthl[x - synth_width] + synthl[x + synth_width] + 1) >> 1;
+
+    synthl = synth + (synth_width << 1);
+    for (y = 1; y < height - 1; y++) {
+        for (x = 0; x < synth_width; x++) {
+            synthl[x + synth_width] -= (synthl[x] + synthl[x + synth_width * 2] + 1) >> 1;
+        }
+        synthl += (synth_width << 1);
+    }
+
+    synthl = synth + (synth_height - 1) * synth_width;
+    for (x = 0; x < synth_width; x++)
+        synthl[x] -= (2*synthl[x - synth_width] + 1) >> 1;
+
+    /* Vertical synthesis: Lifting stage 1. */
+    synthl = synth;
+    for (x = 0; x < synth_width; x++) {
+        synthl[x] += (2*synthl[synth_width + x] + 2) >> 2;
+    }
+
+    synthl = synth + (synth_width << 1);
+    for (y = 1; y < height - 1; y++) {
+        for (x = 0; x < synth_width; x++) {
+            synthl[x] += (synthl[x + synth_width] + synthl[x - synth_width] + 2) >> 2;
+        }
+        synthl += (synth_width << 1);
+    }
+
+    synthl = synth + (synth_height - 2)*synth_width;
+    for (x = 0; x < synth_width; x++)
+        synthl[x] += (synthl[x - synth_width] + synthl[x + synth_width] + 2) >> 2;
+
+
+    deinterleave(data, stride, width, height, synth);
+}
+
+static void dirac_subband_noop(DiracTransforms *t, dwtcoef *data, int stride,
+                               int width, int height)
+{
+    av_assert0(0);
+}
+
+int diracenc_init_transforms(DiracTransforms *s, int p_width, int p_height)
+{
+    s->dirac_subband_dwt[DIRAC_TRANSFORM_9_7]    = dirac_subband_dwt_97;
+    s->dirac_subband_dwt[DIRAC_TRANSFORM_5_3]    = dirac_subband_dwt_53;
+    s->dirac_subband_dwt[DIRAC_TRANSFORM_13_7]   = dirac_subband_noop;
+    s->dirac_subband_dwt[DIRAC_TRANSFORM_HAAR]   = dirac_subband_noop;
+    s->dirac_subband_dwt[DIRAC_TRANSFORM_HAAR_S] = dirac_subband_noop;
+    s->dirac_subband_dwt[DIRAC_TRANSFORM_FIDEL]  = dirac_subband_noop;
+    s->dirac_subband_dwt[DIRAC_TRANSFORM_9_7_I]  = dirac_subband_noop;
+
+    s->buffer = av_malloc(2*p_width*p_height*sizeof(dwtcoef));
+    if (!s->buffer)
+        return 1;
+
+    return 0;
+}
+
+void diracenc_deinit_transforms(DiracTransforms *s)
+{
+    av_freep(&s->buffer);
+}
diff --git a/libavcodec/diracenc_transforms.h b/libavcodec/diracenc_transforms.h
new file mode 100644
index 0000000..ee3191c
--- /dev/null
+++ b/libavcodec/diracenc_transforms.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2016 Open Broadcast Systemd Ltd.
+ * Author    (C) 2016 Rostislav Pehlivanov <atomnuker at gmail.com>
+ *
+ * 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, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_DIRAC_WAVELET_H
+#define AVCODEC_DIRAC_WAVELET_H
+
+#include <stdint.h>
+
+typedef int16_t dwtcoef;
+typedef int32_t qcoef;   /* Quantization needs more precision */
+
+/* Only Deslauriers-Dubuc (9,7) and LeGall (5,3) supported! */
+
+enum DiracTransformType {
+    DIRAC_TRANSFORM_9_7    = 0,   /* Deslauriers-Dubuc (9,7)  */
+    DIRAC_TRANSFORM_5_3    = 1,   /* LeGall (5,3)             */
+    DIRAC_TRANSFORM_13_7   = 2,   /* Deslauriers-Dubuc (13,7) */
+    DIRAC_TRANSFORM_HAAR   = 3,   /* Haar without shift       */
+    DIRAC_TRANSFORM_HAAR_S = 4,   /* Haar with 1 shift/lvl    */
+    DIRAC_TRANSFORM_FIDEL  = 5,   /* Fidelity filter          */
+    DIRAC_TRANSFORM_9_7_I  = 6,   /* Daubechies (9,7)         */
+    DIRAC_TRANSFORMS_NB
+};
+
+typedef struct DiracTransforms {
+    dwtcoef *buffer;
+    void (*dirac_subband_dwt[DIRAC_TRANSFORMS_NB])(struct DiracTransforms *t,
+                                                   dwtcoef *data, int stride,
+                                                   int width, int height);
+} DiracTransforms;
+
+int diracenc_init_transforms(DiracTransforms *t, int p_width, int p_height);
+void diracenc_deinit_transforms(DiracTransforms *t);
+
+#endif /* AVCODEC_DIRACWAVELET_H */
-- 
2.7.0.rc3



More information about the ffmpeg-devel mailing list