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 
156  return 0;
157 }
158 
160 {
161  CinDemuxContext *cin = s->priv_data;
162  AVIOContext *pb = s->pb;
163  CinFrameHeader *hdr = &cin->frame_header;
164  int rc, palette_type, pkt_size;
165  int ret;
166 
167  if (cin->audio_buffer_size == 0) {
168  rc = cin_read_frame_header(cin, pb);
169  if (rc)
170  return rc;
171 
172  if ((int16_t)hdr->pal_colors_count < 0) {
173  hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
174  palette_type = 1;
175  } else {
176  palette_type = 0;
177  }
178 
179  /* palette and video packet */
180  pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
181 
182  ret = av_new_packet(pkt, 4 + pkt_size);
183  if (ret < 0)
184  return ret;
185 
186  pkt->stream_index = cin->video_stream_index;
187  pkt->pts = cin->video_stream_pts++;
188 
189  pkt->data[0] = palette_type;
190  pkt->data[1] = hdr->pal_colors_count & 0xFF;
191  pkt->data[2] = hdr->pal_colors_count >> 8;
192  pkt->data[3] = hdr->video_frame_type;
193 
194  ret = avio_read(pb, &pkt->data[4], pkt_size);
195  if (ret < 0) {
196  av_free_packet(pkt);
197  return ret;
198  }
199  if (ret < pkt_size)
200  av_shrink_packet(pkt, 4 + ret);
201 
202  /* sound buffer will be processed on next read_packet() call */
204  return 0;
205  }
206 
207  /* audio packet */
208  ret = av_get_packet(pb, pkt, cin->audio_buffer_size);
209  if (ret < 0)
210  return ret;
211 
212  pkt->stream_index = cin->audio_stream_index;
213  pkt->pts = cin->audio_stream_pts;
214  pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
215  cin->audio_stream_pts += pkt->duration;
216  cin->audio_buffer_size = 0;
217  return 0;
218 }
219 
221  .name = "dsicin",
222  .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN format"),
223  .priv_data_size = sizeof(CinDemuxContext),
227 };