| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2005 Matthieu CASTET | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <stdlib.h> | ||
| 22 | #include "libavcodec/avcodec.h" | ||
| 23 | #include "libavcodec/bytestream.h" | ||
| 24 | #include "libavcodec/flac.h" | ||
| 25 | #include "avformat.h" | ||
| 26 | #include "internal.h" | ||
| 27 | #include "oggdec.h" | ||
| 28 | |||
| 29 | #define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F | ||
| 30 | #define OGG_FLAC_MAGIC "\177FLAC" | ||
| 31 | #define OGG_FLAC_MAGIC_SIZE sizeof(OGG_FLAC_MAGIC)-1 | ||
| 32 | |||
| 33 | static int | ||
| 34 | 9 | flac_header (AVFormatContext * s, int idx) | |
| 35 | { | ||
| 36 | 9 | struct ogg *ogg = s->priv_data; | |
| 37 | 9 | struct ogg_stream *os = ogg->streams + idx; | |
| 38 | 9 | AVStream *st = s->streams[idx]; | |
| 39 | GetByteContext gb; | ||
| 40 | int mdt, ret; | ||
| 41 | |||
| 42 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | if (os->buf[os->pstart] == 0xff) |
| 43 | 3 | return 0; | |
| 44 | |||
| 45 | 6 | bytestream2_init(&gb, os->buf + os->pstart, os->psize); | |
| 46 | 6 | mdt = bytestream2_get_byte(&gb) & 0x7F; | |
| 47 | |||
| 48 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) { |
| 49 | uint32_t samplerate; | ||
| 50 | |||
| 51 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (bytestream2_get_bytes_left(&gb) < 4 + 4 + 4 + 4 + FLAC_STREAMINFO_SIZE) |
| 52 | ✗ | return AVERROR_INVALIDDATA; | |
| 53 | 3 | bytestream2_skipu(&gb, 4); /* "FLAC" */ | |
| 54 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (bytestream2_get_byteu(&gb) != 1) /* unsupported major version */ |
| 55 | ✗ | return -1; | |
| 56 | 3 | bytestream2_skipu(&gb, 1 + 2); /* minor version + header count */ | |
| 57 | 3 | bytestream2_skipu(&gb, 4); /* "fLaC" */ | |
| 58 | |||
| 59 | /* METADATA_BLOCK_HEADER */ | ||
| 60 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (bytestream2_get_be32u(&gb) != FLAC_STREAMINFO_SIZE) |
| 61 | ✗ | return -1; | |
| 62 | |||
| 63 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 64 | 3 | st->codecpar->codec_id = AV_CODEC_ID_FLAC; | |
| 65 | 3 | ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS; | |
| 66 | |||
| 67 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = ff_alloc_extradata(st->codecpar, FLAC_STREAMINFO_SIZE)) < 0) |
| 68 | ✗ | return ret; | |
| 69 | 3 | bytestream2_get_bufferu(&gb, st->codecpar->extradata, FLAC_STREAMINFO_SIZE); | |
| 70 | |||
| 71 | 3 | samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4; | |
| 72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!samplerate) |
| 73 | ✗ | return AVERROR_INVALIDDATA; | |
| 74 | |||
| 75 | 3 | avpriv_set_pts_info(st, 64, 1, samplerate); | |
| 76 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) { |
| 77 | 3 | ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 4, os->psize - 4); | |
| 78 | } | ||
| 79 | |||
| 80 | 6 | return 1; | |
| 81 | } | ||
| 82 | |||
| 83 | static int | ||
| 84 | 108 | flac_packet (AVFormatContext * s, int idx) | |
| 85 | { | ||
| 86 | 108 | struct ogg *ogg = s->priv_data; | |
| 87 | 108 | struct ogg_stream *os = ogg->streams + idx; | |
| 88 | 108 | AVStream *st = s->streams[idx]; | |
| 89 | int ret; | ||
| 90 | |||
| 91 |
1/2✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
|
108 | if (os->psize > OGG_FLAC_MAGIC_SIZE && |
| 92 | 108 | !memcmp( | |
| 93 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 89 times.
|
108 | os->buf + os->pstart, |
| 94 | OGG_FLAC_MAGIC, | ||
| 95 | OGG_FLAC_MAGIC_SIZE)) | ||
| 96 | 19 | return 1; | |
| 97 | |||
| 98 |
1/2✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
|
89 | if (os->psize > 0 && |
| 99 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 70 times.
|
89 | ((os->buf[os->pstart] & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT)) { |
| 100 | 19 | ret = ff_vorbis_update_metadata(s, st, os->buf + os->pstart + 4, | |
| 101 | 19 | os->psize - 4); | |
| 102 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | if (ret < 0) |
| 103 | ✗ | return ret; | |
| 104 | |||
| 105 | 19 | return 1; | |
| 106 | } | ||
| 107 | |||
| 108 | 70 | return 0; | |
| 109 | } | ||
| 110 | |||
| 111 | static int | ||
| 112 | ✗ | old_flac_header (AVFormatContext * s, int idx) | |
| 113 | { | ||
| 114 | ✗ | struct ogg *ogg = s->priv_data; | |
| 115 | ✗ | AVStream *st = s->streams[idx]; | |
| 116 | ✗ | struct ogg_stream *os = ogg->streams + idx; | |
| 117 | ✗ | AVCodecParserContext *parser = av_parser_init(AV_CODEC_ID_FLAC); | |
| 118 | AVCodecContext *avctx; | ||
| 119 | int size, ret; | ||
| 120 | uint8_t *data; | ||
| 121 | |||
| 122 | ✗ | if (!parser) | |
| 123 | ✗ | return -1; | |
| 124 | |||
| 125 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 126 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_FLAC; | |
| 127 | |||
| 128 | ✗ | avctx = avcodec_alloc_context3(NULL); | |
| 129 | ✗ | if (!avctx) { | |
| 130 | ✗ | ret = AVERROR(ENOMEM); | |
| 131 | ✗ | goto fail; | |
| 132 | } | ||
| 133 | |||
| 134 | ✗ | ret = avcodec_parameters_to_context(avctx, st->codecpar); | |
| 135 | ✗ | if (ret < 0) | |
| 136 | ✗ | goto fail; | |
| 137 | |||
| 138 | ✗ | parser->flags = PARSER_FLAG_COMPLETE_FRAMES; | |
| 139 | ✗ | av_parser_parse2(parser, avctx, | |
| 140 | ✗ | &data, &size, os->buf + os->pstart, os->psize, | |
| 141 | AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1); | ||
| 142 | |||
| 143 | ✗ | av_parser_close(parser); | |
| 144 | |||
| 145 | ✗ | if (avctx->sample_rate) { | |
| 146 | ✗ | avpriv_set_pts_info(st, 64, 1, avctx->sample_rate); | |
| 147 | ✗ | avcodec_free_context(&avctx); | |
| 148 | ✗ | return 0; | |
| 149 | } | ||
| 150 | |||
| 151 | ✗ | avcodec_free_context(&avctx); | |
| 152 | ✗ | return 1; | |
| 153 | ✗ | fail: | |
| 154 | ✗ | av_parser_close(parser); | |
| 155 | ✗ | avcodec_free_context(&avctx); | |
| 156 | ✗ | return ret; | |
| 157 | } | ||
| 158 | |||
| 159 | const struct ogg_codec ff_flac_codec = { | ||
| 160 | .magic = OGG_FLAC_MAGIC, | ||
| 161 | .magicsize = OGG_FLAC_MAGIC_SIZE, | ||
| 162 | .header = flac_header, | ||
| 163 | .nb_header = 2, | ||
| 164 | .packet = flac_packet, | ||
| 165 | }; | ||
| 166 | |||
| 167 | const struct ogg_codec ff_old_flac_codec = { | ||
| 168 | .magic = "fLaC", | ||
| 169 | .magicsize = 4, | ||
| 170 | .header = old_flac_header, | ||
| 171 | .nb_header = 0, | ||
| 172 | }; | ||
| 173 |