Line | Branch | Exec | Source |
---|---|---|---|
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 | 5 | static int sup_read_header(AVFormatContext *s) | |
27 | { | ||
28 | 5 | AVStream *st = avformat_new_stream(s, NULL); | |
29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!st) |
30 | ✗ | return AVERROR(ENOMEM); | |
31 | 5 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
32 | 5 | st->codecpar->codec_id = AV_CODEC_ID_HDMV_PGS_SUBTITLE; | |
33 | 5 | avpriv_set_pts_info(st, 32, 1, 90000); | |
34 | |||
35 | 5 | return 0; | |
36 | } | ||
37 | |||
38 | 55 | static int sup_read_packet(AVFormatContext *s, AVPacket *pkt) | |
39 | { | ||
40 | int64_t pts, dts, pos; | ||
41 | int ret; | ||
42 | |||
43 | 55 | pos = avio_tell(s->pb); | |
44 | |||
45 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 50 times.
|
55 | if (avio_rb16(s->pb) != SUP_PGS_MAGIC) |
46 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | return avio_feof(s->pb) ? AVERROR_EOF : AVERROR_INVALIDDATA; |
47 | |||
48 | 50 | pts = avio_rb32(s->pb); | |
49 | 50 | dts = avio_rb32(s->pb); | |
50 | |||
51 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
50 | if ((ret = av_get_packet(s->pb, pkt, 3)) < 0) |
52 | ✗ | return ret; | |
53 | |||
54 | 50 | pkt->stream_index = 0; | |
55 | 50 | pkt->flags |= AV_PKT_FLAG_KEY; | |
56 | 50 | pkt->pos = pos; | |
57 | 50 | pkt->pts = pts; | |
58 | // Many files have DTS set to 0 for all packets, so assume 0 means unset. | ||
59 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 16 times.
|
50 | pkt->dts = dts ? dts : AV_NOPTS_VALUE; |
60 | |||
61 |
1/2✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
|
50 | if (pkt->size >= 3) { |
62 | // The full packet size is stored as part of the packet. | ||
63 | 50 | size_t len = AV_RB16(pkt->data + 1); | |
64 | |||
65 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
50 | if ((ret = av_append_packet(s->pb, pkt, len)) < 0) |
66 | ✗ | return ret; | |
67 | } | ||
68 | |||
69 | 50 | return 0; | |
70 | } | ||
71 | |||
72 | 7186 | static int sup_probe(const AVProbeData *p) | |
73 | { | ||
74 | 7186 | unsigned char *buf = p->buf; | |
75 | 7186 | size_t buf_size = p->buf_size; | |
76 | int nb_packets; | ||
77 | |||
78 |
1/2✓ Branch 0 taken 7228 times.
✗ Branch 1 not taken.
|
7228 | for (nb_packets = 0; nb_packets < 10; nb_packets++) { |
79 | size_t full_packet_size; | ||
80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7228 times.
|
7228 | if (buf_size < 10 + 3) |
81 | ✗ | break; | |
82 |
2/2✓ Branch 0 taken 7176 times.
✓ Branch 1 taken 52 times.
|
7228 | if (AV_RB16(buf) != SUP_PGS_MAGIC) |
83 | 7176 | return 0; | |
84 | 52 | full_packet_size = AV_RB16(buf + 10 + 1) + 10 + 3; | |
85 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 42 times.
|
52 | if (buf_size < full_packet_size) |
86 | 10 | break; | |
87 | 42 | buf += full_packet_size; | |
88 | 42 | buf_size -= full_packet_size; | |
89 | } | ||
90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!nb_packets) |
91 | ✗ | return 0; | |
92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (nb_packets < 2) |
93 | ✗ | return AVPROBE_SCORE_RETRY / 2; | |
94 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
|
10 | if (nb_packets < 4) |
95 | 8 | return AVPROBE_SCORE_RETRY; | |
96 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (nb_packets < 10) |
97 | 2 | 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 | }; | ||
111 |