2 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
24 #define SUP_PGS_MAGIC 0x5047 /* "PG", big endian */
26 static int sup_read_header(AVFormatContext
*s
)
28 AVStream
*st
= avformat_new_stream(s
, NULL
);
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);
38 static int sup_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
40 int64_t pts
, dts
, pos
;
43 pos
= avio_tell(s
->pb
);
45 if (avio_rb16(s
->pb
) != SUP_PGS_MAGIC
)
46 return avio_feof(s
->pb
) ? AVERROR_EOF
: AVERROR_INVALIDDATA
;
48 pts
= avio_rb32(s
->pb
);
49 dts
= avio_rb32(s
->pb
);
51 if ((ret
= av_get_packet(s
->pb
, pkt
, 3)) < 0)
54 pkt
->stream_index
= 0;
55 pkt
->flags
|= AV_PKT_FLAG_KEY
;
58 // Many files have DTS set to 0 for all packets, so assume 0 means unset.
59 pkt
->dts
= dts
? dts
: AV_NOPTS_VALUE
;
62 // The full packet size is stored as part of the packet.
63 size_t len
= AV_RB16(pkt
->data
+ 1);
65 if ((ret
= av_append_packet(s
->pb
, pkt
, len
)) < 0)
72 static int sup_probe(const AVProbeData
*p
)
74 unsigned char *buf
= p
->buf
;
75 size_t buf_size
= p
->buf_size
;
78 for (nb_packets
= 0; nb_packets
< 10; nb_packets
++) {
79 size_t full_packet_size
;
80 if (buf_size
< 10 + 3)
82 if (AV_RB16(buf
) != SUP_PGS_MAGIC
)
84 full_packet_size
= AV_RB16(buf
+ 10 + 1) + 10 + 3;
85 if (buf_size
< full_packet_size
)
87 buf
+= full_packet_size
;
88 buf_size
-= full_packet_size
;
93 return AVPROBE_SCORE_RETRY
/ 2;
95 return AVPROBE_SCORE_RETRY
;
97 return AVPROBE_SCORE_EXTENSION
;
98 return AVPROBE_SCORE_MAX
;
101 const FFInputFormat ff_sup_demuxer
= {
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
,