LCOV - code coverage report
Current view: top level - libavformat - dash.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 69 0.0 %
Date: 2017-12-17 23:02:56 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /*
       2             :  * MPEG-DASH ISO BMFF segmenter
       3             :  * Copyright (c) 2014 Martin Storsjo
       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 "config.h"
      23             : #if HAVE_UNISTD_H
      24             : #include <unistd.h>
      25             : #endif
      26             : 
      27             : #include "libavutil/avassert.h"
      28             : #include "libavutil/avstring.h"
      29             : #include "libavutil/intreadwrite.h"
      30             : #include "libavutil/mathematics.h"
      31             : #include "libavutil/opt.h"
      32             : #include "libavutil/rational.h"
      33             : #include "libavutil/time_internal.h"
      34             : 
      35             : #include "avc.h"
      36             : #include "avformat.h"
      37             : #include "avio_internal.h"
      38             : #include "internal.h"
      39             : #include "isom.h"
      40             : #include "os_support.h"
      41             : #include "url.h"
      42             : #include "dash.h"
      43             : 
      44           0 : static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag,
      45             :                                     size_t format_tag_size, const char **ptr) {
      46             :     const char *next_ptr;
      47           0 :     DASHTmplId id_type = DASH_TMPL_ID_UNDEFINED;
      48             : 
      49           0 :     if (av_strstart(identifier, "$$", &next_ptr)) {
      50           0 :         id_type = DASH_TMPL_ID_ESCAPE;
      51           0 :         *ptr = next_ptr;
      52           0 :     } else if (av_strstart(identifier, "$RepresentationID$", &next_ptr)) {
      53           0 :         id_type = DASH_TMPL_ID_REP_ID;
      54             :         // default to basic format, as $RepresentationID$ identifiers
      55             :         // are not allowed to have custom format-tags.
      56           0 :         av_strlcpy(format_tag, "%d", format_tag_size);
      57           0 :         *ptr = next_ptr;
      58             :     } else { // the following identifiers may have an explicit format_tag
      59           0 :         if (av_strstart(identifier, "$Number", &next_ptr))
      60           0 :             id_type = DASH_TMPL_ID_NUMBER;
      61           0 :         else if (av_strstart(identifier, "$Bandwidth", &next_ptr))
      62           0 :             id_type = DASH_TMPL_ID_BANDWIDTH;
      63           0 :         else if (av_strstart(identifier, "$Time", &next_ptr))
      64           0 :             id_type = DASH_TMPL_ID_TIME;
      65             :         else
      66           0 :             id_type = DASH_TMPL_ID_UNDEFINED;
      67             : 
      68             :         // next parse the dash format-tag and generate a c-string format tag
      69             :         // (next_ptr now points at the first '%' at the beginning of the format-tag)
      70           0 :         if (id_type != DASH_TMPL_ID_UNDEFINED) {
      71           0 :             const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
      72           0 :             if (next_ptr[0] == '$') { // no dash format-tag
      73           0 :                 snprintf(format_tag, format_tag_size, "%%%s", number_format);
      74           0 :                 *ptr = &next_ptr[1];
      75             :             } else {
      76             :                 const char *width_ptr;
      77             :                 // only tolerate single-digit width-field (i.e. up to 9-digit width)
      78           0 :                 if (av_strstart(next_ptr, "%0", &width_ptr) &&
      79           0 :                     av_isdigit(width_ptr[0]) &&
      80           0 :                     av_strstart(&width_ptr[1], "d$", &next_ptr)) {
      81             :                     // yes, we're using a format tag to build format_tag.
      82           0 :                     snprintf(format_tag, format_tag_size, "%s%c%s", "%0", width_ptr[0], number_format);
      83           0 :                     *ptr = next_ptr;
      84             :                 } else {
      85           0 :                     av_log(NULL, AV_LOG_WARNING, "Failed to parse format-tag beginning with %s. Expected either a "
      86             :                                                  "closing '$' character or a format-string like '%%0[width]d', "
      87             :                                                  "where width must be a single digit\n", next_ptr);
      88           0 :                     id_type = DASH_TMPL_ID_UNDEFINED;
      89             :                 }
      90             :             }
      91             :         }
      92             :     }
      93           0 :     return id_type;
      94             : }
      95             : 
      96           0 : void ff_dash_fill_tmpl_params(char *dst, size_t buffer_size,
      97             :                                   const char *template, int rep_id,
      98             :                                   int number, int bit_rate,
      99             :                                   int64_t time) {
     100           0 :     int dst_pos = 0;
     101           0 :     const char *t_cur = template;
     102           0 :     while (dst_pos < buffer_size - 1 && *t_cur) {
     103             :         char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
     104           0 :         int n = 0;
     105             :         DASHTmplId id_type;
     106           0 :         const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
     107           0 :         if (t_next) {
     108           0 :             int num_copy_bytes = FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
     109           0 :             av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
     110             :             // advance
     111           0 :             dst_pos += num_copy_bytes;
     112           0 :             t_cur = t_next;
     113             :         } else { // no more DASH identifiers to substitute - just copy the rest over and break
     114           0 :             av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
     115           0 :             break;
     116             :         }
     117             : 
     118           0 :         if (dst_pos >= buffer_size - 1 || !*t_cur)
     119             :             break;
     120             : 
     121             :         // t_cur is now pointing to a '$' character
     122           0 :         id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
     123           0 :         switch (id_type) {
     124           0 :         case DASH_TMPL_ID_ESCAPE:
     125           0 :             av_strlcpy(&dst[dst_pos], "$", 2);
     126           0 :             n = 1;
     127           0 :             break;
     128           0 :         case DASH_TMPL_ID_REP_ID:
     129           0 :             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
     130           0 :             break;
     131           0 :         case DASH_TMPL_ID_NUMBER:
     132           0 :             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
     133           0 :             break;
     134           0 :         case DASH_TMPL_ID_BANDWIDTH:
     135           0 :             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
     136           0 :             break;
     137           0 :         case DASH_TMPL_ID_TIME:
     138           0 :             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
     139           0 :             break;
     140           0 :         case DASH_TMPL_ID_UNDEFINED:
     141             :             // copy over one byte and advance
     142           0 :             av_strlcpy(&dst[dst_pos], t_cur, 2);
     143           0 :             n = 1;
     144           0 :             t_next = &t_cur[1];
     145           0 :             break;
     146             :         }
     147             :         // t_next points just past the processed identifier
     148             :         // n is the number of bytes that were attempted to be written to dst
     149             :         // (may have failed to write all because buffer_size).
     150             : 
     151             :         // advance
     152           0 :         dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
     153           0 :         t_cur = t_next;
     154             :     }
     155           0 : }
     156             : 
     157             : 

Generated by: LCOV version 1.13