FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/pdvenc.c
Date: 2026-09-20 22:07:40
Exec Total Coverage
Lines: 69 98 70.4%
Functions: 5 5 100.0%
Branches: 32 60 53.3%

Line Branch Exec Source
1 /*
2 * PDV muxer
3 *
4 * Copyright (c) 2026 Priyanshu Thapliyal <priyanshuthapliyal2005@gmail.com>
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 "libavutil/mem.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/rational.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "mux.h"
29
30 #define PDV_MAGIC "Playdate VID\x00\x00\x00\x00"
31 #define PDV_MAX_FRAMES UINT16_MAX
32 #define PDV_MAX_OFFSET ((1U << 30) - 1)
33
34 typedef struct PDVMuxContext {
35 const AVClass *class;
36 uint32_t *entries;
37 int nb_frames;
38 int max_frames;
39 uint32_t fps_bits;
40 int64_t nb_frames_pos;
41 int64_t table_pos;
42 int64_t payload_start;
43 } PDVMuxContext;
44
45 2 static void pdv_deinit(AVFormatContext *s)
46 {
47 2 PDVMuxContext *pdv = s->priv_data;
48
49 2 av_freep(&pdv->entries);
50 2 }
51
52 2 static int pdv_get_fps(AVFormatContext *s, AVStream *st, uint32_t *fps_bits)
53 {
54 2 AVRational rate = st->avg_frame_rate;
55 2 const AVRational zero = { 0, 1 };
56
57
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!rate.num || !rate.den)
58 rate = av_inv_q(st->time_base);
59
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!rate.num || !rate.den) {
60 av_log(s, AV_LOG_ERROR, "A valid frame rate is required for PDV output.\n");
61 return AVERROR(EINVAL);
62 }
63
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (av_cmp_q(rate, zero) <= 0) {
65 av_log(s, AV_LOG_ERROR, "Invalid frame rate for PDV output.\n");
66 return AVERROR(EINVAL);
67 }
68
69 2 *fps_bits = av_q2intfloat(rate);
70 2 return 0;
71 }
72
73 2 static int pdv_write_header(AVFormatContext *s)
74 {
75 2 PDVMuxContext *pdv = s->priv_data;
76 AVStream *st;
77 int ret;
78
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
80 av_log(s, AV_LOG_ERROR, "PDV muxer requires seekable output.\n");
81 return AVERROR(EINVAL);
82 }
83
84 2 st = s->streams[0];
85
86
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (st->codecpar->width > UINT16_MAX || st->codecpar->height > UINT16_MAX) {
87 av_log(s, AV_LOG_ERROR, "Output dimensions exceed PDV limits.\n");
88 return AVERROR(EINVAL);
89 }
90
91 2 ret = pdv_get_fps(s, st, &pdv->fps_bits);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
93 return ret;
94
95
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (pdv->max_frames < 1 || pdv->max_frames > PDV_MAX_FRAMES) {
96 av_log(s, AV_LOG_ERROR,
97 "The -max_frames option must be set to a value in [1, %u].\n",
98 PDV_MAX_FRAMES);
99 return AVERROR(EINVAL);
100 }
101
102 2 pdv->entries = av_malloc_array(pdv->max_frames + 1, sizeof(*pdv->entries));
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!pdv->entries)
104 return AVERROR(ENOMEM);
105
106 2 avio_write(s->pb, PDV_MAGIC, 16);
107 2 pdv->nb_frames_pos = avio_tell(s->pb);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (pdv->nb_frames_pos < 0)
109 return pdv->nb_frames_pos;
110 2 avio_wl16(s->pb, 0);
111 2 avio_wl16(s->pb, 0);
112 2 avio_wl32(s->pb, pdv->fps_bits);
113 2 avio_wl16(s->pb, st->codecpar->width);
114 2 avio_wl16(s->pb, st->codecpar->height);
115
116 2 pdv->table_pos = avio_tell(s->pb);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (pdv->table_pos < 0)
118 return pdv->table_pos;
119 2 ffio_fill(s->pb, 0, 4LL * (pdv->max_frames + 1));
120 2 pdv->payload_start = avio_tell(s->pb);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (pdv->payload_start < 0)
122 return pdv->payload_start;
123
124 2 return 0;
125 }
126
127 6 static int pdv_write_packet(AVFormatContext *s, AVPacket *pkt)
128 {
129 6 PDVMuxContext *pdv = s->priv_data;
130 6 int64_t offset = avio_tell(s->pb);
131 6 const uint32_t max_table_gap = 4U * pdv->max_frames;
132
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (offset < 0)
134 return offset;
135 6 offset -= pdv->payload_start;
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (offset < 0)
137 return AVERROR(EINVAL);
138
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pkt->size <= 0)
140 return AVERROR_INVALIDDATA;
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pdv->nb_frames >= pdv->max_frames) {
142 av_log(s, AV_LOG_ERROR, "Too many frames for PDV output.\n");
143 return AVERROR(EINVAL);
144 }
145
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (offset > PDV_MAX_OFFSET - max_table_gap ||
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 pkt->size > PDV_MAX_OFFSET - max_table_gap - offset) {
147 av_log(s, AV_LOG_ERROR, "PDV payload exceeds container limits.\n");
148 return AVERROR(EINVAL);
149 }
150
151 12 pdv->entries[pdv->nb_frames] = ((uint32_t)offset << 2) |
152
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 (pkt->flags & AV_PKT_FLAG_KEY ? 1 : 2);
153 6 avio_write(s->pb, pkt->data, pkt->size);
154
155 6 pdv->nb_frames++;
156
157 6 return 0;
158 }
159
160 2 static int pdv_write_trailer(AVFormatContext *s)
161 {
162 2 PDVMuxContext *pdv = s->priv_data;
163 2 int64_t payload_size = avio_tell(s->pb);
164 2 const uint32_t table_gap = 4U * (pdv->max_frames - pdv->nb_frames);
165 int64_t ret;
166
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (payload_size < 0)
168 return payload_size;
169 2 payload_size -= pdv->payload_start;
170
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (payload_size < 0 || payload_size > PDV_MAX_OFFSET - table_gap)
171 return AVERROR(EINVAL);
172
173 2 pdv->entries[pdv->nb_frames] = (uint32_t)payload_size << 2;
174
175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = avio_seek(s->pb, pdv->nb_frames_pos, SEEK_SET)) < 0)
176 return ret;
177 2 avio_wl16(s->pb, pdv->nb_frames);
178
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = avio_seek(s->pb, pdv->table_pos, SEEK_SET)) < 0)
180 return ret;
181
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (int i = 0; i <= pdv->nb_frames; i++) {
182 8 const uint32_t frame_off = (pdv->entries[i] >> 2) + table_gap;
183
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (frame_off > PDV_MAX_OFFSET)
185 return AVERROR(EINVAL);
186 8 avio_wl32(s->pb, frame_off << 2 | (pdv->entries[i] & 3));
187 }
188
189
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = avio_seek(s->pb, pdv->payload_start + payload_size, SEEK_SET)) < 0)
190 return ret;
191
192 2 return 0;
193 }
194
195 #define OFFSET(x) offsetof(PDVMuxContext, x)
196 #define ENC AV_OPT_FLAG_ENCODING_PARAM
197 static const AVOption options[] = {
198 { "max_frames", "maximum number of frames reserved in table (mandatory)",
199 OFFSET(max_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, PDV_MAX_FRAMES, ENC },
200 { NULL },
201 };
202
203 static const AVClass pdv_muxer_class = {
204 .class_name = "PDV muxer",
205 .item_name = av_default_item_name,
206 .option = options,
207 .version = LIBAVUTIL_VERSION_INT,
208 .category = AV_CLASS_CATEGORY_MUXER,
209 };
210
211 const FFOutputFormat ff_pdv_muxer = {
212 .p.name = "pdv",
213 .p.long_name = NULL_IF_CONFIG_SMALL("PlayDate Video"),
214 .p.extensions = "pdv",
215 .p.priv_class = &pdv_muxer_class,
216 .p.audio_codec = AV_CODEC_ID_NONE,
217 .p.video_codec = AV_CODEC_ID_PDV,
218 .p.subtitle_codec = AV_CODEC_ID_NONE,
219 .priv_data_size = sizeof(PDVMuxContext),
220 .p.flags = AVFMT_NOTIMESTAMPS,
221 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
222 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
223 .write_header = pdv_write_header,
224 .write_packet = pdv_write_packet,
225 .write_trailer = pdv_write_trailer,
226 .deinit = pdv_deinit,
227 };
228