FFmpeg: libavformat/hnm.c Source File

FFmpeg
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   uint8_t version;
41   uint16_t width;
42   uint16_t height;
43   uint32_t filesize;
44   uint32_t frames;
45   uint32_t taboffset;
46   uint16_t bits;
47   uint16_t channels;
48   uint32_t framesize;
49   uint32_t currentframe;
50   int64_t pts;
51   uint32_t superchunk_remaining;
52   AVPacket vpkt;
53 } Hnm4DemuxContext;
54 
55  static int hnm_probe(AVProbeData *p)
56 {
57  if (p->buf_size < 4)
58  return 0;
59 
60  // check for HNM4 header.
61  // currently only HNM v4/v4A is supported
62  if (AV_RL32(&p->buf[0]) == HNM4_TAG)
63  return AVPROBE_SCORE_MAX;
64 
65  return 0;
66 }
67 
68  static int hnm_read_header(AVFormatContext *s)
69 {
70  Hnm4DemuxContext *hnm = s->priv_data;
71  AVIOContext *pb = s->pb;
72  AVStream *vst;
73 
74  /* default context members */
75  hnm->pts = 0;
76  av_init_packet(&hnm->vpkt);
77  hnm->vpkt.data = NULL;
78  hnm->vpkt.size = 0;
79 
80  hnm->superchunk_remaining = 0;
81 
82  avio_skip(pb, 8);
83  hnm->width = avio_rl16(pb);
84  hnm->height = avio_rl16(pb);
85  hnm->filesize = avio_rl32(pb);
86  hnm->frames = avio_rl32(pb);
87  hnm->taboffset = avio_rl32(pb);
88  hnm->bits = avio_rl16(pb);
89  hnm->channels = avio_rl16(pb);
90  hnm->framesize = avio_rl32(pb);
91  avio_skip(pb, 32);
92 
93  hnm->currentframe = 0;
94 
95  if (hnm->width < 256 || hnm->width > 640 ||
96  hnm->height < 150 || hnm->height > 480) {
97  av_log(s, AV_LOG_ERROR,
98  "invalid resolution: %ux%u\n", hnm->width, hnm->height);
99  return AVERROR_INVALIDDATA;
100  }
101 
102  // TODO: find a better way to detect HNM4A
103  if (hnm->width == 640)
104  hnm->version = 0x4a;
105  else
106  hnm->version = 0x40;
107 
108  if (!(vst = avformat_new_stream(s, NULL)))
109  return AVERROR(ENOMEM);
110 
111  vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
112  vst->codecpar->codec_id = AV_CODEC_ID_HNM4_VIDEO;
113  vst->codecpar->codec_tag = 0;
114  vst->codecpar->width = hnm->width;
115  vst->codecpar->height = hnm->height;
116  vst->codecpar->extradata = av_mallocz(1);
117 
118  vst->codecpar->extradata_size = 1;
119  memcpy(vst->codecpar->extradata, &hnm->version, 1);
120 
121  vst->start_time = 0;
122 
123  avpriv_set_pts_info(vst, 33, 1, HNM4_FRAME_FPS);
124 
125  return 0;
126 }
127 
128  static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
129 {
130  Hnm4DemuxContext *hnm = s->priv_data;
131  AVIOContext *pb = s->pb;
132  int ret = 0;
133 
134  uint32_t superchunk_size, chunk_size;
135  uint16_t chunk_id;
136 
137  if (hnm->currentframe == hnm->frames || pb->eof_reached)
138  return AVERROR_EOF;
139 
140  if (hnm->superchunk_remaining == 0) {
141  /* parse next superchunk */
142  superchunk_size = avio_rl24(pb);
143  avio_skip(pb, 1);
144 
145  hnm->superchunk_remaining = superchunk_size - 4;
146  }
147 
148  chunk_size = avio_rl24(pb);
149  avio_skip(pb, 1);
150  chunk_id = avio_rl16(pb);
151  avio_skip(pb, 2);
152 
153  if (chunk_size > hnm->superchunk_remaining || !chunk_size) {
154  av_log(s, AV_LOG_ERROR,
155  "invalid chunk size: %"PRIu32", offset: %"PRId64"\n",
156  chunk_size, avio_tell(pb));
157  avio_skip(pb, hnm->superchunk_remaining - 8);
158  hnm->superchunk_remaining = 0;
159  }
160 
161  switch (chunk_id) {
162  case HNM4_CHUNK_ID_PL:
163  case HNM4_CHUNK_ID_IZ:
164  case HNM4_CHUNK_ID_IU:
165  avio_seek(pb, -8, SEEK_CUR);
166  ret += av_get_packet(pb, pkt, chunk_size);
167  hnm->superchunk_remaining -= chunk_size;
168  if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU)
169  hnm->currentframe++;
170  break;
171 
172  case HNM4_CHUNK_ID_SD:
173  avio_skip(pb, chunk_size - 8);
174  hnm->superchunk_remaining -= chunk_size;
175  break;
176 
177  default:
178  av_log(s, AV_LOG_WARNING, "unknown chunk found: %"PRIu16", offset: %"PRId64"\n",
179  chunk_id, avio_tell(pb));
180  avio_skip(pb, chunk_size - 8);
181  hnm->superchunk_remaining -= chunk_size;
182  break;
183  }
184 
185  return ret;
186 }
187 
188  static int hnm_read_close(AVFormatContext *s)
189 {
190  Hnm4DemuxContext *hnm = s->priv_data;
191 
192  if (hnm->vpkt.size > 0)
193  av_packet_unref(&hnm->vpkt);
194 
195  return 0;
196 }
197 
198  AVInputFormat ff_hnm_demuxer = {
199  .name = "hnm",
200  .long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
201  .priv_data_size = sizeof(Hnm4DemuxContext),
202  .read_probe = hnm_probe,
203  .read_header = hnm_read_header,
204  .read_packet = hnm_read_packet,
205  .read_close = hnm_read_close,
206  .flags = AVFMT_NO_BYTE_SEEK | AVFMT_NOGENSEARCH | AVFMT_NOBINSEARCH
207 };
NULL
#define NULL
Definition: coverity.c:32
AVFMT_NOBINSEARCH
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:475
s
const char * s
Definition: avisynth_c.h:768
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Hnm4DemuxContext::version
uint8_t version
Definition: hnm.c:40
hnm_probe
static int hnm_probe(AVProbeData *p)
Definition: hnm.c:55
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
avpriv_set_pts_info
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:4820
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
AVPacket::size
int size
Definition: avcodec.h:1431
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
av_mallocz
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
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
HNM4_TAG
#define HNM4_TAG
Definition: hnm.c:29
Hnm4DemuxContext::vpkt
AVPacket vpkt
Definition: hnm.c:52
Hnm4DemuxContext::pts
int64_t pts
Definition: hnm.c:50
Hnm4DemuxContext::bits
uint16_t bits
Definition: hnm.c:46
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
Hnm4DemuxContext::frames
uint32_t frames
Definition: hnm.c:44
Hnm4DemuxContext::channels
uint16_t channels
Definition: hnm.c:47
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:3950
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4450
hnm_read_packet
static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hnm.c:128
AVPacket::data
uint8_t * data
Definition: avcodec.h:1430
flags
static int flags
Definition: log.c:55
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
HNM4_CHUNK_ID_IZ
#define HNM4_CHUNK_ID_IZ
Definition: hnm.c:35
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
av_get_packet
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
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
HNM4_FRAME_FPS
#define HNM4_FRAME_FPS
Definition: hnm.c:32
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:770
AVERROR
#define AVERROR(e)
Definition: error.h:43
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3902
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
Hnm4DemuxContext::superchunk_remaining
uint32_t superchunk_remaining
Definition: hnm.c:51
read_probe
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
Hnm4DemuxContext::taboffset
uint32_t taboffset
Definition: hnm.c:45
ff_hnm_demuxer
AVInputFormat ff_hnm_demuxer
Definition: hnm.c:198
hnm_read_header
static int hnm_read_header(AVFormatContext *s)
Definition: hnm.c:68
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
AVStream
Stream structure.
Definition: avformat.h:873
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
hnm_read_close
static int hnm_read_close(AVFormatContext *s)
Definition: hnm.c:188
Hnm4DemuxContext::framesize
uint32_t framesize
Definition: hnm.c:48
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
AVFMT_NOGENSEARCH
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:476
HNM4_CHUNK_ID_PL
#define HNM4_CHUNK_ID_PL
Definition: hnm.c:34
Hnm4DemuxContext::height
uint16_t height
Definition: hnm.c:42
HNM4_CHUNK_ID_IU
#define HNM4_CHUNK_ID_IU
Definition: hnm.c:36
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
Hnm4DemuxContext::width
uint16_t width
Definition: hnm.c:41
Hnm4DemuxContext::filesize
uint32_t filesize
Definition: hnm.c:43
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
Hnm4DemuxContext::currentframe
uint32_t currentframe
Definition: hnm.c:49
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:754
avformat.h
Main libavformat public API header.
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:912
av_init_packet
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
AVFMT_NO_BYTE_SEEK
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:477
AVIOContext::eof_reached
int eof_reached
true if eof reached
Definition: avio.h:239
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1370
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3898
HNM4_CHUNK_ID_SD
#define HNM4_CHUNK_ID_SD
Definition: hnm.c:37
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3888
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:762
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1407

Generated on Sun May 13 2018 02:04:05 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /