FFmpeg
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 "avformat.h"
23 #include "internal.h"
24 
25 #define PDV_MAGIC "Playdate VID\x00\x00\x00\x00"
26 
27 typedef struct PDVDemuxContext {
29  uint8_t *frame_flags;
30  uint32_t *frame_offsets;
32 
33 static int pdv_probe(const AVProbeData *pd)
34 {
35  if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
36  return AVPROBE_SCORE_MAX;
37  return 0;
38 }
39 
41 {
42  PDVDemuxContext *p = s->priv_data;
43  AVIOContext *pb = s->pb;
44  AVCodecParameters *par;
45  AVStream *st;
46  uint64_t start;
47  uint32_t fps;
48 
49  avio_skip(pb, 16);
50 
51  st = avformat_new_stream(s, NULL);
52  if (!st)
53  return AVERROR(ENOMEM);
54 
55  par = st->codecpar;
58  st->start_time = 0;
59  st->duration =
60  st->nb_frames = avio_rl16(pb);
61  avio_skip(pb, 2);
62  fps = avio_rl32(pb);
63  st->avg_frame_rate = av_d2q(av_int2float(fps), INT_MAX);
64  par->width = avio_rl16(pb);
65  par->height = avio_rl16(pb);
66 
68 
69  p->current_frame = 0;
70  p->frame_flags = av_calloc(st->nb_frames + 1, sizeof(*p->frame_flags));
71  p->frame_offsets = av_calloc(st->nb_frames + 1, sizeof(*p->frame_offsets));
72 
73  if (!p->frame_flags || !p->frame_offsets)
74  return AVERROR(ENOMEM);
75 
76  for (int n = 0; n <= st->nb_frames; n++) {
77  const uint32_t entry = avio_rl32(pb);
78 
79  p->frame_flags[n] = entry & 3;
80  p->frame_offsets[n] = entry >> 2;
81  }
82 
83  start = avio_tell(pb);
84 
85  for (int n = 0; n < st->nb_frames; n++) {
86  const uint64_t pos = start + p->frame_offsets[n];
87  const int32_t size = p->frame_offsets[n+1] - p->frame_offsets[n];
88  const int flags = p->frame_flags[n] & 1 ? AVINDEX_KEYFRAME : 0;
89 
90  if (p->frame_flags[n] == 0 || size <= 0 ||
91  ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)))
92  break;
93  av_add_index_entry(st, pos, n, size, 0, flags);
94  }
95 
96  return 0;
97 }
98 
100 {
101  PDVDemuxContext *p = s->priv_data;
102  AVStream *st = s->streams[0];
103  FFStream *const sti = ffstream(st);
104  AVIOContext *pb = s->pb;
105  int32_t size, flags, ret;
106  int64_t pos;
107 
108  if (p->current_frame >= st->nb_frames)
109  return AVERROR_EOF;
110 
111  if (p->current_frame >= sti->nb_index_entries)
112  return AVERROR(EIO);
113 
114  pos = sti->index_entries[p->current_frame].pos;
117 
118  avio_seek(pb, pos, SEEK_SET);
119  if (avio_feof(pb) || ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)) || size == 0)
120  return AVERROR_EOF;
121 
122  ret = av_get_packet(pb, pkt, size);
123  if (ret < 0)
124  return ret;
125 
126  if (flags & AVINDEX_KEYFRAME)
128  pkt->stream_index = 0;
129  pkt->pts = p->current_frame++;
130  pkt->duration = 1;
131 
132  return 0;
133 }
134 
136 {
137  PDVDemuxContext *p = s->priv_data;
138 
139  av_freep(&p->frame_flags);
140  av_freep(&p->frame_offsets);
141 
142  return 0;
143 }
144 
145 static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
146 {
147  PDVDemuxContext *p = s->priv_data;
148  AVStream *st = s->streams[stream_index];
149  int index = av_index_search_timestamp(st, timestamp, flags);
150 
151  if (index < 0)
152  return -1;
153 
154  if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
155  return -1;
156 
157  p->current_frame = index;
158 
159  return 0;
160 }
161 
163  .name = "pdv",
164  .long_name = NULL_IF_CONFIG_SMALL("PlayDate Video"),
165  .priv_data_size = sizeof(PDVDemuxContext),
166  .flags_internal = FF_FMT_INIT_CLEANUP,
172  .extensions = "pdv",
173 };
PDVDemuxContext::current_frame
int current_frame
Definition: pdvdec.c:28
FF_FMT_INIT_CLEANUP
#define FF_FMT_INIT_CLEANUP
For an AVInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: internal.h:46
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
pdv_read_header
static int pdv_read_header(AVFormatContext *s)
Definition: pdvdec.c:40
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:930
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:370
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:577
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:708
PDVDemuxContext::frame_flags
uint8_t * frame_flags
Definition: pdvdec.c:29
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:464
avpriv_set_pts_info
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:843
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:423
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
av_add_index_entry
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:120
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:513
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:900
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:761
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:549
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:554
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:454
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:121
AVIndexEntry::size
int size
Definition: avformat.h:711
pdv_read_close
static int pdv_read_close(AVFormatContext *s)
Definition: pdvdec.c:135
AVFormatContext
Format I/O context.
Definition: avformat.h:1363
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
AVIndexEntry::flags
int flags
Definition: avformat.h:710
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:257
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:452
AV_CODEC_ID_PDV
@ AV_CODEC_ID_PDV
Definition: codec_id.h:323
index
int index
Definition: gxfenc.c:89
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:902
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:777
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:271
FFStream
Definition: internal.h:199
size
int size
Definition: twinvq_data.h:10344
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
PDV_MAGIC
#define PDV_MAGIC
Definition: pdvdec.c:25
AVCodecParameters::height
int height
Definition: codec_par.h:122
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
pdv_probe
static int pdv_probe(const AVProbeData *pd)
Definition: pdvdec.c:33
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
av_get_packet
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:103
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:841
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:278
PDVDemuxContext::frame_offsets
uint32_t * frame_offsets
Definition: pdvdec.c:30
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
AVRational::den
int den
Denominator.
Definition: rational.h:60
pdv_read_seek
static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pdvdec.c:145
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:701
AVPacket::stream_index
int stream_index
Definition: packet.h:524
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:365
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:255
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:29
ff_pdv_demuxer
const AVInputFormat ff_pdv_demuxer
Definition: pdvdec.c:162
pdv_read_packet
static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pdvdec.c:99
PDVDemuxContext
Definition: pdvdec.c:27
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
int32_t
int32_t
Definition: audioconvert.c:56
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:890
av_index_search_timestamp
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: seek.c:243
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:393