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 "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 {
31  uint8_t *frame_flags;
32  uint32_t *frame_offsets;
34 
35 static int pdv_probe(const AVProbeData *pd)
36 {
37  if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
38  return AVPROBE_SCORE_MAX;
39  return 0;
40 }
41 
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;
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 
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 
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;
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)
130  pkt->stream_index = 0;
131  pkt->pts = p->current_frame++;
132  pkt->duration = 1;
133 
134  return 0;
135 }
136 
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 
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,
175 };
PDVDemuxContext::current_frame
int current_frame
Definition: pdvdec.c:30
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:42
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:164
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:837
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:323
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:610
PDVDemuxContext::frame_flags
uint8_t * frame_flags
Definition: pdvdec.c:31
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:867
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:358
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:122
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:807
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:60
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
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:137
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:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
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:190
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:324
index
int index
Definition: gxfenc.c:90
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:809
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
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:94
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:132
size
int size
Definition: twinvq_data.h:10344
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
PDV_MAGIC
#define PDV_MAGIC
Definition: pdvdec.c:27
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:35
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
PDVDemuxContext::frame_offsets
uint32_t * frame_offsets
Definition: pdvdec.c:32
pos
unsigned int pos
Definition: spdifenc.c:414
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:147
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:603
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:188
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
mem.h
pdv_read_packet
static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pdvdec.c:101
PDVDemuxContext
Definition: pdvdec.c:29
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:516
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFInputFormat
Definition: demux.h:42
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:797
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:245
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346