xref: /third_party/ffmpeg/libavcodec/mvha.c (revision cabdff1a)
1/*
2 * MidiVid Archive codec
3 *
4 * Copyright (c) 2019 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#define CACHED_BITSTREAM_READER !ARCH_X86_32
28#include "libavutil/intreadwrite.h"
29
30#include "avcodec.h"
31#include "bytestream.h"
32#include "codec_internal.h"
33#include "get_bits.h"
34#include "internal.h"
35#include "lossless_videodsp.h"
36#include "zlib_wrapper.h"
37
38#include <zlib.h>
39
40typedef struct MVHAContext {
41    GetBitContext     gb;
42    int nb_symbols;
43
44    uint8_t           symb[256];
45    uint32_t          prob[256];
46    VLC               vlc;
47
48    FFZStream         zstream;
49    LLVidDSPContext   llviddsp;
50} MVHAContext;
51
52typedef struct Node {
53    int16_t  sym;
54    int16_t  n0;
55    int16_t  l, r;
56    uint32_t count;
57} Node;
58
59static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
60                           Node *nodes, int node,
61                           uint32_t pfx, int pl, int *pos)
62{
63    int s;
64
65    s = nodes[node].sym;
66    if (s != -1) {
67        bits[*pos] = (~pfx) & ((1ULL << FFMAX(pl, 1)) - 1);
68        lens[*pos] = FFMAX(pl, 1);
69        xlat[*pos] = s + (pl == 0);
70        (*pos)++;
71    } else {
72        pfx <<= 1;
73        pl++;
74        get_tree_codes(bits, lens, xlat, nodes, nodes[node].l, pfx, pl,
75                       pos);
76        pfx |= 1;
77        get_tree_codes(bits, lens, xlat, nodes, nodes[node].r, pfx, pl,
78                       pos);
79    }
80}
81
82static int build_vlc(AVCodecContext *avctx, VLC *vlc)
83{
84    MVHAContext *s = avctx->priv_data;
85    Node nodes[512];
86    uint32_t bits[256];
87    int16_t lens[256];
88    uint8_t xlat[256];
89    int cur_node, i, j, pos = 0;
90
91    ff_free_vlc(vlc);
92
93    for (i = 0; i < s->nb_symbols; i++) {
94        nodes[i].count = s->prob[i];
95        nodes[i].sym   = s->symb[i];
96        nodes[i].n0    = -2;
97        nodes[i].l     = i;
98        nodes[i].r     = i;
99    }
100
101    cur_node = s->nb_symbols;
102    j = 0;
103    do {
104        for (i = 0; ; i++) {
105            int new_node = j;
106            int first_node = cur_node;
107            int second_node = cur_node;
108            unsigned nd, st;
109
110            nodes[cur_node].count = -1;
111
112            do {
113                int val = nodes[new_node].count;
114                if (val && (val < nodes[first_node].count)) {
115                    if (val >= nodes[second_node].count) {
116                        first_node = new_node;
117                    } else {
118                        first_node = second_node;
119                        second_node = new_node;
120                    }
121                }
122                new_node += 1;
123            } while (new_node != cur_node);
124
125            if (first_node == cur_node)
126                break;
127
128            nd = nodes[second_node].count;
129            st = nodes[first_node].count;
130            nodes[second_node].count = 0;
131            nodes[first_node].count  = 0;
132            if (nd >= UINT32_MAX - st) {
133                av_log(avctx, AV_LOG_ERROR, "count overflow\n");
134                return AVERROR_INVALIDDATA;
135            }
136            nodes[cur_node].count = nd + st;
137            nodes[cur_node].sym = -1;
138            nodes[cur_node].n0 = cur_node;
139            nodes[cur_node].l = first_node;
140            nodes[cur_node].r = second_node;
141            cur_node++;
142        }
143        j++;
144    } while (cur_node - s->nb_symbols == j);
145
146    get_tree_codes(bits, lens, xlat, nodes, cur_node - 1, 0, 0, &pos);
147
148    return ff_init_vlc_sparse(vlc, 12, pos, lens, 2, 2, bits, 4, 4, xlat, 1, 1, 0);
149}
150
151static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
152                        int *got_frame, AVPacket *avpkt)
153{
154    MVHAContext *s = avctx->priv_data;
155    uint32_t type, size;
156    int ret;
157
158    if (avpkt->size <= 8)
159        return AVERROR_INVALIDDATA;
160
161    type = AV_RB32(avpkt->data);
162    size = AV_RL32(avpkt->data + 4);
163
164    if (size < 1 || size >= avpkt->size)
165        return AVERROR_INVALIDDATA;
166
167    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
168        return ret;
169
170    if (type == MKTAG('L','Z','Y','V')) {
171        z_stream *const zstream = &s->zstream.zstream;
172        ret = inflateReset(zstream);
173        if (ret != Z_OK) {
174            av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
175            return AVERROR_EXTERNAL;
176        }
177
178        zstream->next_in  = avpkt->data + 8;
179        zstream->avail_in = avpkt->size - 8;
180
181        for (int p = 0; p < 3; p++) {
182            for (int y = 0; y < avctx->height; y++) {
183                zstream->next_out  = frame->data[p] + (avctx->height - y - 1) * frame->linesize[p];
184                zstream->avail_out = avctx->width >> (p > 0);
185
186                ret = inflate(zstream, Z_SYNC_FLUSH);
187                if (ret != Z_OK && ret != Z_STREAM_END) {
188                    av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
189                    return AVERROR_EXTERNAL;
190                }
191                if (zsteam->avail_out > 0)
192                    memset(zstream->next_out, 0, zstream->avail_out);
193            }
194        }
195    } else if (type == MKTAG('H','U','F','Y')) {
196        GetBitContext *gb = &s->gb;
197        int first_symbol, symbol;
198
199        ret = init_get_bits8(gb, avpkt->data + 8, avpkt->size - 8);
200        if (ret < 0)
201            return ret;
202
203        skip_bits(gb, 24);
204
205        first_symbol = get_bits(gb, 8);
206        s->nb_symbols = get_bits(gb, 8) + 1;
207
208        symbol = first_symbol;
209        for (int i = 0; i < s->nb_symbols; symbol++) {
210            int prob;
211
212            if (get_bits_left(gb) < 4)
213                return AVERROR_INVALIDDATA;
214
215            if (get_bits1(gb)) {
216                prob = get_bits(gb, 12);
217            } else {
218                prob = get_bits(gb, 3);
219            }
220
221            if (prob) {
222                s->symb[i] = symbol;
223                s->prob[i] = prob;
224                i++;
225            }
226        }
227
228        ret = build_vlc(avctx, &s->vlc);
229        if (ret < 0)
230            return ret;
231
232        for (int p = 0; p < 3; p++) {
233            int width = avctx->width >> (p > 0);
234            ptrdiff_t stride = frame->linesize[p];
235            uint8_t *dst;
236
237            dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
238            for (int y = 0; y < avctx->height; y++) {
239                if (get_bits_left(gb) < width)
240                    return AVERROR_INVALIDDATA;
241                for (int x = 0; x < width; x++) {
242                    int v = get_vlc2(gb, s->vlc.table, s->vlc.bits, 3);
243
244                    if (v < 0)
245                        return AVERROR_INVALIDDATA;
246
247                    dst[x] = v;
248                }
249                dst -= stride;
250            }
251        }
252    } else {
253        return AVERROR_INVALIDDATA;
254    }
255
256    for (int p = 0; p < 3; p++) {
257        int left, lefttop;
258        int width = avctx->width >> (p > 0);
259        ptrdiff_t stride = frame->linesize[p];
260        uint8_t *dst;
261
262        dst = frame->data[p] + (avctx->height - 1) * frame->linesize[p];
263        s->llviddsp.add_left_pred(dst, dst, width, 0);
264        if (avctx->height > 1) {
265            dst -= stride;
266            lefttop = left = dst[0];
267            for (int y = 1; y < avctx->height; y++) {
268                s->llviddsp.add_median_pred(dst, dst + stride, dst, width, &left, &lefttop);
269                lefttop = left = dst[0];
270                dst -= stride;
271            }
272        }
273    }
274
275    frame->pict_type = AV_PICTURE_TYPE_I;
276    frame->key_frame = 1;
277    *got_frame = 1;
278
279    return avpkt->size;
280}
281
282static av_cold int decode_init(AVCodecContext *avctx)
283{
284    MVHAContext *s = avctx->priv_data;
285
286    avctx->pix_fmt = AV_PIX_FMT_YUV422P;
287
288    ff_llviddsp_init(&s->llviddsp);
289
290    return ff_inflate_init(&s->zstream, avctx);
291}
292
293static av_cold int decode_close(AVCodecContext *avctx)
294{
295    MVHAContext *s = avctx->priv_data;
296
297    ff_inflate_end(&s->zstream);
298    ff_free_vlc(&s->vlc);
299
300    return 0;
301}
302
303const FFCodec ff_mvha_decoder = {
304    .p.name           = "mvha",
305    .p.long_name      = NULL_IF_CONFIG_SMALL("MidiVid Archive Codec"),
306    .p.type           = AVMEDIA_TYPE_VIDEO,
307    .p.id             = AV_CODEC_ID_MVHA,
308    .priv_data_size   = sizeof(MVHAContext),
309    .init             = decode_init,
310    .close            = decode_close,
311    FF_CODEC_DECODE_CB(decode_frame),
312    .p.capabilities   = AV_CODEC_CAP_DR1,
313    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
314                        FF_CODEC_CAP_INIT_CLEANUP,
315};
316