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