FFmpeg
c93.c
Go to the documentation of this file.
1 /*
2  * Interplay C93 demuxer
3  * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
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 "avformat.h"
23 #include "demux.h"
24 #include "internal.h"
25 #include "voc.h"
26 #include "libavutil/intreadwrite.h"
27 
28 typedef struct C93BlockRecord {
29  uint16_t index;
30  uint8_t length;
31  uint8_t frames;
33 
34 typedef struct C93DemuxContext {
35  VocDecContext voc;
36 
39 
40  uint32_t frame_offsets[32];
43 
46 
47 static int probe(const AVProbeData *p)
48 {
49  int i;
50  int index = 1;
51  if (p->buf_size < 16)
52  return 0;
53  for (i = 0; i < 16; i += 4) {
54  if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3])
55  return 0;
56  index += p->buf[i + 2];
57  }
58  return AVPROBE_SCORE_MAX;
59 }
60 
62 {
63  AVStream *video;
64  AVIOContext *pb = s->pb;
65  C93DemuxContext *c93 = s->priv_data;
66  int i;
67  int framecount = 0;
68 
69  for (i = 0; i < 512; i++) {
70  c93->block_records[i].index = avio_rl16(pb);
71  c93->block_records[i].length = avio_r8(pb);
72  c93->block_records[i].frames = avio_r8(pb);
73  if (c93->block_records[i].frames > 32) {
74  av_log(s, AV_LOG_ERROR, "too many frames in block\n");
75  return AVERROR_INVALIDDATA;
76  }
77  framecount += c93->block_records[i].frames;
78  }
79 
80  /* Audio streams are added if audio packets are found */
81  s->ctx_flags |= AVFMTCTX_NOHEADER;
82 
84  if (!video)
85  return AVERROR(ENOMEM);
86 
87  video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
88  video->codecpar->codec_id = AV_CODEC_ID_C93;
89  video->codecpar->width = 320;
90  video->codecpar->height = 192;
91  /* 4:3 320x200 with 8 empty lines */
92  video->sample_aspect_ratio = (AVRational) { 5, 6 };
93  avpriv_set_pts_info(video, 64, 2, 25);
94  video->nb_frames = framecount;
95  video->duration = framecount;
96  video->start_time = 0;
97 
98  c93->current_block = 0;
99  c93->current_frame = 0;
100  c93->next_pkt_is_audio = 0;
101  return 0;
102 }
103 
104 #define C93_HAS_PALETTE 0x01
105 #define C93_FIRST_FRAME 0x02
106 
108 {
109  AVIOContext *pb = s->pb;
110  C93DemuxContext *c93 = s->priv_data;
111  C93BlockRecord *br = &c93->block_records[c93->current_block];
112  int datasize;
113  int ret, i;
114 
115  if (c93->next_pkt_is_audio) {
116  c93->current_frame++;
117  c93->next_pkt_is_audio = 0;
118  datasize = avio_rl16(pb);
119  if (datasize > 42) {
120  if (!c93->audio) {
121  c93->audio = avformat_new_stream(s, NULL);
122  if (!c93->audio)
123  return AVERROR(ENOMEM);
125  }
126  avio_skip(pb, 26); /* VOC header */
127  ret = ff_voc_get_packet(s, pkt, c93->audio, datasize - 26);
128  if (ret > 0) {
129  pkt->stream_index = 1;
131  return ret;
132  }
133  }
134  }
135  if (c93->current_frame >= br->frames) {
136  if (c93->current_block >= 511 || !br[1].length)
137  return AVERROR_EOF;
138  br++;
139  c93->current_block++;
140  c93->current_frame = 0;
141  }
142 
143  if (c93->current_frame == 0) {
144  avio_seek(pb, br->index * 2048, SEEK_SET);
145  for (i = 0; i < 32; i++) {
146  c93->frame_offsets[i] = avio_rl32(pb);
147  }
148  }
149 
150  avio_seek(pb,br->index * 2048 +
151  c93->frame_offsets[c93->current_frame], SEEK_SET);
152  datasize = avio_rl16(pb); /* video frame size */
153 
154  ret = av_new_packet(pkt, datasize + 768 + 1);
155  if (ret < 0)
156  return ret;
157  pkt->data[0] = 0;
158  pkt->size = datasize + 1;
159 
160  ret = avio_read(pb, pkt->data + 1, datasize);
161  if (ret < datasize) {
162  return AVERROR(EIO);
163  }
164 
165  datasize = avio_rl16(pb); /* palette size */
166  if (datasize) {
167  if (datasize != 768) {
168  av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize);
169  return AVERROR_INVALIDDATA;
170  }
171  pkt->data[0] |= C93_HAS_PALETTE;
172  ret = avio_read(pb, pkt->data + pkt->size, datasize);
173  if (ret < datasize) {
174  return AVERROR(EIO);
175  }
176  pkt->size += 768;
177  }
178  pkt->stream_index = 0;
179  c93->next_pkt_is_audio = 1;
180 
181  /* only the first frame is guaranteed to not reference previous frames */
182  if (c93->current_block == 0 && c93->current_frame == 0) {
184  pkt->data[0] |= C93_FIRST_FRAME;
185  }
186  return 0;
187 }
188 
190  .p.name = "c93",
191  .p.long_name = NULL_IF_CONFIG_SMALL("Interplay C93"),
192  .priv_data_size = sizeof(C93DemuxContext),
193  .read_probe = probe,
196 };
C93DemuxContext::next_pkt_is_audio
int next_pkt_is_audio
Definition: c93.c:42
AVERROR
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
C93BlockRecord::frames
uint8_t frames
Definition: c93.c:31
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: c93.c:107
AVPacket::data
uint8_t * data
Definition: packet.h:539
C93DemuxContext::voc
VocDecContext voc
Definition: c93.c:35
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
read_header
static int read_header(AVFormatContext *s)
Definition: c93.c:61
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:867
C93_FIRST_FRAME
#define C93_FIRST_FRAME
Definition: c93.c:105
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
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
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
C93BlockRecord::length
uint8_t length
Definition: c93.c:30
C93DemuxContext::block_records
C93BlockRecord block_records[512]
Definition: c93.c:37
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
C93DemuxContext::current_frame
int current_frame
Definition: c93.c:41
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
C93BlockRecord
Definition: c93.c:28
NULL
#define NULL
Definition: coverity.c:32
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1251
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
C93_HAS_PALETTE
#define C93_HAS_PALETTE
Definition: c93.c:104
index
int index
Definition: gxfenc.c:90
C93DemuxContext::current_block
int current_block
Definition: c93.c:38
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
C93DemuxContext::audio
AVStream * audio
Definition: c93.c:44
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
voc.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_voc_get_packet
int ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
Definition: voc_packet.c:27
ff_c93_demuxer
const FFInputFormat ff_c93_demuxer
Definition: c93.c:189
AV_CODEC_ID_C93
@ AV_CODEC_ID_C93
Definition: codec_id.h:154
demux.h
C93DemuxContext
Definition: c93.c:34
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
avformat.h
video
A Quick Description Of Rate Distortion Theory We want to encode a video
Definition: rate_distortion.txt:3
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
probe
static int probe(const AVProbeData *p)
Definition: c93.c:47
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
C93DemuxContext::frame_offsets
uint32_t frame_offsets[32]
Definition: c93.c:40
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FFInputFormat
Definition: demux.h:42
C93BlockRecord::index
uint16_t index
Definition: c93.c:29
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61