FFmpeg
supdec.c
Go to the documentation of this file.
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 
27 {
29  if (!st)
30  return AVERROR(ENOMEM);
33  avpriv_set_pts_info(st, 32, 1, 90000);
34 
35  return 0;
36 }
37 
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)
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;
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)
98  return AVPROBE_SCORE_MAX;
99 }
100 
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 };
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
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
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AV_CODEC_ID_HDMV_PGS_SUBTITLE
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h:562
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
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
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
pts
static int64_t pts
Definition: transcode_aac.c:644
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:761
pkt
AVPacket * pkt
Definition: movenc.c:60
intreadwrite.h
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
sup_read_header
static int sup_read_header(AVFormatContext *s)
Definition: supdec.c:26
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
NULL
#define NULL
Definition: coverity.c:32
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
SUP_PGS_MAGIC
#define SUP_PGS_MAGIC
Definition: supdec.c:24
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVPacket::size
int size
Definition: packet.h:540
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
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
sup_read_packet
static int sup_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: supdec.c:38
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:538
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
sup_probe
static int sup_probe(const AVProbeData *p)
Definition: supdec.c:72
AVPROBE_SCORE_RETRY
#define AVPROBE_SCORE_RETRY
Definition: avformat.h:458
demux.h
len
int len
Definition: vorbis_enc_data.h:426
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_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:746
av_append_packet
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition: utils.c:107
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
ff_sup_demuxer
const FFInputFormat ff_sup_demuxer
Definition: supdec.c:101
AVPacket::stream_index
int stream_index
Definition: packet.h:541
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
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346