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 (!url_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 
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 
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(!url_feof(s->pb)) {
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
Definition: avformat.h:1089
static void get_line(AVIOContext *s, uint8_t *buf, int size)
Definition: ffmetadec.c:35
#define ID_CHAPTER
Definition: ffmeta.h:26
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
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.
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ffmetadec.c:164
int num
numerator
Definition: rational.h:44
AVDictionary * metadata
Definition: avformat.h:922
static int probe(AVProbeData *p)
Definition: ffmetadec.c:28
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that&#39;s been allocated with av_malloc() and children.
Definition: dict.h:69
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Format I/O context.
Definition: avformat.h:944
Public dictionary API.
uint8_t
Opaque data information usually continuous.
Definition: avutil.h:145
window constants for m
#define ID_STRING
Definition: ffmeta.h:25
static AVPacket pkt
Definition: demuxing.c:56
end end
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
Definition: error.h:55
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
AVDictionary * metadata
Definition: avformat.h:1092
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVChapter ** chapters
Definition: avformat.h:1090
Definition: graph2dot.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and chilren.
Definition: dict.h:72
ret
Definition: avfilter.c:821
#define ID_STREAM
Definition: ffmeta.h:27
AVDictionary * metadata
Definition: avformat.h:711
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
Stream structure.
Definition: avformat.h:643
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:921
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:202
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
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:62
double value
Definition: eval.c:82
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:73
synthesis window for stochastic i
rational number numerator/denominator
Definition: rational.h:43
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
int64_t start_time
Decoding: position of the first frame of the component, in AV_TIME_BASE fractional seconds...
Definition: avformat.h:1001
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
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:920
int den
denominator
Definition: rational.h:45
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip please avoid problems through this extra level of scrutiny For cosmetics only commits you should e g by running git config global user name My Name git config global user email my email which is either set in your personal configuration file through git config core editor or set by one of the following environment VISUAL or EDITOR Log messages should be concise but descriptive Explain why you made a what you did will be obvious from the changes themselves most of the time Saying just bug fix or is bad Remember that people of varying skill levels look at and educate themselves while reading through your code Don t include filenames in log Git provides that information Possibly make the commit message have a descriptive first line
Definition: git-howto.txt:153
int64_t duration
Decoding: duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1009
void INT64 start
Definition: avisynth_c.h:594
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
This structure stores compressed data.
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
#define tb
Definition: regdef.h:68