FFmpeg  4.0
aixdec.c
Go to the documentation of this file.
1 /*
2  * AIX demuxer
3  * Copyright (c) 2016 Paul B Mahol
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 
26 static int aix_probe(AVProbeData *p)
27 {
28  if (AV_RL32(p->buf) != MKTAG('A','I','X','F') ||
29  AV_RB32(p->buf + 8) != 0x01000014 ||
30  AV_RB32(p->buf + 12) != 0x00000800)
31  return 0;
32 
33  return AVPROBE_SCORE_MAX;
34 }
35 
37 {
38  unsigned nb_streams, first_offset, nb_segments;
39  unsigned stream_list_offset;
40  unsigned segment_list_offset = 0x20;
41  unsigned segment_list_entry_size = 0x10;
42  unsigned size;
43  int i;
44 
45  avio_skip(s->pb, 4);
46  first_offset = avio_rb32(s->pb) + 8;
47  avio_skip(s->pb, 16);
48  nb_segments = avio_rb16(s->pb);
49  if (nb_segments == 0)
50  return AVERROR_INVALIDDATA;
51  stream_list_offset = segment_list_offset + segment_list_entry_size * nb_segments + 0x10;
52  if (stream_list_offset >= first_offset)
53  return AVERROR_INVALIDDATA;
54  avio_seek(s->pb, stream_list_offset, SEEK_SET);
55  nb_streams = avio_r8(s->pb);
56  if (nb_streams == 0)
57  return AVERROR_INVALIDDATA;
58  avio_skip(s->pb, 7);
59  for (i = 0; i < nb_streams; i++) {
61 
62  if (!st)
63  return AVERROR(ENOMEM);
66  st->codecpar->sample_rate = avio_rb32(s->pb);
67  st->codecpar->channels = avio_r8(s->pb);
68  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
69  avio_skip(s->pb, 3);
70  }
71 
72  avio_seek(s->pb, first_offset, SEEK_SET);
73  for (i = 0; i < nb_streams; i++) {
74  if (avio_rl32(s->pb) != MKTAG('A','I','X','P'))
75  return AVERROR_INVALIDDATA;
76  size = avio_rb32(s->pb);
77  if (size <= 8)
78  return AVERROR_INVALIDDATA;
79  avio_skip(s->pb, 8);
80  ff_get_extradata(s, s->streams[i]->codecpar, s->pb, size - 8);
81  }
82 
83  return 0;
84 }
85 
87 {
88  unsigned size, index, duration, chunk;
89  int64_t pos;
90  int sequence, ret, i;
91 
92  pos = avio_tell(s->pb);
93  if (avio_feof(s->pb))
94  return AVERROR_EOF;
95  chunk = avio_rl32(s->pb);
96  size = avio_rb32(s->pb);
97  if (chunk == MKTAG('A','I','X','E')) {
98  avio_skip(s->pb, size);
99  for (i = 0; i < s->nb_streams; i++) {
100  if (avio_feof(s->pb))
101  return AVERROR_EOF;
102  chunk = avio_rl32(s->pb);
103  size = avio_rb32(s->pb);
104  avio_skip(s->pb, size);
105  }
106  pos = avio_tell(s->pb);
107  chunk = avio_rl32(s->pb);
108  size = avio_rb32(s->pb);
109  }
110 
111  if (chunk != MKTAG('A','I','X','P'))
112  return AVERROR_INVALIDDATA;
113  if (size <= 8)
114  return AVERROR_INVALIDDATA;
115  index = avio_r8(s->pb);
116  if (avio_r8(s->pb) != s->nb_streams || index >= s->nb_streams)
117  return AVERROR_INVALIDDATA;
118  duration = avio_rb16(s->pb);
119  sequence = avio_rb32(s->pb);
120  if (sequence < 0) {
121  avio_skip(s->pb, size - 8);
122  return 0;
123  }
124 
125  ret = av_get_packet(s->pb, pkt, size - 8);
126  pkt->stream_index = index;
127  pkt->duration = duration;
128  pkt->pos = pos;
129  return ret;
130 }
131 
133  .name = "aix",
134  .long_name = NULL_IF_CONFIG_SMALL("CRI AIX"),
135  .read_probe = aix_probe,
136  .read_header = aix_read_header,
137  .read_packet = aix_read_packet,
138  .extensions = "aix",
139  .flags = AVFMT_GENERIC_INDEX,
140 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1450
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:4811
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:786
Format I/O context.
Definition: avformat.h:1342
static int nb_streams
Definition: ffprobe.c:276
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:801
#define AV_RB32
Definition: intreadwrite.h:130
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1448
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4441
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
int64_t duration
Definition: movenc.c:63
static int aix_probe(AVProbeData *p)
Definition: aixdec.c:26
static int aix_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: aixdec.c:86
#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
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:770
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
AVInputFormat ff_aix_demuxer
Definition: aixdec.c:132
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:639
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1398
#define AV_RL32
Definition: intreadwrite.h:146
Stream structure.
Definition: avformat.h:873
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
int index
Definition: gxfenc.c:89
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:470
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
int sample_rate
Audio only.
Definition: avcodec.h:3994
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
Main libavformat public API header.
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:3289
static int aix_read_header(AVFormatContext *s)
Definition: aixdec.c:36
int channels
Audio only.
Definition: avcodec.h:3990
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
int stream_index
Definition: avcodec.h:1432
#define MKTAG(a, b, c, d)
Definition: common.h:366
This structure stores compressed data.
Definition: avcodec.h:1407