FFmpeg
Loading...
Searching...
No Matches
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
25#include "avformat.h"
26#include "avio_internal.h"
27#include "demux.h"
28#include "internal.h"
29#include "riff.h"
30
31#define DXA_EXTRA_SIZE 9
32
41
42static int dxa_probe(const AVProbeData *p)
43{
44 int w, h;
45 if (p->buf_size < 15)
46 return 0;
47 w = AV_RB16(p->buf + 11);
48 h = AV_RB16(p->buf + 13);
49 /* check file header */
50 if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
51 p->buf[2] == 'X' && p->buf[3] == 'A' &&
52 w && w <= 2048 && h && h <= 2048)
53 return AVPROBE_SCORE_MAX;
54 else
55 return 0;
56}
57
59{
60 AVIOContext *pb = s->pb;
61 DXAContext *c = s->priv_data;
62 AVStream *st, *ast;
63 uint32_t tag;
64 int32_t fps;
65 int w, h;
66 int num, den;
67 int flags;
68 int ret;
69
70 tag = avio_rl32(pb);
71 if (tag != MKTAG('D', 'E', 'X', 'A'))
73 flags = avio_r8(pb);
74 c->frames = avio_rb16(pb);
75 if(!c->frames){
76 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
78 }
79
80 fps = avio_rb32(pb);
81 if(fps > 0){
82 den = 1000;
83 num = fps;
84 }else if (fps < 0 && fps > INT_MIN){
85 den = 100000;
86 num = -fps;
87 }else{
88 den = 10;
89 num = 1;
90 }
91 w = avio_rb16(pb);
92 h = avio_rb16(pb);
93 c->has_sound = 0;
94
96 if (!st)
97 return AVERROR(ENOMEM);
98
99 // Parse WAV data header
100 if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
101 uint32_t size, fsize;
102 c->has_sound = 1;
103 size = avio_rb32(pb);
104 c->vidpos = avio_tell(pb) + size;
105 avio_skip(pb, 16);
106 fsize = avio_rl32(pb);
107
108 ast = avformat_new_stream(s, NULL);
109 if (!ast)
110 return AVERROR(ENOMEM);
111 ret = ff_get_wav_header(s, pb, ast->codecpar, fsize, 0);
112 if (ret < 0)
113 return ret;
114 if (ast->codecpar->sample_rate > 0)
115 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
116 // find 'data' chunk
117 while(avio_tell(pb) < c->vidpos && !avio_feof(pb)){
118 tag = avio_rl32(pb);
119 fsize = avio_rl32(pb);
120 if(tag == MKTAG('d', 'a', 't', 'a')) break;
121 avio_skip(pb, fsize);
122 }
123 c->bpc = (fsize + (int64_t)c->frames - 1) / c->frames;
124 if (c->bpc < 0)
125 return AVERROR_INVALIDDATA;
126 if(ast->codecpar->block_align) {
127 if (c->bpc > INT_MAX - ast->codecpar->block_align + 1)
128 return AVERROR_INVALIDDATA;
129 c->bpc = ((c->bpc - 1 + ast->codecpar->block_align) / ast->codecpar->block_align) * ast->codecpar->block_align;
130 }
131 c->bytes_left = fsize;
132 c->wavpos = avio_tell(pb);
133 avio_seek(pb, c->vidpos, SEEK_SET);
134 }
135
136 /* now we are ready: build format streams */
139 st->codecpar->width = w;
140 st->codecpar->height = h;
141 av_reduce(&den, &num, den, num, (1UL<<31)-1);
142 avpriv_set_pts_info(st, 33, num, den);
143 /* flags & 0x80 means that image is interlaced,
144 * flags & 0x40 means that image has double height
145 * either way set true height
146 */
147 if(flags & 0xC0){
148 st->codecpar->height >>= 1;
149 }
150 c->readvid = !c->has_sound;
151 c->vidpos = avio_tell(pb);
152 s->start_time = 0;
153 s->duration = av_rescale(c->frames, AV_TIME_BASE * (int64_t)num, den);
154 av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
155
156 return 0;
157}
158
160{
161 DXAContext *c = s->priv_data;
162 int ret;
163 uint32_t size;
164 uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
165 int pal_size = 0;
166
167 if(!c->readvid && c->has_sound && c->bytes_left){
168 c->readvid = 1;
169 avio_seek(s->pb, c->wavpos, SEEK_SET);
170 size = FFMIN(c->bytes_left, c->bpc);
171 ret = av_get_packet(s->pb, pkt, size);
172 pkt->stream_index = 1;
173 if(ret != size)
174 return AVERROR_INVALIDDATA;
175 c->bytes_left -= size;
176 c->wavpos = avio_tell(s->pb);
177 return 0;
178 }
179 avio_seek(s->pb, c->vidpos, SEEK_SET);
180 while(!avio_feof(s->pb) && c->frames){
181 uint32_t tag;
182 if ((ret = avio_read(s->pb, buf, 4)) != 4) {
183 av_log(s, AV_LOG_ERROR, "failed reading chunk type\n");
184 return ret < 0 ? ret : AVERROR_INVALIDDATA;
185 }
186 tag = AV_RL32(buf);
187 switch (tag) {
188 case MKTAG('N', 'U', 'L', 'L'):
189 if ((ret = av_new_packet(pkt, 4 + pal_size)) < 0)
190 return ret;
191 pkt->stream_index = 0;
192 if(pal_size) memcpy(pkt->data, pal, pal_size);
193 memcpy(pkt->data + pal_size, buf, 4);
194 c->frames--;
195 c->vidpos = avio_tell(s->pb);
196 c->readvid = 0;
197 return 0;
198 case MKTAG('C', 'M', 'A', 'P'):
199 pal_size = 768+4;
200 memcpy(pal, buf, 4);
201 avio_read(s->pb, pal + 4, 768);
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 = ffio_read_size(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
219 if (ret < 0)
220 return ret;
221 if(pal_size) memcpy(pkt->data, pal, pal_size);
222 pkt->stream_index = 0;
223 c->frames--;
224 c->vidpos = avio_tell(s->pb);
225 c->readvid = 0;
226 return 0;
227 default:
228 av_log(s, AV_LOG_ERROR, "Unknown tag %s\n", av_fourcc2str(tag));
229 return AVERROR_INVALIDDATA;
230 }
231 }
232 return AVERROR_EOF;
233}
234
236 .p.name = "dxa",
237 .p.long_name = NULL_IF_CONFIG_SMALL("DXA"),
238 .priv_data_size = sizeof(DXAContext),
242};
const FFInputFormat ff_dxa_demuxer
Definition dxa.c:235
int32_t
static int64_t fsize(FILE *f)
Definition audiomatch.c:29
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
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:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_DXA
Definition codec_id.h:148
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
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.
#define av_fourcc2str(fourcc)
Definition avutil.h:323
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_RB16(p)
static int dxa_probe(const AVProbeData *p)
Definition dxa.c:42
static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition dxa.c:159
static int dxa_read_header(AVFormatContext *s)
Definition dxa.c:58
#define DXA_EXTRA_SIZE
Definition dxa.c:31
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
uint32_t tag
Definition movenc.c:2087
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:141
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int bpc
Definition dxa.c:36
int frames
Definition dxa.c:34
uint32_t bytes_left
Definition dxa.c:37
int64_t wavpos
Definition dxa.c:38
int readvid
Definition dxa.c:39
int has_sound
Definition dxa.c:35
int64_t vidpos
Definition dxa.c:38
#define av_log(a,...)
int size
static double c[64]