Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/pdvdec.c
Line
Count
Source
1
/*
2
 * PDV video format
3
 *
4
 * Copyright (c) 2023 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 "avcodec.h"
24
#include "codec_internal.h"
25
#include "decode.h"
26
#include "zlib_wrapper.h"
27
28
#include "libavutil/attributes.h"
29
30
#include <zlib.h>
31
32
typedef struct PDVContext {
33
    AVFrame  *previous_frame;
34
    FFZStream zstream;
35
} PDVContext;
36
37
static av_cold int decode_init(AVCodecContext *avctx)
38
1.47k
{
39
1.47k
    PDVContext *s = avctx->priv_data;
40
41
1.47k
    avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
42
43
1.47k
    s->previous_frame = av_frame_alloc();
44
1.47k
    if (!s->previous_frame)
45
0
        return AVERROR(ENOMEM);
46
47
1.47k
    return ff_inflate_init(&s->zstream, avctx);
48
1.47k
}
49
50
static av_cold int decode_end(AVCodecContext *avctx)
51
1.47k
{
52
1.47k
    PDVContext *s = avctx->priv_data;
53
54
1.47k
    av_frame_free(&s->previous_frame);
55
1.47k
    ff_inflate_end(&s->zstream);
56
57
1.47k
    return 0;
58
1.47k
}
59
60
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
61
                        int *got_frame, AVPacket *avpkt)
62
314k
{
63
314k
    PDVContext *s = avctx->priv_data;
64
314k
    AVFrame *prev_frame = s->previous_frame;
65
314k
    z_stream *const zstream = &s->zstream.zstream;
66
314k
    uint8_t *dst, *prev = prev_frame->data[0];
67
314k
    int ret, zret;
68
69
314k
    if (avctx->skip_frame >= AVDISCARD_ALL ||
70
203k
        (avctx->skip_frame >= AVDISCARD_NONINTRA &&
71
23.7k
         !(avpkt->flags & AV_PKT_FLAG_KEY)))
72
128k
        return avpkt->size;
73
74
185k
    zret = inflateReset(zstream);
75
185k
    if (zret != Z_OK) {
76
0
        av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret);
77
0
        return AVERROR_INVALIDDATA;
78
0
    }
79
80
185k
    if (avpkt->size * 1032LL < ((avctx->width + 7) >> 3) * avctx->height) //Asymptotic max compression of deflate
81
133k
        return AVERROR_INVALIDDATA;
82
83
52.4k
    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
84
9.80k
        return ret;
85
86
42.6k
    zstream->next_in  = avpkt->data;
87
42.6k
    zstream->avail_in = avpkt->size;
88
89
42.6k
    dst = frame->data[0];
90
9.77M
    for (int i = 0; i < avctx->height; i++) {
91
9.75M
        zstream->next_out  = dst;
92
9.75M
        zstream->avail_out = (avctx->width + 7) >> 3;
93
94
9.75M
        zret = inflate(zstream, Z_SYNC_FLUSH);
95
9.75M
        if (zret != Z_OK && zret != Z_STREAM_END) {
96
28.1k
            av_log(avctx, AV_LOG_ERROR,
97
28.1k
                   "Inflate failed with return code: %d.\n", zret);
98
28.1k
            return AVERROR_INVALIDDATA;
99
28.1k
        }
100
101
9.72M
        if (prev && !(avpkt->flags & AV_PKT_FLAG_KEY)) {
102
35.4M
            for (int j = 0; j < (avctx->width + 7) >> 3; j++)
103
34.6M
                dst[j] ^= prev[j];
104
821k
            prev += prev_frame->linesize[0];
105
821k
        }
106
107
9.72M
        dst += frame->linesize[0];
108
9.72M
    }
109
110
14.5k
    if ((ret = av_frame_replace(s->previous_frame, frame)) < 0)
111
0
        return ret;
112
113
14.5k
    if (avpkt->flags & AV_PKT_FLAG_KEY) {
114
4.06k
        frame->flags |= AV_FRAME_FLAG_KEY;
115
4.06k
        frame->pict_type = AV_PICTURE_TYPE_I;
116
10.4k
    } else {
117
10.4k
        frame->pict_type = AV_PICTURE_TYPE_P;
118
10.4k
    }
119
120
14.5k
    *got_frame       = 1;
121
122
14.5k
    return avpkt->size;
123
14.5k
}
124
125
static av_cold void decode_flush(AVCodecContext *avctx)
126
133k
{
127
133k
    PDVContext *s = avctx->priv_data;
128
129
133k
    av_frame_unref(s->previous_frame);
130
133k
}
131
132
const FFCodec ff_pdv_decoder = {
133
    .p.name         = "pdv",
134
    CODEC_LONG_NAME("PDV (PlayDate Video)"),
135
    .priv_data_size = sizeof(PDVContext),
136
    .p.type         = AVMEDIA_TYPE_VIDEO,
137
    .p.id           = AV_CODEC_ID_PDV,
138
    .p.capabilities = AV_CODEC_CAP_DR1,
139
    .caps_internal  = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
140
                      FF_CODEC_CAP_INIT_CLEANUP,
141
    .init           = decode_init,
142
    .close          = decode_end,
143
    .flush          = decode_flush,
144
    FF_CODEC_DECODE_CB(decode_frame),
145
};