FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/dtshddec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 68 82 82.9%
Functions: 3 3 100.0%
Branches: 23 36 63.9%

Line Branch Exec Source
1 /*
2 * Raw DTS-HD demuxer
3 * Copyright (c) 2012 Paul B Mahol
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 "libavutil/intreadwrite.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/mem.h"
25 #include "libavcodec/dca.h"
26 #include "avformat.h"
27 #include "demux.h"
28 #include "internal.h"
29
30 #define AUPR_HDR 0x415550522D484452
31 #define AUPRINFO 0x41555052494E464F
32 #define BITSHVTB 0x4249545348565442
33 #define BLACKOUT 0x424C41434B4F5554
34 #define BRANCHPT 0x4252414E43485054
35 #define BUILDVER 0x4255494C44564552
36 #define CORESSMD 0x434F524553534D44
37 #define DTSHDHDR 0x4454534844484452
38 #define EXTSS_MD 0x45585453535f4d44
39 #define FILEINFO 0x46494C45494E464F
40 #define NAVI_TBL 0x4E4156492D54424C
41 #define STRMDATA 0x5354524D44415441
42 #define TIMECODE 0x54494D45434F4445
43
44 typedef struct DTSHDDemuxContext {
45 uint64_t data_end;
46 } DTSHDDemuxContext;
47
48 7186 static int dtshd_probe(const AVProbeData *p)
49 {
50
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7144 times.
7186 if (AV_RB64(p->buf) == DTSHDHDR)
51 42 return AVPROBE_SCORE_MAX;
52 7144 return 0;
53 }
54
55 42 static int dtshd_read_header(AVFormatContext *s)
56 {
57 42 DTSHDDemuxContext *dtshd = s->priv_data;
58 42 AVIOContext *pb = s->pb;
59 uint64_t chunk_type, chunk_size;
60 int64_t duration, orig_nb_samples, data_start;
61 AVStream *st;
62 int ret;
63 char *value;
64
65 42 st = avformat_new_stream(s, NULL);
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!st)
67 return AVERROR(ENOMEM);
68 42 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
69 42 st->codecpar->codec_id = AV_CODEC_ID_DTS;
70 42 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
71
72 for (;;) {
73 395 chunk_type = avio_rb64(pb);
74 395 chunk_size = avio_rb64(pb);
75
76
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 353 times.
395 if (avio_feof(pb))
77 42 break;
78
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353 times.
353 if (chunk_size < 4) {
80 av_log(s, AV_LOG_ERROR, "chunk size too small\n");
81 return AVERROR_INVALIDDATA;
82 }
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353 times.
353 if (chunk_size > ((uint64_t)1 << 61)) {
84 av_log(s, AV_LOG_ERROR, "chunk size too big\n");
85 return AVERROR_INVALIDDATA;
86 }
87
88
4/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 227 times.
353 switch (chunk_type) {
89 42 case STRMDATA:
90 42 data_start = avio_tell(pb);
91 42 dtshd->data_end = data_start + chunk_size;
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (dtshd->data_end <= chunk_size)
93 return AVERROR_INVALIDDATA;
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
95 goto break_loop;
96 42 goto skip;
97 break;
98 42 case AUPR_HDR:
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (chunk_size < 21)
100 return AVERROR_INVALIDDATA;
101 42 avio_skip(pb, 3);
102 42 st->codecpar->sample_rate = avio_rb24(pb);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!st->codecpar->sample_rate)
104 return AVERROR_INVALIDDATA;
105 42 duration = avio_rb32(pb); // num_frames
106 42 duration *= avio_rb16(pb); // samples_per_frames
107 42 st->duration = duration;
108 42 orig_nb_samples = avio_rb32(pb);
109 42 orig_nb_samples <<= 8;
110 42 orig_nb_samples |= avio_r8(pb);
111 42 st->codecpar->ch_layout.nb_channels = ff_dca_count_chs_for_mask(avio_rb16(pb));
112 42 st->codecpar->initial_padding = avio_rb16(pb);
113 42 st->codecpar->trailing_padding = FFMAX(st->duration - orig_nb_samples - st->codecpar->initial_padding, 0);
114 42 avio_skip(pb, chunk_size - 21);
115 42 break;
116 42 case FILEINFO:
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (chunk_size > INT_MAX)
118 goto skip;
119 42 value = av_malloc(chunk_size);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!value)
121 goto skip;
122 42 avio_read(pb, value, chunk_size);
123 42 value[chunk_size - 1] = 0;
124 42 av_dict_set(&s->metadata, "fileinfo", value,
125 AV_DICT_DONT_STRDUP_VAL);
126 42 break;
127 default:
128 269 skip:
129 269 ret = avio_skip(pb, chunk_size);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 if (ret < 0)
131 return ret;
132 };
133 }
134
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!dtshd->data_end)
136 return AVERROR_EOF;
137
138 42 avio_seek(pb, data_start, SEEK_SET);
139
140 42 break_loop:
141
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (st->codecpar->sample_rate)
142 42 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
143
144 42 return 0;
145 }
146
147 1408 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
148 {
149 1408 DTSHDDemuxContext *dtshd = s->priv_data;
150 int64_t size, left;
151 int ret;
152
153 1408 left = dtshd->data_end - avio_tell(s->pb);
154 1408 size = FFMIN(left, 1024);
155
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 1282 times.
1408 if (size <= 0)
156 126 return AVERROR_EOF;
157
158 1282 ret = av_get_packet(s->pb, pkt, size);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1282 times.
1282 if (ret < 0)
160 return ret;
161
162 1282 pkt->stream_index = 0;
163
164 1282 return ret;
165 }
166
167 const FFInputFormat ff_dtshd_demuxer = {
168 .p.name = "dtshd",
169 .p.long_name = NULL_IF_CONFIG_SMALL("raw DTS-HD"),
170 .p.flags = AVFMT_GENERIC_INDEX,
171 .p.extensions = "dtshd",
172 .priv_data_size = sizeof(DTSHDDemuxContext),
173 .read_probe = dtshd_probe,
174 .read_header = dtshd_read_header,
175 .read_packet = raw_read_packet,
176 .raw_codec_id = AV_CODEC_ID_DTS,
177 };
178