FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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(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->codec->codec_tag = 0; /* no fourcc */
122  st->codec->width = hdr->video_frame_width;
123  st->codec->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->codec->codec_tag = 0; /* no tag */
135  st->codec->channels = 1;
137  st->codec->sample_rate = 22050;
138  st->codec->bits_per_coded_sample = 8;
140 
141  return 0;
142 }
143 
145  CinFrameHeader *hdr = &cin->frame_header;
146 
147  hdr->video_frame_type = avio_r8(pb);
148  hdr->audio_frame_type = avio_r8(pb);
149  hdr->pal_colors_count = avio_rl16(pb);
150  hdr->video_frame_size = avio_rl32(pb);
151  hdr->audio_frame_size = avio_rl32(pb);
152 
153  if (avio_feof(pb) || pb->error)
154  return AVERROR(EIO);
155 
156  if (avio_rl32(pb) != 0xAA55AA55)
157  return AVERROR_INVALIDDATA;
158  if (hdr->video_frame_size < 0 || hdr->audio_frame_size < 0)
159  return AVERROR_INVALIDDATA;
160 
161  return 0;
162 }
163 
165 {
166  CinDemuxContext *cin = s->priv_data;
167  AVIOContext *pb = s->pb;
168  CinFrameHeader *hdr = &cin->frame_header;
169  int rc, palette_type, pkt_size;
170  int ret;
171 
172  if (cin->audio_buffer_size == 0) {
173  rc = cin_read_frame_header(cin, pb);
174  if (rc)
175  return rc;
176 
177  if ((int16_t)hdr->pal_colors_count < 0) {
178  hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
179  palette_type = 1;
180  } else {
181  palette_type = 0;
182  }
183 
184  /* palette and video packet */
185  pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
186 
187  pkt_size = ffio_limit(pb, pkt_size);
188 
189  ret = av_new_packet(pkt, 4 + pkt_size);
190  if (ret < 0)
191  return ret;
192 
193  pkt->stream_index = cin->video_stream_index;
194  pkt->pts = cin->video_stream_pts++;
195 
196  pkt->data[0] = palette_type;
197  pkt->data[1] = hdr->pal_colors_count & 0xFF;
198  pkt->data[2] = hdr->pal_colors_count >> 8;
199  pkt->data[3] = hdr->video_frame_type;
200 
201  ret = avio_read(pb, &pkt->data[4], pkt_size);
202  if (ret < 0) {
203  av_free_packet(pkt);
204  return ret;
205  }
206  if (ret < pkt_size)
207  av_shrink_packet(pkt, 4 + ret);
208 
209  /* sound buffer will be processed on next read_packet() call */
211  return 0;
212  }
213 
214  /* audio packet */
215  ret = av_get_packet(pb, pkt, cin->audio_buffer_size);
216  if (ret < 0)
217  return ret;
218 
219  pkt->stream_index = cin->audio_stream_index;
220  pkt->pts = cin->audio_stream_pts;
221  pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
222  cin->audio_stream_pts += pkt->duration;
223  cin->audio_buffer_size = 0;
224  return 0;
225 }
226 
228  .name = "dsicin",
229  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN"),
230  .priv_data_size = sizeof(CinDemuxContext),
234 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:103
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:4083
int video_stream_index
Definition: dsicin.c:54
int index
stream index in AVFormatContext
Definition: avformat.h:843
static int cin_read_frame_header(CinDemuxContext *cin, AVIOContext *pb)
Definition: dsicin.c:144
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1273
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:181
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
uint8_t * data
Definition: avcodec.h:1423
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:244
static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dsicin.c:164
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2996
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1441
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:538
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
int video_frame_size
Definition: dsicin.c:49
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:659
#define AVERROR(e)
Definition: error.h:43
int audio_frequency
Definition: dsicin.c:38
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
CinFrameHeader frame_header
Definition: dsicin.c:58
int video_frame_width
Definition: dsicin.c:36
CinFileHeader file_header
Definition: dsicin.c:55
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2323
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:529
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
int64_t video_stream_pts
Definition: dsicin.c:57
int bit_rate
the average bitrate
Definition: avcodec.h:1567
audio channel layout utility functions
int audio_frame_size
Definition: dsicin.c:48
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1681
int audio_stream_index
Definition: dsicin.c:53
AVInputFormat ff_dsicin_demuxer
Definition: dsicin.c:227
int pal_colors_count
Definition: dsicin.c:47
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:623
Stream structure.
Definition: avformat.h:842
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
static int cin_read_file_header(CinDemuxContext *cin, AVIOContext *pb)
Definition: dsicin.c:76
int sample_rate
samples per second
Definition: avcodec.h:2262
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
int video_frame_height
Definition: dsicin.c:37
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1534
int error
contains the error code or 0 if no error happened
Definition: avio.h:145
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
static int cin_read_header(AVFormatContext *s)
Definition: dsicin.c:96
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:643
Main libavformat public API header.
int64_t audio_stream_pts
Definition: dsicin.c:56
int audio_bits
Definition: dsicin.c:39
int video_frame_type
Definition: dsicin.c:46
int video_frame_size
Definition: dsicin.c:35
int channels
number of audio channels
Definition: avcodec.h:2263
void * priv_data
Format private data.
Definition: avformat.h:1301
int audio_frame_size
Definition: dsicin.c:41
int audio_stereo
Definition: dsicin.c:40
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int audio_buffer_size
Definition: dsicin.c:59
static int cin_probe(AVProbeData *p)
Definition: dsicin.c:63
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:301
int stream_index
Definition: avcodec.h:1425
#define AV_CH_LAYOUT_MONO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1400
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
int audio_frame_type
Definition: dsicin.c:45