Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/mathematics.h"
23 #include "avformat.h"
24 #include "ffmeta.h"
25 #include "internal.h"
26 #include "libavutil/dict.h"
27 
28 static int probe(AVProbeData *p)
29 {
30  if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
31  return AVPROBE_SCORE_MAX;
32  return 0;
33 }
34 
35 static void get_line(AVIOContext *s, uint8_t *buf, int size)
36 {
37  do {
38  uint8_t c;
39  int i = 0;
40 
41  while ((c = avio_r8(s))) {
42  if (c == '\\') {
43  if (i < size - 1)
44  buf[i++] = c;
45  c = avio_r8(s);
46  } else if (c == '\n')
47  break;
48 
49  if (i < size - 1)
50  buf[i++] = c;
51  }
52  buf[i] = 0;
53  } while (!s->eof_reached && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
54 }
55 
57 {
58  uint8_t line[256];
59  int64_t start, end;
60  AVRational tb = {1, 1e9};
61 
62  get_line(s->pb, line, sizeof(line));
63 
64  if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
65  get_line(s->pb, line, sizeof(line));
66  if (!sscanf(line, "START=%"SCNd64, &start)) {
67  av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
68  start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
69  s->chapters[s->nb_chapters - 1]->end : 0;
70  } else
71  get_line(s->pb, line, sizeof(line));
72 
73  if (!sscanf(line, "END=%"SCNd64, &end)) {
74  av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
75  end = AV_NOPTS_VALUE;
76  }
77 
78  return avpriv_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
79 }
80 
81 static uint8_t *unescape(uint8_t *buf, int size)
82 {
83  uint8_t *ret = av_malloc(size + 1);
84  uint8_t *p1 = ret, *p2 = buf;
85 
86  if (!ret)
87  return NULL;
88 
89  while (p2 < buf + size) {
90  if (*p2 == '\\')
91  p2++;
92  *p1++ = *p2++;
93  }
94  *p1 = 0;
95  return ret;
96 }
97 
98 static int read_tag(uint8_t *line, AVDictionary **m)
99 {
100  uint8_t *key, *value, *p = line;
101 
102  /* find first not escaped '=' */
103  while (1) {
104  if (*p == '=')
105  break;
106  else if (*p == '\\')
107  p++;
108 
109  if (*p++)
110  continue;
111 
112  return 0;
113  }
114 
115  if (!(key = unescape(line, p - line)))
116  return AVERROR(ENOMEM);
117  if (!(value = unescape(p + 1, strlen(p + 1)))) {
118  av_free(key);
119  return AVERROR(ENOMEM);
120  }
121 
123  return 0;
124 }
125 
127 {
128  AVDictionary **m = &s->metadata;
129  uint8_t line[1024];
130 
131  while(!s->pb->eof_reached) {
132  get_line(s->pb, line, sizeof(line));
133 
134  if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
136 
137  if (!st)
138  return -1;
139 
142 
143  m = &st->metadata;
144  } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
145  AVChapter *ch = read_chapter(s);
146 
147  if (!ch)
148  return -1;
149 
150  m = &ch->metadata;
151  } else
152  read_tag(line, m);
153  }
154 
155  s->start_time = 0;
156  if (s->nb_chapters)
157  s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
158  s->chapters[s->nb_chapters - 1]->time_base,
160 
161  return 0;
162 }
163 
165 {
166  return AVERROR_EOF;
167 }
168 
170  .name = "ffmetadata",
171  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
172  .read_probe = probe,
173  .read_header = read_header,
174  .read_packet = read_packet,
175 };
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1119
static void get_line(AVIOContext *s, uint8_t *buf, int size)
Definition: ffmetadec.c:35
#define ID_CHAPTER
Definition: ffmeta.h:26
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:68
int size
static int read_header(AVFormatContext *s)
Definition: ffmetadec.c:126
static int read_tag(uint8_t *line, AVDictionary **m)
Definition: ffmetadec.c:98
Dummy codec for streams containing only metadata information.
Definition: avcodec.h:470
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmetadec.c:164
int num
numerator
Definition: rational.h:44
AVDictionary * metadata
Definition: avformat.h:909
static int probe(AVProbeData *p)
Definition: ffmetadec.c:28
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() and children.
Definition: dict.h:63
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:2601
Format I/O context.
Definition: avformat.h:922
Public dictionary API.
uint8_t
Opaque data information usually continuous.
Definition: avutil.h:189
#define ID_STRING
Definition: ffmeta.h:25
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
#define AVERROR_EOF
End of file.
Definition: error.h:51
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
AVChapter ** chapters
Definition: avformat.h:1120
Definition: graph2dot.c:49
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() and chilren.
Definition: dict.h:66
#define ID_STREAM
Definition: ffmeta.h:27
AVDictionary * metadata
Definition: avformat.h:771
Stream structure.
Definition: avformat.h:699
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:908
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:240
AVIOContext * pb
I/O context.
Definition: avformat.h:964
AVInputFormat ff_ffmetadata_demuxer
Definition: ffmetadec.c:169
static uint8_t * unescape(uint8_t *buf, int size)
Definition: ffmetadec.c:81
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:68
rational number numerator/denominator
Definition: rational.h:43
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1007
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
static AVChapter * read_chapter(AVFormatContext *s)
Definition: ffmetadec.c:56
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:907
int den
denominator
Definition: rational.h:45
int eof_reached
true if eof reached
Definition: avio.h:96
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1017
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
This structure stores compressed data.
Definition: avcodec.h:950
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228