FFmpeg
ffmetadec.c
Go to the documentation of this file.
1 /*
2  * Metadata demuxer
3  * Copyright (c) 2010 Anton Khirnov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/bprint.h"
23 #include "libavutil/mathematics.h"
24 #include "libavutil/mem.h"
25 #include "avformat.h"
26 #include "demux.h"
27 #include "ffmeta.h"
28 #include "libavutil/dict.h"
29 
30 static int probe(const AVProbeData *p)
31 {
32  if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
33  return AVPROBE_SCORE_MAX;
34  return 0;
35 }
36 
38 {
39  int len, end;
40  int64_t read = 0;
41  char tmp[1024];
42  char c;
43  char prev = ' ';
44 
45  do {
46  len = 0;
47  do {
48  c = avio_r8(s);
49  end = prev != '\\' && (c == '\r' || c == '\n' || c == '\0');
50  if (!end)
51  tmp[len++] = c;
52  prev = c;
53  } while (!end && len < sizeof(tmp));
55  read += len;
56  } while (!end);
57 
58  if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
59  avio_skip(s, -1);
60 
61  if (!c && s->error)
62  return s->error;
63 
64  if (!c && !read && avio_feof(s))
65  return AVERROR_EOF;
66 
67  return read;
68 }
69 
70 static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
71 {
72 
73  do {
74  av_bprint_clear(bp);
76  } while (!avio_feof(s) && (bp->str[0] == ';' || bp->str[0] == '#' || bp->str[0] == 0));
77 }
78 
79 static void get_line(AVIOContext *s, uint8_t *buf, int size)
80 {
81  do {
82  uint8_t c;
83  int i = 0;
84 
85  while ((c = avio_r8(s))) {
86  if (c == '\\') {
87  if (i < size - 1)
88  buf[i++] = c;
89  c = avio_r8(s);
90  } else if (c == '\n')
91  break;
92 
93  if (i < size - 1)
94  buf[i++] = c;
95  }
96  buf[i] = 0;
97  } while (!avio_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
98 }
99 
101 {
102  uint8_t line[256];
103  int64_t start, end;
104  AVRational tb = {1, 1e9};
105  int ret;
106 
107  get_line(s->pb, line, sizeof(line));
108 
109  if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
110  get_line(s->pb, line, sizeof(line));
111  ret = sscanf(line, "START=%"SCNd64, &start);
112  if (ret <= 0) {
113  av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
114  start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
115  s->chapters[s->nb_chapters - 1]->end : 0;
116  } else
117  get_line(s->pb, line, sizeof(line));
118 
119  ret = sscanf(line, "END=%"SCNd64, &end);
120  if (ret <= 0) {
121  av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
122  end = AV_NOPTS_VALUE;
123  }
124 
125  return avpriv_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
126 }
127 
128 static uint8_t *unescape(const uint8_t *buf, int size)
129 {
130  uint8_t *ret = av_malloc(size + 1);
131  uint8_t *p1 = ret;
132  const uint8_t *p2 = buf;
133 
134  if (!ret)
135  return NULL;
136 
137  while (p2 < buf + size) {
138  if (*p2 == '\\')
139  p2++;
140  *p1++ = *p2++;
141  }
142  *p1 = 0;
143  return ret;
144 }
145 
146 static int read_tag(const uint8_t *line, AVDictionary **m)
147 {
148  uint8_t *key, *value;
149  const uint8_t *p = line;
150 
151  /* find first not escaped '=' */
152  while (1) {
153  if (*p == '=')
154  break;
155  else if (*p == '\\')
156  p++;
157 
158  if (*p++)
159  continue;
160 
161  return 0;
162  }
163 
164  if (!(key = unescape(line, p - line)))
165  return AVERROR(ENOMEM);
166  if (!(value = unescape(p + 1, strlen(p + 1)))) {
167  av_free(key);
168  return AVERROR(ENOMEM);
169  }
170 
172  return 0;
173 }
174 
176 {
177  AVDictionary **m = &s->metadata;
178  AVBPrint bp;
179 
181 
182  while(!avio_feof(s->pb)) {
183  get_bprint_line(s->pb, &bp);
184 
185  if (!memcmp(bp.str, ID_STREAM, strlen(ID_STREAM))) {
187 
188  if (!st)
189  goto nomem;
190 
193 
194  m = &st->metadata;
195  } else if (!memcmp(bp.str, ID_CHAPTER, strlen(ID_CHAPTER))) {
196  AVChapter *ch = read_chapter(s);
197 
198  if (!ch)
199  goto nomem;
200 
201  m = &ch->metadata;
202  } else
203  read_tag(bp.str, m);
204  }
205 
206  av_bprint_finalize(&bp, NULL);
207 
208  s->start_time = 0;
209  if (s->nb_chapters)
210  s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
211  s->chapters[s->nb_chapters - 1]->time_base,
213 
214  return 0;
215 nomem:
216  av_bprint_finalize(&bp, NULL);
217 
218  return AVERROR(ENOMEM);
219 }
220 
222 {
223  return AVERROR_EOF;
224 }
225 
227  .p.name = "ffmetadata",
228  .p.long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
229  .read_probe = probe,
230  .read_header = read_header,
231  .read_packet = read_packet,
232 };
avpriv_new_chapter
AVChapter * avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: demux_utils.c:43
read_line_to_bprint_escaped
static int64_t read_line_to_bprint_escaped(AVIOContext *s, AVBPrint *bp)
Definition: ffmetadec.c:37
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
ff_ffmetadata_demuxer
const FFInputFormat ff_ffmetadata_demuxer
Definition: ffmetadec.c:226
AVChapter::metadata
AVDictionary * metadata
Definition: avformat.h:1263
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
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
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
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
int64_t
long long int64_t
Definition: coverity.c:34
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AV_CODEC_ID_FFMETADATA
@ AV_CODEC_ID_FFMETADATA
Dummy codec for streams containing only metadata information.
Definition: codec_id.h:607
mathematics.h
AVDictionary
Definition: dict.c:34
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
AVChapter
Definition: avformat.h:1259
ID_CHAPTER
#define ID_CHAPTER
Definition: ffmeta.h:26
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
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
read_header
static int read_header(AVFormatContext *s)
Definition: ffmetadec.c:175
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
key
const char * key
Definition: hwcontext_opencl.c:189
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmetadec.c:221
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:828
get_line
static void get_line(AVIOContext *s, uint8_t *buf, int size)
Definition: ffmetadec.c:79
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
unescape
static uint8_t * unescape(const uint8_t *buf, int size)
Definition: ffmetadec.c:128
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_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
line
Definition: graph2dot.c:48
bprint.h
read_chapter
static AVChapter * read_chapter(AVFormatContext *s)
Definition: ffmetadec.c:100
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
read_tag
static int read_tag(const uint8_t *line, AVDictionary **m)
Definition: ffmetadec.c:146
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
demux.h
len
int len
Definition: vorbis_enc_data.h:426
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
get_bprint_line
static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
Definition: ffmetadec.c:70
avformat.h
dict.h
ID_STREAM
#define ID_STREAM
Definition: ffmeta.h:27
probe
static int probe(const AVProbeData *p)
Definition: ffmetadec.c:30
AVRational::den
int den
Denominator.
Definition: rational.h:60
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
ffmeta.h
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
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
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
FFInputFormat
Definition: demux.h:42
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_bprint_append_data
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:163
line
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:40
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:231
AV_DICT_DONT_STRDUP_KEY
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function.
Definition: dict.h:77
ID_STRING
#define ID_STRING
Definition: ffmeta.h:25
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346