FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/pdvdec.c
Date: 2024-05-03 15:42:48
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/mem.h"
23 #include "avformat.h"
24 #include "demux.h"
25 #include "internal.h"
26
27 #define PDV_MAGIC "Playdate VID\x00\x00\x00\x00"
28
29 typedef struct PDVDemuxContext {
30 int current_frame;
31 uint8_t *frame_flags;
32 uint32_t *frame_offsets;
33 } PDVDemuxContext;
34
35 7128 static int pdv_probe(const AVProbeData *pd)
36 {
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7128 times.
7128 if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
38 return AVPROBE_SCORE_MAX;
39 7128 return 0;
40 }
41
42 static int pdv_read_header(AVFormatContext *s)
43 {
44 PDVDemuxContext *p = s->priv_data;
45 AVIOContext *pb = s->pb;
46 AVCodecParameters *par;
47 AVStream *st;
48 uint64_t start;
49 uint32_t fps;
50
51 avio_skip(pb, 16);
52
53 st = avformat_new_stream(s, NULL);
54 if (!st)
55 return AVERROR(ENOMEM);
56
57 par = st->codecpar;
58 par->codec_type = AVMEDIA_TYPE_VIDEO;
59 par->codec_id = AV_CODEC_ID_PDV;
60 st->start_time = 0;
61 st->duration =
62 st->nb_frames = avio_rl16(pb);
63 avio_skip(pb, 2);
64 fps = avio_rl32(pb);
65 st->avg_frame_rate = av_d2q(av_int2float(fps), INT_MAX);
66 par->width = avio_rl16(pb);
67 par->height = avio_rl16(pb);
68
69 avpriv_set_pts_info(st, 64, st->avg_frame_rate.den, st->avg_frame_rate.num);
70
71 p->current_frame = 0;
72 p->frame_flags = av_calloc(st->nb_frames + 1, sizeof(*p->frame_flags));
73 p->frame_offsets = av_calloc(st->nb_frames + 1, sizeof(*p->frame_offsets));
74
75 if (!p->frame_flags || !p->frame_offsets)
76 return AVERROR(ENOMEM);
77
78 for (int n = 0; n <= st->nb_frames; n++) {
79 const uint32_t entry = avio_rl32(pb);
80
81 p->frame_flags[n] = entry & 3;
82 p->frame_offsets[n] = entry >> 2;
83 }
84
85 start = avio_tell(pb);
86
87 for (int n = 0; n < st->nb_frames; n++) {
88 const uint64_t pos = start + p->frame_offsets[n];
89 const int32_t size = p->frame_offsets[n+1] - p->frame_offsets[n];
90 const int flags = p->frame_flags[n] & 1 ? AVINDEX_KEYFRAME : 0;
91
92 if (p->frame_flags[n] == 0 || size <= 0 ||
93 ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)))
94 break;
95 av_add_index_entry(st, pos, n, size, 0, flags);
96 }
97
98 return 0;
99 }
100
101 static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
102 {
103 PDVDemuxContext *p = s->priv_data;
104 AVStream *st = s->streams[0];
105 FFStream *const sti = ffstream(st);
106 AVIOContext *pb = s->pb;
107 int32_t size, flags, ret;
108 int64_t pos;
109
110 if (p->current_frame >= st->nb_frames)
111 return AVERROR_EOF;
112
113 if (p->current_frame >= sti->nb_index_entries)
114 return AVERROR(EIO);
115
116 pos = sti->index_entries[p->current_frame].pos;
117 flags = sti->index_entries[p->current_frame].flags;
118 size = sti->index_entries[p->current_frame].size;
119
120 avio_seek(pb, pos, SEEK_SET);
121 if (avio_feof(pb) || ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)) || size == 0)
122 return AVERROR_EOF;
123
124 ret = av_get_packet(pb, pkt, size);
125 if (ret < 0)
126 return ret;
127
128 if (flags & AVINDEX_KEYFRAME)
129 pkt->flags |= AV_PKT_FLAG_KEY;
130 pkt->stream_index = 0;
131 pkt->pts = p->current_frame++;
132 pkt->duration = 1;
133
134 return 0;
135 }
136
137 static int pdv_read_close(AVFormatContext *s)
138 {
139 PDVDemuxContext *p = s->priv_data;
140
141 av_freep(&p->frame_flags);
142 av_freep(&p->frame_offsets);
143
144 return 0;
145 }
146
147 static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
148 {
149 PDVDemuxContext *p = s->priv_data;
150 AVStream *st = s->streams[stream_index];
151 int index = av_index_search_timestamp(st, timestamp, flags);
152
153 if (index < 0)
154 return -1;
155
156 if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
157 return -1;
158
159 p->current_frame = index;
160
161 return 0;
162 }
163
164 const FFInputFormat ff_pdv_demuxer = {
165 .p.name = "pdv",
166 .p.long_name = NULL_IF_CONFIG_SMALL("PlayDate Video"),
167 .p.extensions = "pdv",
168 .priv_data_size = sizeof(PDVDemuxContext),
169 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
170 .read_probe = pdv_probe,
171 .read_header = pdv_read_header,
172 .read_packet = pdv_read_packet,
173 .read_close = pdv_read_close,
174 .read_seek = pdv_read_seek,
175 };
176