FFmpeg
Loading...
Searching...
No Matches
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 "avio_internal.h"
24#include "demux.h"
25#include "internal.h"
26#include "voc.h"
28
29typedef struct C93BlockRecord {
30 uint16_t index;
31 uint8_t length;
32 uint8_t frames;
34
47
48static int probe(const AVProbeData *p)
49{
50 int i;
51 int index = 1;
52 if (p->buf_size < 16)
53 return 0;
54 for (i = 0; i < 16; i += 4) {
55 if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3])
56 return 0;
57 index += p->buf[i + 2];
58 }
59 return AVPROBE_SCORE_MAX;
60}
61
63{
64 AVStream *video;
65 AVIOContext *pb = s->pb;
66 C93DemuxContext *c93 = s->priv_data;
67 int i;
68 int framecount = 0;
69
70 for (i = 0; i < 512; i++) {
71 c93->block_records[i].index = avio_rl16(pb);
72 c93->block_records[i].length = avio_r8(pb);
73 c93->block_records[i].frames = avio_r8(pb);
74 if (c93->block_records[i].frames > 32) {
75 av_log(s, AV_LOG_ERROR, "too many frames in block\n");
77 }
78 framecount += c93->block_records[i].frames;
79 }
80
81 /* Audio streams are added if audio packets are found */
82 s->ctx_flags |= AVFMTCTX_NOHEADER;
83
84 video = avformat_new_stream(s, NULL);
85 if (!video)
86 return AVERROR(ENOMEM);
87
90 video->codecpar->width = 320;
91 video->codecpar->height = 192;
92 /* 4:3 320x200 with 8 empty lines */
93 video->sample_aspect_ratio = (AVRational) { 5, 6 };
94 avpriv_set_pts_info(video, 64, 2, 25);
95 video->nb_frames = framecount;
96 video->duration = framecount;
97 video->start_time = 0;
98
99 c93->current_block = 0;
100 c93->current_frame = 0;
101 c93->next_pkt_is_audio = 0;
102 return 0;
103}
104
105#define C93_HAS_PALETTE 0x01
106#define C93_FIRST_FRAME 0x02
107
109{
110 AVIOContext *pb = s->pb;
111 C93DemuxContext *c93 = s->priv_data;
113 int datasize;
114 int ret, i;
115
116 if (c93->next_pkt_is_audio) {
117 c93->current_frame++;
118 c93->next_pkt_is_audio = 0;
119 datasize = avio_rl16(pb);
120 if (datasize > 42) {
121 if (!c93->audio) {
123 if (!c93->audio)
124 return AVERROR(ENOMEM);
126 }
127 avio_skip(pb, 26); /* VOC header */
128 ret = ff_voc_get_packet(s, pkt, c93->audio, datasize - 26);
129 if (ret > 0) {
130 pkt->stream_index = 1;
131 pkt->flags |= AV_PKT_FLAG_KEY;
132 return ret;
133 }
134 }
135 }
136 if (c93->current_frame >= br->frames) {
137 if (c93->current_block >= 511 || !br[1].length)
138 return AVERROR_EOF;
139 br++;
140 c93->current_block++;
141 c93->current_frame = 0;
142 }
143
144 if (c93->current_frame == 0) {
145 avio_seek(pb, br->index * 2048, SEEK_SET);
146 for (i = 0; i < 32; i++) {
147 c93->frame_offsets[i] = avio_rl32(pb);
148 }
149 }
150
151 avio_seek(pb,br->index * 2048 +
152 c93->frame_offsets[c93->current_frame], SEEK_SET);
153 datasize = avio_rl16(pb); /* video frame size */
154
155 ret = av_new_packet(pkt, datasize + 768 + 1);
156 if (ret < 0)
157 return ret;
158 pkt->data[0] = 0;
159 pkt->size = datasize + 1;
160
161 ret = ffio_read_size(pb, pkt->data + 1, datasize);
162 if (ret < 0) {
163 return ret;
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 return AVERROR_INVALIDDATA;
171 }
172 pkt->data[0] |= C93_HAS_PALETTE;
173 ret = ffio_read_size(pb, pkt->data + pkt->size, datasize);
174 if (ret < 0) {
175 return ret;
176 }
177 pkt->size += 768;
178 }
179 pkt->stream_index = 0;
180 c93->next_pkt_is_audio = 1;
181
182 /* only the first frame is guaranteed to not reference previous frames */
183 if (c93->current_block == 0 && c93->current_frame == 0) {
184 pkt->flags |= AV_PKT_FLAG_KEY;
185 pkt->data[0] |= C93_FIRST_FRAME;
186 }
187 return 0;
188}
189
191 .p.name = "c93",
192 .p.long_name = NULL_IF_CONFIG_SMALL("Interplay C93"),
193 .priv_data_size = sizeof(C93DemuxContext),
194 .read_probe = probe,
197};
static int probe(const AVProbeData *p)
Definition act.c:39
const FFInputFormat ff_c93_demuxer
Definition c93.c:190
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
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#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
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_C93
Definition codec_id.h:152
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
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_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int index
Definition gxfenc.c:90
#define AV_RL16(p)
#define C93_FIRST_FRAME
Definition c93.c:49
#define C93_HAS_PALETTE
Definition c93.c:48
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition c93.c:108
static int read_header(AVFormatContext *s)
Definition c93.c:62
static int probe(const AVProbeData *p)
Definition c93.c:48
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
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
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
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
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:844
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
uint8_t length
Definition c93.c:31
uint16_t index
Definition c93.c:30
uint8_t frames
Definition c93.c:32
uint32_t frame_offsets[32]
Definition c93.c:41
AVStream * audio
Definition c93.c:45
C93BlockRecord block_records[512]
Definition c93.c:38
VocDecContext voc
Definition c93.c:36
int next_pkt_is_audio
Definition c93.c:43
int current_frame
Definition c93.c:42
int current_block
Definition c93.c:39
#define av_log(a,...)
int ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
Definition voc_packet.c:27