Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/pdvdec.c
Line
Count
Source
1
/*
2
 * PDV demuxer
3
 * Copyright (c) 2023 Paul B Mahol
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include "libavutil/intfloat.h"
23
#include "libavutil/mem.h"
24
#include "avformat.h"
25
#include "demux.h"
26
#include "internal.h"
27
28
1.92M
#define PDV_MAGIC "Playdate VID\x00\x00\x00\x00"
29
30
typedef struct PDVDemuxContext {
31
    int current_frame;
32
    uint8_t *frame_flags;
33
    uint32_t *frame_offsets;
34
} PDVDemuxContext;
35
36
static int pdv_probe(const AVProbeData *pd)
37
964k
{
38
964k
    if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
39
176
        return AVPROBE_SCORE_MAX;
40
964k
    return 0;
41
964k
}
42
43
static int pdv_read_header(AVFormatContext *s)
44
1.88k
{
45
1.88k
    PDVDemuxContext *p = s->priv_data;
46
1.88k
    AVIOContext *pb = s->pb;
47
1.88k
    AVCodecParameters *par;
48
1.88k
    AVStream *st;
49
1.88k
    uint64_t start;
50
1.88k
    uint32_t fps;
51
52
1.88k
    avio_skip(pb, 16);
53
54
1.88k
    st = avformat_new_stream(s, NULL);
55
1.88k
    if (!st)
56
0
        return AVERROR(ENOMEM);
57
58
1.88k
    par              = st->codecpar;
59
1.88k
    par->codec_type  = AVMEDIA_TYPE_VIDEO;
60
1.88k
    par->codec_id    = AV_CODEC_ID_PDV;
61
1.88k
    st->start_time   = 0;
62
1.88k
    st->duration     =
63
1.88k
    st->nb_frames    = avio_rl16(pb);
64
1.88k
    avio_skip(pb, 2);
65
1.88k
    fps = avio_rl32(pb);
66
1.88k
    st->avg_frame_rate = av_d2q(av_int2float(fps), INT_MAX);
67
1.88k
    par->width       = avio_rl16(pb);
68
1.88k
    par->height      = avio_rl16(pb);
69
70
1.88k
    avpriv_set_pts_info(st, 64, st->avg_frame_rate.den, st->avg_frame_rate.num);
71
72
1.88k
    p->current_frame = 0;
73
1.88k
    p->frame_flags = av_calloc(st->nb_frames + 1, sizeof(*p->frame_flags));
74
1.88k
    p->frame_offsets = av_calloc(st->nb_frames + 1, sizeof(*p->frame_offsets));
75
76
1.88k
    if (!p->frame_flags || !p->frame_offsets)
77
0
        return AVERROR(ENOMEM);
78
79
24.8M
    for (int n = 0; n <= st->nb_frames; n++) {
80
24.8M
        const uint32_t entry = avio_rl32(pb);
81
82
24.8M
        p->frame_flags[n] = entry & 3;
83
24.8M
        p->frame_offsets[n] = entry >> 2;
84
24.8M
    }
85
86
1.88k
    start = avio_tell(pb);
87
88
22.4k
    for (int n = 0; n < st->nb_frames; n++) {
89
21.7k
        const uint64_t pos = start + p->frame_offsets[n];
90
21.7k
        const int32_t size = p->frame_offsets[n+1] - p->frame_offsets[n];
91
21.7k
        const int flags = p->frame_flags[n] & 1 ? AVINDEX_KEYFRAME : 0;
92
93
21.7k
        if (p->frame_flags[n] == 0 || size <= 0 ||
94
20.5k
            ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)))
95
1.19k
            break;
96
20.5k
        av_add_index_entry(st, pos, n, size, 0, flags);
97
20.5k
    }
98
99
1.88k
    return 0;
100
1.88k
}
101
102
static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
103
7.56k
{
104
7.56k
    PDVDemuxContext *p = s->priv_data;
105
7.56k
    AVStream *st = s->streams[0];
106
7.56k
    FFStream *const sti = ffstream(st);
107
7.56k
    AVIOContext *pb = s->pb;
108
7.56k
    int32_t size, flags, ret;
109
7.56k
    int64_t pos;
110
111
7.56k
    if (p->current_frame >= st->nb_frames)
112
872
        return AVERROR_EOF;
113
114
6.69k
    if (p->current_frame >= sti->nb_index_entries)
115
1.54k
        return AVERROR_INVALIDDATA;
116
117
5.15k
    pos   = sti->index_entries[p->current_frame].pos;
118
5.15k
    flags = sti->index_entries[p->current_frame].flags;
119
5.15k
    size  = sti->index_entries[p->current_frame].size;
120
121
5.15k
    avio_seek(pb, pos, SEEK_SET);
122
5.15k
    if (avio_feof(pb) || ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)) || size == 0)
123
656
        return AVERROR_EOF;
124
125
4.49k
    ret = av_get_packet(pb, pkt, size);
126
4.49k
    if (ret < 0)
127
549
        return ret;
128
129
3.94k
    if (flags & AVINDEX_KEYFRAME)
130
3.39k
        pkt->flags |= AV_PKT_FLAG_KEY;
131
3.94k
    pkt->stream_index = 0;
132
3.94k
    pkt->pts = p->current_frame++;
133
3.94k
    pkt->duration = 1;
134
135
3.94k
    return 0;
136
4.49k
}
137
138
static int pdv_read_close(AVFormatContext *s)
139
1.88k
{
140
1.88k
    PDVDemuxContext *p = s->priv_data;
141
142
1.88k
    av_freep(&p->frame_flags);
143
1.88k
    av_freep(&p->frame_offsets);
144
145
1.88k
    return 0;
146
1.88k
}
147
148
static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
149
0
{
150
0
    PDVDemuxContext *p = s->priv_data;
151
0
    AVStream *st = s->streams[stream_index];
152
0
    int index = av_index_search_timestamp(st, timestamp, flags);
153
154
0
    if (index < 0)
155
0
        return -1;
156
157
0
    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
158
0
        return -1;
159
160
0
    p->current_frame = index;
161
162
0
    return 0;
163
0
}
164
165
const FFInputFormat ff_pdv_demuxer = {
166
    .p.name         = "pdv",
167
    .p.long_name    = NULL_IF_CONFIG_SMALL("PlayDate Video"),
168
    .p.extensions   = "pdv",
169
    .priv_data_size = sizeof(PDVDemuxContext),
170
    .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
171
    .read_probe     = pdv_probe,
172
    .read_header    = pdv_read_header,
173
    .read_packet    = pdv_read_packet,
174
    .read_close     = pdv_read_close,
175
    .read_seek      = pdv_read_seek,
176
};