LCOV - code coverage report
Current view: top level - libavformat - flacdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 116 181 64.1 %
Date: 2017-12-15 18:13:28 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Raw FLAC demuxer
       3             :  * Copyright (c) 2001 Fabrice Bellard
       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 "libavcodec/flac.h"
      23             : #include "avformat.h"
      24             : #include "flac_picture.h"
      25             : #include "internal.h"
      26             : #include "rawdec.h"
      27             : #include "oggdec.h"
      28             : #include "vorbiscomment.h"
      29             : #include "replaygain.h"
      30             : 
      31             : #define SEEKPOINT_SIZE 18
      32             : 
      33             : typedef struct FLACDecContext {
      34             :     int found_seektable;
      35             : } FLACDecContext;
      36             : 
      37          26 : static void reset_index_position(int64_t metadata_head_size, AVStream *st)
      38             : {
      39             :     /* the real seek index offset should be the size of metadata blocks with the offset in the frame blocks */
      40             :     int i;
      41          26 :     for(i=0; i<st->nb_index_entries; i++) {
      42           0 :         st->index_entries[i].pos += metadata_head_size;
      43             :     }
      44          26 : }
      45             : 
      46          26 : static int flac_read_header(AVFormatContext *s)
      47             : {
      48          26 :     int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
      49             :     uint8_t header[4];
      50          26 :     uint8_t *buffer=NULL;
      51          26 :     FLACDecContext *flac = s->priv_data;
      52          26 :     AVStream *st = avformat_new_stream(s, NULL);
      53          26 :     if (!st)
      54           0 :         return AVERROR(ENOMEM);
      55          26 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
      56          26 :     st->codecpar->codec_id = AV_CODEC_ID_FLAC;
      57          26 :     st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
      58             :     /* the parameters will be extracted from the compressed bitstream */
      59             : 
      60             :     /* if fLaC marker is not found, assume there is no header */
      61          26 :     if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
      62           0 :         avio_seek(s->pb, -4, SEEK_CUR);
      63           0 :         return 0;
      64             :     }
      65             : 
      66             :     /* process metadata blocks */
      67         139 :     while (!avio_feof(s->pb) && !metadata_last) {
      68          87 :         if (avio_read(s->pb, header, 4) != 4)
      69           0 :             return AVERROR(AVERROR_INVALIDDATA);
      70          87 :         flac_parse_block_header(header, &metadata_last, &metadata_type,
      71             :                                    &metadata_size);
      72          87 :         switch (metadata_type) {
      73             :         /* allocate and read metadata block for supported types */
      74          61 :         case FLAC_METADATA_TYPE_STREAMINFO:
      75             :         case FLAC_METADATA_TYPE_CUESHEET:
      76             :         case FLAC_METADATA_TYPE_PICTURE:
      77             :         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
      78             :         case FLAC_METADATA_TYPE_SEEKTABLE:
      79          61 :             buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
      80          61 :             if (!buffer) {
      81           0 :                 return AVERROR(ENOMEM);
      82             :             }
      83          61 :             if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
      84           0 :                 RETURN_ERROR(AVERROR(EIO));
      85             :             }
      86          61 :             break;
      87             :         /* skip metadata block for unsupported types */
      88          26 :         default:
      89          26 :             ret = avio_skip(s->pb, metadata_size);
      90          26 :             if (ret < 0)
      91           0 :                 return ret;
      92             :         }
      93             : 
      94          87 :         if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
      95             :             uint32_t samplerate;
      96             :             uint64_t samples;
      97             : 
      98             :             /* STREAMINFO can only occur once */
      99          26 :             if (found_streaminfo) {
     100           0 :                 RETURN_ERROR(AVERROR_INVALIDDATA);
     101             :             }
     102          26 :             if (metadata_size != FLAC_STREAMINFO_SIZE) {
     103           0 :                 RETURN_ERROR(AVERROR_INVALIDDATA);
     104             :             }
     105          26 :             found_streaminfo = 1;
     106          26 :             st->codecpar->extradata      = buffer;
     107          26 :             st->codecpar->extradata_size = metadata_size;
     108          26 :             buffer = NULL;
     109             : 
     110             :             /* get sample rate and sample count from STREAMINFO header;
     111             :              * other parameters will be extracted by the parser */
     112          26 :             samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
     113          26 :             samples    = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);
     114             : 
     115             :             /* set time base and duration */
     116          26 :             if (samplerate > 0) {
     117          26 :                 avpriv_set_pts_info(st, 64, 1, samplerate);
     118          26 :                 if (samples > 0)
     119          26 :                     st->duration = samples;
     120             :             }
     121          61 :         } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
     122             :             uint8_t isrc[13];
     123             :             uint64_t start;
     124             :             const uint8_t *offset;
     125             :             int i, chapters, track, ti;
     126           0 :             if (metadata_size < 431)
     127           0 :                 RETURN_ERROR(AVERROR_INVALIDDATA);
     128           0 :             offset = buffer + 395;
     129           0 :             chapters = bytestream_get_byte(&offset) - 1;
     130           0 :             if (chapters <= 0)
     131           0 :                 RETURN_ERROR(AVERROR_INVALIDDATA);
     132           0 :             for (i = 0; i < chapters; i++) {
     133           0 :                 if (offset + 36 - buffer > metadata_size)
     134           0 :                     RETURN_ERROR(AVERROR_INVALIDDATA);
     135           0 :                 start = bytestream_get_be64(&offset);
     136           0 :                 track = bytestream_get_byte(&offset);
     137           0 :                 bytestream_get_buffer(&offset, isrc, 12);
     138           0 :                 isrc[12] = 0;
     139           0 :                 offset += 14;
     140           0 :                 ti = bytestream_get_byte(&offset);
     141           0 :                 if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
     142           0 :                 offset += ti * 12;
     143           0 :                 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
     144             :             }
     145           0 :             av_freep(&buffer);
     146          61 :         } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
     147           1 :             ret = ff_flac_parse_picture(s, buffer, metadata_size);
     148           1 :             av_freep(&buffer);
     149           1 :             if (ret < 0) {
     150           0 :                 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
     151           0 :                 return ret;
     152             :             }
     153          60 :         } else if (metadata_type == FLAC_METADATA_TYPE_SEEKTABLE) {
     154           9 :             const uint8_t *seekpoint = buffer;
     155           9 :             int i, seek_point_count = metadata_size/SEEKPOINT_SIZE;
     156           9 :             flac->found_seektable = 1;
     157           9 :             if ((s->flags&AVFMT_FLAG_FAST_SEEK)) {
     158           0 :                 for(i=0; i<seek_point_count; i++) {
     159           0 :                     int64_t timestamp = bytestream_get_be64(&seekpoint);
     160           0 :                     int64_t pos = bytestream_get_be64(&seekpoint);
     161             :                     /* skip number of samples */
     162           0 :                     bytestream_get_be16(&seekpoint);
     163           0 :                     av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
     164             :                 }
     165             :             }
     166           9 :             av_freep(&buffer);
     167             :         }
     168             :         else {
     169             : 
     170             :             /* STREAMINFO must be the first block */
     171          51 :             if (!found_streaminfo) {
     172           0 :                 RETURN_ERROR(AVERROR_INVALIDDATA);
     173             :             }
     174             :             /* process supported blocks other than STREAMINFO */
     175          51 :             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
     176             :                 AVDictionaryEntry *chmask;
     177             : 
     178          25 :                 ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
     179          25 :                 if (ret < 0) {
     180           0 :                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
     181          25 :                 } else if (ret > 0) {
     182          13 :                     s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
     183             :                 }
     184             : 
     185             :                 /* parse the channels mask if present */
     186          25 :                 chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
     187          25 :                 if (chmask) {
     188           0 :                     uint64_t mask = strtol(chmask->value, NULL, 0);
     189           0 :                     if (!mask || mask & ~0x3ffffULL) {
     190           0 :                         av_log(s, AV_LOG_WARNING,
     191             :                                "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
     192             :                     } else {
     193           0 :                         st->codecpar->channel_layout = mask;
     194           0 :                         av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
     195             :                     }
     196             :                 }
     197             :             }
     198          51 :             av_freep(&buffer);
     199             :         }
     200             :     }
     201             : 
     202          26 :     ret = ff_replaygain_export(st, s->metadata);
     203          26 :     if (ret < 0)
     204           0 :         return ret;
     205             : 
     206          26 :     reset_index_position(avio_tell(s->pb), st);
     207          26 :     return 0;
     208             : 
     209           0 : fail:
     210           0 :     av_free(buffer);
     211           0 :     return ret;
     212             : }
     213             : 
     214          16 : static int raw_flac_probe(AVProbeData *p)
     215             : {
     216          16 :     if ((p->buf[2] & 0xF0) == 0)    // blocksize code invalid
     217           0 :         return 0;
     218          16 :     if ((p->buf[2] & 0x0F) == 0x0F) // sample rate code invalid
     219           0 :         return 0;
     220          16 :     if ((p->buf[3] & 0xF0) >= FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE << 4)
     221             :         // channel mode invalid
     222           0 :         return 0;
     223          16 :     if ((p->buf[3] & 0x06) == 0x06) // bits per sample code invalid
     224           0 :         return 0;
     225          16 :     if ((p->buf[3] & 0x01) == 0x01) // reserved bit set
     226           0 :         return 0;
     227          16 :     return AVPROBE_SCORE_EXTENSION / 4 + 1;
     228             : }
     229             : 
     230        6130 : static int flac_probe(AVProbeData *p)
     231             : {
     232        6130 :     if ((AV_RB16(p->buf) & 0xFFFE) == 0xFFF8)
     233          16 :         return raw_flac_probe(p);
     234             : 
     235             :     /* file header + metadata header + checked bytes of streaminfo */
     236        6114 :     if (p->buf_size >= 4 + 4 + 13) {
     237        6114 :         int type           = p->buf[4] & 0x7f;
     238        6114 :         int size           = AV_RB24(p->buf + 5);
     239        6114 :         int min_block_size = AV_RB16(p->buf + 8);
     240        6114 :         int max_block_size = AV_RB16(p->buf + 10);
     241        6114 :         int sample_rate    = AV_RB24(p->buf + 18) >> 4;
     242             : 
     243        6114 :         if (memcmp(p->buf, "fLaC", 4))
     244        6088 :             return 0;
     245          26 :         if (type == FLAC_METADATA_TYPE_STREAMINFO &&
     246          26 :             size == FLAC_STREAMINFO_SIZE          &&
     247          26 :             min_block_size >= 16                  &&
     248          26 :             max_block_size >= min_block_size      &&
     249          26 :             sample_rate && sample_rate <= 655350)
     250          26 :             return AVPROBE_SCORE_MAX;
     251           0 :         return AVPROBE_SCORE_EXTENSION;
     252             :     }
     253             : 
     254           0 :     return 0;
     255             : }
     256             : 
     257         114 : static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
     258             :                                              int64_t *ppos, int64_t pos_limit)
     259             : {
     260             :     AVPacket pkt, out_pkt;
     261         114 :     AVStream *st = s->streams[stream_index];
     262             :     AVCodecParserContext *parser;
     263             :     int ret;
     264         114 :     int64_t pts = AV_NOPTS_VALUE;
     265             : 
     266         114 :     if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
     267           0 :         return AV_NOPTS_VALUE;
     268             : 
     269         114 :     av_init_packet(&pkt);
     270         114 :     parser = av_parser_init(st->codecpar->codec_id);
     271         114 :     if (!parser){
     272           0 :         return AV_NOPTS_VALUE;
     273             :     }
     274         114 :     parser->flags |= PARSER_FLAG_USE_CODEC_TS;
     275             : 
     276             :     for (;;){
     277        3078 :         ret = ff_raw_read_partial_packet(s, &pkt);
     278        1596 :         if (ret < 0){
     279          24 :             if (ret == AVERROR(EAGAIN))
     280           0 :                 continue;
     281             :             else {
     282          24 :                 av_packet_unref(&pkt);
     283             :                 av_assert1(!pkt.size);
     284             :             }
     285             :         }
     286        1596 :         av_init_packet(&out_pkt);
     287        3192 :         av_parser_parse2(parser, st->internal->avctx,
     288        1596 :                          &out_pkt.data, &out_pkt.size, pkt.data, pkt.size,
     289             :                          pkt.pts, pkt.dts, *ppos);
     290             : 
     291        1596 :         av_packet_unref(&pkt);
     292        1596 :         if (out_pkt.size){
     293         220 :             int size = out_pkt.size;
     294         220 :             if (parser->pts != AV_NOPTS_VALUE){
     295             :                 // seeking may not have started from beginning of a frame
     296             :                 // calculate frame start position from next frame backwards
     297         108 :                 *ppos = parser->next_frame_offset - size;
     298         108 :                 pts = parser->pts;
     299         108 :                 break;
     300             :             }
     301        1376 :         } else if (ret < 0)
     302           6 :             break;
     303             :     }
     304         114 :     av_parser_close(parser);
     305         114 :     return pts;
     306             : }
     307             : 
     308          26 : static int flac_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
     309             :     int index;
     310             :     int64_t pos;
     311             :     AVIndexEntry e;
     312          26 :     FLACDecContext *flac = s->priv_data;
     313             : 
     314          26 :     if (!flac->found_seektable || !(s->flags&AVFMT_FLAG_FAST_SEEK)) {
     315          26 :         return -1;
     316             :     }
     317             : 
     318           0 :     index = av_index_search_timestamp(s->streams[0], timestamp, flags);
     319           0 :     if(index<0 || index >= s->streams[0]->nb_index_entries)
     320           0 :         return -1;
     321             : 
     322           0 :     e = s->streams[0]->index_entries[index];
     323           0 :     pos = avio_seek(s->pb, e.pos, SEEK_SET);
     324           0 :     if (pos >= 0) {
     325           0 :         return 0;
     326             :     }
     327           0 :     return -1;
     328             : }
     329             : 
     330             : AVInputFormat ff_flac_demuxer = {
     331             :     .name           = "flac",
     332             :     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
     333             :     .read_probe     = flac_probe,
     334             :     .read_header    = flac_read_header,
     335             :     .read_packet    = ff_raw_read_partial_packet,
     336             :     .read_seek      = flac_seek,
     337             :     .read_timestamp = flac_read_timestamp,
     338             :     .flags          = AVFMT_GENERIC_INDEX,
     339             :     .extensions     = "flac",
     340             :     .raw_codec_id   = AV_CODEC_ID_FLAC,
     341             :     .priv_data_size = sizeof(FLACDecContext),
     342             : };

Generated by: LCOV version 1.13