tools/sofa2wavs: fix build on Windows
[ffmpeg.git] / libavformat / supdec.c
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "avformat.h"
20 #include "demux.h"
21 #include "internal.h"
22 #include "libavutil/intreadwrite.h"
23
24 #define SUP_PGS_MAGIC 0x5047 /* "PG", big endian */
25
26 static int sup_read_header(AVFormatContext *s)
27 {
28 AVStream *st = avformat_new_stream(s, NULL);
29 if (!st)
30 return AVERROR(ENOMEM);
31 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
32 st->codecpar->codec_id = AV_CODEC_ID_HDMV_PGS_SUBTITLE;
33 avpriv_set_pts_info(st, 32, 1, 90000);
34
35 return 0;
36 }
37
38 static int sup_read_packet(AVFormatContext *s, AVPacket *pkt)
39 {
40 int64_t pts, dts, pos;
41 int ret;
42
43 pos = avio_tell(s->pb);
44
45 if (avio_rb16(s->pb) != SUP_PGS_MAGIC)
46 return avio_feof(s->pb) ? AVERROR_EOF : AVERROR_INVALIDDATA;
47
48 pts = avio_rb32(s->pb);
49 dts = avio_rb32(s->pb);
50
51 if ((ret = av_get_packet(s->pb, pkt, 3)) < 0)
52 return ret;
53
54 pkt->stream_index = 0;
55 pkt->flags |= AV_PKT_FLAG_KEY;
56 pkt->pos = pos;
57 pkt->pts = pts;
58 // Many files have DTS set to 0 for all packets, so assume 0 means unset.
59 pkt->dts = dts ? dts : AV_NOPTS_VALUE;
60
61 if (pkt->size >= 3) {
62 // The full packet size is stored as part of the packet.
63 size_t len = AV_RB16(pkt->data + 1);
64
65 if ((ret = av_append_packet(s->pb, pkt, len)) < 0)
66 return ret;
67 }
68
69 return 0;
70 }
71
72 static int sup_probe(const AVProbeData *p)
73 {
74 unsigned char *buf = p->buf;
75 size_t buf_size = p->buf_size;
76 int nb_packets;
77
78 for (nb_packets = 0; nb_packets < 10; nb_packets++) {
79 size_t full_packet_size;
80 if (buf_size < 10 + 3)
81 break;
82 if (AV_RB16(buf) != SUP_PGS_MAGIC)
83 return 0;
84 full_packet_size = AV_RB16(buf + 10 + 1) + 10 + 3;
85 if (buf_size < full_packet_size)
86 break;
87 buf += full_packet_size;
88 buf_size -= full_packet_size;
89 }
90 if (!nb_packets)
91 return 0;
92 if (nb_packets < 2)
93 return AVPROBE_SCORE_RETRY / 2;
94 if (nb_packets < 4)
95 return AVPROBE_SCORE_RETRY;
96 if (nb_packets < 10)
97 return AVPROBE_SCORE_EXTENSION;
98 return AVPROBE_SCORE_MAX;
99 }
100
101 const FFInputFormat ff_sup_demuxer = {
102 .p.name = "sup",
103 .p.long_name = NULL_IF_CONFIG_SMALL("raw HDMV Presentation Graphic Stream subtitles"),
104 .p.extensions = "sup",
105 .p.mime_type = "application/x-pgs",
106 .p.flags = AVFMT_GENERIC_INDEX,
107 .read_probe = sup_probe,
108 .read_header = sup_read_header,
109 .read_packet = sup_read_packet,
110 };