FFmpeg
dsicin.c
Go to the documentation of this file.
1 /*
2  * Delphine Software International CIN File Demuxer
3  * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
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 /**
23  * @file
24  * Delphine Software International CIN file demuxer
25  */
26 
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 #include "internal.h"
31 #include "avio_internal.h"
32 
33 
34 typedef struct CinFileHeader {
43 
44 typedef struct CinFrameHeader {
51 
52 typedef struct CinDemuxContext {
61 
62 
63 static int cin_probe(const AVProbeData *p)
64 {
65  /* header starts with this special marker */
66  if (AV_RL32(&p->buf[0]) != 0x55AA0000)
67  return 0;
68 
69  /* for accuracy, check some header field values */
70  if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
71  return 0;
72 
73  return AVPROBE_SCORE_MAX;
74 }
75 
77  CinFileHeader *hdr = &cin->file_header;
78 
79  if (avio_rl32(pb) != 0x55AA0000)
80  return AVERROR_INVALIDDATA;
81 
82  hdr->video_frame_size = avio_rl32(pb);
83  hdr->video_frame_width = avio_rl16(pb);
84  hdr->video_frame_height = avio_rl16(pb);
85  hdr->audio_frequency = avio_rl32(pb);
86  hdr->audio_bits = avio_r8(pb);
87  hdr->audio_stereo = avio_r8(pb);
88  hdr->audio_frame_size = avio_rl16(pb);
89 
90  if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0)
91  return AVERROR_INVALIDDATA;
92 
93  return 0;
94 }
95 
97 {
98  int rc;
99  CinDemuxContext *cin = s->priv_data;
100  CinFileHeader *hdr = &cin->file_header;
101  AVIOContext *pb = s->pb;
102  AVStream *st;
103 
104  rc = cin_read_file_header(cin, pb);
105  if (rc)
106  return rc;
107 
108  cin->video_stream_pts = 0;
109  cin->audio_stream_pts = 0;
110  cin->audio_buffer_size = 0;
111 
112  /* initialize the video decoder stream */
113  st = avformat_new_stream(s, NULL);
114  if (!st)
115  return AVERROR(ENOMEM);
116 
117  avpriv_set_pts_info(st, 32, 1, 12);
118  cin->video_stream_index = st->index;
121  st->codecpar->codec_tag = 0; /* no fourcc */
122  st->codecpar->width = hdr->video_frame_width;
123  st->codecpar->height = hdr->video_frame_height;
124 
125  /* initialize the audio decoder stream */
126  st = avformat_new_stream(s, NULL);
127  if (!st)
128  return AVERROR(ENOMEM);
129 
130  avpriv_set_pts_info(st, 32, 1, 22050);
131  cin->audio_stream_index = st->index;
134  st->codecpar->codec_tag = 0; /* no tag */
136  st->codecpar->sample_rate = 22050;
138  st->codecpar->bit_rate = st->codecpar->sample_rate *
141 
142  return 0;
143 }
144 
146  CinFrameHeader *hdr = &cin->frame_header;
147 
148  hdr->video_frame_type = avio_r8(pb);
149  hdr->audio_frame_type = avio_r8(pb);
150  hdr->pal_colors_count = avio_rl16(pb);
151  hdr->video_frame_size = avio_rl32(pb);
152  hdr->audio_frame_size = avio_rl32(pb);
153 
154  if (avio_feof(pb) || pb->error)
155  return AVERROR(EIO);
156 
157  if (avio_rl32(pb) != 0xAA55AA55)
158  return AVERROR_INVALIDDATA;
159  if (hdr->video_frame_size < 0 || hdr->audio_frame_size < 0)
160  return AVERROR_INVALIDDATA;
161 
162  return 0;
163 }
164 
166 {
167  CinDemuxContext *cin = s->priv_data;
168  AVIOContext *pb = s->pb;
169  CinFrameHeader *hdr = &cin->frame_header;
170  int rc, palette_type;
171  int64_t pkt_size;
172  int ret;
173 
174  if (cin->audio_buffer_size == 0) {
175  rc = cin_read_frame_header(cin, pb);
176  if (rc)
177  return rc;
178 
179  if ((int16_t)hdr->pal_colors_count < 0) {
180  hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
181  palette_type = 1;
182  } else {
183  palette_type = 0;
184  }
185 
186  /* palette and video packet */
187  pkt_size = (palette_type + 3LL) * hdr->pal_colors_count + hdr->video_frame_size;
188  if (pkt_size + 4 > INT_MAX)
189  return AVERROR_INVALIDDATA;
190 
191  pkt_size = ffio_limit(pb, pkt_size);
192 
193  ret = av_new_packet(pkt, 4 + pkt_size);
194  if (ret < 0)
195  return ret;
196 
198  pkt->pts = cin->video_stream_pts++;
199 
200  pkt->data[0] = palette_type;
201  pkt->data[1] = hdr->pal_colors_count & 0xFF;
202  pkt->data[2] = hdr->pal_colors_count >> 8;
203  pkt->data[3] = hdr->video_frame_type;
204 
205  ret = avio_read(pb, &pkt->data[4], pkt_size);
206  if (ret < 0) {
207  return ret;
208  }
209  if (ret < pkt_size)
210  av_shrink_packet(pkt, 4 + ret);
211 
212  /* sound buffer will be processed on next read_packet() call */
214  return 0;
215  }
216 
217  /* audio packet */
218  ret = av_get_packet(pb, pkt, cin->audio_buffer_size);
219  if (ret < 0)
220  return ret;
221 
223  pkt->pts = cin->audio_stream_pts;
224  pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
225  cin->audio_stream_pts += pkt->duration;
226  cin->audio_buffer_size = 0;
227  return 0;
228 }
229 
231  .name = "dsicin",
232  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN"),
233  .priv_data_size = sizeof(CinDemuxContext),
237 };
CinDemuxContext::audio_buffer_size
int audio_buffer_size
Definition: dsicin.c:59
CinFrameHeader::audio_frame_size
int audio_frame_size
Definition: dsicin.c:48
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:237
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:57
CinFrameHeader
Definition: dsicin.c:44
CinFrameHeader::audio_frame_type
int audio_frame_type
Definition: dsicin.c:45
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVIOContext::error
int error
contains the error code or 0 if no error happened
Definition: avio.h:241
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:353
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:65
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
CinFileHeader::audio_bits
int audio_bits
Definition: dsicin.c:39
AV_CODEC_ID_DSICINVIDEO
@ AV_CODEC_ID_DSICINVIDEO
Definition: codec_id.h:144
CinFileHeader::video_frame_width
int video_frame_width
Definition: dsicin.c:36
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:465
CinFileHeader::audio_frame_size
int audio_frame_size
Definition: dsicin.c:41
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:697
CinFrameHeader::video_frame_size
int video_frame_size
Definition: dsicin.c:49
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:112
CinDemuxContext::video_stream_index
int video_stream_index
Definition: dsicin.c:54
CinDemuxContext::frame_header
CinFrameHeader frame_header
Definition: dsicin.c:58
cin_read_packet
static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dsicin.c:165
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:656
CinFrameHeader::pal_colors_count
int pal_colors_count
Definition: dsicin.c:47
AV_CODEC_ID_DSICINAUDIO
@ AV_CODEC_ID_DSICINAUDIO
Definition: codec_id.h:455
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
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: avpacket.c:97
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
CinFrameHeader::video_frame_type
int video_frame_type
Definition: dsicin.c:46
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:455
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:127
if
if(ret)
Definition: filter_design.txt:179
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:532
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
CinFileHeader::audio_frequency
int audio_frequency
Definition: dsicin.c:38
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:453
CinDemuxContext::file_header
CinFileHeader file_header
Definition: dsicin.c:55
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:212
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:177
CinFileHeader::video_frame_height
int video_frame_height
Definition: dsicin.c:37
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
CinDemuxContext::video_stream_pts
int64_t video_stream_pts
Definition: dsicin.c:57
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:117
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:290
CinFileHeader::audio_stereo
int audio_stereo
Definition: dsicin.c:40
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
ff_dsicin_demuxer
const AVInputFormat ff_dsicin_demuxer
Definition: dsicin.c:230
cin_probe
static int cin_probe(const AVProbeData *p)
Definition: dsicin.c:63
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: aviobuf.c:1093
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:128
CinDemuxContext::audio_stream_pts
int64_t audio_stream_pts
Definition: dsicin.c:56
CinDemuxContext::audio_stream_index
int audio_stream_index
Definition: dsicin.c:53
av_get_packet
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:102
cin_read_header
static int cin_read_header(AVFormatContext *s)
Definition: dsicin.c:96
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:948
cin_read_frame_header
static int cin_read_frame_header(CinDemuxContext *cin, AVIOContext *pb)
Definition: dsicin.c:145
avformat.h
CinFileHeader
Definition: dsicin.c:34
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:956
channel_layout.h
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:641
cin_read_file_header
static int cin_read_file_header(CinDemuxContext *cin, AVIOContext *pb)
Definition: dsicin.c:76
AVPacket::stream_index
int stream_index
Definition: packet.h:376
CinDemuxContext
Definition: dsicin.c:52
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:103
CinFileHeader::video_frame_size
int video_frame_size
Definition: dsicin.c:35
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:90
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:375