summaryrefslogtreecommitdiff
path: root/libavcodec/pdvdec.c
blob: d50c4e729c8dd80f189b7d9f3a346e5e6e85a1a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * PDV video format
 *
 * Copyright (c) 2023 Paul B Mahol
 *
 * 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 "avcodec.h"
#include "codec_internal.h"
#include "decode.h"
#include "zlib_wrapper.h"

#include <zlib.h>

typedef struct PDVContext {
    AVFrame  *previous_frame;
    FFZStream zstream;
} PDVContext;

static av_cold int decode_init(AVCodecContext *avctx)
{
    PDVContext *s = avctx->priv_data;

    avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;

    s->previous_frame = av_frame_alloc();
    if (!s->previous_frame)
        return AVERROR(ENOMEM);

    return ff_inflate_init(&s->zstream, avctx);
}

static av_cold int decode_end(AVCodecContext *avctx)
{
    PDVContext *s = avctx->priv_data;

    av_frame_free(&s->previous_frame);
    ff_inflate_end(&s->zstream);

    return 0;
}

static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
                        int *got_frame, AVPacket *avpkt)
{
    PDVContext *s = avctx->priv_data;
    AVFrame *prev_frame = s->previous_frame;
    z_stream *const zstream = &s->zstream.zstream;
    uint8_t *dst, *prev = prev_frame->data[0];
    int ret, zret;

    if (avctx->skip_frame >= AVDISCARD_ALL ||
        (avctx->skip_frame >= AVDISCARD_NONINTRA &&
         !(avpkt->flags & AV_PKT_FLAG_KEY)))
        return avpkt->size;

    zret = inflateReset(zstream);
    if (zret != Z_OK) {
        av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret);
        return AVERROR_INVALIDDATA;
    }

    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
        return ret;

    zstream->next_in  = avpkt->data;
    zstream->avail_in = avpkt->size;

    dst = frame->data[0];
    for (int i = 0; i < avctx->height; i++) {
        zstream->next_out  = dst;
        zstream->avail_out = (avctx->width + 7) >> 3;

        zret = inflate(zstream, Z_SYNC_FLUSH);
        if (zret != Z_OK && zret != Z_STREAM_END) {
            av_log(avctx, AV_LOG_ERROR,
                   "Inflate failed with return code: %d.\n", zret);
            return AVERROR_INVALIDDATA;
        }

        if (prev && !(avpkt->flags & AV_PKT_FLAG_KEY)) {
            for (int j = 0; j < (avctx->width + 7) >> 3; j++)
                dst[j] ^= prev[j];
            prev += prev_frame->linesize[0];
        }

        dst += frame->linesize[0];
    }

    av_frame_unref(s->previous_frame);
    if ((ret = av_frame_ref(s->previous_frame, frame)) < 0)
        return ret;

    if (avpkt->flags & AV_PKT_FLAG_KEY) {
        frame->flags |= AV_FRAME_FLAG_KEY;
        frame->pict_type = AV_PICTURE_TYPE_I;
    } else {
        frame->pict_type = AV_PICTURE_TYPE_P;
    }

    *got_frame       = 1;

    return avpkt->size;
}

static void decode_flush(AVCodecContext *avctx)
{
    PDVContext *s = avctx->priv_data;

    av_frame_unref(s->previous_frame);
}

const FFCodec ff_pdv_decoder = {
    .p.name         = "pdv",
    CODEC_LONG_NAME("PDV (PlayDate Video)"),
    .priv_data_size = sizeof(PDVContext),
    .p.type         = AVMEDIA_TYPE_VIDEO,
    .p.id           = AV_CODEC_ID_PDV,
    .p.capabilities = AV_CODEC_CAP_DR1,
    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
                      FF_CODEC_CAP_INIT_CLEANUP,
    .init           = decode_init,
    .close          = decode_end,
    .flush          = decode_flush,
    FF_CODEC_DECODE_CB(decode_frame),
};