FFmpeg
dxa.c
Go to the documentation of this file.
1 /*
2  * DXA demuxer
3  * Copyright (c) 2007 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <inttypes.h>
23 
24 #include "libavutil/intreadwrite.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "riff.h"
28 
29 #define DXA_EXTRA_SIZE 9
30 
31 typedef struct DXAContext {
32  int frames;
33  int has_sound;
34  int bpc;
35  uint32_t bytes_left;
36  int64_t wavpos, vidpos;
37  int readvid;
38 }DXAContext;
39 
40 static int dxa_probe(const AVProbeData *p)
41 {
42  int w, h;
43  if (p->buf_size < 15)
44  return 0;
45  w = AV_RB16(p->buf + 11);
46  h = AV_RB16(p->buf + 13);
47  /* check file header */
48  if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
49  p->buf[2] == 'X' && p->buf[3] == 'A' &&
50  w && w <= 2048 && h && h <= 2048)
51  return AVPROBE_SCORE_MAX;
52  else
53  return 0;
54 }
55 
57 {
58  AVIOContext *pb = s->pb;
59  DXAContext *c = s->priv_data;
60  AVStream *st, *ast;
61  uint32_t tag;
62  int32_t fps;
63  int w, h;
64  int num, den;
65  int flags;
66  int ret;
67 
68  tag = avio_rl32(pb);
69  if (tag != MKTAG('D', 'E', 'X', 'A'))
70  return AVERROR_INVALIDDATA;
71  flags = avio_r8(pb);
72  c->frames = avio_rb16(pb);
73  if(!c->frames){
74  av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
75  return AVERROR_INVALIDDATA;
76  }
77 
78  fps = avio_rb32(pb);
79  if(fps > 0){
80  den = 1000;
81  num = fps;
82  }else if (fps < 0){
83  den = 100000;
84  num = -fps;
85  }else{
86  den = 10;
87  num = 1;
88  }
89  w = avio_rb16(pb);
90  h = avio_rb16(pb);
91  c->has_sound = 0;
92 
93  st = avformat_new_stream(s, NULL);
94  if (!st)
95  return AVERROR(ENOMEM);
96 
97  // Parse WAV data header
98  if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
99  uint32_t size, fsize;
100  c->has_sound = 1;
101  size = avio_rb32(pb);
102  c->vidpos = avio_tell(pb) + size;
103  avio_skip(pb, 16);
104  fsize = avio_rl32(pb);
105 
106  ast = avformat_new_stream(s, NULL);
107  if (!ast)
108  return AVERROR(ENOMEM);
109  ret = ff_get_wav_header(s, pb, ast->codecpar, fsize, 0);
110  if (ret < 0)
111  return ret;
112  if (ast->codecpar->sample_rate > 0)
113  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
114  // find 'data' chunk
115  while(avio_tell(pb) < c->vidpos && !avio_feof(pb)){
116  tag = avio_rl32(pb);
117  fsize = avio_rl32(pb);
118  if(tag == MKTAG('d', 'a', 't', 'a')) break;
119  avio_skip(pb, fsize);
120  }
121  c->bpc = (fsize + c->frames - 1) / c->frames;
122  if(ast->codecpar->block_align)
123  c->bpc = ((c->bpc + ast->codecpar->block_align - 1) / ast->codecpar->block_align) * ast->codecpar->block_align;
124  c->bytes_left = fsize;
125  c->wavpos = avio_tell(pb);
126  avio_seek(pb, c->vidpos, SEEK_SET);
127  }
128 
129  /* now we are ready: build format streams */
132  st->codecpar->width = w;
133  st->codecpar->height = h;
134  av_reduce(&den, &num, den, num, (1UL<<31)-1);
135  avpriv_set_pts_info(st, 33, num, den);
136  /* flags & 0x80 means that image is interlaced,
137  * flags & 0x40 means that image has double height
138  * either way set true height
139  */
140  if(flags & 0xC0){
141  st->codecpar->height >>= 1;
142  }
143  c->readvid = !c->has_sound;
144  c->vidpos = avio_tell(pb);
145  s->start_time = 0;
146  s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
147  av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
148 
149  return 0;
150 }
151 
153 {
154  DXAContext *c = s->priv_data;
155  int ret;
156  uint32_t size;
157  uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
158  int pal_size = 0;
159 
160  if(!c->readvid && c->has_sound && c->bytes_left){
161  c->readvid = 1;
162  avio_seek(s->pb, c->wavpos, SEEK_SET);
163  size = FFMIN(c->bytes_left, c->bpc);
164  ret = av_get_packet(s->pb, pkt, size);
165  pkt->stream_index = 1;
166  if(ret != size)
167  return AVERROR(EIO);
168  c->bytes_left -= size;
169  c->wavpos = avio_tell(s->pb);
170  return 0;
171  }
172  avio_seek(s->pb, c->vidpos, SEEK_SET);
173  while(!avio_feof(s->pb) && c->frames){
174  uint32_t tag;
175  if ((ret = avio_read(s->pb, buf, 4)) != 4) {
176  av_log(s, AV_LOG_ERROR, "failed reading chunk type\n");
177  return ret < 0 ? ret : AVERROR_INVALIDDATA;
178  }
179  tag = AV_RL32(buf);
180  switch (tag) {
181  case MKTAG('N', 'U', 'L', 'L'):
182  if(av_new_packet(pkt, 4 + pal_size) < 0)
183  return AVERROR(ENOMEM);
184  pkt->stream_index = 0;
185  if(pal_size) memcpy(pkt->data, pal, pal_size);
186  memcpy(pkt->data + pal_size, buf, 4);
187  c->frames--;
188  c->vidpos = avio_tell(s->pb);
189  c->readvid = 0;
190  return 0;
191  case MKTAG('C', 'M', 'A', 'P'):
192  pal_size = 768+4;
193  memcpy(pal, buf, 4);
194  avio_read(s->pb, pal + 4, 768);
195  break;
196  case MKTAG('F', 'R', 'A', 'M'):
197  if ((ret = avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4)) != DXA_EXTRA_SIZE - 4) {
198  av_log(s, AV_LOG_ERROR, "failed reading dxa_extra\n");
199  return ret < 0 ? ret : AVERROR_INVALIDDATA;
200  }
201  size = AV_RB32(buf + 5);
202  if(size > 0xFFFFFF){
203  av_log(s, AV_LOG_ERROR, "Frame size is too big: %"PRIu32"\n",
204  size);
205  return AVERROR_INVALIDDATA;
206  }
207  if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
208  return AVERROR(ENOMEM);
209  memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
210  ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
211  if(ret != size){
212  av_packet_unref(pkt);
213  return AVERROR(EIO);
214  }
215  if(pal_size) memcpy(pkt->data, pal, pal_size);
216  pkt->stream_index = 0;
217  c->frames--;
218  c->vidpos = avio_tell(s->pb);
219  c->readvid = 0;
220  return 0;
221  default:
222  av_log(s, AV_LOG_ERROR, "Unknown tag %s\n", av_fourcc2str(tag));
223  return AVERROR_INVALIDDATA;
224  }
225  }
226  return AVERROR_EOF;
227 }
228 
230  .name = "dxa",
231  .long_name = NULL_IF_CONFIG_SMALL("DXA"),
232  .priv_data_size = sizeof(DXAContext),
236 };
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVInputFormat ff_dxa_demuxer
Definition: dxa.c:229
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
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3968
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
int readvid
Definition: dxa.c:37
static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dxa.c:152
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:334
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:788
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
Format I/O context.
Definition: avformat.h:1358
uint8_t
int width
Video only.
Definition: avcodec.h:4034
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:803
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4502
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
uint8_t * data
Definition: avcodec.h:1480
uint32_t tag
Definition: movenc.c:1531
int64_t vidpos
Definition: dxa.c:36
#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
int has_sound
Definition: dxa.c:33
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
#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
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:91
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:772
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3964
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:641
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:449
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
int block_align
Audio only.
Definition: avcodec.h:4085
static int dxa_read_header(AVFormatContext *s)
Definition: dxa.c:56
int frames
Definition: dxa.c:32
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define FFMIN(a, b)
Definition: common.h:96
uint8_t w
Definition: llviddspenc.c:38
Definition: dxa.c:31
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
#define s(width, name)
Definition: cbs_vp9.c:257
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
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
void * buf
Definition: avisynth_c.h:766
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1463
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
#define DXA_EXTRA_SIZE
Definition: dxa.c:29
int sample_rate
Audio only.
Definition: avcodec.h:4078
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
Main libavformat public API header.
static int dxa_probe(const AVProbeData *p)
Definition: dxa.c:40
uint32_t bytes_left
Definition: dxa.c:35
void * priv_data
Format private data.
Definition: avformat.h:1386
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1473
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
static int64_t fsize(FILE *f)
Definition: audiomatch.c:28
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
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1457
int bpc
Definition: dxa.c:34
int64_t wavpos
Definition: dxa.c:36