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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "internal.h"
24 #include "voc.h"
25 #include "libavutil/intreadwrite.h"
26 
27 typedef struct {
28  uint16_t index;
29  uint8_t length;
30  uint8_t frames;
32 
33 typedef struct {
35 
36  C93BlockRecord block_records[512];
38 
39  uint32_t frame_offsets[32];
42 
45 
46 static int probe(AVProbeData *p)
47 {
48  int i;
49  int index = 1;
50  if (p->buf_size < 16)
51  return 0;
52  for (i = 0; i < 16; i += 4) {
53  if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3])
54  return 0;
55  index += p->buf[i + 2];
56  }
57  return AVPROBE_SCORE_MAX;
58 }
59 
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 */
82 
83  video = avformat_new_stream(s, NULL);
84  if (!video)
85  return AVERROR(ENOMEM);
86 
88  video->codec->codec_id = CODEC_ID_C93;
89  video->codec->width = 320;
90  video->codec->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 = voc_get_packet(s, pkt, c93->audio, datasize - 26);
128  if (ret > 0) {
129  pkt->stream_index = 1;
130  pkt->flags |= AV_PKT_FLAG_KEY;
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(EIO);
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  ret = AVERROR(EIO);
163  goto fail;
164  }
165 
166  datasize = avio_rl16(pb); /* palette size */
167  if (datasize) {
168  if (datasize != 768) {
169  av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize);
170  ret = AVERROR_INVALIDDATA;
171  goto fail;
172  }
173  pkt->data[0] |= C93_HAS_PALETTE;
174  ret = avio_read(pb, pkt->data + pkt->size, datasize);
175  if (ret < datasize) {
176  ret = AVERROR(EIO);
177  goto fail;
178  }
179  pkt->size += 768;
180  }
181  pkt->stream_index = 0;
182  c93->next_pkt_is_audio = 1;
183 
184  /* only the first frame is guaranteed to not reference previous frames */
185  if (c93->current_block == 0 && c93->current_frame == 0) {
186  pkt->flags |= AV_PKT_FLAG_KEY;
187  pkt->data[0] |= C93_FIRST_FRAME;
188  }
189  return 0;
190 
191  fail:
192  av_free_packet(pkt);
193  return ret;
194 }
195 
197  .name = "c93",
198  .long_name = NULL_IF_CONFIG_SMALL("Interplay C93"),
199  .priv_data_size = sizeof(C93DemuxContext),
200  .read_probe = probe,
203 };
uint32_t frame_offsets[32]
Definition: c93.c:39
static int probe(AVProbeData *p)
Definition: c93.c:46
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
uint8_t frames
Definition: c93.c:30
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:716
int size
Definition: avcodec.h:909
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: c93.c:107
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:211
VocDecContext voc
Definition: c93.c:34
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:919
Format I/O context.
Definition: avformat.h:863
int next_pkt_is_audio
Definition: c93.c:41
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:844
int current_frame
Definition: c93.c:40
uint8_t * data
Definition: avcodec.h:908
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:151
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
#define C93_HAS_PALETTE
Definition: c93.c:104
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#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:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
int voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
Definition: vocdec.c:65
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVCodecContext * codec
codec context
Definition: avformat.h:623
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:342
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
struct AVRational AVRational
rational number numerator/denominator
int width
picture width / height.
Definition: avcodec.h:1408
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
static int read_probe(AVProbeData *p)
Definition: img2.c:185
int current_block
Definition: c93.c:37
C93BlockRecord block_records[512]
Definition: c93.c:36
Stream structure.
Definition: avformat.h:620
AVStream * audio
Definition: c93.c:43
NULL
Definition: eval.c:50
uint16_t index
Definition: c93.c:28
enum AVMediaType codec_type
Definition: avcodec.h:1574
AVIOContext * pb
Definition: avformat.h:896
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
int index
Definition: gxfenc.c:73
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
static int read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: c93.c:60
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:684
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
Main libavformat public API header.
AV_WL32 AV_WL24 AV_RL16
Definition: bytestream.h:89
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:677
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:701
AVInputFormat ff_c93_demuxer
Definition: c93.c:196
void * priv_data
Format private data.
Definition: avformat.h:883
#define C93_FIRST_FRAME
Definition: c93.c:105
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
int stream_index
Definition: avcodec.h:910
uint8_t length
Definition: c93.c:29
enum CodecID codec_id
Definition: avcodec.h:1575