00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00071 #include "libavutil/intreadwrite.h"
00072 #include "avformat.h"
00073
00074 #define HUFFMAN_TABLE_SIZE (64 * 1024)
00075 #define IDCIN_FPS 14
00076
00077 typedef struct IdcinDemuxContext {
00078 int video_stream_index;
00079 int audio_stream_index;
00080 int audio_chunk_size1;
00081 int audio_chunk_size2;
00082
00083
00084 int current_audio_chunk;
00085 int next_chunk_is_video;
00086 int audio_present;
00087
00088 int64_t pts;
00089
00090 AVPaletteControl palctrl;
00091 } IdcinDemuxContext;
00092
00093 static int idcin_probe(AVProbeData *p)
00094 {
00095 unsigned int number;
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 if (p->buf_size < 20)
00111 return 0;
00112
00113
00114 number = AV_RL32(&p->buf[0]);
00115 if ((number == 0) || (number > 1024))
00116 return 0;
00117
00118
00119 number = AV_RL32(&p->buf[4]);
00120 if ((number == 0) || (number > 1024))
00121 return 0;
00122
00123
00124 number = AV_RL32(&p->buf[8]);
00125 if ((number != 0) && ((number < 8000) | (number > 48000)))
00126 return 0;
00127
00128
00129 number = AV_RL32(&p->buf[12]);
00130 if (number > 2)
00131 return 0;
00132
00133
00134 number = AV_RL32(&p->buf[16]);
00135 if (number > 2)
00136 return 0;
00137
00138
00139 return AVPROBE_SCORE_MAX / 2;
00140 }
00141
00142 static int idcin_read_header(AVFormatContext *s,
00143 AVFormatParameters *ap)
00144 {
00145 ByteIOContext *pb = s->pb;
00146 IdcinDemuxContext *idcin = s->priv_data;
00147 AVStream *st;
00148 unsigned int width, height;
00149 unsigned int sample_rate, bytes_per_sample, channels;
00150
00151
00152 width = get_le32(pb);
00153 height = get_le32(pb);
00154 sample_rate = get_le32(pb);
00155 bytes_per_sample = get_le32(pb);
00156 channels = get_le32(pb);
00157
00158 st = av_new_stream(s, 0);
00159 if (!st)
00160 return AVERROR(ENOMEM);
00161 av_set_pts_info(st, 33, 1, IDCIN_FPS);
00162 idcin->video_stream_index = st->index;
00163 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00164 st->codec->codec_id = CODEC_ID_IDCIN;
00165 st->codec->codec_tag = 0;
00166 st->codec->width = width;
00167 st->codec->height = height;
00168
00169
00170 st->codec->extradata_size = HUFFMAN_TABLE_SIZE;
00171 st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE);
00172 if (get_buffer(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE) !=
00173 HUFFMAN_TABLE_SIZE)
00174 return AVERROR(EIO);
00175
00176 st->codec->palctrl = &idcin->palctrl;
00177
00178
00179 if (sample_rate) {
00180 idcin->audio_present = 1;
00181 st = av_new_stream(s, 0);
00182 if (!st)
00183 return AVERROR(ENOMEM);
00184 av_set_pts_info(st, 33, 1, IDCIN_FPS);
00185 idcin->audio_stream_index = st->index;
00186 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00187 st->codec->codec_tag = 1;
00188 st->codec->channels = channels;
00189 st->codec->sample_rate = sample_rate;
00190 st->codec->bits_per_coded_sample = bytes_per_sample * 8;
00191 st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels;
00192 st->codec->block_align = bytes_per_sample * channels;
00193 if (bytes_per_sample == 1)
00194 st->codec->codec_id = CODEC_ID_PCM_U8;
00195 else
00196 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00197
00198 if (sample_rate % 14 != 0) {
00199 idcin->audio_chunk_size1 = (sample_rate / 14) *
00200 bytes_per_sample * channels;
00201 idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
00202 bytes_per_sample * channels;
00203 } else {
00204 idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
00205 (sample_rate / 14) * bytes_per_sample * channels;
00206 }
00207 idcin->current_audio_chunk = 0;
00208 } else
00209 idcin->audio_present = 1;
00210
00211 idcin->next_chunk_is_video = 1;
00212 idcin->pts = 0;
00213
00214 return 0;
00215 }
00216
00217 static int idcin_read_packet(AVFormatContext *s,
00218 AVPacket *pkt)
00219 {
00220 int ret;
00221 unsigned int command;
00222 unsigned int chunk_size;
00223 IdcinDemuxContext *idcin = s->priv_data;
00224 ByteIOContext *pb = s->pb;
00225 int i;
00226 int palette_scale;
00227 unsigned char r, g, b;
00228 unsigned char palette_buffer[768];
00229
00230 if (url_feof(s->pb))
00231 return AVERROR(EIO);
00232
00233 if (idcin->next_chunk_is_video) {
00234 command = get_le32(pb);
00235 if (command == 2) {
00236 return AVERROR(EIO);
00237 } else if (command == 1) {
00238
00239 idcin->palctrl.palette_changed = 1;
00240 if (get_buffer(pb, palette_buffer, 768) != 768)
00241 return AVERROR(EIO);
00242
00243 palette_scale = 2;
00244 for (i = 0; i < 768; i++)
00245 if (palette_buffer[i] > 63) {
00246 palette_scale = 0;
00247 break;
00248 }
00249
00250 for (i = 0; i < 256; i++) {
00251 r = palette_buffer[i * 3 ] << palette_scale;
00252 g = palette_buffer[i * 3 + 1] << palette_scale;
00253 b = palette_buffer[i * 3 + 2] << palette_scale;
00254 idcin->palctrl.palette[i] = (r << 16) | (g << 8) | (b);
00255 }
00256 }
00257
00258 chunk_size = get_le32(pb);
00259
00260 url_fseek(pb, 4, SEEK_CUR);
00261 chunk_size -= 4;
00262 ret= av_get_packet(pb, pkt, chunk_size);
00263 if (ret < 0)
00264 return ret;
00265 pkt->stream_index = idcin->video_stream_index;
00266 pkt->pts = idcin->pts;
00267 } else {
00268
00269 if (idcin->current_audio_chunk)
00270 chunk_size = idcin->audio_chunk_size2;
00271 else
00272 chunk_size = idcin->audio_chunk_size1;
00273 ret= av_get_packet(pb, pkt, chunk_size);
00274 if (ret < 0)
00275 return ret;
00276 pkt->stream_index = idcin->audio_stream_index;
00277 pkt->pts = idcin->pts;
00278
00279 idcin->current_audio_chunk ^= 1;
00280 idcin->pts++;
00281 }
00282
00283 if (idcin->audio_present)
00284 idcin->next_chunk_is_video ^= 1;
00285
00286 return ret;
00287 }
00288
00289 AVInputFormat idcin_demuxer = {
00290 "idcin",
00291 NULL_IF_CONFIG_SMALL("id Cinematic format"),
00292 sizeof(IdcinDemuxContext),
00293 idcin_probe,
00294 idcin_read_header,
00295 idcin_read_packet,
00296 };