]> git.sesse.net Git - ffmpeg/blob - libavformat/subtitles.c
ffplay: get rid of void casts in the option table
[ffmpeg] / libavformat / subtitles.c
1 /*
2  * Copyright (c) 2012 Clément Bœsch
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "avformat.h"
22 #include "subtitles.h"
23 #include "libavutil/avstring.h"
24
25 AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
26                                     const uint8_t *event, int len, int merge)
27 {
28     AVPacket *subs, *sub;
29
30     if (merge && q->nb_subs > 0) {
31         /* merge with previous event */
32
33         int old_len;
34         sub = &q->subs[q->nb_subs - 1];
35         old_len = sub->size;
36         if (av_grow_packet(sub, len) < 0)
37             return NULL;
38         memcpy(sub->data + old_len, event, len);
39     } else {
40         /* new event */
41
42         if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
43             return NULL;
44         subs = av_fast_realloc(q->subs, &q->allocated_size,
45                                (q->nb_subs + 1) * sizeof(*q->subs));
46         if (!subs)
47             return NULL;
48         q->subs = subs;
49         sub = &subs[q->nb_subs++];
50         if (av_new_packet(sub, len) < 0)
51             return NULL;
52         sub->destruct = NULL;
53         sub->flags |= AV_PKT_FLAG_KEY;
54         sub->pts = sub->dts = 0;
55         memcpy(sub->data, event, len);
56     }
57     return sub;
58 }
59
60 static int cmp_pkt_sub(const void *a, const void *b)
61 {
62     const AVPacket *s1 = a;
63     const AVPacket *s2 = b;
64     if (s1->pts == s2->pts) {
65         if (s1->pos == s2->pos)
66             return 0;
67         return s1->pos > s2->pos ? 1 : -1;
68     }
69     return s1->pts > s2->pts ? 1 : -1;
70 }
71
72 void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
73 {
74     int i;
75
76     qsort(q->subs, q->nb_subs, sizeof(*q->subs), cmp_pkt_sub);
77     for (i = 0; i < q->nb_subs; i++)
78         if (q->subs[i].duration == -1 && i < q->nb_subs - 1)
79             q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
80 }
81
82 int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
83 {
84     AVPacket *sub = q->subs + q->current_sub_idx;
85
86     if (q->current_sub_idx == q->nb_subs)
87         return AVERROR_EOF;
88     *pkt = *sub;
89     pkt->dts = pkt->pts;
90     q->current_sub_idx++;
91     return 0;
92 }
93
94 void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
95 {
96     int i;
97
98     for (i = 0; i < q->nb_subs; i++)
99         av_destruct_packet(&q->subs[i]);
100     av_freep(&q->subs);
101     q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
102 }
103
104 int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c)
105 {
106     int i = 0;
107     char end_chr;
108
109     if (!*c) // cached char?
110         *c = avio_r8(pb);
111     if (!*c)
112         return 0;
113
114     end_chr = *c == '<' ? '>' : '<';
115     do {
116         av_bprint_chars(buf, *c, 1);
117         *c = avio_r8(pb);
118         i++;
119     } while (*c != end_chr && *c);
120     if (end_chr == '>') {
121         av_bprint_chars(buf, '>', 1);
122         *c = 0;
123     }
124     return i;
125 }
126
127 const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
128 {
129     int in_quotes = 0;
130     const int len = strlen(attr);
131
132     while (*s) {
133         while (*s) {
134             if (!in_quotes && isspace(*s))
135                 break;
136             in_quotes ^= *s == '"'; // XXX: support escaping?
137             s++;
138         }
139         while (isspace(*s))
140             s++;
141         if (!av_strncasecmp(s, attr, len) && s[len] == '=')
142             return s + len + 1 + (s[len + 1] == '"');
143     }
144     return NULL;
145 }