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 "internal.h"
21 #include "libavutil/intreadwrite.h"
22 
23 #define SUP_PGS_MAGIC 0x5047 /* "PG", big endian */
24 
26 {
28  if (!st)
29  return AVERROR(ENOMEM);
32  avpriv_set_pts_info(st, 32, 1, 90000);
33 
34  return 0;
35 }
36 
38 {
39  int64_t pts, dts, pos;
40  int ret;
41 
42  pos = avio_tell(s->pb);
43 
44  if (avio_rb16(s->pb) != SUP_PGS_MAGIC)
46 
47  pts = avio_rb32(s->pb);
48  dts = avio_rb32(s->pb);
49 
50  if ((ret = av_get_packet(s->pb, pkt, 3)) < 0)
51  return ret;
52 
53  pkt->stream_index = 0;
55  pkt->pos = pos;
56  pkt->pts = pts;
57  // Many files have DTS set to 0 for all packets, so assume 0 means unset.
58  pkt->dts = dts ? dts : AV_NOPTS_VALUE;
59 
60  if (pkt->size >= 3) {
61  // The full packet size is stored as part of the packet.
62  size_t len = AV_RB16(pkt->data + 1);
63 
64  if ((ret = av_append_packet(s->pb, pkt, len)) < 0)
65  return ret;
66  }
67 
68  return 0;
69 }
70 
71 static int sup_probe(const AVProbeData *p)
72 {
73  unsigned char *buf = p->buf;
74  size_t buf_size = p->buf_size;
75  int nb_packets;
76 
77  for (nb_packets = 0; nb_packets < 10; nb_packets++) {
78  size_t full_packet_size;
79  if (buf_size < 10 + 3)
80  break;
81  if (AV_RB16(buf) != SUP_PGS_MAGIC)
82  return 0;
83  full_packet_size = AV_RB16(buf + 10 + 1) + 10 + 3;
84  if (buf_size < full_packet_size)
85  break;
86  buf += full_packet_size;
87  buf_size -= full_packet_size;
88  }
89  if (!nb_packets)
90  return 0;
91  if (nb_packets < 2)
92  return AVPROBE_SCORE_RETRY / 2;
93  if (nb_packets < 4)
94  return AVPROBE_SCORE_RETRY;
95  if (nb_packets < 10)
97  return AVPROBE_SCORE_MAX;
98 }
99 
101  .name = "sup",
102  .long_name = NULL_IF_CONFIG_SMALL("raw HDMV Presentation Graphic Stream subtitles"),
103  .extensions = "sup",
104  .mime_type = "application/x-pgs",
105  .read_probe = sup_probe,
106  .read_header = sup_read_header,
107  .read_packet = sup_read_packet,
108  .flags = AVFMT_GENERIC_INDEX,
109 };
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:450
AV_CODEC_ID_HDMV_PGS_SUBTITLE
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h:528
ff_sup_demuxer
const AVInputFormat ff_sup_demuxer
Definition: supdec.c:100
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:428
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:504
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:476
pts
static int64_t pts
Definition: transcode_aac.c:653
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:650
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
sup_read_header
static int sup_read_header(AVFormatContext *s)
Definition: supdec.c:25
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
NULL
#define NULL
Definition: coverity.c:32
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
SUP_PGS_MAGIC
#define SUP_PGS_MAGIC
Definition: supdec.c:23
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:457
AVPacket::size
int size
Definition: packet.h:374
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:117
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:37
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:372
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
sup_probe
static int sup_probe(const AVProbeData *p)
Definition: supdec.c:71
AVPROBE_SCORE_RETRY
#define AVPROBE_SCORE_RETRY
Definition: avformat.h:454
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:197
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:775
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:213
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
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: utils.c:1196
AVPacket::stream_index
int stream_index
Definition: packet.h:375
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:393
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:375