FFmpeg  4.4.9
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 "avio_internal.h"
27 #include "internal.h"
28 #include "riff.h"
29 
30 #define DXA_EXTRA_SIZE 9
31 
32 typedef struct DXAContext {
33  int frames;
34  int has_sound;
35  int bpc;
36  uint32_t bytes_left;
38  int readvid;
39 }DXAContext;
40 
41 static int dxa_probe(const AVProbeData *p)
42 {
43  int w, h;
44  if (p->buf_size < 15)
45  return 0;
46  w = AV_RB16(p->buf + 11);
47  h = AV_RB16(p->buf + 13);
48  /* check file header */
49  if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
50  p->buf[2] == 'X' && p->buf[3] == 'A' &&
51  w && w <= 2048 && h && h <= 2048)
52  return AVPROBE_SCORE_MAX;
53  else
54  return 0;
55 }
56 
58 {
59  AVIOContext *pb = s->pb;
60  DXAContext *c = s->priv_data;
61  AVStream *st, *ast;
62  uint32_t tag;
63  int32_t fps;
64  int w, h;
65  int num, den;
66  int flags;
67  int ret;
68 
69  tag = avio_rl32(pb);
70  if (tag != MKTAG('D', 'E', 'X', 'A'))
71  return AVERROR_INVALIDDATA;
72  flags = avio_r8(pb);
73  c->frames = avio_rb16(pb);
74  if(!c->frames){
75  av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
76  return AVERROR_INVALIDDATA;
77  }
78 
79  fps = avio_rb32(pb);
80  if(fps > 0){
81  den = 1000;
82  num = fps;
83  }else if (fps < 0 && fps > INT_MIN){
84  den = 100000;
85  num = -fps;
86  }else{
87  den = 10;
88  num = 1;
89  }
90  w = avio_rb16(pb);
91  h = avio_rb16(pb);
92  c->has_sound = 0;
93 
94  st = avformat_new_stream(s, NULL);
95  if (!st)
96  return AVERROR(ENOMEM);
97 
98  // Parse WAV data header
99  if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
100  uint32_t size, fsize;
101  c->has_sound = 1;
102  size = avio_rb32(pb);
103  c->vidpos = avio_tell(pb) + size;
104  avio_skip(pb, 16);
105  fsize = avio_rl32(pb);
106 
107  ast = avformat_new_stream(s, NULL);
108  if (!ast)
109  return AVERROR(ENOMEM);
110  ret = ff_get_wav_header(s, pb, ast->codecpar, fsize, 0);
111  if (ret < 0)
112  return ret;
113  if (ast->codecpar->sample_rate > 0)
114  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
115  // find 'data' chunk
116  while(avio_tell(pb) < c->vidpos && !avio_feof(pb)){
117  tag = avio_rl32(pb);
118  fsize = avio_rl32(pb);
119  if(tag == MKTAG('d', 'a', 't', 'a')) break;
120  avio_skip(pb, fsize);
121  }
122  c->bpc = (fsize + (int64_t)c->frames - 1) / c->frames;
123  if (c->bpc < 0)
124  return AVERROR_INVALIDDATA;
125  if(ast->codecpar->block_align) {
126  if (c->bpc > INT_MAX - ast->codecpar->block_align + 1)
127  return AVERROR_INVALIDDATA;
128  c->bpc = ((c->bpc - 1 + ast->codecpar->block_align) / ast->codecpar->block_align) * ast->codecpar->block_align;
129  }
130  c->bytes_left = fsize;
131  c->wavpos = avio_tell(pb);
132  avio_seek(pb, c->vidpos, SEEK_SET);
133  }
134 
135  /* now we are ready: build format streams */
138  st->codecpar->width = w;
139  st->codecpar->height = h;
140  av_reduce(&den, &num, den, num, (1UL<<31)-1);
141  avpriv_set_pts_info(st, 33, num, den);
142  /* flags & 0x80 means that image is interlaced,
143  * flags & 0x40 means that image has double height
144  * either way set true height
145  */
146  if(flags & 0xC0){
147  st->codecpar->height >>= 1;
148  }
149  c->readvid = !c->has_sound;
150  c->vidpos = avio_tell(pb);
151  s->start_time = 0;
152  s->duration = av_rescale(c->frames, AV_TIME_BASE * (int64_t)num, den);
153  av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
154 
155  return 0;
156 }
157 
159 {
160  DXAContext *c = s->priv_data;
161  int ret;
162  uint32_t size;
163  uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
164  int pal_size = 0;
165 
166  if(!c->readvid && c->has_sound && c->bytes_left){
167  c->readvid = 1;
168  avio_seek(s->pb, c->wavpos, SEEK_SET);
169  size = FFMIN(c->bytes_left, c->bpc);
170  ret = av_get_packet(s->pb, pkt, size);
171  pkt->stream_index = 1;
172  if(ret != size)
173  return AVERROR(EIO);
174  c->bytes_left -= size;
175  c->wavpos = avio_tell(s->pb);
176  return 0;
177  }
178  avio_seek(s->pb, c->vidpos, SEEK_SET);
179  while(!avio_feof(s->pb) && c->frames){
180  uint32_t tag;
181  if ((ret = avio_read(s->pb, buf, 4)) != 4) {
182  av_log(s, AV_LOG_ERROR, "failed reading chunk type\n");
183  return ret < 0 ? ret : AVERROR_INVALIDDATA;
184  }
185  tag = AV_RL32(buf);
186  switch (tag) {
187  case MKTAG('N', 'U', 'L', 'L'):
188  if ((ret = av_new_packet(pkt, 4 + pal_size)) < 0)
189  return ret;
190  pkt->stream_index = 0;
191  if(pal_size) memcpy(pkt->data, pal, pal_size);
192  memcpy(pkt->data + pal_size, buf, 4);
193  c->frames--;
194  c->vidpos = avio_tell(s->pb);
195  c->readvid = 0;
196  return 0;
197  case MKTAG('C', 'M', 'A', 'P'):
198  pal_size = 768+4;
199  memcpy(pal, buf, 4);
200  if ((ret = ffio_read_size(s->pb, pal + 4, 768)) < 0)
201  return ret;
202  break;
203  case MKTAG('F', 'R', 'A', 'M'):
204  if ((ret = avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4)) != DXA_EXTRA_SIZE - 4) {
205  av_log(s, AV_LOG_ERROR, "failed reading dxa_extra\n");
206  return ret < 0 ? ret : AVERROR_INVALIDDATA;
207  }
208  size = AV_RB32(buf + 5);
209  if(size > 0xFFFFFF){
210  av_log(s, AV_LOG_ERROR, "Frame size is too big: %"PRIu32"\n",
211  size);
212  return AVERROR_INVALIDDATA;
213  }
214  ret = av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size);
215  if (ret < 0)
216  return ret;
217  memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
218  ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
219  if(ret != size){
220  return AVERROR(EIO);
221  }
222  if(pal_size) memcpy(pkt->data, pal, pal_size);
223  pkt->stream_index = 0;
224  c->frames--;
225  c->vidpos = avio_tell(s->pb);
226  c->readvid = 0;
227  return 0;
228  default:
229  av_log(s, AV_LOG_ERROR, "Unknown tag %s\n", av_fourcc2str(tag));
230  return AVERROR_INVALIDDATA;
231  }
232  }
233  return AVERROR_EOF;
234 }
235 
237  .name = "dxa",
238  .long_name = NULL_IF_CONFIG_SMALL("DXA"),
239  .priv_data_size = sizeof(DXAContext),
243 };
uint8_t
int32_t
static int64_t fsize(FILE *f)
Definition: audiomatch.c:29
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
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:260
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:782
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:371
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:344
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:645
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:766
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:797
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:636
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:698
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RB32
Definition: intreadwrite.h:130
#define AV_RB16
Definition: intreadwrite.h:53
#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 FFMIN(a, b)
Definition: common.h:105
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:555
@ AV_CODEC_ID_DXA
Definition: codec_id.h:147
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
#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_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
static int dxa_probe(const AVProbeData *p)
Definition: dxa.c:41
AVInputFormat ff_dxa_demuxer
Definition: dxa.c:236
static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dxa.c:158
static int dxa_read_header(AVFormatContext *s)
Definition: dxa.c:57
#define DXA_EXTRA_SIZE
Definition: dxa.c:30
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:4944
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
uint8_t w
Definition: llviddspenc.c:39
uint32_t tag
Definition: movenc.c:1611
internal header for RIFF based (de)muxers do NOT include this in end user applications
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:91
int width
Video only.
Definition: codec_par.h:126
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int block_align
Audio only.
Definition: codec_par.h:177
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
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
int stream_index
Definition: packet.h:371
uint8_t * data
Definition: packet.h:369
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
Definition: dxa.c:32
int bpc
Definition: dxa.c:35
int frames
Definition: dxa.c:33
uint32_t bytes_left
Definition: dxa.c:36
int64_t wavpos
Definition: dxa.c:37
int readvid
Definition: dxa.c:38
int has_sound
Definition: dxa.c:34
int64_t vidpos
Definition: dxa.c:37
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
int size
static double c[64]