FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/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 (!avio_feof(s) && (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(const uint8_t *buf, int size)
82 {
83  uint8_t *ret = av_malloc(size + 1);
84  uint8_t *p1 = ret;
85  const uint8_t *p2 = buf;
86 
87  if (!ret)
88  return NULL;
89 
90  while (p2 < buf + size) {
91  if (*p2 == '\\')
92  p2++;
93  *p1++ = *p2++;
94  }
95  *p1 = 0;
96  return ret;
97 }
98 
99 static int read_tag(const uint8_t *line, AVDictionary **m)
100 {
101  uint8_t *key, *value;
102  const uint8_t *p = line;
103 
104  /* find first not escaped '=' */
105  while (1) {
106  if (*p == '=')
107  break;
108  else if (*p == '\\')
109  p++;
110 
111  if (*p++)
112  continue;
113 
114  return 0;
115  }
116 
117  if (!(key = unescape(line, p - line)))
118  return AVERROR(ENOMEM);
119  if (!(value = unescape(p + 1, strlen(p + 1)))) {
120  av_free(key);
121  return AVERROR(ENOMEM);
122  }
123 
125  return 0;
126 }
127 
129 {
130  AVDictionary **m = &s->metadata;
131  uint8_t line[1024];
132 
133  while(!avio_feof(s->pb)) {
134  get_line(s->pb, line, sizeof(line));
135 
136  if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
138 
139  if (!st)
140  return AVERROR(ENOMEM);
141 
144 
145  m = &st->metadata;
146  } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
147  AVChapter *ch = read_chapter(s);
148 
149  if (!ch)
150  return AVERROR(ENOMEM);
151 
152  m = &ch->metadata;
153  } else
154  read_tag(line, m);
155  }
156 
157  s->start_time = 0;
158  if (s->nb_chapters)
159  s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
160  s->chapters[s->nb_chapters - 1]->time_base,
162 
163  return 0;
164 }
165 
167 {
168  return AVERROR_EOF;
169 }
170 
172  .name = "ffmetadata",
173  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
174  .read_probe = probe,
175  .read_header = read_header,
176  .read_packet = read_packet,
177 };
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1528
static void get_line(AVIOContext *s, uint8_t *buf, int size)
Definition: ffmetadec.c:35
#define ID_CHAPTER
Definition: ffmeta.h:26
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
static int read_header(AVFormatContext *s)
Definition: ffmetadec.c:128
Dummy codec for streams containing only metadata information.
Definition: avcodec.h:648
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmetadec.c:166
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3922
int num
numerator
Definition: rational.h:44
static AVPacket pkt
AVDictionary * metadata
Definition: avformat.h:1286
static int probe(AVProbeData *p)
Definition: ffmetadec.c:28
static int read_tag(const uint8_t *line, AVDictionary **m)
Definition: ffmetadec.c:99
#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: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:4189
Format I/O context.
Definition: avformat.h:1325
Public dictionary API.
static uint8_t * unescape(const uint8_t *buf, int size)
Definition: ffmetadec.c:81
uint8_t
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:195
#define ID_STRING
Definition: ffmeta.h:25
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4065
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
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:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1539
#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:176
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3918
AVChapter ** chapters
Definition: avformat.h:1529
Definition: graph2dot.c:48
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:589
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:461
#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:76
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define ID_STREAM
Definition: ffmeta.h:27
AVDictionary * metadata
Definition: avformat.h:945
Stream structure.
Definition: avformat.h:876
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1285
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
void * buf
Definition: avisynth_c.h:553
AVInputFormat ff_ffmetadata_demuxer
Definition: ffmetadec.c:171
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:69
rational number numerator/denominator
Definition: rational.h:43
This structure contains the data a format has to probe a file.
Definition: avformat.h:459
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1410
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:471
Main libavformat public API header.
static AVChapter * read_chapter(AVFormatContext *s)
Definition: ffmetadec.c:56
static double c[64]
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1284
int den
denominator
Definition: rational.h:45
#define av_free(p)
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1420
void INT64 start
Definition: avisynth_c.h:553
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:660
AVCodecParameters * codecpar
Definition: avformat.h:1006
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:328
This structure stores compressed data.
Definition: avcodec.h:1557
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
#define tb
Definition: regdef.h:68