FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/oggparseflac.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 37 72 51.4%
Functions: 2 3 66.7%
Branches: 16 32 50.0%

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
89
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 if (os->psize > OGG_FLAC_MAGIC_SIZE &&
90 108 !memcmp(
91
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 89 times.
108 os->buf + os->pstart,
92 OGG_FLAC_MAGIC,
93 OGG_FLAC_MAGIC_SIZE))
94 19 return 1;
95
96
1/2
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
89 if (os->psize > 0 &&
97
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 70 times.
89 ((os->buf[os->pstart] & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT)) {
98 19 return 1;
99 }
100
101 70 return 0;
102 }
103
104 static int
105 old_flac_header (AVFormatContext * s, int idx)
106 {
107 struct ogg *ogg = s->priv_data;
108 AVStream *st = s->streams[idx];
109 struct ogg_stream *os = ogg->streams + idx;
110 AVCodecParserContext *parser = av_parser_init(AV_CODEC_ID_FLAC);
111 AVCodecContext *avctx;
112 int size, ret;
113 uint8_t *data;
114
115 if (!parser)
116 return -1;
117
118 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
119 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
120
121 avctx = avcodec_alloc_context3(NULL);
122 if (!avctx) {
123 ret = AVERROR(ENOMEM);
124 goto fail;
125 }
126
127 ret = avcodec_parameters_to_context(avctx, st->codecpar);
128 if (ret < 0)
129 goto fail;
130
131 parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
132 av_parser_parse2(parser, avctx,
133 &data, &size, os->buf + os->pstart, os->psize,
134 AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1);
135
136 av_parser_close(parser);
137
138 if (avctx->sample_rate) {
139 avpriv_set_pts_info(st, 64, 1, avctx->sample_rate);
140 avcodec_free_context(&avctx);
141 return 0;
142 }
143
144 avcodec_free_context(&avctx);
145 return 1;
146 fail:
147 av_parser_close(parser);
148 avcodec_free_context(&avctx);
149 return ret;
150 }
151
152 const struct ogg_codec ff_flac_codec = {
153 .magic = OGG_FLAC_MAGIC,
154 .magicsize = OGG_FLAC_MAGIC_SIZE,
155 .header = flac_header,
156 .nb_header = 2,
157 .packet = flac_packet,
158 };
159
160 const struct ogg_codec ff_old_flac_codec = {
161 .magic = "fLaC",
162 .magicsize = 4,
163 .header = old_flac_header,
164 .nb_header = 0,
165 };
166