LCOV - code coverage report
Current view: top level - libavformat - sauce.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 26 49 53.1 %
Date: 2017-12-14 08:27:08 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * SAUCE header parser
       3             :  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
       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             : /**
      23             :  * @file
      24             :  * SAUCE header parser
      25             :  */
      26             : 
      27             : #include "libavutil/intreadwrite.h"
      28             : #include "libavutil/dict.h"
      29             : #include "avformat.h"
      30             : #include "sauce.h"
      31             : 
      32           2 : int ff_sauce_read(AVFormatContext *avctx, uint64_t *fsize, int *got_width, int get_height)
      33             : {
      34           2 :     AVIOContext *pb = avctx->pb;
      35             :     char buf[36];
      36             :     int datatype, filetype, t1, t2, nb_comments;
      37           2 :     uint64_t start_pos = avio_size(pb) - 128;
      38             : 
      39           2 :     avio_seek(pb, start_pos, SEEK_SET);
      40           2 :     if (avio_read(pb, buf, 7) != 7)
      41           0 :         return -1;
      42           2 :     if (memcmp(buf, "SAUCE00", 7))
      43           1 :         return -1;
      44             : 
      45             : #define GET_SAUCE_META(name,size) \
      46             :     if (avio_read(pb, buf, size) == size && buf[0]) { \
      47             :         buf[size] = 0; \
      48             :         av_dict_set(&avctx->metadata, name, buf, 0); \
      49             :     }
      50             : 
      51           1 :     GET_SAUCE_META("title",     35)
      52           1 :     GET_SAUCE_META("artist",    20)
      53           1 :     GET_SAUCE_META("publisher", 20)
      54           1 :     GET_SAUCE_META("date",      8)
      55           1 :     avio_skip(pb, 4);
      56           1 :     datatype    = avio_r8(pb);
      57           1 :     filetype    = avio_r8(pb);
      58           1 :     t1          = avio_rl16(pb);
      59           1 :     t2          = avio_rl16(pb);
      60           1 :     nb_comments = avio_r8(pb);
      61           1 :     avio_skip(pb, 1); /* flags */
      62           1 :     avio_skip(pb, 4);
      63           1 :     GET_SAUCE_META("encoder",   22);
      64             : 
      65           1 :     if (got_width && datatype && filetype) {
      66           0 :         if ((datatype == 1 && filetype <=2) || (datatype == 5 && filetype == 255) || datatype == 6) {
      67           0 :             if (t1) {
      68           0 :                 avctx->streams[0]->codecpar->width = t1<<3;
      69           0 :                 *got_width = 1;
      70             :             }
      71           0 :             if (get_height && t2)
      72           0 :                 avctx->streams[0]->codecpar->height = t2<<4;
      73           0 :         } else if (datatype == 5) {
      74           0 :             if (filetype) {
      75           0 :                 avctx->streams[0]->codecpar->width = (filetype == 1 ? t1 : filetype) << 4;
      76           0 :                 *got_width = 1;
      77             :             }
      78           0 :             if (get_height && t2)
      79           0 :                 avctx->streams[0]->codecpar->height = t2<<4;
      80             :         }
      81             :     }
      82             : 
      83           1 :     *fsize -= 128;
      84             : 
      85           1 :     if (nb_comments > 0) {
      86           1 :         avio_seek(pb, start_pos - 64*nb_comments - 5, SEEK_SET);
      87           1 :         if (avio_read(pb, buf, 5) == 5 && !memcmp(buf, "COMNT", 5)) {
      88             :             int i;
      89           0 :             char *str = av_malloc(65*nb_comments + 1);
      90           0 :             *fsize -= 64*nb_comments + 5;
      91           0 :             if (!str)
      92           0 :                 return 0;
      93           0 :             for (i = 0; i < nb_comments; i++) {
      94           0 :                 if (avio_read(pb, str + 65*i, 64) != 64)
      95           0 :                     break;
      96           0 :                 str[65*i + 64] = '\n';
      97             :             }
      98           0 :             str[65*i] = 0;
      99           0 :             av_dict_set(&avctx->metadata, "comment", str, AV_DICT_DONT_STRDUP_VAL);
     100             :         }
     101             :     }
     102             : 
     103           1 :     return 0;
     104             : }

Generated by: LCOV version 1.13