FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/dhav.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 4 297 1.3%
Functions: 1 9 11.1%
Branches: 2 143 1.4%

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