summaryrefslogtreecommitdiff
path: root/libavformat/pdvdec.c
blob: 9d3f386e40d6cf81c059f892309f7b13d260d7b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * PDV demuxer
 * Copyright (c) 2023 Paul B Mahol
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avformat.h"
#include "internal.h"

#define PDV_MAGIC "Playdate VID\x00\x00\x00\x00"

typedef struct PDVDemuxContext {
    int current_frame;
    uint8_t *frame_flags;
    uint32_t *frame_offsets;
} PDVDemuxContext;

static int pdv_probe(const AVProbeData *pd)
{
    if (strncmp(pd->buf, PDV_MAGIC, sizeof(PDV_MAGIC) - 1) == 0)
        return AVPROBE_SCORE_MAX;
    return 0;
}

static int pdv_read_header(AVFormatContext *s)
{
    PDVDemuxContext *p = s->priv_data;
    AVIOContext *pb = s->pb;
    AVCodecParameters *par;
    AVStream *st;
    uint64_t start;
    uint32_t fps;

    avio_skip(pb, 16);

    st = avformat_new_stream(s, NULL);
    if (!st)
        return AVERROR(ENOMEM);

    par              = st->codecpar;
    par->codec_type  = AVMEDIA_TYPE_VIDEO;
    par->codec_id    = AV_CODEC_ID_PDV;
    st->start_time   = 0;
    st->duration     =
    st->nb_frames    = avio_rl16(pb);
    avio_skip(pb, 2);
    fps = avio_rl32(pb);
    st->avg_frame_rate = av_d2q(av_int2float(fps), INT_MAX);
    par->width       = avio_rl16(pb);
    par->height      = avio_rl16(pb);

    avpriv_set_pts_info(st, 64, st->avg_frame_rate.den, st->avg_frame_rate.num);

    p->current_frame = 0;
    p->frame_flags = av_calloc(st->nb_frames + 1, sizeof(*p->frame_flags));
    p->frame_offsets = av_calloc(st->nb_frames + 1, sizeof(*p->frame_offsets));

    if (!p->frame_flags || !p->frame_offsets)
        return AVERROR(ENOMEM);

    for (int n = 0; n <= st->nb_frames; n++) {
        const uint32_t entry = avio_rl32(pb);

        p->frame_flags[n] = entry & 3;
        p->frame_offsets[n] = entry >> 2;
    }

    start = avio_tell(pb);

    for (int n = 0; n < st->nb_frames; n++) {
        const uint64_t pos = start + p->frame_offsets[n];
        const int32_t size = p->frame_offsets[n+1] - p->frame_offsets[n];
        const int flags = p->frame_flags[n] & 1 ? AVINDEX_KEYFRAME : 0;

        if (p->frame_flags[n] == 0 || size <= 0 ||
            ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)))
            break;
        av_add_index_entry(st, pos, n, size, 0, flags);
    }

    return 0;
}

static int pdv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    PDVDemuxContext *p = s->priv_data;
    AVStream *st = s->streams[0];
    FFStream *const sti = ffstream(st);
    AVIOContext *pb = s->pb;
    int32_t size, flags, ret;
    int64_t pos;

    if (p->current_frame >= st->nb_frames)
        return AVERROR_EOF;

    if (p->current_frame >= sti->nb_index_entries)
        return AVERROR(EIO);

    pos   = sti->index_entries[p->current_frame].pos;
    flags = sti->index_entries[p->current_frame].flags;
    size  = sti->index_entries[p->current_frame].size;

    avio_seek(pb, pos, SEEK_SET);
    if (avio_feof(pb) || ((pb->seekable & AVIO_SEEKABLE_NORMAL) && pos + size > avio_size(pb)) || size == 0)
        return AVERROR_EOF;

    ret = av_get_packet(pb, pkt, size);
    if (ret < 0)
        return ret;

    if (flags & AVINDEX_KEYFRAME)
        pkt->flags |= AV_PKT_FLAG_KEY;
    pkt->stream_index = 0;
    pkt->pts = p->current_frame++;
    pkt->duration = 1;

    return 0;
}

static int pdv_read_close(AVFormatContext *s)
{
    PDVDemuxContext *p = s->priv_data;

    av_freep(&p->frame_flags);
    av_freep(&p->frame_offsets);

    return 0;
}

static int pdv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
{
    PDVDemuxContext *p = s->priv_data;
    AVStream *st = s->streams[stream_index];
    int index = av_index_search_timestamp(st, timestamp, flags);

    if (index < 0)
        return -1;

    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
        return -1;

    p->current_frame = index;

    return 0;
}

const AVInputFormat ff_pdv_demuxer = {
    .name           = "pdv",
    .long_name      = NULL_IF_CONFIG_SMALL("PlayDate Video"),
    .priv_data_size = sizeof(PDVDemuxContext),
    .flags_internal = FF_FMT_INIT_CLEANUP,
    .read_probe     = pdv_probe,
    .read_header    = pdv_read_header,
    .read_packet    = pdv_read_packet,
    .read_close     = pdv_read_close,
    .read_seek      = pdv_read_seek,
    .extensions     = "pdv",
};