libavformat/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 "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "riff.h"
26 
27 #define DXA_EXTRA_SIZE 9
28 
29 typedef struct{
30  int frames;
31  int has_sound;
32  int bpc;
33  uint32_t bytes_left;
34  int64_t wavpos, vidpos;
35  int readvid;
36 }DXAContext;
37 
38 static int dxa_probe(AVProbeData *p)
39 {
40  int w, h;
41  if (p->buf_size < 15)
42  return 0;
43  w = AV_RB16(p->buf + 11);
44  h = AV_RB16(p->buf + 13);
45  /* check file header */
46  if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
47  p->buf[2] == 'X' && p->buf[3] == 'A' &&
48  w && w <= 2048 && h && h <= 2048)
49  return AVPROBE_SCORE_MAX;
50  else
51  return 0;
52 }
53 
55 {
56  AVIOContext *pb = s->pb;
57  DXAContext *c = s->priv_data;
58  AVStream *st, *ast;
59  uint32_t tag;
60  int32_t fps;
61  int w, h;
62  int num, den;
63  int flags;
64  int ret;
65 
66  tag = avio_rl32(pb);
67  if (tag != MKTAG('D', 'E', 'X', 'A'))
68  return AVERROR_INVALIDDATA;
69  flags = avio_r8(pb);
70  c->frames = avio_rb16(pb);
71  if(!c->frames){
72  av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
73  return AVERROR_INVALIDDATA;
74  }
75 
76  fps = avio_rb32(pb);
77  if(fps > 0){
78  den = 1000;
79  num = fps;
80  }else if (fps < 0){
81  den = 100000;
82  num = -fps;
83  }else{
84  den = 10;
85  num = 1;
86  }
87  w = avio_rb16(pb);
88  h = avio_rb16(pb);
89  c->has_sound = 0;
90 
91  st = avformat_new_stream(s, NULL);
92  if (!st)
93  return AVERROR(ENOMEM);
94 
95  // Parse WAV data header
96  if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
97  uint32_t size, fsize;
98  c->has_sound = 1;
99  size = avio_rb32(pb);
100  c->vidpos = avio_tell(pb) + size;
101  avio_skip(pb, 16);
102  fsize = avio_rl32(pb);
103 
104  ast = avformat_new_stream(s, NULL);
105  if (!ast)
106  return AVERROR(ENOMEM);
107  ret = ff_get_wav_header(pb, ast->codec, fsize);
108  if (ret < 0)
109  return ret;
110  if (ast->codec->sample_rate > 0)
111  avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
112  // find 'data' chunk
113  while(avio_tell(pb) < c->vidpos && !url_feof(pb)){
114  tag = avio_rl32(pb);
115  fsize = avio_rl32(pb);
116  if(tag == MKTAG('d', 'a', 't', 'a')) break;
117  avio_skip(pb, fsize);
118  }
119  c->bpc = (fsize + c->frames - 1) / c->frames;
120  if(ast->codec->block_align)
121  c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
122  c->bytes_left = fsize;
123  c->wavpos = avio_tell(pb);
124  avio_seek(pb, c->vidpos, SEEK_SET);
125  }
126 
127  /* now we are ready: build format streams */
130  st->codec->width = w;
131  st->codec->height = h;
132  av_reduce(&den, &num, den, num, (1UL<<31)-1);
133  avpriv_set_pts_info(st, 33, num, den);
134  /* flags & 0x80 means that image is interlaced,
135  * flags & 0x40 means that image has double height
136  * either way set true height
137  */
138  if(flags & 0xC0){
139  st->codec->height >>= 1;
140  }
141  c->readvid = !c->has_sound;
142  c->vidpos = avio_tell(pb);
143  s->start_time = 0;
144  s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
145  av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
146 
147  return 0;
148 }
149 
151 {
152  DXAContext *c = s->priv_data;
153  int ret;
154  uint32_t size;
155  uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
156  int pal_size = 0;
157 
158  if(!c->readvid && c->has_sound && c->bytes_left){
159  c->readvid = 1;
160  avio_seek(s->pb, c->wavpos, SEEK_SET);
161  size = FFMIN(c->bytes_left, c->bpc);
162  ret = av_get_packet(s->pb, pkt, size);
163  pkt->stream_index = 1;
164  if(ret != size)
165  return AVERROR(EIO);
166  c->bytes_left -= size;
167  c->wavpos = avio_tell(s->pb);
168  return 0;
169  }
170  avio_seek(s->pb, c->vidpos, SEEK_SET);
171  while(!url_feof(s->pb) && c->frames){
172  avio_read(s->pb, buf, 4);
173  switch(AV_RL32(buf)){
174  case MKTAG('N', 'U', 'L', 'L'):
175  if(av_new_packet(pkt, 4 + pal_size) < 0)
176  return AVERROR(ENOMEM);
177  pkt->stream_index = 0;
178  if(pal_size) memcpy(pkt->data, pal, pal_size);
179  memcpy(pkt->data + pal_size, buf, 4);
180  c->frames--;
181  c->vidpos = avio_tell(s->pb);
182  c->readvid = 0;
183  return 0;
184  case MKTAG('C', 'M', 'A', 'P'):
185  pal_size = 768+4;
186  memcpy(pal, buf, 4);
187  avio_read(s->pb, pal + 4, 768);
188  break;
189  case MKTAG('F', 'R', 'A', 'M'):
190  avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
191  size = AV_RB32(buf + 5);
192  if(size > 0xFFFFFF){
193  av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
194  return AVERROR_INVALIDDATA;
195  }
196  if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
197  return AVERROR(ENOMEM);
198  memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
199  ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
200  if(ret != size){
201  av_free_packet(pkt);
202  return AVERROR(EIO);
203  }
204  if(pal_size) memcpy(pkt->data, pal, pal_size);
205  pkt->stream_index = 0;
206  c->frames--;
207  c->vidpos = avio_tell(s->pb);
208  c->readvid = 0;
209  return 0;
210  default:
211  av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
212  return AVERROR_INVALIDDATA;
213  }
214  }
215  return AVERROR_EOF;
216 }
217 
219  .name = "dxa",
220  .long_name = NULL_IF_CONFIG_SMALL("DXA"),
221  .priv_data_size = sizeof(DXAContext),
225 };
static int dxa_probe(AVProbeData *p)
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
AVInputFormat ff_dxa_demuxer
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.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:595
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
output residual component w
Format I/O context.
Definition: avformat.h:944
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
#define AV_RB32
static AVPacket pkt
Definition: demuxing.c:56
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
uint32_t tag
Definition: movenc.c:894
int64_t vidpos
#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.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
#define AV_RB16
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:36
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
static int dxa_read_header(AVFormatContext *s)
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:196
#define FFMIN(a, b)
Definition: common.h:58
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
int width
picture width / height.
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
#define AV_RL32
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
int64_t start_time
Decoding: position of the first frame of the component, in AV_TIME_BASE fractional seconds...
Definition: avformat.h:1001
#define DXA_EXTRA_SIZE
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
Main libavformat public API header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
static double c[64]
uint32_t bytes_left
void * priv_data
Format private data.
Definition: avformat.h:964
int64_t duration
Decoding: duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1009
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.
int64_t wavpos