FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/oggparseflac.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 27 61 44.3%
Branches: 9 22 40.9%

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/get_bits.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
31 static int
32 6 flac_header (AVFormatContext * s, int idx)
33 {
34 6 struct ogg *ogg = s->priv_data;
35 6 struct ogg_stream *os = ogg->streams + idx;
36 6 AVStream *st = s->streams[idx];
37 GetBitContext gb;
38 int mdt, ret;
39
40
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (os->buf[os->pstart] == 0xff)
41 2 return 0;
42
43 4 init_get_bits(&gb, os->buf + os->pstart, os->psize*8);
44 4 skip_bits1(&gb); /* metadata_last */
45 4 mdt = get_bits(&gb, 7);
46
47
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
48 2 uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
49 uint32_t samplerate;
50
51 2 skip_bits_long(&gb, 4*8); /* "FLAC" */
52
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if(get_bits(&gb, 8) != 1) /* unsupported major version */
53 return -1;
54 2 skip_bits(&gb, 8 + 16); /* minor version + header count */
55 2 skip_bits_long(&gb, 4*8); /* "fLaC" */
56
57 /* METADATA_BLOCK_HEADER */
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE)
59 return -1;
60
61 2 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
62 2 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
63 2 ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
64
65
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_alloc_extradata(st->codecpar, FLAC_STREAMINFO_SIZE)) < 0)
66 return ret;
67 2 memcpy(st->codecpar->extradata, streaminfo_start, st->codecpar->extradata_size);
68
69 2 samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!samplerate)
71 return AVERROR_INVALIDDATA;
72
73 2 avpriv_set_pts_info(st, 64, 1, samplerate);
74
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
75 2 ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 4, os->psize - 4);
76 }
77
78 4 return 1;
79 }
80
81 static int
82 old_flac_header (AVFormatContext * s, int idx)
83 {
84 struct ogg *ogg = s->priv_data;
85 AVStream *st = s->streams[idx];
86 struct ogg_stream *os = ogg->streams + idx;
87 AVCodecParserContext *parser = av_parser_init(AV_CODEC_ID_FLAC);
88 AVCodecContext *avctx;
89 int size, ret;
90 uint8_t *data;
91
92 if (!parser)
93 return -1;
94
95 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
96 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
97
98 avctx = avcodec_alloc_context3(NULL);
99 if (!avctx) {
100 ret = AVERROR(ENOMEM);
101 goto fail;
102 }
103
104 ret = avcodec_parameters_to_context(avctx, st->codecpar);
105 if (ret < 0)
106 goto fail;
107
108 parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
109 av_parser_parse2(parser, avctx,
110 &data, &size, os->buf + os->pstart, os->psize,
111 AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1);
112
113 av_parser_close(parser);
114
115 if (avctx->sample_rate) {
116 avpriv_set_pts_info(st, 64, 1, avctx->sample_rate);
117 avcodec_free_context(&avctx);
118 return 0;
119 }
120
121 avcodec_free_context(&avctx);
122 return 1;
123 fail:
124 av_parser_close(parser);
125 avcodec_free_context(&avctx);
126 return ret;
127 }
128
129 const struct ogg_codec ff_flac_codec = {
130 .magic = "\177FLAC",
131 .magicsize = 5,
132 .header = flac_header,
133 .nb_header = 2,
134 };
135
136 const struct ogg_codec ff_old_flac_codec = {
137 .magic = "fLaC",
138 .magicsize = 4,
139 .header = old_flac_header,
140 .nb_header = 0,
141 };
142