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 "avformat.h"
25 #include "ffmeta.h"
26 #include "internal.h"
27 #include "libavutil/dict.h"
28 
29 static int probe(const AVProbeData *p)
30 {
31  if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
32  return AVPROBE_SCORE_MAX;
33  return 0;
34 }
35 
36 static int64_t read_line_to_bprint_escaped(AVIOContext *s, AVBPrint *bp)
37 {
38  int len, end;
39  int64_t read = 0;
40  char tmp[1024];
41  char c;
42  char prev = ' ';
43 
44  do {
45  len = 0;
46  do {
47  c = avio_r8(s);
48  end = prev != '\\' && (c == '\r' || c == '\n' || c == '\0');
49  if (!end)
50  tmp[len++] = c;
51  prev = c;
52  } while (!end && len < sizeof(tmp));
53  av_bprint_append_data(bp, tmp, len);
54  read += len;
55  } while (!end);
56 
57  if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
58  avio_skip(s, -1);
59 
60  if (!c && s->error)
61  return s->error;
62 
63  if (!c && !read && avio_feof(s))
64  return AVERROR_EOF;
65 
66  return read;
67 }
68 
69 static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
70 {
71 
72  do {
73  av_bprint_clear(bp);
75  } while (!avio_feof(s) && (bp->str[0] == ';' || bp->str[0] == '#' || bp->str[0] == 0));
76 }
77 
78 static void get_line(AVIOContext *s, uint8_t *buf, int size)
79 {
80  do {
81  uint8_t c;
82  int i = 0;
83 
84  while ((c = avio_r8(s))) {
85  if (c == '\\') {
86  if (i < size - 1)
87  buf[i++] = c;
88  c = avio_r8(s);
89  } else if (c == '\n')
90  break;
91 
92  if (i < size - 1)
93  buf[i++] = c;
94  }
95  buf[i] = 0;
96  } while (!avio_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
97 }
98 
100 {
101  uint8_t line[256];
102  int64_t start, end;
103  AVRational tb = {1, 1e9};
104  int ret;
105 
106  get_line(s->pb, line, sizeof(line));
107 
108  if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
109  get_line(s->pb, line, sizeof(line));
110  ret = sscanf(line, "START=%"SCNd64, &start);
111  if (ret <= 0) {
112  av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
113  start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
114  s->chapters[s->nb_chapters - 1]->end : 0;
115  } else
116  get_line(s->pb, line, sizeof(line));
117 
118  ret = sscanf(line, "END=%"SCNd64, &end);
119  if (ret <= 0) {
120  av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
121  end = AV_NOPTS_VALUE;
122  }
123 
124  return avpriv_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
125 }
126 
127 static uint8_t *unescape(const uint8_t *buf, int size)
128 {
129  uint8_t *ret = av_malloc(size + 1);
130  uint8_t *p1 = ret;
131  const uint8_t *p2 = buf;
132 
133  if (!ret)
134  return NULL;
135 
136  while (p2 < buf + size) {
137  if (*p2 == '\\')
138  p2++;
139  *p1++ = *p2++;
140  }
141  *p1 = 0;
142  return ret;
143 }
144 
145 static int read_tag(const uint8_t *line, AVDictionary **m)
146 {
147  uint8_t *key, *value;
148  const uint8_t *p = line;
149 
150  /* find first not escaped '=' */
151  while (1) {
152  if (*p == '=')
153  break;
154  else if (*p == '\\')
155  p++;
156 
157  if (*p++)
158  continue;
159 
160  return 0;
161  }
162 
163  if (!(key = unescape(line, p - line)))
164  return AVERROR(ENOMEM);
165  if (!(value = unescape(p + 1, strlen(p + 1)))) {
166  av_free(key);
167  return AVERROR(ENOMEM);
168  }
169 
171  return 0;
172 }
173 
175 {
176  AVDictionary **m = &s->metadata;
177  AVBPrint bp;
178 
180 
181  while(!avio_feof(s->pb)) {
182  get_bprint_line(s->pb, &bp);
183 
184  if (!memcmp(bp.str, ID_STREAM, strlen(ID_STREAM))) {
186 
187  if (!st)
188  goto nomem;
189 
192 
193  m = &st->metadata;
194  } else if (!memcmp(bp.str, ID_CHAPTER, strlen(ID_CHAPTER))) {
195  AVChapter *ch = read_chapter(s);
196 
197  if (!ch)
198  goto nomem;
199 
200  m = &ch->metadata;
201  } else
202  read_tag(bp.str, m);
203  }
204 
205  av_bprint_finalize(&bp, NULL);
206 
207  s->start_time = 0;
208  if (s->nb_chapters)
209  s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
210  s->chapters[s->nb_chapters - 1]->time_base,
212 
213  return 0;
214 nomem:
215  av_bprint_finalize(&bp, NULL);
216 
217  return AVERROR(ENOMEM);
218 }
219 
221 {
222  return AVERROR_EOF;
223 }
224 
226  .name = "ffmetadata",
227  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
228  .read_probe = probe,
229  .read_header = read_header,
230  .read_packet = read_packet,
231 };
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1584
static void get_line(AVIOContext *s, uint8_t *buf, int size)
Definition: ffmetadec.c:78
#define ID_CHAPTER
Definition: ffmeta.h:26
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
static int read_header(AVFormatContext *s)
Definition: ffmetadec.c:174
Dummy codec for streams containing only metadata information.
Definition: codec_id.h:567
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmetadec.c:220
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int num
Numerator.
Definition: rational.h:59
static int probe(const AVProbeData *p)
Definition: ffmetadec.c:29
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:341
const char * key
static AVPacket pkt
AVDictionary * metadata
Definition: avformat.h:1316
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
static int read_tag(const uint8_t *line, AVDictionary **m)
Definition: ffmetadec.c:145
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that&#39;s been allocated with av_malloc() or another memory allocation function...
Definition: dict.h:73
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:4577
Format I/O context.
Definition: avformat.h:1355
Public dictionary API.
static uint8_t * unescape(const uint8_t *buf, int size)
Definition: ffmetadec.c:127
uint8_t
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:203
#define ID_STRING
Definition: ffmeta.h:25
static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
Definition: ffmetadec.c:69
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
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
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4449
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:100
#define av_log(a,...)
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
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1595
#define AV_BPRINT_SIZE_UNLIMITED
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVChapter ** chapters
Definition: avformat.h:1585
Definition: graph2dot.c:48
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
#define ID_STREAM
Definition: ffmeta.h:27
#define s(width, name)
Definition: cbs_vp9.c:257
AVDictionary * metadata
Definition: avformat.h:944
Stream structure.
Definition: avformat.h:880
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1315
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
AVIOContext * pb
I/O context.
Definition: avformat.h:1397
AVInputFormat ff_ffmetadata_demuxer
Definition: ffmetadec.c:225
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:70
static int64_t read_line_to_bprint_escaped(AVIOContext *s, AVBPrint *bp)
Definition: ffmetadec.c:36
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1460
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
Main libavformat public API header.
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:33
static AVChapter * read_chapter(AVFormatContext *s)
Definition: ffmetadec.c:99
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1314
int den
Denominator.
Definition: rational.h:60
#define av_free(p)
int len
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1470
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:652
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1027
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:368
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
This structure stores compressed data.
Definition: packet.h:340
int i
Definition: input.c:407
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define tb
Definition: regdef.h:68
static uint8_t tmp[11]
Definition: aes_ctr.c:26