FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/pdvdec.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 3 80 3.8%
Functions: 1 5 20.0%
Branches: 1 40 2.5%

Line Branch Exec 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 #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 7211 static int pdv_probe(const AVProbeData *pd)
37 {
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7211 times.
7211 if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
39 return AVPROBE_SCORE_MAX;
40 7211 return 0;
41 }
42
43 static int pdv_read_header(AVFormatContext *s)
44 {
45 PDVDemuxContext *p = s->priv_data;
46 AVIOContext *pb = s->pb;
47 AVCodecParameters *par;
48 AVStream *st;
49 uint64_t start;
50 uint32_t fps;
51
52 avio_skip(pb, 16);
53
54 st = avformat_new_stream(s, NULL);
55 if (!st)
56 return AVERROR(ENOMEM);
57
58 par = st->codecpar;
59 par->codec_type = AVMEDIA_TYPE_VIDEO;
60 par->codec_id = AV_CODEC_ID_PDV;
61 st->start_time = 0;
62 st->duration =
63 st->nb_frames = avio_rl16(pb);
64 avio_skip(pb, 2);
65 fps = avio_rl32(pb);
66 st->avg_frame_rate = av_d2q(av_int2float(fps), INT_MAX);
67 par->width = avio_rl16(pb);
68 par->height = avio_rl16(pb);
69
70 avpriv_set_pts_info(st, 64, st->avg_frame_rate.den, st->avg_frame_rate.num);
71
72 p->current_frame = 0;
73 p->frame_flags = av_calloc(st->nb_frames + 1, sizeof(*p->frame_flags));
74 p->frame_offsets = av_calloc(st->nb_frames + 1, sizeof(*p->frame_offsets));
75
76 if (!p->frame_flags || !p->frame_offsets)
77 return AVERROR(ENOMEM);
78
79 for (int n = 0; n <= st->nb_frames; n++) {
80 const uint32_t entry = avio_rl32(pb);
81
82 p->frame_flags[n] = entry & 3;
83 p->frame_offsets[n] = entry >> 2;
84 }
85
86 start = avio_tell(pb);
87
88 for (int n = 0; n < st->nb_frames; n++) {
89 const uint64_t pos = start + p->frame_offsets[n];
90 const int32_t size = p->frame_offsets[n+1] - p->frame_offsets[n];
91 const int flags = p->frame_flags[n] & 1 ? AVINDEX_KEYFRAME : 0;
92
93 if (p->frame_flags[n] == 0 || size <= 0 ||
94 ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)))
95 break;
96 av_add_index_entry(st, pos, n, size, 0, flags);
97 }
98
99 return 0;
100 }
101
102 static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
103 {
104 PDVDemuxContext *p = s->priv_data;
105 AVStream *st = s->streams[0];
106 FFStream *const sti = ffstream(st);
107 AVIOContext *pb = s->pb;
108 int32_t size, flags, ret;
109 int64_t pos;
110
111 if (p->current_frame >= st->nb_frames)
112 return AVERROR_EOF;
113
114 if (p->current_frame >= sti->nb_index_entries)
115 return AVERROR(EIO);
116
117 pos = sti->index_entries[p->current_frame].pos;
118 flags = sti->index_entries[p->current_frame].flags;
119 size = sti->index_entries[p->current_frame].size;
120
121 avio_seek(pb, pos, SEEK_SET);
122 if (avio_feof(pb) || ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)) || size == 0)
123 return AVERROR_EOF;
124
125 ret = av_get_packet(pb, pkt, size);
126 if (ret < 0)
127 return ret;
128
129 if (flags & AVINDEX_KEYFRAME)
130 pkt->flags |= AV_PKT_FLAG_KEY;
131 pkt->stream_index = 0;
132 pkt->pts = p->current_frame++;
133 pkt->duration = 1;
134
135 return 0;
136 }
137
138 static int pdv_read_close(AVFormatContext *s)
139 {
140 PDVDemuxContext *p = s->priv_data;
141
142 av_freep(&p->frame_flags);
143 av_freep(&p->frame_offsets);
144
145 return 0;
146 }
147
148 static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
149 {
150 PDVDemuxContext *p = s->priv_data;
151 AVStream *st = s->streams[stream_index];
152 int index = av_index_search_timestamp(st, timestamp, flags);
153
154 if (index < 0)
155 return -1;
156
157 if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
158 return -1;
159
160 p->current_frame = index;
161
162 return 0;
163 }
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 };
177