Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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. static int flac_read_header(AVFormatContext *s)
  32. {
  33.     int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
  34.     uint8_t header[4];
  35.     uint8_t *buffer=NULL;
  36.     AVStream *st = avformat_new_stream(s, NULL);
  37.     if (!st)
  38.         return AVERROR(ENOMEM);
  39.     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  40.     st->codec->codec_id = AV_CODEC_ID_FLAC;
  41.     st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  42.     /* the parameters will be extracted from the compressed bitstream */
  43.  
  44.     /* if fLaC marker is not found, assume there is no header */
  45.     if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  46.         avio_seek(s->pb, -4, SEEK_CUR);
  47.         return 0;
  48.     }
  49.  
  50.     /* process metadata blocks */
  51.     while (!avio_feof(s->pb) && !metadata_last) {
  52.         avio_read(s->pb, header, 4);
  53.         flac_parse_block_header(header, &metadata_last, &metadata_type,
  54.                                    &metadata_size);
  55.         switch (metadata_type) {
  56.         /* allocate and read metadata block for supported types */
  57.         case FLAC_METADATA_TYPE_STREAMINFO:
  58.         case FLAC_METADATA_TYPE_CUESHEET:
  59.         case FLAC_METADATA_TYPE_PICTURE:
  60.         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  61.             buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  62.             if (!buffer) {
  63.                 return AVERROR(ENOMEM);
  64.             }
  65.             if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  66.                 RETURN_ERROR(AVERROR(EIO));
  67.             }
  68.             break;
  69.         /* skip metadata block for unsupported types */
  70.         default:
  71.             ret = avio_skip(s->pb, metadata_size);
  72.             if (ret < 0)
  73.                 return ret;
  74.         }
  75.  
  76.         if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  77.             uint32_t samplerate;
  78.             uint64_t samples;
  79.  
  80.             /* STREAMINFO can only occur once */
  81.             if (found_streaminfo) {
  82.                 RETURN_ERROR(AVERROR_INVALIDDATA);
  83.             }
  84.             if (metadata_size != FLAC_STREAMINFO_SIZE) {
  85.                 RETURN_ERROR(AVERROR_INVALIDDATA);
  86.             }
  87.             found_streaminfo = 1;
  88.             st->codec->extradata      = buffer;
  89.             st->codec->extradata_size = metadata_size;
  90.             buffer = NULL;
  91.  
  92.             /* get sample rate and sample count from STREAMINFO header;
  93.              * other parameters will be extracted by the parser */
  94.             samplerate = AV_RB24(st->codec->extradata + 10) >> 4;
  95.             samples    = (AV_RB64(st->codec->extradata + 13) >> 24) & ((1ULL << 36) - 1);
  96.  
  97.             /* set time base and duration */
  98.             if (samplerate > 0) {
  99.                 avpriv_set_pts_info(st, 64, 1, samplerate);
  100.                 if (samples > 0)
  101.                     st->duration = samples;
  102.             }
  103.         } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  104.             uint8_t isrc[13];
  105.             uint64_t start;
  106.             const uint8_t *offset;
  107.             int i, chapters, track, ti;
  108.             if (metadata_size < 431)
  109.                 RETURN_ERROR(AVERROR_INVALIDDATA);
  110.             offset = buffer + 395;
  111.             chapters = bytestream_get_byte(&offset) - 1;
  112.             if (chapters <= 0)
  113.                 RETURN_ERROR(AVERROR_INVALIDDATA);
  114.             for (i = 0; i < chapters; i++) {
  115.                 if (offset + 36 - buffer > metadata_size)
  116.                     RETURN_ERROR(AVERROR_INVALIDDATA);
  117.                 start = bytestream_get_be64(&offset);
  118.                 track = bytestream_get_byte(&offset);
  119.                 bytestream_get_buffer(&offset, isrc, 12);
  120.                 isrc[12] = 0;
  121.                 offset += 14;
  122.                 ti = bytestream_get_byte(&offset);
  123.                 if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
  124.                 offset += ti * 12;
  125.                 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  126.             }
  127.             av_freep(&buffer);
  128.         } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
  129.             ret = ff_flac_parse_picture(s, buffer, metadata_size);
  130.             av_freep(&buffer);
  131.             if (ret < 0) {
  132.                 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
  133.                 return ret;
  134.             }
  135.         } else {
  136.             /* STREAMINFO must be the first block */
  137.             if (!found_streaminfo) {
  138.                 RETURN_ERROR(AVERROR_INVALIDDATA);
  139.             }
  140.             /* process supported blocks other than STREAMINFO */
  141.             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  142.                 AVDictionaryEntry *chmask;
  143.  
  144.                 ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
  145.                 if (ret < 0) {
  146.                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  147.                 } else if (ret > 0) {
  148.                     s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  149.                 }
  150.  
  151.                 /* parse the channels mask if present */
  152.                 chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  153.                 if (chmask) {
  154.                     uint64_t mask = strtol(chmask->value, NULL, 0);
  155.                     if (!mask || mask & ~0x3ffffULL) {
  156.                         av_log(s, AV_LOG_WARNING,
  157.                                "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
  158.                     } else {
  159.                         st->codec->channel_layout = mask;
  160.                         av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  161.                     }
  162.                 }
  163.             }
  164.             av_freep(&buffer);
  165.         }
  166.     }
  167.  
  168.     ret = ff_replaygain_export(st, s->metadata);
  169.     if (ret < 0)
  170.         return ret;
  171.  
  172.     return 0;
  173.  
  174. fail:
  175.     av_free(buffer);
  176.     return ret;
  177. }
  178.  
  179. static int raw_flac_probe(AVProbeData *p)
  180. {
  181.     if ((p->buf[2] & 0xF0) == 0)    // blocksize code invalid
  182.         return 0;
  183.     if ((p->buf[2] & 0x0F) == 0x0F) // sample rate code invalid
  184.         return 0;
  185.     if ((p->buf[3] & 0xF0) >= FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE << 4)
  186.         // channel mode invalid
  187.         return 0;
  188.     if ((p->buf[3] & 0x06) == 0x06) // bits per sample code invalid
  189.         return 0;
  190.     if ((p->buf[3] & 0x01) == 0x01) // reserved bit set
  191.         return 0;
  192.     return AVPROBE_SCORE_EXTENSION / 4 + 1;
  193. }
  194.  
  195. static int flac_probe(AVProbeData *p)
  196. {
  197.     if ((AV_RB16(p->buf) & 0xFFFE) == 0xFFF8)
  198.         return raw_flac_probe(p);
  199.     if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
  200.         return 0;
  201.     return AVPROBE_SCORE_EXTENSION;
  202. }
  203.  
  204. static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
  205.                                              int64_t *ppos, int64_t pos_limit)
  206. {
  207.     AVPacket pkt, out_pkt;
  208.     AVStream *st = s->streams[stream_index];
  209.     AVCodecParserContext *parser;
  210.     int ret;
  211.     int64_t pts = AV_NOPTS_VALUE;
  212.  
  213.     if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
  214.         return AV_NOPTS_VALUE;
  215.  
  216.     av_init_packet(&pkt);
  217.     parser = av_parser_init(st->codec->codec_id);
  218.     if (!parser){
  219.         return AV_NOPTS_VALUE;
  220.     }
  221.     parser->flags |= PARSER_FLAG_USE_CODEC_TS;
  222.  
  223.     for (;;){
  224.         ret = ff_raw_read_partial_packet(s, &pkt);
  225.         if (ret < 0){
  226.             if (ret == AVERROR(EAGAIN))
  227.                 continue;
  228.             else
  229.                 break;
  230.         }
  231.         av_init_packet(&out_pkt);
  232.         ret = av_parser_parse2(parser, st->codec,
  233.                                &out_pkt.data, &out_pkt.size, pkt.data, pkt.size,
  234.                                pkt.pts, pkt.dts, *ppos);
  235.  
  236.         av_free_packet(&pkt);
  237.         if (out_pkt.size){
  238.             int size = out_pkt.size;
  239.             if (parser->pts != AV_NOPTS_VALUE){
  240.                 // seeking may not have started from beginning of a frame
  241.                 // calculate frame start position from next frame backwards
  242.                 *ppos = parser->next_frame_offset - size;
  243.                 pts = parser->pts;
  244.                 break;
  245.             }
  246.         }
  247.     }
  248.     av_parser_close(parser);
  249.     return pts;
  250. }
  251.  
  252. AVInputFormat ff_flac_demuxer = {
  253.     .name           = "flac",
  254.     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
  255.     .read_probe     = flac_probe,
  256.     .read_header    = flac_read_header,
  257.     .read_packet    = ff_raw_read_partial_packet,
  258.     .read_timestamp = flac_read_timestamp,
  259.     .flags          = AVFMT_GENERIC_INDEX,
  260.     .extensions     = "flac",
  261.     .raw_codec_id   = AV_CODEC_ID_FLAC,
  262. };
  263.