LCOV - code coverage report
Current view: top level - src/libavformat - ffmetadec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 57 85 67.1 %
Date: 2017-01-28 02:43:52 Functions: 6 7 85.7 %

          Line data    Source code
       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        5565 : static int probe(AVProbeData *p)
      29             : {
      30        5565 :     if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
      31           0 :         return AVPROBE_SCORE_MAX;
      32        5565 :     return 0;
      33             : }
      34             : 
      35          10 : static void get_line(AVIOContext *s, uint8_t *buf, int size)
      36             : {
      37             :     do {
      38             :         uint8_t c;
      39          10 :         int i = 0;
      40             : 
      41         232 :         while ((c = avio_r8(s))) {
      42         221 :             if (c == '\\') {
      43           2 :                 if (i < size - 1)
      44           2 :                     buf[i++] = c;
      45           2 :                 c = avio_r8(s);
      46         219 :             } else if (c == '\n')
      47           9 :                 break;
      48             : 
      49         212 :             if (i < size - 1)
      50         212 :                 buf[i++] = c;
      51             :         }
      52          10 :         buf[i] = 0;
      53          10 :     } while (!avio_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
      54           9 : }
      55             : 
      56           0 : static AVChapter *read_chapter(AVFormatContext *s)
      57             : {
      58             :     uint8_t line[256];
      59             :     int64_t start, end;
      60           0 :     AVRational tb = {1, 1e9};
      61             : 
      62           0 :     get_line(s->pb, line, sizeof(line));
      63             : 
      64           0 :     if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
      65           0 :         get_line(s->pb, line, sizeof(line));
      66           0 :     if (!sscanf(line, "START=%"SCNd64, &start)) {
      67           0 :         av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
      68           0 :         start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
      69           0 :                  s->chapters[s->nb_chapters - 1]->end : 0;
      70             :     } else
      71           0 :         get_line(s->pb, line, sizeof(line));
      72             : 
      73           0 :     if (!sscanf(line, "END=%"SCNd64, &end)) {
      74           0 :         av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
      75           0 :         end = AV_NOPTS_VALUE;
      76             :     }
      77             : 
      78           0 :     return avpriv_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
      79             : }
      80             : 
      81          12 : static uint8_t *unescape(const uint8_t *buf, int size)
      82             : {
      83          12 :     uint8_t *ret = av_malloc(size + 1);
      84          12 :     uint8_t *p1  = ret;
      85          12 :     const uint8_t *p2 = buf;
      86             : 
      87          12 :     if (!ret)
      88           0 :         return NULL;
      89             : 
      90         202 :     while (p2 < buf + size) {
      91         178 :         if (*p2 == '\\')
      92           2 :             p2++;
      93         178 :         *p1++ = *p2++;
      94             :     }
      95          12 :     *p1 = 0;
      96          12 :     return ret;
      97             : }
      98             : 
      99           7 : static int read_tag(const uint8_t *line, AVDictionary **m)
     100             : {
     101             :     uint8_t *key, *value;
     102           7 :     const uint8_t *p = line;
     103             : 
     104             :     /* find first not escaped '=' */
     105             :     while (1) {
     106          81 :         if (*p == '=')
     107           6 :             break;
     108          38 :         else if (*p == '\\')
     109           0 :             p++;
     110             : 
     111          38 :         if (*p++)
     112          37 :             continue;
     113             : 
     114           1 :         return 0;
     115             :     }
     116             : 
     117           6 :     if (!(key = unescape(line, p - line)))
     118           0 :         return AVERROR(ENOMEM);
     119           6 :     if (!(value = unescape(p + 1, strlen(p + 1)))) {
     120           0 :         av_free(key);
     121           0 :         return AVERROR(ENOMEM);
     122             :     }
     123             : 
     124           6 :     av_dict_set(m, key, value, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
     125           6 :     return 0;
     126             : }
     127             : 
     128           1 : static int read_header(AVFormatContext *s)
     129             : {
     130           1 :     AVDictionary **m = &s->metadata;
     131             :     uint8_t line[1024];
     132             : 
     133          11 :     while(!avio_feof(s->pb)) {
     134           9 :         get_line(s->pb, line, sizeof(line));
     135             : 
     136           9 :         if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
     137           2 :             AVStream *st = avformat_new_stream(s, NULL);
     138             : 
     139           2 :             if (!st)
     140           0 :                 return AVERROR(ENOMEM);
     141             : 
     142           2 :             st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
     143           2 :             st->codecpar->codec_id   = AV_CODEC_ID_FFMETADATA;
     144             : 
     145           2 :             m = &st->metadata;
     146           7 :         } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
     147           0 :             AVChapter *ch = read_chapter(s);
     148             : 
     149           0 :             if (!ch)
     150           0 :                 return AVERROR(ENOMEM);
     151             : 
     152           0 :             m = &ch->metadata;
     153             :         } else
     154           7 :             read_tag(line, m);
     155             :     }
     156             : 
     157           1 :     s->start_time = 0;
     158           1 :     if (s->nb_chapters)
     159           0 :         s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
     160           0 :                                    s->chapters[s->nb_chapters - 1]->time_base,
     161           0 :                                    AV_TIME_BASE_Q);
     162             : 
     163           1 :     return 0;
     164             : }
     165             : 
     166           1 : static int read_packet(AVFormatContext *s, AVPacket *pkt)
     167             : {
     168           1 :     return AVERROR_EOF;
     169             : }
     170             : 
     171             : AVInputFormat ff_ffmetadata_demuxer = {
     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             : };

Generated by: LCOV version 1.12