FFmpeg  4.4.6
hnm.c
Go to the documentation of this file.
1 /*
2  * Cryo Interactive Entertainment HNM4 demuxer
3  *
4  * Copyright (c) 2012 David Kment
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 <inttypes.h>
24 
25 #include "libavutil/intreadwrite.h"
26 #include "avformat.h"
27 #include "internal.h"
28 
29 #define HNM4_TAG MKTAG('H', 'N', 'M', '4')
30 
31 #define HNM4_SAMPLE_RATE 22050
32 #define HNM4_FRAME_FPS 24
33 
34 #define HNM4_CHUNK_ID_PL 19536
35 #define HNM4_CHUNK_ID_IZ 23113
36 #define HNM4_CHUNK_ID_IU 21833
37 #define HNM4_CHUNK_ID_SD 17491
38 
39 typedef struct Hnm4DemuxContext {
40  uint32_t frames;
41  uint32_t currentframe;
44 
45 static int hnm_probe(const AVProbeData *p)
46 {
47  if (p->buf_size < 4)
48  return 0;
49 
50  // check for HNM4 header.
51  // currently only HNM v4/v4A is supported
52  if (AV_RL32(&p->buf[0]) == HNM4_TAG)
53  return AVPROBE_SCORE_MAX;
54 
55  return 0;
56 }
57 
59 {
60  Hnm4DemuxContext *hnm = s->priv_data;
61  AVIOContext *pb = s->pb;
62  unsigned width, height;
63  AVStream *vst;
64  int ret;
65 
66  avio_skip(pb, 8);
67  width = avio_rl16(pb);
68  height = avio_rl16(pb);
69  avio_rl32(pb); // filesize
70  hnm->frames = avio_rl32(pb);
71  avio_skip(pb, 44);
72 
73  if (width < 256 || width > 640 ||
74  height < 150 || height > 480) {
76  "invalid resolution: %ux%u\n", width, height);
77  return AVERROR_INVALIDDATA;
78  }
79 
80  if (!(vst = avformat_new_stream(s, NULL)))
81  return AVERROR(ENOMEM);
82 
85  vst->codecpar->codec_tag = 0;
86  vst->codecpar->width = width;
87  vst->codecpar->height = height;
88  if ((ret = ff_alloc_extradata(vst->codecpar, 1)) < 0)
89  return ret;
90 
91  // TODO: find a better way to detect HNM4A
92  vst->codecpar->extradata[0] = width == 640 ? 0x4a : 0x40;
93 
94  vst->start_time = 0;
95 
97 
98  return 0;
99 }
100 
102 {
103  Hnm4DemuxContext *hnm = s->priv_data;
104  AVIOContext *pb = s->pb;
105  int ret = 0;
106 
107  uint32_t superchunk_size, chunk_size;
108  uint16_t chunk_id;
109 
110  if (hnm->currentframe == hnm->frames || pb->eof_reached)
111  return AVERROR_EOF;
112 
113  if (hnm->superchunk_remaining == 0) {
114  /* parse next superchunk */
115  superchunk_size = avio_rl24(pb);
116  if (superchunk_size < 4)
117  return AVERROR_INVALIDDATA;
118  avio_skip(pb, 1);
119 
120  hnm->superchunk_remaining = superchunk_size - 4;
121  }
122 
123  chunk_size = avio_rl24(pb);
124  avio_skip(pb, 1);
125  chunk_id = avio_rl16(pb);
126  avio_skip(pb, 2);
127 
128  if (chunk_size > hnm->superchunk_remaining || chunk_size < 8) {
130  "invalid chunk size: %"PRIu32", offset: %"PRId64"\n",
131  chunk_size, avio_tell(pb));
132  avio_skip(pb, hnm->superchunk_remaining - 8);
133  hnm->superchunk_remaining = 0;
134  }
135 
136  switch (chunk_id) {
137  case HNM4_CHUNK_ID_PL:
138  case HNM4_CHUNK_ID_IZ:
139  case HNM4_CHUNK_ID_IU:
140  avio_seek(pb, -8, SEEK_CUR);
141  ret += av_get_packet(pb, pkt, chunk_size);
142  hnm->superchunk_remaining -= chunk_size;
143  if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU)
144  hnm->currentframe++;
145  break;
146 
147  case HNM4_CHUNK_ID_SD:
148  avio_skip(pb, chunk_size - 8);
149  hnm->superchunk_remaining -= chunk_size;
150  break;
151 
152  default:
153  av_log(s, AV_LOG_WARNING, "unknown chunk found: %"PRIu16", offset: %"PRId64"\n",
154  chunk_id, avio_tell(pb));
155  avio_skip(pb, chunk_size - 8);
156  hnm->superchunk_remaining -= chunk_size;
157  break;
158  }
159 
160  return ret;
161 }
162 
164  .name = "hnm",
165  .long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
166  .priv_data_size = sizeof(Hnm4DemuxContext),
171 };
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:470
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:469
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:468
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
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:734
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:750
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:742
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RL32
Definition: intreadwrite.h:146
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define NULL
Definition: coverity.c:32
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
@ AV_CODEC_ID_HNM4_VIDEO
Definition: codec_id.h:222
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hnm.c:101
#define HNM4_CHUNK_ID_PL
Definition: hnm.c:34
static int hnm_probe(const AVProbeData *p)
Definition: hnm.c:45
#define HNM4_CHUNK_ID_IU
Definition: hnm.c:36
#define HNM4_CHUNK_ID_IZ
Definition: hnm.c:35
static int hnm_read_header(AVFormatContext *s)
Definition: hnm.c:58
#define HNM4_TAG
Definition: hnm.c:29
AVInputFormat ff_hnm_demuxer
Definition: hnm.c:163
#define HNM4_FRAME_FPS
Definition: hnm.c:32
#define HNM4_CHUNK_ID_SD
Definition: hnm.c:37
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:4945
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:3314
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
int width
Video only.
Definition: codec_par.h:126
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:912
uint32_t frames
Definition: hnm.c:40
uint32_t superchunk_remaining
Definition: hnm.c:42
uint32_t currentframe
Definition: hnm.c:41
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width