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 "demux.h"
24 #include "internal.h"
25 
26 #define PDV_MAGIC "Playdate VID\x00\x00\x00\x00"
27 
28 typedef struct PDVDemuxContext {
30  uint8_t *frame_flags;
31  uint32_t *frame_offsets;
33 
34 static int pdv_probe(const AVProbeData *pd)
35 {
36  if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
37  return AVPROBE_SCORE_MAX;
38  return 0;
39 }
40 
42 {
43  PDVDemuxContext *p = s->priv_data;
44  AVIOContext *pb = s->pb;
45  AVCodecParameters *par;
46  AVStream *st;
47  uint64_t start;
48  uint32_t fps;
49 
50  avio_skip(pb, 16);
51 
52  st = avformat_new_stream(s, NULL);
53  if (!st)
54  return AVERROR(ENOMEM);
55 
56  par = st->codecpar;
59  st->start_time = 0;
60  st->duration =
61  st->nb_frames = avio_rl16(pb);
62  avio_skip(pb, 2);
63  fps = avio_rl32(pb);
64  st->avg_frame_rate = av_d2q(av_int2float(fps), INT_MAX);
65  par->width = avio_rl16(pb);
66  par->height = avio_rl16(pb);
67 
69 
70  p->current_frame = 0;
71  p->frame_flags = av_calloc(st->nb_frames + 1, sizeof(*p->frame_flags));
72  p->frame_offsets = av_calloc(st->nb_frames + 1, sizeof(*p->frame_offsets));
73 
74  if (!p->frame_flags || !p->frame_offsets)
75  return AVERROR(ENOMEM);
76 
77  for (int n = 0; n <= st->nb_frames; n++) {
78  const uint32_t entry = avio_rl32(pb);
79 
80  p->frame_flags[n] = entry & 3;
81  p->frame_offsets[n] = entry >> 2;
82  }
83 
84  start = avio_tell(pb);
85 
86  for (int n = 0; n < st->nb_frames; n++) {
87  const uint64_t pos = start + p->frame_offsets[n];
88  const int32_t size = p->frame_offsets[n+1] - p->frame_offsets[n];
89  const int flags = p->frame_flags[n] & 1 ? AVINDEX_KEYFRAME : 0;
90 
91  if (p->frame_flags[n] == 0 || size <= 0 ||
92  ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)))
93  break;
94  av_add_index_entry(st, pos, n, size, 0, flags);
95  }
96 
97  return 0;
98 }
99 
101 {
102  PDVDemuxContext *p = s->priv_data;
103  AVStream *st = s->streams[0];
104  FFStream *const sti = ffstream(st);
105  AVIOContext *pb = s->pb;
106  int32_t size, flags, ret;
107  int64_t pos;
108 
109  if (p->current_frame >= st->nb_frames)
110  return AVERROR_EOF;
111 
112  if (p->current_frame >= sti->nb_index_entries)
113  return AVERROR(EIO);
114 
115  pos = sti->index_entries[p->current_frame].pos;
118 
119  avio_seek(pb, pos, SEEK_SET);
120  if (avio_feof(pb) || ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)) || size == 0)
121  return AVERROR_EOF;
122 
123  ret = av_get_packet(pb, pkt, size);
124  if (ret < 0)
125  return ret;
126 
127  if (flags & AVINDEX_KEYFRAME)
129  pkt->stream_index = 0;
130  pkt->pts = p->current_frame++;
131  pkt->duration = 1;
132 
133  return 0;
134 }
135 
137 {
138  PDVDemuxContext *p = s->priv_data;
139 
140  av_freep(&p->frame_flags);
141  av_freep(&p->frame_offsets);
142 
143  return 0;
144 }
145 
146 static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
147 {
148  PDVDemuxContext *p = s->priv_data;
149  AVStream *st = s->streams[stream_index];
150  int index = av_index_search_timestamp(st, timestamp, flags);
151 
152  if (index < 0)
153  return -1;
154 
155  if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
156  return -1;
157 
158  p->current_frame = index;
159 
160  return 0;
161 }
162 
164  .p.name = "pdv",
165  .p.long_name = NULL_IF_CONFIG_SMALL("PlayDate Video"),
166  .p.extensions = "pdv",
167  .priv_data_size = sizeof(PDVDemuxContext),
168  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
174 };
PDVDemuxContext::current_frame
int current_frame
Definition: pdvdec.c:29
entry
#define entry
Definition: aom_film_grain_template.c:66
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:41
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
ff_pdv_demuxer
const FFInputFormat ff_pdv_demuxer
Definition: pdvdec.c:163
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
int64_t
long long int64_t
Definition: coverity.c:34
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:832
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:322
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:610
PDVDemuxContext::frame_flags
uint8_t * frame_flags
Definition: pdvdec.c:30
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
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:853
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:417
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:494
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:713
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:59
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:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
AVIndexEntry::size
int size
Definition: avformat.h:613
pdv_read_close
static int pdv_read_close(AVFormatContext *s)
Definition: pdvdec.c:136
FF_INFMT_FLAG_INIT_CLEANUP
#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
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
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:612
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:251
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AV_CODEC_ID_PDV
@ AV_CODEC_ID_PDV
Definition: codec_id.h:320
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:804
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:729
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
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:261
FFStream
Definition: internal.h:193
size
int size
Definition: twinvq_data.h:10344
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
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:26
AVCodecParameters::height
int height
Definition: codec_par.h:135
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
demux.h
pdv_probe
static int pdv_probe(const AVProbeData *pd)
Definition: pdvdec.c:34
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:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
PDVDemuxContext::frame_offsets
uint32_t * frame_offsets
Definition: pdvdec.c:31
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:146
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:603
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:317
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:249
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
pdv_read_packet
static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pdvdec.c:100
PDVDemuxContext
Definition: pdvdec.c:28
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
FFInputFormat
Definition: demux.h:37
int32_t
int32_t
Definition: audioconvert.c:56
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
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:792
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:345