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 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 
27 #include "libavutil/intreadwrite.h"
28 #include "avformat.h"
29 #include "internal.h"
30 
31 
32 typedef struct CinFileHeader {
41 
42 typedef struct CinFrameHeader {
49 
50 typedef struct CinDemuxContext {
59 
60 
61 static int cin_probe(AVProbeData *p)
62 {
63  /* header starts with this special marker */
64  if (AV_RL32(&p->buf[0]) != 0x55AA0000)
65  return 0;
66 
67  /* for accuracy, check some header field values */
68  if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
69  return 0;
70 
71  return AVPROBE_SCORE_MAX;
72 }
73 
75  CinFileHeader *hdr = &cin->file_header;
76 
77  if (avio_rl32(pb) != 0x55AA0000)
78  return AVERROR_INVALIDDATA;
79 
80  hdr->video_frame_size = avio_rl32(pb);
81  hdr->video_frame_width = avio_rl16(pb);
82  hdr->video_frame_height = avio_rl16(pb);
83  hdr->audio_frequency = avio_rl32(pb);
84  hdr->audio_bits = avio_r8(pb);
85  hdr->audio_stereo = avio_r8(pb);
86  hdr->audio_frame_size = avio_rl16(pb);
87 
88  if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0)
89  return AVERROR_INVALIDDATA;
90 
91  return 0;
92 }
93 
95 {
96  int rc;
97  CinDemuxContext *cin = s->priv_data;
98  CinFileHeader *hdr = &cin->file_header;
99  AVIOContext *pb = s->pb;
100  AVStream *st;
101 
102  rc = cin_read_file_header(cin, pb);
103  if (rc)
104  return rc;
105 
106  cin->video_stream_pts = 0;
107  cin->audio_stream_pts = 0;
108  cin->audio_buffer_size = 0;
109 
110  /* initialize the video decoder stream */
111  st = avformat_new_stream(s, NULL);
112  if (!st)
113  return AVERROR(ENOMEM);
114 
115  avpriv_set_pts_info(st, 32, 1, 12);
116  cin->video_stream_index = st->index;
119  st->codec->codec_tag = 0; /* no fourcc */
120  st->codec->width = hdr->video_frame_width;
121  st->codec->height = hdr->video_frame_height;
122 
123  /* initialize the audio decoder stream */
124  st = avformat_new_stream(s, NULL);
125  if (!st)
126  return AVERROR(ENOMEM);
127 
128  avpriv_set_pts_info(st, 32, 1, 22050);
129  cin->audio_stream_index = st->index;
132  st->codec->codec_tag = 0; /* no tag */
133  st->codec->channels = 1;
134  st->codec->sample_rate = 22050;
135  st->codec->bits_per_coded_sample = 8;
137 
138  return 0;
139 }
140 
142  CinFrameHeader *hdr = &cin->frame_header;
143 
144  hdr->video_frame_type = avio_r8(pb);
145  hdr->audio_frame_type = avio_r8(pb);
146  hdr->pal_colors_count = avio_rl16(pb);
147  hdr->video_frame_size = avio_rl32(pb);
148  hdr->audio_frame_size = avio_rl32(pb);
149 
150  if (pb->eof_reached || pb->error)
151  return AVERROR(EIO);
152 
153  if (avio_rl32(pb) != 0xAA55AA55)
154  return AVERROR_INVALIDDATA;
155  if (hdr->video_frame_size < 0 || hdr->audio_frame_size < 0)
156  return AVERROR_INVALIDDATA;
157 
158  return 0;
159 }
160 
162 {
163  CinDemuxContext *cin = s->priv_data;
164  AVIOContext *pb = s->pb;
165  CinFrameHeader *hdr = &cin->frame_header;
166  int rc, palette_type, pkt_size;
167  int ret;
168 
169  if (cin->audio_buffer_size == 0) {
170  rc = cin_read_frame_header(cin, pb);
171  if (rc)
172  return rc;
173 
174  if ((int16_t)hdr->pal_colors_count < 0) {
175  hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
176  palette_type = 1;
177  } else {
178  palette_type = 0;
179  }
180 
181  /* palette and video packet */
182  pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
183 
184  ret = av_new_packet(pkt, 4 + pkt_size);
185  if (ret < 0)
186  return ret;
187 
188  pkt->stream_index = cin->video_stream_index;
189  pkt->pts = cin->video_stream_pts++;
190 
191  pkt->data[0] = palette_type;
192  pkt->data[1] = hdr->pal_colors_count & 0xFF;
193  pkt->data[2] = hdr->pal_colors_count >> 8;
194  pkt->data[3] = hdr->video_frame_type;
195 
196  ret = avio_read(pb, &pkt->data[4], pkt_size);
197  if (ret < 0) {
198  av_free_packet(pkt);
199  return ret;
200  }
201  if (ret < pkt_size)
202  av_shrink_packet(pkt, 4 + ret);
203 
204  /* sound buffer will be processed on next read_packet() call */
206  return 0;
207  }
208 
209  /* audio packet */
210  ret = av_get_packet(pb, pkt, cin->audio_buffer_size);
211  if (ret < 0)
212  return ret;
213 
214  pkt->stream_index = cin->audio_stream_index;
215  pkt->pts = cin->audio_stream_pts;
216  pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
217  cin->audio_stream_pts += pkt->duration;
218  cin->audio_buffer_size = 0;
219  return 0;
220 }
221 
223  .name = "dsicin",
224  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN format"),
225  .priv_data_size = sizeof(CinDemuxContext),
229 };
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
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
int video_stream_index
Definition: dsicin.c:52
int index
stream index in AVFormatContext
Definition: avformat.h:621
static int cin_read_frame_header(CinDemuxContext *cin, AVIOContext *pb)
Definition: dsicin.c:141
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:79
Format I/O context.
Definition: avformat.h:863
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
uint8_t * data
Definition: avcodec.h:908
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:151
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:269
static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dsicin.c:161
struct CinFileHeader CinFileHeader
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:930
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
int video_frame_size
Definition: dsicin.c:47
struct CinFrameHeader CinFrameHeader
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#define AVERROR(e)
Definition: error.h:43
int audio_frequency
Definition: dsicin.c:36
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
AV_RL32
Definition: bytestream.h:89
CinFrameHeader frame_header
Definition: dsicin.c:56
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
int video_frame_width
Definition: dsicin.c:34
CinFileHeader file_header
Definition: dsicin.c:53
struct CinDemuxContext CinDemuxContext
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVCodecContext * codec
codec context
Definition: avformat.h:623
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
int64_t video_stream_pts
Definition: dsicin.c:55
int bit_rate
the average bitrate
Definition: avcodec.h:1340
int audio_frame_size
Definition: dsicin.c:46
int width
picture width / height.
Definition: avcodec.h:1408
static int read_probe(AVProbeData *p)
Definition: img2.c:185
int audio_stream_index
Definition: dsicin.c:51
AVInputFormat ff_dsicin_demuxer
Definition: dsicin.c:222
int pal_colors_count
Definition: dsicin.c:45
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
static int cin_read_file_header(CinDemuxContext *cin, AVIOContext *pb)
Definition: dsicin.c:74
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
int video_frame_height
Definition: dsicin.c:35
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
int error
contains the error code or 0 if no error happened
Definition: avio.h:107
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
#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.
int64_t audio_stream_pts
Definition: dsicin.c:54
int audio_bits
Definition: dsicin.c:37
int video_frame_type
Definition: dsicin.c:44
int video_frame_size
Definition: dsicin.c:33
int eof_reached
true if eof reached
Definition: avio.h:98
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
int audio_frame_size
Definition: dsicin.c:39
int audio_stereo
Definition: dsicin.c:38
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
int audio_buffer_size
Definition: dsicin.c:57
static int cin_probe(AVProbeData *p)
Definition: dsicin.c:61
int stream_index
Definition: avcodec.h:910
static int cin_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: dsicin.c:94
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
int audio_frame_type
Definition: dsicin.c:43
enum CodecID codec_id
Definition: avcodec.h:1575