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/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 
30 typedef struct PDVDemuxContext {
32  uint8_t *frame_flags;
33  uint32_t *frame_offsets;
35 
36 static 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;
47  AVCodecParameters *par;
48  AVStream *st;
49  uint64_t start;
50  uint32_t fps;
51 
52  avio_skip(pb, 16);
53 
54  st = avformat_new_stream(s, NULL);
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(EIO);
116 
117  pos = sti->index_entries[p->current_frame].pos;
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 
129  if (flags & AVINDEX_KEYFRAME)
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 
148 static 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 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
PDVDemuxContext::current_frame
int current_frame
Definition: pdvdec.c:31
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:43
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:165
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:834
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:553
intfloat.h
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:590
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:607
PDVDemuxContext::frame_flags
uint8_t * frame_flags
Definition: pdvdec.c:32
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:777
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:347
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:804
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:550
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:610
pdv_read_close
static int pdv_read_close(AVFormatContext *s)
Definition: pdvdec.c:138
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:1265
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:768
NULL
#define NULL
Definition: coverity.c:32
AVIndexEntry::flags
int flags
Definition: avformat.h:609
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:186
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:806
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:128
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:541
read_header
static int read_header(FFV1Context *f, RangeCoder *c)
Definition: ffv1dec.c:489
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:528
PDV_MAGIC
#define PDV_MAGIC
Definition: pdvdec.c:28
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:36
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:94
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:745
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:33
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:148
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:600
AVPacket::stream_index
int stream_index
Definition: packet.h:537
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:184
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:102
PDVDemuxContext
Definition: pdvdec.c:30
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:512
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
FFInputFormat
Definition: demux.h:42
int32_t
int32_t
Definition: audioconvert.c:56
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:794
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