FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/dhav.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 4 280 1.4%
Functions: 1 9 11.1%
Branches: 2 135 1.5%

Line Branch Exec Source
1 /*
2 * DHAV demuxer
3 *
4 * Copyright (c) 2018 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <time.h>
24
25 #include "libavutil/mem.h"
26 #include "libavutil/parseutils.h"
27 #include "avio_internal.h"
28 #include "avformat.h"
29 #include "demux.h"
30 #include "internal.h"
31
32 typedef struct DHAVContext {
33 unsigned type;
34 unsigned subtype;
35 unsigned channel;
36 unsigned frame_subnumber;
37 unsigned frame_number;
38 unsigned date;
39 unsigned timestamp;
40 int width, height;
41 int video_codec;
42 int frame_rate;
43 int audio_channels;
44 int audio_codec;
45 int sample_rate;
46 int64_t last_good_pos;
47 int64_t duration;
48
49 int video_stream_index;
50 int audio_stream_index;
51 } DHAVContext;
52
53 typedef struct DHAVStream {
54 int64_t last_frame_number;
55 int64_t last_timestamp;
56 int64_t last_time;
57 int64_t pts;
58 } DHAVStream;
59
60 7186 static int dhav_probe(const AVProbeData *p)
61 {
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7186 times.
7186 if (!memcmp(p->buf, "DAHUA", 5))
63 return AVPROBE_SCORE_MAX;
64
65
1/2
✓ Branch 0 taken 7186 times.
✗ Branch 1 not taken.
7186 if (memcmp(p->buf, "DHAV", 4))
66 7186 return 0;
67
68 if (p->buf[4] == 0xf0 ||
69 p->buf[4] == 0xf1 ||
70 p->buf[4] == 0xfc ||
71 p->buf[4] == 0xfd)
72 return AVPROBE_SCORE_MAX;
73 return 0;
74 }
75
76 static const uint32_t sample_rates[] = {
77 8000, 4000, 8000, 11025, 16000,
78 20000, 22050, 32000, 44100, 48000,
79 96000, 192000, 64000,
80 };
81
82 static int parse_ext(AVFormatContext *s, int length)
83 {
84 DHAVContext *dhav = s->priv_data;
85 int64_t ret = 0;
86
87 while (length > 0) {
88 int type = avio_r8(s->pb);
89 int index;
90
91 switch (type) {
92 case 0x80:
93 ret = avio_skip(s->pb, 1);
94 dhav->width = 8 * avio_r8(s->pb);
95 dhav->height = 8 * avio_r8(s->pb);
96 length -= 4;
97 break;
98 case 0x81:
99 ret = avio_skip(s->pb, 1);
100 dhav->video_codec = avio_r8(s->pb);
101 dhav->frame_rate = avio_r8(s->pb);
102 length -= 4;
103 break;
104 case 0x82:
105 ret = avio_skip(s->pb, 3);
106 dhav->width = avio_rl16(s->pb);
107 dhav->height = avio_rl16(s->pb);
108 length -= 8;
109 break;
110 case 0x83:
111 dhav->audio_channels = avio_r8(s->pb);
112 dhav->audio_codec = avio_r8(s->pb);
113 index = avio_r8(s->pb);
114 if (index < FF_ARRAY_ELEMS(sample_rates)) {
115 dhav->sample_rate = sample_rates[index];
116 } else {
117 dhav->sample_rate = 8000;
118 }
119 length -= 4;
120 break;
121 case 0x88:
122 ret = avio_skip(s->pb, 7);
123 length -= 8;
124 break;
125 case 0x8c:
126 ret = avio_skip(s->pb, 1);
127 dhav->audio_channels = avio_r8(s->pb);
128 dhav->audio_codec = avio_r8(s->pb);
129 index = avio_r8(s->pb);
130 if (index < FF_ARRAY_ELEMS(sample_rates)) {
131 dhav->sample_rate = sample_rates[index];
132 } else {
133 dhav->sample_rate = 8000;
134 }
135 ret = avio_skip(s->pb, 3);
136 length -= 8;
137 break;
138 case 0x91:
139 case 0x92:
140 case 0x93:
141 case 0x95:
142 case 0x9a:
143 case 0x9b: // sample aspect ratio
144 case 0xb3:
145 ret = avio_skip(s->pb, 7);
146 length -= 8;
147 break;
148 case 0x84:
149 case 0x85:
150 case 0x8b:
151 case 0x94:
152 case 0x96:
153 case 0xa0:
154 case 0xb2:
155 case 0xb4:
156 ret = avio_skip(s->pb, 3);
157 length -= 4;
158 break;
159 default:
160 av_log(s, AV_LOG_INFO, "Unknown type: %X, skipping rest of header.\n", type);
161 ret = avio_skip(s->pb, length - 1);
162 length = 0;
163 }
164
165 if (ret < 0)
166 return ret;
167 }
168
169 return 0;
170 }
171
172 static int read_chunk(AVFormatContext *s)
173 {
174 DHAVContext *dhav = s->priv_data;
175 int frame_length, ext_length;
176 int64_t start, end, ret;
177
178 if (avio_feof(s->pb))
179 return AVERROR_EOF;
180
181 while (avio_r8(s->pb) != 'D' || avio_r8(s->pb) != 'H' || avio_r8(s->pb) != 'A' || avio_r8(s->pb) != 'V') {
182 if (avio_feof(s->pb))
183 return AVERROR_EOF;
184 }
185
186 start = avio_tell(s->pb) - 4;
187 dhav->last_good_pos = start;
188 dhav->type = avio_r8(s->pb);
189 dhav->subtype = avio_r8(s->pb);
190 dhav->channel = avio_r8(s->pb);
191 dhav->frame_subnumber = avio_r8(s->pb);
192 dhav->frame_number = avio_rl32(s->pb);
193 frame_length = avio_rl32(s->pb);
194 dhav->date = avio_rl32(s->pb);
195
196 if (frame_length < 24)
197 return AVERROR_INVALIDDATA;
198 if (dhav->type == 0xf1) {
199 ret = avio_skip(s->pb, frame_length - 20);
200 return ret < 0 ? ret : 0;
201 }
202
203 dhav->timestamp = avio_rl16(s->pb);
204 ext_length = avio_r8(s->pb);
205 avio_skip(s->pb, 1); // checksum
206
207 ret = parse_ext(s, ext_length);
208 if (ret < 0)
209 return ret;
210
211 end = avio_tell(s->pb);
212
213 return frame_length - 8 - (end - start);
214 }
215
216 static void get_timeinfo(unsigned date, struct tm *timeinfo)
217 {
218 int year, month, day, hour, min, sec;
219
220 sec = date & 0x3F;
221 min = (date >> 6) & 0x3F;
222 hour = (date >> 12) & 0x1F;
223 day = (date >> 17) & 0x1F;
224 month = (date >> 22) & 0x0F;
225 year = ((date >> 26) & 0x3F) + 2000;
226
227 timeinfo->tm_year = year - 1900;
228 timeinfo->tm_mon = month - 1;
229 timeinfo->tm_mday = day;
230 timeinfo->tm_hour = hour;
231 timeinfo->tm_min = min;
232 timeinfo->tm_sec = sec;
233 }
234
235 static int64_t get_duration(AVFormatContext *s)
236 {
237 DHAVContext *dhav = s->priv_data;
238 int64_t start_pos = avio_tell(s->pb);
239 int64_t start = 0, end = 0;
240 struct tm timeinfo;
241 int max_interations = 100000;
242
243 if (!s->pb->seekable)
244 return 0;
245
246 avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET);
247 while (avio_tell(s->pb) > 12 && max_interations--) {
248 if (avio_rl32(s->pb) == MKTAG('d','h','a','v')) {
249 int64_t seek_back = avio_rl32(s->pb);
250
251 avio_seek(s->pb, -seek_back, SEEK_CUR);
252 read_chunk(s);
253 get_timeinfo(dhav->date, &timeinfo);
254 end = av_timegm(&timeinfo) * 1000LL;
255 break;
256 } else {
257 avio_seek(s->pb, -12, SEEK_CUR);
258 }
259 }
260
261 avio_seek(s->pb, start_pos, SEEK_SET);
262
263 read_chunk(s);
264 get_timeinfo(dhav->date, &timeinfo);
265 start = av_timegm(&timeinfo) * 1000LL;
266
267 avio_seek(s->pb, start_pos, SEEK_SET);
268
269 return end - start;
270 }
271
272 static int dhav_read_header(AVFormatContext *s)
273 {
274 DHAVContext *dhav = s->priv_data;
275 uint8_t signature[5];
276 int ret = ffio_ensure_seekback(s->pb, 5);
277
278 if (ret < 0)
279 return ret;
280
281 ret = ffio_read_size(s->pb, signature, sizeof(signature));
282 if (ret < 0)
283 return ret;
284 if (!memcmp(signature, "DAHUA", 5)) {
285 avio_skip(s->pb, 0x400 - 5);
286 dhav->last_good_pos = avio_tell(s->pb);
287 } else {
288 if (!memcmp(signature, "DHAV", 4)) {
289 avio_seek(s->pb, -5, SEEK_CUR);
290 dhav->last_good_pos = avio_tell(s->pb);
291 } else if (s->pb->seekable) {
292 avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET);
293 while (avio_rl32(s->pb) == MKTAG('d','h','a','v')) {
294 int seek_back;
295
296 seek_back = avio_rl32(s->pb) + 8;
297 if (seek_back < 9)
298 break;
299 dhav->last_good_pos = avio_tell(s->pb);
300 avio_seek(s->pb, -seek_back, SEEK_CUR);
301 }
302 avio_seek(s->pb, dhav->last_good_pos, SEEK_SET);
303 }
304 }
305
306 dhav->duration = get_duration(s);
307 dhav->last_good_pos = avio_tell(s->pb);
308 s->ctx_flags |= AVFMTCTX_NOHEADER;
309 dhav->video_stream_index = -1;
310 dhav->audio_stream_index = -1;
311
312 return 0;
313 }
314
315 static int64_t get_pts(AVFormatContext *s, int stream_index)
316 {
317 DHAVStream *dst = s->streams[stream_index]->priv_data;
318 DHAVContext *dhav = s->priv_data;
319 struct tm timeinfo;
320 time_t t;
321
322 get_timeinfo(dhav->date, &timeinfo);
323
324 t = av_timegm(&timeinfo);
325 if (dst->last_time == t) {
326 int64_t diff = dhav->timestamp - dst->last_timestamp;
327
328 if (diff < 0)
329 diff += 65535;
330 if (diff == 0 && dhav->frame_rate)
331 diff = av_rescale(dhav->frame_number - dst->last_frame_number, 1000, dhav->frame_rate);
332 dst->pts += diff;
333 } else {
334 dst->pts = t * 1000LL;
335 }
336
337 dst->last_time = t;
338 dst->last_timestamp = dhav->timestamp;
339 dst->last_frame_number = dhav->frame_number;
340
341 return dst->pts;
342 }
343
344 static int dhav_read_packet(AVFormatContext *s, AVPacket *pkt)
345 {
346 DHAVContext *dhav = s->priv_data;
347 int size, ret, stream_index;
348
349 retry:
350 while ((ret = read_chunk(s)) == 0)
351 ;
352
353 if (ret < 0)
354 return ret;
355
356 if (dhav->type == 0xfd && dhav->video_stream_index == -1) {
357 AVStream *st = avformat_new_stream(s, NULL);
358 DHAVStream *dst;
359
360 if (!st)
361 return AVERROR(ENOMEM);
362
363 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
364 switch (dhav->video_codec) {
365 case 0x1: st->codecpar->codec_id = AV_CODEC_ID_MPEG4; break;
366 case 0x3: st->codecpar->codec_id = AV_CODEC_ID_MJPEG; break;
367 case 0x2:
368 case 0x4:
369 case 0x8: st->codecpar->codec_id = AV_CODEC_ID_H264; break;
370 case 0xc: st->codecpar->codec_id = AV_CODEC_ID_HEVC; break;
371 default: avpriv_request_sample(s, "Unknown video codec %X", dhav->video_codec);
372 }
373 st->duration = dhav->duration;
374 st->codecpar->width = dhav->width;
375 st->codecpar->height = dhav->height;
376 st->avg_frame_rate.num = dhav->frame_rate;
377 st->avg_frame_rate.den = 1;
378 st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
379 if (!st->priv_data)
380 return AVERROR(ENOMEM);
381 dst->last_time = AV_NOPTS_VALUE;
382 dhav->video_stream_index = st->index;
383
384 avpriv_set_pts_info(st, 64, 1, 1000);
385 } else if (dhav->type == 0xf0 && dhav->audio_stream_index == -1) {
386 AVStream *st = avformat_new_stream(s, NULL);
387 DHAVStream *dst;
388
389 if (!st)
390 return AVERROR(ENOMEM);
391
392 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
393 switch (dhav->audio_codec) {
394 case 0x07: st->codecpar->codec_id = AV_CODEC_ID_PCM_S8; break;
395 case 0x0c: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
396 case 0x10: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
397 case 0x0a: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
398 case 0x16: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
399 case 0x0e: st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW; break;
400 case 0x1a: st->codecpar->codec_id = AV_CODEC_ID_AAC; break;
401 case 0x1f: st->codecpar->codec_id = AV_CODEC_ID_MP2; break;
402 case 0x21: st->codecpar->codec_id = AV_CODEC_ID_MP3; break;
403 case 0x0d: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS; break;
404 default: avpriv_request_sample(s, "Unknown audio codec %X", dhav->audio_codec);
405 }
406 st->duration = dhav->duration;
407 st->codecpar->ch_layout.nb_channels = dhav->audio_channels;
408 st->codecpar->sample_rate = dhav->sample_rate;
409 st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
410 if (!st->priv_data)
411 return AVERROR(ENOMEM);
412 dst->last_time = AV_NOPTS_VALUE;
413 dhav->audio_stream_index = st->index;
414
415 avpriv_set_pts_info(st, 64, 1, 1000);
416 }
417
418 stream_index = dhav->type == 0xf0 ? dhav->audio_stream_index : dhav->video_stream_index;
419 if (stream_index < 0) {
420 avio_skip(s->pb, ret);
421 if (avio_rl32(s->pb) == MKTAG('d','h','a','v'))
422 avio_skip(s->pb, 4);
423 goto retry;
424 }
425
426 size = ret;
427 ret = av_get_packet(s->pb, pkt, size);
428 if (ret < 0)
429 return ret;
430 pkt->stream_index = stream_index;
431 if (dhav->type != 0xfc)
432 pkt->flags |= AV_PKT_FLAG_KEY;
433 pkt->duration = 1;
434 if (pkt->stream_index >= 0)
435 pkt->pts = get_pts(s, pkt->stream_index);
436 pkt->pos = dhav->last_good_pos;
437 if (avio_rl32(s->pb) == MKTAG('d','h','a','v'))
438 avio_skip(s->pb, 4);
439
440 return ret;
441 }
442
443 static int dhav_read_seek(AVFormatContext *s, int stream_index,
444 int64_t timestamp, int flags)
445 {
446 DHAVContext *dhav = s->priv_data;
447 AVStream *st = s->streams[stream_index];
448 FFStream *const sti = ffstream(st);
449 int index = av_index_search_timestamp(st, timestamp, flags);
450 int64_t pts;
451
452 if (index < 0)
453 return -1;
454 pts = sti->index_entries[index].timestamp;
455 if (pts < timestamp)
456 return AVERROR(EAGAIN);
457 if (avio_seek(s->pb, sti->index_entries[index].pos, SEEK_SET) < 0)
458 return -1;
459
460 for (int n = 0; n < s->nb_streams; n++) {
461 AVStream *st = s->streams[n];
462 DHAVStream *dst = st->priv_data;
463
464 dst->pts = pts;
465 dst->last_time = AV_NOPTS_VALUE;
466 }
467 dhav->last_good_pos = avio_tell(s->pb);
468
469 return 0;
470 }
471
472 const FFInputFormat ff_dhav_demuxer = {
473 .p.name = "dhav",
474 .p.long_name = NULL_IF_CONFIG_SMALL("Video DAV"),
475 .p.extensions = "dav",
476 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_TS_DISCONT | AVFMT_TS_NONSTRICT | AVFMT_SEEK_TO_PTS,
477 .priv_data_size = sizeof(DHAVContext),
478 .read_probe = dhav_probe,
479 .read_header = dhav_read_header,
480 .read_packet = dhav_read_packet,
481 .read_seek = dhav_read_seek,
482 };
483