FFmpeg
dhav.c
Go to the documentation of this file.
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;
42  int64_t last_good_pos;
43 
46 } DHAVContext;
47 
48 typedef struct DHAVStream {
49  int64_t last_timestamp;
50  int64_t pts;
51 } DHAVStream;
52 
53 static int dhav_probe(const AVProbeData *p)
54 {
55  if (!memcmp(p->buf, "DAHUA", 5))
56  return AVPROBE_SCORE_MAX;
57 
58  if (memcmp(p->buf, "DHAV", 4))
59  return 0;
60 
61  if (p->buf[4] == 0xf0 ||
62  p->buf[4] == 0xf1 ||
63  p->buf[4] == 0xfc ||
64  p->buf[4] == 0xfd)
65  return AVPROBE_SCORE_MAX;
66  return 0;
67 }
68 
70 {
71  DHAVContext *dhav = s->priv_data;
72  uint8_t signature[5];
73 
74  ffio_ensure_seekback(s->pb, 5);
75  avio_read(s->pb, signature, sizeof(signature));
76  if (!memcmp(signature, "DAHUA", 5)) {
77  avio_skip(s->pb, 0x400 - 5);
78  dhav->last_good_pos = avio_tell(s->pb);
79  } else {
80  if (!memcmp(signature, "DHAV", 4)) {
81  avio_seek(s->pb, -5, SEEK_CUR);
82  dhav->last_good_pos = avio_tell(s->pb);
83  } else if (s->pb->seekable) {
84  avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET);
85  while (avio_rl32(s->pb) == MKTAG('d','h','a','v')) {
86  int seek_back;
87 
88  seek_back = avio_rl32(s->pb) + 8;
89  dhav->last_good_pos = avio_tell(s->pb);
90  if (dhav->last_good_pos < seek_back)
91  break;
92  avio_seek(s->pb, -seek_back, SEEK_CUR);
93  }
94  }
95  }
96 
98  dhav->video_stream_index = -1;
99  dhav->audio_stream_index = -1;
100 
101  return 0;
102 }
103 
104 static int64_t get_pts(AVFormatContext *s, DHAVStream *st)
105 {
106  DHAVContext *dhav = s->priv_data;
107  /*
108  int year, month, day, hour, min, sec;
109  struct tm timeinfo;
110 
111  sec = dhav->date & 0x3F;
112  min = (dhav->date >> 6) & 0x3F;
113  hour = (dhav->date >> 12) & 0x1F;
114  day = (dhav->date >> 17) & 0x1F;
115  month = (dhav->date >> 22) & 0x0F;
116  year = ((dhav->date >> 26) & 0x3F) + 2000;
117 
118  timeinfo.tm_year = year - 1900;
119  timeinfo.tm_mon = month - 1;
120  timeinfo.tm_mday = day;
121  timeinfo.tm_hour = hour;
122  timeinfo.tm_min = min;
123  timeinfo.tm_sec = sec;*/
124 
125  if (st->last_timestamp == AV_NOPTS_VALUE) {
126  st->last_timestamp = dhav->timestamp;
127  }
128 
129  if (st->last_timestamp <= dhav->timestamp) {
130  st->pts += dhav->timestamp - st->last_timestamp;
131  } else {
132  st->pts += 65535 + dhav->timestamp - st->last_timestamp;
133  }
134 
135  st->last_timestamp = dhav->timestamp;
136 
137  return st->pts;
138 }
139 
140 static const uint32_t sample_rates[] = {
141  8000, 4000, 8000, 11025, 16000,
142  20000, 22050, 32000, 44100, 48000,
143  96000, 192000, 64000,
144 };
145 
147 {
148  DHAVContext *dhav = s->priv_data;
149  int index, ret = 0;
150 
151  while (length > 0) {
152  int type = avio_r8(s->pb);
153 
154  switch (type) {
155  case 0x80:
156  ret = avio_skip(s->pb, 1);
157  dhav->width = 8 * avio_r8(s->pb);
158  dhav->height = 8 * avio_r8(s->pb);
159  length -= 4;
160  break;
161  case 0x81:
162  ret = avio_skip(s->pb, 1);
163  dhav->video_codec = avio_r8(s->pb);
164  dhav->frame_rate = avio_r8(s->pb);
165  length -= 4;
166  break;
167  case 0x82:
168  ret = avio_skip(s->pb, 3);
169  dhav->width = avio_rl16(s->pb);
170  dhav->height = avio_rl16(s->pb);
171  length -= 8;
172  break;
173  case 0x83:
174  dhav->audio_channels = avio_r8(s->pb);
175  dhav->audio_codec = avio_r8(s->pb);
176  index = avio_r8(s->pb);
177  if (index < FF_ARRAY_ELEMS(sample_rates)) {
178  dhav->sample_rate = sample_rates[index];
179  } else {
180  dhav->sample_rate = 8000;
181  }
182  length -= 4;
183  break;
184  case 0x88:
185  ret = avio_skip(s->pb, 7);
186  length -= 8;
187  break;
188  case 0x8c:
189  ret = avio_skip(s->pb, 1);
190  dhav->audio_channels = avio_r8(s->pb);
191  dhav->audio_codec = avio_r8(s->pb);
192  index = avio_r8(s->pb);
193  if (index < FF_ARRAY_ELEMS(sample_rates)) {
194  dhav->sample_rate = sample_rates[index];
195  } else {
196  dhav->sample_rate = 8000;
197  }
198  ret = avio_skip(s->pb, 3);
199  length -= 8;
200  break;
201  case 0x91:
202  case 0x92:
203  case 0x93:
204  case 0x95:
205  case 0x9a:
206  case 0x9b: // sample aspect ratio
207  case 0xb3:
208  ret = avio_skip(s->pb, 7);
209  length -= 8;
210  break;
211  case 0x84:
212  case 0x85:
213  case 0x8b:
214  case 0x94:
215  case 0x96:
216  case 0xa0:
217  case 0xb2:
218  case 0xb4:
219  ret = avio_skip(s->pb, 3);
220  length -= 4;
221  break;
222  default:
223  av_log(s, AV_LOG_INFO, "Unknown type: %X, skipping rest of header.\n", type);
224  ret = avio_skip(s->pb, length - 1);
225  length = 0;
226  }
227 
228  if (ret < 0)
229  return ret;
230  }
231 
232  return 0;
233 }
234 
236 {
237  DHAVContext *dhav = s->priv_data;
238  int frame_length, ext_length;
239  int64_t start, end;
240  int ret;
241 
242  if (avio_feof(s->pb))
243  return AVERROR_EOF;
244 
245  if (avio_rl32(s->pb) != MKTAG('D','H','A','V')) {
246  dhav->last_good_pos += 0x8000;
247  avio_seek(s->pb, dhav->last_good_pos, SEEK_SET);
248 
249  while (avio_rl32(s->pb) != MKTAG('D','H','A','V')) {
250  if (avio_feof(s->pb))
251  return AVERROR_EOF;
252  dhav->last_good_pos += 0x8000;
253  ret = avio_skip(s->pb, 0x8000 - 4);
254  if (ret < 0)
255  return ret;
256  }
257  }
258 
259  start = avio_tell(s->pb) - 4;
260  dhav->last_good_pos = start;
261  dhav->type = avio_r8(s->pb);
262  dhav->subtype = avio_r8(s->pb);
263  dhav->channel = avio_r8(s->pb);
264  dhav->frame_subnumber = avio_r8(s->pb);
265  dhav->frame_number = avio_rl32(s->pb);
266  frame_length = avio_rl32(s->pb);
267 
268  if (frame_length < 24)
269  return AVERROR_INVALIDDATA;
270  if (dhav->type == 0xf1) {
271  ret = avio_skip(s->pb, frame_length - 16);
272  return ret < 0 ? ret : 0;
273  }
274 
275  dhav->date = avio_rl32(s->pb);
276  dhav->timestamp = avio_rl16(s->pb);
277  ext_length = avio_r8(s->pb);
278  avio_skip(s->pb, 1); // checksum
279 
280  ret = parse_ext(s, ext_length);
281  if (ret < 0)
282  return ret;
283 
284  end = avio_tell(s->pb);
285 
286  return frame_length - 8 - (end - start);
287 }
288 
290 {
291  DHAVContext *dhav = s->priv_data;
292  int ret, stream_index;
293 
294 retry:
295  while ((ret = read_chunk(s)) == 0)
296  ;
297 
298  if (ret < 0)
299  return ret;
300 
301  if (dhav->type == 0xfd && dhav->video_stream_index == -1) {
303  DHAVStream *dst;
304 
305  if (!st)
306  return AVERROR(ENOMEM);
307 
309  switch (dhav->video_codec) {
310  case 0x1: st->codecpar->codec_id = AV_CODEC_ID_MPEG4; break;
311  case 0x3: st->codecpar->codec_id = AV_CODEC_ID_MJPEG; break;
312  case 0x2:
313  case 0x4:
314  case 0x8: st->codecpar->codec_id = AV_CODEC_ID_H264; break;
315  case 0xc: st->codecpar->codec_id = AV_CODEC_ID_HEVC; break;
316  default: avpriv_request_sample(s, "Unknown video codec %X\n", dhav->video_codec);
317  }
318  st->codecpar->width = dhav->width;
319  st->codecpar->height = dhav->height;
320  st->avg_frame_rate.num = dhav->frame_rate;
321  st->avg_frame_rate.den = 1;
322  st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
323  if (!st->priv_data)
324  return AVERROR(ENOMEM);
326  dhav->video_stream_index = st->index;
327 
328  avpriv_set_pts_info(st, 64, 1, 1000);
329  } else if (dhav->type == 0xf0 && dhav->audio_stream_index == -1) {
331  DHAVStream *dst;
332 
333  if (!st)
334  return AVERROR(ENOMEM);
335 
337  switch (dhav->audio_codec) {
338  case 0x07: st->codecpar->codec_id = AV_CODEC_ID_PCM_S8; break;
339  case 0x0c: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
340  case 0x10: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
341  case 0x0a: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
342  case 0x16: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
343  case 0x0e: st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW; break;
344  case 0x1a: st->codecpar->codec_id = AV_CODEC_ID_AAC; break;
345  case 0x1f: st->codecpar->codec_id = AV_CODEC_ID_MP2; break;
346  case 0x21: st->codecpar->codec_id = AV_CODEC_ID_MP3; break;
347  case 0x0d: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS; break;
348  default: avpriv_request_sample(s, "Unknown audio codec %X\n", dhav->audio_codec);
349  }
350  st->codecpar->channels = dhav->audio_channels;
351  st->codecpar->sample_rate = dhav->sample_rate;
352  st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
353  if (!st->priv_data)
354  return AVERROR(ENOMEM);
356  dhav->audio_stream_index = st->index;
357 
358  avpriv_set_pts_info(st, 64, 1, 1000);
359  }
360 
361  stream_index = dhav->type == 0xf0 ? dhav->audio_stream_index : dhav->video_stream_index;
362  if (stream_index < 0) {
363  avio_skip(s->pb, ret);
364  if (avio_rl32(s->pb) == MKTAG('d','h','a','v'))
365  avio_skip(s->pb, 4);
366  goto retry;
367  }
368 
369  ret = av_get_packet(s->pb, pkt, ret);
370  if (ret < 0)
371  return ret;
372  pkt->stream_index = stream_index;
373  if (dhav->type != 0xfc)
374  pkt->flags |= AV_PKT_FLAG_KEY;
375  if (pkt->stream_index >= 0)
376  pkt->pts = get_pts(s, s->streams[pkt->stream_index]->priv_data);
377  pkt->duration = 1;
378  pkt->pos = dhav->last_good_pos;
379  if (avio_rl32(s->pb) == MKTAG('d','h','a','v'))
380  avio_skip(s->pb, 4);
381 
382  return ret;
383 }
384 
385 static int dhav_read_seek(AVFormatContext *s, int stream_index,
386  int64_t timestamp, int flags)
387 {
388  DHAVContext *dhav = s->priv_data;
389  AVStream *st = s->streams[stream_index];
390  int index = av_index_search_timestamp(st, timestamp, flags);
391  int64_t pts;
392 
393  if (index < 0)
394  return -1;
395  if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
396  return -1;
397 
398  pts = st->index_entries[index].timestamp;
399 
400  for (int n = 0; n < s->nb_streams; n++) {
401  AVStream *st = s->streams[n];
402  DHAVStream *dst = st->priv_data;
403 
404  dst->pts = pts;
406  }
407  dhav->last_good_pos = avio_tell(s->pb);
408 
409  return 0;
410 }
411 
413  .name = "dhav",
414  .long_name = NULL_IF_CONFIG_SMALL("Video DAV"),
415  .priv_data_size = sizeof(DHAVContext),
420  .extensions = "dav",
422 };
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:339
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1500
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4929
int64_t pos
Definition: avformat.h:810
#define avpriv_request_sample(...)
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
static int read_chunk(AVFormatContext *s)
Definition: dhav.c:235
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3968
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:882
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1110
GLint GLenum type
Definition: opengl_enc.c:104
void * priv_data
Definition: avformat.h:896
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:334
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static AVPacket pkt
static const uint32_t sample_rates[]
Definition: dhav.c:140
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1407
Format I/O context.
Definition: avformat.h:1358
unsigned channel
Definition: dhav.c:31
static const char signature[]
Definition: ipmovie.c:615
uint8_t
int width
Video only.
Definition: avcodec.h:4034
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1302
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1498
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4502
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
unsigned date
Definition: dhav.c:34
#define AVERROR_EOF
End of file.
Definition: error.h:55
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:310
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos)
Definition: mpegts.c:3008
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:650
static int dhav_read_header(AVFormatContext *s)
Definition: dhav.c:69
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1512
unsigned subtype
Definition: dhav.c:30
int64_t last_timestamp
Definition: dhav.c:49
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2164
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:772
unsigned type
Definition: dhav.c:29
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:811
int64_t pts
Definition: dhav.c:50
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int audio_channels
Definition: dhav.c:39
static int dhav_probe(const AVProbeData *p)
Definition: dhav.c:53
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:566
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3964
static int64_t get_pts(AVFormatContext *s, DHAVStream *st)
Definition: dhav.c:104
GLsizei GLsizei * length
Definition: opengl_enc.c:114
int video_codec
Definition: dhav.c:37
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:954
AVInputFormat ff_dhav_demuxer
Definition: dhav.c:412
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1486
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:641
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1414
unsigned timestamp
Definition: dhav.c:35
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
static int dhav_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dhav.c:289
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:760
int video_stream_index
Definition: dhav.c:44
int width
Definition: dhav.c:36
#define FF_ARRAY_ELEMS(a)
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
Stream structure.
Definition: avformat.h:881
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
static int dhav_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: dhav.c:385
unsigned frame_number
Definition: dhav.c:33
int64_t last_good_pos
Definition: dhav.c:42
int sample_rate
Definition: dhav.c:41
int index
Definition: gxfenc.c:89
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:468
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
misc parsing utilities
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:561
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1054
int sample_rate
Audio only.
Definition: avcodec.h:4078
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:756
Main libavformat public API header.
int den
Denominator.
Definition: rational.h:60
int audio_stream_index
Definition: dhav.c:45
unsigned frame_subnumber
Definition: dhav.c:32
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:475
void * priv_data
Format private data.
Definition: avformat.h:1386
int audio_codec
Definition: dhav.c:40
int height
Definition: dhav.c:36
int channels
Audio only.
Definition: avcodec.h:4074
void INT64 start
Definition: avisynth_c.h:766
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:361
int stream_index
Definition: avcodec.h:1482
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
#define MKTAG(a, b, c, d)
Definition: common.h:366
int frame_rate
Definition: dhav.c:38
This structure stores compressed data.
Definition: avcodec.h:1457
static int parse_ext(AVFormatContext *s, int length)
Definition: dhav.c:146
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1473
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248