FFmpeg
Loading...
Searching...
No Matches
pdvdec.c
Go to the documentation of this file.
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
30typedef struct PDVDemuxContext {
32 uint8_t *frame_flags;
33 uint32_t *frame_offsets;
35
36static int pdv_probe(const AVProbeData *pd)
37{
38 if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
39 return AVPROBE_SCORE_MAX;
40 return 0;
41}
42
44{
45 PDVDemuxContext *p = s->priv_data;
46 AVIOContext *pb = s->pb;
48 AVStream *st;
49 uint64_t start;
50 uint32_t fps;
51
52 avio_skip(pb, 16);
53
55 if (!st)
56 return AVERROR(ENOMEM);
57
58 par = st->codecpar;
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
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
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_INVALIDDATA;
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
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
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
148static 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
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,
176};
const FFInputFormat ff_pdv_demuxer
Definition pdvdec.c:165
#define entry
int32_t
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PDV
Definition codec_id.h:315
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition rational.c:110
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int index
Definition gxfenc.c:90
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition intfloat.h:40
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition pdvdec.c:102
static int pdv_read_close(AVFormatContext *s)
Definition pdvdec.c:138
static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition pdvdec.c:148
static int pdv_probe(const AVProbeData *pd)
Definition pdvdec.c:36
static int pdv_read_header(AVFormatContext *s)
Definition pdvdec.c:43
#define PDV_MAGIC
Definition pdvdec.c:28
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
unsigned int pos
Definition spdifenc.c:431
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
int64_t pos
Definition avformat.h:621
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition avformat.h:473
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
int nb_index_entries
Definition internal.h:193
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
uint8_t * frame_flags
Definition pdvdec.c:32
int current_frame
Definition pdvdec.c:31
uint32_t * frame_offsets
Definition pdvdec.c:33
#define av_freep(p)
int size